/
CryptoWrappersStore.h
472 lines (371 loc) · 16.3 KB
/
CryptoWrappersStore.h
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
/*
Module : CryptoWrappersStore.h
Purpose: Defines the interface for a C++ wrapper class for a CryptoAPI HCERTSTORE
History: PJN / 01-08-2014 1. Initial public release
PJN / 12-05-2016 1. Updated copyright details.
2. Updated code to be /analyze clean on VC 2015
Copyright (c) 2014 - 2017 by PJ Naughter (Web: www.naughter.com, Email: pjna@naughter.com)
All rights reserved.
Copyright / Usage Details:
You are allowed to include the source code in any product (commercial, shareware, freeware or otherwise)
when your product is released in binary form. You are allowed to modify the source code in any way you want
except you cannot modify the copyright details at the top of each module. If you want to distribute source
code with your application, then you are only allowed to distribute versions released by the author. This is
to maintain a single distribution point for the source code.
*/
////////////////////////// Macros / Defines ///////////////////////////////////
#pragma once
#ifndef __CRYPTOWRAPPERSSTORE_H__
#define __CRYPTOWRAPPERSSTORE_H__
#pragma comment(lib, "Crypt32.lib") //Automatically link in the Crypto dll
////////////////////////// Includes ///////////////////////////////////////////
#ifndef __WINCRYPT_H__
#pragma message("To avoid this message, please put wincrypt.h in your pre compiled header (normally stdafx.h)")
#include <wincrypt.h>
#endif //#ifndef __WINCRYPT_H__
#include "CryptoWrappersCertificate.h"
#include "CryptoWrappersCRL.h"
#include "CryptoWrappersCTL.h"
#include "CryptoWrappersChain.h"
////////////////////////// Classes ////////////////////////////////////////////
namespace CryptoWrappers
{
//Encapsulates a CryptoAPI HCERTSTORE
class CStore
{
public:
//Constructors / Destructors
CStore() : m_hStore(nullptr)
{
}
CStore(_In_ const CStore& store) : m_hStore(nullptr)
{
if (!store.Duplicate(*this))
m_hStore = nullptr;
}
CStore(_In_ CStore&& store) : m_hStore(nullptr)
{
Attach(store.Detach());
}
explicit CStore(_In_opt_ HCERTSTORE hStore) : m_hStore(hStore)
{
}
~CStore()
{
if (m_hStore != nullptr)
Close();
}
//Methods
CStore& operator=(_In_ const CStore& store)
{
if (this != &store)
{
if (m_hStore != nullptr)
Close();
if (!store.Duplicate(*this))
m_hStore = nullptr;
}
return *this;
}
CStore& operator=(_In_ CStore&& store)
{
if (m_hStore != nullptr)
Close();
Attach(store.Detach());
return *this;
}
HCERTSTORE Handle() const
{
return m_hStore;
}
void Attach(_In_opt_ HCERTSTORE hStore)
{
//Validate our parameters
ATLASSERT(m_hStore == nullptr);
m_hStore = hStore;
}
HCERTSTORE Detach()
{
HCERTSTORE hStore = m_hStore;
m_hStore = nullptr;
return hStore;
}
_Must_inspect_result_
BOOL Open(_In_ LPCSTR lpszStoreProvider, _In_ DWORD dwEncodingType, _In_ DWORD dwFlags, _In_opt_ const void* pvPara)
{
//Validate our parameters
ATLASSERT(m_hStore == nullptr);
m_hStore = CertOpenStore(lpszStoreProvider, dwEncodingType, NULL, dwFlags, pvPara);
return (m_hStore != nullptr);
}
_Must_inspect_result_
BOOL OpenSystemStore(_In_ LPCTSTR szSubsystemProtocol)
{
//Validate our parameters
ATLASSERT(m_hStore == nullptr);
m_hStore = CertOpenSystemStore(NULL, szSubsystemProtocol);
return (m_hStore != nullptr);
}
_Success_(return != FALSE)
BOOL Close(_In_ DWORD dwFlags = 0)
{
//Validate our parameters
ATLASSERT(m_hStore != nullptr);
BOOL bResult = CertCloseStore(m_hStore, dwFlags);
m_hStore = nullptr;
return bResult;
}
_Success_(return != FALSE)
BOOL Control(_In_ DWORD dwFlags, _In_ DWORD dwCtrlType, _In_opt_ void const* pvCtrlPara)
{
//Validate our parameters
ATLASSERT(m_hStore != nullptr);
return CertControlStore(m_hStore, dwFlags, dwCtrlType, pvCtrlPara);
}
_Success_(return != FALSE)
BOOL Duplicate(_Inout_ CStore& store) const
{
//Validate our parameters
ATLASSERT(m_hStore != nullptr);
ATLASSERT(store.m_hStore == nullptr);
store.m_hStore = CertDuplicateStore(m_hStore);
return (store.m_hStore != nullptr);
}
_Success_(return != FALSE)
BOOL AddStoreToCollection(_In_opt_ HCERTSTORE hSiblingStore, _In_ DWORD dwUpdateFlags, _In_ DWORD dwPriority)
{
//Validate our parameters
ATLASSERT(m_hStore != nullptr);
return CertAddStoreToCollection(m_hStore, hSiblingStore, dwUpdateFlags, dwPriority);
}
void RemoveStoreToCollection(_In_ const CStore& siblingStore)
{
//Validate our parameters
ATLASSERT(m_hStore != nullptr);
CertRemoveStoreFromCollection(m_hStore, siblingStore.m_hStore);
}
_Success_(return != FALSE)
BOOL Save(_In_ DWORD dwEncodingType, _In_ DWORD dwSaveAs, _In_ DWORD dwSaveTo, _Inout_ void* pvSaveToPara)
{
//Validate our parameters
ATLASSERT(m_hStore != nullptr);
return CertSaveStore(m_hStore, dwEncodingType, dwSaveAs, dwSaveTo, pvSaveToPara, 0);
}
_Success_(return != FALSE)
BOOL SetProperty(_In_ DWORD dwPropId, _In_opt_ const void* pvData)
{
//Validate our parameters
ATLASSERT(m_hStore != nullptr);
return CertSetStoreProperty(m_hStore, dwPropId, 0, pvData);
}
_Success_(return != FALSE)
BOOL GetProperty(_In_ DWORD dwPropId, _Out_writes_bytes_to_opt_(*pcbData, *pcbData) void* pvData, _Inout_ DWORD* pcbData)
{
//Validate our parameters
ATLASSERT(m_hStore != nullptr);
return CertGetStoreProperty(m_hStore, dwPropId, pvData, pcbData);
}
_Success_(return != FALSE)
BOOL AddSerializedElement(_In_reads_bytes_(cbElement) const BYTE *pbElement, _In_ DWORD cbElement, _In_ DWORD dwAddDisposition, _In_ DWORD dwContextTypeFlags, _Out_opt_ DWORD* pdwContextType, _Outptr_opt_ const void** ppvContext)
{
return CertAddSerializedElementToStore(m_hStore, pbElement, cbElement, dwAddDisposition, 0, dwContextTypeFlags, pdwContextType, ppvContext);
}
_Success_(return != FALSE)
BOOL AddCertifcate(_In_ CCertificate& cert, _In_ DWORD dwAddDisposition, _Inout_opt_ CCertificate* pStoreCertificate)
{
//Validate our parameters
ATLASSERT(m_hStore != nullptr);
if (pStoreCertificate != nullptr)
ATLASSERT(pStoreCertificate->m_pCert == nullptr);
return CertAddCertificateContextToStore(m_hStore, cert.m_pCert, dwAddDisposition, pStoreCertificate ? &pStoreCertificate->m_pCert : nullptr);
}
_Success_(return != FALSE)
BOOL AddCRL(_In_ CCRL& crl, _In_ DWORD dwAddDisposition, _Inout_opt_ CCRL* pStoreCRL)
{
//Validate our parameters
ATLASSERT(m_hStore != nullptr);
if (pStoreCRL != nullptr)
ATLASSERT(pStoreCRL->m_pCRL == nullptr);
return CertAddCRLContextToStore(m_hStore, crl.m_pCRL, dwAddDisposition, pStoreCRL ? &pStoreCRL->m_pCRL : nullptr);
}
_Success_(return != FALSE)
BOOL AddCTL(_In_ CCTL& ctl, _In_ DWORD dwAddDisposition, _Inout_opt_ CCTL* pStoreCTL)
{
//Validate our parameters
ATLASSERT(m_hStore != nullptr);
if (pStoreCTL != nullptr)
ATLASSERT(pStoreCTL->m_pCTL == nullptr);
return CertAddCTLContextToStore(m_hStore, ctl.m_pCTL, dwAddDisposition, pStoreCTL ? &pStoreCTL->m_pCTL : nullptr);
}
CCertificate SelectCertificate(_In_opt_ HWND hwnd, _In_opt_ LPCWSTR pwszTitle, _In_opt_ LPCWSTR pwszDisplayString, _In_ DWORD dwDontUseColumn)
{
//Validate our parameters
ATLASSERT(m_hStore != nullptr);
#pragma warning(suppress: 6387) //There are SAL annotation errors on the seventh parameter to CryptUIDlgSelectCertificateFromStore even in the latest Windows 10 SDK
CCertificate certificate(CryptUIDlgSelectCertificateFromStore(m_hStore, hwnd, pwszTitle, pwszDisplayString, dwDontUseColumn, 0, nullptr));
return certificate;
}
CCertificate EnumCertificates(_In_ CCertificate& prevCertificate)
{
//Validate our parameters
ATLASSERT(m_hStore != nullptr);
CCertificate certificate(CertEnumCertificatesInStore(m_hStore, prevCertificate.m_pCert));
prevCertificate.m_pCert = nullptr; //Required because CertEnumCertificatesInStore auto frees any non nullptr pPrevCertContext parameter
return certificate;
}
CCRL EnumCRLs(_In_ CCRL& prevCRL)
{
//Validate our parameters
ATLASSERT(m_hStore != nullptr);
CCRL crl(CertEnumCRLsInStore(m_hStore, prevCRL.m_pCRL));
prevCRL.m_pCRL = nullptr; //Required because CertEnumCRLsInStore auto frees any non nullptr pPrevCrlContext parameter
return crl;
}
CCTL EnumCTLs(_In_ CCTL& prevCTL)
{
//Validate our parameters
ATLASSERT(m_hStore != nullptr);
CCTL crl(CertEnumCTLsInStore(m_hStore, prevCTL.m_pCTL));
prevCTL.m_pCTL = nullptr; //Required because CertEnumCTLsInStore auto frees any non nullptr pPrevCtlContext parameter
return crl;
}
CCertificate FindCertificate(_In_ DWORD dwCertEncodingType, _In_ DWORD dwFindFlags, _In_ DWORD dwFindType, _In_opt_ const void* pvFindPara, _In_ CCertificate& prevCertificate)
{
//Validate our parameters
ATLASSERT(m_hStore != nullptr);
CCertificate certificate(CertFindCertificateInStore(m_hStore, dwCertEncodingType, dwFindFlags, dwFindType, pvFindPara, prevCertificate.m_pCert));
prevCertificate.m_pCert = nullptr; //Required because CertFindCertificateInStore auto frees any non nullptr pPrevCertContext parameter
return certificate;
}
CCRL FindCRL(_In_ DWORD dwCertEncodingType, _In_ DWORD dwFindFlags, _In_ DWORD dwFindType, _In_opt_ const void* pvFindPara, _In_ CCRL& prevCRL)
{
//Validate our parameters
ATLASSERT(m_hStore != nullptr);
CCRL crl(CertFindCRLInStore(m_hStore, dwCertEncodingType, dwFindFlags, dwFindType, pvFindPara, prevCRL.m_pCRL));
prevCRL.m_pCRL = nullptr; //Required because CertFindCRLInStore auto frees any non nullptr pPrevCertContext parameter
return crl;
}
CCTL FindCTL(_In_ DWORD dwMsgAndCertEncodingType, _In_ DWORD dwFindFlags, _In_ DWORD dwFindType, _In_opt_ const void* pvFindPara, _In_ CCTL& prevCTL)
{
//Validate our parameters
ATLASSERT(m_hStore != nullptr);
CCTL ctl(CertFindCTLInStore(m_hStore, dwMsgAndCertEncodingType, dwFindFlags, dwFindType, pvFindPara, prevCTL.m_pCTL));
prevCTL.m_pCTL = nullptr; //Required because CertFindCRLInStore auto frees any non nullptr pPrevCertContext parameter
return ctl;
}
CChain FindChain(_In_ DWORD dwCertEncodingType, _In_ DWORD dwFindFlags, _In_ DWORD dwFindType, _In_opt_ const void* pvFindPara, _In_ CChain& prevChain)
{
//Validate our parameters
ATLASSERT(m_hStore != nullptr);
CChain chain(CertFindChainInStore(m_hStore, dwCertEncodingType, dwFindFlags, dwFindType, pvFindPara, prevChain.m_pChain));
prevChain.m_pChain = nullptr; //Required because CertFindChainInStore auto frees any non nullptr pPrevCertContext parameter
return chain;
}
_Success_(return != FALSE)
BOOL AddCertificateLink(_In_ CCertificate& certificate, _In_ DWORD dwAddDisposition, _Inout_opt_ CCertificate* pStoreCertificate)
{
//Validate our parameters
ATLASSERT(m_hStore != nullptr);
if (pStoreCertificate != nullptr)
ATLASSERT(pStoreCertificate->m_pCert == nullptr);
return CertAddCertificateLinkToStore(m_hStore, certificate.m_pCert, dwAddDisposition, pStoreCertificate ? &pStoreCertificate->m_pCert : nullptr);
}
_Success_(return != FALSE)
BOOL AddCRLLink(_In_ CCRL& crl, _In_ DWORD dwAddDisposition, _Inout_opt_ CCRL* pStoreCRL)
{
//Validate our parameters
ATLASSERT(m_hStore != nullptr);
if (pStoreCRL != nullptr)
ATLASSERT(pStoreCRL->m_pCRL == nullptr);
return CertAddCRLLinkToStore(m_hStore, crl.m_pCRL, dwAddDisposition, pStoreCRL ? &pStoreCRL->m_pCRL : nullptr);
}
_Success_(return != FALSE)
BOOL AddCTLLink(_In_ CCTL& ctl, _In_ DWORD dwAddDisposition, _Inout_opt_ CCTL* pStoreCTL)
{
//Validate our parameters
ATLASSERT(m_hStore != nullptr);
if (pStoreCTL != nullptr)
ATLASSERT(pStoreCTL->m_pCTL == nullptr);
return CertAddCTLLinkToStore(m_hStore, ctl.m_pCTL, dwAddDisposition, pStoreCTL ? &pStoreCTL->m_pCTL : nullptr);
}
_Success_(return != FALSE)
BOOL AddEncodedCertificate(_In_ DWORD dwCertEncodingType, _In_reads_bytes_(cbCertEncoded) const BYTE* pbCertEncoded, _In_ DWORD cbCertEncoded, _In_ DWORD dwAddDisposition, _Inout_opt_ CCertificate* pCertificate)
{
//Validate our parameters
ATLASSERT(m_hStore != nullptr);
if (pCertificate != nullptr)
ATLASSERT(pCertificate->m_pCert == nullptr);
return CertAddEncodedCertificateToStore(m_hStore, dwCertEncodingType, pbCertEncoded, cbCertEncoded, dwAddDisposition, (pCertificate != NULL) ? &pCertificate->m_pCert : nullptr);
}
_Success_(return != FALSE)
BOOL AddEncodedCRL(_In_ DWORD dwCertEncodingType, _In_reads_bytes_(cbCrlEncoded) const BYTE* pbCrlEncoded, _In_ DWORD cbCrlEncoded, _In_ DWORD dwAddDisposition, _Inout_opt_ CCRL* pCRL)
{
//Validate our parameters
ATLASSERT(m_hStore != nullptr);
if (pCRL != nullptr)
ATLASSERT(pCRL->m_pCRL == nullptr);
return CertAddEncodedCRLToStore(m_hStore, dwCertEncodingType, pbCrlEncoded, cbCrlEncoded, dwAddDisposition, (pCRL != NULL) ? &pCRL->m_pCRL : nullptr);
}
_Success_(return != FALSE)
BOOL AddEncodedCTL(_In_ DWORD dwMsgAndCertEncodingType, _In_reads_bytes_(cbCtlEncoded) const BYTE* pbCtlEncoded, _In_ DWORD cbCtlEncoded, _In_ DWORD dwAddDisposition, _Inout_opt_ CCTL* pCTL)
{
//Validate our parameters
ATLASSERT(m_hStore != nullptr);
if (pCTL != nullptr)
ATLASSERT(pCTL->m_pCTL == nullptr);
return CertAddEncodedCTLToStore(m_hStore, dwMsgAndCertEncodingType, pbCtlEncoded, cbCtlEncoded, dwAddDisposition, (pCTL != NULL) ? &pCTL->m_pCTL : nullptr);
}
CCertificate GetIssuerCertificate(_In_ CCertificate& subjectContext, _In_ CCertificate& prevIssuerContext, _Inout_ DWORD* pdwFlags)
{
//Validate our parameters
ATLASSERT(m_hStore != nullptr);
CCertificate certificate(CertGetIssuerCertificateFromStore(m_hStore, subjectContext.Handle(), prevIssuerContext.m_pCert, pdwFlags));
prevIssuerContext.m_pCert = nullptr; //Required because CertGetIssuerCertificateFromStore auto frees any non nullptr pPrevCertContext parameter
return certificate;
}
CCertificate GetSubjectCertificate(_In_ DWORD dwCertEncodingType, _In_ PCERT_INFO pCertId)
{
//Validate our parameters
ATLASSERT(m_hStore != nullptr);
CCertificate certificate(CertGetSubjectCertificateFromStore(m_hStore, dwCertEncodingType, pCertId));
return certificate;
}
#if (NTDDI_VERSION >= NTDDI_WIN7)
_Success_(return != FALSE)
BOOL SelectCertificateChains(_In_opt_ LPCGUID pSelectionContext, _In_ DWORD dwFlags, _In_opt_ PCCERT_SELECT_CHAIN_PARA pChainParameters, _In_ DWORD cCriteria,
_In_opt_ PCCERT_SELECT_CRITERIA rgpCriteria, _Out_ PDWORD pcSelection, _Out_ PCCERT_CHAIN_CONTEXT** pprgpSelection)
{
//Validate our parameters
ATLASSERT(m_hStore != nullptr);
return CertSelectCertificateChains(pSelectionContext, dwFlags, pChainParameters, cCriteria, rgpCriteria, m_hStore, pcSelection, pprgpSelection);
}
#endif //#if (NTDDI_VERSION >= NTDDI_WIN7)
CCRL GetCRL(_In_ CCertificate& issuer, _In_ CCRL& prevCRL, _Inout_ DWORD* pdwFlags)
{
//Validate our parameters
ATLASSERT(m_hStore != nullptr);
CCRL crl;
crl.m_pCRL = CertGetCRLFromStore(m_hStore, issuer.Handle(), prevCRL.Handle(), pdwFlags);
return crl;
}
_Success_(return != FALSE)
BOOL PFXExport(_Inout_ CRYPT_DATA_BLOB* pPFX, _In_ LPCWSTR szPassword, _In_opt_ void* pvPara, _In_ DWORD dwFlags)
{
//Validate our parameters
ATLASSERT(m_hStore != nullptr);
#pragma warning(suppress: 6387)
return PFXExportCertStoreEx(m_hStore, pPFX, szPassword, pvPara, dwFlags);
}
_Success_(return != FALSE)
BOOL PFXImport(_In_ CRYPT_DATA_BLOB* pPFX, _In_ LPCWSTR szPassword, _In_ DWORD dwFlags)
{
//Validate our parameters
ATLASSERT(m_hStore == nullptr);
m_hStore = PFXImportCertStore(pPFX, szPassword, dwFlags);
return (m_hStore != nullptr);
}
//Member variables
HCERTSTORE m_hStore;
};
}; //namespace CryptoWrappers
#endif //#ifndef __CRYPTOWRAPPERSSTORE_H__