WinStd
Additional templates and function helpers for Microsoft Windows using Standard C++ classes
Crypt.h
1/*
2 SPDX-License-Identifier: MIT
3 Copyright © 1991-2022 Amebis
4 Copyright © 2016 GÉANT
5*/
6
7#pragma once
8
9#include "Common.h"
10#include <assert.h>
11#include <WinCrypt.h>
12#include <algorithm>
13#include <string>
14#include <vector>
15
21
23template<class _Traits, class _Ax>
24static DWORD CertGetNameStringA(_In_ PCCERT_CONTEXT pCertContext, _In_ DWORD dwType, _In_ DWORD dwFlags, _In_opt_ void *pvTypePara, _Out_ std::basic_string<char, _Traits, _Ax> &sNameString)
25{
26 // Query the final string length first.
27 DWORD dwSize = ::CertGetNameStringA(pCertContext, dwType, dwFlags, pvTypePara, NULL, 0);
28
29 // Allocate buffer on heap to format the string data into and read it.
30 std::unique_ptr<char[]> szBuffer(new char[dwSize]);
31 dwSize = ::CertGetNameStringA(pCertContext, dwType, dwFlags, pvTypePara, szBuffer.get(), dwSize);
32 sNameString.assign(szBuffer.get(), dwSize - 1);
33 return dwSize;
34}
35
41template<class _Traits, class _Ax>
42static DWORD CertGetNameStringW(_In_ PCCERT_CONTEXT pCertContext, _In_ DWORD dwType, _In_ DWORD dwFlags, _In_opt_ void *pvTypePara, _Out_ std::basic_string<wchar_t, _Traits, _Ax> &sNameString)
43{
44 // Query the final string length first.
45 DWORD dwSize = ::CertGetNameStringW(pCertContext, dwType, dwFlags, pvTypePara, NULL, 0);
46
47 // Allocate buffer on heap to format the string data into and read it.
48 std::unique_ptr<wchar_t[]> szBuffer(new wchar_t[dwSize]);
49 dwSize = ::CertGetNameStringW(pCertContext, dwType, dwFlags, pvTypePara, szBuffer.get(), dwSize);
50 sNameString.assign(szBuffer.get(), dwSize - 1);
51 return dwSize;
52}
53
59template<class _Ty, class _Ax>
60static _Success_(return != 0) BOOL WINAPI CertGetCertificateContextProperty(_In_ PCCERT_CONTEXT pCertContext, _In_ DWORD dwPropId, _Out_ std::vector<_Ty, _Ax> &aData)
61{
63 DWORD dwSize = WINSTD_STACK_BUFFER_BYTES;
64
65 // Try with the stack buffer first.
66 if (CertGetCertificateContextProperty(pCertContext, dwPropId, buf, &dwSize)) {
67 // Copy from stack.
68 aData.assign((const _Ty*)buf, (const _Ty*)buf + (dwSize + sizeof(_Ty) - 1) / sizeof(_Ty));
69 return TRUE;
70 } else if (GetLastError() == ERROR_MORE_DATA) {
71 aData.resize((dwSize + sizeof(_Ty) - 1) / sizeof(_Ty));
72 if (CertGetCertificateContextProperty(pCertContext, dwPropId, (BYTE*)aData.data(), &dwSize))
73 return TRUE;
74 }
75
76 return FALSE;
77}
78
84template<class _Ty, class _Ax>
85static _Success_(return != 0) BOOL CryptGetHashParam(_In_ HCRYPTHASH hHash, _In_ DWORD dwParam, _Out_ std::vector<_Ty, _Ax> &aData, _In_ DWORD dwFlags)
86{
88 DWORD dwSize = WINSTD_STACK_BUFFER_BYTES;
89
90 // Try with the stack buffer first.
91 if (CryptGetHashParam(hHash, dwParam, buf, &dwSize, dwFlags)) {
92 // Copy from stack.
93 aData.assign((const _Ty*)buf, (const _Ty*)buf + (dwSize + sizeof(_Ty) - 1) / sizeof(_Ty));
94 return TRUE;
95 } else if (GetLastError() == ERROR_MORE_DATA) {
96 aData.resize((dwSize + sizeof(_Ty) - 1) / sizeof(_Ty));
97 if (CryptGetHashParam(hHash, dwParam, (BYTE*)aData.data(), &dwSize, dwFlags))
98 return TRUE;
99 }
100
101 return FALSE;
102}
103
109template<class T>
110static _Success_(return != 0) BOOL CryptGetHashParam(_In_ HCRYPTHASH hHash, _In_ DWORD dwParam, _Out_ T &data, _In_ DWORD dwFlags)
111{
112 DWORD dwSize = sizeof(T);
113 return CryptGetHashParam(hHash, dwParam, (BYTE*)&data, &dwSize, dwFlags);
114}
115
121template<class _Ty, class _Ax>
122static _Success_(return != 0) BOOL CryptGetKeyParam(_In_ HCRYPTKEY hKey, _In_ DWORD dwParam, _Out_ std::vector<_Ty, _Ax> &aData, _In_ DWORD dwFlags)
123{
125 DWORD dwSize = WINSTD_STACK_BUFFER_BYTES;
126
127 // Try with the stack buffer first.
128 if (CryptGetKeyParam(hKey, dwParam, buf, &dwSize, dwFlags)) {
129 // Copy from stack.
130 aData.assign((const _Ty*)buf, (const _Ty*)buf + (dwSize + sizeof(_Ty) - 1) / sizeof(_Ty));
131 return TRUE;
132 } else if (GetLastError() == ERROR_MORE_DATA) {
133 aData.resize((dwSize + sizeof(_Ty) - 1) / sizeof(_Ty));
134 if (CryptGetKeyParam(hKey, dwParam, (BYTE*)aData.data(), &dwSize, dwFlags))
135 return TRUE;
136 }
137
138 return FALSE;
139}
140
146template<class T>
147static BOOL CryptGetKeyParam(_In_ HCRYPTKEY hKey, _In_ DWORD dwParam, _Out_ T &data, _In_ DWORD dwFlags)
148{
149 DWORD dwSize = sizeof(T);
150 return CryptGetKeyParam(hKey, dwParam, (BYTE*)&data, &dwSize, dwFlags);
151}
152
158template<class _Ty, class _Ax>
159static _Success_(return != 0) BOOL CryptExportKey(_In_ HCRYPTKEY hKey, _In_ HCRYPTKEY hExpKey, _In_ DWORD dwBlobType, _In_ DWORD dwFlags, _Out_ std::vector<_Ty, _Ax> &aData)
160{
161 DWORD dwKeyBLOBSize = 0;
162
163 if (CryptExportKey(hKey, hExpKey, dwBlobType, dwFlags, NULL, &dwKeyBLOBSize)) {
164 aData.resize((dwKeyBLOBSize + sizeof(_Ty) - 1) / sizeof(_Ty));
165 if (CryptExportKey(hKey, hExpKey, dwBlobType, dwFlags, aData.data(), &dwKeyBLOBSize))
166 return TRUE;
167 }
168
169 return FALSE;
170}
171
177template<class _Ty, class _Ax>
178static _Success_(return != 0) BOOL CryptEncrypt(_In_ HCRYPTKEY hKey, _In_opt_ HCRYPTHASH hHash, _In_ BOOL Final, _In_ DWORD dwFlags, _Inout_ std::vector<_Ty, _Ax> &aData)
179{
180 DWORD
181 dwDataLen = (DWORD)(aData.size() * sizeof(_Ty)),
182 dwBufLen = (DWORD)(aData.capacity() * sizeof(_Ty)),
183 dwEncLen = dwDataLen,
184 dwResult;
185
186 if (dwBufLen) {
187 aData.resize(dwBufLen);
188 if (CryptEncrypt(hKey, hHash, Final, dwFlags, (BYTE*)aData.data(), &dwEncLen, dwBufLen)) {
189 // Encryption succeeded.
190 assert(dwEncLen <= dwBufLen);
191 if (dwEncLen < dwBufLen)
192 aData.resize((dwEncLen + sizeof(_Ty) - 1) / sizeof(_Ty));
193 return TRUE;
194 } else
195 dwResult = GetLastError();
196 } else if (CryptEncrypt(hKey, NULL, Final, dwFlags, NULL, &dwEncLen, 0)) {
197 // CryptEncrypt() always succeeds for output data size queries.
198 // dwEncLen contains required output data size. Continue as if the buffer was to small. Actually, the output buffer _was_ too small!
199 dwResult = ERROR_MORE_DATA;
200 } else
201 dwResult = GetLastError();
202
203 if (dwResult == ERROR_MORE_DATA) {
204 // Encrypted data will be longer. Reserve more space and retry.
205 aData.resize(((dwBufLen = dwEncLen) + sizeof(_Ty) - 1) / sizeof(_Ty));
206 dwEncLen = dwDataLen;
207 if (CryptEncrypt(hKey, hHash, Final, dwFlags, (BYTE*)aData.data(), &dwEncLen, dwBufLen)) {
208 // Encryption succeeded.
209 assert(dwEncLen <= dwBufLen);
210 if (dwEncLen < dwBufLen)
211 aData.resize((dwEncLen + sizeof(_Ty) - 1) / sizeof(_Ty));
212 return TRUE;
213 }
214 } else {
215 // Resize back to data length.
216 aData.resize((dwDataLen + sizeof(_Ty) - 1) / sizeof(_Ty));
217 }
218
219 return FALSE;
220}
221
227template<class _Ty, class _Ax>
228static _Success_(return != 0) BOOL CryptDecrypt(_In_ HCRYPTKEY hKey, _In_opt_ HCRYPTHASH hHash, _In_ BOOL Final, _In_ DWORD dwFlags, _Inout_ std::vector<_Ty, _Ax> &aData)
229{
230 DWORD dwDataLen = (DWORD)(aData.size() * sizeof(_Ty));
231
232 if (CryptDecrypt(hKey, hHash, Final, dwFlags, (BYTE*)aData.data(), &dwDataLen)) {
233 // Decryption succeeded.
234 aData.resize((dwDataLen + sizeof(_Ty) - 1) / sizeof(_Ty));
235 return TRUE;
236 }
237
238 return FALSE;
239}
240
242
243namespace winstd
244{
247
251 class cert_context : public dplhandle<PCCERT_CONTEXT, NULL>
252 {
254
255 public:
262 {
263 if (m_h != invalid)
265 }
266
276 bool create(_In_ DWORD dwCertEncodingType, _In_ LPCBYTE pbCertEncoded, _In_ DWORD cbCertEncoded) noexcept
277 {
278 handle_type h = CertCreateCertificateContext(dwCertEncodingType, pbCertEncoded, cbCertEncoded);
279 if (h != invalid) {
280 attach(h);
281 return true;
282 } else
283 return false;
284 }
285
294 bool operator==(_In_ const handle_type &other) const noexcept
295 {
296 // TODO: [Crypto] Make constant time.
297 return
298 m_h == other ||
299 m_h->cbCertEncoded == other->cbCertEncoded && memcmp(m_h->pbCertEncoded, other->pbCertEncoded, m_h->cbCertEncoded) == 0;
300 }
301
310 bool operator!=(_In_ const handle_type &other) const noexcept
311 {
312 return !operator==(other);
313 }
314
323 bool operator<(_In_ const handle_type &other) const noexcept
324 {
325 // TODO: [Crypto] Make constant time.
326 const int r = memcmp(m_h->pbCertEncoded, other->pbCertEncoded, std::min<DWORD>(m_h->cbCertEncoded, other->cbCertEncoded));
327 return r < 0 || r == 0 && m_h->cbCertEncoded < other->cbCertEncoded;
328 }
329
338 bool operator>(_In_ const handle_type &other) const noexcept
339 {
340 // TODO: [Crypto] Make constant time.
341 const int r = memcmp(m_h->pbCertEncoded, other->pbCertEncoded, std::min<DWORD>(m_h->cbCertEncoded, other->cbCertEncoded));
342 return r > 0 || r == 0 && m_h->cbCertEncoded > other->cbCertEncoded;
343 }
344
353 bool operator<=(_In_ const handle_type &other) const noexcept
354 {
355 return !operator>(other);
356 }
357
366 bool operator>=(_In_ const handle_type &other) const noexcept
367 {
368 return !operator<(other);
369 }
370
371 protected:
377 void free_internal() noexcept override
378 {
379 CertFreeCertificateContext(m_h);
380 }
381
391 handle_type duplicate_internal(_In_ handle_type h) const noexcept override
392 {
393 return CertDuplicateCertificateContext(h);
394 }
395 };
396
400 class cert_chain_context : public dplhandle<PCCERT_CHAIN_CONTEXT, NULL>
401 {
403
404 public:
411 {
412 if (m_h != invalid)
414 }
415
425 bool create(_In_opt_ HCERTCHAINENGINE hChainEngine, _In_ PCCERT_CONTEXT pCertContext, _In_opt_ LPFILETIME pTime, _In_opt_ HCERTSTORE hAdditionalStore, _In_ PCERT_CHAIN_PARA pChainPara, _In_ DWORD dwFlags, __reserved LPVOID pvReserved = NULL) noexcept
426 {
427 handle_type h;
428 if (CertGetCertificateChain(hChainEngine, pCertContext, pTime, hAdditionalStore, pChainPara, dwFlags, pvReserved, &h)) {
429 attach(h);
430 return true;
431 } else
432 return false;
433 }
434
435 protected:
441 void free_internal() noexcept override
442 {
443 CertFreeCertificateChain(m_h);
444 }
445
455 handle_type duplicate_internal(_In_ handle_type h) const noexcept override
456 {
457 return CertDuplicateCertificateChain(h);
458 }
459 };
460
464 class cert_store : public handle<HCERTSTORE, NULL>
465 {
467
468 public:
474 virtual ~cert_store()
475 {
476 if (m_h != invalid)
478 }
479
489 bool create(_In_ LPCSTR lpszStoreProvider, _In_ DWORD dwEncodingType, _In_opt_ HCRYPTPROV_LEGACY hCryptProv, _In_ DWORD dwFlags, _In_opt_ const void *pvPara) noexcept
490 {
491 handle_type h = CertOpenStore(lpszStoreProvider, dwEncodingType, hCryptProv, dwFlags, pvPara);
492 if (h != invalid) {
493 attach(h);
494 return true;
495 } else
496 return false;
497 }
498
508 bool create(_In_opt_ HCRYPTPROV_LEGACY hCryptProv, _In_z_ LPCTSTR szSubsystemProtocol) noexcept
509 {
510 handle_type h = CertOpenSystemStore(hCryptProv, szSubsystemProtocol);
511 if (h != invalid) {
512 attach(h);
513 return true;
514 } else
515 return false;
516 }
517
518 protected:
524 void free_internal() noexcept override
525 {
526 CertCloseStore(m_h, 0);
527 }
528 };
529
533 class crypt_prov : public handle<HCRYPTPROV, NULL>
534 {
536
537 public:
543 virtual ~crypt_prov()
544 {
545 if (m_h != invalid)
547 }
548
558 bool create(_In_opt_z_ LPCTSTR szContainer, _In_opt_z_ LPCTSTR szProvider, _In_ DWORD dwProvType, _In_ DWORD dwFlags = 0) noexcept
559 {
560 handle_type h;
561 if (CryptAcquireContext(&h, szContainer, szProvider, dwProvType, dwFlags)) {
562 attach(h);
563 return true;
564 } else
565 return false;
566 }
567
568 protected:
574 void free_internal() noexcept override
575 {
576 CryptReleaseContext(m_h, 0);
577 }
578 };
579
583 class crypt_hash : public dplhandle<HCRYPTHASH, NULL>
584 {
586
587 public:
593 virtual ~crypt_hash()
594 {
595 if (m_h != invalid)
597 }
598
608 bool create(_In_ HCRYPTPROV hProv, _In_ ALG_ID Algid, _In_opt_ HCRYPTKEY hKey = NULL, _In_opt_ DWORD dwFlags = 0) noexcept
609 {
610 handle_type h;
611 if (CryptCreateHash(hProv, Algid, hKey, dwFlags, &h)) {
612 attach(h);
613 return true;
614 } else
615 return false;
616 }
617
618 protected:
624 void free_internal() noexcept override
625 {
626 CryptDestroyHash(m_h);
627 }
628
638 handle_type duplicate_internal(_In_ handle_type h) const noexcept override
639 {
640 handle_type hNew = invalid;
641 return CryptDuplicateHash(h, NULL, 0, &hNew) ? hNew : invalid;
642 }
643 };
644
648 class crypt_key : public dplhandle<HCRYPTKEY, NULL>
649 {
651
652 public:
658 virtual ~crypt_key()
659 {
660 if (m_h != invalid)
662 }
663
669 bool generate(_In_ HCRYPTPROV hProv, _In_ ALG_ID Algid, _In_ DWORD dwFlags) noexcept
670 {
671 handle_type h;
672 if (CryptGenKey(hProv, Algid, dwFlags, &h)) {
673 attach(h);
674 return true;
675 } else
676 return false;
677 }
678
684 bool import(_In_ HCRYPTPROV hProv, __in_bcount(dwDataLen) LPCBYTE pbData, _In_ DWORD dwDataLen, _In_ HCRYPTKEY hPubKey, _In_ DWORD dwFlags) noexcept
685 {
686 handle_type h;
687 if (CryptImportKey(hProv, pbData, dwDataLen, hPubKey, dwFlags, &h)) {
688 attach(h);
689 return true;
690 } else
691 return false;
692 }
693
699 bool import_public(_In_ HCRYPTPROV hCryptProv, _In_ DWORD dwCertEncodingType, _In_ PCERT_PUBLIC_KEY_INFO pInfo) noexcept
700 {
701 handle_type h;
702 if (CryptImportPublicKeyInfo(hCryptProv, dwCertEncodingType, pInfo, &h)) {
703 attach(h);
704 return true;
705 } else
706 return false;
707 }
708
714 bool derive(_In_ HCRYPTPROV hProv, _In_ ALG_ID Algid, _In_ HCRYPTHASH hBaseData, _In_ DWORD dwFlags) noexcept
715 {
716 handle_type h;
717 if (CryptDeriveKey(hProv, Algid, hBaseData, dwFlags, &h)) {
718 attach(h);
719 return true;
720 } else
721 return false;
722 }
723
732 bool create_exp1(_In_ HCRYPTPROV hProv, _In_ DWORD dwKeySpec)
733 {
734 if (dwKeySpec != AT_KEYEXCHANGE && dwKeySpec != AT_SIGNATURE) {
735 SetLastError(ERROR_INVALID_PARAMETER);
736 return false;
737 }
738
739 // Generate the private key.
740 handle_type h;
741 if (CryptGenKey(hProv, dwKeySpec, CRYPT_EXPORTABLE, &h)) {
742 // Export the private key, we'll convert it to a private exponent of one key.
743 std::vector<BYTE, sanitizing_allocator<BYTE>> key_blob;
744 if (CryptExportKey(h, 0, PRIVATEKEYBLOB, 0, key_blob)) {
745 CryptDestroyKey(h);
746
747 // Get the byte length of the key.
748 size_t
749 size_key = *reinterpret_cast<DWORD*>(&key_blob[12])/8,
750 size_prime = size_key/2;
751
752 // Modify the Exponent in Key BLOB format
753 // Key BLOB format is documented in SDK
754
755 // Convert pubexp in rsapubkey to 1
756 LPBYTE ptr = &key_blob[16];
757 *reinterpret_cast<DWORD*>(ptr) = 1;
758 ptr += sizeof(DWORD);
759
760 // Skip modulus, prime1, prime2
761 ptr += size_key;
762 ptr += size_prime;
763 ptr += size_prime;
764
765 // Convert exponent1 to 1
766 ptr[0] = 1;
767 memset(ptr + 1, 0, size_prime - 1);
768 ptr += size_prime;
769
770 // Convert exponent2 to 1
771 ptr[0] = 1;
772 memset(ptr + 1, 0, size_prime - 1);
773 ptr += size_prime;
774
775 // Skip coefficient
776 ptr += size_prime;
777
778 // Convert privateExponent to 1
779 ptr[0] = 1;
780 memset(ptr + 1, 0, size_key - 1);
781
782 // Import the exponent-of-one private key.
783 if (CryptImportKey(hProv, key_blob.data(), static_cast<DWORD>(key_blob.size()), 0, 0, &h)) {
784 attach(h);
785 return true;
786 }
787 } else
788 CryptDestroyKey(h);
789 }
790
791 return false;
792 }
793
794 protected:
800 void free_internal() noexcept override
801 {
802 CryptDestroyKey(m_h);
803 }
804
814 handle_type duplicate_internal(_In_ handle_type h) const noexcept override
815 {
816 handle_type hNew = invalid;
817 return CryptDuplicateKey(h, NULL, 0, &hNew) ? hNew : invalid;
818 }
819 };
820
824 #pragma warning(push)
825 #pragma warning(disable: 26432) // Copy constructor and assignment operator are also present, but not detected by code analysis as they are using base type source object reference.
826 class data_blob : public DATA_BLOB
827 {
828 public:
832 data_blob() noexcept
833 {
834 cbData = 0;
835 pbData = NULL;
836 }
837
841 data_blob(_In_count_(size) BYTE *data, _In_ DWORD size) noexcept
842 {
843 cbData = size;
844 pbData = data;
845 }
846
850 data_blob(_In_ const DATA_BLOB &other)
851 {
852 cbData = other.cbData;
853 if (cbData) {
854 pbData = static_cast<BYTE*>(LocalAlloc(LMEM_FIXED, other.cbData));
855 if (!pbData) throw win_runtime_error("LocalAlloc failed.");
856 memcpy(pbData, other.pbData, other.cbData);
857 } else
858 pbData = NULL;
859 }
860
864 data_blob(_Inout_ data_blob &&other) noexcept
865 {
866 cbData = other.cbData;
867 pbData = other.pbData;
868 other.cbData = 0;
869 other.pbData = NULL;
870 }
871
875 virtual ~data_blob()
876 {
877 if (pbData != NULL)
878 LocalFree(pbData);
879 }
880
884 data_blob& operator=(_In_ const DATA_BLOB &other)
885 {
886 if (this != &other) {
887 cbData = other.cbData;
888 if (pbData)
889 LocalFree(pbData);
890 if (cbData) {
891 pbData = static_cast<BYTE*>(LocalAlloc(LMEM_FIXED, other.cbData));
892 if (!pbData) throw win_runtime_error("LocalAlloc failed.");
893 memcpy(pbData, other.pbData, other.cbData);
894 } else
895 pbData = NULL;
896 }
897
898 return *this;
899 }
900
904 data_blob& operator=(_Inout_ data_blob &&other) noexcept
905 {
906 if (this != &other) {
907 cbData = other.cbData;
908 if (pbData)
909 LocalFree(pbData);
910 pbData = other.pbData;
911 other.cbData = 0;
912 other.pbData = NULL;
913 }
914
915 return *this;
916 }
917
921 DWORD size() const noexcept
922 {
923 return cbData;
924 }
925
929 const BYTE* data() const noexcept
930 {
931 return pbData;
932 }
933
937 BYTE* data() noexcept
938 {
939 return pbData;
940 }
941 };
942 #pragma warning(pop)
943
945}
PCCERT_CHAIN_CONTEXT wrapper class.
Definition: Crypt.h:401
handle_type duplicate_internal(handle_type h) const noexcept override
Duplicates the certificate chain context.
Definition: Crypt.h:455
virtual ~cert_chain_context()
Destroys the certificate chain context.
Definition: Crypt.h:410
bool create(HCERTCHAINENGINE hChainEngine, PCCERT_CONTEXT pCertContext, LPFILETIME pTime, HCERTSTORE hAdditionalStore, PCERT_CHAIN_PARA pChainPara, DWORD dwFlags, __reserved LPVOID pvReserved=NULL) noexcept
Creates the certificate chain context.
Definition: Crypt.h:425
void free_internal() noexcept override
Destroys the certificate chain context.
Definition: Crypt.h:441
PCCERT_CONTEXT wrapper class.
Definition: Crypt.h:252
bool operator<=(const handle_type &other) const noexcept
Is certificate less than or equal?
Definition: Crypt.h:353
void free_internal() noexcept override
Destroys the certificate context.
Definition: Crypt.h:377
bool operator==(const handle_type &other) const noexcept
Is certificate equal to?
Definition: Crypt.h:294
handle_type duplicate_internal(handle_type h) const noexcept override
Duplicates the certificate context.
Definition: Crypt.h:391
bool create(DWORD dwCertEncodingType, LPCBYTE pbCertEncoded, DWORD cbCertEncoded) noexcept
Creates the certificate context.
Definition: Crypt.h:276
bool operator>=(const handle_type &other) const noexcept
Is certificate greater than or equal?
Definition: Crypt.h:366
bool operator>(const handle_type &other) const noexcept
Is certificate greater than?
Definition: Crypt.h:338
bool operator<(const handle_type &other) const noexcept
Is certificate less than?
Definition: Crypt.h:323
bool operator!=(const handle_type &other) const noexcept
Is certificate not equal to?
Definition: Crypt.h:310
virtual ~cert_context()
Destroys the certificate context.
Definition: Crypt.h:261
HCERTSTORE wrapper class.
Definition: Crypt.h:465
bool create(LPCSTR lpszStoreProvider, DWORD dwEncodingType, HCRYPTPROV_LEGACY hCryptProv, DWORD dwFlags, const void *pvPara) noexcept
Opens the certificate store.
Definition: Crypt.h:489
virtual ~cert_store()
Closes the certificate store.
Definition: Crypt.h:474
void free_internal() noexcept override
Closes the certificate store.
Definition: Crypt.h:524
bool create(HCRYPTPROV_LEGACY hCryptProv, LPCTSTR szSubsystemProtocol) noexcept
Opens the most common system certificate store. To open certificate stores with more complex requirem...
Definition: Crypt.h:508
HCRYPTHASH wrapper class.
Definition: Crypt.h:584
void free_internal() noexcept override
Destroys the hash context.
Definition: Crypt.h:624
bool create(HCRYPTPROV hProv, ALG_ID Algid, HCRYPTKEY hKey=NULL, DWORD dwFlags=0) noexcept
Creates the hash context.
Definition: Crypt.h:608
virtual ~crypt_hash()
Destroys the hash context.
Definition: Crypt.h:593
handle_type duplicate_internal(handle_type h) const noexcept override
Duplicates the hash context.
Definition: Crypt.h:638
HCRYPTKEY wrapper class.
Definition: Crypt.h:649
bool generate(HCRYPTPROV hProv, ALG_ID Algid, DWORD dwFlags) noexcept
Generates the key.
Definition: Crypt.h:669
bool derive(HCRYPTPROV hProv, ALG_ID Algid, HCRYPTHASH hBaseData, DWORD dwFlags) noexcept
Generates cryptographic session keys derived from a base data value.
Definition: Crypt.h:714
virtual ~crypt_key()
Destroys the key.
Definition: Crypt.h:658
bool create_exp1(HCRYPTPROV hProv, DWORD dwKeySpec)
Creates Exponent-of-one key.
Definition: Crypt.h:732
handle_type duplicate_internal(handle_type h) const noexcept override
Duplicates the key.
Definition: Crypt.h:814
bool import_public(HCRYPTPROV hCryptProv, DWORD dwCertEncodingType, PCERT_PUBLIC_KEY_INFO pInfo) noexcept
Imports the public key.
Definition: Crypt.h:699
void free_internal() noexcept override
Destroys the key.
Definition: Crypt.h:800
HCRYPTPROV wrapper class.
Definition: Crypt.h:534
bool create(LPCTSTR szContainer, LPCTSTR szProvider, DWORD dwProvType, DWORD dwFlags=0) noexcept
Acquires the cryptographic context.
Definition: Crypt.h:558
virtual ~crypt_prov()
Releases the cryptographic context.
Definition: Crypt.h:543
void free_internal() noexcept override
Releases the cryptographic context.
Definition: Crypt.h:574
DATA_BLOB wrapper class.
Definition: Crypt.h:827
data_blob(const DATA_BLOB &other)
Duplicate an existing BLOB.
Definition: Crypt.h:850
virtual ~data_blob()
Destroys the BLOB.
Definition: Crypt.h:875
BYTE * data() noexcept
Get BLOB buffer.
Definition: Crypt.h:937
const BYTE * data() const noexcept
Get BLOB buffer.
Definition: Crypt.h:929
data_blob() noexcept
Initializes an empty BLOB.
Definition: Crypt.h:832
data_blob(data_blob &&other) noexcept
Move an existing BLOB.
Definition: Crypt.h:864
data_blob & operator=(data_blob &&other) noexcept
Move an existing BLOB.
Definition: Crypt.h:904
data_blob(BYTE *data, DWORD size) noexcept
Initializes a BLOB from existing data.
Definition: Crypt.h:841
DWORD size() const noexcept
Get BLOB size.
Definition: Crypt.h:921
data_blob & operator=(const DATA_BLOB &other)
Copy an existing BLOB.
Definition: Crypt.h:884
Base abstract template class to support object handle keeping for objects that support handle duplica...
Definition: Common.h:866
Base abstract template class to support generic object handle keeping.
Definition: Common.h:604
PCCERT_CONTEXT handle_type
Datatype of the object handle this template class handles.
Definition: Common.h:609
handle_type m_h
Object handle.
Definition: Common.h:855
void attach(handle_type h) noexcept
Sets a new object handle for the class.
Definition: Common.h:818
Windows runtime error.
Definition: Common.h:1048
#define WINSTD_STACK_BUFFER_BYTES
Size of the stack buffer in bytes used for initial system function call.
Definition: Common.h:80
#define WINSTD_DPLHANDLE_IMPL(C, INVAL)
Implements default constructors and operators to prevent their auto-generation by compiler.
Definition: Common.h:174
#define WINSTD_HANDLE_IMPL(C, INVAL)
Implements default constructors and operators to prevent their auto-generation by compiler.
Definition: Common.h:162
static const PCCERT_CONTEXT invalid
Invalid handle value.
Definition: Common.h:614