flight5bhandler.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package dtls
  2. import (
  3. "context"
  4. "github.com/pion/dtls/v2/pkg/crypto/prf"
  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 flight5bParse(ctx context.Context, c flightConn, state *State, cache *handshakeCache, cfg *handshakeConfig) (flightVal, *alert.Alert, error) {
  11. _, msgs, ok := cache.fullPullMap(state.handshakeRecvSequence-1, state.cipherSuite,
  12. handshakeCachePullRule{handshake.TypeFinished, cfg.initialEpoch + 1, false, false},
  13. )
  14. if !ok {
  15. // No valid message received. Keep reading
  16. return 0, nil, nil
  17. }
  18. if _, ok = msgs[handshake.TypeFinished].(*handshake.MessageFinished); !ok {
  19. return 0, &alert.Alert{Level: alert.Fatal, Description: alert.InternalError}, nil
  20. }
  21. // Other party may re-transmit the last flight. Keep state to be flight5b.
  22. return flight5b, nil, nil
  23. }
  24. func flight5bGenerate(c flightConn, state *State, cache *handshakeCache, cfg *handshakeConfig) ([]*packet, *alert.Alert, error) { //nolint:gocognit
  25. var pkts []*packet
  26. pkts = append(pkts,
  27. &packet{
  28. record: &recordlayer.RecordLayer{
  29. Header: recordlayer.Header{
  30. Version: protocol.Version1_2,
  31. },
  32. Content: &protocol.ChangeCipherSpec{},
  33. },
  34. })
  35. if len(state.localVerifyData) == 0 {
  36. plainText := cache.pullAndMerge(
  37. handshakeCachePullRule{handshake.TypeClientHello, cfg.initialEpoch, true, false},
  38. handshakeCachePullRule{handshake.TypeServerHello, cfg.initialEpoch, false, false},
  39. handshakeCachePullRule{handshake.TypeFinished, cfg.initialEpoch + 1, false, false},
  40. )
  41. var err error
  42. state.localVerifyData, err = prf.VerifyDataClient(state.masterSecret, plainText, state.cipherSuite.HashFunc())
  43. if err != nil {
  44. return nil, &alert.Alert{Level: alert.Fatal, Description: alert.InternalError}, err
  45. }
  46. }
  47. pkts = append(pkts,
  48. &packet{
  49. record: &recordlayer.RecordLayer{
  50. Header: recordlayer.Header{
  51. Version: protocol.Version1_2,
  52. Epoch: 1,
  53. },
  54. Content: &handshake.Handshake{
  55. Message: &handshake.MessageFinished{
  56. VerifyData: state.localVerifyData,
  57. },
  58. },
  59. },
  60. shouldEncrypt: true,
  61. resetLocalSequenceNumber: true,
  62. })
  63. return pkts, nil, nil
  64. }