@@ -413,7 +413,7 @@ impl<'a> Iterator for DistinguishedNameIterator<'a> {
413
413
}
414
414
415
415
/// One of the purposes contained in the [key usage](https://datatracker.ietf.org/doc/html/rfc5280#section-4.2.1.3) extension
416
- #[ derive( Debug , PartialEq , Eq , Hash , Clone ) ]
416
+ #[ derive( Debug , PartialEq , Eq , Hash , Clone , Copy ) ]
417
417
pub enum KeyUsagePurpose {
418
418
/// digitalSignature
419
419
DigitalSignature ,
@@ -452,6 +452,28 @@ impl KeyUsagePurpose {
452
452
KeyUsagePurpose :: DecipherOnly => 8 ,
453
453
}
454
454
}
455
+
456
+ /// Parse a collection of key usages from a [`u16`] representing the value
457
+ /// of a KeyUsage BIT STRING as defined by RFC 5280.
458
+ fn from_u16 ( value : u16 ) -> Vec < Self > {
459
+ [
460
+ KeyUsagePurpose :: DigitalSignature ,
461
+ KeyUsagePurpose :: ContentCommitment ,
462
+ KeyUsagePurpose :: KeyEncipherment ,
463
+ KeyUsagePurpose :: DataEncipherment ,
464
+ KeyUsagePurpose :: KeyAgreement ,
465
+ KeyUsagePurpose :: KeyCertSign ,
466
+ KeyUsagePurpose :: CrlSign ,
467
+ KeyUsagePurpose :: EncipherOnly ,
468
+ KeyUsagePurpose :: DecipherOnly ,
469
+ ]
470
+ . iter ( )
471
+ . filter_map ( |key_usage| {
472
+ let present = key_usage. to_u16 ( ) & value != 0 ;
473
+ present. then_some ( * key_usage)
474
+ } )
475
+ . collect ( )
476
+ }
455
477
}
456
478
457
479
/// Method to generate key identifiers from public keys.
0 commit comments