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.
 
 
 
 
 
 

419 rivejä
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/dh.h>
  61. #include <openssl/dsa.h>
  62. #include <openssl/ec.h>
  63. #include <openssl/err.h>
  64. #include <openssl/mem.h>
  65. #include <openssl/obj.h>
  66. #include <openssl/rsa.h>
  67. #include <openssl/thread.h>
  68. #include "internal.h"
  69. #include "../internal.h"
  70. extern const EVP_PKEY_ASN1_METHOD dsa_asn1_meth;
  71. extern const EVP_PKEY_ASN1_METHOD ec_asn1_meth;
  72. extern const EVP_PKEY_ASN1_METHOD rsa_asn1_meth;
  73. EVP_PKEY *EVP_PKEY_new(void) {
  74. EVP_PKEY *ret;
  75. ret = OPENSSL_malloc(sizeof(EVP_PKEY));
  76. if (ret == NULL) {
  77. OPENSSL_PUT_ERROR(EVP, EVP_PKEY_new, ERR_R_MALLOC_FAILURE);
  78. return NULL;
  79. }
  80. memset(ret, 0, sizeof(EVP_PKEY));
  81. ret->type = EVP_PKEY_NONE;
  82. ret->references = 1;
  83. return ret;
  84. }
  85. static void free_it(EVP_PKEY *pkey) {
  86. if (pkey->ameth && pkey->ameth->pkey_free) {
  87. pkey->ameth->pkey_free(pkey);
  88. pkey->pkey.ptr = NULL;
  89. pkey->type = EVP_PKEY_NONE;
  90. }
  91. }
  92. void EVP_PKEY_free(EVP_PKEY *pkey) {
  93. if (pkey == NULL) {
  94. return;
  95. }
  96. if (!CRYPTO_refcount_dec_and_test_zero(&pkey->references)) {
  97. return;
  98. }
  99. free_it(pkey);
  100. OPENSSL_free(pkey);
  101. }
  102. EVP_PKEY *EVP_PKEY_up_ref(EVP_PKEY *pkey) {
  103. CRYPTO_refcount_inc(&pkey->references);
  104. return pkey;
  105. }
  106. int EVP_PKEY_is_opaque(const EVP_PKEY *pkey) {
  107. if (pkey->ameth && pkey->ameth->pkey_opaque) {
  108. return pkey->ameth->pkey_opaque(pkey);
  109. }
  110. return 0;
  111. }
  112. int EVP_PKEY_supports_digest(const EVP_PKEY *pkey, const EVP_MD *md) {
  113. if (pkey->ameth && pkey->ameth->pkey_supports_digest) {
  114. return pkey->ameth->pkey_supports_digest(pkey, md);
  115. }
  116. return 1;
  117. }
  118. int EVP_PKEY_cmp(const EVP_PKEY *a, const EVP_PKEY *b) {
  119. if (a->type != b->type) {
  120. return -1;
  121. }
  122. if (a->ameth) {
  123. int ret;
  124. /* Compare parameters if the algorithm has them */
  125. if (a->ameth->param_cmp) {
  126. ret = a->ameth->param_cmp(a, b);
  127. if (ret <= 0) {
  128. return ret;
  129. }
  130. }
  131. if (a->ameth->pub_cmp) {
  132. return a->ameth->pub_cmp(a, b);
  133. }
  134. }
  135. return -2;
  136. }
  137. int EVP_PKEY_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from) {
  138. if (to->type != from->type) {
  139. OPENSSL_PUT_ERROR(EVP, EVP_PKEY_copy_parameters, EVP_R_DIFFERENT_KEY_TYPES);
  140. goto err;
  141. }
  142. if (EVP_PKEY_missing_parameters(from)) {
  143. OPENSSL_PUT_ERROR(EVP, EVP_PKEY_copy_parameters, EVP_R_MISSING_PARAMETERS);
  144. goto err;
  145. }
  146. if (from->ameth && from->ameth->param_copy) {
  147. return from->ameth->param_copy(to, from);
  148. }
  149. err:
  150. return 0;
  151. }
  152. int EVP_PKEY_missing_parameters(const EVP_PKEY *pkey) {
  153. if (pkey->ameth && pkey->ameth->param_missing) {
  154. return pkey->ameth->param_missing(pkey);
  155. }
  156. return 0;
  157. }
  158. int EVP_PKEY_size(const EVP_PKEY *pkey) {
  159. if (pkey && pkey->ameth && pkey->ameth->pkey_size) {
  160. return pkey->ameth->pkey_size(pkey);
  161. }
  162. return 0;
  163. }
  164. int EVP_PKEY_bits(EVP_PKEY *pkey) {
  165. if (pkey && pkey->ameth && pkey->ameth->pkey_bits) {
  166. return pkey->ameth->pkey_bits(pkey);
  167. }
  168. return 0;
  169. }
  170. int EVP_PKEY_id(const EVP_PKEY *pkey) {
  171. return pkey->type;
  172. }
  173. /* TODO(fork): remove the first argument. */
  174. const EVP_PKEY_ASN1_METHOD *EVP_PKEY_asn1_find(ENGINE **pengine, int nid) {
  175. switch (nid) {
  176. case EVP_PKEY_RSA:
  177. case EVP_PKEY_RSA2:
  178. return &rsa_asn1_meth;
  179. case EVP_PKEY_EC:
  180. return &ec_asn1_meth;
  181. case EVP_PKEY_DSA:
  182. return &dsa_asn1_meth;
  183. default:
  184. return NULL;
  185. }
  186. }
  187. int EVP_PKEY_type(int nid) {
  188. const EVP_PKEY_ASN1_METHOD *meth = EVP_PKEY_asn1_find(NULL, nid);
  189. if (meth == NULL) {
  190. return NID_undef;
  191. }
  192. return meth->pkey_id;
  193. }
  194. int EVP_PKEY_set1_RSA(EVP_PKEY *pkey, RSA *key) {
  195. if (EVP_PKEY_assign_RSA(pkey, key)) {
  196. RSA_up_ref(key);
  197. return 1;
  198. }
  199. return 0;
  200. }
  201. int EVP_PKEY_assign_RSA(EVP_PKEY *pkey, RSA *key) {
  202. return EVP_PKEY_assign(pkey, EVP_PKEY_RSA, key);
  203. }
  204. RSA *EVP_PKEY_get1_RSA(EVP_PKEY *pkey) {
  205. if (pkey->type != EVP_PKEY_RSA) {
  206. OPENSSL_PUT_ERROR(EVP, EVP_PKEY_get1_RSA, EVP_R_EXPECTING_AN_RSA_KEY);
  207. return NULL;
  208. }
  209. RSA_up_ref(pkey->pkey.rsa);
  210. return pkey->pkey.rsa;
  211. }
  212. int EVP_PKEY_set1_DSA(EVP_PKEY *pkey, DSA *key) {
  213. if (EVP_PKEY_assign_DSA(pkey, key)) {
  214. DSA_up_ref(key);
  215. return 1;
  216. }
  217. return 0;
  218. }
  219. int EVP_PKEY_assign_DSA(EVP_PKEY *pkey, DSA *key) {
  220. return EVP_PKEY_assign(pkey, EVP_PKEY_DSA, key);
  221. }
  222. DSA *EVP_PKEY_get1_DSA(EVP_PKEY *pkey) {
  223. if (pkey->type != EVP_PKEY_DSA) {
  224. OPENSSL_PUT_ERROR(EVP, EVP_PKEY_get1_DSA, EVP_R_EXPECTING_A_DSA_KEY);
  225. return NULL;
  226. }
  227. DSA_up_ref(pkey->pkey.dsa);
  228. return pkey->pkey.dsa;
  229. }
  230. int EVP_PKEY_set1_EC_KEY(EVP_PKEY *pkey, EC_KEY *key) {
  231. if (EVP_PKEY_assign_EC_KEY(pkey, key)) {
  232. EC_KEY_up_ref(key);
  233. return 1;
  234. }
  235. return 0;
  236. }
  237. int EVP_PKEY_assign_EC_KEY(EVP_PKEY *pkey, EC_KEY *key) {
  238. return EVP_PKEY_assign(pkey, EVP_PKEY_EC, key);
  239. }
  240. EC_KEY *EVP_PKEY_get1_EC_KEY(EVP_PKEY *pkey) {
  241. if (pkey->type != EVP_PKEY_EC) {
  242. OPENSSL_PUT_ERROR(EVP, EVP_PKEY_get1_EC_KEY, EVP_R_EXPECTING_AN_EC_KEY_KEY);
  243. return NULL;
  244. }
  245. EC_KEY_up_ref(pkey->pkey.ec);
  246. return pkey->pkey.ec;
  247. }
  248. int EVP_PKEY_set1_DH(EVP_PKEY *pkey, DH *key) {
  249. if (EVP_PKEY_assign_DH(pkey, key)) {
  250. DH_up_ref(key);
  251. return 1;
  252. }
  253. return 0;
  254. }
  255. int EVP_PKEY_assign_DH(EVP_PKEY *pkey, DH *key) {
  256. return EVP_PKEY_assign(pkey, EVP_PKEY_DH, key);
  257. }
  258. DH *EVP_PKEY_get1_DH(EVP_PKEY *pkey) {
  259. if (pkey->type != EVP_PKEY_DH) {
  260. OPENSSL_PUT_ERROR(EVP, EVP_PKEY_get1_DH, EVP_R_EXPECTING_A_DH_KEY);
  261. return NULL;
  262. }
  263. DH_up_ref(pkey->pkey.dh);
  264. return pkey->pkey.dh;
  265. }
  266. int EVP_PKEY_assign(EVP_PKEY *pkey, int type, void *key) {
  267. if (!EVP_PKEY_set_type(pkey, type)) {
  268. return 0;
  269. }
  270. pkey->pkey.ptr = key;
  271. return key != NULL;
  272. }
  273. const EVP_PKEY_ASN1_METHOD *EVP_PKEY_asn1_find_str(ENGINE **pengine,
  274. const char *name,
  275. size_t len) {
  276. if (len == 3 && memcmp(name, "RSA", 3) == 0) {
  277. return &rsa_asn1_meth;
  278. } if (len == 2 && memcmp(name, "EC", 2) == 0) {
  279. return &ec_asn1_meth;
  280. }
  281. return NULL;
  282. }
  283. int EVP_PKEY_set_type(EVP_PKEY *pkey, int type) {
  284. const EVP_PKEY_ASN1_METHOD *ameth;
  285. if (pkey && pkey->pkey.ptr) {
  286. free_it(pkey);
  287. }
  288. ameth = EVP_PKEY_asn1_find(NULL, type);
  289. if (ameth == NULL) {
  290. OPENSSL_PUT_ERROR(EVP, EVP_PKEY_set_type, EVP_R_UNSUPPORTED_ALGORITHM);
  291. ERR_add_error_dataf("algorithm %d (%s)", type, OBJ_nid2sn(type));
  292. return 0;
  293. }
  294. if (pkey) {
  295. pkey->ameth = ameth;
  296. pkey->type = pkey->ameth->pkey_id;
  297. }
  298. return 1;
  299. }
  300. int EVP_PKEY_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b) {
  301. if (a->type != b->type) {
  302. return -1;
  303. }
  304. if (a->ameth && a->ameth->param_cmp) {
  305. return a->ameth->param_cmp(a, b);
  306. }
  307. return -2;
  308. }
  309. static int print_unsupported(BIO *out, const EVP_PKEY *pkey, int indent,
  310. const char *kstr) {
  311. BIO_indent(out, indent, 128);
  312. BIO_printf(out, "%s algorithm \"%s\" unsupported\n", kstr,
  313. OBJ_nid2ln(pkey->type));
  314. return 1;
  315. }
  316. int EVP_PKEY_print_public(BIO *out, const EVP_PKEY *pkey, int indent,
  317. ASN1_PCTX *pctx) {
  318. if (pkey->ameth && pkey->ameth->pub_print) {
  319. return pkey->ameth->pub_print(out, pkey, indent, pctx);
  320. }
  321. return print_unsupported(out, pkey, indent, "Public Key");
  322. }
  323. int EVP_PKEY_print_private(BIO *out, const EVP_PKEY *pkey, int indent,
  324. ASN1_PCTX *pctx) {
  325. if (pkey->ameth && pkey->ameth->priv_print) {
  326. return pkey->ameth->priv_print(out, pkey, indent, pctx);
  327. }
  328. return print_unsupported(out, pkey, indent, "Private Key");
  329. }
  330. int EVP_PKEY_print_params(BIO *out, const EVP_PKEY *pkey, int indent,
  331. ASN1_PCTX *pctx) {
  332. if (pkey->ameth && pkey->ameth->param_print) {
  333. return pkey->ameth->param_print(out, pkey, indent, pctx);
  334. }
  335. return print_unsupported(out, pkey, indent, "Parameters");
  336. }
  337. int EVP_PKEY_CTX_set_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD *md) {
  338. return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_TYPE_SIG, EVP_PKEY_CTRL_MD, 0,
  339. (void *)md);
  340. }
  341. int EVP_PKEY_CTX_get_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD **out_md) {
  342. return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_TYPE_SIG, EVP_PKEY_CTRL_GET_MD,
  343. 0, (void *)out_md);
  344. }
  345. EVP_PKEY *EVP_PKEY_dup(EVP_PKEY *pkey) {
  346. return EVP_PKEY_up_ref(pkey);
  347. }
  348. void OpenSSL_add_all_algorithms(void) {}
  349. void OpenSSL_add_all_ciphers(void) {}
  350. void OpenSSL_add_all_digests(void) {}
  351. void EVP_cleanup(void) {}