Alternative TLS implementation in Go
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

108 regels
3.5 KiB

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