forked from globalsign/est
-
Notifications
You must be signed in to change notification settings - Fork 0
/
encoding.go
276 lines (228 loc) · 7.26 KB
/
encoding.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*
Copyright (c) 2020 GMO GlobalSign, Inc.
Licensed under the MIT License (the "License"); you may not use this file except
in compliance with the License. You may obtain a copy of the License at
https://opensource.org/licenses/MIT
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package est
import (
"bytes"
"crypto"
"crypto/x509"
"encoding/base64"
"errors"
"fmt"
"io"
"io/ioutil"
"reflect"
"github.com/google/go-tpm/tpm2"
"go.mozilla.org/pkcs7"
)
const (
base64LineLength = 76
)
// base64Encode base64-encodes a slice of bytes using standard encoding.
func base64Encode(src []byte) []byte {
enc := make([]byte, base64.StdEncoding.EncodedLen(len(src)))
base64.StdEncoding.Encode(enc, src)
return breakLines(enc, base64LineLength)
}
// base64Decode base64-decodes a slice of bytes using standard encoding.
func base64Decode(src []byte) ([]byte, error) {
dec := make([]byte, base64.StdEncoding.DecodedLen(len(src)))
n, err := base64.StdEncoding.Decode(dec, src)
if err != nil {
return nil, err
}
return dec[:n], nil
}
// encodePKCS7CertsOnly encodes a slice of certificates as a PKCS#7 degenerate
// "certs-only" response.
func encodePKCS7CertsOnly(certs []*x509.Certificate) ([]byte, error) {
var cb []byte
for _, cert := range certs {
cb = append(cb, cert.Raw...)
}
return pkcs7.DegenerateCertificate(cb)
}
// decodePKCS7CertsOnly decodes a PKCS#7 degenerate "certs-only" response and
// returns the certificate(s) it contains.
func decodePKCS7CertsOnly(b []byte) ([]*x509.Certificate, error) {
p7, err := pkcs7.Parse(b)
if err != nil {
return nil, err
}
return p7.Certificates, nil
}
// readAllBase64Response reads all data from a reader and base64-decodes it.
// It returns a normal error and is intended to be used from client code.
func readAllBase64Response(r io.Reader) ([]byte, error) {
b, err := ioutil.ReadAll(r)
if err != nil {
return nil, fmt.Errorf("failed to read HTTP response body: %w", err)
}
decoded, err := base64Decode(b)
if err != nil {
return nil, fmt.Errorf("failed to base64-decode HTTP response body: %w", err)
}
return decoded, nil
}
// readCertsResponse reads all data from a reader and decodes it as a base64
// encoded PKCS#7 certs-only structure. It returns a normal error and is
// intended to be used from client code.
func readCertsResponse(r io.Reader) ([]*x509.Certificate, error) {
p7, err := readAllBase64Response(r)
if err != nil {
return nil, err
}
certs, err := decodePKCS7CertsOnly(p7)
if err != nil {
return nil, fmt.Errorf("failed to decode PKCS7: %w", err)
}
return certs, nil
}
// readCertResponse reads all data from a reader and decodes it as a base64
// encoded PKCS#7 certs-only structure which is expected to contain exactly
// one certificate. It returns a normal error and is intended to be used from
// client code.
func readCertResponse(r io.Reader) (*x509.Certificate, error) {
p7, err := readAllBase64Response(r)
if err != nil {
return nil, err
}
certs, err := decodePKCS7CertsOnly(p7)
if err != nil {
return nil, fmt.Errorf("failed to decode PKCS7: %w", err)
}
if n := len(certs); n < 1 {
return nil, errors.New("no certificate returned")
} else if n > 1 {
return nil, fmt.Errorf("%d certificates returned", n)
}
return certs[0], nil
}
// readCSRAttrsResponse reads all data from a reader and decodes it as a base64
// encoded CSR attributes structure. It returns a normal error and is intended
// to be used from client code.
func readCSRAttrsResponse(r io.Reader) (CSRAttrs, error) {
der, err := readAllBase64Response(r)
if err != nil {
return CSRAttrs{}, err
}
var attrs CSRAttrs
if err := attrs.Unmarshal(der); err != nil {
return CSRAttrs{}, fmt.Errorf("failed to unmarshal CSR attributes: %w", err)
}
return attrs, nil
}
// readAllBase64Response reads all data from a reader and base64-decodes it.
// It returns an error implementing Error and is intended to be used by server
// code.
func readAllBase64Request(r io.Reader) ([]byte, error) {
b, err := ioutil.ReadAll(r)
if err != nil {
return nil, errInternal
}
decoded, err := base64Decode(b)
if err != nil {
return nil, errInvalidBase64
}
return decoded, nil
}
// readCSRResponse reads all data from a reader and decodes it as a base64
// encoded PKCS#10 CSR. If checkSignature is true, the CSR signature will be
// cryptographically verified after decoding. It returns an error implementing
// Error and is intended to be used by server code.
func readCSRRequest(r io.Reader, checkSignature bool) (*x509.CertificateRequest, error) {
der, estErr := readAllBase64Request(r)
if estErr != nil {
return nil, estErr
}
csr, err := x509.ParseCertificateRequest(der)
if err != nil {
return nil, errInvalidPKCS10
}
if checkSignature {
if err := csr.CheckSignature(); err != nil {
return nil, errInvalidPKCS10Signature
}
}
return csr, nil
}
// readCertsRequest reads all data from a reader and decodes it as a base64
// encoded PKCS#7 certs-only structure. It returns an error implementing Error
// and is intended to be used by server code.
func readCertsRequest(r io.Reader) ([]*x509.Certificate, error) {
der, estErr := readAllBase64Request(r)
if estErr != nil {
return nil, estErr
}
certs, err := decodePKCS7CertsOnly(der)
if err != nil {
return nil, errInvalidPKCS7
}
if n := len(certs); n < 1 {
return nil, errNoCertificatesInPKCS7
}
return certs, nil
}
// readTPMPublicAreaRequest reads all data from a reader and decodes it as a
// base64 encoded TPM object public area. It returns an error implementing
// Error and is intended to be used by server code.
func readTPMPublicAreaRequest(r io.Reader) ([]byte, error) {
pub, estErr := readAllBase64Request(r)
if estErr != nil {
return nil, estErr
}
if _, err := tpm2.DecodePublic(pub); err != nil {
return nil, errInvalidTPMPublicArea
}
return pub, nil
}
// validatePublicAreaPublicKey checks if the public key in the provided TPM
// object public area matches the provided public key. It returns an error
// implementing Error and is intended to be used by server code.
func validatePublicAreaPublicKey(pub []byte, key crypto.PublicKey) error {
dec, err := tpm2.DecodePublic(pub)
if err != nil {
return errInvalidTPMPublicArea
}
var pk crypto.PublicKey
if pk, err = dec.Key(); err != nil {
return errExtractPublicAreaKey
}
if !reflect.DeepEqual(pk, key) {
return errTPMPublicKeyNoMatch
}
return nil
}
// breakLines inserts a CRLF line break in the provided slice of bytes every n
// bytes, including a terminating CRLF for the last line.
func breakLines(b []byte, n int) []byte {
crlf := []byte{'\r', '\n'}
initialLen := len(b)
// Just return a terminating CRLF if the input is empty.
if initialLen == 0 {
return crlf
}
// Allocate a buffer with suitable capacity to minimize allocations.
buf := bytes.NewBuffer(make([]byte, 0, initialLen+((initialLen/n)+1)*2))
// Split input into CRLF-terminated lines.
for {
lineLen := len(b)
if lineLen == 0 {
break
} else if lineLen > n {
lineLen = n
}
buf.Write(b[0:lineLen])
b = b[lineLen:]
buf.Write(crlf)
}
return buf.Bytes()
}