forked from nymsio/pgpmail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencrypt.go
260 lines (236 loc) · 7.24 KB
/
encrypt.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
package pgpmail
import (
"bytes"
"errors"
"fmt"
"net/mail"
"time"
"golang.org/x/crypto/openpgp"
"golang.org/x/crypto/openpgp/armor"
)
const (
StatusNone = iota
StatusSignedOnly
StatusEncryptedOnly
StatusSignedAndEncrypted
StatusFailedNoSignKey
StatusFailedPassphraseNeeded
StatusFailedNeedPubkeys
StatusFailed
)
type EncryptStatus struct {
Code int
MissingKeys []string
FailureMessage string
Message *Message
}
func (m *Message) Encrypt(keysrc KeySource) *EncryptStatus {
return encryptMessage(m, keysrc, false, "")
}
func (m *Message) EncryptAndSign(keysrc KeySource, passphrase string) *EncryptStatus {
return encryptMessage(m, keysrc, true, passphrase)
}
func createEncryptFailure(msg string) *EncryptStatus {
status := new(EncryptStatus)
status.Code = StatusFailed
status.FailureMessage = msg
return status
}
func encryptMessage(m *Message, keysrc KeySource, sign bool, passphrase string) *EncryptStatus {
as := getRecipientAddresses(m)
if len(as) == 0 {
return createEncryptFailure("cannot encrypt message, no recipients")
}
pubkeys, err := getRecipientKeys(keysrc, as)
if err != nil {
return createEncryptFailure(err.Error())
}
if sign {
return encryptAndSignMessage(m, pubkeys, keysrc, passphrase)
}
return encryptWith(m, pubkeys, nil, "")
}
func encryptAndSignMessage(m *Message, pubkeys openpgp.EntityList, keysrc KeySource, passphrase string) *EncryptStatus {
if !useCombinedSignatures {
st := m.Sign(keysrc, passphrase)
if st.Code != StatusSignedOnly {
return st
}
st = encryptWith(m, pubkeys, nil, "")
if st.Code == StatusEncryptedOnly {
st.Code = StatusSignedAndEncrypted
}
return st
}
signingKey, err := getSigningKey(m, keysrc)
if err != nil {
if _, ok := err.(NoSignaturePrivateKeyError); ok {
return &EncryptStatus{Code: StatusFailedNoSignKey}
}
return createEncryptFailure(err.Error())
}
return encryptWith(m, pubkeys, signingKey, passphrase)
}
func getRecipientKeys(keysrc KeySource, addresses []string) ([]*openpgp.Entity, error) {
var missing []string
pubkeys := []*openpgp.Entity{}
for _, a := range addresses {
k, err := keysrc.GetPublicKey(a)
if err != nil {
return nil, errors.New("error looking up recipient key '" + a + "': " + err.Error())
}
if k == nil {
missing = append(missing, a)
} else {
pubkeys = append(pubkeys, k)
}
}
if len(missing) > 0 {
return nil, PublicKeysNeededError{missing}
}
return pubkeys, nil
}
var recipientHeaders = []string{"To", "Cc", "Bcc"}
func getRecipientAddresses(m *Message) []string {
as := []string{}
for _, hName := range recipientHeaders {
for _, hVal := range m.GetHeaders(hName) {
addrs, err := mail.ParseAddressList(hVal)
if err == nil {
for _, addr := range addrs {
as = append(as, addr.Address)
}
}
}
}
if encryptToSelf {
self := getSenderAddress(m)
if self == "" {
logger.Warning("Was unable to determine sender address while compiling recipient key list")
} else {
as = append(as, self)
}
}
return as
}
func encryptWith(m *Message, pubkeys openpgp.EntityList, signingEntity *openpgp.Entity, passphrase string) *EncryptStatus {
if len(pubkeys) == 0 {
return createEncryptFailure("no recipient keys")
}
buffer := new(bytes.Buffer)
ar, err := armor.Encode(buffer, "PGP MESSAGE", nil)
if err != nil {
return createEncryptFailure("error encoding output message: " + err.Error())
}
if signingEntity.PrivateKey == nil {
return createEncryptFailure("signing key has no private key")
}
if signingEntity != nil && isSigningKeyLocked(signingEntity, passphrase) {
return &EncryptStatus{Code: StatusFailedPassphraseNeeded}
}
w, err := openpgp.Encrypt(ar, pubkeys, signingEntity, nil, openpgpConfig)
if err != nil {
return createEncryptFailure("encryption operation failed: " + err.Error())
}
bodyPart := createBodyMimePart(m)
w.Write(bodyPart.rawContent)
w.Close()
ar.Close()
buffer.WriteString("\n")
err = writeEncryptedMimeBody(m, buffer.Bytes())
if err != nil {
return createEncryptFailure(err.Error())
}
if signingEntity != nil {
return &EncryptStatus{Code: StatusSignedAndEncrypted, Message: m}
} else {
return &EncryptStatus{Code: StatusEncryptedOnly, Message: m}
}
}
func isSigningKeyLocked(e *openpgp.Entity, passphrase string) bool {
sk, ok := signingKey(e, openpgpConfig.Now())
// if !ok then openpgp.Encrypt() is going to fail and return the right error
if !ok || !sk.PrivateKey.Encrypted {
return false
}
err := sk.PrivateKey.Decrypt([]byte(passphrase))
return err != nil
}
// signingKey return the best candidate Key for signing a message with this
// Entity.
func signingKey(e *openpgp.Entity, now time.Time) (openpgp.Key, bool) {
candidateSubkey := -1
for i, subkey := range e.Subkeys {
if subkey.Sig.FlagsValid &&
subkey.Sig.FlagSign &&
subkey.PublicKey.PubKeyAlgo.CanSign() &&
!subkey.Sig.KeyExpired(now) {
candidateSubkey = i
break
}
}
if candidateSubkey != -1 {
subkey := e.Subkeys[candidateSubkey]
return openpgp.Key{e, subkey.PublicKey, subkey.PrivateKey, subkey.Sig}, true
}
// If we have no candidate subkey then we assume that it's ok to sign
// with the primary key.
i := primaryIdentity(e)
if !i.SelfSignature.FlagsValid || i.SelfSignature.FlagSign &&
!i.SelfSignature.KeyExpired(now) {
return openpgp.Key{e, e.PrimaryKey, e.PrivateKey, i.SelfSignature}, true
}
return openpgp.Key{}, false
}
// primaryIdentity returns the Identity marked as primary or the first identity
// if none are so marked.
func primaryIdentity(e *openpgp.Entity) *openpgp.Identity {
var firstIdentity *openpgp.Identity
for _, ident := range e.Identities {
if firstIdentity == nil {
firstIdentity = ident
}
if ident.SelfSignature.IsPrimaryId != nil && *ident.SelfSignature.IsPrimaryId {
return ident
}
}
return firstIdentity
}
func writeEncryptedMimeBody(m *Message, encryptedBody []byte) error {
boundary := randomBoundary()
ct := fmt.Sprintf("multipart/encrypted; boundary=%s; protocol=\"application/pgp-encrypted\"", boundary)
m.AddHeader(ctHeader, ct)
m.parseContentType()
m.mpContent = createEncryptedMultipart(boundary, encryptedBody)
if err := m.PackMultiparts(); err != nil {
return errors.New("failed writing multipart/encrypted body: " + err.Error())
}
return nil
}
func createEncryptedMultipart(boundary string, encryptedBody []byte) *multipartContent {
const preamble = "This is an OpenPGP/MIME encrypted message (RFC 4880 and 3156)\r\n"
mp := newMultipartContent(boundary, preamble)
mp.addPart(createVersionMimePart())
mp.addPart(createEncryptedMimePart(encryptedBody))
return mp
}
func createVersionMimePart() *MessagePart {
p := new(MessagePart)
const contentType = "application/pgp-encrypted"
const description = "PGP/MIME version identification"
p.AddHeader(ctHeader, contentType)
p.AddHeader("Content-Description", description)
p.Body = "Version: 1\r\n"
return p
}
func createEncryptedMimePart(encryptedBody []byte) *MessagePart {
p := new(MessagePart)
const contentType = "application/octet-stream; name=\"encrypted.asc\""
const description = "OpenPGP encrypted message"
const disposition = "inline; filename=\"encrypted.asc\""
p.AddHeader(ctHeader, contentType)
p.AddHeader("Content-Description", description)
p.AddHeader("Content-Disposition", disposition)
p.Body = insertCR(string(encryptedBody))
return p
}