auth.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. package tls
  5. import (
  6. "crypto"
  7. "crypto/ecdsa"
  8. "crypto/elliptic"
  9. "crypto/rsa"
  10. "encoding/asn1"
  11. "errors"
  12. "fmt"
  13. "hash"
  14. "io"
  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 := hashFromSignatureScheme(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. // verifyHandshakeSignature verifies a signature against pre-hashed handshake
  67. // contents.
  68. func verifyHandshakeSignature(sigType uint8, pubkey crypto.PublicKey, hashFunc crypto.Hash, digest, sig []byte) error {
  69. switch sigType {
  70. case signatureECDSA:
  71. pubKey, ok := pubkey.(*ecdsa.PublicKey)
  72. if !ok {
  73. return errors.New("tls: ECDSA signing requires a ECDSA public key")
  74. }
  75. ecdsaSig := new(ecdsaSignature)
  76. if _, err := asn1.Unmarshal(sig, ecdsaSig); err != nil {
  77. return err
  78. }
  79. if ecdsaSig.R.Sign() <= 0 || ecdsaSig.S.Sign() <= 0 {
  80. return errors.New("tls: ECDSA signature contained zero or negative values")
  81. }
  82. if !ecdsa.Verify(pubKey, digest, ecdsaSig.R, ecdsaSig.S) {
  83. return errors.New("tls: ECDSA verification failure")
  84. }
  85. case signaturePKCS1v15:
  86. pubKey, ok := pubkey.(*rsa.PublicKey)
  87. if !ok {
  88. return errors.New("tls: RSA signing requires a RSA public key")
  89. }
  90. if err := rsa.VerifyPKCS1v15(pubKey, hashFunc, digest, sig); err != nil {
  91. return err
  92. }
  93. case signatureRSAPSS:
  94. pubKey, ok := pubkey.(*rsa.PublicKey)
  95. if !ok {
  96. return errors.New("tls: RSA signing requires a RSA public key")
  97. }
  98. signOpts := &rsa.PSSOptions{SaltLength: rsa.PSSSaltLengthEqualsHash}
  99. if err := rsa.VerifyPSS(pubKey, hashFunc, digest, sig, signOpts); err != nil {
  100. return err
  101. }
  102. default:
  103. return errors.New("tls: unknown signature algorithm")
  104. }
  105. return nil
  106. }
  107. const (
  108. serverSignatureContext = "TLS 1.3, server CertificateVerify\x00"
  109. clientSignatureContext = "TLS 1.3, client CertificateVerify\x00"
  110. )
  111. var signaturePadding = []byte{
  112. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  113. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  114. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  115. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  116. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  117. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  118. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  119. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  120. }
  121. // writeSignedMessage writes the content to be signed by certificate keys in TLS
  122. // 1.3 to sigHash. See RFC 8446, Section 4.4.3.
  123. func writeSignedMessage(sigHash io.Writer, context string, transcript hash.Hash) {
  124. sigHash.Write(signaturePadding)
  125. io.WriteString(sigHash, context)
  126. sigHash.Write(transcript.Sum(nil))
  127. }
  128. // signatureSchemesForCertificate returns the list of supported SignatureSchemes
  129. // for a given certificate, based on the public key and the protocol version. It
  130. // does not support the crypto.Decrypter interface, so shouldn't be used on the
  131. // server side in TLS 1.2 and earlier.
  132. func signatureSchemesForCertificate(version uint16, cert *Certificate) []SignatureScheme {
  133. priv, ok := cert.PrivateKey.(crypto.Signer)
  134. if !ok {
  135. return nil
  136. }
  137. switch pub := priv.Public().(type) {
  138. case *ecdsa.PublicKey:
  139. if version != VersionTLS13 {
  140. // In TLS 1.2 and earlier, ECDSA algorithms are not
  141. // constrained to a single curve.
  142. return []SignatureScheme{
  143. ECDSAWithP256AndSHA256,
  144. ECDSAWithP384AndSHA384,
  145. ECDSAWithP521AndSHA512,
  146. ECDSAWithSHA1,
  147. }
  148. }
  149. switch pub.Curve {
  150. case elliptic.P256():
  151. return []SignatureScheme{ECDSAWithP256AndSHA256}
  152. case elliptic.P384():
  153. return []SignatureScheme{ECDSAWithP384AndSHA384}
  154. case elliptic.P521():
  155. return []SignatureScheme{ECDSAWithP521AndSHA512}
  156. default:
  157. return nil
  158. }
  159. case *rsa.PublicKey:
  160. if version != VersionTLS13 {
  161. return []SignatureScheme{
  162. PSSWithSHA256,
  163. PSSWithSHA384,
  164. PSSWithSHA512,
  165. PKCS1WithSHA256,
  166. PKCS1WithSHA384,
  167. PKCS1WithSHA512,
  168. PKCS1WithSHA1,
  169. }
  170. }
  171. // RSA keys with RSA-PSS OID are not supported by crypto/x509.
  172. return []SignatureScheme{
  173. PSSWithSHA256,
  174. PSSWithSHA384,
  175. PSSWithSHA512,
  176. }
  177. default:
  178. return nil
  179. }
  180. }
  181. // unsupportedCertificateError returns a helpful error for certificates with
  182. // an unsupported private key.
  183. func unsupportedCertificateError(cert *Certificate) error {
  184. switch cert.PrivateKey.(type) {
  185. case rsa.PrivateKey, ecdsa.PrivateKey:
  186. return fmt.Errorf("tls: unsupported certificate: private key is %T, expected *%T",
  187. cert.PrivateKey, cert.PrivateKey)
  188. }
  189. signer, ok := cert.PrivateKey.(crypto.Signer)
  190. if !ok {
  191. return fmt.Errorf("tls: certificate private key (%T) does not implement crypto.Signer",
  192. cert.PrivateKey)
  193. }
  194. switch pub := signer.Public().(type) {
  195. case *ecdsa.PublicKey:
  196. switch pub.Curve {
  197. case elliptic.P256():
  198. case elliptic.P384():
  199. case elliptic.P521():
  200. default:
  201. return fmt.Errorf("tls: unsupported certificate curve (%s)", pub.Curve.Params().Name)
  202. }
  203. case *rsa.PublicKey:
  204. default:
  205. return fmt.Errorf("tls: unsupported certificate key (%T)", pub)
  206. }
  207. return fmt.Errorf("tls: internal error: unsupported key (%T)", cert.PrivateKey)
  208. }