-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwincertstore.go
193 lines (183 loc) · 4.49 KB
/
wincertstore.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
package wincertstore
import (
"encoding/pem"
"errors"
"fmt"
"unsafe"
"golang.org/x/sys/windows"
)
// Store is a certificate store.
type Store struct {
h windows.Handle
}
const (
// CA is the name of the system store that contains
// intermediate certificates.
CA = "CA"
// My is the name of the system store that contains per-user
// private keys.
//
// For TLS servers and client certs.
My = "MY"
// Root is the name of the system store that contains root
// certificates.
Root = "ROOT"
)
// OpenSystemStore opens the named system certificate store.
func OpenSystemStore(name string) (*Store, error) {
system, err := windows.UTF16PtrFromString(name)
if err != nil {
return nil, err
}
h, err := windows.CertOpenStore(
windows.CERT_STORE_PROV_SYSTEM,
windows.PKCS_7_ASN_ENCODING|windows.X509_ASN_ENCODING,
0,
windows.CERT_SYSTEM_STORE_LOCAL_MACHINE,
uintptr(unsafe.Pointer(system)),
)
if err != nil {
return nil, err
}
return &Store{h: h}, nil
}
// OpenStore creates an in-memory certificate store.
func OpenStore() (*Store, error) {
h, err := windows.CertOpenStore(
windows.CERT_STORE_PROV_MEMORY,
windows.PKCS_7_ASN_ENCODING|windows.X509_ASN_ENCODING,
0,
windows.CERT_STORE_CREATE_NEW_FLAG,
0,
)
if err != nil {
return nil, err
}
return &Store{h: h}, nil
}
func (s *Store) Close() error {
return windows.CertCloseStore(s.h, 0)
}
// AppendCertsFromPEM appends the PEM-encoded certificates to the
// store and reports whether at least one certificate was added.
func (s *Store) AppendCertsFromPEM(data []byte) error {
for len(data) > 0 {
var b *pem.Block
b, data = pem.Decode(data)
if b == nil {
break
}
if b.Type != "CERTIFICATE" || len(b.Headers) != 0 {
return fmt.Errorf("winhttp: block not a 'CERTIFICATE'")
}
if len(b.Bytes) == 0 {
return errors.New("winhttp: empty block")
}
cert, err := windows.CertCreateCertificateContext(
windows.X509_ASN_ENCODING|windows.PKCS_7_ASN_ENCODING,
&b.Bytes[0],
uint32(len(b.Bytes)),
)
if err != nil {
return fmt.Errorf("winhttp: unable to create cert context: %w", err)
}
err = windows.CertAddCertificateContextToStore(
s.h,
cert,
windows.CERT_STORE_ADD_NEW,
nil,
)
windows.CertFreeCertificateContext(cert)
if err != nil {
return fmt.Errorf("winhttp: unable to add cert to store: %w", err)
}
}
return nil
}
// RemoveCertsFromPEM removes the PEM-encoded certificates from
// the store.
func (s *Store) RemoveCertsFromPEM(data []byte) error {
var errs []error
for len(data) > 0 {
var b *pem.Block
b, data = pem.Decode(data)
if b == nil {
break
}
if b.Type != "CERTIFICATE" || len(b.Headers) != 0 {
continue
}
if len(b.Bytes) == 0 {
continue
}
if err := s.removeCert(b.Bytes); err != nil {
errs = append(errs, err)
}
}
switch len(errs) {
case 0:
return nil
case 1:
return fmt.Errorf("winhttp: %w", errs[0])
default:
return fmt.Errorf("winhttp: %w (and %d other errors)", errs[0], len(errs)-1)
}
}
// removeCerts removes the certificate from the store.
func (s *Store) removeCert(buf []byte) error {
targ, err := windows.CertCreateCertificateContext(
windows.X509_ASN_ENCODING|windows.PKCS_7_ASN_ENCODING,
&buf[0],
uint32(len(buf)),
)
if err != nil {
return fmt.Errorf("unable to parse certificate: %w", err)
}
const (
CERT_ID_ISSUER_SERIAL_NUMBER = 1
)
// typedef struct _CERT_ID {
// DWORD dwIdChoice;
// union {
// CERT_ISSUER_SERIAL_NUMBER IssuerSerialNumber;
// CRYPT_HASH_BLOB KeyId;
// CRYPT_HASH_BLOB HashId;
// } DUMMYUNIONNAME;
// } CERT_ID, *PCERT_ID;
//
// typedef struct _CERT_ISSUER_SERIAL_NUMBER {
// CERT_NAME_BLOB Issuer;
// CRYPT_INTEGER_BLOB SerialNumber;
// } CERT_ISSUER_SERIAL_NUMBER, *PCERT_ISSUER_SERIAL_NUMBER;
certID := struct {
idChoice uint32
name windows.CertNameBlob
serial windows.CryptIntegerBlob
}{
idChoice: CERT_ID_ISSUER_SERIAL_NUMBER,
name: targ.CertInfo.Issuer,
serial: targ.CertInfo.SerialNumber,
}
cert, err := windows.CertFindCertificateInStore(
s.h,
windows.X509_ASN_ENCODING|windows.PKCS_7_ASN_ENCODING,
0,
windows.CERT_FIND_CERT_ID,
unsafe.Pointer(&certID),
nil,
)
if err != nil {
const (
notFound = windows.Errno(windows.CRYPT_E_NOT_FOUND)
)
if errors.Is(err, notFound) {
return nil
}
return fmt.Errorf("unable to enumerate certificates: %w", err)
}
err = windows.CertDeleteCertificateFromStore(cert)
if err != nil {
return fmt.Errorf("unable to delete certificate: %w", err)
}
return nil
}