schannel/
ctl_context.rs

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
//! Bindings to Certificate Trust Lists (CTL) in winapi.

#![allow(dead_code)]

use std::io;
use std::mem;
use std::ptr;

use windows_sys::Win32::Security::Cryptography;

use crate::cert_context::CertContext;
use crate::Inner;

/// Wrapped `PCCTL_CONTEXT` which represents a certificate trust list to
/// Windows.
pub struct CtlContext(*const Cryptography::CTL_CONTEXT);

unsafe impl Send for CtlContext {}
unsafe impl Sync for CtlContext {}

impl Drop for CtlContext {
    fn drop(&mut self) {
        unsafe {
            Cryptography::CertFreeCTLContext(self.0);
        }
    }
}

impl Inner<*const Cryptography::CTL_CONTEXT> for CtlContext {
    unsafe fn from_inner(t: *const Cryptography::CTL_CONTEXT) -> CtlContext {
        CtlContext(t)
    }

    fn as_inner(&self) -> *const Cryptography::CTL_CONTEXT {
        self.0
    }

    fn get_mut(&mut self) -> &mut *const Cryptography::CTL_CONTEXT {
        &mut self.0
    }
}

impl CtlContext {
    /// Returns a builder reader to create an encoded `CtlContext`.
    pub fn builder() -> Builder {
        Builder {
            certificates: vec![],
            usages: vec![],
        }
    }
}

/// Used to build an encoded `CtlContext` which can be added to a `Memory` store
/// to get back the actual `CtlContext`.
pub struct Builder {
    certificates: Vec<CertContext>,
    usages: Vec<Vec<u8>>,
}

impl Builder {
    /// Adds a certificate to be passed to `CryptMsgEncodeAndSignCTL` later on.
    pub fn certificate(&mut self, cert: CertContext) -> &mut Builder {
        self.certificates.push(cert);
        self
    }

    /// Adds a usage string to be passed in the `SubjectUsage` field to
    /// `CryptMsgEncodeAndSignCTL` later on.
    pub fn usage(&mut self, usage: &str) -> &mut Builder {
        let mut usage = usage.as_bytes().to_owned();
        usage.push(0);
        self.usages.push(usage);
        self
    }

    /// Calls `CryptMsgEncodeAndSignCTL` to encode this list of certificates
    /// into a CTL.
    ///
    /// This can later be passed to `Memory::add_encoded_ctl`.
    pub fn encode_and_sign(&self) -> io::Result<Vec<u8>> {
        unsafe {
            let encoding = Cryptography::X509_ASN_ENCODING | Cryptography::PKCS_7_ASN_ENCODING;

            let mut usages = self
                .usages
                .iter()
                .map(|u| u.as_ptr() as _)
                .collect::<Vec<_>>();
            let mut entry_data = vec![];
            let mut entries = vec![];
            for certificate in &self.certificates {
                let data = cert_entry(certificate)?;
                entries.push(*(data.as_ptr() as *const Cryptography::CTL_ENTRY));
                entry_data.push(data);
            }

            let mut ctl_info: Cryptography::CTL_INFO = mem::zeroed();
            ctl_info.dwVersion = Cryptography::CTL_V1;
            ctl_info.SubjectUsage.cUsageIdentifier = usages.len() as u32;
            ctl_info.SubjectUsage.rgpszUsageIdentifier = usages.as_mut_ptr();
            ctl_info.SubjectAlgorithm.pszObjId = Cryptography::szOID_OIWSEC_sha1 as _;
            ctl_info.cCTLEntry = entries.len() as u32;
            ctl_info.rgCTLEntry = entries.as_mut_ptr();

            let mut sign_info: Cryptography::CMSG_SIGNED_ENCODE_INFO = mem::zeroed();
            sign_info.cbSize = mem::size_of_val(&sign_info) as u32;
            let mut encoded_certs = self
                .certificates
                .iter()
                .map(|c| Cryptography::CRYPT_INTEGER_BLOB {
                    cbData: (*c.as_inner()).cbCertEncoded,
                    pbData: (*c.as_inner()).pbCertEncoded,
                })
                .collect::<Vec<_>>();
            sign_info.rgCertEncoded = encoded_certs.as_mut_ptr();
            sign_info.cCertEncoded = encoded_certs.len() as u32;

            let flags = Cryptography::CMSG_ENCODE_SORTED_CTL_FLAG
                | Cryptography::CMSG_ENCODE_HASHED_SUBJECT_IDENTIFIER_FLAG;

            let mut size = 0;

            let res = Cryptography::CryptMsgEncodeAndSignCTL(
                encoding,
                &ctl_info,
                &sign_info,
                flags,
                ptr::null_mut(),
                &mut size,
            );
            if res == 0 {
                return Err(io::Error::last_os_error());
            }

            let mut encoded = vec![0; size as usize];

            let res = Cryptography::CryptMsgEncodeAndSignCTL(
                encoding,
                &ctl_info,
                &sign_info,
                flags,
                encoded.as_mut_ptr() as *mut u8,
                &mut size,
            );
            if res == 0 {
                return Err(io::Error::last_os_error());
            }

            Ok(encoded)
        }
    }
}

fn cert_entry(cert: &CertContext) -> io::Result<Vec<u8>> {
    unsafe {
        let mut size: u32 = 0;

        let res = Cryptography::CertCreateCTLEntryFromCertificateContextProperties(
            cert.as_inner(),
            0,
            ptr::null(),
            Cryptography::CTL_ENTRY_FROM_PROP_CHAIN_FLAG,
            ptr::null_mut(),
            ptr::null_mut(),
            &mut size,
        );
        if res == 0 {
            return Err(io::Error::last_os_error());
        }

        let mut entry = vec![0u8; size as usize];
        let res = Cryptography::CertCreateCTLEntryFromCertificateContextProperties(
            cert.as_inner(),
            0,
            ptr::null(),
            Cryptography::CTL_ENTRY_FROM_PROP_CHAIN_FLAG,
            ptr::null_mut(),
            entry.as_mut_ptr() as *mut Cryptography::CTL_ENTRY,
            &mut size,
        );
        if res == 0 {
            Err(io::Error::last_os_error())
        } else {
            Ok(entry)
        }
    }
}