Support for EC keys, fix Attributes processing#369
Support for EC keys, fix Attributes processing#369ya-mouse wants to merge 1 commit intogoogle:mainfrom
Conversation
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
9c95185 to
8387a48
Compare
| "Unsupported attribute type - marking as unavailable" | ||
| ); | ||
| attribute.ulValueLen = CK_UNAVAILABLE_INFORMATION; | ||
| // result = Err(Error::AttributeTypeInvalid(attribute.type_)); |
There was a problem hiding this comment.
we should skip unknown attributes otherwise a tool that handles pkcs11 calls silently tries to interpret random values from the template: pkcs11-tool and p11-kit produces weird values for attributes when inspecting with pkcs11-spy.
There was a problem hiding this comment.
Can you please split the bug fix for attribute parsing into a separate PR?
|
|
||
| pub type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>; | ||
| pub type Digest = [u8; 20]; | ||
| pub type Digest = [u8; 64]; |
There was a problem hiding this comment.
was small to fit ECC P-256 digest value
| AttributeType::Verify => Some(Attribute::Verify(true)), | ||
| AttributeType::VerifyRecover => Some(Attribute::VerifyRecover(false)), | ||
| AttributeType::Wrap => Some(Attribute::Wrap(false)), | ||
| AttributeType::Encrypt => Some(Attribute::Encrypt(false)), |
There was a problem hiding this comment.
Added some missed attributes with default values that pkcs11-tool/p11-kit tools complains about.
There was a problem hiding this comment.
Apologies for the churn but can you also make this change in a separate PR? I'm working on adding integration tests for pkcs11-tool output I would like to use to validate this change.
8387a48 to
cf42ba3
Compare
|
Thank you for the PR! Just to set expectations, I probably won't have an opportunity to review and test until the new year. |
9302927 to
32315ac
Compare
f84d10f to
2c5b44e
Compare
2c5b44e to
b66b6e0
Compare
| // For EC keys, the algorithm parameters contain the curve OID | ||
| // For EC keys, the subject public key is the EC point | ||
| Some(( | ||
| ObjectIdentifier::from_bytes(spki.algorithm.parameters.unwrap().value()) |
There was a problem hiding this comment.
spki.algorithm.parameters_oid().unwrap().to_der().unwrap() seems to do the same thing.
| .unwrap() | ||
| .to_der() | ||
| .unwrap(), | ||
| OctetString::new(spki.subject_public_key.raw_bytes()).unwrap().to_der().unwrap(), |
There was a problem hiding this comment.
spki.subject_public_key.to_der().unwrap() also seems like an easier way to get the key as DER encoded unless I'm missing something.
| // Parse the DER-encoded SPKI | ||
| let spki: SubjectPublicKeyInfoRef<'_> = SubjectPublicKeyInfoRef::try_from(der_bytes).unwrap(); | ||
| // Parse the RSA public key bytes from the SPKI | ||
| let rsa_pubkey = RsaPublicKey::from_der(spki.subject_public_key.raw_bytes()).ok()?; |
There was a problem hiding this comment.
RsaPublicKey::from_der(spki.subject_public_key.as_bytes()?).ok()? works as well.
| )) | ||
| } | ||
|
|
||
| fn extract_rsa_params(der_bytes: &[u8]) -> Option<(Vec<u8>, Vec<u8>, u64)> { |
There was a problem hiding this comment.
Nit: return a usize from the method and do the type casting further down.
| let spki: SubjectPublicKeyInfoRef<'_> = SubjectPublicKeyInfoRef::try_from(der_bytes).unwrap(); | ||
| // Parse the RSA public key bytes from the SPKI | ||
| let rsa_pubkey = RsaPublicKey::from_der(spki.subject_public_key.raw_bytes()).ok()?; | ||
| let modulus = rsa_pubkey.modulus.as_bytes(); |
There was a problem hiding this comment.
Nit: call rsa_pubkey.modulus.as_bytes().to_vec() in the struct constructor instead of assigning to a variable.
This change adds proper handling for EC keys (addressing #303) as well as proper RSA parameters calculation.
NOTE: For instance, openssl expects that EC params (curve OID) and points are in specific uncompressed and padded format. In Go encoding EC_POINTS be like:
I faced this issue when implementing fake custom backend using regular PEM/DER files or passing signature produced by AWS KMS.
Perhaps, as later improvement we can try to instantiate specific signature from DER bytes and then repack params into expected format. Now this is offloaded to the backend.
The changes were checked with my custom backend using RSA2048 and ECC NIST-P256 keys via OpenSSL.