Skip to content

Commit

Permalink
Merge pull request #39 from kushaldas/say_no_to_null_policy
Browse files Browse the repository at this point in the history
Fixes #29 uses StandardPolicy everywhere
  • Loading branch information
kushaldas authored Oct 19, 2020
2 parents af57cc4 + a338850 commit 22264f6
Show file tree
Hide file tree
Showing 14 changed files with 712 additions and 1,202 deletions.
4 changes: 4 additions & 0 deletions docs/introduction.rst
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ Now, if we check the directory from the shell, we will find the keys imported th
as the key store. `SecureDrop <https://securedrop.org>`_ uses **/var/lib/securedrop/store** as their key storage (via gpg's python binding).


.. warning:: This module does not handle keys still using `sha1` or `md5` for hash algorithms. If you are using any such old key, please generate new key
and use them along wtih the module. `This function <https://docs.sequoia-pgp.org/sequoia_openpgp/policy/struct.StandardPolicy.html#method.reject_hash_at>`_ explains in some details why.


KeyStore path for the applicaitons which can run per user
----------------------------------------------------------

Expand Down
37 changes: 18 additions & 19 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ use crate::openpgp::parse::stream::{
};

use crate::openpgp::parse::Parse;
use crate::openpgp::policy::NullPolicy as NP;
use crate::openpgp::policy::Policy;
use crate::openpgp::policy::StandardPolicy as P;
use crate::openpgp::serialize::stream::{Encryptor, LiteralWriter, Message, Signer};
Expand Down Expand Up @@ -168,12 +167,12 @@ impl VerificationHelper for VHelper {

// To create key pairs; from the given Cert
fn get_keys(cert: &openpgp::cert::Cert, password: String) -> Vec<openpgp::crypto::KeyPair> {
let p = &NP::new();
let p = P::new();

let mut keys = Vec::new();
for key in cert
.keys()
.with_policy(p, None)
.with_policy(&p, None)
.alive()
.revoked(false)
.for_signing()
Expand Down Expand Up @@ -294,15 +293,15 @@ fn internal_parse_cert(
py: Python,
cert: openpgp::Cert,
) -> PyResult<(PyObject, String, bool, PyObject, PyObject)> {
let p = &NP::new();
let p = P::new();
let creationtime = match find_creation_time(cert.clone()) {
Some(ctime) => Some(PyDateTime::from_timestamp(py, ctime, None).unwrap()),
None => None,
};

let expirationtime = match cert
.primary_key()
.with_policy(p, None)
.with_policy(&p, None)
.unwrap()
.key_expiration_time()
{
Expand Down Expand Up @@ -447,10 +446,10 @@ fn encrypt_bytes_to_file(
}
let mode = KeyFlags::empty().set_storage_encryption();

let p = &NP::new();
let p = P::new();
let recipients = certs.iter().flat_map(|cert| {
cert.keys()
.with_policy(p, None)
.with_policy(&p, None)
.alive()
.revoked(false)
.key_flags(&mode)
Expand Down Expand Up @@ -608,10 +607,10 @@ fn encrypt_bytes_to_bytes(

let mode = KeyFlags::empty().set_storage_encryption();

let p = &NP::new();
let p = P::new();
let recipients = certs.iter().flat_map(|cert| {
cert.keys()
.with_policy(p, None)
.with_policy(&p, None)
.alive()
.revoked(false)
.key_flags(&mode)
Expand Down Expand Up @@ -677,11 +676,11 @@ impl Johnny {
armor: Option<bool>,
) -> PyResult<PyObject> {
let mode = KeyFlags::empty().set_storage_encryption();
let p = &NP::new();
let p = P::new();
let recipients = self
.cert
.keys()
.with_policy(p, None)
.with_policy(&p, None)
.alive()
.revoked(false)
.key_flags(&mode);
Expand Down Expand Up @@ -726,7 +725,7 @@ impl Johnny {
}

pub fn decrypt_bytes(&self, py: Python, data: Vec<u8>, password: String) -> PyResult<PyObject> {
let p = &NP::new();
let p = P::new();

let mut result = Vec::new();
let reader = std::io::BufReader::new(&data[..]);
Expand All @@ -741,7 +740,7 @@ impl Johnny {
)))
}
};
let mut decryptor = match dec2.with_policy(p, None, Helper::new(p, &self.cert, &password)) {
let mut decryptor = match dec2.with_policy(&p, None, Helper::new(&p, &self.cert, &password)) {
Ok(decr) => decr,
Err(msg) => return Err(PyValueError::new_err(format!("Failed to decrypt: {}", msg))),
};
Expand Down Expand Up @@ -825,14 +824,14 @@ impl Johnny {
output: Vec<u8>,
password: String,
) -> PyResult<bool> {
let p = &NP::new();
let p = P::new();

let input = File::open(str::from_utf8(&filepath[..]).unwrap()).unwrap();
let mut outfile = File::create(str::from_utf8(&output[..]).unwrap()).unwrap();

let mut decryptor = DecryptorBuilder::from_reader(input)
.unwrap()
.with_policy(p, None, Helper::new(p, &self.cert, &password))
.with_policy(&p, None, Helper::new(&p, &self.cert, &password))
.unwrap();
std::io::copy(&mut decryptor, &mut outfile).unwrap();
Ok(true)
Expand All @@ -850,23 +849,23 @@ impl Johnny {
}

pub fn verify_bytes(&self, data: Vec<u8>, sig: Vec<u8>) -> PyResult<bool> {
let p = &P::new();
let p = P::new();
let vh = VHelper::new(&self.cert);
let mut v = DetachedVerifierBuilder::from_bytes(&sig[..])
.unwrap()
.with_policy(p, None, vh)
.with_policy(&p, None, vh)
.unwrap();
match v.verify_bytes(data) {
Ok(()) => return Ok(true),
Err(_) => return Ok(false),
};
}
pub fn verify_file(&self, filepath: Vec<u8>, sig: Vec<u8>) -> PyResult<bool> {
let p = &P::new();
let p = P::new();
let vh = VHelper::new(&self.cert);
let mut v = DetachedVerifierBuilder::from_bytes(&sig[..])
.unwrap()
.with_policy(p, None, vh)
.with_policy(&p, None, vh)
.unwrap();
let path = Path::new(str::from_utf8(&filepath[..]).unwrap());
match v.verify_file(path) {
Expand Down
41 changes: 16 additions & 25 deletions tests/files/double_recipient.asc
Original file line number Diff line number Diff line change
@@ -1,28 +1,19 @@
-----BEGIN PGP MESSAGE-----

hQIMA3bn6DMj2aOvAQ//emjQs3K7ULFskf6IQqlXKVghq2Mby4tNm075tvH59dmb
0ZyPToTsJf0AVJ+i7l//UDtrK+cbN5qoIyqXKtZAiaVeg+y9sTPFGUZiWPy7OHlg
p8WPhpibhFSpxS5MLLsyXPyF0VFSSo64kKlzS/IXZBx03IOZfCo3ESfUnX4Hor8F
VfMsd0mfqjgRSMyySQaPxy8pbUh7+UizSO1nB6e0z/nZfORWOxUWj7Eir2bmWDCX
nZa0WtkRpThLbnqZ7SbNaHjhsopxwUX3wCAa2QkP+GdenX1HI3RmnnXrhhKl7KSQ
jELMMiadsMlQB/Y720peP8X4b2dIz2TKNatnk1z61pzdsQFAzlcXcEo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