]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/x509/x_pubkey.c
EVP: Have EVP_PKCS82PKEY_ex() pass a correct selection to OSSL_DECODER
[thirdparty/openssl.git] / crypto / x509 / x_pubkey.c
CommitLineData
b1322259 1/*
4333b89f 2 * Copyright 1995-2021 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
115 if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL
c8a9af97
MC
116 || !x509_pubkey_ex_populate((ASN1_VALUE **)&ret, NULL)
117 || !x509_pubkey_set0_libctx(ret, libctx, propq)) {
10315851
RL
118 x509_pubkey_ex_free((ASN1_VALUE **)&ret, NULL);
119 ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
120 } else {
121 *pval = (ASN1_VALUE *)ret;
122 }
123
124 return ret != NULL;
125}
126
c6313780
MC
127static int x509_pubkey_ex_d2i_ex(ASN1_VALUE **pval,
128 const unsigned char **in, long len,
129 const ASN1_ITEM *it, int tag, int aclass,
130 char opt, ASN1_TLC *ctx, OSSL_LIB_CTX *libctx,
131 const char *propq)
10315851
RL
132{
133 const unsigned char *in_saved = *in;
2b049e93 134 size_t publen;
10315851
RL
135 X509_PUBKEY *pubkey;
136 int ret;
137 OSSL_DECODER_CTX *dctx = NULL;
2b049e93 138 unsigned char *tmpbuf = NULL;
10315851 139
c6313780 140 if (*pval == NULL && !x509_pubkey_ex_new_ex(pval, it, libctx, propq))
10315851
RL
141 return 0;
142 if (!x509_pubkey_ex_populate(pval, NULL)) {
143 ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
144 return 0;
145 }
146
147 /* This ensures that |*in| advances properly no matter what */
148 if ((ret = ASN1_item_ex_d2i(pval, in, len,
149 ASN1_ITEM_rptr(X509_PUBKEY_INTERNAL),
150 tag, aclass, opt, ctx)) <= 0)
151 return ret;
152
2b049e93
MC
153 publen = *in - in_saved;
154 if (!ossl_assert(publen > 0)) {
155 ERR_raise(ERR_LIB_ASN1, ERR_R_INTERNAL_ERROR);
156 return 0;
157 }
158
10315851
RL
159 pubkey = (X509_PUBKEY *)*pval;
160 EVP_PKEY_free(pubkey->pkey);
161 pubkey->pkey = NULL;
162
163 /*
164 * Opportunistically decode the key but remove any non fatal errors
165 * from the queue. Subsequent explicit attempts to decode/use the key
166 * will return an appropriate error.
167 */
168 ERR_set_mark();
169
170 /*
171 * Try to decode with legacy method first. This ensures that engines
172 * aren't overriden by providers.
173 */
174 if ((ret = x509_pubkey_decode(&pubkey->pkey, pubkey)) == -1) {
175 /* -1 indicates a fatal error, like malloc failure */
176 ERR_clear_last_mark();
177 goto end;
178 }
179
180 /* Try to decode it into an EVP_PKEY with OSSL_DECODER */
181 if (ret <= 0 && !pubkey->flag_force_legacy) {
2b049e93 182 const unsigned char *p;
10315851 183 char txtoidname[OSSL_MAX_NAME_SIZE];
f8da1d80 184 size_t slen = publen;
10315851 185
2b049e93
MC
186 /*
187 * The decoders don't know how to handle anything other than Universal
188 * class so we modify the data accordingly.
189 */
190 if (aclass != V_ASN1_UNIVERSAL) {
191 tmpbuf = OPENSSL_memdup(in_saved, publen);
192 if (tmpbuf == NULL) {
193 ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
194 return 0;
195 }
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
286 if (pubkey == NULL
287 || !x509_pubkey_set0_libctx(pubkey, a->libctx, a->propq)
288 || (pubkey->algor = X509_ALGOR_dup(a->algor)) == NULL
289 || (pubkey->public_key = ASN1_BIT_STRING_new()) == NULL
290 || !ASN1_BIT_STRING_set(pubkey->public_key,
291 a->public_key->data, a->public_key->length)
292 || (a->pkey != NULL && !EVP_PKEY_up_ref(a->pkey))) {
10315851
RL
293 x509_pubkey_ex_free((ASN1_VALUE **)&pubkey,
294 ASN1_ITEM_rptr(X509_PUBKEY_INTERNAL));
295 ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
296 return NULL;
297 }
10315851
RL
298 pubkey->pkey = a->pkey;
299 return pubkey;
300}
d02b48c6 301
6b691a5c 302int X509_PUBKEY_set(X509_PUBKEY **x, EVP_PKEY *pkey)
0f113f3e
MC
303{
304 X509_PUBKEY *pk = NULL;
305
7836f949
DDO
306 if (x == NULL || pkey == NULL) {
307 ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER);
26a7d938 308 return 0;
7836f949 309 }
0f113f3e 310
e56ba0e1
RL
311 if (pkey->ameth != NULL) {
312 if ((pk = X509_PUBKEY_new()) == NULL) {
9311d0c4 313 ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
e56ba0e1
RL
314 goto error;
315 }
316 if (pkey->ameth->pub_encode != NULL) {
0f113f3e 317 if (!pkey->ameth->pub_encode(pk, pkey)) {
9311d0c4 318 ERR_raise(ERR_LIB_X509, X509_R_PUBLIC_KEY_ENCODE_ERROR);
0f113f3e
MC
319 goto error;
320 }
321 } else {
9311d0c4 322 ERR_raise(ERR_LIB_X509, X509_R_METHOD_NOT_SUPPORTED);
0f113f3e
MC
323 goto error;
324 }
113adc1f 325 } else if (evp_pkey_is_provided(pkey)) {
113adc1f
RL
326 unsigned char *der = NULL;
327 size_t derlen = 0;
ece9304c 328 OSSL_ENCODER_CTX *ectx =
fe75766c
TM
329 OSSL_ENCODER_CTX_new_for_pkey(pkey, EVP_PKEY_PUBLIC_KEY,
330 "DER", "SubjectPublicKeyInfo",
331 NULL);
e56ba0e1 332
113adc1f
RL
333 if (OSSL_ENCODER_to_data(ectx, &der, &derlen)) {
334 const unsigned char *pder = der;
e56ba0e1 335
113adc1f 336 pk = d2i_X509_PUBKEY(NULL, &pder, (long)derlen);
e56ba0e1
RL
337 }
338
ece9304c 339 OSSL_ENCODER_CTX_free(ectx);
113adc1f 340 OPENSSL_free(der);
0f113f3e
MC
341 }
342
7836f949
DDO
343 if (pk == NULL) {
344 ERR_raise(ERR_LIB_X509, X509_R_UNSUPPORTED_ALGORITHM);
345 goto error;
346 }
e56ba0e1 347
222561fe 348 X509_PUBKEY_free(*x);
e56ba0e1 349 if (!EVP_PKEY_up_ref(pkey)) {
9311d0c4 350 ERR_raise(ERR_LIB_X509, ERR_R_INTERNAL_ERROR);
e56ba0e1
RL
351 goto error;
352 }
0f113f3e 353 *x = pk;
a076951b
RL
354
355 /*
356 * pk->pkey is NULL when using the legacy routine, but is non-NULL when
ece9304c 357 * going through the encoder, and for all intents and purposes, it's
0d5bbaaa
MC
358 * a perfect copy of the public key portions of |pkey|, just not the same
359 * instance. If that's all there was to pkey then we could simply return
360 * early, right here. However, some application might very well depend on
361 * the passed |pkey| being used and none other, so we spend a few more
362 * cycles throwing away the newly created |pk->pkey| and replace it with
363 * |pkey|.
a076951b
RL
364 */
365 if (pk->pkey != NULL)
366 EVP_PKEY_free(pk->pkey);
367
fa0a9d71 368 pk->pkey = pkey;
e56ba0e1
RL
369 return 1;
370
0f113f3e 371 error:
222561fe 372 X509_PUBKEY_free(pk);
0f113f3e
MC
373 return 0;
374}
d02b48c6 375
fa0a9d71
DSH
376/*
377 * Attempt to decode a public key.
378 * Returns 1 on success, 0 for a decode failure and -1 for a fatal
379 * error e.g. malloc failure.
10315851
RL
380 *
381 * This function is #legacy.
fa0a9d71 382 */
7674e923 383static int x509_pubkey_decode(EVP_PKEY **ppkey, const X509_PUBKEY *key)
7fcdbd83 384{
29bf83c8
MC
385 EVP_PKEY *pkey;
386 int nid;
387
388 nid = OBJ_obj2nid(key->algor->algorithm);
389 if (!key->flag_force_legacy) {
390#ifndef OPENSSL_NO_ENGINE
391 ENGINE *e = NULL;
392
393 e = ENGINE_get_pkey_meth_engine(nid);
394 if (e == NULL)
395 return 0;
396 ENGINE_finish(e);
397#else
398 return 0;
399#endif
400 }
0f113f3e 401
29bf83c8 402 pkey = EVP_PKEY_new();
fa0a9d71 403 if (pkey == NULL) {
9311d0c4 404 ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
fa0a9d71 405 return -1;
0f113f3e
MC
406 }
407
29bf83c8 408 if (!EVP_PKEY_set_type(pkey, nid)) {
9311d0c4 409 ERR_raise(ERR_LIB_X509, X509_R_UNSUPPORTED_ALGORITHM);
0f113f3e
MC
410 goto error;
411 }
412
fa0a9d71
DSH
413 if (pkey->ameth->pub_decode) {
414 /*
415 * Treat any failure of pub_decode as a decode error. In
416 * future we could have different return codes for decode
417 * errors and fatal errors such as malloc failure.
418 */
66066e1b 419 if (!pkey->ameth->pub_decode(pkey, key))
0f113f3e 420 goto error;
0f113f3e 421 } else {
9311d0c4 422 ERR_raise(ERR_LIB_X509, X509_R_METHOD_NOT_SUPPORTED);
0f113f3e
MC
423 goto error;
424 }
425
fa0a9d71
DSH
426 *ppkey = pkey;
427 return 1;
0f113f3e
MC
428
429 error:
fa0a9d71
DSH
430 EVP_PKEY_free(pkey);
431 return 0;
432}
433
7674e923 434EVP_PKEY *X509_PUBKEY_get0(const X509_PUBKEY *key)
fa0a9d71 435{
1df8322c
MC
436 if (key == NULL) {
437 ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER);
fa0a9d71 438 return NULL;
1df8322c 439 }
fa0a9d71 440
1df8322c
MC
441 if (key->pkey == NULL) {
442 /* We failed to decode the key when we loaded it, or it was never set */
443 ERR_raise(ERR_LIB_EVP, EVP_R_DECODE_ERROR);
444 return NULL;
fa0a9d71
DSH
445 }
446
1df8322c 447 return key->pkey;
0f113f3e
MC
448}
449
7674e923 450EVP_PKEY *X509_PUBKEY_get(const X509_PUBKEY *key)
c01ff880
DSH
451{
452 EVP_PKEY *ret = X509_PUBKEY_get0(key);
e9e7b5df
BE
453
454 if (ret != NULL && !EVP_PKEY_up_ref(ret)) {
9311d0c4 455 ERR_raise(ERR_LIB_X509, ERR_R_INTERNAL_ERROR);
e9e7b5df
BE
456 ret = NULL;
457 }
c01ff880
DSH
458 return ret;
459}
460
0f113f3e 461/*
22b81444
RL
462 * Now three pseudo ASN1 routines that take an EVP_PKEY structure and encode
463 * or decode as X509_PUBKEY
52664f50 464 */
10315851
RL
465static EVP_PKEY *d2i_PUBKEY_int(EVP_PKEY **a,
466 const unsigned char **pp, long length,
467 OSSL_LIB_CTX *libctx, const char *propq,
468 unsigned int force_legacy,
469 X509_PUBKEY *
470 (*d2i_x509_pubkey)(X509_PUBKEY **a,
471 const unsigned char **in,
472 long len))
0f113f3e 473{
22b81444
RL
474 X509_PUBKEY *xpk, *xpk2 = NULL, **pxpk = NULL;
475 EVP_PKEY *pktmp = NULL;
a46c9789 476 const unsigned char *q;
12a765a5 477
a46c9789 478 q = *pp;
22b81444
RL
479
480 /*
481 * If libctx or propq are non-NULL, we take advantage of the reuse
482 * feature. It's not generally recommended, but is safe enough for
483 * newly created structures.
484 */
10315851 485 if (libctx != NULL || propq != NULL || force_legacy) {
22b81444
RL
486 xpk2 = OPENSSL_zalloc(sizeof(*xpk2));
487 if (xpk2 == NULL) {
488 ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
489 return NULL;
490 }
637dce3c
SL
491 if (!x509_pubkey_set0_libctx(xpk2, libctx, propq))
492 goto end;
10315851 493 xpk2->flag_force_legacy = !!force_legacy;
22b81444
RL
494 pxpk = &xpk2;
495 }
10315851 496 xpk = d2i_x509_pubkey(pxpk, &q, length);
12a765a5 497 if (xpk == NULL)
22b81444 498 goto end;
0f113f3e
MC
499 pktmp = X509_PUBKEY_get(xpk);
500 X509_PUBKEY_free(xpk);
22b81444 501 xpk2 = NULL; /* We know that xpk == xpk2 */
12a765a5 502 if (pktmp == NULL)
22b81444 503 goto end;
a46c9789 504 *pp = q;
12a765a5 505 if (a != NULL) {
0f113f3e
MC
506 EVP_PKEY_free(*a);
507 *a = pktmp;
508 }
22b81444
RL
509 end:
510 X509_PUBKEY_free(xpk2);
0f113f3e
MC
511 return pktmp;
512}
52664f50 513
10315851 514/* For the algorithm specific d2i functions further down */
b2f1b365
MC
515EVP_PKEY *ossl_d2i_PUBKEY_legacy(EVP_PKEY **a, const unsigned char **pp,
516 long length)
10315851
RL
517{
518 return d2i_PUBKEY_int(a, pp, length, NULL, NULL, 1, d2i_X509_PUBKEY);
519}
520
521EVP_PKEY *d2i_PUBKEY_ex(EVP_PKEY **a, const unsigned char **pp, long length,
522 OSSL_LIB_CTX *libctx, const char *propq)
523{
524 return d2i_PUBKEY_int(a, pp, length, libctx, propq, 0, d2i_X509_PUBKEY);
525}
526
22b81444
RL
527EVP_PKEY *d2i_PUBKEY(EVP_PKEY **a, const unsigned char **pp, long length)
528{
529 return d2i_PUBKEY_ex(a, pp, length, NULL, NULL);
530}
531
9fdcc21f 532int i2d_PUBKEY(const EVP_PKEY *a, unsigned char **pp)
0f113f3e 533{
9fdcc21f
DO
534 int ret = -1;
535
536 if (a == NULL)
0f113f3e 537 return 0;
cdb16632
RL
538 if (a->ameth != NULL) {
539 X509_PUBKEY *xpk = NULL;
540
541 if ((xpk = X509_PUBKEY_new()) == NULL)
542 return -1;
543
544 /* pub_encode() only encode parameters, not the key itself */
545 if (a->ameth->pub_encode != NULL && a->ameth->pub_encode(xpk, a)) {
546 xpk->pkey = (EVP_PKEY *)a;
547 ret = i2d_X509_PUBKEY(xpk, pp);
548 xpk->pkey = NULL;
549 }
550 X509_PUBKEY_free(xpk);
3c6ed955 551 } else if (a->keymgmt != NULL) {
ece9304c 552 OSSL_ENCODER_CTX *ctx =
fe75766c
TM
553 OSSL_ENCODER_CTX_new_for_pkey(a, EVP_PKEY_PUBLIC_KEY,
554 "DER", "SubjectPublicKeyInfo",
555 NULL);
cdb16632
RL
556 BIO *out = BIO_new(BIO_s_mem());
557 BUF_MEM *buf = NULL;
558
97bb8dff 559 if (OSSL_ENCODER_CTX_get_num_encoders(ctx) != 0
cdb16632 560 && out != NULL
ece9304c 561 && OSSL_ENCODER_to_bio(ctx, out)
cdb16632
RL
562 && BIO_get_mem_ptr(out, &buf) > 0) {
563 ret = buf->length;
564
565 if (pp != NULL) {
566 if (*pp == NULL) {
567 *pp = (unsigned char *)buf->data;
568 buf->length = 0;
569 buf->data = NULL;
570 } else {
571 memcpy(*pp, buf->data, ret);
572 *pp += ret;
573 }
574 }
575 }
576 BIO_free(out);
ece9304c 577 OSSL_ENCODER_CTX_free(ctx);
cdb16632
RL
578 }
579
0f113f3e
MC
580 return ret;
581}
582
583/*
584 * The following are equivalents but which return RSA and DSA keys
52664f50 585 */
0f113f3e
MC
586RSA *d2i_RSA_PUBKEY(RSA **a, const unsigned char **pp, long length)
587{
588 EVP_PKEY *pkey;
06f67612 589 RSA *key = NULL;
0f113f3e 590 const unsigned char *q;
12a765a5 591
0f113f3e 592 q = *pp;
b2f1b365 593 pkey = ossl_d2i_PUBKEY_legacy(NULL, &q, length);
12a765a5 594 if (pkey == NULL)
0f113f3e
MC
595 return NULL;
596 key = EVP_PKEY_get1_RSA(pkey);
597 EVP_PKEY_free(pkey);
12a765a5 598 if (key == NULL)
0f113f3e
MC
599 return NULL;
600 *pp = q;
12a765a5 601 if (a != NULL) {
0f113f3e
MC
602 RSA_free(*a);
603 *a = key;
604 }
605 return key;
606}
52664f50 607
9fdcc21f 608int i2d_RSA_PUBKEY(const RSA *a, unsigned char **pp)
0f113f3e
MC
609{
610 EVP_PKEY *pktmp;
611 int ret;
612 if (!a)
613 return 0;
614 pktmp = EVP_PKEY_new();
90945fa3 615 if (pktmp == NULL) {
9311d0c4 616 ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
157997f0 617 return -1;
0f113f3e 618 }
9fdcc21f 619 (void)EVP_PKEY_assign_RSA(pktmp, (RSA *)a);
0f113f3e 620 ret = i2d_PUBKEY(pktmp, pp);
9fdcc21f 621 pktmp->pkey.ptr = NULL;
0f113f3e
MC
622 EVP_PKEY_free(pktmp);
623 return ret;
624}
52664f50 625
06f67612
RL
626#ifndef OPENSSL_NO_DH
627DH *ossl_d2i_DH_PUBKEY(DH **a, const unsigned char **pp, long length)
628{
629 EVP_PKEY *pkey;
630 DH *key = NULL;
631 const unsigned char *q;
632
633 q = *pp;
b2f1b365 634 pkey = ossl_d2i_PUBKEY_legacy(NULL, &q, length);
06f67612
RL
635 if (pkey == NULL)
636 return NULL;
ed576acd 637 if (EVP_PKEY_get_id(pkey) == EVP_PKEY_DH)
06f67612
RL
638 key = EVP_PKEY_get1_DH(pkey);
639 EVP_PKEY_free(pkey);
640 if (key == NULL)
641 return NULL;
642 *pp = q;
643 if (a != NULL) {
644 DH_free(*a);
645 *a = key;
646 }
647 return key;
648}
649
650int ossl_i2d_DH_PUBKEY(const DH *a, unsigned char **pp)
651{
652 EVP_PKEY *pktmp;
653 int ret;
654 if (!a)
655 return 0;
656 pktmp = EVP_PKEY_new();
657 if (pktmp == NULL) {
658 ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
659 return -1;
660 }
661 (void)EVP_PKEY_assign_DH(pktmp, (DH *)a);
662 ret = i2d_PUBKEY(pktmp, pp);
663 pktmp->pkey.ptr = NULL;
664 EVP_PKEY_free(pktmp);
665 return ret;
666}
667
668DH *ossl_d2i_DHx_PUBKEY(DH **a, const unsigned char **pp, long length)
669{
670 EVP_PKEY *pkey;
671 DH *key = NULL;
672 const unsigned char *q;
673
674 q = *pp;
b2f1b365 675 pkey = ossl_d2i_PUBKEY_legacy(NULL, &q, length);
06f67612
RL
676 if (pkey == NULL)
677 return NULL;
ed576acd 678 if (EVP_PKEY_get_id(pkey) == EVP_PKEY_DHX)
06f67612
RL
679 key = EVP_PKEY_get1_DH(pkey);
680 EVP_PKEY_free(pkey);
681 if (key == NULL)
682 return NULL;
683 *pp = q;
684 if (a != NULL) {
685 DH_free(*a);
686 *a = key;
687 }
688 return key;
689}
690
691int ossl_i2d_DHx_PUBKEY(const DH *a, unsigned char **pp)
692{
693 EVP_PKEY *pktmp;
694 int ret;
695 if (!a)
696 return 0;
697 pktmp = EVP_PKEY_new();
698 if (pktmp == NULL) {
699 ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
700 return -1;
701 }
702 (void)EVP_PKEY_assign(pktmp, EVP_PKEY_DHX, (DH *)a);
703 ret = i2d_PUBKEY(pktmp, pp);
704 pktmp->pkey.ptr = NULL;
705 EVP_PKEY_free(pktmp);
706 return ret;
707}
708#endif
709
cf1b7d96 710#ifndef OPENSSL_NO_DSA
0f113f3e
MC
711DSA *d2i_DSA_PUBKEY(DSA **a, const unsigned char **pp, long length)
712{
713 EVP_PKEY *pkey;
06f67612 714 DSA *key = NULL;
0f113f3e 715 const unsigned char *q;
12a765a5 716
0f113f3e 717 q = *pp;
b2f1b365 718 pkey = ossl_d2i_PUBKEY_legacy(NULL, &q, length);
12a765a5 719 if (pkey == NULL)
0f113f3e
MC
720 return NULL;
721 key = EVP_PKEY_get1_DSA(pkey);
722 EVP_PKEY_free(pkey);
12a765a5 723 if (key == NULL)
0f113f3e
MC
724 return NULL;
725 *pp = q;
12a765a5 726 if (a != NULL) {
0f113f3e
MC
727 DSA_free(*a);
728 *a = key;
729 }
730 return key;
731}
52664f50 732
9fdcc21f 733int i2d_DSA_PUBKEY(const DSA *a, unsigned char **pp)
0f113f3e
MC
734{
735 EVP_PKEY *pktmp;
736 int ret;
737 if (!a)
738 return 0;
739 pktmp = EVP_PKEY_new();
90945fa3 740 if (pktmp == NULL) {
9311d0c4 741 ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
157997f0 742 return -1;
0f113f3e 743 }
9fdcc21f 744 (void)EVP_PKEY_assign_DSA(pktmp, (DSA *)a);
0f113f3e 745 ret = i2d_PUBKEY(pktmp, pp);
9fdcc21f 746 pktmp->pkey.ptr = NULL;
0f113f3e
MC
747 EVP_PKEY_free(pktmp);
748 return ret;
749}
4d94ae00
BM
750#endif
751
14a7cfb3 752#ifndef OPENSSL_NO_EC
6343829a 753EC_KEY *d2i_EC_PUBKEY(EC_KEY **a, const unsigned char **pp, long length)
0f113f3e
MC
754{
755 EVP_PKEY *pkey;
06f67612 756 EC_KEY *key = NULL;
0f113f3e 757 const unsigned char *q;
b4c4a2c6 758 int type;
12a765a5 759
0f113f3e 760 q = *pp;
b2f1b365 761 pkey = ossl_d2i_PUBKEY_legacy(NULL, &q, length);
12a765a5 762 if (pkey == NULL)
26a7d938 763 return NULL;
ed576acd 764 type = EVP_PKEY_get_id(pkey);
b4c4a2c6 765 if (type == EVP_PKEY_EC || type == EVP_PKEY_SM2)
06f67612 766 key = EVP_PKEY_get1_EC_KEY(pkey);
0f113f3e 767 EVP_PKEY_free(pkey);
12a765a5 768 if (key == NULL)
26a7d938 769 return NULL;
0f113f3e 770 *pp = q;
12a765a5 771 if (a != NULL) {
0f113f3e
MC
772 EC_KEY_free(*a);
773 *a = key;
774 }
26a7d938 775 return key;
0f113f3e 776}
4d94ae00 777
9fdcc21f 778int i2d_EC_PUBKEY(const EC_KEY *a, unsigned char **pp)
0f113f3e
MC
779{
780 EVP_PKEY *pktmp;
781 int ret;
12a765a5
RS
782
783 if (a == NULL)
26a7d938 784 return 0;
0f113f3e 785 if ((pktmp = EVP_PKEY_new()) == NULL) {
9311d0c4 786 ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
157997f0 787 return -1;
0f113f3e 788 }
9fdcc21f 789 (void)EVP_PKEY_assign_EC_KEY(pktmp, (EC_KEY *)a);
0f113f3e 790 ret = i2d_PUBKEY(pktmp, pp);
9fdcc21f 791 pktmp->pkey.ptr = NULL;
0f113f3e 792 EVP_PKEY_free(pktmp);
26a7d938 793 return ret;
0f113f3e 794}
06f67612
RL
795
796ECX_KEY *ossl_d2i_ED25519_PUBKEY(ECX_KEY **a,
797 const unsigned char **pp, long length)
798{
799 EVP_PKEY *pkey;
800 ECX_KEY *key = NULL;
801 const unsigned char *q;
802
803 q = *pp;
b2f1b365 804 pkey = ossl_d2i_PUBKEY_legacy(NULL, &q, length);
06f67612
RL
805 if (pkey == NULL)
806 return NULL;
807 key = ossl_evp_pkey_get1_ED25519(pkey);
808 EVP_PKEY_free(pkey);
809 if (key == NULL)
810 return NULL;
811 *pp = q;
812 if (a != NULL) {
813 ossl_ecx_key_free(*a);
814 *a = key;
815 }
816 return key;
817}
818
819int ossl_i2d_ED25519_PUBKEY(const ECX_KEY *a, unsigned char **pp)
820{
821 EVP_PKEY *pktmp;
822 int ret;
823
824 if (a == NULL)
825 return 0;
826 if ((pktmp = EVP_PKEY_new()) == NULL) {
827 ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
828 return -1;
829 }
830 (void)EVP_PKEY_assign(pktmp, EVP_PKEY_ED25519, (ECX_KEY *)a);
831 ret = i2d_PUBKEY(pktmp, pp);
832 pktmp->pkey.ptr = NULL;
833 EVP_PKEY_free(pktmp);
834 return ret;
835}
836
837ECX_KEY *ossl_d2i_ED448_PUBKEY(ECX_KEY **a,
838 const unsigned char **pp, long length)
839{
840 EVP_PKEY *pkey;
841 ECX_KEY *key = NULL;
842 const unsigned char *q;
843
844 q = *pp;
b2f1b365 845 pkey = ossl_d2i_PUBKEY_legacy(NULL, &q, length);
06f67612
RL
846 if (pkey == NULL)
847 return NULL;
ed576acd 848 if (EVP_PKEY_get_id(pkey) == EVP_PKEY_ED448)
06f67612
RL
849 key = ossl_evp_pkey_get1_ED448(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_ED448_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) {
869 ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
870 return -1;
871 }
872 (void)EVP_PKEY_assign(pktmp, EVP_PKEY_ED448, (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_X25519_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_X25519)
06f67612
RL
891 key = ossl_evp_pkey_get1_X25519(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_X25519_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) {
911 ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
912 return -1;
913 }
914 (void)EVP_PKEY_assign(pktmp, EVP_PKEY_X25519, (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_X448_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_X448)
06f67612
RL
933 key = ossl_evp_pkey_get1_X448(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_X448_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) {
953 ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
954 return -1;
955 }
956 (void)EVP_PKEY_assign(pktmp, EVP_PKEY_X448, (ECX_KEY *)a);
957 ret = i2d_PUBKEY(pktmp, pp);
958 pktmp->pkey.ptr = NULL;
959 EVP_PKEY_free(pktmp);
960 return ret;
961}
962
12aefe78 963#endif
448be743
DSH
964
965int X509_PUBKEY_set0_param(X509_PUBKEY *pub, ASN1_OBJECT *aobj,
0f113f3e
MC
966 int ptype, void *pval,
967 unsigned char *penc, int penclen)
968{
969 if (!X509_ALGOR_set0(pub->algor, aobj, ptype, pval))
970 return 0;
971 if (penc) {
b548a1f1 972 OPENSSL_free(pub->public_key->data);
0f113f3e
MC
973 pub->public_key->data = penc;
974 pub->public_key->length = penclen;
975 /* Set number of unused bits to zero */
976 pub->public_key->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
977 pub->public_key->flags |= ASN1_STRING_FLAG_BITS_LEFT;
978 }
979 return 1;
980}
448be743
DSH
981
982int X509_PUBKEY_get0_param(ASN1_OBJECT **ppkalg,
0f113f3e 983 const unsigned char **pk, int *ppklen,
7674e923 984 X509_ALGOR **pa, const X509_PUBKEY *pub)
0f113f3e
MC
985{
986 if (ppkalg)
987 *ppkalg = pub->algor->algorithm;
988 if (pk) {
989 *pk = pub->public_key->data;
990 *ppklen = pub->public_key->length;
991 }
992 if (pa)
993 *pa = pub->algor;
994 return 1;
995}
29fa0a1a
DSH
996
997ASN1_BIT_STRING *X509_get0_pubkey_bitstr(const X509 *x)
998{
999 if (x == NULL)
1000 return NULL;
1001 return x->cert_info.key->public_key;
1002}
93f99b68
DDO
1003
1004/* Returns 1 for equal, 0, for non-equal, < 0 on error */
1005int X509_PUBKEY_eq(const X509_PUBKEY *a, const X509_PUBKEY *b)
1006{
1007 X509_ALGOR *algA, *algB;
1008 EVP_PKEY *pA, *pB;
1009
1010 if (a == b)
1011 return 1;
1012 if (a == NULL || b == NULL)
1013 return 0;
1014 if (!X509_PUBKEY_get0_param(NULL, NULL, NULL, &algA, a) || algA == NULL
1015 || !X509_PUBKEY_get0_param(NULL, NULL, NULL, &algB, b) || algB == NULL)
1016 return -2;
1017 if (X509_ALGOR_cmp(algA, algB) != 0)
1018 return 0;
1019 if ((pA = X509_PUBKEY_get0(a)) == NULL
1020 || (pB = X509_PUBKEY_get0(b)) == NULL)
1021 return -2;
c74aaa39 1022 return EVP_PKEY_eq(pA, pB);
93f99b68 1023}
22b81444 1024
4669015d
SL
1025int ossl_x509_PUBKEY_get0_libctx(OSSL_LIB_CTX **plibctx, const char **ppropq,
1026 const X509_PUBKEY *key)
22b81444
RL
1027{
1028 if (plibctx)
1029 *plibctx = key->libctx;
1030 if (ppropq)
1031 *ppropq = key->propq;
1032 return 1;
1033}