2 * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
11 * DSA low level APIs are deprecated for public use, but still ok for
14 #include "internal/deprecated.h"
17 #include "internal/cryptlib.h"
18 #include <openssl/asn1t.h>
19 #include <openssl/x509.h>
20 #include <openssl/engine.h>
21 #include "crypto/asn1.h"
22 #include "crypto/evp.h"
23 #include "crypto/x509.h"
24 #include <openssl/rsa.h>
25 #include <openssl/dsa.h>
26 #include <openssl/decoder.h>
27 #include <openssl/encoder.h>
28 #include "internal/provider.h"
29 #include "internal/sizes.h"
31 struct X509_pubkey_st {
33 ASN1_BIT_STRING *public_key;
37 /* extra data for the callback, used by d2i_PUBKEY_ex */
41 /* Flag to force legacy keys */
42 unsigned int flag_force_legacy : 1;
45 static int x509_pubkey_decode(EVP_PKEY **pk, const X509_PUBKEY *key);
47 static int x509_pubkey_set0_libctx(X509_PUBKEY *x, OSSL_LIB_CTX *libctx,
52 OPENSSL_free(x->propq);
55 x->propq = OPENSSL_strdup(propq);
63 ASN1_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)
68 static void x509_pubkey_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
70 X509_PUBKEY *pubkey = (X509_PUBKEY *)*pval;
72 X509_ALGOR_free(pubkey->algor);
73 ASN1_BIT_STRING_free(pubkey->public_key);
74 EVP_PKEY_free(pubkey->pkey);
75 OPENSSL_free(pubkey->propq);
80 static int x509_pubkey_ex_populate(ASN1_VALUE **pval, const ASN1_ITEM *it)
82 X509_PUBKEY *pubkey = (X509_PUBKEY *)*pval;
84 return (pubkey->algor != NULL
85 || (pubkey->algor = X509_ALGOR_new()) != NULL)
86 && (pubkey->public_key != NULL
87 || (pubkey->public_key = ASN1_BIT_STRING_new()) != NULL);
91 static int x509_pubkey_ex_new_ex(ASN1_VALUE **pval, const ASN1_ITEM *it,
92 OSSL_LIB_CTX *libctx, const char *propq)
96 if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL
97 || !x509_pubkey_ex_populate((ASN1_VALUE **)&ret, NULL)
98 || !x509_pubkey_set0_libctx(ret, libctx, propq)) {
99 x509_pubkey_ex_free((ASN1_VALUE **)&ret, NULL);
100 ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
102 *pval = (ASN1_VALUE *)ret;
108 static int x509_pubkey_ex_d2i_ex(ASN1_VALUE **pval,
109 const unsigned char **in, long len,
110 const ASN1_ITEM *it, int tag, int aclass,
111 char opt, ASN1_TLC *ctx, OSSL_LIB_CTX *libctx,
114 const unsigned char *in_saved = *in;
118 OSSL_DECODER_CTX *dctx = NULL;
119 unsigned char *tmpbuf = NULL;
121 if (*pval == NULL && !x509_pubkey_ex_new_ex(pval, it, libctx, propq))
123 if (!x509_pubkey_ex_populate(pval, NULL)) {
124 ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
128 /* This ensures that |*in| advances properly no matter what */
129 if ((ret = ASN1_item_ex_d2i(pval, in, len,
130 ASN1_ITEM_rptr(X509_PUBKEY_INTERNAL),
131 tag, aclass, opt, ctx)) <= 0)
134 publen = *in - in_saved;
135 if (!ossl_assert(publen > 0)) {
136 ERR_raise(ERR_LIB_ASN1, ERR_R_INTERNAL_ERROR);
140 pubkey = (X509_PUBKEY *)*pval;
141 EVP_PKEY_free(pubkey->pkey);
145 * Opportunistically decode the key but remove any non fatal errors
146 * from the queue. Subsequent explicit attempts to decode/use the key
147 * will return an appropriate error.
152 * Try to decode with legacy method first. This ensures that engines
153 * aren't overriden by providers.
155 if ((ret = x509_pubkey_decode(&pubkey->pkey, pubkey)) == -1) {
156 /* -1 indicates a fatal error, like malloc failure */
157 ERR_clear_last_mark();
161 /* Try to decode it into an EVP_PKEY with OSSL_DECODER */
162 if (ret <= 0 && !pubkey->flag_force_legacy) {
163 const unsigned char *p;
164 char txtoidname[OSSL_MAX_NAME_SIZE];
165 size_t slen = publen;
168 * The decoders don't know how to handle anything other than Universal
169 * class so we modify the data accordingly.
171 if (aclass != V_ASN1_UNIVERSAL) {
172 tmpbuf = OPENSSL_memdup(in_saved, publen);
173 if (tmpbuf == NULL) {
174 ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
178 *tmpbuf = V_ASN1_CONSTRUCTED | V_ASN1_SEQUENCE;
182 if (OBJ_obj2txt(txtoidname, sizeof(txtoidname),
183 pubkey->algor->algorithm, 0) <= 0) {
184 ERR_clear_last_mark();
188 OSSL_DECODER_CTX_new_for_pkey(&pubkey->pkey,
189 "DER", "SubjectPublicKeyInfo",
190 txtoidname, EVP_PKEY_PUBLIC_KEY,
192 pubkey->propq)) != NULL)
194 * As said higher up, we're being opportunistic. In other words,
195 * we don't care if we fail.
197 if (OSSL_DECODER_from_data(dctx, &p, &slen)) {
200 * If we successfully decoded then we *must* consume all the
203 ERR_clear_last_mark();
204 ERR_raise(ERR_LIB_ASN1, EVP_R_DECODE_ERROR);
213 OSSL_DECODER_CTX_free(dctx);
214 OPENSSL_free(tmpbuf);
218 static int x509_pubkey_ex_i2d(const ASN1_VALUE **pval, unsigned char **out,
219 const ASN1_ITEM *it, int tag, int aclass)
221 return ASN1_item_ex_i2d(pval, out, ASN1_ITEM_rptr(X509_PUBKEY_INTERNAL),
225 static int x509_pubkey_ex_print(BIO *out, const ASN1_VALUE **pval, int indent,
226 const char *fname, const ASN1_PCTX *pctx)
228 return ASN1_item_print(out, *pval, indent,
229 ASN1_ITEM_rptr(X509_PUBKEY_INTERNAL), pctx);
232 static const ASN1_EXTERN_FUNCS x509_pubkey_ff = {
236 0, /* Default clear behaviour is OK */
239 x509_pubkey_ex_print,
240 x509_pubkey_ex_new_ex,
241 x509_pubkey_ex_d2i_ex,
244 IMPLEMENT_EXTERN_ASN1(X509_PUBKEY, V_ASN1_SEQUENCE, x509_pubkey_ff)
245 IMPLEMENT_ASN1_FUNCTIONS(X509_PUBKEY)
247 X509_PUBKEY *X509_PUBKEY_new_ex(OSSL_LIB_CTX *libctx, const char *propq)
249 X509_PUBKEY *pubkey = NULL;
251 pubkey = (X509_PUBKEY *)ASN1_item_new_ex(X509_PUBKEY_it(), libctx, propq);
252 if (!x509_pubkey_set0_libctx(pubkey, libctx, propq)) {
253 X509_PUBKEY_free(pubkey);
260 * X509_PUBKEY_dup() must be implemented manually, because there is no
261 * support for it in ASN1_EXTERN_FUNCS.
263 X509_PUBKEY *X509_PUBKEY_dup(const X509_PUBKEY *a)
265 X509_PUBKEY *pubkey = OPENSSL_zalloc(sizeof(*pubkey));
268 || !x509_pubkey_set0_libctx(pubkey, a->libctx, a->propq)
269 || (pubkey->algor = X509_ALGOR_dup(a->algor)) == NULL
270 || (pubkey->public_key = ASN1_BIT_STRING_new()) == NULL
271 || !ASN1_BIT_STRING_set(pubkey->public_key,
272 a->public_key->data, a->public_key->length)
273 || (a->pkey != NULL && !EVP_PKEY_up_ref(a->pkey))) {
274 x509_pubkey_ex_free((ASN1_VALUE **)&pubkey,
275 ASN1_ITEM_rptr(X509_PUBKEY_INTERNAL));
276 ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
279 pubkey->pkey = a->pkey;
283 int X509_PUBKEY_set(X509_PUBKEY **x, EVP_PKEY *pkey)
285 X509_PUBKEY *pk = NULL;
287 if (x == NULL || pkey == NULL) {
288 ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER);
292 if (pkey->ameth != NULL) {
293 if ((pk = X509_PUBKEY_new()) == NULL) {
294 ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
297 if (pkey->ameth->pub_encode != NULL) {
298 if (!pkey->ameth->pub_encode(pk, pkey)) {
299 ERR_raise(ERR_LIB_X509, X509_R_PUBLIC_KEY_ENCODE_ERROR);
303 ERR_raise(ERR_LIB_X509, X509_R_METHOD_NOT_SUPPORTED);
306 } else if (evp_pkey_is_provided(pkey)) {
307 unsigned char *der = NULL;
309 OSSL_ENCODER_CTX *ectx =
310 OSSL_ENCODER_CTX_new_for_pkey(pkey, EVP_PKEY_PUBLIC_KEY,
311 "DER", "SubjectPublicKeyInfo",
314 if (OSSL_ENCODER_to_data(ectx, &der, &derlen)) {
315 const unsigned char *pder = der;
317 pk = d2i_X509_PUBKEY(NULL, &pder, (long)derlen);
320 OSSL_ENCODER_CTX_free(ectx);
325 ERR_raise(ERR_LIB_X509, X509_R_UNSUPPORTED_ALGORITHM);
329 X509_PUBKEY_free(*x);
330 if (!EVP_PKEY_up_ref(pkey)) {
331 ERR_raise(ERR_LIB_X509, ERR_R_INTERNAL_ERROR);
337 * pk->pkey is NULL when using the legacy routine, but is non-NULL when
338 * going through the encoder, and for all intents and purposes, it's
339 * a perfect copy of the public key portions of |pkey|, just not the same
340 * instance. If that's all there was to pkey then we could simply return
341 * early, right here. However, some application might very well depend on
342 * the passed |pkey| being used and none other, so we spend a few more
343 * cycles throwing away the newly created |pk->pkey| and replace it with
346 if (pk->pkey != NULL)
347 EVP_PKEY_free(pk->pkey);
353 X509_PUBKEY_free(pk);
358 * Attempt to decode a public key.
359 * Returns 1 on success, 0 for a decode failure and -1 for a fatal
360 * error e.g. malloc failure.
362 * This function is #legacy.
364 static int x509_pubkey_decode(EVP_PKEY **ppkey, const X509_PUBKEY *key)
369 nid = OBJ_obj2nid(key->algor->algorithm);
370 if (!key->flag_force_legacy) {
371 #ifndef OPENSSL_NO_ENGINE
374 e = ENGINE_get_pkey_meth_engine(nid);
383 pkey = EVP_PKEY_new();
385 ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
389 if (!EVP_PKEY_set_type(pkey, nid)) {
390 ERR_raise(ERR_LIB_X509, X509_R_UNSUPPORTED_ALGORITHM);
394 if (pkey->ameth->pub_decode) {
396 * Treat any failure of pub_decode as a decode error. In
397 * future we could have different return codes for decode
398 * errors and fatal errors such as malloc failure.
400 if (!pkey->ameth->pub_decode(pkey, key))
403 ERR_raise(ERR_LIB_X509, X509_R_METHOD_NOT_SUPPORTED);
415 EVP_PKEY *X509_PUBKEY_get0(const X509_PUBKEY *key)
417 EVP_PKEY *ret = NULL;
419 if (key == NULL || key->public_key == NULL)
422 if (key->pkey != NULL)
426 * When the key ASN.1 is initially parsed an attempt is made to
427 * decode the public key and cache the EVP_PKEY structure. If this
428 * operation fails the cached value will be NULL. Parsing continues
429 * to allow parsing of unknown key types or unsupported forms.
430 * We repeat the decode operation so the appropriate errors are left
433 x509_pubkey_decode(&ret, key);
434 /* If decode doesn't fail something bad happened */
436 ERR_raise(ERR_LIB_X509, ERR_R_INTERNAL_ERROR);
443 EVP_PKEY *X509_PUBKEY_get(const X509_PUBKEY *key)
445 EVP_PKEY *ret = X509_PUBKEY_get0(key);
447 if (ret != NULL && !EVP_PKEY_up_ref(ret)) {
448 ERR_raise(ERR_LIB_X509, ERR_R_INTERNAL_ERROR);
455 * Now three pseudo ASN1 routines that take an EVP_PKEY structure and encode
456 * or decode as X509_PUBKEY
458 static EVP_PKEY *d2i_PUBKEY_int(EVP_PKEY **a,
459 const unsigned char **pp, long length,
460 OSSL_LIB_CTX *libctx, const char *propq,
461 unsigned int force_legacy,
463 (*d2i_x509_pubkey)(X509_PUBKEY **a,
464 const unsigned char **in,
467 X509_PUBKEY *xpk, *xpk2 = NULL, **pxpk = NULL;
468 EVP_PKEY *pktmp = NULL;
469 const unsigned char *q;
474 * If libctx or propq are non-NULL, we take advantage of the reuse
475 * feature. It's not generally recommended, but is safe enough for
476 * newly created structures.
478 if (libctx != NULL || propq != NULL || force_legacy) {
479 xpk2 = OPENSSL_zalloc(sizeof(*xpk2));
481 ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
484 if (!x509_pubkey_set0_libctx(xpk2, libctx, propq))
486 xpk2->flag_force_legacy = !!force_legacy;
489 xpk = d2i_x509_pubkey(pxpk, &q, length);
492 pktmp = X509_PUBKEY_get(xpk);
493 X509_PUBKEY_free(xpk);
494 xpk2 = NULL; /* We know that xpk == xpk2 */
503 X509_PUBKEY_free(xpk2);
507 /* For the algorithm specific d2i functions further down */
508 EVP_PKEY *ossl_d2i_PUBKEY_legacy(EVP_PKEY **a, const unsigned char **pp,
511 return d2i_PUBKEY_int(a, pp, length, NULL, NULL, 1, d2i_X509_PUBKEY);
514 EVP_PKEY *d2i_PUBKEY_ex(EVP_PKEY **a, const unsigned char **pp, long length,
515 OSSL_LIB_CTX *libctx, const char *propq)
517 return d2i_PUBKEY_int(a, pp, length, libctx, propq, 0, d2i_X509_PUBKEY);
520 EVP_PKEY *d2i_PUBKEY(EVP_PKEY **a, const unsigned char **pp, long length)
522 return d2i_PUBKEY_ex(a, pp, length, NULL, NULL);
525 int i2d_PUBKEY(const EVP_PKEY *a, unsigned char **pp)
531 if (a->ameth != NULL) {
532 X509_PUBKEY *xpk = NULL;
534 if ((xpk = X509_PUBKEY_new()) == NULL)
537 /* pub_encode() only encode parameters, not the key itself */
538 if (a->ameth->pub_encode != NULL && a->ameth->pub_encode(xpk, a)) {
539 xpk->pkey = (EVP_PKEY *)a;
540 ret = i2d_X509_PUBKEY(xpk, pp);
543 X509_PUBKEY_free(xpk);
544 } else if (a->keymgmt != NULL) {
545 OSSL_ENCODER_CTX *ctx =
546 OSSL_ENCODER_CTX_new_for_pkey(a, EVP_PKEY_PUBLIC_KEY,
547 "DER", "SubjectPublicKeyInfo",
549 BIO *out = BIO_new(BIO_s_mem());
552 if (OSSL_ENCODER_CTX_get_num_encoders(ctx) != 0
554 && OSSL_ENCODER_to_bio(ctx, out)
555 && BIO_get_mem_ptr(out, &buf) > 0) {
560 *pp = (unsigned char *)buf->data;
564 memcpy(*pp, buf->data, ret);
570 OSSL_ENCODER_CTX_free(ctx);
577 * The following are equivalents but which return RSA and DSA keys
579 RSA *d2i_RSA_PUBKEY(RSA **a, const unsigned char **pp, long length)
583 const unsigned char *q;
586 pkey = ossl_d2i_PUBKEY_legacy(NULL, &q, length);
589 key = EVP_PKEY_get1_RSA(pkey);
601 int i2d_RSA_PUBKEY(const RSA *a, unsigned char **pp)
607 pktmp = EVP_PKEY_new();
609 ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
612 (void)EVP_PKEY_assign_RSA(pktmp, (RSA *)a);
613 ret = i2d_PUBKEY(pktmp, pp);
614 pktmp->pkey.ptr = NULL;
615 EVP_PKEY_free(pktmp);
619 #ifndef OPENSSL_NO_DH
620 DH *ossl_d2i_DH_PUBKEY(DH **a, const unsigned char **pp, long length)
624 const unsigned char *q;
627 pkey = ossl_d2i_PUBKEY_legacy(NULL, &q, length);
630 if (EVP_PKEY_get_id(pkey) == EVP_PKEY_DH)
631 key = EVP_PKEY_get1_DH(pkey);
643 int ossl_i2d_DH_PUBKEY(const DH *a, unsigned char **pp)
649 pktmp = EVP_PKEY_new();
651 ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
654 (void)EVP_PKEY_assign_DH(pktmp, (DH *)a);
655 ret = i2d_PUBKEY(pktmp, pp);
656 pktmp->pkey.ptr = NULL;
657 EVP_PKEY_free(pktmp);
661 DH *ossl_d2i_DHx_PUBKEY(DH **a, const unsigned char **pp, long length)
665 const unsigned char *q;
668 pkey = ossl_d2i_PUBKEY_legacy(NULL, &q, length);
671 if (EVP_PKEY_get_id(pkey) == EVP_PKEY_DHX)
672 key = EVP_PKEY_get1_DH(pkey);
684 int ossl_i2d_DHx_PUBKEY(const DH *a, unsigned char **pp)
690 pktmp = EVP_PKEY_new();
692 ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
695 (void)EVP_PKEY_assign(pktmp, EVP_PKEY_DHX, (DH *)a);
696 ret = i2d_PUBKEY(pktmp, pp);
697 pktmp->pkey.ptr = NULL;
698 EVP_PKEY_free(pktmp);
703 #ifndef OPENSSL_NO_DSA
704 DSA *d2i_DSA_PUBKEY(DSA **a, const unsigned char **pp, long length)
708 const unsigned char *q;
711 pkey = ossl_d2i_PUBKEY_legacy(NULL, &q, length);
714 key = EVP_PKEY_get1_DSA(pkey);
726 int i2d_DSA_PUBKEY(const DSA *a, unsigned char **pp)
732 pktmp = EVP_PKEY_new();
734 ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
737 (void)EVP_PKEY_assign_DSA(pktmp, (DSA *)a);
738 ret = i2d_PUBKEY(pktmp, pp);
739 pktmp->pkey.ptr = NULL;
740 EVP_PKEY_free(pktmp);
745 #ifndef OPENSSL_NO_EC
746 EC_KEY *d2i_EC_PUBKEY(EC_KEY **a, const unsigned char **pp, long length)
750 const unsigned char *q;
754 pkey = ossl_d2i_PUBKEY_legacy(NULL, &q, length);
757 type = EVP_PKEY_get_id(pkey);
758 if (type == EVP_PKEY_EC || type == EVP_PKEY_SM2)
759 key = EVP_PKEY_get1_EC_KEY(pkey);
771 int i2d_EC_PUBKEY(const EC_KEY *a, unsigned char **pp)
778 if ((pktmp = EVP_PKEY_new()) == NULL) {
779 ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
782 (void)EVP_PKEY_assign_EC_KEY(pktmp, (EC_KEY *)a);
783 ret = i2d_PUBKEY(pktmp, pp);
784 pktmp->pkey.ptr = NULL;
785 EVP_PKEY_free(pktmp);
789 ECX_KEY *ossl_d2i_ED25519_PUBKEY(ECX_KEY **a,
790 const unsigned char **pp, long length)
794 const unsigned char *q;
797 pkey = ossl_d2i_PUBKEY_legacy(NULL, &q, length);
800 key = ossl_evp_pkey_get1_ED25519(pkey);
806 ossl_ecx_key_free(*a);
812 int ossl_i2d_ED25519_PUBKEY(const ECX_KEY *a, unsigned char **pp)
819 if ((pktmp = EVP_PKEY_new()) == NULL) {
820 ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
823 (void)EVP_PKEY_assign(pktmp, EVP_PKEY_ED25519, (ECX_KEY *)a);
824 ret = i2d_PUBKEY(pktmp, pp);
825 pktmp->pkey.ptr = NULL;
826 EVP_PKEY_free(pktmp);
830 ECX_KEY *ossl_d2i_ED448_PUBKEY(ECX_KEY **a,
831 const unsigned char **pp, long length)
835 const unsigned char *q;
838 pkey = ossl_d2i_PUBKEY_legacy(NULL, &q, length);
841 if (EVP_PKEY_get_id(pkey) == EVP_PKEY_ED448)
842 key = ossl_evp_pkey_get1_ED448(pkey);
848 ossl_ecx_key_free(*a);
854 int ossl_i2d_ED448_PUBKEY(const ECX_KEY *a, unsigned char **pp)
861 if ((pktmp = EVP_PKEY_new()) == NULL) {
862 ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
865 (void)EVP_PKEY_assign(pktmp, EVP_PKEY_ED448, (ECX_KEY *)a);
866 ret = i2d_PUBKEY(pktmp, pp);
867 pktmp->pkey.ptr = NULL;
868 EVP_PKEY_free(pktmp);
872 ECX_KEY *ossl_d2i_X25519_PUBKEY(ECX_KEY **a,
873 const unsigned char **pp, long length)
877 const unsigned char *q;
880 pkey = ossl_d2i_PUBKEY_legacy(NULL, &q, length);
883 if (EVP_PKEY_get_id(pkey) == EVP_PKEY_X25519)
884 key = ossl_evp_pkey_get1_X25519(pkey);
890 ossl_ecx_key_free(*a);
896 int ossl_i2d_X25519_PUBKEY(const ECX_KEY *a, unsigned char **pp)
903 if ((pktmp = EVP_PKEY_new()) == NULL) {
904 ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
907 (void)EVP_PKEY_assign(pktmp, EVP_PKEY_X25519, (ECX_KEY *)a);
908 ret = i2d_PUBKEY(pktmp, pp);
909 pktmp->pkey.ptr = NULL;
910 EVP_PKEY_free(pktmp);
914 ECX_KEY *ossl_d2i_X448_PUBKEY(ECX_KEY **a,
915 const unsigned char **pp, long length)
919 const unsigned char *q;
922 pkey = ossl_d2i_PUBKEY_legacy(NULL, &q, length);
925 if (EVP_PKEY_get_id(pkey) == EVP_PKEY_X448)
926 key = ossl_evp_pkey_get1_X448(pkey);
932 ossl_ecx_key_free(*a);
938 int ossl_i2d_X448_PUBKEY(const ECX_KEY *a, unsigned char **pp)
945 if ((pktmp = EVP_PKEY_new()) == NULL) {
946 ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
949 (void)EVP_PKEY_assign(pktmp, EVP_PKEY_X448, (ECX_KEY *)a);
950 ret = i2d_PUBKEY(pktmp, pp);
951 pktmp->pkey.ptr = NULL;
952 EVP_PKEY_free(pktmp);
958 int X509_PUBKEY_set0_param(X509_PUBKEY *pub, ASN1_OBJECT *aobj,
959 int ptype, void *pval,
960 unsigned char *penc, int penclen)
962 if (!X509_ALGOR_set0(pub->algor, aobj, ptype, pval))
965 OPENSSL_free(pub->public_key->data);
966 pub->public_key->data = penc;
967 pub->public_key->length = penclen;
968 /* Set number of unused bits to zero */
969 pub->public_key->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
970 pub->public_key->flags |= ASN1_STRING_FLAG_BITS_LEFT;
975 int X509_PUBKEY_get0_param(ASN1_OBJECT **ppkalg,
976 const unsigned char **pk, int *ppklen,
977 X509_ALGOR **pa, const X509_PUBKEY *pub)
980 *ppkalg = pub->algor->algorithm;
982 *pk = pub->public_key->data;
983 *ppklen = pub->public_key->length;
990 ASN1_BIT_STRING *X509_get0_pubkey_bitstr(const X509 *x)
994 return x->cert_info.key->public_key;
997 /* Returns 1 for equal, 0, for non-equal, < 0 on error */
998 int X509_PUBKEY_eq(const X509_PUBKEY *a, const X509_PUBKEY *b)
1000 X509_ALGOR *algA, *algB;
1005 if (a == NULL || b == NULL)
1007 if (!X509_PUBKEY_get0_param(NULL, NULL, NULL, &algA, a) || algA == NULL
1008 || !X509_PUBKEY_get0_param(NULL, NULL, NULL, &algB, b) || algB == NULL)
1010 if (X509_ALGOR_cmp(algA, algB) != 0)
1012 if ((pA = X509_PUBKEY_get0(a)) == NULL
1013 || (pB = X509_PUBKEY_get0(b)) == NULL)
1015 return EVP_PKEY_eq(pA, pB);
1018 int ossl_x509_PUBKEY_get0_libctx(OSSL_LIB_CTX **plibctx, const char **ppropq,
1019 const X509_PUBKEY *key)
1022 *plibctx = key->libctx;
1024 *ppropq = key->propq;