Actually use a legacy route in pem_read_bio_key_legacy()
[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 <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"
30
31 struct X509_pubkey_st {
32     X509_ALGOR *algor;
33     ASN1_BIT_STRING *public_key;
34
35     EVP_PKEY *pkey;
36
37     /* extra data for the callback, used by d2i_PUBKEY_ex */
38     OSSL_LIB_CTX *libctx;
39     char *propq;
40
41     /* Flag to force legacy keys */
42     unsigned int flag_force_legacy : 1;
43 };
44
45 static int x509_pubkey_decode(EVP_PKEY **pk, const X509_PUBKEY *key);
46
47 static 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
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)
67
68 static void x509_pubkey_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
69 {
70     X509_PUBKEY *pubkey = (X509_PUBKEY *)*pval;
71
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);
76     OPENSSL_free(pubkey);
77     *pval = NULL;
78 }
79
80 static int x509_pubkey_ex_populate(ASN1_VALUE **pval, const ASN1_ITEM *it)
81 {
82     X509_PUBKEY *pubkey = (X509_PUBKEY *)*pval;
83
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);
88 }
89
90
91 static int x509_pubkey_ex_new_ex(ASN1_VALUE **pval, const ASN1_ITEM *it,
92                                  OSSL_LIB_CTX *libctx, const char *propq)
93 {
94     X509_PUBKEY *ret;
95
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);
101     } else {
102         *pval = (ASN1_VALUE *)ret;
103     }
104
105     return ret != NULL;
106 }
107
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,
112                                  const char *propq)
113 {
114     const unsigned char *in_saved = *in;
115     size_t publen;
116     X509_PUBKEY *pubkey;
117     int ret;
118     OSSL_DECODER_CTX *dctx = NULL;
119     unsigned char *tmpbuf = NULL;
120
121     if (*pval == NULL && !x509_pubkey_ex_new_ex(pval, it, libctx, propq))
122         return 0;
123     if (!x509_pubkey_ex_populate(pval, NULL)) {
124         ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
125         return 0;
126     }
127
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)
132         return ret;
133
134     publen = *in - in_saved;
135     if (!ossl_assert(publen > 0)) {
136         ERR_raise(ERR_LIB_ASN1, ERR_R_INTERNAL_ERROR);
137         return 0;
138     }
139
140     pubkey = (X509_PUBKEY *)*pval;
141     EVP_PKEY_free(pubkey->pkey);
142     pubkey->pkey = NULL;
143
144     /*
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.
148      */
149     ERR_set_mark();
150
151     /*
152      * Try to decode with legacy method first.  This ensures that engines
153      * aren't overriden by providers.
154      */
155     if ((ret = x509_pubkey_decode(&pubkey->pkey, pubkey)) == -1) {
156         /* -1 indicates a fatal error, like malloc failure */
157         ERR_clear_last_mark();
158         goto end;
159     }
160
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;
166
167         /*
168         * The decoders don't know how to handle anything other than Universal
169         * class so we modify the data accordingly.
170         */
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);
175                 return 0;
176             }
177             in_saved = tmpbuf;
178             *tmpbuf = V_ASN1_CONSTRUCTED | V_ASN1_SEQUENCE;
179         }
180         p = in_saved;
181
182         if (OBJ_obj2txt(txtoidname, sizeof(txtoidname),
183                         pubkey->algor->algorithm, 0) <= 0) {
184             ERR_clear_last_mark();
185             goto end;
186         }
187         if ((dctx =
188              OSSL_DECODER_CTX_new_for_pkey(&pubkey->pkey,
189                                            "DER", "SubjectPublicKeyInfo",
190                                            txtoidname, EVP_PKEY_PUBLIC_KEY,
191                                            pubkey->libctx,
192                                            pubkey->propq)) != NULL)
193             /*
194              * As said higher up, we're being opportunistic.  In other words,
195              * we don't care if we fail.
196              */
197             if (OSSL_DECODER_from_data(dctx, &p, &slen)) {
198                 if (slen != 0) {
199                     /*
200                      * If we successfully decoded then we *must* consume all the
201                      * bytes.
202                      */
203                     ERR_clear_last_mark();
204                     ERR_raise(ERR_LIB_ASN1, EVP_R_DECODE_ERROR);
205                     goto end;
206                 }
207             }
208     }
209
210     ERR_pop_to_mark();
211     ret = 1;
212  end:
213     OSSL_DECODER_CTX_free(dctx);
214     OPENSSL_free(tmpbuf);
215     return ret;
216 }
217
218 static int x509_pubkey_ex_i2d(const ASN1_VALUE **pval, unsigned char **out,
219                               const ASN1_ITEM *it, int tag, int aclass)
220 {
221     return ASN1_item_ex_i2d(pval, out, ASN1_ITEM_rptr(X509_PUBKEY_INTERNAL),
222                             tag, aclass);
223 }
224
225 static int x509_pubkey_ex_print(BIO *out, const ASN1_VALUE **pval, int indent,
226                                 const char *fname, const ASN1_PCTX *pctx)
227 {
228     return ASN1_item_print(out, *pval, indent,
229                            ASN1_ITEM_rptr(X509_PUBKEY_INTERNAL), pctx);
230 }
231
232 static const ASN1_EXTERN_FUNCS x509_pubkey_ff = {
233     NULL,
234     NULL,
235     x509_pubkey_ex_free,
236     0,                          /* Default clear behaviour is OK */
237     NULL,
238     x509_pubkey_ex_i2d,
239     x509_pubkey_ex_print,
240     x509_pubkey_ex_new_ex,
241     x509_pubkey_ex_d2i_ex,
242 };
243
244 IMPLEMENT_EXTERN_ASN1(X509_PUBKEY, V_ASN1_SEQUENCE, x509_pubkey_ff)
245 IMPLEMENT_ASN1_FUNCTIONS(X509_PUBKEY)
246
247 X509_PUBKEY *X509_PUBKEY_new_ex(OSSL_LIB_CTX *libctx, const char *propq)
248 {
249     X509_PUBKEY *pubkey = NULL;
250
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);
254         pubkey = NULL;
255     }
256     return pubkey;
257 }
258
259 /*
260  * X509_PUBKEY_dup() must be implemented manually, because there is no
261  * support for it in ASN1_EXTERN_FUNCS.
262  */
263 X509_PUBKEY *X509_PUBKEY_dup(const X509_PUBKEY *a)
264 {
265     X509_PUBKEY *pubkey = OPENSSL_zalloc(sizeof(*pubkey));
266
267     if (pubkey == NULL
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);
277         return NULL;
278     }
279     pubkey->pkey = a->pkey;
280     return pubkey;
281 }
282
283 int X509_PUBKEY_set(X509_PUBKEY **x, EVP_PKEY *pkey)
284 {
285     X509_PUBKEY *pk = NULL;
286
287     if (x == NULL || pkey == NULL) {
288         ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER);
289         return 0;
290     }
291
292     if (pkey->ameth != NULL) {
293         if ((pk = X509_PUBKEY_new()) == NULL) {
294             ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
295             goto error;
296         }
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);
300                 goto error;
301             }
302         } else {
303             ERR_raise(ERR_LIB_X509, X509_R_METHOD_NOT_SUPPORTED);
304             goto error;
305         }
306     } else if (evp_pkey_is_provided(pkey)) {
307         unsigned char *der = NULL;
308         size_t derlen = 0;
309         OSSL_ENCODER_CTX *ectx =
310             OSSL_ENCODER_CTX_new_for_pkey(pkey, EVP_PKEY_PUBLIC_KEY,
311                                           "DER", "SubjectPublicKeyInfo",
312                                           NULL);
313
314         if (OSSL_ENCODER_to_data(ectx, &der, &derlen)) {
315             const unsigned char *pder = der;
316
317             pk = d2i_X509_PUBKEY(NULL, &pder, (long)derlen);
318         }
319
320         OSSL_ENCODER_CTX_free(ectx);
321         OPENSSL_free(der);
322     }
323
324     if (pk == NULL) {
325         ERR_raise(ERR_LIB_X509, X509_R_UNSUPPORTED_ALGORITHM);
326         goto error;
327     }
328
329     X509_PUBKEY_free(*x);
330     if (!EVP_PKEY_up_ref(pkey)) {
331         ERR_raise(ERR_LIB_X509, ERR_R_INTERNAL_ERROR);
332         goto error;
333     }
334     *x = pk;
335
336     /*
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
344      * |pkey|.
345      */
346     if (pk->pkey != NULL)
347         EVP_PKEY_free(pk->pkey);
348
349     pk->pkey = pkey;
350     return 1;
351
352  error:
353     X509_PUBKEY_free(pk);
354     return 0;
355 }
356
357 /*
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.
361  *
362  * This function is #legacy.
363  */
364 static int x509_pubkey_decode(EVP_PKEY **ppkey, const X509_PUBKEY *key)
365 {
366     EVP_PKEY *pkey;
367     int nid;
368
369     nid = OBJ_obj2nid(key->algor->algorithm);
370     if (!key->flag_force_legacy) {
371 #ifndef OPENSSL_NO_ENGINE
372         ENGINE *e = NULL;
373
374         e = ENGINE_get_pkey_meth_engine(nid);
375         if (e == NULL)
376             return 0;
377         ENGINE_finish(e);
378 #else
379         return 0;
380 #endif
381     }
382
383     pkey = EVP_PKEY_new();
384     if (pkey == NULL) {
385         ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
386         return -1;
387     }
388
389     if (!EVP_PKEY_set_type(pkey, nid)) {
390         ERR_raise(ERR_LIB_X509, X509_R_UNSUPPORTED_ALGORITHM);
391         goto error;
392     }
393
394     if (pkey->ameth->pub_decode) {
395         /*
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.
399          */
400         if (!pkey->ameth->pub_decode(pkey, key))
401             goto error;
402     } else {
403         ERR_raise(ERR_LIB_X509, X509_R_METHOD_NOT_SUPPORTED);
404         goto error;
405     }
406
407     *ppkey = pkey;
408     return 1;
409
410  error:
411     EVP_PKEY_free(pkey);
412     return 0;
413 }
414
415 EVP_PKEY *X509_PUBKEY_get0(const X509_PUBKEY *key)
416 {
417     EVP_PKEY *ret = NULL;
418
419     if (key == NULL || key->public_key == NULL)
420         return NULL;
421
422     if (key->pkey != NULL)
423         return key->pkey;
424
425     /*
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
431      * in the queue.
432      */
433     x509_pubkey_decode(&ret, key);
434     /* If decode doesn't fail something bad happened */
435     if (ret != NULL) {
436         ERR_raise(ERR_LIB_X509, ERR_R_INTERNAL_ERROR);
437         EVP_PKEY_free(ret);
438     }
439
440     return NULL;
441 }
442
443 EVP_PKEY *X509_PUBKEY_get(const X509_PUBKEY *key)
444 {
445     EVP_PKEY *ret = X509_PUBKEY_get0(key);
446
447     if (ret != NULL && !EVP_PKEY_up_ref(ret)) {
448         ERR_raise(ERR_LIB_X509, ERR_R_INTERNAL_ERROR);
449         ret = NULL;
450     }
451     return ret;
452 }
453
454 /*
455  * Now three pseudo ASN1 routines that take an EVP_PKEY structure and encode
456  * or decode as X509_PUBKEY
457  */
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,
462                                 X509_PUBKEY *
463                                 (*d2i_x509_pubkey)(X509_PUBKEY **a,
464                                                    const unsigned char **in,
465                                                    long len))
466 {
467     X509_PUBKEY *xpk, *xpk2 = NULL, **pxpk = NULL;
468     EVP_PKEY *pktmp = NULL;
469     const unsigned char *q;
470
471     q = *pp;
472
473     /*
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.
477      */
478     if (libctx != NULL || propq != NULL || force_legacy) {
479         xpk2 = OPENSSL_zalloc(sizeof(*xpk2));
480         if (xpk2 == NULL) {
481             ERR_raise(ERR_LIB_X509, ERR_R_MALLOC_FAILURE);
482             return NULL;
483         }
484         if (!x509_pubkey_set0_libctx(xpk2, libctx, propq))
485             goto end;
486         xpk2->flag_force_legacy = !!force_legacy;
487         pxpk = &xpk2;
488     }
489     xpk = d2i_x509_pubkey(pxpk, &q, length);
490     if (xpk == NULL)
491         goto end;
492     pktmp = X509_PUBKEY_get(xpk);
493     X509_PUBKEY_free(xpk);
494     xpk2 = NULL;                 /* We know that xpk == xpk2 */
495     if (pktmp == NULL)
496         goto end;
497     *pp = q;
498     if (a != NULL) {
499         EVP_PKEY_free(*a);
500         *a = pktmp;
501     }
502  end:
503     X509_PUBKEY_free(xpk2);
504     return pktmp;
505 }
506
507 /* For the algorithm specific d2i functions further down */
508 EVP_PKEY *ossl_d2i_PUBKEY_legacy(EVP_PKEY **a, const unsigned char **pp,
509                                  long length)
510 {
511     return d2i_PUBKEY_int(a, pp, length, NULL, NULL, 1, d2i_X509_PUBKEY);
512 }
513
514 EVP_PKEY *d2i_PUBKEY_ex(EVP_PKEY **a, const unsigned char **pp, long length,
515                         OSSL_LIB_CTX *libctx, const char *propq)
516 {
517     return d2i_PUBKEY_int(a, pp, length, libctx, propq, 0, d2i_X509_PUBKEY);
518 }
519
520 EVP_PKEY *d2i_PUBKEY(EVP_PKEY **a, const unsigned char **pp, long length)
521 {
522     return d2i_PUBKEY_ex(a, pp, length, NULL, NULL);
523 }
524
525 int i2d_PUBKEY(const EVP_PKEY *a, unsigned char **pp)
526 {
527     int ret = -1;
528
529     if (a == NULL)
530         return 0;
531     if (a->ameth != NULL) {
532         X509_PUBKEY *xpk = NULL;
533
534         if ((xpk = X509_PUBKEY_new()) == NULL)
535             return -1;
536
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);
541             xpk->pkey = NULL;
542         }
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",
548                                           NULL);
549         BIO *out = BIO_new(BIO_s_mem());
550         BUF_MEM *buf = NULL;
551
552         if (OSSL_ENCODER_CTX_get_num_encoders(ctx) != 0
553             && out != NULL
554             && OSSL_ENCODER_to_bio(ctx, out)
555             && BIO_get_mem_ptr(out, &buf) > 0) {
556             ret = buf->length;
557
558             if (pp != NULL) {
559                 if (*pp == NULL) {
560                     *pp = (unsigned char *)buf->data;
561                     buf->length = 0;
562                     buf->data = NULL;
563                 } else {
564                     memcpy(*pp, buf->data, ret);
565                     *pp += ret;
566                 }
567             }
568         }
569         BIO_free(out);
570         OSSL_ENCODER_CTX_free(ctx);
571     }
572
573     return ret;
574 }
575
576 /*
577  * The following are equivalents but which return RSA and DSA keys
578  */
579 RSA *d2i_RSA_PUBKEY(RSA **a, const unsigned char **pp, long length)
580 {
581     EVP_PKEY *pkey;
582     RSA *key = NULL;
583     const unsigned char *q;
584
585     q = *pp;
586     pkey = ossl_d2i_PUBKEY_legacy(NULL, &q, length);
587     if (pkey == NULL)
588         return NULL;
589     key = EVP_PKEY_get1_RSA(pkey);
590     EVP_PKEY_free(pkey);
591     if (key == NULL)
592         return NULL;
593     *pp = q;
594     if (a != NULL) {
595         RSA_free(*a);
596         *a = key;
597     }
598     return key;
599 }
600
601 int i2d_RSA_PUBKEY(const RSA *a, unsigned char **pp)
602 {
603     EVP_PKEY *pktmp;
604     int ret;
605     if (!a)
606         return 0;
607     pktmp = EVP_PKEY_new();
608     if (pktmp == NULL) {
609         ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
610         return -1;
611     }
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);
616     return ret;
617 }
618
619 #ifndef OPENSSL_NO_DH
620 DH *ossl_d2i_DH_PUBKEY(DH **a, const unsigned char **pp, long length)
621 {
622     EVP_PKEY *pkey;
623     DH *key = NULL;
624     const unsigned char *q;
625
626     q = *pp;
627     pkey = ossl_d2i_PUBKEY_legacy(NULL, &q, length);
628     if (pkey == NULL)
629         return NULL;
630     if (EVP_PKEY_get_id(pkey) == EVP_PKEY_DH)
631         key = EVP_PKEY_get1_DH(pkey);
632     EVP_PKEY_free(pkey);
633     if (key == NULL)
634         return NULL;
635     *pp = q;
636     if (a != NULL) {
637         DH_free(*a);
638         *a = key;
639     }
640     return key;
641 }
642
643 int ossl_i2d_DH_PUBKEY(const DH *a, unsigned char **pp)
644 {
645     EVP_PKEY *pktmp;
646     int ret;
647     if (!a)
648         return 0;
649     pktmp = EVP_PKEY_new();
650     if (pktmp == NULL) {
651         ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
652         return -1;
653     }
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);
658     return ret;
659 }
660
661 DH *ossl_d2i_DHx_PUBKEY(DH **a, const unsigned char **pp, long length)
662 {
663     EVP_PKEY *pkey;
664     DH *key = NULL;
665     const unsigned char *q;
666
667     q = *pp;
668     pkey = ossl_d2i_PUBKEY_legacy(NULL, &q, length);
669     if (pkey == NULL)
670         return NULL;
671     if (EVP_PKEY_get_id(pkey) == EVP_PKEY_DHX)
672         key = EVP_PKEY_get1_DH(pkey);
673     EVP_PKEY_free(pkey);
674     if (key == NULL)
675         return NULL;
676     *pp = q;
677     if (a != NULL) {
678         DH_free(*a);
679         *a = key;
680     }
681     return key;
682 }
683
684 int ossl_i2d_DHx_PUBKEY(const DH *a, unsigned char **pp)
685 {
686     EVP_PKEY *pktmp;
687     int ret;
688     if (!a)
689         return 0;
690     pktmp = EVP_PKEY_new();
691     if (pktmp == NULL) {
692         ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
693         return -1;
694     }
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);
699     return ret;
700 }
701 #endif
702
703 #ifndef OPENSSL_NO_DSA
704 DSA *d2i_DSA_PUBKEY(DSA **a, const unsigned char **pp, long length)
705 {
706     EVP_PKEY *pkey;
707     DSA *key = NULL;
708     const unsigned char *q;
709
710     q = *pp;
711     pkey = ossl_d2i_PUBKEY_legacy(NULL, &q, length);
712     if (pkey == NULL)
713         return NULL;
714     key = EVP_PKEY_get1_DSA(pkey);
715     EVP_PKEY_free(pkey);
716     if (key == NULL)
717         return NULL;
718     *pp = q;
719     if (a != NULL) {
720         DSA_free(*a);
721         *a = key;
722     }
723     return key;
724 }
725
726 int i2d_DSA_PUBKEY(const DSA *a, unsigned char **pp)
727 {
728     EVP_PKEY *pktmp;
729     int ret;
730     if (!a)
731         return 0;
732     pktmp = EVP_PKEY_new();
733     if (pktmp == NULL) {
734         ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
735         return -1;
736     }
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);
741     return ret;
742 }
743 #endif
744
745 #ifndef OPENSSL_NO_EC
746 EC_KEY *d2i_EC_PUBKEY(EC_KEY **a, const unsigned char **pp, long length)
747 {
748     EVP_PKEY *pkey;
749     EC_KEY *key = NULL;
750     const unsigned char *q;
751     int type;
752
753     q = *pp;
754     pkey = ossl_d2i_PUBKEY_legacy(NULL, &q, length);
755     if (pkey == NULL)
756         return NULL;
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);
760     EVP_PKEY_free(pkey);
761     if (key == NULL)
762         return NULL;
763     *pp = q;
764     if (a != NULL) {
765         EC_KEY_free(*a);
766         *a = key;
767     }
768     return key;
769 }
770
771 int i2d_EC_PUBKEY(const EC_KEY *a, unsigned char **pp)
772 {
773     EVP_PKEY *pktmp;
774     int ret;
775
776     if (a == NULL)
777         return 0;
778     if ((pktmp = EVP_PKEY_new()) == NULL) {
779         ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
780         return -1;
781     }
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);
786     return ret;
787 }
788
789 ECX_KEY *ossl_d2i_ED25519_PUBKEY(ECX_KEY **a,
790                                  const unsigned char **pp, long length)
791 {
792     EVP_PKEY *pkey;
793     ECX_KEY *key = NULL;
794     const unsigned char *q;
795
796     q = *pp;
797     pkey = ossl_d2i_PUBKEY_legacy(NULL, &q, length);
798     if (pkey == NULL)
799         return NULL;
800     key = ossl_evp_pkey_get1_ED25519(pkey);
801     EVP_PKEY_free(pkey);
802     if (key == NULL)
803         return NULL;
804     *pp = q;
805     if (a != NULL) {
806         ossl_ecx_key_free(*a);
807         *a = key;
808     }
809     return key;
810 }
811
812 int ossl_i2d_ED25519_PUBKEY(const ECX_KEY *a, unsigned char **pp)
813 {
814     EVP_PKEY *pktmp;
815     int ret;
816
817     if (a == NULL)
818         return 0;
819     if ((pktmp = EVP_PKEY_new()) == NULL) {
820         ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
821         return -1;
822     }
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);
827     return ret;
828 }
829
830 ECX_KEY *ossl_d2i_ED448_PUBKEY(ECX_KEY **a,
831                                const unsigned char **pp, long length)
832 {
833     EVP_PKEY *pkey;
834     ECX_KEY *key = NULL;
835     const unsigned char *q;
836
837     q = *pp;
838     pkey = ossl_d2i_PUBKEY_legacy(NULL, &q, length);
839     if (pkey == NULL)
840         return NULL;
841     if (EVP_PKEY_get_id(pkey) == EVP_PKEY_ED448)
842         key = ossl_evp_pkey_get1_ED448(pkey);
843     EVP_PKEY_free(pkey);
844     if (key == NULL)
845         return NULL;
846     *pp = q;
847     if (a != NULL) {
848         ossl_ecx_key_free(*a);
849         *a = key;
850     }
851     return key;
852 }
853
854 int ossl_i2d_ED448_PUBKEY(const ECX_KEY *a, unsigned char **pp)
855 {
856     EVP_PKEY *pktmp;
857     int ret;
858
859     if (a == NULL)
860         return 0;
861     if ((pktmp = EVP_PKEY_new()) == NULL) {
862         ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
863         return -1;
864     }
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);
869     return ret;
870 }
871
872 ECX_KEY *ossl_d2i_X25519_PUBKEY(ECX_KEY **a,
873                                 const unsigned char **pp, long length)
874 {
875     EVP_PKEY *pkey;
876     ECX_KEY *key = NULL;
877     const unsigned char *q;
878
879     q = *pp;
880     pkey = ossl_d2i_PUBKEY_legacy(NULL, &q, length);
881     if (pkey == NULL)
882         return NULL;
883     if (EVP_PKEY_get_id(pkey) == EVP_PKEY_X25519)
884         key = ossl_evp_pkey_get1_X25519(pkey);
885     EVP_PKEY_free(pkey);
886     if (key == NULL)
887         return NULL;
888     *pp = q;
889     if (a != NULL) {
890         ossl_ecx_key_free(*a);
891         *a = key;
892     }
893     return key;
894 }
895
896 int ossl_i2d_X25519_PUBKEY(const ECX_KEY *a, unsigned char **pp)
897 {
898     EVP_PKEY *pktmp;
899     int ret;
900
901     if (a == NULL)
902         return 0;
903     if ((pktmp = EVP_PKEY_new()) == NULL) {
904         ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
905         return -1;
906     }
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);
911     return ret;
912 }
913
914 ECX_KEY *ossl_d2i_X448_PUBKEY(ECX_KEY **a,
915                               const unsigned char **pp, long length)
916 {
917     EVP_PKEY *pkey;
918     ECX_KEY *key = NULL;
919     const unsigned char *q;
920
921     q = *pp;
922     pkey = ossl_d2i_PUBKEY_legacy(NULL, &q, length);
923     if (pkey == NULL)
924         return NULL;
925     if (EVP_PKEY_get_id(pkey) == EVP_PKEY_X448)
926         key = ossl_evp_pkey_get1_X448(pkey);
927     EVP_PKEY_free(pkey);
928     if (key == NULL)
929         return NULL;
930     *pp = q;
931     if (a != NULL) {
932         ossl_ecx_key_free(*a);
933         *a = key;
934     }
935     return key;
936 }
937
938 int ossl_i2d_X448_PUBKEY(const ECX_KEY *a, unsigned char **pp)
939 {
940     EVP_PKEY *pktmp;
941     int ret;
942
943     if (a == NULL)
944         return 0;
945     if ((pktmp = EVP_PKEY_new()) == NULL) {
946         ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
947         return -1;
948     }
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);
953     return ret;
954 }
955
956 #endif
957
958 int X509_PUBKEY_set0_param(X509_PUBKEY *pub, ASN1_OBJECT *aobj,
959                            int ptype, void *pval,
960                            unsigned char *penc, int penclen)
961 {
962     if (!X509_ALGOR_set0(pub->algor, aobj, ptype, pval))
963         return 0;
964     if (penc) {
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;
971     }
972     return 1;
973 }
974
975 int X509_PUBKEY_get0_param(ASN1_OBJECT **ppkalg,
976                            const unsigned char **pk, int *ppklen,
977                            X509_ALGOR **pa, const X509_PUBKEY *pub)
978 {
979     if (ppkalg)
980         *ppkalg = pub->algor->algorithm;
981     if (pk) {
982         *pk = pub->public_key->data;
983         *ppklen = pub->public_key->length;
984     }
985     if (pa)
986         *pa = pub->algor;
987     return 1;
988 }
989
990 ASN1_BIT_STRING *X509_get0_pubkey_bitstr(const X509 *x)
991 {
992     if (x == NULL)
993         return NULL;
994     return x->cert_info.key->public_key;
995 }
996
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)
999 {
1000     X509_ALGOR *algA, *algB;
1001     EVP_PKEY *pA, *pB;
1002
1003     if (a == b)
1004         return 1;
1005     if (a == NULL || b == NULL)
1006         return 0;
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)
1009         return -2;
1010     if (X509_ALGOR_cmp(algA, algB) != 0)
1011         return 0;
1012     if ((pA = X509_PUBKEY_get0(a)) == NULL
1013         || (pB = X509_PUBKEY_get0(b)) == NULL)
1014         return -2;
1015     return EVP_PKEY_eq(pA, pB);
1016 }
1017
1018 int ossl_x509_PUBKEY_get0_libctx(OSSL_LIB_CTX **plibctx, const char **ppropq,
1019                                  const X509_PUBKEY *key)
1020 {
1021     if (plibctx)
1022         *plibctx = key->libctx;
1023     if (ppropq)
1024         *ppropq = key->propq;
1025     return 1;
1026 }