@@ -163,73 +163,64 @@ var rsaSignatureSchemes = []struct {
163163 {PKCS1WithSHA1 , 15 + crypto .SHA1 .Size () + 11 },
164164}
165165
166- // signatureSchemesForCertificate returns the list of supported SignatureSchemes
167- // for a given certificate, based on the public key and the protocol version,
168- // and optionally filtered by its explicit SupportedSignatureAlgorithms.
169- func signatureSchemesForCertificate (version uint16 , cert * Certificate ) []SignatureScheme {
170- priv , ok := cert .PrivateKey .(crypto.Signer )
171- if ! ok {
172- return nil
173- }
174-
175- var sigAlgs []SignatureScheme
176- switch pub := priv .Public ().(type ) {
166+ func signatureSchemesForPublicKey (version uint16 , pub crypto.PublicKey ) []SignatureScheme {
167+ switch pub := pub .(type ) {
177168 case * ecdsa.PublicKey :
178- if version != VersionTLS13 {
169+ if version < VersionTLS13 {
179170 // In TLS 1.2 and earlier, ECDSA algorithms are not
180171 // constrained to a single curve.
181- sigAlgs = []SignatureScheme {
172+ return []SignatureScheme {
182173 ECDSAWithP256AndSHA256 ,
183174 ECDSAWithP384AndSHA384 ,
184175 ECDSAWithP521AndSHA512 ,
185176 ECDSAWithSHA1 ,
186177 }
187- break
188178 }
189179 switch pub .Curve {
190180 case elliptic .P256 ():
191- sigAlgs = []SignatureScheme {ECDSAWithP256AndSHA256 }
181+ return []SignatureScheme {ECDSAWithP256AndSHA256 }
192182 case elliptic .P384 ():
193- sigAlgs = []SignatureScheme {ECDSAWithP384AndSHA384 }
183+ return []SignatureScheme {ECDSAWithP384AndSHA384 }
194184 case elliptic .P521 ():
195- sigAlgs = []SignatureScheme {ECDSAWithP521AndSHA512 }
185+ return []SignatureScheme {ECDSAWithP521AndSHA512 }
196186 default :
197187 return nil
198188 }
199189 case * rsa.PublicKey :
200190 size := pub .Size ()
201- sigAlgs = make ([]SignatureScheme , 0 , len (rsaSignatureSchemes ))
191+ sigAlgs : = make ([]SignatureScheme , 0 , len (rsaSignatureSchemes ))
202192 for _ , candidate := range rsaSignatureSchemes {
203193 if size >= candidate .minModulusBytes {
204194 sigAlgs = append (sigAlgs , candidate .scheme )
205195 }
206196 }
197+ return sigAlgs
207198 case ed25519.PublicKey :
208- sigAlgs = []SignatureScheme {Ed25519 }
199+ return []SignatureScheme {Ed25519 }
209200 default :
210201 return nil
211202 }
212-
213- if cert .SupportedSignatureAlgorithms != nil {
214- sigAlgs = slices .DeleteFunc (sigAlgs , func (sigAlg SignatureScheme ) bool {
215- return ! isSupportedSignatureAlgorithm (sigAlg , cert .SupportedSignatureAlgorithms )
216- })
217- }
218-
219- // Filter out any unsupported signature algorithms, for example due to
220- // FIPS 140-3 policy, or any downstream changes to defaults.go.
221- sigAlgs = slices .DeleteFunc (sigAlgs , func (sigAlg SignatureScheme ) bool {
222- return isDisabledSignatureAlgorithm (version , sigAlg , false )
223- })
224-
225- return sigAlgs
226203}
227204
228205// selectSignatureScheme picks a SignatureScheme from the peer's preference list
229206// that works with the selected certificate. It's only called for protocol
230207// versions that support signature algorithms, so TLS 1.2 and 1.3.
231208func selectSignatureScheme (vers uint16 , c * Certificate , peerAlgs []SignatureScheme ) (SignatureScheme , error ) {
232- supportedAlgs := signatureSchemesForCertificate (vers , c )
209+ priv , ok := c .PrivateKey .(crypto.Signer )
210+ if ! ok {
211+ return 0 , unsupportedCertificateError (c )
212+ }
213+ supportedAlgs := signatureSchemesForPublicKey (vers , priv .Public ())
214+ if c .SupportedSignatureAlgorithms != nil {
215+ supportedAlgs = slices .DeleteFunc (supportedAlgs , func (sigAlg SignatureScheme ) bool {
216+ return ! isSupportedSignatureAlgorithm (sigAlg , c .SupportedSignatureAlgorithms )
217+ })
218+ }
219+ // Filter out any unsupported signature algorithms, for example due to
220+ // FIPS 140-3 policy, tlssha1=0, or protocol version.
221+ supportedAlgs = slices .DeleteFunc (supportedAlgs , func (sigAlg SignatureScheme ) bool {
222+ return isDisabledSignatureAlgorithm (vers , sigAlg , false )
223+ })
233224 if len (supportedAlgs ) == 0 {
234225 return 0 , unsupportedCertificateError (c )
235226 }
0 commit comments