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