x_pubkey.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /*
  2. * Copyright 1995-2019 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. 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. int X509_PUBKEY_set(X509_PUBKEY **x, EVP_PKEY *pkey)
  54. {
  55. X509_PUBKEY *pk = NULL;
  56. if (x == NULL)
  57. return 0;
  58. if ((pk = X509_PUBKEY_new()) == NULL)
  59. goto error;
  60. if (pkey->ameth) {
  61. if (pkey->ameth->pub_encode) {
  62. if (!pkey->ameth->pub_encode(pk, pkey)) {
  63. X509err(X509_F_X509_PUBKEY_SET,
  64. X509_R_PUBLIC_KEY_ENCODE_ERROR);
  65. goto error;
  66. }
  67. } else {
  68. X509err(X509_F_X509_PUBKEY_SET, X509_R_METHOD_NOT_SUPPORTED);
  69. goto error;
  70. }
  71. } else {
  72. X509err(X509_F_X509_PUBKEY_SET, X509_R_UNSUPPORTED_ALGORITHM);
  73. goto error;
  74. }
  75. X509_PUBKEY_free(*x);
  76. *x = pk;
  77. pk->pkey = pkey;
  78. EVP_PKEY_up_ref(pkey);
  79. return 1;
  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(EVP_PKEY *a, unsigned char **pp)
  175. {
  176. X509_PUBKEY *xpk = NULL;
  177. int ret;
  178. if (!a)
  179. return 0;
  180. if (!X509_PUBKEY_set(&xpk, a))
  181. return -1;
  182. ret = i2d_X509_PUBKEY(xpk, pp);
  183. X509_PUBKEY_free(xpk);
  184. return ret;
  185. }
  186. /*
  187. * The following are equivalents but which return RSA and DSA keys
  188. */
  189. #ifndef OPENSSL_NO_RSA
  190. RSA *d2i_RSA_PUBKEY(RSA **a, const unsigned char **pp, long length)
  191. {
  192. EVP_PKEY *pkey;
  193. RSA *key;
  194. const unsigned char *q;
  195. q = *pp;
  196. pkey = d2i_PUBKEY(NULL, &q, length);
  197. if (!pkey)
  198. return NULL;
  199. key = EVP_PKEY_get1_RSA(pkey);
  200. EVP_PKEY_free(pkey);
  201. if (!key)
  202. return NULL;
  203. *pp = q;
  204. if (a) {
  205. RSA_free(*a);
  206. *a = key;
  207. }
  208. return key;
  209. }
  210. int i2d_RSA_PUBKEY(RSA *a, unsigned char **pp)
  211. {
  212. EVP_PKEY *pktmp;
  213. int ret;
  214. if (!a)
  215. return 0;
  216. pktmp = EVP_PKEY_new();
  217. if (pktmp == NULL) {
  218. ASN1err(ASN1_F_I2D_RSA_PUBKEY, ERR_R_MALLOC_FAILURE);
  219. return -1;
  220. }
  221. EVP_PKEY_set1_RSA(pktmp, a);
  222. ret = i2d_PUBKEY(pktmp, pp);
  223. EVP_PKEY_free(pktmp);
  224. return ret;
  225. }
  226. #endif
  227. #ifndef OPENSSL_NO_DSA
  228. DSA *d2i_DSA_PUBKEY(DSA **a, const unsigned char **pp, long length)
  229. {
  230. EVP_PKEY *pkey;
  231. DSA *key;
  232. const unsigned char *q;
  233. q = *pp;
  234. pkey = d2i_PUBKEY(NULL, &q, length);
  235. if (!pkey)
  236. return NULL;
  237. key = EVP_PKEY_get1_DSA(pkey);
  238. EVP_PKEY_free(pkey);
  239. if (!key)
  240. return NULL;
  241. *pp = q;
  242. if (a) {
  243. DSA_free(*a);
  244. *a = key;
  245. }
  246. return key;
  247. }
  248. int i2d_DSA_PUBKEY(DSA *a, unsigned char **pp)
  249. {
  250. EVP_PKEY *pktmp;
  251. int ret;
  252. if (!a)
  253. return 0;
  254. pktmp = EVP_PKEY_new();
  255. if (pktmp == NULL) {
  256. ASN1err(ASN1_F_I2D_DSA_PUBKEY, ERR_R_MALLOC_FAILURE);
  257. return -1;
  258. }
  259. EVP_PKEY_set1_DSA(pktmp, a);
  260. ret = i2d_PUBKEY(pktmp, pp);
  261. EVP_PKEY_free(pktmp);
  262. return ret;
  263. }
  264. #endif
  265. #ifndef OPENSSL_NO_EC
  266. EC_KEY *d2i_EC_PUBKEY(EC_KEY **a, const unsigned char **pp, long length)
  267. {
  268. EVP_PKEY *pkey;
  269. EC_KEY *key;
  270. const unsigned char *q;
  271. q = *pp;
  272. pkey = d2i_PUBKEY(NULL, &q, length);
  273. if (!pkey)
  274. return NULL;
  275. key = EVP_PKEY_get1_EC_KEY(pkey);
  276. EVP_PKEY_free(pkey);
  277. if (!key)
  278. return NULL;
  279. *pp = q;
  280. if (a) {
  281. EC_KEY_free(*a);
  282. *a = key;
  283. }
  284. return key;
  285. }
  286. int i2d_EC_PUBKEY(EC_KEY *a, unsigned char **pp)
  287. {
  288. EVP_PKEY *pktmp;
  289. int ret;
  290. if (!a)
  291. return 0;
  292. if ((pktmp = EVP_PKEY_new()) == NULL) {
  293. ASN1err(ASN1_F_I2D_EC_PUBKEY, ERR_R_MALLOC_FAILURE);
  294. return -1;
  295. }
  296. EVP_PKEY_set1_EC_KEY(pktmp, a);
  297. ret = i2d_PUBKEY(pktmp, pp);
  298. EVP_PKEY_free(pktmp);
  299. return ret;
  300. }
  301. #endif
  302. int X509_PUBKEY_set0_param(X509_PUBKEY *pub, ASN1_OBJECT *aobj,
  303. int ptype, void *pval,
  304. unsigned char *penc, int penclen)
  305. {
  306. if (!X509_ALGOR_set0(pub->algor, aobj, ptype, pval))
  307. return 0;
  308. if (penc) {
  309. OPENSSL_free(pub->public_key->data);
  310. pub->public_key->data = penc;
  311. pub->public_key->length = penclen;
  312. /* Set number of unused bits to zero */
  313. pub->public_key->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
  314. pub->public_key->flags |= ASN1_STRING_FLAG_BITS_LEFT;
  315. }
  316. return 1;
  317. }
  318. int X509_PUBKEY_get0_param(ASN1_OBJECT **ppkalg,
  319. const unsigned char **pk, int *ppklen,
  320. X509_ALGOR **pa, X509_PUBKEY *pub)
  321. {
  322. if (ppkalg)
  323. *ppkalg = pub->algor->algorithm;
  324. if (pk) {
  325. *pk = pub->public_key->data;
  326. *ppklen = pub->public_key->length;
  327. }
  328. if (pa)
  329. *pa = pub->algor;
  330. return 1;
  331. }
  332. ASN1_BIT_STRING *X509_get0_pubkey_bitstr(const X509 *x)
  333. {
  334. if (x == NULL)
  335. return NULL;
  336. return x->cert_info.key->public_key;
  337. }