countersign-native 0.1.0

Native certificate selection and signing for the Countersign desktop Companion
Documentation
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
//! CurrentUser/My discovery and CNG-only signing. CryptoAPI legacy CSP keys
//! are excluded because their custody and UI contracts differ from CNG.
use crate::{Identity, NativeResult, eligible};
use countersign_protocol::wire::SignatureScheme;
use sha2::{Digest, Sha256, Sha384, Sha512};
use std::{ffi::c_void, ptr};
use windows_sys::Win32::Security::Cryptography::*;
use x509_parser::prelude::*;

fn wide(value: &str) -> Vec<u16> {
    value.encode_utf16().chain(Some(0)).collect()
}
fn check(value: i32, label: &str) -> NativeResult<()> {
    if value != 0 {
        Err(format!("{label} failed ({value:#x})"))
    } else {
        Ok(())
    }
}
struct Handle(usize);
impl Drop for Handle {
    fn drop(&mut self) {
        if self.0 != 0 {
            unsafe {
                NCryptFreeObject(self.0);
            }
        }
    }
}
fn provider() -> NativeResult<Handle> {
    let mut provider = 0;
    check(
        unsafe { NCryptOpenStorageProvider(&mut provider, MS_KEY_STORAGE_PROVIDER, 0) },
        "CNG provider",
    )?;
    Ok(Handle(provider))
}
fn pairing(locator: &str) -> NativeResult<Handle> {
    let provider = provider()?;
    let mut key = 0;
    check(
        unsafe { NCryptOpenKey(provider.0, &mut key, wide(locator).as_ptr(), 0, 0) },
        "reopen pairing credential",
    )?;
    let key = Handle(key);
    check_protection(key.0)?;
    Ok(key)
}
fn check_protection(key: usize) -> NativeResult<()> {
    let mut policy = [0u8; 4];
    let mut len = 0;
    check(
        unsafe {
            NCryptGetProperty(
                key,
                NCRYPT_EXPORT_POLICY_PROPERTY,
                policy.as_mut_ptr(),
                4,
                &mut len,
                0,
            )
        },
        "verify pairing key export policy",
    )?;
    if len != 4 || u32::from_le_bytes(policy) != 0 {
        return Err("CNG pairing key allows export".into());
    }
    Ok(())
}
fn public(key: usize) -> NativeResult<Vec<u8>> {
    // A P-256 BCRYPT_ECCPUBLIC_BLOB contains an 8-byte header and the 64 public point bytes.
    let mut buf = [0u8; 72];
    let mut len = 0;
    check(
        unsafe {
            NCryptExportKey(
                key,
                0,
                BCRYPT_ECCPUBLIC_BLOB,
                ptr::null(),
                buf.as_mut_ptr(),
                buf.len() as u32,
                &mut len,
                0,
            )
        },
        "read public key",
    )?;
    if len != 72 || buf[4..8] != 32u32.to_le_bytes() {
        return Err("invalid pairing public key".into());
    }
    let sec1 = [&[4u8][..], &buf[8..]].concat();
    p256::PublicKey::from_sec1_bytes(&sec1).map_err(|_| "invalid P-256 point")?;
    Ok(sec1)
}
pub(crate) fn create_pairing(locator: &str) -> NativeResult<Vec<u8>> {
    let provider = provider()?;
    let mut handle = 0;
    check(
        unsafe {
            NCryptCreatePersistedKey(
                provider.0,
                &mut handle,
                BCRYPT_ECDSA_P256_ALGORITHM,
                wide(locator).as_ptr(),
                0,
                0,
            )
        },
        "create pairing credential",
    )?;
    let key = Handle(handle);
    check(
        unsafe {
            NCryptSetProperty(
                key.0,
                NCRYPT_EXPORT_POLICY_PROPERTY,
                0u32.to_le_bytes().as_ptr(),
                4,
                0,
            )
        },
        "protect key",
    )?;
    check(
        unsafe {
            NCryptSetProperty(
                key.0,
                NCRYPT_KEY_USAGE_PROPERTY,
                NCRYPT_ALLOW_SIGNING_FLAG.to_le_bytes().as_ptr(),
                4,
                0,
            )
        },
        "set signing usage",
    )?;
    check(
        unsafe { NCryptFinalizeKey(key.0, 0) },
        "persist protected credential",
    )?;
    if let Err(err) = check_protection(key.0) {
        // Only delete the newly generated app-owned object.
        let mut key = key;
        if unsafe { NCryptDeleteKey(key.0, 0) } == 0 {
            key.0 = 0;
        }
        return Err(err);
    }
    public(key.0)
}
pub(crate) fn pairing_public(locator: &str) -> NativeResult<Vec<u8>> {
    public(pairing(locator)?.0)
}
pub(crate) fn retire_pairing(locator: &str) -> NativeResult<()> {
    let mut key = pairing(locator)?;
    check(unsafe { NCryptDeleteKey(key.0, 0) }, "retire pairing key")?;
    key.0 = 0;
    Ok(())
}
fn sign(key: usize, scheme: SignatureScheme, msg: &[u8]) -> NativeResult<Vec<u8>> {
    use SignatureScheme::*;
    let (hash, alg) = match scheme {
        EcdsaNistp256Sha256 | RsaPssSha256 | RsaPkcs1Sha256 => {
            (Sha256::digest(msg).to_vec(), BCRYPT_SHA256_ALGORITHM)
        }
        EcdsaNistp384Sha384 | RsaPssSha384 | RsaPkcs1Sha384 => {
            (Sha384::digest(msg).to_vec(), BCRYPT_SHA384_ALGORITHM)
        }
        RsaPssSha512 | RsaPkcs1Sha512 => (Sha512::digest(msg).to_vec(), BCRYPT_SHA512_ALGORITHM),
    };
    let pss = BCRYPT_PSS_PADDING_INFO {
        pszAlgId: alg,
        cbSalt: hash.len() as u32,
    };
    let pkcs = BCRYPT_PKCS1_PADDING_INFO { pszAlgId: alg };
    let (padding, flags) = match scheme {
        RsaPssSha256 | RsaPssSha384 | RsaPssSha512 => (
            (&pss as *const BCRYPT_PSS_PADDING_INFO).cast::<c_void>(),
            NCRYPT_PAD_PSS_FLAG,
        ),
        RsaPkcs1Sha256 | RsaPkcs1Sha384 | RsaPkcs1Sha512 => (
            (&pkcs as *const BCRYPT_PKCS1_PADDING_INFO).cast::<c_void>(),
            NCRYPT_PAD_PKCS1_FLAG,
        ),
        _ => (ptr::null(), 0),
    };
    let mut buf = vec![0; 2048];
    let mut len = 0;
    check(
        unsafe {
            NCryptSignHash(
                key,
                padding,
                hash.as_ptr(),
                hash.len() as u32,
                buf.as_mut_ptr(),
                buf.len() as u32,
                &mut len,
                flags,
            )
        },
        "native signing",
    )?;
    buf.truncate(len as usize);
    Ok(buf)
}
pub(crate) fn pairing_sign(locator: &str, msg: &[u8]) -> NativeResult<Vec<u8>> {
    sign(
        pairing(locator)?.0,
        SignatureScheme::EcdsaNistp256Sha256,
        msg,
    )
}

struct Store(HCERTSTORE);
impl Drop for Store {
    fn drop(&mut self) {
        unsafe {
            CertCloseStore(self.0, 0);
        }
    }
}
struct Cert(*const CERT_CONTEXT);
impl Drop for Cert {
    fn drop(&mut self) {
        unsafe {
            CertFreeCertificateContext(self.0);
        }
    }
}
fn certs() -> NativeResult<Vec<Cert>> {
    let store = Store(unsafe { CertOpenSystemStoreW(0, wide("MY").as_ptr()) });
    if store.0.is_null() {
        return Err("open CurrentUser/My".into());
    }
    let mut result = vec![];
    unsafe {
        let mut cert = ptr::null_mut();
        loop {
            // Each enumeration frees its previous pointer. Only the duplicated
            // references leave the loop and their RAII guards free them.
            cert = CertEnumCertificatesInStore(store.0, cert);
            if cert.is_null() {
                break;
            }
            let copy = CertDuplicateCertificateContext(cert);
            if !copy.is_null() {
                result.push(Cert(copy))
            }
        }
    }
    Ok(result)
}
fn der(cert: *const CERT_CONTEXT) -> NativeResult<Vec<u8>> {
    if cert.is_null() {
        return Err("null certificate".into());
    }
    unsafe {
        if (*cert).pbCertEncoded.is_null()
            || (*cert).cbCertEncoded == 0
            || (*cert).cbCertEncoded > 262144
        {
            return Err("invalid certificate size".into());
        }
        Ok(
            std::slice::from_raw_parts((*cert).pbCertEncoded, (*cert).cbCertEncoded as usize)
                .to_vec(),
        )
    }
}

fn acquire<T>(
    cert: &Cert,
    silent: bool,
    work: impl FnOnce(usize) -> NativeResult<T>,
) -> NativeResult<T> {
    let mut key = 0;
    let mut spec = 0;
    let mut free = 0;
    let flags =
        CRYPT_ACQUIRE_ONLY_NCRYPT_KEY_FLAG | if silent { CRYPT_ACQUIRE_SILENT_FLAG } else { 0 };
    if unsafe {
        CryptAcquireCertificatePrivateKey(
            cert.0,
            flags,
            ptr::null(),
            &mut key,
            &mut spec,
            &mut free,
        )
    } == 0
    {
        return Err("native key access denied".into());
    }
    let _owned = if free != 0 { Some(Handle(key)) } else { None };
    if spec != CERT_NCRYPT_KEY_SPEC {
        return Err("unsupported key provider".into());
    }
    work(key)
}
fn chain(cert: &Cert) -> NativeResult<Vec<Vec<u8>>> {
    let params = CERT_CHAIN_PARA {
        cbSize: std::mem::size_of::<CERT_CHAIN_PARA>() as u32,
        ..Default::default()
    };
    let mut ctx = ptr::null_mut();
    unsafe {
        if CertGetCertificateChain(
            ptr::null_mut(),
            cert.0,
            ptr::null(),
            (*cert.0).hCertStore,
            &params,
            CERT_CHAIN_CACHE_ONLY_URL_RETRIEVAL | CERT_CHAIN_DISABLE_AIA,
            ptr::null(),
            &mut ctx,
        ) == 0
        {
            return Err("build installed certificate chain".into());
        }
        let result = (|| {
            if (*ctx).cChain == 0 {
                return Err("missing certificate chain".into());
            }
            let simple = *(*ctx).rgpChain;
            if (*simple).cElement > 16 {
                return Err("chain exceeds limit".into());
            }
            let mut chain = Vec::new();
            for i in 0..(*simple).cElement as usize {
                let cert = der((**(*simple).rgpElement.add(i)).pCertContext)?;
                if i > 0
                    && X509Certificate::from_der(&cert)
                        .is_ok_and(|(_, c)| c.subject() == c.issuer())
                {
                    break;
                }
                chain.push(cert)
            }
            Ok(chain)
        })();
        CertFreeCertificateChain(ctx);
        result
    }
}
pub(crate) fn identities() -> NativeResult<Vec<Identity>> {
    let mut result = Vec::new();
    for cert in certs()? {
        let leaf = der(cert.0)?;
        if eligible(&leaf).is_err() {
            continue;
        }
        // Inspect CNG key metadata without signing or opening a PIN dialog.
        let mut bytes = 0;
        if unsafe {
            CertGetCertificateContextProperty(
                cert.0,
                CERT_KEY_PROV_INFO_PROP_ID,
                ptr::null_mut(),
                &mut bytes,
            )
        } == 0
            || bytes == 0
        {
            continue;
        }
        let mut info = vec![0usize; (bytes as usize).div_ceil(std::mem::size_of::<usize>())];
        if unsafe {
            CertGetCertificateContextProperty(
                cert.0,
                CERT_KEY_PROV_INFO_PROP_ID,
                info.as_mut_ptr().cast(),
                &mut bytes,
            )
        } == 0
            || (bytes as usize) < std::mem::size_of::<CRYPT_KEY_PROV_INFO>()
        {
            continue;
        }
        if unsafe { (*info.as_ptr().cast::<CRYPT_KEY_PROV_INFO>()).dwProvType } != 0 {
            continue;
        }
        result.push(Identity {
            key: Sha256::digest(&leaf).to_vec(),
            chain: chain(&cert)?,
        })
    }
    Ok(result)
}
pub(crate) fn sign_identity(
    identity: &Identity,
    scheme: SignatureScheme,
    msg: &[u8],
) -> NativeResult<Vec<u8>> {
    let cert = certs()?
        .into_iter()
        .find(|c| {
            der(c.0).is_ok_and(|der| {
                der == identity.chain[0] && Sha256::digest(&der).to_vec() == identity.key
            })
        })
        .ok_or("selected certificate removed")?;
    let raw = acquire(&cert, false, |key| sign(key, scheme, msg))?;
    match scheme {
        SignatureScheme::EcdsaNistp256Sha256 => p256::ecdsa::Signature::from_slice(&raw)
            .map(|s| s.to_der().as_bytes().to_vec())
            .map_err(|_| "invalid P-256 signature".into()),
        SignatureScheme::EcdsaNistp384Sha384 => p384::ecdsa::Signature::from_slice(&raw)
            .map(|s| s.to_der().as_bytes().to_vec())
            .map_err(|_| "invalid P-384 signature".into()),
        _ => Ok(raw),
    }
}