File: tls_test.go

package info (click to toggle)
golang-github-bifurcation-mint 0.0~git20200214.93c820e-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 632 kB
  • sloc: makefile: 3
file content (128 lines) | stat: -rw-r--r-- 3,932 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package syntax

import (
	"bytes"
	"encoding/hex"
	"fmt"
	"reflect"
	"testing"
)

type ProtocolVersion uint16

type ExtensionType uint16
type Extension struct {
	ExtensionType ExtensionType
	ExtensionData []byte `tls:"head=2"`
}

type Random [32]byte
type CipherSuite uint16

type ClientHello struct {
	LegacyVersion            ProtocolVersion
	Random                   Random
	LegacySessionID          []byte        `tls:"head=1,max=32"`
	CipherSuites             []CipherSuite `tls:"head=2,min=2"`
	LegacyCompressionMethods []byte        `tls:"head=1,min=1"`
	Extensions               []Extension   `tls:"head=2"`
}

type ServerHello struct {
	Version     ProtocolVersion
	Random      Random
	CipherSuite CipherSuite
	Extensions  []Extension `tls:"head=2"`
}

var (
	extValidIn = Extension{
		ExtensionType: ExtensionType(0x000a),
		ExtensionData: []byte{0xf0, 0xf1, 0xf2, 0xf3, 0xf4},
	}
	extEmptyIn = Extension{
		ExtensionType: ExtensionType(0x000a),
		ExtensionData: []byte{},
	}
	extListValidIn  = []Extension{extValidIn, extEmptyIn}
	extListValidHex = "000d000a0005f0f1f2f3f4000a0000"

	helloRandom = [32]byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
		0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
		0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
		0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37}
	chValidIn = ClientHello{
		LegacyVersion:            0x0303,
		Random:                   helloRandom,
		LegacySessionID:          []byte{},
		CipherSuites:             []CipherSuite{0x0001, 0x0002, 0x0003},
		LegacyCompressionMethods: []byte{0},
		Extensions:               extListValidIn,
	}
	chValidHex = "0303" + hex.EncodeToString(helloRandom[:]) + "00" +
		"0006000100020003" + "0100" + extListValidHex

	shValidIn = ServerHello{
		Version:     0x7f12,
		Random:      helloRandom,
		CipherSuite: CipherSuite(0x0001),
		Extensions:  extListValidIn,
	}
	shValidHex = "7f12" + hex.EncodeToString(helloRandom[:]) + "0001" + extListValidHex
)

func TestTLSMarshal(t *testing.T) {
	chValid, _ := hex.DecodeString(chValidHex)
	shValid, _ := hex.DecodeString(shValidHex)

	// ClientHello marshal
	out, err := Marshal(chValidIn)
	if err != nil {
		t.Fatalf("Failed to marshal a valid ClientHello [%v]", err)
	}
	if !bytes.Equal(out, chValid) {
		t.Fatalf("Failed to marshal a valid ClientHello [%x] != [%x]", out, chValid)
	}

	// ServerHello marshal
	out, err = Marshal(shValidIn)
	if err != nil {
		t.Fatalf("Failed to marshal a valid ServerHello [%v]", err)
	}
	if !bytes.Equal(out, shValid) {
		t.Fatalf("Failed to marshal a valid ServerHello [%x] != [%x]", out, shValid)
	}
}

func TestTLSUnmarshal(t *testing.T) {
	chValid, _ := hex.DecodeString(chValidHex)
	shValid, _ := hex.DecodeString(shValidHex)

	// ClientHello marshal
	var ch ClientHello
	read, err := Unmarshal(chValid, &ch)
	if err != nil || read != len(chValid) {
		t.Fatalf("Failed to unmarshal a valid ClientHello [%v]", err)
	}
	if !reflect.DeepEqual(ch, chValidIn) {
		fmt.Println("LegacyVersion", reflect.DeepEqual(ch.LegacyVersion, chValidIn.LegacyVersion))
		fmt.Println("Random", reflect.DeepEqual(ch.Random, chValidIn.Random))
		fmt.Println("LegacySessionID")
		fmt.Printf("  %+v\n", ch.LegacySessionID == nil)
		fmt.Printf("  %+v\n", chValidIn.LegacySessionID == nil)
		fmt.Println("CipherSuites", reflect.DeepEqual(ch.CipherSuites, chValidIn.CipherSuites))
		fmt.Println("LegacyCompressionMethods", reflect.DeepEqual(ch.LegacyCompressionMethods, chValidIn.LegacyCompressionMethods))
		fmt.Println("Extensions", reflect.DeepEqual(ch.Extensions, chValidIn.Extensions))
		t.Errorf("Failed to unmarshal a valid ClientHello [%+v] [%+v]", ch, chValidIn)
	}

	// ServerHello marshal
	var sh ServerHello
	read, err = Unmarshal(shValid, &sh)
	if err != nil || read != len(shValid) {
		t.Fatalf("Failed to unmarshal a valid ServerHello [%v]", err)
	}
	if !reflect.DeepEqual(sh, shValidIn) {
		t.Errorf("Failed to unmarshal a valid ServerHello")
	}
}