@@ -9,10 +9,6 @@ use p256::ecdsa::{Signature, VerifyingKey};
9
9
use x509_cert:: der:: { Decode , Encode } ;
10
10
use x509_cert:: Certificate as X509Certificate ;
11
11
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
-
16
12
/// A certificate whose signature has not been verified.
17
13
#[ derive( Debug , PartialEq , Eq ) ]
18
14
pub struct UnverifiedCertificate < ' a > {
@@ -24,7 +20,7 @@ pub struct UnverifiedCertificate<'a> {
24
20
// operations and it's more ergonomic to fail fast than fail later for a
25
21
// bad key or signature
26
22
signature : Signature ,
27
- key : VerifyingKey ,
23
+ pub ( crate ) key : VerifyingKey ,
28
24
}
29
25
30
26
/// A certificate whose signature has been verified.
@@ -65,9 +61,14 @@ impl<'a> UnverifiedCertificate<'a> {
65
61
}
66
62
67
63
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] ;
71
72
key. verify ( tbs_contents, & self . signature )
72
73
. map_err ( |_| Error :: SignatureVerification ) ?;
73
74
Ok ( ( ) )
@@ -129,12 +130,12 @@ mod test {
129
130
use yare:: parameterized;
130
131
131
132
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" ) ;
133
134
const ROOT_CA : & str = include_str ! ( "../../data/tests/root_ca.pem" ) ;
134
135
135
136
#[ parameterized(
136
137
root = { ROOT_CA } ,
137
- intermediate = { INTERMEDIATE_CA } ,
138
+ processor = { PROCESSOR_CA } ,
138
139
leaf = { LEAF_CERT } ,
139
140
) ]
140
141
fn try_from_der ( pem : & str ) {
@@ -150,7 +151,7 @@ mod test {
150
151
pem_rfc7468:: decode_vec ( pem. as_bytes ( ) ) . expect ( "Failed to decode DER from PEM" ) ;
151
152
assert ! ( matches!(
152
153
UnverifiedCertificate :: try_from( & der_bytes. as_slice( ) [ 1 ..] ) ,
153
- Err ( Error :: CertificateDecoding ( _) )
154
+ Err ( Error :: DerDecoding ( _) )
154
155
) ) ;
155
156
}
156
157
@@ -227,8 +228,7 @@ mod test {
227
228
let root_cert = UnverifiedCertificate :: try_from ( der_bytes. as_slice ( ) )
228
229
. expect ( "Failed to decode certificate from DER" ) ;
229
230
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 ( ) )
232
232
. expect ( "Failed to decode DER from PEM" ) ;
233
233
let cert = UnverifiedCertificate :: try_from ( der_bytes. as_slice ( ) )
234
234
. expect ( "Failed to decode certificate from DER" ) ;
@@ -245,7 +245,7 @@ mod test {
245
245
246
246
#[ test]
247
247
fn verify_leaf_certificate ( ) {
248
- let intermediate = INTERMEDIATE_CA ;
248
+ let intermediate = PROCESSOR_CA ;
249
249
let ( _, der_bytes) = pem_rfc7468:: decode_vec ( intermediate. as_bytes ( ) )
250
250
. expect ( "Failed to decode DER from PEM" ) ;
251
251
let intermediate_cert = UnverifiedCertificate :: try_from ( der_bytes. as_slice ( ) )
@@ -269,7 +269,7 @@ mod test {
269
269
270
270
#[ test]
271
271
fn verify_certificate_fails_with_wrong_key ( ) {
272
- let intermediate = INTERMEDIATE_CA ;
272
+ let intermediate = PROCESSOR_CA ;
273
273
let ( _, der_bytes) = pem_rfc7468:: decode_vec ( intermediate. as_bytes ( ) )
274
274
. expect ( "Failed to decode DER from PEM" ) ;
275
275
let intermediate_cert = UnverifiedCertificate :: try_from ( der_bytes. as_slice ( ) )
0 commit comments