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