| 1 |
/* |
| 2 |
* Copyright (c) 2015, Secure Endpoints Inc. |
| 3 |
* All rights reserved. |
| 4 |
* |
| 5 |
* Redistribution and use in source and binary forms, with or without |
| 6 |
* modification, are permitted provided that the following conditions |
| 7 |
* are met: |
| 8 |
* |
| 9 |
* - Redistributions of source code must retain the above copyright |
| 10 |
* notice, this list of conditions and the following disclaimer. |
| 11 |
* |
| 12 |
* - Redistributions in binary form must reproduce the above copyright |
| 13 |
* notice, this list of conditions and the following disclaimer in |
| 14 |
* the documentation and/or other materials provided with the |
| 15 |
* distribution. |
| 16 |
* |
| 17 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 18 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 19 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 20 |
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 21 |
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, |
| 22 |
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 23 |
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 24 |
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 25 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 26 |
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 27 |
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| 28 |
* OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 |
*/ |
| 30 |
|
| 31 |
/* |
| 32 |
* Windows fallback provider: decides whether to use hcrypto or |
| 33 |
* wincng depending on whether bcrypt.dll is available (i.e. it |
| 34 |
* is runtime compatible back to XP, but will use the native |
| 35 |
* crypto APIs from Vista onwards). |
| 36 |
*/ |
| 37 |
|
| 38 |
#include <config.h> |
| 39 |
#include <roken.h> |
| 40 |
|
| 41 |
#include <assert.h> |
| 42 |
|
| 43 |
#include <evp.h> |
| 44 |
#include <evp-w32.h> |
| 45 |
#include <evp-hcrypto.h> |
| 46 |
|
| 47 |
#include <evp-wincng.h> |
| 48 |
|
| 49 |
static LONG wincng_available = -1; |
| 50 |
|
| 51 |
static __inline int |
| 52 |
wincng_check_availability(void) |
| 53 |
{ |
| 54 |
if (wincng_available == -1) { |
| 55 |
char szBCryptDllPath[MAX_PATH]; |
| 56 |
UINT cbBCryptDllPath; |
| 57 |
|
| 58 |
cbBCryptDllPath = GetSystemDirectory(szBCryptDllPath, |
| 59 |
sizeof(szBCryptDllPath)); |
| 60 |
if (cbBCryptDllPath > 0 && |
| 61 |
cbBCryptDllPath < sizeof(szBCryptDllPath) && |
| 62 |
strncat_s(szBCryptDllPath, |
| 63 |
sizeof(szBCryptDllPath), "\\bcrypt.dll", 11) == 0) { |
| 64 |
HANDLE hBCryptDll = LoadLibrary(szBCryptDllPath); |
| 65 |
|
| 66 |
InterlockedCompareExchangeRelease(&wincng_available, |
| 67 |
!!hBCryptDll, -1); |
| 68 |
if (hBCryptDll) |
| 69 |
FreeLibrary(hBCryptDll); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
return wincng_available == 1; |
| 74 |
} |
| 75 |
|
| 76 |
BOOL WINAPI |
| 77 |
_hc_w32crypto_DllMain(HINSTANCE hinstDLL, |
| 78 |
DWORD fdwReason, |
| 79 |
LPVOID lpvReserved) |
| 80 |
{ |
| 81 |
if (fdwReason == DLL_PROCESS_DETACH) { |
| 82 |
/* |
| 83 |
* Don't bother cleaning up on process exit, only on |
| 84 |
* FreeLibrary() (in which case lpvReserved will be NULL). |
| 85 |
*/ |
| 86 |
if (lpvReserved == NULL) |
| 87 |
_hc_wincng_cleanup(); |
| 88 |
} |
| 89 |
|
| 90 |
return TRUE; |
| 91 |
} |
| 92 |
|
| 93 |
#define EVP_W32CRYPTO_PROVIDER(type, name) \ |
| 94 |
\ |
| 95 |
const type *hc_EVP_w32crypto_ ##name (void) \ |
| 96 |
{ \ |
| 97 |
if (wincng_check_availability()) \ |
| 98 |
return hc_EVP_wincng_ ##name (); \ |
| 99 |
else if (HCRYPTO_FALLBACK) \ |
| 100 |
return hc_EVP_hcrypto_ ##name (); \ |
| 101 |
else \ |
| 102 |
return NULL; \ |
| 103 |
} |
| 104 |
|
| 105 |
#define EVP_W32CRYPTO_PROVIDER_CNG_UNAVAILABLE(type, name) \ |
| 106 |
\ |
| 107 |
const type *hc_EVP_w32crypto_ ##name (void) \ |
| 108 |
{ \ |
| 109 |
return hc_EVP_hcrypto_ ##name (); \ |
| 110 |
} |
| 111 |
|
| 112 |
EVP_W32CRYPTO_PROVIDER(EVP_MD, md4) |
| 113 |
EVP_W32CRYPTO_PROVIDER(EVP_MD, md5) |
| 114 |
EVP_W32CRYPTO_PROVIDER(EVP_MD, sha1) |
| 115 |
EVP_W32CRYPTO_PROVIDER(EVP_MD, sha256) |
| 116 |
EVP_W32CRYPTO_PROVIDER(EVP_MD, sha384) |
| 117 |
EVP_W32CRYPTO_PROVIDER(EVP_MD, sha512) |
| 118 |
|
| 119 |
EVP_W32CRYPTO_PROVIDER(EVP_CIPHER, rc2_cbc) |
| 120 |
EVP_W32CRYPTO_PROVIDER(EVP_CIPHER, rc2_40_cbc) |
| 121 |
EVP_W32CRYPTO_PROVIDER(EVP_CIPHER, rc2_64_cbc) |
| 122 |
|
| 123 |
EVP_W32CRYPTO_PROVIDER(EVP_CIPHER, rc4) |
| 124 |
EVP_W32CRYPTO_PROVIDER(EVP_CIPHER, rc4_40) |
| 125 |
|
| 126 |
EVP_W32CRYPTO_PROVIDER(EVP_CIPHER, des_cbc) |
| 127 |
EVP_W32CRYPTO_PROVIDER(EVP_CIPHER, des_ede3_cbc) |
| 128 |
|
| 129 |
EVP_W32CRYPTO_PROVIDER(EVP_CIPHER, aes_128_cbc) |
| 130 |
EVP_W32CRYPTO_PROVIDER(EVP_CIPHER, aes_192_cbc) |
| 131 |
EVP_W32CRYPTO_PROVIDER(EVP_CIPHER, aes_256_cbc) |
| 132 |
|
| 133 |
EVP_W32CRYPTO_PROVIDER(EVP_CIPHER, aes_128_cfb8) |
| 134 |
EVP_W32CRYPTO_PROVIDER(EVP_CIPHER, aes_192_cfb8) |
| 135 |
EVP_W32CRYPTO_PROVIDER(EVP_CIPHER, aes_256_cfb8) |
| 136 |
|
| 137 |
EVP_W32CRYPTO_PROVIDER_CNG_UNAVAILABLE(EVP_CIPHER, camellia_128_cbc) |
| 138 |
EVP_W32CRYPTO_PROVIDER_CNG_UNAVAILABLE(EVP_CIPHER, camellia_192_cbc) |
| 139 |
EVP_W32CRYPTO_PROVIDER_CNG_UNAVAILABLE(EVP_CIPHER, camellia_256_cbc) |