Use the right class/tag when decoding an embedded key
[openssl.git] / crypto / x509 / x_pubkey.c
1 /*
2  * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
3  *
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
8  */
9
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
16 #include <stdio.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"
29
30 struct X509_pubkey_st {
31     X509_ALGOR *algor;
32     ASN1_BIT_STRING *public_key;
33
34     EVP_PKEY *pkey;
35
36     /* extra data for the callback, used by d2i_PUBKEY_ex */
37     OSSL_LIB_CTX *libctx;
38     char *propq;
39
40     /* Flag to force legacy keys */
41     unsigned int flag_force_legacy : 1;
42 };
43
44 static int x509_pubkey_decode(EVP_PKEY **pk, const X509_PUBKEY *key);
45
46 static int x509_pubkey_set0_libctx(X509_PUBKEY *x, OSSL_LIB_CTX *libctx,
47                                    const char *propq)
48 {
49     if (x != NULL) {
50         x->libctx = libctx;
51         OPENSSL_free(x->propq);
52         x->propq = NULL;
53         if (propq != NULL) {
54             x->propq = OPENSSL_strdup(propq);
55             if (x->propq == NULL)
56                 return 0;
57         }
58     }
59     return 1;
60 }
61
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)
66
67 static void x509_pubkey_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
68 {
69     X509_PUBKEY *pubkey = (X509_PUBKEY *)*pval;
70
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);
75     OPENSSL_free(pubkey);
76     *pval = NULL;
77 }
78
79 static int x509_pubkey_ex_populate(ASN1_VALUE **pval, const ASN1_ITEM *it)
80 {
81     X509_PUBKEY *pubkey = (X509_PUBKEY *)*pval;
82
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);
87 }
88
89
90 static int x509_pubkey_ex_new_ex(ASN1_VALUE **pval, const ASN1_ITEM *it,
91                                  OSSL_LIB_CTX *libctx, const char *propq)
92 {
93     X509_PUBKEY *ret;
94
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);
100     } else {
101         *pval = (ASN1_VALUE *)ret;
102     }
103
104     return ret != NULL;
105 }
106
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,
111                                  const char *propq)
112 {
113     const unsigned char *in_saved = *in;
114     size_t publen;
115     X509_PUBKEY *pubkey;
116     int ret;
117     OSSL_DECODER_CTX *dctx = NULL;
118     unsigned char *tmpbuf = NULL;
119
120     if (*pval == NULL && !x509_pubkey_ex_new_ex(pval, it, libctx, propq))
121         return 0;
122     if (!x509_pubkey_ex_populate(pval, NULL)) {
123         ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
124         return 0;
125     }
126
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)
131         return ret;
132
133     publen = *in - in_saved;
134     if (!ossl_assert(publen > 0)) {
135         ERR_raise(ERR_LIB_ASN1, ERR_R_INTERNAL_ERROR);
136         return 0;
137     }
138
139     pubkey = (X509_PUBKEY *)*pval;
140     EVP_PKEY_free(pubkey->pkey);
141     pubkey->pkey = NULL;
142
143     /*
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.
147      */
148     ERR_set_mark();
149
150     /*
151      * Try to decode with legacy method first.  This ensures that engines
152      * aren't overriden by providers.
153      */
154     if ((ret = x509_pubkey_decode(&pubkey->pkey, pubkey)) == -1) {
155         /* -1 indicates a fatal error, like malloc failure */
156         ERR_clear_last_mark();
157         goto end;
158     }
159
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
165         /*
166         * The decoders don't know how to handle anything other than Universal
167         * class so we modify the data accordingly.
168         */
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);
173                 return 0;
174             }
175             in_saved = tmpbuf;
176             *tmpbuf = V_ASN1_CONSTRUCTED | V_ASN1_SEQUENCE;
177         }
178         p = in_saved;
179
180         if (OBJ_obj2txt(txtoidname, sizeof(txtoidname),
181                         pubkey->algor->algorithm, 0) <= 0) {
182             ERR_clear_last_mark();
183             goto end;
184         }
185         if ((dctx =
186              OSSL_DECODER_CTX_new_for_pkey(&pubkey->pkey,
187                                            "DER", "SubjectPublicKeyInfo",
188                                            txtoidname, EVP_PKEY_PUBLIC_KEY,
189                                            pubkey->libctx,
190                                            pubkey->propq)) != NULL)
191             /*
192              * As said higher up, we're being opportunistic.  In other words,
193              * we don't care about what the return value signals.
194              */
195             OSSL_DECODER_from_data(dctx, &p, NULL);
196     }
197
198     ERR_pop_to_mark();
199     ret = 1;
200  end:
201     OSSL_DECODER_CTX_free(dctx);
202     OPENSSL_free(tmpbuf);
203     return ret;
204 }
205
206 static int x509_pubkey_ex_i2d(const ASN1_VALUE **pval, unsigned char **out,
207                               const ASN1_ITEM *it, int tag, int aclass)
208 {
209     return ASN1_item_ex_i2d(pval, out, ASN1_ITEM_rptr(X509_PUBKEY_INTERNAL),
210                             tag, aclass);
211 }
212
213 static int x509_pubkey_ex_print(BIO *out, const ASN1_VALUE **pval, int indent,
214                                 const char *fname, const ASN1_PCTX *pctx)
215 {
216     return ASN1_item_print(out, *pval, indent,
217                            ASN1_ITEM_rptr(X509_PUBKEY_INTERNAL), pctx);
218 }
219
220 static const ASN1_EXTERN_FUNCS x509_pubkey_ff = {
221     NULL,
222     NULL,
223     x509_pubkey_ex_free,
224     0,                          /* Default clear behaviour is OK */
225     NULL,
226     x509_pubkey_ex_i2d,
227     x509_pubkey_ex_print,
228     x509_pubkey_ex_new_ex,
229     x509_pubkey_ex_d2i_ex,
230 };
231
232 IMPLEMENT_EXTERN_ASN1(X509_PUBKEY, V_ASN1_SEQUENCE, x509_pubkey_ff)
233 IMPLEMENT_ASN1_FUNCTIONS(X509_PUBKEY)
234
235 X509_PUBKEY *X509_PUBKEY_new_ex(OSSL_LIB_CTX *libctx, const char *propq)
236 {
237     X509_PUBKEY *pubkey = NULL;
238
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);
242         pubkey = NULL;
243     }
244     return pubkey;
245 }
246
247 /*
248  * X509_PUBKEY_dup() must be implemented manually, because there is no
249  * support for it in ASN1_EXTERN_FUNCS.
250  */
251 X509_PUBKEY *X509_PUBKEY_dup(const X509_PUBKEY *a)
252 {
253     X509_PUBKEY *pubkey = OPENSSL_zalloc(sizeof(*pubkey));
254
255     if (pubkey == NULL
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);
265         return NULL;
266     }
267     pubkey->pkey = a->pkey;
268     return pubkey;
269 }
270
271 int X509_PUBKEY_set(X509_PUBKEY **x, EVP_PKEY *pkey)
272 {
273     X509_PUBKEY *pk = NULL;
274
275     if (x == NULL || pkey == NULL) {
276         ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER);
277         return 0;
278     }
279
280     if (pkey->ameth != NULL) {
281         if ((pk = X509_PUBKEY_new()) == NULL) {
282             ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
283             goto error;
284         }
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);
288                 goto error;
289             }
290         } else {
291             ERR_raise(ERR_LIB_X509, X509_R_METHOD_NOT_SUPPORTED);
292             goto error;
293         }
294     } else if (evp_pkey_is_provided(pkey)) {
295         unsigned char *der = NULL;
296         size_t derlen = 0;
297         OSSL_ENCODER_CTX *ectx =
298             OSSL_ENCODER_CTX_new_for_pkey(pkey, EVP_PKEY_PUBLIC_KEY,
299                                           "DER", "SubjectPublicKeyInfo",
300                                           NULL);
301
302         if (OSSL_ENCODER_to_data(ectx, &der, &derlen)) {
303             const unsigned char *pder = der;
304
305             pk = d2i_X509_PUBKEY(NULL, &pder, (long)derlen);
306         }
307
308         OSSL_ENCODER_CTX_free(ectx);
309         OPENSSL_free(der);
310     }
311
312     if (pk == NULL) {
313         ERR_raise(ERR_LIB_X509, X509_R_UNSUPPORTED_ALGORITHM);
314         goto error;
315     }
316
317     X509_PUBKEY_free(*x);
318     if (!EVP_PKEY_up_ref(pkey)) {
319         ERR_raise(ERR_LIB_X509, ERR_R_INTERNAL_ERROR);
320         goto error;
321     }
322     *x = pk;
323
324     /*
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
332      * |pkey|.
333      */
334     if (pk->pkey != NULL)
335         EVP_PKEY_free(pk->pkey);
336
337     pk->pkey = pkey;
338     return 1;
339
340  error:
341     X509_PUBKEY_free(pk);
342     return 0;
343 }
344
345 /*
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.
349  *
350  * This function is #legacy.
351  */
352 static int x509_pubkey_decode(EVP_PKEY **ppkey, const X509_PUBKEY *key)
353 {
354     EVP_PKEY *pkey = EVP_PKEY_new();
355
356     if (pkey == NULL) {
357         ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
358         return -1;
359     }
360
361     if (!EVP_PKEY_set_type(pkey, OBJ_obj2nid(key->algor->algorithm))) {
362         ERR_raise(ERR_LIB_X509, X509_R_UNSUPPORTED_ALGORITHM);
363         goto error;
364     }
365
366     if (pkey->ameth->pub_decode) {
367         /*
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.
371          */
372         if (!pkey->ameth->pub_decode(pkey, key))
373             goto error;
374     } else {
375         ERR_raise(ERR_LIB_X509, X509_R_METHOD_NOT_SUPPORTED);
376         goto error;
377     }
378
379     *ppkey = pkey;
380     return 1;
381
382  error:
383     EVP_PKEY_free(pkey);
384     return 0;
385 }
386
387 EVP_PKEY *X509_PUBKEY_get0(const X509_PUBKEY *key)
388 {
389     EVP_PKEY *ret = NULL;
390
391     if (key == NULL || key->public_key == NULL)
392         return NULL;
393
394     if (key->pkey != NULL)
395         return key->pkey;
396
397     /*
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
403      * in the queue.
404      */
405     x509_pubkey_decode(&ret, key);
406     /* If decode doesn't fail something bad happened */
407     if (ret != NULL) {
408         ERR_raise(ERR_LIB_X509, ERR_R_INTERNAL_ERROR);
409         EVP_PKEY_free(ret);
410     }
411
412     return NULL;
413 }
414
415 EVP_PKEY *X509_PUBKEY_get(const X509_PUBKEY *key)
416 {
417     EVP_PKEY *ret = X509_PUBKEY_get0(key);
418
419     if (ret != NULL && !EVP_PKEY_up_ref(ret)) {
420         ERR_raise(ERR_LIB_X509, ERR_R_INTERNAL_ERROR);
421         ret = NULL;
422     }
423     return ret;
424 }
425
426 /*
427  * Now three pseudo ASN1 routines that take an EVP_PKEY structure and encode
428  * or decode as X509_PUBKEY
429  */
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,
434                                 X509_PUBKEY *
435                                 (*d2i_x509_pubkey)(X509_PUBKEY **a,
436                                                    const unsigned char **in,
437                                                    long len))
438 {
439     X509_PUBKEY *xpk, *xpk2 = NULL, **pxpk = NULL;
440     EVP_PKEY *pktmp = NULL;
441     const unsigned char *q;
442
443     q = *pp;
444
445     /*
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.
449      */
450     if (libctx != NULL || propq != NULL || force_legacy) {
451         xpk2 = OPENSSL_zalloc(sizeof(*xpk2));
452         if (xpk2 == NULL) {
453             ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
454             return NULL;
455         }
456         if (!x509_pubkey_set0_libctx(xpk2, libctx, propq))
457             goto end;
458         xpk2->flag_force_legacy = !!force_legacy;
459         pxpk = &xpk2;
460     }
461     xpk = d2i_x509_pubkey(pxpk, &q, length);
462     if (xpk == NULL)
463         goto end;
464     pktmp = X509_PUBKEY_get(xpk);
465     X509_PUBKEY_free(xpk);
466     xpk2 = NULL;                 /* We know that xpk == xpk2 */
467     if (pktmp == NULL)
468         goto end;
469     *pp = q;
470     if (a != NULL) {
471         EVP_PKEY_free(*a);
472         *a = pktmp;
473     }
474  end:
475     X509_PUBKEY_free(xpk2);
476     return pktmp;
477 }
478
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)
482 {
483     return d2i_PUBKEY_int(a, pp, length, NULL, NULL, 1, d2i_X509_PUBKEY);
484 }
485
486 EVP_PKEY *d2i_PUBKEY_ex(EVP_PKEY **a, const unsigned char **pp, long length,
487                         OSSL_LIB_CTX *libctx, const char *propq)
488 {
489     return d2i_PUBKEY_int(a, pp, length, libctx, propq, 0, d2i_X509_PUBKEY);
490 }
491
492 EVP_PKEY *d2i_PUBKEY(EVP_PKEY **a, const unsigned char **pp, long length)
493 {
494     return d2i_PUBKEY_ex(a, pp, length, NULL, NULL);
495 }
496
497 int i2d_PUBKEY(const EVP_PKEY *a, unsigned char **pp)
498 {
499     int ret = -1;
500
501     if (a == NULL)
502         return 0;
503     if (a->ameth != NULL) {
504         X509_PUBKEY *xpk = NULL;
505
506         if ((xpk = X509_PUBKEY_new()) == NULL)
507             return -1;
508
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);
513             xpk->pkey = NULL;
514         }
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",
520                                           NULL);
521         BIO *out = BIO_new(BIO_s_mem());
522         BUF_MEM *buf = NULL;
523
524         if (OSSL_ENCODER_CTX_get_num_encoders(ctx) != 0
525             && out != NULL
526             && OSSL_ENCODER_to_bio(ctx, out)
527             && BIO_get_mem_ptr(out, &buf) > 0) {
528             ret = buf->length;
529
530             if (pp != NULL) {
531                 if (*pp == NULL) {
532                     *pp = (unsigned char *)buf->data;
533                     buf->length = 0;
534                     buf->data = NULL;
535                 } else {
536                     memcpy(*pp, buf->data, ret);
537                     *pp += ret;
538                 }
539             }
540         }
541         BIO_free(out);
542         OSSL_ENCODER_CTX_free(ctx);
543     }
544
545     return ret;
546 }
547
548 /*
549  * The following are equivalents but which return RSA and DSA keys
550  */
551 RSA *d2i_RSA_PUBKEY(RSA **a, const unsigned char **pp, long length)
552 {
553     EVP_PKEY *pkey;
554     RSA *key = NULL;
555     const unsigned char *q;
556
557     q = *pp;
558     pkey = d2i_PUBKEY_legacy(NULL, &q, length);
559     if (pkey == NULL)
560         return NULL;
561     key = EVP_PKEY_get1_RSA(pkey);
562     EVP_PKEY_free(pkey);
563     if (key == NULL)
564         return NULL;
565     *pp = q;
566     if (a != NULL) {
567         RSA_free(*a);
568         *a = key;
569     }
570     return key;
571 }
572
573 int i2d_RSA_PUBKEY(const RSA *a, unsigned char **pp)
574 {
575     EVP_PKEY *pktmp;
576     int ret;
577     if (!a)
578         return 0;
579     pktmp = EVP_PKEY_new();
580     if (pktmp == NULL) {
581         ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
582         return -1;
583     }
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);
588     return ret;
589 }
590
591 #ifndef OPENSSL_NO_DH
592 DH *ossl_d2i_DH_PUBKEY(DH **a, const unsigned char **pp, long length)
593 {
594     EVP_PKEY *pkey;
595     DH *key = NULL;
596     const unsigned char *q;
597
598     q = *pp;
599     pkey = d2i_PUBKEY_legacy(NULL, &q, length);
600     if (pkey == NULL)
601         return NULL;
602     if (EVP_PKEY_get_id(pkey) == EVP_PKEY_DH)
603         key = EVP_PKEY_get1_DH(pkey);
604     EVP_PKEY_free(pkey);
605     if (key == NULL)
606         return NULL;
607     *pp = q;
608     if (a != NULL) {
609         DH_free(*a);
610         *a = key;
611     }
612     return key;
613 }
614
615 int ossl_i2d_DH_PUBKEY(const DH *a, unsigned char **pp)
616 {
617     EVP_PKEY *pktmp;
618     int ret;
619     if (!a)
620         return 0;
621     pktmp = EVP_PKEY_new();
622     if (pktmp == NULL) {
623         ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
624         return -1;
625     }
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);
630     return ret;
631 }
632
633 DH *ossl_d2i_DHx_PUBKEY(DH **a, const unsigned char **pp, long length)
634 {
635     EVP_PKEY *pkey;
636     DH *key = NULL;
637     const unsigned char *q;
638
639     q = *pp;
640     pkey = d2i_PUBKEY_legacy(NULL, &q, length);
641     if (pkey == NULL)
642         return NULL;
643     if (EVP_PKEY_get_id(pkey) == EVP_PKEY_DHX)
644         key = EVP_PKEY_get1_DH(pkey);
645     EVP_PKEY_free(pkey);
646     if (key == NULL)
647         return NULL;
648     *pp = q;
649     if (a != NULL) {
650         DH_free(*a);
651         *a = key;
652     }
653     return key;
654 }
655
656 int ossl_i2d_DHx_PUBKEY(const DH *a, unsigned char **pp)
657 {
658     EVP_PKEY *pktmp;
659     int ret;
660     if (!a)
661         return 0;
662     pktmp = EVP_PKEY_new();
663     if (pktmp == NULL) {
664         ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
665         return -1;
666     }
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);
671     return ret;
672 }
673 #endif
674
675 #ifndef OPENSSL_NO_DSA
676 DSA *d2i_DSA_PUBKEY(DSA **a, const unsigned char **pp, long length)
677 {
678     EVP_PKEY *pkey;
679     DSA *key = NULL;
680     const unsigned char *q;
681
682     q = *pp;
683     pkey = d2i_PUBKEY_legacy(NULL, &q, length);
684     if (pkey == NULL)
685         return NULL;
686     key = EVP_PKEY_get1_DSA(pkey);
687     EVP_PKEY_free(pkey);
688     if (key == NULL)
689         return NULL;
690     *pp = q;
691     if (a != NULL) {
692         DSA_free(*a);
693         *a = key;
694     }
695     return key;
696 }
697
698 int i2d_DSA_PUBKEY(const DSA *a, unsigned char **pp)
699 {
700     EVP_PKEY *pktmp;
701     int ret;
702     if (!a)
703         return 0;
704     pktmp = EVP_PKEY_new();
705     if (pktmp == NULL) {
706         ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
707         return -1;
708     }
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);
713     return ret;
714 }
715 #endif
716
717 #ifndef OPENSSL_NO_EC
718 EC_KEY *d2i_EC_PUBKEY(EC_KEY **a, const unsigned char **pp, long length)
719 {
720     EVP_PKEY *pkey;
721     EC_KEY *key = NULL;
722     const unsigned char *q;
723     int type;
724
725     q = *pp;
726     pkey = d2i_PUBKEY_legacy(NULL, &q, length);
727     if (pkey == NULL)
728         return NULL;
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);
732     EVP_PKEY_free(pkey);
733     if (key == NULL)
734         return NULL;
735     *pp = q;
736     if (a != NULL) {
737         EC_KEY_free(*a);
738         *a = key;
739     }
740     return key;
741 }
742
743 int i2d_EC_PUBKEY(const EC_KEY *a, unsigned char **pp)
744 {
745     EVP_PKEY *pktmp;
746     int ret;
747
748     if (a == NULL)
749         return 0;
750     if ((pktmp = EVP_PKEY_new()) == NULL) {
751         ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
752         return -1;
753     }
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);
758     return ret;
759 }
760
761 ECX_KEY *ossl_d2i_ED25519_PUBKEY(ECX_KEY **a,
762                                  const unsigned char **pp, long length)
763 {
764     EVP_PKEY *pkey;
765     ECX_KEY *key = NULL;
766     const unsigned char *q;
767
768     q = *pp;
769     pkey = d2i_PUBKEY_legacy(NULL, &q, length);
770     if (pkey == NULL)
771         return NULL;
772     key = ossl_evp_pkey_get1_ED25519(pkey);
773     EVP_PKEY_free(pkey);
774     if (key == NULL)
775         return NULL;
776     *pp = q;
777     if (a != NULL) {
778         ossl_ecx_key_free(*a);
779         *a = key;
780     }
781     return key;
782 }
783
784 int ossl_i2d_ED25519_PUBKEY(const ECX_KEY *a, unsigned char **pp)
785 {
786     EVP_PKEY *pktmp;
787     int ret;
788
789     if (a == NULL)
790         return 0;
791     if ((pktmp = EVP_PKEY_new()) == NULL) {
792         ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
793         return -1;
794     }
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);
799     return ret;
800 }
801
802 ECX_KEY *ossl_d2i_ED448_PUBKEY(ECX_KEY **a,
803                                const unsigned char **pp, long length)
804 {
805     EVP_PKEY *pkey;
806     ECX_KEY *key = NULL;
807     const unsigned char *q;
808
809     q = *pp;
810     pkey = d2i_PUBKEY_legacy(NULL, &q, length);
811     if (pkey == NULL)
812         return NULL;
813     if (EVP_PKEY_get_id(pkey) == EVP_PKEY_ED448)
814         key = ossl_evp_pkey_get1_ED448(pkey);
815     EVP_PKEY_free(pkey);
816     if (key == NULL)
817         return NULL;
818     *pp = q;
819     if (a != NULL) {
820         ossl_ecx_key_free(*a);
821         *a = key;
822     }
823     return key;
824 }
825
826 int ossl_i2d_ED448_PUBKEY(const ECX_KEY *a, unsigned char **pp)
827 {
828     EVP_PKEY *pktmp;
829     int ret;
830
831     if (a == NULL)
832         return 0;
833     if ((pktmp = EVP_PKEY_new()) == NULL) {
834         ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
835         return -1;
836     }
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);
841     return ret;
842 }
843
844 ECX_KEY *ossl_d2i_X25519_PUBKEY(ECX_KEY **a,
845                                 const unsigned char **pp, long length)
846 {
847     EVP_PKEY *pkey;
848     ECX_KEY *key = NULL;
849     const unsigned char *q;
850
851     q = *pp;
852     pkey = d2i_PUBKEY_legacy(NULL, &q, length);
853     if (pkey == NULL)
854         return NULL;
855     if (EVP_PKEY_get_id(pkey) == EVP_PKEY_X25519)
856         key = ossl_evp_pkey_get1_X25519(pkey);
857     EVP_PKEY_free(pkey);
858     if (key == NULL)
859         return NULL;
860     *pp = q;
861     if (a != NULL) {
862         ossl_ecx_key_free(*a);
863         *a = key;
864     }
865     return key;
866 }
867
868 int ossl_i2d_X25519_PUBKEY(const ECX_KEY *a, unsigned char **pp)
869 {
870     EVP_PKEY *pktmp;
871     int ret;
872
873     if (a == NULL)
874         return 0;
875     if ((pktmp = EVP_PKEY_new()) == NULL) {
876         ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
877         return -1;
878     }
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);
883     return ret;
884 }
885
886 ECX_KEY *ossl_d2i_X448_PUBKEY(ECX_KEY **a,
887                               const unsigned char **pp, long length)
888 {
889     EVP_PKEY *pkey;
890     ECX_KEY *key = NULL;
891     const unsigned char *q;
892
893     q = *pp;
894     pkey = d2i_PUBKEY_legacy(NULL, &q, length);
895     if (pkey == NULL)
896         return NULL;
897     if (EVP_PKEY_get_id(pkey) == EVP_PKEY_X448)
898         key = ossl_evp_pkey_get1_X448(pkey);
899     EVP_PKEY_free(pkey);
900     if (key == NULL)
901         return NULL;
902     *pp = q;
903     if (a != NULL) {
904         ossl_ecx_key_free(*a);
905         *a = key;
906     }
907     return key;
908 }
909
910 int ossl_i2d_X448_PUBKEY(const ECX_KEY *a, unsigned char **pp)
911 {
912     EVP_PKEY *pktmp;
913     int ret;
914
915     if (a == NULL)
916         return 0;
917     if ((pktmp = EVP_PKEY_new()) == NULL) {
918         ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
919         return -1;
920     }
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);
925     return ret;
926 }
927
928 #endif
929
930 int X509_PUBKEY_set0_param(X509_PUBKEY *pub, ASN1_OBJECT *aobj,
931                            int ptype, void *pval,
932                            unsigned char *penc, int penclen)
933 {
934     if (!X509_ALGOR_set0(pub->algor, aobj, ptype, pval))
935         return 0;
936     if (penc) {
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;
943     }
944     return 1;
945 }
946
947 int X509_PUBKEY_get0_param(ASN1_OBJECT **ppkalg,
948                            const unsigned char **pk, int *ppklen,
949                            X509_ALGOR **pa, const X509_PUBKEY *pub)
950 {
951     if (ppkalg)
952         *ppkalg = pub->algor->algorithm;
953     if (pk) {
954         *pk = pub->public_key->data;
955         *ppklen = pub->public_key->length;
956     }
957     if (pa)
958         *pa = pub->algor;
959     return 1;
960 }
961
962 ASN1_BIT_STRING *X509_get0_pubkey_bitstr(const X509 *x)
963 {
964     if (x == NULL)
965         return NULL;
966     return x->cert_info.key->public_key;
967 }
968
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)
971 {
972     X509_ALGOR *algA, *algB;
973     EVP_PKEY *pA, *pB;
974
975     if (a == b)
976         return 1;
977     if (a == NULL || b == NULL)
978         return 0;
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)
981         return -2;
982     if (X509_ALGOR_cmp(algA, algB) != 0)
983         return 0;
984     if ((pA = X509_PUBKEY_get0(a)) == NULL
985         || (pB = X509_PUBKEY_get0(b)) == NULL)
986         return -2;
987     return EVP_PKEY_eq(pA, pB);
988 }
989
990 int ossl_x509_PUBKEY_get0_libctx(OSSL_LIB_CTX **plibctx, const char **ppropq,
991                                  const X509_PUBKEY *key)
992 {
993     if (plibctx)
994         *plibctx = key->libctx;
995     if (ppropq)
996         *ppropq = key->propq;
997     return 1;
998 }