boring.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 (
  7. "crypto/internal/boring/fipstls"
  8. )
  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.
  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.
  21. return VersionTLS12
  22. }
  23. // default defaultFIPSCurvePreferences is the FIPS-allowed curves,
  24. // in preference order (most preferable first).
  25. var defaultFIPSCurvePreferences = []CurveID{CurveP256, CurveP384, CurveP521}
  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. TLS_RSA_WITH_AES_128_GCM_SHA256,
  49. TLS_RSA_WITH_AES_256_GCM_SHA384,
  50. }
  51. // fipsCipherSuites replaces c.cipherSuites in FIPS-only mode.
  52. func fipsCipherSuites(c *Config) []uint16 {
  53. if c == nil || c.CipherSuites == nil {
  54. return defaultCipherSuitesFIPS
  55. }
  56. list := make([]uint16, 0, len(defaultCipherSuitesFIPS))
  57. for _, id := range c.CipherSuites {
  58. for _, allowed := range defaultCipherSuitesFIPS {
  59. if id == allowed {
  60. list = append(list, id)
  61. break
  62. }
  63. }
  64. }
  65. return list
  66. }
  67. // fipsSupportedSignatureAlgorithms currently are a subset of
  68. // defaultSupportedSignatureAlgorithms without Ed25519 and SHA-1.
  69. var fipsSupportedSignatureAlgorithms = []SignatureScheme{
  70. PSSWithSHA256,
  71. PSSWithSHA384,
  72. PSSWithSHA512,
  73. PKCS1WithSHA256,
  74. ECDSAWithP256AndSHA256,
  75. PKCS1WithSHA384,
  76. ECDSAWithP384AndSHA384,
  77. PKCS1WithSHA512,
  78. ECDSAWithP521AndSHA512,
  79. }
  80. // supportedSignatureAlgorithms returns the supported signature algorithms.
  81. func supportedSignatureAlgorithms() []SignatureScheme {
  82. if !needFIPS() {
  83. return defaultSupportedSignatureAlgorithms
  84. }
  85. return fipsSupportedSignatureAlgorithms
  86. }