]> Cypherpunks.ru repositories - gostls13.git/blob - src/crypto/tls/auth.go
crypto/tls,crypto/x509: normalize RFC references
[gostls13.git] / src / crypto / tls / auth.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"
9         "crypto/ecdsa"
10         "crypto/rsa"
11         "encoding/asn1"
12         "errors"
13         "fmt"
14 )
15
16 // pickSignatureAlgorithm selects a signature algorithm that is compatible with
17 // the given public key and the list of algorithms from the peer and this side.
18 // The lists of signature algorithms (peerSigAlgs and ourSigAlgs) are ignored
19 // for tlsVersion < VersionTLS12.
20 //
21 // The returned SignatureScheme codepoint is only meaningful for TLS 1.2,
22 // previous TLS versions have a fixed hash function.
23 func pickSignatureAlgorithm(pubkey crypto.PublicKey, peerSigAlgs, ourSigAlgs []SignatureScheme, tlsVersion uint16) (sigAlg SignatureScheme, sigType uint8, hashFunc crypto.Hash, err error) {
24         if tlsVersion < VersionTLS12 || len(peerSigAlgs) == 0 {
25                 // For TLS 1.1 and before, the signature algorithm could not be
26                 // negotiated and the hash is fixed based on the signature type. For TLS
27                 // 1.2, if the client didn't send signature_algorithms extension then we
28                 // can assume that it supports SHA1. See RFC 5246, Section 7.4.1.4.1.
29                 switch pubkey.(type) {
30                 case *rsa.PublicKey:
31                         if tlsVersion < VersionTLS12 {
32                                 return 0, signaturePKCS1v15, crypto.MD5SHA1, nil
33                         } else {
34                                 return PKCS1WithSHA1, signaturePKCS1v15, crypto.SHA1, nil
35                         }
36                 case *ecdsa.PublicKey:
37                         return ECDSAWithSHA1, signatureECDSA, crypto.SHA1, nil
38                 default:
39                         return 0, 0, 0, fmt.Errorf("tls: unsupported public key: %T", pubkey)
40                 }
41         }
42         for _, sigAlg := range peerSigAlgs {
43                 if !isSupportedSignatureAlgorithm(sigAlg, ourSigAlgs) {
44                         continue
45                 }
46                 hashAlg, err := lookupTLSHash(sigAlg)
47                 if err != nil {
48                         panic("tls: supported signature algorithm has an unknown hash function")
49                 }
50                 sigType := signatureFromSignatureScheme(sigAlg)
51                 switch pubkey.(type) {
52                 case *rsa.PublicKey:
53                         if sigType == signaturePKCS1v15 || sigType == signatureRSAPSS {
54                                 return sigAlg, sigType, hashAlg, nil
55                         }
56                 case *ecdsa.PublicKey:
57                         if sigType == signatureECDSA {
58                                 return sigAlg, sigType, hashAlg, nil
59                         }
60                 default:
61                         return 0, 0, 0, fmt.Errorf("tls: unsupported public key: %T", pubkey)
62                 }
63         }
64         return 0, 0, 0, errors.New("tls: peer doesn't support any common signature algorithms")
65 }
66
67 // verifyHandshakeSignature verifies a signature against pre-hashed handshake
68 // contents.
69 func verifyHandshakeSignature(sigType uint8, pubkey crypto.PublicKey, hashFunc crypto.Hash, digest, sig []byte) error {
70         switch sigType {
71         case signatureECDSA:
72                 pubKey, ok := pubkey.(*ecdsa.PublicKey)
73                 if !ok {
74                         return errors.New("tls: ECDSA signing requires a ECDSA public key")
75                 }
76                 ecdsaSig := new(ecdsaSignature)
77                 if _, err := asn1.Unmarshal(sig, ecdsaSig); err != nil {
78                         return err
79                 }
80                 if ecdsaSig.R.Sign() <= 0 || ecdsaSig.S.Sign() <= 0 {
81                         return errors.New("tls: ECDSA signature contained zero or negative values")
82                 }
83                 if !ecdsa.Verify(pubKey, digest, ecdsaSig.R, ecdsaSig.S) {
84                         return errors.New("tls: ECDSA verification failure")
85                 }
86         case signaturePKCS1v15:
87                 pubKey, ok := pubkey.(*rsa.PublicKey)
88                 if !ok {
89                         return errors.New("tls: RSA signing requires a RSA public key")
90                 }
91                 if err := rsa.VerifyPKCS1v15(pubKey, hashFunc, digest, sig); err != nil {
92                         return err
93                 }
94         case signatureRSAPSS:
95                 pubKey, ok := pubkey.(*rsa.PublicKey)
96                 if !ok {
97                         return errors.New("tls: RSA signing requires a RSA public key")
98                 }
99                 signOpts := &rsa.PSSOptions{SaltLength: rsa.PSSSaltLengthEqualsHash}
100                 if err := rsa.VerifyPSS(pubKey, hashFunc, digest, sig, signOpts); err != nil {
101                         return err
102                 }
103         default:
104                 return errors.New("tls: unknown signature algorithm")
105         }
106         return nil
107 }