33use crate :: { Error , Result , Version } ;
44use core:: fmt;
55use der:: {
6- Decode , DecodeValue , Encode , EncodeValue , FixedTag , Header , Length , Reader , Sequence , TagMode ,
7- TagNumber , Writer ,
8- asn1:: { AnyRef , BitStringRef , ContextSpecific , OctetStringRef , SetOfRef } ,
6+ Decode , DecodeValue , DerOrd , Encode , EncodeValue , FixedTag , Header , Length , Reader , Sequence , TagMode , TagNumber , Writer , asn1:: { AnyRef , BitStringRef , ContextSpecific , OctetStringRef , SetOf , SetOfRef }
97} ;
108use spki:: AlgorithmIdentifier ;
119use x509_cert:: attr:: Attribute ;
@@ -143,7 +141,7 @@ where
143141 PubKey : DecodeValue < ' a , Error = der:: Error > + FixedTag + ' a ,
144142 PubKey : BitStringLike ,
145143 Attr : DecodeValue < ' a , Error = der:: Error > + FixedTag + ' a ,
146- Attr : EncodeValue ,
144+ Attr : SetOfLike < ' a > ,
147145{
148146 /// Encrypt this private key using a symmetric encryption key derived
149147 /// from the provided password.
@@ -195,6 +193,25 @@ where
195193 }
196194}
197195
196+
197+ impl < ' a , Params , Key , PubKey , Attr > PrivateKeyInfo < Params , Key , PubKey , Attr >
198+ where
199+ Params : der:: Choice < ' a > + Encode ,
200+ Attr : SetOfLike < ' a > ,
201+ {
202+ /// Get a `SET OF` representation of the attributes, if present.
203+ fn attributes_string ( & self ) -> Option < ContextSpecific < SetOfRef < ' a , Attr :: Item > > > {
204+ self . attributes . as_ref ( ) . map ( |attributes| {
205+ let value = attributes. as_set_of ( ) ;
206+ ContextSpecific {
207+ tag_number : ATTRIBUTES_TAG ,
208+ tag_mode : TagMode :: Implicit ,
209+ value,
210+ }
211+ } )
212+ }
213+ }
214+
198215impl < ' a , Params , Key , PubKey , Attr > DecodeValue < ' a > for PrivateKeyInfo < Params , Key , PubKey , Attr >
199216where
200217 Params : der:: Choice < ' a , Error = der:: Error > + Encode ,
@@ -243,21 +260,22 @@ where
243260 Params : der:: Choice < ' a , Error = der:: Error > + Encode ,
244261 Key : EncodeValue + FixedTag ,
245262 PubKey : BitStringLike ,
246- Attr : EncodeValue + FixedTag ,
263+ Attr : SetOfLike < ' a > ,
264+ Attr :: Item : Clone + Decode < ' a > + DerOrd + Encode ,
247265{
248266 fn value_len ( & self ) -> der:: Result < Length > {
249267 self . version ( ) . encoded_len ( ) ?
250268 + self . algorithm . encoded_len ( ) ?
251269 + self . private_key . encoded_len ( ) ?
252- + self . attributes . encoded_len ( ) ?
270+ + self . attributes_string ( ) . encoded_len ( ) ?
253271 + self . public_key_bit_string ( ) . encoded_len ( ) ?
254272 }
255273
256274 fn encode_value ( & self , writer : & mut impl Writer ) -> der:: Result < ( ) > {
257275 self . version ( ) . encode ( writer) ?;
258276 self . algorithm . encode ( writer) ?;
259277 self . private_key . encode ( writer) ?;
260- self . attributes . encode ( writer) ?;
278+ self . attributes_string ( ) . encode ( writer) ?;
261279 self . public_key_bit_string ( ) . encode ( writer) ?;
262280 Ok ( ( ) )
263281 }
@@ -270,8 +288,8 @@ where
270288 Key : EncodeValue ,
271289 PubKey : DecodeValue < ' a , Error = der:: Error > + FixedTag + ' a ,
272290 PubKey : BitStringLike ,
273- Attr : DecodeValue < ' a , Error = der:: Error > + FixedTag + ' a ,
274- Attr : EncodeValue ,
291+ Attr : DecodeValue < ' a , Error = der:: Error > + FixedTag + SetOfLike < ' a > + ' a ,
292+ Attr :: Item : Clone + Decode < ' a > + DerOrd + Encode ,
275293{
276294}
277295
@@ -282,8 +300,8 @@ where
282300 Key : EncodeValue ,
283301 PubKey : DecodeValue < ' a , Error = der:: Error > + FixedTag + ' a ,
284302 PubKey : BitStringLike ,
285- Attr : DecodeValue < ' a , Error = der:: Error > + FixedTag + ' a ,
286- Attr : EncodeValue ,
303+ Attr : DecodeValue < ' a , Error = der:: Error > + FixedTag + SetOfLike < ' a > + ' a ,
304+ Attr :: Item : Clone + Decode < ' a > + DerOrd + Encode ,
287305{
288306 type Error = Error ;
289307
@@ -317,8 +335,8 @@ where
317335 Key : EncodeValue ,
318336 PubKey : DecodeValue < ' a , Error = der:: Error > + FixedTag + ' a ,
319337 PubKey : BitStringLike ,
320- Attr : DecodeValue < ' a , Error = der:: Error > + FixedTag + ' a ,
321- Attr : EncodeValue ,
338+ Attr : DecodeValue < ' a , Error = der:: Error > + FixedTag + SetOfLike < ' a > + ' a ,
339+ Attr :: Item : Clone + Decode < ' a > + DerOrd + Encode ,
322340{
323341 type Error = Error ;
324342
@@ -336,8 +354,8 @@ where
336354 Key : EncodeValue ,
337355 PubKey : DecodeValue < ' a , Error = der:: Error > + FixedTag + ' a ,
338356 PubKey : BitStringLike ,
339- Attr : DecodeValue < ' a , Error = der:: Error > + FixedTag + ' a ,
340- Attr : EncodeValue ,
357+ Attr : DecodeValue < ' a , Error = der:: Error > + FixedTag + SetOfLike < ' a > + ' a ,
358+ Attr :: Item : Clone + Decode < ' a > + DerOrd + Encode ,
341359{
342360 type Error = Error ;
343361
@@ -390,9 +408,8 @@ where
390408}
391409
392410/// [`PrivateKeyInfo`] with [`AnyRef`] algorithm parameters, and `&[u8]` key.
393- // pub type PrivateKeyInfoRef<'a> = PrivateKeyInfo<AnyRef<'a>, &'a OctetStringRef, BitStringRef<'a>, &'a[&'a Attribute]>; // Consider attr and value here
394411pub type PrivateKeyInfoRef < ' a > =
395- PrivateKeyInfo < AnyRef < ' a > , & ' a OctetStringRef , BitStringRef < ' a > , SetOfRef < ' a , Attribute > > ; // Consider attr and value here
412+ PrivateKeyInfo < AnyRef < ' a > , & ' a OctetStringRef , BitStringRef < ' a > , SetOfRef < ' a , Attribute > > ;
396413
397414/// [`PrivateKeyInfo`] with [`Any`] algorithm parameters, and `Box<[u8]>` key.
398415#[ cfg( feature = "alloc" ) ]
@@ -411,6 +428,23 @@ impl BitStringLike for BitStringRef<'_> {
411428 }
412429}
413430
431+ pub trait SetOfLike < ' a >
432+ {
433+ type Item : Clone + DerOrd + Encode ;
434+ fn as_set_of ( & self ) -> SetOfRef < ' a , Self :: Item > ;
435+ }
436+
437+ impl < ' a , T > SetOfLike < ' a > for SetOfRef < ' a , T >
438+ where
439+ T : Clone + DerOrd + Encode ,
440+ {
441+ type Item = T ;
442+
443+ fn as_set_of ( & self ) -> SetOfRef < ' a , T > {
444+ SetOfRef :: from ( self )
445+ }
446+ }
447+
414448#[ cfg( feature = "alloc" ) ]
415449mod allocating {
416450 use super :: * ;
@@ -424,6 +458,17 @@ mod allocating {
424458 }
425459 }
426460
461+ impl < ' a , T > SetOfLike < ' a > for & ' a SetOfVec < T >
462+ where
463+ T : Clone + DerOrd + Encode + ' a ,
464+ {
465+ type Item = T ;
466+
467+ fn as_set_of ( & self ) -> SetOfRef < ' a , T > {
468+ self . owned_to_ref ( )
469+ }
470+ }
471+
427472 impl < ' a > RefToOwned < ' a > for PrivateKeyInfoRef < ' a > {
428473 type Owned = PrivateKeyInfoOwned ;
429474 fn ref_to_owned ( & self ) -> Self :: Owned {
0 commit comments