use super::traits;
use crate::errors::RPMError;
use std::io::{Cursor, Read};
use ::pgp::{composed::Deserializable, types::KeyTrait};
use ::pgp::packet::*;
fn now() -> ::chrono::DateTime<::chrono::Utc> {
use ::chrono::offset::TimeZone;
let now = ::chrono::offset::Utc::now();
::chrono::offset::Utc.timestamp(now.timestamp(), 0u32)
}
#[derive(Clone, Debug)]
pub struct Signer {
secret_key: ::pgp::composed::signed_key::SignedSecretKey,
}
impl traits::Signing<traits::algorithm::RSA> for Signer {
type Signature = Vec<u8>;
fn sign<R: Read>(&self, mut data: R) -> Result<Self::Signature, RPMError> {
let passwd_fn = || String::new();
let now = now();
let sig_cfg = SignatureConfig {
version: SignatureVersion::V4,
typ: SignatureType::Binary,
pub_alg: ::pgp::crypto::public_key::PublicKeyAlgorithm::RSA,
hash_alg: ::pgp::crypto::hash::HashAlgorithm::SHA2_256,
issuer: Some(self.secret_key.key_id()),
created: Some(now),
unhashed_subpackets: vec![],
hashed_subpackets: vec![
Subpacket::SignatureCreationTime(now),
Subpacket::Issuer(self.secret_key.key_id()),
],
};
let mut buf = Vec::with_capacity(16384);
let _ = data.read_to_end(&mut buf)?;
let signature_packet = sig_cfg
.sign(&self.secret_key, passwd_fn, buf.as_slice())
.map_err(|e| RPMError::SignError(Box::new(e)))?;
let mut signature_bytes = Vec::with_capacity(1024);
let mut cursor = Cursor::new(&mut signature_bytes);
::pgp::packet::write_packet(&mut cursor, &signature_packet)
.map_err(|e| RPMError::SignError(Box::new(e)))?;
Ok(signature_bytes)
}
}
impl Signer {
pub fn load_from_asc_bytes(input: &[u8]) -> Result<Self, RPMError> {
let input = ::std::str::from_utf8(input).map_err(|e| RPMError::KeyLoadError {
source: Box::new(e),
details: "Failed to parse bytes as utf8 for ascii armored parsing",
})?;
Self::load_from_asc(input)
}
pub fn load_from_asc(input: &str) -> Result<Self, RPMError> {
let (secret_key, _) = ::pgp::composed::signed_key::SignedSecretKey::from_string(input)
.map_err(|e| RPMError::KeyLoadError {
source: Box::new(e),
details: "Failed to parse bytes as ascii armored key",
})?;
Ok(Self { secret_key })
}
}
#[derive(Clone, Debug)]
pub struct Verifier {
public_key: ::pgp::composed::signed_key::SignedPublicKey,
}
impl Verifier {
fn parse_signature(signature: &[u8]) -> Result<::pgp::packet::Signature, RPMError> {
let mut cursor = Cursor::new(signature);
let parser = ::pgp::packet::PacketParser::new(&mut cursor);
let signature = parser
.filter_map(|res| match res {
Ok(::pgp::packet::Packet::Signature(sig_packet)) => Some(sig_packet),
_ => None,
})
.next()
.ok_or_else(|| RPMError::NoSignatureFound)?;
Ok(signature)
}
}
impl traits::Verifying<traits::algorithm::RSA> for Verifier {
type Signature = Vec<u8>;
fn verify<R: Read>(&self, mut data: R, signature: &[u8]) -> Result<(), RPMError> {
let signature = Self::parse_signature(signature)?;
log::debug!("Signature issued by: {:?}", signature.issuer());
let mut buf = Vec::with_capacity(16384);
let _ = data.read_to_end(&mut buf)?;
let buf = &buf[..];
if let Some(key_id) = signature.issuer() {
log::trace!("Signature has issuer ref: {:?}", key_id);
if self.public_key.key_id() == *key_id {
return signature.verify(&self.public_key, buf).map_err(|e| {
RPMError::VerificationError {
source: Box::new(e),
key_ref: format!("{:?}", key_id),
}
});
} else {
log::trace!(
"Signature issuer key id {:?} does not match primary keys key id: {:?}",
key_id,
self.public_key.key_id()
);
}
self.public_key
.public_subkeys
.iter()
.filter_map(|sub_key| {
if sub_key.key_id().as_ref() == key_id.as_ref() {
log::trace!(
"Found a matching key id {:?} == {:?}",
sub_key.key_id(),
key_id
);
Some(sub_key)
} else {
log::trace!("Not the one we want: {:?}", sub_key);
None
}
})
.fold(
Err(RPMError::KeyNotFoundError {
key_ref: format!("{:?}", key_id),
}),
|previous_res, sub_key| {
if previous_res.is_err() {
log::trace!("Test next candidate subkey");
signature.verify(sub_key, buf).map_err(|e| {
RPMError::VerificationError {
source: Box::new(e),
key_ref: format!("{:?}", sub_key.key_id()),
}
})
} else {
log::trace!("Signature already verified, nop");
Ok(())
}
},
)
} else {
log::trace!(
"Signature has no issuer ref, attempting primary key: {:?}",
self.public_key.primary_key.key_id()
);
signature
.verify(&self.public_key, buf)
.map_err(|e| RPMError::VerificationError {
source: Box::new(e),
key_ref: format!("{:?}", self.public_key.key_id()),
})
}
}
}
impl Verifier {
pub fn load_from_asc_bytes(input: &[u8]) -> Result<Self, RPMError> {
let input = ::std::str::from_utf8(input).map_err(|e| RPMError::KeyLoadError {
source: Box::new(e),
details: "Failed to parse bytes as utf8 for ascii armored parsing",
})?;
Self::load_from_asc(input)
}
pub fn load_from_asc(input: &str) -> Result<Self, RPMError> {
let (public_key, _) = ::pgp::composed::signed_key::SignedPublicKey::from_string(input)
.map_err(|e| RPMError::KeyLoadError {
source: Box::new(e),
details: "Failed to parse bytes as ascii armored key",
})?;
Ok(Self { public_key })
}
}
#[cfg(test)]
pub(crate) mod test {
use super::super::{echo_signature, Signing, Verifying};
use super::*;
use super::Signer;
use super::Verifier;
fn prep() -> (Signer, Verifier) {
let _ = env_logger::try_init();
let (signing_key, verification_key) = load_asc_keys();
let verifier =
Verifier::load_from_asc_bytes(verification_key.as_slice()).expect("PK parsing failed");
let signer =
Signer::load_from_asc_bytes(signing_key.as_slice()).expect("PK parsing failed");
(signer, verifier)
}
pub(crate) fn load_asc_keys() -> (Vec<u8>, Vec<u8>) {
let signing_key = include_bytes!("../../../test_assets/secret_key.asc");
let verification_key = include_bytes!("../../../test_assets/public_key.asc");
(signing_key.to_vec(), verification_key.to_vec())
}
#[test]
fn parse_asc() {
let (signing_key, verification_key) = load_asc_keys();
assert!(Signer::load_from_asc_bytes(signing_key.as_ref()).is_ok());
assert!(Verifier::load_from_asc_bytes(verification_key.as_ref()).is_ok());
}
use std::io::Cursor;
#[test]
fn sign_verify_roundtrip() {
let data = b"dfsdfjsd9ivnq320348934752312308205723900000580134850sdf";
let mut cursor = Cursor::new(&data[..]);
let (signer, verifier) = prep();
let signature = signer.sign(&mut cursor).expect("signed");
let signature = signature.as_slice();
{
let _packet =
Verifier::parse_signature(signature).expect("Created signature should be parsable");
}
echo_signature("test/roundtrip", signature);
let mut cursor = Cursor::new(&data[..]);
verifier
.verify(&mut cursor, signature)
.expect("failed to verify just signed signature");
}
#[test]
fn verify_pgp_crate() {
use ::pgp::types::{PublicKeyTrait, SecretKeyTrait};
let (signer, verifier) = prep();
let (signing_key, verification_key) = { (signer.secret_key, verifier.public_key) };
let passwd_fn = || String::new();
let digest = &RPM_SHA2_256[..];
let signature = signing_key
.create_signature(passwd_fn, ::pgp::crypto::HashAlgorithm::SHA2_256, digest)
.expect("Failed to crate signature");
verification_key
.verify_signature(::pgp::crypto::HashAlgorithm::SHA2_256, digest, &signature)
.expect("Failed to validate signature");
let wrapped = ::pgp::Signature::new(
::pgp::types::Version::Old,
::pgp::packet::SignatureVersion::V4,
::pgp::packet::SignatureType::Binary,
::pgp::crypto::public_key::PublicKeyAlgorithm::RSA,
::pgp::crypto::hash::HashAlgorithm::SHA2_256,
[digest[0], digest[1]],
signature,
vec![
::pgp::packet::Subpacket::SignatureCreationTime(now()),
::pgp::packet::Subpacket::Issuer(signing_key.key_id()),
],
vec![],
);
let mut x = Vec::with_capacity(1024);
let mut buff = Cursor::new(&mut x);
::pgp::packet::write_packet(&mut buff, &wrapped).expect("Write should be ok");
log::debug!("{:02x?}", &x[0..15]);
let signature =
Verifier::parse_signature(x.as_slice()).expect("There is a signature for sure");
assert_eq!(signature, wrapped);
let signature = signature.signature;
verification_key
.verify_signature(::pgp::crypto::HashAlgorithm::SHA2_256, digest, &signature)
.expect("Verify must succeed");
}
#[test]
fn verify_pgp_crate2() {
let (signer, verifier) = prep();
let data = [1u8; 322];
let data = &data[..];
let passwd_fn = || String::new();
let now = now();
let sig_cfg = SignatureConfig {
version: SignatureVersion::V4,
typ: SignatureType::Binary,
pub_alg: ::pgp::crypto::public_key::PublicKeyAlgorithm::RSA,
hash_alg: ::pgp::crypto::hash::HashAlgorithm::SHA2_256,
issuer: Some(signer.secret_key.key_id()),
created: Some(now),
unhashed_subpackets: vec![],
hashed_subpackets: vec![
Subpacket::SignatureCreationTime(now),
Subpacket::Issuer(signer.secret_key.key_id()),
],
};
let signature_packet = sig_cfg
.sign(&signer.secret_key, passwd_fn, data)
.expect("Should sign");
signature_packet
.verify(&verifier.public_key, data)
.expect("Failed to validate signature");
}
#[test]
fn verify_subkeys_match() {
use std::collections::HashSet;
let (signer, verifier) = prep();
let subkey_set = verifier.public_key.public_subkeys.iter().fold(
HashSet::with_capacity(signer.secret_key.public_subkeys.len()),
|mut acc, public_subkey| {
log::debug!("public subkeys in public key: {:?}", public_subkey.key_id());
acc.insert(public_subkey.key_id().as_ref().to_vec());
acc
},
);
signer
.secret_key
.secret_subkeys
.iter()
.for_each(|public_subkey| {
log::debug!("secret subkeys in secret key: {:?}", public_subkey.key_id());
assert!(subkey_set.contains(public_subkey.key_id().as_ref()));
});
}
#[test]
fn static_parse_rpm_sign_signature() {
let _ = env_logger::try_init();
std::fs::write(
std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("target")
.join("some.sig"),
&RPM_SIGN_SIGNATURE[..],
)
.expect("Should be able to dump extracted signature");
let signature = &RPM_SIGN_SIGNATURE[..];
let _signature = Verifier::parse_signature(signature).expect("It should load");
}
const RPM_SHA2_256: [u8; 32] = [
0xd9, 0x2b, 0xfe, 0x27, 0x6e, 0x31, 0x1a, 0x67, 0xfe, 0x12, 0x87, 0x68, 0xc5, 0xdf, 0x4d,
0x06, 0xfd, 0x46, 0x1e, 0x04, 0x3a, 0xfd, 0xf8, 0x72, 0xba, 0x4c, 0x67, 0x9d, 0x86, 0x0d,
0xb8, 0x1e,
];
const RPM_SIGN_SIGNATURE: [u8; 536] = [
0x89, 0x2, 0x15, 0x3, 0x5, 0x0, 0x5b, 0xe9, 0x8c, 0x5b, 0x24, 0xc6, 0xa8, 0xa7, 0xf4, 0xa8,
0xe, 0xb5, 0x1, 0x8, 0xa8, 0x4c, 0xf, 0xfd, 0x1a, 0x9d, 0xe3, 0xf, 0x7e, 0xbb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