x_pubkey.c 28 KB

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