WinStd
Windows Win32 API using Standard C++
Loading...
Searching...
No Matches
Crypt.h
1/*
2 SPDX-License-Identifier: MIT
3 Copyright © 1991-2024 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 sNameString.resize(dwSize - 1);
30 return ::CertGetNameStringA(pCertContext, dwType, dwFlags, pvTypePara, &sNameString[0], dwSize);
31}
32
38template<class _Traits, class _Ax>
39static 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)
40{
41 // Query the final string length first.
42 DWORD dwSize = ::CertGetNameStringW(pCertContext, dwType, dwFlags, pvTypePara, NULL, 0);
43
44 // Allocate buffer on heap to format the string data into and read it.
45 sNameString.resize(dwSize - 1);
46 return ::CertGetNameStringW(pCertContext, dwType, dwFlags, pvTypePara, &sNameString[0], dwSize);
47}
48
54template<class _Ty, class _Ax>
55static _Success_(return != 0) BOOL WINAPI CertGetCertificateContextProperty(_In_ PCCERT_CONTEXT pCertContext, _In_ DWORD dwPropId, _Out_ std::vector<_Ty, _Ax> &aData)
56{
58 DWORD dwSize = WINSTD_STACK_BUFFER_BYTES;
59
60 // Try with the stack buffer first.
61 if (CertGetCertificateContextProperty(pCertContext, dwPropId, buf, &dwSize)) {
62 // Copy from stack.
63 aData.assign((const _Ty*)buf, (const _Ty*)buf + (dwSize + sizeof(_Ty) - 1) / sizeof(_Ty));
64 return TRUE;
65 } else if (GetLastError() == ERROR_MORE_DATA) {
66 aData.resize((dwSize + sizeof(_Ty) - 1) / sizeof(_Ty));
67 if (CertGetCertificateContextProperty(pCertContext, dwPropId, aData.data(), &dwSize))
68 return TRUE;
69 }
70
71 return FALSE;
72}
73
79template<class _Ty, class _Ax>
80static _Success_(return != 0) BOOL CryptGetHashParam(_In_ HCRYPTHASH hHash, _In_ DWORD dwParam, _Out_ std::vector<_Ty, _Ax> &aData, _In_ DWORD dwFlags)
81{
83 DWORD dwSize = WINSTD_STACK_BUFFER_BYTES;
84
85 // Try with the stack buffer first.
86 if (CryptGetHashParam(hHash, dwParam, buf, &dwSize, dwFlags)) {
87 // Copy from stack.
88 aData.assign((const _Ty*)buf, (const _Ty*)buf + (dwSize + sizeof(_Ty) - 1) / sizeof(_Ty));
89 return TRUE;
90 } else if (GetLastError() == ERROR_MORE_DATA) {
91 aData.resize((dwSize + sizeof(_Ty) - 1) / sizeof(_Ty));
92 if (CryptGetHashParam(hHash, dwParam, reinterpret_cast<BYTE*>(aData.data()), &dwSize, dwFlags))
93 return TRUE;
94 }
95
96 return FALSE;
97}
98
104template<class T>
105static _Success_(return != 0) BOOL CryptGetHashParam(_In_ HCRYPTHASH hHash, _In_ DWORD dwParam, _Out_ T &data, _In_ DWORD dwFlags)
106{
107 DWORD dwSize = sizeof(T);
108 return CryptGetHashParam(hHash, dwParam, (BYTE*)&data, &dwSize, dwFlags);
109}
110
116template<class _Ty, class _Ax>
117static _Success_(return != 0) BOOL CryptGetKeyParam(_In_ HCRYPTKEY hKey, _In_ DWORD dwParam, _Out_ std::vector<_Ty, _Ax> &aData, _In_ DWORD dwFlags)
118{
120 DWORD dwSize = WINSTD_STACK_BUFFER_BYTES;
121
122 // Try with the stack buffer first.
123 if (CryptGetKeyParam(hKey, dwParam, buf, &dwSize, dwFlags)) {
124 // Copy from stack.
125 aData.assign((const _Ty*)buf, (const _Ty*)buf + (dwSize + sizeof(_Ty) - 1) / sizeof(_Ty));
126 return TRUE;
127 } else if (GetLastError() == ERROR_MORE_DATA) {
128 aData.resize((dwSize + sizeof(_Ty) - 1) / sizeof(_Ty));
129 if (CryptGetKeyParam(hKey, dwParam, reinterpret_cast<BYTE*>(aData.data()), &dwSize, dwFlags))
130 return TRUE;
131 }
132
133 return FALSE;
134}
135
141template<class T>
142static BOOL CryptGetKeyParam(_In_ HCRYPTKEY hKey, _In_ DWORD dwParam, _Out_ T &data, _In_ DWORD dwFlags)
143{
144 DWORD dwSize = sizeof(T);
145 return CryptGetKeyParam(hKey, dwParam, (BYTE*)&data, &dwSize, dwFlags);
146}
147
153template<class _Ty, class _Ax>
154static _Success_(return != 0) BOOL CryptExportKey(_In_ HCRYPTKEY hKey, _In_ HCRYPTKEY hExpKey, _In_ DWORD dwBlobType, _In_ DWORD dwFlags, _Out_ std::vector<_Ty, _Ax> &aData)
155{
156 DWORD dwKeyBLOBSize = 0;
157
158 if (CryptExportKey(hKey, hExpKey, dwBlobType, dwFlags, NULL, &dwKeyBLOBSize)) {
159 aData.resize((dwKeyBLOBSize + sizeof(_Ty) - 1) / sizeof(_Ty));
160 if (CryptExportKey(hKey, hExpKey, dwBlobType, dwFlags, reinterpret_cast<BYTE*>(aData.data()), &dwKeyBLOBSize))
161 return TRUE;
162 }
163
164 return FALSE;
165}
166
172template<class _Ty, class _Ax>
173static _Success_(return != 0) BOOL CryptEncrypt(_In_ HCRYPTKEY hKey, _In_opt_ HCRYPTHASH hHash, _In_ BOOL Final, _In_ DWORD dwFlags, _Inout_ std::vector<_Ty, _Ax> &aData)
174{
175 SIZE_T
176 sDataLen = SIZETMult(aData.size(), sizeof(_Ty)),
177 sBufLen = SIZETMult(aData.capacity(), sizeof(_Ty));
178 if (sDataLen > DWORD_MAX || sBufLen > DWORD_MAX)
179 throw std::invalid_argument("Data too big");
180 DWORD
181 dwDataLen = static_cast<DWORD>(sDataLen),
182 dwBufLen = static_cast<DWORD>(sBufLen),
183 dwEncLen = dwDataLen,
184 dwResult;
185
186 if (dwBufLen) {
187 aData.resize(dwBufLen);
188 if (CryptEncrypt(hKey, hHash, Final, dwFlags, reinterpret_cast<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, reinterpret_cast<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 SIZE_T sDataLen = SIZETMult(aData.size(), sizeof(_Ty));
231 if (sDataLen > DWORD_MAX)
232 throw std::invalid_argument("Data too big");
233 DWORD dwDataLen = static_cast<DWORD>(sDataLen);
234
235 if (CryptDecrypt(hKey, hHash, Final, dwFlags, reinterpret_cast<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 {
258 WINSTD_DPLHANDLE_IMPL(cert_context, PCCERT_CONTEXT, NULL)
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
378 {
379 // As per doc, this only increases refcounter. Should never fail.
380 return CertDuplicateCertificateContext(h);
381 }
382 };
383
389 class cert_chain_context : public dplhandle<PCCERT_CHAIN_CONTEXT, NULL>
390 {
391 WINSTD_DPLHANDLE_IMPL(cert_chain_context, PCCERT_CHAIN_CONTEXT, NULL)
392
393 public:
400 {
401 if (m_h != invalid)
403 }
404
405 protected:
411 void free_internal() noexcept override
412 {
413 CertFreeCertificateChain(m_h);
414 }
415
426 {
427 // As per doc, this only increases refcounter. Should never fail.
428 return CertDuplicateCertificateChain(h);
429 }
430 };
431
438 class cert_store : public handle<HCERTSTORE, NULL>
439 {
440 WINSTD_HANDLE_IMPL(cert_store, HCERTSTORE, NULL)
441
442 public:
448 virtual ~cert_store()
449 {
450 if (m_h != invalid)
452 }
453
454 protected:
460 void free_internal() noexcept override
461 {
462 CertCloseStore(m_h, 0);
463 }
464 };
465
471 class crypt_prov : public handle<HCRYPTPROV, NULL>
472 {
473 WINSTD_HANDLE_IMPL(crypt_prov, HCRYPTPROV, NULL)
474
475 public:
481 virtual ~crypt_prov()
482 {
483 if (m_h != invalid)
485 }
486
487 protected:
493 void free_internal() noexcept override
494 {
495 CryptReleaseContext(m_h, 0);
496 }
497 };
498
504 class crypt_hash : public dplhandle<HCRYPTHASH, NULL>
505 {
506 WINSTD_DPLHANDLE_IMPL(crypt_hash, HCRYPTHASH, NULL)
507
508 public:
514 virtual ~crypt_hash()
515 {
516 if (m_h != invalid)
518 }
519
520 protected:
526 void free_internal() noexcept override
527 {
528 CryptDestroyHash(m_h);
529 }
530
541 {
542 handle_type hNew;
543 if (CryptDuplicateHash(h, NULL, 0, &hNew))
544 return hNew;
545 throw win_runtime_error("CryptDuplicateHash failed");
546 }
547 };
548
557 class crypt_key : public dplhandle<HCRYPTKEY, NULL>
558 {
559 WINSTD_DPLHANDLE_IMPL(crypt_key, HCRYPTKEY, NULL)
560
561 public:
567 virtual ~crypt_key()
568 {
569 if (m_h != invalid)
571 }
572
581 bool create_exp1(_In_ HCRYPTPROV hProv, _In_ DWORD dwKeySpec)
582 {
583 if (dwKeySpec != AT_KEYEXCHANGE && dwKeySpec != AT_SIGNATURE) {
584 SetLastError(ERROR_INVALID_PARAMETER);
585 return false;
586 }
587
588 // Generate the private key.
589 handle_type h;
590 if (CryptGenKey(hProv, dwKeySpec, CRYPT_EXPORTABLE, &h)) {
591 // Export the private key, we'll convert it to a private exponent of one key.
592 std::vector<BYTE, sanitizing_allocator<BYTE>> key_blob;
593 if (CryptExportKey(h, 0, PRIVATEKEYBLOB, 0, key_blob)) {
594 CryptDestroyKey(h);
595
596 // Get the byte length of the key.
597 size_t
598 size_key = *reinterpret_cast<DWORD*>(&key_blob[12])/8,
599 size_prime = size_key/2;
600
601 // Modify the Exponent in Key BLOB format
602 // Key BLOB format is documented in SDK
603
604 // Convert pubexp in rsapubkey to 1
605 LPBYTE ptr = &key_blob[16];
606 *reinterpret_cast<DWORD*>(ptr) = 1;
607 ptr += sizeof(DWORD);
608
609 // Skip modulus, prime1, prime2
610 ptr += size_key;
611 ptr += size_prime;
612 ptr += size_prime;
613
614 // Convert exponent1 to 1
615 ptr[0] = 1;
616 memset(ptr + 1, 0, size_prime - 1);
617 ptr += size_prime;
618
619 // Convert exponent2 to 1
620 ptr[0] = 1;
621 memset(ptr + 1, 0, size_prime - 1);
622 ptr += size_prime;
623
624 // Skip coefficient
625 ptr += size_prime;
626
627 // Convert privateExponent to 1
628 ptr[0] = 1;
629 memset(ptr + 1, 0, size_key - 1);
630
631 // Import the exponent-of-one private key.
632 if (CryptImportKey(hProv, key_blob.data(), static_cast<DWORD>(key_blob.size()), 0, 0, &h)) {
633 attach(h);
634 return true;
635 }
636 } else
637 CryptDestroyKey(h);
638 }
639
640 return false;
641 }
642
643 protected:
649 void free_internal() noexcept override
650 {
651 CryptDestroyKey(m_h);
652 }
653
664 {
665 handle_type hNew;
666 if (CryptDuplicateKey(h, NULL, 0, &hNew))
667 return hNew;
668 throw win_runtime_error("CryptDuplicateKey failed");
669 }
670 };
671
675 #pragma warning(push)
676 #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.
677 class data_blob : public DATA_BLOB
678 {
679 public:
683 data_blob() noexcept
684 {
685 cbData = 0;
686 pbData = NULL;
687 }
688
692 data_blob(_In_count_(size) BYTE *data, _In_ DWORD size) noexcept
693 {
694 cbData = size;
695 pbData = data;
696 }
697
701 data_blob(_In_ const DATA_BLOB &other)
702 {
703 cbData = other.cbData;
704 if (cbData) {
705 pbData = static_cast<BYTE*>(LocalAlloc(LMEM_FIXED, other.cbData));
706 if (!pbData) throw win_runtime_error("LocalAlloc failed");
707 memcpy(pbData, other.pbData, other.cbData);
708 } else
709 pbData = NULL;
710 }
711
715 data_blob(_Inout_ data_blob &&other) noexcept
716 {
717 cbData = other.cbData;
718 pbData = other.pbData;
719 other.cbData = 0;
720 other.pbData = NULL;
721 }
722
726 virtual ~data_blob()
727 {
728 if (pbData != NULL)
729 LocalFree(pbData);
730 }
731
735 data_blob& operator=(_In_ const DATA_BLOB &other)
736 {
737 if (this != &other) {
738 cbData = other.cbData;
739 if (pbData)
740 LocalFree(pbData);
741 if (cbData) {
742 pbData = static_cast<BYTE*>(LocalAlloc(LMEM_FIXED, other.cbData));
743 if (!pbData) throw win_runtime_error("LocalAlloc failed");
744 memcpy(pbData, other.pbData, other.cbData);
745 } else
746 pbData = NULL;
747 }
748
749 return *this;
750 }
751
755 data_blob& operator=(_Inout_ data_blob &&other) noexcept
756 {
757 if (this != &other) {
758 cbData = other.cbData;
759 if (pbData)
760 LocalFree(pbData);
761 pbData = other.pbData;
762 other.cbData = 0;
763 other.pbData = NULL;
764 }
765
766 return *this;
767 }
768
772 DWORD size() const noexcept
773 {
774 return cbData;
775 }
776
780 const BYTE* data() const noexcept
781 {
782 return pbData;
783 }
784
788 BYTE* data() noexcept
789 {
790 return pbData;
791 }
792 };
793 #pragma warning(pop)
794
796}
797
800
801#pragma warning(push)
802#pragma warning(disable: 4505) // Don't warn on unused code
803
809static 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)
810{
811 PCCERT_CHAIN_CONTEXT pChainContext;
812 BOOL bResult = CertGetCertificateChain(hChainEngine, pCertContext, pTime, hAdditionalStore, pChainPara, dwFlags, pvReserved, &pChainContext);
813 if (bResult)
814 ctx.attach(pChainContext);
815 return bResult;
816}
817
819static BOOL CryptAcquireContextA(_Inout_ winstd::crypt_prov &prov, _In_opt_ LPCSTR szContainer, _In_opt_ LPCSTR szProvider, _In_ DWORD dwProvType, _In_ DWORD dwFlags)
820{
821 HCRYPTPROV h;
822 BOOL bResult = CryptAcquireContextA(&h, szContainer, szProvider, dwProvType, dwFlags);
823 if (bResult)
824 prov.attach(h);
825 return bResult;
826}
827
833static BOOL CryptAcquireContextW(_Inout_ winstd::crypt_prov &prov, _In_opt_ LPCWSTR szContainer, _In_opt_ LPCWSTR szProvider, _In_ DWORD dwProvType, _In_ DWORD dwFlags)
834{
835 HCRYPTPROV h;
836 BOOL bResult = CryptAcquireContextW(&h, szContainer, szProvider, dwProvType, dwFlags);
837 if (bResult)
838 prov.attach(h);
839 return bResult;
840}
841
847static BOOL CryptCreateHash(_In_ HCRYPTPROV hProv, _In_ ALG_ID Algid, _In_ HCRYPTKEY hKey, _In_ DWORD dwFlags, _Inout_ winstd::crypt_hash &hash)
848{
849 HCRYPTHASH h;
850 BOOL bResult = CryptCreateHash(hProv, Algid, hKey, dwFlags, &h);
851 if (bResult)
852 hash.attach(h);
853 return bResult;
854}
855
861static BOOL CryptGenKey(_In_ HCRYPTPROV hProv, _In_ ALG_ID Algid, _In_ DWORD dwFlags, _Inout_ winstd::crypt_key &key)
862{
863 HCRYPTKEY h;
864 BOOL bResult = CryptGenKey(hProv, Algid, dwFlags, &h);
865 if (bResult)
866 key.attach(h);
867 return bResult;
868}
869
875static bool CryptImportKey(_In_ HCRYPTPROV hProv, __in_bcount(dwDataLen) LPCBYTE pbData, _In_ DWORD dwDataLen, _In_ HCRYPTKEY hPubKey, _In_ DWORD dwFlags, _Inout_ winstd::crypt_key &key)
876{
877 HCRYPTKEY h;
878 BOOL bResult = CryptImportKey(hProv, pbData, dwDataLen, hPubKey, dwFlags, &h);
879 if (bResult)
880 key.attach(h);
881 return bResult;
882}
883
889static bool CryptImportPublicKeyInfo(_In_ HCRYPTPROV hCryptProv, _In_ DWORD dwCertEncodingType, _In_ PCERT_PUBLIC_KEY_INFO pInfo, _Inout_ winstd::crypt_key &key)
890{
891 HCRYPTKEY h;
892 BOOL bResult = CryptImportPublicKeyInfo(hCryptProv, dwCertEncodingType, pInfo, &h);
893 if (bResult)
894 key.attach(h);
895 return bResult;
896}
897
903static bool CryptDeriveKey(_In_ HCRYPTPROV hProv, _In_ ALG_ID Algid, _In_ HCRYPTHASH hBaseData, _In_ DWORD dwFlags, _Inout_ winstd::crypt_key &key)
904{
905 HCRYPTKEY h;
906 BOOL bResult = CryptDeriveKey(hProv, Algid, hBaseData, dwFlags, &h);
907 if (bResult)
908 key.attach(h);
909 return bResult;
910}
911
912#pragma warning(pop)
913
PCCERT_CHAIN_CONTEXT wrapper class.
Definition Crypt.h:390
virtual ~cert_chain_context()
Destroys the certificate chain context.
Definition Crypt.h:399
void free_internal() noexcept override
Destroys the certificate chain context.
Definition Crypt.h:411
handle_type duplicate_internal(handle_type h) const override
Duplicates the certificate chain context.
Definition Crypt.h:425
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
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
handle_type duplicate_internal(handle_type h) const override
Duplicates the certificate context.
Definition Crypt.h:377
virtual ~cert_context()
Destroys the certificate context.
Definition Crypt.h:266
HCERTSTORE wrapper class.
Definition Crypt.h:439
virtual ~cert_store()
Closes the certificate store.
Definition Crypt.h:448
void free_internal() noexcept override
Closes the certificate store.
Definition Crypt.h:460
HCRYPTHASH wrapper class.
Definition Crypt.h:505
void free_internal() noexcept override
Destroys the hash context.
Definition Crypt.h:526
virtual ~crypt_hash()
Destroys the hash context.
Definition Crypt.h:514
handle_type duplicate_internal(handle_type h) const override
Duplicates the hash context.
Definition Crypt.h:540
HCRYPTKEY wrapper class.
Definition Crypt.h:558
handle_type duplicate_internal(handle_type h) const override
Duplicates the key.
Definition Crypt.h:663
virtual ~crypt_key()
Destroys the key.
Definition Crypt.h:567
bool create_exp1(HCRYPTPROV hProv, DWORD dwKeySpec)
Creates Exponent-of-one key.
Definition Crypt.h:581
void free_internal() noexcept override
Destroys the key.
Definition Crypt.h:649
HCRYPTPROV wrapper class.
Definition Crypt.h:472
virtual ~crypt_prov()
Releases the cryptographic context.
Definition Crypt.h:481
void free_internal() noexcept override
Releases the cryptographic context.
Definition Crypt.h:493
DATA_BLOB wrapper class.
Definition Crypt.h:678
data_blob(const DATA_BLOB &other)
Duplicate an existing BLOB.
Definition Crypt.h:701
virtual ~data_blob()
Destroys the BLOB.
Definition Crypt.h:726
BYTE * data() noexcept
Get BLOB buffer.
Definition Crypt.h:788
const BYTE * data() const noexcept
Get BLOB buffer.
Definition Crypt.h:780
data_blob() noexcept
Initializes an empty BLOB.
Definition Crypt.h:683
data_blob(data_blob &&other) noexcept
Move an existing BLOB.
Definition Crypt.h:715
data_blob & operator=(data_blob &&other) noexcept
Move an existing BLOB.
Definition Crypt.h:755
data_blob(BYTE *data, DWORD size) noexcept
Initializes a BLOB from existing data.
Definition Crypt.h:692
DWORD size() const noexcept
Get BLOB size.
Definition Crypt.h:772
data_blob & operator=(const DATA_BLOB &other)
Copy an existing BLOB.
Definition Crypt.h:735
Base abstract template class to support object handle keeping for objects that support trivial handle...
Definition Common.h:1287
Base abstract template class to support generic object handle keeping.
Definition Common.h:1024
handle_type m_h
Definition Common.h:1276
void attach(handle_type h) noexcept
Definition Common.h:1239
PCCERT_CONTEXT handle_type
Definition Common.h:1029
Windows runtime error.
Definition Common.h:1553
static bool CryptImportPublicKeyInfo(HCRYPTPROV hCryptProv, DWORD dwCertEncodingType, PCERT_PUBLIC_KEY_INFO pInfo, winstd::crypt_key &key)
Imports the public key.
Definition Crypt.h:889
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:55
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:809
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:80
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:39
static BOOL CryptAcquireContextA(winstd::crypt_prov &prov, LPCSTR szContainer, LPCSTR szProvider, DWORD dwProvType, DWORD dwFlags)
Acquires the cryptographic context.
Definition Crypt.h:819
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:861
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:154
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:117
static BOOL CryptCreateHash(HCRYPTPROV hProv, ALG_ID Algid, HCRYPTKEY hKey, DWORD dwFlags, winstd::crypt_hash &hash)
Creates the hash context.
Definition Crypt.h:847
static BOOL CryptAcquireContextW(winstd::crypt_prov &prov, LPCWSTR szContainer, LPCWSTR szProvider, DWORD dwProvType, DWORD dwFlags)
Acquires the cryptographic context.
Definition Crypt.h:833
static BOOL CryptEncrypt(HCRYPTKEY hKey, HCRYPTHASH hHash, BOOL Final, DWORD dwFlags, std::vector< _Ty, _Ax > &aData)
Encrypts data.
Definition Crypt.h:173
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:903
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:228
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:875
#define WINSTD_STACK_BUFFER_BYTES
Size of the stack buffer in bytes used for initial system function call.
Definition Common.h:94
#define WINSTD_DPLHANDLE_IMPL(C, T, INVAL)
Implements default constructors and operators to prevent their auto-generation by compiler.
Definition Common.h:176
#define WINSTD_HANDLE_IMPL(C, T, INVAL)
Implements default constructors and operators to prevent their auto-generation by compiler.
Definition Common.h:164
static const PCCERT_CONTEXT invalid
Definition Common.h:1034