x_pubkey.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /*
  2. * Copyright 1995-2020 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 "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. 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 && !EVP_PKEY_up_ref(ret)) {
  147. X509err(X509_F_X509_PUBKEY_GET, ERR_R_INTERNAL_ERROR);
  148. ret = NULL;
  149. }
  150. return ret;
  151. }
  152. /*
  153. * Now two pseudo ASN1 routines that take an EVP_PKEY structure and encode or
  154. * decode as X509_PUBKEY
  155. */
  156. EVP_PKEY *d2i_PUBKEY(EVP_PKEY **a, const unsigned char **pp, long length)
  157. {
  158. X509_PUBKEY *xpk;
  159. EVP_PKEY *pktmp;
  160. const unsigned char *q;
  161. q = *pp;
  162. xpk = d2i_X509_PUBKEY(NULL, &q, length);
  163. if (!xpk)
  164. return NULL;
  165. pktmp = X509_PUBKEY_get(xpk);
  166. X509_PUBKEY_free(xpk);
  167. if (!pktmp)
  168. return NULL;
  169. *pp = q;
  170. if (a) {
  171. EVP_PKEY_free(*a);
  172. *a = pktmp;
  173. }
  174. return pktmp;
  175. }
  176. int i2d_PUBKEY(EVP_PKEY *a, unsigned char **pp)
  177. {
  178. X509_PUBKEY *xpk = NULL;
  179. int ret;
  180. if (!a)
  181. return 0;
  182. if (!X509_PUBKEY_set(&xpk, a))
  183. return -1;
  184. ret = i2d_X509_PUBKEY(xpk, pp);
  185. X509_PUBKEY_free(xpk);
  186. return ret;
  187. }
  188. /*
  189. * The following are equivalents but which return RSA and DSA keys
  190. */
  191. #ifndef OPENSSL_NO_RSA
  192. RSA *d2i_RSA_PUBKEY(RSA **a, const unsigned char **pp, long length)
  193. {
  194. EVP_PKEY *pkey;
  195. RSA *key;
  196. const unsigned char *q;
  197. q = *pp;
  198. pkey = d2i_PUBKEY(NULL, &q, length);
  199. if (!pkey)
  200. return NULL;
  201. key = EVP_PKEY_get1_RSA(pkey);
  202. EVP_PKEY_free(pkey);
  203. if (!key)
  204. return NULL;
  205. *pp = q;
  206. if (a) {
  207. RSA_free(*a);
  208. *a = key;
  209. }
  210. return key;
  211. }
  212. int i2d_RSA_PUBKEY(RSA *a, unsigned char **pp)
  213. {
  214. EVP_PKEY *pktmp;
  215. int ret;
  216. if (!a)
  217. return 0;
  218. pktmp = EVP_PKEY_new();
  219. if (pktmp == NULL) {
  220. ASN1err(ASN1_F_I2D_RSA_PUBKEY, ERR_R_MALLOC_FAILURE);
  221. return -1;
  222. }
  223. EVP_PKEY_set1_RSA(pktmp, a);
  224. ret = i2d_PUBKEY(pktmp, pp);
  225. EVP_PKEY_free(pktmp);
  226. return ret;
  227. }
  228. #endif
  229. #ifndef OPENSSL_NO_DSA
  230. DSA *d2i_DSA_PUBKEY(DSA **a, const unsigned char **pp, long length)
  231. {
  232. EVP_PKEY *pkey;
  233. DSA *key;
  234. const unsigned char *q;
  235. q = *pp;
  236. pkey = d2i_PUBKEY(NULL, &q, length);
  237. if (!pkey)
  238. return NULL;
  239. key = EVP_PKEY_get1_DSA(pkey);
  240. EVP_PKEY_free(pkey);
  241. if (!key)
  242. return NULL;
  243. *pp = q;
  244. if (a) {
  245. DSA_free(*a);
  246. *a = key;
  247. }
  248. return key;
  249. }
  250. int i2d_DSA_PUBKEY(DSA *a, unsigned char **pp)
  251. {
  252. EVP_PKEY *pktmp;
  253. int ret;
  254. if (!a)
  255. return 0;
  256. pktmp = EVP_PKEY_new();
  257. if (pktmp == NULL) {
  258. ASN1err(ASN1_F_I2D_DSA_PUBKEY, ERR_R_MALLOC_FAILURE);
  259. return -1;
  260. }
  261. EVP_PKEY_set1_DSA(pktmp, a);
  262. ret = i2d_PUBKEY(pktmp, pp);
  263. EVP_PKEY_free(pktmp);
  264. return ret;
  265. }
  266. #endif
  267. #ifndef OPENSSL_NO_EC
  268. EC_KEY *d2i_EC_PUBKEY(EC_KEY **a, const unsigned char **pp, long length)
  269. {
  270. EVP_PKEY *pkey;
  271. EC_KEY *key;
  272. const unsigned char *q;
  273. q = *pp;
  274. pkey = d2i_PUBKEY(NULL, &q, length);
  275. if (!pkey)
  276. return NULL;
  277. key = EVP_PKEY_get1_EC_KEY(pkey);
  278. EVP_PKEY_free(pkey);
  279. if (!key)
  280. return NULL;
  281. *pp = q;
  282. if (a) {
  283. EC_KEY_free(*a);
  284. *a = key;
  285. }
  286. return key;
  287. }
  288. int i2d_EC_PUBKEY(EC_KEY *a, unsigned char **pp)
  289. {
  290. EVP_PKEY *pktmp;
  291. int ret;
  292. if (!a)
  293. return 0;
  294. if ((pktmp = EVP_PKEY_new()) == NULL) {
  295. ASN1err(ASN1_F_I2D_EC_PUBKEY, ERR_R_MALLOC_FAILURE);
  296. return -1;
  297. }
  298. EVP_PKEY_set1_EC_KEY(pktmp, a);
  299. ret = i2d_PUBKEY(pktmp, pp);
  300. EVP_PKEY_free(pktmp);
  301. return ret;
  302. }
  303. #endif
  304. int X509_PUBKEY_set0_param(X509_PUBKEY *pub, ASN1_OBJECT *aobj,
  305. int ptype, void *pval,
  306. unsigned char *penc, int penclen)
  307. {
  308. if (!X509_ALGOR_set0(pub->algor, aobj, ptype, pval))
  309. return 0;
  310. if (penc) {
  311. OPENSSL_free(pub->public_key->data);
  312. pub->public_key->data = penc;
  313. pub->public_key->length = penclen;
  314. /* Set number of unused bits to zero */
  315. pub->public_key->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
  316. pub->public_key->flags |= ASN1_STRING_FLAG_BITS_LEFT;
  317. }
  318. return 1;
  319. }
  320. int X509_PUBKEY_get0_param(ASN1_OBJECT **ppkalg,
  321. const unsigned char **pk, int *ppklen,
  322. X509_ALGOR **pa, X509_PUBKEY *pub)
  323. {
  324. if (ppkalg)
  325. *ppkalg = pub->algor->algorithm;
  326. if (pk) {
  327. *pk = pub->public_key->data;
  328. *ppklen = pub->public_key->length;
  329. }
  330. if (pa)
  331. *pa = pub->algor;
  332. return 1;
  333. }
  334. ASN1_BIT_STRING *X509_get0_pubkey_bitstr(const X509 *x)
  335. {
  336. if (x == NULL)
  337. return NULL;
  338. return x->cert_info.key->public_key;
  339. }