p_lib.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. /* crypto/evp/p_lib.c */
  2. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  3. * All rights reserved.
  4. *
  5. * This package is an SSL implementation written
  6. * by Eric Young (eay@cryptsoft.com).
  7. * The implementation was written so as to conform with Netscapes SSL.
  8. *
  9. * This library is free for commercial and non-commercial use as long as
  10. * the following conditions are aheared to. The following conditions
  11. * apply to all code found in this distribution, be it the RC4, RSA,
  12. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  13. * included with this distribution is covered by the same copyright terms
  14. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  15. *
  16. * Copyright remains Eric Young's, and as such any Copyright notices in
  17. * the code are not to be removed.
  18. * If this package is used in a product, Eric Young should be given attribution
  19. * as the author of the parts of the library used.
  20. * This can be in the form of a textual message at program startup or
  21. * in documentation (online or textual) provided with the package.
  22. *
  23. * Redistribution and use in source and binary forms, with or without
  24. * modification, are permitted provided that the following conditions
  25. * are met:
  26. * 1. Redistributions of source code must retain the copyright
  27. * notice, this list of conditions and the following disclaimer.
  28. * 2. Redistributions in binary form must reproduce the above copyright
  29. * notice, this list of conditions and the following disclaimer in the
  30. * documentation and/or other materials provided with the distribution.
  31. * 3. All advertising materials mentioning features or use of this software
  32. * must display the following acknowledgement:
  33. * "This product includes cryptographic software written by
  34. * Eric Young (eay@cryptsoft.com)"
  35. * The word 'cryptographic' can be left out if the rouines from the library
  36. * being used are not cryptographic related :-).
  37. * 4. If you include any Windows specific code (or a derivative thereof) from
  38. * the apps directory (application code) you must include an acknowledgement:
  39. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  40. *
  41. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  42. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  43. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  44. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  45. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  46. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  47. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  49. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  50. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  51. * SUCH DAMAGE.
  52. *
  53. * The licence and distribution terms for any publically available version or
  54. * derivative of this code cannot be changed. i.e. this code cannot simply be
  55. * copied and put under another distribution licence
  56. * [including the GNU Public Licence.]
  57. */
  58. #include <stdio.h>
  59. #include "internal/cryptlib.h"
  60. #include <openssl/bn.h>
  61. #include <openssl/err.h>
  62. #include <openssl/objects.h>
  63. #include <openssl/evp.h>
  64. #include <openssl/x509.h>
  65. #ifndef OPENSSL_NO_RSA
  66. # include <openssl/rsa.h>
  67. #endif
  68. #ifndef OPENSSL_NO_DSA
  69. # include <openssl/dsa.h>
  70. #endif
  71. #ifndef OPENSSL_NO_DH
  72. # include <openssl/dh.h>
  73. #endif
  74. #ifndef OPENSSL_NO_ENGINE
  75. # include <openssl/engine.h>
  76. #endif
  77. #include "internal/asn1_int.h"
  78. #include "internal/evp_int.h"
  79. static void EVP_PKEY_free_it(EVP_PKEY *x);
  80. int EVP_PKEY_bits(EVP_PKEY *pkey)
  81. {
  82. if (pkey && pkey->ameth && pkey->ameth->pkey_bits)
  83. return pkey->ameth->pkey_bits(pkey);
  84. return 0;
  85. }
  86. int EVP_PKEY_security_bits(const EVP_PKEY *pkey)
  87. {
  88. if (pkey == NULL)
  89. return 0;
  90. if (!pkey->ameth || !pkey->ameth->pkey_security_bits)
  91. return -2;
  92. return pkey->ameth->pkey_security_bits(pkey);
  93. }
  94. int EVP_PKEY_size(EVP_PKEY *pkey)
  95. {
  96. if (pkey && pkey->ameth && pkey->ameth->pkey_size)
  97. return pkey->ameth->pkey_size(pkey);
  98. return 0;
  99. }
  100. int EVP_PKEY_save_parameters(EVP_PKEY *pkey, int mode)
  101. {
  102. #ifndef OPENSSL_NO_DSA
  103. if (pkey->type == EVP_PKEY_DSA) {
  104. int ret = pkey->save_parameters;
  105. if (mode >= 0)
  106. pkey->save_parameters = mode;
  107. return (ret);
  108. }
  109. #endif
  110. #ifndef OPENSSL_NO_EC
  111. if (pkey->type == EVP_PKEY_EC) {
  112. int ret = pkey->save_parameters;
  113. if (mode >= 0)
  114. pkey->save_parameters = mode;
  115. return (ret);
  116. }
  117. #endif
  118. return (0);
  119. }
  120. int EVP_PKEY_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from)
  121. {
  122. if (to->type == EVP_PKEY_NONE) {
  123. if (EVP_PKEY_set_type(to, from->type) == 0)
  124. return 0;
  125. } else if (to->type != from->type) {
  126. EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS, EVP_R_DIFFERENT_KEY_TYPES);
  127. goto err;
  128. }
  129. if (EVP_PKEY_missing_parameters(from)) {
  130. EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS, EVP_R_MISSING_PARAMETERS);
  131. goto err;
  132. }
  133. if (from->ameth && from->ameth->param_copy)
  134. return from->ameth->param_copy(to, from);
  135. err:
  136. return 0;
  137. }
  138. int EVP_PKEY_missing_parameters(const EVP_PKEY *pkey)
  139. {
  140. if (pkey->ameth && pkey->ameth->param_missing)
  141. return pkey->ameth->param_missing(pkey);
  142. return 0;
  143. }
  144. int EVP_PKEY_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b)
  145. {
  146. if (a->type != b->type)
  147. return -1;
  148. if (a->ameth && a->ameth->param_cmp)
  149. return a->ameth->param_cmp(a, b);
  150. return -2;
  151. }
  152. int EVP_PKEY_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
  153. {
  154. if (a->type != b->type)
  155. return -1;
  156. if (a->ameth) {
  157. int ret;
  158. /* Compare parameters if the algorithm has them */
  159. if (a->ameth->param_cmp) {
  160. ret = a->ameth->param_cmp(a, b);
  161. if (ret <= 0)
  162. return ret;
  163. }
  164. if (a->ameth->pub_cmp)
  165. return a->ameth->pub_cmp(a, b);
  166. }
  167. return -2;
  168. }
  169. EVP_PKEY *EVP_PKEY_new(void)
  170. {
  171. EVP_PKEY *ret;
  172. ret = OPENSSL_malloc(sizeof(*ret));
  173. if (ret == NULL) {
  174. EVPerr(EVP_F_EVP_PKEY_NEW, ERR_R_MALLOC_FAILURE);
  175. return (NULL);
  176. }
  177. ret->type = EVP_PKEY_NONE;
  178. ret->save_type = EVP_PKEY_NONE;
  179. ret->references = 1;
  180. ret->ameth = NULL;
  181. ret->engine = NULL;
  182. ret->pkey.ptr = NULL;
  183. ret->attributes = NULL;
  184. ret->save_parameters = 1;
  185. return (ret);
  186. }
  187. void EVP_PKEY_up_ref(EVP_PKEY *pkey)
  188. {
  189. CRYPTO_add(&pkey->references, 1, CRYPTO_LOCK_EVP_PKEY);
  190. }
  191. /*
  192. * Setup a public key ASN1 method and ENGINE from a NID or a string. If pkey
  193. * is NULL just return 1 or 0 if the algorithm exists.
  194. */
  195. static int pkey_set_type(EVP_PKEY *pkey, int type, const char *str, int len)
  196. {
  197. const EVP_PKEY_ASN1_METHOD *ameth;
  198. ENGINE *e = NULL;
  199. if (pkey) {
  200. if (pkey->pkey.ptr)
  201. EVP_PKEY_free_it(pkey);
  202. /*
  203. * If key type matches and a method exists then this lookup has
  204. * succeeded once so just indicate success.
  205. */
  206. if ((type == pkey->save_type) && pkey->ameth)
  207. return 1;
  208. #ifndef OPENSSL_NO_ENGINE
  209. /* If we have an ENGINE release it */
  210. if (pkey->engine) {
  211. ENGINE_finish(pkey->engine);
  212. pkey->engine = NULL;
  213. }
  214. #endif
  215. }
  216. if (str)
  217. ameth = EVP_PKEY_asn1_find_str(&e, str, len);
  218. else
  219. ameth = EVP_PKEY_asn1_find(&e, type);
  220. #ifndef OPENSSL_NO_ENGINE
  221. if (!pkey && e)
  222. ENGINE_finish(e);
  223. #endif
  224. if (!ameth) {
  225. EVPerr(EVP_F_PKEY_SET_TYPE, EVP_R_UNSUPPORTED_ALGORITHM);
  226. return 0;
  227. }
  228. if (pkey) {
  229. pkey->ameth = ameth;
  230. pkey->engine = e;
  231. pkey->type = pkey->ameth->pkey_id;
  232. pkey->save_type = type;
  233. }
  234. return 1;
  235. }
  236. int EVP_PKEY_set_type(EVP_PKEY *pkey, int type)
  237. {
  238. return pkey_set_type(pkey, type, NULL, -1);
  239. }
  240. int EVP_PKEY_set_type_str(EVP_PKEY *pkey, const char *str, int len)
  241. {
  242. return pkey_set_type(pkey, EVP_PKEY_NONE, str, len);
  243. }
  244. int EVP_PKEY_assign(EVP_PKEY *pkey, int type, void *key)
  245. {
  246. if (pkey == NULL || !EVP_PKEY_set_type(pkey, type))
  247. return 0;
  248. pkey->pkey.ptr = key;
  249. return (key != NULL);
  250. }
  251. void *EVP_PKEY_get0(const EVP_PKEY *pkey)
  252. {
  253. return pkey->pkey.ptr;
  254. }
  255. #ifndef OPENSSL_NO_RSA
  256. int EVP_PKEY_set1_RSA(EVP_PKEY *pkey, RSA *key)
  257. {
  258. int ret = EVP_PKEY_assign_RSA(pkey, key);
  259. if (ret)
  260. RSA_up_ref(key);
  261. return ret;
  262. }
  263. RSA *EVP_PKEY_get0_RSA(EVP_PKEY *pkey)
  264. {
  265. if (pkey->type != EVP_PKEY_RSA) {
  266. EVPerr(EVP_F_EVP_PKEY_GET0_RSA, EVP_R_EXPECTING_AN_RSA_KEY);
  267. return NULL;
  268. }
  269. return pkey->pkey.rsa;
  270. }
  271. RSA *EVP_PKEY_get1_RSA(EVP_PKEY *pkey)
  272. {
  273. RSA *ret = EVP_PKEY_get0_RSA(pkey);
  274. if (ret != NULL)
  275. RSA_up_ref(ret);
  276. return ret;
  277. }
  278. #endif
  279. #ifndef OPENSSL_NO_DSA
  280. int EVP_PKEY_set1_DSA(EVP_PKEY *pkey, DSA *key)
  281. {
  282. int ret = EVP_PKEY_assign_DSA(pkey, key);
  283. if (ret)
  284. DSA_up_ref(key);
  285. return ret;
  286. }
  287. DSA *EVP_PKEY_get0_DSA(EVP_PKEY *pkey)
  288. {
  289. if (pkey->type != EVP_PKEY_DSA) {
  290. EVPerr(EVP_F_EVP_PKEY_GET0_DSA, EVP_R_EXPECTING_A_DSA_KEY);
  291. return NULL;
  292. }
  293. return pkey->pkey.dsa;
  294. }
  295. DSA *EVP_PKEY_get1_DSA(EVP_PKEY *pkey)
  296. {
  297. DSA *ret = EVP_PKEY_get0_DSA(pkey);
  298. if (ret != NULL)
  299. DSA_up_ref(ret);
  300. return ret;
  301. }
  302. #endif
  303. #ifndef OPENSSL_NO_EC
  304. int EVP_PKEY_set1_EC_KEY(EVP_PKEY *pkey, EC_KEY *key)
  305. {
  306. int ret = EVP_PKEY_assign_EC_KEY(pkey, key);
  307. if (ret)
  308. EC_KEY_up_ref(key);
  309. return ret;
  310. }
  311. EC_KEY *EVP_PKEY_get0_EC_KEY(EVP_PKEY *pkey)
  312. {
  313. if (pkey->type != EVP_PKEY_EC) {
  314. EVPerr(EVP_F_EVP_PKEY_GET0_EC_KEY, EVP_R_EXPECTING_A_EC_KEY);
  315. return NULL;
  316. }
  317. return pkey->pkey.ec;
  318. }
  319. EC_KEY *EVP_PKEY_get1_EC_KEY(EVP_PKEY *pkey)
  320. {
  321. EC_KEY *ret = EVP_PKEY_get0_EC_KEY(pkey);
  322. if (ret != NULL)
  323. EC_KEY_up_ref(ret);
  324. return ret;
  325. }
  326. #endif
  327. #ifndef OPENSSL_NO_DH
  328. int EVP_PKEY_set1_DH(EVP_PKEY *pkey, DH *key)
  329. {
  330. int ret = EVP_PKEY_assign_DH(pkey, key);
  331. if (ret)
  332. DH_up_ref(key);
  333. return ret;
  334. }
  335. DH *EVP_PKEY_get0_DH(EVP_PKEY *pkey)
  336. {
  337. if (pkey->type != EVP_PKEY_DH && pkey->type != EVP_PKEY_DHX) {
  338. EVPerr(EVP_F_EVP_PKEY_GET0_DH, EVP_R_EXPECTING_A_DH_KEY);
  339. return NULL;
  340. }
  341. return pkey->pkey.dh;
  342. }
  343. DH *EVP_PKEY_get1_DH(EVP_PKEY *pkey)
  344. {
  345. DH *ret = EVP_PKEY_get0_DH(pkey);
  346. if (ret != NULL)
  347. DH_up_ref(ret);
  348. return ret;
  349. }
  350. #endif
  351. int EVP_PKEY_type(int type)
  352. {
  353. int ret;
  354. const EVP_PKEY_ASN1_METHOD *ameth;
  355. ENGINE *e;
  356. ameth = EVP_PKEY_asn1_find(&e, type);
  357. if (ameth)
  358. ret = ameth->pkey_id;
  359. else
  360. ret = NID_undef;
  361. #ifndef OPENSSL_NO_ENGINE
  362. if (e)
  363. ENGINE_finish(e);
  364. #endif
  365. return ret;
  366. }
  367. int EVP_PKEY_id(const EVP_PKEY *pkey)
  368. {
  369. return pkey->type;
  370. }
  371. int EVP_PKEY_base_id(const EVP_PKEY *pkey)
  372. {
  373. return EVP_PKEY_type(pkey->type);
  374. }
  375. void EVP_PKEY_free(EVP_PKEY *x)
  376. {
  377. int i;
  378. if (x == NULL)
  379. return;
  380. i = CRYPTO_add(&x->references, -1, CRYPTO_LOCK_EVP_PKEY);
  381. #ifdef REF_PRINT
  382. REF_PRINT("EVP_PKEY", x);
  383. #endif
  384. if (i > 0)
  385. return;
  386. #ifdef REF_CHECK
  387. if (i < 0) {
  388. fprintf(stderr, "EVP_PKEY_free, bad reference count\n");
  389. abort();
  390. }
  391. #endif
  392. EVP_PKEY_free_it(x);
  393. sk_X509_ATTRIBUTE_pop_free(x->attributes, X509_ATTRIBUTE_free);
  394. OPENSSL_free(x);
  395. }
  396. static void EVP_PKEY_free_it(EVP_PKEY *x)
  397. {
  398. /* internal function; x is never NULL */
  399. if (x->ameth && x->ameth->pkey_free) {
  400. x->ameth->pkey_free(x);
  401. x->pkey.ptr = NULL;
  402. }
  403. #ifndef OPENSSL_NO_ENGINE
  404. if (x->engine) {
  405. ENGINE_finish(x->engine);
  406. x->engine = NULL;
  407. }
  408. #endif
  409. }
  410. static int unsup_alg(BIO *out, const EVP_PKEY *pkey, int indent,
  411. const char *kstr)
  412. {
  413. BIO_indent(out, indent, 128);
  414. BIO_printf(out, "%s algorithm \"%s\" unsupported\n",
  415. kstr, OBJ_nid2ln(pkey->type));
  416. return 1;
  417. }
  418. int EVP_PKEY_print_public(BIO *out, const EVP_PKEY *pkey,
  419. int indent, ASN1_PCTX *pctx)
  420. {
  421. if (pkey->ameth && pkey->ameth->pub_print)
  422. return pkey->ameth->pub_print(out, pkey, indent, pctx);
  423. return unsup_alg(out, pkey, indent, "Public Key");
  424. }
  425. int EVP_PKEY_print_private(BIO *out, const EVP_PKEY *pkey,
  426. int indent, ASN1_PCTX *pctx)
  427. {
  428. if (pkey->ameth && pkey->ameth->priv_print)
  429. return pkey->ameth->priv_print(out, pkey, indent, pctx);
  430. return unsup_alg(out, pkey, indent, "Private Key");
  431. }
  432. int EVP_PKEY_print_params(BIO *out, const EVP_PKEY *pkey,
  433. int indent, ASN1_PCTX *pctx)
  434. {
  435. if (pkey->ameth && pkey->ameth->param_print)
  436. return pkey->ameth->param_print(out, pkey, indent, pctx);
  437. return unsup_alg(out, pkey, indent, "Parameters");
  438. }
  439. int EVP_PKEY_get_default_digest_nid(EVP_PKEY *pkey, int *pnid)
  440. {
  441. if (!pkey->ameth || !pkey->ameth->pkey_ctrl)
  442. return -2;
  443. return pkey->ameth->pkey_ctrl(pkey, ASN1_PKEY_CTRL_DEFAULT_MD_NID,
  444. 0, pnid);
  445. }