x_pubkey.c 13 KB

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