Line data Source code
1 : use std::marker::PhantomData;
2 :
3 : use super::attributes::Attribute;
4 : use super::X509;
5 : use crate::pe::Algorithms;
6 : use crate::to_slice;
7 : use crate::common::{FromFFI, into_optional};
8 : use crate::declare_iterator;
9 : use lief_ffi as ffi;
10 :
11 : /// SignerInfo as described in the [RFC 2315](https://tools.ietf.org/html/rfc2315#section-9.2)
12 : pub struct SignerInfo<'a> {
13 : ptr: cxx::UniquePtr<ffi::PE_SignerInfo>,
14 : _owner: PhantomData<&'a ()>, // Can be own by Signature or PKCS9CounterSignature
15 : }
16 :
17 : impl std::fmt::Debug for SignerInfo<'_> {
18 152 : fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
19 152 : f.debug_struct("SignerInfo")
20 152 : .field("version", &self.version())
21 152 : .field("issuer", &self.issuer())
22 152 : .field("digest_algorithm", &self.digest_algorithm())
23 152 : .field("encryption_algorithm", &self.encryption_algorithm())
24 152 : .finish()
25 152 : }
26 : }
27 :
28 : impl<'a> FromFFI<ffi::PE_SignerInfo> for SignerInfo<'a> {
29 192 : fn from_ffi(ptr: cxx::UniquePtr<ffi::PE_SignerInfo>) -> Self {
30 192 : SignerInfo {
31 192 : ptr,
32 192 : _owner: PhantomData,
33 192 : }
34 192 : }
35 : }
36 :
37 : impl<'a> SignerInfo<'a> {
38 : /// Should be 1
39 152 : pub fn version(&self) -> u32 {
40 152 : self.ptr.version()
41 152 : }
42 :
43 : /// Return the [`X509::issuer`] used by this signer
44 152 : pub fn issuer(&self) -> String {
45 152 : self.ptr.issuer().to_string()
46 152 : }
47 :
48 : /// Algorithm used to hash the file.
49 152 : pub fn digest_algorithm(&self) -> Algorithms {
50 152 : Algorithms::from(self.ptr.digest_algorithm())
51 152 : }
52 :
53 : /// Return the (public-key) algorithm used to encrypt the signature
54 152 : pub fn encryption_algorithm(&self) -> Algorithms {
55 152 : Algorithms::from(self.ptr.encryption_algorithm())
56 152 : }
57 :
58 : /// Return the serial number associated with the x509 certificate
59 : /// used by this signer.
60 152 : pub fn serial_number(&self) -> &[u8] {
61 152 : to_slice!(self.ptr.serial_number());
62 152 : }
63 :
64 : /// Return the signature created by the signing certificate's private key
65 152 : pub fn encrypted_digest(&self) -> Vec<u8> {
66 152 : Vec::from(self.ptr.encrypted_digest().as_slice())
67 152 : }
68 :
69 : /// [`X509`] certificate used by this signer.
70 152 : pub fn cert(&self) -> Option<X509> {
71 152 : into_optional(self.ptr.cert())
72 152 : }
73 :
74 : /// Iterator over the **authenticated** [`Attribute`]
75 192 : pub fn authenticated_attributes(&self) -> AuthenticatedAttributes {
76 192 : AuthenticatedAttributes::new(self.ptr.authenticated_attributes())
77 192 : }
78 :
79 : /// Iterator over the **unauthenticated** [`Attribute`]
80 192 : pub fn unauthenticated_attributes(&self) -> UnAuthenticatedAttributes {
81 192 : UnAuthenticatedAttributes::new(self.ptr.unauthenticated_attributes())
82 192 : }
83 :
84 : /// Raw blob that is signed by the signer certificate
85 40 : pub fn raw_auth_data(&self) -> &[u8] {
86 40 : to_slice!(self.ptr.raw_auth_data());
87 40 : }
88 : }
89 :
90 104 : declare_iterator!(
91 104 : Signers,
92 104 : SignerInfo<'a>,
93 104 : ffi::PE_SignerInfo,
94 104 : ffi::PE_Signature,
95 104 : ffi::PE_Signature_it_signers
96 104 : );
97 704 : declare_iterator!(
98 704 : AuthenticatedAttributes,
99 704 : Attribute<'a>,
100 704 : ffi::PE_Attribute,
101 704 : ffi::PE_SignerInfo,
102 704 : ffi::PE_SignerInfo_it_authenticated_attributes
103 704 : );
104 112 : declare_iterator!(
105 112 : UnAuthenticatedAttributes,
106 112 : Attribute<'a>,
107 112 : ffi::PE_Attribute,
108 112 : ffi::PE_SignerInfo,
109 112 : ffi::PE_SignerInfo_it_unauthenticated_attributes
110 112 : );
|