WinStd
Additional templates and function helpers for Microsoft Windows using Standard C++ classes
Crypt.h
Go to the documentation of this file.
1/*
2 SPDX-License-Identifier: MIT
3 Copyright © 1991-2022 Amebis
4 Copyright © 2016 GÉANT
5*/
6
12
13#pragma once
14
15#include "Common.h"
16#include <assert.h>
17#include <WinCrypt.h>
18#include <algorithm>
19#include <string>
20#include <vector>
21
24
26template<class _Traits, class _Ax>
27static DWORD CertGetNameStringA(_In_ PCCERT_CONTEXT pCertContext, _In_ DWORD dwType, _In_ DWORD dwFlags, _In_opt_ void *pvTypePara, _Out_ std::basic_string<char, _Traits, _Ax> &sNameString)
28{
29 // Query the final string length first.
30 DWORD dwSize = ::CertGetNameStringA(pCertContext, dwType, dwFlags, pvTypePara, NULL, 0);
31
32 // Allocate buffer on heap to format the string data into and read it.
33 std::unique_ptr<char[]> szBuffer(new char[dwSize]);
34 dwSize = ::CertGetNameStringA(pCertContext, dwType, dwFlags, pvTypePara, szBuffer.get(), dwSize);
35 sNameString.assign(szBuffer.get(), dwSize - 1);
36 return dwSize;
37}
38
44template<class _Traits, class _Ax>
45static 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)
46{
47 // Query the final string length first.
48 DWORD dwSize = ::CertGetNameStringW(pCertContext, dwType, dwFlags, pvTypePara, NULL, 0);
49
50 // Allocate buffer on heap to format the string data into and read it.
51 std::unique_ptr<wchar_t[]> szBuffer(new wchar_t[dwSize]);
52 dwSize = ::CertGetNameStringW(pCertContext, dwType, dwFlags, pvTypePara, szBuffer.get(), dwSize);
53 sNameString.assign(szBuffer.get(), dwSize - 1);
54 return dwSize;
55}
56
62template<class _Ty, class _Ax>
63static _Success_(return != 0) BOOL WINAPI CertGetCertificateContextProperty(_In_ PCCERT_CONTEXT pCertContext, _In_ DWORD dwPropId, _Out_ std::vector<_Ty, _Ax> &aData)
64{
66 DWORD dwSize = WINSTD_STACK_BUFFER_BYTES;
67
68 // Try with the stack buffer first.
69 if (CertGetCertificateContextProperty(pCertContext, dwPropId, buf, &dwSize)) {
70 // Copy from stack.
71 aData.assign((const _Ty*)buf, (const _Ty*)buf + (dwSize + sizeof(_Ty) - 1) / sizeof(_Ty));
72 return TRUE;
73 } else if (GetLastError() == ERROR_MORE_DATA) {
74 aData.resize((dwSize + sizeof(_Ty) - 1) / sizeof(_Ty));
75 if (CertGetCertificateContextProperty(pCertContext, dwPropId, (BYTE*)aData.data(), &dwSize))
76 return TRUE;
77 }
78
79 return FALSE;
80}
81
87template<class _Ty, class _Ax>
88static _Success_(return != 0) BOOL CryptGetHashParam(_In_ HCRYPTHASH hHash, _In_ DWORD dwParam, _Out_ std::vector<_Ty, _Ax> &aData, _In_ DWORD dwFlags)
89{
91 DWORD dwSize = WINSTD_STACK_BUFFER_BYTES;
92
93 // Try with the stack buffer first.
94 if (CryptGetHashParam(hHash, dwParam, buf, &dwSize, dwFlags)) {
95 // Copy from stack.
96 aData.assign((const _Ty*)buf, (const _Ty*)buf + (dwSize + sizeof(_Ty) - 1) / sizeof(_Ty));
97 return TRUE;
98 } else if (GetLastError() == ERROR_MORE_DATA) {
99 aData.resize((dwSize + sizeof(_Ty) - 1) / sizeof(_Ty));
100 if (CryptGetHashParam(hHash, dwParam, (BYTE*)aData.data(), &dwSize, dwFlags))
101 return TRUE;
102 }
103
104 return FALSE;
105}
106
112template<class T>
113static _Success_(return != 0) BOOL CryptGetHashParam(_In_ HCRYPTHASH hHash, _In_ DWORD dwParam, _Out_ T &data, _In_ DWORD dwFlags)
114{
115 DWORD dwSize = sizeof(T);
116 return CryptGetHashParam(hHash, dwParam, (BYTE*)&data, &dwSize, dwFlags);
117}
118
124template<class _Ty, class _Ax>
125static _Success_(return != 0) BOOL CryptGetKeyParam(_In_ HCRYPTKEY hKey, _In_ DWORD dwParam, _Out_ std::vector<_Ty, _Ax> &aData, _In_ DWORD dwFlags)
126{
128 DWORD dwSize = WINSTD_STACK_BUFFER_BYTES;
129
130 // Try with the stack buffer first.
131 if (CryptGetKeyParam(hKey, dwParam, buf, &dwSize, dwFlags)) {
132 // Copy from stack.
133 aData.assign((const _Ty*)buf, (const _Ty*)buf + (dwSize + sizeof(_Ty) - 1) / sizeof(_Ty));
134 return TRUE;
135 } else if (GetLastError() == ERROR_MORE_DATA) {
136 aData.resize((dwSize + sizeof(_Ty) - 1) / sizeof(_Ty));
137 if (CryptGetKeyParam(hKey, dwParam, (BYTE*)aData.data(), &dwSize, dwFlags))
138 return TRUE;
139 }
140
141 return FALSE;
142}
143
149template<class T>
150static BOOL CryptGetKeyParam(_In_ HCRYPTKEY hKey, _In_ DWORD dwParam, _Out_ T &data, _In_ DWORD dwFlags)
151{
152 DWORD dwSize = sizeof(T);
153 return CryptGetKeyParam(hKey, dwParam, (BYTE*)&data, &dwSize, dwFlags);
154}
155
161template<class _Ty, class _Ax>
162static _Success_(return != 0) BOOL CryptExportKey(_In_ HCRYPTKEY hKey, _In_ HCRYPTKEY hExpKey, _In_ DWORD dwBlobType, _In_ DWORD dwFlags, _Out_ std::vector<_Ty, _Ax> &aData)
163{
164 DWORD dwKeyBLOBSize = 0;
165
166 if (CryptExportKey(hKey, hExpKey, dwBlobType, dwFlags, NULL, &dwKeyBLOBSize)) {
167 aData.resize((dwKeyBLOBSize + sizeof(_Ty) - 1) / sizeof(_Ty));
168 if (CryptExportKey(hKey, hExpKey, dwBlobType, dwFlags, aData.data(), &dwKeyBLOBSize))
169 return TRUE;
170 }
171
172 return FALSE;
173}
174
180template<class _Ty, class _Ax>
181static _Success_(return != 0) BOOL CryptEncrypt(_In_ HCRYPTKEY hKey, _In_opt_ HCRYPTHASH hHash, _In_ BOOL Final, _In_ DWORD dwFlags, _Inout_ std::vector<_Ty, _Ax> &aData)
182{
183 DWORD
184 dwDataLen = (DWORD)(aData.size() * sizeof(_Ty)),
185 dwBufLen = (DWORD)(aData.capacity() * sizeof(_Ty)),
186 dwEncLen = dwDataLen,
187 dwResult;
188
189 if (dwBufLen) {
190 aData.resize(dwBufLen);
191 if (CryptEncrypt(hKey, hHash, Final, dwFlags, (BYTE*)aData.data(), &dwEncLen, dwBufLen)) {
192 // Encryption succeeded.
193 assert(dwEncLen <= dwBufLen);
194 if (dwEncLen < dwBufLen)
195 aData.resize((dwEncLen + sizeof(_Ty) - 1) / sizeof(_Ty));
196 return TRUE;
197 } else
198 dwResult = GetLastError();
199 } else if (CryptEncrypt(hKey, NULL, Final, dwFlags, NULL, &dwEncLen, 0)) {
200 // CryptEncrypt() always succeeds for output data size queries.
201 // dwEncLen contains required output data size. Continue as if the buffer was to small. Actually, the output buffer _was_ too small!
202 dwResult = ERROR_MORE_DATA;
203 } else
204 dwResult = GetLastError();
205
206 if (dwResult == ERROR_MORE_DATA) {
207 // Encrypted data will be longer. Reserve more space and retry.
208 aData.resize(((dwBufLen = dwEncLen) + sizeof(_Ty) - 1) / sizeof(_Ty));
209 dwEncLen = dwDataLen;
210 if (CryptEncrypt(hKey, hHash, Final, dwFlags, (BYTE*)aData.data(), &dwEncLen, dwBufLen)) {
211 // Encryption succeeded.
212 assert(dwEncLen <= dwBufLen);
213 if (dwEncLen < dwBufLen)
214 aData.resize((dwEncLen + sizeof(_Ty) - 1) / sizeof(_Ty));
215 return TRUE;
216 }
217 } else {
218 // Resize back to data length.
219 aData.resize((dwDataLen + sizeof(_Ty) - 1) / sizeof(_Ty));
220 }
221
222 return FALSE;
223}
224
230template<class _Ty, class _Ax>
231static _Success_(return != 0) BOOL CryptDecrypt(_In_ HCRYPTKEY hKey, _In_opt_ HCRYPTHASH hHash, _In_ BOOL Final, _In_ DWORD dwFlags, _Inout_ std::vector<_Ty, _Ax> &aData)
232{
233 DWORD dwDataLen = (DWORD)(aData.size() * sizeof(_Ty));
234
235 if (CryptDecrypt(hKey, hHash, Final, dwFlags, (BYTE*)aData.data(), &dwDataLen)) {
236 // Decryption succeeded.
237 aData.resize((dwDataLen + sizeof(_Ty) - 1) / sizeof(_Ty));
238 return TRUE;
239 }
240
241 return FALSE;
242}
243
245
246namespace winstd
247{
250
256 class cert_context : public dplhandle<PCCERT_CONTEXT, NULL>
257 {
259
260 public:
267 {
268 if (m_h != invalid)
270 }
271
280 bool operator==(_In_ const handle_type &other) const noexcept
281 {
282 // TODO: [Crypto] Make constant time.
283 return
284 m_h == other ||
285 m_h->cbCertEncoded == other->cbCertEncoded && memcmp(m_h->pbCertEncoded, other->pbCertEncoded, m_h->cbCertEncoded) == 0;
286 }
287
296 bool operator!=(_In_ const handle_type &other) const noexcept
297 {
298 return !operator==(other);
299 }
300
309 bool operator<(_In_ const handle_type &other) const noexcept
310 {
311 // TODO: [Crypto] Make constant time.
312 const int r = memcmp(m_h->pbCertEncoded, other->pbCertEncoded, std::min<DWORD>(m_h->cbCertEncoded, other->cbCertEncoded));
313 return r < 0 || r == 0 && m_h->cbCertEncoded < other->cbCertEncoded;
314 }
315
324 bool operator>(_In_ const handle_type &other) const noexcept
325 {
326 // TODO: [Crypto] Make constant time.
327 const int r = memcmp(m_h->pbCertEncoded, other->pbCertEncoded, std::min<DWORD>(m_h->cbCertEncoded, other->cbCertEncoded));
328 return r > 0 || r == 0 && m_h->cbCertEncoded > other->cbCertEncoded;
329 }
330
339 bool operator<=(_In_ const handle_type &other) const noexcept
340 {
341 return !operator>(other);
342 }
343
352 bool operator>=(_In_ const handle_type &other) const noexcept
353 {
354 return !operator<(other);
355 }
356
357 protected:
363 void free_internal() noexcept override
364 {
365 CertFreeCertificateContext(m_h);
366 }
367
377 handle_type duplicate_internal(_In_ handle_type h) const noexcept override
378 {
379 return CertDuplicateCertificateContext(h);
380 }
381 };
382
388 class cert_chain_context : public dplhandle<PCCERT_CHAIN_CONTEXT, NULL>
389 {
391
392 public:
399 {
400 if (m_h != invalid)
402 }
403
404 protected:
410 void free_internal() noexcept override
411 {
412 CertFreeCertificateChain(m_h);
413 }
414
424 handle_type duplicate_internal(_In_ handle_type h) const noexcept override
425 {
426 return CertDuplicateCertificateChain(h);
427 }
428 };
429
436 class cert_store : public handle<HCERTSTORE, NULL>
437 {
439
440 public:
446 virtual ~cert_store()
447 {
448 if (m_h != invalid)
450 }
451
452 protected:
458 void free_internal() noexcept override
459 {
460 CertCloseStore(m_h, 0);
461 }
462 };
463
469 class crypt_prov : public handle<HCRYPTPROV, NULL>
470 {
472
473 public:
479 virtual ~crypt_prov()
480 {
481 if (m_h != invalid)
483 }
484
485 protected:
491 void free_internal() noexcept override
492 {
493 CryptReleaseContext(m_h, 0);
494 }
495 };
496
502 class crypt_hash : public dplhandle<HCRYPTHASH, NULL>
503 {
505
506 public:
512 virtual ~crypt_hash()
513 {
514 if (m_h != invalid)
516 }
517
518 protected:
524 void free_internal() noexcept override
525 {
526 CryptDestroyHash(m_h);
527 }
528
538 handle_type duplicate_internal(_In_ handle_type h) const noexcept override
539 {
540 handle_type hNew;
541 return CryptDuplicateHash(h, NULL, 0, &hNew) ? hNew : invalid;
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
659 handle_type duplicate_internal(_In_ handle_type h) const noexcept override
660 {
661 handle_type hNew;
662 return CryptDuplicateKey(h, NULL, 0, &hNew) ? hNew : invalid;
663 }
664 };
665
669 #pragma warning(push)
670 #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.
671 class data_blob : public DATA_BLOB
672 {
673 public:
677 data_blob() noexcept
678 {
679 cbData = 0;
680 pbData = NULL;
681 }
682
686 data_blob(_In_count_(size) BYTE *data, _In_ DWORD size) noexcept
687 {
688 cbData = size;
689 pbData = data;
690 }
691
695 data_blob(_In_ const DATA_BLOB &other)
696 {
697 cbData = other.cbData;
698 if (cbData) {
699 pbData = static_cast<BYTE*>(LocalAlloc(LMEM_FIXED, other.cbData));
700 if (!pbData) throw win_runtime_error("LocalAlloc failed.");
701 memcpy(pbData, other.pbData, other.cbData);
702 } else
703 pbData = NULL;
704 }
705
709 data_blob(_Inout_ data_blob &&other) noexcept
710 {
711 cbData = other.cbData;
712 pbData = other.pbData;
713 other.cbData = 0;
714 other.pbData = NULL;
715 }
716
720 virtual ~data_blob()
721 {
722 if (pbData != NULL)
723 LocalFree(pbData);
724 }
725
729 data_blob& operator=(_In_ const DATA_BLOB &other)
730 {
731 if (this != &other) {
732 cbData = other.cbData;
733 if (pbData)
734 LocalFree(pbData);
735 if (cbData) {
736 pbData = static_cast<BYTE*>(LocalAlloc(LMEM_FIXED, other.cbData));
737 if (!pbData) throw win_runtime_error("LocalAlloc failed.");
738 memcpy(pbData, other.pbData, other.cbData);
739 } else
740 pbData = NULL;
741 }
742
743 return *this;
744 }
745
749 data_blob& operator=(_Inout_ data_blob &&other) noexcept
750 {
751 if (this != &other) {
752 cbData = other.cbData;
753 if (pbData)
754 LocalFree(pbData);
755 pbData = other.pbData;
756 other.cbData = 0;
757 other.pbData = NULL;
758 }
759
760 return *this;
761 }
762
766 DWORD size() const noexcept
767 {
768 return cbData;
769 }
770
774 const BYTE* data() const noexcept
775 {
776 return pbData;
777 }
778
782 BYTE* data() noexcept
783 {
784 return pbData;
785 }
786 };
787 #pragma warning(pop)
788
790}
791
794
795#pragma warning(push)
796#pragma warning(disable: 4505) // Don't warn on unused code
797
803static 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)
804{
805 PCCERT_CHAIN_CONTEXT pChainContext;
806 BOOL bResult = CertGetCertificateChain(hChainEngine, pCertContext, pTime, hAdditionalStore, pChainPara, dwFlags, pvReserved, &pChainContext);
807 if (bResult)
808 ctx.attach(pChainContext);
809 return bResult;
810}
811
813static BOOL CryptAcquireContextA(_Inout_ winstd::crypt_prov &prov, _In_opt_ LPCSTR szContainer, _In_opt_ LPCSTR szProvider, _In_ DWORD dwProvType, _In_ DWORD dwFlags)
814{
815 HCRYPTPROV h;
816 BOOL bResult = CryptAcquireContextA(&h, szContainer, szProvider, dwProvType, dwFlags);
817 if (bResult)
818 prov.attach(h);
819 return bResult;
820}
821
827static BOOL CryptAcquireContextW(_Inout_ winstd::crypt_prov &prov, _In_opt_ LPCWSTR szContainer, _In_opt_ LPCWSTR szProvider, _In_ DWORD dwProvType, _In_ DWORD dwFlags)
828{
829 HCRYPTPROV h;
830 BOOL bResult = CryptAcquireContextW(&h, szContainer, szProvider, dwProvType, dwFlags);
831 if (bResult)
832 prov.attach(h);
833 return bResult;
834}
835
841static BOOL CryptCreateHash(_In_ HCRYPTPROV hProv, _In_ ALG_ID Algid, _In_ HCRYPTKEY hKey, _In_ DWORD dwFlags, _Inout_ winstd::crypt_hash &hash)
842{
843 HCRYPTHASH h;
844 BOOL bResult = CryptCreateHash(hProv, Algid, hKey, dwFlags, &h);
845 if (bResult)
846 hash.attach(h);
847 return bResult;
848}
849
855static BOOL CryptGenKey(_In_ HCRYPTPROV hProv, _In_ ALG_ID Algid, _In_ DWORD dwFlags, _Inout_ winstd::crypt_key &key)
856{
857 HCRYPTKEY h;
858 BOOL bResult = CryptGenKey(hProv, Algid, dwFlags, &h);
859 if (bResult)
860 key.attach(h);
861 return bResult;
862}
863
869static bool CryptImportKey(_In_ HCRYPTPROV hProv, __in_bcount(dwDataLen) LPCBYTE pbData, _In_ DWORD dwDataLen, _In_ HCRYPTKEY hPubKey, _In_ DWORD dwFlags, _Inout_ winstd::crypt_key &key)
870{
871 HCRYPTKEY h;
872 BOOL bResult = CryptImportKey(hProv, pbData, dwDataLen, hPubKey, dwFlags, &h);
873 if (bResult)
874 key.attach(h);
875 return bResult;
876}
877
883static bool CryptImportPublicKeyInfo(_In_ HCRYPTPROV hCryptProv, _In_ DWORD dwCertEncodingType, _In_ PCERT_PUBLIC_KEY_INFO pInfo, _Inout_ winstd::crypt_key &key)
884{
885 HCRYPTKEY h;
886 BOOL bResult = CryptImportPublicKeyInfo(hCryptProv, dwCertEncodingType, pInfo, &h);
887 if (bResult)
888 key.attach(h);
889 return bResult;
890}
891
897static bool CryptDeriveKey(_In_ HCRYPTPROV hProv, _In_ ALG_ID Algid, _In_ HCRYPTHASH hBaseData, _In_ DWORD dwFlags, _Inout_ winstd::crypt_key &key)
898{
899 HCRYPTKEY h;
900 BOOL bResult = CryptDeriveKey(hProv, Algid, hBaseData, dwFlags, &h);
901 if (bResult)
902 key.attach(h);
903 return bResult;
904}
905
906#pragma warning(pop)
907
General API.
PCCERT_CHAIN_CONTEXT wrapper class.
Definition: Crypt.h:389
handle_type duplicate_internal(handle_type h) const noexcept override
Duplicates the certificate chain context.
Definition: Crypt.h:424
virtual ~cert_chain_context()
Destroys the certificate chain context.
Definition: Crypt.h:398
void free_internal() noexcept override
Destroys the certificate chain context.
Definition: Crypt.h:410
PCCERT_CONTEXT wrapper class.
Definition: Crypt.h:257
bool operator<=(const handle_type &other) const noexcept
Is certificate less than or equal?
Definition: Crypt.h:339
void free_internal() noexcept override
Destroys the certificate context.
Definition: Crypt.h:363
bool operator==(const handle_type &other) const noexcept
Is certificate equal to?
Definition: Crypt.h:280
handle_type duplicate_internal(handle_type h) const noexcept override
Duplicates the certificate context.
Definition: Crypt.h:377
bool operator>=(const handle_type &other) const noexcept
Is certificate greater than or equal?
Definition: Crypt.h:352
bool operator>(const handle_type &other) const noexcept
Is certificate greater than?
Definition: Crypt.h:324
bool operator<(const handle_type &other) const noexcept
Is certificate less than?
Definition: Crypt.h:309
bool operator!=(const handle_type &other) const noexcept
Is certificate not equal to?
Definition: Crypt.h:296
virtual ~cert_context()
Destroys the certificate context.
Definition: Crypt.h:266
HCERTSTORE wrapper class.
Definition: Crypt.h:437
virtual ~cert_store()
Closes the certificate store.
Definition: Crypt.h:446
void free_internal() noexcept override
Closes the certificate store.
Definition: Crypt.h:458
HCRYPTHASH wrapper class.
Definition: Crypt.h:503
void free_internal() noexcept override
Destroys the hash context.
Definition: Crypt.h:524
virtual ~crypt_hash()
Destroys the hash context.
Definition: Crypt.h:512
handle_type duplicate_internal(handle_type h) const noexcept override
Duplicates the hash context.
Definition: Crypt.h:538
HCRYPTKEY wrapper class.
Definition: Crypt.h:554
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
handle_type duplicate_internal(handle_type h) const noexcept override
Duplicates the key.
Definition: Crypt.h:659
void free_internal() noexcept override
Destroys the key.
Definition: Crypt.h:645
HCRYPTPROV wrapper class.
Definition: Crypt.h:470
virtual ~crypt_prov()
Releases the cryptographic context.
Definition: Crypt.h:479
void free_internal() noexcept override
Releases the cryptographic context.
Definition: Crypt.h:491
DATA_BLOB wrapper class.
Definition: Crypt.h:672
data_blob(const DATA_BLOB &other)
Duplicate an existing BLOB.
Definition: Crypt.h:695
virtual ~data_blob()
Destroys the BLOB.
Definition: Crypt.h:720
BYTE * data() noexcept
Get BLOB buffer.
Definition: Crypt.h:782
const BYTE * data() const noexcept
Get BLOB buffer.
Definition: Crypt.h:774
data_blob() noexcept
Initializes an empty BLOB.
Definition: Crypt.h:677
data_blob(data_blob &&other) noexcept
Move an existing BLOB.
Definition: Crypt.h:709
data_blob & operator=(data_blob &&other) noexcept
Move an existing BLOB.
Definition: Crypt.h:749
data_blob(BYTE *data, DWORD size) noexcept
Initializes a BLOB from existing data.
Definition: Crypt.h:686
DWORD size() const noexcept
Get BLOB size.
Definition: Crypt.h:766
data_blob & operator=(const DATA_BLOB &other)
Copy an existing BLOB.
Definition: Crypt.h:729
Base abstract template class to support object handle keeping for objects that support trivial handle...
Definition: Common.h:877
Base abstract template class to support generic object handle keeping.
Definition: Common.h:615
PCCERT_CONTEXT handle_type
Datatype of the object handle this template class handles.
Definition: Common.h:620
handle_type m_h
Object handle.
Definition: Common.h:866
void attach(handle_type h) noexcept
Sets a new object handle for the class.
Definition: Common.h:829
Windows runtime error.
Definition: Common.h:1056
static bool CryptImportPublicKeyInfo(HCRYPTPROV hCryptProv, DWORD dwCertEncodingType, PCERT_PUBLIC_KEY_INFO pInfo, winstd::crypt_key &key)
Imports the public key.
Definition: Crypt.h:883
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:63
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:803
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:88
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:45
static BOOL CryptAcquireContextA(winstd::crypt_prov &prov, LPCSTR szContainer, LPCSTR szProvider, DWORD dwProvType, DWORD dwFlags)
Acquires the cryptographic context.
Definition: Crypt.h:813
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:27
static BOOL CryptGenKey(HCRYPTPROV hProv, ALG_ID Algid, DWORD dwFlags, winstd::crypt_key &key)
Generates the key.
Definition: Crypt.h:855
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:162
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:125
static BOOL CryptCreateHash(HCRYPTPROV hProv, ALG_ID Algid, HCRYPTKEY hKey, DWORD dwFlags, winstd::crypt_hash &hash)
Creates the hash context.
Definition: Crypt.h:841
static BOOL CryptAcquireContextW(winstd::crypt_prov &prov, LPCWSTR szContainer, LPCWSTR szProvider, DWORD dwProvType, DWORD dwFlags)
Acquires the cryptographic context.
Definition: Crypt.h:827
static BOOL CryptEncrypt(HCRYPTKEY hKey, HCRYPTHASH hHash, BOOL Final, DWORD dwFlags, std::vector< _Ty, _Ax > &aData)
Encrypts data.
Definition: Crypt.h:181
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:897
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:231
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:869
#define WINSTD_STACK_BUFFER_BYTES
Size of the stack buffer in bytes used for initial system function call.
Definition: Common.h:101
#define WINSTD_DPLHANDLE_IMPL(C, INVAL)
Implements default constructors and operators to prevent their auto-generation by compiler.
Definition: Common.h:183
#define WINSTD_HANDLE_IMPL(C, INVAL)
Implements default constructors and operators to prevent their auto-generation by compiler.
Definition: Common.h:171
static const PCCERT_CONTEXT invalid
Invalid handle value.
Definition: Common.h:625