pkcs8.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211
  1. /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
  2. * project 1999.
  3. */
  4. /* ====================================================================
  5. * Copyright (c) 1999 The OpenSSL Project. All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * 3. All advertising materials mentioning features or use of this
  20. * software must display the following acknowledgment:
  21. * "This product includes software developed by the OpenSSL Project
  22. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  23. *
  24. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  25. * endorse or promote products derived from this software without
  26. * prior written permission. For written permission, please contact
  27. * licensing@OpenSSL.org.
  28. *
  29. * 5. Products derived from this software may not be called "OpenSSL"
  30. * nor may "OpenSSL" appear in their names without prior written
  31. * permission of the OpenSSL Project.
  32. *
  33. * 6. Redistributions of any form whatsoever must retain the following
  34. * acknowledgment:
  35. * "This product includes software developed by the OpenSSL Project
  36. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  37. *
  38. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  39. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  40. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  41. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  42. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  43. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  44. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  45. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  46. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  47. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  48. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  49. * OF THE POSSIBILITY OF SUCH DAMAGE.
  50. * ====================================================================
  51. *
  52. * This product includes cryptographic software written by Eric Young
  53. * (eay@cryptsoft.com). This product includes software written by Tim
  54. * Hudson (tjh@cryptsoft.com). */
  55. #include <openssl/pkcs8.h>
  56. #include <assert.h>
  57. #include <limits.h>
  58. #include <string.h>
  59. #include <openssl/asn1.h>
  60. #include <openssl/bn.h>
  61. #include <openssl/buf.h>
  62. #include <openssl/bytestring.h>
  63. #include <openssl/cipher.h>
  64. #include <openssl/digest.h>
  65. #include <openssl/err.h>
  66. #include <openssl/hmac.h>
  67. #include <openssl/mem.h>
  68. #include <openssl/x509.h>
  69. #include "internal.h"
  70. #include "../bytestring/internal.h"
  71. #define PKCS12_KEY_ID 1
  72. #define PKCS12_IV_ID 2
  73. #define PKCS12_MAC_ID 3
  74. static int ascii_to_ucs2(const char *ascii, size_t ascii_len,
  75. uint8_t **out, size_t *out_len) {
  76. uint8_t *unitmp;
  77. size_t ulen, i;
  78. ulen = ascii_len * 2 + 2;
  79. if (ulen < ascii_len) {
  80. return 0;
  81. }
  82. unitmp = OPENSSL_malloc(ulen);
  83. if (unitmp == NULL) {
  84. return 0;
  85. }
  86. for (i = 0; i < ulen - 2; i += 2) {
  87. unitmp[i] = 0;
  88. unitmp[i + 1] = ascii[i >> 1];
  89. }
  90. /* Make result double null terminated */
  91. unitmp[ulen - 2] = 0;
  92. unitmp[ulen - 1] = 0;
  93. *out_len = ulen;
  94. *out = unitmp;
  95. return 1;
  96. }
  97. static int pkcs12_key_gen_raw(const uint8_t *pass_raw, size_t pass_raw_len,
  98. const uint8_t *salt, size_t salt_len,
  99. int id, int iterations,
  100. size_t out_len, uint8_t *out,
  101. const EVP_MD *md_type) {
  102. uint8_t *B, *D, *I, *p, *Ai;
  103. int Slen, Plen, Ilen, Ijlen;
  104. int i, j, v;
  105. size_t u;
  106. int ret = 0;
  107. BIGNUM *Ij, *Bpl1; /* These hold Ij and B + 1 */
  108. EVP_MD_CTX ctx;
  109. EVP_MD_CTX_init(&ctx);
  110. v = EVP_MD_block_size(md_type);
  111. u = EVP_MD_size(md_type);
  112. D = OPENSSL_malloc(v);
  113. Ai = OPENSSL_malloc(u);
  114. B = OPENSSL_malloc(v + 1);
  115. Slen = v * ((salt_len + v - 1) / v);
  116. if (pass_raw_len) {
  117. Plen = v * ((pass_raw_len + v - 1) / v);
  118. } else {
  119. Plen = 0;
  120. }
  121. Ilen = Slen + Plen;
  122. I = OPENSSL_malloc(Ilen);
  123. Ij = BN_new();
  124. Bpl1 = BN_new();
  125. if (!D || !Ai || !B || !I || !Ij || !Bpl1) {
  126. goto err;
  127. }
  128. for (i = 0; i < v; i++) {
  129. D[i] = id;
  130. }
  131. p = I;
  132. for (i = 0; i < Slen; i++) {
  133. *p++ = salt[i % salt_len];
  134. }
  135. for (i = 0; i < Plen; i++) {
  136. *p++ = pass_raw[i % pass_raw_len];
  137. }
  138. for (;;) {
  139. if (!EVP_DigestInit_ex(&ctx, md_type, NULL) ||
  140. !EVP_DigestUpdate(&ctx, D, v) ||
  141. !EVP_DigestUpdate(&ctx, I, Ilen) ||
  142. !EVP_DigestFinal_ex(&ctx, Ai, NULL)) {
  143. goto err;
  144. }
  145. for (j = 1; j < iterations; j++) {
  146. if (!EVP_DigestInit_ex(&ctx, md_type, NULL) ||
  147. !EVP_DigestUpdate(&ctx, Ai, u) ||
  148. !EVP_DigestFinal_ex(&ctx, Ai, NULL)) {
  149. goto err;
  150. }
  151. }
  152. memcpy(out, Ai, out_len < u ? out_len : u);
  153. if (u >= out_len) {
  154. ret = 1;
  155. goto end;
  156. }
  157. out_len -= u;
  158. out += u;
  159. for (j = 0; j < v; j++) {
  160. B[j] = Ai[j % u];
  161. }
  162. /* Work out B + 1 first then can use B as tmp space */
  163. if (!BN_bin2bn(B, v, Bpl1) ||
  164. !BN_add_word(Bpl1, 1)) {
  165. goto err;
  166. }
  167. for (j = 0; j < Ilen; j += v) {
  168. if (!BN_bin2bn(I + j, v, Ij) ||
  169. !BN_add(Ij, Ij, Bpl1) ||
  170. !BN_bn2bin(Ij, B)) {
  171. goto err;
  172. }
  173. Ijlen = BN_num_bytes(Ij);
  174. /* If more than 2^(v*8) - 1 cut off MSB */
  175. if (Ijlen > v) {
  176. if (!BN_bn2bin(Ij, B)) {
  177. goto err;
  178. }
  179. memcpy(I + j, B + 1, v);
  180. /* If less than v bytes pad with zeroes */
  181. } else if (Ijlen < v) {
  182. memset(I + j, 0, v - Ijlen);
  183. if (!BN_bn2bin(Ij, I + j + v - Ijlen)) {
  184. goto err;
  185. }
  186. } else if (!BN_bn2bin(Ij, I + j)) {
  187. goto err;
  188. }
  189. }
  190. }
  191. err:
  192. OPENSSL_PUT_ERROR(PKCS8, ERR_R_MALLOC_FAILURE);
  193. end:
  194. OPENSSL_free(Ai);
  195. OPENSSL_free(B);
  196. OPENSSL_free(D);
  197. OPENSSL_free(I);
  198. BN_free(Ij);
  199. BN_free(Bpl1);
  200. EVP_MD_CTX_cleanup(&ctx);
  201. return ret;
  202. }
  203. static int pkcs12_pbe_keyivgen(EVP_CIPHER_CTX *ctx, const uint8_t *pass_raw,
  204. size_t pass_raw_len, ASN1_TYPE *param,
  205. const EVP_CIPHER *cipher, const EVP_MD *md,
  206. int is_encrypt) {
  207. PBEPARAM *pbe;
  208. int salt_len, iterations, ret;
  209. uint8_t *salt;
  210. const uint8_t *pbuf;
  211. uint8_t key[EVP_MAX_KEY_LENGTH], iv[EVP_MAX_IV_LENGTH];
  212. /* Extract useful info from parameter */
  213. if (param == NULL || param->type != V_ASN1_SEQUENCE ||
  214. param->value.sequence == NULL) {
  215. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_DECODE_ERROR);
  216. return 0;
  217. }
  218. pbuf = param->value.sequence->data;
  219. pbe = d2i_PBEPARAM(NULL, &pbuf, param->value.sequence->length);
  220. if (pbe == NULL) {
  221. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_DECODE_ERROR);
  222. return 0;
  223. }
  224. if (!pbe->iter) {
  225. iterations = 1;
  226. } else {
  227. iterations = ASN1_INTEGER_get(pbe->iter);
  228. }
  229. salt = pbe->salt->data;
  230. salt_len = pbe->salt->length;
  231. if (!pkcs12_key_gen_raw(pass_raw, pass_raw_len, salt, salt_len, PKCS12_KEY_ID,
  232. iterations, EVP_CIPHER_key_length(cipher), key, md)) {
  233. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_KEY_GEN_ERROR);
  234. PBEPARAM_free(pbe);
  235. return 0;
  236. }
  237. if (!pkcs12_key_gen_raw(pass_raw, pass_raw_len, salt, salt_len, PKCS12_IV_ID,
  238. iterations, EVP_CIPHER_iv_length(cipher), iv, md)) {
  239. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_KEY_GEN_ERROR);
  240. PBEPARAM_free(pbe);
  241. return 0;
  242. }
  243. PBEPARAM_free(pbe);
  244. ret = EVP_CipherInit_ex(ctx, cipher, NULL, key, iv, is_encrypt);
  245. OPENSSL_cleanse(key, EVP_MAX_KEY_LENGTH);
  246. OPENSSL_cleanse(iv, EVP_MAX_IV_LENGTH);
  247. return ret;
  248. }
  249. typedef int (*keygen_func)(EVP_CIPHER_CTX *ctx, const uint8_t *pass_raw,
  250. size_t pass_raw_len, ASN1_TYPE *param,
  251. const EVP_CIPHER *cipher, const EVP_MD *md,
  252. int is_encrypt);
  253. struct pbe_suite {
  254. int pbe_nid;
  255. const EVP_CIPHER* (*cipher_func)(void);
  256. const EVP_MD* (*md_func)(void);
  257. keygen_func keygen;
  258. int flags;
  259. };
  260. #define PBE_UCS2_CONVERT_PASSWORD 0x1
  261. static const struct pbe_suite kBuiltinPBE[] = {
  262. {
  263. NID_pbe_WithSHA1And40BitRC2_CBC, EVP_rc2_40_cbc, EVP_sha1,
  264. pkcs12_pbe_keyivgen, PBE_UCS2_CONVERT_PASSWORD
  265. },
  266. {
  267. NID_pbe_WithSHA1And128BitRC4, EVP_rc4, EVP_sha1, pkcs12_pbe_keyivgen,
  268. PBE_UCS2_CONVERT_PASSWORD
  269. },
  270. {
  271. NID_pbe_WithSHA1And3_Key_TripleDES_CBC, EVP_des_ede3_cbc, EVP_sha1,
  272. pkcs12_pbe_keyivgen, PBE_UCS2_CONVERT_PASSWORD
  273. },
  274. {
  275. NID_pbes2, NULL, NULL, PKCS5_v2_PBE_keyivgen, 0
  276. },
  277. };
  278. static const struct pbe_suite *get_pbe_suite(int pbe_nid) {
  279. unsigned i;
  280. for (i = 0; i < sizeof(kBuiltinPBE) / sizeof(kBuiltinPBE[0]); i++) {
  281. if (kBuiltinPBE[i].pbe_nid == pbe_nid) {
  282. return &kBuiltinPBE[i];
  283. }
  284. }
  285. return NULL;
  286. }
  287. /* pass_to_pass_raw performs a password conversion (possibly a no-op)
  288. * appropriate to the supplied |pbe_nid|. The input |pass| is treated as a
  289. * NUL-terminated string if |pass_len| is -1, otherwise it is treated as a
  290. * buffer of the specified length. If the supplied PBE NID sets the
  291. * |PBE_UCS2_CONVERT_PASSWORD| flag, the supplied |pass| will be converted to
  292. * UCS-2.
  293. *
  294. * It sets |*out_pass_raw| to a new buffer that must be freed by the caller. It
  295. * returns one on success and zero on error. */
  296. static int pass_to_pass_raw(int pbe_nid, const char *pass, int pass_len,
  297. uint8_t **out_pass_raw, size_t *out_pass_raw_len) {
  298. if (pass == NULL) {
  299. *out_pass_raw = NULL;
  300. *out_pass_raw_len = 0;
  301. return 1;
  302. }
  303. if (pass_len == -1) {
  304. pass_len = strlen(pass);
  305. } else if (pass_len < 0 || pass_len > 2000000000) {
  306. OPENSSL_PUT_ERROR(PKCS8, ERR_R_OVERFLOW);
  307. return 0;
  308. }
  309. const struct pbe_suite *suite = get_pbe_suite(pbe_nid);
  310. if (suite != NULL && (suite->flags & PBE_UCS2_CONVERT_PASSWORD)) {
  311. if (!ascii_to_ucs2(pass, pass_len, out_pass_raw, out_pass_raw_len)) {
  312. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_DECODE_ERROR);
  313. return 0;
  314. }
  315. } else {
  316. *out_pass_raw = BUF_memdup(pass, pass_len);
  317. if (*out_pass_raw == NULL) {
  318. OPENSSL_PUT_ERROR(PKCS8, ERR_R_MALLOC_FAILURE);
  319. return 0;
  320. }
  321. *out_pass_raw_len = (size_t)pass_len;
  322. }
  323. return 1;
  324. }
  325. static int pbe_cipher_init(ASN1_OBJECT *pbe_obj,
  326. const uint8_t *pass_raw, size_t pass_raw_len,
  327. ASN1_TYPE *param,
  328. EVP_CIPHER_CTX *ctx, int is_encrypt) {
  329. const EVP_CIPHER *cipher;
  330. const EVP_MD *md;
  331. const struct pbe_suite *suite = get_pbe_suite(OBJ_obj2nid(pbe_obj));
  332. if (suite == NULL) {
  333. char obj_str[80];
  334. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_UNKNOWN_ALGORITHM);
  335. if (!pbe_obj) {
  336. strncpy(obj_str, "NULL", sizeof(obj_str));
  337. } else {
  338. i2t_ASN1_OBJECT(obj_str, sizeof(obj_str), pbe_obj);
  339. }
  340. ERR_add_error_data(2, "TYPE=", obj_str);
  341. return 0;
  342. }
  343. if (suite->cipher_func == NULL) {
  344. cipher = NULL;
  345. } else {
  346. cipher = suite->cipher_func();
  347. if (!cipher) {
  348. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_UNKNOWN_CIPHER);
  349. return 0;
  350. }
  351. }
  352. if (suite->md_func == NULL) {
  353. md = NULL;
  354. } else {
  355. md = suite->md_func();
  356. if (!md) {
  357. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_UNKNOWN_DIGEST);
  358. return 0;
  359. }
  360. }
  361. if (!suite->keygen(ctx, pass_raw, pass_raw_len, param, cipher, md,
  362. is_encrypt)) {
  363. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_KEYGEN_FAILURE);
  364. return 0;
  365. }
  366. return 1;
  367. }
  368. static int pbe_crypt(const X509_ALGOR *algor,
  369. const uint8_t *pass_raw, size_t pass_raw_len,
  370. const uint8_t *in, size_t in_len,
  371. uint8_t **out, size_t *out_len,
  372. int is_encrypt) {
  373. uint8_t *buf;
  374. int n, ret = 0;
  375. EVP_CIPHER_CTX ctx;
  376. unsigned block_size;
  377. EVP_CIPHER_CTX_init(&ctx);
  378. if (!pbe_cipher_init(algor->algorithm, pass_raw, pass_raw_len,
  379. algor->parameter, &ctx, is_encrypt)) {
  380. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_UNKNOWN_CIPHER_ALGORITHM);
  381. return 0;
  382. }
  383. block_size = EVP_CIPHER_CTX_block_size(&ctx);
  384. if (in_len + block_size < in_len) {
  385. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_TOO_LONG);
  386. goto err;
  387. }
  388. buf = OPENSSL_malloc(in_len + block_size);
  389. if (buf == NULL) {
  390. OPENSSL_PUT_ERROR(PKCS8, ERR_R_MALLOC_FAILURE);
  391. goto err;
  392. }
  393. if (!EVP_CipherUpdate(&ctx, buf, &n, in, in_len)) {
  394. OPENSSL_free(buf);
  395. OPENSSL_PUT_ERROR(PKCS8, ERR_R_EVP_LIB);
  396. goto err;
  397. }
  398. *out_len = n;
  399. if (!EVP_CipherFinal_ex(&ctx, buf + n, &n)) {
  400. OPENSSL_free(buf);
  401. OPENSSL_PUT_ERROR(PKCS8, ERR_R_EVP_LIB);
  402. goto err;
  403. }
  404. *out_len += n;
  405. *out = buf;
  406. ret = 1;
  407. err:
  408. EVP_CIPHER_CTX_cleanup(&ctx);
  409. return ret;
  410. }
  411. static void *pkcs12_item_decrypt_d2i(X509_ALGOR *algor, const ASN1_ITEM *it,
  412. const uint8_t *pass_raw,
  413. size_t pass_raw_len,
  414. ASN1_OCTET_STRING *oct) {
  415. uint8_t *out;
  416. const uint8_t *p;
  417. void *ret;
  418. size_t out_len;
  419. if (!pbe_crypt(algor, pass_raw, pass_raw_len, oct->data, oct->length,
  420. &out, &out_len, 0 /* decrypt */)) {
  421. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_CRYPT_ERROR);
  422. return NULL;
  423. }
  424. p = out;
  425. ret = ASN1_item_d2i(NULL, &p, out_len, it);
  426. OPENSSL_cleanse(out, out_len);
  427. if (!ret) {
  428. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_DECODE_ERROR);
  429. }
  430. OPENSSL_free(out);
  431. return ret;
  432. }
  433. PKCS8_PRIV_KEY_INFO *PKCS8_decrypt(X509_SIG *pkcs8, const char *pass,
  434. int pass_len) {
  435. uint8_t *pass_raw = NULL;
  436. size_t pass_raw_len = 0;
  437. if (!pass_to_pass_raw(OBJ_obj2nid(pkcs8->algor->algorithm), pass, pass_len,
  438. &pass_raw, &pass_raw_len)) {
  439. return NULL;
  440. }
  441. PKCS8_PRIV_KEY_INFO *ret = PKCS8_decrypt_pbe(pkcs8, pass_raw, pass_raw_len);
  442. if (pass_raw) {
  443. OPENSSL_cleanse(pass_raw, pass_raw_len);
  444. OPENSSL_free(pass_raw);
  445. }
  446. return ret;
  447. }
  448. PKCS8_PRIV_KEY_INFO *PKCS8_decrypt_pbe(X509_SIG *pkcs8, const uint8_t *pass_raw,
  449. size_t pass_raw_len) {
  450. return pkcs12_item_decrypt_d2i(pkcs8->algor,
  451. ASN1_ITEM_rptr(PKCS8_PRIV_KEY_INFO), pass_raw,
  452. pass_raw_len, pkcs8->digest);
  453. }
  454. static ASN1_OCTET_STRING *pkcs12_item_i2d_encrypt(X509_ALGOR *algor,
  455. const ASN1_ITEM *it,
  456. const uint8_t *pass_raw,
  457. size_t pass_raw_len, void *obj) {
  458. ASN1_OCTET_STRING *oct;
  459. uint8_t *in = NULL;
  460. int in_len;
  461. size_t crypt_len;
  462. oct = M_ASN1_OCTET_STRING_new();
  463. if (oct == NULL) {
  464. OPENSSL_PUT_ERROR(PKCS8, ERR_R_MALLOC_FAILURE);
  465. return NULL;
  466. }
  467. in_len = ASN1_item_i2d(obj, &in, it);
  468. if (!in) {
  469. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_ENCODE_ERROR);
  470. return NULL;
  471. }
  472. if (!pbe_crypt(algor, pass_raw, pass_raw_len, in, in_len, &oct->data, &crypt_len,
  473. 1 /* encrypt */)) {
  474. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_ENCRYPT_ERROR);
  475. OPENSSL_free(in);
  476. return NULL;
  477. }
  478. oct->length = crypt_len;
  479. OPENSSL_cleanse(in, in_len);
  480. OPENSSL_free(in);
  481. return oct;
  482. }
  483. X509_SIG *PKCS8_encrypt(int pbe_nid, const EVP_CIPHER *cipher, const char *pass,
  484. int pass_len, uint8_t *salt, size_t salt_len,
  485. int iterations, PKCS8_PRIV_KEY_INFO *p8inf) {
  486. uint8_t *pass_raw = NULL;
  487. size_t pass_raw_len = 0;
  488. if (!pass_to_pass_raw(pbe_nid, pass, pass_len, &pass_raw, &pass_raw_len)) {
  489. return NULL;
  490. }
  491. X509_SIG *ret = PKCS8_encrypt_pbe(pbe_nid, cipher, pass_raw, pass_raw_len,
  492. salt, salt_len, iterations, p8inf);
  493. if (pass_raw) {
  494. OPENSSL_cleanse(pass_raw, pass_raw_len);
  495. OPENSSL_free(pass_raw);
  496. }
  497. return ret;
  498. }
  499. X509_SIG *PKCS8_encrypt_pbe(int pbe_nid, const EVP_CIPHER *cipher,
  500. const uint8_t *pass_raw, size_t pass_raw_len,
  501. uint8_t *salt, size_t salt_len,
  502. int iterations, PKCS8_PRIV_KEY_INFO *p8inf) {
  503. X509_SIG *pkcs8 = NULL;
  504. X509_ALGOR *pbe;
  505. pkcs8 = X509_SIG_new();
  506. if (pkcs8 == NULL) {
  507. OPENSSL_PUT_ERROR(PKCS8, ERR_R_MALLOC_FAILURE);
  508. goto err;
  509. }
  510. if (pbe_nid == -1) {
  511. pbe = PKCS5_pbe2_set(cipher, iterations, salt, salt_len);
  512. } else {
  513. pbe = PKCS5_pbe_set(pbe_nid, iterations, salt, salt_len);
  514. }
  515. if (!pbe) {
  516. OPENSSL_PUT_ERROR(PKCS8, ERR_R_ASN1_LIB);
  517. goto err;
  518. }
  519. X509_ALGOR_free(pkcs8->algor);
  520. pkcs8->algor = pbe;
  521. M_ASN1_OCTET_STRING_free(pkcs8->digest);
  522. pkcs8->digest = pkcs12_item_i2d_encrypt(
  523. pbe, ASN1_ITEM_rptr(PKCS8_PRIV_KEY_INFO), pass_raw, pass_raw_len, p8inf);
  524. if (!pkcs8->digest) {
  525. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_ENCRYPT_ERROR);
  526. goto err;
  527. }
  528. return pkcs8;
  529. err:
  530. X509_SIG_free(pkcs8);
  531. return NULL;
  532. }
  533. EVP_PKEY *EVP_PKCS82PKEY(PKCS8_PRIV_KEY_INFO *p8) {
  534. uint8_t *der = NULL;
  535. int der_len = i2d_PKCS8_PRIV_KEY_INFO(p8, &der);
  536. if (der_len < 0) {
  537. return NULL;
  538. }
  539. CBS cbs;
  540. CBS_init(&cbs, der, (size_t)der_len);
  541. EVP_PKEY *ret = EVP_parse_private_key(&cbs);
  542. if (ret == NULL || CBS_len(&cbs) != 0) {
  543. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_DECODE_ERROR);
  544. EVP_PKEY_free(ret);
  545. OPENSSL_free(der);
  546. return NULL;
  547. }
  548. OPENSSL_free(der);
  549. return ret;
  550. }
  551. PKCS8_PRIV_KEY_INFO *EVP_PKEY2PKCS8(EVP_PKEY *pkey) {
  552. CBB cbb;
  553. uint8_t *der = NULL;
  554. size_t der_len;
  555. if (!CBB_init(&cbb, 0) ||
  556. !EVP_marshal_private_key(&cbb, pkey) ||
  557. !CBB_finish(&cbb, &der, &der_len) ||
  558. der_len > LONG_MAX) {
  559. CBB_cleanup(&cbb);
  560. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_ENCODE_ERROR);
  561. goto err;
  562. }
  563. const uint8_t *p = der;
  564. PKCS8_PRIV_KEY_INFO *p8 = d2i_PKCS8_PRIV_KEY_INFO(NULL, &p, (long)der_len);
  565. if (p8 == NULL || p != der + der_len) {
  566. PKCS8_PRIV_KEY_INFO_free(p8);
  567. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_DECODE_ERROR);
  568. goto err;
  569. }
  570. OPENSSL_free(der);
  571. return p8;
  572. err:
  573. OPENSSL_free(der);
  574. return NULL;
  575. }
  576. struct pkcs12_context {
  577. EVP_PKEY **out_key;
  578. STACK_OF(X509) *out_certs;
  579. uint8_t *password;
  580. size_t password_len;
  581. };
  582. static int PKCS12_handle_content_info(CBS *content_info, unsigned depth,
  583. struct pkcs12_context *ctx);
  584. /* PKCS12_handle_content_infos parses a series of PKCS#7 ContentInfos in a
  585. * SEQUENCE. */
  586. static int PKCS12_handle_content_infos(CBS *content_infos,
  587. unsigned depth,
  588. struct pkcs12_context *ctx) {
  589. uint8_t *der_bytes = NULL;
  590. size_t der_len;
  591. CBS in;
  592. int ret = 0;
  593. /* Generally we only expect depths 0 (the top level, with a
  594. * pkcs7-encryptedData and a pkcs7-data) and depth 1 (the various PKCS#12
  595. * bags). */
  596. if (depth > 3) {
  597. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_PKCS12_TOO_DEEPLY_NESTED);
  598. return 0;
  599. }
  600. /* Although a BER->DER conversion is done at the beginning of |PKCS12_parse|,
  601. * the ASN.1 data gets wrapped in OCTETSTRINGs and/or encrypted and the
  602. * conversion cannot see through those wrappings. So each time we step
  603. * through one we need to convert to DER again. */
  604. if (!CBS_asn1_ber_to_der(content_infos, &der_bytes, &der_len)) {
  605. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_BAD_PKCS12_DATA);
  606. return 0;
  607. }
  608. if (der_bytes != NULL) {
  609. CBS_init(&in, der_bytes, der_len);
  610. } else {
  611. CBS_init(&in, CBS_data(content_infos), CBS_len(content_infos));
  612. }
  613. if (!CBS_get_asn1(&in, &in, CBS_ASN1_SEQUENCE)) {
  614. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_BAD_PKCS12_DATA);
  615. goto err;
  616. }
  617. while (CBS_len(&in) > 0) {
  618. CBS content_info;
  619. if (!CBS_get_asn1(&in, &content_info, CBS_ASN1_SEQUENCE)) {
  620. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_BAD_PKCS12_DATA);
  621. goto err;
  622. }
  623. if (!PKCS12_handle_content_info(&content_info, depth + 1, ctx)) {
  624. goto err;
  625. }
  626. }
  627. /* NSS includes additional data after the SEQUENCE, but it's an (unwrapped)
  628. * copy of the same encrypted private key (with the same IV and
  629. * ciphertext)! */
  630. ret = 1;
  631. err:
  632. OPENSSL_free(der_bytes);
  633. return ret;
  634. }
  635. /* PKCS12_handle_content_info parses a single PKCS#7 ContentInfo element in a
  636. * PKCS#12 structure. */
  637. static int PKCS12_handle_content_info(CBS *content_info, unsigned depth,
  638. struct pkcs12_context *ctx) {
  639. CBS content_type, wrapped_contents, contents, content_infos;
  640. int nid, ret = 0;
  641. uint8_t *storage = NULL;
  642. if (!CBS_get_asn1(content_info, &content_type, CBS_ASN1_OBJECT) ||
  643. !CBS_get_asn1(content_info, &wrapped_contents,
  644. CBS_ASN1_CONTEXT_SPECIFIC | CBS_ASN1_CONSTRUCTED | 0)) {
  645. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_BAD_PKCS12_DATA);
  646. goto err;
  647. }
  648. nid = OBJ_cbs2nid(&content_type);
  649. if (nid == NID_pkcs7_encrypted) {
  650. /* See https://tools.ietf.org/html/rfc2315#section-13.
  651. *
  652. * PKCS#7 encrypted data inside a PKCS#12 structure is generally an
  653. * encrypted certificate bag and it's generally encrypted with 40-bit
  654. * RC2-CBC. */
  655. CBS version_bytes, eci, contents_type, ai, encrypted_contents;
  656. X509_ALGOR *algor = NULL;
  657. const uint8_t *inp;
  658. uint8_t *out;
  659. size_t out_len;
  660. if (!CBS_get_asn1(&wrapped_contents, &contents, CBS_ASN1_SEQUENCE) ||
  661. !CBS_get_asn1(&contents, &version_bytes, CBS_ASN1_INTEGER) ||
  662. /* EncryptedContentInfo, see
  663. * https://tools.ietf.org/html/rfc2315#section-10.1 */
  664. !CBS_get_asn1(&contents, &eci, CBS_ASN1_SEQUENCE) ||
  665. !CBS_get_asn1(&eci, &contents_type, CBS_ASN1_OBJECT) ||
  666. /* AlgorithmIdentifier, see
  667. * https://tools.ietf.org/html/rfc5280#section-4.1.1.2 */
  668. !CBS_get_asn1_element(&eci, &ai, CBS_ASN1_SEQUENCE) ||
  669. !CBS_get_asn1_implicit_string(
  670. &eci, &encrypted_contents, &storage,
  671. CBS_ASN1_CONTEXT_SPECIFIC | 0, CBS_ASN1_OCTETSTRING)) {
  672. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_BAD_PKCS12_DATA);
  673. goto err;
  674. }
  675. if (OBJ_cbs2nid(&contents_type) != NID_pkcs7_data ||
  676. CBS_len(&ai) > LONG_MAX) {
  677. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_BAD_PKCS12_DATA);
  678. goto err;
  679. }
  680. inp = CBS_data(&ai);
  681. algor = d2i_X509_ALGOR(NULL, &inp, (long)CBS_len(&ai));
  682. if (algor == NULL) {
  683. goto err;
  684. }
  685. if (inp != CBS_data(&ai) + CBS_len(&ai)) {
  686. X509_ALGOR_free(algor);
  687. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_BAD_PKCS12_DATA);
  688. goto err;
  689. }
  690. if (!pbe_crypt(algor, ctx->password, ctx->password_len,
  691. CBS_data(&encrypted_contents), CBS_len(&encrypted_contents),
  692. &out, &out_len, 0 /* decrypt */)) {
  693. X509_ALGOR_free(algor);
  694. goto err;
  695. }
  696. X509_ALGOR_free(algor);
  697. CBS_init(&content_infos, out, out_len);
  698. ret = PKCS12_handle_content_infos(&content_infos, depth + 1, ctx);
  699. OPENSSL_free(out);
  700. } else if (nid == NID_pkcs7_data) {
  701. CBS octet_string_contents;
  702. if (!CBS_get_asn1(&wrapped_contents, &octet_string_contents,
  703. CBS_ASN1_OCTETSTRING)) {
  704. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_BAD_PKCS12_DATA);
  705. goto err;
  706. }
  707. ret = PKCS12_handle_content_infos(&octet_string_contents, depth + 1, ctx);
  708. } else if (nid == NID_pkcs8ShroudedKeyBag) {
  709. /* See ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-12/pkcs-12v1.pdf, section
  710. * 4.2.2. */
  711. const uint8_t *inp = CBS_data(&wrapped_contents);
  712. PKCS8_PRIV_KEY_INFO *pki = NULL;
  713. X509_SIG *encrypted = NULL;
  714. if (*ctx->out_key) {
  715. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_MULTIPLE_PRIVATE_KEYS_IN_PKCS12);
  716. goto err;
  717. }
  718. if (CBS_len(&wrapped_contents) > LONG_MAX) {
  719. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_BAD_PKCS12_DATA);
  720. goto err;
  721. }
  722. /* encrypted isn't actually an X.509 signature, but it has the same
  723. * structure as one and so |X509_SIG| is reused to store it. */
  724. encrypted = d2i_X509_SIG(NULL, &inp, (long)CBS_len(&wrapped_contents));
  725. if (encrypted == NULL) {
  726. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_BAD_PKCS12_DATA);
  727. goto err;
  728. }
  729. if (inp != CBS_data(&wrapped_contents) + CBS_len(&wrapped_contents)) {
  730. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_BAD_PKCS12_DATA);
  731. X509_SIG_free(encrypted);
  732. goto err;
  733. }
  734. pki = PKCS8_decrypt_pbe(encrypted, ctx->password, ctx->password_len);
  735. X509_SIG_free(encrypted);
  736. if (pki == NULL) {
  737. goto err;
  738. }
  739. *ctx->out_key = EVP_PKCS82PKEY(pki);
  740. PKCS8_PRIV_KEY_INFO_free(pki);
  741. if (ctx->out_key == NULL) {
  742. goto err;
  743. }
  744. ret = 1;
  745. } else if (nid == NID_certBag) {
  746. CBS cert_bag, cert_type, wrapped_cert, cert;
  747. if (!CBS_get_asn1(&wrapped_contents, &cert_bag, CBS_ASN1_SEQUENCE) ||
  748. !CBS_get_asn1(&cert_bag, &cert_type, CBS_ASN1_OBJECT) ||
  749. !CBS_get_asn1(&cert_bag, &wrapped_cert,
  750. CBS_ASN1_CONTEXT_SPECIFIC | CBS_ASN1_CONSTRUCTED | 0) ||
  751. !CBS_get_asn1(&wrapped_cert, &cert, CBS_ASN1_OCTETSTRING)) {
  752. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_BAD_PKCS12_DATA);
  753. goto err;
  754. }
  755. if (OBJ_cbs2nid(&cert_type) == NID_x509Certificate) {
  756. if (CBS_len(&cert) > LONG_MAX) {
  757. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_BAD_PKCS12_DATA);
  758. goto err;
  759. }
  760. const uint8_t *inp = CBS_data(&cert);
  761. X509 *x509 = d2i_X509(NULL, &inp, (long)CBS_len(&cert));
  762. if (!x509) {
  763. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_BAD_PKCS12_DATA);
  764. goto err;
  765. }
  766. if (inp != CBS_data(&cert) + CBS_len(&cert)) {
  767. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_BAD_PKCS12_DATA);
  768. X509_free(x509);
  769. goto err;
  770. }
  771. if (0 == sk_X509_push(ctx->out_certs, x509)) {
  772. X509_free(x509);
  773. goto err;
  774. }
  775. }
  776. ret = 1;
  777. } else {
  778. /* Unknown element type - ignore it. */
  779. ret = 1;
  780. }
  781. err:
  782. OPENSSL_free(storage);
  783. return ret;
  784. }
  785. int PKCS12_get_key_and_certs(EVP_PKEY **out_key, STACK_OF(X509) *out_certs,
  786. CBS *ber_in, const char *password) {
  787. uint8_t *der_bytes = NULL;
  788. size_t der_len;
  789. CBS in, pfx, mac_data, authsafe, content_type, wrapped_authsafes, authsafes;
  790. uint64_t version;
  791. int ret = 0;
  792. struct pkcs12_context ctx;
  793. const size_t original_out_certs_len = sk_X509_num(out_certs);
  794. /* The input may be in BER format. */
  795. if (!CBS_asn1_ber_to_der(ber_in, &der_bytes, &der_len)) {
  796. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_BAD_PKCS12_DATA);
  797. return 0;
  798. }
  799. if (der_bytes != NULL) {
  800. CBS_init(&in, der_bytes, der_len);
  801. } else {
  802. CBS_init(&in, CBS_data(ber_in), CBS_len(ber_in));
  803. }
  804. *out_key = NULL;
  805. memset(&ctx, 0, sizeof(ctx));
  806. /* See ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-12/pkcs-12v1.pdf, section
  807. * four. */
  808. if (!CBS_get_asn1(&in, &pfx, CBS_ASN1_SEQUENCE) ||
  809. CBS_len(&in) != 0 ||
  810. !CBS_get_asn1_uint64(&pfx, &version)) {
  811. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_BAD_PKCS12_DATA);
  812. goto err;
  813. }
  814. if (version < 3) {
  815. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_BAD_PKCS12_VERSION);
  816. goto err;
  817. }
  818. if (!CBS_get_asn1(&pfx, &authsafe, CBS_ASN1_SEQUENCE)) {
  819. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_BAD_PKCS12_DATA);
  820. goto err;
  821. }
  822. if (CBS_len(&pfx) == 0) {
  823. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_MISSING_MAC);
  824. goto err;
  825. }
  826. if (!CBS_get_asn1(&pfx, &mac_data, CBS_ASN1_SEQUENCE)) {
  827. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_BAD_PKCS12_DATA);
  828. goto err;
  829. }
  830. /* authsafe is a PKCS#7 ContentInfo. See
  831. * https://tools.ietf.org/html/rfc2315#section-7. */
  832. if (!CBS_get_asn1(&authsafe, &content_type, CBS_ASN1_OBJECT) ||
  833. !CBS_get_asn1(&authsafe, &wrapped_authsafes,
  834. CBS_ASN1_CONTEXT_SPECIFIC | CBS_ASN1_CONSTRUCTED | 0)) {
  835. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_BAD_PKCS12_DATA);
  836. goto err;
  837. }
  838. /* The content type can either be |NID_pkcs7_data| or |NID_pkcs7_signed|. The
  839. * latter indicates that it's signed by a public key, which isn't
  840. * supported. */
  841. if (OBJ_cbs2nid(&content_type) != NID_pkcs7_data) {
  842. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_PKCS12_PUBLIC_KEY_INTEGRITY_NOT_SUPPORTED);
  843. goto err;
  844. }
  845. if (!CBS_get_asn1(&wrapped_authsafes, &authsafes, CBS_ASN1_OCTETSTRING)) {
  846. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_BAD_PKCS12_DATA);
  847. goto err;
  848. }
  849. ctx.out_key = out_key;
  850. ctx.out_certs = out_certs;
  851. if (!ascii_to_ucs2(password, password ? strlen(password) : 0, &ctx.password,
  852. &ctx.password_len)) {
  853. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_DECODE_ERROR);
  854. goto err;
  855. }
  856. /* Verify the MAC. */
  857. {
  858. CBS mac, hash_type_seq, hash_oid, salt, expected_mac;
  859. uint64_t iterations;
  860. int hash_nid;
  861. const EVP_MD *md;
  862. uint8_t hmac_key[EVP_MAX_MD_SIZE];
  863. uint8_t hmac[EVP_MAX_MD_SIZE];
  864. unsigned hmac_len;
  865. if (!CBS_get_asn1(&mac_data, &mac, CBS_ASN1_SEQUENCE) ||
  866. !CBS_get_asn1(&mac, &hash_type_seq, CBS_ASN1_SEQUENCE) ||
  867. !CBS_get_asn1(&hash_type_seq, &hash_oid, CBS_ASN1_OBJECT) ||
  868. !CBS_get_asn1(&mac, &expected_mac, CBS_ASN1_OCTETSTRING) ||
  869. !CBS_get_asn1(&mac_data, &salt, CBS_ASN1_OCTETSTRING)) {
  870. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_BAD_PKCS12_DATA);
  871. goto err;
  872. }
  873. /* The iteration count is optional and the default is one. */
  874. iterations = 1;
  875. if (CBS_len(&mac_data) > 0) {
  876. if (!CBS_get_asn1_uint64(&mac_data, &iterations) ||
  877. iterations > INT_MAX) {
  878. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_BAD_PKCS12_DATA);
  879. goto err;
  880. }
  881. }
  882. hash_nid = OBJ_cbs2nid(&hash_oid);
  883. if (hash_nid == NID_undef ||
  884. (md = EVP_get_digestbynid(hash_nid)) == NULL) {
  885. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_UNKNOWN_HASH);
  886. goto err;
  887. }
  888. if (!pkcs12_key_gen_raw(ctx.password, ctx.password_len, CBS_data(&salt),
  889. CBS_len(&salt), PKCS12_MAC_ID, iterations,
  890. EVP_MD_size(md), hmac_key, md)) {
  891. goto err;
  892. }
  893. if (NULL == HMAC(md, hmac_key, EVP_MD_size(md), CBS_data(&authsafes),
  894. CBS_len(&authsafes), hmac, &hmac_len)) {
  895. goto err;
  896. }
  897. if (!CBS_mem_equal(&expected_mac, hmac, hmac_len)) {
  898. OPENSSL_PUT_ERROR(PKCS8, PKCS8_R_INCORRECT_PASSWORD);
  899. goto err;
  900. }
  901. }
  902. /* authsafes contains a series of PKCS#7 ContentInfos. */
  903. if (!PKCS12_handle_content_infos(&authsafes, 0, &ctx)) {
  904. goto err;
  905. }
  906. ret = 1;
  907. err:
  908. OPENSSL_free(ctx.password);
  909. OPENSSL_free(der_bytes);
  910. if (!ret) {
  911. EVP_PKEY_free(*out_key);
  912. *out_key = NULL;
  913. while (sk_X509_num(out_certs) > original_out_certs_len) {
  914. X509 *x509 = sk_X509_pop(out_certs);
  915. X509_free(x509);
  916. }
  917. }
  918. return ret;
  919. }
  920. void PKCS12_PBE_add(void) {}
  921. struct pkcs12_st {
  922. uint8_t *ber_bytes;
  923. size_t ber_len;
  924. };
  925. PKCS12* d2i_PKCS12(PKCS12 **out_p12, const uint8_t **ber_bytes, size_t ber_len) {
  926. PKCS12 *p12;
  927. p12 = OPENSSL_malloc(sizeof(PKCS12));
  928. if (!p12) {
  929. return NULL;
  930. }
  931. p12->ber_bytes = OPENSSL_malloc(ber_len);
  932. if (!p12->ber_bytes) {
  933. OPENSSL_free(p12);
  934. return NULL;
  935. }
  936. memcpy(p12->ber_bytes, *ber_bytes, ber_len);
  937. p12->ber_len = ber_len;
  938. *ber_bytes += ber_len;
  939. if (out_p12) {
  940. PKCS12_free(*out_p12);
  941. *out_p12 = p12;
  942. }
  943. return p12;
  944. }
  945. PKCS12* d2i_PKCS12_bio(BIO *bio, PKCS12 **out_p12) {
  946. size_t used = 0;
  947. BUF_MEM *buf;
  948. const uint8_t *dummy;
  949. static const size_t kMaxSize = 256 * 1024;
  950. PKCS12 *ret = NULL;
  951. buf = BUF_MEM_new();
  952. if (buf == NULL) {
  953. return NULL;
  954. }
  955. if (BUF_MEM_grow(buf, 8192) == 0) {
  956. goto out;
  957. }
  958. for (;;) {
  959. int n = BIO_read(bio, &buf->data[used], buf->length - used);
  960. if (n < 0) {
  961. if (used == 0) {
  962. goto out;
  963. }
  964. /* Workaround a bug in node.js. It uses a memory BIO for this in the wrong
  965. * mode. */
  966. n = 0;
  967. }
  968. if (n == 0) {
  969. break;
  970. }
  971. used += n;
  972. if (used < buf->length) {
  973. continue;
  974. }
  975. if (buf->length > kMaxSize ||
  976. BUF_MEM_grow(buf, buf->length * 2) == 0) {
  977. goto out;
  978. }
  979. }
  980. dummy = (uint8_t*) buf->data;
  981. ret = d2i_PKCS12(out_p12, &dummy, used);
  982. out:
  983. BUF_MEM_free(buf);
  984. return ret;
  985. }
  986. PKCS12* d2i_PKCS12_fp(FILE *fp, PKCS12 **out_p12) {
  987. BIO *bio;
  988. PKCS12 *ret;
  989. bio = BIO_new_fp(fp, 0 /* don't take ownership */);
  990. if (!bio) {
  991. return NULL;
  992. }
  993. ret = d2i_PKCS12_bio(bio, out_p12);
  994. BIO_free(bio);
  995. return ret;
  996. }
  997. int PKCS12_parse(const PKCS12 *p12, const char *password, EVP_PKEY **out_pkey,
  998. X509 **out_cert, STACK_OF(X509) **out_ca_certs) {
  999. CBS ber_bytes;
  1000. STACK_OF(X509) *ca_certs = NULL;
  1001. char ca_certs_alloced = 0;
  1002. if (out_ca_certs != NULL && *out_ca_certs != NULL) {
  1003. ca_certs = *out_ca_certs;
  1004. }
  1005. if (!ca_certs) {
  1006. ca_certs = sk_X509_new_null();
  1007. if (ca_certs == NULL) {
  1008. OPENSSL_PUT_ERROR(PKCS8, ERR_R_MALLOC_FAILURE);
  1009. return 0;
  1010. }
  1011. ca_certs_alloced = 1;
  1012. }
  1013. CBS_init(&ber_bytes, p12->ber_bytes, p12->ber_len);
  1014. if (!PKCS12_get_key_and_certs(out_pkey, ca_certs, &ber_bytes, password)) {
  1015. if (ca_certs_alloced) {
  1016. sk_X509_free(ca_certs);
  1017. }
  1018. return 0;
  1019. }
  1020. *out_cert = NULL;
  1021. if (sk_X509_num(ca_certs) > 0) {
  1022. *out_cert = sk_X509_shift(ca_certs);
  1023. }
  1024. if (out_ca_certs) {
  1025. *out_ca_certs = ca_certs;
  1026. } else {
  1027. sk_X509_pop_free(ca_certs, X509_free);
  1028. }
  1029. return 1;
  1030. }
  1031. int PKCS12_verify_mac(const PKCS12 *p12, const char *password,
  1032. int password_len) {
  1033. if (password == NULL) {
  1034. if (password_len != 0) {
  1035. return 0;
  1036. }
  1037. } else if (password_len != -1 &&
  1038. (password[password_len] != 0 ||
  1039. memchr(password, 0, password_len) != NULL)) {
  1040. return 0;
  1041. }
  1042. EVP_PKEY *pkey = NULL;
  1043. X509 *cert = NULL;
  1044. if (!PKCS12_parse(p12, password, &pkey, &cert, NULL)) {
  1045. ERR_clear_error();
  1046. return 0;
  1047. }
  1048. EVP_PKEY_free(pkey);
  1049. X509_free(cert);
  1050. return 1;
  1051. }
  1052. void PKCS12_free(PKCS12 *p12) {
  1053. if (p12 == NULL) {
  1054. return;
  1055. }
  1056. OPENSSL_free(p12->ber_bytes);
  1057. OPENSSL_free(p12);
  1058. }