-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for parsing detached signatures
Fixes: #27 Signed-off-by: Wiktor Kwapisiewicz <[email protected]>
- Loading branch information
Showing
8 changed files
with
206 additions
and
143 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,16 @@ | ||
# Next version changes | ||
## This file contains changes that will be included in the next version that is released | ||
v0.1.23 | ||
v0.1.24 | ||
|
||
New: | ||
- `decrypt` accepts a function for supplying certificates for signature verification ([#22]) | ||
- the result of `decrypt` and `verify` exposes `valid_sigs` for retrieving a list of valid signatures ([#22]) | ||
- `Sig` - new class exposing signature related functions: | ||
- `Sig.from_file` - read detached signature from file, | ||
- `Sig.from_bytes` - read detached signature from bytes, | ||
- `sig.issuer_fpr` - fingerprint of the issuer (may be `None`), | ||
- `sig.created` - date and time when the signature was issued, | ||
|
||
Changed: | ||
- `verify` accepts a callback for supplying signing certificates ([#20]) | ||
- `encrypt` does not require the `signer` argument ([#22]) | ||
|
||
Removed: | ||
- `Store` and the Cert-D has been removed ([#20]) due to confusing semantics | ||
|
||
[#20]: https://github.com/wiktor-k/pysequoia/pull/20 | ||
[#22]: https://github.com/wiktor-k/pysequoia/pull/22 | ||
### git tag --edit -s -F NEXT.md v... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,86 @@ | ||
use std::borrow::Cow; | ||
|
||
use openpgp::packet::Signature as SqSignature; | ||
use anyhow::anyhow; | ||
use openpgp::{ | ||
packet::Signature as SqSignature, | ||
parse::{PacketParser, PacketParserResult, Parse as _}, | ||
Packet, | ||
}; | ||
use pyo3::prelude::*; | ||
use sequoia_openpgp as openpgp; | ||
|
||
#[pyclass] | ||
pub struct Signature { | ||
pub struct Sig { | ||
sig: SqSignature, | ||
} | ||
|
||
impl Signature { | ||
impl Sig { | ||
pub fn new(sig: SqSignature) -> Self { | ||
Self { sig } | ||
} | ||
|
||
pub fn from_packets(ppr: PacketParserResult<'_>) -> Result<Self, anyhow::Error> { | ||
if let PacketParserResult::Some(pp) = ppr { | ||
let (packet, _next_ppr) = pp.recurse()?; | ||
if let Packet::Signature(sig) = packet { | ||
return Ok(sig.into()); | ||
} | ||
} | ||
Err(anyhow!("Not a signature")) | ||
} | ||
} | ||
|
||
impl From<SqSignature> for Signature { | ||
impl From<SqSignature> for Sig { | ||
fn from(sig: SqSignature) -> Self { | ||
Self { sig } | ||
} | ||
} | ||
|
||
#[pymethods] | ||
impl Signature { | ||
impl Sig { | ||
#[staticmethod] | ||
pub fn from_file(path: String) -> PyResult<Self> { | ||
Ok(Self::from_packets(PacketParser::from_file(path)?)?) | ||
} | ||
|
||
#[staticmethod] | ||
pub fn from_bytes(bytes: &[u8]) -> PyResult<Self> { | ||
Ok(Self::from_packets(PacketParser::from_bytes(bytes)?)?) | ||
} | ||
|
||
pub fn bytes(&self) -> PyResult<Cow<[u8]>> { | ||
Ok(crate::serialize(self.sig.clone().into(), None)?.into()) | ||
} | ||
|
||
#[getter] | ||
pub fn issuer_fpr(&self) -> Option<String> { | ||
self.sig | ||
.issuer_fingerprints() | ||
.next() | ||
.map(|issuer| format!("{:x}", issuer)) | ||
} | ||
|
||
#[getter] | ||
pub fn created(&self) -> Option<chrono::DateTime<chrono::Utc>> { | ||
self.sig.signature_creation_time().map(Into::into) | ||
} | ||
|
||
pub fn __str__(&self) -> PyResult<String> { | ||
let bytes = crate::serialize(self.sig.clone().into(), openpgp::armor::Kind::Signature)?; | ||
Ok(String::from_utf8(bytes)?) | ||
} | ||
|
||
pub fn __repr__(&self) -> String { | ||
format!("<Sig issuer_fpr={}>", self.issuer_fpr().unwrap_or_default()) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_reading_sig() { | ||
Sig::from_packets(PacketParser::from_file("sig.pgp").unwrap()).unwrap(); | ||
} | ||
} |