@@ -40,6 +40,9 @@ const MAX_PRINCIPALS: usize = 256;
4040///
4141/// OpenSSH supports X.509-like certificate authorities, but using a custom encoding format.
4242///
43+ /// The signed portion is retained verbatim when decoding and reused for signature verification
44+ /// and encoding. Certificates created with [`Builder`] retain the exact bytes passed to the signer.
45+ ///
4346/// <div class="warning">
4447/// <b>Security Warning</b>
4548///
@@ -165,6 +168,12 @@ pub struct Certificate {
165168
166169 /// Comment on the certificate.
167170 comment : Comment ,
171+
172+ /// Certificate algorithm, including any RSA hash selection in the wire identifier.
173+ algorithm : Algorithm ,
174+
175+ /// Exact signed bytes, from the initial algorithm string through the signature key.
176+ tbs_certificate : Vec < u8 > ,
168177}
169178
170179impl Certificate {
@@ -187,7 +196,7 @@ impl Certificate {
187196 let mut cert = Certificate :: decode ( & mut reader) ?;
188197
189198 // Verify that the algorithm in the Base64-encoded data matches the text
190- if encapsulation. algorithm_id != cert. algorithm ( ) . to_certificate_type ( ) {
199+ if encapsulation. algorithm_id != cert. algorithm . to_certificate_type ( ) {
191200 return Err ( Error :: AlgorithmUnknown ) ;
192201 }
193202
@@ -211,11 +220,7 @@ impl Certificate {
211220 /// # Errors
212221 /// Returns [`Error::Encoding`] in the event of an encoding error.
213222 pub fn to_openssh ( & self ) -> Result < String > {
214- SshFormat :: encode_string (
215- & self . algorithm ( ) . to_certificate_type ( ) ,
216- self ,
217- self . comment ( ) ,
218- )
223+ SshFormat :: encode_string ( & self . algorithm . to_certificate_type ( ) , self , self . comment ( ) )
219224 }
220225
221226 /// Serialize OpenSSH certificate as raw bytes.
@@ -249,10 +254,10 @@ impl Certificate {
249254 Ok ( ( ) )
250255 }
251256
252- /// Get the public key algorithm for this certificate.
257+ /// Get the algorithm identified by this certificate.
253258 #[ must_use]
254259 pub fn algorithm ( & self ) -> Algorithm {
255- self . public_key . algorithm ( )
260+ self . algorithm . clone ( )
256261 }
257262
258263 /// Get the comment on this certificate.
@@ -480,17 +485,14 @@ impl Certificate {
480485 /// </div>
481486 #[ doc( hidden) ]
482487 pub fn verify_signature ( & self ) -> Result < ( ) > {
483- let mut tbs_certificate = Vec :: new ( ) ;
484- self . encode_tbs ( & mut tbs_certificate) ?;
485488 self . signature_key
486- . verify ( & tbs_certificate, & self . signature )
489+ . verify ( & self . tbs_certificate , & self . signature )
487490 . map_err ( |_| Error :: CertificateValidation )
488491 }
489492
490- /// Encode the portion of the certificate "to be signed" by the CA
491- /// (or to be verified against an existing CA signature)
493+ /// Encode the fields of a newly built certificate before signing it.
492494 fn encode_tbs ( & self , writer : & mut impl Writer ) -> encoding:: Result < ( ) > {
493- self . algorithm ( ) . to_certificate_type ( ) . encode ( writer) ?;
495+ self . algorithm . to_certificate_type ( ) . encode ( writer) ?;
494496 self . nonce . encode ( writer) ?;
495497 self . public_key . encode_key_data ( writer) ?;
496498 self . serial . encode ( writer) ?;
@@ -507,13 +509,34 @@ impl Certificate {
507509
508510 /// Decode [`Certificate`] for the specified algorithm.
509511 ///
512+ /// The initial algorithm string has already been consumed. Its encoding is supplied by
513+ /// `algorithm.to_certificate_type()`; the remaining signed bytes are retained verbatim.
514+ ///
510515 /// # Errors
511516 /// - Returns [`Error::Encoding`] in the event of an encoding error.
512517 /// - Returns [`Error::CertificateFieldInvalid`] if a certificate field is invalid.
513518 pub fn decode_as ( reader : & mut Decoder < impl Reader > , algorithm : Algorithm ) -> Result < Self > {
519+ Self :: decode_inner ( reader, Some ( algorithm) )
520+ }
521+
522+ fn decode_inner (
523+ reader : & mut Decoder < impl Reader > ,
524+ algorithm : Option < Algorithm > ,
525+ ) -> Result < Self > {
526+ let mut source = reader. clone ( ) ;
527+ let mut tbs = Vec :: new ( ) ;
528+ let algorithm = match algorithm {
529+ Some ( algorithm) => {
530+ // The caller already consumed the algorithm string.
531+ algorithm. to_certificate_type ( ) . encode ( & mut tbs) ?;
532+ algorithm
533+ }
534+ None => Algorithm :: new_certificate ( & String :: decode ( reader) ?) ?,
535+ } ;
536+
514537 Ok ( Self {
515538 nonce : Vec :: decode ( reader) ?,
516- public_key : KeyData :: decode_as ( reader, algorithm) ?,
539+ public_key : KeyData :: decode_as ( reader, algorithm. clone ( ) ) ?,
517540 serial : u64:: decode ( reader) ?,
518541 cert_type : CertType :: decode ( reader) ?,
519542 key_id : String :: decode ( reader) ?,
@@ -524,8 +547,18 @@ impl Certificate {
524547 extensions : OptionsMap :: decode ( reader) ?,
525548 reserved : Vec :: decode ( reader) ?,
526549 signature_key : decode_signature_key ( reader) ?,
550+ tbs_certificate : {
551+ // The signature covers everything consumed through the signature key.
552+ let len = source. remaining_len ( ) - reader. remaining_len ( ) ;
553+ let start = tbs. len ( ) ;
554+ tbs. try_reserve_exact ( len) . map_err ( encoding:: Error :: from) ?;
555+ tbs. resize ( start + len, 0 ) ;
556+ source. read ( & mut tbs[ start..] ) ?;
557+ tbs
558+ } ,
527559 signature : reader. read_prefixed ( Signature :: decode) ?,
528560 comment : Comment :: default ( ) ,
561+ algorithm,
529562 } )
530563 }
531564}
@@ -568,34 +601,21 @@ impl Decode for Certificate {
568601 type Error = Error ;
569602
570603 fn decode ( reader : & mut Decoder < impl Reader > ) -> Result < Self > {
571- let algorithm = Algorithm :: new_certificate ( & String :: decode ( reader) ?) ?;
572- Self :: decode_as ( reader, algorithm)
604+ Self :: decode_inner ( reader, None )
573605 }
574606}
575607
576608impl Encode for Certificate {
577609 fn encoded_len ( & self ) -> encoding:: Result < usize > {
578610 [
579- self . algorithm ( ) . to_certificate_type ( ) . encoded_len ( ) ?,
580- self . nonce . encoded_len ( ) ?,
581- self . public_key . encoded_key_data_len ( ) ?,
582- self . serial . encoded_len ( ) ?,
583- self . cert_type . encoded_len ( ) ?,
584- self . key_id . encoded_len ( ) ?,
585- self . valid_principals . encoded_len ( ) ?,
586- self . valid_after . encoded_len ( ) ?,
587- self . valid_before . encoded_len ( ) ?,
588- self . critical_options . encoded_len ( ) ?,
589- self . extensions . encoded_len ( ) ?,
590- self . reserved . encoded_len ( ) ?,
591- self . signature_key . encoded_len_prefixed ( ) ?,
611+ self . tbs_certificate . len ( ) ,
592612 self . signature . encoded_len_prefixed ( ) ?,
593613 ]
594614 . checked_sum ( )
595615 }
596616
597617 fn encode ( & self , writer : & mut impl Writer ) -> encoding:: Result < ( ) > {
598- self . encode_tbs ( writer ) ?;
618+ writer . write ( & self . tbs_certificate ) ?;
599619 self . signature . encode_prefixed ( writer)
600620 }
601621}
0 commit comments