boring.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // Copyright 2017 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. //go:build boringcrypto
  5. package tls
  6. import "crypto/internal/boring/fipstls"
  7. // The FIPS-only policies enforced here currently match BoringSSL's
  8. // ssl_policy_fips_202205.
  9. // needFIPS returns fipstls.Required(); it avoids a new import in common.go.
  10. func needFIPS() bool {
  11. return fipstls.Required()
  12. }
  13. // fipsMinVersion replaces c.minVersion in FIPS-only mode.
  14. func fipsMinVersion(c *Config) uint16 {
  15. // FIPS requires TLS 1.2 or TLS 1.3.
  16. return VersionTLS12
  17. }
  18. // fipsMaxVersion replaces c.maxVersion in FIPS-only mode.
  19. func fipsMaxVersion(c *Config) uint16 {
  20. // FIPS requires TLS 1.2 or TLS 1.3.
  21. return VersionTLS13
  22. }
  23. // default defaultFIPSCurvePreferences is the FIPS-allowed curves,
  24. // in preference order (most preferable first).
  25. var defaultFIPSCurvePreferences = []CurveID{CurveP256, CurveP384}
  26. // fipsCurvePreferences replaces c.curvePreferences in FIPS-only mode.
  27. func fipsCurvePreferences(c *Config) []CurveID {
  28. if c == nil || len(c.CurvePreferences) == 0 {
  29. return defaultFIPSCurvePreferences
  30. }
  31. var list []CurveID
  32. for _, id := range c.CurvePreferences {
  33. for _, allowed := range defaultFIPSCurvePreferences {
  34. if id == allowed {
  35. list = append(list, id)
  36. break
  37. }
  38. }
  39. }
  40. return list
  41. }
  42. // defaultCipherSuitesFIPS are the FIPS-allowed cipher suites.
  43. var defaultCipherSuitesFIPS = []uint16{
  44. TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
  45. TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
  46. TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
  47. TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
  48. }
  49. // fipsCipherSuites replaces c.cipherSuites in FIPS-only mode.
  50. func fipsCipherSuites(c *Config) []uint16 {
  51. if c == nil || c.CipherSuites == nil {
  52. return defaultCipherSuitesFIPS
  53. }
  54. list := make([]uint16, 0, len(defaultCipherSuitesFIPS))
  55. for _, id := range c.CipherSuites {
  56. for _, allowed := range defaultCipherSuitesFIPS {
  57. if id == allowed {
  58. list = append(list, id)
  59. break
  60. }
  61. }
  62. }
  63. return list
  64. }
  65. // defaultCipherSuitesTLS13FIPS are the FIPS-allowed cipher suites for TLS 1.3.
  66. var defaultCipherSuitesTLS13FIPS = []uint16{
  67. TLS_AES_128_GCM_SHA256,
  68. TLS_AES_256_GCM_SHA384,
  69. }
  70. // fipsSupportedSignatureAlgorithms currently are a subset of
  71. // defaultSupportedSignatureAlgorithms without Ed25519, SHA-1, and P-521.
  72. var fipsSupportedSignatureAlgorithms = []SignatureScheme{
  73. PSSWithSHA256,
  74. PSSWithSHA384,
  75. PSSWithSHA512,
  76. PKCS1WithSHA256,
  77. ECDSAWithP256AndSHA256,
  78. PKCS1WithSHA384,
  79. ECDSAWithP384AndSHA384,
  80. PKCS1WithSHA512,
  81. }
  82. // supportedSignatureAlgorithms returns the supported signature algorithms.
  83. func supportedSignatureAlgorithms() []SignatureScheme {
  84. if !needFIPS() {
  85. return defaultSupportedSignatureAlgorithms
  86. }
  87. return fipsSupportedSignatureAlgorithms
  88. }