forked from pavlo-v-chernykh/keystore-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
encoder.go
203 lines (191 loc) · 6.47 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
package keystore
import (
"crypto/rand"
"crypto/sha1"
"errors"
"fmt"
"hash"
"io"
"math"
)
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
byteOrder.PutUint16(kse.b[:blockSize], value)
if _, err := kse.w.Write(kse.b[:blockSize]); err != nil {
return fmt.Errorf("write uint16: %w", err)
}
if _, err := kse.md.Write(kse.b[:blockSize]); err != nil {
return fmt.Errorf("update digest: %w", err)
}
return nil
}
func (kse *keyStoreEncoder) writeUint32(value uint32) error {
const blockSize = 4
byteOrder.PutUint32(kse.b[:blockSize], value)
if _, err := kse.w.Write(kse.b[:blockSize]); err != nil {
return fmt.Errorf("write uint32: %w", err)
}
if _, err := kse.md.Write(kse.b[:blockSize]); err != nil {
return fmt.Errorf("update digest: %w", err)
}
return nil
}
func (kse *keyStoreEncoder) writeUint64(value uint64) error {
const blockSize = 8
byteOrder.PutUint64(kse.b[:blockSize], value)
if _, err := kse.w.Write(kse.b[:blockSize]); err != nil {
return fmt.Errorf("write uint64: %w", err)
}
if _, err := kse.md.Write(kse.b[:blockSize]); err != nil {
return fmt.Errorf("update digest: %w", err)
}
return nil
}
func (kse *keyStoreEncoder) writeBytes(value []byte) error {
if _, err := kse.w.Write(value); err != nil {
return fmt.Errorf("write %d bytes: %w", len(value), err)
}
if _, err := kse.md.Write(value); err != nil {
return fmt.Errorf("update digest: %w", err)
}
return nil
}
func (kse *keyStoreEncoder) writeString(value string) error {
strLen := len(value)
if strLen > math.MaxUint16 {
return fmt.Errorf("got string %d bytes long, max length is %d", strLen, math.MaxUint16)
}
if err := kse.writeUint16(uint16(strLen)); err != nil {
return fmt.Errorf("write length: %w", err)
}
if err := kse.writeBytes([]byte(value)); err != nil {
return fmt.Errorf("write body: %w", err)
}
return nil
}
func (kse *keyStoreEncoder) writeCertificate(cert Certificate) error {
if err := kse.writeString(cert.Type); err != nil {
return fmt.Errorf("write type: %w", err)
}
certLen := uint64(len(cert.Content))
if certLen > math.MaxUint32 {
return fmt.Errorf("got certificate %d bytes long, max length is %d", certLen, uint64(math.MaxUint32))
}
if err := kse.writeUint32(uint32(certLen)); err != nil {
return fmt.Errorf("write length: %w", err)
}
if err := kse.writeBytes(cert.Content); err != nil {
return fmt.Errorf("write content: %w", err)
}
return nil
}
func (kse *keyStoreEncoder) writePrivateKeyEntry(alias string, pke *PrivateKeyEntry, password []byte) error {
if err := kse.writeUint32(privateKeyTag); err != nil {
return fmt.Errorf("write tag: %w", err)
}
if err := kse.writeString(alias); err != nil {
return fmt.Errorf("write alias: %w", err)
}
if err := kse.writeUint64(uint64(timeToMilliseconds(pke.CreationTime))); err != nil {
return fmt.Errorf("write creation timestamp: %w", err)
}
encryptedContent, err := encrypt(kse.rand, pke.PrivateKey, password)
if err != nil {
return fmt.Errorf("encrypt content: %w", err)
}
length := uint64(len(encryptedContent))
if length > math.MaxUint32 {
return fmt.Errorf("got encrypted content %d bytes long, max length is %d", length, uint64(math.MaxUint32))
}
if err := kse.writeUint32(uint32(length)); err != nil {
return fmt.Errorf("filed to write length: %w", err)
}
if err := kse.writeBytes(encryptedContent); err != nil {
return fmt.Errorf("write content: %w", err)
}
certNum := uint64(len(pke.CertificateChain))
if certNum > math.MaxUint32 {
return fmt.Errorf("got certificate chain %d entries long, max number of entries is %d", certNum, uint64(math.MaxUint32))
}
if err := kse.writeUint32(uint32(certNum)); err != nil {
return fmt.Errorf("write number of certificates: %w", err)
}
for i, cert := range pke.CertificateChain {
if err := kse.writeCertificate(cert); err != nil {
return fmt.Errorf("write %d certificate: %w", i, err)
}
}
return nil
}
func (kse *keyStoreEncoder) writeTrustedCertificateEntry(alias string, tce *TrustedCertificateEntry) error {
if err := kse.writeUint32(trustedCertificateTag); err != nil {
return fmt.Errorf("write tag: %w", err)
}
if err := kse.writeString(alias); err != nil {
return fmt.Errorf("write alias: %w", err)
}
if err := kse.writeUint64(uint64(timeToMilliseconds(tce.CreationTime))); err != nil {
return fmt.Errorf("write creation timestamp: %w", err)
}
if err := kse.writeCertificate(tce.Certificate); err != nil {
return fmt.Errorf("write certificate: %w", 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)
if _, err := kse.md.Write(passwordBytes); err != nil {
return fmt.Errorf("update digest with password: %w", err)
}
if _, err := kse.md.Write(whitenerMessage); err != nil {
return fmt.Errorf("update digest with whitener message: %w", err)
}
if err := kse.writeUint32(magic); err != nil {
return fmt.Errorf("write magic: %w", err)
}
// always write latest version
if err := kse.writeUint32(version02); err != nil {
return fmt.Errorf("write version: %w", err)
}
if err := kse.writeUint32(uint32(len(ks))); err != nil {
return fmt.Errorf("write number of entries: %w", err)
}
for alias, entry := range ks {
switch typedEntry := entry.(type) {
case *PrivateKeyEntry:
if err := kse.writePrivateKeyEntry(alias, typedEntry, password); err != nil {
return fmt.Errorf("write private key entry: %w", err)
}
case *TrustedCertificateEntry:
if err := kse.writeTrustedCertificateEntry(alias, typedEntry); err != nil {
return fmt.Errorf("write trusted certificate entry: %w", err)
}
default:
return errors.New("got invalid entry")
}
}
if err := kse.writeBytes(kse.md.Sum(nil)); err != nil {
return fmt.Errorf("write digest: %w", err)
}
return nil
}