p_lib.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  1. /*
  2. * Copyright 1995-2018 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 "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. 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. #endif
  341. int EVP_PKEY_assign(EVP_PKEY *pkey, int type, void *key)
  342. {
  343. if (pkey == NULL || !EVP_PKEY_set_type(pkey, type))
  344. return 0;
  345. pkey->pkey.ptr = key;
  346. return (key != NULL);
  347. }
  348. void *EVP_PKEY_get0(const EVP_PKEY *pkey)
  349. {
  350. return pkey->pkey.ptr;
  351. }
  352. const unsigned char *EVP_PKEY_get0_hmac(const EVP_PKEY *pkey, size_t *len)
  353. {
  354. ASN1_OCTET_STRING *os = NULL;
  355. if (pkey->type != EVP_PKEY_HMAC) {
  356. EVPerr(EVP_F_EVP_PKEY_GET0_HMAC, EVP_R_EXPECTING_AN_HMAC_KEY);
  357. return NULL;
  358. }
  359. os = EVP_PKEY_get0(pkey);
  360. *len = os->length;
  361. return os->data;
  362. }
  363. #ifndef OPENSSL_NO_POLY1305
  364. const unsigned char *EVP_PKEY_get0_poly1305(const EVP_PKEY *pkey, size_t *len)
  365. {
  366. ASN1_OCTET_STRING *os = NULL;
  367. if (pkey->type != EVP_PKEY_POLY1305) {
  368. EVPerr(EVP_F_EVP_PKEY_GET0_POLY1305, EVP_R_EXPECTING_A_POLY1305_KEY);
  369. return NULL;
  370. }
  371. os = EVP_PKEY_get0(pkey);
  372. *len = os->length;
  373. return os->data;
  374. }
  375. #endif
  376. #ifndef OPENSSL_NO_SIPHASH
  377. const unsigned char *EVP_PKEY_get0_siphash(const EVP_PKEY *pkey, size_t *len)
  378. {
  379. ASN1_OCTET_STRING *os = NULL;
  380. if (pkey->type != EVP_PKEY_SIPHASH) {
  381. EVPerr(EVP_F_EVP_PKEY_GET0_SIPHASH, EVP_R_EXPECTING_A_SIPHASH_KEY);
  382. return NULL;
  383. }
  384. os = EVP_PKEY_get0(pkey);
  385. *len = os->length;
  386. return os->data;
  387. }
  388. #endif
  389. #ifndef OPENSSL_NO_RSA
  390. int EVP_PKEY_set1_RSA(EVP_PKEY *pkey, RSA *key)
  391. {
  392. int ret = EVP_PKEY_assign_RSA(pkey, key);
  393. if (ret)
  394. RSA_up_ref(key);
  395. return ret;
  396. }
  397. RSA *EVP_PKEY_get0_RSA(EVP_PKEY *pkey)
  398. {
  399. if (pkey->type != EVP_PKEY_RSA) {
  400. EVPerr(EVP_F_EVP_PKEY_GET0_RSA, EVP_R_EXPECTING_AN_RSA_KEY);
  401. return NULL;
  402. }
  403. return pkey->pkey.rsa;
  404. }
  405. RSA *EVP_PKEY_get1_RSA(EVP_PKEY *pkey)
  406. {
  407. RSA *ret = EVP_PKEY_get0_RSA(pkey);
  408. if (ret != NULL)
  409. RSA_up_ref(ret);
  410. return ret;
  411. }
  412. #endif
  413. #ifndef OPENSSL_NO_DSA
  414. int EVP_PKEY_set1_DSA(EVP_PKEY *pkey, DSA *key)
  415. {
  416. int ret = EVP_PKEY_assign_DSA(pkey, key);
  417. if (ret)
  418. DSA_up_ref(key);
  419. return ret;
  420. }
  421. DSA *EVP_PKEY_get0_DSA(EVP_PKEY *pkey)
  422. {
  423. if (pkey->type != EVP_PKEY_DSA) {
  424. EVPerr(EVP_F_EVP_PKEY_GET0_DSA, EVP_R_EXPECTING_A_DSA_KEY);
  425. return NULL;
  426. }
  427. return pkey->pkey.dsa;
  428. }
  429. DSA *EVP_PKEY_get1_DSA(EVP_PKEY *pkey)
  430. {
  431. DSA *ret = EVP_PKEY_get0_DSA(pkey);
  432. if (ret != NULL)
  433. DSA_up_ref(ret);
  434. return ret;
  435. }
  436. #endif
  437. #ifndef OPENSSL_NO_EC
  438. int EVP_PKEY_set1_EC_KEY(EVP_PKEY *pkey, EC_KEY *key)
  439. {
  440. int ret = EVP_PKEY_assign_EC_KEY(pkey, key);
  441. if (ret)
  442. EC_KEY_up_ref(key);
  443. return ret;
  444. }
  445. EC_KEY *EVP_PKEY_get0_EC_KEY(EVP_PKEY *pkey)
  446. {
  447. if (pkey->type != EVP_PKEY_EC) {
  448. EVPerr(EVP_F_EVP_PKEY_GET0_EC_KEY, EVP_R_EXPECTING_A_EC_KEY);
  449. return NULL;
  450. }
  451. return pkey->pkey.ec;
  452. }
  453. EC_KEY *EVP_PKEY_get1_EC_KEY(EVP_PKEY *pkey)
  454. {
  455. EC_KEY *ret = EVP_PKEY_get0_EC_KEY(pkey);
  456. if (ret != NULL)
  457. EC_KEY_up_ref(ret);
  458. return ret;
  459. }
  460. #endif
  461. #ifndef OPENSSL_NO_DH
  462. int EVP_PKEY_set1_DH(EVP_PKEY *pkey, DH *key)
  463. {
  464. int ret = EVP_PKEY_assign_DH(pkey, key);
  465. if (ret)
  466. DH_up_ref(key);
  467. return ret;
  468. }
  469. DH *EVP_PKEY_get0_DH(EVP_PKEY *pkey)
  470. {
  471. if (pkey->type != EVP_PKEY_DH && pkey->type != EVP_PKEY_DHX) {
  472. EVPerr(EVP_F_EVP_PKEY_GET0_DH, EVP_R_EXPECTING_A_DH_KEY);
  473. return NULL;
  474. }
  475. return pkey->pkey.dh;
  476. }
  477. DH *EVP_PKEY_get1_DH(EVP_PKEY *pkey)
  478. {
  479. DH *ret = EVP_PKEY_get0_DH(pkey);
  480. if (ret != NULL)
  481. DH_up_ref(ret);
  482. return ret;
  483. }
  484. #endif
  485. int EVP_PKEY_type(int type)
  486. {
  487. int ret;
  488. const EVP_PKEY_ASN1_METHOD *ameth;
  489. ENGINE *e;
  490. ameth = EVP_PKEY_asn1_find(&e, type);
  491. if (ameth)
  492. ret = ameth->pkey_id;
  493. else
  494. ret = NID_undef;
  495. #ifndef OPENSSL_NO_ENGINE
  496. ENGINE_finish(e);
  497. #endif
  498. return ret;
  499. }
  500. int EVP_PKEY_id(const EVP_PKEY *pkey)
  501. {
  502. return pkey->type;
  503. }
  504. int EVP_PKEY_base_id(const EVP_PKEY *pkey)
  505. {
  506. return EVP_PKEY_type(pkey->type);
  507. }
  508. void EVP_PKEY_free(EVP_PKEY *x)
  509. {
  510. int i;
  511. if (x == NULL)
  512. return;
  513. CRYPTO_DOWN_REF(&x->references, &i, x->lock);
  514. REF_PRINT_COUNT("EVP_PKEY", x);
  515. if (i > 0)
  516. return;
  517. REF_ASSERT_ISNT(i < 0);
  518. EVP_PKEY_free_it(x);
  519. CRYPTO_THREAD_lock_free(x->lock);
  520. sk_X509_ATTRIBUTE_pop_free(x->attributes, X509_ATTRIBUTE_free);
  521. OPENSSL_free(x);
  522. }
  523. static void EVP_PKEY_free_it(EVP_PKEY *x)
  524. {
  525. /* internal function; x is never NULL */
  526. if (x->ameth && x->ameth->pkey_free) {
  527. x->ameth->pkey_free(x);
  528. x->pkey.ptr = NULL;
  529. }
  530. #ifndef OPENSSL_NO_ENGINE
  531. ENGINE_finish(x->engine);
  532. x->engine = NULL;
  533. ENGINE_finish(x->pmeth_engine);
  534. x->pmeth_engine = NULL;
  535. #endif
  536. }
  537. static int unsup_alg(BIO *out, const EVP_PKEY *pkey, int indent,
  538. const char *kstr)
  539. {
  540. BIO_indent(out, indent, 128);
  541. BIO_printf(out, "%s algorithm \"%s\" unsupported\n",
  542. kstr, OBJ_nid2ln(pkey->type));
  543. return 1;
  544. }
  545. int EVP_PKEY_print_public(BIO *out, const EVP_PKEY *pkey,
  546. int indent, ASN1_PCTX *pctx)
  547. {
  548. if (pkey->ameth && pkey->ameth->pub_print)
  549. return pkey->ameth->pub_print(out, pkey, indent, pctx);
  550. return unsup_alg(out, pkey, indent, "Public Key");
  551. }
  552. int EVP_PKEY_print_private(BIO *out, const EVP_PKEY *pkey,
  553. int indent, ASN1_PCTX *pctx)
  554. {
  555. if (pkey->ameth && pkey->ameth->priv_print)
  556. return pkey->ameth->priv_print(out, pkey, indent, pctx);
  557. return unsup_alg(out, pkey, indent, "Private Key");
  558. }
  559. int EVP_PKEY_print_params(BIO *out, const EVP_PKEY *pkey,
  560. int indent, ASN1_PCTX *pctx)
  561. {
  562. if (pkey->ameth && pkey->ameth->param_print)
  563. return pkey->ameth->param_print(out, pkey, indent, pctx);
  564. return unsup_alg(out, pkey, indent, "Parameters");
  565. }
  566. static int evp_pkey_asn1_ctrl(EVP_PKEY *pkey, int op, int arg1, void *arg2)
  567. {
  568. if (pkey->ameth == NULL || pkey->ameth->pkey_ctrl == NULL)
  569. return -2;
  570. return pkey->ameth->pkey_ctrl(pkey, op, arg1, arg2);
  571. }
  572. int EVP_PKEY_get_default_digest_nid(EVP_PKEY *pkey, int *pnid)
  573. {
  574. return evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_DEFAULT_MD_NID, 0, pnid);
  575. }
  576. int EVP_PKEY_set1_tls_encodedpoint(EVP_PKEY *pkey,
  577. const unsigned char *pt, size_t ptlen)
  578. {
  579. if (ptlen > INT_MAX)
  580. return 0;
  581. if (evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_SET1_TLS_ENCPT, ptlen,
  582. (void *)pt) <= 0)
  583. return 0;
  584. return 1;
  585. }
  586. size_t EVP_PKEY_get1_tls_encodedpoint(EVP_PKEY *pkey, unsigned char **ppt)
  587. {
  588. int rv;
  589. rv = evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_GET1_TLS_ENCPT, 0, ppt);
  590. if (rv <= 0)
  591. return 0;
  592. return rv;
  593. }