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