crypto_cryptoapi.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801
  1. /*
  2. * WPA Supplicant / Crypto wrapper for Microsoft CryptoAPI
  3. * Copyright (c) 2005-2006, Jouni Malinen <j@w1.fi>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * Alternatively, this software may be distributed under the terms of BSD
  10. * license.
  11. *
  12. * See README and COPYING for more details.
  13. */
  14. #include "includes.h"
  15. #include <windows.h>
  16. #include <wincrypt.h>
  17. #include "common.h"
  18. #include "crypto.h"
  19. #ifndef MS_ENH_RSA_AES_PROV
  20. #ifdef UNICODE
  21. #define MS_ENH_RSA_AES_PROV \
  22. L"Microsoft Enhanced RSA and AES Cryptographic Provider (Prototype)"
  23. #else
  24. #define MS_ENH_RSA_AES_PROV \
  25. "Microsoft Enhanced RSA and AES Cryptographic Provider (Prototype)"
  26. #endif
  27. #endif /* MS_ENH_RSA_AES_PROV */
  28. #ifndef CALG_HMAC
  29. #define CALG_HMAC (ALG_CLASS_HASH | ALG_TYPE_ANY | ALG_SID_HMAC)
  30. #endif
  31. #ifdef CONFIG_TLS_INTERNAL
  32. #ifdef __MINGW32_VERSION
  33. /*
  34. * MinGW does not yet include all the needed definitions for CryptoAPI, so
  35. * define here whatever extra is needed.
  36. */
  37. static PCCERT_CONTEXT WINAPI
  38. (*CertCreateCertificateContext)(DWORD dwCertEncodingType,
  39. const BYTE *pbCertEncoded,
  40. DWORD cbCertEncoded)
  41. = NULL; /* to be loaded from crypt32.dll */
  42. static BOOL WINAPI
  43. (*CryptImportPublicKeyInfo)(HCRYPTPROV hCryptProv, DWORD dwCertEncodingType,
  44. PCERT_PUBLIC_KEY_INFO pInfo, HCRYPTKEY *phKey)
  45. = NULL; /* to be loaded from crypt32.dll */
  46. static int mingw_load_crypto_func(void)
  47. {
  48. HINSTANCE dll;
  49. /* MinGW does not yet have full CryptoAPI support, so load the needed
  50. * function here. */
  51. if (CertCreateCertificateContext)
  52. return 0;
  53. dll = LoadLibrary("crypt32");
  54. if (dll == NULL) {
  55. wpa_printf(MSG_DEBUG, "CryptoAPI: Could not load crypt32 "
  56. "library");
  57. return -1;
  58. }
  59. CertCreateCertificateContext = (void *) GetProcAddress(
  60. dll, "CertCreateCertificateContext");
  61. if (CertCreateCertificateContext == NULL) {
  62. wpa_printf(MSG_DEBUG, "CryptoAPI: Could not get "
  63. "CertCreateCertificateContext() address from "
  64. "crypt32 library");
  65. return -1;
  66. }
  67. CryptImportPublicKeyInfo = GetProcAddress(
  68. dll, "CryptImportPublicKeyInfo");
  69. if (CryptImportPublicKeyInfo == NULL) {
  70. wpa_printf(MSG_DEBUG, "CryptoAPI: Could not get "
  71. "CryptImportPublicKeyInfo() address from "
  72. "crypt32 library");
  73. return -1;
  74. }
  75. return 0;
  76. }
  77. #else /* __MINGW32_VERSION */
  78. static int mingw_load_crypto_func(void)
  79. {
  80. return 0;
  81. }
  82. #endif /* __MINGW32_VERSION */
  83. #endif /* CONFIG_TLS_INTERNAL */
  84. static void cryptoapi_report_error(const char *msg)
  85. {
  86. char *s, *pos;
  87. DWORD err = GetLastError();
  88. if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
  89. FORMAT_MESSAGE_FROM_SYSTEM,
  90. NULL, err, 0, (LPTSTR) &s, 0, NULL) == 0) {
  91. wpa_printf(MSG_DEBUG, "CryptoAPI: %s: %d", msg, (int) err);
  92. }
  93. pos = s;
  94. while (*pos) {
  95. if (*pos == '\n' || *pos == '\r') {
  96. *pos = '\0';
  97. break;
  98. }
  99. pos++;
  100. }
  101. wpa_printf(MSG_DEBUG, "CryptoAPI: %s: %d: (%s)", msg, (int) err, s);
  102. LocalFree(s);
  103. }
  104. int cryptoapi_hash_vector(ALG_ID alg, size_t hash_len, size_t num_elem,
  105. const u8 *addr[], const size_t *len, u8 *mac)
  106. {
  107. HCRYPTPROV prov;
  108. HCRYPTHASH hash;
  109. size_t i;
  110. DWORD hlen;
  111. int ret = 0;
  112. if (!CryptAcquireContext(&prov, NULL, NULL, PROV_RSA_FULL, 0)) {
  113. cryptoapi_report_error("CryptAcquireContext");
  114. return -1;
  115. }
  116. if (!CryptCreateHash(prov, alg, 0, 0, &hash)) {
  117. cryptoapi_report_error("CryptCreateHash");
  118. CryptReleaseContext(prov, 0);
  119. return -1;
  120. }
  121. for (i = 0; i < num_elem; i++) {
  122. if (!CryptHashData(hash, (BYTE *) addr[i], len[i], 0)) {
  123. cryptoapi_report_error("CryptHashData");
  124. CryptDestroyHash(hash);
  125. CryptReleaseContext(prov, 0);
  126. }
  127. }
  128. hlen = hash_len;
  129. if (!CryptGetHashParam(hash, HP_HASHVAL, mac, &hlen, 0)) {
  130. cryptoapi_report_error("CryptGetHashParam");
  131. ret = -1;
  132. }
  133. CryptDestroyHash(hash);
  134. CryptReleaseContext(prov, 0);
  135. return ret;
  136. }
  137. void md4_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
  138. {
  139. cryptoapi_hash_vector(CALG_MD4, 16, num_elem, addr, len, mac);
  140. }
  141. void des_encrypt(const u8 *clear, const u8 *key, u8 *cypher)
  142. {
  143. u8 next, tmp;
  144. int i;
  145. HCRYPTPROV prov;
  146. HCRYPTKEY ckey;
  147. DWORD dlen;
  148. struct {
  149. BLOBHEADER hdr;
  150. DWORD len;
  151. BYTE key[8];
  152. } key_blob;
  153. DWORD mode = CRYPT_MODE_ECB;
  154. key_blob.hdr.bType = PLAINTEXTKEYBLOB;
  155. key_blob.hdr.bVersion = CUR_BLOB_VERSION;
  156. key_blob.hdr.reserved = 0;
  157. key_blob.hdr.aiKeyAlg = CALG_DES;
  158. key_blob.len = 8;
  159. /* Add parity bits to the key */
  160. next = 0;
  161. for (i = 0; i < 7; i++) {
  162. tmp = key[i];
  163. key_blob.key[i] = (tmp >> i) | next | 1;
  164. next = tmp << (7 - i);
  165. }
  166. key_blob.key[i] = next | 1;
  167. if (!CryptAcquireContext(&prov, NULL, MS_ENHANCED_PROV, PROV_RSA_FULL,
  168. CRYPT_VERIFYCONTEXT)) {
  169. wpa_printf(MSG_DEBUG, "CryptoAPI: CryptAcquireContext failed: "
  170. "%d", (int) GetLastError());
  171. return;
  172. }
  173. if (!CryptImportKey(prov, (BYTE *) &key_blob, sizeof(key_blob), 0, 0,
  174. &ckey)) {
  175. wpa_printf(MSG_DEBUG, "CryptoAPI: CryptImportKey failed: %d",
  176. (int) GetLastError());
  177. CryptReleaseContext(prov, 0);
  178. return;
  179. }
  180. if (!CryptSetKeyParam(ckey, KP_MODE, (BYTE *) &mode, 0)) {
  181. wpa_printf(MSG_DEBUG, "CryptoAPI: CryptSetKeyParam(KP_MODE) "
  182. "failed: %d", (int) GetLastError());
  183. CryptDestroyKey(ckey);
  184. CryptReleaseContext(prov, 0);
  185. return;
  186. }
  187. os_memcpy(cypher, clear, 8);
  188. dlen = 8;
  189. if (!CryptEncrypt(ckey, 0, FALSE, 0, cypher, &dlen, 8)) {
  190. wpa_printf(MSG_DEBUG, "CryptoAPI: CryptEncrypt failed: %d",
  191. (int) GetLastError());
  192. os_memset(cypher, 0, 8);
  193. }
  194. CryptDestroyKey(ckey);
  195. CryptReleaseContext(prov, 0);
  196. }
  197. #ifdef EAP_TLS_FUNCS
  198. void md5_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
  199. {
  200. cryptoapi_hash_vector(CALG_MD5, 16, num_elem, addr, len, mac);
  201. }
  202. void sha1_vector(size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
  203. {
  204. cryptoapi_hash_vector(CALG_SHA, 20, num_elem, addr, len, mac);
  205. }
  206. struct aes_context {
  207. HCRYPTPROV prov;
  208. HCRYPTKEY ckey;
  209. };
  210. void * aes_encrypt_init(const u8 *key, size_t len)
  211. {
  212. struct aes_context *akey;
  213. struct {
  214. BLOBHEADER hdr;
  215. DWORD len;
  216. BYTE key[16];
  217. } key_blob;
  218. DWORD mode = CRYPT_MODE_ECB;
  219. if (len != 16)
  220. return NULL;
  221. key_blob.hdr.bType = PLAINTEXTKEYBLOB;
  222. key_blob.hdr.bVersion = CUR_BLOB_VERSION;
  223. key_blob.hdr.reserved = 0;
  224. key_blob.hdr.aiKeyAlg = CALG_AES_128;
  225. key_blob.len = len;
  226. os_memcpy(key_blob.key, key, len);
  227. akey = os_zalloc(sizeof(*akey));
  228. if (akey == NULL)
  229. return NULL;
  230. if (!CryptAcquireContext(&akey->prov, NULL,
  231. MS_ENH_RSA_AES_PROV, PROV_RSA_AES,
  232. CRYPT_VERIFYCONTEXT)) {
  233. wpa_printf(MSG_DEBUG, "CryptoAPI: CryptAcquireContext failed: "
  234. "%d", (int) GetLastError());
  235. os_free(akey);
  236. return NULL;
  237. }
  238. if (!CryptImportKey(akey->prov, (BYTE *) &key_blob, sizeof(key_blob),
  239. 0, 0, &akey->ckey)) {
  240. wpa_printf(MSG_DEBUG, "CryptoAPI: CryptImportKey failed: %d",
  241. (int) GetLastError());
  242. CryptReleaseContext(akey->prov, 0);
  243. os_free(akey);
  244. return NULL;
  245. }
  246. if (!CryptSetKeyParam(akey->ckey, KP_MODE, (BYTE *) &mode, 0)) {
  247. wpa_printf(MSG_DEBUG, "CryptoAPI: CryptSetKeyParam(KP_MODE) "
  248. "failed: %d", (int) GetLastError());
  249. CryptDestroyKey(akey->ckey);
  250. CryptReleaseContext(akey->prov, 0);
  251. os_free(akey);
  252. return NULL;
  253. }
  254. return akey;
  255. }
  256. void aes_encrypt(void *ctx, const u8 *plain, u8 *crypt)
  257. {
  258. struct aes_context *akey = ctx;
  259. DWORD dlen;
  260. os_memcpy(crypt, plain, 16);
  261. dlen = 16;
  262. if (!CryptEncrypt(akey->ckey, 0, FALSE, 0, crypt, &dlen, 16)) {
  263. wpa_printf(MSG_DEBUG, "CryptoAPI: CryptEncrypt failed: %d",
  264. (int) GetLastError());
  265. os_memset(crypt, 0, 16);
  266. }
  267. }
  268. void aes_encrypt_deinit(void *ctx)
  269. {
  270. struct aes_context *akey = ctx;
  271. if (akey) {
  272. CryptDestroyKey(akey->ckey);
  273. CryptReleaseContext(akey->prov, 0);
  274. os_free(akey);
  275. }
  276. }
  277. void * aes_decrypt_init(const u8 *key, size_t len)
  278. {
  279. return aes_encrypt_init(key, len);
  280. }
  281. void aes_decrypt(void *ctx, const u8 *crypt, u8 *plain)
  282. {
  283. struct aes_context *akey = ctx;
  284. DWORD dlen;
  285. os_memcpy(plain, crypt, 16);
  286. dlen = 16;
  287. if (!CryptDecrypt(akey->ckey, 0, FALSE, 0, plain, &dlen)) {
  288. wpa_printf(MSG_DEBUG, "CryptoAPI: CryptDecrypt failed: %d",
  289. (int) GetLastError());
  290. }
  291. }
  292. void aes_decrypt_deinit(void *ctx)
  293. {
  294. aes_encrypt_deinit(ctx);
  295. }
  296. #ifdef CONFIG_TLS_INTERNAL
  297. struct crypto_hash {
  298. enum crypto_hash_alg alg;
  299. int error;
  300. HCRYPTPROV prov;
  301. HCRYPTHASH hash;
  302. HCRYPTKEY key;
  303. };
  304. struct crypto_hash * crypto_hash_init(enum crypto_hash_alg alg, const u8 *key,
  305. size_t key_len)
  306. {
  307. struct crypto_hash *ctx;
  308. ALG_ID calg;
  309. struct {
  310. BLOBHEADER hdr;
  311. DWORD len;
  312. BYTE key[32];
  313. } key_blob;
  314. os_memset(&key_blob, 0, sizeof(key_blob));
  315. switch (alg) {
  316. case CRYPTO_HASH_ALG_MD5:
  317. calg = CALG_MD5;
  318. break;
  319. case CRYPTO_HASH_ALG_SHA1:
  320. calg = CALG_SHA;
  321. break;
  322. case CRYPTO_HASH_ALG_HMAC_MD5:
  323. case CRYPTO_HASH_ALG_HMAC_SHA1:
  324. calg = CALG_HMAC;
  325. key_blob.hdr.bType = PLAINTEXTKEYBLOB;
  326. key_blob.hdr.bVersion = CUR_BLOB_VERSION;
  327. key_blob.hdr.reserved = 0;
  328. /*
  329. * Note: RC2 is not really used, but that can be used to
  330. * import HMAC keys of up to 16 byte long.
  331. * CRYPT_IPSEC_HMAC_KEY flag for CryptImportKey() is needed to
  332. * be able to import longer keys (HMAC-SHA1 uses 20-byte key).
  333. */
  334. key_blob.hdr.aiKeyAlg = CALG_RC2;
  335. key_blob.len = key_len;
  336. if (key_len > sizeof(key_blob.key))
  337. return NULL;
  338. os_memcpy(key_blob.key, key, key_len);
  339. break;
  340. default:
  341. return NULL;
  342. }
  343. ctx = os_zalloc(sizeof(*ctx));
  344. if (ctx == NULL)
  345. return NULL;
  346. ctx->alg = alg;
  347. if (!CryptAcquireContext(&ctx->prov, NULL, NULL, PROV_RSA_FULL, 0)) {
  348. cryptoapi_report_error("CryptAcquireContext");
  349. os_free(ctx);
  350. return NULL;
  351. }
  352. if (calg == CALG_HMAC) {
  353. #ifndef CRYPT_IPSEC_HMAC_KEY
  354. #define CRYPT_IPSEC_HMAC_KEY 0x00000100
  355. #endif
  356. if (!CryptImportKey(ctx->prov, (BYTE *) &key_blob,
  357. sizeof(key_blob), 0, CRYPT_IPSEC_HMAC_KEY,
  358. &ctx->key)) {
  359. cryptoapi_report_error("CryptImportKey");
  360. CryptReleaseContext(ctx->prov, 0);
  361. os_free(ctx);
  362. return NULL;
  363. }
  364. }
  365. if (!CryptCreateHash(ctx->prov, calg, ctx->key, 0, &ctx->hash)) {
  366. cryptoapi_report_error("CryptCreateHash");
  367. CryptReleaseContext(ctx->prov, 0);
  368. os_free(ctx);
  369. return NULL;
  370. }
  371. if (calg == CALG_HMAC) {
  372. HMAC_INFO info;
  373. os_memset(&info, 0, sizeof(info));
  374. switch (alg) {
  375. case CRYPTO_HASH_ALG_HMAC_MD5:
  376. info.HashAlgid = CALG_MD5;
  377. break;
  378. case CRYPTO_HASH_ALG_HMAC_SHA1:
  379. info.HashAlgid = CALG_SHA;
  380. break;
  381. default:
  382. /* unreachable */
  383. break;
  384. }
  385. if (!CryptSetHashParam(ctx->hash, HP_HMAC_INFO, (BYTE *) &info,
  386. 0)) {
  387. cryptoapi_report_error("CryptSetHashParam");
  388. CryptDestroyHash(ctx->hash);
  389. CryptReleaseContext(ctx->prov, 0);
  390. os_free(ctx);
  391. return NULL;
  392. }
  393. }
  394. return ctx;
  395. }
  396. void crypto_hash_update(struct crypto_hash *ctx, const u8 *data, size_t len)
  397. {
  398. if (ctx == NULL || ctx->error)
  399. return;
  400. if (!CryptHashData(ctx->hash, (BYTE *) data, len, 0)) {
  401. cryptoapi_report_error("CryptHashData");
  402. ctx->error = 1;
  403. }
  404. }
  405. int crypto_hash_finish(struct crypto_hash *ctx, u8 *mac, size_t *len)
  406. {
  407. int ret = 0;
  408. DWORD hlen;
  409. if (ctx == NULL)
  410. return -2;
  411. if (mac == NULL || len == NULL)
  412. goto done;
  413. if (ctx->error) {
  414. ret = -2;
  415. goto done;
  416. }
  417. hlen = *len;
  418. if (!CryptGetHashParam(ctx->hash, HP_HASHVAL, mac, &hlen, 0)) {
  419. cryptoapi_report_error("CryptGetHashParam");
  420. ret = -2;
  421. }
  422. *len = hlen;
  423. done:
  424. if (ctx->alg == CRYPTO_HASH_ALG_HMAC_SHA1 ||
  425. ctx->alg == CRYPTO_HASH_ALG_HMAC_MD5)
  426. CryptDestroyKey(ctx->key);
  427. os_free(ctx);
  428. return ret;
  429. }
  430. struct crypto_cipher {
  431. HCRYPTPROV prov;
  432. HCRYPTKEY key;
  433. };
  434. struct crypto_cipher * crypto_cipher_init(enum crypto_cipher_alg alg,
  435. const u8 *iv, const u8 *key,
  436. size_t key_len)
  437. {
  438. struct crypto_cipher *ctx;
  439. struct {
  440. BLOBHEADER hdr;
  441. DWORD len;
  442. BYTE key[32];
  443. } key_blob;
  444. DWORD mode = CRYPT_MODE_CBC;
  445. key_blob.hdr.bType = PLAINTEXTKEYBLOB;
  446. key_blob.hdr.bVersion = CUR_BLOB_VERSION;
  447. key_blob.hdr.reserved = 0;
  448. key_blob.len = key_len;
  449. if (key_len > sizeof(key_blob.key))
  450. return NULL;
  451. os_memcpy(key_blob.key, key, key_len);
  452. switch (alg) {
  453. case CRYPTO_CIPHER_ALG_AES:
  454. if (key_len == 32)
  455. key_blob.hdr.aiKeyAlg = CALG_AES_256;
  456. else if (key_len == 24)
  457. key_blob.hdr.aiKeyAlg = CALG_AES_192;
  458. else
  459. key_blob.hdr.aiKeyAlg = CALG_AES_128;
  460. break;
  461. case CRYPTO_CIPHER_ALG_3DES:
  462. key_blob.hdr.aiKeyAlg = CALG_3DES;
  463. break;
  464. case CRYPTO_CIPHER_ALG_DES:
  465. key_blob.hdr.aiKeyAlg = CALG_DES;
  466. break;
  467. case CRYPTO_CIPHER_ALG_RC2:
  468. key_blob.hdr.aiKeyAlg = CALG_RC2;
  469. break;
  470. case CRYPTO_CIPHER_ALG_RC4:
  471. key_blob.hdr.aiKeyAlg = CALG_RC4;
  472. break;
  473. default:
  474. return NULL;
  475. }
  476. ctx = os_zalloc(sizeof(*ctx));
  477. if (ctx == NULL)
  478. return NULL;
  479. if (!CryptAcquireContext(&ctx->prov, NULL, MS_ENH_RSA_AES_PROV,
  480. PROV_RSA_AES, CRYPT_VERIFYCONTEXT)) {
  481. cryptoapi_report_error("CryptAcquireContext");
  482. goto fail1;
  483. }
  484. if (!CryptImportKey(ctx->prov, (BYTE *) &key_blob,
  485. sizeof(key_blob), 0, 0, &ctx->key)) {
  486. cryptoapi_report_error("CryptImportKey");
  487. goto fail2;
  488. }
  489. if (!CryptSetKeyParam(ctx->key, KP_MODE, (BYTE *) &mode, 0)) {
  490. cryptoapi_report_error("CryptSetKeyParam(KP_MODE)");
  491. goto fail3;
  492. }
  493. if (iv && !CryptSetKeyParam(ctx->key, KP_IV, (BYTE *) iv, 0)) {
  494. cryptoapi_report_error("CryptSetKeyParam(KP_IV)");
  495. goto fail3;
  496. }
  497. return ctx;
  498. fail3:
  499. CryptDestroyKey(ctx->key);
  500. fail2:
  501. CryptReleaseContext(ctx->prov, 0);
  502. fail1:
  503. os_free(ctx);
  504. return NULL;
  505. }
  506. int crypto_cipher_encrypt(struct crypto_cipher *ctx, const u8 *plain,
  507. u8 *crypt, size_t len)
  508. {
  509. DWORD dlen;
  510. os_memcpy(crypt, plain, len);
  511. dlen = len;
  512. if (!CryptEncrypt(ctx->key, 0, FALSE, 0, crypt, &dlen, len)) {
  513. cryptoapi_report_error("CryptEncrypt");
  514. os_memset(crypt, 0, len);
  515. return -1;
  516. }
  517. return 0;
  518. }
  519. int crypto_cipher_decrypt(struct crypto_cipher *ctx, const u8 *crypt,
  520. u8 *plain, size_t len)
  521. {
  522. DWORD dlen;
  523. os_memcpy(plain, crypt, len);
  524. dlen = len;
  525. if (!CryptDecrypt(ctx->key, 0, FALSE, 0, plain, &dlen)) {
  526. cryptoapi_report_error("CryptDecrypt");
  527. return -1;
  528. }
  529. return 0;
  530. }
  531. void crypto_cipher_deinit(struct crypto_cipher *ctx)
  532. {
  533. CryptDestroyKey(ctx->key);
  534. CryptReleaseContext(ctx->prov, 0);
  535. os_free(ctx);
  536. }
  537. struct crypto_public_key {
  538. HCRYPTPROV prov;
  539. HCRYPTKEY rsa;
  540. };
  541. struct crypto_private_key {
  542. HCRYPTPROV prov;
  543. HCRYPTKEY rsa;
  544. };
  545. struct crypto_public_key * crypto_public_key_import(const u8 *key, size_t len)
  546. {
  547. /* Use crypto_public_key_from_cert() instead. */
  548. return NULL;
  549. }
  550. struct crypto_private_key * crypto_private_key_import(const u8 *key,
  551. size_t len)
  552. {
  553. /* TODO */
  554. return NULL;
  555. }
  556. struct crypto_public_key * crypto_public_key_from_cert(const u8 *buf,
  557. size_t len)
  558. {
  559. struct crypto_public_key *pk;
  560. PCCERT_CONTEXT cc;
  561. pk = os_zalloc(sizeof(*pk));
  562. if (pk == NULL)
  563. return NULL;
  564. cc = CertCreateCertificateContext(X509_ASN_ENCODING |
  565. PKCS_7_ASN_ENCODING, buf, len);
  566. if (!cc) {
  567. cryptoapi_report_error("CryptCreateCertificateContext");
  568. os_free(pk);
  569. return NULL;
  570. }
  571. if (!CryptAcquireContext(&pk->prov, NULL, MS_DEF_PROV, PROV_RSA_FULL,
  572. 0)) {
  573. cryptoapi_report_error("CryptAcquireContext");
  574. os_free(pk);
  575. CertFreeCertificateContext(cc);
  576. return NULL;
  577. }
  578. if (!CryptImportPublicKeyInfo(pk->prov, X509_ASN_ENCODING |
  579. PKCS_7_ASN_ENCODING,
  580. &cc->pCertInfo->SubjectPublicKeyInfo,
  581. &pk->rsa)) {
  582. cryptoapi_report_error("CryptImportPublicKeyInfo");
  583. CryptReleaseContext(pk->prov, 0);
  584. os_free(pk);
  585. CertFreeCertificateContext(cc);
  586. return NULL;
  587. }
  588. CertFreeCertificateContext(cc);
  589. return pk;
  590. }
  591. int crypto_public_key_encrypt_pkcs1_v15(struct crypto_public_key *key,
  592. const u8 *in, size_t inlen,
  593. u8 *out, size_t *outlen)
  594. {
  595. DWORD clen;
  596. u8 *tmp;
  597. size_t i;
  598. if (*outlen < inlen)
  599. return -1;
  600. tmp = malloc(*outlen);
  601. if (tmp == NULL)
  602. return -1;
  603. os_memcpy(tmp, in, inlen);
  604. clen = inlen;
  605. if (!CryptEncrypt(key->rsa, 0, TRUE, 0, tmp, &clen, *outlen)) {
  606. wpa_printf(MSG_DEBUG, "CryptoAPI: Failed to encrypt using "
  607. "public key: %d", (int) GetLastError());
  608. os_free(tmp);
  609. return -1;
  610. }
  611. *outlen = clen;
  612. /* Reverse the output */
  613. for (i = 0; i < *outlen; i++)
  614. out[i] = tmp[*outlen - 1 - i];
  615. os_free(tmp);
  616. return 0;
  617. }
  618. int crypto_private_key_sign_pkcs1(struct crypto_private_key *key,
  619. const u8 *in, size_t inlen,
  620. u8 *out, size_t *outlen)
  621. {
  622. /* TODO */
  623. return -1;
  624. }
  625. void crypto_public_key_free(struct crypto_public_key *key)
  626. {
  627. if (key) {
  628. CryptDestroyKey(key->rsa);
  629. CryptReleaseContext(key->prov, 0);
  630. os_free(key);
  631. }
  632. }
  633. void crypto_private_key_free(struct crypto_private_key *key)
  634. {
  635. if (key) {
  636. CryptDestroyKey(key->rsa);
  637. CryptReleaseContext(key->prov, 0);
  638. os_free(key);
  639. }
  640. }
  641. int crypto_global_init(void)
  642. {
  643. return mingw_load_crypto_func();
  644. }
  645. void crypto_global_deinit(void)
  646. {
  647. }
  648. #endif /* CONFIG_TLS_INTERNAL */
  649. #endif /* EAP_TLS_FUNCS */