You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

412 lines
11 KiB

  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. case EVP_PKEY_RSA2:
  174. return &rsa_asn1_meth;
  175. case EVP_PKEY_EC:
  176. return &ec_asn1_meth;
  177. case EVP_PKEY_DSA:
  178. return &dsa_asn1_meth;
  179. default:
  180. return NULL;
  181. }
  182. }
  183. int EVP_PKEY_type(int nid) {
  184. const EVP_PKEY_ASN1_METHOD *meth = EVP_PKEY_asn1_find(NULL, nid);
  185. if (meth == NULL) {
  186. return NID_undef;
  187. }
  188. return meth->pkey_id;
  189. }
  190. int EVP_PKEY_set1_RSA(EVP_PKEY *pkey, RSA *key) {
  191. if (EVP_PKEY_assign_RSA(pkey, key)) {
  192. RSA_up_ref(key);
  193. return 1;
  194. }
  195. return 0;
  196. }
  197. int EVP_PKEY_assign_RSA(EVP_PKEY *pkey, RSA *key) {
  198. return EVP_PKEY_assign(pkey, EVP_PKEY_RSA, key);
  199. }
  200. RSA *EVP_PKEY_get0_RSA(EVP_PKEY *pkey) {
  201. if (pkey->type != EVP_PKEY_RSA) {
  202. OPENSSL_PUT_ERROR(EVP, EVP_R_EXPECTING_AN_RSA_KEY);
  203. return NULL;
  204. }
  205. return pkey->pkey.rsa;
  206. }
  207. RSA *EVP_PKEY_get1_RSA(EVP_PKEY *pkey) {
  208. RSA *rsa = EVP_PKEY_get0_RSA(pkey);
  209. if (rsa != NULL) {
  210. RSA_up_ref(rsa);
  211. }
  212. return rsa;
  213. }
  214. int EVP_PKEY_set1_DSA(EVP_PKEY *pkey, DSA *key) {
  215. if (EVP_PKEY_assign_DSA(pkey, key)) {
  216. DSA_up_ref(key);
  217. return 1;
  218. }
  219. return 0;
  220. }
  221. int EVP_PKEY_assign_DSA(EVP_PKEY *pkey, DSA *key) {
  222. return EVP_PKEY_assign(pkey, EVP_PKEY_DSA, key);
  223. }
  224. DSA *EVP_PKEY_get0_DSA(EVP_PKEY *pkey) {
  225. if (pkey->type != EVP_PKEY_DSA) {
  226. OPENSSL_PUT_ERROR(EVP, EVP_R_EXPECTING_A_DSA_KEY);
  227. return NULL;
  228. }
  229. return pkey->pkey.dsa;
  230. }
  231. DSA *EVP_PKEY_get1_DSA(EVP_PKEY *pkey) {
  232. DSA *dsa = EVP_PKEY_get0_DSA(pkey);
  233. if (dsa != NULL) {
  234. DSA_up_ref(dsa);
  235. }
  236. return dsa;
  237. }
  238. int EVP_PKEY_set1_EC_KEY(EVP_PKEY *pkey, EC_KEY *key) {
  239. if (EVP_PKEY_assign_EC_KEY(pkey, key)) {
  240. EC_KEY_up_ref(key);
  241. return 1;
  242. }
  243. return 0;
  244. }
  245. int EVP_PKEY_assign_EC_KEY(EVP_PKEY *pkey, EC_KEY *key) {
  246. return EVP_PKEY_assign(pkey, EVP_PKEY_EC, key);
  247. }
  248. EC_KEY *EVP_PKEY_get0_EC_KEY(EVP_PKEY *pkey) {
  249. if (pkey->type != EVP_PKEY_EC) {
  250. OPENSSL_PUT_ERROR(EVP, EVP_R_EXPECTING_AN_EC_KEY_KEY);
  251. return NULL;
  252. }
  253. return pkey->pkey.ec;
  254. }
  255. EC_KEY *EVP_PKEY_get1_EC_KEY(EVP_PKEY *pkey) {
  256. EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(pkey);
  257. if (ec_key != NULL) {
  258. EC_KEY_up_ref(ec_key);
  259. }
  260. return ec_key;
  261. }
  262. int EVP_PKEY_assign(EVP_PKEY *pkey, int type, void *key) {
  263. if (!EVP_PKEY_set_type(pkey, type)) {
  264. return 0;
  265. }
  266. pkey->pkey.ptr = key;
  267. return key != NULL;
  268. }
  269. const EVP_PKEY_ASN1_METHOD *EVP_PKEY_asn1_find_str(ENGINE **pengine,
  270. const char *name,
  271. size_t len) {
  272. if (len == 3 && memcmp(name, "RSA", 3) == 0) {
  273. return &rsa_asn1_meth;
  274. } if (len == 2 && memcmp(name, "EC", 2) == 0) {
  275. return &ec_asn1_meth;
  276. } else if (len == 3 && memcmp(name, "DSA", 3) == 0) {
  277. return &dsa_asn1_meth;
  278. }
  279. return NULL;
  280. }
  281. int EVP_PKEY_set_type(EVP_PKEY *pkey, int type) {
  282. const EVP_PKEY_ASN1_METHOD *ameth;
  283. if (pkey && pkey->pkey.ptr) {
  284. free_it(pkey);
  285. }
  286. ameth = EVP_PKEY_asn1_find(NULL, type);
  287. if (ameth == NULL) {
  288. OPENSSL_PUT_ERROR(EVP, EVP_R_UNSUPPORTED_ALGORITHM);
  289. ERR_add_error_dataf("algorithm %d (%s)", type, OBJ_nid2sn(type));
  290. return 0;
  291. }
  292. if (pkey) {
  293. pkey->ameth = ameth;
  294. pkey->type = pkey->ameth->pkey_id;
  295. }
  296. return 1;
  297. }
  298. int EVP_PKEY_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b) {
  299. if (a->type != b->type) {
  300. return -1;
  301. }
  302. if (a->ameth && a->ameth->param_cmp) {
  303. return a->ameth->param_cmp(a, b);
  304. }
  305. return -2;
  306. }
  307. static int print_unsupported(BIO *out, const EVP_PKEY *pkey, int indent,
  308. const char *kstr) {
  309. BIO_indent(out, indent, 128);
  310. BIO_printf(out, "%s algorithm \"%s\" unsupported\n", kstr,
  311. OBJ_nid2ln(pkey->type));
  312. return 1;
  313. }
  314. int EVP_PKEY_print_public(BIO *out, const EVP_PKEY *pkey, int indent,
  315. ASN1_PCTX *pctx) {
  316. if (pkey->ameth && pkey->ameth->pub_print) {
  317. return pkey->ameth->pub_print(out, pkey, indent, pctx);
  318. }
  319. return print_unsupported(out, pkey, indent, "Public Key");
  320. }
  321. int EVP_PKEY_print_private(BIO *out, const EVP_PKEY *pkey, int indent,
  322. ASN1_PCTX *pctx) {
  323. if (pkey->ameth && pkey->ameth->priv_print) {
  324. return pkey->ameth->priv_print(out, pkey, indent, pctx);
  325. }
  326. return print_unsupported(out, pkey, indent, "Private Key");
  327. }
  328. int EVP_PKEY_print_params(BIO *out, const EVP_PKEY *pkey, int indent,
  329. ASN1_PCTX *pctx) {
  330. if (pkey->ameth && pkey->ameth->param_print) {
  331. return pkey->ameth->param_print(out, pkey, indent, pctx);
  332. }
  333. return print_unsupported(out, pkey, indent, "Parameters");
  334. }
  335. int EVP_PKEY_CTX_set_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD *md) {
  336. return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_TYPE_SIG, EVP_PKEY_CTRL_MD, 0,
  337. (void *)md);
  338. }
  339. int EVP_PKEY_CTX_get_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD **out_md) {
  340. return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_TYPE_SIG, EVP_PKEY_CTRL_GET_MD,
  341. 0, (void *)out_md);
  342. }
  343. void OpenSSL_add_all_algorithms(void) {}
  344. void OpenSSL_add_all_ciphers(void) {}
  345. void OpenSSL_add_all_digests(void) {}
  346. void EVP_cleanup(void) {}