auth.go 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. "bytes"
  7. "crypto"
  8. "crypto/ecdsa"
  9. "crypto/ed25519"
  10. "crypto/elliptic"
  11. "crypto/rsa"
  12. "errors"
  13. "fmt"
  14. "hash"
  15. "io"
  16. )
  17. // verifyHandshakeSignature verifies a signature against pre-hashed
  18. // (if required) handshake contents.
  19. func verifyHandshakeSignature(sigType uint8, pubkey crypto.PublicKey, hashFunc crypto.Hash, signed, sig []byte) error {
  20. switch sigType {
  21. case signatureECDSA:
  22. pubKey, ok := pubkey.(*ecdsa.PublicKey)
  23. if !ok {
  24. return fmt.Errorf("expected an ECDSA public key, got %T", pubkey)
  25. }
  26. if !ecdsa.VerifyASN1(pubKey, signed, sig) {
  27. return errors.New("ECDSA verification failure")
  28. }
  29. case signatureEd25519:
  30. pubKey, ok := pubkey.(ed25519.PublicKey)
  31. if !ok {
  32. return fmt.Errorf("expected an Ed25519 public key, got %T", pubkey)
  33. }
  34. if !ed25519.Verify(pubKey, signed, sig) {
  35. return errors.New("Ed25519 verification failure")
  36. }
  37. case signaturePKCS1v15:
  38. pubKey, ok := pubkey.(*rsa.PublicKey)
  39. if !ok {
  40. return fmt.Errorf("expected an RSA public key, got %T", pubkey)
  41. }
  42. if err := rsa.VerifyPKCS1v15(pubKey, hashFunc, signed, sig); err != nil {
  43. return err
  44. }
  45. case signatureRSAPSS:
  46. pubKey, ok := pubkey.(*rsa.PublicKey)
  47. if !ok {
  48. return fmt.Errorf("expected an RSA public key, got %T", pubkey)
  49. }
  50. signOpts := &rsa.PSSOptions{SaltLength: rsa.PSSSaltLengthEqualsHash}
  51. if err := rsa.VerifyPSS(pubKey, hashFunc, signed, sig, signOpts); err != nil {
  52. return err
  53. }
  54. default:
  55. return errors.New("internal error: unknown signature type")
  56. }
  57. return nil
  58. }
  59. const (
  60. serverSignatureContext = "TLS 1.3, server CertificateVerify\x00"
  61. clientSignatureContext = "TLS 1.3, client CertificateVerify\x00"
  62. )
  63. var signaturePadding = []byte{
  64. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  65. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  66. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  67. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  68. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  69. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  70. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  71. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  72. }
  73. // signedMessage returns the pre-hashed (if necessary) message to be signed by
  74. // certificate keys in TLS 1.3. See RFC 8446, Section 4.4.3.
  75. func signedMessage(sigHash crypto.Hash, context string, transcript hash.Hash) []byte {
  76. if sigHash == directSigning {
  77. b := &bytes.Buffer{}
  78. b.Write(signaturePadding)
  79. io.WriteString(b, context)
  80. b.Write(transcript.Sum(nil))
  81. return b.Bytes()
  82. }
  83. h := sigHash.New()
  84. h.Write(signaturePadding)
  85. io.WriteString(h, context)
  86. h.Write(transcript.Sum(nil))
  87. return h.Sum(nil)
  88. }
  89. // typeAndHashFromSignatureScheme returns the corresponding signature type and
  90. // crypto.Hash for a given TLS SignatureScheme.
  91. func typeAndHashFromSignatureScheme(signatureAlgorithm SignatureScheme) (sigType uint8, hash crypto.Hash, err error) {
  92. switch signatureAlgorithm {
  93. case PKCS1WithSHA1, PKCS1WithSHA256, PKCS1WithSHA384, PKCS1WithSHA512:
  94. sigType = signaturePKCS1v15
  95. case PSSWithSHA256, PSSWithSHA384, PSSWithSHA512:
  96. sigType = signatureRSAPSS
  97. case ECDSAWithSHA1, ECDSAWithP256AndSHA256, ECDSAWithP384AndSHA384, ECDSAWithP521AndSHA512:
  98. sigType = signatureECDSA
  99. case Ed25519:
  100. sigType = signatureEd25519
  101. default:
  102. return 0, 0, fmt.Errorf("unsupported signature algorithm: %v", signatureAlgorithm)
  103. }
  104. switch signatureAlgorithm {
  105. case PKCS1WithSHA1, ECDSAWithSHA1:
  106. hash = crypto.SHA1
  107. case PKCS1WithSHA256, PSSWithSHA256, ECDSAWithP256AndSHA256:
  108. hash = crypto.SHA256
  109. case PKCS1WithSHA384, PSSWithSHA384, ECDSAWithP384AndSHA384:
  110. hash = crypto.SHA384
  111. case PKCS1WithSHA512, PSSWithSHA512, ECDSAWithP521AndSHA512:
  112. hash = crypto.SHA512
  113. case Ed25519:
  114. hash = directSigning
  115. default:
  116. return 0, 0, fmt.Errorf("unsupported signature algorithm: %v", signatureAlgorithm)
  117. }
  118. return sigType, hash, nil
  119. }
  120. // legacyTypeAndHashFromPublicKey returns the fixed signature type and crypto.Hash for
  121. // a given public key used with TLS 1.0 and 1.1, before the introduction of
  122. // signature algorithm negotiation.
  123. func legacyTypeAndHashFromPublicKey(pub crypto.PublicKey) (sigType uint8, hash crypto.Hash, err error) {
  124. switch pub.(type) {
  125. case *rsa.PublicKey:
  126. return signaturePKCS1v15, crypto.MD5SHA1, nil
  127. case *ecdsa.PublicKey:
  128. return signatureECDSA, crypto.SHA1, nil
  129. case ed25519.PublicKey:
  130. // RFC 8422 specifies support for Ed25519 in TLS 1.0 and 1.1,
  131. // but it requires holding on to a handshake transcript to do a
  132. // full signature, and not even OpenSSL bothers with the
  133. // complexity, so we can't even test it properly.
  134. return 0, 0, fmt.Errorf("tls: Ed25519 public keys are not supported before TLS 1.2")
  135. default:
  136. return 0, 0, fmt.Errorf("tls: unsupported public key: %T", pub)
  137. }
  138. }
  139. var rsaSignatureSchemes = []struct {
  140. scheme SignatureScheme
  141. minModulusBytes int
  142. maxVersion uint16
  143. }{
  144. // RSA-PSS is used with PSSSaltLengthEqualsHash, and requires
  145. // emLen >= hLen + sLen + 2
  146. {PSSWithSHA256, crypto.SHA256.Size()*2 + 2, VersionTLS13},
  147. {PSSWithSHA384, crypto.SHA384.Size()*2 + 2, VersionTLS13},
  148. {PSSWithSHA512, crypto.SHA512.Size()*2 + 2, VersionTLS13},
  149. // PKCS #1 v1.5 uses prefixes from hashPrefixes in crypto/rsa, and requires
  150. // emLen >= len(prefix) + hLen + 11
  151. // TLS 1.3 dropped support for PKCS #1 v1.5 in favor of RSA-PSS.
  152. {PKCS1WithSHA256, 19 + crypto.SHA256.Size() + 11, VersionTLS12},
  153. {PKCS1WithSHA384, 19 + crypto.SHA384.Size() + 11, VersionTLS12},
  154. {PKCS1WithSHA512, 19 + crypto.SHA512.Size() + 11, VersionTLS12},
  155. {PKCS1WithSHA1, 15 + crypto.SHA1.Size() + 11, VersionTLS12},
  156. }
  157. // signatureSchemesForCertificate returns the list of supported SignatureSchemes
  158. // for a given certificate, based on the public key and the protocol version,
  159. // and optionally filtered by its explicit SupportedSignatureAlgorithms.
  160. //
  161. // This function must be kept in sync with supportedSignatureAlgorithms.
  162. // FIPS filtering is applied in the caller, selectSignatureScheme.
  163. func signatureSchemesForCertificate(version uint16, cert *Certificate) []SignatureScheme {
  164. priv, ok := cert.PrivateKey.(crypto.Signer)
  165. if !ok {
  166. return nil
  167. }
  168. var sigAlgs []SignatureScheme
  169. switch pub := priv.Public().(type) {
  170. case *ecdsa.PublicKey:
  171. if version != VersionTLS13 {
  172. // In TLS 1.2 and earlier, ECDSA algorithms are not
  173. // constrained to a single curve.
  174. sigAlgs = []SignatureScheme{
  175. ECDSAWithP256AndSHA256,
  176. ECDSAWithP384AndSHA384,
  177. ECDSAWithP521AndSHA512,
  178. ECDSAWithSHA1,
  179. }
  180. break
  181. }
  182. switch pub.Curve {
  183. case elliptic.P256():
  184. sigAlgs = []SignatureScheme{ECDSAWithP256AndSHA256}
  185. case elliptic.P384():
  186. sigAlgs = []SignatureScheme{ECDSAWithP384AndSHA384}
  187. case elliptic.P521():
  188. sigAlgs = []SignatureScheme{ECDSAWithP521AndSHA512}
  189. default:
  190. return nil
  191. }
  192. case *rsa.PublicKey:
  193. size := pub.Size()
  194. sigAlgs = make([]SignatureScheme, 0, len(rsaSignatureSchemes))
  195. for _, candidate := range rsaSignatureSchemes {
  196. if size >= candidate.minModulusBytes && version <= candidate.maxVersion {
  197. sigAlgs = append(sigAlgs, candidate.scheme)
  198. }
  199. }
  200. case ed25519.PublicKey:
  201. sigAlgs = []SignatureScheme{Ed25519}
  202. default:
  203. return nil
  204. }
  205. if cert.SupportedSignatureAlgorithms != nil {
  206. var filteredSigAlgs []SignatureScheme
  207. for _, sigAlg := range sigAlgs {
  208. if isSupportedSignatureAlgorithm(sigAlg, cert.SupportedSignatureAlgorithms) {
  209. filteredSigAlgs = append(filteredSigAlgs, sigAlg)
  210. }
  211. }
  212. return filteredSigAlgs
  213. }
  214. return sigAlgs
  215. }
  216. // selectSignatureScheme picks a SignatureScheme from the peer's preference list
  217. // that works with the selected certificate. It's only called for protocol
  218. // versions that support signature algorithms, so TLS 1.2 and 1.3.
  219. func selectSignatureScheme(vers uint16, c *Certificate, peerAlgs []SignatureScheme) (SignatureScheme, error) {
  220. supportedAlgs := signatureSchemesForCertificate(vers, c)
  221. if len(supportedAlgs) == 0 {
  222. return 0, unsupportedCertificateError(c)
  223. }
  224. if len(peerAlgs) == 0 && vers == VersionTLS12 {
  225. // For TLS 1.2, if the client didn't send signature_algorithms then we
  226. // can assume that it supports SHA1. See RFC 5246, Section 7.4.1.4.1.
  227. peerAlgs = []SignatureScheme{PKCS1WithSHA1, ECDSAWithSHA1}
  228. }
  229. // Pick signature scheme in the peer's preference order, as our
  230. // preference order is not configurable.
  231. for _, preferredAlg := range peerAlgs {
  232. if needFIPS() && !isSupportedSignatureAlgorithm(preferredAlg, fipsSupportedSignatureAlgorithms) {
  233. continue
  234. }
  235. if isSupportedSignatureAlgorithm(preferredAlg, supportedAlgs) {
  236. return preferredAlg, nil
  237. }
  238. }
  239. return 0, errors.New("tls: peer doesn't support any of the certificate's signature algorithms")
  240. }
  241. // unsupportedCertificateError returns a helpful error for certificates with
  242. // an unsupported private key.
  243. func unsupportedCertificateError(cert *Certificate) error {
  244. switch cert.PrivateKey.(type) {
  245. case rsa.PrivateKey, ecdsa.PrivateKey:
  246. return fmt.Errorf("tls: unsupported certificate: private key is %T, expected *%T",
  247. cert.PrivateKey, cert.PrivateKey)
  248. case *ed25519.PrivateKey:
  249. return fmt.Errorf("tls: unsupported certificate: private key is *ed25519.PrivateKey, expected ed25519.PrivateKey")
  250. }
  251. signer, ok := cert.PrivateKey.(crypto.Signer)
  252. if !ok {
  253. return fmt.Errorf("tls: certificate private key (%T) does not implement crypto.Signer",
  254. cert.PrivateKey)
  255. }
  256. switch pub := signer.Public().(type) {
  257. case *ecdsa.PublicKey:
  258. switch pub.Curve {
  259. case elliptic.P256():
  260. case elliptic.P384():
  261. case elliptic.P521():
  262. default:
  263. return fmt.Errorf("tls: unsupported certificate curve (%s)", pub.Curve.Params().Name)
  264. }
  265. case *rsa.PublicKey:
  266. return fmt.Errorf("tls: certificate RSA key size too small for supported signature algorithms")
  267. case ed25519.PublicKey:
  268. default:
  269. return fmt.Errorf("tls: unsupported certificate key (%T)", pub)
  270. }
  271. if cert.SupportedSignatureAlgorithms != nil {
  272. return fmt.Errorf("tls: peer doesn't support the certificate custom signature algorithms")
  273. }
  274. return fmt.Errorf("tls: internal error: unsupported key (%T)", cert.PrivateKey)
  275. }