• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2     Copyright (c) 2016 mingw-w64 project
3 
4     Contributing authors: Steve Lhomme, Hugo Beauzée-Luyssen
5 
6     Permission is hereby granted, free of charge, to any person obtaining a
7     copy of this software and associated documentation files (the "Software"),
8     to deal in the Software without restriction, including without limitation
9     the rights to use, copy, modify, merge, publish, distribute, sublicense,
10     and/or sell copies of the Software, and to permit persons to whom the
11     Software is furnished to do so, subject to the following conditions:
12 
13     The above copyright notice and this permission notice shall be included in
14     all copies or substantial portions of the Software.
15 
16     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17     IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18     FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19     AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20     LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21     FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22     DEALINGS IN THE SOFTWARE.
23 */
24 
25 #define _WIN32_WINNT 0x602
26 
27 #define CryptAcquireContextW __CryptAcquireContextW
28 #define CryptAcquireContextA __CryptAcquireContextA
29 #define CryptReleaseContext __CryptReleaseContext
30 #define CryptGenRandom __CryptGenRandom
31 #define COBJMACROS
32 #define INITGUID
33 #include <windef.h>
34 #include <windows.h>
35 #include <windows.security.cryptography.h>
36 #include <winstring.h>
37 #include <roapi.h>
38 #undef CertOpenSystemStore
39 #undef CryptGenRandom
40 #undef CryptReleaseContext
41 #undef CryptAcquireContextA
42 #undef CryptAcquireContextW
43 
44 #define HCRYPTPROV ICryptographicBufferStatics *
45 
CryptAcquireContextW(HCRYPTPROV * phProv,LPCTSTR pszContainer,LPCTSTR pszProvider,DWORD dwProvType,DWORD dwFlags)46 BOOL WINAPI CryptAcquireContextW(HCRYPTPROV *phProv, LPCTSTR pszContainer, LPCTSTR pszProvider, DWORD dwProvType, DWORD dwFlags)
47 {
48     static const WCHAR *className = L"Windows.Security.Cryptography.CryptographicBuffer";
49     const UINT32 clen = wcslen(className);
50 
51     HSTRING hClassName = NULL;
52     HSTRING_HEADER header;
53     HRESULT hr = WindowsCreateStringReference(className, clen, &header, &hClassName);
54     if (FAILED(hr)) {
55         WindowsDeleteString(hClassName);
56         return FALSE;
57     }
58 
59     ICryptographicBufferStatics *cryptoStatics = NULL;
60     hr = RoGetActivationFactory(hClassName, &IID_ICryptographicBufferStatics, (void**)&cryptoStatics);
61     WindowsDeleteString(hClassName);
62 
63     if (FAILED(hr))
64         return FALSE;
65 
66     *phProv = cryptoStatics;
67 
68     return TRUE;
69 }
70 
CryptAcquireContextA(HCRYPTPROV * phProv,LPCTSTR pszContainer,LPCTSTR pszProvider,DWORD dwProvType,DWORD dwFlags)71 BOOL WINAPI CryptAcquireContextA(HCRYPTPROV *phProv, LPCTSTR pszContainer, LPCTSTR pszProvider, DWORD dwProvType, DWORD dwFlags)
72 {
73     return FALSE;
74 }
75 
CryptReleaseContext(HCRYPTPROV phProv,DWORD dwFlags)76 BOOL WINAPI CryptReleaseContext(HCRYPTPROV phProv, DWORD dwFlags)
77 {
78     HRESULT hr = ICryptographicBufferStatics_Release(phProv);
79     return SUCCEEDED(hr) && dwFlags==0;
80 }
81 
CryptGenRandom(HCRYPTPROV phProv,DWORD dwLen,BYTE * pbBuffer)82 BOOL WINAPI CryptGenRandom(HCRYPTPROV phProv, DWORD dwLen, BYTE *pbBuffer)
83 {
84     IBuffer *buffer = NULL;
85     HRESULT hr = ICryptographicBufferStatics_GenerateRandom(phProv, dwLen, &buffer);
86     if (FAILED(hr)) {
87         return FALSE;
88     }
89 
90     UINT32 olength;
91     unsigned char *rnd = NULL;
92     hr = ICryptographicBufferStatics_CopyToByteArray(phProv, buffer, &olength, (BYTE**)&rnd);
93     if (FAILED(hr)) {
94         IBuffer_Release(buffer);
95         return FALSE;
96     }
97     memcpy(pbBuffer, rnd, dwLen);
98 
99     IBuffer_Release(buffer);
100     return TRUE;
101 }
102 
103 #ifdef _X86_
104 BOOL (WINAPI *__MINGW_IMP_SYMBOL(CryptAcquireContextW))(HCRYPTPROV*, LPCTSTR, LPCTSTR, DWORD, DWORD) asm("__imp__CryptAcquireContextW@20") = CryptAcquireContextW;
105 BOOL (WINAPI *__MINGW_IMP_SYMBOL(CryptAcquireContextA))(HCRYPTPROV*, LPCTSTR, LPCTSTR, DWORD, DWORD) asm("__imp__CryptAcquireContextA@20") = CryptAcquireContextA;
106 BOOL (WINAPI *__MINGW_IMP_SYMBOL(CryptReleaseContext))(HCRYPTPROV, DWORD) asm("__imp__CryptReleaseContext@8") = CryptReleaseContext;
107 BOOL (WINAPI *__MINGW_IMP_SYMBOL(CryptGenRandom))(HCRYPTPROV, DWORD, BYTE*) asm("__imp__CryptGenRandom@12") = CryptGenRandom;
108 #else
109 BOOL (WINAPI *__MINGW_IMP_SYMBOL(CryptAcquireContextW))(HCRYPTPROV*, LPCTSTR, LPCTSTR, DWORD, DWORD) asm("__imp_CryptAcquireContextW") = CryptAcquireContextW;
110 BOOL (WINAPI *__MINGW_IMP_SYMBOL(CryptAcquireContextA))(HCRYPTPROV*, LPCTSTR, LPCTSTR, DWORD, DWORD) asm("__imp_CryptAcquireContextA") = CryptAcquireContextA;
111 BOOL (WINAPI *__MINGW_IMP_SYMBOL(CryptReleaseContext))(HCRYPTPROV, DWORD) asm("__imp_CryptReleaseContext") = CryptReleaseContext;
112 BOOL (WINAPI *__MINGW_IMP_SYMBOL(CryptGenRandom))(HCRYPTPROV, DWORD, BYTE*) asm("__imp_CryptGenRandom") = CryptGenRandom;
113 #endif
114