]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/x509/x_pubkey.c
Update copyright year
[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
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
e304aa87 173 * aren't overridden by providers.
10315851
RL
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,
7e35458b
TM
292 a->public_key->data,
293 a->public_key->length)) {
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 }
7e35458b
TM
299
300 if (a->pkey != NULL) {
301 ERR_set_mark();
302 pubkey->pkey = EVP_PKEY_dup(a->pkey);
303 if (pubkey->pkey == NULL) {
304 pubkey->flag_force_legacy = 1;
305 if (x509_pubkey_decode(&pubkey->pkey, pubkey) <= 0) {
306 x509_pubkey_ex_free((ASN1_VALUE **)&pubkey,
307 ASN1_ITEM_rptr(X509_PUBKEY_INTERNAL));
308 ERR_clear_last_mark();
309 return NULL;
310 }
311 }
312 ERR_pop_to_mark();
313 }
10315851
RL
314 return pubkey;
315}
d02b48c6 316
6b691a5c 317int X509_PUBKEY_set(X509_PUBKEY **x, EVP_PKEY *pkey)
0f113f3e
MC
318{
319 X509_PUBKEY *pk = NULL;
320
7836f949
DDO
321 if (x == NULL || pkey == NULL) {
322 ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER);
26a7d938 323 return 0;
7836f949 324 }
0f113f3e 325
e56ba0e1
RL
326 if (pkey->ameth != NULL) {
327 if ((pk = X509_PUBKEY_new()) == NULL) {
9311d0c4 328 ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
e56ba0e1
RL
329 goto error;
330 }
331 if (pkey->ameth->pub_encode != NULL) {
0f113f3e 332 if (!pkey->ameth->pub_encode(pk, pkey)) {
9311d0c4 333 ERR_raise(ERR_LIB_X509, X509_R_PUBLIC_KEY_ENCODE_ERROR);
0f113f3e
MC
334 goto error;
335 }
336 } else {
9311d0c4 337 ERR_raise(ERR_LIB_X509, X509_R_METHOD_NOT_SUPPORTED);
0f113f3e
MC
338 goto error;
339 }
113adc1f 340 } else if (evp_pkey_is_provided(pkey)) {
113adc1f
RL
341 unsigned char *der = NULL;
342 size_t derlen = 0;
ece9304c 343 OSSL_ENCODER_CTX *ectx =
fe75766c
TM
344 OSSL_ENCODER_CTX_new_for_pkey(pkey, EVP_PKEY_PUBLIC_KEY,
345 "DER", "SubjectPublicKeyInfo",
346 NULL);
e56ba0e1 347
113adc1f
RL
348 if (OSSL_ENCODER_to_data(ectx, &der, &derlen)) {
349 const unsigned char *pder = der;
e56ba0e1 350
113adc1f 351 pk = d2i_X509_PUBKEY(NULL, &pder, (long)derlen);
e56ba0e1
RL
352 }
353
ece9304c 354 OSSL_ENCODER_CTX_free(ectx);
113adc1f 355 OPENSSL_free(der);
0f113f3e
MC
356 }
357
7836f949
DDO
358 if (pk == NULL) {
359 ERR_raise(ERR_LIB_X509, X509_R_UNSUPPORTED_ALGORITHM);
360 goto error;
361 }
e56ba0e1 362
222561fe 363 X509_PUBKEY_free(*x);
e56ba0e1 364 if (!EVP_PKEY_up_ref(pkey)) {
9311d0c4 365 ERR_raise(ERR_LIB_X509, ERR_R_INTERNAL_ERROR);
e56ba0e1
RL
366 goto error;
367 }
0f113f3e 368 *x = pk;
a076951b
RL
369
370 /*
371 * pk->pkey is NULL when using the legacy routine, but is non-NULL when
ece9304c 372 * going through the encoder, and for all intents and purposes, it's
0d5bbaaa
MC
373 * a perfect copy of the public key portions of |pkey|, just not the same
374 * instance. If that's all there was to pkey then we could simply return
375 * early, right here. However, some application might very well depend on
376 * the passed |pkey| being used and none other, so we spend a few more
377 * cycles throwing away the newly created |pk->pkey| and replace it with
378 * |pkey|.
a076951b
RL
379 */
380 if (pk->pkey != NULL)
381 EVP_PKEY_free(pk->pkey);
382
fa0a9d71 383 pk->pkey = pkey;
e56ba0e1
RL
384 return 1;
385
0f113f3e 386 error:
222561fe 387 X509_PUBKEY_free(pk);
0f113f3e
MC
388 return 0;
389}
d02b48c6 390
fa0a9d71
DSH
391/*
392 * Attempt to decode a public key.
393 * Returns 1 on success, 0 for a decode failure and -1 for a fatal
394 * error e.g. malloc failure.
10315851
RL
395 *
396 * This function is #legacy.
fa0a9d71 397 */
7674e923 398static int x509_pubkey_decode(EVP_PKEY **ppkey, const X509_PUBKEY *key)
7fcdbd83 399{
29bf83c8
MC
400 EVP_PKEY *pkey;
401 int nid;
402
403 nid = OBJ_obj2nid(key->algor->algorithm);
404 if (!key->flag_force_legacy) {
405#ifndef OPENSSL_NO_ENGINE
406 ENGINE *e = NULL;
407
408 e = ENGINE_get_pkey_meth_engine(nid);
409 if (e == NULL)
410 return 0;
411 ENGINE_finish(e);
412#else
413 return 0;
414#endif
415 }
0f113f3e 416
29bf83c8 417 pkey = EVP_PKEY_new();
fa0a9d71 418 if (pkey == NULL) {
9311d0c4 419 ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
fa0a9d71 420 return -1;
0f113f3e
MC
421 }
422
29bf83c8 423 if (!EVP_PKEY_set_type(pkey, nid)) {
9311d0c4 424 ERR_raise(ERR_LIB_X509, X509_R_UNSUPPORTED_ALGORITHM);
0f113f3e
MC
425 goto error;
426 }
427
fa0a9d71
DSH
428 if (pkey->ameth->pub_decode) {
429 /*
430 * Treat any failure of pub_decode as a decode error. In
431 * future we could have different return codes for decode
432 * errors and fatal errors such as malloc failure.
433 */
66066e1b 434 if (!pkey->ameth->pub_decode(pkey, key))
0f113f3e 435 goto error;
0f113f3e 436 } else {
9311d0c4 437 ERR_raise(ERR_LIB_X509, X509_R_METHOD_NOT_SUPPORTED);
0f113f3e
MC
438 goto error;
439 }
440
fa0a9d71
DSH
441 *ppkey = pkey;
442 return 1;
0f113f3e
MC
443
444 error:
fa0a9d71
DSH
445 EVP_PKEY_free(pkey);
446 return 0;
447}
448
7674e923 449EVP_PKEY *X509_PUBKEY_get0(const X509_PUBKEY *key)
fa0a9d71 450{
1df8322c
MC
451 if (key == NULL) {
452 ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER);
fa0a9d71 453 return NULL;
1df8322c 454 }
fa0a9d71 455
1df8322c
MC
456 if (key->pkey == NULL) {
457 /* We failed to decode the key when we loaded it, or it was never set */
458 ERR_raise(ERR_LIB_EVP, EVP_R_DECODE_ERROR);
459 return NULL;
fa0a9d71
DSH
460 }
461
1df8322c 462 return key->pkey;
0f113f3e
MC
463}
464
7674e923 465EVP_PKEY *X509_PUBKEY_get(const X509_PUBKEY *key)
c01ff880
DSH
466{
467 EVP_PKEY *ret = X509_PUBKEY_get0(key);
e9e7b5df
BE
468
469 if (ret != NULL && !EVP_PKEY_up_ref(ret)) {
9311d0c4 470 ERR_raise(ERR_LIB_X509, ERR_R_INTERNAL_ERROR);
e9e7b5df
BE
471 ret = NULL;
472 }
c01ff880
DSH
473 return ret;
474}
475
0f113f3e 476/*
22b81444
RL
477 * Now three pseudo ASN1 routines that take an EVP_PKEY structure and encode
478 * or decode as X509_PUBKEY
52664f50 479 */
10315851
RL
480static EVP_PKEY *d2i_PUBKEY_int(EVP_PKEY **a,
481 const unsigned char **pp, long length,
482 OSSL_LIB_CTX *libctx, const char *propq,
483 unsigned int force_legacy,
484 X509_PUBKEY *
485 (*d2i_x509_pubkey)(X509_PUBKEY **a,
486 const unsigned char **in,
487 long len))
0f113f3e 488{
22b81444
RL
489 X509_PUBKEY *xpk, *xpk2 = NULL, **pxpk = NULL;
490 EVP_PKEY *pktmp = NULL;
a46c9789 491 const unsigned char *q;
12a765a5 492
a46c9789 493 q = *pp;
22b81444
RL
494
495 /*
496 * If libctx or propq are non-NULL, we take advantage of the reuse
497 * feature. It's not generally recommended, but is safe enough for
498 * newly created structures.
499 */
10315851 500 if (libctx != NULL || propq != NULL || force_legacy) {
22b81444
RL
501 xpk2 = OPENSSL_zalloc(sizeof(*xpk2));
502 if (xpk2 == NULL) {
503 ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
504 return NULL;
505 }
637dce3c
SL
506 if (!x509_pubkey_set0_libctx(xpk2, libctx, propq))
507 goto end;
10315851 508 xpk2->flag_force_legacy = !!force_legacy;
22b81444
RL
509 pxpk = &xpk2;
510 }
10315851 511 xpk = d2i_x509_pubkey(pxpk, &q, length);
12a765a5 512 if (xpk == NULL)
22b81444 513 goto end;
0f113f3e
MC
514 pktmp = X509_PUBKEY_get(xpk);
515 X509_PUBKEY_free(xpk);
22b81444 516 xpk2 = NULL; /* We know that xpk == xpk2 */
12a765a5 517 if (pktmp == NULL)
22b81444 518 goto end;
a46c9789 519 *pp = q;
12a765a5 520 if (a != NULL) {
0f113f3e
MC
521 EVP_PKEY_free(*a);
522 *a = pktmp;
523 }
22b81444
RL
524 end:
525 X509_PUBKEY_free(xpk2);
0f113f3e
MC
526 return pktmp;
527}
52664f50 528
10315851 529/* For the algorithm specific d2i functions further down */
b2f1b365
MC
530EVP_PKEY *ossl_d2i_PUBKEY_legacy(EVP_PKEY **a, const unsigned char **pp,
531 long length)
10315851
RL
532{
533 return d2i_PUBKEY_int(a, pp, length, NULL, NULL, 1, d2i_X509_PUBKEY);
534}
535
536EVP_PKEY *d2i_PUBKEY_ex(EVP_PKEY **a, const unsigned char **pp, long length,
537 OSSL_LIB_CTX *libctx, const char *propq)
538{
539 return d2i_PUBKEY_int(a, pp, length, libctx, propq, 0, d2i_X509_PUBKEY);
540}
541
22b81444
RL
542EVP_PKEY *d2i_PUBKEY(EVP_PKEY **a, const unsigned char **pp, long length)
543{
544 return d2i_PUBKEY_ex(a, pp, length, NULL, NULL);
545}
546
9fdcc21f 547int i2d_PUBKEY(const EVP_PKEY *a, unsigned char **pp)
0f113f3e 548{
9fdcc21f
DO
549 int ret = -1;
550
551 if (a == NULL)
0f113f3e 552 return 0;
cdb16632
RL
553 if (a->ameth != NULL) {
554 X509_PUBKEY *xpk = NULL;
555
556 if ((xpk = X509_PUBKEY_new()) == NULL)
557 return -1;
558
559 /* pub_encode() only encode parameters, not the key itself */
560 if (a->ameth->pub_encode != NULL && a->ameth->pub_encode(xpk, a)) {
561 xpk->pkey = (EVP_PKEY *)a;
562 ret = i2d_X509_PUBKEY(xpk, pp);
563 xpk->pkey = NULL;
564 }
565 X509_PUBKEY_free(xpk);
3c6ed955 566 } else if (a->keymgmt != NULL) {
ece9304c 567 OSSL_ENCODER_CTX *ctx =
fe75766c
TM
568 OSSL_ENCODER_CTX_new_for_pkey(a, EVP_PKEY_PUBLIC_KEY,
569 "DER", "SubjectPublicKeyInfo",
570 NULL);
cdb16632
RL
571 BIO *out = BIO_new(BIO_s_mem());
572 BUF_MEM *buf = NULL;
573
97bb8dff 574 if (OSSL_ENCODER_CTX_get_num_encoders(ctx) != 0
cdb16632 575 && out != NULL
ece9304c 576 && OSSL_ENCODER_to_bio(ctx, out)
cdb16632
RL
577 && BIO_get_mem_ptr(out, &buf) > 0) {
578 ret = buf->length;
579
580 if (pp != NULL) {
581 if (*pp == NULL) {
582 *pp = (unsigned char *)buf->data;
583 buf->length = 0;
584 buf->data = NULL;
585 } else {
586 memcpy(*pp, buf->data, ret);
587 *pp += ret;
588 }
589 }
590 }
591 BIO_free(out);
ece9304c 592 OSSL_ENCODER_CTX_free(ctx);
cdb16632
RL
593 }
594
0f113f3e
MC
595 return ret;
596}
597
598/*
599 * The following are equivalents but which return RSA and DSA keys
52664f50 600 */
0f113f3e
MC
601RSA *d2i_RSA_PUBKEY(RSA **a, const unsigned char **pp, long length)
602{
603 EVP_PKEY *pkey;
06f67612 604 RSA *key = NULL;
0f113f3e 605 const unsigned char *q;
12a765a5 606
0f113f3e 607 q = *pp;
b2f1b365 608 pkey = ossl_d2i_PUBKEY_legacy(NULL, &q, length);
12a765a5 609 if (pkey == NULL)
0f113f3e
MC
610 return NULL;
611 key = EVP_PKEY_get1_RSA(pkey);
612 EVP_PKEY_free(pkey);
12a765a5 613 if (key == NULL)
0f113f3e
MC
614 return NULL;
615 *pp = q;
12a765a5 616 if (a != NULL) {
0f113f3e
MC
617 RSA_free(*a);
618 *a = key;
619 }
620 return key;
621}
52664f50 622
9fdcc21f 623int i2d_RSA_PUBKEY(const RSA *a, unsigned char **pp)
0f113f3e
MC
624{
625 EVP_PKEY *pktmp;
626 int ret;
627 if (!a)
628 return 0;
629 pktmp = EVP_PKEY_new();
90945fa3 630 if (pktmp == NULL) {
9311d0c4 631 ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
157997f0 632 return -1;
0f113f3e 633 }
9fdcc21f 634 (void)EVP_PKEY_assign_RSA(pktmp, (RSA *)a);
0f113f3e 635 ret = i2d_PUBKEY(pktmp, pp);
9fdcc21f 636 pktmp->pkey.ptr = NULL;
0f113f3e
MC
637 EVP_PKEY_free(pktmp);
638 return ret;
639}
52664f50 640
06f67612
RL
641#ifndef OPENSSL_NO_DH
642DH *ossl_d2i_DH_PUBKEY(DH **a, const unsigned char **pp, long length)
643{
644 EVP_PKEY *pkey;
645 DH *key = NULL;
646 const unsigned char *q;
647
648 q = *pp;
b2f1b365 649 pkey = ossl_d2i_PUBKEY_legacy(NULL, &q, length);
06f67612
RL
650 if (pkey == NULL)
651 return NULL;
ed576acd 652 if (EVP_PKEY_get_id(pkey) == EVP_PKEY_DH)
06f67612
RL
653 key = EVP_PKEY_get1_DH(pkey);
654 EVP_PKEY_free(pkey);
655 if (key == NULL)
656 return NULL;
657 *pp = q;
658 if (a != NULL) {
659 DH_free(*a);
660 *a = key;
661 }
662 return key;
663}
664
665int ossl_i2d_DH_PUBKEY(const DH *a, unsigned char **pp)
666{
667 EVP_PKEY *pktmp;
668 int ret;
669 if (!a)
670 return 0;
671 pktmp = EVP_PKEY_new();
672 if (pktmp == NULL) {
673 ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
674 return -1;
675 }
676 (void)EVP_PKEY_assign_DH(pktmp, (DH *)a);
677 ret = i2d_PUBKEY(pktmp, pp);
678 pktmp->pkey.ptr = NULL;
679 EVP_PKEY_free(pktmp);
680 return ret;
681}
682
683DH *ossl_d2i_DHx_PUBKEY(DH **a, const unsigned char **pp, long length)
684{
685 EVP_PKEY *pkey;
686 DH *key = NULL;
687 const unsigned char *q;
688
689 q = *pp;
b2f1b365 690 pkey = ossl_d2i_PUBKEY_legacy(NULL, &q, length);
06f67612
RL
691 if (pkey == NULL)
692 return NULL;
ed576acd 693 if (EVP_PKEY_get_id(pkey) == EVP_PKEY_DHX)
06f67612
RL
694 key = EVP_PKEY_get1_DH(pkey);
695 EVP_PKEY_free(pkey);
696 if (key == NULL)
697 return NULL;
698 *pp = q;
699 if (a != NULL) {
700 DH_free(*a);
701 *a = key;
702 }
703 return key;
704}
705
706int ossl_i2d_DHx_PUBKEY(const DH *a, unsigned char **pp)
707{
708 EVP_PKEY *pktmp;
709 int ret;
710 if (!a)
711 return 0;
712 pktmp = EVP_PKEY_new();
713 if (pktmp == NULL) {
714 ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
715 return -1;
716 }
717 (void)EVP_PKEY_assign(pktmp, EVP_PKEY_DHX, (DH *)a);
718 ret = i2d_PUBKEY(pktmp, pp);
719 pktmp->pkey.ptr = NULL;
720 EVP_PKEY_free(pktmp);
721 return ret;
722}
723#endif
724
cf1b7d96 725#ifndef OPENSSL_NO_DSA
0f113f3e
MC
726DSA *d2i_DSA_PUBKEY(DSA **a, const unsigned char **pp, long length)
727{
728 EVP_PKEY *pkey;
06f67612 729 DSA *key = NULL;
0f113f3e 730 const unsigned char *q;
12a765a5 731
0f113f3e 732 q = *pp;
b2f1b365 733 pkey = ossl_d2i_PUBKEY_legacy(NULL, &q, length);
12a765a5 734 if (pkey == NULL)
0f113f3e
MC
735 return NULL;
736 key = EVP_PKEY_get1_DSA(pkey);
737 EVP_PKEY_free(pkey);
12a765a5 738 if (key == NULL)
0f113f3e
MC
739 return NULL;
740 *pp = q;
12a765a5 741 if (a != NULL) {
0f113f3e
MC
742 DSA_free(*a);
743 *a = key;
744 }
745 return key;
746}
52664f50 747
9fdcc21f 748int i2d_DSA_PUBKEY(const DSA *a, unsigned char **pp)
0f113f3e
MC
749{
750 EVP_PKEY *pktmp;
751 int ret;
752 if (!a)
753 return 0;
754 pktmp = EVP_PKEY_new();
90945fa3 755 if (pktmp == NULL) {
9311d0c4 756 ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
157997f0 757 return -1;
0f113f3e 758 }
9fdcc21f 759 (void)EVP_PKEY_assign_DSA(pktmp, (DSA *)a);
0f113f3e 760 ret = i2d_PUBKEY(pktmp, pp);
9fdcc21f 761 pktmp->pkey.ptr = NULL;
0f113f3e
MC
762 EVP_PKEY_free(pktmp);
763 return ret;
764}
4d94ae00
BM
765#endif
766
14a7cfb3 767#ifndef OPENSSL_NO_EC
6343829a 768EC_KEY *d2i_EC_PUBKEY(EC_KEY **a, const unsigned char **pp, long length)
0f113f3e
MC
769{
770 EVP_PKEY *pkey;
06f67612 771 EC_KEY *key = NULL;
0f113f3e 772 const unsigned char *q;
b4c4a2c6 773 int type;
12a765a5 774
0f113f3e 775 q = *pp;
b2f1b365 776 pkey = ossl_d2i_PUBKEY_legacy(NULL, &q, length);
12a765a5 777 if (pkey == NULL)
26a7d938 778 return NULL;
ed576acd 779 type = EVP_PKEY_get_id(pkey);
b4c4a2c6 780 if (type == EVP_PKEY_EC || type == EVP_PKEY_SM2)
06f67612 781 key = EVP_PKEY_get1_EC_KEY(pkey);
0f113f3e 782 EVP_PKEY_free(pkey);
12a765a5 783 if (key == NULL)
26a7d938 784 return NULL;
0f113f3e 785 *pp = q;
12a765a5 786 if (a != NULL) {
0f113f3e
MC
787 EC_KEY_free(*a);
788 *a = key;
789 }
26a7d938 790 return key;
0f113f3e 791}
4d94ae00 792
9fdcc21f 793int i2d_EC_PUBKEY(const EC_KEY *a, unsigned char **pp)
0f113f3e
MC
794{
795 EVP_PKEY *pktmp;
796 int ret;
12a765a5
RS
797
798 if (a == NULL)
26a7d938 799 return 0;
0f113f3e 800 if ((pktmp = EVP_PKEY_new()) == NULL) {
9311d0c4 801 ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
157997f0 802 return -1;
0f113f3e 803 }
9fdcc21f 804 (void)EVP_PKEY_assign_EC_KEY(pktmp, (EC_KEY *)a);
0f113f3e 805 ret = i2d_PUBKEY(pktmp, pp);
9fdcc21f 806 pktmp->pkey.ptr = NULL;
0f113f3e 807 EVP_PKEY_free(pktmp);
26a7d938 808 return ret;
0f113f3e 809}
06f67612
RL
810
811ECX_KEY *ossl_d2i_ED25519_PUBKEY(ECX_KEY **a,
812 const unsigned char **pp, long length)
813{
814 EVP_PKEY *pkey;
815 ECX_KEY *key = NULL;
816 const unsigned char *q;
817
818 q = *pp;
b2f1b365 819 pkey = ossl_d2i_PUBKEY_legacy(NULL, &q, length);
06f67612
RL
820 if (pkey == NULL)
821 return NULL;
822 key = ossl_evp_pkey_get1_ED25519(pkey);
823 EVP_PKEY_free(pkey);
824 if (key == NULL)
825 return NULL;
826 *pp = q;
827 if (a != NULL) {
828 ossl_ecx_key_free(*a);
829 *a = key;
830 }
831 return key;
832}
833
834int ossl_i2d_ED25519_PUBKEY(const ECX_KEY *a, unsigned char **pp)
835{
836 EVP_PKEY *pktmp;
837 int ret;
838
839 if (a == NULL)
840 return 0;
841 if ((pktmp = EVP_PKEY_new()) == NULL) {
842 ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
843 return -1;
844 }
845 (void)EVP_PKEY_assign(pktmp, EVP_PKEY_ED25519, (ECX_KEY *)a);
846 ret = i2d_PUBKEY(pktmp, pp);
847 pktmp->pkey.ptr = NULL;
848 EVP_PKEY_free(pktmp);
849 return ret;
850}
851
852ECX_KEY *ossl_d2i_ED448_PUBKEY(ECX_KEY **a,
853 const unsigned char **pp, long length)
854{
855 EVP_PKEY *pkey;
856 ECX_KEY *key = NULL;
857 const unsigned char *q;
858
859 q = *pp;
b2f1b365 860 pkey = ossl_d2i_PUBKEY_legacy(NULL, &q, length);
06f67612
RL
861 if (pkey == NULL)
862 return NULL;
ed576acd 863 if (EVP_PKEY_get_id(pkey) == EVP_PKEY_ED448)
06f67612
RL
864 key = ossl_evp_pkey_get1_ED448(pkey);
865 EVP_PKEY_free(pkey);
866 if (key == NULL)
867 return NULL;
868 *pp = q;
869 if (a != NULL) {
870 ossl_ecx_key_free(*a);
871 *a = key;
872 }
873 return key;
874}
875
876int ossl_i2d_ED448_PUBKEY(const ECX_KEY *a, unsigned char **pp)
877{
878 EVP_PKEY *pktmp;
879 int ret;
880
881 if (a == NULL)
882 return 0;
883 if ((pktmp = EVP_PKEY_new()) == NULL) {
884 ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
885 return -1;
886 }
887 (void)EVP_PKEY_assign(pktmp, EVP_PKEY_ED448, (ECX_KEY *)a);
888 ret = i2d_PUBKEY(pktmp, pp);
889 pktmp->pkey.ptr = NULL;
890 EVP_PKEY_free(pktmp);
891 return ret;
892}
893
894ECX_KEY *ossl_d2i_X25519_PUBKEY(ECX_KEY **a,
895 const unsigned char **pp, long length)
896{
897 EVP_PKEY *pkey;
898 ECX_KEY *key = NULL;
899 const unsigned char *q;
900
901 q = *pp;
b2f1b365 902 pkey = ossl_d2i_PUBKEY_legacy(NULL, &q, length);
06f67612
RL
903 if (pkey == NULL)
904 return NULL;
ed576acd 905 if (EVP_PKEY_get_id(pkey) == EVP_PKEY_X25519)
06f67612
RL
906 key = ossl_evp_pkey_get1_X25519(pkey);
907 EVP_PKEY_free(pkey);
908 if (key == NULL)
909 return NULL;
910 *pp = q;
911 if (a != NULL) {
912 ossl_ecx_key_free(*a);
913 *a = key;
914 }
915 return key;
916}
917
918int ossl_i2d_X25519_PUBKEY(const ECX_KEY *a, unsigned char **pp)
919{
920 EVP_PKEY *pktmp;
921 int ret;
922
923 if (a == NULL)
924 return 0;
925 if ((pktmp = EVP_PKEY_new()) == NULL) {
926 ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
927 return -1;
928 }
929 (void)EVP_PKEY_assign(pktmp, EVP_PKEY_X25519, (ECX_KEY *)a);
930 ret = i2d_PUBKEY(pktmp, pp);
931 pktmp->pkey.ptr = NULL;
932 EVP_PKEY_free(pktmp);
933 return ret;
934}
935
936ECX_KEY *ossl_d2i_X448_PUBKEY(ECX_KEY **a,
937 const unsigned char **pp, long length)
938{
939 EVP_PKEY *pkey;
940 ECX_KEY *key = NULL;
941 const unsigned char *q;
942
943 q = *pp;
b2f1b365 944 pkey = ossl_d2i_PUBKEY_legacy(NULL, &q, length);
06f67612
RL
945 if (pkey == NULL)
946 return NULL;
ed576acd 947 if (EVP_PKEY_get_id(pkey) == EVP_PKEY_X448)
06f67612
RL
948 key = ossl_evp_pkey_get1_X448(pkey);
949 EVP_PKEY_free(pkey);
950 if (key == NULL)
951 return NULL;
952 *pp = q;
953 if (a != NULL) {
954 ossl_ecx_key_free(*a);
955 *a = key;
956 }
957 return key;
958}
959
960int ossl_i2d_X448_PUBKEY(const ECX_KEY *a, unsigned char **pp)
961{
962 EVP_PKEY *pktmp;
963 int ret;
964
965 if (a == NULL)
966 return 0;
967 if ((pktmp = EVP_PKEY_new()) == NULL) {
968 ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
969 return -1;
970 }
971 (void)EVP_PKEY_assign(pktmp, EVP_PKEY_X448, (ECX_KEY *)a);
972 ret = i2d_PUBKEY(pktmp, pp);
973 pktmp->pkey.ptr = NULL;
974 EVP_PKEY_free(pktmp);
975 return ret;
976}
977
12aefe78 978#endif
448be743
DSH
979
980int X509_PUBKEY_set0_param(X509_PUBKEY *pub, ASN1_OBJECT *aobj,
0f113f3e
MC
981 int ptype, void *pval,
982 unsigned char *penc, int penclen)
983{
984 if (!X509_ALGOR_set0(pub->algor, aobj, ptype, pval))
985 return 0;
986 if (penc) {
b548a1f1 987 OPENSSL_free(pub->public_key->data);
0f113f3e
MC
988 pub->public_key->data = penc;
989 pub->public_key->length = penclen;
990 /* Set number of unused bits to zero */
991 pub->public_key->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
992 pub->public_key->flags |= ASN1_STRING_FLAG_BITS_LEFT;
993 }
994 return 1;
995}
448be743
DSH
996
997int X509_PUBKEY_get0_param(ASN1_OBJECT **ppkalg,
0f113f3e 998 const unsigned char **pk, int *ppklen,
7674e923 999 X509_ALGOR **pa, const X509_PUBKEY *pub)
0f113f3e
MC
1000{
1001 if (ppkalg)
1002 *ppkalg = pub->algor->algorithm;
1003 if (pk) {
1004 *pk = pub->public_key->data;
1005 *ppklen = pub->public_key->length;
1006 }
1007 if (pa)
1008 *pa = pub->algor;
1009 return 1;
1010}
29fa0a1a
DSH
1011
1012ASN1_BIT_STRING *X509_get0_pubkey_bitstr(const X509 *x)
1013{
1014 if (x == NULL)
1015 return NULL;
1016 return x->cert_info.key->public_key;
1017}
93f99b68
DDO
1018
1019/* Returns 1 for equal, 0, for non-equal, < 0 on error */
1020int X509_PUBKEY_eq(const X509_PUBKEY *a, const X509_PUBKEY *b)
1021{
1022 X509_ALGOR *algA, *algB;
1023 EVP_PKEY *pA, *pB;
1024
1025 if (a == b)
1026 return 1;
1027 if (a == NULL || b == NULL)
1028 return 0;
1029 if (!X509_PUBKEY_get0_param(NULL, NULL, NULL, &algA, a) || algA == NULL
1030 || !X509_PUBKEY_get0_param(NULL, NULL, NULL, &algB, b) || algB == NULL)
1031 return -2;
1032 if (X509_ALGOR_cmp(algA, algB) != 0)
1033 return 0;
1034 if ((pA = X509_PUBKEY_get0(a)) == NULL
1035 || (pB = X509_PUBKEY_get0(b)) == NULL)
1036 return -2;
c74aaa39 1037 return EVP_PKEY_eq(pA, pB);
93f99b68 1038}
22b81444 1039
4669015d
SL
1040int ossl_x509_PUBKEY_get0_libctx(OSSL_LIB_CTX **plibctx, const char **ppropq,
1041 const X509_PUBKEY *key)
22b81444
RL
1042{
1043 if (plibctx)
1044 *plibctx = key->libctx;
1045 if (ppropq)
1046 *ppropq = key->propq;
1047 return 1;
1048}