]> Cypherpunks.ru repositories - gostls13.git/blob - src/crypto/tls/boring.go
[dev.boringcrypto] all: merge commit 9d0819b27c (CL 314609) into dev.boringcrypto
[gostls13.git] / src / crypto / tls / boring.go
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
5 package tls
6
7 import (
8         "crypto/ecdsa"
9         "crypto/internal/boring/fipstls"
10         "crypto/rsa"
11         "crypto/x509"
12 )
13
14 // needFIPS returns fipstls.Required(); it avoids a new import in common.go.
15 func needFIPS() bool {
16         return fipstls.Required()
17 }
18
19 // fipsMinVersion replaces c.minVersion in FIPS-only mode.
20 func fipsMinVersion(c *Config) uint16 {
21         // FIPS requires TLS 1.2.
22         return VersionTLS12
23 }
24
25 // fipsMaxVersion replaces c.maxVersion in FIPS-only mode.
26 func fipsMaxVersion(c *Config) uint16 {
27         // FIPS requires TLS 1.2.
28         return VersionTLS12
29 }
30
31 // default defaultFIPSCurvePreferences is the FIPS-allowed curves,
32 // in preference order (most preferable first).
33 var defaultFIPSCurvePreferences = []CurveID{CurveP256, CurveP384, CurveP521}
34
35 // fipsCurvePreferences replaces c.curvePreferences in FIPS-only mode.
36 func fipsCurvePreferences(c *Config) []CurveID {
37         if c == nil || len(c.CurvePreferences) == 0 {
38                 return defaultFIPSCurvePreferences
39         }
40         var list []CurveID
41         for _, id := range c.CurvePreferences {
42                 for _, allowed := range defaultFIPSCurvePreferences {
43                         if id == allowed {
44                                 list = append(list, id)
45                                 break
46                         }
47                 }
48         }
49         return list
50 }
51
52 // defaultCipherSuitesFIPS are the FIPS-allowed cipher suites.
53 var defaultCipherSuitesFIPS = []uint16{
54         TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
55         TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
56         TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
57         TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
58         TLS_RSA_WITH_AES_128_GCM_SHA256,
59         TLS_RSA_WITH_AES_256_GCM_SHA384,
60 }
61
62 // fipsCipherSuites replaces c.cipherSuites in FIPS-only mode.
63 func fipsCipherSuites(c *Config) []uint16 {
64         if c == nil || c.CipherSuites == nil {
65                 return defaultCipherSuitesFIPS
66         }
67         list := make([]uint16, 0, len(defaultCipherSuitesFIPS))
68         for _, id := range c.CipherSuites {
69                 for _, allowed := range defaultCipherSuitesFIPS {
70                         if id == allowed {
71                                 list = append(list, id)
72                                 break
73                         }
74                 }
75         }
76         return list
77 }
78
79 // isBoringCertificate reports whether a certificate may be used
80 // when constructing a verified chain.
81 // It is called for each leaf, intermediate, and root certificate.
82 func isBoringCertificate(c *x509.Certificate) bool {
83         if !needFIPS() {
84                 // Everything is OK if we haven't forced FIPS-only mode.
85                 return true
86         }
87
88         // Otherwise the key must be RSA 2048, RSA 3072, or ECDSA P-256.
89         switch k := c.PublicKey.(type) {
90         default:
91                 return false
92         case *rsa.PublicKey:
93                 if size := k.N.BitLen(); size != 2048 && size != 3072 {
94                         return false
95                 }
96         case *ecdsa.PublicKey:
97                 if name := k.Curve.Params().Name; name != "P-256" && name != "P-384" {
98                         return false
99                 }
100         }
101
102         return true
103 }
104
105 // fipsSupportedSignatureAlgorithms currently are a subset of
106 // defaultSupportedSignatureAlgorithms without Ed25519 and SHA-1.
107 var fipsSupportedSignatureAlgorithms = []SignatureScheme{
108         PSSWithSHA256,
109         PSSWithSHA384,
110         PSSWithSHA512,
111         PKCS1WithSHA256,
112         ECDSAWithP256AndSHA256,
113         PKCS1WithSHA384,
114         ECDSAWithP384AndSHA384,
115         PKCS1WithSHA512,
116         ECDSAWithP521AndSHA512,
117 }
118
119 // supportedSignatureAlgorithms returns the supported signature algorithms.
120 func supportedSignatureAlgorithms() []SignatureScheme {
121         if !needFIPS() {
122                 return defaultSupportedSignatureAlgorithms
123         }
124         return fipsSupportedSignatureAlgorithms
125 }
126
127 var testingOnlyForceClientHelloSignatureAlgorithms []SignatureScheme