evp.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  2. * All rights reserved.
  3. *
  4. * This package is an SSL implementation written
  5. * by Eric Young (eay@cryptsoft.com).
  6. * The implementation was written so as to conform with Netscapes SSL.
  7. *
  8. * This library is free for commercial and non-commercial use as long as
  9. * the following conditions are aheared to. The following conditions
  10. * apply to all code found in this distribution, be it the RC4, RSA,
  11. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  12. * included with this distribution is covered by the same copyright terms
  13. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  14. *
  15. * Copyright remains Eric Young's, and as such any Copyright notices in
  16. * the code are not to be removed.
  17. * If this package is used in a product, Eric Young should be given attribution
  18. * as the author of the parts of the library used.
  19. * This can be in the form of a textual message at program startup or
  20. * in documentation (online or textual) provided with the package.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * 3. All advertising materials mentioning features or use of this software
  31. * must display the following acknowledgement:
  32. * "This product includes cryptographic software written by
  33. * Eric Young (eay@cryptsoft.com)"
  34. * The word 'cryptographic' can be left out if the rouines from the library
  35. * being used are not cryptographic related :-).
  36. * 4. If you include any Windows specific code (or a derivative thereof) from
  37. * the apps directory (application code) you must include an acknowledgement:
  38. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  41. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  48. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  49. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. *
  52. * The licence and distribution terms for any publically available version or
  53. * derivative of this code cannot be changed. i.e. this code cannot simply be
  54. * copied and put under another distribution licence
  55. * [including the GNU Public Licence.] */
  56. #include <openssl/evp.h>
  57. #include <assert.h>
  58. #include <string.h>
  59. #include <openssl/bio.h>
  60. #include <openssl/dsa.h>
  61. #include <openssl/ec.h>
  62. #include <openssl/err.h>
  63. #include <openssl/mem.h>
  64. #include <openssl/obj.h>
  65. #include <openssl/rsa.h>
  66. #include <openssl/thread.h>
  67. #include "internal.h"
  68. #include "../internal.h"
  69. EVP_PKEY *EVP_PKEY_new(void) {
  70. EVP_PKEY *ret;
  71. ret = OPENSSL_malloc(sizeof(EVP_PKEY));
  72. if (ret == NULL) {
  73. OPENSSL_PUT_ERROR(EVP, ERR_R_MALLOC_FAILURE);
  74. return NULL;
  75. }
  76. memset(ret, 0, sizeof(EVP_PKEY));
  77. ret->type = EVP_PKEY_NONE;
  78. ret->references = 1;
  79. return ret;
  80. }
  81. static void free_it(EVP_PKEY *pkey) {
  82. if (pkey->ameth && pkey->ameth->pkey_free) {
  83. pkey->ameth->pkey_free(pkey);
  84. pkey->pkey.ptr = NULL;
  85. pkey->type = EVP_PKEY_NONE;
  86. }
  87. }
  88. void EVP_PKEY_free(EVP_PKEY *pkey) {
  89. if (pkey == NULL) {
  90. return;
  91. }
  92. if (!CRYPTO_refcount_dec_and_test_zero(&pkey->references)) {
  93. return;
  94. }
  95. free_it(pkey);
  96. OPENSSL_free(pkey);
  97. }
  98. EVP_PKEY *EVP_PKEY_up_ref(EVP_PKEY *pkey) {
  99. CRYPTO_refcount_inc(&pkey->references);
  100. return pkey;
  101. }
  102. int EVP_PKEY_is_opaque(const EVP_PKEY *pkey) {
  103. if (pkey->ameth && pkey->ameth->pkey_opaque) {
  104. return pkey->ameth->pkey_opaque(pkey);
  105. }
  106. return 0;
  107. }
  108. int EVP_PKEY_supports_digest(const EVP_PKEY *pkey, const EVP_MD *md) {
  109. if (pkey->ameth && pkey->ameth->pkey_supports_digest) {
  110. return pkey->ameth->pkey_supports_digest(pkey, md);
  111. }
  112. return 1;
  113. }
  114. int EVP_PKEY_cmp(const EVP_PKEY *a, const EVP_PKEY *b) {
  115. if (a->type != b->type) {
  116. return -1;
  117. }
  118. if (a->ameth) {
  119. int ret;
  120. /* Compare parameters if the algorithm has them */
  121. if (a->ameth->param_cmp) {
  122. ret = a->ameth->param_cmp(a, b);
  123. if (ret <= 0) {
  124. return ret;
  125. }
  126. }
  127. if (a->ameth->pub_cmp) {
  128. return a->ameth->pub_cmp(a, b);
  129. }
  130. }
  131. return -2;
  132. }
  133. int EVP_PKEY_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from) {
  134. if (to->type != from->type) {
  135. OPENSSL_PUT_ERROR(EVP, EVP_R_DIFFERENT_KEY_TYPES);
  136. goto err;
  137. }
  138. if (EVP_PKEY_missing_parameters(from)) {
  139. OPENSSL_PUT_ERROR(EVP, EVP_R_MISSING_PARAMETERS);
  140. goto err;
  141. }
  142. if (from->ameth && from->ameth->param_copy) {
  143. return from->ameth->param_copy(to, from);
  144. }
  145. err:
  146. return 0;
  147. }
  148. int EVP_PKEY_missing_parameters(const EVP_PKEY *pkey) {
  149. if (pkey->ameth && pkey->ameth->param_missing) {
  150. return pkey->ameth->param_missing(pkey);
  151. }
  152. return 0;
  153. }
  154. int EVP_PKEY_size(const EVP_PKEY *pkey) {
  155. if (pkey && pkey->ameth && pkey->ameth->pkey_size) {
  156. return pkey->ameth->pkey_size(pkey);
  157. }
  158. return 0;
  159. }
  160. int EVP_PKEY_bits(EVP_PKEY *pkey) {
  161. if (pkey && pkey->ameth && pkey->ameth->pkey_bits) {
  162. return pkey->ameth->pkey_bits(pkey);
  163. }
  164. return 0;
  165. }
  166. int EVP_PKEY_id(const EVP_PKEY *pkey) {
  167. return pkey->type;
  168. }
  169. /* TODO(fork): remove the first argument. */
  170. const EVP_PKEY_ASN1_METHOD *EVP_PKEY_asn1_find(ENGINE **pengine, int nid) {
  171. switch (nid) {
  172. case EVP_PKEY_RSA:
  173. return &rsa_asn1_meth;
  174. case EVP_PKEY_EC:
  175. return &ec_asn1_meth;
  176. case EVP_PKEY_DSA:
  177. return &dsa_asn1_meth;
  178. default:
  179. return NULL;
  180. }
  181. }
  182. int EVP_PKEY_type(int nid) {
  183. const EVP_PKEY_ASN1_METHOD *meth = EVP_PKEY_asn1_find(NULL, nid);
  184. if (meth == NULL) {
  185. return NID_undef;
  186. }
  187. return meth->pkey_id;
  188. }
  189. int EVP_PKEY_set1_RSA(EVP_PKEY *pkey, RSA *key) {
  190. if (EVP_PKEY_assign_RSA(pkey, key)) {
  191. RSA_up_ref(key);
  192. return 1;
  193. }
  194. return 0;
  195. }
  196. int EVP_PKEY_assign_RSA(EVP_PKEY *pkey, RSA *key) {
  197. return EVP_PKEY_assign(pkey, EVP_PKEY_RSA, key);
  198. }
  199. RSA *EVP_PKEY_get0_RSA(EVP_PKEY *pkey) {
  200. if (pkey->type != EVP_PKEY_RSA) {
  201. OPENSSL_PUT_ERROR(EVP, EVP_R_EXPECTING_AN_RSA_KEY);
  202. return NULL;
  203. }
  204. return pkey->pkey.rsa;
  205. }
  206. RSA *EVP_PKEY_get1_RSA(EVP_PKEY *pkey) {
  207. RSA *rsa = EVP_PKEY_get0_RSA(pkey);
  208. if (rsa != NULL) {
  209. RSA_up_ref(rsa);
  210. }
  211. return rsa;
  212. }
  213. int EVP_PKEY_set1_DSA(EVP_PKEY *pkey, DSA *key) {
  214. if (EVP_PKEY_assign_DSA(pkey, key)) {
  215. DSA_up_ref(key);
  216. return 1;
  217. }
  218. return 0;
  219. }
  220. int EVP_PKEY_assign_DSA(EVP_PKEY *pkey, DSA *key) {
  221. return EVP_PKEY_assign(pkey, EVP_PKEY_DSA, key);
  222. }
  223. DSA *EVP_PKEY_get0_DSA(EVP_PKEY *pkey) {
  224. if (pkey->type != EVP_PKEY_DSA) {
  225. OPENSSL_PUT_ERROR(EVP, EVP_R_EXPECTING_A_DSA_KEY);
  226. return NULL;
  227. }
  228. return pkey->pkey.dsa;
  229. }
  230. DSA *EVP_PKEY_get1_DSA(EVP_PKEY *pkey) {
  231. DSA *dsa = EVP_PKEY_get0_DSA(pkey);
  232. if (dsa != NULL) {
  233. DSA_up_ref(dsa);
  234. }
  235. return dsa;
  236. }
  237. int EVP_PKEY_set1_EC_KEY(EVP_PKEY *pkey, EC_KEY *key) {
  238. if (EVP_PKEY_assign_EC_KEY(pkey, key)) {
  239. EC_KEY_up_ref(key);
  240. return 1;
  241. }
  242. return 0;
  243. }
  244. int EVP_PKEY_assign_EC_KEY(EVP_PKEY *pkey, EC_KEY *key) {
  245. return EVP_PKEY_assign(pkey, EVP_PKEY_EC, key);
  246. }
  247. EC_KEY *EVP_PKEY_get0_EC_KEY(EVP_PKEY *pkey) {
  248. if (pkey->type != EVP_PKEY_EC) {
  249. OPENSSL_PUT_ERROR(EVP, EVP_R_EXPECTING_AN_EC_KEY_KEY);
  250. return NULL;
  251. }
  252. return pkey->pkey.ec;
  253. }
  254. EC_KEY *EVP_PKEY_get1_EC_KEY(EVP_PKEY *pkey) {
  255. EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(pkey);
  256. if (ec_key != NULL) {
  257. EC_KEY_up_ref(ec_key);
  258. }
  259. return ec_key;
  260. }
  261. int EVP_PKEY_assign(EVP_PKEY *pkey, int type, void *key) {
  262. if (!EVP_PKEY_set_type(pkey, type)) {
  263. return 0;
  264. }
  265. pkey->pkey.ptr = key;
  266. return key != NULL;
  267. }
  268. const EVP_PKEY_ASN1_METHOD *EVP_PKEY_asn1_find_str(ENGINE **pengine,
  269. const char *name,
  270. size_t len) {
  271. if (len == 3 && memcmp(name, "RSA", 3) == 0) {
  272. return &rsa_asn1_meth;
  273. }
  274. if (len == 2 && memcmp(name, "EC", 2) == 0) {
  275. return &ec_asn1_meth;
  276. }
  277. if (len == 3 && memcmp(name, "DSA", 3) == 0) {
  278. return &dsa_asn1_meth;
  279. }
  280. return NULL;
  281. }
  282. int EVP_PKEY_set_type(EVP_PKEY *pkey, int type) {
  283. const EVP_PKEY_ASN1_METHOD *ameth;
  284. if (pkey && pkey->pkey.ptr) {
  285. free_it(pkey);
  286. }
  287. ameth = EVP_PKEY_asn1_find(NULL, type);
  288. if (ameth == NULL) {
  289. OPENSSL_PUT_ERROR(EVP, EVP_R_UNSUPPORTED_ALGORITHM);
  290. ERR_add_error_dataf("algorithm %d (%s)", type, OBJ_nid2sn(type));
  291. return 0;
  292. }
  293. if (pkey) {
  294. pkey->ameth = ameth;
  295. pkey->type = pkey->ameth->pkey_id;
  296. }
  297. return 1;
  298. }
  299. int EVP_PKEY_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b) {
  300. if (a->type != b->type) {
  301. return -1;
  302. }
  303. if (a->ameth && a->ameth->param_cmp) {
  304. return a->ameth->param_cmp(a, b);
  305. }
  306. return -2;
  307. }
  308. static int print_unsupported(BIO *out, const EVP_PKEY *pkey, int indent,
  309. const char *kstr) {
  310. BIO_indent(out, indent, 128);
  311. BIO_printf(out, "%s algorithm \"%s\" unsupported\n", kstr,
  312. OBJ_nid2ln(pkey->type));
  313. return 1;
  314. }
  315. int EVP_PKEY_print_public(BIO *out, const EVP_PKEY *pkey, int indent,
  316. ASN1_PCTX *pctx) {
  317. if (pkey->ameth && pkey->ameth->pub_print) {
  318. return pkey->ameth->pub_print(out, pkey, indent, pctx);
  319. }
  320. return print_unsupported(out, pkey, indent, "Public Key");
  321. }
  322. int EVP_PKEY_print_private(BIO *out, const EVP_PKEY *pkey, int indent,
  323. ASN1_PCTX *pctx) {
  324. if (pkey->ameth && pkey->ameth->priv_print) {
  325. return pkey->ameth->priv_print(out, pkey, indent, pctx);
  326. }
  327. return print_unsupported(out, pkey, indent, "Private Key");
  328. }
  329. int EVP_PKEY_print_params(BIO *out, const EVP_PKEY *pkey, int indent,
  330. ASN1_PCTX *pctx) {
  331. if (pkey->ameth && pkey->ameth->param_print) {
  332. return pkey->ameth->param_print(out, pkey, indent, pctx);
  333. }
  334. return print_unsupported(out, pkey, indent, "Parameters");
  335. }
  336. int EVP_PKEY_CTX_set_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD *md) {
  337. return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_TYPE_SIG, EVP_PKEY_CTRL_MD, 0,
  338. (void *)md);
  339. }
  340. int EVP_PKEY_CTX_get_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD **out_md) {
  341. return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_TYPE_SIG, EVP_PKEY_CTRL_GET_MD,
  342. 0, (void *)out_md);
  343. }
  344. void OpenSSL_add_all_algorithms(void) {}
  345. void OpenSSL_add_all_ciphers(void) {}
  346. void OpenSSL_add_all_digests(void) {}
  347. void EVP_cleanup(void) {}