This repository has been archived by the owner on May 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crypt.go
406 lines (347 loc) · 12 KB
/
crypt.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
package gpgutil
import (
"bytes"
"compress/gzip"
"io"
"io/ioutil"
"os"
"os/exec"
"github.com/sbreitf1/errors"
"golang.org/x/crypto/openpgp"
pgperr "golang.org/x/crypto/openpgp/errors"
)
var (
// ErrTechnicalProblem occurs on all errors not related to cryptography.
ErrTechnicalProblem = errors.New("Technical error")
// ErrNoKeySpecified occurs when a mandatory key is not specified.
ErrNoKeySpecified = errors.New("No key specified")
// ErrNoPrivateKey occurs when a public key is supplied, but a private key is expected.
ErrNoPrivateKey = errors.New("Require private key")
// ErrWrongKey occurs when a wrong key is used for decrypting or signature checking.
ErrWrongKey = errors.New("Wrong key")
// ErrKeySourceNotAccepted occurs on importing a disabled key source (See gpgutil.AcceptFileKeySources and gpgutil.AcceptNamedKeySources)
ErrKeySourceNotAccepted = errors.New("Key source %q not accepted")
// ErrImportKeyFailed occurs when a specified key could not be imported.
ErrImportKeyFailed = errors.New("Failed to import key")
// ErrDecryptKeyFailed occurs when a specified key was imported but could not be decrypted.
ErrDecryptKeyFailed = errors.New("Failed to decrypt key")
// ErrWrongSignatureVerificationKey occurs when the key for signature verification does not match the signer.
ErrWrongSignatureVerificationKey = errors.New("Wrong key for signature verification")
// ErrWrongSignature occurs when a signature does not verify the given message.
ErrWrongSignature = errors.New("Wrong signature")
// ErrMissingSignature occurs when a signature was expected but does not exist.
ErrMissingSignature = errors.New("Missing signature")
// ErrGPG occurs on all cryptography related errors.
ErrGPG = errors.New("A cryptographic method failed")
)
var (
// GPGCommand can be used to override the command to execute gpg on this machine.
GPGCommand string
// AcceptFileKeySources determines whether file key sources are accepted.
AcceptFileKeySources bool
// AcceptNamedKeySources determines whether named key sources are accepted.
AcceptNamedKeySources bool
)
// Options define additional parameters
type Options struct {
GZIP bool
SignatureKey KeySource
}
func init() {
GPGCommand = "gpg"
AcceptFileKeySources = true
AcceptNamedKeySources = true
}
// EncryptAndSignFile encrypts and signs the given file using the given GPG keys and applies GZIP compression.
//
// DEPRECATED: Use EncryptFile(... &Options{SignatureKey: SignKeySource}) instead.
func EncryptAndSignFile(inFile, outFile string, encryptKeySrc, signKeySrc KeySource, options *Options) errors.Error {
if !signKeySrc.HasValue() {
return ErrNoKeySpecified.Msg("No signing key specified").Make()
}
if options == nil {
options = &Options{SignatureKey: signKeySrc}
} else {
if signKeySrc.HasValue() {
options.SignatureKey = signKeySrc
}
}
return EncryptFile(inFile, outFile, encryptKeySrc, options)
}
// EncryptFile encrypts the given file using the given GPG keys.
func EncryptFile(inFile, outFile string, encryptKeySrc KeySource, options *Options) errors.Error {
reader, err := os.Open(inFile)
if err != nil {
return ErrTechnicalProblem.Msg("Could not open input file").Make().Cause(err)
}
defer reader.Close()
writer, err := os.Create(outFile)
if err != nil {
return ErrTechnicalProblem.Msg("Could not create output file").Make().Cause(err)
}
defer writer.Close()
return encrypt(reader, writer, encryptKeySrc, options)
}
// EncryptByteSliceToFile encrypts a byte slice and writes the data to a file.
func EncryptByteSliceToFile(data []byte, outFile string, keySrc KeySource, options *Options) errors.Error {
reader := bytes.NewReader(data)
writer, err := os.Create(outFile)
if err != nil {
return ErrTechnicalProblem.Msg("Could not create output file").Make().Cause(err)
}
defer writer.Close()
return encrypt(reader, writer, keySrc, options)
}
func encrypt(reader io.Reader, writer io.Writer, encryptKeySrc KeySource, options *Options) errors.Error {
if !encryptKeySrc.HasValue() {
return ErrNoKeySpecified.Msg("No encryption key specified").Make()
}
encryptKey, importErr := importKeySource(encryptKeySrc, false)
if importErr != nil {
return importErr.Expand("Could not import encryption key")
}
var signKey *openpgp.Entity
if options != nil && options.SignatureKey.HasValue() {
key, err := importKeySource(options.SignatureKey, true)
if err != nil {
return err.Expand("Could not import signing key")
}
//TODO assert len=1
signKey = key[0]
}
pgpWriter, err := openpgp.Encrypt(writer, encryptKey, signKey, &openpgp.FileHints{}, nil)
if err != nil {
return ErrGPG.Msg("Failed to encrypt file").Make().Cause(err)
}
defer pgpWriter.Close()
dstWriter := pgpWriter
if options != nil && options.GZIP {
gzipWriter, err := gzip.NewWriterLevel(dstWriter, 9)
if err != nil {
return ErrTechnicalProblem.Msg("Unable to open gzip writer").Make().Cause(err)
}
defer gzipWriter.Close()
dstWriter = gzipWriter
}
if _, err := io.Copy(dstWriter, reader); err != nil {
return ErrTechnicalProblem.Msg("Failed to write encrypted and gzipped file").Make().Cause(err)
}
return nil
}
// ComputeDetachedSignature creates a detached signature of inFile and writes it to outFile using the given GPG key.
func ComputeDetachedSignature(inFile, outFile string, keySrc KeySource) errors.Error {
if !keySrc.HasValue() {
return ErrNoKeySpecified.Msg("No signing key specified").Make()
}
reader, err := os.Open(inFile)
if err != nil {
return ErrTechnicalProblem.Msg("Could not open input file").Make().Cause(err)
}
key, importErr := importKeySource(keySrc, true)
if importErr != nil {
return importErr.Expand("Could not import signing key")
}
writer, err := os.Create(outFile)
if err != nil {
return ErrTechnicalProblem.Msg("Could not create output file").Make().Cause(err)
}
defer writer.Close()
if err := openpgp.DetachSign(writer, key[0], reader, nil); err != nil {
return ErrGPG.Msg("Failed to generate detached signature").Make().Cause(err)
}
return nil
}
// DecryptFile decrypts a gpg file.
func DecryptFile(inFile, outFile string, keySrc KeySource, options *Options) errors.Error {
reader, err := os.Open(inFile)
if err != nil {
return ErrTechnicalProblem.Msg("Could not open input file").Make().Cause(err)
}
defer reader.Close()
writer, err := os.Create(outFile)
if err != nil {
return ErrTechnicalProblem.Msg("Could not create output file").Make().Cause(err)
}
defer writer.Close()
if err := decrypt(reader, writer, keySrc, options); err != nil {
return err
}
return nil
}
// DecryptFileAndCheckSignature decrypts a gpg file and checks the signature.
//
// DEPRECATED: Use DecryptFile(... &Options{SignatureKey: SignKeySource}) instead.
func DecryptFileAndCheckSignature(inFile, outFile string, decryptKeySrc, signKeySrc KeySource, options *Options) errors.Error {
if !signKeySrc.HasValue() {
return ErrNoKeySpecified.Msg("No signature key specified").Make()
}
if options == nil {
options = &Options{SignatureKey: signKeySrc}
} else {
if signKeySrc.HasValue() {
options.SignatureKey = signKeySrc
}
}
return DecryptFile(inFile, outFile, decryptKeySrc, options)
}
// DecryptFileToByteSlice decrypts a gpg file and returns the data as byte slice.
func DecryptFileToByteSlice(inFile string, keySrc KeySource, options *Options) ([]byte, errors.Error) {
reader, err := os.Open(inFile)
if err != nil {
return nil, ErrTechnicalProblem.Msg("Could not open input file").Make().Cause(err)
}
defer reader.Close()
writer := bytes.NewBuffer(nil)
if err := decrypt(reader, writer, keySrc, options); err != nil {
return nil, err
}
return writer.Bytes(), nil
}
func decrypt(reader io.Reader, writer io.Writer, decryptKeySrc KeySource, options *Options) errors.Error {
if !decryptKeySrc.HasValue() {
return ErrNoKeySpecified.Msg("No decryption key specified").Make()
}
decryptKey, importErr := importKeySource(decryptKeySrc, true)
if importErr != nil {
return importErr.Expand("Could not import decryption key")
}
keyRing := decryptKey
var signKey *openpgp.Entity
if options != nil && options.SignatureKey.HasValue() {
key, err := importKeySource(options.SignatureKey, false)
if err != nil {
return err.Expand("Could not import signature key")
}
//TODO assert len=1
signKey = key[0]
keyRing = append(keyRing, signKey)
}
msg, err := openpgp.ReadMessage(reader, keyRing, nil, nil)
if err != nil {
if err == pgperr.ErrKeyIncorrect {
return ErrWrongKey.Make()
}
return ErrGPG.Msg("Failed to read encrypted message").Make().Cause(err)
}
if signKey != nil {
if !msg.IsSigned {
return ErrMissingSignature.Make()
}
if msg.SignedBy == nil || msg.SignedBy.PublicKey.Fingerprint != signKey.PrimaryKey.Fingerprint {
return ErrWrongSignatureVerificationKey.Make()
}
}
srcReader := msg.UnverifiedBody
if options != nil && options.GZIP {
srcReader, err = gzip.NewReader(srcReader)
if err != nil {
return ErrTechnicalProblem.Msg("Unable to open gzip reader").Make().Cause(err)
}
}
if signKey != nil {
if msg.SignatureError != nil {
return ErrWrongSignature.Make().Cause(msg.SignatureError)
}
}
if _, err := io.Copy(writer, srcReader); err != nil {
return ErrTechnicalProblem.Msg("Failed to decrypt and unzip file").Make().Cause(err)
}
return nil
}
func importKeySource(src KeySource, privateKey bool) (openpgp.EntityList, errors.Error) {
var key openpgp.EntityList
var err errors.Error
if src.IsFileSource() {
if AcceptFileKeySources {
key, err = importKeyFile(src.File, privateKey)
} else {
return nil, ErrKeySourceNotAccepted.Args(src.String()).Make()
}
} else if src.IsNamedSource() {
if AcceptNamedKeySources {
key, err = importNamedKey(src.Owner, privateKey, src.Passphrase)
} else {
return nil, ErrKeySourceNotAccepted.Args(src.String()).Make()
}
} else {
return nil, ErrNoKeySpecified.Make()
}
if err != nil {
return nil, err
}
if len(src.Passphrase) > 0 {
for i := range key {
if err := decryptKey(key[i], src.Passphrase); err != nil {
return nil, err
}
}
}
if privateKey {
privateKeyCount := 0
for i := range key {
if key[i].PrivateKey != nil {
privateKeyCount++
}
}
if privateKeyCount == 0 {
return nil, ErrNoPrivateKey.Make()
}
}
return key, nil
}
func importKeyFile(file string, privateKey bool) (openpgp.EntityList, errors.Error) {
data, err := ioutil.ReadFile(file)
if err != nil {
return nil, ErrTechnicalProblem.Msg("Unable to read key file").Make().Cause(err)
}
return importKey(data)
}
func importNamedKey(owner string, privateKey bool, passphrase string) (openpgp.EntityList, errors.Error) {
//TODO allow custom named key importers
var cmd *exec.Cmd
if len(passphrase) > 0 {
cmd = exec.Command(GPGCommand, "--pinentry-mode=loopback", "--passphrase", passphrase, "--armor", iif(privateKey, "--export-secret-keys", "--export"), owner)
} else {
cmd = exec.Command(GPGCommand, "--armor", iif(privateKey, "--export-secret-keys", "--export"), owner)
}
out, err := cmd.CombinedOutput()
if err != nil {
return nil, ErrTechnicalProblem.Msg("Could not execute gpg command").Make().Cause(err)
}
return importKey(out)
}
func iif(cond bool, then, otherwise string) string {
if cond {
return then
}
return otherwise
}
func importKey(data []byte) (openpgp.EntityList, errors.Error) {
reader := bytes.NewBuffer(data)
var key openpgp.EntityList
var err error
if len(data) >= 3 && data[0] == 45 && data[1] == 45 && data[2] == 45 {
key, err = openpgp.ReadArmoredKeyRing(reader)
} else {
key, err = openpgp.ReadKeyRing(reader)
}
if err != nil {
return nil, ErrImportKeyFailed.Make().Cause(err)
}
return key, nil
}
func decryptKey(entity *openpgp.Entity, passphrase string) errors.Error {
if entity.PrivateKey != nil && entity.PrivateKey.Encrypted {
if err := entity.PrivateKey.Decrypt([]byte(passphrase)); err != nil {
return ErrDecryptKeyFailed.Make().Cause(err)
}
}
for _, subkey := range entity.Subkeys {
if subkey.PrivateKey != nil && subkey.PrivateKey.Encrypted {
if err := subkey.PrivateKey.Decrypt([]byte(passphrase)); err != nil {
return ErrDecryptKeyFailed.Msg("Unable to decrypt a sub key").Make().Cause(err)
}
}
}
return nil
}