Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Error Send + Sync (fixes #191) #198

Merged
merged 3 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

### Changed

- `Error` now implements `Send + Sync`.
udoprog marked this conversation as resolved.
Show resolved Hide resolved
udoprog marked this conversation as resolved.
Show resolved Hide resolved

## 0.13.0

### Breaking Changes
Expand Down
33 changes: 25 additions & 8 deletions src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::io;
use std::{io, str::Utf8Error};

use thiserror::Error;

Expand Down Expand Up @@ -56,18 +56,30 @@ pub enum Error {
#[error("signature packet not found in what is supposed to be a signature")]
NoSignatureFound,

#[cfg(feature = "signature-pgp")]
#[error("error creating signature: {0}")]
SignError(Box<dyn std::error::Error>),
SignError(#[source] pgp::errors::Error),

#[error("error parsing key - {details}. underlying error was: {source}")]
KeyLoadError {
source: Box<dyn std::error::Error>,
details: &'static str,
},
#[error("error parsing keys, failed to parse bytes as utf8 for ascii armored parsing")]
KeyLoadUtf8Error(
#[from]
#[source]
Utf8Error,
),

#[cfg(feature = "signature-pgp")]
#[error("errors parsing keys, failed to parse bytes as ascii armored key")]
KeyLoadSecretKeyError(
#[from]
#[source]
pgp::errors::Error,
),

#[cfg(feature = "signature-pgp")]
#[error("error verifying signature with key {key_ref}: {source}")]
VerificationError {
source: Box<dyn std::error::Error>,
#[source]
source: pgp::errors::Error,
key_ref: String,
},

Expand Down Expand Up @@ -110,3 +122,8 @@ impl From<TimestampError> for Error {
Error::TimestampConv(error)
}
}

udoprog marked this conversation as resolved.
Show resolved Hide resolved
const _: () = {
const fn assert_send_sync<T: Send + Sync>() {}
assert_send_sync::<Error>();
};
udoprog marked this conversation as resolved.
Show resolved Hide resolved
37 changes: 12 additions & 25 deletions src/rpm/signature/pgp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,11 @@ impl traits::Signing for Signer {
let passwd_fn = || self.key_passphrase.clone().unwrap_or_default();
let signature_packet = sig_cfg
.sign(&self.secret_key, passwd_fn, data)
.map_err(|e| Error::SignError(Box::new(e)))?;
.map_err(Error::SignError)?;

let mut signature_bytes = Vec::with_capacity(1024);
let mut cursor = io::Cursor::new(&mut signature_bytes);
pgp::packet::write_packet(&mut cursor, &signature_packet)
.map_err(|e| Error::SignError(Box::new(e)))?;
pgp::packet::write_packet(&mut cursor, &signature_packet).map_err(Error::SignError)?;

Ok(signature_bytes)
}
Expand All @@ -82,19 +81,13 @@ impl Signer {
/// load the private key for signing
pub fn load_from_asc_bytes(input: &[u8]) -> Result<Self, Error> {
// only asc loading is supported right now
let input = std::str::from_utf8(input).map_err(|e| Error::KeyLoadError {
source: Box::new(e),
details: "Failed to parse bytes as utf8 for ascii armored parsing",
})?;
let input = std::str::from_utf8(input).map_err(Error::KeyLoadUtf8Error)?;
Self::load_from_asc(input)
}

pub fn load_from_asc(input: &str) -> Result<Self, Error> {
let (secret_key, _) =
SignedSecretKey::from_string(input).map_err(|e| Error::KeyLoadError {
source: Box::new(e),
details: "Failed to parse bytes as ascii armored key",
})?;
SignedSecretKey::from_string(input).map_err(Error::KeyLoadSecretKeyError)?;
match secret_key.algorithm() {
PublicKeyAlgorithm::RSA => Ok(Self {
secret_key,
Expand Down Expand Up @@ -159,9 +152,9 @@ impl traits::Verifying for Verifier {
log::trace!("Signature has issuer ref: {:?}", key_id);

if self.public_key.key_id() == *key_id {
return signature.verify(&self.public_key, data).map_err(|e| {
return signature.verify(&self.public_key, data).map_err(|source| {
Error::VerificationError {
source: Box::new(e),
source,
key_ref: format!("{:?}", key_id),
}
});
Expand Down Expand Up @@ -193,10 +186,10 @@ impl traits::Verifying for Verifier {
);
return Ok(());
}
Err(e) => {
Err(source) => {
log::trace!("Subkey verification failed");
result = Err(Error::VerificationError {
source: Box::new(e),
source,
key_ref: format!("{:?}", sub_key.key_id()),
})
}
Expand All @@ -216,8 +209,8 @@ impl traits::Verifying for Verifier {
);
signature
.verify(&self.public_key, data)
.map_err(|e| Error::VerificationError {
source: Box::new(e),
.map_err(|source| Error::VerificationError {
source,
key_ref: format!("{:?}", self.public_key.key_id()),
})
}
Expand All @@ -231,19 +224,13 @@ impl traits::Verifying for Verifier {
impl Verifier {
pub fn load_from_asc_bytes(input: &[u8]) -> Result<Self, Error> {
// only asc loading is supported right now
let input = std::str::from_utf8(input).map_err(|e| Error::KeyLoadError {
source: Box::new(e),
details: "Failed to parse bytes as utf8 for ascii armored parsing",
})?;
let input = std::str::from_utf8(input).map_err(Error::KeyLoadUtf8Error)?;
Self::load_from_asc(input)
}

pub fn load_from_asc(input: &str) -> Result<Self, Error> {
let (public_key, _) =
SignedPublicKey::from_string(input).map_err(|e| Error::KeyLoadError {
source: Box::new(e),
details: "Failed to parse bytes as ascii armored key",
})?;
SignedPublicKey::from_string(input).map_err(Error::KeyLoadSecretKeyError)?;

match public_key.algorithm() {
PublicKeyAlgorithm::RSA => Ok(Self {
Expand Down