This repository has been archived by the owner on Oct 11, 2023. It is now read-only.
forked from pavlo-v-chernykh/keystore-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencoder.go
241 lines (226 loc) · 5.29 KB
/
encoder.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
package keystore
import (
"crypto/rand"
"crypto/sha1"
"errors"
"hash"
"io"
"math"
)
// ErrEncodedSequenceTooLong indicates that size of string or bytes trying to encode too big
var ErrEncodedSequenceTooLong = errors.New("keystore: encoded sequence too long")
// ErrIncorrectEntryType indicates incorrect entry type addressing
var ErrIncorrectEntryType = errors.New("keystore: incorrect entry type")
type keyStoreEncoder struct {
w io.Writer
b [bufSize]byte
md hash.Hash
rand io.Reader
}
func (kse *keyStoreEncoder) writeUint16(value uint16) error {
const blockSize = 2
order.PutUint16(kse.b[:blockSize], value)
_, err := kse.w.Write(kse.b[:blockSize])
if err != nil {
return err
}
_, err = kse.md.Write(kse.b[:blockSize])
if err != nil {
return err
}
return nil
}
func (kse *keyStoreEncoder) writeUint32(value uint32) error {
const blockSize = 4
order.PutUint32(kse.b[:blockSize], value)
_, err := kse.w.Write(kse.b[:blockSize])
if err != nil {
return err
}
_, err = kse.md.Write(kse.b[:blockSize])
if err != nil {
return err
}
return nil
}
func (kse *keyStoreEncoder) writeUint64(value uint64) error {
const blockSize = 8
order.PutUint64(kse.b[:blockSize], value)
_, err := kse.w.Write(kse.b[:blockSize])
if err != nil {
return err
}
_, err = kse.md.Write(kse.b[:blockSize])
if err != nil {
return err
}
return nil
}
func (kse *keyStoreEncoder) writeBytes(value []byte) error {
_, err := kse.w.Write(value)
if err != nil {
return err
}
_, err = kse.md.Write(value)
if err != nil {
return err
}
return nil
}
func (kse *keyStoreEncoder) writeString(value string) error {
strLen := len(value)
if strLen > math.MaxUint16 {
return ErrEncodedSequenceTooLong
}
err := kse.writeUint16(uint16(strLen))
if err != nil {
return err
}
err = kse.writeBytes([]byte(value))
if err != nil {
return err
}
return nil
}
func (kse *keyStoreEncoder) writeCertificate(cert *Certificate) error {
err := kse.writeString(cert.Type)
if err != nil {
return err
}
certLen := uint64(len(cert.Content))
if certLen > math.MaxUint32 {
return ErrEncodedSequenceTooLong
}
err = kse.writeUint32(uint32(certLen))
if err != nil {
return err
}
err = kse.writeBytes(cert.Content)
if err != nil {
return err
}
return nil
}
func (kse *keyStoreEncoder) writeTrustedCertificateEntry(alias string, tce *TrustedCertificateEntry) error {
err := kse.writeUint32(trustedCertificateTag)
if err != nil {
return err
}
err = kse.writeString(alias)
if err != nil {
return err
}
err = kse.writeUint64(uint64(timeToMilliseconds(tce.CreationDate)))
if err != nil {
return err
}
err = kse.writeCertificate(&tce.Certificate)
if err != nil {
return err
}
return nil
}
func (kse *keyStoreEncoder) writePrivateKeyEntry(alias string, pke *PrivateKeyEntry, password []byte) error {
err := kse.writeUint32(privateKeyTag)
if err != nil {
return err
}
err = kse.writeString(alias)
if err != nil {
return err
}
err = kse.writeUint64(uint64(timeToMilliseconds(pke.CreationDate)))
if err != nil {
return err
}
encodedPrivKeyContent, err := protectKey(kse.rand, pke.PrivKey, password)
if err != nil {
return err
}
privKeyLen := uint64(len(encodedPrivKeyContent))
if privKeyLen > math.MaxUint32 {
return ErrEncodedSequenceTooLong
}
err = kse.writeUint32(uint32(privKeyLen))
if err != nil {
return err
}
err = kse.writeBytes(encodedPrivKeyContent)
if err != nil {
return err
}
certCount := uint64(len(pke.CertChain))
if certCount > math.MaxUint32 {
return ErrEncodedSequenceTooLong
}
err = kse.writeUint32(uint32(certCount))
if err != nil {
return err
}
for _, cert := range pke.CertChain {
err = kse.writeCertificate(&cert)
if err != nil {
return err
}
}
return nil
}
// Encode encrypts and signs keystore using password and writes its representation into w
// It is strongly recommended to fill password slice with zero after usage
func Encode(w io.Writer, ks KeyStore, password []byte) error {
return EncodeWithRand(rand.Reader, w, ks, password)
}
// Encode encrypts and signs keystore using password and writes its representation into w
// Random bytes are read from rand, which must be a cryptographically secure source of randomness
// It is strongly recommended to fill password slice with zero after usage
func EncodeWithRand(rand io.Reader, w io.Writer, ks KeyStore, password []byte) error {
kse := keyStoreEncoder{
w: w,
md: sha1.New(),
rand: rand,
}
passwordBytes := passwordBytes(password)
defer zeroing(passwordBytes)
_, err := kse.md.Write(passwordBytes)
if err != nil {
return err
}
_, err = kse.md.Write(whitenerMessage)
if err != nil {
return err
}
err = kse.writeUint32(magic)
if err != nil {
return err
}
// always write latest version
err = kse.writeUint32(version02)
if err != nil {
return err
}
err = kse.writeUint32(uint32(len(ks)))
if err != nil {
return err
}
for alias, entry := range ks {
switch typedEntry := entry.(type) {
case *PrivateKeyEntry:
err = kse.writePrivateKeyEntry(alias, typedEntry, password)
if err != nil {
return err
}
case *TrustedCertificateEntry:
err = kse.writeTrustedCertificateEntry(alias, typedEntry)
if err != nil {
return err
}
default:
return ErrIncorrectEntryType
}
}
err = kse.writeBytes(kse.md.Sum(nil))
if err != nil {
return err
}
return nil
}