Line data Source code
1 : use lief_ffi as ffi;
2 : use num_bigint::BigUint;
3 :
4 : use crate::common::FromFFI;
5 : use std::marker::PhantomData;
6 :
7 : /// Structure that wraps an RSA key
8 : pub struct RsaInfo<'a> {
9 : ptr: cxx::UniquePtr<ffi::PE_RsaInfo>,
10 : _owner: PhantomData<&'a ()>,
11 : }
12 :
13 : impl std::fmt::Debug for RsaInfo<'_> {
14 816 : fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
15 816 : f.debug_struct("RsaInfo")
16 816 : .field("key_size", &self.key_size())
17 816 : .field("has_public_key", &self.has_public_key())
18 816 : .field("has_private_key", &self.has_private_key())
19 816 : .finish()
20 816 : }
21 : }
22 :
23 : impl<'a> FromFFI<ffi::PE_RsaInfo> for RsaInfo<'a> {
24 816 : fn from_ffi(ptr: cxx::UniquePtr<ffi::PE_RsaInfo>) -> Self {
25 816 : RsaInfo {
26 816 : ptr,
27 816 : _owner: PhantomData,
28 816 : }
29 816 : }
30 : }
31 :
32 : impl<'a> RsaInfo<'a> {
33 : #[allow(non_snake_case)]
34 : /// RSA public modulus
35 816 : pub fn N(&self) -> BigUint {
36 816 : BigUint::from_bytes_le(self.ptr.N().as_slice())
37 816 : }
38 :
39 : /// RSA public exponent
40 : #[allow(non_snake_case)]
41 816 : pub fn E(&self) -> BigUint {
42 816 : BigUint::from_bytes_le(self.ptr.E().as_slice())
43 816 : }
44 :
45 : #[allow(non_snake_case)]
46 : /// RSA private exponent
47 816 : pub fn D(&self) -> BigUint {
48 816 : BigUint::from_bytes_le(self.ptr.D().as_slice())
49 816 : }
50 :
51 : #[allow(non_snake_case)]
52 : /// First prime factor
53 816 : pub fn P(&self) -> BigUint {
54 816 : BigUint::from_bytes_le(self.ptr.P().as_slice())
55 816 : }
56 : #[allow(non_snake_case)]
57 : /// Second prime factor
58 816 : pub fn Q(&self) -> BigUint {
59 816 : BigUint::from_bytes_le(self.ptr.Q().as_slice())
60 816 : }
61 :
62 : /// Size of the public modulus (in bits)
63 816 : pub fn key_size(&self) -> u32 {
64 816 : self.ptr.key_size()
65 816 : }
66 :
67 : /// True if it embeds a public key
68 816 : pub fn has_public_key(&self) -> bool {
69 816 : self.ptr.has_public_key()
70 816 : }
71 :
72 : /// True if it embeds a private key
73 816 : pub fn has_private_key(&self) -> bool {
74 816 : self.ptr.has_private_key()
75 816 : }
76 : }
|