x_pubkey.c 9.6 KB

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