Skip to content

Commit 5b81bde

Browse files
committed
Add PreSpecified(Vec<u8>) option to KeyIdMethod.
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.
1 parent 1a579ca commit 5b81bde

File tree

1 file changed

+148
-1
lines changed

1 file changed

+148
-1
lines changed

rcgen/src/lib.rs

Lines changed: 148 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,16 @@ impl CertificateParams {
611611
let name_constraints = Self::convert_x509_name_constraints(&x509)?;
612612
let serial_number = Some(x509.serial.to_bytes_be().into());
613613

614+
let key_identifier_method = x509
615+
.iter_extensions()
616+
.find_map(|ext| match ext.parsed_extension() {
617+
x509_parser::extensions::ParsedExtension::SubjectKeyIdentifier(key_id) => {
618+
Some(KeyIdMethod::PreSpecified(key_id.0.into()))
619+
},
620+
_ => None,
621+
})
622+
.unwrap_or(KeyIdMethod::Sha256);
623+
614624
Ok(CertificateParams {
615625
alg,
616626
is_ca,
@@ -619,6 +629,7 @@ impl CertificateParams {
619629
extended_key_usages,
620630
name_constraints,
621631
serial_number,
632+
key_identifier_method,
622633
distinguished_name: dn,
623634
key_pair: Some(key_pair),
624635
not_before: validity.not_before.to_datetime(),
@@ -1128,10 +1139,13 @@ impl CertificateParams {
11281139
/// This key identifier is used in the SubjectKeyIdentifier X.509v3 extension.
11291140
fn key_identifier<K: PublicKeyData>(&self, pub_key: &K) -> Vec<u8> {
11301141
// Decide which method from RFC 7093 to use
1131-
let digest_method = match self.key_identifier_method {
1142+
let digest_method = match &self.key_identifier_method {
11321143
KeyIdMethod::Sha256 => &digest::SHA256,
11331144
KeyIdMethod::Sha384 => &digest::SHA384,
11341145
KeyIdMethod::Sha512 => &digest::SHA512,
1146+
KeyIdMethod::PreSpecified(b) => {
1147+
return b.to_vec();
1148+
},
11351149
};
11361150
let digest = digest::digest(digest_method, pub_key.raw_bytes());
11371151
let truncated_digest = &digest.as_ref()[0..20];
@@ -1347,6 +1361,8 @@ pub enum KeyIdMethod {
13471361
Sha384,
13481362
/// RFC 7093 method 3
13491363
Sha512,
1364+
/// Pre-specified identifier.
1365+
PreSpecified(Vec<u8>),
13501366
}
13511367

13521368
/// Helper to obtain an `OffsetDateTime` from year, month, day values
@@ -1936,4 +1952,135 @@ mod tests {
19361952
assert_eq!(SanType::IpAddress(IpAddr::from(octets)), actual);
19371953
}
19381954
}
1955+
1956+
#[cfg(feature = "x509-parser")]
1957+
mod test_key_identifier_from_ca {
1958+
use super::*;
1959+
1960+
#[test]
1961+
fn load_ca_and_sign_cert() {
1962+
let ca_cert = r#"-----BEGIN CERTIFICATE-----
1963+
MIIFDTCCAvWgAwIBAgIUVuDfDt/BUVfObGOHsM+L5/qPZfIwDQYJKoZIhvcNAQEL
1964+
BQAwFjEUMBIGA1UEAwwLZXhhbXBsZS5jb20wHhcNMjMxMjA4MTAwOTI2WhcNMjQx
1965+
MTI4MTAwOTI2WjAWMRQwEgYDVQQDDAtleGFtcGxlLmNvbTCCAiIwDQYJKoZIhvcN
1966+
AQEBBQADggIPADCCAgoCggIBAKXyZsv7Zwek9yc54IXWjCkMwU4eDMz9Uw06WETF
1967+
hZtauwDo4usCeYJa/7x8RZbGcI99s/vOMHjIdVzY6g9p5c6qS+7EUBhXARYVB74z
1968+
XUGwgVGss7lgw+0dNxhQ8F0M2smBXUP9FlJJjJpbWeU+93iynGy+PTXFtYMnOoVI
1969+
4G7YKsG5lX0zBJUNYZslEz6Kp8eRYu7FAdccU0u5bmg02a1WiXOYJeN1+AifUbRN
1970+
zNInZCqMCFgoHczb0DvKU3QX/xrcBxfr/SNJPqxlecUvsozteUoAFAUF1uTxH31q
1971+
cVmCHf9I0r6JJoGxs+XMVbH2SJLdsq/+zpjeHz6gy0z4aRMBpaUWUQ9pEENeSq15
1972+
PXCuX3yPT2BII30mL86OWO6qgms70iALak6xZ/xAT7RT22E1bOF+XJsiUM3OgG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+
VwKCAQB7nOsjguXliXXpayr1ojg1T5gk+R+JJMbOw7fuhexavVLi2I/yGqAq9gfQ
2039+
KoumYrd8EYb0WddqK0rdfjZyPmiqCNr72w3QKiEDx8o3FHUajSL1+eXpJJ03shee
2040+
BqN6QWlRz8fu7MAZ0oqv06Cln+3MZRUvc6vtMHAEzD7y65HV+Do7z61YmvwVZ2N2
2041+
+30kckNnDVdggOklBmlSk5duej+RVoAKP8U5wV3Z/bS5J0OI75fxhuzybPcVfkwE
2042+
JiY98T5oN1X0C/qAXxJfSvklbru9fipwGt3dho5Tm6Ee3cYf+plnk4WZhSnqyef4
2043+
PITGdT9dgN88nHPCle0B1+OY+OZ5
2044+
-----END PRIVATE KEY-----"#;
2045+
2046+
let kp = KeyPair::from_pem(&ca_key).unwrap();
2047+
let params = CertificateParams::from_ca_cert_pem(ca_cert, kp).unwrap();
2048+
2049+
assert_eq!(
2050+
KeyIdMethod::PreSpecified(vec![
2051+
0x97, 0xD4, 0x76, 0xA1, 0x9B, 0x1A, 0x71, 0x35, 0x2A, 0xC7, 0xF4, 0xA1, 0x84,
2052+
0x12, 0x56, 0x06, 0xBA, 0x5D, 0x61, 0x84
2053+
]),
2054+
params.key_identifier_method
2055+
);
2056+
2057+
let ca_cert = Certificate::from_params(params).unwrap();
2058+
2059+
assert_eq!(
2060+
vec![
2061+
0x97, 0xD4, 0x76, 0xA1, 0x9B, 0x1A, 0x71, 0x35, 0x2A, 0xC7, 0xF4, 0xA1, 0x84,
2062+
0x12, 0x56, 0x06, 0xBA, 0x5D, 0x61, 0x84
2063+
],
2064+
ca_cert.get_key_identifier()
2065+
);
2066+
2067+
let ca_der = ca_cert.serialize_der().unwrap();
2068+
2069+
let (_remainder, x509) = x509_parser::parse_x509_certificate(&ca_der).unwrap();
2070+
assert_eq!(
2071+
vec![
2072+
0x97, 0xD4, 0x76, 0xA1, 0x9B, 0x1A, 0x71, 0x35, 0x2A, 0xC7, 0xF4, 0xA1, 0x84,
2073+
0x12, 0x56, 0x06, 0xBA, 0x5D, 0x61, 0x84
2074+
],
2075+
x509.iter_extensions()
2076+
.find_map(|ext| match ext.parsed_extension() {
2077+
x509_parser::extensions::ParsedExtension::SubjectKeyIdentifier(key_id) => {
2078+
Some(key_id.0.to_vec())
2079+
},
2080+
_ => None,
2081+
})
2082+
.unwrap()
2083+
);
2084+
}
2085+
}
19392086
}

0 commit comments

Comments
 (0)