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