2
0

x_pubkey.c 9.5 KB

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