p_lib.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  1. /*
  2. * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdio.h>
  10. #include "internal/cryptlib.h"
  11. #include "internal/refcount.h"
  12. #include <openssl/bn.h>
  13. #include <openssl/err.h>
  14. #include <openssl/objects.h>
  15. #include <openssl/evp.h>
  16. #include <openssl/x509.h>
  17. #include <openssl/rsa.h>
  18. #include <openssl/dsa.h>
  19. #include <openssl/dh.h>
  20. #include <openssl/cmac.h>
  21. #include <openssl/engine.h>
  22. #include "internal/asn1_int.h"
  23. #include "internal/evp_int.h"
  24. static void EVP_PKEY_free_it(EVP_PKEY *x);
  25. int EVP_PKEY_bits(const EVP_PKEY *pkey)
  26. {
  27. if (pkey && pkey->ameth && pkey->ameth->pkey_bits)
  28. return pkey->ameth->pkey_bits(pkey);
  29. return 0;
  30. }
  31. int EVP_PKEY_security_bits(const EVP_PKEY *pkey)
  32. {
  33. if (pkey == NULL)
  34. return 0;
  35. if (!pkey->ameth || !pkey->ameth->pkey_security_bits)
  36. return -2;
  37. return pkey->ameth->pkey_security_bits(pkey);
  38. }
  39. int EVP_PKEY_size(EVP_PKEY *pkey)
  40. {
  41. if (pkey && pkey->ameth && pkey->ameth->pkey_size)
  42. return pkey->ameth->pkey_size(pkey);
  43. return 0;
  44. }
  45. int EVP_PKEY_save_parameters(EVP_PKEY *pkey, int mode)
  46. {
  47. #ifndef OPENSSL_NO_DSA
  48. if (pkey->type == EVP_PKEY_DSA) {
  49. int ret = pkey->save_parameters;
  50. if (mode >= 0)
  51. pkey->save_parameters = mode;
  52. return ret;
  53. }
  54. #endif
  55. #ifndef OPENSSL_NO_EC
  56. if (pkey->type == EVP_PKEY_EC) {
  57. int ret = pkey->save_parameters;
  58. if (mode >= 0)
  59. pkey->save_parameters = mode;
  60. return ret;
  61. }
  62. #endif
  63. return 0;
  64. }
  65. int EVP_PKEY_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from)
  66. {
  67. if (to->type == EVP_PKEY_NONE) {
  68. if (EVP_PKEY_set_type(to, from->type) == 0)
  69. return 0;
  70. } else if (to->type != from->type) {
  71. EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS, EVP_R_DIFFERENT_KEY_TYPES);
  72. goto err;
  73. }
  74. if (EVP_PKEY_missing_parameters(from)) {
  75. EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS, EVP_R_MISSING_PARAMETERS);
  76. goto err;
  77. }
  78. if (!EVP_PKEY_missing_parameters(to)) {
  79. if (EVP_PKEY_cmp_parameters(to, from) == 1)
  80. return 1;
  81. EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS, EVP_R_DIFFERENT_PARAMETERS);
  82. return 0;
  83. }
  84. if (from->ameth && from->ameth->param_copy)
  85. return from->ameth->param_copy(to, from);
  86. err:
  87. return 0;
  88. }
  89. int EVP_PKEY_missing_parameters(const EVP_PKEY *pkey)
  90. {
  91. if (pkey->ameth && pkey->ameth->param_missing)
  92. return pkey->ameth->param_missing(pkey);
  93. return 0;
  94. }
  95. int EVP_PKEY_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b)
  96. {
  97. if (a->type != b->type)
  98. return -1;
  99. if (a->ameth && a->ameth->param_cmp)
  100. return a->ameth->param_cmp(a, b);
  101. return -2;
  102. }
  103. int EVP_PKEY_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
  104. {
  105. if (a->type != b->type)
  106. return -1;
  107. if (a->ameth) {
  108. int ret;
  109. /* Compare parameters if the algorithm has them */
  110. if (a->ameth->param_cmp) {
  111. ret = a->ameth->param_cmp(a, b);
  112. if (ret <= 0)
  113. return ret;
  114. }
  115. if (a->ameth->pub_cmp)
  116. return a->ameth->pub_cmp(a, b);
  117. }
  118. return -2;
  119. }
  120. EVP_PKEY *EVP_PKEY_new(void)
  121. {
  122. EVP_PKEY *ret = OPENSSL_zalloc(sizeof(*ret));
  123. if (ret == NULL) {
  124. EVPerr(EVP_F_EVP_PKEY_NEW, ERR_R_MALLOC_FAILURE);
  125. return NULL;
  126. }
  127. ret->type = EVP_PKEY_NONE;
  128. ret->save_type = EVP_PKEY_NONE;
  129. ret->references = 1;
  130. ret->save_parameters = 1;
  131. ret->lock = CRYPTO_THREAD_lock_new();
  132. if (ret->lock == NULL) {
  133. EVPerr(EVP_F_EVP_PKEY_NEW, ERR_R_MALLOC_FAILURE);
  134. OPENSSL_free(ret);
  135. return NULL;
  136. }
  137. return ret;
  138. }
  139. int EVP_PKEY_up_ref(EVP_PKEY *pkey)
  140. {
  141. int i;
  142. if (CRYPTO_UP_REF(&pkey->references, &i, pkey->lock) <= 0)
  143. return 0;
  144. REF_PRINT_COUNT("EVP_PKEY", pkey);
  145. REF_ASSERT_ISNT(i < 2);
  146. return ((i > 1) ? 1 : 0);
  147. }
  148. /*
  149. * Setup a public key ASN1 method and ENGINE from a NID or a string. If pkey
  150. * is NULL just return 1 or 0 if the algorithm exists.
  151. */
  152. static int pkey_set_type(EVP_PKEY *pkey, ENGINE *e, int type, const char *str,
  153. int len)
  154. {
  155. const EVP_PKEY_ASN1_METHOD *ameth;
  156. ENGINE **eptr = (e == NULL) ? &e : NULL;
  157. if (pkey) {
  158. if (pkey->pkey.ptr)
  159. EVP_PKEY_free_it(pkey);
  160. /*
  161. * If key type matches and a method exists then this lookup has
  162. * succeeded once so just indicate success.
  163. */
  164. if ((type == pkey->save_type) && pkey->ameth)
  165. return 1;
  166. #ifndef OPENSSL_NO_ENGINE
  167. /* If we have ENGINEs release them */
  168. ENGINE_finish(pkey->engine);
  169. pkey->engine = NULL;
  170. ENGINE_finish(pkey->pmeth_engine);
  171. pkey->pmeth_engine = NULL;
  172. #endif
  173. }
  174. if (str)
  175. ameth = EVP_PKEY_asn1_find_str(eptr, str, len);
  176. else
  177. ameth = EVP_PKEY_asn1_find(eptr, type);
  178. #ifndef OPENSSL_NO_ENGINE
  179. if (pkey == NULL && eptr != NULL)
  180. ENGINE_finish(e);
  181. #endif
  182. if (ameth == NULL) {
  183. EVPerr(EVP_F_PKEY_SET_TYPE, EVP_R_UNSUPPORTED_ALGORITHM);
  184. return 0;
  185. }
  186. if (pkey) {
  187. pkey->ameth = ameth;
  188. pkey->engine = e;
  189. pkey->type = pkey->ameth->pkey_id;
  190. pkey->save_type = type;
  191. }
  192. return 1;
  193. }
  194. EVP_PKEY *EVP_PKEY_new_raw_private_key(int type, ENGINE *e,
  195. const unsigned char *priv,
  196. size_t len)
  197. {
  198. EVP_PKEY *ret = EVP_PKEY_new();
  199. if (ret == NULL
  200. || !pkey_set_type(ret, e, type, NULL, -1)) {
  201. /* EVPerr already called */
  202. goto err;
  203. }
  204. if (ret->ameth->set_priv_key == NULL) {
  205. EVPerr(EVP_F_EVP_PKEY_NEW_RAW_PRIVATE_KEY,
  206. EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  207. goto err;
  208. }
  209. if (!ret->ameth->set_priv_key(ret, priv, len)) {
  210. EVPerr(EVP_F_EVP_PKEY_NEW_RAW_PRIVATE_KEY, EVP_R_KEY_SETUP_FAILED);
  211. goto err;
  212. }
  213. return ret;
  214. err:
  215. EVP_PKEY_free(ret);
  216. return NULL;
  217. }
  218. EVP_PKEY *EVP_PKEY_new_raw_public_key(int type, ENGINE *e,
  219. const unsigned char *pub,
  220. size_t len)
  221. {
  222. EVP_PKEY *ret = EVP_PKEY_new();
  223. if (ret == NULL
  224. || !pkey_set_type(ret, e, type, NULL, -1)) {
  225. /* EVPerr already called */
  226. goto err;
  227. }
  228. if (ret->ameth->set_pub_key == NULL) {
  229. EVPerr(EVP_F_EVP_PKEY_NEW_RAW_PUBLIC_KEY,
  230. EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  231. goto err;
  232. }
  233. if (!ret->ameth->set_pub_key(ret, pub, len)) {
  234. EVPerr(EVP_F_EVP_PKEY_NEW_RAW_PUBLIC_KEY, EVP_R_KEY_SETUP_FAILED);
  235. goto err;
  236. }
  237. return ret;
  238. err:
  239. EVP_PKEY_free(ret);
  240. return NULL;
  241. }
  242. int EVP_PKEY_get_raw_private_key(const EVP_PKEY *pkey, unsigned char *priv,
  243. size_t *len)
  244. {
  245. if (pkey->ameth->get_priv_key == NULL) {
  246. EVPerr(EVP_F_EVP_PKEY_GET_RAW_PRIVATE_KEY,
  247. EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  248. return 0;
  249. }
  250. if (!pkey->ameth->get_priv_key(pkey, priv, len)) {
  251. EVPerr(EVP_F_EVP_PKEY_GET_RAW_PRIVATE_KEY, EVP_R_GET_RAW_KEY_FAILED);
  252. return 0;
  253. }
  254. return 1;
  255. }
  256. int EVP_PKEY_get_raw_public_key(const EVP_PKEY *pkey, unsigned char *pub,
  257. size_t *len)
  258. {
  259. if (pkey->ameth->get_pub_key == NULL) {
  260. EVPerr(EVP_F_EVP_PKEY_GET_RAW_PUBLIC_KEY,
  261. EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  262. return 0;
  263. }
  264. if (!pkey->ameth->get_pub_key(pkey, pub, len)) {
  265. EVPerr(EVP_F_EVP_PKEY_GET_RAW_PUBLIC_KEY, EVP_R_GET_RAW_KEY_FAILED);
  266. return 0;
  267. }
  268. return 1;
  269. }
  270. EVP_PKEY *EVP_PKEY_new_CMAC_key(ENGINE *e, const unsigned char *priv,
  271. size_t len, const EVP_CIPHER *cipher)
  272. {
  273. #ifndef OPENSSL_NO_CMAC
  274. EVP_PKEY *ret = EVP_PKEY_new();
  275. EVP_MAC_CTX *cmctx = EVP_MAC_CTX_new_id(EVP_MAC_CMAC);
  276. if (ret == NULL
  277. || cmctx == NULL
  278. || !pkey_set_type(ret, e, EVP_PKEY_CMAC, NULL, -1)) {
  279. /* EVPerr already called */
  280. goto err;
  281. }
  282. if (EVP_MAC_ctrl(cmctx, EVP_MAC_CTRL_SET_ENGINE, e) <= 0
  283. || EVP_MAC_ctrl(cmctx, EVP_MAC_CTRL_SET_CIPHER, cipher) <= 0
  284. || EVP_MAC_ctrl(cmctx, EVP_MAC_CTRL_SET_KEY, priv, len) <= 0) {
  285. EVPerr(EVP_F_EVP_PKEY_NEW_CMAC_KEY, EVP_R_KEY_SETUP_FAILED);
  286. goto err;
  287. }
  288. ret->pkey.ptr = cmctx;
  289. return ret;
  290. err:
  291. EVP_PKEY_free(ret);
  292. EVP_MAC_CTX_free(cmctx);
  293. return NULL;
  294. #else
  295. EVPerr(EVP_F_EVP_PKEY_NEW_CMAC_KEY,
  296. EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  297. return NULL;
  298. #endif
  299. }
  300. int EVP_PKEY_set_type(EVP_PKEY *pkey, int type)
  301. {
  302. return pkey_set_type(pkey, NULL, type, NULL, -1);
  303. }
  304. int EVP_PKEY_set_type_str(EVP_PKEY *pkey, const char *str, int len)
  305. {
  306. return pkey_set_type(pkey, NULL, EVP_PKEY_NONE, str, len);
  307. }
  308. int EVP_PKEY_set_alias_type(EVP_PKEY *pkey, int type)
  309. {
  310. if (pkey->type == type) {
  311. return 1; /* it already is that type */
  312. }
  313. /*
  314. * The application is requesting to alias this to a different pkey type,
  315. * but not one that resolves to the base type.
  316. */
  317. if (EVP_PKEY_type(type) != EVP_PKEY_base_id(pkey)) {
  318. EVPerr(EVP_F_EVP_PKEY_SET_ALIAS_TYPE, EVP_R_UNSUPPORTED_ALGORITHM);
  319. return 0;
  320. }
  321. pkey->type = type;
  322. return 1;
  323. }
  324. #ifndef OPENSSL_NO_ENGINE
  325. int EVP_PKEY_set1_engine(EVP_PKEY *pkey, ENGINE *e)
  326. {
  327. if (e != NULL) {
  328. if (!ENGINE_init(e)) {
  329. EVPerr(EVP_F_EVP_PKEY_SET1_ENGINE, ERR_R_ENGINE_LIB);
  330. return 0;
  331. }
  332. if (ENGINE_get_pkey_meth(e, pkey->type) == NULL) {
  333. ENGINE_finish(e);
  334. EVPerr(EVP_F_EVP_PKEY_SET1_ENGINE, EVP_R_UNSUPPORTED_ALGORITHM);
  335. return 0;
  336. }
  337. }
  338. ENGINE_finish(pkey->pmeth_engine);
  339. pkey->pmeth_engine = e;
  340. return 1;
  341. }
  342. #endif
  343. int EVP_PKEY_assign(EVP_PKEY *pkey, int type, void *key)
  344. {
  345. if (pkey == NULL || !EVP_PKEY_set_type(pkey, type))
  346. return 0;
  347. pkey->pkey.ptr = key;
  348. return (key != NULL);
  349. }
  350. void *EVP_PKEY_get0(const EVP_PKEY *pkey)
  351. {
  352. return pkey->pkey.ptr;
  353. }
  354. const unsigned char *EVP_PKEY_get0_hmac(const EVP_PKEY *pkey, size_t *len)
  355. {
  356. ASN1_OCTET_STRING *os = NULL;
  357. if (pkey->type != EVP_PKEY_HMAC) {
  358. EVPerr(EVP_F_EVP_PKEY_GET0_HMAC, EVP_R_EXPECTING_AN_HMAC_KEY);
  359. return NULL;
  360. }
  361. os = EVP_PKEY_get0(pkey);
  362. *len = os->length;
  363. return os->data;
  364. }
  365. #ifndef OPENSSL_NO_POLY1305
  366. const unsigned char *EVP_PKEY_get0_poly1305(const EVP_PKEY *pkey, size_t *len)
  367. {
  368. ASN1_OCTET_STRING *os = NULL;
  369. if (pkey->type != EVP_PKEY_POLY1305) {
  370. EVPerr(EVP_F_EVP_PKEY_GET0_POLY1305, EVP_R_EXPECTING_A_POLY1305_KEY);
  371. return NULL;
  372. }
  373. os = EVP_PKEY_get0(pkey);
  374. *len = os->length;
  375. return os->data;
  376. }
  377. #endif
  378. #ifndef OPENSSL_NO_SIPHASH
  379. const unsigned char *EVP_PKEY_get0_siphash(const EVP_PKEY *pkey, size_t *len)
  380. {
  381. ASN1_OCTET_STRING *os = NULL;
  382. if (pkey->type != EVP_PKEY_SIPHASH) {
  383. EVPerr(EVP_F_EVP_PKEY_GET0_SIPHASH, EVP_R_EXPECTING_A_SIPHASH_KEY);
  384. return NULL;
  385. }
  386. os = EVP_PKEY_get0(pkey);
  387. *len = os->length;
  388. return os->data;
  389. }
  390. #endif
  391. #ifndef OPENSSL_NO_RSA
  392. int EVP_PKEY_set1_RSA(EVP_PKEY *pkey, RSA *key)
  393. {
  394. int ret = EVP_PKEY_assign_RSA(pkey, key);
  395. if (ret)
  396. RSA_up_ref(key);
  397. return ret;
  398. }
  399. RSA *EVP_PKEY_get0_RSA(EVP_PKEY *pkey)
  400. {
  401. if (pkey->type != EVP_PKEY_RSA) {
  402. EVPerr(EVP_F_EVP_PKEY_GET0_RSA, EVP_R_EXPECTING_AN_RSA_KEY);
  403. return NULL;
  404. }
  405. return pkey->pkey.rsa;
  406. }
  407. RSA *EVP_PKEY_get1_RSA(EVP_PKEY *pkey)
  408. {
  409. RSA *ret = EVP_PKEY_get0_RSA(pkey);
  410. if (ret != NULL)
  411. RSA_up_ref(ret);
  412. return ret;
  413. }
  414. #endif
  415. #ifndef OPENSSL_NO_DSA
  416. int EVP_PKEY_set1_DSA(EVP_PKEY *pkey, DSA *key)
  417. {
  418. int ret = EVP_PKEY_assign_DSA(pkey, key);
  419. if (ret)
  420. DSA_up_ref(key);
  421. return ret;
  422. }
  423. DSA *EVP_PKEY_get0_DSA(EVP_PKEY *pkey)
  424. {
  425. if (pkey->type != EVP_PKEY_DSA) {
  426. EVPerr(EVP_F_EVP_PKEY_GET0_DSA, EVP_R_EXPECTING_A_DSA_KEY);
  427. return NULL;
  428. }
  429. return pkey->pkey.dsa;
  430. }
  431. DSA *EVP_PKEY_get1_DSA(EVP_PKEY *pkey)
  432. {
  433. DSA *ret = EVP_PKEY_get0_DSA(pkey);
  434. if (ret != NULL)
  435. DSA_up_ref(ret);
  436. return ret;
  437. }
  438. #endif
  439. #ifndef OPENSSL_NO_EC
  440. int EVP_PKEY_set1_EC_KEY(EVP_PKEY *pkey, EC_KEY *key)
  441. {
  442. int ret = EVP_PKEY_assign_EC_KEY(pkey, key);
  443. if (ret)
  444. EC_KEY_up_ref(key);
  445. return ret;
  446. }
  447. EC_KEY *EVP_PKEY_get0_EC_KEY(EVP_PKEY *pkey)
  448. {
  449. if (pkey->type != EVP_PKEY_EC) {
  450. EVPerr(EVP_F_EVP_PKEY_GET0_EC_KEY, EVP_R_EXPECTING_A_EC_KEY);
  451. return NULL;
  452. }
  453. return pkey->pkey.ec;
  454. }
  455. EC_KEY *EVP_PKEY_get1_EC_KEY(EVP_PKEY *pkey)
  456. {
  457. EC_KEY *ret = EVP_PKEY_get0_EC_KEY(pkey);
  458. if (ret != NULL)
  459. EC_KEY_up_ref(ret);
  460. return ret;
  461. }
  462. #endif
  463. #ifndef OPENSSL_NO_DH
  464. int EVP_PKEY_set1_DH(EVP_PKEY *pkey, DH *key)
  465. {
  466. int ret = EVP_PKEY_assign_DH(pkey, key);
  467. if (ret)
  468. DH_up_ref(key);
  469. return ret;
  470. }
  471. DH *EVP_PKEY_get0_DH(EVP_PKEY *pkey)
  472. {
  473. if (pkey->type != EVP_PKEY_DH && pkey->type != EVP_PKEY_DHX) {
  474. EVPerr(EVP_F_EVP_PKEY_GET0_DH, EVP_R_EXPECTING_A_DH_KEY);
  475. return NULL;
  476. }
  477. return pkey->pkey.dh;
  478. }
  479. DH *EVP_PKEY_get1_DH(EVP_PKEY *pkey)
  480. {
  481. DH *ret = EVP_PKEY_get0_DH(pkey);
  482. if (ret != NULL)
  483. DH_up_ref(ret);
  484. return ret;
  485. }
  486. #endif
  487. int EVP_PKEY_type(int type)
  488. {
  489. int ret;
  490. const EVP_PKEY_ASN1_METHOD *ameth;
  491. ENGINE *e;
  492. ameth = EVP_PKEY_asn1_find(&e, type);
  493. if (ameth)
  494. ret = ameth->pkey_id;
  495. else
  496. ret = NID_undef;
  497. #ifndef OPENSSL_NO_ENGINE
  498. ENGINE_finish(e);
  499. #endif
  500. return ret;
  501. }
  502. int EVP_PKEY_id(const EVP_PKEY *pkey)
  503. {
  504. return pkey->type;
  505. }
  506. int EVP_PKEY_base_id(const EVP_PKEY *pkey)
  507. {
  508. return EVP_PKEY_type(pkey->type);
  509. }
  510. void EVP_PKEY_free(EVP_PKEY *x)
  511. {
  512. int i;
  513. if (x == NULL)
  514. return;
  515. CRYPTO_DOWN_REF(&x->references, &i, x->lock);
  516. REF_PRINT_COUNT("EVP_PKEY", x);
  517. if (i > 0)
  518. return;
  519. REF_ASSERT_ISNT(i < 0);
  520. EVP_PKEY_free_it(x);
  521. CRYPTO_THREAD_lock_free(x->lock);
  522. sk_X509_ATTRIBUTE_pop_free(x->attributes, X509_ATTRIBUTE_free);
  523. OPENSSL_free(x);
  524. }
  525. static void EVP_PKEY_free_it(EVP_PKEY *x)
  526. {
  527. /* internal function; x is never NULL */
  528. if (x->ameth && x->ameth->pkey_free) {
  529. x->ameth->pkey_free(x);
  530. x->pkey.ptr = NULL;
  531. }
  532. #ifndef OPENSSL_NO_ENGINE
  533. ENGINE_finish(x->engine);
  534. x->engine = NULL;
  535. ENGINE_finish(x->pmeth_engine);
  536. x->pmeth_engine = NULL;
  537. #endif
  538. }
  539. static int unsup_alg(BIO *out, const EVP_PKEY *pkey, int indent,
  540. const char *kstr)
  541. {
  542. BIO_indent(out, indent, 128);
  543. BIO_printf(out, "%s algorithm \"%s\" unsupported\n",
  544. kstr, OBJ_nid2ln(pkey->type));
  545. return 1;
  546. }
  547. int EVP_PKEY_print_public(BIO *out, const EVP_PKEY *pkey,
  548. int indent, ASN1_PCTX *pctx)
  549. {
  550. if (pkey->ameth && pkey->ameth->pub_print)
  551. return pkey->ameth->pub_print(out, pkey, indent, pctx);
  552. return unsup_alg(out, pkey, indent, "Public Key");
  553. }
  554. int EVP_PKEY_print_private(BIO *out, const EVP_PKEY *pkey,
  555. int indent, ASN1_PCTX *pctx)
  556. {
  557. if (pkey->ameth && pkey->ameth->priv_print)
  558. return pkey->ameth->priv_print(out, pkey, indent, pctx);
  559. return unsup_alg(out, pkey, indent, "Private Key");
  560. }
  561. int EVP_PKEY_print_params(BIO *out, const EVP_PKEY *pkey,
  562. int indent, ASN1_PCTX *pctx)
  563. {
  564. if (pkey->ameth && pkey->ameth->param_print)
  565. return pkey->ameth->param_print(out, pkey, indent, pctx);
  566. return unsup_alg(out, pkey, indent, "Parameters");
  567. }
  568. static int evp_pkey_asn1_ctrl(EVP_PKEY *pkey, int op, int arg1, void *arg2)
  569. {
  570. if (pkey->ameth == NULL || pkey->ameth->pkey_ctrl == NULL)
  571. return -2;
  572. return pkey->ameth->pkey_ctrl(pkey, op, arg1, arg2);
  573. }
  574. int EVP_PKEY_get_default_digest_nid(EVP_PKEY *pkey, int *pnid)
  575. {
  576. return evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_DEFAULT_MD_NID, 0, pnid);
  577. }
  578. int EVP_PKEY_supports_digest_nid(EVP_PKEY *pkey, int nid)
  579. {
  580. int rv, default_nid;
  581. rv = evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_SUPPORTS_MD_NID, nid, NULL);
  582. if (rv == -2) {
  583. /*
  584. * If there is a mandatory default digest and this isn't it, then
  585. * the answer is 'no'.
  586. */
  587. rv = EVP_PKEY_get_default_digest_nid(pkey, &default_nid);
  588. if (rv == 2)
  589. return (nid == default_nid);
  590. /* zero is an error from EVP_PKEY_get_default_digest_nid() */
  591. if (rv == 0)
  592. return -1;
  593. }
  594. return rv;
  595. }
  596. int EVP_PKEY_set1_tls_encodedpoint(EVP_PKEY *pkey,
  597. const unsigned char *pt, size_t ptlen)
  598. {
  599. if (ptlen > INT_MAX)
  600. return 0;
  601. if (evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_SET1_TLS_ENCPT, ptlen,
  602. (void *)pt) <= 0)
  603. return 0;
  604. return 1;
  605. }
  606. size_t EVP_PKEY_get1_tls_encodedpoint(EVP_PKEY *pkey, unsigned char **ppt)
  607. {
  608. int rv;
  609. rv = evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_GET1_TLS_ENCPT, 0, ppt);
  610. if (rv <= 0)
  611. return 0;
  612. return rv;
  613. }