x_pubkey.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079
  1. /*
  2. * Copyright 1995-2023 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 <openssl/engine.h>
  19. #include "crypto/asn1.h"
  20. #include "crypto/evp.h"
  21. #include "crypto/x509.h"
  22. #include <openssl/rsa.h>
  23. #include <openssl/dsa.h>
  24. #include <openssl/decoder.h>
  25. #include <openssl/encoder.h>
  26. #include "internal/provider.h"
  27. #include "internal/sizes.h"
  28. struct X509_pubkey_st {
  29. X509_ALGOR *algor;
  30. ASN1_BIT_STRING *public_key;
  31. EVP_PKEY *pkey;
  32. /* extra data for the callback, used by d2i_PUBKEY_ex */
  33. OSSL_LIB_CTX *libctx;
  34. char *propq;
  35. /* Flag to force legacy keys */
  36. unsigned int flag_force_legacy : 1;
  37. };
  38. static int x509_pubkey_decode(EVP_PKEY **pk, const X509_PUBKEY *key);
  39. static int x509_pubkey_set0_libctx(X509_PUBKEY *x, OSSL_LIB_CTX *libctx,
  40. const char *propq)
  41. {
  42. if (x != NULL) {
  43. x->libctx = libctx;
  44. OPENSSL_free(x->propq);
  45. x->propq = NULL;
  46. if (propq != NULL) {
  47. x->propq = OPENSSL_strdup(propq);
  48. if (x->propq == NULL)
  49. return 0;
  50. }
  51. }
  52. return 1;
  53. }
  54. ASN1_SEQUENCE(X509_PUBKEY_INTERNAL) = {
  55. ASN1_SIMPLE(X509_PUBKEY, algor, X509_ALGOR),
  56. ASN1_SIMPLE(X509_PUBKEY, public_key, ASN1_BIT_STRING)
  57. } static_ASN1_SEQUENCE_END_name(X509_PUBKEY, X509_PUBKEY_INTERNAL)
  58. X509_PUBKEY *ossl_d2i_X509_PUBKEY_INTERNAL(const unsigned char **pp,
  59. long len, OSSL_LIB_CTX *libctx,
  60. const char *propq)
  61. {
  62. X509_PUBKEY *xpub = OPENSSL_zalloc(sizeof(*xpub));
  63. if (xpub == NULL)
  64. return NULL;
  65. return (X509_PUBKEY *)ASN1_item_d2i_ex((ASN1_VALUE **)&xpub, pp, len,
  66. ASN1_ITEM_rptr(X509_PUBKEY_INTERNAL),
  67. libctx, propq);
  68. }
  69. void ossl_X509_PUBKEY_INTERNAL_free(X509_PUBKEY *xpub)
  70. {
  71. ASN1_item_free((ASN1_VALUE *)xpub, ASN1_ITEM_rptr(X509_PUBKEY_INTERNAL));
  72. }
  73. static void x509_pubkey_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
  74. {
  75. X509_PUBKEY *pubkey;
  76. if (pval != NULL && (pubkey = (X509_PUBKEY *)*pval) != NULL) {
  77. X509_ALGOR_free(pubkey->algor);
  78. ASN1_BIT_STRING_free(pubkey->public_key);
  79. EVP_PKEY_free(pubkey->pkey);
  80. OPENSSL_free(pubkey->propq);
  81. OPENSSL_free(pubkey);
  82. *pval = NULL;
  83. }
  84. }
  85. static int x509_pubkey_ex_populate(ASN1_VALUE **pval, const ASN1_ITEM *it)
  86. {
  87. X509_PUBKEY *pubkey = (X509_PUBKEY *)*pval;
  88. return (pubkey->algor != NULL
  89. || (pubkey->algor = X509_ALGOR_new()) != NULL)
  90. && (pubkey->public_key != NULL
  91. || (pubkey->public_key = ASN1_BIT_STRING_new()) != NULL);
  92. }
  93. static int x509_pubkey_ex_new_ex(ASN1_VALUE **pval, const ASN1_ITEM *it,
  94. OSSL_LIB_CTX *libctx, const char *propq)
  95. {
  96. X509_PUBKEY *ret;
  97. if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL)
  98. return 0;
  99. if (!x509_pubkey_ex_populate((ASN1_VALUE **)&ret, NULL)
  100. || !x509_pubkey_set0_libctx(ret, libctx, propq)) {
  101. x509_pubkey_ex_free((ASN1_VALUE **)&ret, NULL);
  102. ret = NULL;
  103. ERR_raise(ERR_LIB_ASN1, ERR_R_X509_LIB);
  104. } else {
  105. *pval = (ASN1_VALUE *)ret;
  106. }
  107. return ret != NULL;
  108. }
  109. static int x509_pubkey_ex_d2i_ex(ASN1_VALUE **pval,
  110. const unsigned char **in, long len,
  111. const ASN1_ITEM *it, int tag, int aclass,
  112. char opt, ASN1_TLC *ctx, OSSL_LIB_CTX *libctx,
  113. const char *propq)
  114. {
  115. const unsigned char *in_saved = *in;
  116. size_t publen;
  117. X509_PUBKEY *pubkey;
  118. int ret;
  119. OSSL_DECODER_CTX *dctx = NULL;
  120. unsigned char *tmpbuf = NULL;
  121. if (*pval == NULL && !x509_pubkey_ex_new_ex(pval, it, libctx, propq))
  122. return 0;
  123. if (!x509_pubkey_ex_populate(pval, NULL)) {
  124. ERR_raise(ERR_LIB_ASN1, ERR_R_X509_LIB);
  125. return 0;
  126. }
  127. /* This ensures that |*in| advances properly no matter what */
  128. if ((ret = ASN1_item_ex_d2i(pval, in, len,
  129. ASN1_ITEM_rptr(X509_PUBKEY_INTERNAL),
  130. tag, aclass, opt, ctx)) <= 0)
  131. return ret;
  132. publen = *in - in_saved;
  133. if (!ossl_assert(publen > 0)) {
  134. ERR_raise(ERR_LIB_ASN1, ERR_R_INTERNAL_ERROR);
  135. return 0;
  136. }
  137. pubkey = (X509_PUBKEY *)*pval;
  138. EVP_PKEY_free(pubkey->pkey);
  139. pubkey->pkey = NULL;
  140. /*
  141. * Opportunistically decode the key but remove any non fatal errors
  142. * from the queue. Subsequent explicit attempts to decode/use the key
  143. * will return an appropriate error.
  144. */
  145. ERR_set_mark();
  146. /*
  147. * Try to decode with legacy method first. This ensures that engines
  148. * aren't overridden by providers.
  149. */
  150. if ((ret = x509_pubkey_decode(&pubkey->pkey, pubkey)) == -1) {
  151. /* -1 indicates a fatal error, like malloc failure */
  152. ERR_clear_last_mark();
  153. goto end;
  154. }
  155. /* Try to decode it into an EVP_PKEY with OSSL_DECODER */
  156. if (ret <= 0 && !pubkey->flag_force_legacy) {
  157. const unsigned char *p;
  158. char txtoidname[OSSL_MAX_NAME_SIZE];
  159. size_t slen = publen;
  160. /*
  161. * The decoders don't know how to handle anything other than Universal
  162. * class so we modify the data accordingly.
  163. */
  164. if (aclass != V_ASN1_UNIVERSAL) {
  165. tmpbuf = OPENSSL_memdup(in_saved, publen);
  166. if (tmpbuf == NULL)
  167. return 0;
  168. in_saved = tmpbuf;
  169. *tmpbuf = V_ASN1_CONSTRUCTED | V_ASN1_SEQUENCE;
  170. }
  171. p = in_saved;
  172. if (OBJ_obj2txt(txtoidname, sizeof(txtoidname),
  173. pubkey->algor->algorithm, 0) <= 0) {
  174. ERR_clear_last_mark();
  175. goto end;
  176. }
  177. if ((dctx =
  178. OSSL_DECODER_CTX_new_for_pkey(&pubkey->pkey,
  179. "DER", "SubjectPublicKeyInfo",
  180. txtoidname, EVP_PKEY_PUBLIC_KEY,
  181. pubkey->libctx,
  182. pubkey->propq)) != NULL)
  183. /*
  184. * As said higher up, we're being opportunistic. In other words,
  185. * we don't care if we fail.
  186. */
  187. if (OSSL_DECODER_from_data(dctx, &p, &slen)) {
  188. if (slen != 0) {
  189. /*
  190. * If we successfully decoded then we *must* consume all the
  191. * bytes.
  192. */
  193. ERR_clear_last_mark();
  194. ERR_raise(ERR_LIB_ASN1, EVP_R_DECODE_ERROR);
  195. goto end;
  196. }
  197. }
  198. }
  199. ERR_pop_to_mark();
  200. ret = 1;
  201. end:
  202. OSSL_DECODER_CTX_free(dctx);
  203. OPENSSL_free(tmpbuf);
  204. return ret;
  205. }
  206. static int x509_pubkey_ex_i2d(const ASN1_VALUE **pval, unsigned char **out,
  207. const ASN1_ITEM *it, int tag, int aclass)
  208. {
  209. return ASN1_item_ex_i2d(pval, out, ASN1_ITEM_rptr(X509_PUBKEY_INTERNAL),
  210. tag, aclass);
  211. }
  212. static int x509_pubkey_ex_print(BIO *out, const ASN1_VALUE **pval, int indent,
  213. const char *fname, const ASN1_PCTX *pctx)
  214. {
  215. return ASN1_item_print(out, *pval, indent,
  216. ASN1_ITEM_rptr(X509_PUBKEY_INTERNAL), pctx);
  217. }
  218. static const ASN1_EXTERN_FUNCS x509_pubkey_ff = {
  219. NULL,
  220. NULL,
  221. x509_pubkey_ex_free,
  222. 0, /* Default clear behaviour is OK */
  223. NULL,
  224. x509_pubkey_ex_i2d,
  225. x509_pubkey_ex_print,
  226. x509_pubkey_ex_new_ex,
  227. x509_pubkey_ex_d2i_ex,
  228. };
  229. IMPLEMENT_EXTERN_ASN1(X509_PUBKEY, V_ASN1_SEQUENCE, x509_pubkey_ff)
  230. IMPLEMENT_ASN1_FUNCTIONS(X509_PUBKEY)
  231. X509_PUBKEY *X509_PUBKEY_new_ex(OSSL_LIB_CTX *libctx, const char *propq)
  232. {
  233. X509_PUBKEY *pubkey = NULL;
  234. pubkey = (X509_PUBKEY *)ASN1_item_new_ex(X509_PUBKEY_it(), libctx, propq);
  235. if (!x509_pubkey_set0_libctx(pubkey, libctx, propq)) {
  236. X509_PUBKEY_free(pubkey);
  237. pubkey = NULL;
  238. }
  239. return pubkey;
  240. }
  241. /*
  242. * X509_PUBKEY_dup() must be implemented manually, because there is no
  243. * support for it in ASN1_EXTERN_FUNCS.
  244. */
  245. X509_PUBKEY *X509_PUBKEY_dup(const X509_PUBKEY *a)
  246. {
  247. X509_PUBKEY *pubkey = OPENSSL_zalloc(sizeof(*pubkey));
  248. if (pubkey == NULL)
  249. return NULL;
  250. if (!x509_pubkey_set0_libctx(pubkey, a->libctx, a->propq)) {
  251. ERR_raise(ERR_LIB_X509, ERR_R_X509_LIB);
  252. x509_pubkey_ex_free((ASN1_VALUE **)&pubkey,
  253. ASN1_ITEM_rptr(X509_PUBKEY_INTERNAL));
  254. return NULL;
  255. }
  256. if ((pubkey->algor = X509_ALGOR_dup(a->algor)) == NULL
  257. || (pubkey->public_key = ASN1_BIT_STRING_new()) == NULL
  258. || !ASN1_BIT_STRING_set(pubkey->public_key,
  259. a->public_key->data,
  260. a->public_key->length)) {
  261. x509_pubkey_ex_free((ASN1_VALUE **)&pubkey,
  262. ASN1_ITEM_rptr(X509_PUBKEY_INTERNAL));
  263. ERR_raise(ERR_LIB_X509, ERR_R_ASN1_LIB);
  264. return NULL;
  265. }
  266. if (a->pkey != NULL) {
  267. ERR_set_mark();
  268. pubkey->pkey = EVP_PKEY_dup(a->pkey);
  269. if (pubkey->pkey == NULL) {
  270. pubkey->flag_force_legacy = 1;
  271. if (x509_pubkey_decode(&pubkey->pkey, pubkey) <= 0) {
  272. x509_pubkey_ex_free((ASN1_VALUE **)&pubkey,
  273. ASN1_ITEM_rptr(X509_PUBKEY_INTERNAL));
  274. ERR_clear_last_mark();
  275. return NULL;
  276. }
  277. }
  278. ERR_pop_to_mark();
  279. }
  280. return pubkey;
  281. }
  282. int X509_PUBKEY_set(X509_PUBKEY **x, EVP_PKEY *pkey)
  283. {
  284. X509_PUBKEY *pk = NULL;
  285. if (x == NULL || pkey == NULL) {
  286. ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER);
  287. return 0;
  288. }
  289. if (pkey->ameth != NULL) {
  290. if ((pk = X509_PUBKEY_new()) == NULL) {
  291. ERR_raise(ERR_LIB_X509, ERR_R_ASN1_LIB);
  292. goto error;
  293. }
  294. if (pkey->ameth->pub_encode != NULL) {
  295. if (!pkey->ameth->pub_encode(pk, pkey)) {
  296. ERR_raise(ERR_LIB_X509, X509_R_PUBLIC_KEY_ENCODE_ERROR);
  297. goto error;
  298. }
  299. } else {
  300. ERR_raise(ERR_LIB_X509, X509_R_METHOD_NOT_SUPPORTED);
  301. goto error;
  302. }
  303. } else if (evp_pkey_is_provided(pkey)) {
  304. unsigned char *der = NULL;
  305. size_t derlen = 0;
  306. OSSL_ENCODER_CTX *ectx =
  307. OSSL_ENCODER_CTX_new_for_pkey(pkey, EVP_PKEY_PUBLIC_KEY,
  308. "DER", "SubjectPublicKeyInfo",
  309. NULL);
  310. if (OSSL_ENCODER_to_data(ectx, &der, &derlen)) {
  311. const unsigned char *pder = der;
  312. pk = d2i_X509_PUBKEY(NULL, &pder, (long)derlen);
  313. }
  314. OSSL_ENCODER_CTX_free(ectx);
  315. OPENSSL_free(der);
  316. }
  317. if (pk == NULL) {
  318. ERR_raise(ERR_LIB_X509, X509_R_UNSUPPORTED_ALGORITHM);
  319. goto error;
  320. }
  321. X509_PUBKEY_free(*x);
  322. if (!EVP_PKEY_up_ref(pkey)) {
  323. ERR_raise(ERR_LIB_X509, ERR_R_INTERNAL_ERROR);
  324. goto error;
  325. }
  326. *x = pk;
  327. /*
  328. * pk->pkey is NULL when using the legacy routine, but is non-NULL when
  329. * going through the encoder, and for all intents and purposes, it's
  330. * a perfect copy of the public key portions of |pkey|, just not the same
  331. * instance. If that's all there was to pkey then we could simply return
  332. * early, right here. However, some application might very well depend on
  333. * the passed |pkey| being used and none other, so we spend a few more
  334. * cycles throwing away the newly created |pk->pkey| and replace it with
  335. * |pkey|.
  336. */
  337. if (pk->pkey != NULL)
  338. EVP_PKEY_free(pk->pkey);
  339. pk->pkey = pkey;
  340. return 1;
  341. error:
  342. X509_PUBKEY_free(pk);
  343. return 0;
  344. }
  345. /*
  346. * Attempt to decode a public key.
  347. * Returns 1 on success, 0 for a decode failure and -1 for a fatal
  348. * error e.g. malloc failure.
  349. *
  350. * This function is #legacy.
  351. */
  352. static int x509_pubkey_decode(EVP_PKEY **ppkey, const X509_PUBKEY *key)
  353. {
  354. EVP_PKEY *pkey;
  355. int nid;
  356. nid = OBJ_obj2nid(key->algor->algorithm);
  357. if (!key->flag_force_legacy) {
  358. #ifndef OPENSSL_NO_ENGINE
  359. ENGINE *e = NULL;
  360. e = ENGINE_get_pkey_meth_engine(nid);
  361. if (e == NULL)
  362. return 0;
  363. ENGINE_finish(e);
  364. #else
  365. return 0;
  366. #endif
  367. }
  368. pkey = EVP_PKEY_new();
  369. if (pkey == NULL) {
  370. ERR_raise(ERR_LIB_X509, ERR_R_EVP_LIB);
  371. return -1;
  372. }
  373. if (!EVP_PKEY_set_type(pkey, nid)) {
  374. ERR_raise(ERR_LIB_X509, X509_R_UNSUPPORTED_ALGORITHM);
  375. goto error;
  376. }
  377. if (pkey->ameth->pub_decode) {
  378. /*
  379. * Treat any failure of pub_decode as a decode error. In
  380. * future we could have different return codes for decode
  381. * errors and fatal errors such as malloc failure.
  382. */
  383. if (!pkey->ameth->pub_decode(pkey, key))
  384. goto error;
  385. } else {
  386. ERR_raise(ERR_LIB_X509, X509_R_METHOD_NOT_SUPPORTED);
  387. goto error;
  388. }
  389. *ppkey = pkey;
  390. return 1;
  391. error:
  392. EVP_PKEY_free(pkey);
  393. return 0;
  394. }
  395. EVP_PKEY *X509_PUBKEY_get0(const X509_PUBKEY *key)
  396. {
  397. if (key == NULL) {
  398. ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER);
  399. return NULL;
  400. }
  401. if (key->pkey == NULL) {
  402. /* We failed to decode the key when we loaded it, or it was never set */
  403. ERR_raise(ERR_LIB_EVP, EVP_R_DECODE_ERROR);
  404. return NULL;
  405. }
  406. return key->pkey;
  407. }
  408. EVP_PKEY *X509_PUBKEY_get(const X509_PUBKEY *key)
  409. {
  410. EVP_PKEY *ret = X509_PUBKEY_get0(key);
  411. if (ret != NULL && !EVP_PKEY_up_ref(ret)) {
  412. ERR_raise(ERR_LIB_X509, ERR_R_INTERNAL_ERROR);
  413. ret = NULL;
  414. }
  415. return ret;
  416. }
  417. /*
  418. * Now three pseudo ASN1 routines that take an EVP_PKEY structure and encode
  419. * or decode as X509_PUBKEY
  420. */
  421. static EVP_PKEY *d2i_PUBKEY_int(EVP_PKEY **a,
  422. const unsigned char **pp, long length,
  423. OSSL_LIB_CTX *libctx, const char *propq,
  424. unsigned int force_legacy,
  425. X509_PUBKEY *
  426. (*d2i_x509_pubkey)(X509_PUBKEY **a,
  427. const unsigned char **in,
  428. long len))
  429. {
  430. X509_PUBKEY *xpk, *xpk2 = NULL, **pxpk = NULL;
  431. EVP_PKEY *pktmp = NULL;
  432. const unsigned char *q;
  433. q = *pp;
  434. /*
  435. * If libctx or propq are non-NULL, we take advantage of the reuse
  436. * feature. It's not generally recommended, but is safe enough for
  437. * newly created structures.
  438. */
  439. if (libctx != NULL || propq != NULL || force_legacy) {
  440. xpk2 = OPENSSL_zalloc(sizeof(*xpk2));
  441. if (xpk2 == NULL)
  442. return NULL;
  443. if (!x509_pubkey_set0_libctx(xpk2, libctx, propq))
  444. goto end;
  445. xpk2->flag_force_legacy = !!force_legacy;
  446. pxpk = &xpk2;
  447. }
  448. xpk = d2i_x509_pubkey(pxpk, &q, length);
  449. if (xpk == NULL)
  450. goto end;
  451. pktmp = X509_PUBKEY_get(xpk);
  452. X509_PUBKEY_free(xpk);
  453. xpk2 = NULL; /* We know that xpk == xpk2 */
  454. if (pktmp == NULL)
  455. goto end;
  456. *pp = q;
  457. if (a != NULL) {
  458. EVP_PKEY_free(*a);
  459. *a = pktmp;
  460. }
  461. end:
  462. X509_PUBKEY_free(xpk2);
  463. return pktmp;
  464. }
  465. /* For the algorithm specific d2i functions further down */
  466. EVP_PKEY *ossl_d2i_PUBKEY_legacy(EVP_PKEY **a, const unsigned char **pp,
  467. long length)
  468. {
  469. return d2i_PUBKEY_int(a, pp, length, NULL, NULL, 1, d2i_X509_PUBKEY);
  470. }
  471. EVP_PKEY *d2i_PUBKEY_ex(EVP_PKEY **a, const unsigned char **pp, long length,
  472. OSSL_LIB_CTX *libctx, const char *propq)
  473. {
  474. return d2i_PUBKEY_int(a, pp, length, libctx, propq, 0, d2i_X509_PUBKEY);
  475. }
  476. EVP_PKEY *d2i_PUBKEY(EVP_PKEY **a, const unsigned char **pp, long length)
  477. {
  478. return d2i_PUBKEY_ex(a, pp, length, NULL, NULL);
  479. }
  480. int i2d_PUBKEY(const EVP_PKEY *a, unsigned char **pp)
  481. {
  482. int ret = -1;
  483. if (a == NULL)
  484. return 0;
  485. if (a->ameth != NULL) {
  486. X509_PUBKEY *xpk = NULL;
  487. if ((xpk = X509_PUBKEY_new()) == NULL)
  488. return -1;
  489. /* pub_encode() only encode parameters, not the key itself */
  490. if (a->ameth->pub_encode != NULL && a->ameth->pub_encode(xpk, a)) {
  491. xpk->pkey = (EVP_PKEY *)a;
  492. ret = i2d_X509_PUBKEY(xpk, pp);
  493. xpk->pkey = NULL;
  494. }
  495. X509_PUBKEY_free(xpk);
  496. } else if (a->keymgmt != NULL) {
  497. OSSL_ENCODER_CTX *ctx =
  498. OSSL_ENCODER_CTX_new_for_pkey(a, EVP_PKEY_PUBLIC_KEY,
  499. "DER", "SubjectPublicKeyInfo",
  500. NULL);
  501. BIO *out = BIO_new(BIO_s_mem());
  502. BUF_MEM *buf = NULL;
  503. if (OSSL_ENCODER_CTX_get_num_encoders(ctx) != 0
  504. && out != NULL
  505. && OSSL_ENCODER_to_bio(ctx, out)
  506. && BIO_get_mem_ptr(out, &buf) > 0) {
  507. ret = buf->length;
  508. if (pp != NULL) {
  509. if (*pp == NULL) {
  510. *pp = (unsigned char *)buf->data;
  511. buf->length = 0;
  512. buf->data = NULL;
  513. } else {
  514. memcpy(*pp, buf->data, ret);
  515. *pp += ret;
  516. }
  517. }
  518. }
  519. BIO_free(out);
  520. OSSL_ENCODER_CTX_free(ctx);
  521. }
  522. return ret;
  523. }
  524. /*
  525. * The following are equivalents but which return RSA and DSA keys
  526. */
  527. RSA *d2i_RSA_PUBKEY(RSA **a, const unsigned char **pp, long length)
  528. {
  529. EVP_PKEY *pkey;
  530. RSA *key = NULL;
  531. const unsigned char *q;
  532. q = *pp;
  533. pkey = ossl_d2i_PUBKEY_legacy(NULL, &q, length);
  534. if (pkey == NULL)
  535. return NULL;
  536. key = EVP_PKEY_get1_RSA(pkey);
  537. EVP_PKEY_free(pkey);
  538. if (key == NULL)
  539. return NULL;
  540. *pp = q;
  541. if (a != NULL) {
  542. RSA_free(*a);
  543. *a = key;
  544. }
  545. return key;
  546. }
  547. int i2d_RSA_PUBKEY(const RSA *a, unsigned char **pp)
  548. {
  549. EVP_PKEY *pktmp;
  550. int ret;
  551. if (!a)
  552. return 0;
  553. pktmp = EVP_PKEY_new();
  554. if (pktmp == NULL) {
  555. ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB);
  556. return -1;
  557. }
  558. (void)EVP_PKEY_assign_RSA(pktmp, (RSA *)a);
  559. ret = i2d_PUBKEY(pktmp, pp);
  560. pktmp->pkey.ptr = NULL;
  561. EVP_PKEY_free(pktmp);
  562. return ret;
  563. }
  564. #ifndef OPENSSL_NO_DH
  565. DH *ossl_d2i_DH_PUBKEY(DH **a, const unsigned char **pp, long length)
  566. {
  567. EVP_PKEY *pkey;
  568. DH *key = NULL;
  569. const unsigned char *q;
  570. q = *pp;
  571. pkey = ossl_d2i_PUBKEY_legacy(NULL, &q, length);
  572. if (pkey == NULL)
  573. return NULL;
  574. if (EVP_PKEY_get_id(pkey) == EVP_PKEY_DH)
  575. key = EVP_PKEY_get1_DH(pkey);
  576. EVP_PKEY_free(pkey);
  577. if (key == NULL)
  578. return NULL;
  579. *pp = q;
  580. if (a != NULL) {
  581. DH_free(*a);
  582. *a = key;
  583. }
  584. return key;
  585. }
  586. int ossl_i2d_DH_PUBKEY(const DH *a, unsigned char **pp)
  587. {
  588. EVP_PKEY *pktmp;
  589. int ret;
  590. if (!a)
  591. return 0;
  592. pktmp = EVP_PKEY_new();
  593. if (pktmp == NULL) {
  594. ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB);
  595. return -1;
  596. }
  597. (void)EVP_PKEY_assign_DH(pktmp, (DH *)a);
  598. ret = i2d_PUBKEY(pktmp, pp);
  599. pktmp->pkey.ptr = NULL;
  600. EVP_PKEY_free(pktmp);
  601. return ret;
  602. }
  603. DH *ossl_d2i_DHx_PUBKEY(DH **a, const unsigned char **pp, long length)
  604. {
  605. EVP_PKEY *pkey;
  606. DH *key = NULL;
  607. const unsigned char *q;
  608. q = *pp;
  609. pkey = ossl_d2i_PUBKEY_legacy(NULL, &q, length);
  610. if (pkey == NULL)
  611. return NULL;
  612. if (EVP_PKEY_get_id(pkey) == EVP_PKEY_DHX)
  613. key = EVP_PKEY_get1_DH(pkey);
  614. EVP_PKEY_free(pkey);
  615. if (key == NULL)
  616. return NULL;
  617. *pp = q;
  618. if (a != NULL) {
  619. DH_free(*a);
  620. *a = key;
  621. }
  622. return key;
  623. }
  624. int ossl_i2d_DHx_PUBKEY(const DH *a, unsigned char **pp)
  625. {
  626. EVP_PKEY *pktmp;
  627. int ret;
  628. if (!a)
  629. return 0;
  630. pktmp = EVP_PKEY_new();
  631. if (pktmp == NULL) {
  632. ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB);
  633. return -1;
  634. }
  635. (void)EVP_PKEY_assign(pktmp, EVP_PKEY_DHX, (DH *)a);
  636. ret = i2d_PUBKEY(pktmp, pp);
  637. pktmp->pkey.ptr = NULL;
  638. EVP_PKEY_free(pktmp);
  639. return ret;
  640. }
  641. #endif
  642. #ifndef OPENSSL_NO_DSA
  643. DSA *d2i_DSA_PUBKEY(DSA **a, const unsigned char **pp, long length)
  644. {
  645. EVP_PKEY *pkey;
  646. DSA *key = NULL;
  647. const unsigned char *q;
  648. q = *pp;
  649. pkey = ossl_d2i_PUBKEY_legacy(NULL, &q, length);
  650. if (pkey == NULL)
  651. return NULL;
  652. key = EVP_PKEY_get1_DSA(pkey);
  653. EVP_PKEY_free(pkey);
  654. if (key == NULL)
  655. return NULL;
  656. *pp = q;
  657. if (a != NULL) {
  658. DSA_free(*a);
  659. *a = key;
  660. }
  661. return key;
  662. }
  663. /* Called from decoders; disallows provided DSA keys without parameters. */
  664. DSA *ossl_d2i_DSA_PUBKEY(DSA **a, const unsigned char **pp, long length)
  665. {
  666. DSA *key = NULL;
  667. const unsigned char *data;
  668. const BIGNUM *p, *q, *g;
  669. data = *pp;
  670. key = d2i_DSA_PUBKEY(NULL, &data, length);
  671. if (key == NULL)
  672. return NULL;
  673. DSA_get0_pqg(key, &p, &q, &g);
  674. if (p == NULL || q == NULL || g == NULL) {
  675. DSA_free(key);
  676. return NULL;
  677. }
  678. *pp = data;
  679. if (a != NULL) {
  680. DSA_free(*a);
  681. *a = key;
  682. }
  683. return key;
  684. }
  685. int i2d_DSA_PUBKEY(const DSA *a, unsigned char **pp)
  686. {
  687. EVP_PKEY *pktmp;
  688. int ret;
  689. if (!a)
  690. return 0;
  691. pktmp = EVP_PKEY_new();
  692. if (pktmp == NULL) {
  693. ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB);
  694. return -1;
  695. }
  696. (void)EVP_PKEY_assign_DSA(pktmp, (DSA *)a);
  697. ret = i2d_PUBKEY(pktmp, pp);
  698. pktmp->pkey.ptr = NULL;
  699. EVP_PKEY_free(pktmp);
  700. return ret;
  701. }
  702. #endif
  703. #ifndef OPENSSL_NO_EC
  704. EC_KEY *d2i_EC_PUBKEY(EC_KEY **a, const unsigned char **pp, long length)
  705. {
  706. EVP_PKEY *pkey;
  707. EC_KEY *key = NULL;
  708. const unsigned char *q;
  709. int type;
  710. q = *pp;
  711. pkey = ossl_d2i_PUBKEY_legacy(NULL, &q, length);
  712. if (pkey == NULL)
  713. return NULL;
  714. type = EVP_PKEY_get_id(pkey);
  715. if (type == EVP_PKEY_EC || type == EVP_PKEY_SM2)
  716. key = EVP_PKEY_get1_EC_KEY(pkey);
  717. EVP_PKEY_free(pkey);
  718. if (key == NULL)
  719. return NULL;
  720. *pp = q;
  721. if (a != NULL) {
  722. EC_KEY_free(*a);
  723. *a = key;
  724. }
  725. return key;
  726. }
  727. int i2d_EC_PUBKEY(const EC_KEY *a, unsigned char **pp)
  728. {
  729. EVP_PKEY *pktmp;
  730. int ret;
  731. if (a == NULL)
  732. return 0;
  733. if ((pktmp = EVP_PKEY_new()) == NULL) {
  734. ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB);
  735. return -1;
  736. }
  737. (void)EVP_PKEY_assign_EC_KEY(pktmp, (EC_KEY *)a);
  738. ret = i2d_PUBKEY(pktmp, pp);
  739. pktmp->pkey.ptr = NULL;
  740. EVP_PKEY_free(pktmp);
  741. return ret;
  742. }
  743. # ifndef OPENSSL_NO_ECX
  744. ECX_KEY *ossl_d2i_ED25519_PUBKEY(ECX_KEY **a,
  745. const unsigned char **pp, long length)
  746. {
  747. EVP_PKEY *pkey;
  748. ECX_KEY *key = NULL;
  749. const unsigned char *q;
  750. q = *pp;
  751. pkey = ossl_d2i_PUBKEY_legacy(NULL, &q, length);
  752. if (pkey == NULL)
  753. return NULL;
  754. key = ossl_evp_pkey_get1_ED25519(pkey);
  755. EVP_PKEY_free(pkey);
  756. if (key == NULL)
  757. return NULL;
  758. *pp = q;
  759. if (a != NULL) {
  760. ossl_ecx_key_free(*a);
  761. *a = key;
  762. }
  763. return key;
  764. }
  765. int ossl_i2d_ED25519_PUBKEY(const ECX_KEY *a, unsigned char **pp)
  766. {
  767. EVP_PKEY *pktmp;
  768. int ret;
  769. if (a == NULL)
  770. return 0;
  771. if ((pktmp = EVP_PKEY_new()) == NULL) {
  772. ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB);
  773. return -1;
  774. }
  775. (void)EVP_PKEY_assign(pktmp, EVP_PKEY_ED25519, (ECX_KEY *)a);
  776. ret = i2d_PUBKEY(pktmp, pp);
  777. pktmp->pkey.ptr = NULL;
  778. EVP_PKEY_free(pktmp);
  779. return ret;
  780. }
  781. ECX_KEY *ossl_d2i_ED448_PUBKEY(ECX_KEY **a,
  782. const unsigned char **pp, long length)
  783. {
  784. EVP_PKEY *pkey;
  785. ECX_KEY *key = NULL;
  786. const unsigned char *q;
  787. q = *pp;
  788. pkey = ossl_d2i_PUBKEY_legacy(NULL, &q, length);
  789. if (pkey == NULL)
  790. return NULL;
  791. if (EVP_PKEY_get_id(pkey) == EVP_PKEY_ED448)
  792. key = ossl_evp_pkey_get1_ED448(pkey);
  793. EVP_PKEY_free(pkey);
  794. if (key == NULL)
  795. return NULL;
  796. *pp = q;
  797. if (a != NULL) {
  798. ossl_ecx_key_free(*a);
  799. *a = key;
  800. }
  801. return key;
  802. }
  803. int ossl_i2d_ED448_PUBKEY(const ECX_KEY *a, unsigned char **pp)
  804. {
  805. EVP_PKEY *pktmp;
  806. int ret;
  807. if (a == NULL)
  808. return 0;
  809. if ((pktmp = EVP_PKEY_new()) == NULL) {
  810. ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB);
  811. return -1;
  812. }
  813. (void)EVP_PKEY_assign(pktmp, EVP_PKEY_ED448, (ECX_KEY *)a);
  814. ret = i2d_PUBKEY(pktmp, pp);
  815. pktmp->pkey.ptr = NULL;
  816. EVP_PKEY_free(pktmp);
  817. return ret;
  818. }
  819. ECX_KEY *ossl_d2i_X25519_PUBKEY(ECX_KEY **a,
  820. const unsigned char **pp, long length)
  821. {
  822. EVP_PKEY *pkey;
  823. ECX_KEY *key = NULL;
  824. const unsigned char *q;
  825. q = *pp;
  826. pkey = ossl_d2i_PUBKEY_legacy(NULL, &q, length);
  827. if (pkey == NULL)
  828. return NULL;
  829. if (EVP_PKEY_get_id(pkey) == EVP_PKEY_X25519)
  830. key = ossl_evp_pkey_get1_X25519(pkey);
  831. EVP_PKEY_free(pkey);
  832. if (key == NULL)
  833. return NULL;
  834. *pp = q;
  835. if (a != NULL) {
  836. ossl_ecx_key_free(*a);
  837. *a = key;
  838. }
  839. return key;
  840. }
  841. int ossl_i2d_X25519_PUBKEY(const ECX_KEY *a, unsigned char **pp)
  842. {
  843. EVP_PKEY *pktmp;
  844. int ret;
  845. if (a == NULL)
  846. return 0;
  847. if ((pktmp = EVP_PKEY_new()) == NULL) {
  848. ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB);
  849. return -1;
  850. }
  851. (void)EVP_PKEY_assign(pktmp, EVP_PKEY_X25519, (ECX_KEY *)a);
  852. ret = i2d_PUBKEY(pktmp, pp);
  853. pktmp->pkey.ptr = NULL;
  854. EVP_PKEY_free(pktmp);
  855. return ret;
  856. }
  857. ECX_KEY *ossl_d2i_X448_PUBKEY(ECX_KEY **a,
  858. const unsigned char **pp, long length)
  859. {
  860. EVP_PKEY *pkey;
  861. ECX_KEY *key = NULL;
  862. const unsigned char *q;
  863. q = *pp;
  864. pkey = ossl_d2i_PUBKEY_legacy(NULL, &q, length);
  865. if (pkey == NULL)
  866. return NULL;
  867. if (EVP_PKEY_get_id(pkey) == EVP_PKEY_X448)
  868. key = ossl_evp_pkey_get1_X448(pkey);
  869. EVP_PKEY_free(pkey);
  870. if (key == NULL)
  871. return NULL;
  872. *pp = q;
  873. if (a != NULL) {
  874. ossl_ecx_key_free(*a);
  875. *a = key;
  876. }
  877. return key;
  878. }
  879. int ossl_i2d_X448_PUBKEY(const ECX_KEY *a, unsigned char **pp)
  880. {
  881. EVP_PKEY *pktmp;
  882. int ret;
  883. if (a == NULL)
  884. return 0;
  885. if ((pktmp = EVP_PKEY_new()) == NULL) {
  886. ERR_raise(ERR_LIB_ASN1, ERR_R_EVP_LIB);
  887. return -1;
  888. }
  889. (void)EVP_PKEY_assign(pktmp, EVP_PKEY_X448, (ECX_KEY *)a);
  890. ret = i2d_PUBKEY(pktmp, pp);
  891. pktmp->pkey.ptr = NULL;
  892. EVP_PKEY_free(pktmp);
  893. return ret;
  894. }
  895. # endif /* OPENSSL_NO_ECX */
  896. #endif
  897. void X509_PUBKEY_set0_public_key(X509_PUBKEY *pub,
  898. unsigned char *penc, int penclen)
  899. {
  900. ASN1_STRING_set0(pub->public_key, penc, penclen);
  901. ossl_asn1_string_set_bits_left(pub->public_key, 0);
  902. }
  903. int X509_PUBKEY_set0_param(X509_PUBKEY *pub, ASN1_OBJECT *aobj,
  904. int ptype, void *pval,
  905. unsigned char *penc, int penclen)
  906. {
  907. if (!X509_ALGOR_set0(pub->algor, aobj, ptype, pval))
  908. return 0;
  909. if (penc != NULL)
  910. X509_PUBKEY_set0_public_key(pub, penc, penclen);
  911. return 1;
  912. }
  913. int X509_PUBKEY_get0_param(ASN1_OBJECT **ppkalg,
  914. const unsigned char **pk, int *ppklen,
  915. X509_ALGOR **pa, const X509_PUBKEY *pub)
  916. {
  917. if (ppkalg)
  918. *ppkalg = pub->algor->algorithm;
  919. if (pk) {
  920. *pk = pub->public_key->data;
  921. *ppklen = pub->public_key->length;
  922. }
  923. if (pa)
  924. *pa = pub->algor;
  925. return 1;
  926. }
  927. ASN1_BIT_STRING *X509_get0_pubkey_bitstr(const X509 *x)
  928. {
  929. if (x == NULL)
  930. return NULL;
  931. return x->cert_info.key->public_key;
  932. }
  933. /* Returns 1 for equal, 0, for non-equal, < 0 on error */
  934. int X509_PUBKEY_eq(const X509_PUBKEY *a, const X509_PUBKEY *b)
  935. {
  936. X509_ALGOR *algA, *algB;
  937. EVP_PKEY *pA, *pB;
  938. if (a == b)
  939. return 1;
  940. if (a == NULL || b == NULL)
  941. return 0;
  942. if (!X509_PUBKEY_get0_param(NULL, NULL, NULL, &algA, a) || algA == NULL
  943. || !X509_PUBKEY_get0_param(NULL, NULL, NULL, &algB, b) || algB == NULL)
  944. return -2;
  945. if (X509_ALGOR_cmp(algA, algB) != 0)
  946. return 0;
  947. if ((pA = X509_PUBKEY_get0(a)) == NULL
  948. || (pB = X509_PUBKEY_get0(b)) == NULL)
  949. return -2;
  950. return EVP_PKEY_eq(pA, pB);
  951. }
  952. int ossl_x509_PUBKEY_get0_libctx(OSSL_LIB_CTX **plibctx, const char **ppropq,
  953. const X509_PUBKEY *key)
  954. {
  955. if (plibctx)
  956. *plibctx = key->libctx;
  957. if (ppropq)
  958. *ppropq = key->propq;
  959. return 1;
  960. }