tls_curves.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. Copyright © The ESO Authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. https://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package esutils
  14. import (
  15. "crypto/tls"
  16. "fmt"
  17. "strconv"
  18. "strings"
  19. "unicode"
  20. )
  21. // ParseCurvePreferences converts curve names to tls.CurveID values, preserving order.
  22. // An empty slice returns (nil, nil) so tls.Config uses Go defaults.
  23. //
  24. // Each entry may be a well-known name matching crypto/tls constants (for example
  25. // X25519, CurveP256, CurveP384, CurveP521) or a decimal tls.CurveID as supported
  26. // by the Go toolchain (for example hybrid post-quantum groups when available).
  27. func ParseCurvePreferences(names []string) ([]tls.CurveID, error) {
  28. if len(names) == 0 {
  29. return nil, nil
  30. }
  31. out := make([]tls.CurveID, 0, len(names))
  32. for _, raw := range names {
  33. name := strings.TrimSpace(raw)
  34. if name == "" {
  35. return nil, fmt.Errorf("empty curve preference entry")
  36. }
  37. id, err := parseCurveID(name)
  38. if err != nil {
  39. return nil, err
  40. }
  41. out = append(out, id)
  42. }
  43. return out, nil
  44. }
  45. var knownCurves = map[tls.CurveID]bool{
  46. tls.CurveP256: true,
  47. tls.CurveP384: true,
  48. tls.CurveP521: true,
  49. tls.X25519: true,
  50. tls.X25519MLKEM768: true,
  51. tls.SecP256r1MLKEM768: true,
  52. tls.SecP384r1MLKEM1024: true,
  53. }
  54. func parseCurveID(name string) (tls.CurveID, error) {
  55. if isAllDecimal(name) {
  56. u, err := strconv.ParseUint(name, 10, 16)
  57. if err != nil {
  58. return 0, fmt.Errorf("invalid tls curve id %q: %w", name, err)
  59. }
  60. id := tls.CurveID(u)
  61. if !knownCurves[id] {
  62. return 0, fmt.Errorf(
  63. "unsupported tls curve id %s: not a known CurveID"+
  64. " (valid: 23=CurveP256, 24=CurveP384, 25=CurveP521,"+
  65. " 29=X25519, 4587=SecP256r1MLKEM768,"+
  66. " 4588=X25519MLKEM768, 4589=SecP384r1MLKEM1024)",
  67. name,
  68. )
  69. }
  70. return id, nil
  71. }
  72. switch name {
  73. case "X25519":
  74. return tls.X25519, nil
  75. case "CurveP256", "P-256", "P256":
  76. return tls.CurveP256, nil
  77. case "CurveP384", "P-384", "P384":
  78. return tls.CurveP384, nil
  79. case "CurveP521", "P-521", "P521":
  80. return tls.CurveP521, nil
  81. default:
  82. return 0, fmt.Errorf("unknown tls curve preference %q (use a name like X25519 or a decimal CurveID)", name)
  83. }
  84. }
  85. func isAllDecimal(s string) bool {
  86. if s == "" {
  87. return false
  88. }
  89. for _, r := range s {
  90. if !unicode.IsDigit(r) {
  91. return false
  92. }
  93. }
  94. return true
  95. }