@@ -436,23 +436,6 @@ pub enum KeyUsagePurpose {
436
436
}
437
437
438
438
impl KeyUsagePurpose {
439
- /// Encode a key usage as the value of a BIT STRING as defined by RFC 5280.
440
- /// [`u16`] is sufficient to encode the largest possible key usage value (two bytes).
441
- fn to_u16 ( & self ) -> u16 {
442
- const FLAG : u16 = 0b1000_0000_0000_0000 ;
443
- FLAG >> match self {
444
- KeyUsagePurpose :: DigitalSignature => 0 ,
445
- KeyUsagePurpose :: ContentCommitment => 1 ,
446
- KeyUsagePurpose :: KeyEncipherment => 2 ,
447
- KeyUsagePurpose :: DataEncipherment => 3 ,
448
- KeyUsagePurpose :: KeyAgreement => 4 ,
449
- KeyUsagePurpose :: KeyCertSign => 5 ,
450
- KeyUsagePurpose :: CrlSign => 6 ,
451
- KeyUsagePurpose :: EncipherOnly => 7 ,
452
- KeyUsagePurpose :: DecipherOnly => 8 ,
453
- }
454
- }
455
-
456
439
/// Parse a collection of key usages from a [`u16`] representing the value
457
440
/// of a KeyUsage BIT STRING as defined by RFC 5280.
458
441
#[ cfg( feature = "x509-parser" ) ]
@@ -470,13 +453,32 @@ impl KeyUsagePurpose {
470
453
]
471
454
. iter ( )
472
455
. filter_map ( |key_usage| {
473
- let present = key_usage . to_u16 ( ) & value != 0 ;
456
+ let present = u16 :: from ( key_usage ) & value != 0 ;
474
457
present. then_some ( * key_usage)
475
458
} )
476
459
. collect ( )
477
460
}
478
461
}
479
462
463
+ /// Encode a key usage as the value of a BIT STRING as defined by RFC 5280.
464
+ /// [`u16`] is sufficient to encode the largest possible key usage value (two bytes).
465
+ impl From < KeyUsagePurpose > for u16 {
466
+ fn from ( value : KeyUsagePurpose ) -> Self {
467
+ const FLAG : u16 = 0b1000_0000_0000_0000 ;
468
+ FLAG >> match value {
469
+ KeyUsagePurpose :: DigitalSignature => 0 ,
470
+ KeyUsagePurpose :: ContentCommitment => 1 ,
471
+ KeyUsagePurpose :: KeyEncipherment => 2 ,
472
+ KeyUsagePurpose :: DataEncipherment => 3 ,
473
+ KeyUsagePurpose :: KeyAgreement => 4 ,
474
+ KeyUsagePurpose :: KeyCertSign => 5 ,
475
+ KeyUsagePurpose :: CrlSign => 6 ,
476
+ KeyUsagePurpose :: EncipherOnly => 7 ,
477
+ KeyUsagePurpose :: DecipherOnly => 8 ,
478
+ }
479
+ }
480
+ }
481
+
480
482
/// Method to generate key identifiers from public keys.
481
483
///
482
484
/// Key identifiers should be derived from the public key data. [RFC 7093] defines
0 commit comments