I'm trying to convert a Python lib to a Rust one. In this process I need to extract x509 certs from a pkcs7 (or at least the oids).
In Python I used the lib Cryptography:
from typing import List
import base64
from cryptography import x509
from cryptography.hazmat.primitives.serialization import pkcs7
my_sig: str = "something"
signature_decoded: bytes = base64.b64decode(my_sig)
pkcs_certs: List[x509.Certificate] = pkcs7.load_der_pkcs7_certificates(signature_decoded)
Here my_sig is a detached PKCS #7 signature, Base64 encoded as string.
To do my Rust equivalent I tried to use rust crypto. But from what I saw the pkcs7 format is only a struct and don't implement the loading part.
That said I assume I need to implement a trait from the lib that loads my u8 pkcs7 as a pkcs7 struct but I can't find any simple example.
What I have for now:
asn1_extractor.rs
use der::{
asn1::{Any, ObjectIdentifier},
Decodable, Decoder, Encodable, Sequence
};
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub struct AlgorithmIdentifier<'a> {
pub algorithm: ObjectIdentifier,
pub parameters: Option<Any<'a>>
}
impl<'a> Decodable<'a> for AlgorithmIdentifier<'a> {
fn decode(decoder: &mut Decoder<'a>) -> der::Result<Self> {
decoder.sequence(|decoder| {
let algorithm = decoder.decode()?;
let parameters = decoder.decode()?;
Ok(Self { algorithm, parameters })
})
}
}
impl<'a> Sequence<'a> for AlgorithmIdentifier<'a> {
fn fields<F, T>(&self, field_encoder: F) -> der::Result<T>
where
F: FnOnce(&[&dyn Encodable]) -> der::Result<T>,
{
field_encoder(&[&self.algorithm, &self.parameters])
}
}
lib.rs
extern crate core;
use pkcs7;
use der::{Decodable, ValueOrd, Decoder, Sequence};
pub mod asn1_extractor;
pub fn pkcs7_oid_verifier(signature: String) -> () {
let bytes = base64::decode(signature).unwrap();
let mut decoder = Decoder::new(&bytes).expect("bruh it doesn work :(");
let asn1 = asn1_extractor::AlgorithmIdentifier::decode(&mut decoder);
// ===> here I want to have a pkcs7 struct as result of the fn
}