@@ -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,29 @@ 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
+ #[ cfg( feature = "x509-parser" ) ]
459
+ fn from_u16 ( value : u16 ) -> Vec < Self > {
460
+ [
461
+ KeyUsagePurpose :: DigitalSignature ,
462
+ KeyUsagePurpose :: ContentCommitment ,
463
+ KeyUsagePurpose :: KeyEncipherment ,
464
+ KeyUsagePurpose :: DataEncipherment ,
465
+ KeyUsagePurpose :: KeyAgreement ,
466
+ KeyUsagePurpose :: KeyCertSign ,
467
+ KeyUsagePurpose :: CrlSign ,
468
+ KeyUsagePurpose :: EncipherOnly ,
469
+ KeyUsagePurpose :: DecipherOnly ,
470
+ ]
471
+ . iter ( )
472
+ . filter_map ( |key_usage| {
473
+ let present = key_usage. to_u16 ( ) & value != 0 ;
474
+ present. then_some ( * key_usage)
475
+ } )
476
+ . collect ( )
477
+ }
455
478
}
456
479
457
480
/// Method to generate key identifiers from public keys.
0 commit comments