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.
 
 
 
 
 
 

363 lines
9.4 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/dsa.h>
  60. #include <openssl/ec.h>
  61. #include <openssl/err.h>
  62. #include <openssl/mem.h>
  63. #include <openssl/nid.h>
  64. #include <openssl/rsa.h>
  65. #include <openssl/thread.h>
  66. #include "internal.h"
  67. #include "../internal.h"
  68. EVP_PKEY *EVP_PKEY_new(void) {
  69. EVP_PKEY *ret;
  70. ret = OPENSSL_malloc(sizeof(EVP_PKEY));
  71. if (ret == NULL) {
  72. OPENSSL_PUT_ERROR(EVP, ERR_R_MALLOC_FAILURE);
  73. return NULL;
  74. }
  75. OPENSSL_memset(ret, 0, sizeof(EVP_PKEY));
  76. ret->type = EVP_PKEY_NONE;
  77. ret->references = 1;
  78. return ret;
  79. }
  80. static void free_it(EVP_PKEY *pkey) {
  81. if (pkey->ameth && pkey->ameth->pkey_free) {
  82. pkey->ameth->pkey_free(pkey);
  83. pkey->pkey.ptr = NULL;
  84. pkey->type = EVP_PKEY_NONE;
  85. }
  86. }
  87. void EVP_PKEY_free(EVP_PKEY *pkey) {
  88. if (pkey == NULL) {
  89. return;
  90. }
  91. if (!CRYPTO_refcount_dec_and_test_zero(&pkey->references)) {
  92. return;
  93. }
  94. free_it(pkey);
  95. OPENSSL_free(pkey);
  96. }
  97. int EVP_PKEY_up_ref(EVP_PKEY *pkey) {
  98. CRYPTO_refcount_inc(&pkey->references);
  99. return 1;
  100. }
  101. int EVP_PKEY_is_opaque(const EVP_PKEY *pkey) {
  102. if (pkey->ameth && pkey->ameth->pkey_opaque) {
  103. return pkey->ameth->pkey_opaque(pkey);
  104. }
  105. return 0;
  106. }
  107. int EVP_PKEY_cmp(const EVP_PKEY *a, const EVP_PKEY *b) {
  108. if (a->type != b->type) {
  109. return -1;
  110. }
  111. if (a->ameth) {
  112. int ret;
  113. // Compare parameters if the algorithm has them
  114. if (a->ameth->param_cmp) {
  115. ret = a->ameth->param_cmp(a, b);
  116. if (ret <= 0) {
  117. return ret;
  118. }
  119. }
  120. if (a->ameth->pub_cmp) {
  121. return a->ameth->pub_cmp(a, b);
  122. }
  123. }
  124. return -2;
  125. }
  126. int EVP_PKEY_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from) {
  127. if (to->type != from->type) {
  128. OPENSSL_PUT_ERROR(EVP, EVP_R_DIFFERENT_KEY_TYPES);
  129. goto err;
  130. }
  131. if (EVP_PKEY_missing_parameters(from)) {
  132. OPENSSL_PUT_ERROR(EVP, EVP_R_MISSING_PARAMETERS);
  133. goto err;
  134. }
  135. if (from->ameth && from->ameth->param_copy) {
  136. return from->ameth->param_copy(to, from);
  137. }
  138. err:
  139. return 0;
  140. }
  141. int EVP_PKEY_missing_parameters(const EVP_PKEY *pkey) {
  142. if (pkey->ameth && pkey->ameth->param_missing) {
  143. return pkey->ameth->param_missing(pkey);
  144. }
  145. return 0;
  146. }
  147. int EVP_PKEY_size(const EVP_PKEY *pkey) {
  148. if (pkey && pkey->ameth && pkey->ameth->pkey_size) {
  149. return pkey->ameth->pkey_size(pkey);
  150. }
  151. return 0;
  152. }
  153. int EVP_PKEY_bits(EVP_PKEY *pkey) {
  154. if (pkey && pkey->ameth && pkey->ameth->pkey_bits) {
  155. return pkey->ameth->pkey_bits(pkey);
  156. }
  157. return 0;
  158. }
  159. int EVP_PKEY_id(const EVP_PKEY *pkey) {
  160. return pkey->type;
  161. }
  162. // evp_pkey_asn1_find returns the ASN.1 method table for the given |nid|, which
  163. // should be one of the |EVP_PKEY_*| values. It returns NULL if |nid| is
  164. // unknown.
  165. static const EVP_PKEY_ASN1_METHOD *evp_pkey_asn1_find(int nid) {
  166. switch (nid) {
  167. case EVP_PKEY_RSA:
  168. return &rsa_asn1_meth;
  169. case EVP_PKEY_EC:
  170. return &ec_asn1_meth;
  171. case EVP_PKEY_DSA:
  172. return &dsa_asn1_meth;
  173. case EVP_PKEY_ED25519:
  174. return &ed25519_asn1_meth;
  175. default:
  176. return NULL;
  177. }
  178. }
  179. int EVP_PKEY_type(int nid) {
  180. const EVP_PKEY_ASN1_METHOD *meth = evp_pkey_asn1_find(nid);
  181. if (meth == NULL) {
  182. return NID_undef;
  183. }
  184. return meth->pkey_id;
  185. }
  186. int EVP_PKEY_set1_RSA(EVP_PKEY *pkey, RSA *key) {
  187. if (EVP_PKEY_assign_RSA(pkey, key)) {
  188. RSA_up_ref(key);
  189. return 1;
  190. }
  191. return 0;
  192. }
  193. int EVP_PKEY_assign_RSA(EVP_PKEY *pkey, RSA *key) {
  194. return EVP_PKEY_assign(pkey, EVP_PKEY_RSA, key);
  195. }
  196. RSA *EVP_PKEY_get0_RSA(EVP_PKEY *pkey) {
  197. if (pkey->type != EVP_PKEY_RSA) {
  198. OPENSSL_PUT_ERROR(EVP, EVP_R_EXPECTING_AN_RSA_KEY);
  199. return NULL;
  200. }
  201. return pkey->pkey.rsa;
  202. }
  203. RSA *EVP_PKEY_get1_RSA(EVP_PKEY *pkey) {
  204. RSA *rsa = EVP_PKEY_get0_RSA(pkey);
  205. if (rsa != NULL) {
  206. RSA_up_ref(rsa);
  207. }
  208. return rsa;
  209. }
  210. int EVP_PKEY_set1_DSA(EVP_PKEY *pkey, DSA *key) {
  211. if (EVP_PKEY_assign_DSA(pkey, key)) {
  212. DSA_up_ref(key);
  213. return 1;
  214. }
  215. return 0;
  216. }
  217. int EVP_PKEY_assign_DSA(EVP_PKEY *pkey, DSA *key) {
  218. return EVP_PKEY_assign(pkey, EVP_PKEY_DSA, key);
  219. }
  220. DSA *EVP_PKEY_get0_DSA(EVP_PKEY *pkey) {
  221. if (pkey->type != EVP_PKEY_DSA) {
  222. OPENSSL_PUT_ERROR(EVP, EVP_R_EXPECTING_A_DSA_KEY);
  223. return NULL;
  224. }
  225. return pkey->pkey.dsa;
  226. }
  227. DSA *EVP_PKEY_get1_DSA(EVP_PKEY *pkey) {
  228. DSA *dsa = EVP_PKEY_get0_DSA(pkey);
  229. if (dsa != NULL) {
  230. DSA_up_ref(dsa);
  231. }
  232. return dsa;
  233. }
  234. int EVP_PKEY_set1_EC_KEY(EVP_PKEY *pkey, EC_KEY *key) {
  235. if (EVP_PKEY_assign_EC_KEY(pkey, key)) {
  236. EC_KEY_up_ref(key);
  237. return 1;
  238. }
  239. return 0;
  240. }
  241. int EVP_PKEY_assign_EC_KEY(EVP_PKEY *pkey, EC_KEY *key) {
  242. return EVP_PKEY_assign(pkey, EVP_PKEY_EC, key);
  243. }
  244. EC_KEY *EVP_PKEY_get0_EC_KEY(EVP_PKEY *pkey) {
  245. if (pkey->type != EVP_PKEY_EC) {
  246. OPENSSL_PUT_ERROR(EVP, EVP_R_EXPECTING_AN_EC_KEY_KEY);
  247. return NULL;
  248. }
  249. return pkey->pkey.ec;
  250. }
  251. EC_KEY *EVP_PKEY_get1_EC_KEY(EVP_PKEY *pkey) {
  252. EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(pkey);
  253. if (ec_key != NULL) {
  254. EC_KEY_up_ref(ec_key);
  255. }
  256. return ec_key;
  257. }
  258. DH *EVP_PKEY_get0_DH(EVP_PKEY *pkey) { return NULL; }
  259. int EVP_PKEY_assign(EVP_PKEY *pkey, int type, void *key) {
  260. if (!EVP_PKEY_set_type(pkey, type)) {
  261. return 0;
  262. }
  263. pkey->pkey.ptr = key;
  264. return key != NULL;
  265. }
  266. int EVP_PKEY_set_type(EVP_PKEY *pkey, int type) {
  267. const EVP_PKEY_ASN1_METHOD *ameth;
  268. if (pkey && pkey->pkey.ptr) {
  269. free_it(pkey);
  270. }
  271. ameth = evp_pkey_asn1_find(type);
  272. if (ameth == NULL) {
  273. OPENSSL_PUT_ERROR(EVP, EVP_R_UNSUPPORTED_ALGORITHM);
  274. ERR_add_error_dataf("algorithm %d", type);
  275. return 0;
  276. }
  277. if (pkey) {
  278. pkey->ameth = ameth;
  279. pkey->type = pkey->ameth->pkey_id;
  280. }
  281. return 1;
  282. }
  283. int EVP_PKEY_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b) {
  284. if (a->type != b->type) {
  285. return -1;
  286. }
  287. if (a->ameth && a->ameth->param_cmp) {
  288. return a->ameth->param_cmp(a, b);
  289. }
  290. return -2;
  291. }
  292. int EVP_PKEY_CTX_set_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD *md) {
  293. return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_TYPE_SIG, EVP_PKEY_CTRL_MD, 0,
  294. (void *)md);
  295. }
  296. int EVP_PKEY_CTX_get_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD **out_md) {
  297. return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_TYPE_SIG, EVP_PKEY_CTRL_GET_MD,
  298. 0, (void *)out_md);
  299. }
  300. void OpenSSL_add_all_algorithms(void) {}
  301. void OPENSSL_add_all_algorithms_conf(void) {}
  302. void OpenSSL_add_all_ciphers(void) {}
  303. void OpenSSL_add_all_digests(void) {}
  304. void EVP_cleanup(void) {}