1*e7be843bSPierre Pronchery /*
2*e7be843bSPierre Pronchery * Copyright 2025 The OpenSSL Project Authors. All Rights Reserved.
3*e7be843bSPierre Pronchery *
4*e7be843bSPierre Pronchery * Licensed under the Apache License 2.0 (the "License"). You may not use
5*e7be843bSPierre Pronchery * this file except in compliance with the License. You can obtain a copy
6*e7be843bSPierre Pronchery * in the file LICENSE in the source distribution or at
7*e7be843bSPierre Pronchery * https://www.openssl.org/source/license.html
8*e7be843bSPierre Pronchery */
9*e7be843bSPierre Pronchery
10*e7be843bSPierre Pronchery #include <string.h>
11*e7be843bSPierre Pronchery #include <openssl/crypto.h>
12*e7be843bSPierre Pronchery #include <openssl/err.h>
13*e7be843bSPierre Pronchery #include <openssl/proverr.h>
14*e7be843bSPierre Pronchery #include "ml_common_codecs.h"
15*e7be843bSPierre Pronchery
pref_cmp(const void * va,const void * vb)16*e7be843bSPierre Pronchery static int pref_cmp(const void *va, const void *vb)
17*e7be843bSPierre Pronchery {
18*e7be843bSPierre Pronchery const ML_COMMON_PKCS8_FMT_PREF *a = va;
19*e7be843bSPierre Pronchery const ML_COMMON_PKCS8_FMT_PREF *b = vb;
20*e7be843bSPierre Pronchery
21*e7be843bSPierre Pronchery /*
22*e7be843bSPierre Pronchery * Zeros sort last, otherwise the sort is in increasing order.
23*e7be843bSPierre Pronchery *
24*e7be843bSPierre Pronchery * The preferences are small enough to ensure the comparison is transitive
25*e7be843bSPierre Pronchery * as required by qsort(3). When overflow or underflow is possible, the
26*e7be843bSPierre Pronchery * correct transitive comparison would be: (b < a) - (a < b).
27*e7be843bSPierre Pronchery */
28*e7be843bSPierre Pronchery if (a->pref > 0 && b->pref > 0)
29*e7be843bSPierre Pronchery return a->pref - b->pref;
30*e7be843bSPierre Pronchery /* A preference of 0 is "larger" than (sorts after) any nonzero value. */
31*e7be843bSPierre Pronchery return b->pref - a->pref;
32*e7be843bSPierre Pronchery }
33*e7be843bSPierre Pronchery
34*e7be843bSPierre Pronchery ML_COMMON_PKCS8_FMT_PREF *
ossl_ml_common_pkcs8_fmt_order(const char * algorithm_name,const ML_COMMON_PKCS8_FMT * p8fmt,const char * direction,const char * formats)35*e7be843bSPierre Pronchery ossl_ml_common_pkcs8_fmt_order(const char *algorithm_name,
36*e7be843bSPierre Pronchery const ML_COMMON_PKCS8_FMT *p8fmt,
37*e7be843bSPierre Pronchery const char *direction, const char *formats)
38*e7be843bSPierre Pronchery {
39*e7be843bSPierre Pronchery ML_COMMON_PKCS8_FMT_PREF *ret;
40*e7be843bSPierre Pronchery int i, count = 0;
41*e7be843bSPierre Pronchery const char *fmt = formats, *end;
42*e7be843bSPierre Pronchery const char *sep = "\t ,";
43*e7be843bSPierre Pronchery
44*e7be843bSPierre Pronchery /* Reserve an extra terminal slot with fmt == NULL */
45*e7be843bSPierre Pronchery if ((ret = OPENSSL_zalloc((NUM_PKCS8_FORMATS + 1) * sizeof(*ret))) == NULL)
46*e7be843bSPierre Pronchery return NULL;
47*e7be843bSPierre Pronchery
48*e7be843bSPierre Pronchery /* Entries that match a format will get a non-zero preference. */
49*e7be843bSPierre Pronchery for (i = 0; i < NUM_PKCS8_FORMATS; ++i) {
50*e7be843bSPierre Pronchery ret[i].fmt = &p8fmt[i];
51*e7be843bSPierre Pronchery ret[i].pref = 0;
52*e7be843bSPierre Pronchery }
53*e7be843bSPierre Pronchery
54*e7be843bSPierre Pronchery /* Default to compile-time table order when none specified. */
55*e7be843bSPierre Pronchery if (formats == NULL)
56*e7be843bSPierre Pronchery return ret;
57*e7be843bSPierre Pronchery
58*e7be843bSPierre Pronchery /*
59*e7be843bSPierre Pronchery * Formats are case-insensitive, separated by spaces, tabs or commas.
60*e7be843bSPierre Pronchery * Duplicate formats are allowed, the first occurence determines the order.
61*e7be843bSPierre Pronchery */
62*e7be843bSPierre Pronchery do {
63*e7be843bSPierre Pronchery if (*(fmt += strspn(fmt, sep)) == '\0')
64*e7be843bSPierre Pronchery break;
65*e7be843bSPierre Pronchery end = fmt + strcspn(fmt, sep);
66*e7be843bSPierre Pronchery for (i = 0; i < NUM_PKCS8_FORMATS; ++i) {
67*e7be843bSPierre Pronchery /* Skip slots already selected or with a different name. */
68*e7be843bSPierre Pronchery if (ret[i].pref > 0
69*e7be843bSPierre Pronchery || OPENSSL_strncasecmp(ret[i].fmt->p8_name,
70*e7be843bSPierre Pronchery fmt, (end - fmt)) != 0)
71*e7be843bSPierre Pronchery continue;
72*e7be843bSPierre Pronchery /* First time match */
73*e7be843bSPierre Pronchery ret[i].pref = ++count;
74*e7be843bSPierre Pronchery break;
75*e7be843bSPierre Pronchery }
76*e7be843bSPierre Pronchery fmt = end;
77*e7be843bSPierre Pronchery } while (count < NUM_PKCS8_FORMATS);
78*e7be843bSPierre Pronchery
79*e7be843bSPierre Pronchery /* No formats matched, raise an error */
80*e7be843bSPierre Pronchery if (count == 0) {
81*e7be843bSPierre Pronchery OPENSSL_free(ret);
82*e7be843bSPierre Pronchery ERR_raise_data(ERR_LIB_PROV, PROV_R_ML_DSA_NO_FORMAT,
83*e7be843bSPierre Pronchery "no %s private key %s formats are enabled",
84*e7be843bSPierre Pronchery algorithm_name, direction);
85*e7be843bSPierre Pronchery return NULL;
86*e7be843bSPierre Pronchery }
87*e7be843bSPierre Pronchery /* Sort by preference, with 0's last */
88*e7be843bSPierre Pronchery qsort(ret, NUM_PKCS8_FORMATS, sizeof(*ret), pref_cmp);
89*e7be843bSPierre Pronchery /* Terminate the list at first unselected entry, perhaps reserved slot. */
90*e7be843bSPierre Pronchery ret[count].fmt = NULL;
91*e7be843bSPierre Pronchery return ret;
92*e7be843bSPierre Pronchery }
93