Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 
 

581 Zeilen
17 KiB

  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 <openssl/asn1.h>
  57. #include <openssl/bn.h>
  58. #include <openssl/cipher.h>
  59. #include <openssl/digest.h>
  60. #include <openssl/err.h>
  61. #include <openssl/mem.h>
  62. #include <openssl/x509.h>
  63. #include "../evp/internal.h"
  64. #define PKCS12_KEY_ID 1
  65. #define PKCS12_IV_ID 2
  66. static int ascii_to_ucs2(const char *ascii, size_t ascii_len,
  67. uint8_t **out, size_t *out_len) {
  68. uint8_t *unitmp;
  69. size_t ulen, i;
  70. ulen = ascii_len * 2 + 2;
  71. if (ulen < ascii_len) {
  72. return 0;
  73. }
  74. unitmp = OPENSSL_malloc(ulen);
  75. if (unitmp == NULL) {
  76. return 0;
  77. }
  78. for (i = 0; i < ulen - 2; i += 2) {
  79. unitmp[i] = 0;
  80. unitmp[i + 1] = ascii[i >> 1];
  81. }
  82. /* Make result double null terminated */
  83. unitmp[ulen - 2] = 0;
  84. unitmp[ulen - 1] = 0;
  85. *out_len = ulen;
  86. *out = unitmp;
  87. return 1;
  88. }
  89. static int pkcs12_key_gen_uni(uint8_t *pass, size_t pass_len, uint8_t *salt,
  90. size_t salt_len, int id, int iterations,
  91. size_t out_len, uint8_t *out,
  92. const EVP_MD *md_type) {
  93. uint8_t *B, *D, *I, *p, *Ai;
  94. int Slen, Plen, Ilen, Ijlen;
  95. int i, j, v;
  96. size_t u;
  97. int ret = 0;
  98. BIGNUM *Ij, *Bpl1; /* These hold Ij and B + 1 */
  99. EVP_MD_CTX ctx;
  100. EVP_MD_CTX_init(&ctx);
  101. v = EVP_MD_block_size(md_type);
  102. u = EVP_MD_size(md_type);
  103. D = OPENSSL_malloc(v);
  104. Ai = OPENSSL_malloc(u);
  105. B = OPENSSL_malloc(v + 1);
  106. Slen = v * ((salt_len + v - 1) / v);
  107. if (pass_len)
  108. Plen = v * ((pass_len + v - 1) / v);
  109. else
  110. Plen = 0;
  111. Ilen = Slen + Plen;
  112. I = OPENSSL_malloc(Ilen);
  113. Ij = BN_new();
  114. Bpl1 = BN_new();
  115. if (!D || !Ai || !B || !I || !Ij || !Bpl1)
  116. goto err;
  117. for (i = 0; i < v; i++)
  118. D[i] = id;
  119. p = I;
  120. for (i = 0; i < Slen; i++)
  121. *p++ = salt[i % salt_len];
  122. for (i = 0; i < Plen; i++)
  123. *p++ = pass[i % pass_len];
  124. for (;;) {
  125. if (!EVP_DigestInit_ex(&ctx, md_type, NULL) ||
  126. !EVP_DigestUpdate(&ctx, D, v) ||
  127. !EVP_DigestUpdate(&ctx, I, Ilen) ||
  128. !EVP_DigestFinal_ex(&ctx, Ai, NULL)) {
  129. goto err;
  130. }
  131. for (j = 1; j < iterations; j++) {
  132. if (!EVP_DigestInit_ex(&ctx, md_type, NULL) ||
  133. !EVP_DigestUpdate(&ctx, Ai, u) ||
  134. !EVP_DigestFinal_ex(&ctx, Ai, NULL)) {
  135. goto err;
  136. }
  137. }
  138. memcpy(out, Ai, out_len < u ? out_len : u);
  139. if (u >= out_len) {
  140. ret = 1;
  141. goto end;
  142. }
  143. out_len -= u;
  144. out += u;
  145. for (j = 0; j < v; j++)
  146. B[j] = Ai[j % u];
  147. /* Work out B + 1 first then can use B as tmp space */
  148. if (!BN_bin2bn(B, v, Bpl1))
  149. goto err;
  150. if (!BN_add_word(Bpl1, 1))
  151. goto err;
  152. for (j = 0; j < Ilen; j += v) {
  153. if (!BN_bin2bn(I + j, v, Ij))
  154. goto err;
  155. if (!BN_add(Ij, Ij, Bpl1))
  156. goto err;
  157. if (!BN_bn2bin(Ij, B))
  158. goto err;
  159. Ijlen = BN_num_bytes(Ij);
  160. /* If more than 2^(v*8) - 1 cut off MSB */
  161. if (Ijlen > v) {
  162. if (!BN_bn2bin(Ij, B))
  163. goto err;
  164. memcpy(I + j, B + 1, v);
  165. /* If less than v bytes pad with zeroes */
  166. } else if (Ijlen < v) {
  167. memset(I + j, 0, v - Ijlen);
  168. if (!BN_bn2bin(Ij, I + j + v - Ijlen))
  169. goto err;
  170. } else if (!BN_bn2bin(Ij, I + j)) {
  171. goto err;
  172. }
  173. }
  174. }
  175. err:
  176. OPENSSL_PUT_ERROR(PKCS8, pkcs12_key_gen_uni, ERR_R_MALLOC_FAILURE);
  177. end:
  178. OPENSSL_free(Ai);
  179. OPENSSL_free(B);
  180. OPENSSL_free(D);
  181. OPENSSL_free(I);
  182. BN_free(Ij);
  183. BN_free(Bpl1);
  184. EVP_MD_CTX_cleanup(&ctx);
  185. return ret;
  186. }
  187. static int pkcs12_key_gen_asc(const char *pass, size_t pass_len, uint8_t *salt,
  188. size_t salt_len, int id, int iterations,
  189. int out_len, uint8_t *out,
  190. const EVP_MD *md_type) {
  191. int ret;
  192. uint8_t *ucs2_pass = NULL;
  193. size_t ucs2_pass_len = 0;
  194. if (pass && !ascii_to_ucs2(pass, pass_len, &ucs2_pass, &ucs2_pass_len)) {
  195. OPENSSL_PUT_ERROR(PKCS8, pkcs12_key_gen_asc, PKCS8_R_DECODE_ERROR);
  196. return 0;
  197. }
  198. ret = pkcs12_key_gen_uni(ucs2_pass, ucs2_pass_len, salt, salt_len, id,
  199. iterations, out_len, out, md_type);
  200. if (ucs2_pass) {
  201. OPENSSL_cleanse(ucs2_pass, ucs2_pass_len);
  202. OPENSSL_free(ucs2_pass);
  203. }
  204. return ret;
  205. }
  206. static int pkcs12_pbe_keyivgen(EVP_CIPHER_CTX *ctx, const char *pass,
  207. size_t pass_len, ASN1_TYPE *param,
  208. const EVP_CIPHER *cipher, const EVP_MD *md,
  209. int is_encrypt) {
  210. PBEPARAM *pbe;
  211. int salt_len, iterations, ret;
  212. uint8_t *salt;
  213. const uint8_t *pbuf;
  214. uint8_t key[EVP_MAX_KEY_LENGTH], iv[EVP_MAX_IV_LENGTH];
  215. /* Extract useful info from parameter */
  216. if (param == NULL || param->type != V_ASN1_SEQUENCE ||
  217. param->value.sequence == NULL) {
  218. OPENSSL_PUT_ERROR(PKCS8, pkcs12_pbe_keyivgen, PKCS8_R_DECODE_ERROR);
  219. return 0;
  220. }
  221. pbuf = param->value.sequence->data;
  222. pbe = d2i_PBEPARAM(NULL, &pbuf, param->value.sequence->length);
  223. if (pbe == NULL) {
  224. OPENSSL_PUT_ERROR(PKCS8, pkcs12_pbe_keyivgen, PKCS8_R_DECODE_ERROR);
  225. return 0;
  226. }
  227. if (!pbe->iter) {
  228. iterations = 1;
  229. } else {
  230. iterations = ASN1_INTEGER_get(pbe->iter);
  231. }
  232. salt = pbe->salt->data;
  233. salt_len = pbe->salt->length;
  234. if (!pkcs12_key_gen_asc(pass, pass_len, salt, salt_len, PKCS12_KEY_ID,
  235. iterations, EVP_CIPHER_key_length(cipher), key, md)) {
  236. OPENSSL_PUT_ERROR(PKCS8, pkcs12_pbe_keyivgen, PKCS8_R_KEY_GEN_ERROR);
  237. PBEPARAM_free(pbe);
  238. return 0;
  239. }
  240. if (!pkcs12_key_gen_asc(pass, pass_len, salt, salt_len, PKCS12_IV_ID,
  241. iterations, EVP_CIPHER_iv_length(cipher), iv, md)) {
  242. OPENSSL_PUT_ERROR(PKCS8, pkcs12_pbe_keyivgen, PKCS8_R_KEY_GEN_ERROR);
  243. PBEPARAM_free(pbe);
  244. return 0;
  245. }
  246. PBEPARAM_free(pbe);
  247. ret = EVP_CipherInit_ex(ctx, cipher, NULL, key, iv, is_encrypt);
  248. OPENSSL_cleanse(key, EVP_MAX_KEY_LENGTH);
  249. OPENSSL_cleanse(iv, EVP_MAX_IV_LENGTH);
  250. return ret;
  251. }
  252. typedef int (*keygen_func)(EVP_CIPHER_CTX *ctx, const char *pass,
  253. size_t pass_len, ASN1_TYPE *param,
  254. const EVP_CIPHER *cipher, const EVP_MD *md,
  255. int is_encrypt);
  256. struct pbe_suite {
  257. int pbe_nid;
  258. int cipher_nid;
  259. int md_nid;
  260. keygen_func keygen;
  261. };
  262. static const struct pbe_suite kBuiltinPBE[] = {
  263. {
  264. NID_pbe_WithSHA1And128BitRC4, NID_rc4, NID_sha1, pkcs12_pbe_keyivgen,
  265. },
  266. {
  267. NID_pbe_WithSHA1And3_Key_TripleDES_CBC, NID_des_ede3_cbc, NID_sha1,
  268. pkcs12_pbe_keyivgen,
  269. },
  270. };
  271. static int pbe_cipher_init(ASN1_OBJECT *pbe_obj, const char *pass,
  272. size_t pass_len, ASN1_TYPE *param,
  273. EVP_CIPHER_CTX *ctx, int is_encrypt) {
  274. const EVP_CIPHER *cipher;
  275. const EVP_MD *md;
  276. unsigned i;
  277. const struct pbe_suite *suite = NULL;
  278. const int pbe_nid = OBJ_obj2nid(pbe_obj);
  279. for (i = 0; i < sizeof(kBuiltinPBE) / sizeof(struct pbe_suite); i++) {
  280. suite = &kBuiltinPBE[i];
  281. if (suite->pbe_nid == pbe_nid) {
  282. break;
  283. }
  284. }
  285. if (suite == NULL) {
  286. char obj_str[80];
  287. OPENSSL_PUT_ERROR(PKCS8, pbe_cipher_init, PKCS8_R_UNKNOWN_ALGORITHM);
  288. if (!pbe_obj) {
  289. strncpy(obj_str, "NULL", sizeof(obj_str));
  290. } else {
  291. i2t_ASN1_OBJECT(obj_str, sizeof(obj_str), pbe_obj);
  292. }
  293. ERR_add_error_data(2, "TYPE=", obj_str);
  294. return 0;
  295. }
  296. if (suite->cipher_nid == -1) {
  297. cipher = NULL;
  298. } else {
  299. cipher = EVP_get_cipherbynid(suite->cipher_nid);
  300. if (!cipher) {
  301. OPENSSL_PUT_ERROR(PKCS8, pbe_cipher_init, PKCS8_R_UNKNOWN_CIPHER);
  302. return 0;
  303. }
  304. }
  305. if (suite->md_nid == -1) {
  306. md = NULL;
  307. } else {
  308. md = EVP_get_digestbynid(suite->md_nid);
  309. if (!md) {
  310. OPENSSL_PUT_ERROR(PKCS8, pbe_cipher_init, PKCS8_R_UNKNOWN_DIGEST);
  311. return 0;
  312. }
  313. }
  314. if (!suite->keygen(ctx, pass, pass_len, param, cipher, md, is_encrypt)) {
  315. OPENSSL_PUT_ERROR(PKCS8, pbe_cipher_init, PKCS8_R_KEYGEN_FAILURE);
  316. return 0;
  317. }
  318. return 1;
  319. }
  320. static int pbe_crypt(const X509_ALGOR *algor, const char *pass, size_t pass_len,
  321. uint8_t *in, size_t in_len, uint8_t **out, size_t *out_len,
  322. int is_encrypt) {
  323. uint8_t *buf;
  324. int n, ret = 0;
  325. EVP_CIPHER_CTX ctx;
  326. unsigned block_size;
  327. EVP_CIPHER_CTX_init(&ctx);
  328. if (!pbe_cipher_init(algor->algorithm, pass, pass_len, algor->parameter, &ctx,
  329. is_encrypt)) {
  330. OPENSSL_PUT_ERROR(PKCS8, pbe_crypt, PKCS8_R_UNKNOWN_CIPHER_ALGORITHM);
  331. return 0;
  332. }
  333. block_size = EVP_CIPHER_CTX_block_size(&ctx);
  334. if (in_len + block_size < in_len) {
  335. OPENSSL_PUT_ERROR(PKCS8, pbe_crypt, PKCS8_R_TOO_LONG);
  336. goto err;
  337. }
  338. buf = OPENSSL_malloc(in_len + block_size);
  339. if (buf == NULL) {
  340. OPENSSL_PUT_ERROR(PKCS8, pbe_crypt, ERR_R_MALLOC_FAILURE);
  341. goto err;
  342. }
  343. if (!EVP_CipherUpdate(&ctx, buf, &n, in, in_len)) {
  344. OPENSSL_free(buf);
  345. OPENSSL_PUT_ERROR(PKCS8, pbe_crypt, ERR_R_EVP_LIB);
  346. goto err;
  347. }
  348. *out_len = n;
  349. if (!EVP_CipherFinal_ex(&ctx, buf + n, &n)) {
  350. OPENSSL_free(buf);
  351. OPENSSL_PUT_ERROR(PKCS8, pbe_crypt, ERR_R_EVP_LIB);
  352. goto err;
  353. }
  354. *out_len += n;
  355. *out = buf;
  356. ret = 1;
  357. err:
  358. EVP_CIPHER_CTX_cleanup(&ctx);
  359. return ret;
  360. }
  361. static void *pkcs12_item_decrypt_d2i(X509_ALGOR *algor, const ASN1_ITEM *it,
  362. const char *pass, size_t pass_len,
  363. ASN1_OCTET_STRING *oct) {
  364. uint8_t *out;
  365. const uint8_t *p;
  366. void *ret;
  367. size_t out_len;
  368. if (!pbe_crypt(algor, pass, pass_len, oct->data, oct->length, &out, &out_len,
  369. 0 /* decrypt */)) {
  370. OPENSSL_PUT_ERROR(PKCS8, pkcs12_item_decrypt_d2i, PKCS8_R_CRYPT_ERROR);
  371. return NULL;
  372. }
  373. p = out;
  374. ret = ASN1_item_d2i(NULL, &p, out_len, it);
  375. OPENSSL_cleanse(out, out_len);
  376. if (!ret) {
  377. OPENSSL_PUT_ERROR(PKCS8, pkcs12_item_decrypt_d2i, PKCS8_R_DECODE_ERROR);
  378. }
  379. OPENSSL_free(out);
  380. return ret;
  381. }
  382. PKCS8_PRIV_KEY_INFO *PKCS8_decrypt(X509_SIG *pkcs8, const char *pass,
  383. int pass_len) {
  384. if (pass && pass_len == -1) {
  385. pass_len = strlen(pass);
  386. }
  387. return pkcs12_item_decrypt_d2i(pkcs8->algor,
  388. ASN1_ITEM_rptr(PKCS8_PRIV_KEY_INFO), pass,
  389. pass_len, pkcs8->digest);
  390. }
  391. static ASN1_OCTET_STRING *pkcs12_item_i2d_encrypt(X509_ALGOR *algor,
  392. const ASN1_ITEM *it,
  393. const char *pass,
  394. size_t passlen, void *obj) {
  395. ASN1_OCTET_STRING *oct;
  396. uint8_t *in = NULL;
  397. int in_len;
  398. size_t crypt_len;
  399. oct = M_ASN1_OCTET_STRING_new();
  400. if (oct == NULL) {
  401. OPENSSL_PUT_ERROR(PKCS8, pkcs12_item_i2d_encrypt, ERR_R_MALLOC_FAILURE);
  402. return NULL;
  403. }
  404. in_len = ASN1_item_i2d(obj, &in, it);
  405. if (!in) {
  406. OPENSSL_PUT_ERROR(PKCS8, pkcs12_item_i2d_encrypt, PKCS8_R_ENCODE_ERROR);
  407. return NULL;
  408. }
  409. if (!pbe_crypt(algor, pass, passlen, in, in_len, &oct->data, &crypt_len,
  410. 1 /* encrypt */)) {
  411. OPENSSL_PUT_ERROR(PKCS8, pkcs12_item_i2d_encrypt, PKCS8_R_ENCRYPT_ERROR);
  412. OPENSSL_free(in);
  413. return NULL;
  414. }
  415. oct->length = crypt_len;
  416. OPENSSL_cleanse(in, in_len);
  417. OPENSSL_free(in);
  418. return oct;
  419. }
  420. X509_SIG *PKCS8_encrypt(int pbe_nid, const EVP_CIPHER *cipher, const char *pass,
  421. int pass_len, uint8_t *salt, size_t salt_len,
  422. int iterations, PKCS8_PRIV_KEY_INFO *p8inf) {
  423. X509_SIG *pkcs8 = NULL;
  424. X509_ALGOR *pbe;
  425. if (pass && pass_len == -1) {
  426. pass_len = strlen(pass);
  427. }
  428. pkcs8 = X509_SIG_new();
  429. if (pkcs8 == NULL) {
  430. OPENSSL_PUT_ERROR(PKCS8, PKCS8_encrypt, ERR_R_MALLOC_FAILURE);
  431. goto err;
  432. }
  433. if (pbe_nid == -1) {
  434. pbe = PKCS5_pbe2_set(cipher, iterations, salt, salt_len);
  435. } else {
  436. pbe = PKCS5_pbe_set(pbe_nid, iterations, salt, salt_len);
  437. }
  438. if (!pbe) {
  439. OPENSSL_PUT_ERROR(PKCS8, PKCS8_encrypt, ERR_R_ASN1_LIB);
  440. goto err;
  441. }
  442. X509_ALGOR_free(pkcs8->algor);
  443. pkcs8->algor = pbe;
  444. M_ASN1_OCTET_STRING_free(pkcs8->digest);
  445. pkcs8->digest = pkcs12_item_i2d_encrypt(
  446. pbe, ASN1_ITEM_rptr(PKCS8_PRIV_KEY_INFO), pass, pass_len, p8inf);
  447. if (!pkcs8->digest) {
  448. OPENSSL_PUT_ERROR(PKCS8, PKCS8_encrypt, PKCS8_R_ENCRYPT_ERROR);
  449. goto err;
  450. }
  451. return pkcs8;
  452. err:
  453. X509_SIG_free(pkcs8);
  454. return NULL;
  455. }
  456. EVP_PKEY *EVP_PKCS82PKEY(PKCS8_PRIV_KEY_INFO *p8) {
  457. EVP_PKEY *pkey = NULL;
  458. ASN1_OBJECT *algoid;
  459. char obj_tmp[80];
  460. if (!PKCS8_pkey_get0(&algoid, NULL, NULL, NULL, p8))
  461. return NULL;
  462. pkey = EVP_PKEY_new();
  463. if (pkey == NULL) {
  464. OPENSSL_PUT_ERROR(PKCS8, EVP_PKCS82PKEY, ERR_R_MALLOC_FAILURE);
  465. return NULL;
  466. }
  467. if (!EVP_PKEY_set_type(pkey, OBJ_obj2nid(algoid))) {
  468. OPENSSL_PUT_ERROR(PKCS8, EVP_PKCS82PKEY,
  469. PKCS8_R_UNSUPPORTED_PRIVATE_KEY_ALGORITHM);
  470. i2t_ASN1_OBJECT(obj_tmp, 80, algoid);
  471. ERR_add_error_data(2, "TYPE=", obj_tmp);
  472. goto error;
  473. }
  474. if (pkey->ameth->priv_decode) {
  475. if (!pkey->ameth->priv_decode(pkey, p8)) {
  476. OPENSSL_PUT_ERROR(PKCS8, EVP_PKCS82PKEY, PKCS8_R_PRIVATE_KEY_DECODE_ERROR);
  477. goto error;
  478. }
  479. } else {
  480. OPENSSL_PUT_ERROR(PKCS8, EVP_PKCS82PKEY, PKCS8_R_METHOD_NOT_SUPPORTED);
  481. goto error;
  482. }
  483. return pkey;
  484. error:
  485. EVP_PKEY_free(pkey);
  486. return NULL;
  487. }
  488. PKCS8_PRIV_KEY_INFO *EVP_PKEY2PKCS8(EVP_PKEY *pkey) {
  489. PKCS8_PRIV_KEY_INFO *p8;
  490. p8 = PKCS8_PRIV_KEY_INFO_new();
  491. if (p8 == NULL) {
  492. OPENSSL_PUT_ERROR(PKCS8, EVP_PKEY2PKCS8, ERR_R_MALLOC_FAILURE);
  493. return NULL;
  494. }
  495. p8->broken = PKCS8_OK;
  496. if (pkey->ameth) {
  497. if (pkey->ameth->priv_encode) {
  498. if (!pkey->ameth->priv_encode(p8, pkey)) {
  499. OPENSSL_PUT_ERROR(PKCS8, EVP_PKEY2PKCS8,
  500. PKCS8_R_PRIVATE_KEY_ENCODE_ERROR);
  501. goto error;
  502. }
  503. } else {
  504. OPENSSL_PUT_ERROR(PKCS8, EVP_PKEY2PKCS8, PKCS8_R_METHOD_NOT_SUPPORTED);
  505. goto error;
  506. }
  507. } else {
  508. OPENSSL_PUT_ERROR(PKCS8, EVP_PKEY2PKCS8,
  509. PKCS8_R_UNSUPPORTED_PRIVATE_KEY_ALGORITHM);
  510. goto error;
  511. }
  512. return p8;
  513. error:
  514. PKCS8_PRIV_KEY_INFO_free(p8);
  515. return NULL;
  516. }