auth.go 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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 qtls
  5. import (
  6. "bytes"
  7. "crypto"
  8. "crypto/ecdsa"
  9. "crypto/ed25519"
  10. "crypto/elliptic"
  11. "crypto/rsa"
  12. "encoding/asn1"
  13. "errors"
  14. "fmt"
  15. "hash"
  16. "io"
  17. )
  18. // pickSignatureAlgorithm selects a signature algorithm that is compatible with
  19. // the given public key and the list of algorithms from the peer and this side.
  20. // The lists of signature algorithms (peerSigAlgs and ourSigAlgs) are ignored
  21. // for tlsVersion < VersionTLS12.
  22. //
  23. // The returned SignatureScheme codepoint is only meaningful for TLS 1.2,
  24. // previous TLS versions have a fixed hash function.
  25. func pickSignatureAlgorithm(pubkey crypto.PublicKey, peerSigAlgs, ourSigAlgs []SignatureScheme, tlsVersion uint16) (sigAlg SignatureScheme, sigType uint8, hashFunc crypto.Hash, err error) {
  26. if tlsVersion < VersionTLS12 || len(peerSigAlgs) == 0 {
  27. // For TLS 1.1 and before, the signature algorithm could not be
  28. // negotiated and the hash is fixed based on the signature type. For TLS
  29. // 1.2, if the client didn't send signature_algorithms extension then we
  30. // can assume that it supports SHA1. See RFC 5246, Section 7.4.1.4.1.
  31. switch pubkey.(type) {
  32. case *rsa.PublicKey:
  33. if tlsVersion < VersionTLS12 {
  34. return 0, signaturePKCS1v15, crypto.MD5SHA1, nil
  35. } else {
  36. return PKCS1WithSHA1, signaturePKCS1v15, crypto.SHA1, nil
  37. }
  38. case *ecdsa.PublicKey:
  39. return ECDSAWithSHA1, signatureECDSA, crypto.SHA1, nil
  40. case ed25519.PublicKey:
  41. if tlsVersion < VersionTLS12 {
  42. // RFC 8422 specifies support for Ed25519 in TLS 1.0 and 1.1,
  43. // but it requires holding on to a handshake transcript to do a
  44. // full signature, and not even OpenSSL bothers with the
  45. // complexity, so we can't even test it properly.
  46. return 0, 0, 0, fmt.Errorf("tls: Ed25519 public keys are not supported before TLS 1.2")
  47. }
  48. return Ed25519, signatureEd25519, directSigning, nil
  49. default:
  50. return 0, 0, 0, fmt.Errorf("tls: unsupported public key: %T", pubkey)
  51. }
  52. }
  53. for _, sigAlg := range peerSigAlgs {
  54. if !isSupportedSignatureAlgorithm(sigAlg, ourSigAlgs) {
  55. continue
  56. }
  57. hashAlg, err := hashFromSignatureScheme(sigAlg)
  58. if err != nil {
  59. panic("tls: supported signature algorithm has an unknown hash function")
  60. }
  61. sigType := signatureFromSignatureScheme(sigAlg)
  62. switch pubkey.(type) {
  63. case *rsa.PublicKey:
  64. if sigType == signaturePKCS1v15 || sigType == signatureRSAPSS {
  65. return sigAlg, sigType, hashAlg, nil
  66. }
  67. case *ecdsa.PublicKey:
  68. if sigType == signatureECDSA {
  69. return sigAlg, sigType, hashAlg, nil
  70. }
  71. case ed25519.PublicKey:
  72. if sigType == signatureEd25519 {
  73. return sigAlg, sigType, hashAlg, nil
  74. }
  75. default:
  76. return 0, 0, 0, fmt.Errorf("tls: unsupported public key: %T", pubkey)
  77. }
  78. }
  79. return 0, 0, 0, errors.New("tls: peer doesn't support any common signature algorithms")
  80. }
  81. // verifyHandshakeSignature verifies a signature against pre-hashed
  82. // (if required) handshake contents.
  83. func verifyHandshakeSignature(sigType uint8, pubkey crypto.PublicKey, hashFunc crypto.Hash, signed, sig []byte) error {
  84. switch sigType {
  85. case signatureECDSA:
  86. pubKey, ok := pubkey.(*ecdsa.PublicKey)
  87. if !ok {
  88. return errors.New("tls: ECDSA signing requires a ECDSA public key")
  89. }
  90. ecdsaSig := new(ecdsaSignature)
  91. if _, err := asn1.Unmarshal(sig, ecdsaSig); err != nil {
  92. return err
  93. }
  94. if ecdsaSig.R.Sign() <= 0 || ecdsaSig.S.Sign() <= 0 {
  95. return errors.New("tls: ECDSA signature contained zero or negative values")
  96. }
  97. if !ecdsa.Verify(pubKey, signed, ecdsaSig.R, ecdsaSig.S) {
  98. return errors.New("tls: ECDSA verification failure")
  99. }
  100. case signatureEd25519:
  101. pubKey, ok := pubkey.(ed25519.PublicKey)
  102. if !ok {
  103. return errors.New("tls: Ed25519 signing requires a Ed25519 public key")
  104. }
  105. if !ed25519.Verify(pubKey, signed, sig) {
  106. return errors.New("tls: Ed25519 verification failure")
  107. }
  108. case signaturePKCS1v15:
  109. pubKey, ok := pubkey.(*rsa.PublicKey)
  110. if !ok {
  111. return errors.New("tls: RSA signing requires a RSA public key")
  112. }
  113. if err := rsa.VerifyPKCS1v15(pubKey, hashFunc, signed, sig); err != nil {
  114. return err
  115. }
  116. case signatureRSAPSS:
  117. pubKey, ok := pubkey.(*rsa.PublicKey)
  118. if !ok {
  119. return errors.New("tls: RSA signing requires a RSA public key")
  120. }
  121. signOpts := &rsa.PSSOptions{SaltLength: rsa.PSSSaltLengthEqualsHash}
  122. if err := rsa.VerifyPSS(pubKey, hashFunc, signed, sig, signOpts); err != nil {
  123. return err
  124. }
  125. default:
  126. return errors.New("tls: unknown signature algorithm")
  127. }
  128. return nil
  129. }
  130. const (
  131. serverSignatureContext = "TLS 1.3, server CertificateVerify\x00"
  132. clientSignatureContext = "TLS 1.3, client CertificateVerify\x00"
  133. )
  134. var signaturePadding = []byte{
  135. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  136. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  137. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  138. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  139. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  140. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  141. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  142. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  143. }
  144. // signedMessage returns the pre-hashed (if necessary) message to be signed by
  145. // certificate keys in TLS 1.3. See RFC 8446, Section 4.4.3.
  146. func signedMessage(sigHash crypto.Hash, context string, transcript hash.Hash) []byte {
  147. if sigHash == directSigning {
  148. b := &bytes.Buffer{}
  149. b.Write(signaturePadding)
  150. io.WriteString(b, context)
  151. b.Write(transcript.Sum(nil))
  152. return b.Bytes()
  153. }
  154. h := sigHash.New()
  155. h.Write(signaturePadding)
  156. io.WriteString(h, context)
  157. h.Write(transcript.Sum(nil))
  158. return h.Sum(nil)
  159. }
  160. // signatureSchemesForCertificate returns the list of supported SignatureSchemes
  161. // for a given certificate, based on the public key and the protocol version.
  162. //
  163. // It does not support the crypto.Decrypter interface, so shouldn't be used for
  164. // server certificates in TLS 1.2 and earlier, and it must be kept in sync with
  165. // supportedSignatureAlgorithms.
  166. func signatureSchemesForCertificate(version uint16, cert *Certificate) []SignatureScheme {
  167. priv, ok := cert.PrivateKey.(crypto.Signer)
  168. if !ok {
  169. return nil
  170. }
  171. switch pub := priv.Public().(type) {
  172. case *ecdsa.PublicKey:
  173. if version != VersionTLS13 {
  174. // In TLS 1.2 and earlier, ECDSA algorithms are not
  175. // constrained to a single curve.
  176. return []SignatureScheme{
  177. ECDSAWithP256AndSHA256,
  178. ECDSAWithP384AndSHA384,
  179. ECDSAWithP521AndSHA512,
  180. ECDSAWithSHA1,
  181. }
  182. }
  183. switch pub.Curve {
  184. case elliptic.P256():
  185. return []SignatureScheme{ECDSAWithP256AndSHA256}
  186. case elliptic.P384():
  187. return []SignatureScheme{ECDSAWithP384AndSHA384}
  188. case elliptic.P521():
  189. return []SignatureScheme{ECDSAWithP521AndSHA512}
  190. default:
  191. return nil
  192. }
  193. case *rsa.PublicKey:
  194. if version != VersionTLS13 {
  195. return []SignatureScheme{
  196. PKCS1WithSHA256,
  197. PKCS1WithSHA384,
  198. PKCS1WithSHA512,
  199. PKCS1WithSHA1,
  200. }
  201. }
  202. return []SignatureScheme{
  203. PSSWithSHA256,
  204. PSSWithSHA384,
  205. PSSWithSHA512,
  206. }
  207. case ed25519.PublicKey:
  208. return []SignatureScheme{Ed25519}
  209. default:
  210. return nil
  211. }
  212. }
  213. // unsupportedCertificateError returns a helpful error for certificates with
  214. // an unsupported private key.
  215. func unsupportedCertificateError(cert *Certificate) error {
  216. switch cert.PrivateKey.(type) {
  217. case rsa.PrivateKey, ecdsa.PrivateKey:
  218. return fmt.Errorf("tls: unsupported certificate: private key is %T, expected *%T",
  219. cert.PrivateKey, cert.PrivateKey)
  220. case *ed25519.PrivateKey:
  221. return fmt.Errorf("tls: unsupported certificate: private key is *ed25519.PrivateKey, expected ed25519.PrivateKey")
  222. }
  223. signer, ok := cert.PrivateKey.(crypto.Signer)
  224. if !ok {
  225. return fmt.Errorf("tls: certificate private key (%T) does not implement crypto.Signer",
  226. cert.PrivateKey)
  227. }
  228. switch pub := signer.Public().(type) {
  229. case *ecdsa.PublicKey:
  230. switch pub.Curve {
  231. case elliptic.P256():
  232. case elliptic.P384():
  233. case elliptic.P521():
  234. default:
  235. return fmt.Errorf("tls: unsupported certificate curve (%s)", pub.Curve.Params().Name)
  236. }
  237. case *rsa.PublicKey:
  238. case ed25519.PublicKey:
  239. default:
  240. return fmt.Errorf("tls: unsupported certificate key (%T)", pub)
  241. }
  242. return fmt.Errorf("tls: internal error: unsupported key (%T)", cert.PrivateKey)
  243. }