-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathencrypt.go
More file actions
391 lines (331 loc) · 11.7 KB
/
encrypt.go
File metadata and controls
391 lines (331 loc) · 11.7 KB
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
package gpg
import (
"bytes"
"context"
"fmt"
"net/mail"
"os/exec"
"regexp"
"strings"
"time"
)
const keyserverFetchTimeout = 5 * time.Second
// ListPublicKeys lists all public keys in the keyring.
func (s *service) ListPublicKeys(ctx context.Context) ([]KeyInfo, error) {
cmd := exec.CommandContext(ctx, "gpg", "--list-keys", "--with-colons", "--with-fingerprint")
output, err := cmd.Output()
if err != nil {
if exitErr, ok := err.(*exec.ExitError); ok && len(exitErr.Stderr) > 0 {
return nil, fmt.Errorf("gpg list public keys failed: %s", string(exitErr.Stderr))
}
// Empty keyring is not an error - return empty slice
return []KeyInfo{}, nil
}
return parsePublicKeys(string(output))
}
// FindPublicKeyByEmail finds a public key by email, auto-fetching from key servers if not found locally.
func (s *service) FindPublicKeyByEmail(ctx context.Context, email string) (*KeyInfo, error) {
// Normalize email for comparison
email = strings.ToLower(strings.TrimSpace(email))
// Step 1: Search local keyring first
keys, err := s.ListPublicKeys(ctx)
if err != nil {
return nil, err
}
for i := range keys {
if keyMatchesEmail(&keys[i], email) {
// Check if key is expired
if keys[i].Expires != nil && keys[i].Expires.Before(time.Now()) {
continue // Skip expired keys
}
return &keys[i], nil
}
}
// Reserved domains are never expected to resolve through public key infrastructure.
// Avoid network-dependent lookups for test-only addresses so CI stays deterministic.
if isReservedLookupEmail(email) {
return nil, fmt.Errorf("no public key found for %s (checked local keyring only; skipped remote lookup for reserved domain)", email)
}
// Step 2: Not found locally - try to fetch from key servers
if fetchErr := s.fetchKeyByEmail(ctx, email); fetchErr != nil {
return nil, fmt.Errorf("no public key found for %s (checked local keyring and %d key servers): %w",
email, len(KeyServers), fetchErr)
}
// Step 3: Retry local search after fetch
keys, err = s.ListPublicKeys(ctx)
if err != nil {
return nil, err
}
for i := range keys {
if keyMatchesEmail(&keys[i], email) {
return &keys[i], nil
}
}
return nil, fmt.Errorf("key fetched but not found for %s", email)
}
// fetchKeyByEmail tries to fetch a public key by email from key servers.
func (s *service) fetchKeyByEmail(ctx context.Context, email string) error {
// Validate email format
parsed, err := mail.ParseAddress(email)
if err != nil {
return fmt.Errorf("invalid email format: %q", email)
}
email = strings.ToLower(parsed.Address)
var lastErr error
for _, server := range KeyServers {
serverCtx, cancel := context.WithTimeout(ctx, keyserverFetchTimeout)
// Use --auto-key-locate with WKD (Web Key Directory) and keyserver fallback
// #nosec G204 - email is validated by mail.ParseAddress above
cmd := exec.CommandContext(serverCtx, "gpg", "--auto-key-locate", "wkd,keyserver", "--keyserver", server, "--locate-keys", email)
var stderr bytes.Buffer
cmd.Stderr = &stderr
if err := cmd.Run(); err != nil {
cancel()
lastErr = fmt.Errorf("failed to fetch from %s: %w", server, err)
continue
}
cancel()
// Success
return nil
}
return fmt.Errorf("failed to fetch key for %s from any server: %w", email, lastErr)
}
func isReservedLookupEmail(email string) bool {
parsed, err := mail.ParseAddress(email)
if err != nil {
return false
}
addr := strings.ToLower(parsed.Address)
at := strings.LastIndex(addr, "@")
if at == -1 || at == len(addr)-1 {
return false
}
return isReservedLookupDomain(addr[at+1:])
}
func isReservedLookupDomain(domain string) bool {
domain = strings.Trim(strings.ToLower(domain), ".")
for _, suffix := range []string{"test", "example", "invalid", "localhost"} {
if domain == suffix || strings.HasSuffix(domain, "."+suffix) {
return true
}
}
return false
}
// keyMatchesEmail checks if a key contains the given email in its UIDs.
func keyMatchesEmail(key *KeyInfo, email string) bool {
email = strings.ToLower(email)
for _, uid := range key.UIDs {
uidLower := strings.ToLower(uid)
// Check for email in angle brackets: "Name <email@example.com>"
if strings.Contains(uidLower, "<"+email+">") {
return true
}
// Check for bare email
if uidLower == email {
return true
}
}
return false
}
// EncryptData encrypts data for one or more recipients using their public keys.
func (s *service) EncryptData(ctx context.Context, recipientKeyIDs []string, data []byte) (*EncryptResult, error) {
if len(recipientKeyIDs) == 0 {
return nil, fmt.Errorf("at least one recipient key ID is required")
}
// Validate all key IDs
for _, keyID := range recipientKeyIDs {
if !isValidGPGKeyID(keyID) {
return nil, fmt.Errorf("invalid GPG key ID format: %q", keyID)
}
}
// Build GPG arguments
args := []string{
"--encrypt",
"--armor",
"--trust-model", "always", // Trust the key for this operation
}
// Add each recipient
for _, keyID := range recipientKeyIDs {
args = append(args, "--recipient", keyID)
}
args = append(args, "--output", "-")
// #nosec G204 - recipientKeyIDs are validated above by isValidGPGKeyID
cmd := exec.CommandContext(ctx, "gpg", args...)
cmd.Stdin = bytes.NewReader(data)
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
if err := cmd.Run(); err != nil {
errMsg := stderr.String()
if strings.Contains(errMsg, "No public key") {
return nil, fmt.Errorf("public key not found for one or more recipients")
}
if strings.Contains(errMsg, "unusable public key") {
return nil, fmt.Errorf("one or more recipient keys are unusable (expired, revoked, or invalid)")
}
return nil, fmt.Errorf("gpg encryption failed: %s", errMsg)
}
ciphertext := stdout.Bytes()
if len(ciphertext) == 0 {
return nil, fmt.Errorf("gpg produced empty ciphertext")
}
return &EncryptResult{
Ciphertext: ciphertext,
RecipientKeys: recipientKeyIDs,
}, nil
}
// SignAndEncryptData signs data with the sender's private key and encrypts for recipients.
// This provides maximum security: only recipients can decrypt, and they can verify the sender.
func (s *service) SignAndEncryptData(ctx context.Context, signerKeyID string, recipientKeyIDs []string, data []byte, senderEmail string) (*EncryptResult, error) {
if signerKeyID == "" {
return nil, fmt.Errorf("signer key ID is required for sign+encrypt")
}
if len(recipientKeyIDs) == 0 {
return nil, fmt.Errorf("at least one recipient key ID is required")
}
// Validate signer key ID
if !isValidGPGKeyID(signerKeyID) {
return nil, fmt.Errorf("invalid signer GPG key ID format: %q", signerKeyID)
}
// Validate all recipient key IDs
for _, keyID := range recipientKeyIDs {
if !isValidGPGKeyID(keyID) {
return nil, fmt.Errorf("invalid recipient GPG key ID format: %q", keyID)
}
}
// Validate senderEmail if provided
if senderEmail != "" {
if _, err := mail.ParseAddress(senderEmail); err != nil {
return nil, fmt.Errorf("invalid sender email format: %q", senderEmail)
}
}
// Build GPG arguments for sign+encrypt
args := []string{
"--sign",
"--encrypt",
"--armor",
"--trust-model", "always",
"--local-user", signerKeyID,
}
// Add --sender for proper UID embedding
if senderEmail != "" {
args = append(args, "--sender", senderEmail)
}
// Add each recipient
for _, keyID := range recipientKeyIDs {
args = append(args, "--recipient", keyID)
}
args = append(args, "--output", "-")
// #nosec G204 - all key IDs are validated above by isValidGPGKeyID
cmd := exec.CommandContext(ctx, "gpg", args...)
cmd.Stdin = bytes.NewReader(data)
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
if err := cmd.Run(); err != nil {
errMsg := stderr.String()
if strings.Contains(errMsg, "No secret key") {
return nil, fmt.Errorf("GPG signing key %s not found or not usable", signerKeyID)
}
if strings.Contains(errMsg, "No public key") {
return nil, fmt.Errorf("public key not found for one or more recipients")
}
if strings.Contains(errMsg, "unusable public key") {
return nil, fmt.Errorf("one or more recipient keys are unusable (expired, revoked, or invalid)")
}
if strings.Contains(errMsg, "Timeout") || strings.Contains(errMsg, "timeout") {
return nil, fmt.Errorf("GPG passphrase prompt timed out. Please ensure gpg-agent is running")
}
return nil, fmt.Errorf("gpg sign+encrypt failed: %s", errMsg)
}
ciphertext := stdout.Bytes()
if len(ciphertext) == 0 {
return nil, fmt.Errorf("gpg produced empty ciphertext")
}
return &EncryptResult{
Ciphertext: ciphertext,
RecipientKeys: recipientKeyIDs,
}, nil
}
// DecryptData decrypts PGP encrypted data using the user's private key.
// It also handles signed+encrypted messages, returning signature verification info.
func (s *service) DecryptData(ctx context.Context, ciphertext []byte) (*DecryptResult, error) {
if len(ciphertext) == 0 {
return nil, fmt.Errorf("ciphertext is empty")
}
// Build GPG arguments
args := []string{
"--decrypt",
"--status-fd", "2", // Output status to stderr for parsing
}
// #nosec G204 - no user input in command
cmd := exec.CommandContext(ctx, "gpg", args...)
cmd.Stdin = bytes.NewReader(ciphertext)
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run()
stderrOutput := stderr.String()
// Parse the result even if there was an error (bad signature still decrypts)
result := parseDecryptOutput(stderrOutput)
result.Plaintext = stdout.Bytes()
if err != nil {
// Check for common errors
if strings.Contains(stderrOutput, "No secret key") {
return nil, fmt.Errorf("no secret key available to decrypt this message. The message was encrypted for a different recipient")
}
if strings.Contains(stderrOutput, "decryption failed") {
return nil, fmt.Errorf("decryption failed: %s", stderrOutput)
}
// If we got plaintext despite the error (e.g., bad signature), return the result
if len(result.Plaintext) > 0 {
return result, nil
}
return nil, fmt.Errorf("gpg decryption failed: %s", stderrOutput)
}
if len(result.Plaintext) == 0 {
return nil, fmt.Errorf("gpg produced empty plaintext")
}
return result, nil
}
// parseDecryptOutput parses GPG status output during decryption.
func parseDecryptOutput(stderrOutput string) *DecryptResult {
result := &DecryptResult{}
// Check for signature status
if strings.Contains(stderrOutput, "GOODSIG") || strings.Contains(stderrOutput, "Good signature") {
result.WasSigned = true
result.SignatureOK = true
} else if strings.Contains(stderrOutput, "BADSIG") || strings.Contains(stderrOutput, "BAD signature") {
result.WasSigned = true
result.SignatureOK = false
}
// Extract signer key ID using regex
// Pattern: "gpg: Signature made ... using RSA key <KEY_ID>"
// or "[GNUPG:] GOODSIG <KEY_ID> <UID>"
keyIDPattern := regexp.MustCompile(`using \w+ key ([A-F0-9]+)`)
if matches := keyIDPattern.FindStringSubmatch(stderrOutput); len(matches) > 1 {
result.SignerKeyID = matches[1]
}
// Also try GOODSIG/BADSIG format: "[GNUPG:] GOODSIG <keyid> <uid>"
if result.SignerKeyID == "" {
goodsigPattern := regexp.MustCompile(`\[GNUPG:\] (?:GOODSIG|BADSIG) ([A-F0-9]+) (.+)`)
if matches := goodsigPattern.FindStringSubmatch(stderrOutput); len(matches) > 2 {
result.SignerKeyID = matches[1]
result.SignerUID = strings.TrimSpace(matches[2])
}
}
// Extract signer UID from "Good signature from" pattern
if result.SignerUID == "" {
uidPattern := regexp.MustCompile(`Good signature from "([^"]+)"`)
if matches := uidPattern.FindStringSubmatch(stderrOutput); len(matches) > 1 {
result.SignerUID = matches[1]
}
}
// Extract decryption key ID
// Pattern: "gpg: encrypted with ... <KEY_ID>"
decKeyPattern := regexp.MustCompile(`encrypted with.*?([A-F0-9]{8,})`)
if matches := decKeyPattern.FindStringSubmatch(stderrOutput); len(matches) > 1 {
result.DecryptKeyID = matches[1]
}
return result
}