Alternative TLS implementation in Go
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

102 lignes
4.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 th5
  5. import (
  6. "crypto"
  7. "testing"
  8. )
  9. func TestSignatureSelection(t *testing.T) {
  10. rsaCert := &testRSAPrivateKey.PublicKey
  11. ecdsaCert := &testECDSAPrivateKey.PublicKey
  12. sigsPKCS1WithSHA := []SignatureScheme{PKCS1WithSHA256, PKCS1WithSHA1}
  13. sigsPSSWithSHA := []SignatureScheme{PSSWithSHA256, PSSWithSHA384}
  14. sigsECDSAWithSHA := []SignatureScheme{ECDSAWithP256AndSHA256, ECDSAWithSHA1}
  15. tests := []struct {
  16. pubkey crypto.PublicKey
  17. peerSigAlgs []SignatureScheme
  18. ourSigAlgs []SignatureScheme
  19. tlsVersion uint16
  20. expectedSigAlg SignatureScheme // or 0 if ignored
  21. expectedSigType uint8
  22. expectedHash crypto.Hash
  23. }{
  24. // Hash is fixed for RSA in TLS 1.1 and before.
  25. // https://tools.ietf.org/html/rfc4346#page-44
  26. {rsaCert, nil, nil, VersionTLS11, 0, signaturePKCS1v15, crypto.MD5SHA1},
  27. {rsaCert, nil, nil, VersionTLS10, 0, signaturePKCS1v15, crypto.MD5SHA1},
  28. {rsaCert, nil, nil, VersionSSL30, 0, signaturePKCS1v15, crypto.MD5SHA1},
  29. // Before TLS 1.2, there is no signature_algorithms extension
  30. // nor field in CertificateRequest and digitally-signed and thus
  31. // it should be ignored.
  32. {rsaCert, sigsPKCS1WithSHA, nil, VersionTLS11, 0, signaturePKCS1v15, crypto.MD5SHA1},
  33. {rsaCert, sigsPKCS1WithSHA, sigsPKCS1WithSHA, VersionTLS11, 0, signaturePKCS1v15, crypto.MD5SHA1},
  34. // Use SHA-1 for TLS 1.0 and 1.1 with ECDSA, see https://tools.ietf.org/html/rfc4492#page-20
  35. {ecdsaCert, sigsPKCS1WithSHA, sigsPKCS1WithSHA, VersionTLS11, 0, signatureECDSA, crypto.SHA1},
  36. {ecdsaCert, sigsPKCS1WithSHA, sigsPKCS1WithSHA, VersionTLS10, 0, signatureECDSA, crypto.SHA1},
  37. // TLS 1.2 without signature_algorithms extension
  38. // https://tools.ietf.org/html/rfc5246#page-47
  39. {rsaCert, nil, sigsPKCS1WithSHA, VersionTLS12, PKCS1WithSHA1, signaturePKCS1v15, crypto.SHA1},
  40. {ecdsaCert, nil, sigsPKCS1WithSHA, VersionTLS12, ECDSAWithSHA1, signatureECDSA, crypto.SHA1},
  41. {rsaCert, []SignatureScheme{PKCS1WithSHA1}, sigsPKCS1WithSHA, VersionTLS12, PKCS1WithSHA1, signaturePKCS1v15, crypto.SHA1},
  42. {rsaCert, []SignatureScheme{PKCS1WithSHA256}, sigsPKCS1WithSHA, VersionTLS12, PKCS1WithSHA256, signaturePKCS1v15, crypto.SHA256},
  43. // "sha_hash" may denote hashes other than SHA-1
  44. // https://tools.ietf.org/html/draft-ietf-tls-rfc4492bis-17#page-17
  45. {ecdsaCert, []SignatureScheme{ECDSAWithSHA1}, sigsECDSAWithSHA, VersionTLS12, ECDSAWithSHA1, signatureECDSA, crypto.SHA1},
  46. {ecdsaCert, []SignatureScheme{ECDSAWithP256AndSHA256}, sigsECDSAWithSHA, VersionTLS12, ECDSAWithP256AndSHA256, signatureECDSA, crypto.SHA256},
  47. // RSASSA-PSS is defined in TLS 1.3 for TLS 1.2
  48. // https://tools.ietf.org/html/draft-ietf-tls-tls13-21#page-45
  49. {rsaCert, []SignatureScheme{PSSWithSHA256}, sigsPSSWithSHA, VersionTLS12, PSSWithSHA256, signatureRSAPSS, crypto.SHA256},
  50. }
  51. for testNo, test := range tests {
  52. sigAlg, sigType, hashFunc, err := pickSignatureAlgorithm(test.pubkey, test.peerSigAlgs, test.ourSigAlgs, test.tlsVersion)
  53. if err != nil {
  54. t.Errorf("test[%d]: unexpected error: %v", testNo, err)
  55. }
  56. if test.expectedSigAlg != 0 && test.expectedSigAlg != sigAlg {
  57. t.Errorf("test[%d]: expected signature scheme %#x, got %#x", testNo, test.expectedSigAlg, sigAlg)
  58. }
  59. if test.expectedSigType != sigType {
  60. t.Errorf("test[%d]: expected signature algorithm %#x, got %#x", testNo, test.expectedSigType, sigType)
  61. }
  62. if test.expectedHash != hashFunc {
  63. t.Errorf("test[%d]: expected hash function %#x, got %#x", testNo, test.expectedHash, hashFunc)
  64. }
  65. }
  66. badTests := []struct {
  67. pubkey crypto.PublicKey
  68. peerSigAlgs []SignatureScheme
  69. ourSigAlgs []SignatureScheme
  70. tlsVersion uint16
  71. }{
  72. {rsaCert, sigsECDSAWithSHA, sigsPKCS1WithSHA, VersionTLS12},
  73. {ecdsaCert, sigsPKCS1WithSHA, sigsPKCS1WithSHA, VersionTLS12},
  74. {ecdsaCert, sigsECDSAWithSHA, sigsPKCS1WithSHA, VersionTLS12},
  75. {rsaCert, []SignatureScheme{0}, sigsPKCS1WithSHA, VersionTLS12},
  76. // ECDSA is unspecified for SSL 3.0 in RFC 4492.
  77. // TODO a SSL 3.0 client cannot advertise signature_algorithms,
  78. // but if an application feeds an ECDSA certificate anyway, it
  79. // will be accepted rather than trigger a handshake failure. Ok?
  80. //{ecdsaCert, nil, nil, VersionSSL30},
  81. }
  82. for testNo, test := range badTests {
  83. sigAlg, sigType, hashFunc, err := pickSignatureAlgorithm(test.pubkey, test.peerSigAlgs, test.ourSigAlgs, test.tlsVersion)
  84. if err == nil {
  85. t.Errorf("test[%d]: unexpected success, got %#x %#x %#x", testNo, sigAlg, sigType, hashFunc)
  86. }
  87. }
  88. }