x_pubkey.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /*
  2. * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdio.h>
  10. #include "internal/cryptlib.h"
  11. #include <openssl/asn1t.h>
  12. #include <openssl/x509.h>
  13. #include "internal/asn1_int.h"
  14. #include "internal/evp_int.h"
  15. #include "internal/x509_int.h"
  16. #include <openssl/rsa.h>
  17. #include <openssl/dsa.h>
  18. struct X509_pubkey_st {
  19. X509_ALGOR *algor;
  20. ASN1_BIT_STRING *public_key;
  21. EVP_PKEY *pkey;
  22. };
  23. static int x509_pubkey_decode(EVP_PKEY **pk, X509_PUBKEY *key);
  24. /* Minor tweak to operation: free up EVP_PKEY */
  25. static int pubkey_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
  26. void *exarg)
  27. {
  28. if (operation == ASN1_OP_FREE_POST) {
  29. X509_PUBKEY *pubkey = (X509_PUBKEY *)*pval;
  30. EVP_PKEY_free(pubkey->pkey);
  31. } else if (operation == ASN1_OP_D2I_POST) {
  32. /* Attempt to decode public key and cache in pubkey structure. */
  33. X509_PUBKEY *pubkey = (X509_PUBKEY *)*pval;
  34. EVP_PKEY_free(pubkey->pkey);
  35. /*
  36. * Opportunistically decode the key but remove any non fatal errors
  37. * from the queue. Subsequent explicit attempts to decode/use the key
  38. * will return an appropriate error.
  39. */
  40. ERR_set_mark();
  41. if (x509_pubkey_decode(&pubkey->pkey, pubkey) == -1)
  42. return 0;
  43. ERR_pop_to_mark();
  44. }
  45. return 1;
  46. }
  47. ASN1_SEQUENCE_cb(X509_PUBKEY, pubkey_cb) = {
  48. ASN1_SIMPLE(X509_PUBKEY, algor, X509_ALGOR),
  49. ASN1_SIMPLE(X509_PUBKEY, public_key, ASN1_BIT_STRING)
  50. } ASN1_SEQUENCE_END_cb(X509_PUBKEY, X509_PUBKEY)
  51. IMPLEMENT_ASN1_FUNCTIONS(X509_PUBKEY)
  52. int X509_PUBKEY_set(X509_PUBKEY **x, EVP_PKEY *pkey)
  53. {
  54. X509_PUBKEY *pk = NULL;
  55. if (x == NULL)
  56. return 0;
  57. if ((pk = X509_PUBKEY_new()) == NULL)
  58. goto error;
  59. if (pkey->ameth) {
  60. if (pkey->ameth->pub_encode) {
  61. if (!pkey->ameth->pub_encode(pk, pkey)) {
  62. X509err(X509_F_X509_PUBKEY_SET,
  63. X509_R_PUBLIC_KEY_ENCODE_ERROR);
  64. goto error;
  65. }
  66. } else {
  67. X509err(X509_F_X509_PUBKEY_SET, X509_R_METHOD_NOT_SUPPORTED);
  68. goto error;
  69. }
  70. } else {
  71. X509err(X509_F_X509_PUBKEY_SET, X509_R_UNSUPPORTED_ALGORITHM);
  72. goto error;
  73. }
  74. X509_PUBKEY_free(*x);
  75. *x = pk;
  76. pk->pkey = pkey;
  77. EVP_PKEY_up_ref(pkey);
  78. return 1;
  79. error:
  80. X509_PUBKEY_free(pk);
  81. return 0;
  82. }
  83. /*
  84. * Attempt to decode a public key.
  85. * Returns 1 on success, 0 for a decode failure and -1 for a fatal
  86. * error e.g. malloc failure.
  87. */
  88. static int x509_pubkey_decode(EVP_PKEY **ppkey, X509_PUBKEY *key)
  89. {
  90. EVP_PKEY *pkey = EVP_PKEY_new();
  91. if (pkey == NULL) {
  92. X509err(X509_F_X509_PUBKEY_DECODE, ERR_R_MALLOC_FAILURE);
  93. return -1;
  94. }
  95. if (!EVP_PKEY_set_type(pkey, OBJ_obj2nid(key->algor->algorithm))) {
  96. X509err(X509_F_X509_PUBKEY_DECODE, X509_R_UNSUPPORTED_ALGORITHM);
  97. goto error;
  98. }
  99. if (pkey->ameth->pub_decode) {
  100. /*
  101. * Treat any failure of pub_decode as a decode error. In
  102. * future we could have different return codes for decode
  103. * errors and fatal errors such as malloc failure.
  104. */
  105. if (!pkey->ameth->pub_decode(pkey, key)) {
  106. X509err(X509_F_X509_PUBKEY_DECODE, X509_R_PUBLIC_KEY_DECODE_ERROR);
  107. goto error;
  108. }
  109. } else {
  110. X509err(X509_F_X509_PUBKEY_DECODE, X509_R_METHOD_NOT_SUPPORTED);
  111. goto error;
  112. }
  113. *ppkey = pkey;
  114. return 1;
  115. error:
  116. EVP_PKEY_free(pkey);
  117. return 0;
  118. }
  119. EVP_PKEY *X509_PUBKEY_get0(X509_PUBKEY *key)
  120. {
  121. EVP_PKEY *ret = NULL;
  122. if (key == NULL || key->public_key == NULL)
  123. return NULL;
  124. if (key->pkey != NULL)
  125. return key->pkey;
  126. /*
  127. * When the key ASN.1 is initially parsed an attempt is made to
  128. * decode the public key and cache the EVP_PKEY structure. If this
  129. * operation fails the cached value will be NULL. Parsing continues
  130. * to allow parsing of unknown key types or unsupported forms.
  131. * We repeat the decode operation so the appropriate errors are left
  132. * in the queue.
  133. */
  134. x509_pubkey_decode(&ret, key);
  135. /* If decode doesn't fail something bad happened */
  136. if (ret != NULL) {
  137. X509err(X509_F_X509_PUBKEY_GET0, ERR_R_INTERNAL_ERROR);
  138. EVP_PKEY_free(ret);
  139. }
  140. return NULL;
  141. }
  142. EVP_PKEY *X509_PUBKEY_get(X509_PUBKEY *key)
  143. {
  144. EVP_PKEY *ret = X509_PUBKEY_get0(key);
  145. if (ret != NULL)
  146. EVP_PKEY_up_ref(ret);
  147. return ret;
  148. }
  149. /*
  150. * Now two pseudo ASN1 routines that take an EVP_PKEY structure and encode or
  151. * decode as X509_PUBKEY
  152. */
  153. EVP_PKEY *d2i_PUBKEY(EVP_PKEY **a, const unsigned char **pp, long length)
  154. {
  155. X509_PUBKEY *xpk;
  156. EVP_PKEY *pktmp;
  157. const unsigned char *q;
  158. q = *pp;
  159. xpk = d2i_X509_PUBKEY(NULL, &q, length);
  160. if (!xpk)
  161. return NULL;
  162. pktmp = X509_PUBKEY_get(xpk);
  163. X509_PUBKEY_free(xpk);
  164. if (!pktmp)
  165. return NULL;
  166. *pp = q;
  167. if (a) {
  168. EVP_PKEY_free(*a);
  169. *a = pktmp;
  170. }
  171. return pktmp;
  172. }
  173. int i2d_PUBKEY(EVP_PKEY *a, unsigned char **pp)
  174. {
  175. X509_PUBKEY *xpk = NULL;
  176. int ret;
  177. if (!a)
  178. return 0;
  179. if (!X509_PUBKEY_set(&xpk, a))
  180. return -1;
  181. ret = i2d_X509_PUBKEY(xpk, pp);
  182. X509_PUBKEY_free(xpk);
  183. return ret;
  184. }
  185. /*
  186. * The following are equivalents but which return RSA and DSA keys
  187. */
  188. #ifndef OPENSSL_NO_RSA
  189. RSA *d2i_RSA_PUBKEY(RSA **a, const unsigned char **pp, long length)
  190. {
  191. EVP_PKEY *pkey;
  192. RSA *key;
  193. const unsigned char *q;
  194. q = *pp;
  195. pkey = d2i_PUBKEY(NULL, &q, length);
  196. if (!pkey)
  197. return NULL;
  198. key = EVP_PKEY_get1_RSA(pkey);
  199. EVP_PKEY_free(pkey);
  200. if (!key)
  201. return NULL;
  202. *pp = q;
  203. if (a) {
  204. RSA_free(*a);
  205. *a = key;
  206. }
  207. return key;
  208. }
  209. int i2d_RSA_PUBKEY(RSA *a, unsigned char **pp)
  210. {
  211. EVP_PKEY *pktmp;
  212. int ret;
  213. if (!a)
  214. return 0;
  215. pktmp = EVP_PKEY_new();
  216. if (pktmp == NULL) {
  217. ASN1err(ASN1_F_I2D_RSA_PUBKEY, ERR_R_MALLOC_FAILURE);
  218. return -1;
  219. }
  220. EVP_PKEY_set1_RSA(pktmp, a);
  221. ret = i2d_PUBKEY(pktmp, pp);
  222. EVP_PKEY_free(pktmp);
  223. return ret;
  224. }
  225. #endif
  226. #ifndef OPENSSL_NO_DSA
  227. DSA *d2i_DSA_PUBKEY(DSA **a, const unsigned char **pp, long length)
  228. {
  229. EVP_PKEY *pkey;
  230. DSA *key;
  231. const unsigned char *q;
  232. q = *pp;
  233. pkey = d2i_PUBKEY(NULL, &q, length);
  234. if (!pkey)
  235. return NULL;
  236. key = EVP_PKEY_get1_DSA(pkey);
  237. EVP_PKEY_free(pkey);
  238. if (!key)
  239. return NULL;
  240. *pp = q;
  241. if (a) {
  242. DSA_free(*a);
  243. *a = key;
  244. }
  245. return key;
  246. }
  247. int i2d_DSA_PUBKEY(DSA *a, unsigned char **pp)
  248. {
  249. EVP_PKEY *pktmp;
  250. int ret;
  251. if (!a)
  252. return 0;
  253. pktmp = EVP_PKEY_new();
  254. if (pktmp == NULL) {
  255. ASN1err(ASN1_F_I2D_DSA_PUBKEY, ERR_R_MALLOC_FAILURE);
  256. return -1;
  257. }
  258. EVP_PKEY_set1_DSA(pktmp, a);
  259. ret = i2d_PUBKEY(pktmp, pp);
  260. EVP_PKEY_free(pktmp);
  261. return ret;
  262. }
  263. #endif
  264. #ifndef OPENSSL_NO_EC
  265. EC_KEY *d2i_EC_PUBKEY(EC_KEY **a, const unsigned char **pp, long length)
  266. {
  267. EVP_PKEY *pkey;
  268. EC_KEY *key;
  269. const unsigned char *q;
  270. q = *pp;
  271. pkey = d2i_PUBKEY(NULL, &q, length);
  272. if (!pkey)
  273. return NULL;
  274. key = EVP_PKEY_get1_EC_KEY(pkey);
  275. EVP_PKEY_free(pkey);
  276. if (!key)
  277. return NULL;
  278. *pp = q;
  279. if (a) {
  280. EC_KEY_free(*a);
  281. *a = key;
  282. }
  283. return key;
  284. }
  285. int i2d_EC_PUBKEY(EC_KEY *a, unsigned char **pp)
  286. {
  287. EVP_PKEY *pktmp;
  288. int ret;
  289. if (!a)
  290. return 0;
  291. if ((pktmp = EVP_PKEY_new()) == NULL) {
  292. ASN1err(ASN1_F_I2D_EC_PUBKEY, ERR_R_MALLOC_FAILURE);
  293. return -1;
  294. }
  295. EVP_PKEY_set1_EC_KEY(pktmp, a);
  296. ret = i2d_PUBKEY(pktmp, pp);
  297. EVP_PKEY_free(pktmp);
  298. return ret;
  299. }
  300. #endif
  301. int X509_PUBKEY_set0_param(X509_PUBKEY *pub, ASN1_OBJECT *aobj,
  302. int ptype, void *pval,
  303. unsigned char *penc, int penclen)
  304. {
  305. if (!X509_ALGOR_set0(pub->algor, aobj, ptype, pval))
  306. return 0;
  307. if (penc) {
  308. OPENSSL_free(pub->public_key->data);
  309. pub->public_key->data = penc;
  310. pub->public_key->length = penclen;
  311. /* Set number of unused bits to zero */
  312. pub->public_key->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
  313. pub->public_key->flags |= ASN1_STRING_FLAG_BITS_LEFT;
  314. }
  315. return 1;
  316. }
  317. int X509_PUBKEY_get0_param(ASN1_OBJECT **ppkalg,
  318. const unsigned char **pk, int *ppklen,
  319. X509_ALGOR **pa, X509_PUBKEY *pub)
  320. {
  321. if (ppkalg)
  322. *ppkalg = pub->algor->algorithm;
  323. if (pk) {
  324. *pk = pub->public_key->data;
  325. *ppklen = pub->public_key->length;
  326. }
  327. if (pa)
  328. *pa = pub->algor;
  329. return 1;
  330. }
  331. ASN1_BIT_STRING *X509_get0_pubkey_bitstr(const X509 *x)
  332. {
  333. if (x == NULL)
  334. return NULL;
  335. return x->cert_info.key->public_key;
  336. }