-
Notifications
You must be signed in to change notification settings - Fork 71
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add tests checking if key attributes are respected #135
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
// Copyright (c) 2020, Arm Limited, All Rights Reserved | ||
// SPDX-License-Identifier: Apache-2.0 | ||
use parsec_client_test::TestClient; | ||
use parsec_interface::operations::psa_algorithm::{Algorithm, AsymmetricSignature, Cipher, Hash}; | ||
use parsec_interface::operations::psa_key_attributes::{ | ||
KeyAttributes, KeyPolicy, KeyType, UsageFlags, | ||
}; | ||
use parsec_interface::requests::ResponseStatus; | ||
|
||
#[ignore] | ||
#[test] | ||
fn wrong_type() { | ||
let mut client = TestClient::new(); | ||
let key_name = String::from("wrong_type"); | ||
|
||
// Wrong key type | ||
let key_type = KeyType::Derive; | ||
let permitted_algorithm = | ||
Algorithm::AsymmetricSignature(AsymmetricSignature::RsaPkcs1v15Sign { | ||
hash_alg: Hash::Sha256, | ||
}); | ||
let key_attributes = KeyAttributes { | ||
key_type, | ||
key_bits: 1024, | ||
key_policy: KeyPolicy { | ||
key_usage_flags: UsageFlags { | ||
sign_hash: true, | ||
verify_hash: false, | ||
sign_message: false, | ||
verify_message: false, | ||
export: false, | ||
encrypt: false, | ||
decrypt: false, | ||
cache: false, | ||
copy: false, | ||
derive: false, | ||
}, | ||
key_algorithm: permitted_algorithm, | ||
}, | ||
}; | ||
|
||
client | ||
.generate_key(key_name.clone(), key_attributes) | ||
.unwrap(); | ||
let status = client | ||
.sign_with_rsa_sha256(key_name, vec![0xDE, 0xAD, 0xBE, 0xEF]) | ||
.unwrap_err(); | ||
|
||
assert_eq!(status, ResponseStatus::PsaErrorNotPermitted); | ||
} | ||
|
||
#[ignore] | ||
#[test] | ||
fn wrong_usage_flags() { | ||
let mut client = TestClient::new(); | ||
let key_name = String::from("wrong_usage_flags"); | ||
|
||
let key_type = KeyType::RsaKeyPair; | ||
let permitted_algorithm = | ||
Algorithm::AsymmetricSignature(AsymmetricSignature::RsaPkcs1v15Sign { | ||
hash_alg: Hash::Sha256, | ||
}); | ||
let key_attributes = KeyAttributes { | ||
key_type, | ||
key_bits: 1024, | ||
key_policy: KeyPolicy { | ||
key_usage_flags: UsageFlags { | ||
// Forbid signing | ||
sign_hash: false, | ||
verify_hash: false, | ||
sign_message: false, | ||
verify_message: false, | ||
export: false, | ||
encrypt: false, | ||
decrypt: false, | ||
cache: false, | ||
copy: false, | ||
derive: false, | ||
}, | ||
key_algorithm: permitted_algorithm, | ||
}, | ||
}; | ||
|
||
client | ||
.generate_key(key_name.clone(), key_attributes) | ||
.unwrap(); | ||
let status = client | ||
.sign_with_rsa_sha256(key_name, vec![0xDE, 0xAD, 0xBE, 0xEF]) | ||
.unwrap_err(); | ||
|
||
assert_eq!(status, ResponseStatus::PsaErrorNotPermitted); | ||
} | ||
|
||
#[ignore] | ||
#[test] | ||
fn wrong_permitted_algorithm() { | ||
let mut client = TestClient::new(); | ||
let key_name = String::from("wrong_permitted_algorithm"); | ||
|
||
let key_type = KeyType::RsaKeyPair; | ||
// Do not permit RSA PKCS 1v15 signing algorithm with SHA-256. | ||
let permitted_algorithm = Algorithm::Cipher(Cipher::Ctr); | ||
let key_attributes = KeyAttributes { | ||
key_type, | ||
key_bits: 1024, | ||
key_policy: KeyPolicy { | ||
key_usage_flags: UsageFlags { | ||
sign_hash: true, | ||
verify_hash: false, | ||
sign_message: false, | ||
verify_message: false, | ||
export: false, | ||
encrypt: false, | ||
decrypt: false, | ||
cache: false, | ||
copy: false, | ||
derive: false, | ||
}, | ||
key_algorithm: permitted_algorithm, | ||
}, | ||
}; | ||
Comment on lines
+102
to
+121
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm wondering if this should even be permitted to exist. Feels like the kind of thing we can enforce, because we know what is allowed and what isn't. We can't use type checking directly, but maybe we can use constructors (e.g. for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. True! Those things could be done as part of parallaxsecond/parsec-interface-rs#32 |
||
|
||
client | ||
.generate_key(key_name.clone(), key_attributes) | ||
.unwrap(); | ||
let status = client | ||
.sign_with_rsa_sha256(key_name, vec![0xDE, 0xAD, 0xBE, 0xEF]) | ||
.unwrap_err(); | ||
|
||
assert_eq!(status, ResponseStatus::PsaErrorNotPermitted); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,4 +18,5 @@ mod basic; | |
mod create_destroy_key; | ||
mod export_public_key; | ||
mod import_key; | ||
mod key_attributes; | ||
mod ping; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice 👍