capabilities.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. * Copyright 2019-2022 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 <assert.h>
  10. #include <string.h>
  11. #include <openssl/core_dispatch.h>
  12. #include <openssl/core_names.h>
  13. /* For TLS1_VERSION etc */
  14. #include <openssl/prov_ssl.h>
  15. #include <openssl/params.h>
  16. #include "internal/nelem.h"
  17. #include "internal/tlsgroups.h"
  18. #include "prov/providercommon.h"
  19. #include "internal/e_os.h"
  20. /* If neither ec or dh is available then we have no TLS-GROUP capabilities */
  21. #if !defined(OPENSSL_NO_EC) || !defined(OPENSSL_NO_DH)
  22. typedef struct tls_group_constants_st {
  23. unsigned int group_id; /* Group ID */
  24. unsigned int secbits; /* Bits of security */
  25. int mintls; /* Minimum TLS version, -1 unsupported */
  26. int maxtls; /* Maximum TLS version (or 0 for undefined) */
  27. int mindtls; /* Minimum DTLS version, -1 unsupported */
  28. int maxdtls; /* Maximum DTLS version (or 0 for undefined) */
  29. } TLS_GROUP_CONSTANTS;
  30. static const TLS_GROUP_CONSTANTS group_list[35] = {
  31. { OSSL_TLS_GROUP_ID_sect163k1, 80, TLS1_VERSION, TLS1_2_VERSION,
  32. DTLS1_VERSION, DTLS1_2_VERSION },
  33. { OSSL_TLS_GROUP_ID_sect163r1, 80, TLS1_VERSION, TLS1_2_VERSION,
  34. DTLS1_VERSION, DTLS1_2_VERSION },
  35. { OSSL_TLS_GROUP_ID_sect163r2, 80, TLS1_VERSION, TLS1_2_VERSION,
  36. DTLS1_VERSION, DTLS1_2_VERSION },
  37. { OSSL_TLS_GROUP_ID_sect193r1, 80, TLS1_VERSION, TLS1_2_VERSION,
  38. DTLS1_VERSION, DTLS1_2_VERSION },
  39. { OSSL_TLS_GROUP_ID_sect193r2, 80, TLS1_VERSION, TLS1_2_VERSION,
  40. DTLS1_VERSION, DTLS1_2_VERSION },
  41. { OSSL_TLS_GROUP_ID_sect233k1, 112, TLS1_VERSION, TLS1_2_VERSION,
  42. DTLS1_VERSION, DTLS1_2_VERSION },
  43. { OSSL_TLS_GROUP_ID_sect233r1, 112, TLS1_VERSION, TLS1_2_VERSION,
  44. DTLS1_VERSION, DTLS1_2_VERSION },
  45. { OSSL_TLS_GROUP_ID_sect239k1, 112, TLS1_VERSION, TLS1_2_VERSION,
  46. DTLS1_VERSION, DTLS1_2_VERSION },
  47. { OSSL_TLS_GROUP_ID_sect283k1, 128, TLS1_VERSION, TLS1_2_VERSION,
  48. DTLS1_VERSION, DTLS1_2_VERSION },
  49. { OSSL_TLS_GROUP_ID_sect283r1, 128, TLS1_VERSION, TLS1_2_VERSION,
  50. DTLS1_VERSION, DTLS1_2_VERSION },
  51. { OSSL_TLS_GROUP_ID_sect409k1, 192, TLS1_VERSION, TLS1_2_VERSION,
  52. DTLS1_VERSION, DTLS1_2_VERSION },
  53. { OSSL_TLS_GROUP_ID_sect409r1, 192, TLS1_VERSION, TLS1_2_VERSION,
  54. DTLS1_VERSION, DTLS1_2_VERSION },
  55. { OSSL_TLS_GROUP_ID_sect571k1, 256, TLS1_VERSION, TLS1_2_VERSION,
  56. DTLS1_VERSION, DTLS1_2_VERSION },
  57. { OSSL_TLS_GROUP_ID_sect571r1, 256, TLS1_VERSION, TLS1_2_VERSION,
  58. DTLS1_VERSION, DTLS1_2_VERSION },
  59. { OSSL_TLS_GROUP_ID_secp160k1, 80, TLS1_VERSION, TLS1_2_VERSION,
  60. DTLS1_VERSION, DTLS1_2_VERSION },
  61. { OSSL_TLS_GROUP_ID_secp160r1, 80, TLS1_VERSION, TLS1_2_VERSION,
  62. DTLS1_VERSION, DTLS1_2_VERSION },
  63. { OSSL_TLS_GROUP_ID_secp160r2, 80, TLS1_VERSION, TLS1_2_VERSION,
  64. DTLS1_VERSION, DTLS1_2_VERSION },
  65. { OSSL_TLS_GROUP_ID_secp192k1, 80, TLS1_VERSION, TLS1_2_VERSION,
  66. DTLS1_VERSION, DTLS1_2_VERSION },
  67. { OSSL_TLS_GROUP_ID_secp192r1, 80, TLS1_VERSION, TLS1_2_VERSION,
  68. DTLS1_VERSION, DTLS1_2_VERSION },
  69. { OSSL_TLS_GROUP_ID_secp224k1, 112, TLS1_VERSION, TLS1_2_VERSION,
  70. DTLS1_VERSION, DTLS1_2_VERSION },
  71. { OSSL_TLS_GROUP_ID_secp224r1, 112, TLS1_VERSION, TLS1_2_VERSION,
  72. DTLS1_VERSION, DTLS1_2_VERSION },
  73. { OSSL_TLS_GROUP_ID_secp256k1, 128, TLS1_VERSION, TLS1_2_VERSION,
  74. DTLS1_VERSION, DTLS1_2_VERSION },
  75. { OSSL_TLS_GROUP_ID_secp256r1, 128, TLS1_VERSION, 0, DTLS1_VERSION, 0 },
  76. { OSSL_TLS_GROUP_ID_secp384r1, 192, TLS1_VERSION, 0, DTLS1_VERSION, 0 },
  77. { OSSL_TLS_GROUP_ID_secp521r1, 256, TLS1_VERSION, 0, DTLS1_VERSION, 0 },
  78. { OSSL_TLS_GROUP_ID_brainpoolP256r1, 128, TLS1_VERSION, TLS1_2_VERSION,
  79. DTLS1_VERSION, DTLS1_2_VERSION },
  80. { OSSL_TLS_GROUP_ID_brainpoolP384r1, 192, TLS1_VERSION, TLS1_2_VERSION,
  81. DTLS1_VERSION, DTLS1_2_VERSION },
  82. { OSSL_TLS_GROUP_ID_brainpoolP512r1, 256, TLS1_VERSION, TLS1_2_VERSION,
  83. DTLS1_VERSION, DTLS1_2_VERSION },
  84. { OSSL_TLS_GROUP_ID_x25519, 128, TLS1_VERSION, 0, DTLS1_VERSION, 0 },
  85. { OSSL_TLS_GROUP_ID_x448, 224, TLS1_VERSION, 0, DTLS1_VERSION, 0 },
  86. /* Security bit values as given by BN_security_bits() */
  87. { OSSL_TLS_GROUP_ID_ffdhe2048, 112, TLS1_3_VERSION, 0, -1, -1 },
  88. { OSSL_TLS_GROUP_ID_ffdhe3072, 128, TLS1_3_VERSION, 0, -1, -1 },
  89. { OSSL_TLS_GROUP_ID_ffdhe4096, 128, TLS1_3_VERSION, 0, -1, -1 },
  90. { OSSL_TLS_GROUP_ID_ffdhe6144, 128, TLS1_3_VERSION, 0, -1, -1 },
  91. { OSSL_TLS_GROUP_ID_ffdhe8192, 192, TLS1_3_VERSION, 0, -1, -1 },
  92. };
  93. #define TLS_GROUP_ENTRY(tlsname, realname, algorithm, idx) \
  94. { \
  95. OSSL_PARAM_utf8_string(OSSL_CAPABILITY_TLS_GROUP_NAME, \
  96. tlsname, \
  97. sizeof(tlsname)), \
  98. OSSL_PARAM_utf8_string(OSSL_CAPABILITY_TLS_GROUP_NAME_INTERNAL, \
  99. realname, \
  100. sizeof(realname)), \
  101. OSSL_PARAM_utf8_string(OSSL_CAPABILITY_TLS_GROUP_ALG, \
  102. algorithm, \
  103. sizeof(algorithm)), \
  104. OSSL_PARAM_uint(OSSL_CAPABILITY_TLS_GROUP_ID, \
  105. (unsigned int *)&group_list[idx].group_id), \
  106. OSSL_PARAM_uint(OSSL_CAPABILITY_TLS_GROUP_SECURITY_BITS, \
  107. (unsigned int *)&group_list[idx].secbits), \
  108. OSSL_PARAM_int(OSSL_CAPABILITY_TLS_GROUP_MIN_TLS, \
  109. (unsigned int *)&group_list[idx].mintls), \
  110. OSSL_PARAM_int(OSSL_CAPABILITY_TLS_GROUP_MAX_TLS, \
  111. (unsigned int *)&group_list[idx].maxtls), \
  112. OSSL_PARAM_int(OSSL_CAPABILITY_TLS_GROUP_MIN_DTLS, \
  113. (unsigned int *)&group_list[idx].mindtls), \
  114. OSSL_PARAM_int(OSSL_CAPABILITY_TLS_GROUP_MAX_DTLS, \
  115. (unsigned int *)&group_list[idx].maxdtls), \
  116. OSSL_PARAM_END \
  117. }
  118. static const OSSL_PARAM param_group_list[][10] = {
  119. # ifndef OPENSSL_NO_EC
  120. # ifndef OPENSSL_NO_EC2M
  121. TLS_GROUP_ENTRY("sect163k1", "sect163k1", "EC", 0),
  122. TLS_GROUP_ENTRY("K-163", "sect163k1", "EC", 0), /* Alias of above */
  123. # endif
  124. # ifndef FIPS_MODULE
  125. TLS_GROUP_ENTRY("sect163r1", "sect163r1", "EC", 1),
  126. # endif
  127. # ifndef OPENSSL_NO_EC2M
  128. TLS_GROUP_ENTRY("sect163r2", "sect163r2", "EC", 2),
  129. TLS_GROUP_ENTRY("B-163", "sect163r2", "EC", 2), /* Alias of above */
  130. # endif
  131. # ifndef FIPS_MODULE
  132. TLS_GROUP_ENTRY("sect193r1", "sect193r1", "EC", 3),
  133. TLS_GROUP_ENTRY("sect193r2", "sect193r2", "EC", 4),
  134. # endif
  135. # ifndef OPENSSL_NO_EC2M
  136. TLS_GROUP_ENTRY("sect233k1", "sect233k1", "EC", 5),
  137. TLS_GROUP_ENTRY("K-233", "sect233k1", "EC", 5), /* Alias of above */
  138. TLS_GROUP_ENTRY("sect233r1", "sect233r1", "EC", 6),
  139. TLS_GROUP_ENTRY("B-233", "sect233r1", "EC", 6), /* Alias of above */
  140. # endif
  141. # ifndef FIPS_MODULE
  142. TLS_GROUP_ENTRY("sect239k1", "sect239k1", "EC", 7),
  143. # endif
  144. # ifndef OPENSSL_NO_EC2M
  145. TLS_GROUP_ENTRY("sect283k1", "sect283k1", "EC", 8),
  146. TLS_GROUP_ENTRY("K-283", "sect283k1", "EC", 8), /* Alias of above */
  147. TLS_GROUP_ENTRY("sect283r1", "sect283r1", "EC", 9),
  148. TLS_GROUP_ENTRY("B-283", "sect283r1", "EC", 9), /* Alias of above */
  149. TLS_GROUP_ENTRY("sect409k1", "sect409k1", "EC", 10),
  150. TLS_GROUP_ENTRY("K-409", "sect409k1", "EC", 10), /* Alias of above */
  151. TLS_GROUP_ENTRY("sect409r1", "sect409r1", "EC", 11),
  152. TLS_GROUP_ENTRY("B-409", "sect409r1", "EC", 11), /* Alias of above */
  153. TLS_GROUP_ENTRY("sect571k1", "sect571k1", "EC", 12),
  154. TLS_GROUP_ENTRY("K-571", "sect571k1", "EC", 12), /* Alias of above */
  155. TLS_GROUP_ENTRY("sect571r1", "sect571r1", "EC", 13),
  156. TLS_GROUP_ENTRY("B-571", "sect571r1", "EC", 13), /* Alias of above */
  157. # endif
  158. # ifndef FIPS_MODULE
  159. TLS_GROUP_ENTRY("secp160k1", "secp160k1", "EC", 14),
  160. TLS_GROUP_ENTRY("secp160r1", "secp160r1", "EC", 15),
  161. TLS_GROUP_ENTRY("secp160r2", "secp160r2", "EC", 16),
  162. TLS_GROUP_ENTRY("secp192k1", "secp192k1", "EC", 17),
  163. # endif
  164. TLS_GROUP_ENTRY("secp192r1", "prime192v1", "EC", 18),
  165. TLS_GROUP_ENTRY("P-192", "prime192v1", "EC", 18), /* Alias of above */
  166. # ifndef FIPS_MODULE
  167. TLS_GROUP_ENTRY("secp224k1", "secp224k1", "EC", 19),
  168. # endif
  169. TLS_GROUP_ENTRY("secp224r1", "secp224r1", "EC", 20),
  170. TLS_GROUP_ENTRY("P-224", "secp224r1", "EC", 20), /* Alias of above */
  171. # ifndef FIPS_MODULE
  172. TLS_GROUP_ENTRY("secp256k1", "secp256k1", "EC", 21),
  173. # endif
  174. TLS_GROUP_ENTRY("secp256r1", "prime256v1", "EC", 22),
  175. TLS_GROUP_ENTRY("P-256", "prime256v1", "EC", 22), /* Alias of above */
  176. TLS_GROUP_ENTRY("secp384r1", "secp384r1", "EC", 23),
  177. TLS_GROUP_ENTRY("P-384", "secp384r1", "EC", 23), /* Alias of above */
  178. TLS_GROUP_ENTRY("secp521r1", "secp521r1", "EC", 24),
  179. TLS_GROUP_ENTRY("P-521", "secp521r1", "EC", 24), /* Alias of above */
  180. # ifndef FIPS_MODULE
  181. TLS_GROUP_ENTRY("brainpoolP256r1", "brainpoolP256r1", "EC", 25),
  182. TLS_GROUP_ENTRY("brainpoolP384r1", "brainpoolP384r1", "EC", 26),
  183. TLS_GROUP_ENTRY("brainpoolP512r1", "brainpoolP512r1", "EC", 27),
  184. # endif
  185. TLS_GROUP_ENTRY("x25519", "X25519", "X25519", 28),
  186. TLS_GROUP_ENTRY("x448", "X448", "X448", 29),
  187. # endif /* OPENSSL_NO_EC */
  188. # ifndef OPENSSL_NO_DH
  189. /* Security bit values for FFDHE groups are as per RFC 7919 */
  190. TLS_GROUP_ENTRY("ffdhe2048", "ffdhe2048", "DH", 30),
  191. TLS_GROUP_ENTRY("ffdhe3072", "ffdhe3072", "DH", 31),
  192. TLS_GROUP_ENTRY("ffdhe4096", "ffdhe4096", "DH", 32),
  193. TLS_GROUP_ENTRY("ffdhe6144", "ffdhe6144", "DH", 33),
  194. TLS_GROUP_ENTRY("ffdhe8192", "ffdhe8192", "DH", 34),
  195. # endif
  196. };
  197. #endif /* !defined(OPENSSL_NO_EC) || !defined(OPENSSL_NO_DH) */
  198. static int tls_group_capability(OSSL_CALLBACK *cb, void *arg)
  199. {
  200. #if !defined(OPENSSL_NO_EC) || !defined(OPENSSL_NO_DH)
  201. size_t i;
  202. for (i = 0; i < OSSL_NELEM(param_group_list); i++)
  203. if (!cb(param_group_list[i], arg))
  204. return 0;
  205. #endif
  206. return 1;
  207. }
  208. int ossl_prov_get_capabilities(void *provctx, const char *capability,
  209. OSSL_CALLBACK *cb, void *arg)
  210. {
  211. if (OPENSSL_strcasecmp(capability, "TLS-GROUP") == 0)
  212. return tls_group_capability(cb, arg);
  213. /* We don't support this capability */
  214. return 0;
  215. }