x_pubkey.c 10 KB

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