-
Notifications
You must be signed in to change notification settings - Fork 8
/
openpgp.go
211 lines (172 loc) · 5.21 KB
/
openpgp.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
// Copyright Suneido Software Corp. All rights reserved.
// Governed by the MIT license found in the LICENSE file.
package builtin
import (
"bytes"
"io"
"os"
"path/filepath"
"strings"
. "github.com/apmckinlay/gsuneido/core"
"github.com/apmckinlay/gsuneido/util/system"
"github.com/ProtonMail/go-crypto/openpgp"
"github.com/ProtonMail/gopenpgp/v2/crypto"
"github.com/ProtonMail/gopenpgp/v2/helper"
"golang.org/x/exp/maps"
)
type suOpenPGP struct {
staticClass[suOpenPGP]
}
func init() {
Global.Builtin("OpenPGP", &suOpenPGP{})
}
func (*suOpenPGP) String() string {
return "OpenPGP /* builtin class */"
}
func (pgp *suOpenPGP) Equal(other any) bool {
return pgp == other
}
func (*suOpenPGP) Lookup(_ *Thread, method string) Callable {
return openpgpMethods[method]
}
var openpgpMethods = methods()
var _ = staticMethod(opgp_Members, "()")
func opgp_Members() Value {
return SuObjectOfStrs(maps.Keys(openpgpMethods))
}
var _ = staticMethod(opgp_SymmetricEncrypt,
"(passphrase, source, toFile = false)")
func opgp_SymmetricEncrypt(passphrase, source, toFile Value) Value {
if toFile == False {
return opgpStr(passphrase, source, symEncrypt)
}
return opgpFile(passphrase, source, toFile, symEncrypt)
}
var _ = staticMethod(opgp_SymmetricDecrypt,
"(passphrase, source, toFile = false)")
func opgp_SymmetricDecrypt(passphrase, source, toFile Value) Value {
if toFile == False {
return opgpStr(passphrase, source, symDecrypt)
}
return opgpFile(passphrase, source, toFile, symDecrypt)
}
var _ = staticMethod(opgp_PublicEncrypt,
"(publicKey, source, toFile = false)")
func opgp_PublicEncrypt(publicKey, source, toFile Value) Value {
if toFile == False {
return opgpStr(ToStr(publicKey), source, asymEncrypt)
}
return opgpFile(ToStr(publicKey), source, toFile, asymEncrypt)
}
var _ = staticMethod(opgp_PrivateDecrypt,
"(privateKey, passphrase, source, toFile = false)")
func opgp_PrivateDecrypt(privateKey, passphrase, source, toFile Value) Value {
kp := keyPair{privateKey: ToStr(privateKey), passphrase: ToStr(passphrase)}
if toFile == False {
return opgpStr(kp, source, asymDecrypt)
}
return opgpFile(kp, source, toFile, asymDecrypt)
}
type keyPair struct {
privateKey string
passphrase string
}
var _ = staticMethod(opgp_KeyGen, "(name, email, passphrase)")
func opgp_KeyGen(name, email, passphrase Value) Value {
const rsaBits = 2048
privateKey, err := helper.GenerateKey(ToStr(name), ToStr(email),
[]byte(ToStr(passphrase)), "rsa", rsaBits)
ck(err)
keyRing, err := crypto.NewKeyFromArmoredReader(strings.NewReader(privateKey))
ck(err)
publicKey, err := keyRing.GetArmoredPublicKey()
ck(err)
ob := &SuObject{}
ob.Set(SuStr("public"), SuStr(publicKey))
ob.Set(SuStr("private"), SuStr(privateKey))
return ob
}
var _ = staticMethod(opgp_KeyId, "(key)")
func opgp_KeyId(key Value) Value {
keyOb, err := crypto.NewKeyFromArmored(ToStr(key))
ck(err)
return SuStr(keyOb.GetHexKeyID())
}
var _ = staticMethod(opgp_KeyEntity, "(key)")
func opgp_KeyEntity(key Value) Value {
keyOb, err := crypto.NewKeyFromArmored(ToStr(key))
ck(err)
e := keyOb.GetEntity()
for name := range e.Identities {
return SuStr(name)
}
return False
}
//-------------------------------------------------------------------
type encdec[T any] func(passphrase T, src io.Reader, dst io.Writer)
func opgpStr[T any](key T, source Value, f encdec[T]) Value {
src := strings.NewReader(ToStr(source))
dst := new(bytes.Buffer)
f(key, src, dst)
return SuStr(dst.String())
}
func opgpFile[T any](key T, fromFile, toFile Value, f encdec[T]) Value {
src, err := os.Open(ToStr(fromFile))
ck(err)
defer src.Close()
to := ToStr(toFile)
dst, err := os.CreateTemp(filepath.Dir(to), "su")
ck(err)
defer os.Remove(dst.Name())
defer dst.Close()
f(key, src, dst)
dst.Close()
system.RenameBak(dst.Name(), to)
return nil
}
func symEncrypt(passphrase Value, src io.Reader, dst io.Writer) {
encrypter, err := openpgp.SymmetricallyEncrypt(
dst, []byte(ToStr(passphrase)), nil, nil)
ck(err)
defer encrypter.Close()
_, err = io.Copy(encrypter, src)
ck(err)
}
func symDecrypt(passphrase Value, src io.Reader, dst io.Writer) {
keyRing := func(keys []openpgp.Key, symmetric bool) ([]byte, error) {
return []byte(ToStr(passphrase)), nil
}
md, err := openpgp.ReadMessage(src, nil, keyRing, nil)
ck(err)
_, err = io.Copy(dst, md.UnverifiedBody)
ck(err)
}
func asymEncrypt(publicKey string, src io.Reader, dst io.Writer) {
publicKeyObj, err := crypto.NewKeyFromArmored(publicKey)
ck(err)
publicKeyRing, err := crypto.NewKeyRing(publicKeyObj)
ck(err)
encryptor, err := publicKeyRing.EncryptStreamWithCompression(dst, nil, nil)
ck(err)
defer encryptor.Close()
_, err = io.Copy(encryptor, src)
ck(err)
}
func asymDecrypt(kp keyPair, src io.Reader, dst io.Writer) {
privateKeyObj, err := crypto.NewKeyFromArmored(kp.privateKey)
ck(err)
privateKeyUnlocked, err := privateKeyObj.Unlock([]byte(kp.passphrase))
ck(err)
defer privateKeyUnlocked.ClearPrivateParams()
privateKeyRing, err := crypto.NewKeyRing(privateKeyUnlocked)
ck(err)
decryptor, err := privateKeyRing.DecryptStream(src, nil, 0)
ck(err)
_, err = io.Copy(dst, decryptor)
ck(err)
}
func ck(err error) {
if err != nil {
panic("OpenPGP: " + strings.Replace(err.Error(), "openpgp: ", "", 1))
}
}