-
Notifications
You must be signed in to change notification settings - Fork 11
/
age.go
169 lines (153 loc) · 4.33 KB
/
age.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
package main
import (
"bytes"
"fmt"
"io"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
"filippo.io/age"
"filippo.io/age/armor"
)
const (
recipientFilename = ".strongbox_recipient"
defaultIdentityFilename = ".strongbox_identity"
)
var identityFilename string
func ageGenIdentity(desc string) {
identity, err := age.GenerateX25519Identity()
if err != nil {
log.Fatalf("Failed to generate identity: %v", err)
}
fmt.Printf("public key: %s\n", identity.Recipient().String())
f, err := os.OpenFile(identityFilename, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Fatal(err)
}
defer f.Close()
// we assume that file has a trailing newline
if _, err := f.Write([]byte(fmt.Sprintf("# description: %s\n# public key: %s\n%s\n", desc, identity.Recipient().String(), identity.String()))); err != nil {
log.Fatal(err)
}
if err := f.Close(); err != nil {
log.Fatal(err)
}
}
func ageFileToRecipient(filename string) ([]age.Recipient, error) {
file, err := os.Open(filename)
if err != nil {
return nil, err
}
defer file.Close()
return age.ParseRecipients(file)
}
func ageEncrypt(w io.Writer, r []age.Recipient, in []byte, f string) {
// We have to do check the following because age's encryption is non
// deterministic
//
// if there's no difference between the decrypted version of the file
// at HEAD and the new contents AND file's recipient hasn't changed, do
// not re-encrypt
if agePlaintextEqual(in, f) && !ageRecipientChanged(f) {
fah := ageFileAtHEAD(f)
if _, err := io.Copy(w, bytes.NewReader(fah)); err != nil {
log.Fatal(err)
}
return
}
armorWriter := armor.NewWriter(w)
wc, err := age.Encrypt(armorWriter, r...)
if err != nil {
log.Fatalf("Failed to create encrypted file: %v", err)
}
if _, err := io.Copy(wc, bytes.NewReader(in)); err != nil {
log.Fatal(err)
}
if err := wc.Close(); err != nil {
log.Fatalf("Failed to close encrypted file: %v", err)
}
if err := armorWriter.Close(); err != nil {
log.Fatalf("Failed to close armor: %v", err)
}
}
func ageDecrypt(w io.Writer, in []byte) {
identityFile, err := os.Open(identityFilename)
if err != nil {
// identity file doesn't exist, copy as is and return
if _, err = io.Copy(w, bytes.NewReader(in)); err != nil {
log.Println(err)
}
return
}
defer identityFile.Close()
identities, err := age.ParseIdentities(identityFile)
if err != nil {
// could not parse identity file, copy as is and return
if _, err = io.Copy(w, bytes.NewReader(in)); err != nil {
log.Println(err)
}
return
}
armorReader := armor.NewReader(bytes.NewReader(in))
ar, err := age.Decrypt(armorReader, identities...)
if err != nil {
// couldn't find the key, copy as is and return
if _, err = io.Copy(w, bytes.NewReader(in)); err != nil {
log.Println(err)
}
return
}
if _, err := io.Copy(w, ar); err != nil {
log.Fatal(err)
}
}
func agePlaintextEqual(in []byte, f string) bool {
command := []string{"cat-file", "-e", fmt.Sprintf("HEAD:%s", f)}
cmd := exec.Command("git", command...)
// if git cat-file -e fails, then the file doesn't exist at HEAD, so it's new,
// meaning we need to encrypt it for the first time
if _, err := cmd.CombinedOutput(); err != nil {
return false
}
fileAtHEAD := ageFileAtHEAD(f)
// potentially re-encrypting SIV file
if !strings.HasPrefix(string(fileAtHEAD), armor.Header) {
return false
}
var plaintext bytes.Buffer
ageDecrypt(&plaintext, fileAtHEAD)
return bytes.Equal(plaintext.Bytes(), in)
}
func ageFileAtHEAD(f string) []byte {
command := []string{"cat-file", "-p", fmt.Sprintf("HEAD:%s", f)}
cmd := exec.Command("git", command...)
fileAtHEAD, err := cmd.CombinedOutput()
if err != nil {
log.Fatal(err)
}
return fileAtHEAD
}
func ageRecipientChanged(filename string) bool {
path := filepath.Dir(filename)
for {
if fi, err := os.Stat(path); err == nil && fi.IsDir() {
ageRecipientFilename := filepath.Join(path, recipientFilename)
// If we found `.strongbox_recipient` - compare it with HEAD version
if keyFile, err := os.Stat(ageRecipientFilename); err == nil && !keyFile.IsDir() {
fah := ageFileAtHEAD(ageRecipientFilename)
fod, err := os.ReadFile(ageRecipientFilename)
if err != nil {
log.Fatalf("Failed to open private keys file: %v", err)
}
return !bytes.Equal(fah, fod)
}
}
if path == "." {
break
}
path = filepath.Dir(path)
}
return false
}