Skip to content

Commit f9049f3

Browse files
Add CRL decode and verification
1 parent 73e1893 commit f9049f3

File tree

8 files changed

+324
-20
lines changed

8 files changed

+324
-20
lines changed

verifier/data/tests/README.md

+10-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,15 @@
22

33
* `root_ca.pem` - Root CA of a certificate chain. This is a copy of an Intel
44
root CA which was in an actual hardware quote.
5-
* `intermediate_ca.pem` - Intermediate CA in a certificate chain. This is a copy
6-
of an Intel intermediate CA which was in an actual hardware quote.
5+
* `processor_ca.pem` - Processor CA in a certificate chain. This is a copy
6+
of an Intel intermediate CA which was in an actual hardware quote. There are
7+
two types of certificate chains "processor" and "platform". "Platform" isn't
8+
currently used by Intel.
79
* `leaf_cert.pem` - Leaf of a certificate chain. This is a copy of an Intel
810
leaf certificate which was in an actual hardware quote.
11+
* `root_crl.pem` - CRL for the root CA of a certificate chain. This was
12+
retrieved via the CRL Distribution Points URI in the root CA,
13+
<https://certificates.trustedservices.intel.com/IntelSGXRootCA.der>
14+
* `processor_crl.pem` - CRL for the processor CA in a certificate chain. This
15+
was retrieved from
16+
<https://api.trustedservices.intel.com/sgx/certification/v4/pckcrl?ca=processor>.
File renamed without changes.

verifier/data/tests/processor_crl.pem

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
-----BEGIN X509 CRL-----
2+
MIIBKzCB0QIBATAKBggqhkjOPQQDAjBxMSMwIQYDVQQDDBpJbnRlbCBTR1ggUENL
3+
IFByb2Nlc3NvciBDQTEaMBgGA1UECgwRSW50ZWwgQ29ycG9yYXRpb24xFDASBgNV
4+
BAcMC1NhbnRhIENsYXJhMQswCQYDVQQIDAJDQTELMAkGA1UEBhMCVVMXDTIzMDQy
5+
MTIyMDAzNloXDTIzMDUyMTIyMDAzNlqgLzAtMAoGA1UdFAQDAgEBMB8GA1UdIwQY
6+
MBaAFNDoqtp11/kuSReYPHsUZdDV8llNMAoGCCqGSM49BAMCA0kAMEYCIQCS4Aod
7+
Y76yImk36lD/U+zHgYWsTNOxgMGAlpoDdDGQDQIhANQMOFTfpB58Pnb6LXs4M5hj
8+
lHpqqWyfsASPMeV8stnc
9+
-----END X509 CRL-----

verifier/data/tests/root_crl.pem

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
-----BEGIN X509 CRL-----
2+
MIIBITCByAIBATAKBggqhkjOPQQDAjBoMRowGAYDVQQDDBFJbnRlbCBTR1ggUm9v
3+
dCBDQTEaMBgGA1UECgwRSW50ZWwgQ29ycG9yYXRpb24xFDASBgNVBAcMC1NhbnRh
4+
IENsYXJhMQswCQYDVQQIDAJDQTELMAkGA1UEBhMCVVMXDTIzMDQwMzEwMjI1MVoX
5+
DTI0MDQwMjEwMjI1MVqgLzAtMAoGA1UdFAQDAgEBMB8GA1UdIwQYMBaAFCJlDNZa
6+
nTSJ84O0lVK/UBs5JwasMAoGCCqGSM49BAMCA0gAMEUCIFFXfUfZ+6FXtl8etfRl
7+
e7xeVsyvc1oD8blj1wSAWrEYAiEAk5AV7BY25+r6X0JsHkAmR8ZzEytoUMq9aM72
8+
utdoKgM=
9+
-----END X509 CRL-----

verifier/src/x509.rs

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// TODO: Remove dead_code exception once this is connected up to the rest of the codebase
44
#![allow(dead_code)]
55
mod certs;
6+
mod crl;
67
mod error;
78

89
pub use error::Error;

verifier/src/x509/certs.rs

+15-15
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@ use p256::ecdsa::{Signature, VerifyingKey};
99
use x509_cert::der::{Decode, Encode};
1010
use x509_cert::Certificate as X509Certificate;
1111

12-
/// Offset from the start of a certificate to the "to be signed" (TBS) portion
13-
/// of the certificate.
14-
const TBS_OFFSET: usize = 4;
15-
1612
/// A certificate whose signature has not been verified.
1713
#[derive(Debug, PartialEq, Eq)]
1814
pub struct UnverifiedCertificate<'a> {
@@ -24,7 +20,7 @@ pub struct UnverifiedCertificate<'a> {
2420
// operations and it's more ergonomic to fail fast than fail later for a
2521
// bad key or signature
2622
signature: Signature,
27-
key: VerifyingKey,
23+
pub(crate) key: VerifyingKey,
2824
}
2925

3026
/// A certificate whose signature has been verified.
@@ -65,9 +61,14 @@ impl<'a> UnverifiedCertificate<'a> {
6561
}
6662

6763
fn verify_signature(&self, key: &VerifyingKey) -> Result<()> {
68-
let tbs_length = self.certificate.tbs_certificate.encoded_len()?;
69-
let tbs_size = u32::from(tbs_length) as usize;
70-
let tbs_contents = &self.der_bytes[TBS_OFFSET..tbs_size + TBS_OFFSET];
64+
let tbs_size = u32::from(self.certificate.tbs_certificate.encoded_len()?) as usize;
65+
let signature_size = u32::from(self.certificate.signature.encoded_len()?) as usize;
66+
let algorithm_size =
67+
u32::from(self.certificate.signature_algorithm.encoded_len()?) as usize;
68+
let overall_size = u32::from(self.certificate.encoded_len()?) as usize;
69+
70+
let tbs_offset = overall_size - (tbs_size + signature_size + algorithm_size);
71+
let tbs_contents = &self.der_bytes[tbs_offset..tbs_size + tbs_offset];
7172
key.verify(tbs_contents, &self.signature)
7273
.map_err(|_| Error::SignatureVerification)?;
7374
Ok(())
@@ -129,12 +130,12 @@ mod test {
129130
use yare::parameterized;
130131

131132
const LEAF_CERT: &str = include_str!("../../data/tests/leaf_cert.pem");
132-
const INTERMEDIATE_CA: &str = include_str!("../../data/tests/intermediate_ca.pem");
133+
const PROCESSOR_CA: &str = include_str!("../../data/tests/processor_ca.pem");
133134
const ROOT_CA: &str = include_str!("../../data/tests/root_ca.pem");
134135

135136
#[parameterized(
136137
root = { ROOT_CA },
137-
intermediate = { INTERMEDIATE_CA },
138+
processor = { PROCESSOR_CA },
138139
leaf = { LEAF_CERT },
139140
)]
140141
fn try_from_der(pem: &str) {
@@ -150,7 +151,7 @@ mod test {
150151
pem_rfc7468::decode_vec(pem.as_bytes()).expect("Failed to decode DER from PEM");
151152
assert!(matches!(
152153
UnverifiedCertificate::try_from(&der_bytes.as_slice()[1..]),
153-
Err(Error::CertificateDecoding(_))
154+
Err(Error::DerDecoding(_))
154155
));
155156
}
156157

@@ -227,8 +228,7 @@ mod test {
227228
let root_cert = UnverifiedCertificate::try_from(der_bytes.as_slice())
228229
.expect("Failed to decode certificate from DER");
229230

230-
let intermediate = INTERMEDIATE_CA;
231-
let (_, der_bytes) = pem_rfc7468::decode_vec(intermediate.as_bytes())
231+
let (_, der_bytes) = pem_rfc7468::decode_vec(PROCESSOR_CA.as_bytes())
232232
.expect("Failed to decode DER from PEM");
233233
let cert = UnverifiedCertificate::try_from(der_bytes.as_slice())
234234
.expect("Failed to decode certificate from DER");
@@ -245,7 +245,7 @@ mod test {
245245

246246
#[test]
247247
fn verify_leaf_certificate() {
248-
let intermediate = INTERMEDIATE_CA;
248+
let intermediate = PROCESSOR_CA;
249249
let (_, der_bytes) = pem_rfc7468::decode_vec(intermediate.as_bytes())
250250
.expect("Failed to decode DER from PEM");
251251
let intermediate_cert = UnverifiedCertificate::try_from(der_bytes.as_slice())
@@ -269,7 +269,7 @@ mod test {
269269

270270
#[test]
271271
fn verify_certificate_fails_with_wrong_key() {
272-
let intermediate = INTERMEDIATE_CA;
272+
let intermediate = PROCESSOR_CA;
273273
let (_, der_bytes) = pem_rfc7468::decode_vec(intermediate.as_bytes())
274274
.expect("Failed to decode DER from PEM");
275275
let intermediate_cert = UnverifiedCertificate::try_from(der_bytes.as_slice())

0 commit comments

Comments
 (0)