Skip to content

Import private key support for TPM provider #243

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

Merged

Conversation

joechrisellis
Copy link
Contributor

No description provided.

Copy link
Member

@hug-dev hug-dev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this! I like the scaffolding you did, something we need to do/coordinate on all our providers.

I left a few comments and I have a request: could you please add a e2e test importing a RSA key pair? There might already be existing tests on that which are not activated for the TPM provider.


let (int_bytes, _) = private_key
.public_exponent()
.split_at(std::mem::size_of::<u32>());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

split_at could panic if the public exponent is smaller than 4 bytes but I guess that's fine because of the validate_ functions above.

pub fn validate_private_key(private_key: &RSAPrivateKey, attributes: &Attributes) -> Result<()> {
// NOTE: potentially incomplete, but any errors that aren't caught here should be caught
// further down the stack (i.e. in the tss crate).
let key_prime = &private_key.primes()[0];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it is better to use the fields of RSAPrivateKey directly

Type::RsaKeyPair => self.psa_import_key_internal_rsa_keypair(app_name, op),
_ => {
error!(
"The TPM provider does not support the {:?} key type.",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I know, the TPM provider does not support importing these other key types but it might support generating keys of those types

@joechrisellis joechrisellis force-pushed the import_private_key_support branch from d164d5d to d7f4cbe Compare September 8, 2020 16:54
Copy link
Member

@ionut-arm ionut-arm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the change! 🚀

Comment on lines 294 to 347
/// Validates an RSAPrivateKey against the attributes we expect. Returns ok on success, otherwise
/// returns an error.
pub fn validate_private_key(private_key: &RSAPrivateKey, attributes: &Attributes) -> Result<()> {
// NOTE: potentially incomplete, but any errors that aren't caught here should be caught
// further down the stack (i.e. in the tss crate).
let key_prime = &private_key.prime_1;
let key_len = key_prime.len();
if key_len != attributes.bits / 2 {
error!(
"The key prime is not of the expected size (expected {}, got {})",
attributes.bits / 2,
key_len,
);
return Err(ResponseStatus::PsaErrorInvalidArgument);
}
Ok(())
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💯

let len = key_data.len();

let key_bits = attributes.bits;
if key_bits != 0 && len * 8 != key_bits {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering - given that the modulus is given as a vector of bytes, the assumed size is always a multiple of 8 (i.e. len * 8). But the size of the modulus could theoretically be something in between, like 1245, in which case the MSBits of the last byte won't be set. Now, given that the TPM will most likely reject that anyway (I think only a few values are accepted by the spec), should we reject it from here? The log messages below wouldn't be very helpful in this (rather edge) case

if len != 128 && len != 256 {
if crate::utils::GlobalConfig::log_error_details() {
error!(
"The TPM provider only supports 1024 and 2048 bits RSA public keys ({} bits given).",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this limitation in the TransientKeyContext? I think TPMs support one of 1024, 2048, 3072 or 4096

@joechrisellis joechrisellis force-pushed the import_private_key_support branch 2 times, most recently from 2cb263c to fb747ab Compare September 9, 2020 13:33
@joechrisellis joechrisellis force-pushed the import_private_key_support branch 4 times, most recently from 3be245a to e10d335 Compare September 10, 2020 11:44
@joechrisellis joechrisellis force-pushed the import_private_key_support branch from e10d335 to 8197ce9 Compare September 10, 2020 12:21
Copy link
Member

@hug-dev hug-dev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!
It all look good to me! Just one question about the test, if we want to add it in the e2e_tests folder instead so that it benefits other providers as well. Probably fine for now.

@@ -284,3 +321,61 @@ fn asym_encrypt_with_crate() {

assert_eq!(&initial_plaintext[..], &plaintext[..]);
}

#[test]
fn import_private_key() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding the test!
@ionut-arm is it fine for this test to be here or should we add it as an e2e test?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leave it there! I'll rebase my changes, though I won't finish today

@hug-dev hug-dev merged commit 72ebbee into parallaxsecond:master Sep 10, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants