forked from Danny-Dasilva/utls
/
t_certificate_compression.go
145 lines (120 loc) · 3.29 KB
/
t_certificate_compression.go
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package tls
import (
"bytes"
"compress/zlib"
"errors"
"fmt"
"io"
"github.com/dsnet/compress/brotli"
"golang.org/x/crypto/cryptobyte"
)
const (
// TEMPORARY: draft-ietf-tls-certificate-compression-04
typeCompressedCertificate uint8 = 25
extensionCompressCertificate uint16 = 27
)
type CompressCertificateExtension struct {
Algorithms []CertCompressionAlgo
}
func (e *CompressCertificateExtension) writeToUConn(uc *UConn) error {
uc.extCompressCerts = true
return nil
}
func (e *CompressCertificateExtension) Len() int {
return 4 + 1 + (2 * len(e.Algorithms))
}
func (e *CompressCertificateExtension) Read(b []byte) (int, error) {
if len(b) < e.Len() {
return 0, io.ErrShortBuffer
}
extLen := 2 * len(e.Algorithms)
if extLen > 255 {
return 0, errors.New("too many supported algorithms")
}
b[0] = byte(extensionCompressCertificate >> 8)
b[1] = byte(extensionCompressCertificate)
b[2] = byte((extLen + 1) >> 8)
b[3] = byte((extLen + 1))
b[4] = byte(extLen)
i := 5
for _, alg := range e.Algorithms {
b[i] = byte(alg >> 8)
b[i+1] = byte(alg)
i += 2
}
return e.Len(), io.EOF
}
type compressedCertificateMsg struct {
raw []byte
algorithm CertCompressionAlgo
uncompressedLength uint32
compressedCertificateMessage []byte
}
func (m *compressedCertificateMsg) marshal() []byte {
if m.raw != nil {
return m.raw
}
panic("utls: compressedCertificateMsg.marshal() not actually implemented")
}
func (m *compressedCertificateMsg) unmarshal(data []byte) bool {
m.raw = append([]byte{}, data...)
s := cryptobyte.String(data[4:])
var algID uint16
if !s.ReadUint16(&algID) {
return false
}
if !s.ReadUint24(&m.uncompressedLength) {
return false
}
if !readUint24LengthPrefixed(&s, &m.compressedCertificateMessage) {
return false
}
m.algorithm = CertCompressionAlgo(algID)
return true
}
func (m *compressedCertificateMsg) toCertificateMsg() (*certificateMsgTLS13, error) {
var (
decompressed []byte
rd io.ReadCloser
err error
)
if m.uncompressedLength > 1<<24 {
return nil, fmt.Errorf("utls: oversized decompressed certificate length")
}
compressed := bytes.NewBuffer(m.compressedCertificateMessage)
switch m.algorithm {
case CertCompressionZlib:
rd, err = zlib.NewReader(compressed)
case CertCompressionBrotli:
rd, err = brotli.NewReader(compressed, nil)
default:
return nil, fmt.Errorf("utls: unknown certificate compression algorithm: %v", m.algorithm)
}
if err != nil {
return nil, err
}
defer rd.Close()
decompressed = make([]byte, m.uncompressedLength)
if _, err = io.ReadFull(rd, decompressed); err != nil {
return nil, err
}
// Enforce the length just to be sure.
length := len(decompressed)
if length != int(m.uncompressedLength) {
return nil, fmt.Errorf("utls: invalid decompressed certificate length: %v", length)
}
// Prepend the type and record length to the synthetic Certificate message.
// Technically this can be 4 bytes of 0x00 since nothing examines it, but
// being correct doesn't hurt.
decompressed = append([]byte{
typeCertificate,
byte(length >> 16),
byte(length >> 8),
byte(length),
}, decompressed...)
var mm certificateMsgTLS13
if !mm.unmarshal(decompressed) {
return nil, fmt.Errorf("utls: failed to unmarshal decompressed certificateMsgTLS13")
}
return &mm, nil
}