x_pubkey.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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. */
  57. #include <stdio.h>
  58. #include "internal/cryptlib.h"
  59. #include <openssl/asn1t.h>
  60. #include <openssl/x509.h>
  61. #include "internal/asn1_int.h"
  62. #include "internal/evp_int.h"
  63. #ifndef OPENSSL_NO_RSA
  64. # include <openssl/rsa.h>
  65. #endif
  66. #ifndef OPENSSL_NO_DSA
  67. # include <openssl/dsa.h>
  68. #endif
  69. /* Minor tweak to operation: free up EVP_PKEY */
  70. static int pubkey_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
  71. void *exarg)
  72. {
  73. if (operation == ASN1_OP_FREE_POST) {
  74. X509_PUBKEY *pubkey = (X509_PUBKEY *)*pval;
  75. EVP_PKEY_free(pubkey->pkey);
  76. }
  77. return 1;
  78. }
  79. ASN1_SEQUENCE_cb(X509_PUBKEY, pubkey_cb) = {
  80. ASN1_SIMPLE(X509_PUBKEY, algor, X509_ALGOR),
  81. ASN1_SIMPLE(X509_PUBKEY, public_key, ASN1_BIT_STRING)
  82. } ASN1_SEQUENCE_END_cb(X509_PUBKEY, X509_PUBKEY)
  83. IMPLEMENT_ASN1_FUNCTIONS(X509_PUBKEY)
  84. int X509_PUBKEY_set(X509_PUBKEY **x, EVP_PKEY *pkey)
  85. {
  86. X509_PUBKEY *pk = NULL;
  87. if (x == NULL)
  88. return (0);
  89. if ((pk = X509_PUBKEY_new()) == NULL)
  90. goto error;
  91. if (pkey->ameth) {
  92. if (pkey->ameth->pub_encode) {
  93. if (!pkey->ameth->pub_encode(pk, pkey)) {
  94. X509err(X509_F_X509_PUBKEY_SET,
  95. X509_R_PUBLIC_KEY_ENCODE_ERROR);
  96. goto error;
  97. }
  98. } else {
  99. X509err(X509_F_X509_PUBKEY_SET, X509_R_METHOD_NOT_SUPPORTED);
  100. goto error;
  101. }
  102. } else {
  103. X509err(X509_F_X509_PUBKEY_SET, X509_R_UNSUPPORTED_ALGORITHM);
  104. goto error;
  105. }
  106. X509_PUBKEY_free(*x);
  107. *x = pk;
  108. return 1;
  109. error:
  110. X509_PUBKEY_free(pk);
  111. return 0;
  112. }
  113. EVP_PKEY *X509_PUBKEY_get0(X509_PUBKEY *key)
  114. {
  115. EVP_PKEY *ret = NULL;
  116. if (key == NULL)
  117. goto error;
  118. if (key->pkey != NULL)
  119. return key->pkey;
  120. if (key->public_key == NULL)
  121. goto error;
  122. if ((ret = EVP_PKEY_new()) == NULL) {
  123. X509err(X509_F_X509_PUBKEY_GET0, ERR_R_MALLOC_FAILURE);
  124. goto error;
  125. }
  126. if (!EVP_PKEY_set_type(ret, OBJ_obj2nid(key->algor->algorithm))) {
  127. X509err(X509_F_X509_PUBKEY_GET0, X509_R_UNSUPPORTED_ALGORITHM);
  128. goto error;
  129. }
  130. if (ret->ameth->pub_decode) {
  131. if (!ret->ameth->pub_decode(ret, key)) {
  132. X509err(X509_F_X509_PUBKEY_GET0, X509_R_PUBLIC_KEY_DECODE_ERROR);
  133. goto error;
  134. }
  135. } else {
  136. X509err(X509_F_X509_PUBKEY_GET0, X509_R_METHOD_NOT_SUPPORTED);
  137. goto error;
  138. }
  139. /* Check to see if another thread set key->pkey first */
  140. CRYPTO_w_lock(CRYPTO_LOCK_EVP_PKEY);
  141. if (key->pkey) {
  142. CRYPTO_w_unlock(CRYPTO_LOCK_EVP_PKEY);
  143. EVP_PKEY_free(ret);
  144. ret = key->pkey;
  145. } else {
  146. key->pkey = ret;
  147. CRYPTO_w_unlock(CRYPTO_LOCK_EVP_PKEY);
  148. }
  149. return ret;
  150. error:
  151. EVP_PKEY_free(ret);
  152. return (NULL);
  153. }
  154. EVP_PKEY *X509_PUBKEY_get(X509_PUBKEY *key)
  155. {
  156. EVP_PKEY *ret = X509_PUBKEY_get0(key);
  157. if (ret != NULL)
  158. EVP_PKEY_up_ref(ret);
  159. return ret;
  160. }
  161. /*
  162. * Now two pseudo ASN1 routines that take an EVP_PKEY structure and encode or
  163. * decode as X509_PUBKEY
  164. */
  165. EVP_PKEY *d2i_PUBKEY(EVP_PKEY **a, const unsigned char **pp, long length)
  166. {
  167. X509_PUBKEY *xpk;
  168. EVP_PKEY *pktmp;
  169. const unsigned char *q;
  170. q = *pp;
  171. xpk = d2i_X509_PUBKEY(NULL, &q, length);
  172. if (!xpk)
  173. return NULL;
  174. pktmp = X509_PUBKEY_get(xpk);
  175. X509_PUBKEY_free(xpk);
  176. if (!pktmp)
  177. return NULL;
  178. *pp = q;
  179. if (a) {
  180. EVP_PKEY_free(*a);
  181. *a = pktmp;
  182. }
  183. return pktmp;
  184. }
  185. int i2d_PUBKEY(EVP_PKEY *a, unsigned char **pp)
  186. {
  187. X509_PUBKEY *xpk = NULL;
  188. int ret;
  189. if (!a)
  190. return 0;
  191. if (!X509_PUBKEY_set(&xpk, a))
  192. return 0;
  193. ret = i2d_X509_PUBKEY(xpk, pp);
  194. X509_PUBKEY_free(xpk);
  195. return ret;
  196. }
  197. /*
  198. * The following are equivalents but which return RSA and DSA keys
  199. */
  200. #ifndef OPENSSL_NO_RSA
  201. RSA *d2i_RSA_PUBKEY(RSA **a, const unsigned char **pp, long length)
  202. {
  203. EVP_PKEY *pkey;
  204. RSA *key;
  205. const unsigned char *q;
  206. q = *pp;
  207. pkey = d2i_PUBKEY(NULL, &q, length);
  208. if (!pkey)
  209. return NULL;
  210. key = EVP_PKEY_get1_RSA(pkey);
  211. EVP_PKEY_free(pkey);
  212. if (!key)
  213. return NULL;
  214. *pp = q;
  215. if (a) {
  216. RSA_free(*a);
  217. *a = key;
  218. }
  219. return key;
  220. }
  221. int i2d_RSA_PUBKEY(RSA *a, unsigned char **pp)
  222. {
  223. EVP_PKEY *pktmp;
  224. int ret;
  225. if (!a)
  226. return 0;
  227. pktmp = EVP_PKEY_new();
  228. if (pktmp == NULL) {
  229. ASN1err(ASN1_F_I2D_RSA_PUBKEY, ERR_R_MALLOC_FAILURE);
  230. return 0;
  231. }
  232. EVP_PKEY_set1_RSA(pktmp, a);
  233. ret = i2d_PUBKEY(pktmp, pp);
  234. EVP_PKEY_free(pktmp);
  235. return ret;
  236. }
  237. #endif
  238. #ifndef OPENSSL_NO_DSA
  239. DSA *d2i_DSA_PUBKEY(DSA **a, const unsigned char **pp, long length)
  240. {
  241. EVP_PKEY *pkey;
  242. DSA *key;
  243. const unsigned char *q;
  244. q = *pp;
  245. pkey = d2i_PUBKEY(NULL, &q, length);
  246. if (!pkey)
  247. return NULL;
  248. key = EVP_PKEY_get1_DSA(pkey);
  249. EVP_PKEY_free(pkey);
  250. if (!key)
  251. return NULL;
  252. *pp = q;
  253. if (a) {
  254. DSA_free(*a);
  255. *a = key;
  256. }
  257. return key;
  258. }
  259. int i2d_DSA_PUBKEY(DSA *a, unsigned char **pp)
  260. {
  261. EVP_PKEY *pktmp;
  262. int ret;
  263. if (!a)
  264. return 0;
  265. pktmp = EVP_PKEY_new();
  266. if (pktmp == NULL) {
  267. ASN1err(ASN1_F_I2D_DSA_PUBKEY, ERR_R_MALLOC_FAILURE);
  268. return 0;
  269. }
  270. EVP_PKEY_set1_DSA(pktmp, a);
  271. ret = i2d_PUBKEY(pktmp, pp);
  272. EVP_PKEY_free(pktmp);
  273. return ret;
  274. }
  275. #endif
  276. #ifndef OPENSSL_NO_EC
  277. EC_KEY *d2i_EC_PUBKEY(EC_KEY **a, const unsigned char **pp, long length)
  278. {
  279. EVP_PKEY *pkey;
  280. EC_KEY *key;
  281. const unsigned char *q;
  282. q = *pp;
  283. pkey = d2i_PUBKEY(NULL, &q, length);
  284. if (!pkey)
  285. return (NULL);
  286. key = EVP_PKEY_get1_EC_KEY(pkey);
  287. EVP_PKEY_free(pkey);
  288. if (!key)
  289. return (NULL);
  290. *pp = q;
  291. if (a) {
  292. EC_KEY_free(*a);
  293. *a = key;
  294. }
  295. return (key);
  296. }
  297. int i2d_EC_PUBKEY(EC_KEY *a, unsigned char **pp)
  298. {
  299. EVP_PKEY *pktmp;
  300. int ret;
  301. if (!a)
  302. return (0);
  303. if ((pktmp = EVP_PKEY_new()) == NULL) {
  304. ASN1err(ASN1_F_I2D_EC_PUBKEY, ERR_R_MALLOC_FAILURE);
  305. return (0);
  306. }
  307. EVP_PKEY_set1_EC_KEY(pktmp, a);
  308. ret = i2d_PUBKEY(pktmp, pp);
  309. EVP_PKEY_free(pktmp);
  310. return (ret);
  311. }
  312. #endif
  313. int X509_PUBKEY_set0_param(X509_PUBKEY *pub, ASN1_OBJECT *aobj,
  314. int ptype, void *pval,
  315. unsigned char *penc, int penclen)
  316. {
  317. if (!X509_ALGOR_set0(pub->algor, aobj, ptype, pval))
  318. return 0;
  319. if (penc) {
  320. OPENSSL_free(pub->public_key->data);
  321. pub->public_key->data = penc;
  322. pub->public_key->length = penclen;
  323. /* Set number of unused bits to zero */
  324. pub->public_key->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
  325. pub->public_key->flags |= ASN1_STRING_FLAG_BITS_LEFT;
  326. }
  327. return 1;
  328. }
  329. int X509_PUBKEY_get0_param(ASN1_OBJECT **ppkalg,
  330. const unsigned char **pk, int *ppklen,
  331. X509_ALGOR **pa, X509_PUBKEY *pub)
  332. {
  333. if (ppkalg)
  334. *ppkalg = pub->algor->algorithm;
  335. if (pk) {
  336. *pk = pub->public_key->data;
  337. *ppklen = pub->public_key->length;
  338. }
  339. if (pa)
  340. *pa = pub->algor;
  341. return 1;
  342. }