flight4handler_test.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
  2. // SPDX-License-Identifier: MIT
  3. package dtls
  4. import (
  5. "context"
  6. "testing"
  7. "time"
  8. "github.com/pion/dtls/v2/internal/ciphersuite"
  9. "github.com/pion/dtls/v2/pkg/protocol/alert"
  10. "github.com/pion/dtls/v2/pkg/protocol/handshake"
  11. "github.com/pion/transport/v2/test"
  12. )
  13. type flight4TestMockFlightConn struct{}
  14. func (f *flight4TestMockFlightConn) notify(context.Context, alert.Level, alert.Description) error {
  15. return nil
  16. }
  17. func (f *flight4TestMockFlightConn) writePackets(context.Context, []*packet) error { return nil }
  18. func (f *flight4TestMockFlightConn) recvHandshake() <-chan chan struct{} { return nil }
  19. func (f *flight4TestMockFlightConn) setLocalEpoch(uint16) {}
  20. func (f *flight4TestMockFlightConn) handleQueuedPackets(context.Context) error { return nil }
  21. func (f *flight4TestMockFlightConn) sessionKey() []byte { return nil }
  22. type flight4TestMockCipherSuite struct {
  23. ciphersuite.TLSEcdheEcdsaWithAes128GcmSha256
  24. t *testing.T
  25. }
  26. func (f *flight4TestMockCipherSuite) IsInitialized() bool {
  27. f.t.Fatal("IsInitialized called with Certificate but not CertificateVerify")
  28. return true
  29. }
  30. // Assert that if a Client sends a certificate they
  31. // must also send a CertificateVerify message.
  32. // The flight4handler must not interact with the CipherSuite
  33. // if the CertificateVerify is missing
  34. func TestFlight4_Process_CertificateVerify(t *testing.T) {
  35. // Limit runtime in case of deadlocks
  36. lim := test.TimeOut(5 * time.Second)
  37. defer lim.Stop()
  38. // Check for leaking routines
  39. report := test.CheckRoutines(t)
  40. defer report()
  41. mockConn := &flight4TestMockFlightConn{}
  42. state := &State{
  43. cipherSuite: &flight4TestMockCipherSuite{t: t},
  44. }
  45. cache := newHandshakeCache()
  46. cfg := &handshakeConfig{}
  47. rawCertificate := []byte{
  48. 0x0b, 0x00, 0x01, 0x9b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  49. 0x01, 0x9b, 0x00, 0x01, 0x98, 0x00, 0x01, 0x95, 0x30, 0x82,
  50. 0x01, 0x91, 0x30, 0x82, 0x01, 0x38, 0xa0, 0x03, 0x02, 0x01,
  51. 0x02, 0x02, 0x11, 0x01, 0x65, 0x03, 0x3f, 0x4d, 0x0b, 0x9a,
  52. 0x62, 0x91, 0xdb, 0x4d, 0x28, 0x2c, 0x1f, 0xd6, 0x73, 0x32,
  53. 0x30, 0x0a, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x04,
  54. 0x03, 0x02, 0x30, 0x00, 0x30, 0x1e, 0x17, 0x0d, 0x32, 0x32,
  55. 0x30, 0x35, 0x31, 0x35, 0x31, 0x38, 0x34, 0x33, 0x35, 0x35,
  56. 0x5a, 0x17, 0x0d, 0x32, 0x32, 0x30, 0x36, 0x31, 0x35, 0x31,
  57. 0x38, 0x34, 0x33, 0x35, 0x35, 0x5a, 0x30, 0x00, 0x30, 0x59,
  58. 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02,
  59. 0x01, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01,
  60. 0x07, 0x03, 0x42, 0x00, 0x04, 0xc3, 0xb7, 0x13, 0x1a, 0x0a,
  61. 0xfc, 0xd0, 0x82, 0xf8, 0x94, 0x5e, 0xc0, 0x77, 0x07, 0x81,
  62. 0x28, 0xc9, 0xcb, 0x08, 0x84, 0x50, 0x6b, 0xf0, 0x22, 0xe8,
  63. 0x79, 0xb9, 0x15, 0x33, 0xc4, 0x56, 0xa1, 0xd3, 0x1b, 0x24,
  64. 0xe3, 0x61, 0xbd, 0x4d, 0x65, 0x80, 0x6b, 0x5d, 0x96, 0x48,
  65. 0xa2, 0x44, 0x9e, 0xce, 0xe8, 0x65, 0xd6, 0x3c, 0xe0, 0x9b,
  66. 0x6b, 0xa1, 0x36, 0x34, 0xb2, 0x39, 0xe2, 0x03, 0x00, 0xa3,
  67. 0x81, 0x92, 0x30, 0x81, 0x8f, 0x30, 0x0e, 0x06, 0x03, 0x55,
  68. 0x1d, 0x0f, 0x01, 0x01, 0xff, 0x04, 0x04, 0x03, 0x02, 0x02,
  69. 0xa4, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d, 0x25, 0x04, 0x16,
  70. 0x30, 0x14, 0x06, 0x08, 0x2b, 0x06, 0x01, 0x05, 0x05, 0x07,
  71. 0x03, 0x02, 0x06, 0x08, 0x2b, 0x06, 0x01, 0x05, 0x05, 0x07,
  72. 0x03, 0x01, 0x30, 0x0f, 0x06, 0x03, 0x55, 0x1d, 0x13, 0x01,
  73. 0x01, 0xff, 0x04, 0x05, 0x30, 0x03, 0x01, 0x01, 0xff, 0x30,
  74. 0x1d, 0x06, 0x03, 0x55, 0x1d, 0x0e, 0x04, 0x16, 0x04, 0x14,
  75. 0xb1, 0x1a, 0xe3, 0xeb, 0x6f, 0x7c, 0xc3, 0x8f, 0xba, 0x6f,
  76. 0x1c, 0xe8, 0xf0, 0x23, 0x08, 0x50, 0x8d, 0x3c, 0xea, 0x31,
  77. 0x30, 0x2e, 0x06, 0x03, 0x55, 0x1d, 0x11, 0x01, 0x01, 0xff,
  78. 0x04, 0x24, 0x30, 0x22, 0x82, 0x20, 0x30, 0x30, 0x30, 0x30,
  79. 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
  80. 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
  81. 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x0a,
  82. 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x04, 0x03, 0x02,
  83. 0x03, 0x47, 0x00, 0x30, 0x44, 0x02, 0x20, 0x06, 0x31, 0x43,
  84. 0xac, 0x03, 0x45, 0x79, 0x3c, 0xd7, 0x5f, 0x6e, 0x6a, 0xf8,
  85. 0x0e, 0xfd, 0x35, 0x49, 0xee, 0x1b, 0xbc, 0x47, 0xce, 0xe3,
  86. 0x39, 0xec, 0xe4, 0x62, 0xe1, 0x30, 0x1a, 0xa1, 0x89, 0x02,
  87. 0x20, 0x35, 0xcd, 0x7a, 0x15, 0x68, 0x09, 0x50, 0x49, 0x9e,
  88. 0x3e, 0x05, 0xd7, 0xc2, 0x69, 0x3f, 0x9c, 0x0c, 0x98, 0x92,
  89. 0x65, 0xec, 0xae, 0x44, 0xfe, 0xe5, 0x68, 0xb8, 0x09, 0x78,
  90. 0x7f, 0x6b, 0x77,
  91. }
  92. rawClientKeyExchange := []byte{
  93. 0x10, 0x00, 0x00, 0x21, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
  94. 0x00, 0x21, 0x20, 0x96, 0xed, 0x0c, 0xee, 0xf3, 0x11, 0xb1,
  95. 0x9d, 0x8b, 0x1c, 0x02, 0x7f, 0x06, 0x7c, 0x57, 0x7a, 0x14,
  96. 0xa6, 0x41, 0xde, 0x63, 0x57, 0x9e, 0xcd, 0x34, 0x54, 0xba,
  97. 0x37, 0x4d, 0x34, 0x15, 0x18,
  98. }
  99. cache.push(rawCertificate, 0, 0, handshake.TypeCertificate, true)
  100. cache.push(rawClientKeyExchange, 0, 1, handshake.TypeClientKeyExchange, true)
  101. if _, _, err := flight4Parse(context.TODO(), mockConn, state, cache, cfg); err != nil {
  102. t.Fatal(err)
  103. }
  104. }