1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#[macro_use]
extern crate log;

mod winapi;

extern crate widestring;

use std::ptr::{null_mut, null, read};
use widestring::WideCString;
use winapi::*;

#[derive(Clone,Default,Debug)]
pub struct CodeSigned {
    pub path: String,
    pub signed: Option<bool>,
    pub catalog: bool,
    pub issuer_name: String,
    pub subject_name: String,
    pub timestamp_issuer_name: String,
    pub timestamp_subject_name: String,
    pub serial_number: String,
}

impl CodeSigned {
    pub fn file(&mut self, path: &str) {
        self.path = path.to_owned();
        if let Ok(path) = WideCString::from_str(path) {
            let mut file_info = WinTrustFileInfo::from_path(&path);
            let mut win_trust_data = WinTrustData::default();
            win_trust_data.data = &mut file_info;

            let action = Guid::wintrust_action_generic_verify_v2();
            match unsafe {
                WinVerifyTrust(null(), &action, &win_trust_data)
            } {
                0 => self.embedded(&path),
                _ => self.catalog(&path)
            }

            win_trust_data.state_action = WTD_STATEACTION_CLOSE;
            unsafe { WinVerifyTrust(null(), &action, &win_trust_data); }
        }
    }

    fn embedded(&mut self, path: &WideCString) {
        let mut encoding: u32 = 0;
        let mut content_type: u32 = 0;
        let mut format_type: u32 = 0;
        let mut h_store: *mut u8 = null_mut();
        let mut h_msg: *mut u8 = null_mut();


        match unsafe {
            CryptQueryObject(
                CERT_QUERY_OBJECT_FILE,
                path.as_ptr() as *const _,
                CERT_QUERY_CONTENT_FLAG_PKCS7_SIGNED_EMBED,
                CERT_QUERY_FORMAT_FLAG_BINARY,
                0,
                &mut encoding,
                &mut content_type,
                &mut format_type,
                &mut h_store,
                &mut h_msg,
                null_mut()
            )
        } {
            0 => { /* error */ },
            _ => {
                let mut data_len: u32 = 0;
                unsafe {
                    CryptMsgGetParam(
                        h_msg,
                        CMSG_SIGNER_INFO_PARAM,
                        0,
                        null_mut(),
                        &mut data_len
                    );
                }

                let mut data: Vec<u8> = vec![0; data_len as usize];
                match unsafe {
                    CryptMsgGetParam(
                        h_msg,
                        CMSG_SIGNER_INFO_PARAM,
                        0,
                        data.as_mut_ptr(),
                        &mut data_len
                    )
                } {
                    0 => { /* error */ },
                    _ => {
                        let msg: MsgSignerInfo = unsafe {
                            read(data.as_mut_ptr() as *const _)
                        };

                        let mut cert_info = CertInfo::default();
                        cert_info.serial_number.from(&msg.serial_number);
                        cert_info.issuer.from(&msg.issuer);

                        let context = CertStoreContext::new(unsafe {
                            CertFindCertificateInStore(
                                h_store,
                                ENCODING,
                                0,
                                CERT_FIND_SUBJECT_CERT,
                                &cert_info,
                                null()
                            )
                        });

                        let needed: u32 = unsafe {
                            CertGetNameStringA(
                                context.context(),
                                CERT_NAME_SIMPLE_DISPLAY_TYPE,
                                0,
                                null(),
                                null(),
                                0
                            )
                        };
                        let mut subject_name_data: Vec<u8> = vec![0; needed as usize];
                        unsafe {
                            CertGetNameStringA(
                                context.context(),
                                CERT_NAME_SIMPLE_DISPLAY_TYPE,
                                0,
                                null(),
                                subject_name_data.as_mut_ptr(),
                                needed as u32
                            );
                        }

                        self.serial_number = msg.serial_number.to_string();
                        self.issuer_name = msg.issuer.to_string();
                        self.subject_name = String::from_utf8((&subject_name_data[0..needed as usize -1]).to_vec()).unwrap_or("(unknown)".to_owned());
                        self.signed = Some(true);
                        unsafe {
                            CryptMsgClose(h_msg);
                            CertCloseStore(h_store, 2);
                        }
                    }
                }
            }
        }
    }

    fn catalog(&mut self, path: &WideCString) {
        let f = FileHandle::new().with_file(&path);
        if let Some(handle) = f.handle() {
            let mut hash_length: u32 = 0;
            match unsafe {
                CryptCATAdminCalcHashFromFileHandle(
                    handle,
                    &mut hash_length,
                    null_mut(),
                    0
                )
            } {
                0 => warn!("Could not obtain file hash for {}", path.to_string_lossy()),
                _ => {
                    self.signed = Some(false);
                    let mut hash_data: Vec<u8> = vec![0; hash_length as usize];
                    unsafe {
                        CryptCATAdminCalcHashFromFileHandle(
                            handle,
                            &mut hash_length,
                            hash_data.as_mut_ptr(),
                            0
                        );
                    }

                    let driver_action = Guid::driver_action_verify();
                    let mut admin_context: *mut u8 = null_mut();
                    if unsafe { CryptCATAdminAcquireContext(&mut admin_context, &driver_action, 0) } == 0 {
                        /* error? */
                        return;
                    }

                    let mut cat = unsafe { CryptCATAdminEnumCatalogFromHash(admin_context, hash_data.as_ptr(), hash_length, 0, null_mut()) };
                    loop {
                        let mut cat_info = CatalogInfo::default();
                        if 0 == unsafe { CryptCATCatalogInfoFromContext(cat, &mut cat_info, 0) } {
                            /* out of catalogs */
                            break;
                        }
                        let cat_path = unsafe { WideCString::from_ptr_str(&cat_info.catalog_file as *const u16) };
                        self.catalog = true;
                        self.embedded(&cat_path);
                        cat = unsafe { CryptCATAdminEnumCatalogFromHash(admin_context, hash_data.as_ptr(), hash_length, 0, &mut cat) };
                        if cat == null_mut() { break; }
                    }

                    unsafe {
                        CryptCATAdminReleaseCatalogContext(admin_context, cat, 0);
                        CryptCATAdminReleaseContext(admin_context, 0);
                    }
                }
            }
        }
    }
}