@@ -66,6 +66,7 @@ use pkcs8::spki::ObjectIdentifier;
66
66
67
67
mod components;
68
68
mod generate;
69
+ mod signature_ref;
69
70
mod signing_key;
70
71
mod size;
71
72
mod verifying_key;
@@ -77,10 +78,10 @@ pub const OID: ObjectIdentifier = ObjectIdentifier::new_unwrap("1.2.840.10040.4.
77
78
78
79
use alloc:: { boxed:: Box , vec:: Vec } ;
79
80
use pkcs8:: der:: {
80
- self , Decode , DecodeValue , Encode , EncodeValue , FixedTag , Header , Length , Reader , Sequence ,
81
- Writer , asn1:: UintRef ,
81
+ self , Decode , DecodeValue , Encode , EncodeValue , Header , Length , Reader , Sequence , Writer ,
82
82
} ;
83
83
use signature:: SignatureEncoding ;
84
+ use signature_ref:: { SignatureBoxed , SignatureRef } ;
84
85
85
86
/// Container of the DSA signature
86
87
#[ derive( Clone , Debug ) ]
@@ -112,34 +113,33 @@ impl Signature {
112
113
pub fn s ( & self ) -> & NonZero < BoxedUint > {
113
114
& self . s
114
115
}
116
+
117
+ fn to_boxed ( & self ) -> SignatureBoxed {
118
+ SignatureBoxed :: new ( self )
119
+ }
120
+ fn to_der_using_ref ( & self ) -> der:: Result < Vec < u8 > > {
121
+ self . to_boxed ( ) . to_ref ( ) ?. to_der ( )
122
+ }
115
123
}
116
124
117
125
impl < ' a > DecodeValue < ' a > for Signature {
118
126
type Error = der:: Error ;
119
127
120
- fn decode_value < R : Reader < ' a > > ( reader : & mut R , _header : Header ) -> der:: Result < Self > {
121
- let r = UintRef :: decode ( reader) ?;
122
- let s = UintRef :: decode ( reader) ?;
123
-
124
- let r = BoxedUint :: from_be_slice ( r. as_bytes ( ) , r. as_bytes ( ) . len ( ) as u32 * 8 )
125
- . map_err ( |_| UintRef :: TAG . value_error ( ) ) ?;
126
- let s = BoxedUint :: from_be_slice ( s. as_bytes ( ) , s. as_bytes ( ) . len ( ) as u32 * 8 )
127
- . map_err ( |_| UintRef :: TAG . value_error ( ) ) ?;
128
+ fn decode_value < R : Reader < ' a > > ( reader : & mut R , header : Header ) -> der:: Result < Self > {
129
+ let signature_ref = SignatureRef :: decode_value ( reader, header) ?;
128
130
129
- Self :: from_components ( r , s ) . ok_or_else ( || reader . error ( UintRef :: TAG . value_error ( ) ) )
131
+ signature_ref . to_owned ( )
130
132
}
131
133
}
132
134
133
135
impl EncodeValue for Signature {
134
136
fn value_len ( & self ) -> der:: Result < Length > {
135
- UintRef :: new ( & self . r . to_be_bytes ( ) ) ? . encoded_len ( ) ?
136
- + UintRef :: new ( & self . s . to_be_bytes ( ) ) ? . encoded_len ( ) ?
137
+ // TODO: avoid Box<[u8]> allocation here
138
+ self . to_boxed ( ) . to_ref ( ) ? . value_len ( )
137
139
}
138
140
139
141
fn encode_value ( & self , writer : & mut impl Writer ) -> der:: Result < ( ) > {
140
- UintRef :: new ( & self . r . to_be_bytes ( ) ) ?. encode ( writer) ?;
141
- UintRef :: new ( & self . s . to_be_bytes ( ) ) ?. encode ( writer) ?;
142
- Ok ( ( ) )
142
+ self . to_boxed ( ) . to_ref ( ) ?. encode_value ( writer)
143
143
}
144
144
}
145
145
@@ -171,7 +171,7 @@ impl SignatureEncoding for Signature {
171
171
}
172
172
173
173
fn to_vec ( & self ) -> Vec < u8 > {
174
- self . to_der ( ) . expect ( "DER encoding error" )
174
+ self . to_der_using_ref ( ) . expect ( "DER encoding error" )
175
175
}
176
176
}
177
177
0 commit comments