File: decode_der2key.c.in

package info (click to toggle)
openssl 3.6.0-2
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 148,104 kB
  • sloc: ansic: 612,658; perl: 248,939; asm: 6,332; sh: 1,755; pascal: 997; python: 648; makefile: 551; lisp: 35; ruby: 16; cpp: 10; sed: 6
file content (1314 lines) | stat: -rw-r--r-- 53,539 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
/*
 * Copyright 2020-2025 The OpenSSL Project Authors. All Rights Reserved.
 *
 * Licensed under the Apache License 2.0 (the "License").  You may not use
 * this file except in compliance with the License.  You can obtain a copy
 * in the file LICENSE in the source distribution or at
 * https://www.openssl.org/source/license.html
 */
{-
use OpenSSL::paramnames qw(produce_param_decoder);
-}

/*
 * low level APIs are deprecated for public use, but still ok for
 * internal use.
 */
#include "internal/deprecated.h"

#include <openssl/byteorder.h>
#include <openssl/core_dispatch.h>
#include <openssl/core_names.h>
#include <openssl/core_object.h>
#include <openssl/crypto.h>
#include <openssl/err.h>
#include <openssl/params.h>
#include <openssl/pem.h>         /* PEM_BUFSIZE and public PEM functions */
#include <openssl/pkcs12.h>
#include <openssl/provider.h>
#include <openssl/x509.h>
#include <openssl/proverr.h>
#include <openssl/asn1t.h>
#include "internal/cryptlib.h"   /* ossl_assert() */
#include "crypto/dh.h"
#include "crypto/dsa.h"
#include "crypto/ec.h"
#include "crypto/evp.h"
#include "crypto/ecx.h"
#include "crypto/rsa.h"
#include "crypto/ml_dsa.h"
#include "crypto/slh_dsa.h"
#include "crypto/x509.h"
#include "crypto/ml_kem.h"
#include "openssl/obj_mac.h"
#include "prov/bio.h"
#include "prov/implementations.h"
#include "prov/endecoder_local.h"
#include "internal/nelem.h"
#include "prov/ml_dsa_codecs.h"
#include "prov/ml_kem_codecs.h"

#ifndef OPENSSL_NO_SLH_DSA
typedef struct {
    ASN1_OBJECT *oid;
} BARE_ALGOR;

typedef struct {
    BARE_ALGOR algor;
    ASN1_BIT_STRING *pubkey;
} BARE_PUBKEY;

ASN1_SEQUENCE(BARE_ALGOR) = {
    ASN1_SIMPLE(BARE_ALGOR, oid, ASN1_OBJECT),
} static_ASN1_SEQUENCE_END(BARE_ALGOR)

ASN1_SEQUENCE(BARE_PUBKEY) = {
    ASN1_EMBED(BARE_PUBKEY, algor, BARE_ALGOR),
    ASN1_SIMPLE(BARE_PUBKEY, pubkey, ASN1_BIT_STRING)
} static_ASN1_SEQUENCE_END(BARE_PUBKEY)
#endif /* OPENSSL_NO_SLH_DSA */

struct der2key_ctx_st;           /* Forward declaration */
typedef int check_key_fn(void *, struct der2key_ctx_st *ctx);
typedef void adjust_key_fn(void *, struct der2key_ctx_st *ctx);
typedef void free_key_fn(void *);
typedef void *d2i_PKCS8_fn(const unsigned char **, long,
                           struct der2key_ctx_st *);
typedef void *d2i_PUBKEY_fn(const unsigned char **, long,
                            struct der2key_ctx_st *);
struct keytype_desc_st {
    const char *keytype_name;
    const OSSL_DISPATCH *fns; /* Keymgmt (to pilfer functions from) */

    /* The input structure name */
    const char *structure_name;

    /*
     * The EVP_PKEY_xxx type macro.  Should be zero for type specific
     * structures, non-zero when the outermost structure is PKCS#8 or
     * SubjectPublicKeyInfo.  This determines which of the function
     * pointers below will be used.
     */
    int evp_type;

    /* The selection mask for OSSL_FUNC_decoder_does_selection() */
    int selection_mask;

    /* For type specific decoders, we use the corresponding d2i */
    d2i_of_void *d2i_private_key; /* From type-specific DER */
    d2i_of_void *d2i_public_key;  /* From type-specific DER */
    d2i_of_void *d2i_key_params;  /* From type-specific DER */
    d2i_PKCS8_fn *d2i_PKCS8;      /* Wrapped in a PrivateKeyInfo */
    d2i_PUBKEY_fn *d2i_PUBKEY;    /* Wrapped in a SubjectPublicKeyInfo */

    /*
     * For any key, we may need to check that the key meets expectations.
     * This is useful when the same functions can decode several variants
     * of a key.
     */
    check_key_fn *check_key;

    /*
     * For any key, we may need to make provider specific adjustments, such
     * as ensure the key carries the correct library context.
     */
    adjust_key_fn *adjust_key;
    /* {type}_free() */
    free_key_fn *free_key;
};

/*
 * Context used for DER to key decoding.
 */
struct der2key_ctx_st {
    PROV_CTX *provctx;
    char propq[OSSL_MAX_PROPQUERY_SIZE];
    const struct keytype_desc_st *desc;
    /* The selection that is passed to der2key_decode() */
    int selection;
    /* Flag used to signal that a failure is fatal */
    unsigned int flag_fatal : 1;
};

typedef void *key_from_pkcs8_t(const PKCS8_PRIV_KEY_INFO *p8inf,
                               OSSL_LIB_CTX *libctx, const char *propq);
static void *der2key_decode_p8(const unsigned char **input_der,
                               long input_der_len, struct der2key_ctx_st *ctx,
                               key_from_pkcs8_t *key_from_pkcs8)
{
    PKCS8_PRIV_KEY_INFO *p8inf = NULL;
    const X509_ALGOR *alg = NULL;
    void *key = NULL;

    if ((p8inf = d2i_PKCS8_PRIV_KEY_INFO(NULL, input_der, input_der_len)) != NULL
        && PKCS8_pkey_get0(NULL, NULL, NULL, &alg, p8inf)
        && (OBJ_obj2nid(alg->algorithm) == ctx->desc->evp_type
            /* Allow decoding sm2 private key with id_ecPublicKey */
            || (OBJ_obj2nid(alg->algorithm) == NID_X9_62_id_ecPublicKey
                && ctx->desc->evp_type == NID_sm2)))
        key = key_from_pkcs8(p8inf, PROV_LIBCTX_OF(ctx->provctx), ctx->propq);
    PKCS8_PRIV_KEY_INFO_free(p8inf);

    return key;
}

/* ---------------------------------------------------------------------- */

static OSSL_FUNC_decoder_freectx_fn der2key_freectx;
static OSSL_FUNC_decoder_decode_fn der2key_decode;
static OSSL_FUNC_decoder_export_object_fn der2key_export_object;
static OSSL_FUNC_decoder_settable_ctx_params_fn der2key_settable_ctx_params;
static OSSL_FUNC_decoder_set_ctx_params_fn der2key_set_ctx_params;

static struct der2key_ctx_st *
der2key_newctx(void *provctx, const struct keytype_desc_st *desc)
{
    struct der2key_ctx_st *ctx = OPENSSL_zalloc(sizeof(*ctx));

    if (ctx != NULL) {
        ctx->provctx = provctx;
        ctx->desc = desc;
    }
    return ctx;
}

{- produce_param_decoder('der2key_set_ctx_params',
                         (['OSSL_DECODER_PARAM_PROPERTIES', 'propq', 'utf8_string'],
                         )); -}

static const OSSL_PARAM *der2key_settable_ctx_params(ossl_unused void *provctx)
{
    return der2key_set_ctx_params_list;
}

static int der2key_set_ctx_params(void *vctx, const OSSL_PARAM params[])
{
    struct der2key_ctx_st *ctx = vctx;
    struct der2key_set_ctx_params_st p;
    char *str;

    if (ctx == NULL || !der2key_set_ctx_params_decoder(params, &p))
        return 0;

    str = ctx->propq;
    if (p.propq != NULL
            && !OSSL_PARAM_get_utf8_string(p.propq, &str, sizeof(ctx->propq)))
        return 0;

    return 1;
}

static void der2key_freectx(void *vctx)
{
    struct der2key_ctx_st *ctx = vctx;

    OPENSSL_free(ctx);
}

static int der2key_check_selection(int selection,
                                   const struct keytype_desc_st *desc)
{
    /*
     * The selections are kinda sorta "levels", i.e. each selection given
     * here is assumed to include those following.
     */
    int checks[] = {
        OSSL_KEYMGMT_SELECT_PRIVATE_KEY,
        OSSL_KEYMGMT_SELECT_PUBLIC_KEY,
        OSSL_KEYMGMT_SELECT_ALL_PARAMETERS
    };
    size_t i;

    /* The decoder implementations made here support guessing */
    if (selection == 0)
        return 1;

    for (i = 0; i < OSSL_NELEM(checks); i++) {
        int check1 = (selection & checks[i]) != 0;
        int check2 = (desc->selection_mask & checks[i]) != 0;

        /*
         * If the caller asked for the currently checked bit(s), return
         * whether the decoder description says it's supported.
         */
        if (check1)
            return check2;
    }

    /* This should be dead code, but just to be safe... */
    return 0;
}

static int der2key_decode(void *vctx, OSSL_CORE_BIO *cin, int selection,
                          OSSL_CALLBACK *data_cb, void *data_cbarg,
                          OSSL_PASSPHRASE_CALLBACK *pw_cb, void *pw_cbarg)
{
    struct der2key_ctx_st *ctx = vctx;
    unsigned char *der = NULL;
    const unsigned char *derp;
    long der_len = 0;
    void *key = NULL;
    int ok = 0;

    ctx->selection = selection;
    /*
     * The caller is allowed to specify 0 as a selection mask, to have the
     * structure and key type guessed.  For type-specific structures, this
     * is not recommended, as some structures are very similar.
     * Note that 0 isn't the same as OSSL_KEYMGMT_SELECT_ALL, as the latter
     * signifies a private key structure, where everything else is assumed
     * to be present as well.
     */
    if (selection == 0)
        selection = ctx->desc->selection_mask;
    if ((selection & ctx->desc->selection_mask) == 0) {
        ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT);
        return 0;
    }

    ok = ossl_read_der(ctx->provctx, cin, &der, &der_len);
    if (!ok)
        goto next;

    ok = 0; /* Assume that we fail */

    ERR_set_mark();
    if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
        derp = der;
        if (ctx->desc->d2i_PKCS8 != NULL) {
            key = ctx->desc->d2i_PKCS8(&derp, der_len, ctx);
            if (ctx->flag_fatal) {
                ERR_clear_last_mark();
                goto end;
            }
        } else if (ctx->desc->d2i_private_key != NULL) {
            key = ctx->desc->d2i_private_key(NULL, &derp, der_len);
        }
        if (key == NULL && ctx->selection != 0) {
            ERR_clear_last_mark();
            goto next;
        }
    }
    if (key == NULL && (selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) {
        derp = der;
        if (ctx->desc->d2i_PUBKEY != NULL)
            key = ctx->desc->d2i_PUBKEY(&derp, der_len, ctx);
        else if (ctx->desc->d2i_public_key != NULL)
            key = ctx->desc->d2i_public_key(NULL, &derp, der_len);
        if (key == NULL && ctx->selection != 0) {
            ERR_clear_last_mark();
            goto next;
        }
    }
    if (key == NULL && (selection & OSSL_KEYMGMT_SELECT_ALL_PARAMETERS) != 0) {
        derp = der;
        if (ctx->desc->d2i_key_params != NULL)
            key = ctx->desc->d2i_key_params(NULL, &derp, der_len);
        if (key == NULL && ctx->selection != 0) {
            ERR_clear_last_mark();
            goto next;
        }
    }
    if (key == NULL)
        ERR_clear_last_mark();
    else
        ERR_pop_to_mark();

    /*
     * Last minute check to see if this was the correct type of key.  This
     * should never lead to a fatal error, i.e. the decoding itself was
     * correct, it was just an unexpected key type.  This is generally for
     * classes of key types that have subtle variants, like RSA-PSS keys as
     * opposed to plain RSA keys.
     */
    if (key != NULL
        && ctx->desc->check_key != NULL
        && !ctx->desc->check_key(key, ctx)) {
        ctx->desc->free_key(key);
        key = NULL;
    }

    if (key != NULL && ctx->desc->adjust_key != NULL)
        ctx->desc->adjust_key(key, ctx);

 next:
    /*
     * Indicated that we successfully decoded something, or not at all.
     * Ending up "empty handed" is not an error.
     */
    ok = 1;

    /*
     * We free memory here so it's not held up during the callback, because
     * we know the process is recursive and the allocated chunks of memory
     * add up.
     */
    OPENSSL_free(der);
    der = NULL;

    if (key != NULL) {
        OSSL_PARAM params[4];
        int object_type = OSSL_OBJECT_PKEY;

        params[0] =
            OSSL_PARAM_construct_int(OSSL_OBJECT_PARAM_TYPE, &object_type);

#ifndef OPENSSL_NO_SM2
        if (strcmp(ctx->desc->keytype_name, "EC") == 0
            && (EC_KEY_get_flags(key) & EC_FLAG_SM2_RANGE) != 0)
            params[1] =
                OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_TYPE,
                                                 "SM2", 0);
        else
#endif
            params[1] =
                OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_TYPE,
                                                 (char *)ctx->desc->keytype_name,
                                                 0);
        /* The address of the key becomes the octet string */
        params[2] =
            OSSL_PARAM_construct_octet_string(OSSL_OBJECT_PARAM_REFERENCE,
                                              &key, sizeof(key));
        params[3] = OSSL_PARAM_construct_end();

        ok = data_cb(params, data_cbarg);
    }

 end:
    ctx->desc->free_key(key);
    OPENSSL_free(der);

    return ok;
}

static int der2key_export_object(void *vctx,
                                 const void *reference, size_t reference_sz,
                                 OSSL_CALLBACK *export_cb, void *export_cbarg)
{
    struct der2key_ctx_st *ctx = vctx;
    OSSL_FUNC_keymgmt_export_fn *export =
        ossl_prov_get_keymgmt_export(ctx->desc->fns);
    void *keydata;

    if (reference_sz == sizeof(keydata) && export != NULL) {
        int selection = ctx->selection;

        if (selection == 0)
            selection = OSSL_KEYMGMT_SELECT_ALL;
        /* The contents of the reference is the address to our object */
        keydata = *(void **)reference;

        return export(keydata, selection, export_cb, export_cbarg);
    }
    return 0;
}

#define D2I_PUBKEY_NOCTX(n, f)                              \
    static void *                                           \
    n##_d2i_PUBKEY(const unsigned char **der, long der_len, \
                   ossl_unused struct der2key_ctx_st *ctx)  \
    {                                                       \
        return f(NULL, der, der_len);                       \
    }

/* ---------------------------------------------------------------------- */

#ifndef OPENSSL_NO_DH
# define dh_evp_type                    EVP_PKEY_DH
# define dh_d2i_private_key             NULL
# define dh_d2i_public_key              NULL
# define dh_d2i_key_params              (d2i_of_void *)d2i_DHparams
# define dh_free                        (free_key_fn *)DH_free
# define dh_check                       NULL

static void *dh_d2i_PKCS8(const unsigned char **der, long der_len,
                          struct der2key_ctx_st *ctx)
{
    return der2key_decode_p8(der, der_len, ctx,
                             (key_from_pkcs8_t *)ossl_dh_key_from_pkcs8);
}

D2I_PUBKEY_NOCTX(dh, ossl_d2i_DH_PUBKEY)
D2I_PUBKEY_NOCTX(dhx, ossl_d2i_DHx_PUBKEY)

static void dh_adjust(void *key, struct der2key_ctx_st *ctx)
{
    ossl_dh_set0_libctx(key, PROV_LIBCTX_OF(ctx->provctx));
}

# define dhx_evp_type                   EVP_PKEY_DHX
# define dhx_d2i_private_key            NULL
# define dhx_d2i_public_key             NULL
# define dhx_d2i_key_params             (d2i_of_void *)d2i_DHxparams
# define dhx_d2i_PKCS8                  dh_d2i_PKCS8
# define dhx_free                       (free_key_fn *)DH_free
# define dhx_check                      NULL
# define dhx_adjust                     dh_adjust
#endif

/* ---------------------------------------------------------------------- */

#ifndef OPENSSL_NO_DSA
# define dsa_evp_type                   EVP_PKEY_DSA
# define dsa_d2i_private_key            (d2i_of_void *)d2i_DSAPrivateKey
# define dsa_d2i_public_key             (d2i_of_void *)d2i_DSAPublicKey
# define dsa_d2i_key_params             (d2i_of_void *)d2i_DSAparams
# define dsa_free                       (free_key_fn *)DSA_free
# define dsa_check                      NULL

static void *dsa_d2i_PKCS8(const unsigned char **der, long der_len,
                           struct der2key_ctx_st *ctx)
{
    return der2key_decode_p8(der, der_len, ctx,
                             (key_from_pkcs8_t *)ossl_dsa_key_from_pkcs8);
}

D2I_PUBKEY_NOCTX(dsa, ossl_d2i_DSA_PUBKEY)

static void dsa_adjust(void *key, struct der2key_ctx_st *ctx)
{
    ossl_dsa_set0_libctx(key, PROV_LIBCTX_OF(ctx->provctx));
}
#endif

/* ---------------------------------------------------------------------- */

#ifndef OPENSSL_NO_EC
# define ec_evp_type                    EVP_PKEY_EC
# define ec_d2i_private_key             (d2i_of_void *)d2i_ECPrivateKey
# define ec_d2i_public_key              NULL
# define ec_d2i_key_params              (d2i_of_void *)d2i_ECParameters
# define ec_free                        (free_key_fn *)EC_KEY_free

static void *ec_d2i_PKCS8(const unsigned char **der, long der_len,
                          struct der2key_ctx_st *ctx)
{
    return der2key_decode_p8(der, der_len, ctx,
                             (key_from_pkcs8_t *)ossl_ec_key_from_pkcs8);
}

D2I_PUBKEY_NOCTX(ec, d2i_EC_PUBKEY)

static int ec_check(void *key, struct der2key_ctx_st *ctx)
{
    /* We're trying to be clever by comparing two truths */
    int ret = 0;
    int sm2 = (EC_KEY_get_flags(key) & EC_FLAG_SM2_RANGE) != 0;

    if (sm2)
        ret = ctx->desc->evp_type == EVP_PKEY_SM2
            || ctx->desc->evp_type == NID_X9_62_id_ecPublicKey;
    else
        ret = ctx->desc->evp_type != EVP_PKEY_SM2;

    return ret;
}

static void ec_adjust(void *key, struct der2key_ctx_st *ctx)
{
    ossl_ec_key_set0_libctx(key, PROV_LIBCTX_OF(ctx->provctx));
}

# ifndef OPENSSL_NO_ECX
/*
 * ED25519, ED448, X25519, X448 only implement PKCS#8 and SubjectPublicKeyInfo,
 * so no d2i functions to be had.
 */

static void *ecx_d2i_PKCS8(const unsigned char **der, long der_len,
                           struct der2key_ctx_st *ctx)
{
    return der2key_decode_p8(der, der_len, ctx,
                             (key_from_pkcs8_t *)ossl_ecx_key_from_pkcs8);
}

D2I_PUBKEY_NOCTX(ed25519, ossl_d2i_ED25519_PUBKEY)
D2I_PUBKEY_NOCTX(ed448, ossl_d2i_ED448_PUBKEY)
D2I_PUBKEY_NOCTX(x25519, ossl_d2i_X25519_PUBKEY)
D2I_PUBKEY_NOCTX(x448, ossl_d2i_X448_PUBKEY)

static void ecx_key_adjust(void *key, struct der2key_ctx_st *ctx)
{
    ossl_ecx_key_set0_libctx(key, PROV_LIBCTX_OF(ctx->provctx));
}

#  define ed25519_evp_type               EVP_PKEY_ED25519
#  define ed25519_d2i_private_key        NULL
#  define ed25519_d2i_public_key         NULL
#  define ed25519_d2i_key_params         NULL
#  define ed25519_d2i_PKCS8              ecx_d2i_PKCS8
#  define ed25519_free                   (free_key_fn *)ossl_ecx_key_free
#  define ed25519_check                  NULL
#  define ed25519_adjust                 ecx_key_adjust

#  define ed448_evp_type                 EVP_PKEY_ED448
#  define ed448_d2i_private_key          NULL
#  define ed448_d2i_public_key           NULL
#  define ed448_d2i_key_params           NULL
#  define ed448_d2i_PKCS8                ecx_d2i_PKCS8
#  define ed448_free                     (free_key_fn *)ossl_ecx_key_free
#  define ed448_check                    NULL
#  define ed448_adjust                   ecx_key_adjust

#  define x25519_evp_type                EVP_PKEY_X25519
#  define x25519_d2i_private_key         NULL
#  define x25519_d2i_public_key          NULL
#  define x25519_d2i_key_params          NULL
#  define x25519_d2i_PKCS8               ecx_d2i_PKCS8
#  define x25519_free                    (free_key_fn *)ossl_ecx_key_free
#  define x25519_check                   NULL
#  define x25519_adjust                  ecx_key_adjust

#  define x448_evp_type                  EVP_PKEY_X448
#  define x448_d2i_private_key           NULL
#  define x448_d2i_public_key            NULL
#  define x448_d2i_key_params            NULL
#  define x448_d2i_PKCS8                 ecx_d2i_PKCS8
#  define x448_free                      (free_key_fn *)ossl_ecx_key_free
#  define x448_check                     NULL
#  define x448_adjust                    ecx_key_adjust
# endif /* OPENSSL_NO_ECX */

# ifndef OPENSSL_NO_SM2
#  define sm2_evp_type                  EVP_PKEY_SM2
#  define sm2_d2i_private_key           (d2i_of_void *)d2i_ECPrivateKey
#  define sm2_d2i_public_key            NULL
#  define sm2_d2i_key_params            (d2i_of_void *)d2i_ECParameters
#  define sm2_d2i_PUBKEY                ec_d2i_PUBKEY
#  define sm2_free                      (free_key_fn *)EC_KEY_free
#  define sm2_check                     ec_check
#  define sm2_adjust                    ec_adjust

static void *sm2_d2i_PKCS8(const unsigned char **der, long der_len,
                           struct der2key_ctx_st *ctx)
{
    return der2key_decode_p8(der, der_len, ctx,
                             (key_from_pkcs8_t *)ossl_ec_key_from_pkcs8);
}
# endif

#endif

/* ---------------------------------------------------------------------- */

#ifndef OPENSSL_NO_ML_KEM
static void *
ml_kem_d2i_PKCS8(const uint8_t **der, long der_len, struct der2key_ctx_st *ctx)
{
    ML_KEM_KEY *key;

    key = ossl_ml_kem_d2i_PKCS8(*der, der_len, ctx->desc->evp_type,
                                ctx->provctx, ctx->propq);
    if (key != NULL)
        *der += der_len;
    return key;
}

static ossl_inline void *
ml_kem_d2i_PUBKEY(const uint8_t **der, long der_len,
                  struct der2key_ctx_st *ctx)
{
    ML_KEM_KEY *key;

    key = ossl_ml_kem_d2i_PUBKEY(*der, der_len, ctx->desc->evp_type,
                                 ctx->provctx, ctx->propq);
    if (key != NULL)
        *der += der_len;
    return key;
}

# define ml_kem_512_evp_type                EVP_PKEY_ML_KEM_512
# define ml_kem_512_d2i_private_key         NULL
# define ml_kem_512_d2i_public_key          NULL
# define ml_kem_512_d2i_key_params          NULL
# define ml_kem_512_d2i_PUBKEY              ml_kem_d2i_PUBKEY
# define ml_kem_512_d2i_PKCS8               ml_kem_d2i_PKCS8
# define ml_kem_512_free                    (free_key_fn *)ossl_ml_kem_key_free
# define ml_kem_512_check                   NULL
# define ml_kem_512_adjust                  NULL

# define ml_kem_768_evp_type                EVP_PKEY_ML_KEM_768
# define ml_kem_768_d2i_private_key         NULL
# define ml_kem_768_d2i_public_key          NULL
# define ml_kem_768_d2i_key_params          NULL
# define ml_kem_768_d2i_PUBKEY              ml_kem_d2i_PUBKEY
# define ml_kem_768_d2i_PKCS8               ml_kem_d2i_PKCS8
# define ml_kem_768_free                    (free_key_fn *)ossl_ml_kem_key_free
# define ml_kem_768_check                   NULL
# define ml_kem_768_adjust                  NULL

# define ml_kem_1024_evp_type               EVP_PKEY_ML_KEM_1024
# define ml_kem_1024_d2i_private_key        NULL
# define ml_kem_1024_d2i_public_key         NULL
# define ml_kem_1024_d2i_PUBKEY             ml_kem_d2i_PUBKEY
# define ml_kem_1024_d2i_PKCS8              ml_kem_d2i_PKCS8
# define ml_kem_1024_d2i_key_params         NULL
# define ml_kem_1024_free                   (free_key_fn *)ossl_ml_kem_key_free
# define ml_kem_1024_check                  NULL
# define ml_kem_1024_adjust                 NULL

#endif

#ifndef OPENSSL_NO_SLH_DSA
static void *
slh_dsa_d2i_PKCS8(const uint8_t **der, long der_len, struct der2key_ctx_st *ctx)
{
    SLH_DSA_KEY *key = NULL, *ret = NULL;
    OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(ctx->provctx);
    PKCS8_PRIV_KEY_INFO *p8inf = NULL;
    const unsigned char *p;
    const X509_ALGOR *alg = NULL;
    int plen, ptype;

    if ((p8inf = d2i_PKCS8_PRIV_KEY_INFO(NULL, der, der_len)) == NULL
        || !PKCS8_pkey_get0(NULL, &p, &plen, &alg, p8inf))
        goto end;

    /* Algorithm parameters must be absent. */
    if ((X509_ALGOR_get0(NULL, &ptype, NULL, alg), ptype != V_ASN1_UNDEF)) {
        ERR_raise_data(ERR_LIB_PROV, PROV_R_UNEXPECTED_KEY_PARAMETERS,
                       "unexpected parameters with a PKCS#8 %s private key",
                       ctx->desc->keytype_name);
        goto end;
    }
    if (OBJ_obj2nid(alg->algorithm) != ctx->desc->evp_type)
        goto end;
    if ((key = ossl_slh_dsa_key_new(libctx, ctx->propq,
                                    ctx->desc->keytype_name)) == NULL)
        goto end;

    if (!ossl_slh_dsa_set_priv(key, p, plen))
        goto end;
    ret = key;
 end:
    PKCS8_PRIV_KEY_INFO_free(p8inf);
    if (ret == NULL)
        ossl_slh_dsa_key_free(key);
    return ret;
}

static ossl_inline void *slh_dsa_d2i_PUBKEY(const uint8_t **der, long der_len,
                                            struct der2key_ctx_st *ctx)
{
    int ok = 0;
    OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(ctx->provctx);
    SLH_DSA_KEY *ret = NULL;
    BARE_PUBKEY *spki = NULL;
    const uint8_t *end = *der;
    size_t len;

    ret = ossl_slh_dsa_key_new(libctx, ctx->propq, ctx->desc->keytype_name);
    if (ret == NULL)
        return NULL;
    len = ossl_slh_dsa_key_get_pub_len(ret);

    /*-
     * The DER ASN.1 encoding of SLH-DSA public keys prepends 18 bytes to the
     * encoded public key (since the largest public key size is 64 bytes):
     *
     * - 2 byte outer sequence tag and length
     * -  2 byte algorithm sequence tag and length
     * -    2 byte algorithm OID tag and length
     * -      9 byte algorithm OID
     * -  2 byte bit string tag and length
     * -    1 bitstring lead byte
     *
     * Check that we have the right OID, the bit string has no "bits left" and
     * that we consume all the input exactly.
     */
    if (der_len != 18 + (long)len) {
        ERR_raise_data(ERR_LIB_PROV, PROV_R_BAD_ENCODING,
                       "unexpected %s public key length: %ld != %ld",
                       ctx->desc->keytype_name, der_len,
                       18 + (long)len);
        goto err;
    }

    if ((spki = OPENSSL_zalloc(sizeof(*spki))) == NULL)
        goto err;

    /* The spki storage is freed on error */
    if (ASN1_item_d2i_ex((ASN1_VALUE **)&spki, &end, der_len,
                         ASN1_ITEM_rptr(BARE_PUBKEY), NULL, NULL) == NULL) {
        ERR_raise_data(ERR_LIB_PROV, PROV_R_BAD_ENCODING,
                       "malformed %s public key ASN.1 encoding",
                       ossl_slh_dsa_key_get_name(ret));
        goto err;
    }

    /* The spki structure now owns some memory */
    if ((spki->pubkey->flags & 0x7) != 0 || end != *der + der_len) {
        ERR_raise_data(ERR_LIB_PROV, PROV_R_BAD_ENCODING,
                       "malformed %s public key ASN.1 encoding",
                       ossl_slh_dsa_key_get_name(ret));
        goto err;
    }
    if (OBJ_cmp(OBJ_nid2obj(ctx->desc->evp_type), spki->algor.oid) != 0) {
        ERR_raise_data(ERR_LIB_PROV, PROV_R_BAD_ENCODING,
                       "unexpected algorithm OID for an %s public key",
                       ossl_slh_dsa_key_get_name(ret));
        goto err;
    }

    if (!ossl_slh_dsa_set_pub(ret, spki->pubkey->data, spki->pubkey->length)) {
        ERR_raise_data(ERR_LIB_PROV, PROV_R_BAD_ENCODING,
                       "failed to parse %s public key from the input data",
                       ossl_slh_dsa_key_get_name(ret));
        goto err;
    }
    ok = 1;
 err:
    if (spki != NULL) {
        ASN1_OBJECT_free(spki->algor.oid);
        ASN1_BIT_STRING_free(spki->pubkey);
        OPENSSL_free(spki);
    }
    if (!ok) {
        ossl_slh_dsa_key_free(ret);
        ret = NULL;
    }
    return ret;
}

# define slh_dsa_sha2_128s_evp_type        EVP_PKEY_SLH_DSA_SHA2_128S
# define slh_dsa_sha2_128s_d2i_private_key NULL
# define slh_dsa_sha2_128s_d2i_public_key  NULL
# define slh_dsa_sha2_128s_d2i_key_params  NULL
# define slh_dsa_sha2_128s_d2i_PKCS8       slh_dsa_d2i_PKCS8
# define slh_dsa_sha2_128s_d2i_PUBKEY      slh_dsa_d2i_PUBKEY
# define slh_dsa_sha2_128s_free            (free_key_fn *)ossl_slh_dsa_key_free
# define slh_dsa_sha2_128s_check           NULL
# define slh_dsa_sha2_128s_adjust          NULL

# define slh_dsa_sha2_128f_evp_type        EVP_PKEY_SLH_DSA_SHA2_128F
# define slh_dsa_sha2_128f_d2i_private_key NULL
# define slh_dsa_sha2_128f_d2i_public_key  NULL
# define slh_dsa_sha2_128f_d2i_key_params  NULL
# define slh_dsa_sha2_128f_d2i_PKCS8       slh_dsa_d2i_PKCS8
# define slh_dsa_sha2_128f_d2i_PUBKEY      slh_dsa_d2i_PUBKEY
# define slh_dsa_sha2_128f_free            (free_key_fn *)ossl_slh_dsa_key_free
# define slh_dsa_sha2_128f_check           NULL
# define slh_dsa_sha2_128f_adjust          NULL

# define slh_dsa_sha2_192s_evp_type        EVP_PKEY_SLH_DSA_SHA2_192S
# define slh_dsa_sha2_192s_d2i_private_key NULL
# define slh_dsa_sha2_192s_d2i_public_key  NULL
# define slh_dsa_sha2_192s_d2i_key_params  NULL
# define slh_dsa_sha2_192s_d2i_PKCS8       slh_dsa_d2i_PKCS8
# define slh_dsa_sha2_192s_d2i_PUBKEY      slh_dsa_d2i_PUBKEY
# define slh_dsa_sha2_192s_free            (free_key_fn *)ossl_slh_dsa_key_free
# define slh_dsa_sha2_192s_check           NULL
# define slh_dsa_sha2_192s_adjust          NULL

# define slh_dsa_sha2_192f_evp_type        EVP_PKEY_SLH_DSA_SHA2_192F
# define slh_dsa_sha2_192f_d2i_private_key NULL
# define slh_dsa_sha2_192f_d2i_public_key  NULL
# define slh_dsa_sha2_192f_d2i_key_params  NULL
# define slh_dsa_sha2_192f_d2i_PKCS8       slh_dsa_d2i_PKCS8
# define slh_dsa_sha2_192f_d2i_PUBKEY      slh_dsa_d2i_PUBKEY
# define slh_dsa_sha2_192f_free            (free_key_fn *)ossl_slh_dsa_key_free
# define slh_dsa_sha2_192f_check           NULL
# define slh_dsa_sha2_192f_adjust          NULL

# define slh_dsa_sha2_256s_evp_type        EVP_PKEY_SLH_DSA_SHA2_256S
# define slh_dsa_sha2_256s_d2i_private_key NULL
# define slh_dsa_sha2_256s_d2i_public_key  NULL
# define slh_dsa_sha2_256s_d2i_key_params  NULL
# define slh_dsa_sha2_256s_d2i_PKCS8       slh_dsa_d2i_PKCS8
# define slh_dsa_sha2_256s_d2i_PUBKEY      slh_dsa_d2i_PUBKEY
# define slh_dsa_sha2_256s_free            (free_key_fn *)ossl_slh_dsa_key_free
# define slh_dsa_sha2_256s_check           NULL
# define slh_dsa_sha2_256s_adjust          NULL

# define slh_dsa_sha2_256f_evp_type        EVP_PKEY_SLH_DSA_SHA2_256F
# define slh_dsa_sha2_256f_d2i_private_key NULL
# define slh_dsa_sha2_256f_d2i_public_key  NULL
# define slh_dsa_sha2_256f_d2i_key_params  NULL
# define slh_dsa_sha2_256f_d2i_PKCS8       slh_dsa_d2i_PKCS8
# define slh_dsa_sha2_256f_d2i_PUBKEY      slh_dsa_d2i_PUBKEY
# define slh_dsa_sha2_256f_free            (free_key_fn *)ossl_slh_dsa_key_free
# define slh_dsa_sha2_256f_check           NULL
# define slh_dsa_sha2_256f_adjust          NULL

# define slh_dsa_shake_128s_evp_type        EVP_PKEY_SLH_DSA_SHAKE_128S
# define slh_dsa_shake_128s_d2i_private_key NULL
# define slh_dsa_shake_128s_d2i_public_key  NULL
# define slh_dsa_shake_128s_d2i_key_params  NULL
# define slh_dsa_shake_128s_d2i_PKCS8       slh_dsa_d2i_PKCS8
# define slh_dsa_shake_128s_d2i_PUBKEY      slh_dsa_d2i_PUBKEY
# define slh_dsa_shake_128s_free            (free_key_fn *)ossl_slh_dsa_key_free
# define slh_dsa_shake_128s_check           NULL
# define slh_dsa_shake_128s_adjust          NULL

# define slh_dsa_shake_128f_evp_type        EVP_PKEY_SLH_DSA_SHAKE_128F
# define slh_dsa_shake_128f_d2i_private_key NULL
# define slh_dsa_shake_128f_d2i_public_key  NULL
# define slh_dsa_shake_128f_d2i_key_params  NULL
# define slh_dsa_shake_128f_d2i_PKCS8       slh_dsa_d2i_PKCS8
# define slh_dsa_shake_128f_d2i_PUBKEY      slh_dsa_d2i_PUBKEY
# define slh_dsa_shake_128f_free            (free_key_fn *)ossl_slh_dsa_key_free
# define slh_dsa_shake_128f_check           NULL
# define slh_dsa_shake_128f_adjust          NULL

# define slh_dsa_shake_192s_evp_type        EVP_PKEY_SLH_DSA_SHAKE_192S
# define slh_dsa_shake_192s_d2i_private_key NULL
# define slh_dsa_shake_192s_d2i_public_key  NULL
# define slh_dsa_shake_192s_d2i_key_params  NULL
# define slh_dsa_shake_192s_d2i_PKCS8       slh_dsa_d2i_PKCS8
# define slh_dsa_shake_192s_d2i_PUBKEY      slh_dsa_d2i_PUBKEY
# define slh_dsa_shake_192s_free            (free_key_fn *)ossl_slh_dsa_key_free
# define slh_dsa_shake_192s_check           NULL
# define slh_dsa_shake_192s_adjust          NULL

# define slh_dsa_shake_192f_evp_type        EVP_PKEY_SLH_DSA_SHAKE_192F
# define slh_dsa_shake_192f_d2i_private_key NULL
# define slh_dsa_shake_192f_d2i_public_key  NULL
# define slh_dsa_shake_192f_d2i_key_params  NULL
# define slh_dsa_shake_192f_d2i_PKCS8       slh_dsa_d2i_PKCS8
# define slh_dsa_shake_192f_d2i_PUBKEY      slh_dsa_d2i_PUBKEY
# define slh_dsa_shake_192f_free            (free_key_fn *)ossl_slh_dsa_key_free
# define slh_dsa_shake_192f_check           NULL
# define slh_dsa_shake_192f_adjust          NULL

# define slh_dsa_shake_256s_evp_type        EVP_PKEY_SLH_DSA_SHAKE_256S
# define slh_dsa_shake_256s_d2i_private_key NULL
# define slh_dsa_shake_256s_d2i_public_key  NULL
# define slh_dsa_shake_256s_d2i_key_params  NULL
# define slh_dsa_shake_256s_d2i_PKCS8       slh_dsa_d2i_PKCS8
# define slh_dsa_shake_256s_d2i_PUBKEY      slh_dsa_d2i_PUBKEY
# define slh_dsa_shake_256s_free            (free_key_fn *)ossl_slh_dsa_key_free
# define slh_dsa_shake_256s_check           NULL
# define slh_dsa_shake_256s_adjust          NULL

# define slh_dsa_shake_256f_evp_type        EVP_PKEY_SLH_DSA_SHAKE_256F
# define slh_dsa_shake_256f_d2i_private_key NULL
# define slh_dsa_shake_256f_d2i_public_key  NULL
# define slh_dsa_shake_256f_d2i_key_params  NULL
# define slh_dsa_shake_256f_d2i_PKCS8       slh_dsa_d2i_PKCS8
# define slh_dsa_shake_256f_d2i_PUBKEY      slh_dsa_d2i_PUBKEY
# define slh_dsa_shake_256f_free            (free_key_fn *)ossl_slh_dsa_key_free
# define slh_dsa_shake_256f_check           NULL
# define slh_dsa_shake_256f_adjust          NULL
#endif /* OPENSSL_NO_SLH_DSA */

/* ---------------------------------------------------------------------- */

#define rsa_evp_type                    EVP_PKEY_RSA
#define rsa_d2i_private_key             (d2i_of_void *)d2i_RSAPrivateKey
#define rsa_d2i_public_key              (d2i_of_void *)d2i_RSAPublicKey
#define rsa_d2i_key_params              NULL
#define rsa_free                        (free_key_fn *)RSA_free

static void *rsa_d2i_PKCS8(const unsigned char **der, long der_len,
                           struct der2key_ctx_st *ctx)
{
    return der2key_decode_p8(der, der_len, ctx,
                             (key_from_pkcs8_t *)ossl_rsa_key_from_pkcs8);
}

static void *
rsa_d2i_PUBKEY(const unsigned char **der, long der_len,
               ossl_unused struct der2key_ctx_st *ctx)
{
    return d2i_RSA_PUBKEY(NULL, der, der_len);
}

static int rsa_check(void *key, struct der2key_ctx_st *ctx)
{
    int valid;

    switch (RSA_test_flags(key, RSA_FLAG_TYPE_MASK)) {
    case RSA_FLAG_TYPE_RSA:
        valid = (ctx->desc->evp_type == EVP_PKEY_RSA);
        break;
    case RSA_FLAG_TYPE_RSASSAPSS:
        valid = (ctx->desc->evp_type == EVP_PKEY_RSA_PSS);
        break;
    default:
        /* Currently unsupported RSA key type */
        valid = 0;
    }

    valid = (valid && ossl_rsa_check_factors(key));

    return valid;
}

static void rsa_adjust(void *key, struct der2key_ctx_st *ctx)
{
    ossl_rsa_set0_libctx(key, PROV_LIBCTX_OF(ctx->provctx));
}

#define rsapss_evp_type                 EVP_PKEY_RSA_PSS
#define rsapss_d2i_private_key          (d2i_of_void *)d2i_RSAPrivateKey
#define rsapss_d2i_public_key           (d2i_of_void *)d2i_RSAPublicKey
#define rsapss_d2i_key_params           NULL
#define rsapss_d2i_PKCS8                rsa_d2i_PKCS8
#define rsapss_d2i_PUBKEY               rsa_d2i_PUBKEY
#define rsapss_free                     (free_key_fn *)RSA_free
#define rsapss_check                    rsa_check
#define rsapss_adjust                   rsa_adjust

/* ---------------------------------------------------------------------- */

#ifndef OPENSSL_NO_ML_DSA
static void *
ml_dsa_d2i_PKCS8(const uint8_t **der, long der_len, struct der2key_ctx_st *ctx)
{
    ML_DSA_KEY *key;

    key = ossl_ml_dsa_d2i_PKCS8(*der, der_len, ctx->desc->evp_type,
                                ctx->provctx, ctx->propq);
    if (key != NULL)
        *der += der_len;
    return key;
}

static ossl_inline void * ml_dsa_d2i_PUBKEY(const uint8_t **der, long der_len,
                                            struct der2key_ctx_st *ctx)
{
    ML_DSA_KEY *key;

    key = ossl_ml_dsa_d2i_PUBKEY(*der, der_len, ctx->desc->evp_type,
                                 ctx->provctx, ctx->propq);
    if (key != NULL)
        *der += der_len;
    return key;
}

# define ml_dsa_44_evp_type                EVP_PKEY_ML_DSA_44
# define ml_dsa_44_d2i_private_key         NULL
# define ml_dsa_44_d2i_public_key          NULL
# define ml_dsa_44_d2i_key_params          NULL
# define ml_dsa_44_d2i_PUBKEY              ml_dsa_d2i_PUBKEY
# define ml_dsa_44_d2i_PKCS8               ml_dsa_d2i_PKCS8
# define ml_dsa_44_free                    (free_key_fn *)ossl_ml_dsa_key_free
# define ml_dsa_44_check                   NULL
# define ml_dsa_44_adjust                  NULL

# define ml_dsa_65_evp_type                EVP_PKEY_ML_DSA_65
# define ml_dsa_65_d2i_private_key         NULL
# define ml_dsa_65_d2i_public_key          NULL
# define ml_dsa_65_d2i_key_params          NULL
# define ml_dsa_65_d2i_PUBKEY              ml_dsa_d2i_PUBKEY
# define ml_dsa_65_d2i_PKCS8               ml_dsa_d2i_PKCS8
# define ml_dsa_65_free                    (free_key_fn *)ossl_ml_dsa_key_free
# define ml_dsa_65_check                   NULL
# define ml_dsa_65_adjust                  NULL

# define ml_dsa_87_evp_type               EVP_PKEY_ML_DSA_87
# define ml_dsa_87_d2i_private_key        NULL
# define ml_dsa_87_d2i_public_key         NULL
# define ml_dsa_87_d2i_PUBKEY             ml_dsa_d2i_PUBKEY
# define ml_dsa_87_d2i_PKCS8              ml_dsa_d2i_PKCS8
# define ml_dsa_87_d2i_key_params         NULL
# define ml_dsa_87_free                   (free_key_fn *)ossl_ml_dsa_key_free
# define ml_dsa_87_check                  NULL
# define ml_dsa_87_adjust                 NULL

#endif

/* ---------------------------------------------------------------------- */

/*
 * The DO_ macros help define the selection mask and the method functions
 * for each kind of object we want to decode.
 */
#define DO_type_specific_keypair(keytype)               \
    "type-specific", keytype##_evp_type,                \
        ( OSSL_KEYMGMT_SELECT_KEYPAIR ),                \
        keytype##_d2i_private_key,                      \
        keytype##_d2i_public_key,                       \
        NULL,                                           \
        NULL,                                           \
        NULL,                                           \
        keytype##_check,                                \
        keytype##_adjust,                               \
        keytype##_free

#define DO_type_specific_pub(keytype)                   \
    "type-specific", keytype##_evp_type,                \
        ( OSSL_KEYMGMT_SELECT_PUBLIC_KEY ),             \
        NULL,                                           \
        keytype##_d2i_public_key,                       \
        NULL,                                           \
        NULL,                                           \
        NULL,                                           \
        keytype##_check,                                \
        keytype##_adjust,                               \
        keytype##_free

#define DO_type_specific_priv(keytype)                  \
    "type-specific", keytype##_evp_type,                \
        ( OSSL_KEYMGMT_SELECT_PRIVATE_KEY ),            \
        keytype##_d2i_private_key,                      \
        NULL,                                           \
        NULL,                                           \
        NULL,                                           \
        NULL,                                           \
        keytype##_check,                                \
        keytype##_adjust,                               \
        keytype##_free

#define DO_type_specific_params(keytype)                \
    "type-specific", keytype##_evp_type,                \
        ( OSSL_KEYMGMT_SELECT_ALL_PARAMETERS ),         \
        NULL,                                           \
        NULL,                                           \
        keytype##_d2i_key_params,                       \
        NULL,                                           \
        NULL,                                           \
        keytype##_check,                                \
        keytype##_adjust,                               \
        keytype##_free

#define DO_type_specific(keytype)                       \
    "type-specific", keytype##_evp_type,                \
        ( OSSL_KEYMGMT_SELECT_ALL ),                    \
        keytype##_d2i_private_key,                      \
        keytype##_d2i_public_key,                       \
        keytype##_d2i_key_params,                       \
        NULL,                                           \
        NULL,                                           \
        keytype##_check,                                \
        keytype##_adjust,                               \
        keytype##_free

#define DO_type_specific_no_pub(keytype)                \
    "type-specific", keytype##_evp_type,                \
        ( OSSL_KEYMGMT_SELECT_PRIVATE_KEY               \
          | OSSL_KEYMGMT_SELECT_ALL_PARAMETERS ),       \
        keytype##_d2i_private_key,                      \
        NULL,                                           \
        keytype##_d2i_key_params,                       \
        NULL,                                           \
        NULL,                                           \
        keytype##_check,                                \
        keytype##_adjust,                               \
        keytype##_free

#define DO_PrivateKeyInfo(keytype)                      \
    "PrivateKeyInfo", keytype##_evp_type,               \
        ( OSSL_KEYMGMT_SELECT_PRIVATE_KEY ),            \
        NULL,                                           \
        NULL,                                           \
        NULL,                                           \
        keytype##_d2i_PKCS8,                            \
        NULL,                                           \
        keytype##_check,                                \
        keytype##_adjust,                               \
        keytype##_free

#define DO_SubjectPublicKeyInfo(keytype)                \
    "SubjectPublicKeyInfo", keytype##_evp_type,         \
        ( OSSL_KEYMGMT_SELECT_PUBLIC_KEY ),             \
        NULL,                                           \
        NULL,                                           \
        NULL,                                           \
        NULL,                                           \
        keytype##_d2i_PUBKEY,                           \
        keytype##_check,                                \
        keytype##_adjust,                               \
        keytype##_free

#define DO_DH(keytype)                                  \
    "DH", keytype##_evp_type,                           \
        ( OSSL_KEYMGMT_SELECT_ALL_PARAMETERS ),         \
        NULL,                                           \
        NULL,                                           \
        keytype##_d2i_key_params,                       \
        NULL,                                           \
        NULL,                                           \
        keytype##_check,                                \
        keytype##_adjust,                               \
        keytype##_free

#define DO_DHX(keytype)                                 \
    "DHX", keytype##_evp_type,                          \
        ( OSSL_KEYMGMT_SELECT_ALL_PARAMETERS ),         \
        NULL,                                           \
        NULL,                                           \
        keytype##_d2i_key_params,                       \
        NULL,                                           \
        NULL,                                           \
        keytype##_check,                                \
        keytype##_adjust,                               \
        keytype##_free

#define DO_DSA(keytype)                                 \
    "DSA", keytype##_evp_type,                          \
        ( OSSL_KEYMGMT_SELECT_ALL ),                    \
        keytype##_d2i_private_key,                      \
        keytype##_d2i_public_key,                       \
        keytype##_d2i_key_params,                       \
        NULL,                                           \
        NULL,                                           \
        keytype##_check,                                \
        keytype##_adjust,                               \
        keytype##_free

#define DO_EC(keytype)                                  \
    "EC", keytype##_evp_type,                           \
        ( OSSL_KEYMGMT_SELECT_PRIVATE_KEY               \
          | OSSL_KEYMGMT_SELECT_ALL_PARAMETERS ),       \
        keytype##_d2i_private_key,                      \
        NULL,                                           \
        keytype##_d2i_key_params,                       \
        NULL,                                           \
        NULL,                                           \
        keytype##_check,                                \
        keytype##_adjust,                               \
        keytype##_free

#define DO_RSA(keytype)                                 \
    "RSA", keytype##_evp_type,                          \
        ( OSSL_KEYMGMT_SELECT_KEYPAIR ),                \
        keytype##_d2i_private_key,                      \
        keytype##_d2i_public_key,                       \
        NULL,                                           \
        NULL,                                           \
        NULL,                                           \
        keytype##_check,                                \
        keytype##_adjust,                               \
        keytype##_free

/*
 * MAKE_DECODER is the single driver for creating OSSL_DISPATCH tables.
 * It takes the following arguments:
 *
 * keytype_name The implementation key type as a string.
 * keytype      The implementation key type.  This must correspond exactly
 *              to our existing keymgmt keytype names...  in other words,
 *              there must exist an ossl_##keytype##_keymgmt_functions.
 * type         The type name for the set of functions that implement the
 *              decoder for the key type.  This isn't necessarily the same
 *              as keytype.  For example, the key types ed25519, ed448,
 *              x25519 and x448 are all handled by the same functions with
 *              the common type name ecx.
 * kind         The kind of support to implement.  This translates into
 *              the DO_##kind macros above, to populate the keytype_desc_st
 *              structure.
 */
#define MAKE_DECODER(keytype_name, keytype, type, kind)                 \
    static const struct keytype_desc_st kind##_##keytype##_desc =       \
        { keytype_name, ossl_##keytype##_keymgmt_functions,             \
          DO_##kind(keytype) };                                         \
                                                                        \
    static OSSL_FUNC_decoder_newctx_fn kind##_der2##keytype##_newctx;   \
                                                                        \
    static void *kind##_der2##keytype##_newctx(void *provctx)           \
    {                                                                   \
        return der2key_newctx(provctx, &kind##_##keytype##_desc);       \
    }                                                                   \
    static int kind##_der2##keytype##_does_selection(void *provctx,     \
                                                     int selection)     \
    {                                                                   \
        return der2key_check_selection(selection,                       \
                                       &kind##_##keytype##_desc);       \
    }                                                                   \
    const OSSL_DISPATCH                                                 \
    ossl_##kind##_der_to_##keytype##_decoder_functions[] = {            \
        { OSSL_FUNC_DECODER_NEWCTX,                                     \
          (void (*)(void))kind##_der2##keytype##_newctx },              \
        { OSSL_FUNC_DECODER_FREECTX,                                    \
          (void (*)(void))der2key_freectx },                            \
        { OSSL_FUNC_DECODER_DOES_SELECTION,                             \
          (void (*)(void))kind##_der2##keytype##_does_selection },      \
        { OSSL_FUNC_DECODER_DECODE,                                     \
          (void (*)(void))der2key_decode },                             \
        { OSSL_FUNC_DECODER_EXPORT_OBJECT,                              \
          (void (*)(void))der2key_export_object },                      \
        { OSSL_FUNC_DECODER_SETTABLE_CTX_PARAMS,                        \
          (void (*)(void))der2key_settable_ctx_params },                \
        { OSSL_FUNC_DECODER_SET_CTX_PARAMS,                             \
          (void (*)(void))der2key_set_ctx_params },                     \
        OSSL_DISPATCH_END                                               \
    }

#ifndef OPENSSL_NO_DH
MAKE_DECODER("DH", dh, dh, PrivateKeyInfo);
MAKE_DECODER("DH", dh, dh, SubjectPublicKeyInfo);
MAKE_DECODER("DH", dh, dh, type_specific_params);
MAKE_DECODER("DH", dh, dh, DH);
MAKE_DECODER("DHX", dhx, dhx, PrivateKeyInfo);
MAKE_DECODER("DHX", dhx, dhx, SubjectPublicKeyInfo);
MAKE_DECODER("DHX", dhx, dhx, type_specific_params);
MAKE_DECODER("DHX", dhx, dhx, DHX);
#endif
#ifndef OPENSSL_NO_DSA
MAKE_DECODER("DSA", dsa, dsa, PrivateKeyInfo);
MAKE_DECODER("DSA", dsa, dsa, SubjectPublicKeyInfo);
MAKE_DECODER("DSA", dsa, dsa, type_specific);
MAKE_DECODER("DSA", dsa, dsa, DSA);
#endif
#ifndef OPENSSL_NO_EC
MAKE_DECODER("EC", ec, ec, PrivateKeyInfo);
MAKE_DECODER("EC", ec, ec, SubjectPublicKeyInfo);
MAKE_DECODER("EC", ec, ec, type_specific_no_pub);
MAKE_DECODER("EC", ec, ec, EC);
# ifndef OPENSSL_NO_ECX
MAKE_DECODER("X25519", x25519, ecx, PrivateKeyInfo);
MAKE_DECODER("X25519", x25519, ecx, SubjectPublicKeyInfo);
MAKE_DECODER("X448", x448, ecx, PrivateKeyInfo);
MAKE_DECODER("X448", x448, ecx, SubjectPublicKeyInfo);
MAKE_DECODER("ED25519", ed25519, ecx, PrivateKeyInfo);
MAKE_DECODER("ED25519", ed25519, ecx, SubjectPublicKeyInfo);
MAKE_DECODER("ED448", ed448, ecx, PrivateKeyInfo);
MAKE_DECODER("ED448", ed448, ecx, SubjectPublicKeyInfo);
# endif
# ifndef OPENSSL_NO_SM2
MAKE_DECODER("SM2", sm2, ec, PrivateKeyInfo);
MAKE_DECODER("SM2", sm2, ec, SubjectPublicKeyInfo);
MAKE_DECODER("SM2", sm2, sm2, type_specific_no_pub);
# endif
#endif
#ifndef OPENSSL_NO_ML_KEM
MAKE_DECODER("ML-KEM-512", ml_kem_512, ml_kem_512, PrivateKeyInfo);
MAKE_DECODER("ML-KEM-512", ml_kem_512, ml_kem_512, SubjectPublicKeyInfo);
MAKE_DECODER("ML-KEM-768", ml_kem_768, ml_kem_768, PrivateKeyInfo);
MAKE_DECODER("ML-KEM-768", ml_kem_768, ml_kem_768, SubjectPublicKeyInfo);
MAKE_DECODER("ML-KEM-1024", ml_kem_1024, ml_kem_1024, PrivateKeyInfo);
MAKE_DECODER("ML-KEM-1024", ml_kem_1024, ml_kem_1024, SubjectPublicKeyInfo);
#endif
#ifndef OPENSSL_NO_SLH_DSA
MAKE_DECODER("SLH-DSA-SHA2-128s", slh_dsa_sha2_128s, slh_dsa, PrivateKeyInfo);
MAKE_DECODER("SLH-DSA-SHA2-128f", slh_dsa_sha2_128f, slh_dsa, PrivateKeyInfo);
MAKE_DECODER("SLH-DSA-SHA2-192s", slh_dsa_sha2_192s, slh_dsa, PrivateKeyInfo);
MAKE_DECODER("SLH-DSA-SHA2-192f", slh_dsa_sha2_192f, slh_dsa, PrivateKeyInfo);
MAKE_DECODER("SLH-DSA-SHA2-256s", slh_dsa_sha2_256s, slh_dsa, PrivateKeyInfo);
MAKE_DECODER("SLH-DSA-SHA2-256f", slh_dsa_sha2_256f, slh_dsa, PrivateKeyInfo);
MAKE_DECODER("SLH-DSA-SHAKE-128s", slh_dsa_shake_128s, slh_dsa, PrivateKeyInfo);
MAKE_DECODER("SLH-DSA-SHAKE-128f", slh_dsa_shake_128f, slh_dsa, PrivateKeyInfo);
MAKE_DECODER("SLH-DSA-SHAKE-192s", slh_dsa_shake_192s, slh_dsa, PrivateKeyInfo);
MAKE_DECODER("SLH-DSA-SHAKE-192f", slh_dsa_shake_192f, slh_dsa, PrivateKeyInfo);
MAKE_DECODER("SLH-DSA-SHAKE-256s", slh_dsa_shake_256s, slh_dsa, PrivateKeyInfo);
MAKE_DECODER("SLH-DSA-SHAKE-256f", slh_dsa_shake_256f, slh_dsa, PrivateKeyInfo);

MAKE_DECODER("SLH-DSA-SHA2-128s", slh_dsa_sha2_128s, slh_dsa, SubjectPublicKeyInfo);
MAKE_DECODER("SLH-DSA-SHA2-128f", slh_dsa_sha2_128f, slh_dsa, SubjectPublicKeyInfo);
MAKE_DECODER("SLH-DSA-SHA2-192s", slh_dsa_sha2_192s, slh_dsa, SubjectPublicKeyInfo);
MAKE_DECODER("SLH-DSA-SHA2-192f", slh_dsa_sha2_192f, slh_dsa, SubjectPublicKeyInfo);
MAKE_DECODER("SLH-DSA-SHA2-256s", slh_dsa_sha2_256s, slh_dsa, SubjectPublicKeyInfo);
MAKE_DECODER("SLH-DSA-SHA2-256f", slh_dsa_sha2_256f, slh_dsa, SubjectPublicKeyInfo);
MAKE_DECODER("SLH-DSA-SHAKE-128s", slh_dsa_shake_128s, slh_dsa, SubjectPublicKeyInfo);
MAKE_DECODER("SLH-DSA-SHAKE-128f", slh_dsa_shake_128f, slh_dsa, SubjectPublicKeyInfo);
MAKE_DECODER("SLH-DSA-SHAKE-192s", slh_dsa_shake_192s, slh_dsa, SubjectPublicKeyInfo);
MAKE_DECODER("SLH-DSA-SHAKE-192f", slh_dsa_shake_192f, slh_dsa, SubjectPublicKeyInfo);
MAKE_DECODER("SLH-DSA-SHAKE-256s", slh_dsa_shake_256s, slh_dsa, SubjectPublicKeyInfo);
MAKE_DECODER("SLH-DSA-SHAKE-256f", slh_dsa_shake_256f, slh_dsa, SubjectPublicKeyInfo);
#endif /* OPENSSL_NO_SLH_DSA */
MAKE_DECODER("RSA", rsa, rsa, PrivateKeyInfo);
MAKE_DECODER("RSA", rsa, rsa, SubjectPublicKeyInfo);
MAKE_DECODER("RSA", rsa, rsa, type_specific_keypair);
MAKE_DECODER("RSA", rsa, rsa, RSA);
MAKE_DECODER("RSA-PSS", rsapss, rsapss, PrivateKeyInfo);
MAKE_DECODER("RSA-PSS", rsapss, rsapss, SubjectPublicKeyInfo);

#ifndef OPENSSL_NO_ML_DSA
MAKE_DECODER("ML-DSA-44", ml_dsa_44, ml_dsa_44, PrivateKeyInfo);
MAKE_DECODER("ML-DSA-44", ml_dsa_44, ml_dsa_44, SubjectPublicKeyInfo);
MAKE_DECODER("ML-DSA-65", ml_dsa_65, ml_dsa_65, PrivateKeyInfo);
MAKE_DECODER("ML-DSA-65", ml_dsa_65, ml_dsa_65, SubjectPublicKeyInfo);
MAKE_DECODER("ML-DSA-87", ml_dsa_87, ml_dsa_87, PrivateKeyInfo);
MAKE_DECODER("ML-DSA-87", ml_dsa_87, ml_dsa_87, SubjectPublicKeyInfo);
#endif