p_lib.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  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. static void EVP_PKEY_free_it(EVP_PKEY *x);
  76. int EVP_PKEY_bits(EVP_PKEY *pkey)
  77. {
  78. if (0)
  79. return 0;
  80. #ifndef OPENSSL_NO_RSA
  81. else if (pkey->type == EVP_PKEY_RSA)
  82. return(BN_num_bits(pkey->pkey.rsa->n));
  83. #endif
  84. #ifndef OPENSSL_NO_DSA
  85. else if (pkey->type == EVP_PKEY_DSA)
  86. return(BN_num_bits(pkey->pkey.dsa->p));
  87. #endif
  88. #ifndef OPENSSL_NO_EC
  89. else if (pkey->type == EVP_PKEY_EC)
  90. {
  91. BIGNUM *order = BN_new();
  92. const EC_GROUP *group;
  93. int ret;
  94. if (!order)
  95. {
  96. ERR_clear_error();
  97. return 0;
  98. }
  99. group = EC_KEY_get0_group(pkey->pkey.ec);
  100. if (!EC_GROUP_get_order(group, order, NULL))
  101. {
  102. ERR_clear_error();
  103. return 0;
  104. }
  105. ret = BN_num_bits(order);
  106. BN_free(order);
  107. return ret;
  108. }
  109. #endif
  110. return(0);
  111. }
  112. int EVP_PKEY_size(EVP_PKEY *pkey)
  113. {
  114. if (pkey == NULL)
  115. return(0);
  116. #ifndef OPENSSL_NO_RSA
  117. if (pkey->type == EVP_PKEY_RSA)
  118. return(RSA_size(pkey->pkey.rsa));
  119. else
  120. #endif
  121. #ifndef OPENSSL_NO_DSA
  122. if (pkey->type == EVP_PKEY_DSA)
  123. return(DSA_size(pkey->pkey.dsa));
  124. #endif
  125. #ifndef OPENSSL_NO_ECDSA
  126. if (pkey->type == EVP_PKEY_EC)
  127. return(ECDSA_size(pkey->pkey.ec));
  128. #endif
  129. return(0);
  130. }
  131. int EVP_PKEY_save_parameters(EVP_PKEY *pkey, int mode)
  132. {
  133. #ifndef OPENSSL_NO_DSA
  134. if (pkey->type == EVP_PKEY_DSA)
  135. {
  136. int ret=pkey->save_parameters;
  137. if (mode >= 0)
  138. pkey->save_parameters=mode;
  139. return(ret);
  140. }
  141. #endif
  142. #ifndef OPENSSL_NO_EC
  143. if (pkey->type == EVP_PKEY_EC)
  144. {
  145. int ret = pkey->save_parameters;
  146. if (mode >= 0)
  147. pkey->save_parameters = mode;
  148. return(ret);
  149. }
  150. #endif
  151. return(0);
  152. }
  153. int EVP_PKEY_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from)
  154. {
  155. if (to->type != from->type)
  156. {
  157. EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS,EVP_R_DIFFERENT_KEY_TYPES);
  158. goto err;
  159. }
  160. if (EVP_PKEY_missing_parameters(from))
  161. {
  162. EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS,EVP_R_MISSING_PARAMETERS);
  163. goto err;
  164. }
  165. #ifndef OPENSSL_NO_DSA
  166. if (to->type == EVP_PKEY_DSA)
  167. {
  168. BIGNUM *a;
  169. if ((a=BN_dup(from->pkey.dsa->p)) == NULL) goto err;
  170. if (to->pkey.dsa->p != NULL) BN_free(to->pkey.dsa->p);
  171. to->pkey.dsa->p=a;
  172. if ((a=BN_dup(from->pkey.dsa->q)) == NULL) goto err;
  173. if (to->pkey.dsa->q != NULL) BN_free(to->pkey.dsa->q);
  174. to->pkey.dsa->q=a;
  175. if ((a=BN_dup(from->pkey.dsa->g)) == NULL) goto err;
  176. if (to->pkey.dsa->g != NULL) BN_free(to->pkey.dsa->g);
  177. to->pkey.dsa->g=a;
  178. }
  179. #endif
  180. #ifndef OPENSSL_NO_EC
  181. if (to->type == EVP_PKEY_EC)
  182. {
  183. EC_GROUP *group = EC_GROUP_dup(EC_KEY_get0_group(from->pkey.ec));
  184. if (group == NULL)
  185. goto err;
  186. if (EC_KEY_set_group(to->pkey.ec, group) == 0)
  187. goto err;
  188. EC_GROUP_free(group);
  189. }
  190. #endif
  191. return(1);
  192. err:
  193. return(0);
  194. }
  195. int EVP_PKEY_missing_parameters(const EVP_PKEY *pkey)
  196. {
  197. #ifndef OPENSSL_NO_DSA
  198. if (pkey->type == EVP_PKEY_DSA)
  199. {
  200. DSA *dsa;
  201. dsa=pkey->pkey.dsa;
  202. if ((dsa->p == NULL) || (dsa->q == NULL) || (dsa->g == NULL))
  203. return(1);
  204. }
  205. #endif
  206. #ifndef OPENSSL_NO_EC
  207. if (pkey->type == EVP_PKEY_EC)
  208. {
  209. if (EC_KEY_get0_group(pkey->pkey.ec) == NULL)
  210. return(1);
  211. }
  212. #endif
  213. return(0);
  214. }
  215. int EVP_PKEY_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b)
  216. {
  217. #ifndef OPENSSL_NO_DSA
  218. if ((a->type == EVP_PKEY_DSA) && (b->type == EVP_PKEY_DSA))
  219. {
  220. if ( BN_cmp(a->pkey.dsa->p,b->pkey.dsa->p) ||
  221. BN_cmp(a->pkey.dsa->q,b->pkey.dsa->q) ||
  222. BN_cmp(a->pkey.dsa->g,b->pkey.dsa->g))
  223. return(0);
  224. else
  225. return(1);
  226. }
  227. #endif
  228. #ifndef OPENSSL_NO_EC
  229. if (a->type == EVP_PKEY_EC && b->type == EVP_PKEY_EC)
  230. {
  231. const EC_GROUP *group_a = EC_KEY_get0_group(a->pkey.ec),
  232. *group_b = EC_KEY_get0_group(b->pkey.ec);
  233. if (EC_GROUP_cmp(group_a, group_b, NULL))
  234. return 0;
  235. else
  236. return 1;
  237. }
  238. #endif
  239. return(-1);
  240. }
  241. int EVP_PKEY_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
  242. {
  243. if (a->type != b->type)
  244. return -1;
  245. if (EVP_PKEY_cmp_parameters(a, b) == 0)
  246. return 0;
  247. switch (a->type)
  248. {
  249. #ifndef OPENSSL_NO_RSA
  250. case EVP_PKEY_RSA:
  251. if (BN_cmp(b->pkey.rsa->n,a->pkey.rsa->n) != 0
  252. || BN_cmp(b->pkey.rsa->e,a->pkey.rsa->e) != 0)
  253. return 0;
  254. break;
  255. #endif
  256. #ifndef OPENSSL_NO_DSA
  257. case EVP_PKEY_DSA:
  258. if (BN_cmp(b->pkey.dsa->pub_key,a->pkey.dsa->pub_key) != 0)
  259. return 0;
  260. break;
  261. #endif
  262. #ifndef OPENSSL_NO_EC
  263. case EVP_PKEY_EC:
  264. {
  265. int r;
  266. const EC_GROUP *group = EC_KEY_get0_group(b->pkey.ec);
  267. const EC_POINT *pa = EC_KEY_get0_public_key(a->pkey.ec),
  268. *pb = EC_KEY_get0_public_key(b->pkey.ec);
  269. r = EC_POINT_cmp(group, pa, pb, NULL);
  270. if (r != 0)
  271. {
  272. if (r == 1)
  273. return 0;
  274. else
  275. return -2;
  276. }
  277. }
  278. break;
  279. #endif
  280. #ifndef OPENSSL_NO_DH
  281. case EVP_PKEY_DH:
  282. return -2;
  283. #endif
  284. default:
  285. return -2;
  286. }
  287. return 1;
  288. }
  289. EVP_PKEY *EVP_PKEY_new(void)
  290. {
  291. EVP_PKEY *ret;
  292. ret=(EVP_PKEY *)OPENSSL_malloc(sizeof(EVP_PKEY));
  293. if (ret == NULL)
  294. {
  295. EVPerr(EVP_F_EVP_PKEY_NEW,ERR_R_MALLOC_FAILURE);
  296. return(NULL);
  297. }
  298. ret->type=EVP_PKEY_NONE;
  299. ret->references=1;
  300. ret->pkey.ptr=NULL;
  301. ret->attributes=NULL;
  302. ret->save_parameters=1;
  303. return(ret);
  304. }
  305. int EVP_PKEY_assign(EVP_PKEY *pkey, int type, char *key)
  306. {
  307. if (pkey == NULL) return(0);
  308. if (pkey->pkey.ptr != NULL)
  309. EVP_PKEY_free_it(pkey);
  310. pkey->type=EVP_PKEY_type(type);
  311. pkey->save_type=type;
  312. pkey->pkey.ptr=key;
  313. return(key != NULL);
  314. }
  315. #ifndef OPENSSL_NO_RSA
  316. int EVP_PKEY_set1_RSA(EVP_PKEY *pkey, RSA *key)
  317. {
  318. int ret = EVP_PKEY_assign_RSA(pkey, key);
  319. if(ret)
  320. RSA_up_ref(key);
  321. return ret;
  322. }
  323. RSA *EVP_PKEY_get1_RSA(EVP_PKEY *pkey)
  324. {
  325. if(pkey->type != EVP_PKEY_RSA) {
  326. EVPerr(EVP_F_EVP_PKEY_GET1_RSA, EVP_R_EXPECTING_AN_RSA_KEY);
  327. return NULL;
  328. }
  329. RSA_up_ref(pkey->pkey.rsa);
  330. return pkey->pkey.rsa;
  331. }
  332. #endif
  333. #ifndef OPENSSL_NO_DSA
  334. int EVP_PKEY_set1_DSA(EVP_PKEY *pkey, DSA *key)
  335. {
  336. int ret = EVP_PKEY_assign_DSA(pkey, key);
  337. if(ret)
  338. DSA_up_ref(key);
  339. return ret;
  340. }
  341. DSA *EVP_PKEY_get1_DSA(EVP_PKEY *pkey)
  342. {
  343. if(pkey->type != EVP_PKEY_DSA) {
  344. EVPerr(EVP_F_EVP_PKEY_GET1_DSA, EVP_R_EXPECTING_A_DSA_KEY);
  345. return NULL;
  346. }
  347. DSA_up_ref(pkey->pkey.dsa);
  348. return pkey->pkey.dsa;
  349. }
  350. #endif
  351. #ifndef OPENSSL_NO_EC
  352. int EVP_PKEY_set1_EC_KEY(EVP_PKEY *pkey, EC_KEY *key)
  353. {
  354. int ret = EVP_PKEY_assign_EC_KEY(pkey,key);
  355. if (ret)
  356. EC_KEY_up_ref(key);
  357. return ret;
  358. }
  359. EC_KEY *EVP_PKEY_get1_EC_KEY(EVP_PKEY *pkey)
  360. {
  361. if (pkey->type != EVP_PKEY_EC)
  362. {
  363. EVPerr(EVP_F_EVP_PKEY_GET1_EC_KEY, EVP_R_EXPECTING_A_EC_KEY);
  364. return NULL;
  365. }
  366. EC_KEY_up_ref(pkey->pkey.ec);
  367. return pkey->pkey.ec;
  368. }
  369. #endif
  370. #ifndef OPENSSL_NO_DH
  371. int EVP_PKEY_set1_DH(EVP_PKEY *pkey, DH *key)
  372. {
  373. int ret = EVP_PKEY_assign_DH(pkey, key);
  374. if(ret)
  375. DH_up_ref(key);
  376. return ret;
  377. }
  378. DH *EVP_PKEY_get1_DH(EVP_PKEY *pkey)
  379. {
  380. if(pkey->type != EVP_PKEY_DH) {
  381. EVPerr(EVP_F_EVP_PKEY_GET1_DH, EVP_R_EXPECTING_A_DH_KEY);
  382. return NULL;
  383. }
  384. DH_up_ref(pkey->pkey.dh);
  385. return pkey->pkey.dh;
  386. }
  387. #endif
  388. int EVP_PKEY_type(int type)
  389. {
  390. switch (type)
  391. {
  392. case EVP_PKEY_RSA:
  393. case EVP_PKEY_RSA2:
  394. return(EVP_PKEY_RSA);
  395. case EVP_PKEY_DSA:
  396. case EVP_PKEY_DSA1:
  397. case EVP_PKEY_DSA2:
  398. case EVP_PKEY_DSA3:
  399. case EVP_PKEY_DSA4:
  400. return(EVP_PKEY_DSA);
  401. case EVP_PKEY_DH:
  402. return(EVP_PKEY_DH);
  403. case EVP_PKEY_EC:
  404. return(EVP_PKEY_EC);
  405. default:
  406. return(NID_undef);
  407. }
  408. }
  409. void EVP_PKEY_free(EVP_PKEY *x)
  410. {
  411. int i;
  412. if (x == NULL) return;
  413. i=CRYPTO_add(&x->references,-1,CRYPTO_LOCK_EVP_PKEY);
  414. #ifdef REF_PRINT
  415. REF_PRINT("EVP_PKEY",x);
  416. #endif
  417. if (i > 0) return;
  418. #ifdef REF_CHECK
  419. if (i < 0)
  420. {
  421. fprintf(stderr,"EVP_PKEY_free, bad reference count\n");
  422. abort();
  423. }
  424. #endif
  425. EVP_PKEY_free_it(x);
  426. if (x->attributes)
  427. sk_X509_ATTRIBUTE_pop_free(x->attributes, X509_ATTRIBUTE_free);
  428. OPENSSL_free(x);
  429. }
  430. static void EVP_PKEY_free_it(EVP_PKEY *x)
  431. {
  432. switch (x->type)
  433. {
  434. #ifndef OPENSSL_NO_RSA
  435. case EVP_PKEY_RSA:
  436. case EVP_PKEY_RSA2:
  437. RSA_free(x->pkey.rsa);
  438. break;
  439. #endif
  440. #ifndef OPENSSL_NO_DSA
  441. case EVP_PKEY_DSA:
  442. case EVP_PKEY_DSA2:
  443. case EVP_PKEY_DSA3:
  444. case EVP_PKEY_DSA4:
  445. DSA_free(x->pkey.dsa);
  446. break;
  447. #endif
  448. #ifndef OPENSSL_NO_EC
  449. case EVP_PKEY_EC:
  450. EC_KEY_free(x->pkey.ec);
  451. break;
  452. #endif
  453. #ifndef OPENSSL_NO_DH
  454. case EVP_PKEY_DH:
  455. DH_free(x->pkey.dh);
  456. break;
  457. #endif
  458. }
  459. }