Skip to content

Commit

Permalink
Add PreSpecified(Vec<u8>) option to KeyIdMethod. (#197)
Browse files Browse the repository at this point in the history
If using from_ca_cert_der/_pem, the key_identifier_method would always
be set to Sha256, which is not always true. If using OpenSSL for example
SHA1 would be used.

If the provided CA certificate contains a SubjectKeyIdentifier
extension, then this option will be automatically set.

Fixes #195.
  • Loading branch information
brocaar authored Dec 9, 2023
1 parent 8798e81 commit 5ed5fcc
Showing 1 changed file with 139 additions and 1 deletion.
140 changes: 139 additions & 1 deletion rcgen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,16 @@ impl CertificateParams {
let name_constraints = Self::convert_x509_name_constraints(&x509)?;
let serial_number = Some(x509.serial.to_bytes_be().into());

let key_identifier_method = x509
.iter_extensions()
.find_map(|ext| match ext.parsed_extension() {
x509_parser::extensions::ParsedExtension::SubjectKeyIdentifier(key_id) => {
Some(KeyIdMethod::PreSpecified(key_id.0.into()))
},
_ => None,
})
.unwrap_or(KeyIdMethod::Sha256);

Ok(CertificateParams {
alg,
is_ca,
Expand All @@ -619,6 +629,7 @@ impl CertificateParams {
extended_key_usages,
name_constraints,
serial_number,
key_identifier_method,
distinguished_name: dn,
key_pair: Some(key_pair),
not_before: validity.not_before.to_datetime(),
Expand Down Expand Up @@ -1128,10 +1139,13 @@ impl CertificateParams {
/// This key identifier is used in the SubjectKeyIdentifier X.509v3 extension.
fn key_identifier<K: PublicKeyData>(&self, pub_key: &K) -> Vec<u8> {
// Decide which method from RFC 7093 to use
let digest_method = match self.key_identifier_method {
let digest_method = match &self.key_identifier_method {
KeyIdMethod::Sha256 => &digest::SHA256,
KeyIdMethod::Sha384 => &digest::SHA384,
KeyIdMethod::Sha512 => &digest::SHA512,
KeyIdMethod::PreSpecified(b) => {
return b.to_vec();
},
};
let digest = digest::digest(digest_method, pub_key.raw_bytes());
let truncated_digest = &digest.as_ref()[0..20];
Expand Down Expand Up @@ -1347,6 +1361,8 @@ pub enum KeyIdMethod {
Sha384,
/// RFC 7093 method 3
Sha512,
/// Pre-specified identifier.
PreSpecified(Vec<u8>),
}

/// Helper to obtain an `OffsetDateTime` from year, month, day values
Expand Down Expand Up @@ -1936,4 +1952,126 @@ mod tests {
assert_eq!(SanType::IpAddress(IpAddr::from(octets)), actual);
}
}

#[cfg(feature = "x509-parser")]
mod test_key_identifier_from_ca {
use super::*;

#[test]
fn load_ca_and_sign_cert() {
let ca_cert = r#"-----BEGIN CERTIFICATE-----
MIIFDTCCAvWgAwIBAgIUVuDfDt/BUVfObGOHsM+L5/qPZfIwDQYJKoZIhvcNAQEL
BQAwFjEUMBIGA1UEAwwLZXhhbXBsZS5jb20wHhcNMjMxMjA4MTAwOTI2WhcNMjQx
MTI4MTAwOTI2WjAWMRQwEgYDVQQDDAtleGFtcGxlLmNvbTCCAiIwDQYJKoZIhvcN
AQEBBQADggIPADCCAgoCggIBAKXyZsv7Zwek9yc54IXWjCkMwU4eDMz9Uw06WETF
hZtauwDo4usCeYJa/7x8RZbGcI99s/vOMHjIdVzY6g9p5c6qS+7EUBhXARYVB74z
XUGwgVGss7lgw+0dNxhQ8F0M2smBXUP9FlJJjJpbWeU+93iynGy+PTXFtYMnOoVI
4G7YKsG5lX0zBJUNYZslEz6Kp8eRYu7FAdccU0u5bmg02a1WiXOYJeN1+AifUbRN
zNInZCqMCFgoHczb0DvKU3QX/xrcBxfr/SNJPqxlecUvsozteUoAFAUF1uTxH31q
cVmCHf9I0r6JJoGxs+XMVbH2SJLdsq/+zpjeHz6gy0z4aRMBpaUWUQ9pEENeSq15
PXCuX3yPT2BII30mL86OWO6qgms70iALak6xZ/xAT7RT22E1bOF+XJsiUM3OgGF0
TPmDcpafEMH4kwzdaC7U5hqhYk9I2lfTMEghV86kUXClExuHEQD4GZLcd1HMD/Wg
qOZO4y/t/yzBPNq01FpeilFph/tW6pxr1X7Jloz1/yIuNFK0oXTB24J/TUi+/S1B
kavOBg3eNHHDXDjESKtnV+iwo1cFt6LVCrnKhKJ6m95+c+YKQGIrc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T8aR9j4JzTZg1wTKgOM004N+Y8DrtN9CLQia
xPwzEP2kvT6sn2rQpA9MNrSmgA0Gmqe1qa45LFk23K+8dnuHCP36TupZGBuMj0vP
/4kcrQENCfZnm8VPWnE/4pM1mBHiNWQ7b9fO93qV1cGmXIGD2Aj92bRHyAmsKk/n
D3lMkohUI4JjePOdlu/hzjVvmcTS9d0UPc1VwTyHcaBA2Rb8yM16bvOu8580SgzR
LpsUrVJi64X95a9u2MeyjF8quyWTh4s900wTzW0CggEAJrGNHMTKtJmfXAp4OoHv
CHNs8Fd3a6zdIFQuulqxKGKgmyfyj0ZVmHmizLEm+GSnpqKk73u4u7jNSgF2w85u
2teg6BH23VN/roe/hRrWV5czegzOAj5ZSZjmWlmZYXJEyKwKdG89ZOhit7RkVe0x
xBeyjWPDwoP0d1WbQGwyboflaEmcO8kOX8ITa9CMNokMkrScGvSlWYRlBiz1LzIE
E0i3Uj90pFtoCpKv6JsAF88bnHHrltOjnK3oTdAontTLZNuFjbsOBGmWd9XK5tGd
yPaor0EknPNpW9OYsssDq9vVvqXHc+GERTkS+RsBW7JKyoCuqKlhdVmkFoAmgppS
VwKCAQB7nOsjguXliXXpayr1ojg1T5gk+R+JJMbOw7fuhexavVLi2I/yGqAq9gfQ
KoumYrd8EYb0WddqK0rdfjZyPmiqCNr72w3QKiEDx8o3FHUajSL1+eXpJJ03shee
BqN6QWlRz8fu7MAZ0oqv06Cln+3MZRUvc6vtMHAEzD7y65HV+Do7z61YmvwVZ2N2
+30kckNnDVdggOklBmlSk5duej+RVoAKP8U5wV3Z/bS5J0OI75fxhuzybPcVfkwE
JiY98T5oN1X0C/qAXxJfSvklbru9fipwGt3dho5Tm6Ee3cYf+plnk4WZhSnqyef4
PITGdT9dgN88nHPCle0B1+OY+OZ5
-----END PRIVATE KEY-----"#;

let kp = KeyPair::from_pem(&ca_key).unwrap();
let params = CertificateParams::from_ca_cert_pem(ca_cert, kp).unwrap();
let expected_ski = vec![
0x97, 0xD4, 0x76, 0xA1, 0x9B, 0x1A, 0x71, 0x35, 0x2A, 0xC7, 0xF4, 0xA1, 0x84, 0x12,
0x56, 0x06, 0xBA, 0x5D, 0x61, 0x84,
];

assert_eq!(
KeyIdMethod::PreSpecified(expected_ski.clone()),
params.key_identifier_method
);

let ca_cert = Certificate::from_params(params).unwrap();
assert_eq!(&expected_ski, &ca_cert.get_key_identifier());

let ca_der = ca_cert.serialize_der().unwrap();
let (_remainder, x509) = x509_parser::parse_x509_certificate(&ca_der).unwrap();
assert_eq!(
&expected_ski,
&x509
.iter_extensions()
.find_map(|ext| match ext.parsed_extension() {
x509_parser::extensions::ParsedExtension::SubjectKeyIdentifier(key_id) => {
Some(key_id.0.to_vec())
},
_ => None,
})
.unwrap()
);
}
}
}

0 comments on commit 5ed5fcc

Please sign in to comment.