@@ -56,59 +56,59 @@ impl_newtype!(KeyUsage, FlagSet<KeyUsages>);
56
56
impl KeyUsage {
57
57
/// The subject public key is used for verifying digital signatures
58
58
pub fn digital_signature ( & self ) -> bool {
59
- self . 0 . bits ( ) & KeyUsages :: DigitalSignature as u16 == KeyUsages :: DigitalSignature as u16
59
+ self . 0 . contains ( KeyUsages :: DigitalSignature )
60
60
}
61
61
62
62
/// When the subject public key is used to verify digital signatures,
63
63
/// it is asserted as non-repudiation.
64
64
pub fn non_repudiation ( & self ) -> bool {
65
- self . 0 . bits ( ) & KeyUsages :: NonRepudiation as u16 == KeyUsages :: NonRepudiation as u16
65
+ self . 0 . contains ( KeyUsages :: NonRepudiation )
66
66
}
67
67
68
68
/// The subject public key is used for enciphering private or
69
69
/// secret keys, i.e., for key transport.
70
70
pub fn key_encipherment ( & self ) -> bool {
71
- self . 0 . bits ( ) & KeyUsages :: KeyEncipherment as u16 == KeyUsages :: KeyEncipherment as u16
71
+ self . 0 . contains ( KeyUsages :: KeyEncipherment )
72
72
}
73
73
74
74
/// The subject public key is used for directly enciphering
75
75
/// raw user data without the use of an intermediate symmetric cipher.
76
76
pub fn data_encipherment ( & self ) -> bool {
77
- self . 0 . bits ( ) & KeyUsages :: DataEncipherment as u16 == KeyUsages :: DataEncipherment as u16
77
+ self . 0 . contains ( KeyUsages :: DataEncipherment )
78
78
}
79
79
80
80
/// The subject public key is used for key agreement
81
81
pub fn key_agreement ( & self ) -> bool {
82
- self . 0 . bits ( ) & KeyUsages :: KeyAgreement as u16 == KeyUsages :: KeyAgreement as u16
82
+ self . 0 . contains ( KeyUsages :: KeyAgreement )
83
83
}
84
84
85
85
/// The subject public key is used for enciphering private or
86
86
/// secret keys, i.e., for key transport.
87
87
pub fn key_cert_sign ( & self ) -> bool {
88
- self . 0 . bits ( ) & KeyUsages :: KeyCertSign as u16 == KeyUsages :: KeyCertSign as u16
88
+ self . 0 . contains ( KeyUsages :: KeyCertSign )
89
89
}
90
90
91
91
/// The subject public key is used for verifying signatures
92
92
/// on certificate revocation lists (e.g., CRLs, delta CRLs,
93
93
/// or ARLs).
94
94
pub fn crl_sign ( & self ) -> bool {
95
- self . 0 . bits ( ) & KeyUsages :: CRLSign as u16 == KeyUsages :: CRLSign as u16
95
+ self . 0 . contains ( KeyUsages :: CRLSign )
96
96
}
97
97
98
98
/// The meaning of the `encipher_only` is undefined when `key_agreement`
99
99
/// returns false. When `encipher_only` returns true and
100
100
/// `key_agreement` also returns true, the subject public key may be
101
101
/// used only for enciphering data while performing key agreement.
102
102
pub fn encipher_only ( & self ) -> bool {
103
- self . 0 . bits ( ) & KeyUsages :: EncipherOnly as u16 == KeyUsages :: EncipherOnly as u16
103
+ self . 0 . contains ( KeyUsages :: EncipherOnly )
104
104
}
105
105
106
106
/// The meaning of the `decipher_only` is undefined when `key_agreement`
107
107
/// returns false. When `encipher_only` returns true and
108
108
/// `key_agreement` also returns true, the subject public key may be
109
109
/// used only for deciphering data while performing key agreement.
110
110
pub fn decipher_only ( & self ) -> bool {
111
- self . 0 . bits ( ) & KeyUsages :: DecipherOnly as u16 == KeyUsages :: DecipherOnly as u16
111
+ self . 0 . contains ( KeyUsages :: DecipherOnly )
112
112
}
113
113
}
114
114
@@ -162,3 +162,32 @@ pub struct PrivateKeyUsagePeriod {
162
162
impl AssociatedOid for PrivateKeyUsagePeriod {
163
163
const OID : ObjectIdentifier = ID_CE_PRIVATE_KEY_USAGE_PERIOD ;
164
164
}
165
+
166
+ #[ cfg( test) ]
167
+ mod tests {
168
+ use super :: * ;
169
+
170
+ #[ test]
171
+ fn digital_signature_contains_digital_signature ( ) {
172
+ let key_usage = KeyUsage ( KeyUsages :: DigitalSignature . into ( ) ) ;
173
+ assert ! ( key_usage. digital_signature( ) ) ;
174
+ }
175
+
176
+ #[ test]
177
+ fn all_contains_digital_signature ( ) {
178
+ let key_usage = KeyUsage ( FlagSet :: full ( ) ) ;
179
+ assert ! ( key_usage. digital_signature( ) ) ;
180
+ }
181
+
182
+ #[ test]
183
+ fn key_encipherment_not_contains_digital_signature ( ) {
184
+ let key_usage = KeyUsage ( KeyUsages :: KeyEncipherment . into ( ) ) ;
185
+ assert ! ( !key_usage. digital_signature( ) ) ;
186
+ }
187
+
188
+ #[ test]
189
+ fn empty_not_contains_digital_signature ( ) {
190
+ let key_usage = KeyUsage ( None . into ( ) ) ;
191
+ assert ! ( !key_usage. digital_signature( ) ) ;
192
+ }
193
+ }
0 commit comments