WinStd
Windows Win32 API using Standard C++
Loading...
Searching...
No Matches
Crypt.h
1/*
2 SPDX-License-Identifier: MIT
3 Copyright © 1991-2023 Amebis
4 Copyright © 2016 GÉANT
5*/
6
8
9#pragma once
10
11#include "Common.h"
12#include <assert.h>
13#include <WinCrypt.h>
14#include <algorithm>
15#include <string>
16#include <vector>
17
20
22template<class _Traits, class _Ax>
23static DWORD CertGetNameStringA(_In_ PCCERT_CONTEXT pCertContext, _In_ DWORD dwType, _In_ DWORD dwFlags, _In_opt_ void *pvTypePara, _Out_ std::basic_string<char, _Traits, _Ax> &sNameString)
24{
25 // Query the final string length first.
26 DWORD dwSize = ::CertGetNameStringA(pCertContext, dwType, dwFlags, pvTypePara, NULL, 0);
27
28 // Allocate buffer on heap to format the string data into and read it.
29 std::unique_ptr<char[]> szBuffer(new char[dwSize]);
30 dwSize = ::CertGetNameStringA(pCertContext, dwType, dwFlags, pvTypePara, szBuffer.get(), dwSize);
31 sNameString.assign(szBuffer.get(), dwSize - 1);
32 return dwSize;
33}
34
40template<class _Traits, class _Ax>
41static 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)
42{
43 // Query the final string length first.
44 DWORD dwSize = ::CertGetNameStringW(pCertContext, dwType, dwFlags, pvTypePara, NULL, 0);
45
46 // Allocate buffer on heap to format the string data into and read it.
47 std::unique_ptr<wchar_t[]> szBuffer(new wchar_t[dwSize]);
48 dwSize = ::CertGetNameStringW(pCertContext, dwType, dwFlags, pvTypePara, szBuffer.get(), dwSize);
49 sNameString.assign(szBuffer.get(), dwSize - 1);
50 return dwSize;
51}
52
58template<class _Ty, class _Ax>
59static _Success_(return != 0) BOOL WINAPI CertGetCertificateContextProperty(_In_ PCCERT_CONTEXT pCertContext, _In_ DWORD dwPropId, _Out_ std::vector<_Ty, _Ax> &aData)
60{
62 DWORD dwSize = WINSTD_STACK_BUFFER_BYTES;
63
64 // Try with the stack buffer first.
65 if (CertGetCertificateContextProperty(pCertContext, dwPropId, buf, &dwSize)) {
66 // Copy from stack.
67 aData.assign((const _Ty*)buf, (const _Ty*)buf + (dwSize + sizeof(_Ty) - 1) / sizeof(_Ty));
68 return TRUE;
69 } else if (GetLastError() == ERROR_MORE_DATA) {
70 aData.resize((dwSize + sizeof(_Ty) - 1) / sizeof(_Ty));
71 if (CertGetCertificateContextProperty(pCertContext, dwPropId, aData.data(), &dwSize))
72 return TRUE;
73 }
74
75 return FALSE;
76}
77
83template<class _Ty, class _Ax>
84static _Success_(return != 0) BOOL CryptGetHashParam(_In_ HCRYPTHASH hHash, _In_ DWORD dwParam, _Out_ std::vector<_Ty, _Ax> &aData, _In_ DWORD dwFlags)
85{
87 DWORD dwSize = WINSTD_STACK_BUFFER_BYTES;
88
89 // Try with the stack buffer first.
90 if (CryptGetHashParam(hHash, dwParam, buf, &dwSize, dwFlags)) {
91 // Copy from stack.
92 aData.assign((const _Ty*)buf, (const _Ty*)buf + (dwSize + sizeof(_Ty) - 1) / sizeof(_Ty));
93 return TRUE;
94 } else if (GetLastError() == ERROR_MORE_DATA) {
95 aData.resize((dwSize + sizeof(_Ty) - 1) / sizeof(_Ty));
96 if (CryptGetHashParam(hHash, dwParam, reinterpret_cast<BYTE*>(aData.data()), &dwSize, dwFlags))
97 return TRUE;
98 }
99
100 return FALSE;
101}
102
108template<class T>
109static _Success_(return != 0) BOOL CryptGetHashParam(_In_ HCRYPTHASH hHash, _In_ DWORD dwParam, _Out_ T &data, _In_ DWORD dwFlags)
110{
111 DWORD dwSize = sizeof(T);
112 return CryptGetHashParam(hHash, dwParam, (BYTE*)&data, &dwSize, dwFlags);
113}
114
120template<class _Ty, class _Ax>
121static _Success_(return != 0) BOOL CryptGetKeyParam(_In_ HCRYPTKEY hKey, _In_ DWORD dwParam, _Out_ std::vector<_Ty, _Ax> &aData, _In_ DWORD dwFlags)
122{
124 DWORD dwSize = WINSTD_STACK_BUFFER_BYTES;
125
126 // Try with the stack buffer first.
127 if (CryptGetKeyParam(hKey, dwParam, buf, &dwSize, dwFlags)) {
128 // Copy from stack.
129 aData.assign((const _Ty*)buf, (const _Ty*)buf + (dwSize + sizeof(_Ty) - 1) / sizeof(_Ty));
130 return TRUE;
131 } else if (GetLastError() == ERROR_MORE_DATA) {
132 aData.resize((dwSize + sizeof(_Ty) - 1) / sizeof(_Ty));
133 if (CryptGetKeyParam(hKey, dwParam, reinterpret_cast<BYTE*>(aData.data()), &dwSize, dwFlags))
134 return TRUE;
135 }
136
137 return FALSE;
138}
139
145template<class T>
146static BOOL CryptGetKeyParam(_In_ HCRYPTKEY hKey, _In_ DWORD dwParam, _Out_ T &data, _In_ DWORD dwFlags)
147{
148 DWORD dwSize = sizeof(T);
149 return CryptGetKeyParam(hKey, dwParam, (BYTE*)&data, &dwSize, dwFlags);
150}
151
157template<class _Ty, class _Ax>
158static _Success_(return != 0) BOOL CryptExportKey(_In_ HCRYPTKEY hKey, _In_ HCRYPTKEY hExpKey, _In_ DWORD dwBlobType, _In_ DWORD dwFlags, _Out_ std::vector<_Ty, _Ax> &aData)
159{
160 DWORD dwKeyBLOBSize = 0;
161
162 if (CryptExportKey(hKey, hExpKey, dwBlobType, dwFlags, NULL, &dwKeyBLOBSize)) {
163 aData.resize((dwKeyBLOBSize + sizeof(_Ty) - 1) / sizeof(_Ty));
164 if (CryptExportKey(hKey, hExpKey, dwBlobType, dwFlags, reinterpret_cast<BYTE*>(aData.data()), &dwKeyBLOBSize))
165 return TRUE;
166 }
167
168 return FALSE;
169}
170
176template<class _Ty, class _Ax>
177static _Success_(return != 0) BOOL CryptEncrypt(_In_ HCRYPTKEY hKey, _In_opt_ HCRYPTHASH hHash, _In_ BOOL Final, _In_ DWORD dwFlags, _Inout_ std::vector<_Ty, _Ax> &aData)
178{
179 DWORD
180 dwDataLen = (DWORD)(aData.size() * sizeof(_Ty)),
181 dwBufLen = (DWORD)(aData.capacity() * sizeof(_Ty)),
182 dwEncLen = dwDataLen,
183 dwResult;
184
185 if (dwBufLen) {
186 aData.resize(dwBufLen);
187 if (CryptEncrypt(hKey, hHash, Final, dwFlags, reinterpret_cast<BYTE*>(aData.data()), &dwEncLen, dwBufLen)) {
188 // Encryption succeeded.
189 assert(dwEncLen <= dwBufLen);
190 if (dwEncLen < dwBufLen)
191 aData.resize((dwEncLen + sizeof(_Ty) - 1) / sizeof(_Ty));
192 return TRUE;
193 } else
194 dwResult = GetLastError();
195 } else if (CryptEncrypt(hKey, NULL, Final, dwFlags, NULL, &dwEncLen, 0)) {
196 // CryptEncrypt() always succeeds for output data size queries.
197 // dwEncLen contains required output data size. Continue as if the buffer was to small. Actually, the output buffer _was_ too small!
198 dwResult = ERROR_MORE_DATA;
199 } else
200 dwResult = GetLastError();
201
202 if (dwResult == ERROR_MORE_DATA) {
203 // Encrypted data will be longer. Reserve more space and retry.
204 aData.resize(((dwBufLen = dwEncLen) + sizeof(_Ty) - 1) / sizeof(_Ty));
205 dwEncLen = dwDataLen;
206 if (CryptEncrypt(hKey, hHash, Final, dwFlags, reinterpret_cast<BYTE*>(aData.data()), &dwEncLen, dwBufLen)) {
207 // Encryption succeeded.
208 assert(dwEncLen <= dwBufLen);
209 if (dwEncLen < dwBufLen)
210 aData.resize((dwEncLen + sizeof(_Ty) - 1) / sizeof(_Ty));
211 return TRUE;
212 }
213 } else {
214 // Resize back to data length.
215 aData.resize((dwDataLen + sizeof(_Ty) - 1) / sizeof(_Ty));
216 }
217
218 return FALSE;
219}
220
226template<class _Ty, class _Ax>
227static _Success_(return != 0) BOOL CryptDecrypt(_In_ HCRYPTKEY hKey, _In_opt_ HCRYPTHASH hHash, _In_ BOOL Final, _In_ DWORD dwFlags, _Inout_ std::vector<_Ty, _Ax> &aData)
228{
229 DWORD dwDataLen = (DWORD)(aData.size() * sizeof(_Ty));
230
231 if (CryptDecrypt(hKey, hHash, Final, dwFlags, reinterpret_cast<BYTE*>(aData.data()), &dwDataLen)) {
232 // Decryption succeeded.
233 aData.resize((dwDataLen + sizeof(_Ty) - 1) / sizeof(_Ty));
234 return TRUE;
235 }
236
237 return FALSE;
238}
239
241
242namespace winstd
243{
246
252 class cert_context : public dplhandle<PCCERT_CONTEXT, NULL>
253 {
255
256 public:
263 {
264 if (m_h != invalid)
266 }
267
276 bool operator==(_In_ const handle_type &other) const noexcept
277 {
278 // TODO: [Crypto] Make constant time.
279 return
280 m_h == other ||
281 m_h->cbCertEncoded == other->cbCertEncoded && memcmp(m_h->pbCertEncoded, other->pbCertEncoded, m_h->cbCertEncoded) == 0;
282 }
283
292 bool operator!=(_In_ const handle_type &other) const noexcept
293 {
294 return !operator==(other);
295 }
296
305 bool operator<(_In_ const handle_type &other) const noexcept
306 {
307 // TODO: [Crypto] Make constant time.
308 const int r = memcmp(m_h->pbCertEncoded, other->pbCertEncoded, std::min<DWORD>(m_h->cbCertEncoded, other->cbCertEncoded));
309 return r < 0 || r == 0 && m_h->cbCertEncoded < other->cbCertEncoded;
310 }
311
320 bool operator>(_In_ const handle_type &other) const noexcept
321 {
322 // TODO: [Crypto] Make constant time.
323 const int r = memcmp(m_h->pbCertEncoded, other->pbCertEncoded, std::min<DWORD>(m_h->cbCertEncoded, other->cbCertEncoded));
324 return r > 0 || r == 0 && m_h->cbCertEncoded > other->cbCertEncoded;
325 }
326
335 bool operator<=(_In_ const handle_type &other) const noexcept
336 {
337 return !operator>(other);
338 }
339
348 bool operator>=(_In_ const handle_type &other) const noexcept
349 {
350 return !operator<(other);
351 }
352
353 protected:
359 void free_internal() noexcept override
360 {
361 CertFreeCertificateContext(m_h);
362 }
363
374 {
375 // As per doc, this only increases refcounter. Should never fail.
376 return CertDuplicateCertificateContext(h);
377 }
378 };
379
385 class cert_chain_context : public dplhandle<PCCERT_CHAIN_CONTEXT, NULL>
386 {
388
389 public:
396 {
397 if (m_h != invalid)
399 }
400
401 protected:
407 void free_internal() noexcept override
408 {
409 CertFreeCertificateChain(m_h);
410 }
411
422 {
423 // As per doc, this only increases refcounter. Should never fail.
424 return CertDuplicateCertificateChain(h);
425 }
426 };
427
434 class cert_store : public handle<HCERTSTORE, NULL>
435 {
437
438 public:
444 virtual ~cert_store()
445 {
446 if (m_h != invalid)
448 }
449
450 protected:
456 void free_internal() noexcept override
457 {
458 CertCloseStore(m_h, 0);
459 }
460 };
461
467 class crypt_prov : public handle<HCRYPTPROV, NULL>
468 {
470
471 public:
477 virtual ~crypt_prov()
478 {
479 if (m_h != invalid)
481 }
482
483 protected:
489 void free_internal() noexcept override
490 {
491 CryptReleaseContext(m_h, 0);
492 }
493 };
494
500 class crypt_hash : public dplhandle<HCRYPTHASH, NULL>
501 {
503
504 public:
510 virtual ~crypt_hash()
511 {
512 if (m_h != invalid)
514 }
515
516 protected:
522 void free_internal() noexcept override
523 {
524 CryptDestroyHash(m_h);
525 }
526
537 {
538 handle_type hNew;
539 if (CryptDuplicateHash(h, NULL, 0, &hNew))
540 return hNew;
541 throw win_runtime_error("CryptDuplicateHash failed");
542 }
543 };
544
553 class crypt_key : public dplhandle<HCRYPTKEY, NULL>
554 {
556
557 public:
563 virtual ~crypt_key()
564 {
565 if (m_h != invalid)
567 }
568
577 bool create_exp1(_In_ HCRYPTPROV hProv, _In_ DWORD dwKeySpec)
578 {
579 if (dwKeySpec != AT_KEYEXCHANGE && dwKeySpec != AT_SIGNATURE) {
580 SetLastError(ERROR_INVALID_PARAMETER);
581 return false;
582 }
583
584 // Generate the private key.
585 handle_type h;
586 if (CryptGenKey(hProv, dwKeySpec, CRYPT_EXPORTABLE, &h)) {
587 // Export the private key, we'll convert it to a private exponent of one key.
588 std::vector<BYTE, sanitizing_allocator<BYTE>> key_blob;
589 if (CryptExportKey(h, 0, PRIVATEKEYBLOB, 0, key_blob)) {
590 CryptDestroyKey(h);
591
592 // Get the byte length of the key.
593 size_t
594 size_key = *reinterpret_cast<DWORD*>(&key_blob[12])/8,
595 size_prime = size_key/2;
596
597 // Modify the Exponent in Key BLOB format
598 // Key BLOB format is documented in SDK
599
600 // Convert pubexp in rsapubkey to 1
601 LPBYTE ptr = &key_blob[16];
602 *reinterpret_cast<DWORD*>(ptr) = 1;
603 ptr += sizeof(DWORD);
604
605 // Skip modulus, prime1, prime2
606 ptr += size_key;
607 ptr += size_prime;
608 ptr += size_prime;
609
610 // Convert exponent1 to 1
611 ptr[0] = 1;
612 memset(ptr + 1, 0, size_prime - 1);
613 ptr += size_prime;
614
615 // Convert exponent2 to 1
616 ptr[0] = 1;
617 memset(ptr + 1, 0, size_prime - 1);
618 ptr += size_prime;
619
620 // Skip coefficient
621 ptr += size_prime;
622
623 // Convert privateExponent to 1
624 ptr[0] = 1;
625 memset(ptr + 1, 0, size_key - 1);
626
627 // Import the exponent-of-one private key.
628 if (CryptImportKey(hProv, key_blob.data(), static_cast<DWORD>(key_blob.size()), 0, 0, &h)) {
629 attach(h);
630 return true;
631 }
632 } else
633 CryptDestroyKey(h);
634 }
635
636 return false;
637 }
638
639 protected:
645 void free_internal() noexcept override
646 {
647 CryptDestroyKey(m_h);
648 }
649
660 {
661 handle_type hNew;
662 if (CryptDuplicateKey(h, NULL, 0, &hNew))
663 return hNew;
664 throw win_runtime_error("CryptDuplicateKey failed");
665 }
666 };
667
671 #pragma warning(push)
672 #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.
673 class data_blob : public DATA_BLOB
674 {
675 public:
679 data_blob() noexcept
680 {
681 cbData = 0;
682 pbData = NULL;
683 }
684
688 data_blob(_In_count_(size) BYTE *data, _In_ DWORD size) noexcept
689 {
690 cbData = size;
691 pbData = data;
692 }
693
697 data_blob(_In_ const DATA_BLOB &other)
698 {
699 cbData = other.cbData;
700 if (cbData) {
701 pbData = static_cast<BYTE*>(LocalAlloc(LMEM_FIXED, other.cbData));
702 if (!pbData) throw win_runtime_error("LocalAlloc failed");
703 memcpy(pbData, other.pbData, other.cbData);
704 } else
705 pbData = NULL;
706 }
707
711 data_blob(_Inout_ data_blob &&other) noexcept
712 {
713 cbData = other.cbData;
714 pbData = other.pbData;
715 other.cbData = 0;
716 other.pbData = NULL;
717 }
718
722 virtual ~data_blob()
723 {
724 if (pbData != NULL)
725 LocalFree(pbData);
726 }
727
731 data_blob& operator=(_In_ const DATA_BLOB &other)
732 {
733 if (this != &other) {
734 cbData = other.cbData;
735 if (pbData)
736 LocalFree(pbData);
737 if (cbData) {
738 pbData = static_cast<BYTE*>(LocalAlloc(LMEM_FIXED, other.cbData));
739 if (!pbData) throw win_runtime_error("LocalAlloc failed");
740 memcpy(pbData, other.pbData, other.cbData);
741 } else
742 pbData = NULL;
743 }
744
745 return *this;
746 }
747
751 data_blob& operator=(_Inout_ data_blob &&other) noexcept
752 {
753 if (this != &other) {
754 cbData = other.cbData;
755 if (pbData)
756 LocalFree(pbData);
757 pbData = other.pbData;
758 other.cbData = 0;
759 other.pbData = NULL;
760 }
761
762 return *this;
763 }
764
768 DWORD size() const noexcept
769 {
770 return cbData;
771 }
772
776 const BYTE* data() const noexcept
777 {
778 return pbData;
779 }
780
784 BYTE* data() noexcept
785 {
786 return pbData;
787 }
788 };
789 #pragma warning(pop)
790
792}
793
796
797#pragma warning(push)
798#pragma warning(disable: 4505) // Don't warn on unused code
799
805static BOOL CertGetCertificateChain(_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, _Inout_ winstd::cert_chain_context &ctx)
806{
807 PCCERT_CHAIN_CONTEXT pChainContext;
808 BOOL bResult = CertGetCertificateChain(hChainEngine, pCertContext, pTime, hAdditionalStore, pChainPara, dwFlags, pvReserved, &pChainContext);
809 if (bResult)
810 ctx.attach(pChainContext);
811 return bResult;
812}
813
815static BOOL CryptAcquireContextA(_Inout_ winstd::crypt_prov &prov, _In_opt_ LPCSTR szContainer, _In_opt_ LPCSTR szProvider, _In_ DWORD dwProvType, _In_ DWORD dwFlags)
816{
817 HCRYPTPROV h;
818 BOOL bResult = CryptAcquireContextA(&h, szContainer, szProvider, dwProvType, dwFlags);
819 if (bResult)
820 prov.attach(h);
821 return bResult;
822}
823
829static BOOL CryptAcquireContextW(_Inout_ winstd::crypt_prov &prov, _In_opt_ LPCWSTR szContainer, _In_opt_ LPCWSTR szProvider, _In_ DWORD dwProvType, _In_ DWORD dwFlags)
830{
831 HCRYPTPROV h;
832 BOOL bResult = CryptAcquireContextW(&h, szContainer, szProvider, dwProvType, dwFlags);
833 if (bResult)
834 prov.attach(h);
835 return bResult;
836}
837
843static BOOL CryptCreateHash(_In_ HCRYPTPROV hProv, _In_ ALG_ID Algid, _In_ HCRYPTKEY hKey, _In_ DWORD dwFlags, _Inout_ winstd::crypt_hash &hash)
844{
845 HCRYPTHASH h;
846 BOOL bResult = CryptCreateHash(hProv, Algid, hKey, dwFlags, &h);
847 if (bResult)
848 hash.attach(h);
849 return bResult;
850}
851
857static BOOL CryptGenKey(_In_ HCRYPTPROV hProv, _In_ ALG_ID Algid, _In_ DWORD dwFlags, _Inout_ winstd::crypt_key &key)
858{
859 HCRYPTKEY h;
860 BOOL bResult = CryptGenKey(hProv, Algid, dwFlags, &h);
861 if (bResult)
862 key.attach(h);
863 return bResult;
864}
865
871static bool CryptImportKey(_In_ HCRYPTPROV hProv, __in_bcount(dwDataLen) LPCBYTE pbData, _In_ DWORD dwDataLen, _In_ HCRYPTKEY hPubKey, _In_ DWORD dwFlags, _Inout_ winstd::crypt_key &key)
872{
873 HCRYPTKEY h;
874 BOOL bResult = CryptImportKey(hProv, pbData, dwDataLen, hPubKey, dwFlags, &h);
875 if (bResult)
876 key.attach(h);
877 return bResult;
878}
879
885static bool CryptImportPublicKeyInfo(_In_ HCRYPTPROV hCryptProv, _In_ DWORD dwCertEncodingType, _In_ PCERT_PUBLIC_KEY_INFO pInfo, _Inout_ winstd::crypt_key &key)
886{
887 HCRYPTKEY h;
888 BOOL bResult = CryptImportPublicKeyInfo(hCryptProv, dwCertEncodingType, pInfo, &h);
889 if (bResult)
890 key.attach(h);
891 return bResult;
892}
893
899static bool CryptDeriveKey(_In_ HCRYPTPROV hProv, _In_ ALG_ID Algid, _In_ HCRYPTHASH hBaseData, _In_ DWORD dwFlags, _Inout_ winstd::crypt_key &key)
900{
901 HCRYPTKEY h;
902 BOOL bResult = CryptDeriveKey(hProv, Algid, hBaseData, dwFlags, &h);
903 if (bResult)
904 key.attach(h);
905 return bResult;
906}
907
908#pragma warning(pop)
909
PCCERT_CHAIN_CONTEXT wrapper class.
Definition Crypt.h:386
virtual ~cert_chain_context()
Destroys the certificate chain context.
Definition Crypt.h:395
void free_internal() noexcept override
Destroys the certificate chain context.
Definition Crypt.h:407
handle_type duplicate_internal(handle_type h) const override
Duplicates the certificate chain context.
Definition Crypt.h:421
PCCERT_CONTEXT wrapper class.
Definition Crypt.h:253
bool operator<=(const handle_type &other) const noexcept
Is certificate less than or equal?
Definition Crypt.h:335
void free_internal() noexcept override
Destroys the certificate context.
Definition Crypt.h:359
bool operator==(const handle_type &other) const noexcept
Is certificate equal to?
Definition Crypt.h:276
bool operator>=(const handle_type &other) const noexcept
Is certificate greater than or equal?
Definition Crypt.h:348
bool operator>(const handle_type &other) const noexcept
Is certificate greater than?
Definition Crypt.h:320
bool operator<(const handle_type &other) const noexcept
Is certificate less than?
Definition Crypt.h:305
bool operator!=(const handle_type &other) const noexcept
Is certificate not equal to?
Definition Crypt.h:292
handle_type duplicate_internal(handle_type h) const override
Duplicates the certificate context.
Definition Crypt.h:373
virtual ~cert_context()
Destroys the certificate context.
Definition Crypt.h:262
HCERTSTORE wrapper class.
Definition Crypt.h:435
virtual ~cert_store()
Closes the certificate store.
Definition Crypt.h:444
void free_internal() noexcept override
Closes the certificate store.
Definition Crypt.h:456
HCRYPTHASH wrapper class.
Definition Crypt.h:501
void free_internal() noexcept override
Destroys the hash context.
Definition Crypt.h:522
virtual ~crypt_hash()
Destroys the hash context.
Definition Crypt.h:510
handle_type duplicate_internal(handle_type h) const override
Duplicates the hash context.
Definition Crypt.h:536
HCRYPTKEY wrapper class.
Definition Crypt.h:554
handle_type duplicate_internal(handle_type h) const override
Duplicates the key.
Definition Crypt.h:659
virtual ~crypt_key()
Destroys the key.
Definition Crypt.h:563
bool create_exp1(HCRYPTPROV hProv, DWORD dwKeySpec)
Creates Exponent-of-one key.
Definition Crypt.h:577
void free_internal() noexcept override
Destroys the key.
Definition Crypt.h:645
HCRYPTPROV wrapper class.
Definition Crypt.h:468
virtual ~crypt_prov()
Releases the cryptographic context.
Definition Crypt.h:477
void free_internal() noexcept override
Releases the cryptographic context.
Definition Crypt.h:489
DATA_BLOB wrapper class.
Definition Crypt.h:674
data_blob(const DATA_BLOB &other)
Duplicate an existing BLOB.
Definition Crypt.h:697
virtual ~data_blob()
Destroys the BLOB.
Definition Crypt.h:722
BYTE * data() noexcept
Get BLOB buffer.
Definition Crypt.h:784
const BYTE * data() const noexcept
Get BLOB buffer.
Definition Crypt.h:776
data_blob() noexcept
Initializes an empty BLOB.
Definition Crypt.h:679
data_blob(data_blob &&other) noexcept
Move an existing BLOB.
Definition Crypt.h:711
data_blob & operator=(data_blob &&other) noexcept
Move an existing BLOB.
Definition Crypt.h:751
data_blob(BYTE *data, DWORD size) noexcept
Initializes a BLOB from existing data.
Definition Crypt.h:688
DWORD size() const noexcept
Get BLOB size.
Definition Crypt.h:768
data_blob & operator=(const DATA_BLOB &other)
Copy an existing BLOB.
Definition Crypt.h:731
Base abstract template class to support object handle keeping for objects that support trivial handle...
Definition Common.h:1248
Base abstract template class to support generic object handle keeping.
Definition Common.h:983
T handle_type
Datatype of the object handle this template class handles.
Definition Common.h:988
handle_type m_h
Object handle.
Definition Common.h:1237
void attach(handle_type h) noexcept
Sets a new object handle for the class.
Definition Common.h:1200
Windows runtime error.
Definition Common.h:1491
static bool CryptImportPublicKeyInfo(HCRYPTPROV hCryptProv, DWORD dwCertEncodingType, PCERT_PUBLIC_KEY_INFO pInfo, winstd::crypt_key &key)
Imports the public key.
Definition Crypt.h:885
static BOOL WINAPI CertGetCertificateContextProperty(PCCERT_CONTEXT pCertContext, DWORD dwPropId, std::vector< _Ty, _Ax > &aData)
Retrieves the information contained in an extended property of a certificate context.
Definition Crypt.h:59
static BOOL CertGetCertificateChain(HCERTCHAINENGINE hChainEngine, PCCERT_CONTEXT pCertContext, LPFILETIME pTime, HCERTSTORE hAdditionalStore, PCERT_CHAIN_PARA pChainPara, DWORD dwFlags, LPVOID pvReserved, winstd::cert_chain_context &ctx)
The CertGetCertificateChain function builds a certificate chain context starting from an end certific...
Definition Crypt.h:805
static BOOL CryptGetHashParam(HCRYPTHASH hHash, DWORD dwParam, std::vector< _Ty, _Ax > &aData, DWORD dwFlags)
Retrieves data that governs the operations of a hash object. The actual hash value can be retrieved b...
Definition Crypt.h:84
static DWORD CertGetNameStringW(PCCERT_CONTEXT pCertContext, DWORD dwType, DWORD dwFlags, void *pvTypePara, std::basic_string< wchar_t, _Traits, _Ax > &sNameString)
Obtains the subject or issuer name from a certificate CERT_CONTEXT structure and stores it in a std::...
Definition Crypt.h:41
static BOOL CryptAcquireContextA(winstd::crypt_prov &prov, LPCSTR szContainer, LPCSTR szProvider, DWORD dwProvType, DWORD dwFlags)
Acquires the cryptographic context.
Definition Crypt.h:815
static DWORD CertGetNameStringA(PCCERT_CONTEXT pCertContext, DWORD dwType, DWORD dwFlags, void *pvTypePara, std::basic_string< char, _Traits, _Ax > &sNameString)
Obtains the subject or issuer name from a certificate CERT_CONTEXT structure and stores it in a std::...
Definition Crypt.h:23
static BOOL CryptGenKey(HCRYPTPROV hProv, ALG_ID Algid, DWORD dwFlags, winstd::crypt_key &key)
Generates the key.
Definition Crypt.h:857
static BOOL CryptExportKey(HCRYPTKEY hKey, HCRYPTKEY hExpKey, DWORD dwBlobType, DWORD dwFlags, std::vector< _Ty, _Ax > &aData)
Exports a cryptographic key or a key pair from a cryptographic service provider (CSP) in a secure man...
Definition Crypt.h:158
static BOOL CryptGetKeyParam(HCRYPTKEY hKey, DWORD dwParam, std::vector< _Ty, _Ax > &aData, DWORD dwFlags)
Retrieves data that governs the operations of a key.
Definition Crypt.h:121
static BOOL CryptCreateHash(HCRYPTPROV hProv, ALG_ID Algid, HCRYPTKEY hKey, DWORD dwFlags, winstd::crypt_hash &hash)
Creates the hash context.
Definition Crypt.h:843
static BOOL CryptAcquireContextW(winstd::crypt_prov &prov, LPCWSTR szContainer, LPCWSTR szProvider, DWORD dwProvType, DWORD dwFlags)
Acquires the cryptographic context.
Definition Crypt.h:829
static BOOL CryptEncrypt(HCRYPTKEY hKey, HCRYPTHASH hHash, BOOL Final, DWORD dwFlags, std::vector< _Ty, _Ax > &aData)
Encrypts data.
Definition Crypt.h:177
static bool CryptDeriveKey(HCRYPTPROV hProv, ALG_ID Algid, HCRYPTHASH hBaseData, DWORD dwFlags, winstd::crypt_key &key)
Generates cryptographic session keys derived from a base data value.
Definition Crypt.h:899
static BOOL CryptDecrypt(HCRYPTKEY hKey, HCRYPTHASH hHash, BOOL Final, DWORD dwFlags, std::vector< _Ty, _Ax > &aData)
Decrypts data previously encrypted by using the CryptEncrypt function.
Definition Crypt.h:227
static bool CryptImportKey(HCRYPTPROV hProv, __in_bcount(dwDataLen) LPCBYTE pbData, DWORD dwDataLen, HCRYPTKEY hPubKey, DWORD dwFlags, winstd::crypt_key &key)
Imports the key.
Definition Crypt.h:871
#define WINSTD_STACK_BUFFER_BYTES
Size of the stack buffer in bytes used for initial system function call.
Definition Common.h:93
#define WINSTD_DPLHANDLE_IMPL(C, INVAL)
Implements default constructors and operators to prevent their auto-generation by compiler.
Definition Common.h:175
#define WINSTD_HANDLE_IMPL(C, INVAL)
Implements default constructors and operators to prevent their auto-generation by compiler.
Definition Common.h:163
static const T invalid
Invalid handle value.
Definition Common.h:993