p_lib.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  1. /*
  2. * Copyright 1995-2021 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->type = pkey->ameth->pkey_id;
  189. pkey->save_type = type;
  190. # ifndef OPENSSL_NO_ENGINE
  191. if (eptr == NULL && e != NULL && !ENGINE_init(e)) {
  192. EVPerr(EVP_F_PKEY_SET_TYPE, EVP_R_INITIALIZATION_ERROR);
  193. return 0;
  194. }
  195. # endif
  196. pkey->engine = e;
  197. }
  198. return 1;
  199. }
  200. EVP_PKEY *EVP_PKEY_new_raw_private_key(int type, ENGINE *e,
  201. const unsigned char *priv,
  202. size_t len)
  203. {
  204. EVP_PKEY *ret = EVP_PKEY_new();
  205. if (ret == NULL
  206. || !pkey_set_type(ret, e, type, NULL, -1)) {
  207. /* EVPerr already called */
  208. goto err;
  209. }
  210. if (ret->ameth->set_priv_key == NULL) {
  211. EVPerr(EVP_F_EVP_PKEY_NEW_RAW_PRIVATE_KEY,
  212. EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  213. goto err;
  214. }
  215. if (!ret->ameth->set_priv_key(ret, priv, len)) {
  216. EVPerr(EVP_F_EVP_PKEY_NEW_RAW_PRIVATE_KEY, EVP_R_KEY_SETUP_FAILED);
  217. goto err;
  218. }
  219. return ret;
  220. err:
  221. EVP_PKEY_free(ret);
  222. return NULL;
  223. }
  224. EVP_PKEY *EVP_PKEY_new_raw_public_key(int type, ENGINE *e,
  225. const unsigned char *pub,
  226. size_t len)
  227. {
  228. EVP_PKEY *ret = EVP_PKEY_new();
  229. if (ret == NULL
  230. || !pkey_set_type(ret, e, type, NULL, -1)) {
  231. /* EVPerr already called */
  232. goto err;
  233. }
  234. if (ret->ameth->set_pub_key == NULL) {
  235. EVPerr(EVP_F_EVP_PKEY_NEW_RAW_PUBLIC_KEY,
  236. EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  237. goto err;
  238. }
  239. if (!ret->ameth->set_pub_key(ret, pub, len)) {
  240. EVPerr(EVP_F_EVP_PKEY_NEW_RAW_PUBLIC_KEY, EVP_R_KEY_SETUP_FAILED);
  241. goto err;
  242. }
  243. return ret;
  244. err:
  245. EVP_PKEY_free(ret);
  246. return NULL;
  247. }
  248. int EVP_PKEY_get_raw_private_key(const EVP_PKEY *pkey, unsigned char *priv,
  249. size_t *len)
  250. {
  251. if (pkey->ameth->get_priv_key == NULL) {
  252. EVPerr(EVP_F_EVP_PKEY_GET_RAW_PRIVATE_KEY,
  253. EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  254. return 0;
  255. }
  256. if (!pkey->ameth->get_priv_key(pkey, priv, len)) {
  257. EVPerr(EVP_F_EVP_PKEY_GET_RAW_PRIVATE_KEY, EVP_R_GET_RAW_KEY_FAILED);
  258. return 0;
  259. }
  260. return 1;
  261. }
  262. int EVP_PKEY_get_raw_public_key(const EVP_PKEY *pkey, unsigned char *pub,
  263. size_t *len)
  264. {
  265. if (pkey->ameth->get_pub_key == NULL) {
  266. EVPerr(EVP_F_EVP_PKEY_GET_RAW_PUBLIC_KEY,
  267. EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  268. return 0;
  269. }
  270. if (!pkey->ameth->get_pub_key(pkey, pub, len)) {
  271. EVPerr(EVP_F_EVP_PKEY_GET_RAW_PUBLIC_KEY, EVP_R_GET_RAW_KEY_FAILED);
  272. return 0;
  273. }
  274. return 1;
  275. }
  276. EVP_PKEY *EVP_PKEY_new_CMAC_key(ENGINE *e, const unsigned char *priv,
  277. size_t len, const EVP_CIPHER *cipher)
  278. {
  279. #ifndef OPENSSL_NO_CMAC
  280. EVP_PKEY *ret = EVP_PKEY_new();
  281. CMAC_CTX *cmctx = CMAC_CTX_new();
  282. if (ret == NULL
  283. || cmctx == NULL
  284. || !pkey_set_type(ret, e, EVP_PKEY_CMAC, NULL, -1)) {
  285. /* EVPerr already called */
  286. goto err;
  287. }
  288. if (!CMAC_Init(cmctx, priv, len, cipher, e)) {
  289. EVPerr(EVP_F_EVP_PKEY_NEW_CMAC_KEY, EVP_R_KEY_SETUP_FAILED);
  290. goto err;
  291. }
  292. ret->pkey.ptr = cmctx;
  293. return ret;
  294. err:
  295. EVP_PKEY_free(ret);
  296. CMAC_CTX_free(cmctx);
  297. return NULL;
  298. #else
  299. EVPerr(EVP_F_EVP_PKEY_NEW_CMAC_KEY,
  300. EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  301. return NULL;
  302. #endif
  303. }
  304. int EVP_PKEY_set_type(EVP_PKEY *pkey, int type)
  305. {
  306. return pkey_set_type(pkey, NULL, type, NULL, -1);
  307. }
  308. int EVP_PKEY_set_type_str(EVP_PKEY *pkey, const char *str, int len)
  309. {
  310. return pkey_set_type(pkey, NULL, EVP_PKEY_NONE, str, len);
  311. }
  312. int EVP_PKEY_set_alias_type(EVP_PKEY *pkey, int type)
  313. {
  314. if (pkey->type == type) {
  315. return 1; /* it already is that type */
  316. }
  317. /*
  318. * The application is requesting to alias this to a different pkey type,
  319. * but not one that resolves to the base type.
  320. */
  321. if (EVP_PKEY_type(type) != EVP_PKEY_base_id(pkey)) {
  322. EVPerr(EVP_F_EVP_PKEY_SET_ALIAS_TYPE, EVP_R_UNSUPPORTED_ALGORITHM);
  323. return 0;
  324. }
  325. pkey->type = type;
  326. return 1;
  327. }
  328. #ifndef OPENSSL_NO_ENGINE
  329. int EVP_PKEY_set1_engine(EVP_PKEY *pkey, ENGINE *e)
  330. {
  331. if (e != NULL) {
  332. if (!ENGINE_init(e)) {
  333. EVPerr(EVP_F_EVP_PKEY_SET1_ENGINE, ERR_R_ENGINE_LIB);
  334. return 0;
  335. }
  336. if (ENGINE_get_pkey_meth(e, pkey->type) == NULL) {
  337. ENGINE_finish(e);
  338. EVPerr(EVP_F_EVP_PKEY_SET1_ENGINE, EVP_R_UNSUPPORTED_ALGORITHM);
  339. return 0;
  340. }
  341. }
  342. ENGINE_finish(pkey->pmeth_engine);
  343. pkey->pmeth_engine = e;
  344. return 1;
  345. }
  346. ENGINE *EVP_PKEY_get0_engine(const EVP_PKEY *pkey)
  347. {
  348. return pkey->engine;
  349. }
  350. #endif
  351. int EVP_PKEY_assign(EVP_PKEY *pkey, int type, void *key)
  352. {
  353. if (pkey == NULL || !EVP_PKEY_set_type(pkey, type))
  354. return 0;
  355. pkey->pkey.ptr = key;
  356. return (key != NULL);
  357. }
  358. void *EVP_PKEY_get0(const EVP_PKEY *pkey)
  359. {
  360. return pkey->pkey.ptr;
  361. }
  362. const unsigned char *EVP_PKEY_get0_hmac(const EVP_PKEY *pkey, size_t *len)
  363. {
  364. ASN1_OCTET_STRING *os = NULL;
  365. if (pkey->type != EVP_PKEY_HMAC) {
  366. EVPerr(EVP_F_EVP_PKEY_GET0_HMAC, EVP_R_EXPECTING_AN_HMAC_KEY);
  367. return NULL;
  368. }
  369. os = EVP_PKEY_get0(pkey);
  370. *len = os->length;
  371. return os->data;
  372. }
  373. #ifndef OPENSSL_NO_POLY1305
  374. const unsigned char *EVP_PKEY_get0_poly1305(const EVP_PKEY *pkey, size_t *len)
  375. {
  376. ASN1_OCTET_STRING *os = NULL;
  377. if (pkey->type != EVP_PKEY_POLY1305) {
  378. EVPerr(EVP_F_EVP_PKEY_GET0_POLY1305, EVP_R_EXPECTING_A_POLY1305_KEY);
  379. return NULL;
  380. }
  381. os = EVP_PKEY_get0(pkey);
  382. *len = os->length;
  383. return os->data;
  384. }
  385. #endif
  386. #ifndef OPENSSL_NO_SIPHASH
  387. const unsigned char *EVP_PKEY_get0_siphash(const EVP_PKEY *pkey, size_t *len)
  388. {
  389. ASN1_OCTET_STRING *os = NULL;
  390. if (pkey->type != EVP_PKEY_SIPHASH) {
  391. EVPerr(EVP_F_EVP_PKEY_GET0_SIPHASH, EVP_R_EXPECTING_A_SIPHASH_KEY);
  392. return NULL;
  393. }
  394. os = EVP_PKEY_get0(pkey);
  395. *len = os->length;
  396. return os->data;
  397. }
  398. #endif
  399. #ifndef OPENSSL_NO_RSA
  400. int EVP_PKEY_set1_RSA(EVP_PKEY *pkey, RSA *key)
  401. {
  402. int ret = EVP_PKEY_assign_RSA(pkey, key);
  403. if (ret)
  404. RSA_up_ref(key);
  405. return ret;
  406. }
  407. RSA *EVP_PKEY_get0_RSA(EVP_PKEY *pkey)
  408. {
  409. if (pkey->type != EVP_PKEY_RSA && pkey->type != EVP_PKEY_RSA_PSS) {
  410. EVPerr(EVP_F_EVP_PKEY_GET0_RSA, EVP_R_EXPECTING_AN_RSA_KEY);
  411. return NULL;
  412. }
  413. return pkey->pkey.rsa;
  414. }
  415. RSA *EVP_PKEY_get1_RSA(EVP_PKEY *pkey)
  416. {
  417. RSA *ret = EVP_PKEY_get0_RSA(pkey);
  418. if (ret != NULL)
  419. RSA_up_ref(ret);
  420. return ret;
  421. }
  422. #endif
  423. #ifndef OPENSSL_NO_DSA
  424. int EVP_PKEY_set1_DSA(EVP_PKEY *pkey, DSA *key)
  425. {
  426. int ret = EVP_PKEY_assign_DSA(pkey, key);
  427. if (ret)
  428. DSA_up_ref(key);
  429. return ret;
  430. }
  431. DSA *EVP_PKEY_get0_DSA(EVP_PKEY *pkey)
  432. {
  433. if (pkey->type != EVP_PKEY_DSA) {
  434. EVPerr(EVP_F_EVP_PKEY_GET0_DSA, EVP_R_EXPECTING_A_DSA_KEY);
  435. return NULL;
  436. }
  437. return pkey->pkey.dsa;
  438. }
  439. DSA *EVP_PKEY_get1_DSA(EVP_PKEY *pkey)
  440. {
  441. DSA *ret = EVP_PKEY_get0_DSA(pkey);
  442. if (ret != NULL)
  443. DSA_up_ref(ret);
  444. return ret;
  445. }
  446. #endif
  447. #ifndef OPENSSL_NO_EC
  448. int EVP_PKEY_set1_EC_KEY(EVP_PKEY *pkey, EC_KEY *key)
  449. {
  450. int ret = EVP_PKEY_assign_EC_KEY(pkey, key);
  451. if (ret)
  452. EC_KEY_up_ref(key);
  453. return ret;
  454. }
  455. EC_KEY *EVP_PKEY_get0_EC_KEY(EVP_PKEY *pkey)
  456. {
  457. if (EVP_PKEY_base_id(pkey) != EVP_PKEY_EC) {
  458. EVPerr(EVP_F_EVP_PKEY_GET0_EC_KEY, EVP_R_EXPECTING_A_EC_KEY);
  459. return NULL;
  460. }
  461. return pkey->pkey.ec;
  462. }
  463. EC_KEY *EVP_PKEY_get1_EC_KEY(EVP_PKEY *pkey)
  464. {
  465. EC_KEY *ret = EVP_PKEY_get0_EC_KEY(pkey);
  466. if (ret != NULL)
  467. EC_KEY_up_ref(ret);
  468. return ret;
  469. }
  470. #endif
  471. #ifndef OPENSSL_NO_DH
  472. int EVP_PKEY_set1_DH(EVP_PKEY *pkey, DH *key)
  473. {
  474. int type = DH_get0_q(key) == NULL ? EVP_PKEY_DH : EVP_PKEY_DHX;
  475. int ret = EVP_PKEY_assign(pkey, type, key);
  476. if (ret)
  477. DH_up_ref(key);
  478. return ret;
  479. }
  480. DH *EVP_PKEY_get0_DH(EVP_PKEY *pkey)
  481. {
  482. if (pkey->type != EVP_PKEY_DH && pkey->type != EVP_PKEY_DHX) {
  483. EVPerr(EVP_F_EVP_PKEY_GET0_DH, EVP_R_EXPECTING_A_DH_KEY);
  484. return NULL;
  485. }
  486. return pkey->pkey.dh;
  487. }
  488. DH *EVP_PKEY_get1_DH(EVP_PKEY *pkey)
  489. {
  490. DH *ret = EVP_PKEY_get0_DH(pkey);
  491. if (ret != NULL)
  492. DH_up_ref(ret);
  493. return ret;
  494. }
  495. #endif
  496. int EVP_PKEY_type(int type)
  497. {
  498. int ret;
  499. const EVP_PKEY_ASN1_METHOD *ameth;
  500. ENGINE *e;
  501. ameth = EVP_PKEY_asn1_find(&e, type);
  502. if (ameth)
  503. ret = ameth->pkey_id;
  504. else
  505. ret = NID_undef;
  506. #ifndef OPENSSL_NO_ENGINE
  507. ENGINE_finish(e);
  508. #endif
  509. return ret;
  510. }
  511. int EVP_PKEY_id(const EVP_PKEY *pkey)
  512. {
  513. return pkey->type;
  514. }
  515. int EVP_PKEY_base_id(const EVP_PKEY *pkey)
  516. {
  517. return EVP_PKEY_type(pkey->type);
  518. }
  519. void EVP_PKEY_free(EVP_PKEY *x)
  520. {
  521. int i;
  522. if (x == NULL)
  523. return;
  524. CRYPTO_DOWN_REF(&x->references, &i, x->lock);
  525. REF_PRINT_COUNT("EVP_PKEY", x);
  526. if (i > 0)
  527. return;
  528. REF_ASSERT_ISNT(i < 0);
  529. EVP_PKEY_free_it(x);
  530. CRYPTO_THREAD_lock_free(x->lock);
  531. sk_X509_ATTRIBUTE_pop_free(x->attributes, X509_ATTRIBUTE_free);
  532. OPENSSL_free(x);
  533. }
  534. static void EVP_PKEY_free_it(EVP_PKEY *x)
  535. {
  536. /* internal function; x is never NULL */
  537. if (x->ameth && x->ameth->pkey_free) {
  538. x->ameth->pkey_free(x);
  539. x->pkey.ptr = NULL;
  540. }
  541. #ifndef OPENSSL_NO_ENGINE
  542. ENGINE_finish(x->engine);
  543. x->engine = NULL;
  544. ENGINE_finish(x->pmeth_engine);
  545. x->pmeth_engine = NULL;
  546. #endif
  547. }
  548. static int unsup_alg(BIO *out, const EVP_PKEY *pkey, int indent,
  549. const char *kstr)
  550. {
  551. BIO_indent(out, indent, 128);
  552. BIO_printf(out, "%s algorithm \"%s\" unsupported\n",
  553. kstr, OBJ_nid2ln(pkey->type));
  554. return 1;
  555. }
  556. int EVP_PKEY_print_public(BIO *out, const EVP_PKEY *pkey,
  557. int indent, ASN1_PCTX *pctx)
  558. {
  559. if (pkey->ameth && pkey->ameth->pub_print)
  560. return pkey->ameth->pub_print(out, pkey, indent, pctx);
  561. return unsup_alg(out, pkey, indent, "Public Key");
  562. }
  563. int EVP_PKEY_print_private(BIO *out, const EVP_PKEY *pkey,
  564. int indent, ASN1_PCTX *pctx)
  565. {
  566. if (pkey->ameth && pkey->ameth->priv_print)
  567. return pkey->ameth->priv_print(out, pkey, indent, pctx);
  568. return unsup_alg(out, pkey, indent, "Private Key");
  569. }
  570. int EVP_PKEY_print_params(BIO *out, const EVP_PKEY *pkey,
  571. int indent, ASN1_PCTX *pctx)
  572. {
  573. if (pkey->ameth && pkey->ameth->param_print)
  574. return pkey->ameth->param_print(out, pkey, indent, pctx);
  575. return unsup_alg(out, pkey, indent, "Parameters");
  576. }
  577. static int evp_pkey_asn1_ctrl(EVP_PKEY *pkey, int op, int arg1, void *arg2)
  578. {
  579. if (pkey->ameth == NULL || pkey->ameth->pkey_ctrl == NULL)
  580. return -2;
  581. return pkey->ameth->pkey_ctrl(pkey, op, arg1, arg2);
  582. }
  583. int EVP_PKEY_get_default_digest_nid(EVP_PKEY *pkey, int *pnid)
  584. {
  585. return evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_DEFAULT_MD_NID, 0, pnid);
  586. }
  587. int EVP_PKEY_set1_tls_encodedpoint(EVP_PKEY *pkey,
  588. const unsigned char *pt, size_t ptlen)
  589. {
  590. if (ptlen > INT_MAX)
  591. return 0;
  592. if (evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_SET1_TLS_ENCPT, ptlen,
  593. (void *)pt) <= 0)
  594. return 0;
  595. return 1;
  596. }
  597. size_t EVP_PKEY_get1_tls_encodedpoint(EVP_PKEY *pkey, unsigned char **ppt)
  598. {
  599. int rv;
  600. rv = evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_GET1_TLS_ENCPT, 0, ppt);
  601. if (rv <= 0)
  602. return 0;
  603. return rv;
  604. }