-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
473 lines (389 loc) · 10.7 KB
/
main.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
/*
* Copyright (c) 2018 Alec Newman <alecnwmn904@gmail.com>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
package main
import (
"bytes"
"crypto/rand"
"encoding/binary"
"encoding/hex"
"encoding/pem"
"flag"
"fmt"
"golang.org/x/crypto/nacl/box"
"golang.org/x/crypto/ssh/terminal"
"io"
"io/ioutil"
"os"
"os/user"
"path"
"regexp"
)
const maxChunkSize = 16 * 1024
func emitChunk(w io.Writer, chunk []byte) error {
n := uint16(len(chunk))
if n > maxChunkSize {
panic("chunk too big")
}
var header [4]byte
binary.LittleEndian.PutUint16(header[:], n)
if _, err := w.Write(header[:]); err != nil {
return err
}
_, err := w.Write(chunk)
return err
}
func readChunk(r io.Reader, chunk []byte) (int, error) {
var header [4]byte
if _, err := io.ReadFull(r, header[:]); err != nil {
return 0, err
}
n := int(binary.LittleEndian.Uint16(header[:]))
if n > maxChunkSize {
return 0, fmt.Errorf("Chunk is too big")
}
return io.ReadFull(r, chunk[:n])
}
type entity struct {
publicKey *[32]byte
secretKey *[32]byte
}
func loadEntity(name string) (*entity, error) {
data, err := ioutil.ReadFile(name)
if err != nil {
return nil, err
}
var entity entity
for {
block, rest := pem.Decode(data)
if block == nil {
break
}
if block.Type == "BOX SECRET KEY" {
var key [32]byte
copy(key[:], block.Bytes)
entity.secretKey = &key
} else if block.Type == "BOX PUBLIC KEY" {
var key [32]byte
copy(key[:], block.Bytes)
entity.publicKey = &key
}
data = rest
}
return &entity, nil
}
func usage() {
fmt.Fprintln(os.Stderr, "box help")
fmt.Fprintln(os.Stderr, "box new-identity [-name NAME]")
fmt.Fprintln(os.Stderr, "box add-peer -name NAME -key PUBLICKEY")
fmt.Fprintln(os.Stderr, "box list [NAME ...]")
fmt.Fprintln(os.Stderr, "box seal [-from IDENTITY] -to PEER <MESSAGE >SEALED")
fmt.Fprintln(os.Stderr, "box open -from PEER [-to IDENTITY] <SEALED >MESSAGE")
fmt.Fprintln(os.Stderr, "")
}
const nameReString = `[a-zA-Z0-9-_]+`
var nameRe = regexp.MustCompile(nameReString)
func validateName(name string) error {
if !nameRe.MatchString(name) {
return fmt.Errorf("Invalid name %q; must have form %s", name, nameReString)
}
return nil
}
func doMain(args []string) error {
if len(args) < 2 {
usage()
return fmt.Errorf("Command required")
}
boxdir := os.Getenv("BOXDIR")
if boxdir == "" {
current, err := user.Current()
if err != nil {
return fmt.Errorf("Getting current user: %s", err)
}
boxdir = path.Join(current.HomeDir, ".box")
}
switch args[1] {
case "help":
usage()
case "seal":
var from, to string
f := flag.NewFlagSet("seal", flag.ExitOnError)
f.StringVar(&from, "from", "self", "Box is authenticated as coming from this identity")
f.StringVar(&to, "to", "", "Box can only be opened by this peer")
f.Parse(args[2:])
if from == "" {
usage()
return fmt.Errorf("-from required")
}
if to == "" {
usage()
return fmt.Errorf("-to required")
}
sender, err := loadEntity(path.Join(boxdir, from))
if err != nil {
return fmt.Errorf("Loading identity: %s", err)
}
if sender.publicKey == nil || sender.secretKey == nil {
return fmt.Errorf("Sender is not an identity")
}
receiver, err := loadEntity(path.Join(boxdir, to))
if err != nil {
return fmt.Errorf("Loading peer: %s", err)
}
if receiver.publicKey == nil {
return fmt.Errorf("Receiver is not a peer")
}
var sharedKey [32]byte
box.Precompute(&sharedKey, receiver.publicKey, sender.secretKey)
buf := make([]byte, maxChunkSize-24-box.Overhead)
chunk := make([]byte, maxChunkSize)
if terminal.IsTerminal(int(os.Stdin.Fd())) {
fmt.Fprintln(os.Stderr, "Note: reading payload from stdin")
}
if terminal.IsTerminal(int(os.Stdout.Fd())) {
return fmt.Errorf("Refusing to write sealed data to a terminal")
}
if err := emitChunk(os.Stdout, []byte("v0")); err != nil {
return fmt.Errorf("Emitting header chunk: %s", err)
}
for {
n, err := os.Stdin.Read(buf)
if n == 0 && err == io.EOF {
break
} else if err != nil && err != io.EOF {
return fmt.Errorf("Reading chunk: %s", err)
}
var nonce [24]byte
if _, err := rand.Read(nonce[:]); err != nil {
return fmt.Errorf("Creating nonce: %s", err)
}
chunk = chunk[:0]
chunk = append(chunk, nonce[:]...)
chunk = box.SealAfterPrecomputation(chunk, buf[:n], &nonce, &sharedKey)
if err := emitChunk(os.Stdout, chunk); err != nil {
return fmt.Errorf("Emitting chunk: %s", err)
}
}
case "open":
var from, to string
f := flag.NewFlagSet("open", flag.ExitOnError)
f.StringVar(&from, "from", "", "Box originates from this peer")
f.StringVar(&to, "to", "self", "Box is intended to be received by this identity.")
f.Parse(args[2:])
if from == "" {
usage()
return fmt.Errorf("-from required")
}
if to == "" {
usage()
return fmt.Errorf("-to required")
}
sender, err := loadEntity(path.Join(boxdir, from))
if err != nil {
return fmt.Errorf("Loading peer: %s", err)
}
if sender.publicKey == nil {
return fmt.Errorf("Sender is not a peer")
}
receiver, err := loadEntity(path.Join(boxdir, to))
if err != nil {
return fmt.Errorf("Loading identity: %s", err)
}
if receiver.publicKey == nil || receiver.secretKey == nil {
return fmt.Errorf("Receiver is not an identity")
}
var sharedKey [32]byte
box.Precompute(&sharedKey, sender.publicKey, receiver.secretKey)
chunk := make([]byte, maxChunkSize)
buf := make([]byte, maxChunkSize-24-box.Overhead)
if terminal.IsTerminal(int(os.Stdin.Fd())) {
return fmt.Errorf("Refusing to read sealed payload from terminal")
}
n, err := readChunk(os.Stdin, chunk)
if err == io.EOF {
return fmt.Errorf("Expected header chunk")
} else if err != nil {
return fmt.Errorf("Reading header chunk: %s", err)
}
if !bytes.Equal(chunk[:n], []byte("v0")) {
return fmt.Errorf("Unknown version; got chunk %q", chunk[:n])
}
for {
n, err := readChunk(os.Stdin, chunk)
if err == io.EOF {
break
} else if err != nil {
return fmt.Errorf("Reading sealed chunk: %s", err)
}
if n < 24 {
return fmt.Errorf("Chunk is too small")
}
var nonce [24]byte
copy(nonce[:], chunk[:24])
buf, ok := box.OpenAfterPrecomputation(buf[:0], chunk[24:n], &nonce, &sharedKey)
if !ok {
return fmt.Errorf("Failed to unseal chunk")
}
if _, err := os.Stdout.Write(buf); err != nil {
return fmt.Errorf("Writing out message: %s", err)
}
}
case "new-identity":
var name string
f := flag.NewFlagSet("new-identity", flag.ExitOnError)
f.StringVar(&name, "name", "self", "Name of identity to create.")
f.Parse(args[2:])
if err := validateName(name); err != nil {
return err
}
seed := make([]byte, 32)
if _, err := rand.Read(seed); err != nil {
return err
}
publicKey, secretKey, err := box.GenerateKey(bytes.NewReader(seed))
if err != nil {
return err
}
if err := os.MkdirAll(boxdir, 0700); err != nil {
return err
}
out, err := os.OpenFile(path.Join(boxdir, name), os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0400)
if err != nil {
return err
}
defer out.Close()
if err := pem.Encode(out, &pem.Block{
Type: "BOX SECRET SEED",
Bytes: seed,
}); err != nil {
return err
}
if err := pem.Encode(out, &pem.Block{
Type: "BOX SECRET KEY",
Bytes: secretKey[:],
}); err != nil {
return err
}
if err := pem.Encode(out, &pem.Block{
Type: "BOX PUBLIC KEY",
Bytes: publicKey[:],
}); err != nil {
return err
}
case "add-peer":
var name, publicKeyHex string
f := flag.NewFlagSet("add-peer", flag.ExitOnError)
f.StringVar(&name, "name", "self", "Name of peer to add")
f.StringVar(&publicKeyHex, "key", "", "Public key of peer")
f.Parse(args[2:])
if name == "" {
usage()
return fmt.Errorf("-name is required")
}
if publicKeyHex == "" {
usage()
return fmt.Errorf("-key is required")
}
if err := validateName(name); err != nil {
return err
}
publicKey, err := hex.DecodeString(publicKeyHex)
if err != nil {
return err
}
if len(publicKey) != 32 {
return fmt.Errorf("Public key has wrong length")
}
if err := os.MkdirAll(boxdir, 0700); err != nil {
return err
}
out, err := os.OpenFile(path.Join(boxdir, name), os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0400)
if err != nil {
return err
}
defer out.Close()
if err := pem.Encode(out, &pem.Block{
Type: "BOX PUBLIC KEY",
Bytes: publicKey,
}); err != nil {
return err
}
case "list":
var onlyKey bool
f := flag.NewFlagSet("list", flag.ExitOnError)
f.BoolVar(&onlyKey, "only-key", false, "Only show public key (for scripts)")
f.Parse(args[2:])
names := f.Args()
type row struct {
name string
kind string
publicKey *[32]byte
}
if len(names) == 0 {
dir, err := os.Open(boxdir)
if os.IsNotExist(err) {
return nil
} else if err != nil {
return fmt.Errorf("Opening %s: %s", boxdir, err)
}
names, err = dir.Readdirnames(-1)
if err != nil {
return fmt.Errorf("Reading entries in %s: %s", boxdir, err)
}
}
rows := make([]row, 0)
longestNameLen := 0
for _, name := range names {
if len(name) > longestNameLen {
longestNameLen = len(name)
}
entity, err := loadEntity(path.Join(boxdir, name))
if err != nil {
return err
}
kind := "none"
if entity.publicKey != nil && entity.secretKey == nil {
kind = "peer"
} else if entity.publicKey != nil && entity.secretKey != nil {
kind = "identity"
}
rows = append(rows, row{
name: name,
kind: kind,
publicKey: entity.publicKey,
})
}
if onlyKey {
for _, row := range rows {
fmt.Printf("%x\n", *row.publicKey)
}
} else {
fmt.Printf("%-[1]*s %-8s %-64s\n", longestNameLen, "NAME", "KIND", "PUBLIC KEY")
for _, row := range rows {
fmt.Printf("%-[1]*s %-8s %-64x\n", longestNameLen, row.name, row.kind, *row.publicKey)
}
}
default:
usage()
return fmt.Errorf("Unknown command %s", args[1])
}
return nil
}
func main() {
if err := doMain(os.Args); err != nil {
fmt.Fprintf(os.Stderr, "Error: %s\n", err)
os.Exit(1)
}
}