From aef143f89e74f6b44f16561da2b6093f4166730b Mon Sep 17 00:00:00 2001 From: Justus Winter Date: Mon, 9 Jul 2018 15:24:56 +0200 Subject: openpgp: Add and use a type specifying reasons for revocations. --- openpgp/src/subpacket.rs | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) (limited to 'openpgp/src/subpacket.rs') diff --git a/openpgp/src/subpacket.rs b/openpgp/src/subpacket.rs index 677eca49..c79d3965 100644 --- a/openpgp/src/subpacket.rs +++ b/openpgp/src/subpacket.rs @@ -77,6 +77,7 @@ use constants::{ CompressionAlgorithm, HashAlgorithm, PublicKeyAlgorithm, + ReasonForRevocation, SymmetricAlgorithm, }; use conversions::{ @@ -616,7 +617,7 @@ pub enum SubpacketValue<'a> { /// 1 octet of revocation code, N octets of reason string ReasonForRevocation { /// Machine-readable reason for revocation. - code: u8, + code: ReasonForRevocation, /// Human-readable reason for revocation. reason: &'a [u8], @@ -937,7 +938,7 @@ impl<'a> From> for Subpacket<'a> { // 1 octet of revocation code, N octets of reason string if raw.value.len() >= 1 { Some(SubpacketValue::ReasonForRevocation { - code: raw.value[0], + code: raw.value[0].into(), reason: &raw.value[1..], }) } else { @@ -2190,7 +2191,8 @@ impl Signature { /// /// Note: if the signature contains multiple instances of this /// subpacket, only the last one is considered. - pub fn reason_for_revocation(&self) -> Option<(u8, &[u8])> { + pub fn reason_for_revocation(&self) + -> Option<(ReasonForRevocation, &[u8])> { // 1 octet of revocation code, N octets of reason string if let Some(sb) = self.subpacket(SubpacketTag::ReasonForRevocation) { if let SubpacketValue::ReasonForRevocation { @@ -2206,7 +2208,8 @@ impl Signature { } /// Sets the value of the Reason for Revocation subpacket. - pub fn set_reason_for_revocation(&mut self, code: u8, reason: &[u8]) + pub fn set_reason_for_revocation(&mut self, code: ReasonForRevocation, + reason: &[u8]) -> Result<()> { self.hashed_area.replace(Subpacket::new( SubpacketValue::ReasonForRevocation { @@ -2505,8 +2508,10 @@ fn accessors() { sig.set_signers_user_id(b"foobar").unwrap(); assert_eq!(sig.signers_user_id(), Some(&b"foobar"[..])); - sig.set_reason_for_revocation(3, b"foobar").unwrap(); - assert_eq!(sig.reason_for_revocation(), Some((3, &b"foobar"[..]))); + sig.set_reason_for_revocation(ReasonForRevocation::KeyRetired, + b"foobar").unwrap(); + assert_eq!(sig.reason_for_revocation(), + Some((ReasonForRevocation::KeyRetired, &b"foobar"[..]))); let feats = Features::default().set_mdc(true); sig.set_features(&feats).unwrap(); @@ -2916,13 +2921,14 @@ fn subpacket_test_2() { })); assert_eq!(sig.reason_for_revocation(), - Some((0, &b"Forgot to set a sig expiration."[..]))); + Some((ReasonForRevocation::Unspecified, + &b"Forgot to set a sig expiration."[..]))); assert_eq!(sig.subpacket(SubpacketTag::ReasonForRevocation), Some(Subpacket { critical: false, tag: SubpacketTag::ReasonForRevocation, value: SubpacketValue::ReasonForRevocation { - code: 0, + code: ReasonForRevocation::Unspecified, reason: &b"Forgot to set a sig expiration."[..], }, })); -- cgit v1.2.3