-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathkey.go
332 lines (310 loc) · 6.88 KB
/
key.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
package gitcrypt
import (
"bytes"
"errors"
"fmt"
"io"
"log"
"os"
)
// KeyFromFile instantiates a new key from a specified file
func (g *GitCrypt) KeyFromFile(filename string) (Key, error) {
k := Key{Parent: g}
err := k.LoadFromFile(filename)
return k, err
}
// Key is a git-crypt key structure
type Key struct {
Parent *GitCrypt
Version uint32
Entries []KeyEntry
KeyName string
Debug bool
}
// Latest returns the latest of the entries from the key file
func (k *Key) Latest() (KeyEntry, error) {
if len(k.Entries) == 0 {
return KeyEntry{}, fmt.Errorf("no entries")
}
return k.Entries[len(k.Entries)-1], nil
}
// Get retrieves an entry by version number
func (k *Key) Get(version uint32) (KeyEntry, error) {
for _, v := range k.Entries {
if v.Version == version {
return v, nil
}
}
return KeyEntry{}, fmt.Errorf("not found")
}
// LoadFromFile loads a key from a filesystem file
func (k *Key) LoadFromFile(filename string) error {
if !k.Parent.fileExists(filename) {
return errors.New("file does not exist")
}
fp, err := os.Open(filename)
if err != nil {
return err
}
defer fp.Close()
return k.Load(fp)
}
// Load imports a key from an io.Reader
func (k *Key) Load(in io.Reader) error {
preamble, err := readXBytes(in, 16)
if err != nil {
return err
}
if preamble[0] != byte(0) && bytes.Compare(preamble[1:13], []byte("GITCRYPTKEY")) != 0 {
return fmt.Errorf("malformed preamble")
}
format, err := readBigEndianUint32(bytes.NewBuffer(preamble[12:]))
if err != nil {
return err
}
if format != formatVersion {
return fmt.Errorf("incompatible version %d", format)
}
k.Version = format
if k.Debug {
log.Printf("format: %x", format)
}
err = k.loadHeader(in)
if err != nil {
return fmt.Errorf("LoadHeader: %s", err.Error())
}
k.Entries = make([]KeyEntry, 0)
for {
entry := KeyEntry{}
err = entry.Load(in)
if err != nil {
break
}
k.Entries = append(k.Entries, entry)
}
return nil
}
// Store stores a copy of the key to a file
func (k Key) Store(out io.Writer) error {
n, err := out.Write([]byte("\x00GITCRYPTKEY"))
if err != nil {
return err
}
if n != 12 {
return fmt.Errorf("unable to write 12 bytes, wrote %d bytes", n)
}
writeBigEndianUint32(out, formatVersion)
if k.KeyName != "" {
if k.Debug {
log.Printf("Store: KeyName = %s", k.KeyName)
}
err = writeBigEndianUint32(out, headerFieldKeyName)
if err != nil {
return err
}
kn := stringToASCIIBytes(k.KeyName)
err = writeBigEndianUint32(out, uint32(len(kn)))
if err != nil {
return err
}
_, err = out.Write(kn)
if err != nil {
return err
}
}
_ = writeBigEndianUint32(out, headerFieldEnd)
for _, e := range k.Entries {
if k.Debug {
log.Printf("Store: Write Entry: %#v", e)
}
err = e.Store(out)
if err != nil {
return err
}
}
return nil
}
func (k *Key) loadHeader(in io.Reader) error {
for {
fieldID, err := readBigEndianUint32(in)
if err != nil {
return errors.New("malformed")
}
if fieldID == headerFieldEnd {
break
}
fieldLen, err := readBigEndianUint32(in)
if err != nil {
return errors.New("malformed")
}
switch fieldID {
case headerFieldKeyName:
if fieldLen > keyNameMaxLength {
return errors.New("malformed")
}
if fieldLen == 0 {
// special case field_len==0 to avoid possible undefined behavior
// edge cases with an empty std::vector (particularly, &bytes[0]).
k.KeyName = ""
} else {
raw, err := readXBytes(in, int(fieldLen))
if err != nil {
k.KeyName = ""
return errors.New("malformed")
}
k.KeyName = string(raw)
err = validateKeyName(k.KeyName)
if err != nil {
k.KeyName = ""
return errors.New("malformed")
}
}
case fieldID & 1:
return errors.New("incompatible")
default:
// unknown non-critical field - safe to ignore
if fieldLen > maxFieldLength {
return errors.New("malformed")
}
_, err := readXBytes(in, int(fieldLen))
if err != nil {
return errors.New("malformed")
}
}
}
return nil
}
// KeyEntry is a key entry
type KeyEntry struct {
Version uint32
AesKey []byte
HmacKey []byte
}
// Store implements writing a Key
// TODO: FIXME: XXX: FINISH IMPLEMENT
func (k KeyEntry) Store(out io.Writer) error {
err := writeBigEndianUint32(out, keyFieldVersion)
if err != nil {
return err
}
err = writeBigEndianUint32(out, 4)
if err != nil {
return err
}
err = writeBigEndianUint32(out, k.Version)
if err != nil {
return err
}
// AES key
err = writeBigEndianUint32(out, keyFieldAesKey)
if err != nil {
return err
}
err = writeBigEndianUint32(out, aesKeyLen)
if err != nil {
return err
}
// HMAC key
err = writeBigEndianUint32(out, keyFieldHmacKey)
if err != nil {
return err
}
err = writeBigEndianUint32(out, hmacKeyLen)
if err != nil {
return err
}
// End
err = writeBigEndianUint32(out, keyFieldEnd)
return err
}
// Generate generates a new key
func (k *KeyEntry) Generate(version uint32) error {
k.Version = version
k.AesKey = randomBytes(aesKeyLen)
k.HmacKey = randomBytes(hmacKeyLen)
return nil
}
// Load loads an entry from a stream
func (k *KeyEntry) Load(in io.Reader) error {
for {
fieldID, err := readBigEndianUint32(in)
if err != nil {
return errors.New("malformed")
}
if fieldID == keyFieldEnd {
break
}
fieldLen, err := readBigEndianUint32(in)
if err != nil {
return errors.New("malformed")
}
switch fieldID {
case keyFieldVersion:
if fieldLen != 4 {
return fmt.Errorf("malformed version")
}
k.Version, err = readBigEndianUint32(in)
if err != nil {
return err
}
case keyFieldAesKey:
if fieldLen != aesKeyLen {
return fmt.Errorf("malformed (bad AES key)")
}
raw, err := readXBytes(in, int(fieldLen))
if err != nil {
return err
}
k.AesKey = raw
case keyFieldHmacKey:
if fieldLen != hmacKeyLen {
return fmt.Errorf("malformed (bad HMAC key)")
}
raw, err := readXBytes(in, int(fieldLen))
if err != nil {
return err
}
k.HmacKey = raw
case fieldID & 1:
return fmt.Errorf("malformed")
default:
if fieldLen > maxFieldLength {
return fmt.Errorf("malformed (> maxFieldLength)")
}
_, err := readXBytes(in, int(fieldLen))
if err != nil {
return err
}
}
}
return nil
}
func validateKeyName(keyName string) error {
if keyName == "" {
return errors.New("key name may not be empty")
}
if keyName == "default" {
return errors.New("`default' is not a legal key name")
}
// Need to be restrictive with key names because they're used as part of a Git filter name
for i := 0; i < len(keyName); i++ {
ch := byte(keyName[i])
if ch >= 'a' && ch <= 'z' {
continue
}
if ch >= 'A' && ch <= 'Z' {
continue
}
if ch >= '0' && ch <= '9' {
continue
}
if ch == '-' || ch == '_' {
continue
}
return errors.New("Key names may contain only A-Z, a-z, 0-9, '-', and '_'")
}
if len(keyName) > keyNameMaxLength {
return errors.New("key name is too long")
}
return nil
}