// Copyright 2023 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package tls

import (
	
	
	
)

// QUICEncryptionLevel represents a QUIC encryption level used to transmit
// handshake messages.
type QUICEncryptionLevel int

const (
	QUICEncryptionLevelInitial = QUICEncryptionLevel(iota)
	QUICEncryptionLevelEarly
	QUICEncryptionLevelHandshake
	QUICEncryptionLevelApplication
)

func ( QUICEncryptionLevel) () string {
	switch  {
	case QUICEncryptionLevelInitial:
		return "Initial"
	case QUICEncryptionLevelEarly:
		return "Early"
	case QUICEncryptionLevelHandshake:
		return "Handshake"
	case QUICEncryptionLevelApplication:
		return "Application"
	default:
		return fmt.Sprintf("QUICEncryptionLevel(%v)", int())
	}
}

// A QUICConn represents a connection which uses a QUIC implementation as the underlying
// transport as described in RFC 9001.
//
// Methods of QUICConn are not safe for concurrent use.
type QUICConn struct {
	conn *Conn

	sessionTicketSent bool
}

// A QUICConfig configures a [QUICConn].
type QUICConfig struct {
	TLSConfig *Config

	// EnableSessionEvents may be set to true to enable the
	// [QUICStoreSession] and [QUICResumeSession] events for client connections.
	// When this event is enabled, sessions are not automatically
	// stored in the client session cache.
	// The application should use [QUICConn.StoreSession] to store sessions.
	EnableSessionEvents bool
}

// A QUICEventKind is a type of operation on a QUIC connection.
type QUICEventKind int

const (
	// QUICNoEvent indicates that there are no events available.
	QUICNoEvent QUICEventKind = iota

	// QUICSetReadSecret and QUICSetWriteSecret provide the read and write
	// secrets for a given encryption level.
	// QUICEvent.Level, QUICEvent.Data, and QUICEvent.Suite are set.
	//
	// Secrets for the Initial encryption level are derived from the initial
	// destination connection ID, and are not provided by the QUICConn.
	QUICSetReadSecret
	QUICSetWriteSecret

	// QUICWriteData provides data to send to the peer in CRYPTO frames.
	// QUICEvent.Data is set.
	QUICWriteData

	// QUICTransportParameters provides the peer's QUIC transport parameters.
	// QUICEvent.Data is set.
	QUICTransportParameters

	// QUICTransportParametersRequired indicates that the caller must provide
	// QUIC transport parameters to send to the peer. The caller should set
	// the transport parameters with QUICConn.SetTransportParameters and call
	// QUICConn.NextEvent again.
	//
	// If transport parameters are set before calling QUICConn.Start, the
	// connection will never generate a QUICTransportParametersRequired event.
	QUICTransportParametersRequired

	// QUICRejectedEarlyData indicates that the server rejected 0-RTT data even
	// if we offered it. It's returned before QUICEncryptionLevelApplication
	// keys are returned.
	// This event only occurs on client connections.
	QUICRejectedEarlyData

	// QUICHandshakeDone indicates that the TLS handshake has completed.
	QUICHandshakeDone

	// QUICResumeSession indicates that a client is attempting to resume a previous session.
	// [QUICEvent.SessionState] is set.
	//
	// For client connections, this event occurs when the session ticket is selected.
	// For server connections, this event occurs when receiving the client's session ticket.
	//
	// The application may set [QUICEvent.SessionState.EarlyData] to false before the
	// next call to [QUICConn.NextEvent] to decline 0-RTT even if the session supports it.
	QUICResumeSession

	// QUICStoreSession indicates that the server has provided state permitting
	// the client to resume the session.
	// [QUICEvent.SessionState] is set.
	// The application should use [QUICConn.StoreSession] session to store the [SessionState].
	// The application may modify the [SessionState] before storing it.
	// This event only occurs on client connections.
	QUICStoreSession
)

// A QUICEvent is an event occurring on a QUIC connection.
//
// The type of event is specified by the Kind field.
// The contents of the other fields are kind-specific.
type QUICEvent struct {
	Kind QUICEventKind

	// Set for QUICSetReadSecret, QUICSetWriteSecret, and QUICWriteData.
	Level QUICEncryptionLevel

	// Set for QUICTransportParameters, QUICSetReadSecret, QUICSetWriteSecret, and QUICWriteData.
	// The contents are owned by crypto/tls, and are valid until the next NextEvent call.
	Data []byte

	// Set for QUICSetReadSecret and QUICSetWriteSecret.
	Suite uint16

	// Set for QUICResumeSession and QUICStoreSession.
	SessionState *SessionState
}

type quicState struct {
	events    []QUICEvent
	nextEvent int

	// eventArr is a statically allocated event array, large enough to handle
	// the usual maximum number of events resulting from a single call: transport
	// parameters, Initial data, Early read secret, Handshake write and read
	// secrets, Handshake data, Application write secret, Application data.
	eventArr [8]QUICEvent

	started  bool
	signalc  chan struct{}   // handshake data is available to be read
	blockedc chan struct{}   // handshake is waiting for data, closed when done
	cancelc  <-chan struct{} // handshake has been canceled
	cancel   context.CancelFunc

	waitingForDrain bool

	// readbuf is shared between HandleData and the handshake goroutine.
	// HandshakeCryptoData passes ownership to the handshake goroutine by
	// reading from signalc, and reclaims ownership by reading from blockedc.
	readbuf []byte

	transportParams []byte // to send to the peer

	enableSessionEvents bool
}

// QUICClient returns a new TLS client side connection using QUICTransport as the
// underlying transport. The config cannot be nil.
//
// The config's MinVersion must be at least TLS 1.3.
func ( *QUICConfig) *QUICConn {
	return newQUICConn(Client(nil, .TLSConfig), )
}

// QUICServer returns a new TLS server side connection using QUICTransport as the
// underlying transport. The config cannot be nil.
//
// The config's MinVersion must be at least TLS 1.3.
func ( *QUICConfig) *QUICConn {
	return newQUICConn(Server(nil, .TLSConfig), )
}

func newQUICConn( *Conn,  *QUICConfig) *QUICConn {
	.quic = &quicState{
		signalc:             make(chan struct{}),
		blockedc:            make(chan struct{}),
		enableSessionEvents: .EnableSessionEvents,
	}
	.quic.events = .quic.eventArr[:0]
	return &QUICConn{
		conn: ,
	}
}

// Start starts the client or server handshake protocol.
// It may produce connection events, which may be read with [QUICConn.NextEvent].
//
// Start must be called at most once.
func ( *QUICConn) ( context.Context) error {
	if .conn.quic.started {
		return quicError(errors.New("tls: Start called more than once"))
	}
	.conn.quic.started = true
	if .conn.config.MinVersion < VersionTLS13 {
		return quicError(errors.New("tls: Config MinVersion must be at least TLS 1.13"))
	}
	go .conn.HandshakeContext()
	if ,  := <-.conn.quic.blockedc; ! {
		return .conn.handshakeErr
	}
	return nil
}

// NextEvent returns the next event occurring on the connection.
// It returns an event with a Kind of [QUICNoEvent] when no events are available.
func ( *QUICConn) () QUICEvent {
	 := .conn.quic
	if  := .nextEvent - 1;  >= 0 && len(.events[].Data) > 0 {
		// Write over some of the previous event's data,
		// to catch callers erroniously retaining it.
		.events[].Data[0] = 0
	}
	if .nextEvent >= len(.events) && .waitingForDrain {
		.waitingForDrain = false
		<-.signalc
		<-.blockedc
	}
	if .nextEvent >= len(.events) {
		.events = .events[:0]
		.nextEvent = 0
		return QUICEvent{Kind: QUICNoEvent}
	}
	 := .events[.nextEvent]
	.events[.nextEvent] = QUICEvent{} // zero out references to data
	.nextEvent++
	return 
}

// Close closes the connection and stops any in-progress handshake.
func ( *QUICConn) () error {
	if .conn.quic.cancel == nil {
		return nil // never started
	}
	.conn.quic.cancel()
	for range .conn.quic.blockedc {
		// Wait for the handshake goroutine to return.
	}
	return .conn.handshakeErr
}

// HandleData handles handshake bytes received from the peer.
// It may produce connection events, which may be read with [QUICConn.NextEvent].
func ( *QUICConn) ( QUICEncryptionLevel,  []byte) error {
	 := .conn
	if .in.level !=  {
		return quicError(.in.setErrorLocked(errors.New("tls: handshake data received at wrong level")))
	}
	.quic.readbuf = 
	<-.quic.signalc
	,  := <-.quic.blockedc
	if  {
		// The handshake goroutine is waiting for more data.
		return nil
	}
	// The handshake goroutine has exited.
	.handshakeMutex.Lock()
	defer .handshakeMutex.Unlock()
	.hand.Write(.quic.readbuf)
	.quic.readbuf = nil
	for .conn.hand.Len() >= 4 && .conn.handshakeErr == nil {
		 := .conn.hand.Bytes()
		 := int([1])<<16 | int([2])<<8 | int([3])
		if  > maxHandshake {
			.conn.handshakeErr = fmt.Errorf("tls: handshake message of length %d bytes exceeds maximum of %d bytes", , maxHandshake)
			break
		}
		if len() < 4+ {
			return nil
		}
		if  := .conn.handlePostHandshakeMessage();  != nil {
			.conn.handshakeErr = 
		}
	}
	if .conn.handshakeErr != nil {
		return quicError(.conn.handshakeErr)
	}
	return nil
}

type QUICSessionTicketOptions struct {
	// EarlyData specifies whether the ticket may be used for 0-RTT.
	EarlyData bool
	Extra     [][]byte
}

// SendSessionTicket sends a session ticket to the client.
// It produces connection events, which may be read with [QUICConn.NextEvent].
// Currently, it can only be called once.
func ( *QUICConn) ( QUICSessionTicketOptions) error {
	 := .conn
	if !.isHandshakeComplete.Load() {
		return quicError(errors.New("tls: SendSessionTicket called before handshake completed"))
	}
	if .isClient {
		return quicError(errors.New("tls: SendSessionTicket called on the client"))
	}
	if .sessionTicketSent {
		return quicError(errors.New("tls: SendSessionTicket called multiple times"))
	}
	.sessionTicketSent = true
	return quicError(.sendSessionTicket(.EarlyData, .Extra))
}

// StoreSession stores a session previously received in a QUICStoreSession event
// in the ClientSessionCache.
// The application may process additional events or modify the SessionState
// before storing the session.
func ( *QUICConn) ( *SessionState) error {
	 := .conn
	if !.isClient {
		return quicError(errors.New("tls: StoreSessionTicket called on the server"))
	}
	 := .clientSessionCacheKey()
	if  == "" {
		return nil
	}
	 := &ClientSessionState{session: }
	.config.ClientSessionCache.Put(, )
	return nil
}

// ConnectionState returns basic TLS details about the connection.
func ( *QUICConn) () ConnectionState {
	return .conn.ConnectionState()
}

// SetTransportParameters sets the transport parameters to send to the peer.
//
// Server connections may delay setting the transport parameters until after
// receiving the client's transport parameters. See [QUICTransportParametersRequired].
func ( *QUICConn) ( []byte) {
	if  == nil {
		 = []byte{}
	}
	.conn.quic.transportParams = 
	if .conn.quic.started {
		<-.conn.quic.signalc
		<-.conn.quic.blockedc
	}
}

// quicError ensures err is an AlertError.
// If err is not already, quicError wraps it with alertInternalError.
func quicError( error) error {
	if  == nil {
		return nil
	}
	var  AlertError
	if errors.As(, &) {
		return 
	}
	var  alert
	if !errors.As(, &) {
		 = alertInternalError
	}
	// Return an error wrapping the original error and an AlertError.
	// Truncate the text of the alert to 0 characters.
	return fmt.Errorf("%w%.0w", , AlertError())
}

func ( *Conn) ( int) error {
	for .hand.Len() <  {
		if  := .quicWaitForSignal();  != nil {
			return 
		}
	}
	return nil
}

func ( *Conn) ( QUICEncryptionLevel,  uint16,  []byte) {
	.quic.events = append(.quic.events, QUICEvent{
		Kind:  QUICSetReadSecret,
		Level: ,
		Suite: ,
		Data:  ,
	})
}

func ( *Conn) ( QUICEncryptionLevel,  uint16,  []byte) {
	.quic.events = append(.quic.events, QUICEvent{
		Kind:  QUICSetWriteSecret,
		Level: ,
		Suite: ,
		Data:  ,
	})
}

func ( *Conn) ( QUICEncryptionLevel,  []byte) {
	var  *QUICEvent
	if len(.quic.events) > 0 {
		 = &.quic.events[len(.quic.events)-1]
	}
	if  == nil || .Kind != QUICWriteData || .Level !=  {
		.quic.events = append(.quic.events, QUICEvent{
			Kind:  QUICWriteData,
			Level: ,
		})
		 = &.quic.events[len(.quic.events)-1]
	}
	.Data = append(.Data, ...)
}

func ( *Conn) ( *SessionState) error {
	.quic.events = append(.quic.events, QUICEvent{
		Kind:         QUICResumeSession,
		SessionState: ,
	})
	.quic.waitingForDrain = true
	for .quic.waitingForDrain {
		if  := .quicWaitForSignal();  != nil {
			return 
		}
	}
	return nil
}

func ( *Conn) ( *SessionState) {
	.quic.events = append(.quic.events, QUICEvent{
		Kind:         QUICStoreSession,
		SessionState: ,
	})
}

func ( *Conn) ( []byte) {
	.quic.events = append(.quic.events, QUICEvent{
		Kind: QUICTransportParameters,
		Data: ,
	})
}

func ( *Conn) () ([]byte, error) {
	if .quic.transportParams == nil {
		.quic.events = append(.quic.events, QUICEvent{
			Kind: QUICTransportParametersRequired,
		})
	}
	for .quic.transportParams == nil {
		if  := .quicWaitForSignal();  != nil {
			return nil, 
		}
	}
	return .quic.transportParams, nil
}

func ( *Conn) () {
	.quic.events = append(.quic.events, QUICEvent{
		Kind: QUICHandshakeDone,
	})
}

func ( *Conn) () {
	.quic.events = append(.quic.events, QUICEvent{
		Kind: QUICRejectedEarlyData,
	})
}

// quicWaitForSignal notifies the QUICConn that handshake progress is blocked,
// and waits for a signal that the handshake should proceed.
//
// The handshake may become blocked waiting for handshake bytes
// or for the user to provide transport parameters.
func ( *Conn) () error {
	// Drop the handshake mutex while blocked to allow the user
	// to call ConnectionState before the handshake completes.
	.handshakeMutex.Unlock()
	defer .handshakeMutex.Lock()
	// Send on blockedc to notify the QUICConn that the handshake is blocked.
	// Exported methods of QUICConn wait for the handshake to become blocked
	// before returning to the user.
	select {
	case .quic.blockedc <- struct{}{}:
	case <-.quic.cancelc:
		return .sendAlertLocked(alertCloseNotify)
	}
	// The QUICConn reads from signalc to notify us that the handshake may
	// be able to proceed. (The QUICConn reads, because we close signalc to
	// indicate that the handshake has completed.)
	select {
	case .quic.signalc <- struct{}{}:
		.hand.Write(.quic.readbuf)
		.quic.readbuf = nil
	case <-.quic.cancelc:
		return .sendAlertLocked(alertCloseNotify)
	}
	return nil
}