github.com/MerlinKodo/quic-go@v0.39.2/fuzzing/handshake/cmd/corpus.go (about)

     1  package main
     2  
     3  import (
     4  	"crypto/tls"
     5  	"log"
     6  	"net"
     7  
     8  	fuzzhandshake "github.com/MerlinKodo/quic-go/fuzzing/handshake"
     9  	"github.com/MerlinKodo/quic-go/fuzzing/internal/helper"
    10  	"github.com/MerlinKodo/quic-go/internal/handshake"
    11  	"github.com/MerlinKodo/quic-go/internal/protocol"
    12  	"github.com/MerlinKodo/quic-go/internal/testdata"
    13  	"github.com/MerlinKodo/quic-go/internal/utils"
    14  	"github.com/MerlinKodo/quic-go/internal/wire"
    15  )
    16  
    17  const alpn = "fuzz"
    18  
    19  func main() {
    20  	client := handshake.NewCryptoSetupClient(
    21  		protocol.ConnectionID{},
    22  		&wire.TransportParameters{ActiveConnectionIDLimit: 2},
    23  		&tls.Config{
    24  			MinVersion:         tls.VersionTLS13,
    25  			ServerName:         "localhost",
    26  			NextProtos:         []string{alpn},
    27  			RootCAs:            testdata.GetRootCA(),
    28  			ClientSessionCache: tls.NewLRUClientSessionCache(1),
    29  		},
    30  		false,
    31  		utils.NewRTTStats(),
    32  		nil,
    33  		utils.DefaultLogger.WithPrefix("client"),
    34  		protocol.Version1,
    35  	)
    36  
    37  	config := testdata.GetTLSConfig()
    38  	config.NextProtos = []string{alpn}
    39  	server := handshake.NewCryptoSetupServer(
    40  		protocol.ConnectionID{},
    41  		&net.UDPAddr{IP: net.IPv6loopback, Port: 1234},
    42  		&net.UDPAddr{IP: net.IPv6loopback, Port: 4321},
    43  		&wire.TransportParameters{ActiveConnectionIDLimit: 2},
    44  		config,
    45  		false,
    46  		utils.NewRTTStats(),
    47  		nil,
    48  		utils.DefaultLogger.WithPrefix("server"),
    49  		protocol.Version1,
    50  	)
    51  
    52  	if err := client.StartHandshake(); err != nil {
    53  		log.Fatal(err)
    54  	}
    55  
    56  	if err := server.StartHandshake(); err != nil {
    57  		log.Fatal(err)
    58  	}
    59  
    60  	var clientHandshakeComplete, serverHandshakeComplete bool
    61  	var messages [][]byte
    62  	for {
    63  	clientLoop:
    64  		for {
    65  			ev := client.NextEvent()
    66  			//nolint:exhaustive // only need to process a few events
    67  			switch ev.Kind {
    68  			case handshake.EventNoEvent:
    69  				break clientLoop
    70  			case handshake.EventWriteInitialData:
    71  				messages = append(messages, ev.Data)
    72  				if err := server.HandleMessage(ev.Data, protocol.EncryptionInitial); err != nil {
    73  					log.Fatal(err)
    74  				}
    75  			case handshake.EventWriteHandshakeData:
    76  				messages = append(messages, ev.Data)
    77  				if err := server.HandleMessage(ev.Data, protocol.EncryptionHandshake); err != nil {
    78  					log.Fatal(err)
    79  				}
    80  			case handshake.EventHandshakeComplete:
    81  				clientHandshakeComplete = true
    82  			}
    83  		}
    84  
    85  	serverLoop:
    86  		for {
    87  			ev := server.NextEvent()
    88  			//nolint:exhaustive // only need to process a few events
    89  			switch ev.Kind {
    90  			case handshake.EventNoEvent:
    91  				break serverLoop
    92  			case handshake.EventWriteInitialData:
    93  				messages = append(messages, ev.Data)
    94  				if err := client.HandleMessage(ev.Data, protocol.EncryptionInitial); err != nil {
    95  					log.Fatal(err)
    96  				}
    97  			case handshake.EventWriteHandshakeData:
    98  				messages = append(messages, ev.Data)
    99  				if err := client.HandleMessage(ev.Data, protocol.EncryptionHandshake); err != nil {
   100  					log.Fatal(err)
   101  				}
   102  			case handshake.EventHandshakeComplete:
   103  				serverHandshakeComplete = true
   104  			}
   105  		}
   106  
   107  		if serverHandshakeComplete && clientHandshakeComplete {
   108  			break
   109  		}
   110  	}
   111  
   112  	ticket, err := server.GetSessionTicket()
   113  	if err != nil {
   114  		log.Fatal(err)
   115  	}
   116  	if ticket == nil {
   117  		log.Fatal("expected a session ticket")
   118  	}
   119  	messages = append(messages, ticket)
   120  
   121  	for _, m := range messages {
   122  		if err := helper.WriteCorpusFileWithPrefix("corpus", m, fuzzhandshake.PrefixLen); err != nil {
   123  			log.Fatal(err)
   124  		}
   125  	}
   126  }