x_pubkey.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. /*
  2. * Copyright 1995-2020 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. /*
  10. * DSA low level APIs are deprecated for public use, but still ok for
  11. * internal use.
  12. */
  13. #include "internal/deprecated.h"
  14. #include <stdio.h>
  15. #include "internal/cryptlib.h"
  16. #include <openssl/asn1t.h>
  17. #include <openssl/x509.h>
  18. #include "crypto/asn1.h"
  19. #include "crypto/evp.h"
  20. #include "crypto/x509.h"
  21. #include <openssl/rsa.h>
  22. #include <openssl/dsa.h>
  23. #include <openssl/encoder.h>
  24. #include "internal/provider.h"
  25. struct X509_pubkey_st {
  26. X509_ALGOR *algor;
  27. ASN1_BIT_STRING *public_key;
  28. EVP_PKEY *pkey;
  29. /* extra data for the callback, used by d2i_PUBKEY_ex */
  30. OPENSSL_CTX *libctx;
  31. const char *propq;
  32. };
  33. static int x509_pubkey_decode(EVP_PKEY **pk, const X509_PUBKEY *key);
  34. /* Minor tweak to operation: free up EVP_PKEY */
  35. static int pubkey_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
  36. void *exarg)
  37. {
  38. X509_PUBKEY *pubkey = (X509_PUBKEY *)*pval;
  39. if (operation == ASN1_OP_FREE_POST) {
  40. EVP_PKEY_free(pubkey->pkey);
  41. } else if (operation == ASN1_OP_D2I_POST) {
  42. /* Attempt to decode public key and cache in pubkey structure. */
  43. EVP_PKEY_free(pubkey->pkey);
  44. pubkey->pkey = NULL;
  45. /*
  46. * Opportunistically decode the key but remove any non fatal errors
  47. * from the queue. Subsequent explicit attempts to decode/use the key
  48. * will return an appropriate error.
  49. */
  50. ERR_set_mark();
  51. if (x509_pubkey_decode(&pubkey->pkey, pubkey) == -1) {
  52. ERR_clear_last_mark();
  53. return 0;
  54. }
  55. ERR_pop_to_mark();
  56. }
  57. return 1;
  58. }
  59. ASN1_SEQUENCE_cb(X509_PUBKEY, pubkey_cb) = {
  60. ASN1_SIMPLE(X509_PUBKEY, algor, X509_ALGOR),
  61. ASN1_SIMPLE(X509_PUBKEY, public_key, ASN1_BIT_STRING)
  62. } ASN1_SEQUENCE_END_cb(X509_PUBKEY, X509_PUBKEY)
  63. IMPLEMENT_ASN1_FUNCTIONS(X509_PUBKEY)
  64. IMPLEMENT_ASN1_DUP_FUNCTION(X509_PUBKEY)
  65. /* TODO should better be called X509_PUBKEY_set1 */
  66. int X509_PUBKEY_set(X509_PUBKEY **x, EVP_PKEY *pkey)
  67. {
  68. X509_PUBKEY *pk = NULL;
  69. if (x == NULL)
  70. return 0;
  71. if (pkey == NULL)
  72. goto unsupported;
  73. if (pkey->ameth != NULL) {
  74. if ((pk = X509_PUBKEY_new()) == NULL) {
  75. X509err(X509_F_X509_PUBKEY_SET, ERR_R_MALLOC_FAILURE);
  76. goto error;
  77. }
  78. if (pkey->ameth->pub_encode != NULL) {
  79. if (!pkey->ameth->pub_encode(pk, pkey)) {
  80. X509err(X509_F_X509_PUBKEY_SET,
  81. X509_R_PUBLIC_KEY_ENCODE_ERROR);
  82. goto error;
  83. }
  84. } else {
  85. X509err(X509_F_X509_PUBKEY_SET, X509_R_METHOD_NOT_SUPPORTED);
  86. goto error;
  87. }
  88. } else if (pkey->keymgmt != NULL) {
  89. const OSSL_PROVIDER *pkprov = EVP_KEYMGMT_provider(pkey->keymgmt);
  90. OPENSSL_CTX *libctx = ossl_provider_library_context(pkprov);
  91. BIO *bmem = BIO_new(BIO_s_mem());
  92. int selection = (OSSL_KEYMGMT_SELECT_PUBLIC_KEY
  93. | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS);
  94. OSSL_ENCODER_CTX *ectx =
  95. OSSL_ENCODER_CTX_new_by_EVP_PKEY(pkey, "DER", selection,
  96. libctx, NULL);
  97. if (OSSL_ENCODER_to_bio(ectx, bmem)) {
  98. const unsigned char *der = NULL;
  99. long derlen = BIO_get_mem_data(bmem, (char **)&der);
  100. pk = d2i_X509_PUBKEY(NULL, &der, derlen);
  101. }
  102. OSSL_ENCODER_CTX_free(ectx);
  103. BIO_free(bmem);
  104. }
  105. if (pk == NULL)
  106. goto unsupported;
  107. X509_PUBKEY_free(*x);
  108. if (!EVP_PKEY_up_ref(pkey)) {
  109. X509err(X509_F_X509_PUBKEY_SET, ERR_R_INTERNAL_ERROR);
  110. goto error;
  111. }
  112. *x = pk;
  113. /*
  114. * pk->pkey is NULL when using the legacy routine, but is non-NULL when
  115. * going through the encoder, and for all intents and purposes, it's
  116. * a perfect copy of |pkey|, just not the same instance. In that case,
  117. * we could simply return early, right here.
  118. * However, in the interest of being cautious leaning on paranoia, some
  119. * application might very well depend on the passed |pkey| being used
  120. * and none other, so we spend a few more cycles throwing away the newly
  121. * created |pk->pkey| and replace it with |pkey|.
  122. * TODO(3.0) Investigate if it's safe to change to simply return here
  123. * if |pk->pkey != NULL|.
  124. */
  125. if (pk->pkey != NULL)
  126. EVP_PKEY_free(pk->pkey);
  127. pk->pkey = pkey;
  128. return 1;
  129. unsupported:
  130. X509err(X509_F_X509_PUBKEY_SET, X509_R_UNSUPPORTED_ALGORITHM);
  131. error:
  132. X509_PUBKEY_free(pk);
  133. return 0;
  134. }
  135. /*
  136. * Attempt to decode a public key.
  137. * Returns 1 on success, 0 for a decode failure and -1 for a fatal
  138. * error e.g. malloc failure.
  139. */
  140. static int x509_pubkey_decode(EVP_PKEY **ppkey, const X509_PUBKEY *key)
  141. {
  142. EVP_PKEY *pkey = EVP_PKEY_new();
  143. if (pkey == NULL) {
  144. X509err(X509_F_X509_PUBKEY_DECODE, ERR_R_MALLOC_FAILURE);
  145. return -1;
  146. }
  147. if (!EVP_PKEY_set_type(pkey, OBJ_obj2nid(key->algor->algorithm))) {
  148. X509err(X509_F_X509_PUBKEY_DECODE, X509_R_UNSUPPORTED_ALGORITHM);
  149. goto error;
  150. }
  151. if (pkey->ameth->pub_decode) {
  152. /*
  153. * Treat any failure of pub_decode as a decode error. In
  154. * future we could have different return codes for decode
  155. * errors and fatal errors such as malloc failure.
  156. */
  157. if (!pkey->ameth->pub_decode(pkey, key))
  158. goto error;
  159. } else {
  160. X509err(X509_F_X509_PUBKEY_DECODE, X509_R_METHOD_NOT_SUPPORTED);
  161. goto error;
  162. }
  163. *ppkey = pkey;
  164. return 1;
  165. error:
  166. EVP_PKEY_free(pkey);
  167. return 0;
  168. }
  169. EVP_PKEY *X509_PUBKEY_get0(const X509_PUBKEY *key)
  170. {
  171. EVP_PKEY *ret = NULL;
  172. if (key == NULL || key->public_key == NULL)
  173. return NULL;
  174. if (key->pkey != NULL)
  175. return key->pkey;
  176. /*
  177. * When the key ASN.1 is initially parsed an attempt is made to
  178. * decode the public key and cache the EVP_PKEY structure. If this
  179. * operation fails the cached value will be NULL. Parsing continues
  180. * to allow parsing of unknown key types or unsupported forms.
  181. * We repeat the decode operation so the appropriate errors are left
  182. * in the queue.
  183. */
  184. x509_pubkey_decode(&ret, key);
  185. /* If decode doesn't fail something bad happened */
  186. if (ret != NULL) {
  187. X509err(X509_F_X509_PUBKEY_GET0, ERR_R_INTERNAL_ERROR);
  188. EVP_PKEY_free(ret);
  189. }
  190. return NULL;
  191. }
  192. EVP_PKEY *X509_PUBKEY_get(const X509_PUBKEY *key)
  193. {
  194. EVP_PKEY *ret = X509_PUBKEY_get0(key);
  195. if (ret != NULL && !EVP_PKEY_up_ref(ret)) {
  196. X509err(X509_F_X509_PUBKEY_GET, ERR_R_INTERNAL_ERROR);
  197. ret = NULL;
  198. }
  199. return ret;
  200. }
  201. /*
  202. * Now three pseudo ASN1 routines that take an EVP_PKEY structure and encode
  203. * or decode as X509_PUBKEY
  204. */
  205. EVP_PKEY *d2i_PUBKEY_ex(EVP_PKEY **a, const unsigned char **pp, long length,
  206. OPENSSL_CTX *libctx, const char *propq)
  207. {
  208. X509_PUBKEY *xpk, *xpk2 = NULL, **pxpk = NULL;
  209. EVP_PKEY *pktmp = NULL;
  210. const unsigned char *q;
  211. q = *pp;
  212. /*
  213. * If libctx or propq are non-NULL, we take advantage of the reuse
  214. * feature. It's not generally recommended, but is safe enough for
  215. * newly created structures.
  216. */
  217. if (libctx != NULL || propq != NULL) {
  218. xpk2 = OPENSSL_zalloc(sizeof(*xpk2));
  219. if (xpk2 == NULL) {
  220. ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
  221. return NULL;
  222. }
  223. xpk2->libctx = libctx;
  224. xpk2->propq = propq;
  225. pxpk = &xpk2;
  226. }
  227. xpk = d2i_X509_PUBKEY(pxpk, &q, length);
  228. if (xpk == NULL)
  229. goto end;
  230. pktmp = X509_PUBKEY_get(xpk);
  231. X509_PUBKEY_free(xpk);
  232. xpk2 = NULL; /* We know that xpk == xpk2 */
  233. if (pktmp == NULL)
  234. goto end;
  235. *pp = q;
  236. if (a != NULL) {
  237. EVP_PKEY_free(*a);
  238. *a = pktmp;
  239. }
  240. end:
  241. X509_PUBKEY_free(xpk2);
  242. return pktmp;
  243. }
  244. EVP_PKEY *d2i_PUBKEY(EVP_PKEY **a, const unsigned char **pp, long length)
  245. {
  246. return d2i_PUBKEY_ex(a, pp, length, NULL, NULL);
  247. }
  248. int i2d_PUBKEY(const EVP_PKEY *a, unsigned char **pp)
  249. {
  250. int ret = -1;
  251. if (a == NULL)
  252. return 0;
  253. if (a->ameth != NULL) {
  254. X509_PUBKEY *xpk = NULL;
  255. if ((xpk = X509_PUBKEY_new()) == NULL)
  256. return -1;
  257. /* pub_encode() only encode parameters, not the key itself */
  258. if (a->ameth->pub_encode != NULL && a->ameth->pub_encode(xpk, a)) {
  259. xpk->pkey = (EVP_PKEY *)a;
  260. ret = i2d_X509_PUBKEY(xpk, pp);
  261. xpk->pkey = NULL;
  262. }
  263. X509_PUBKEY_free(xpk);
  264. } else if (a->keymgmt != NULL) {
  265. const OSSL_PROVIDER *pkprov = EVP_KEYMGMT_provider(a->keymgmt);
  266. OPENSSL_CTX *libctx = ossl_provider_library_context(pkprov);
  267. int selection = (OSSL_KEYMGMT_SELECT_PUBLIC_KEY
  268. | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS);
  269. OSSL_ENCODER_CTX *ctx =
  270. OSSL_ENCODER_CTX_new_by_EVP_PKEY(a, "DER", selection, libctx, NULL);
  271. BIO *out = BIO_new(BIO_s_mem());
  272. BUF_MEM *buf = NULL;
  273. if (OSSL_ENCODER_CTX_get_num_encoders(ctx) != 0
  274. && out != NULL
  275. && OSSL_ENCODER_to_bio(ctx, out)
  276. && BIO_get_mem_ptr(out, &buf) > 0) {
  277. ret = buf->length;
  278. if (pp != NULL) {
  279. if (*pp == NULL) {
  280. *pp = (unsigned char *)buf->data;
  281. buf->length = 0;
  282. buf->data = NULL;
  283. } else {
  284. memcpy(*pp, buf->data, ret);
  285. *pp += ret;
  286. }
  287. }
  288. }
  289. BIO_free(out);
  290. OSSL_ENCODER_CTX_free(ctx);
  291. }
  292. return ret;
  293. }
  294. /*
  295. * The following are equivalents but which return RSA and DSA keys
  296. */
  297. #ifndef OPENSSL_NO_RSA
  298. RSA *d2i_RSA_PUBKEY(RSA **a, const unsigned char **pp, long length)
  299. {
  300. EVP_PKEY *pkey;
  301. RSA *key;
  302. const unsigned char *q;
  303. q = *pp;
  304. pkey = d2i_PUBKEY(NULL, &q, length);
  305. if (pkey == NULL)
  306. return NULL;
  307. key = EVP_PKEY_get1_RSA(pkey);
  308. EVP_PKEY_free(pkey);
  309. if (key == NULL)
  310. return NULL;
  311. *pp = q;
  312. if (a != NULL) {
  313. RSA_free(*a);
  314. *a = key;
  315. }
  316. return key;
  317. }
  318. int i2d_RSA_PUBKEY(const RSA *a, unsigned char **pp)
  319. {
  320. EVP_PKEY *pktmp;
  321. int ret;
  322. if (!a)
  323. return 0;
  324. pktmp = EVP_PKEY_new();
  325. if (pktmp == NULL) {
  326. ASN1err(ASN1_F_I2D_RSA_PUBKEY, ERR_R_MALLOC_FAILURE);
  327. return -1;
  328. }
  329. (void)EVP_PKEY_assign_RSA(pktmp, (RSA *)a);
  330. ret = i2d_PUBKEY(pktmp, pp);
  331. pktmp->pkey.ptr = NULL;
  332. EVP_PKEY_free(pktmp);
  333. return ret;
  334. }
  335. #endif
  336. #ifndef OPENSSL_NO_DSA
  337. DSA *d2i_DSA_PUBKEY(DSA **a, const unsigned char **pp, long length)
  338. {
  339. EVP_PKEY *pkey;
  340. DSA *key;
  341. const unsigned char *q;
  342. q = *pp;
  343. pkey = d2i_PUBKEY(NULL, &q, length);
  344. if (pkey == NULL)
  345. return NULL;
  346. key = EVP_PKEY_get1_DSA(pkey);
  347. EVP_PKEY_free(pkey);
  348. if (key == NULL)
  349. return NULL;
  350. *pp = q;
  351. if (a != NULL) {
  352. DSA_free(*a);
  353. *a = key;
  354. }
  355. return key;
  356. }
  357. int i2d_DSA_PUBKEY(const DSA *a, unsigned char **pp)
  358. {
  359. EVP_PKEY *pktmp;
  360. int ret;
  361. if (!a)
  362. return 0;
  363. pktmp = EVP_PKEY_new();
  364. if (pktmp == NULL) {
  365. ASN1err(ASN1_F_I2D_DSA_PUBKEY, ERR_R_MALLOC_FAILURE);
  366. return -1;
  367. }
  368. (void)EVP_PKEY_assign_DSA(pktmp, (DSA *)a);
  369. ret = i2d_PUBKEY(pktmp, pp);
  370. pktmp->pkey.ptr = NULL;
  371. EVP_PKEY_free(pktmp);
  372. return ret;
  373. }
  374. #endif
  375. #ifndef OPENSSL_NO_EC
  376. EC_KEY *d2i_EC_PUBKEY(EC_KEY **a, const unsigned char **pp, long length)
  377. {
  378. EVP_PKEY *pkey;
  379. EC_KEY *key;
  380. const unsigned char *q;
  381. q = *pp;
  382. pkey = d2i_PUBKEY(NULL, &q, length);
  383. if (pkey == NULL)
  384. return NULL;
  385. key = EVP_PKEY_get1_EC_KEY(pkey);
  386. EVP_PKEY_free(pkey);
  387. if (key == NULL)
  388. return NULL;
  389. *pp = q;
  390. if (a != NULL) {
  391. EC_KEY_free(*a);
  392. *a = key;
  393. }
  394. return key;
  395. }
  396. int i2d_EC_PUBKEY(const EC_KEY *a, unsigned char **pp)
  397. {
  398. EVP_PKEY *pktmp;
  399. int ret;
  400. if (a == NULL)
  401. return 0;
  402. if ((pktmp = EVP_PKEY_new()) == NULL) {
  403. ASN1err(ASN1_F_I2D_EC_PUBKEY, ERR_R_MALLOC_FAILURE);
  404. return -1;
  405. }
  406. (void)EVP_PKEY_assign_EC_KEY(pktmp, (EC_KEY *)a);
  407. ret = i2d_PUBKEY(pktmp, pp);
  408. pktmp->pkey.ptr = NULL;
  409. EVP_PKEY_free(pktmp);
  410. return ret;
  411. }
  412. #endif
  413. int X509_PUBKEY_set0_param(X509_PUBKEY *pub, ASN1_OBJECT *aobj,
  414. int ptype, void *pval,
  415. unsigned char *penc, int penclen)
  416. {
  417. if (!X509_ALGOR_set0(pub->algor, aobj, ptype, pval))
  418. return 0;
  419. if (penc) {
  420. OPENSSL_free(pub->public_key->data);
  421. pub->public_key->data = penc;
  422. pub->public_key->length = penclen;
  423. /* Set number of unused bits to zero */
  424. pub->public_key->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
  425. pub->public_key->flags |= ASN1_STRING_FLAG_BITS_LEFT;
  426. }
  427. return 1;
  428. }
  429. int X509_PUBKEY_get0_param(ASN1_OBJECT **ppkalg,
  430. const unsigned char **pk, int *ppklen,
  431. X509_ALGOR **pa, const X509_PUBKEY *pub)
  432. {
  433. if (ppkalg)
  434. *ppkalg = pub->algor->algorithm;
  435. if (pk) {
  436. *pk = pub->public_key->data;
  437. *ppklen = pub->public_key->length;
  438. }
  439. if (pa)
  440. *pa = pub->algor;
  441. return 1;
  442. }
  443. ASN1_BIT_STRING *X509_get0_pubkey_bitstr(const X509 *x)
  444. {
  445. if (x == NULL)
  446. return NULL;
  447. return x->cert_info.key->public_key;
  448. }
  449. /* Returns 1 for equal, 0, for non-equal, < 0 on error */
  450. int X509_PUBKEY_eq(const X509_PUBKEY *a, const X509_PUBKEY *b)
  451. {
  452. X509_ALGOR *algA, *algB;
  453. EVP_PKEY *pA, *pB;
  454. if (a == b)
  455. return 1;
  456. if (a == NULL || b == NULL)
  457. return 0;
  458. if (!X509_PUBKEY_get0_param(NULL, NULL, NULL, &algA, a) || algA == NULL
  459. || !X509_PUBKEY_get0_param(NULL, NULL, NULL, &algB, b) || algB == NULL)
  460. return -2;
  461. if (X509_ALGOR_cmp(algA, algB) != 0)
  462. return 0;
  463. if ((pA = X509_PUBKEY_get0(a)) == NULL
  464. || (pB = X509_PUBKEY_get0(b)) == NULL)
  465. return -2;
  466. return EVP_PKEY_eq(pA, pB);
  467. }
  468. int X509_PUBKEY_get0_libctx(OPENSSL_CTX **plibctx, const char **ppropq,
  469. const X509_PUBKEY *key)
  470. {
  471. if (plibctx)
  472. *plibctx = key->libctx;
  473. if (ppropq)
  474. *ppropq = key->propq;
  475. return 1;
  476. }