flight2handler.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package dtls
  2. import (
  3. "bytes"
  4. "context"
  5. "github.com/pion/dtls/v2/pkg/protocol"
  6. "github.com/pion/dtls/v2/pkg/protocol/alert"
  7. "github.com/pion/dtls/v2/pkg/protocol/handshake"
  8. "github.com/pion/dtls/v2/pkg/protocol/recordlayer"
  9. )
  10. func flight2Parse(ctx context.Context, c flightConn, state *State, cache *handshakeCache, cfg *handshakeConfig) (flightVal, *alert.Alert, error) {
  11. seq, msgs, ok := cache.fullPullMap(state.handshakeRecvSequence, state.cipherSuite,
  12. handshakeCachePullRule{handshake.TypeClientHello, cfg.initialEpoch, true, false},
  13. )
  14. if !ok {
  15. // Client may retransmit the first ClientHello when HelloVerifyRequest is dropped.
  16. // Parse as flight 0 in this case.
  17. return flight0Parse(ctx, c, state, cache, cfg)
  18. }
  19. state.handshakeRecvSequence = seq
  20. var clientHello *handshake.MessageClientHello
  21. // Validate type
  22. if clientHello, ok = msgs[handshake.TypeClientHello].(*handshake.MessageClientHello); !ok {
  23. return 0, &alert.Alert{Level: alert.Fatal, Description: alert.InternalError}, nil
  24. }
  25. if !clientHello.Version.Equal(protocol.Version1_2) {
  26. return 0, &alert.Alert{Level: alert.Fatal, Description: alert.ProtocolVersion}, errUnsupportedProtocolVersion
  27. }
  28. if len(clientHello.Cookie) == 0 {
  29. return 0, nil, nil
  30. }
  31. if !bytes.Equal(state.cookie, clientHello.Cookie) {
  32. return 0, &alert.Alert{Level: alert.Fatal, Description: alert.AccessDenied}, errCookieMismatch
  33. }
  34. return flight4, nil, nil
  35. }
  36. func flight2Generate(c flightConn, state *State, cache *handshakeCache, cfg *handshakeConfig) ([]*packet, *alert.Alert, error) {
  37. state.handshakeSendSequence = 0
  38. return []*packet{
  39. {
  40. record: &recordlayer.RecordLayer{
  41. Header: recordlayer.Header{
  42. Version: protocol.Version1_2,
  43. },
  44. Content: &handshake.Handshake{
  45. Message: &handshake.MessageHelloVerifyRequest{
  46. Version: protocol.Version1_2,
  47. Cookie: state.cookie,
  48. },
  49. },
  50. },
  51. },
  52. }, nil, nil
  53. }