-
Notifications
You must be signed in to change notification settings - Fork 0
/
gpg.go
210 lines (181 loc) · 4.76 KB
/
gpg.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
package MIMEMail
import (
"fmt"
"io"
"net/mail"
"golang.org/x/crypto/openpgp"
"golang.org/x/crypto/openpgp/armor"
"golang.org/x/crypto/openpgp/packet"
)
// UnpackKey parses the ASCIIArmored data read from r.
func UnpackKey(r io.Reader) (*packet.PublicKey, error) {
deArmored, err := armor.Decode(r)
if err != nil {
return nil, err
}
pack, err := packet.Read(deArmored.Body)
if err != nil {
return nil, err
}
pubKey, ok := pack.(*packet.PublicKey)
if !ok {
return nil, fmt.Errorf("not a public key")
}
return pubKey, nil
}
// UnPackPrivateKey parses the ASCIIArmored data read from r as a private key.
func UnPackPrivateKey(r io.Reader) (*packet.PrivateKey, error) {
deArmored, err := armor.Decode(r)
if err != nil {
return nil, err
}
pack, err := packet.Read(deArmored.Body)
if err != nil {
return nil, err
}
pubKey, ok := pack.(*packet.PrivateKey)
if !ok {
return nil, fmt.Errorf("not a private key")
}
return pubKey, nil
}
// addrToPGPUserID converts the given mail.Address into a pgp.UserId.
func addrToPGPUserID(addr mail.Address) *packet.UserId {
return packet.NewUserId(addr.Name, "", addr.Address)
}
// CreateEntity creates a reciepient entity using the given account.
func CreateEntity(a *Account) (*openpgp.Entity, error) {
key, err := a.Key.Open()
if err != nil {
return nil, err
}
pubKey, err := UnpackKey(key)
if err != nil {
return nil, err
}
userID := addrToPGPUserID(mail.Address{})
prim := true
return &openpgp.Entity{
PrimaryKey: pubKey,
Identities: map[string]*openpgp.Identity{
userID.Id: &openpgp.Identity{
Name: userID.Id,
UserId: userID,
SelfSignature: &packet.Signature{
IsPrimaryId: &prim,
PreferredSymmetric: []uint8{
uint8(packet.CipherAES128),
uint8(packet.CipherAES256),
uint8(packet.CipherCAST5),
},
PreferredHash: []uint8{8, 10},
//PreferredCompression: []uint8{0},
},
},
},
}, nil
}
// CreateSigningEntity creates a signing entity for the given Account
func CreateSigningEntity(a *Account) (*openpgp.Entity, error) {
key, err := a.Key.Open()
if err != nil {
return nil, err
}
privKey, err := UnPackPrivateKey(key)
if err != nil {
return nil, err
}
if privKey.Encrypted {
if err := privKey.Decrypt([]byte(a.Key.Pass)); err != nil {
return nil, err
}
}
userID := addrToPGPUserID(mail.Address{})
prim := true
return &openpgp.Entity{
PrimaryKey: &privKey.PublicKey,
PrivateKey: privKey,
Identities: map[string]*openpgp.Identity{
userID.Id: &openpgp.Identity{
Name: userID.Id,
UserId: userID,
SelfSignature: &packet.Signature{
IsPrimaryId: &prim,
PreferredSymmetric: []uint8{
uint8(packet.CipherAES128),
uint8(packet.CipherAES256),
uint8(packet.CipherCAST5),
},
PreferredHash: []uint8{8, 10},
//PreferredCompression: []uint8{0},
},
},
},
}, nil
}
// newASCIIArmorer returns an ASCII armor encoding writer for pgp messages.
func newASCIIArmorer(w io.Writer) (io.WriteCloser, error) {
return armor.Encode(w, "PGP MESSAGE", nil)
}
// Encrypt encrypts (and ASCII armor encodes) the data written to the returned writer
// Remember to Close the writer when you are done.
func Encrypt(out io.Writer, to *Account, signer *Account) (io.WriteCloser, error) {
enc, err := CreateEntity(to)
if err != nil {
return nil, err
}
var (
sign *openpgp.Entity
config *packet.Config
)
if signer != nil {
if sign, err = CreateSigningEntity(signer); err != nil {
return nil, err
}
config = signer.Key.Config
}
arm, err := newASCIIArmorer(out)
if err != nil {
return nil, err
}
plain, err := openpgp.Encrypt(arm, []*openpgp.Entity{enc}, sign, nil, config)
if err != nil {
return nil, err
}
return closeWrapper{WriteCloser: plain, close: arm}, nil
}
// Sign signs (and ASCII armor encodes) the data written to the returned writer
// Remember to Close the writer when you are done.
func Sign(out io.Writer, signer *Account) (io.WriteCloser, error) {
if signer == nil {
return nil, fmt.Errorf("signer cannot be nil!")
}
sign, err := CreateSigningEntity(signer)
if err != nil {
return nil, err
}
config := signer.Key.Config
arm, err := newASCIIArmorer(out)
if err != nil {
return nil, err
}
plain, err := openpgp.Sign(arm, sign, nil, config)
if err != nil {
return nil, err
}
return closeWrapper{WriteCloser: plain, close: arm}, nil
}
// closeWrapper works arround a quirk in the openpgp implementation.
// The implementation wraps it's writer in a NoOPCloser to guard against closing
// before all data has been written if signer is not nil.
type closeWrapper struct {
io.WriteCloser
close io.WriteCloser
}
// Close implements io.Closer
func (c closeWrapper) Close() error {
if err := c.WriteCloser.Close(); err != nil {
return err
}
return c.close.Close()
}