-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsign.go
135 lines (123 loc) · 3.56 KB
/
sign.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
package pgpmail
import (
"bytes"
"errors"
"fmt"
"net/mail"
"crypto"
"code.google.com/p/go.crypto/openpgp"
"code.google.com/p/go.crypto/openpgp/packet"
)
const ctHeader = "Content-Type"
const cteHeader = "Content-Transfer-Encoding"
func (m *Message) Sign(keysrc KeySource, passphrase string) *EncryptStatus {
signingKey, err := getSigningKey(m, keysrc)
if err != nil {
if _, ok := err.(NoSignaturePrivateKeyError); ok {
return &EncryptStatus{Code: StatusFailedNoSignKey}
}
return createEncryptFailure(err.Error())
}
err = signMessage(m, passphrase, signingKey)
if err != nil {
if _, ok := err.(PassphraseNeededError); ok {
return &EncryptStatus{Code: StatusFailedPassphraseNeeded}
}
}
return &EncryptStatus{Code: StatusSignedOnly, Message: m}
}
func signMessage(m *Message, passphrase string, signingKey *openpgp.Entity) error {
if isSigningKeyLocked(signingKey, passphrase) {
var e PassphraseNeededError
e.KeyIds = append(e.KeyIds, signingKey.PrimaryKey.KeyId)
return e
}
sigBody := createBodyMimePart(m)
sig, err := createSignature([]byte(sigBody.String()), signingKey, openpgpConfig)
if err != nil {
return err
}
return writeMimeSignatureMessage(m, sigBody, sig)
}
func getSigningKey(m *Message, keysrc KeySource) (*openpgp.Entity, error) {
sender := getSenderAddress(m)
if sender == "" {
return nil, errors.New("signing failed, no sender address found")
}
k, err := keysrc.GetSecretKey(sender)
if err != nil {
return nil, errors.New("error looking up signing key: " + err.Error())
}
if k == nil {
return nil, NoSignaturePrivateKeyError{sender}
}
return k, nil
}
func getSenderAddress(m *Message) string {
hdr := m.GetHeaderValue("From")
if hdr == "" {
return ""
}
as, err := mail.ParseAddressList(hdr)
if err != nil {
logger.Warning("Failed to parse sender address " + hdr)
return ""
}
if len(as) == 0 {
return ""
} else if len(as) > 1 {
logger.Warning("Multiple addresses found as sender address " + hdr)
}
return as[0].Address
}
func hashName(hash crypto.Hash) string {
switch hash {
case crypto.MD5:
return "md5"
case crypto.RIPEMD160:
return "ripemd160"
case crypto.SHA1:
return "sha1"
case crypto.SHA224:
return "sha224"
case crypto.SHA256:
return "sha256"
case crypto.SHA384:
return "sha384"
case crypto.SHA512:
return "sha512"
}
panic(fmt.Sprintf("unknown hash %v", hash))
}
func writeMimeSignatureMessage(m *Message, body *MessagePart, sig string) error {
b := randomBoundary()
ct := fmt.Sprintf("multipart/signed; boundary=%s; micalg=pgp-%s; protocol=\"application/pgp-signature\"", b, hashName(openpgpConfig.Hash()))
m.AddHeader(ctHeader, ct)
m.parseContentType()
m.mpContent = newMultipartContent(b, "")
m.mpContent.addPart(body)
m.mpContent.addPart(createSignaturePart(sig))
if err := m.PackMultiparts(); err != nil {
return errors.New("failed writing signature multipart: " + err.Error())
}
return nil
}
func createSignaturePart(sig string) *MessagePart {
p := new(MessagePart)
const contentType = "application/pgp-signature; name=\"signature.asc\""
const description = "OpenPGP digital signature"
const disposition = "attachment; filename=\"signature.asc\""
p.AddHeader(ctHeader, contentType)
p.AddHeader("Content-Description", description)
p.AddHeader("Content-Disposition", disposition)
p.Body = insertCR(sig)
return p
}
func createSignature(sigBody []byte, k *openpgp.Entity, config *packet.Config) (string, error) {
r := bytes.NewReader(sigBody)
b := new(bytes.Buffer)
if err := openpgp.ArmoredDetachSignText(b, k, r, config); err != nil {
return "", err
}
return b.String(), nil
}