diff options
author | Wiktor Kwapisiewicz <wiktor@metacode.biz> | 2020-04-01 15:43:48 +0200 |
---|---|---|
committer | Wiktor Kwapisiewicz <wiktor@metacode.biz> | 2020-04-03 14:22:46 +0200 |
commit | a3e3ab4d03a25c576961d20fa42c494280114be3 (patch) | |
tree | fbee1ae6cc6736f89df3ff41d15adb27502c089a | |
parent | ad582b769fe2a8ce2bfc278f2304e4266f131afb (diff) |
openpgp: Convert `Sexp::from_ciphertext` to `TryFrom<&mpis::Ciphertext>`
-rw-r--r-- | ipc/src/gnupg.rs | 3 | ||||
-rw-r--r-- | openpgp/src/crypto/sexp.rs | 103 |
2 files changed, 56 insertions, 50 deletions
diff --git a/ipc/src/gnupg.rs b/ipc/src/gnupg.rs index 408df3cf..7220b7f0 100644 --- a/ipc/src/gnupg.rs +++ b/ipc/src/gnupg.rs @@ -3,6 +3,7 @@ #![warn(missing_docs)] use std::collections::BTreeMap; +use std::convert::TryFrom; use std::ops::{Deref, DerefMut}; use std::path::{Path, PathBuf}; use std::process::Command; @@ -630,7 +631,7 @@ impl<'a, 'b, 'c, R> Future for DecryptionRequest<'a, 'b, 'c, R> }, Async::Ready(None) => { let mut buf = Vec::new(); - Sexp::from_ciphertext(&self.ciphertext)? + Sexp::try_from(self.ciphertext)? .serialize(&mut buf)?; self.c.data(&buf)?; self.state = Inquire(Vec::new(), true); diff --git a/openpgp/src/crypto/sexp.rs b/openpgp/src/crypto/sexp.rs index a7da7f0c..c4325e57 100644 --- a/openpgp/src/crypto/sexp.rs +++ b/openpgp/src/crypto/sexp.rs @@ -6,6 +6,7 @@ //! //! [S-Expressions]: https://people.csail.mit.edu/rivest/Sexp.txt +use std::convert::TryFrom; use std::fmt; use std::ops::Deref; use quickcheck::{Arbitrary, Gen}; @@ -37,55 +38,6 @@ impl fmt::Debug for Sexp { } impl Sexp { - /// Constructs an S-Expression representing `ciphertext`. - /// - /// The resulting expression is suitable for gpg-agent's `INQUIRE - /// CIPHERTEXT` inquiry. - pub fn from_ciphertext(ciphertext: &mpis::Ciphertext) -> Result<Self> { - use crate::crypto::mpis::Ciphertext::*; - match ciphertext { - RSA { ref c } => - Ok(Sexp::List(vec![ - Sexp::String("enc-val".into()), - Sexp::List(vec![ - Sexp::String("rsa".into()), - Sexp::List(vec![ - Sexp::String("a".into()), - Sexp::String(c.value().into())])])])), - - &ElGamal { ref e, ref c } => - Ok(Sexp::List(vec![ - Sexp::String("enc-val".into()), - Sexp::List(vec![ - Sexp::String("elg".into()), - Sexp::List(vec![ - Sexp::String("a".into()), - Sexp::String(e.value().into())]), - Sexp::List(vec![ - Sexp::String("b".into()), - Sexp::String(c.value().into())])])])), - - &ECDH { ref e, ref key } => - Ok(Sexp::List(vec![ - Sexp::String("enc-val".into()), - Sexp::List(vec![ - Sexp::String("ecdh".into()), - Sexp::List(vec![ - Sexp::String("s".into()), - Sexp::String(key.as_ref().into())]), - Sexp::List(vec![ - Sexp::String("e".into()), - Sexp::String(e.value().into())])])])), - - &Unknown { .. } => - Err(Error::InvalidArgument( - format!("Don't know how to convert {:?}", ciphertext)) - .into()), - - __Nonexhaustive => unreachable!(), - } - } - /// Completes the decryption of this S-Expression representing a /// wrapped session key. /// @@ -288,6 +240,59 @@ impl Sexp { } } +impl TryFrom<&mpis::Ciphertext> for Sexp { + type Error = anyhow::Error; + + /// Constructs an S-Expression representing `ciphertext`. + /// + /// The resulting expression is suitable for gpg-agent's `INQUIRE + /// CIPHERTEXT` inquiry. + fn try_from(ciphertext: &mpis::Ciphertext) -> Result<Self> { + use crate::crypto::mpis::Ciphertext::*; + match ciphertext { + RSA { ref c } => + Ok(Sexp::List(vec![ + Sexp::String("enc-val".into()), + Sexp::List(vec![ + Sexp::String("rsa".into()), + Sexp::List(vec![ + Sexp::String("a".into()), + Sexp::String(c.value().into())])])])), + + &ElGamal { ref e, ref c } => + Ok(Sexp::List(vec![ + Sexp::String("enc-val".into()), + Sexp::List(vec![ + Sexp::String("elg".into()), + Sexp::List(vec![ + Sexp::String("a".into()), + Sexp::String(e.value().into())]), + Sexp::List(vec![ + Sexp::String("b".into()), + Sexp::String(c.value().into())])])])), + + &ECDH { ref e, ref key } => + Ok(Sexp::List(vec![ + Sexp::String("enc-val".into()), + Sexp::List(vec![ + Sexp::String("ecdh".into()), + Sexp::List(vec![ + Sexp::String("s".into()), + Sexp::String(key.as_ref().into())]), + Sexp::List(vec![ + Sexp::String("e".into()), + Sexp::String(e.value().into())])])])), + + &Unknown { .. } => + Err(Error::InvalidArgument( + format!("Don't know how to convert {:?}", ciphertext)) + .into()), + + __Nonexhaustive => unreachable!(), + } + } +} + impl Arbitrary for Sexp { fn arbitrary<G: Gen>(g: &mut G) -> Self { if f32::arbitrary(g) < 0.7 { |