forked from pavlo-v-chernykh/keystore-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
decoder.go
297 lines (233 loc) · 6.9 KB
/
decoder.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
package keystore
import (
"bytes"
"crypto/sha1"
"errors"
"fmt"
"hash"
"io"
)
const defaultCertificateType = "X509"
type keyStoreDecoder struct {
r io.Reader
b [bufSize]byte
md hash.Hash
}
func (ksd *keyStoreDecoder) readUint16() (uint16, error) {
const blockSize = 2
if _, err := io.ReadFull(ksd.r, ksd.b[:blockSize]); err != nil {
return 0, fmt.Errorf("read uint16: %w", err)
}
if _, err := ksd.md.Write(ksd.b[:blockSize]); err != nil {
return 0, fmt.Errorf("update digest: %w", err)
}
return byteOrder.Uint16(ksd.b[:blockSize]), nil
}
func (ksd *keyStoreDecoder) readUint32() (uint32, error) {
const blockSize = 4
if _, err := io.ReadFull(ksd.r, ksd.b[:blockSize]); err != nil {
return 0, fmt.Errorf("read uint32: %w", err)
}
if _, err := ksd.md.Write(ksd.b[:blockSize]); err != nil {
return 0, fmt.Errorf("update digest: %w", err)
}
return byteOrder.Uint32(ksd.b[:blockSize]), nil
}
func (ksd *keyStoreDecoder) readUint64() (uint64, error) {
const blockSize = 8
if _, err := io.ReadFull(ksd.r, ksd.b[:blockSize]); err != nil {
return 0, fmt.Errorf("read uint64: %w", err)
}
if _, err := ksd.md.Write(ksd.b[:blockSize]); err != nil {
return 0, fmt.Errorf("update digest: %w", err)
}
return byteOrder.Uint64(ksd.b[:blockSize]), nil
}
func (ksd *keyStoreDecoder) readBytes(num uint32) ([]byte, error) {
var result []byte
for lenToRead := num; lenToRead > 0; {
blockSize := lenToRead
if blockSize > bufSize {
blockSize = bufSize
}
if _, err := io.ReadFull(ksd.r, ksd.b[:blockSize]); err != nil {
return result, fmt.Errorf("read %d bytes: %w", num, err)
}
result = append(result, ksd.b[:blockSize]...)
lenToRead -= blockSize
}
if _, err := ksd.md.Write(result); err != nil {
return nil, fmt.Errorf("update digest: %w", err)
}
return result, nil
}
func (ksd *keyStoreDecoder) readString() (string, error) {
strLen, err := ksd.readUint16()
if err != nil {
return "", fmt.Errorf("read length: %w", err)
}
strBody, err := ksd.readBytes(uint32(strLen))
if err != nil {
return "", fmt.Errorf("read body: %w", err)
}
return string(strBody), nil
}
func (ksd *keyStoreDecoder) readCertificate(version uint32) (*Certificate, error) {
var certType string
switch version {
case version01:
certType = defaultCertificateType
case version02:
readCertType, err := ksd.readString()
if err != nil {
return nil, fmt.Errorf("read type: %w", err)
}
certType = readCertType
default:
return nil, errors.New("got unknown version")
}
certLen, err := ksd.readUint32()
if err != nil {
return nil, fmt.Errorf("read length: %w", err)
}
certContent, err := ksd.readBytes(certLen)
if err != nil {
return nil, fmt.Errorf("read content: %w", err)
}
certificate := Certificate{
Type: certType,
Content: certContent,
}
return &certificate, nil
}
func (ksd *keyStoreDecoder) readPrivateKeyEntry(version uint32, password []byte) (*PrivateKeyEntry, error) {
creationTimeStamp, err := ksd.readUint64()
if err != nil {
return nil, fmt.Errorf("read creation timestamp: %w", err)
}
length, err := ksd.readUint32()
if err != nil {
return nil, fmt.Errorf("read length: %w", err)
}
encryptedPrivateKey, err := ksd.readBytes(length)
if err != nil {
return nil, fmt.Errorf("read encrypted private key: %w", err)
}
certNum, err := ksd.readUint32()
if err != nil {
return nil, fmt.Errorf("read number of certificates: %w", err)
}
chain := make([]Certificate, 0, certNum)
for i := uint32(0); i < certNum; i++ {
cert, err := ksd.readCertificate(version)
if err != nil {
return nil, fmt.Errorf("read %d certificate: %w", i, err)
}
chain = append(chain, *cert)
}
decryptedPrivateKey, err := decrypt(encryptedPrivateKey, password)
if err != nil {
return nil, fmt.Errorf("decrypt content: %w", err)
}
creationDateTime := millisecondsToTime(int64(creationTimeStamp))
privateKeyEntry := PrivateKeyEntry{
Entry: Entry{
CreationTime: creationDateTime,
},
PrivateKey: decryptedPrivateKey,
CertificateChain: chain,
}
return &privateKeyEntry, nil
}
func (ksd *keyStoreDecoder) readTrustedCertificateEntry(version uint32) (*TrustedCertificateEntry, error) {
creationTimeStamp, err := ksd.readUint64()
if err != nil {
return nil, fmt.Errorf("read creation timestamp: %w", err)
}
certificate, err := ksd.readCertificate(version)
if err != nil {
return nil, fmt.Errorf("read certificate: %w", err)
}
creationDateTime := millisecondsToTime(int64(creationTimeStamp))
trustedCertificateEntry := TrustedCertificateEntry{
Entry: Entry{
CreationTime: creationDateTime,
},
Certificate: *certificate,
}
return &trustedCertificateEntry, nil
}
func (ksd *keyStoreDecoder) readEntry(version uint32, password []byte) (string, interface{}, error) {
tag, err := ksd.readUint32()
if err != nil {
return "", nil, fmt.Errorf("read tag: %w", err)
}
alias, err := ksd.readString()
if err != nil {
return "", nil, fmt.Errorf("read alias: %w", err)
}
switch tag {
case privateKeyTag:
entry, err := ksd.readPrivateKeyEntry(version, password)
if err != nil {
return "", nil, fmt.Errorf("read private key entry: %w", err)
}
return alias, entry, nil
case trustedCertificateTag:
entry, err := ksd.readTrustedCertificateEntry(version)
if err != nil {
return "", nil, fmt.Errorf("read trusted certificate entry: %w", err)
}
return alias, entry, nil
default:
return "", nil, errors.New("got unknown entry tag")
}
}
// Decode reads keystore representation from r then decrypts and check signature using password
// It is strongly recommended to fill password slice with zero after usage.
func Decode(r io.Reader, password []byte) (KeyStore, error) {
ksd := keyStoreDecoder{
r: r,
md: sha1.New(),
}
passwordBytes := passwordBytes(password)
defer zeroing(passwordBytes)
if _, err := ksd.md.Write(passwordBytes); err != nil {
return nil, fmt.Errorf("update digest with password: %w", err)
}
if _, err := ksd.md.Write(whitenerMessage); err != nil {
return nil, fmt.Errorf("update digest with whitener message: %w", err)
}
readMagic, err := ksd.readUint32()
if err != nil {
return nil, fmt.Errorf("read magic: %w", err)
}
if readMagic != magic {
return nil, errors.New("got invalid magic")
}
version, err := ksd.readUint32()
if err != nil {
return nil, fmt.Errorf("read version: %w", err)
}
entryNum, err := ksd.readUint32()
if err != nil {
return nil, fmt.Errorf("read number of entries: %w", err)
}
keyStore := make(KeyStore, entryNum)
for i := uint32(0); i < entryNum; i++ {
alias, entry, err := ksd.readEntry(version, password)
if err != nil {
return nil, fmt.Errorf("read %d entry: %w", i, err)
}
keyStore[alias] = entry
}
actualDigest, err := ksd.readBytes(uint32(ksd.md.Size()))
if err != nil {
return nil, fmt.Errorf("read digest: %w", err)
}
computedDigest := ksd.md.Sum(nil)
if !bytes.Equal(actualDigest, computedDigest) {
return nil, errors.New("got invalid digest")
}
return keyStore, nil
}