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];
164 size_t slen = publen;
167 * The decoders don't know how to handle anything other than Universal
168 * class so we modify the data accordingly.
170 if (aclass != V_ASN1_UNIVERSAL) {
171 tmpbuf = OPENSSL_memdup(in_saved, publen);
172 if (tmpbuf == NULL) {
173 ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
177 *tmpbuf = V_ASN1_CONSTRUCTED | V_ASN1_SEQUENCE;
181 if (OBJ_obj2txt(txtoidname, sizeof(txtoidname),
182 pubkey->algor->algorithm, 0) <= 0) {
183 ERR_clear_last_mark();
187 OSSL_DECODER_CTX_new_for_pkey(&pubkey->pkey,
188 "DER", "SubjectPublicKeyInfo",
189 txtoidname, EVP_PKEY_PUBLIC_KEY,
191 pubkey->propq)) != NULL)
193 * As said higher up, we're being opportunistic. In other words,
194 * we don't care if we fail.
196 if (OSSL_DECODER_from_data(dctx, &p, &slen)) {
199 * If we successfully decoded then we *must* consume all the
202 ERR_clear_last_mark();
203 ERR_raise(ERR_LIB_ASN1, EVP_R_DECODE_ERROR);
212 OSSL_DECODER_CTX_free(dctx);
213 OPENSSL_free(tmpbuf);
217 static int x509_pubkey_ex_i2d(const ASN1_VALUE **pval, unsigned char **out,
218 const ASN1_ITEM *it, int tag, int aclass)
220 return ASN1_item_ex_i2d(pval, out, ASN1_ITEM_rptr(X509_PUBKEY_INTERNAL),
224 static int x509_pubkey_ex_print(BIO *out, const ASN1_VALUE **pval, int indent,
225 const char *fname, const ASN1_PCTX *pctx)
227 return ASN1_item_print(out, *pval, indent,
228 ASN1_ITEM_rptr(X509_PUBKEY_INTERNAL), pctx);
231 static const ASN1_EXTERN_FUNCS x509_pubkey_ff = {
235 0, /* Default clear behaviour is OK */
238 x509_pubkey_ex_print,
239 x509_pubkey_ex_new_ex,
240 x509_pubkey_ex_d2i_ex,
243 IMPLEMENT_EXTERN_ASN1(X509_PUBKEY, V_ASN1_SEQUENCE, x509_pubkey_ff)
244 IMPLEMENT_ASN1_FUNCTIONS(X509_PUBKEY)
246 X509_PUBKEY *X509_PUBKEY_new_ex(OSSL_LIB_CTX *libctx, const char *propq)
248 X509_PUBKEY *pubkey = NULL;
250 pubkey = (X509_PUBKEY *)ASN1_item_new_ex(X509_PUBKEY_it(), libctx, propq);
251 if (!x509_pubkey_set0_libctx(pubkey, libctx, propq)) {
252 X509_PUBKEY_free(pubkey);
259 * X509_PUBKEY_dup() must be implemented manually, because there is no
260 * support for it in ASN1_EXTERN_FUNCS.
262 X509_PUBKEY *X509_PUBKEY_dup(const X509_PUBKEY *a)
264 X509_PUBKEY *pubkey = OPENSSL_zalloc(sizeof(*pubkey));
267 || !x509_pubkey_set0_libctx(pubkey, a->libctx, a->propq)
268 || (pubkey->algor = X509_ALGOR_dup(a->algor)) == NULL
269 || (pubkey->public_key = ASN1_BIT_STRING_new()) == NULL
270 || !ASN1_BIT_STRING_set(pubkey->public_key,
271 a->public_key->data, a->public_key->length)
272 || (a->pkey != NULL && !EVP_PKEY_up_ref(a->pkey))) {
273 x509_pubkey_ex_free((ASN1_VALUE **)&pubkey,
274 ASN1_ITEM_rptr(X509_PUBKEY_INTERNAL));
275 ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
278 pubkey->pkey = a->pkey;
282 int X509_PUBKEY_set(X509_PUBKEY **x, EVP_PKEY *pkey)
284 X509_PUBKEY *pk = NULL;
286 if (x == NULL || pkey == NULL) {
287 ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER);
291 if (pkey->ameth != NULL) {
292 if ((pk = X509_PUBKEY_new()) == NULL) {
293 ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
296 if (pkey->ameth->pub_encode != NULL) {
297 if (!pkey->ameth->pub_encode(pk, pkey)) {
298 ERR_raise(ERR_LIB_X509, X509_R_PUBLIC_KEY_ENCODE_ERROR);
302 ERR_raise(ERR_LIB_X509, X509_R_METHOD_NOT_SUPPORTED);
305 } else if (evp_pkey_is_provided(pkey)) {
306 unsigned char *der = NULL;
308 OSSL_ENCODER_CTX *ectx =
309 OSSL_ENCODER_CTX_new_for_pkey(pkey, EVP_PKEY_PUBLIC_KEY,
310 "DER", "SubjectPublicKeyInfo",
313 if (OSSL_ENCODER_to_data(ectx, &der, &derlen)) {
314 const unsigned char *pder = der;
316 pk = d2i_X509_PUBKEY(NULL, &pder, (long)derlen);
319 OSSL_ENCODER_CTX_free(ectx);
324 ERR_raise(ERR_LIB_X509, X509_R_UNSUPPORTED_ALGORITHM);
328 X509_PUBKEY_free(*x);
329 if (!EVP_PKEY_up_ref(pkey)) {
330 ERR_raise(ERR_LIB_X509, ERR_R_INTERNAL_ERROR);
336 * pk->pkey is NULL when using the legacy routine, but is non-NULL when
337 * going through the encoder, and for all intents and purposes, it's
338 * a perfect copy of the public key portions of |pkey|, just not the same
339 * instance. If that's all there was to pkey then we could simply return
340 * early, right here. However, some application might very well depend on
341 * the passed |pkey| being used and none other, so we spend a few more
342 * cycles throwing away the newly created |pk->pkey| and replace it with
345 if (pk->pkey != NULL)
346 EVP_PKEY_free(pk->pkey);
352 X509_PUBKEY_free(pk);
357 * Attempt to decode a public key.
358 * Returns 1 on success, 0 for a decode failure and -1 for a fatal
359 * error e.g. malloc failure.
361 * This function is #legacy.
363 static int x509_pubkey_decode(EVP_PKEY **ppkey, const X509_PUBKEY *key)
365 EVP_PKEY *pkey = EVP_PKEY_new();
368 ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
372 if (!EVP_PKEY_set_type(pkey, OBJ_obj2nid(key->algor->algorithm))) {
373 ERR_raise(ERR_LIB_X509, X509_R_UNSUPPORTED_ALGORITHM);
377 if (pkey->ameth->pub_decode) {
379 * Treat any failure of pub_decode as a decode error. In
380 * future we could have different return codes for decode
381 * errors and fatal errors such as malloc failure.
383 if (!pkey->ameth->pub_decode(pkey, key))
386 ERR_raise(ERR_LIB_X509, X509_R_METHOD_NOT_SUPPORTED);
398 EVP_PKEY *X509_PUBKEY_get0(const X509_PUBKEY *key)
400 EVP_PKEY *ret = NULL;
402 if (key == NULL || key->public_key == NULL)
405 if (key->pkey != NULL)
409 * When the key ASN.1 is initially parsed an attempt is made to
410 * decode the public key and cache the EVP_PKEY structure. If this
411 * operation fails the cached value will be NULL. Parsing continues
412 * to allow parsing of unknown key types or unsupported forms.
413 * We repeat the decode operation so the appropriate errors are left
416 x509_pubkey_decode(&ret, key);
417 /* If decode doesn't fail something bad happened */
419 ERR_raise(ERR_LIB_X509, ERR_R_INTERNAL_ERROR);
426 EVP_PKEY *X509_PUBKEY_get(const X509_PUBKEY *key)
428 EVP_PKEY *ret = X509_PUBKEY_get0(key);
430 if (ret != NULL && !EVP_PKEY_up_ref(ret)) {
431 ERR_raise(ERR_LIB_X509, ERR_R_INTERNAL_ERROR);
438 * Now three pseudo ASN1 routines that take an EVP_PKEY structure and encode
439 * or decode as X509_PUBKEY
441 static EVP_PKEY *d2i_PUBKEY_int(EVP_PKEY **a,
442 const unsigned char **pp, long length,
443 OSSL_LIB_CTX *libctx, const char *propq,
444 unsigned int force_legacy,
446 (*d2i_x509_pubkey)(X509_PUBKEY **a,
447 const unsigned char **in,
450 X509_PUBKEY *xpk, *xpk2 = NULL, **pxpk = NULL;
451 EVP_PKEY *pktmp = NULL;
452 const unsigned char *q;
457 * If libctx or propq are non-NULL, we take advantage of the reuse
458 * feature. It's not generally recommended, but is safe enough for
459 * newly created structures.
461 if (libctx != NULL || propq != NULL || force_legacy) {
462 xpk2 = OPENSSL_zalloc(sizeof(*xpk2));
464 ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
467 if (!x509_pubkey_set0_libctx(xpk2, libctx, propq))
469 xpk2->flag_force_legacy = !!force_legacy;
472 xpk = d2i_x509_pubkey(pxpk, &q, length);
475 pktmp = X509_PUBKEY_get(xpk);
476 X509_PUBKEY_free(xpk);
477 xpk2 = NULL; /* We know that xpk == xpk2 */
486 X509_PUBKEY_free(xpk2);
490 /* For the algorithm specific d2i functions further down */
491 static EVP_PKEY *d2i_PUBKEY_legacy(EVP_PKEY **a,
492 const unsigned char **pp, long length)
494 return d2i_PUBKEY_int(a, pp, length, NULL, NULL, 1, d2i_X509_PUBKEY);
497 EVP_PKEY *d2i_PUBKEY_ex(EVP_PKEY **a, const unsigned char **pp, long length,
498 OSSL_LIB_CTX *libctx, const char *propq)
500 return d2i_PUBKEY_int(a, pp, length, libctx, propq, 0, d2i_X509_PUBKEY);
503 EVP_PKEY *d2i_PUBKEY(EVP_PKEY **a, const unsigned char **pp, long length)
505 return d2i_PUBKEY_ex(a, pp, length, NULL, NULL);
508 int i2d_PUBKEY(const EVP_PKEY *a, unsigned char **pp)
514 if (a->ameth != NULL) {
515 X509_PUBKEY *xpk = NULL;
517 if ((xpk = X509_PUBKEY_new()) == NULL)
520 /* pub_encode() only encode parameters, not the key itself */
521 if (a->ameth->pub_encode != NULL && a->ameth->pub_encode(xpk, a)) {
522 xpk->pkey = (EVP_PKEY *)a;
523 ret = i2d_X509_PUBKEY(xpk, pp);
526 X509_PUBKEY_free(xpk);
527 } else if (a->keymgmt != NULL) {
528 OSSL_ENCODER_CTX *ctx =
529 OSSL_ENCODER_CTX_new_for_pkey(a, EVP_PKEY_PUBLIC_KEY,
530 "DER", "SubjectPublicKeyInfo",
532 BIO *out = BIO_new(BIO_s_mem());
535 if (OSSL_ENCODER_CTX_get_num_encoders(ctx) != 0
537 && OSSL_ENCODER_to_bio(ctx, out)
538 && BIO_get_mem_ptr(out, &buf) > 0) {
543 *pp = (unsigned char *)buf->data;
547 memcpy(*pp, buf->data, ret);
553 OSSL_ENCODER_CTX_free(ctx);
560 * The following are equivalents but which return RSA and DSA keys
562 RSA *d2i_RSA_PUBKEY(RSA **a, const unsigned char **pp, long length)
566 const unsigned char *q;
569 pkey = d2i_PUBKEY_legacy(NULL, &q, length);
572 key = EVP_PKEY_get1_RSA(pkey);
584 int i2d_RSA_PUBKEY(const RSA *a, unsigned char **pp)
590 pktmp = EVP_PKEY_new();
592 ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
595 (void)EVP_PKEY_assign_RSA(pktmp, (RSA *)a);
596 ret = i2d_PUBKEY(pktmp, pp);
597 pktmp->pkey.ptr = NULL;
598 EVP_PKEY_free(pktmp);
602 #ifndef OPENSSL_NO_DH
603 DH *ossl_d2i_DH_PUBKEY(DH **a, const unsigned char **pp, long length)
607 const unsigned char *q;
610 pkey = d2i_PUBKEY_legacy(NULL, &q, length);
613 if (EVP_PKEY_get_id(pkey) == EVP_PKEY_DH)
614 key = EVP_PKEY_get1_DH(pkey);
626 int ossl_i2d_DH_PUBKEY(const DH *a, unsigned char **pp)
632 pktmp = EVP_PKEY_new();
634 ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
637 (void)EVP_PKEY_assign_DH(pktmp, (DH *)a);
638 ret = i2d_PUBKEY(pktmp, pp);
639 pktmp->pkey.ptr = NULL;
640 EVP_PKEY_free(pktmp);
644 DH *ossl_d2i_DHx_PUBKEY(DH **a, const unsigned char **pp, long length)
648 const unsigned char *q;
651 pkey = d2i_PUBKEY_legacy(NULL, &q, length);
654 if (EVP_PKEY_get_id(pkey) == EVP_PKEY_DHX)
655 key = EVP_PKEY_get1_DH(pkey);
667 int ossl_i2d_DHx_PUBKEY(const DH *a, unsigned char **pp)
673 pktmp = EVP_PKEY_new();
675 ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
678 (void)EVP_PKEY_assign(pktmp, EVP_PKEY_DHX, (DH *)a);
679 ret = i2d_PUBKEY(pktmp, pp);
680 pktmp->pkey.ptr = NULL;
681 EVP_PKEY_free(pktmp);
686 #ifndef OPENSSL_NO_DSA
687 DSA *d2i_DSA_PUBKEY(DSA **a, const unsigned char **pp, long length)
691 const unsigned char *q;
694 pkey = d2i_PUBKEY_legacy(NULL, &q, length);
697 key = EVP_PKEY_get1_DSA(pkey);
709 int i2d_DSA_PUBKEY(const DSA *a, unsigned char **pp)
715 pktmp = EVP_PKEY_new();
717 ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
720 (void)EVP_PKEY_assign_DSA(pktmp, (DSA *)a);
721 ret = i2d_PUBKEY(pktmp, pp);
722 pktmp->pkey.ptr = NULL;
723 EVP_PKEY_free(pktmp);
728 #ifndef OPENSSL_NO_EC
729 EC_KEY *d2i_EC_PUBKEY(EC_KEY **a, const unsigned char **pp, long length)
733 const unsigned char *q;
737 pkey = d2i_PUBKEY_legacy(NULL, &q, length);
740 type = EVP_PKEY_get_id(pkey);
741 if (type == EVP_PKEY_EC || type == EVP_PKEY_SM2)
742 key = EVP_PKEY_get1_EC_KEY(pkey);
754 int i2d_EC_PUBKEY(const EC_KEY *a, unsigned char **pp)
761 if ((pktmp = EVP_PKEY_new()) == NULL) {
762 ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
765 (void)EVP_PKEY_assign_EC_KEY(pktmp, (EC_KEY *)a);
766 ret = i2d_PUBKEY(pktmp, pp);
767 pktmp->pkey.ptr = NULL;
768 EVP_PKEY_free(pktmp);
772 ECX_KEY *ossl_d2i_ED25519_PUBKEY(ECX_KEY **a,
773 const unsigned char **pp, long length)
777 const unsigned char *q;
780 pkey = d2i_PUBKEY_legacy(NULL, &q, length);
783 key = ossl_evp_pkey_get1_ED25519(pkey);
789 ossl_ecx_key_free(*a);
795 int ossl_i2d_ED25519_PUBKEY(const ECX_KEY *a, unsigned char **pp)
802 if ((pktmp = EVP_PKEY_new()) == NULL) {
803 ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
806 (void)EVP_PKEY_assign(pktmp, EVP_PKEY_ED25519, (ECX_KEY *)a);
807 ret = i2d_PUBKEY(pktmp, pp);
808 pktmp->pkey.ptr = NULL;
809 EVP_PKEY_free(pktmp);
813 ECX_KEY *ossl_d2i_ED448_PUBKEY(ECX_KEY **a,
814 const unsigned char **pp, long length)
818 const unsigned char *q;
821 pkey = d2i_PUBKEY_legacy(NULL, &q, length);
824 if (EVP_PKEY_get_id(pkey) == EVP_PKEY_ED448)
825 key = ossl_evp_pkey_get1_ED448(pkey);
831 ossl_ecx_key_free(*a);
837 int ossl_i2d_ED448_PUBKEY(const ECX_KEY *a, unsigned char **pp)
844 if ((pktmp = EVP_PKEY_new()) == NULL) {
845 ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
848 (void)EVP_PKEY_assign(pktmp, EVP_PKEY_ED448, (ECX_KEY *)a);
849 ret = i2d_PUBKEY(pktmp, pp);
850 pktmp->pkey.ptr = NULL;
851 EVP_PKEY_free(pktmp);
855 ECX_KEY *ossl_d2i_X25519_PUBKEY(ECX_KEY **a,
856 const unsigned char **pp, long length)
860 const unsigned char *q;
863 pkey = d2i_PUBKEY_legacy(NULL, &q, length);
866 if (EVP_PKEY_get_id(pkey) == EVP_PKEY_X25519)
867 key = ossl_evp_pkey_get1_X25519(pkey);
873 ossl_ecx_key_free(*a);
879 int ossl_i2d_X25519_PUBKEY(const ECX_KEY *a, unsigned char **pp)
886 if ((pktmp = EVP_PKEY_new()) == NULL) {
887 ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
890 (void)EVP_PKEY_assign(pktmp, EVP_PKEY_X25519, (ECX_KEY *)a);
891 ret = i2d_PUBKEY(pktmp, pp);
892 pktmp->pkey.ptr = NULL;
893 EVP_PKEY_free(pktmp);
897 ECX_KEY *ossl_d2i_X448_PUBKEY(ECX_KEY **a,
898 const unsigned char **pp, long length)
902 const unsigned char *q;
905 pkey = d2i_PUBKEY_legacy(NULL, &q, length);
908 if (EVP_PKEY_get_id(pkey) == EVP_PKEY_X448)
909 key = ossl_evp_pkey_get1_X448(pkey);
915 ossl_ecx_key_free(*a);
921 int ossl_i2d_X448_PUBKEY(const ECX_KEY *a, unsigned char **pp)
928 if ((pktmp = EVP_PKEY_new()) == NULL) {
929 ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
932 (void)EVP_PKEY_assign(pktmp, EVP_PKEY_X448, (ECX_KEY *)a);
933 ret = i2d_PUBKEY(pktmp, pp);
934 pktmp->pkey.ptr = NULL;
935 EVP_PKEY_free(pktmp);
941 int X509_PUBKEY_set0_param(X509_PUBKEY *pub, ASN1_OBJECT *aobj,
942 int ptype, void *pval,
943 unsigned char *penc, int penclen)
945 if (!X509_ALGOR_set0(pub->algor, aobj, ptype, pval))
948 OPENSSL_free(pub->public_key->data);
949 pub->public_key->data = penc;
950 pub->public_key->length = penclen;
951 /* Set number of unused bits to zero */
952 pub->public_key->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
953 pub->public_key->flags |= ASN1_STRING_FLAG_BITS_LEFT;
958 int X509_PUBKEY_get0_param(ASN1_OBJECT **ppkalg,
959 const unsigned char **pk, int *ppklen,
960 X509_ALGOR **pa, const X509_PUBKEY *pub)
963 *ppkalg = pub->algor->algorithm;
965 *pk = pub->public_key->data;
966 *ppklen = pub->public_key->length;
973 ASN1_BIT_STRING *X509_get0_pubkey_bitstr(const X509 *x)
977 return x->cert_info.key->public_key;
980 /* Returns 1 for equal, 0, for non-equal, < 0 on error */
981 int X509_PUBKEY_eq(const X509_PUBKEY *a, const X509_PUBKEY *b)
983 X509_ALGOR *algA, *algB;
988 if (a == NULL || b == NULL)
990 if (!X509_PUBKEY_get0_param(NULL, NULL, NULL, &algA, a) || algA == NULL
991 || !X509_PUBKEY_get0_param(NULL, NULL, NULL, &algB, b) || algB == NULL)
993 if (X509_ALGOR_cmp(algA, algB) != 0)
995 if ((pA = X509_PUBKEY_get0(a)) == NULL
996 || (pB = X509_PUBKEY_get0(b)) == NULL)
998 return EVP_PKEY_eq(pA, pB);
1001 int ossl_x509_PUBKEY_get0_libctx(OSSL_LIB_CTX **plibctx, const char **ppropq,
1002 const X509_PUBKEY *key)
1005 *plibctx = key->libctx;
1007 *ppropq = key->propq;