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 250 : fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
19 250 : f.debug_struct("SignerInfo")
20 250 : .field("version", &self.version())
21 250 : .field("issuer", &self.issuer())
22 250 : .field("digest_algorithm", &self.digest_algorithm())
23 250 : .field("encryption_algorithm", &self.encryption_algorithm())
24 250 : .finish()
25 250 : }
26 : }
27 :
28 : impl<'a> FromFFI<ffi::PE_SignerInfo> for SignerInfo<'a> {
29 300 : fn from_ffi(ptr: cxx::UniquePtr<ffi::PE_SignerInfo>) -> Self {
30 300 : SignerInfo {
31 300 : ptr,
32 300 : _owner: PhantomData,
33 300 : }
34 300 : }
35 : }
36 :
37 : impl<'a> SignerInfo<'a> {
38 : /// Should be 1
39 250 : pub fn version(&self) -> u32 {
40 250 : self.ptr.version()
41 250 : }
42 :
43 : /// Return the [`X509::issuer`] used by this signer
44 250 : pub fn issuer(&self) -> String {
45 250 : self.ptr.issuer().to_string()
46 250 : }
47 :
48 : /// Algorithm used to hash the file.
49 250 : pub fn digest_algorithm(&self) -> Algorithms {
50 250 : Algorithms::from(self.ptr.digest_algorithm())
51 250 : }
52 :
53 : /// Return the (public-key) algorithm used to encrypt the signature
54 250 : pub fn encryption_algorithm(&self) -> Algorithms {
55 250 : Algorithms::from(self.ptr.encryption_algorithm())
56 250 : }
57 :
58 : /// Return the serial number associated with the x509 certificate
59 : /// used by this signer.
60 250 : pub fn serial_number(&self) -> &[u8] {
61 250 : to_slice!(self.ptr.serial_number());
62 250 : }
63 :
64 : /// Return the signature created by the signing certificate's private key
65 250 : pub fn encrypted_digest(&self) -> Vec<u8> {
66 250 : Vec::from(self.ptr.encrypted_digest().as_slice())
67 250 : }
68 :
69 : /// [`X509`] certificate used by this signer.
70 250 : pub fn cert(&self) -> Option<X509> {
71 250 : into_optional(self.ptr.cert())
72 250 : }
73 :
74 : /// Iterator over the **authenticated** [`Attribute`]
75 300 : pub fn authenticated_attributes(&self) -> AuthenticatedAttributes {
76 300 : AuthenticatedAttributes::new(self.ptr.authenticated_attributes())
77 300 : }
78 :
79 : /// Iterator over the **unauthenticated** [`Attribute`]
80 300 : pub fn unauthenticated_attributes(&self) -> UnAuthenticatedAttributes {
81 300 : UnAuthenticatedAttributes::new(self.ptr.unauthenticated_attributes())
82 300 : }
83 :
84 : /// Raw blob that is signed by the signer certificate
85 50 : pub fn raw_auth_data(&self) -> &[u8] {
86 50 : to_slice!(self.ptr.raw_auth_data());
87 50 : }
88 : }
89 :
90 160 : declare_iterator!(
91 160 : Signers,
92 160 : SignerInfo<'a>,
93 160 : ffi::PE_SignerInfo,
94 160 : ffi::PE_Signature,
95 160 : ffi::PE_Signature_it_signers
96 160 : );
97 1110 : declare_iterator!(
98 1110 : AuthenticatedAttributes,
99 1110 : Attribute<'a>,
100 1110 : ffi::PE_Attribute,
101 1110 : ffi::PE_SignerInfo,
102 1110 : ffi::PE_SignerInfo_it_authenticated_attributes
103 1110 : );
104 170 : declare_iterator!(
105 170 : UnAuthenticatedAttributes,
106 170 : Attribute<'a>,
107 170 : ffi::PE_Attribute,
108 170 : ffi::PE_SignerInfo,
109 170 : ffi::PE_SignerInfo_it_unauthenticated_attributes
110 170 : );
|