evp_pkey.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782
  1. /* evp_pkey.c */
  2. /*
  3. * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
  4. * 1999.
  5. */
  6. /* ====================================================================
  7. * Copyright (c) 1999-2002 The OpenSSL Project. All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. *
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in
  18. * the documentation and/or other materials provided with the
  19. * distribution.
  20. *
  21. * 3. All advertising materials mentioning features or use of this
  22. * software must display the following acknowledgment:
  23. * "This product includes software developed by the OpenSSL Project
  24. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  25. *
  26. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  27. * endorse or promote products derived from this software without
  28. * prior written permission. For written permission, please contact
  29. * licensing@OpenSSL.org.
  30. *
  31. * 5. Products derived from this software may not be called "OpenSSL"
  32. * nor may "OpenSSL" appear in their names without prior written
  33. * permission of the OpenSSL Project.
  34. *
  35. * 6. Redistributions of any form whatsoever must retain the following
  36. * acknowledgment:
  37. * "This product includes software developed by the OpenSSL Project
  38. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  41. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  43. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  44. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  45. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  46. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  47. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  49. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  50. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  51. * OF THE POSSIBILITY OF SUCH DAMAGE.
  52. * ====================================================================
  53. *
  54. * This product includes cryptographic software written by Eric Young
  55. * (eay@cryptsoft.com). This product includes software written by Tim
  56. * Hudson (tjh@cryptsoft.com).
  57. *
  58. */
  59. #include <stdio.h>
  60. #include <stdlib.h>
  61. #include "cryptlib.h"
  62. #include <openssl/x509.h>
  63. #include <openssl/rand.h>
  64. #ifndef OPENSSL_NO_RSA
  65. # include <openssl/rsa.h>
  66. #endif
  67. #ifndef OPENSSL_NO_DSA
  68. # include <openssl/dsa.h>
  69. #endif
  70. #include <openssl/bn.h>
  71. #ifndef OPENSSL_NO_DSA
  72. static int dsa_pkey2pkcs8(PKCS8_PRIV_KEY_INFO *p8inf, EVP_PKEY *pkey);
  73. #endif
  74. #ifndef OPENSSL_NO_EC
  75. static int eckey_pkey2pkcs8(PKCS8_PRIV_KEY_INFO *p8inf, EVP_PKEY *pkey);
  76. #endif
  77. /* Extract a private key from a PKCS8 structure */
  78. EVP_PKEY *EVP_PKCS82PKEY(PKCS8_PRIV_KEY_INFO *p8)
  79. {
  80. EVP_PKEY *pkey = NULL;
  81. #ifndef OPENSSL_NO_RSA
  82. RSA *rsa = NULL;
  83. #endif
  84. #ifndef OPENSSL_NO_DSA
  85. DSA *dsa = NULL;
  86. ASN1_TYPE *t1, *t2;
  87. ASN1_INTEGER *privkey;
  88. STACK_OF(ASN1_TYPE) *ndsa = NULL;
  89. #endif
  90. #ifndef OPENSSL_NO_EC
  91. EC_KEY *eckey = NULL;
  92. const unsigned char *p_tmp;
  93. #endif
  94. #if !defined(OPENSSL_NO_DSA) || !defined(OPENSSL_NO_EC)
  95. ASN1_TYPE *param = NULL;
  96. BN_CTX *ctx = NULL;
  97. int plen;
  98. #endif
  99. X509_ALGOR *a;
  100. const unsigned char *p;
  101. const unsigned char *cp;
  102. int pkeylen;
  103. int nid;
  104. char obj_tmp[80];
  105. if (p8->pkey->type == V_ASN1_OCTET_STRING) {
  106. p8->broken = PKCS8_OK;
  107. p = p8->pkey->value.octet_string->data;
  108. pkeylen = p8->pkey->value.octet_string->length;
  109. } else {
  110. p8->broken = PKCS8_NO_OCTET;
  111. p = p8->pkey->value.sequence->data;
  112. pkeylen = p8->pkey->value.sequence->length;
  113. }
  114. if (!(pkey = EVP_PKEY_new())) {
  115. EVPerr(EVP_F_EVP_PKCS82PKEY, ERR_R_MALLOC_FAILURE);
  116. return NULL;
  117. }
  118. a = p8->pkeyalg;
  119. nid = OBJ_obj2nid(a->algorithm);
  120. switch (nid) {
  121. #ifndef OPENSSL_NO_RSA
  122. case NID_rsaEncryption:
  123. cp = p;
  124. if (!(rsa = d2i_RSAPrivateKey(NULL, &cp, pkeylen))) {
  125. EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR);
  126. return NULL;
  127. }
  128. EVP_PKEY_assign_RSA(pkey, rsa);
  129. break;
  130. #endif
  131. #ifndef OPENSSL_NO_DSA
  132. case NID_dsa:
  133. /*
  134. * PKCS#8 DSA is weird: you just get a private key integer and
  135. * parameters in the AlgorithmIdentifier the pubkey must be
  136. * recalculated.
  137. */
  138. /* Check for broken DSA PKCS#8, UGH! */
  139. if (*p == (V_ASN1_SEQUENCE | V_ASN1_CONSTRUCTED)) {
  140. if (!(ndsa = ASN1_seq_unpack_ASN1_TYPE(p, pkeylen,
  141. d2i_ASN1_TYPE,
  142. ASN1_TYPE_free))) {
  143. EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR);
  144. goto dsaerr;
  145. }
  146. if (sk_ASN1_TYPE_num(ndsa) != 2) {
  147. EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR);
  148. goto dsaerr;
  149. }
  150. /*
  151. * Handle Two broken types: SEQUENCE {parameters, priv_key}
  152. * SEQUENCE {pub_key, priv_key}
  153. */
  154. t1 = sk_ASN1_TYPE_value(ndsa, 0);
  155. t2 = sk_ASN1_TYPE_value(ndsa, 1);
  156. if (t1->type == V_ASN1_SEQUENCE) {
  157. p8->broken = PKCS8_EMBEDDED_PARAM;
  158. param = t1;
  159. } else if (a->parameter->type == V_ASN1_SEQUENCE) {
  160. p8->broken = PKCS8_NS_DB;
  161. param = a->parameter;
  162. } else {
  163. EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR);
  164. goto dsaerr;
  165. }
  166. if (t2->type != V_ASN1_INTEGER) {
  167. EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR);
  168. goto dsaerr;
  169. }
  170. privkey = t2->value.integer;
  171. } else {
  172. if (!(privkey = d2i_ASN1_INTEGER(NULL, &p, pkeylen))) {
  173. EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR);
  174. goto dsaerr;
  175. }
  176. param = p8->pkeyalg->parameter;
  177. }
  178. if (!param || (param->type != V_ASN1_SEQUENCE)) {
  179. EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR);
  180. goto dsaerr;
  181. }
  182. cp = p = param->value.sequence->data;
  183. plen = param->value.sequence->length;
  184. if (!(dsa = d2i_DSAparams(NULL, &cp, plen))) {
  185. EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR);
  186. goto dsaerr;
  187. }
  188. /* We have parameters now set private key */
  189. if (!(dsa->priv_key = ASN1_INTEGER_to_BN(privkey, NULL))) {
  190. EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_BN_DECODE_ERROR);
  191. goto dsaerr;
  192. }
  193. /* Calculate public key (ouch!) */
  194. if (!(dsa->pub_key = BN_new())) {
  195. EVPerr(EVP_F_EVP_PKCS82PKEY, ERR_R_MALLOC_FAILURE);
  196. goto dsaerr;
  197. }
  198. if (!(ctx = BN_CTX_new())) {
  199. EVPerr(EVP_F_EVP_PKCS82PKEY, ERR_R_MALLOC_FAILURE);
  200. goto dsaerr;
  201. }
  202. if (!BN_mod_exp(dsa->pub_key, dsa->g, dsa->priv_key, dsa->p, ctx)) {
  203. EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_BN_PUBKEY_ERROR);
  204. goto dsaerr;
  205. }
  206. EVP_PKEY_assign_DSA(pkey, dsa);
  207. BN_CTX_free(ctx);
  208. if (ndsa)
  209. sk_ASN1_TYPE_pop_free(ndsa, ASN1_TYPE_free);
  210. else
  211. ASN1_INTEGER_free(privkey);
  212. break;
  213. dsaerr:
  214. BN_CTX_free(ctx);
  215. sk_ASN1_TYPE_pop_free(ndsa, ASN1_TYPE_free);
  216. DSA_free(dsa);
  217. EVP_PKEY_free(pkey);
  218. return NULL;
  219. break;
  220. #endif
  221. #ifndef OPENSSL_NO_EC
  222. case NID_X9_62_id_ecPublicKey:
  223. p_tmp = p;
  224. /* extract the ec parameters */
  225. param = p8->pkeyalg->parameter;
  226. if (!param || ((param->type != V_ASN1_SEQUENCE) &&
  227. (param->type != V_ASN1_OBJECT))) {
  228. EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR);
  229. goto ecerr;
  230. }
  231. if (param->type == V_ASN1_SEQUENCE) {
  232. cp = p = param->value.sequence->data;
  233. plen = param->value.sequence->length;
  234. if (!(eckey = d2i_ECParameters(NULL, &cp, plen))) {
  235. EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR);
  236. goto ecerr;
  237. }
  238. } else {
  239. EC_GROUP *group;
  240. cp = p = param->value.object->data;
  241. plen = param->value.object->length;
  242. /*
  243. * type == V_ASN1_OBJECT => the parameters are given by an asn1
  244. * OID
  245. */
  246. if ((eckey = EC_KEY_new()) == NULL) {
  247. EVPerr(EVP_F_EVP_PKCS82PKEY, ERR_R_MALLOC_FAILURE);
  248. goto ecerr;
  249. }
  250. group =
  251. EC_GROUP_new_by_curve_name(OBJ_obj2nid
  252. (a->parameter->value.object));
  253. if (group == NULL)
  254. goto ecerr;
  255. EC_GROUP_set_asn1_flag(group, OPENSSL_EC_NAMED_CURVE);
  256. if (EC_KEY_set_group(eckey, group) == 0)
  257. goto ecerr;
  258. EC_GROUP_free(group);
  259. }
  260. /* We have parameters now set private key */
  261. if (!d2i_ECPrivateKey(&eckey, &p_tmp, pkeylen)) {
  262. EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR);
  263. goto ecerr;
  264. }
  265. /* calculate public key (if necessary) */
  266. if (EC_KEY_get0_public_key(eckey) == NULL) {
  267. const BIGNUM *priv_key;
  268. const EC_GROUP *group;
  269. EC_POINT *pub_key;
  270. /*
  271. * the public key was not included in the SEC1 private key =>
  272. * calculate the public key
  273. */
  274. group = EC_KEY_get0_group(eckey);
  275. pub_key = EC_POINT_new(group);
  276. if (pub_key == NULL) {
  277. EVPerr(EVP_F_EVP_PKCS82PKEY, ERR_R_EC_LIB);
  278. goto ecerr;
  279. }
  280. if (!EC_POINT_copy(pub_key, EC_GROUP_get0_generator(group))) {
  281. EC_POINT_free(pub_key);
  282. EVPerr(EVP_F_EVP_PKCS82PKEY, ERR_R_EC_LIB);
  283. goto ecerr;
  284. }
  285. priv_key = EC_KEY_get0_private_key(eckey);
  286. if (!EC_POINT_mul(group, pub_key, priv_key, NULL, NULL, ctx)) {
  287. EC_POINT_free(pub_key);
  288. EVPerr(EVP_F_EVP_PKCS82PKEY, ERR_R_EC_LIB);
  289. goto ecerr;
  290. }
  291. if (EC_KEY_set_public_key(eckey, pub_key) == 0) {
  292. EC_POINT_free(pub_key);
  293. EVPerr(EVP_F_EVP_PKCS82PKEY, ERR_R_EC_LIB);
  294. goto ecerr;
  295. }
  296. EC_POINT_free(pub_key);
  297. }
  298. EVP_PKEY_assign_EC_KEY(pkey, eckey);
  299. if (ctx)
  300. BN_CTX_free(ctx);
  301. break;
  302. ecerr:
  303. if (ctx)
  304. BN_CTX_free(ctx);
  305. if (eckey)
  306. EC_KEY_free(eckey);
  307. if (pkey)
  308. EVP_PKEY_free(pkey);
  309. return NULL;
  310. #endif
  311. default:
  312. EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_UNSUPPORTED_PRIVATE_KEY_ALGORITHM);
  313. if (!a->algorithm)
  314. BUF_strlcpy(obj_tmp, "NULL", sizeof obj_tmp);
  315. else
  316. i2t_ASN1_OBJECT(obj_tmp, 80, a->algorithm);
  317. ERR_add_error_data(2, "TYPE=", obj_tmp);
  318. EVP_PKEY_free(pkey);
  319. return NULL;
  320. }
  321. return pkey;
  322. }
  323. PKCS8_PRIV_KEY_INFO *EVP_PKEY2PKCS8(EVP_PKEY *pkey)
  324. {
  325. return EVP_PKEY2PKCS8_broken(pkey, PKCS8_OK);
  326. }
  327. /* Turn a private key into a PKCS8 structure */
  328. PKCS8_PRIV_KEY_INFO *EVP_PKEY2PKCS8_broken(EVP_PKEY *pkey, int broken)
  329. {
  330. PKCS8_PRIV_KEY_INFO *p8;
  331. if (!(p8 = PKCS8_PRIV_KEY_INFO_new())) {
  332. EVPerr(EVP_F_EVP_PKEY2PKCS8_BROKEN, ERR_R_MALLOC_FAILURE);
  333. return NULL;
  334. }
  335. p8->broken = broken;
  336. if (!ASN1_INTEGER_set(p8->version, 0)) {
  337. EVPerr(EVP_F_EVP_PKEY2PKCS8_BROKEN, ERR_R_MALLOC_FAILURE);
  338. PKCS8_PRIV_KEY_INFO_free(p8);
  339. return NULL;
  340. }
  341. if (!(p8->pkeyalg->parameter = ASN1_TYPE_new())) {
  342. EVPerr(EVP_F_EVP_PKEY2PKCS8_BROKEN, ERR_R_MALLOC_FAILURE);
  343. PKCS8_PRIV_KEY_INFO_free(p8);
  344. return NULL;
  345. }
  346. p8->pkey->type = V_ASN1_OCTET_STRING;
  347. switch (EVP_PKEY_type(pkey->type)) {
  348. #ifndef OPENSSL_NO_RSA
  349. case EVP_PKEY_RSA:
  350. if (p8->broken == PKCS8_NO_OCTET)
  351. p8->pkey->type = V_ASN1_SEQUENCE;
  352. p8->pkeyalg->algorithm = OBJ_nid2obj(NID_rsaEncryption);
  353. p8->pkeyalg->parameter->type = V_ASN1_NULL;
  354. if (!ASN1_pack_string_of(EVP_PKEY, pkey, i2d_PrivateKey,
  355. &p8->pkey->value.octet_string)) {
  356. EVPerr(EVP_F_EVP_PKEY2PKCS8_BROKEN, ERR_R_MALLOC_FAILURE);
  357. PKCS8_PRIV_KEY_INFO_free(p8);
  358. return NULL;
  359. }
  360. break;
  361. #endif
  362. #ifndef OPENSSL_NO_DSA
  363. case EVP_PKEY_DSA:
  364. if (!dsa_pkey2pkcs8(p8, pkey)) {
  365. PKCS8_PRIV_KEY_INFO_free(p8);
  366. return NULL;
  367. }
  368. break;
  369. #endif
  370. #ifndef OPENSSL_NO_EC
  371. case EVP_PKEY_EC:
  372. if (!eckey_pkey2pkcs8(p8, pkey)) {
  373. PKCS8_PRIV_KEY_INFO_free(p8);
  374. return (NULL);
  375. }
  376. break;
  377. #endif
  378. default:
  379. EVPerr(EVP_F_EVP_PKEY2PKCS8_BROKEN,
  380. EVP_R_UNSUPPORTED_PRIVATE_KEY_ALGORITHM);
  381. PKCS8_PRIV_KEY_INFO_free(p8);
  382. return NULL;
  383. }
  384. RAND_add(p8->pkey->value.octet_string->data,
  385. p8->pkey->value.octet_string->length, 0.0);
  386. return p8;
  387. }
  388. PKCS8_PRIV_KEY_INFO *PKCS8_set_broken(PKCS8_PRIV_KEY_INFO *p8, int broken)
  389. {
  390. switch (broken) {
  391. case PKCS8_OK:
  392. p8->broken = PKCS8_OK;
  393. return p8;
  394. break;
  395. case PKCS8_NO_OCTET:
  396. p8->broken = PKCS8_NO_OCTET;
  397. p8->pkey->type = V_ASN1_SEQUENCE;
  398. return p8;
  399. break;
  400. default:
  401. EVPerr(EVP_F_PKCS8_SET_BROKEN, EVP_R_PKCS8_UNKNOWN_BROKEN_TYPE);
  402. return NULL;
  403. }
  404. }
  405. #ifndef OPENSSL_NO_DSA
  406. static int dsa_pkey2pkcs8(PKCS8_PRIV_KEY_INFO *p8, EVP_PKEY *pkey)
  407. {
  408. ASN1_STRING *params = NULL;
  409. ASN1_INTEGER *prkey = NULL;
  410. ASN1_TYPE *ttmp = NULL;
  411. STACK_OF(ASN1_TYPE) *ndsa = NULL;
  412. unsigned char *p = NULL, *q;
  413. int len;
  414. p8->pkeyalg->algorithm = OBJ_nid2obj(NID_dsa);
  415. len = i2d_DSAparams(pkey->pkey.dsa, NULL);
  416. if (!(p = OPENSSL_malloc(len))) {
  417. EVPerr(EVP_F_DSA_PKEY2PKCS8, ERR_R_MALLOC_FAILURE);
  418. goto err;
  419. }
  420. q = p;
  421. i2d_DSAparams(pkey->pkey.dsa, &q);
  422. if (!(params = ASN1_STRING_new())) {
  423. EVPerr(EVP_F_DSA_PKEY2PKCS8, ERR_R_MALLOC_FAILURE);
  424. goto err;
  425. }
  426. if (!ASN1_STRING_set(params, p, len)) {
  427. EVPerr(EVP_F_DSA_PKEY2PKCS8, ERR_R_MALLOC_FAILURE);
  428. goto err;
  429. }
  430. OPENSSL_free(p);
  431. p = NULL;
  432. /* Get private key into integer */
  433. if (!(prkey = BN_to_ASN1_INTEGER(pkey->pkey.dsa->priv_key, NULL))) {
  434. EVPerr(EVP_F_DSA_PKEY2PKCS8, EVP_R_ENCODE_ERROR);
  435. goto err;
  436. }
  437. switch (p8->broken) {
  438. case PKCS8_OK:
  439. case PKCS8_NO_OCTET:
  440. if (!ASN1_pack_string_of(ASN1_INTEGER, prkey, i2d_ASN1_INTEGER,
  441. &p8->pkey->value.octet_string)) {
  442. EVPerr(EVP_F_DSA_PKEY2PKCS8, ERR_R_MALLOC_FAILURE);
  443. goto err;
  444. }
  445. M_ASN1_INTEGER_free(prkey);
  446. prkey = NULL;
  447. p8->pkeyalg->parameter->value.sequence = params;
  448. params = NULL;
  449. p8->pkeyalg->parameter->type = V_ASN1_SEQUENCE;
  450. break;
  451. case PKCS8_NS_DB:
  452. p8->pkeyalg->parameter->value.sequence = params;
  453. params = NULL;
  454. p8->pkeyalg->parameter->type = V_ASN1_SEQUENCE;
  455. if (!(ndsa = sk_ASN1_TYPE_new_null())) {
  456. EVPerr(EVP_F_DSA_PKEY2PKCS8, ERR_R_MALLOC_FAILURE);
  457. goto err;
  458. }
  459. if (!(ttmp = ASN1_TYPE_new())) {
  460. EVPerr(EVP_F_DSA_PKEY2PKCS8, ERR_R_MALLOC_FAILURE);
  461. goto err;
  462. }
  463. if (!(ttmp->value.integer =
  464. BN_to_ASN1_INTEGER(pkey->pkey.dsa->pub_key, NULL))) {
  465. EVPerr(EVP_F_DSA_PKEY2PKCS8, EVP_R_ENCODE_ERROR);
  466. goto err;
  467. }
  468. ttmp->type = V_ASN1_INTEGER;
  469. if (!sk_ASN1_TYPE_push(ndsa, ttmp)) {
  470. EVPerr(EVP_F_DSA_PKEY2PKCS8, ERR_R_MALLOC_FAILURE);
  471. goto err;
  472. }
  473. if (!(ttmp = ASN1_TYPE_new())) {
  474. EVPerr(EVP_F_DSA_PKEY2PKCS8, ERR_R_MALLOC_FAILURE);
  475. goto err;
  476. }
  477. ttmp->value.integer = prkey;
  478. prkey = NULL;
  479. ttmp->type = V_ASN1_INTEGER;
  480. if (!sk_ASN1_TYPE_push(ndsa, ttmp)) {
  481. EVPerr(EVP_F_DSA_PKEY2PKCS8, ERR_R_MALLOC_FAILURE);
  482. goto err;
  483. }
  484. ttmp = NULL;
  485. if (!(p8->pkey->value.octet_string = ASN1_OCTET_STRING_new())) {
  486. EVPerr(EVP_F_DSA_PKEY2PKCS8, ERR_R_MALLOC_FAILURE);
  487. goto err;
  488. }
  489. if (!ASN1_seq_pack_ASN1_TYPE(ndsa, i2d_ASN1_TYPE,
  490. &p8->pkey->value.octet_string->data,
  491. &p8->pkey->value.octet_string->length)) {
  492. EVPerr(EVP_F_DSA_PKEY2PKCS8, ERR_R_MALLOC_FAILURE);
  493. goto err;
  494. }
  495. sk_ASN1_TYPE_pop_free(ndsa, ASN1_TYPE_free);
  496. break;
  497. case PKCS8_EMBEDDED_PARAM:
  498. p8->pkeyalg->parameter->type = V_ASN1_NULL;
  499. if (!(ndsa = sk_ASN1_TYPE_new_null())) {
  500. EVPerr(EVP_F_DSA_PKEY2PKCS8, ERR_R_MALLOC_FAILURE);
  501. goto err;
  502. }
  503. if (!(ttmp = ASN1_TYPE_new())) {
  504. EVPerr(EVP_F_DSA_PKEY2PKCS8, ERR_R_MALLOC_FAILURE);
  505. goto err;
  506. }
  507. ttmp->value.sequence = params;
  508. params = NULL;
  509. ttmp->type = V_ASN1_SEQUENCE;
  510. if (!sk_ASN1_TYPE_push(ndsa, ttmp)) {
  511. EVPerr(EVP_F_DSA_PKEY2PKCS8, ERR_R_MALLOC_FAILURE);
  512. goto err;
  513. }
  514. if (!(ttmp = ASN1_TYPE_new())) {
  515. EVPerr(EVP_F_DSA_PKEY2PKCS8, ERR_R_MALLOC_FAILURE);
  516. goto err;
  517. }
  518. ttmp->value.integer = prkey;
  519. prkey = NULL;
  520. ttmp->type = V_ASN1_INTEGER;
  521. if (!sk_ASN1_TYPE_push(ndsa, ttmp)) {
  522. EVPerr(EVP_F_DSA_PKEY2PKCS8, ERR_R_MALLOC_FAILURE);
  523. goto err;
  524. }
  525. ttmp = NULL;
  526. if (!(p8->pkey->value.octet_string = ASN1_OCTET_STRING_new())) {
  527. EVPerr(EVP_F_DSA_PKEY2PKCS8, ERR_R_MALLOC_FAILURE);
  528. goto err;
  529. }
  530. if (!ASN1_seq_pack_ASN1_TYPE(ndsa, i2d_ASN1_TYPE,
  531. &p8->pkey->value.octet_string->data,
  532. &p8->pkey->value.octet_string->length)) {
  533. EVPerr(EVP_F_DSA_PKEY2PKCS8, ERR_R_MALLOC_FAILURE);
  534. goto err;
  535. }
  536. sk_ASN1_TYPE_pop_free(ndsa, ASN1_TYPE_free);
  537. break;
  538. }
  539. return 1;
  540. err:
  541. if (p != NULL)
  542. OPENSSL_free(p);
  543. if (params != NULL)
  544. ASN1_STRING_free(params);
  545. if (prkey != NULL)
  546. M_ASN1_INTEGER_free(prkey);
  547. if (ttmp != NULL)
  548. ASN1_TYPE_free(ttmp);
  549. if (ndsa != NULL)
  550. sk_ASN1_TYPE_pop_free(ndsa, ASN1_TYPE_free);
  551. return 0;
  552. }
  553. #endif
  554. #ifndef OPENSSL_NO_EC
  555. static int eckey_pkey2pkcs8(PKCS8_PRIV_KEY_INFO *p8, EVP_PKEY *pkey)
  556. {
  557. EC_KEY *ec_key;
  558. const EC_GROUP *group;
  559. unsigned char *p, *pp;
  560. int nid, i, ret = 0;
  561. unsigned int tmp_flags, old_flags;
  562. ec_key = pkey->pkey.ec;
  563. if (ec_key == NULL || (group = EC_KEY_get0_group(ec_key)) == NULL) {
  564. EVPerr(EVP_F_ECKEY_PKEY2PKCS8, EVP_R_MISSING_PARAMETERS);
  565. return 0;
  566. }
  567. /* set the ec parameters OID */
  568. if (p8->pkeyalg->algorithm)
  569. ASN1_OBJECT_free(p8->pkeyalg->algorithm);
  570. p8->pkeyalg->algorithm = OBJ_nid2obj(NID_X9_62_id_ecPublicKey);
  571. /* set the ec parameters */
  572. if (p8->pkeyalg->parameter) {
  573. ASN1_TYPE_free(p8->pkeyalg->parameter);
  574. p8->pkeyalg->parameter = NULL;
  575. }
  576. if ((p8->pkeyalg->parameter = ASN1_TYPE_new()) == NULL) {
  577. EVPerr(EVP_F_ECKEY_PKEY2PKCS8, ERR_R_MALLOC_FAILURE);
  578. return 0;
  579. }
  580. if (EC_GROUP_get_asn1_flag(group)
  581. && (nid = EC_GROUP_get_curve_name(group))) {
  582. /* we have a 'named curve' => just set the OID */
  583. p8->pkeyalg->parameter->type = V_ASN1_OBJECT;
  584. p8->pkeyalg->parameter->value.object = OBJ_nid2obj(nid);
  585. } else { /* explicit parameters */
  586. if ((i = i2d_ECParameters(ec_key, NULL)) == 0) {
  587. EVPerr(EVP_F_ECKEY_PKEY2PKCS8, ERR_R_EC_LIB);
  588. return 0;
  589. }
  590. if ((p = (unsigned char *)OPENSSL_malloc(i)) == NULL) {
  591. EVPerr(EVP_F_ECKEY_PKEY2PKCS8, ERR_R_MALLOC_FAILURE);
  592. return 0;
  593. }
  594. pp = p;
  595. if (!i2d_ECParameters(ec_key, &pp)) {
  596. EVPerr(EVP_F_ECKEY_PKEY2PKCS8, ERR_R_EC_LIB);
  597. OPENSSL_free(p);
  598. return 0;
  599. }
  600. p8->pkeyalg->parameter->type = V_ASN1_SEQUENCE;
  601. if ((p8->pkeyalg->parameter->value.sequence
  602. = ASN1_STRING_new()) == NULL) {
  603. EVPerr(EVP_F_ECKEY_PKEY2PKCS8, ERR_R_ASN1_LIB);
  604. OPENSSL_free(p);
  605. return 0;
  606. }
  607. ASN1_STRING_set(p8->pkeyalg->parameter->value.sequence, p, i);
  608. OPENSSL_free(p);
  609. }
  610. /* set the private key */
  611. /*
  612. * do not include the parameters in the SEC1 private key see PKCS#11
  613. * 12.11
  614. */
  615. old_flags = EC_KEY_get_enc_flags(pkey->pkey.ec);
  616. tmp_flags = old_flags | EC_PKEY_NO_PARAMETERS;
  617. EC_KEY_set_enc_flags(pkey->pkey.ec, tmp_flags);
  618. i = i2d_ECPrivateKey(pkey->pkey.ec, NULL);
  619. if (!i) {
  620. EC_KEY_set_enc_flags(pkey->pkey.ec, old_flags);
  621. EVPerr(EVP_F_ECKEY_PKEY2PKCS8, ERR_R_EC_LIB);
  622. return 0;
  623. }
  624. p = (unsigned char *)OPENSSL_malloc(i);
  625. if (!p) {
  626. EC_KEY_set_enc_flags(pkey->pkey.ec, old_flags);
  627. EVPerr(EVP_F_ECKEY_PKEY2PKCS8, ERR_R_MALLOC_FAILURE);
  628. return 0;
  629. }
  630. pp = p;
  631. if (!i2d_ECPrivateKey(pkey->pkey.ec, &pp)) {
  632. EC_KEY_set_enc_flags(pkey->pkey.ec, old_flags);
  633. EVPerr(EVP_F_ECKEY_PKEY2PKCS8, ERR_R_EC_LIB);
  634. OPENSSL_free(p);
  635. return 0;
  636. }
  637. /* restore old encoding flags */
  638. EC_KEY_set_enc_flags(pkey->pkey.ec, old_flags);
  639. switch (p8->broken) {
  640. case PKCS8_OK:
  641. p8->pkey->value.octet_string = ASN1_OCTET_STRING_new();
  642. if (!p8->pkey->value.octet_string ||
  643. !M_ASN1_OCTET_STRING_set(p8->pkey->value.octet_string,
  644. (const void *)p, i)) {
  645. EVPerr(EVP_F_ECKEY_PKEY2PKCS8, ERR_R_MALLOC_FAILURE);
  646. } else
  647. ret = 1;
  648. break;
  649. case PKCS8_NO_OCTET: /* RSA specific */
  650. case PKCS8_NS_DB: /* DSA specific */
  651. case PKCS8_EMBEDDED_PARAM: /* DSA specific */
  652. default:
  653. EVPerr(EVP_F_ECKEY_PKEY2PKCS8, EVP_R_ENCODE_ERROR);
  654. }
  655. OPENSSL_cleanse(p, (size_t)i);
  656. OPENSSL_free(p);
  657. return ret;
  658. }
  659. #endif
  660. /* EVP_PKEY attribute functions */
  661. int EVP_PKEY_get_attr_count(const EVP_PKEY *key)
  662. {
  663. return X509at_get_attr_count(key->attributes);
  664. }
  665. int EVP_PKEY_get_attr_by_NID(const EVP_PKEY *key, int nid, int lastpos)
  666. {
  667. return X509at_get_attr_by_NID(key->attributes, nid, lastpos);
  668. }
  669. int EVP_PKEY_get_attr_by_OBJ(const EVP_PKEY *key, ASN1_OBJECT *obj,
  670. int lastpos)
  671. {
  672. return X509at_get_attr_by_OBJ(key->attributes, obj, lastpos);
  673. }
  674. X509_ATTRIBUTE *EVP_PKEY_get_attr(const EVP_PKEY *key, int loc)
  675. {
  676. return X509at_get_attr(key->attributes, loc);
  677. }
  678. X509_ATTRIBUTE *EVP_PKEY_delete_attr(EVP_PKEY *key, int loc)
  679. {
  680. return X509at_delete_attr(key->attributes, loc);
  681. }
  682. int EVP_PKEY_add1_attr(EVP_PKEY *key, X509_ATTRIBUTE *attr)
  683. {
  684. if (X509at_add1_attr(&key->attributes, attr))
  685. return 1;
  686. return 0;
  687. }
  688. int EVP_PKEY_add1_attr_by_OBJ(EVP_PKEY *key,
  689. const ASN1_OBJECT *obj, int type,
  690. const unsigned char *bytes, int len)
  691. {
  692. if (X509at_add1_attr_by_OBJ(&key->attributes, obj, type, bytes, len))
  693. return 1;
  694. return 0;
  695. }
  696. int EVP_PKEY_add1_attr_by_NID(EVP_PKEY *key,
  697. int nid, int type,
  698. const unsigned char *bytes, int len)
  699. {
  700. if (X509at_add1_attr_by_NID(&key->attributes, nid, type, bytes, len))
  701. return 1;
  702. return 0;
  703. }
  704. int EVP_PKEY_add1_attr_by_txt(EVP_PKEY *key,
  705. const char *attrname, int type,
  706. const unsigned char *bytes, int len)
  707. {
  708. if (X509at_add1_attr_by_txt(&key->attributes, attrname, type, bytes, len))
  709. return 1;
  710. return 0;
  711. }