]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // crypt.hpp --------------------------------------------------------------// |
2 | ||
3 | // Copyright 2014 Antony Polukhin | |
4 | // Copyright 2015 Andrey Semashev | |
5 | ||
6 | // Distributed under the Boost Software License, Version 1.0. | |
7 | // See http://www.boost.org/LICENSE_1_0.txt | |
8 | ||
9 | ||
10 | #ifndef BOOST_DETAIL_WINAPI_CRYPT_HPP | |
11 | #define BOOST_DETAIL_WINAPI_CRYPT_HPP | |
12 | ||
13 | #include <boost/detail/winapi/basic_types.hpp> | |
14 | #include <boost/detail/winapi/detail/cast_ptr.hpp> | |
15 | #if defined( BOOST_USE_WINDOWS_H ) | |
16 | // This header is not always included as part of windows.h | |
17 | #include <wincrypt.h> | |
18 | #endif | |
19 | ||
20 | #ifdef BOOST_HAS_PRAGMA_ONCE | |
21 | #pragma once | |
22 | #endif | |
23 | ||
24 | #if !defined( BOOST_USE_WINDOWS_H ) | |
25 | namespace boost { namespace detail { namespace winapi { | |
26 | typedef ULONG_PTR_ HCRYPTPROV_; | |
27 | }}} | |
28 | ||
29 | // Some versions of MinGW (including the latest ones) contain buggy declarations of CryptEnumProvidersA and CryptEnumProvidersW. | |
30 | // We cannot detect those broken versions, and we can't include the system header because it's incomplete. | |
31 | // So below we duplicate the broken declarations here and work around the problem with cast_ptr. These declarations | |
32 | // will have to be removed when MinGW is fixed. | |
33 | ||
34 | extern "C" { | |
35 | #if !defined( BOOST_NO_ANSI_APIS ) | |
36 | #if !defined( BOOST_WINAPI_IS_MINGW ) || !defined( UNICODE ) | |
37 | BOOST_SYMBOL_IMPORT boost::detail::winapi::BOOL_ WINAPI | |
38 | CryptEnumProvidersA( | |
39 | boost::detail::winapi::DWORD_ dwIndex, | |
40 | boost::detail::winapi::DWORD_ *pdwReserved, | |
41 | boost::detail::winapi::DWORD_ dwFlags, | |
42 | boost::detail::winapi::DWORD_ *pdwProvType, | |
43 | boost::detail::winapi::LPSTR_ szProvName, | |
44 | boost::detail::winapi::DWORD_ *pcbProvName); | |
45 | #else | |
46 | // Broken declaration in MinGW | |
47 | BOOST_SYMBOL_IMPORT boost::detail::winapi::BOOL_ WINAPI | |
48 | CryptEnumProvidersA( | |
49 | boost::detail::winapi::DWORD_ dwIndex, | |
50 | boost::detail::winapi::DWORD_ *pdwReserved, | |
51 | boost::detail::winapi::DWORD_ dwFlags, | |
52 | boost::detail::winapi::DWORD_ *pdwProvType, | |
53 | boost::detail::winapi::LPWSTR_ szProvName, | |
54 | boost::detail::winapi::DWORD_ *pcbProvName); | |
55 | #endif | |
56 | ||
57 | BOOST_SYMBOL_IMPORT boost::detail::winapi::BOOL_ WINAPI | |
58 | CryptAcquireContextA( | |
59 | boost::detail::winapi::HCRYPTPROV_ *phProv, | |
60 | boost::detail::winapi::LPCSTR_ pszContainer, | |
61 | boost::detail::winapi::LPCSTR_ pszProvider, | |
62 | boost::detail::winapi::DWORD_ dwProvType, | |
63 | boost::detail::winapi::DWORD_ dwFlags); | |
64 | #endif // !defined( BOOST_NO_ANSI_APIS ) | |
65 | ||
66 | #if !defined( BOOST_WINAPI_IS_MINGW ) || defined( UNICODE ) | |
67 | BOOST_SYMBOL_IMPORT boost::detail::winapi::BOOL_ WINAPI | |
68 | CryptEnumProvidersW( | |
69 | boost::detail::winapi::DWORD_ dwIndex, | |
70 | boost::detail::winapi::DWORD_ *pdwReserved, | |
71 | boost::detail::winapi::DWORD_ dwFlags, | |
72 | boost::detail::winapi::DWORD_ *pdwProvType, | |
73 | boost::detail::winapi::LPWSTR_ szProvName, | |
74 | boost::detail::winapi::DWORD_ *pcbProvName); | |
75 | #else | |
76 | // Broken declaration in MinGW | |
77 | BOOST_SYMBOL_IMPORT boost::detail::winapi::BOOL_ WINAPI | |
78 | CryptEnumProvidersW( | |
79 | boost::detail::winapi::DWORD_ dwIndex, | |
80 | boost::detail::winapi::DWORD_ *pdwReserved, | |
81 | boost::detail::winapi::DWORD_ dwFlags, | |
82 | boost::detail::winapi::DWORD_ *pdwProvType, | |
83 | boost::detail::winapi::LPSTR_ szProvName, | |
84 | boost::detail::winapi::DWORD_ *pcbProvName); | |
85 | #endif | |
86 | ||
87 | BOOST_SYMBOL_IMPORT boost::detail::winapi::BOOL_ WINAPI | |
88 | CryptAcquireContextW( | |
89 | boost::detail::winapi::HCRYPTPROV_ *phProv, | |
90 | boost::detail::winapi::LPCWSTR_ szContainer, | |
91 | boost::detail::winapi::LPCWSTR_ szProvider, | |
92 | boost::detail::winapi::DWORD_ dwProvType, | |
93 | boost::detail::winapi::DWORD_ dwFlags); | |
94 | ||
95 | BOOST_SYMBOL_IMPORT boost::detail::winapi::BOOL_ WINAPI | |
96 | CryptGenRandom( | |
97 | boost::detail::winapi::HCRYPTPROV_ hProv, | |
98 | boost::detail::winapi::DWORD_ dwLen, | |
99 | boost::detail::winapi::BYTE_ *pbBuffer); | |
100 | ||
101 | BOOST_SYMBOL_IMPORT boost::detail::winapi::BOOL_ WINAPI | |
102 | CryptReleaseContext( | |
103 | boost::detail::winapi::HCRYPTPROV_ hProv, | |
104 | boost::detail::winapi::DWORD_ dwFlags); | |
105 | } | |
106 | #endif // !defined( BOOST_USE_WINDOWS_H ) | |
107 | ||
108 | namespace boost { | |
109 | namespace detail { | |
110 | namespace winapi { | |
111 | ||
112 | #if defined( BOOST_USE_WINDOWS_H ) | |
113 | ||
114 | typedef ::HCRYPTPROV HCRYPTPROV_; | |
115 | ||
116 | const DWORD_ PROV_RSA_FULL_ = PROV_RSA_FULL; | |
117 | ||
118 | const DWORD_ CRYPT_VERIFYCONTEXT_ = CRYPT_VERIFYCONTEXT; | |
119 | const DWORD_ CRYPT_NEWKEYSET_ = CRYPT_NEWKEYSET; | |
120 | const DWORD_ CRYPT_DELETEKEYSET_ = CRYPT_DELETEKEYSET; | |
121 | const DWORD_ CRYPT_MACHINE_KEYSET_ = CRYPT_MACHINE_KEYSET; | |
122 | const DWORD_ CRYPT_SILENT_ = CRYPT_SILENT; | |
123 | ||
124 | #else | |
125 | ||
126 | const DWORD_ PROV_RSA_FULL_ = 1; | |
127 | ||
128 | const DWORD_ CRYPT_VERIFYCONTEXT_ = 0xF0000000; | |
129 | const DWORD_ CRYPT_NEWKEYSET_ = 8; | |
130 | const DWORD_ CRYPT_DELETEKEYSET_ = 16; | |
131 | const DWORD_ CRYPT_MACHINE_KEYSET_ = 32; | |
132 | const DWORD_ CRYPT_SILENT_ = 64; | |
133 | ||
134 | #endif | |
135 | ||
136 | #if !defined( BOOST_NO_ANSI_APIS ) | |
137 | using ::CryptEnumProvidersA; | |
138 | using ::CryptAcquireContextA; | |
139 | #endif | |
140 | using ::CryptEnumProvidersW; | |
141 | using ::CryptAcquireContextW; | |
142 | using ::CryptGenRandom; | |
143 | using ::CryptReleaseContext; | |
144 | ||
145 | #if !defined( BOOST_NO_ANSI_APIS ) | |
146 | BOOST_FORCEINLINE BOOL_ crypt_enum_providers( | |
147 | DWORD_ dwIndex, | |
148 | DWORD_ *pdwReserved, | |
149 | DWORD_ dwFlags, | |
150 | DWORD_ *pdwProvType, | |
151 | LPSTR_ szProvName, | |
152 | DWORD_ *pcbProvName) | |
153 | { | |
154 | return ::CryptEnumProvidersA(dwIndex, pdwReserved, dwFlags, pdwProvType, winapi::detail::cast_ptr(szProvName), pcbProvName); | |
155 | } | |
156 | ||
157 | BOOST_FORCEINLINE BOOL_ crypt_acquire_context( | |
158 | HCRYPTPROV_ *phProv, | |
159 | LPCSTR_ pszContainer, | |
160 | LPCSTR_ pszProvider, | |
161 | DWORD_ dwProvType, | |
162 | DWORD_ dwFlags) | |
163 | { | |
164 | return ::CryptAcquireContextA(phProv, pszContainer, pszProvider, dwProvType, dwFlags); | |
165 | } | |
166 | #endif | |
167 | ||
168 | BOOST_FORCEINLINE BOOL_ crypt_enum_providers( | |
169 | DWORD_ dwIndex, | |
170 | DWORD_ *pdwReserved, | |
171 | DWORD_ dwFlags, | |
172 | DWORD_ *pdwProvType, | |
173 | LPWSTR_ szProvName, | |
174 | DWORD_ *pcbProvName) | |
175 | { | |
176 | return ::CryptEnumProvidersW(dwIndex, pdwReserved, dwFlags, pdwProvType, winapi::detail::cast_ptr(szProvName), pcbProvName); | |
177 | } | |
178 | ||
179 | BOOST_FORCEINLINE BOOL_ crypt_acquire_context( | |
180 | HCRYPTPROV_ *phProv, | |
181 | LPCWSTR_ szContainer, | |
182 | LPCWSTR_ szProvider, | |
183 | DWORD_ dwProvType, | |
184 | DWORD_ dwFlags) | |
185 | { | |
186 | return ::CryptAcquireContextW(phProv, szContainer, szProvider, dwProvType, dwFlags); | |
187 | } | |
188 | ||
189 | } | |
190 | } | |
191 | } | |
192 | ||
193 | #endif // BOOST_DETAIL_WINAPI_CRYPT_HPP |