Skip to content

Commit 6c2f542

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 6c2f542

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

rcgen/src/lib.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,15 @@ 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 mut key_identifier_method = KeyIdMethod::Sha256;
615+
for ext in x509.extensions() {
616+
if let x509_parser::extensions::ParsedExtension::SubjectKeyIdentifier(v) =
617+
ext.parsed_extension()
618+
{
619+
key_identifier_method = KeyIdMethod::PreSpecified(v.0.to_vec());
620+
}
621+
}
622+
614623
Ok(CertificateParams {
615624
alg,
616625
is_ca,
@@ -619,6 +628,7 @@ impl CertificateParams {
619628
extended_key_usages,
620629
name_constraints,
621630
serial_number,
631+
key_identifier_method,
622632
distinguished_name: dn,
623633
key_pair: Some(key_pair),
624634
not_before: validity.not_before.to_datetime(),
@@ -1128,10 +1138,13 @@ impl CertificateParams {
11281138
/// This key identifier is used in the SubjectKeyIdentifier X.509v3 extension.
11291139
fn key_identifier<K: PublicKeyData>(&self, pub_key: &K) -> Vec<u8> {
11301140
// Decide which method from RFC 7093 to use
1131-
let digest_method = match self.key_identifier_method {
1141+
let digest_method = match &self.key_identifier_method {
11321142
KeyIdMethod::Sha256 => &digest::SHA256,
11331143
KeyIdMethod::Sha384 => &digest::SHA384,
11341144
KeyIdMethod::Sha512 => &digest::SHA512,
1145+
KeyIdMethod::PreSpecified(b) => {
1146+
return b.to_vec();
1147+
},
11351148
};
11361149
let digest = digest::digest(digest_method, pub_key.raw_bytes());
11371150
let truncated_digest = &digest.as_ref()[0..20];
@@ -1347,6 +1360,8 @@ pub enum KeyIdMethod {
13471360
Sha384,
13481361
/// RFC 7093 method 3
13491362
Sha512,
1363+
/// Pre-specified identifier.
1364+
PreSpecified(Vec<u8>),
13501365
}
13511366

13521367
/// Helper to obtain an `OffsetDateTime` from year, month, day values

0 commit comments

Comments
 (0)