auth.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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.
  130. func signatureSchemesForCertificate(cert *Certificate) []SignatureScheme {
  131. priv, ok := cert.PrivateKey.(crypto.Signer)
  132. if !ok {
  133. return nil
  134. }
  135. switch priv := priv.Public().(type) {
  136. case *ecdsa.PublicKey:
  137. switch priv.Curve {
  138. case elliptic.P256():
  139. return []SignatureScheme{ECDSAWithP256AndSHA256}
  140. case elliptic.P384():
  141. return []SignatureScheme{ECDSAWithP384AndSHA384}
  142. case elliptic.P521():
  143. return []SignatureScheme{ECDSAWithP521AndSHA512}
  144. default:
  145. return nil
  146. }
  147. case *rsa.PublicKey:
  148. // RSA keys with RSA-PSS OID are not supported by crypto/x509.
  149. return []SignatureScheme{
  150. PSSWithSHA256,
  151. PSSWithSHA384,
  152. PSSWithSHA512,
  153. }
  154. default:
  155. return nil
  156. }
  157. }