Skip to content

Commit 657ef97

Browse files
authored
cms: impl From<Certificate> for SignerIdentifier (#1962)
This one is a bit problematic, the [RFC] would read the `SignerIdentifier` is to reuse the `SubjectKeyIdentifier` (SKI) for X.509 exclusively: ``` sid specifies the signer's certificate (and thereby the signer's public key). The signer's public key is needed by the recipient to verify the signature. SignerIdentifier provides two alternatives for specifying the signer's public key. The issuerAndSerialNumber alternative identifies the signer's certificate by the issuer's distinguished name and the certificate serial number; the subjectKeyIdentifier identifies the signer's certificate by a key identifier. When an X.509 certificate is referenced, the key identifier matches the X.509 subjectKeyIdentifier extension value. When other certificate formats are referenced, the documents that specify the certificate format and their use with the CMS must include details on matching the key identifier to the appropriate certificate field. Implementations MUST support the reception of the issuerAndSerialNumber and subjectKeyIdentifier forms of SignerIdentifier. When generating a SignerIdentifier, implementations MAY support one of the forms (either issuerAndSerialNumber or subjectKeyIdentifier) and always use it, or implementations MAY arbitrarily mix the two forms. However, subjectKeyIdentifier MUST be used to refer to a public key contained in a non-X.509 certificate. ``` While the CABF, in the [CodeSigning BR], is fairly explicit that the SKI should no longer be included in the certificate. The best option I've found would be to use the SKI when present, and otherwise rely on the `IssuerAndSerialNumber` [RFC]: https://datatracker.ietf.org/doc/html/rfc5652#section-5.3 [CodeSigning BR]: https://github.com/cabforum/code-signing/blob/main/docs/CSBR.md#7123-code-signing-and-timestamp-certificate
1 parent d3901b5 commit 657ef97

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

cms/src/signed_data.rs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ use core::cmp::Ordering;
88
use der::asn1::{ObjectIdentifier, OctetString, SetOfVec};
99
use der::{Any, Choice, DerOrd, Sequence, ValueOrd};
1010
use spki::AlgorithmIdentifierOwned;
11-
use x509_cert::attr::Attributes;
12-
use x509_cert::ext::pkix::SubjectKeyIdentifier;
13-
use x509_cert::impl_newtype;
11+
use x509_cert::{
12+
attr::Attributes, certificate::Certificate, ext::pkix::SubjectKeyIdentifier, impl_newtype,
13+
};
1414

1515
/// The `SignedData` type is defined in [RFC 5652 Section 5.1].
1616
///
@@ -181,6 +181,23 @@ impl ValueOrd for SignerIdentifier {
181181
}
182182
}
183183

184+
impl From<Certificate> for SignerIdentifier {
185+
fn from(cert: Certificate) -> Self {
186+
let tbs = cert.tbs_certificate();
187+
188+
match tbs.get_extension::<SubjectKeyIdentifier>() {
189+
Ok(Some((_critical, ski))) => Self::SubjectKeyIdentifier(ski),
190+
_ => {
191+
let isn = IssuerAndSerialNumber {
192+
issuer: tbs.issuer().clone(),
193+
serial_number: tbs.serial_number().clone(),
194+
};
195+
Self::IssuerAndSerialNumber(isn)
196+
}
197+
}
198+
}
199+
}
200+
184201
/// The `UnsignedAttributes` type is defined in [RFC 5652 Section 5.3].
185202
///
186203
/// ```text

0 commit comments

Comments
 (0)