forked from github/smimesign
-
Notifications
You must be signed in to change notification settings - Fork 1
/
certstore_windows.go
682 lines (550 loc) · 17.1 KB
/
certstore_windows.go
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
package certstore
/*
#cgo windows LDFLAGS: -lcrypt32 -lncrypt
#include <windows.h>
#include <wincrypt.h>
#include <ncrypt.h>
char* errMsg(DWORD code) {
char* lpMsgBuf;
DWORD ret = 0;
ret = FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
code,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &lpMsgBuf,
0, NULL);
if (ret == 0) {
return NULL;
} else {
return lpMsgBuf;
}
}
*/
import "C"
import (
"crypto"
"crypto/ecdsa"
"crypto/rsa"
"crypto/x509"
"encoding/asn1"
"fmt"
"io"
"math/big"
"unicode/utf16"
"unsafe"
"github.com/pkg/errors"
)
const (
winTrue C.WINBOOL = 1
winFalse C.WINBOOL = 0
// ERROR_SUCCESS
ERROR_SUCCESS = 0x00000000
// CRYPT_E_NOT_FOUND — Cannot find object or property.
CRYPT_E_NOT_FOUND = 0x80092004
// NTE_BAD_ALGID — Invalid algorithm specified.
NTE_BAD_ALGID = 0x80090008
)
// winAPIFlag specifies the flags that should be passed to
// CryptAcquireCertificatePrivateKey. This impacts whether the CryptoAPI or CNG
// API will be used.
//
// Possible values are:
// 0x00000000 — — Only use CryptoAPI.
// 0x00010000 — CRYPT_ACQUIRE_ALLOW_NCRYPT_KEY_FLAG — Prefer CryptoAPI.
// 0x00020000 — CRYPT_ACQUIRE_PREFER_NCRYPT_KEY_FLAG — Prefer CNG.
// 0x00040000 — CRYPT_ACQUIRE_ONLY_NCRYPT_KEY_FLAG — Only uyse CNG.
var winAPIFlag C.DWORD = C.CRYPT_ACQUIRE_PREFER_NCRYPT_KEY_FLAG
// winStore is a wrapper around a C.HCERTSTORE.
type winStore struct {
store C.HCERTSTORE
}
// openStore opens the current user's personal cert store.
func openStore() (*winStore, error) {
storeName := unsafe.Pointer(stringToUTF16("MY"))
defer C.free(storeName)
store := C.CertOpenStore(CERT_STORE_PROV_SYSTEM_W, 0, 0, C.CERT_SYSTEM_STORE_CURRENT_USER, storeName)
if store == nil {
return nil, lastError("failed to open system cert store")
}
return &winStore{store}, nil
}
// Identities implements the Store interface.
func (s *winStore) Identities() ([]Identity, error) {
var (
err error
idents = []Identity{}
// CertFindChainInStore parameters
encoding = C.DWORD(C.X509_ASN_ENCODING)
flags = C.DWORD(C.CERT_CHAIN_FIND_BY_ISSUER_CACHE_ONLY_FLAG | C.CERT_CHAIN_FIND_BY_ISSUER_CACHE_ONLY_URL_FLAG)
findType = C.DWORD(C.CERT_CHAIN_FIND_BY_ISSUER)
params = &C.CERT_CHAIN_FIND_BY_ISSUER_PARA{cbSize: C.DWORD(unsafe.Sizeof(C.CERT_CHAIN_FIND_BY_ISSUER_PARA{}))}
paramsPtr = unsafe.Pointer(params)
chainCtx = C.PCCERT_CHAIN_CONTEXT(nil)
)
for {
if chainCtx = C.CertFindChainInStore(s.store, encoding, flags, findType, paramsPtr, chainCtx); chainCtx == nil {
break
}
if chainCtx.cChain < 1 {
err = errors.New("bad chain")
goto fail
}
// not sure why this isn't 1 << 29
const maxPointerArray = 1 << 28
// rgpChain is actually an array, but we only care about the first one.
simpleChain := *chainCtx.rgpChain
if simpleChain.cElement < 1 || simpleChain.cElement > maxPointerArray {
err = errors.New("bad chain")
goto fail
}
// Hacky way to get chain elements (c array) as a slice.
chainElts := (*[maxPointerArray]C.PCERT_CHAIN_ELEMENT)(unsafe.Pointer(simpleChain.rgpElement))[:simpleChain.cElement:simpleChain.cElement]
// Build chain of certificates from each elt's certificate context.
chain := make([]C.PCCERT_CONTEXT, len(chainElts))
for j := range chainElts {
chain[j] = chainElts[j].pCertContext
}
idents = append(idents, newWinIdentity(chain))
}
if err = checkError("failed to iterate certs in store"); err != nil && errors.Cause(err) != errCode(CRYPT_E_NOT_FOUND) {
goto fail
}
return idents, nil
fail:
for _, ident := range idents {
ident.Close()
}
return nil, err
}
// Import implements the Store interface.
func (s *winStore) Import(data []byte, password string) error {
cdata := C.CBytes(data)
defer C.free(cdata)
cpw := stringToUTF16(password)
defer C.free(unsafe.Pointer(cpw))
pfx := &C.CRYPT_DATA_BLOB{
cbData: C.DWORD(len(data)),
pbData: (*C.BYTE)(cdata),
}
flags := C.CRYPT_USER_KEYSET
// import into preferred KSP
if winAPIFlag&C.CRYPT_ACQUIRE_PREFER_NCRYPT_KEY_FLAG > 0 {
flags |= C.PKCS12_PREFER_CNG_KSP
} else if winAPIFlag&C.CRYPT_ACQUIRE_ONLY_NCRYPT_KEY_FLAG > 0 {
flags |= C.PKCS12_ALWAYS_CNG_KSP
}
store := C.PFXImportCertStore(pfx, cpw, C.DWORD(flags))
if store == nil {
return lastError("failed to import PFX cert store")
}
defer C.CertCloseStore(store, C.CERT_CLOSE_STORE_FORCE_FLAG)
var (
ctx = C.PCCERT_CONTEXT(nil)
encoding = C.DWORD(C.X509_ASN_ENCODING | C.PKCS_7_ASN_ENCODING)
)
for {
// iterate through certs in temporary store
if ctx = C.CertFindCertificateInStore(store, encoding, 0, C.CERT_FIND_ANY, nil, ctx); ctx == nil {
if err := checkError("failed to iterate certs in store"); err != nil && errors.Cause(err) != errCode(CRYPT_E_NOT_FOUND) {
return err
}
break
}
// Copy the cert to the system store.
if ok := C.CertAddCertificateContextToStore(s.store, ctx, C.CERT_STORE_ADD_REPLACE_EXISTING, nil); ok == winFalse {
return lastError("failed to add importerd certificate to MY store")
}
}
return nil
}
// Close implements the Store interface.
func (s *winStore) Close() {
C.CertCloseStore(s.store, 0)
s.store = nil
}
// winIdentity implements the Identity interface.
type winIdentity struct {
chain []C.PCCERT_CONTEXT
signer *winPrivateKey
}
func newWinIdentity(chain []C.PCCERT_CONTEXT) *winIdentity {
for _, ctx := range chain {
C.CertDuplicateCertificateContext(ctx)
}
return &winIdentity{chain: chain}
}
// Certificate implements the Identity interface.
func (i *winIdentity) Certificate() (*x509.Certificate, error) {
return exportCertCtx(i.chain[0])
}
// CertificateChain implements the Identity interface.
func (i *winIdentity) CertificateChain() ([]*x509.Certificate, error) {
var (
certs = make([]*x509.Certificate, len(i.chain))
err error
)
for j := range i.chain {
if certs[j], err = exportCertCtx(i.chain[j]); err != nil {
return nil, err
}
}
return certs, nil
}
// Signer implements the Identity interface.
func (i *winIdentity) Signer() (crypto.Signer, error) {
return i.getPrivateKey()
}
// getPrivateKey gets this identity's private *winPrivateKey.
func (i *winIdentity) getPrivateKey() (*winPrivateKey, error) {
if i.signer != nil {
return i.signer, nil
}
cert, err := i.Certificate()
if err != nil {
return nil, errors.Wrap(err, "failed to get identity certificate")
}
signer, err := newWinPrivateKey(i.chain[0], cert.PublicKey)
if err != nil {
return nil, errors.Wrap(err, "failed to load identity private key")
}
i.signer = signer
return i.signer, nil
}
// Delete implements the Identity interface.
func (i *winIdentity) Delete() error {
// duplicate cert context, since CertDeleteCertificateFromStore will free it.
deleteCtx := C.CertDuplicateCertificateContext(i.chain[0])
// try deleting cert
if ok := C.CertDeleteCertificateFromStore(deleteCtx); ok == winFalse {
return lastError("failed to delete certificate from store")
}
// try deleting private key
wpk, err := i.getPrivateKey()
if err != nil {
return errors.Wrap(err, "failed to get identity private key")
}
if err := wpk.Delete(); err != nil {
return errors.Wrap(err, "failed to delete identity private key")
}
return nil
}
// Close implements the Identity interface.
func (i *winIdentity) Close() {
if i.signer != nil {
i.signer.Close()
i.signer = nil
}
for _, ctx := range i.chain {
C.CertFreeCertificateContext(ctx)
i.chain = nil
}
}
// winPrivateKey is a wrapper around a HCRYPTPROV_OR_NCRYPT_KEY_HANDLE.
type winPrivateKey struct {
publicKey crypto.PublicKey
// CryptoAPI fields
capiProv C.HCRYPTPROV
// CNG fields
cngHandle C.NCRYPT_KEY_HANDLE
keySpec C.DWORD
}
// newWinPrivateKey gets a *winPrivateKey for the given certificate.
func newWinPrivateKey(certCtx C.PCCERT_CONTEXT, publicKey crypto.PublicKey) (*winPrivateKey, error) {
var (
provOrKey C.HCRYPTPROV_OR_NCRYPT_KEY_HANDLE
keySpec C.DWORD
mustFree C.WINBOOL
)
if publicKey == nil {
return nil, errors.New("nil public key")
}
// Get a handle for the found private key.
if ok := C.CryptAcquireCertificatePrivateKey(certCtx, winAPIFlag, nil, &provOrKey, &keySpec, &mustFree); ok == winFalse {
return nil, lastError("failed to get private key for certificate")
}
if mustFree != winTrue {
// This shouldn't happen since we're not asking for cached keys.
return nil, errors.New("CryptAcquireCertificatePrivateKey set mustFree")
}
if keySpec == C.CERT_NCRYPT_KEY_SPEC {
return &winPrivateKey{
publicKey: publicKey,
cngHandle: C.NCRYPT_KEY_HANDLE(provOrKey),
}, nil
} else {
return &winPrivateKey{
publicKey: publicKey,
capiProv: C.HCRYPTPROV(provOrKey),
keySpec: keySpec,
}, nil
}
}
// Public implements the crypto.Signer interface.
func (wpk *winPrivateKey) Public() crypto.PublicKey {
return wpk.publicKey
}
// Sign implements the crypto.Signer interface.
func (wpk *winPrivateKey) Sign(rand io.Reader, digest []byte, opts crypto.SignerOpts) ([]byte, error) {
if wpk.capiProv != 0 {
return wpk.capiSignHash(opts.HashFunc(), digest)
} else if wpk.cngHandle != 0 {
return wpk.cngSignHash(opts.HashFunc(), digest)
} else {
return nil, errors.New("bad private key")
}
}
// cngSignHash signs a digest using the CNG APIs.
func (wpk *winPrivateKey) cngSignHash(hash crypto.Hash, digest []byte) ([]byte, error) {
if len(digest) != hash.Size() {
return nil, errors.New("bad digest for hash")
}
var (
// input
padPtr = unsafe.Pointer(nil)
digestPtr = (*C.BYTE)(&digest[0])
digestLen = C.DWORD(len(digest))
flags = C.DWORD(0)
// output
sigLen = C.DWORD(0)
)
// setup pkcs1v1.5 padding for RSA
if _, isRSA := wpk.publicKey.(*rsa.PublicKey); isRSA {
flags |= C.BCRYPT_PAD_PKCS1
padInfo := C.BCRYPT_PKCS1_PADDING_INFO{}
padPtr = unsafe.Pointer(&padInfo)
switch hash {
case crypto.SHA1:
padInfo.pszAlgId = BCRYPT_SHA1_ALGORITHM
case crypto.SHA256:
padInfo.pszAlgId = BCRYPT_SHA256_ALGORITHM
case crypto.SHA384:
padInfo.pszAlgId = BCRYPT_SHA384_ALGORITHM
case crypto.SHA512:
padInfo.pszAlgId = BCRYPT_SHA512_ALGORITHM
default:
return nil, ErrUnsupportedHash
}
}
// get signature length
if err := checkStatus(C.NCryptSignHash(wpk.cngHandle, padPtr, digestPtr, digestLen, nil, 0, &sigLen, flags)); err != nil {
return nil, errors.Wrap(err, "failed to get signature length")
}
// get signature
sig := make([]byte, sigLen)
sigPtr := (*C.BYTE)(&sig[0])
if err := checkStatus(C.NCryptSignHash(wpk.cngHandle, padPtr, digestPtr, digestLen, sigPtr, sigLen, &sigLen, flags)); err != nil {
return nil, errors.Wrap(err, "failed to sign digest")
}
// CNG returns a raw ECDSA signature, but we wan't ASN.1 DER encoding.
if _, isEC := wpk.publicKey.(*ecdsa.PublicKey); isEC {
if len(sig)%2 != 0 {
return nil, errors.New("bad ecdsa signature from CNG")
}
type ecdsaSignature struct {
R, S *big.Int
}
r := new(big.Int).SetBytes(sig[:len(sig)/2])
s := new(big.Int).SetBytes(sig[len(sig)/2:])
encoded, err := asn1.Marshal(ecdsaSignature{r, s})
if err != nil {
return nil, errors.Wrap(err, "failed to ASN.1 encode EC signature")
}
return encoded, nil
}
return sig, nil
}
// capiSignHash signs a digest using the CryptoAPI APIs.
func (wpk *winPrivateKey) capiSignHash(hash crypto.Hash, digest []byte) ([]byte, error) {
if len(digest) != hash.Size() {
return nil, errors.New("bad digest for hash")
}
// Figure out which CryptoAPI hash algorithm we're using.
var hash_alg C.ALG_ID
switch hash {
case crypto.SHA1:
hash_alg = C.CALG_SHA1
case crypto.SHA256:
hash_alg = C.CALG_SHA_256
case crypto.SHA384:
hash_alg = C.CALG_SHA_384
case crypto.SHA512:
hash_alg = C.CALG_SHA_512
default:
return nil, ErrUnsupportedHash
}
// Instantiate a CryptoAPI hash object.
var chash C.HCRYPTHASH
if ok := C.CryptCreateHash(C.HCRYPTPROV(wpk.capiProv), hash_alg, 0, 0, &chash); ok == winFalse {
if err := lastError("failed to create hash"); errors.Cause(err) == errCode(NTE_BAD_ALGID) {
return nil, ErrUnsupportedHash
} else {
return nil, err
}
}
defer C.CryptDestroyHash(chash)
// Make sure the hash size matches.
var (
hashSize C.DWORD
hashSizePtr = (*C.BYTE)(unsafe.Pointer(&hashSize))
hashSizeLen = C.DWORD(unsafe.Sizeof(hashSize))
)
if ok := C.CryptGetHashParam(chash, C.HP_HASHSIZE, hashSizePtr, &hashSizeLen, 0); ok == winFalse {
return nil, lastError("failed to get hash size")
}
if hash.Size() != int(hashSize) {
return nil, errors.New("invalid CryptoAPI hash")
}
// Put our digest into the hash object.
digestPtr := (*C.BYTE)(unsafe.Pointer(&digest[0]))
if ok := C.CryptSetHashParam(chash, C.HP_HASHVAL, digestPtr, 0); ok == winFalse {
return nil, lastError("failed to set hash digest")
}
// Get signature length.
var sigLen C.DWORD
if ok := C.CryptSignHash(chash, wpk.keySpec, nil, 0, nil, &sigLen); ok == winFalse {
return nil, lastError("failed to get signature length")
}
// Get signature
var (
sig = make([]byte, int(sigLen))
sigPtr = (*C.BYTE)(unsafe.Pointer(&sig[0]))
)
if ok := C.CryptSignHash(chash, wpk.keySpec, nil, 0, sigPtr, &sigLen); ok == winFalse {
return nil, lastError("failed to sign digest")
}
// Signature is little endian, but we want big endian. Reverse it.
for i := len(sig)/2 - 1; i >= 0; i-- {
opp := len(sig) - 1 - i
sig[i], sig[opp] = sig[opp], sig[i]
}
return sig, nil
}
func (wpk *winPrivateKey) Delete() error {
if wpk.cngHandle != 0 {
// Delete CNG key
if err := checkStatus(C.NCryptDeleteKey(wpk.cngHandle, 0)); err != nil {
return err
}
} else if wpk.capiProv != 0 {
// Delete CryptoAPI key
var (
param unsafe.Pointer
err error
containerName C.LPCTSTR
providerName C.LPCTSTR
providerType *C.DWORD
)
if param, err = wpk.getProviderParam(C.PP_CONTAINER); err != nil {
return errors.Wrap(err, "failed to get PP_CONTAINER")
} else {
containerName = C.LPCTSTR(param)
}
if param, err = wpk.getProviderParam(C.PP_NAME); err != nil {
return errors.Wrap(err, "failed to get PP_NAME")
} else {
providerName = C.LPCTSTR(param)
}
if param, err = wpk.getProviderParam(C.PP_PROVTYPE); err != nil {
return errors.Wrap(err, "failed to get PP_PROVTYPE")
} else {
providerType = (*C.DWORD)(param)
}
// use CRYPT_SILENT too?
var prov C.HCRYPTPROV
if ok := C.CryptAcquireContext(&prov, containerName, providerName, *providerType, C.CRYPT_DELETEKEYSET); ok == winFalse {
return lastError("failed to delete key set")
}
} else {
return errors.New("bad private key")
}
return nil
}
// getProviderParam gets a parameter about a provider.
func (wpk *winPrivateKey) getProviderParam(param C.DWORD) (unsafe.Pointer, error) {
var dataLen C.DWORD
if ok := C.CryptGetProvParam(wpk.capiProv, param, nil, &dataLen, 0); ok == winFalse {
return nil, lastError("failed to get provider parameter size")
}
data := make([]byte, dataLen)
dataPtr := (*C.BYTE)(unsafe.Pointer(&data[0]))
if ok := C.CryptGetProvParam(wpk.capiProv, param, dataPtr, &dataLen, 0); ok == winFalse {
return nil, lastError("failed to get provider parameter")
}
// TODO leaking memory here
return C.CBytes(data), nil
}
// Close closes this winPrivateKey.
func (wpk *winPrivateKey) Close() {
if wpk.cngHandle != 0 {
C.NCryptFreeObject(C.NCRYPT_HANDLE(wpk.cngHandle))
wpk.cngHandle = 0
}
if wpk.capiProv != 0 {
C.CryptReleaseContext(wpk.capiProv, 0)
wpk.capiProv = 0
}
}
// exportCertCtx exports a PCCERT_CONTEXT as an *x509.Certificate.
func exportCertCtx(ctx C.PCCERT_CONTEXT) (*x509.Certificate, error) {
der := C.GoBytes(unsafe.Pointer(ctx.pbCertEncoded), C.int(ctx.cbCertEncoded))
cert, err := x509.ParseCertificate(der)
if err != nil {
return nil, errors.Wrap(err, "certificate parsing failed")
}
return cert, nil
}
type errCode uint64
// lastError gets the last error from the current thread. If there isn't one, it
// returns a new error.
func lastError(msg string) error {
if err := checkError(msg); err != nil {
return err
}
return errors.New(msg)
}
// checkError tries to get the last error from the current thread. If there
// isn't one, it returns nil.
func checkError(msg string) error {
if code := errCode(C.GetLastError()); code != 0 {
return errors.Wrap(code, msg)
}
return nil
}
func (c errCode) Error() string {
cmsg := C.errMsg(C.DWORD(c))
if cmsg == nil {
return fmt.Sprintf("Error %X", int(c))
}
defer C.LocalFree(C.HLOCAL(cmsg))
gomsg := C.GoString(cmsg)
return fmt.Sprintf("Error: %X %s", int(c), gomsg)
}
type securityStatus uint64
func checkStatus(s C.SECURITY_STATUS) error {
ss := securityStatus(s)
if ss == ERROR_SUCCESS {
return nil
}
if ss == NTE_BAD_ALGID {
return ErrUnsupportedHash
}
return ss
}
func (ss securityStatus) Error() string {
return fmt.Sprintf("SECURITY_STATUS %d", int(ss))
}
func stringToUTF16(s string) C.LPCWSTR {
// Not sure why this isn't 1 << 30...
const maxUint16Array = 1 << 29
if len(s) > maxUint16Array {
panic("string too long")
}
wstr := utf16.Encode([]rune(s))
p := C.calloc(C.size_t(len(wstr)+1), C.size_t(unsafe.Sizeof(uint16(0))))
pp := (*[maxUint16Array]uint16)(p)
copy(pp[:], wstr)
return (C.LPCWSTR)(p)
}