Skip to content

Commit 776c757

Browse files
x509-cert: fix KeyUsage bit tests (#993)
1 parent e907165 commit 776c757

File tree

1 file changed

+38
-9
lines changed

1 file changed

+38
-9
lines changed

x509-cert/src/ext/pkix/keyusage.rs

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,59 +56,59 @@ impl_newtype!(KeyUsage, FlagSet<KeyUsages>);
5656
impl KeyUsage {
5757
/// The subject public key is used for verifying digital signatures
5858
pub fn digital_signature(&self) -> bool {
59-
self.0.bits() & KeyUsages::DigitalSignature as u16 == KeyUsages::DigitalSignature as u16
59+
self.0.contains(KeyUsages::DigitalSignature)
6060
}
6161

6262
/// When the subject public key is used to verify digital signatures,
6363
/// it is asserted as non-repudiation.
6464
pub fn non_repudiation(&self) -> bool {
65-
self.0.bits() & KeyUsages::NonRepudiation as u16 == KeyUsages::NonRepudiation as u16
65+
self.0.contains(KeyUsages::NonRepudiation)
6666
}
6767

6868
/// The subject public key is used for enciphering private or
6969
/// secret keys, i.e., for key transport.
7070
pub fn key_encipherment(&self) -> bool {
71-
self.0.bits() & KeyUsages::KeyEncipherment as u16 == KeyUsages::KeyEncipherment as u16
71+
self.0.contains(KeyUsages::KeyEncipherment)
7272
}
7373

7474
/// The subject public key is used for directly enciphering
7575
/// raw user data without the use of an intermediate symmetric cipher.
7676
pub fn data_encipherment(&self) -> bool {
77-
self.0.bits() & KeyUsages::DataEncipherment as u16 == KeyUsages::DataEncipherment as u16
77+
self.0.contains(KeyUsages::DataEncipherment)
7878
}
7979

8080
/// The subject public key is used for key agreement
8181
pub fn key_agreement(&self) -> bool {
82-
self.0.bits() & KeyUsages::KeyAgreement as u16 == KeyUsages::KeyAgreement as u16
82+
self.0.contains(KeyUsages::KeyAgreement)
8383
}
8484

8585
/// The subject public key is used for enciphering private or
8686
/// secret keys, i.e., for key transport.
8787
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)
8989
}
9090

9191
/// The subject public key is used for verifying signatures
9292
/// on certificate revocation lists (e.g., CRLs, delta CRLs,
9393
/// or ARLs).
9494
pub fn crl_sign(&self) -> bool {
95-
self.0.bits() & KeyUsages::CRLSign as u16 == KeyUsages::CRLSign as u16
95+
self.0.contains(KeyUsages::CRLSign)
9696
}
9797

9898
/// The meaning of the `encipher_only` is undefined when `key_agreement`
9999
/// returns false. When `encipher_only` returns true and
100100
/// `key_agreement` also returns true, the subject public key may be
101101
/// used only for enciphering data while performing key agreement.
102102
pub fn encipher_only(&self) -> bool {
103-
self.0.bits() & KeyUsages::EncipherOnly as u16 == KeyUsages::EncipherOnly as u16
103+
self.0.contains(KeyUsages::EncipherOnly)
104104
}
105105

106106
/// The meaning of the `decipher_only` is undefined when `key_agreement`
107107
/// returns false. When `encipher_only` returns true and
108108
/// `key_agreement` also returns true, the subject public key may be
109109
/// used only for deciphering data while performing key agreement.
110110
pub fn decipher_only(&self) -> bool {
111-
self.0.bits() & KeyUsages::DecipherOnly as u16 == KeyUsages::DecipherOnly as u16
111+
self.0.contains(KeyUsages::DecipherOnly)
112112
}
113113
}
114114

@@ -162,3 +162,32 @@ pub struct PrivateKeyUsagePeriod {
162162
impl AssociatedOid for PrivateKeyUsagePeriod {
163163
const OID: ObjectIdentifier = ID_CE_PRIVATE_KEY_USAGE_PERIOD;
164164
}
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

Comments
 (0)