ml_common_codecs.c 3.1 KB

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