/
credential.go
145 lines (121 loc) · 3.63 KB
/
credential.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
// Copyright 2014 Jamie Hall. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package frames
import (
"bytes"
"crypto/x509"
"fmt"
"github.com/albus01/ibenchmark/gospdy/common"
"io"
)
type CREDENTIAL struct {
Slot uint16
Proof []byte
Certificates []*x509.Certificate
}
func (frame *CREDENTIAL) Compress(comp common.Compressor) error {
return nil
}
func (frame *CREDENTIAL) Decompress(decomp common.Decompressor) error {
return nil
}
func (frame *CREDENTIAL) Name() string {
return "CREDENTIAL"
}
func (frame *CREDENTIAL) ReadFrom(reader io.Reader) (int64, error) {
data, err := common.ReadExactly(reader, 18)
if err != nil {
return 0, err
}
err = controlFrameCommonProcessing(data[:5], _CREDENTIAL, 0)
if err != nil {
return 18, err
}
// Get and check length.
length := int(common.BytesToUint24(data[5:8]))
if length < 6 {
return 18, common.IncorrectDataLength(length, 6)
} else if length > common.MAX_FRAME_SIZE-8 {
return 18, common.FrameTooLarge
}
// Read in data.
certs, err := common.ReadExactly(reader, length-10)
if err != nil {
return 18, err
}
frame.Slot = common.BytesToUint16(data[8:10])
proofLen := int(common.BytesToUint32(data[10:14]))
if proofLen > 0 {
frame.Proof = data[14 : 14+proofLen]
} else {
frame.Proof = []byte{}
}
numCerts := 0
for offset := 0; offset < length-10; {
offset += int(common.BytesToUint32(certs[offset:offset+4])) + 4
numCerts++
}
frame.Certificates = make([]*x509.Certificate, numCerts)
for i, offset := 0, 0; offset < length-10; i++ {
length := int(common.BytesToUint32(certs[offset : offset+4]))
rawCert := certs[offset+4 : offset+4+length]
frame.Certificates[i], err = x509.ParseCertificate(rawCert)
if err != nil {
return int64(length + 8), err
}
offset += length + 4
}
return int64(length + 8), nil
}
func (frame *CREDENTIAL) String() string {
buf := new(bytes.Buffer)
buf.WriteString("CREDENTIAL {\n\t")
buf.WriteString(fmt.Sprintf("Version: 3\n\t"))
buf.WriteString(fmt.Sprintf("Slot: %d\n\t", frame.Slot))
buf.WriteString(fmt.Sprintf("Proof: %v\n\t", frame.Proof))
buf.WriteString(fmt.Sprintf("Certificates: %v\n}\n", frame.Certificates))
return buf.String()
}
func (frame *CREDENTIAL) WriteTo(writer io.Writer) (int64, error) {
proofLength := len(frame.Proof)
certsLength := 0
for _, cert := range frame.Certificates {
certsLength += len(cert.Raw)
}
length := 6 + proofLength + certsLength
out := make([]byte, 14)
out[0] = 128 // Control bit and Version
out[1] = 3 // Version
out[2] = 0 // Type
out[3] = 10 // Type
out[4] = 0 // common.Flags
out[5] = byte(length >> 16) // Length
out[6] = byte(length >> 8) // Length
out[7] = byte(length) // Length
out[8] = byte(frame.Slot >> 8) // Slot
out[9] = byte(frame.Slot) // Slot
out[10] = byte(proofLength >> 24) // Proof Length
out[11] = byte(proofLength >> 16) // Proof Length
out[12] = byte(proofLength >> 8) // Proof Length
out[13] = byte(proofLength) // Proof Length
err := common.WriteExactly(writer, out)
if err != nil {
return 0, err
}
if len(frame.Proof) > 0 {
err = common.WriteExactly(writer, frame.Proof)
if err != nil {
return 14, err
}
}
written := int64(14 + len(frame.Proof))
for _, cert := range frame.Certificates {
err = common.WriteExactly(writer, cert.Raw)
if err != nil {
return written, err
}
written += int64(len(cert.Raw))
}
return written, nil
}