p_lib.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  1. /*
  2. * Copyright 1995-2019 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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 "crypto/asn1.h"
  23. #include "crypto/evp.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(const 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 != NULL && 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. CMAC_CTX *cmctx = CMAC_CTX_new();
  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 (!CMAC_Init(cmctx, priv, len, cipher, e)) {
  283. EVPerr(EVP_F_EVP_PKEY_NEW_CMAC_KEY, EVP_R_KEY_SETUP_FAILED);
  284. goto err;
  285. }
  286. ret->pkey.ptr = cmctx;
  287. return ret;
  288. err:
  289. EVP_PKEY_free(ret);
  290. CMAC_CTX_free(cmctx);
  291. return NULL;
  292. #else
  293. EVPerr(EVP_F_EVP_PKEY_NEW_CMAC_KEY,
  294. EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  295. return NULL;
  296. #endif
  297. }
  298. int EVP_PKEY_set_type(EVP_PKEY *pkey, int type)
  299. {
  300. return pkey_set_type(pkey, NULL, type, NULL, -1);
  301. }
  302. int EVP_PKEY_set_type_str(EVP_PKEY *pkey, const char *str, int len)
  303. {
  304. return pkey_set_type(pkey, NULL, EVP_PKEY_NONE, str, len);
  305. }
  306. int EVP_PKEY_set_alias_type(EVP_PKEY *pkey, int type)
  307. {
  308. if (pkey->type == type) {
  309. return 1; /* it already is that type */
  310. }
  311. /*
  312. * The application is requesting to alias this to a different pkey type,
  313. * but not one that resolves to the base type.
  314. */
  315. if (EVP_PKEY_type(type) != EVP_PKEY_base_id(pkey)) {
  316. EVPerr(EVP_F_EVP_PKEY_SET_ALIAS_TYPE, EVP_R_UNSUPPORTED_ALGORITHM);
  317. return 0;
  318. }
  319. pkey->type = type;
  320. return 1;
  321. }
  322. #ifndef OPENSSL_NO_ENGINE
  323. int EVP_PKEY_set1_engine(EVP_PKEY *pkey, ENGINE *e)
  324. {
  325. if (e != NULL) {
  326. if (!ENGINE_init(e)) {
  327. EVPerr(EVP_F_EVP_PKEY_SET1_ENGINE, ERR_R_ENGINE_LIB);
  328. return 0;
  329. }
  330. if (ENGINE_get_pkey_meth(e, pkey->type) == NULL) {
  331. ENGINE_finish(e);
  332. EVPerr(EVP_F_EVP_PKEY_SET1_ENGINE, EVP_R_UNSUPPORTED_ALGORITHM);
  333. return 0;
  334. }
  335. }
  336. ENGINE_finish(pkey->pmeth_engine);
  337. pkey->pmeth_engine = e;
  338. return 1;
  339. }
  340. ENGINE *EVP_PKEY_get0_engine(const EVP_PKEY *pkey)
  341. {
  342. return pkey->engine;
  343. }
  344. #endif
  345. int EVP_PKEY_assign(EVP_PKEY *pkey, int type, void *key)
  346. {
  347. if (pkey == NULL || !EVP_PKEY_set_type(pkey, type))
  348. return 0;
  349. pkey->pkey.ptr = key;
  350. return (key != NULL);
  351. }
  352. void *EVP_PKEY_get0(const EVP_PKEY *pkey)
  353. {
  354. return pkey->pkey.ptr;
  355. }
  356. const unsigned char *EVP_PKEY_get0_hmac(const EVP_PKEY *pkey, size_t *len)
  357. {
  358. ASN1_OCTET_STRING *os = NULL;
  359. if (pkey->type != EVP_PKEY_HMAC) {
  360. EVPerr(EVP_F_EVP_PKEY_GET0_HMAC, EVP_R_EXPECTING_AN_HMAC_KEY);
  361. return NULL;
  362. }
  363. os = EVP_PKEY_get0(pkey);
  364. *len = os->length;
  365. return os->data;
  366. }
  367. #ifndef OPENSSL_NO_POLY1305
  368. const unsigned char *EVP_PKEY_get0_poly1305(const EVP_PKEY *pkey, size_t *len)
  369. {
  370. ASN1_OCTET_STRING *os = NULL;
  371. if (pkey->type != EVP_PKEY_POLY1305) {
  372. EVPerr(EVP_F_EVP_PKEY_GET0_POLY1305, EVP_R_EXPECTING_A_POLY1305_KEY);
  373. return NULL;
  374. }
  375. os = EVP_PKEY_get0(pkey);
  376. *len = os->length;
  377. return os->data;
  378. }
  379. #endif
  380. #ifndef OPENSSL_NO_SIPHASH
  381. const unsigned char *EVP_PKEY_get0_siphash(const EVP_PKEY *pkey, size_t *len)
  382. {
  383. ASN1_OCTET_STRING *os = NULL;
  384. if (pkey->type != EVP_PKEY_SIPHASH) {
  385. EVPerr(EVP_F_EVP_PKEY_GET0_SIPHASH, EVP_R_EXPECTING_A_SIPHASH_KEY);
  386. return NULL;
  387. }
  388. os = EVP_PKEY_get0(pkey);
  389. *len = os->length;
  390. return os->data;
  391. }
  392. #endif
  393. #ifndef OPENSSL_NO_RSA
  394. int EVP_PKEY_set1_RSA(EVP_PKEY *pkey, RSA *key)
  395. {
  396. int ret = EVP_PKEY_assign_RSA(pkey, key);
  397. if (ret)
  398. RSA_up_ref(key);
  399. return ret;
  400. }
  401. RSA *EVP_PKEY_get0_RSA(EVP_PKEY *pkey)
  402. {
  403. if (pkey->type != EVP_PKEY_RSA && pkey->type != EVP_PKEY_RSA_PSS) {
  404. EVPerr(EVP_F_EVP_PKEY_GET0_RSA, EVP_R_EXPECTING_AN_RSA_KEY);
  405. return NULL;
  406. }
  407. return pkey->pkey.rsa;
  408. }
  409. RSA *EVP_PKEY_get1_RSA(EVP_PKEY *pkey)
  410. {
  411. RSA *ret = EVP_PKEY_get0_RSA(pkey);
  412. if (ret != NULL)
  413. RSA_up_ref(ret);
  414. return ret;
  415. }
  416. #endif
  417. #ifndef OPENSSL_NO_DSA
  418. int EVP_PKEY_set1_DSA(EVP_PKEY *pkey, DSA *key)
  419. {
  420. int ret = EVP_PKEY_assign_DSA(pkey, key);
  421. if (ret)
  422. DSA_up_ref(key);
  423. return ret;
  424. }
  425. DSA *EVP_PKEY_get0_DSA(EVP_PKEY *pkey)
  426. {
  427. if (pkey->type != EVP_PKEY_DSA) {
  428. EVPerr(EVP_F_EVP_PKEY_GET0_DSA, EVP_R_EXPECTING_A_DSA_KEY);
  429. return NULL;
  430. }
  431. return pkey->pkey.dsa;
  432. }
  433. DSA *EVP_PKEY_get1_DSA(EVP_PKEY *pkey)
  434. {
  435. DSA *ret = EVP_PKEY_get0_DSA(pkey);
  436. if (ret != NULL)
  437. DSA_up_ref(ret);
  438. return ret;
  439. }
  440. #endif
  441. #ifndef OPENSSL_NO_EC
  442. int EVP_PKEY_set1_EC_KEY(EVP_PKEY *pkey, EC_KEY *key)
  443. {
  444. int ret = EVP_PKEY_assign_EC_KEY(pkey, key);
  445. if (ret)
  446. EC_KEY_up_ref(key);
  447. return ret;
  448. }
  449. EC_KEY *EVP_PKEY_get0_EC_KEY(EVP_PKEY *pkey)
  450. {
  451. if (pkey->type != EVP_PKEY_EC) {
  452. EVPerr(EVP_F_EVP_PKEY_GET0_EC_KEY, EVP_R_EXPECTING_A_EC_KEY);
  453. return NULL;
  454. }
  455. return pkey->pkey.ec;
  456. }
  457. EC_KEY *EVP_PKEY_get1_EC_KEY(EVP_PKEY *pkey)
  458. {
  459. EC_KEY *ret = EVP_PKEY_get0_EC_KEY(pkey);
  460. if (ret != NULL)
  461. EC_KEY_up_ref(ret);
  462. return ret;
  463. }
  464. #endif
  465. #ifndef OPENSSL_NO_DH
  466. int EVP_PKEY_set1_DH(EVP_PKEY *pkey, DH *key)
  467. {
  468. int type = DH_get0_q(key) == NULL ? EVP_PKEY_DH : EVP_PKEY_DHX;
  469. int ret = EVP_PKEY_assign(pkey, type, key);
  470. if (ret)
  471. DH_up_ref(key);
  472. return ret;
  473. }
  474. DH *EVP_PKEY_get0_DH(EVP_PKEY *pkey)
  475. {
  476. if (pkey->type != EVP_PKEY_DH && pkey->type != EVP_PKEY_DHX) {
  477. EVPerr(EVP_F_EVP_PKEY_GET0_DH, EVP_R_EXPECTING_A_DH_KEY);
  478. return NULL;
  479. }
  480. return pkey->pkey.dh;
  481. }
  482. DH *EVP_PKEY_get1_DH(EVP_PKEY *pkey)
  483. {
  484. DH *ret = EVP_PKEY_get0_DH(pkey);
  485. if (ret != NULL)
  486. DH_up_ref(ret);
  487. return ret;
  488. }
  489. #endif
  490. int EVP_PKEY_type(int type)
  491. {
  492. int ret;
  493. const EVP_PKEY_ASN1_METHOD *ameth;
  494. ENGINE *e;
  495. ameth = EVP_PKEY_asn1_find(&e, type);
  496. if (ameth)
  497. ret = ameth->pkey_id;
  498. else
  499. ret = NID_undef;
  500. #ifndef OPENSSL_NO_ENGINE
  501. ENGINE_finish(e);
  502. #endif
  503. return ret;
  504. }
  505. int EVP_PKEY_id(const EVP_PKEY *pkey)
  506. {
  507. return pkey->type;
  508. }
  509. int EVP_PKEY_base_id(const EVP_PKEY *pkey)
  510. {
  511. return EVP_PKEY_type(pkey->type);
  512. }
  513. void EVP_PKEY_free(EVP_PKEY *x)
  514. {
  515. int i;
  516. if (x == NULL)
  517. return;
  518. CRYPTO_DOWN_REF(&x->references, &i, x->lock);
  519. REF_PRINT_COUNT("EVP_PKEY", x);
  520. if (i > 0)
  521. return;
  522. REF_ASSERT_ISNT(i < 0);
  523. EVP_PKEY_free_it(x);
  524. CRYPTO_THREAD_lock_free(x->lock);
  525. sk_X509_ATTRIBUTE_pop_free(x->attributes, X509_ATTRIBUTE_free);
  526. OPENSSL_free(x);
  527. }
  528. static void EVP_PKEY_free_it(EVP_PKEY *x)
  529. {
  530. /* internal function; x is never NULL */
  531. if (x->ameth && x->ameth->pkey_free) {
  532. x->ameth->pkey_free(x);
  533. x->pkey.ptr = NULL;
  534. }
  535. #ifndef OPENSSL_NO_ENGINE
  536. ENGINE_finish(x->engine);
  537. x->engine = NULL;
  538. ENGINE_finish(x->pmeth_engine);
  539. x->pmeth_engine = NULL;
  540. #endif
  541. }
  542. static int unsup_alg(BIO *out, const EVP_PKEY *pkey, int indent,
  543. const char *kstr)
  544. {
  545. BIO_indent(out, indent, 128);
  546. BIO_printf(out, "%s algorithm \"%s\" unsupported\n",
  547. kstr, OBJ_nid2ln(pkey->type));
  548. return 1;
  549. }
  550. int EVP_PKEY_print_public(BIO *out, const EVP_PKEY *pkey,
  551. int indent, ASN1_PCTX *pctx)
  552. {
  553. if (pkey->ameth && pkey->ameth->pub_print)
  554. return pkey->ameth->pub_print(out, pkey, indent, pctx);
  555. return unsup_alg(out, pkey, indent, "Public Key");
  556. }
  557. int EVP_PKEY_print_private(BIO *out, const EVP_PKEY *pkey,
  558. int indent, ASN1_PCTX *pctx)
  559. {
  560. if (pkey->ameth && pkey->ameth->priv_print)
  561. return pkey->ameth->priv_print(out, pkey, indent, pctx);
  562. return unsup_alg(out, pkey, indent, "Private Key");
  563. }
  564. int EVP_PKEY_print_params(BIO *out, const EVP_PKEY *pkey,
  565. int indent, ASN1_PCTX *pctx)
  566. {
  567. if (pkey->ameth && pkey->ameth->param_print)
  568. return pkey->ameth->param_print(out, pkey, indent, pctx);
  569. return unsup_alg(out, pkey, indent, "Parameters");
  570. }
  571. static int evp_pkey_asn1_ctrl(EVP_PKEY *pkey, int op, int arg1, void *arg2)
  572. {
  573. if (pkey->ameth == NULL || pkey->ameth->pkey_ctrl == NULL)
  574. return -2;
  575. return pkey->ameth->pkey_ctrl(pkey, op, arg1, arg2);
  576. }
  577. int EVP_PKEY_get_default_digest_nid(EVP_PKEY *pkey, int *pnid)
  578. {
  579. return evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_DEFAULT_MD_NID, 0, pnid);
  580. }
  581. int EVP_PKEY_set1_tls_encodedpoint(EVP_PKEY *pkey,
  582. const unsigned char *pt, size_t ptlen)
  583. {
  584. if (ptlen > INT_MAX)
  585. return 0;
  586. if (evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_SET1_TLS_ENCPT, ptlen,
  587. (void *)pt) <= 0)
  588. return 0;
  589. return 1;
  590. }
  591. size_t EVP_PKEY_get1_tls_encodedpoint(EVP_PKEY *pkey, unsigned char **ppt)
  592. {
  593. int rv;
  594. rv = evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_GET1_TLS_ENCPT, 0, ppt);
  595. if (rv <= 0)
  596. return 0;
  597. return rv;
  598. }