p_lib.c 9.5 KB

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