p_lib.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. /*
  2. * Copyright 1995-2016 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 <openssl/bn.h>
  12. #include <openssl/err.h>
  13. #include <openssl/objects.h>
  14. #include <openssl/evp.h>
  15. #include <openssl/x509.h>
  16. #include <openssl/rsa.h>
  17. #include <openssl/dsa.h>
  18. #include <openssl/dh.h>
  19. #include <openssl/engine.h>
  20. #include "internal/asn1_int.h"
  21. #include "internal/evp_int.h"
  22. static void EVP_PKEY_free_it(EVP_PKEY *x);
  23. int EVP_PKEY_bits(EVP_PKEY *pkey)
  24. {
  25. if (pkey && pkey->ameth && pkey->ameth->pkey_bits)
  26. return pkey->ameth->pkey_bits(pkey);
  27. return 0;
  28. }
  29. int EVP_PKEY_security_bits(const EVP_PKEY *pkey)
  30. {
  31. if (pkey == NULL)
  32. return 0;
  33. if (!pkey->ameth || !pkey->ameth->pkey_security_bits)
  34. return -2;
  35. return pkey->ameth->pkey_security_bits(pkey);
  36. }
  37. int EVP_PKEY_size(EVP_PKEY *pkey)
  38. {
  39. if (pkey && pkey->ameth && pkey->ameth->pkey_size)
  40. return pkey->ameth->pkey_size(pkey);
  41. return 0;
  42. }
  43. int EVP_PKEY_save_parameters(EVP_PKEY *pkey, int mode)
  44. {
  45. #ifndef OPENSSL_NO_DSA
  46. if (pkey->type == EVP_PKEY_DSA) {
  47. int ret = pkey->save_parameters;
  48. if (mode >= 0)
  49. pkey->save_parameters = mode;
  50. return (ret);
  51. }
  52. #endif
  53. #ifndef OPENSSL_NO_EC
  54. if (pkey->type == EVP_PKEY_EC) {
  55. int ret = pkey->save_parameters;
  56. if (mode >= 0)
  57. pkey->save_parameters = mode;
  58. return (ret);
  59. }
  60. #endif
  61. return (0);
  62. }
  63. int EVP_PKEY_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from)
  64. {
  65. if (to->type == EVP_PKEY_NONE) {
  66. if (EVP_PKEY_set_type(to, from->type) == 0)
  67. return 0;
  68. } else if (to->type != from->type) {
  69. EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS, EVP_R_DIFFERENT_KEY_TYPES);
  70. goto err;
  71. }
  72. if (EVP_PKEY_missing_parameters(from)) {
  73. EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS, EVP_R_MISSING_PARAMETERS);
  74. goto err;
  75. }
  76. if (from->ameth && from->ameth->param_copy)
  77. return from->ameth->param_copy(to, from);
  78. err:
  79. return 0;
  80. }
  81. int EVP_PKEY_missing_parameters(const EVP_PKEY *pkey)
  82. {
  83. if (pkey->ameth && pkey->ameth->param_missing)
  84. return pkey->ameth->param_missing(pkey);
  85. return 0;
  86. }
  87. int EVP_PKEY_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b)
  88. {
  89. if (a->type != b->type)
  90. return -1;
  91. if (a->ameth && a->ameth->param_cmp)
  92. return a->ameth->param_cmp(a, b);
  93. return -2;
  94. }
  95. int EVP_PKEY_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
  96. {
  97. if (a->type != b->type)
  98. return -1;
  99. if (a->ameth) {
  100. int ret;
  101. /* Compare parameters if the algorithm has them */
  102. if (a->ameth->param_cmp) {
  103. ret = a->ameth->param_cmp(a, b);
  104. if (ret <= 0)
  105. return ret;
  106. }
  107. if (a->ameth->pub_cmp)
  108. return a->ameth->pub_cmp(a, b);
  109. }
  110. return -2;
  111. }
  112. EVP_PKEY *EVP_PKEY_new(void)
  113. {
  114. EVP_PKEY *ret = OPENSSL_zalloc(sizeof(*ret));
  115. if (ret == NULL) {
  116. EVPerr(EVP_F_EVP_PKEY_NEW, ERR_R_MALLOC_FAILURE);
  117. return NULL;
  118. }
  119. ret->type = EVP_PKEY_NONE;
  120. ret->save_type = EVP_PKEY_NONE;
  121. ret->references = 1;
  122. ret->save_parameters = 1;
  123. ret->lock = CRYPTO_THREAD_lock_new();
  124. if (ret->lock == NULL) {
  125. EVPerr(EVP_F_EVP_PKEY_NEW, ERR_R_MALLOC_FAILURE);
  126. OPENSSL_free(ret);
  127. return NULL;
  128. }
  129. return ret;
  130. }
  131. int EVP_PKEY_up_ref(EVP_PKEY *pkey)
  132. {
  133. int i;
  134. if (CRYPTO_atomic_add(&pkey->references, 1, &i, pkey->lock) <= 0)
  135. return 0;
  136. REF_PRINT_COUNT("EVP_PKEY", pkey);
  137. REF_ASSERT_ISNT(i < 2);
  138. return ((i > 1) ? 1 : 0);
  139. }
  140. /*
  141. * Setup a public key ASN1 method and ENGINE from a NID or a string. If pkey
  142. * is NULL just return 1 or 0 if the algorithm exists.
  143. */
  144. static int pkey_set_type(EVP_PKEY *pkey, int type, const char *str, int len)
  145. {
  146. const EVP_PKEY_ASN1_METHOD *ameth;
  147. ENGINE *e = NULL;
  148. if (pkey) {
  149. if (pkey->pkey.ptr)
  150. EVP_PKEY_free_it(pkey);
  151. /*
  152. * If key type matches and a method exists then this lookup has
  153. * succeeded once so just indicate success.
  154. */
  155. if ((type == pkey->save_type) && pkey->ameth)
  156. return 1;
  157. #ifndef OPENSSL_NO_ENGINE
  158. /* If we have an ENGINE release it */
  159. ENGINE_finish(pkey->engine);
  160. pkey->engine = NULL;
  161. #endif
  162. }
  163. if (str)
  164. ameth = EVP_PKEY_asn1_find_str(&e, str, len);
  165. else
  166. ameth = EVP_PKEY_asn1_find(&e, type);
  167. #ifndef OPENSSL_NO_ENGINE
  168. if (pkey == NULL)
  169. ENGINE_finish(e);
  170. #endif
  171. if (ameth == NULL) {
  172. EVPerr(EVP_F_PKEY_SET_TYPE, EVP_R_UNSUPPORTED_ALGORITHM);
  173. return 0;
  174. }
  175. if (pkey) {
  176. pkey->ameth = ameth;
  177. pkey->engine = e;
  178. pkey->type = pkey->ameth->pkey_id;
  179. pkey->save_type = type;
  180. }
  181. return 1;
  182. }
  183. int EVP_PKEY_set_type(EVP_PKEY *pkey, int type)
  184. {
  185. return pkey_set_type(pkey, type, NULL, -1);
  186. }
  187. int EVP_PKEY_set_type_str(EVP_PKEY *pkey, const char *str, int len)
  188. {
  189. return pkey_set_type(pkey, EVP_PKEY_NONE, str, len);
  190. }
  191. int EVP_PKEY_assign(EVP_PKEY *pkey, int type, void *key)
  192. {
  193. if (pkey == NULL || !EVP_PKEY_set_type(pkey, type))
  194. return 0;
  195. pkey->pkey.ptr = key;
  196. return (key != NULL);
  197. }
  198. void *EVP_PKEY_get0(const EVP_PKEY *pkey)
  199. {
  200. return pkey->pkey.ptr;
  201. }
  202. #ifndef OPENSSL_NO_RSA
  203. int EVP_PKEY_set1_RSA(EVP_PKEY *pkey, RSA *key)
  204. {
  205. int ret = EVP_PKEY_assign_RSA(pkey, key);
  206. if (ret)
  207. RSA_up_ref(key);
  208. return ret;
  209. }
  210. RSA *EVP_PKEY_get0_RSA(EVP_PKEY *pkey)
  211. {
  212. if (pkey->type != EVP_PKEY_RSA) {
  213. EVPerr(EVP_F_EVP_PKEY_GET0_RSA, EVP_R_EXPECTING_AN_RSA_KEY);
  214. return NULL;
  215. }
  216. return pkey->pkey.rsa;
  217. }
  218. RSA *EVP_PKEY_get1_RSA(EVP_PKEY *pkey)
  219. {
  220. RSA *ret = EVP_PKEY_get0_RSA(pkey);
  221. if (ret != NULL)
  222. RSA_up_ref(ret);
  223. return ret;
  224. }
  225. #endif
  226. #ifndef OPENSSL_NO_DSA
  227. int EVP_PKEY_set1_DSA(EVP_PKEY *pkey, DSA *key)
  228. {
  229. int ret = EVP_PKEY_assign_DSA(pkey, key);
  230. if (ret)
  231. DSA_up_ref(key);
  232. return ret;
  233. }
  234. DSA *EVP_PKEY_get0_DSA(EVP_PKEY *pkey)
  235. {
  236. if (pkey->type != EVP_PKEY_DSA) {
  237. EVPerr(EVP_F_EVP_PKEY_GET0_DSA, EVP_R_EXPECTING_A_DSA_KEY);
  238. return NULL;
  239. }
  240. return pkey->pkey.dsa;
  241. }
  242. DSA *EVP_PKEY_get1_DSA(EVP_PKEY *pkey)
  243. {
  244. DSA *ret = EVP_PKEY_get0_DSA(pkey);
  245. if (ret != NULL)
  246. DSA_up_ref(ret);
  247. return ret;
  248. }
  249. #endif
  250. #ifndef OPENSSL_NO_EC
  251. int EVP_PKEY_set1_EC_KEY(EVP_PKEY *pkey, EC_KEY *key)
  252. {
  253. int ret = EVP_PKEY_assign_EC_KEY(pkey, key);
  254. if (ret)
  255. EC_KEY_up_ref(key);
  256. return ret;
  257. }
  258. EC_KEY *EVP_PKEY_get0_EC_KEY(EVP_PKEY *pkey)
  259. {
  260. if (pkey->type != EVP_PKEY_EC) {
  261. EVPerr(EVP_F_EVP_PKEY_GET0_EC_KEY, EVP_R_EXPECTING_A_EC_KEY);
  262. return NULL;
  263. }
  264. return pkey->pkey.ec;
  265. }
  266. EC_KEY *EVP_PKEY_get1_EC_KEY(EVP_PKEY *pkey)
  267. {
  268. EC_KEY *ret = EVP_PKEY_get0_EC_KEY(pkey);
  269. if (ret != NULL)
  270. EC_KEY_up_ref(ret);
  271. return ret;
  272. }
  273. #endif
  274. #ifndef OPENSSL_NO_DH
  275. int EVP_PKEY_set1_DH(EVP_PKEY *pkey, DH *key)
  276. {
  277. int ret = EVP_PKEY_assign_DH(pkey, key);
  278. if (ret)
  279. DH_up_ref(key);
  280. return ret;
  281. }
  282. DH *EVP_PKEY_get0_DH(EVP_PKEY *pkey)
  283. {
  284. if (pkey->type != EVP_PKEY_DH && pkey->type != EVP_PKEY_DHX) {
  285. EVPerr(EVP_F_EVP_PKEY_GET0_DH, EVP_R_EXPECTING_A_DH_KEY);
  286. return NULL;
  287. }
  288. return pkey->pkey.dh;
  289. }
  290. DH *EVP_PKEY_get1_DH(EVP_PKEY *pkey)
  291. {
  292. DH *ret = EVP_PKEY_get0_DH(pkey);
  293. if (ret != NULL)
  294. DH_up_ref(ret);
  295. return ret;
  296. }
  297. #endif
  298. int EVP_PKEY_type(int type)
  299. {
  300. int ret;
  301. const EVP_PKEY_ASN1_METHOD *ameth;
  302. ENGINE *e;
  303. ameth = EVP_PKEY_asn1_find(&e, type);
  304. if (ameth)
  305. ret = ameth->pkey_id;
  306. else
  307. ret = NID_undef;
  308. #ifndef OPENSSL_NO_ENGINE
  309. ENGINE_finish(e);
  310. #endif
  311. return ret;
  312. }
  313. int EVP_PKEY_id(const EVP_PKEY *pkey)
  314. {
  315. return pkey->type;
  316. }
  317. int EVP_PKEY_base_id(const EVP_PKEY *pkey)
  318. {
  319. return EVP_PKEY_type(pkey->type);
  320. }
  321. void EVP_PKEY_free(EVP_PKEY *x)
  322. {
  323. int i;
  324. if (x == NULL)
  325. return;
  326. CRYPTO_atomic_add(&x->references, -1, &i, x->lock);
  327. REF_PRINT_COUNT("EVP_PKEY", x);
  328. if (i > 0)
  329. return;
  330. REF_ASSERT_ISNT(i < 0);
  331. EVP_PKEY_free_it(x);
  332. sk_X509_ATTRIBUTE_pop_free(x->attributes, X509_ATTRIBUTE_free);
  333. OPENSSL_free(x);
  334. }
  335. static void EVP_PKEY_free_it(EVP_PKEY *x)
  336. {
  337. /* internal function; x is never NULL */
  338. if (x->ameth && x->ameth->pkey_free) {
  339. x->ameth->pkey_free(x);
  340. x->pkey.ptr = NULL;
  341. }
  342. #ifndef OPENSSL_NO_ENGINE
  343. ENGINE_finish(x->engine);
  344. x->engine = NULL;
  345. #endif
  346. CRYPTO_THREAD_lock_free(x->lock);
  347. }
  348. static int unsup_alg(BIO *out, const EVP_PKEY *pkey, int indent,
  349. const char *kstr)
  350. {
  351. BIO_indent(out, indent, 128);
  352. BIO_printf(out, "%s algorithm \"%s\" unsupported\n",
  353. kstr, OBJ_nid2ln(pkey->type));
  354. return 1;
  355. }
  356. int EVP_PKEY_print_public(BIO *out, const EVP_PKEY *pkey,
  357. int indent, ASN1_PCTX *pctx)
  358. {
  359. if (pkey->ameth && pkey->ameth->pub_print)
  360. return pkey->ameth->pub_print(out, pkey, indent, pctx);
  361. return unsup_alg(out, pkey, indent, "Public Key");
  362. }
  363. int EVP_PKEY_print_private(BIO *out, const EVP_PKEY *pkey,
  364. int indent, ASN1_PCTX *pctx)
  365. {
  366. if (pkey->ameth && pkey->ameth->priv_print)
  367. return pkey->ameth->priv_print(out, pkey, indent, pctx);
  368. return unsup_alg(out, pkey, indent, "Private Key");
  369. }
  370. int EVP_PKEY_print_params(BIO *out, const EVP_PKEY *pkey,
  371. int indent, ASN1_PCTX *pctx)
  372. {
  373. if (pkey->ameth && pkey->ameth->param_print)
  374. return pkey->ameth->param_print(out, pkey, indent, pctx);
  375. return unsup_alg(out, pkey, indent, "Parameters");
  376. }
  377. int EVP_PKEY_get_default_digest_nid(EVP_PKEY *pkey, int *pnid)
  378. {
  379. if (!pkey->ameth || !pkey->ameth->pkey_ctrl)
  380. return -2;
  381. return pkey->ameth->pkey_ctrl(pkey, ASN1_PKEY_CTRL_DEFAULT_MD_NID,
  382. 0, pnid);
  383. }