Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

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