2024-05-15 23:15:00 +00:00
|
|
|
// SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
2022-03-10 09:44:48 +00:00
|
|
|
package dtls
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/pion/dtls/v2/pkg/protocol"
|
|
|
|
"github.com/pion/dtls/v2/pkg/protocol/alert"
|
|
|
|
"github.com/pion/dtls/v2/pkg/protocol/handshake"
|
|
|
|
"github.com/pion/dtls/v2/pkg/protocol/recordlayer"
|
|
|
|
)
|
|
|
|
|
|
|
|
func flight2Parse(ctx context.Context, c flightConn, state *State, cache *handshakeCache, cfg *handshakeConfig) (flightVal, *alert.Alert, error) {
|
2024-05-15 23:15:00 +00:00
|
|
|
seq, msgs, ok := cache.fullPullMap(state.handshakeRecvSequence, state.cipherSuite,
|
2022-03-10 09:44:48 +00:00
|
|
|
handshakeCachePullRule{handshake.TypeClientHello, cfg.initialEpoch, true, false},
|
|
|
|
)
|
|
|
|
if !ok {
|
|
|
|
// Client may retransmit the first ClientHello when HelloVerifyRequest is dropped.
|
|
|
|
// Parse as flight 0 in this case.
|
|
|
|
return flight0Parse(ctx, c, state, cache, cfg)
|
|
|
|
}
|
|
|
|
state.handshakeRecvSequence = seq
|
|
|
|
|
|
|
|
var clientHello *handshake.MessageClientHello
|
|
|
|
|
|
|
|
// Validate type
|
|
|
|
if clientHello, ok = msgs[handshake.TypeClientHello].(*handshake.MessageClientHello); !ok {
|
|
|
|
return 0, &alert.Alert{Level: alert.Fatal, Description: alert.InternalError}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if !clientHello.Version.Equal(protocol.Version1_2) {
|
|
|
|
return 0, &alert.Alert{Level: alert.Fatal, Description: alert.ProtocolVersion}, errUnsupportedProtocolVersion
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(clientHello.Cookie) == 0 {
|
|
|
|
return 0, nil, nil
|
|
|
|
}
|
|
|
|
if !bytes.Equal(state.cookie, clientHello.Cookie) {
|
|
|
|
return 0, &alert.Alert{Level: alert.Fatal, Description: alert.AccessDenied}, errCookieMismatch
|
|
|
|
}
|
|
|
|
return flight4, nil, nil
|
|
|
|
}
|
|
|
|
|
2024-05-15 23:15:00 +00:00
|
|
|
func flight2Generate(_ flightConn, state *State, _ *handshakeCache, _ *handshakeConfig) ([]*packet, *alert.Alert, error) {
|
2022-03-10 09:44:48 +00:00
|
|
|
state.handshakeSendSequence = 0
|
|
|
|
return []*packet{
|
|
|
|
{
|
|
|
|
record: &recordlayer.RecordLayer{
|
|
|
|
Header: recordlayer.Header{
|
|
|
|
Version: protocol.Version1_2,
|
|
|
|
},
|
|
|
|
Content: &handshake.Handshake{
|
|
|
|
Message: &handshake.MessageHelloVerifyRequest{
|
|
|
|
Version: protocol.Version1_2,
|
|
|
|
Cookie: state.cookie,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, nil, nil
|
|
|
|
}
|