-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrypto.go
198 lines (158 loc) · 4.81 KB
/
crypto.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
// pago - a command-line password manager.
//
// License: MIT.
// See the file LICENSE.
package main
import (
"bytes"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"filippo.io/age"
"filippo.io/age/armor"
)
// Parse the entire text of an age recipients file.
func parseRecipients(contents string) ([]age.Recipient, error) {
var recips []age.Recipient
for _, line := range strings.Split(contents, "\n") {
line = strings.TrimSpace(line)
if line == "" || strings.HasPrefix(line, "#") {
continue
}
recipient, err := age.ParseX25519Recipient(line)
if err != nil {
return nil, fmt.Errorf("invalid recipient: %v", err)
}
recips = append(recips, recipient)
}
return recips, nil
}
// Encrypt the password and save it to a file.
func saveEntry(recipients, passwordStore, name, password string) error {
recipientsData, err := os.ReadFile(recipients)
if err != nil {
return fmt.Errorf("failed to read recipients file: %v", err)
}
recips, err := parseRecipients(string(recipientsData))
if err != nil {
return err
}
dest, err := entryFile(passwordStore, name)
if err != nil {
return err
}
err = os.MkdirAll(filepath.Dir(dest), dirPerms)
if err != nil {
return fmt.Errorf("failed to create output path: %v", err)
}
f, err := os.Create(dest)
if err != nil {
return fmt.Errorf("failed to create output file: %v", err)
}
defer f.Close()
armorWriter := armor.NewWriter(f)
w, err := age.Encrypt(armorWriter, recips...)
if err != nil {
return fmt.Errorf("failed to create encryption writer: %v", err)
}
if _, err := io.WriteString(w, password); err != nil {
return fmt.Errorf("failed to encrypt: %v", err)
}
if err := w.Close(); err != nil {
return fmt.Errorf("failed to finish encryption: %v", err)
}
if err := armorWriter.Close(); err != nil {
return fmt.Errorf("failed to close armor writer: %w", err)
}
return nil
}
// Returns a reader that can handle both armored and binary age files.
func wrapDecrypt(r io.Reader, identities ...age.Identity) (io.Reader, error) {
buffer := make([]byte, len(armor.Header))
// Check if the input starts with an armor header.
n, err := io.ReadFull(r, buffer)
if err != nil && !errors.Is(err, io.EOF) && n < len(armor.Header) {
return nil, fmt.Errorf("failed to read header: %v", err)
}
armored := string(buffer[:n]) == armor.Header
r = io.MultiReader(bytes.NewReader(buffer[:n]), r)
if armored {
return age.Decrypt(armor.NewReader(r), identities...)
}
return age.Decrypt(r, identities...)
}
func decryptIdentities(identitiesPath string) (string, error) {
encryptedData, err := os.ReadFile(identitiesPath)
if err != nil {
return "", fmt.Errorf("failed to read identities file: %v", err)
}
password, err := secureRead("Enter password to unlock identities: ")
if err != nil {
return "", fmt.Errorf("failed to read password: %v", err)
}
// Create a passphrase-based identity and decrypt the private keys with it.
identity, err := age.NewScryptIdentity(password)
if err != nil {
return "", fmt.Errorf("failed to create password-based identity: %v", err)
}
r, err := wrapDecrypt(bytes.NewReader(encryptedData), identity)
if err != nil {
return "", fmt.Errorf("failed to decrypt identities: %v", err)
}
decrypted, err := io.ReadAll(r)
if err != nil {
return "", fmt.Errorf("failed to read decrypted content: %v", err)
}
return string(decrypted), nil
}
func decryptEntry(agentSocket, identities, passwordStore, name string) (string, error) {
file, err := entryFile(passwordStore, name)
if err != nil {
return "", err
}
encryptedData, err := os.ReadFile(file)
if err != nil {
return "", fmt.Errorf("failed to read password file: %v", err)
}
// If an agent socket is configured, try to use the agent.
if agentSocket != "" {
if err := pingAgent(agentSocket); err != nil {
// Ping failed.
// Attempt to start the agent.
identitiesText, err := decryptIdentities(identities)
if err != nil {
return "", err
}
if err := startAgentProcess(agentSocket, identitiesText); err != nil {
return "", fmt.Errorf("failed to start agent: %v", err)
}
}
password, err := decryptWithAgent(agentSocket, encryptedData)
if err != nil {
return "", err
}
return password, nil
}
// When no agent socket is configured, decrypt directly.
// Decrypt the password-protected identities file first.
identitiesText, err := decryptIdentities(identities)
if err != nil {
return "", err
}
ids, err := age.ParseIdentities(strings.NewReader(identitiesText))
if err != nil {
return "", fmt.Errorf("failed to parse identities: %v", err)
}
r, err := wrapDecrypt(bytes.NewReader(encryptedData), ids...)
if err != nil {
return "", fmt.Errorf("failed to decrypt: %v", err)
}
password, err := io.ReadAll(r)
if err != nil {
return "", fmt.Errorf("failed to read decrypted content: %v", err)
}
return string(password), nil
}