@@ -6,13 +6,11 @@ package tls
6
6
7
7
import (
8
8
"crypto"
9
- "crypto/ecdsa"
10
9
"crypto/elliptic"
11
10
"crypto/md5"
12
11
"crypto/rsa"
13
12
"crypto/sha1"
14
13
"crypto/x509"
15
- "encoding/asn1"
16
14
"errors"
17
15
"io"
18
16
"math/big"
@@ -110,58 +108,20 @@ func md5SHA1Hash(slices [][]byte) []byte {
110
108
}
111
109
112
110
// hashForServerKeyExchange hashes the given slices and returns their digest
113
- // and the identifier of the hash function used. The signatureAlgorithm argument
114
- // is only used for >= TLS 1.2 and identifies the hash function to use.
115
- func hashForServerKeyExchange (sigType uint8 , signatureAlgorithm SignatureScheme , version uint16 , slices ... []byte ) ([]byte , crypto.Hash , error ) {
111
+ // using the given hash function.
112
+ func hashForServerKeyExchange (sigType uint8 , hashFunc crypto.Hash , version uint16 , slices ... []byte ) ([]byte , error ) {
116
113
if version >= VersionTLS12 {
117
- if ! isSupportedSignatureAlgorithm (signatureAlgorithm , supportedSignatureAlgorithms ) {
118
- return nil , crypto .Hash (0 ), errors .New ("tls: unsupported hash function used by peer" )
119
- }
120
- hashFunc , err := lookupTLSHash (signatureAlgorithm )
121
- if err != nil {
122
- return nil , crypto .Hash (0 ), err
123
- }
124
114
h := hashFunc .New ()
125
115
for _ , slice := range slices {
126
116
h .Write (slice )
127
117
}
128
118
digest := h .Sum (nil )
129
- return digest , hashFunc , nil
119
+ return digest , nil
130
120
}
131
121
if sigType == signatureECDSA {
132
- return sha1Hash (slices ), crypto .SHA1 , nil
133
- }
134
- return md5SHA1Hash (slices ), crypto .MD5SHA1 , nil
135
- }
136
-
137
- // pickTLS12HashForSignature returns a TLS 1.2 hash identifier for signing a
138
- // ServerKeyExchange given the signature type being used and the client's
139
- // advertised list of supported signature and hash combinations.
140
- func pickTLS12HashForSignature (sigType uint8 , clientList []SignatureScheme ) (SignatureScheme , error ) {
141
- if len (clientList ) == 0 {
142
- // If the client didn't specify any signature_algorithms
143
- // extension then we can assume that it supports SHA1. See
144
- // http://tools.ietf.org/html/rfc5246#section-7.4.1.4.1
145
- switch sigType {
146
- case signatureRSA :
147
- return PKCS1WithSHA1 , nil
148
- case signatureECDSA :
149
- return ECDSAWithSHA1 , nil
150
- default :
151
- return 0 , errors .New ("tls: unknown signature algorithm" )
152
- }
122
+ return sha1Hash (slices ), nil
153
123
}
154
-
155
- for _ , sigAlg := range clientList {
156
- if signatureFromSignatureScheme (sigAlg ) != sigType {
157
- continue
158
- }
159
- if isSupportedSignatureAlgorithm (sigAlg , supportedSignatureAlgorithms ) {
160
- return sigAlg , nil
161
- }
162
- }
163
-
164
- return 0 , errors .New ("tls: client doesn't support any common hash functions" )
124
+ return md5SHA1Hash (slices ), nil
165
125
}
166
126
167
127
func curveForCurveID (id CurveID ) (elliptic.Curve , bool ) {
@@ -247,40 +207,25 @@ NextCandidate:
247
207
serverECDHParams [3 ] = byte (len (ecdhePublic ))
248
208
copy (serverECDHParams [4 :], ecdhePublic )
249
209
250
- var signatureAlgorithm SignatureScheme
251
-
252
- if ka .version >= VersionTLS12 {
253
- var err error
254
- signatureAlgorithm , err = pickTLS12HashForSignature (ka .sigType , clientHello .supportedSignatureAlgorithms )
255
- if err != nil {
256
- return nil , err
257
- }
210
+ priv , ok := cert .PrivateKey .(crypto.Signer )
211
+ if ! ok {
212
+ return nil , errors .New ("tls: certificate private key does not implement crypto.Signer" )
258
213
}
259
214
260
- digest , hashFunc , err := hashForServerKeyExchange ( ka . sigType , signatureAlgorithm , ka . version , clientHello .random , hello . random , serverECDHParams )
215
+ signatureAlgorithm , sigType , hashFunc , err := pickSignatureAlgorithm ( priv . Public (), clientHello .supportedSignatureAlgorithms , supportedSignatureAlgorithms , ka . version )
261
216
if err != nil {
262
217
return nil , err
263
218
}
219
+ if sigType != ka .sigType {
220
+ return nil , errors .New ("tls: certificate cannot be used with the selected cipher suite" )
221
+ }
264
222
265
- priv , ok := cert . PrivateKey .(crypto. Signer )
266
- if ! ok {
267
- return nil , errors . New ( "tls: certificate private key does not implement crypto.Signer" )
223
+ digest , err := hashForServerKeyExchange ( sigType , hashFunc , ka . version , clientHello . random , hello . random , serverECDHParams )
224
+ if err != nil {
225
+ return nil , err
268
226
}
227
+
269
228
var sig []byte
270
- switch ka .sigType {
271
- case signatureECDSA :
272
- _ , ok := priv .Public ().(* ecdsa.PublicKey )
273
- if ! ok {
274
- return nil , errors .New ("tls: ECDHE ECDSA requires an ECDSA server key" )
275
- }
276
- case signatureRSA :
277
- _ , ok := priv .Public ().(* rsa.PublicKey )
278
- if ! ok {
279
- return nil , errors .New ("tls: ECDHE RSA requires a RSA server key" )
280
- }
281
- default :
282
- return nil , errors .New ("tls: unknown ECDHE signature algorithm" )
283
- }
284
229
sig , err = priv .Sign (config .rand (), digest , hashFunc )
285
230
if err != nil {
286
231
return nil , errors .New ("tls: failed to sign ECDHE parameters: " + err .Error ())
@@ -380,53 +325,30 @@ func (ka *ecdheKeyAgreement) processServerKeyExchange(config *Config, clientHell
380
325
if ka .version >= VersionTLS12 {
381
326
// handle SignatureAndHashAlgorithm
382
327
signatureAlgorithm = SignatureScheme (sig [0 ])<< 8 | SignatureScheme (sig [1 ])
383
- if signatureFromSignatureScheme (signatureAlgorithm ) != ka .sigType {
384
- return errServerKeyExchange
385
- }
386
328
sig = sig [2 :]
387
329
if len (sig ) < 2 {
388
330
return errServerKeyExchange
389
331
}
390
332
}
333
+ _ , sigType , hashFunc , err := pickSignatureAlgorithm (cert .PublicKey , []SignatureScheme {signatureAlgorithm }, supportedSignatureAlgorithms , ka .version )
334
+ if err != nil {
335
+ return err
336
+ }
337
+ if sigType != ka .sigType {
338
+ return errServerKeyExchange
339
+ }
340
+
391
341
sigLen := int (sig [0 ])<< 8 | int (sig [1 ])
392
342
if sigLen + 2 != len (sig ) {
393
343
return errServerKeyExchange
394
344
}
395
345
sig = sig [2 :]
396
346
397
- digest , hashFunc , err := hashForServerKeyExchange (ka . sigType , signatureAlgorithm , ka .version , clientHello .random , serverHello .random , serverECDHParams )
347
+ digest , err := hashForServerKeyExchange (sigType , hashFunc , ka .version , clientHello .random , serverHello .random , serverECDHParams )
398
348
if err != nil {
399
349
return err
400
350
}
401
- switch ka .sigType {
402
- case signatureECDSA :
403
- pubKey , ok := cert .PublicKey .(* ecdsa.PublicKey )
404
- if ! ok {
405
- return errors .New ("tls: ECDHE ECDSA requires a ECDSA server public key" )
406
- }
407
- ecdsaSig := new (ecdsaSignature )
408
- if _ , err := asn1 .Unmarshal (sig , ecdsaSig ); err != nil {
409
- return err
410
- }
411
- if ecdsaSig .R .Sign () <= 0 || ecdsaSig .S .Sign () <= 0 {
412
- return errors .New ("tls: ECDSA signature contained zero or negative values" )
413
- }
414
- if ! ecdsa .Verify (pubKey , digest , ecdsaSig .R , ecdsaSig .S ) {
415
- return errors .New ("tls: ECDSA verification failure" )
416
- }
417
- case signatureRSA :
418
- pubKey , ok := cert .PublicKey .(* rsa.PublicKey )
419
- if ! ok {
420
- return errors .New ("tls: ECDHE RSA requires a RSA server public key" )
421
- }
422
- if err := rsa .VerifyPKCS1v15 (pubKey , hashFunc , digest , sig ); err != nil {
423
- return err
424
- }
425
- default :
426
- return errors .New ("tls: unknown ECDHE signature algorithm" )
427
- }
428
-
429
- return nil
351
+ return verifyHandshakeSignature (sigType , cert .PublicKey , hashFunc , digest , sig )
430
352
}
431
353
432
354
func (ka * ecdheKeyAgreement ) generateClientKeyExchange (config * Config , clientHello * clientHelloMsg , cert * x509.Certificate ) ([]byte , * clientKeyExchangeMsg , error ) {
0 commit comments