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 "crypto/asn1.h"
21 #include "crypto/evp.h"
22 #include "crypto/x509.h"
23 #include <openssl/rsa.h>
24 #include <openssl/dsa.h>
25 #include <openssl/decoder.h>
26 #include <openssl/encoder.h>
27 #include "internal/provider.h"
28 #include "internal/sizes.h"
30 struct X509_pubkey_st {
32 ASN1_BIT_STRING *public_key;
36 /* extra data for the callback, used by d2i_PUBKEY_ex */
40 /* Flag to force legacy keys */
41 unsigned int flag_force_legacy : 1;
44 static int x509_pubkey_decode(EVP_PKEY **pk, const X509_PUBKEY *key);
46 static int x509_pubkey_set0_libctx(X509_PUBKEY *x, OSSL_LIB_CTX *libctx,
51 OPENSSL_free(x->propq);
54 x->propq = OPENSSL_strdup(propq);
62 ASN1_SEQUENCE(X509_PUBKEY_INTERNAL) = {
63 ASN1_SIMPLE(X509_PUBKEY, algor, X509_ALGOR),
64 ASN1_SIMPLE(X509_PUBKEY, public_key, ASN1_BIT_STRING)
65 } static_ASN1_SEQUENCE_END_name(X509_PUBKEY, X509_PUBKEY_INTERNAL)
67 static void x509_pubkey_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
69 X509_PUBKEY *pubkey = (X509_PUBKEY *)*pval;
71 X509_ALGOR_free(pubkey->algor);
72 ASN1_BIT_STRING_free(pubkey->public_key);
73 EVP_PKEY_free(pubkey->pkey);
74 OPENSSL_free(pubkey->propq);
79 static int x509_pubkey_ex_populate(ASN1_VALUE **pval, const ASN1_ITEM *it)
81 X509_PUBKEY *pubkey = (X509_PUBKEY *)*pval;
83 return (pubkey->algor != NULL
84 || (pubkey->algor = X509_ALGOR_new()) != NULL)
85 && (pubkey->public_key != NULL
86 || (pubkey->public_key = ASN1_BIT_STRING_new()) != NULL);
90 static int x509_pubkey_ex_new_ex(ASN1_VALUE **pval, const ASN1_ITEM *it,
91 OSSL_LIB_CTX *libctx, const char *propq)
95 if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL
96 || !x509_pubkey_ex_populate((ASN1_VALUE **)&ret, NULL)
97 || !x509_pubkey_set0_libctx(ret, libctx, propq)) {
98 x509_pubkey_ex_free((ASN1_VALUE **)&ret, NULL);
99 ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
101 *pval = (ASN1_VALUE *)ret;
107 static int x509_pubkey_ex_d2i_ex(ASN1_VALUE **pval,
108 const unsigned char **in, long len,
109 const ASN1_ITEM *it, int tag, int aclass,
110 char opt, ASN1_TLC *ctx, OSSL_LIB_CTX *libctx,
113 const unsigned char *in_saved = *in;
117 OSSL_DECODER_CTX *dctx = NULL;
118 unsigned char *tmpbuf = NULL;
120 if (*pval == NULL && !x509_pubkey_ex_new_ex(pval, it, libctx, propq))
122 if (!x509_pubkey_ex_populate(pval, NULL)) {
123 ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
127 /* This ensures that |*in| advances properly no matter what */
128 if ((ret = ASN1_item_ex_d2i(pval, in, len,
129 ASN1_ITEM_rptr(X509_PUBKEY_INTERNAL),
130 tag, aclass, opt, ctx)) <= 0)
133 publen = *in - in_saved;
134 if (!ossl_assert(publen > 0)) {
135 ERR_raise(ERR_LIB_ASN1, ERR_R_INTERNAL_ERROR);
139 pubkey = (X509_PUBKEY *)*pval;
140 EVP_PKEY_free(pubkey->pkey);
144 * Opportunistically decode the key but remove any non fatal errors
145 * from the queue. Subsequent explicit attempts to decode/use the key
146 * will return an appropriate error.
151 * Try to decode with legacy method first. This ensures that engines
152 * aren't overriden by providers.
154 if ((ret = x509_pubkey_decode(&pubkey->pkey, pubkey)) == -1) {
155 /* -1 indicates a fatal error, like malloc failure */
156 ERR_clear_last_mark();
160 /* Try to decode it into an EVP_PKEY with OSSL_DECODER */
161 if (ret <= 0 && !pubkey->flag_force_legacy) {
162 const unsigned char *p;
163 char txtoidname[OSSL_MAX_NAME_SIZE];
166 * The decoders don't know how to handle anything other than Universal
167 * class so we modify the data accordingly.
169 if (aclass != V_ASN1_UNIVERSAL) {
170 tmpbuf = OPENSSL_memdup(in_saved, publen);
171 if (tmpbuf == NULL) {
172 ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
176 *tmpbuf = V_ASN1_CONSTRUCTED | V_ASN1_SEQUENCE;
180 if (OBJ_obj2txt(txtoidname, sizeof(txtoidname),
181 pubkey->algor->algorithm, 0) <= 0) {
182 ERR_clear_last_mark();
186 OSSL_DECODER_CTX_new_for_pkey(&pubkey->pkey,
187 "DER", "SubjectPublicKeyInfo",
188 txtoidname, EVP_PKEY_PUBLIC_KEY,
190 pubkey->propq)) != NULL)
192 * As said higher up, we're being opportunistic. In other words,
193 * we don't care about what the return value signals.
195 OSSL_DECODER_from_data(dctx, &p, NULL);
201 OSSL_DECODER_CTX_free(dctx);
202 OPENSSL_free(tmpbuf);
206 static int x509_pubkey_ex_i2d(const ASN1_VALUE **pval, unsigned char **out,
207 const ASN1_ITEM *it, int tag, int aclass)
209 return ASN1_item_ex_i2d(pval, out, ASN1_ITEM_rptr(X509_PUBKEY_INTERNAL),
213 static int x509_pubkey_ex_print(BIO *out, const ASN1_VALUE **pval, int indent,
214 const char *fname, const ASN1_PCTX *pctx)
216 return ASN1_item_print(out, *pval, indent,
217 ASN1_ITEM_rptr(X509_PUBKEY_INTERNAL), pctx);
220 static const ASN1_EXTERN_FUNCS x509_pubkey_ff = {
224 0, /* Default clear behaviour is OK */
227 x509_pubkey_ex_print,
228 x509_pubkey_ex_new_ex,
229 x509_pubkey_ex_d2i_ex,
232 IMPLEMENT_EXTERN_ASN1(X509_PUBKEY, V_ASN1_SEQUENCE, x509_pubkey_ff)
233 IMPLEMENT_ASN1_FUNCTIONS(X509_PUBKEY)
235 X509_PUBKEY *X509_PUBKEY_new_ex(OSSL_LIB_CTX *libctx, const char *propq)
237 X509_PUBKEY *pubkey = NULL;
239 pubkey = (X509_PUBKEY *)ASN1_item_new_ex(X509_PUBKEY_it(), libctx, propq);
240 if (!x509_pubkey_set0_libctx(pubkey, libctx, propq)) {
241 X509_PUBKEY_free(pubkey);
248 * X509_PUBKEY_dup() must be implemented manually, because there is no
249 * support for it in ASN1_EXTERN_FUNCS.
251 X509_PUBKEY *X509_PUBKEY_dup(const X509_PUBKEY *a)
253 X509_PUBKEY *pubkey = OPENSSL_zalloc(sizeof(*pubkey));
256 || !x509_pubkey_set0_libctx(pubkey, a->libctx, a->propq)
257 || (pubkey->algor = X509_ALGOR_dup(a->algor)) == NULL
258 || (pubkey->public_key = ASN1_BIT_STRING_new()) == NULL
259 || !ASN1_BIT_STRING_set(pubkey->public_key,
260 a->public_key->data, a->public_key->length)
261 || (a->pkey != NULL && !EVP_PKEY_up_ref(a->pkey))) {
262 x509_pubkey_ex_free((ASN1_VALUE **)&pubkey,
263 ASN1_ITEM_rptr(X509_PUBKEY_INTERNAL));
264 ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
267 pubkey->pkey = a->pkey;
271 int X509_PUBKEY_set(X509_PUBKEY **x, EVP_PKEY *pkey)
273 X509_PUBKEY *pk = NULL;
275 if (x == NULL || pkey == NULL) {
276 ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER);
280 if (pkey->ameth != NULL) {
281 if ((pk = X509_PUBKEY_new()) == NULL) {
282 ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
285 if (pkey->ameth->pub_encode != NULL) {
286 if (!pkey->ameth->pub_encode(pk, pkey)) {
287 ERR_raise(ERR_LIB_X509, X509_R_PUBLIC_KEY_ENCODE_ERROR);
291 ERR_raise(ERR_LIB_X509, X509_R_METHOD_NOT_SUPPORTED);
294 } else if (evp_pkey_is_provided(pkey)) {
295 unsigned char *der = NULL;
297 OSSL_ENCODER_CTX *ectx =
298 OSSL_ENCODER_CTX_new_for_pkey(pkey, EVP_PKEY_PUBLIC_KEY,
299 "DER", "SubjectPublicKeyInfo",
302 if (OSSL_ENCODER_to_data(ectx, &der, &derlen)) {
303 const unsigned char *pder = der;
305 pk = d2i_X509_PUBKEY(NULL, &pder, (long)derlen);
308 OSSL_ENCODER_CTX_free(ectx);
313 ERR_raise(ERR_LIB_X509, X509_R_UNSUPPORTED_ALGORITHM);
317 X509_PUBKEY_free(*x);
318 if (!EVP_PKEY_up_ref(pkey)) {
319 ERR_raise(ERR_LIB_X509, ERR_R_INTERNAL_ERROR);
325 * pk->pkey is NULL when using the legacy routine, but is non-NULL when
326 * going through the encoder, and for all intents and purposes, it's
327 * a perfect copy of the public key portions of |pkey|, just not the same
328 * instance. If that's all there was to pkey then we could simply return
329 * early, right here. However, some application might very well depend on
330 * the passed |pkey| being used and none other, so we spend a few more
331 * cycles throwing away the newly created |pk->pkey| and replace it with
334 if (pk->pkey != NULL)
335 EVP_PKEY_free(pk->pkey);
341 X509_PUBKEY_free(pk);
346 * Attempt to decode a public key.
347 * Returns 1 on success, 0 for a decode failure and -1 for a fatal
348 * error e.g. malloc failure.
350 * This function is #legacy.
352 static int x509_pubkey_decode(EVP_PKEY **ppkey, const X509_PUBKEY *key)
354 EVP_PKEY *pkey = EVP_PKEY_new();
357 ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
361 if (!EVP_PKEY_set_type(pkey, OBJ_obj2nid(key->algor->algorithm))) {
362 ERR_raise(ERR_LIB_X509, X509_R_UNSUPPORTED_ALGORITHM);
366 if (pkey->ameth->pub_decode) {
368 * Treat any failure of pub_decode as a decode error. In
369 * future we could have different return codes for decode
370 * errors and fatal errors such as malloc failure.
372 if (!pkey->ameth->pub_decode(pkey, key))
375 ERR_raise(ERR_LIB_X509, X509_R_METHOD_NOT_SUPPORTED);
387 EVP_PKEY *X509_PUBKEY_get0(const X509_PUBKEY *key)
389 EVP_PKEY *ret = NULL;
391 if (key == NULL || key->public_key == NULL)
394 if (key->pkey != NULL)
398 * When the key ASN.1 is initially parsed an attempt is made to
399 * decode the public key and cache the EVP_PKEY structure. If this
400 * operation fails the cached value will be NULL. Parsing continues
401 * to allow parsing of unknown key types or unsupported forms.
402 * We repeat the decode operation so the appropriate errors are left
405 x509_pubkey_decode(&ret, key);
406 /* If decode doesn't fail something bad happened */
408 ERR_raise(ERR_LIB_X509, ERR_R_INTERNAL_ERROR);
415 EVP_PKEY *X509_PUBKEY_get(const X509_PUBKEY *key)
417 EVP_PKEY *ret = X509_PUBKEY_get0(key);
419 if (ret != NULL && !EVP_PKEY_up_ref(ret)) {
420 ERR_raise(ERR_LIB_X509, ERR_R_INTERNAL_ERROR);
427 * Now three pseudo ASN1 routines that take an EVP_PKEY structure and encode
428 * or decode as X509_PUBKEY
430 static EVP_PKEY *d2i_PUBKEY_int(EVP_PKEY **a,
431 const unsigned char **pp, long length,
432 OSSL_LIB_CTX *libctx, const char *propq,
433 unsigned int force_legacy,
435 (*d2i_x509_pubkey)(X509_PUBKEY **a,
436 const unsigned char **in,
439 X509_PUBKEY *xpk, *xpk2 = NULL, **pxpk = NULL;
440 EVP_PKEY *pktmp = NULL;
441 const unsigned char *q;
446 * If libctx or propq are non-NULL, we take advantage of the reuse
447 * feature. It's not generally recommended, but is safe enough for
448 * newly created structures.
450 if (libctx != NULL || propq != NULL || force_legacy) {
451 xpk2 = OPENSSL_zalloc(sizeof(*xpk2));
453 ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
456 if (!x509_pubkey_set0_libctx(xpk2, libctx, propq))
458 xpk2->flag_force_legacy = !!force_legacy;
461 xpk = d2i_x509_pubkey(pxpk, &q, length);
464 pktmp = X509_PUBKEY_get(xpk);
465 X509_PUBKEY_free(xpk);
466 xpk2 = NULL; /* We know that xpk == xpk2 */
475 X509_PUBKEY_free(xpk2);
479 /* For the algorithm specific d2i functions further down */
480 static EVP_PKEY *d2i_PUBKEY_legacy(EVP_PKEY **a,
481 const unsigned char **pp, long length)
483 return d2i_PUBKEY_int(a, pp, length, NULL, NULL, 1, d2i_X509_PUBKEY);
486 EVP_PKEY *d2i_PUBKEY_ex(EVP_PKEY **a, const unsigned char **pp, long length,
487 OSSL_LIB_CTX *libctx, const char *propq)
489 return d2i_PUBKEY_int(a, pp, length, libctx, propq, 0, d2i_X509_PUBKEY);
492 EVP_PKEY *d2i_PUBKEY(EVP_PKEY **a, const unsigned char **pp, long length)
494 return d2i_PUBKEY_ex(a, pp, length, NULL, NULL);
497 int i2d_PUBKEY(const EVP_PKEY *a, unsigned char **pp)
503 if (a->ameth != NULL) {
504 X509_PUBKEY *xpk = NULL;
506 if ((xpk = X509_PUBKEY_new()) == NULL)
509 /* pub_encode() only encode parameters, not the key itself */
510 if (a->ameth->pub_encode != NULL && a->ameth->pub_encode(xpk, a)) {
511 xpk->pkey = (EVP_PKEY *)a;
512 ret = i2d_X509_PUBKEY(xpk, pp);
515 X509_PUBKEY_free(xpk);
516 } else if (a->keymgmt != NULL) {
517 OSSL_ENCODER_CTX *ctx =
518 OSSL_ENCODER_CTX_new_for_pkey(a, EVP_PKEY_PUBLIC_KEY,
519 "DER", "SubjectPublicKeyInfo",
521 BIO *out = BIO_new(BIO_s_mem());
524 if (OSSL_ENCODER_CTX_get_num_encoders(ctx) != 0
526 && OSSL_ENCODER_to_bio(ctx, out)
527 && BIO_get_mem_ptr(out, &buf) > 0) {
532 *pp = (unsigned char *)buf->data;
536 memcpy(*pp, buf->data, ret);
542 OSSL_ENCODER_CTX_free(ctx);
549 * The following are equivalents but which return RSA and DSA keys
551 RSA *d2i_RSA_PUBKEY(RSA **a, const unsigned char **pp, long length)
555 const unsigned char *q;
558 pkey = d2i_PUBKEY_legacy(NULL, &q, length);
561 key = EVP_PKEY_get1_RSA(pkey);
573 int i2d_RSA_PUBKEY(const RSA *a, unsigned char **pp)
579 pktmp = EVP_PKEY_new();
581 ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
584 (void)EVP_PKEY_assign_RSA(pktmp, (RSA *)a);
585 ret = i2d_PUBKEY(pktmp, pp);
586 pktmp->pkey.ptr = NULL;
587 EVP_PKEY_free(pktmp);
591 #ifndef OPENSSL_NO_DH
592 DH *ossl_d2i_DH_PUBKEY(DH **a, const unsigned char **pp, long length)
596 const unsigned char *q;
599 pkey = d2i_PUBKEY_legacy(NULL, &q, length);
602 if (EVP_PKEY_get_id(pkey) == EVP_PKEY_DH)
603 key = EVP_PKEY_get1_DH(pkey);
615 int ossl_i2d_DH_PUBKEY(const DH *a, unsigned char **pp)
621 pktmp = EVP_PKEY_new();
623 ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
626 (void)EVP_PKEY_assign_DH(pktmp, (DH *)a);
627 ret = i2d_PUBKEY(pktmp, pp);
628 pktmp->pkey.ptr = NULL;
629 EVP_PKEY_free(pktmp);
633 DH *ossl_d2i_DHx_PUBKEY(DH **a, const unsigned char **pp, long length)
637 const unsigned char *q;
640 pkey = d2i_PUBKEY_legacy(NULL, &q, length);
643 if (EVP_PKEY_get_id(pkey) == EVP_PKEY_DHX)
644 key = EVP_PKEY_get1_DH(pkey);
656 int ossl_i2d_DHx_PUBKEY(const DH *a, unsigned char **pp)
662 pktmp = EVP_PKEY_new();
664 ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
667 (void)EVP_PKEY_assign(pktmp, EVP_PKEY_DHX, (DH *)a);
668 ret = i2d_PUBKEY(pktmp, pp);
669 pktmp->pkey.ptr = NULL;
670 EVP_PKEY_free(pktmp);
675 #ifndef OPENSSL_NO_DSA
676 DSA *d2i_DSA_PUBKEY(DSA **a, const unsigned char **pp, long length)
680 const unsigned char *q;
683 pkey = d2i_PUBKEY_legacy(NULL, &q, length);
686 key = EVP_PKEY_get1_DSA(pkey);
698 int i2d_DSA_PUBKEY(const DSA *a, unsigned char **pp)
704 pktmp = EVP_PKEY_new();
706 ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
709 (void)EVP_PKEY_assign_DSA(pktmp, (DSA *)a);
710 ret = i2d_PUBKEY(pktmp, pp);
711 pktmp->pkey.ptr = NULL;
712 EVP_PKEY_free(pktmp);
717 #ifndef OPENSSL_NO_EC
718 EC_KEY *d2i_EC_PUBKEY(EC_KEY **a, const unsigned char **pp, long length)
722 const unsigned char *q;
726 pkey = d2i_PUBKEY_legacy(NULL, &q, length);
729 type = EVP_PKEY_get_id(pkey);
730 if (type == EVP_PKEY_EC || type == EVP_PKEY_SM2)
731 key = EVP_PKEY_get1_EC_KEY(pkey);
743 int i2d_EC_PUBKEY(const EC_KEY *a, unsigned char **pp)
750 if ((pktmp = EVP_PKEY_new()) == NULL) {
751 ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
754 (void)EVP_PKEY_assign_EC_KEY(pktmp, (EC_KEY *)a);
755 ret = i2d_PUBKEY(pktmp, pp);
756 pktmp->pkey.ptr = NULL;
757 EVP_PKEY_free(pktmp);
761 ECX_KEY *ossl_d2i_ED25519_PUBKEY(ECX_KEY **a,
762 const unsigned char **pp, long length)
766 const unsigned char *q;
769 pkey = d2i_PUBKEY_legacy(NULL, &q, length);
772 key = ossl_evp_pkey_get1_ED25519(pkey);
778 ossl_ecx_key_free(*a);
784 int ossl_i2d_ED25519_PUBKEY(const ECX_KEY *a, unsigned char **pp)
791 if ((pktmp = EVP_PKEY_new()) == NULL) {
792 ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
795 (void)EVP_PKEY_assign(pktmp, EVP_PKEY_ED25519, (ECX_KEY *)a);
796 ret = i2d_PUBKEY(pktmp, pp);
797 pktmp->pkey.ptr = NULL;
798 EVP_PKEY_free(pktmp);
802 ECX_KEY *ossl_d2i_ED448_PUBKEY(ECX_KEY **a,
803 const unsigned char **pp, long length)
807 const unsigned char *q;
810 pkey = d2i_PUBKEY_legacy(NULL, &q, length);
813 if (EVP_PKEY_get_id(pkey) == EVP_PKEY_ED448)
814 key = ossl_evp_pkey_get1_ED448(pkey);
820 ossl_ecx_key_free(*a);
826 int ossl_i2d_ED448_PUBKEY(const ECX_KEY *a, unsigned char **pp)
833 if ((pktmp = EVP_PKEY_new()) == NULL) {
834 ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
837 (void)EVP_PKEY_assign(pktmp, EVP_PKEY_ED448, (ECX_KEY *)a);
838 ret = i2d_PUBKEY(pktmp, pp);
839 pktmp->pkey.ptr = NULL;
840 EVP_PKEY_free(pktmp);
844 ECX_KEY *ossl_d2i_X25519_PUBKEY(ECX_KEY **a,
845 const unsigned char **pp, long length)
849 const unsigned char *q;
852 pkey = d2i_PUBKEY_legacy(NULL, &q, length);
855 if (EVP_PKEY_get_id(pkey) == EVP_PKEY_X25519)
856 key = ossl_evp_pkey_get1_X25519(pkey);
862 ossl_ecx_key_free(*a);
868 int ossl_i2d_X25519_PUBKEY(const ECX_KEY *a, unsigned char **pp)
875 if ((pktmp = EVP_PKEY_new()) == NULL) {
876 ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
879 (void)EVP_PKEY_assign(pktmp, EVP_PKEY_X25519, (ECX_KEY *)a);
880 ret = i2d_PUBKEY(pktmp, pp);
881 pktmp->pkey.ptr = NULL;
882 EVP_PKEY_free(pktmp);
886 ECX_KEY *ossl_d2i_X448_PUBKEY(ECX_KEY **a,
887 const unsigned char **pp, long length)
891 const unsigned char *q;
894 pkey = d2i_PUBKEY_legacy(NULL, &q, length);
897 if (EVP_PKEY_get_id(pkey) == EVP_PKEY_X448)
898 key = ossl_evp_pkey_get1_X448(pkey);
904 ossl_ecx_key_free(*a);
910 int ossl_i2d_X448_PUBKEY(const ECX_KEY *a, unsigned char **pp)
917 if ((pktmp = EVP_PKEY_new()) == NULL) {
918 ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
921 (void)EVP_PKEY_assign(pktmp, EVP_PKEY_X448, (ECX_KEY *)a);
922 ret = i2d_PUBKEY(pktmp, pp);
923 pktmp->pkey.ptr = NULL;
924 EVP_PKEY_free(pktmp);
930 int X509_PUBKEY_set0_param(X509_PUBKEY *pub, ASN1_OBJECT *aobj,
931 int ptype, void *pval,
932 unsigned char *penc, int penclen)
934 if (!X509_ALGOR_set0(pub->algor, aobj, ptype, pval))
937 OPENSSL_free(pub->public_key->data);
938 pub->public_key->data = penc;
939 pub->public_key->length = penclen;
940 /* Set number of unused bits to zero */
941 pub->public_key->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
942 pub->public_key->flags |= ASN1_STRING_FLAG_BITS_LEFT;
947 int X509_PUBKEY_get0_param(ASN1_OBJECT **ppkalg,
948 const unsigned char **pk, int *ppklen,
949 X509_ALGOR **pa, const X509_PUBKEY *pub)
952 *ppkalg = pub->algor->algorithm;
954 *pk = pub->public_key->data;
955 *ppklen = pub->public_key->length;
962 ASN1_BIT_STRING *X509_get0_pubkey_bitstr(const X509 *x)
966 return x->cert_info.key->public_key;
969 /* Returns 1 for equal, 0, for non-equal, < 0 on error */
970 int X509_PUBKEY_eq(const X509_PUBKEY *a, const X509_PUBKEY *b)
972 X509_ALGOR *algA, *algB;
977 if (a == NULL || b == NULL)
979 if (!X509_PUBKEY_get0_param(NULL, NULL, NULL, &algA, a) || algA == NULL
980 || !X509_PUBKEY_get0_param(NULL, NULL, NULL, &algB, b) || algB == NULL)
982 if (X509_ALGOR_cmp(algA, algB) != 0)
984 if ((pA = X509_PUBKEY_get0(a)) == NULL
985 || (pB = X509_PUBKEY_get0(b)) == NULL)
987 return EVP_PKEY_eq(pA, pB);
990 int ossl_x509_PUBKEY_get0_libctx(OSSL_LIB_CTX **plibctx, const char **ppropq,
991 const X509_PUBKEY *key)
994 *plibctx = key->libctx;
996 *ppropq = key->propq;