-
Notifications
You must be signed in to change notification settings - Fork 4
/
sshbox.go
314 lines (275 loc) · 7.36 KB
/
sshbox.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
/*
* sshbox is a utility to encrypt a file using SSH keys.
*/
package main
import (
"crypto/ecdsa"
"crypto/rand"
"crypto/rsa"
"crypto/sha256"
"encoding/asn1"
"encoding/pem"
"flag"
"fmt"
"github.com/cryptobox/gocryptobox/secretbox"
"github.com/gokyle/ecies"
"github.com/gokyle/sshkey"
"io/ioutil"
"os"
"regexp"
)
type boxPackage struct {
LockedKey []byte
Box []byte
}
type messageBox struct {
Message []byte
Signature []byte `asn1:"optional"`
}
type sshPublicKey struct {
Algorithm []byte
Modulus []byte
Exponent []byte
}
var pubkeyRegexp = regexp.MustCompile("(?m)^ssh-... (\\S+).*$")
var remoteCheck = regexp.MustCompile("^https?://")
func main() {
flArmour := flag.Bool("a", false, "ASCII armour the box")
flDecrypt := flag.Bool("d", false, "decrypt file")
flEncrypt := flag.Bool("e", false, "encrypt file")
flKeyFile := flag.String("k", "", "SSH key file")
flSignKey := flag.String("s", "", "SSH private key for signing")
flVerifyKey := flag.String("v", "", "SSH public for signature verification")
flag.Parse()
if *flDecrypt && *flEncrypt {
fmt.Println("[!] only one of -d or -e can be specified!")
os.Exit(1)
}
if *flDecrypt && *flSignKey != "" {
fmt.Println("[!] cannot sign encrypted message.")
os.Exit(1)
} else if *flEncrypt && *flVerifyKey != "" {
fmt.Println("[!] cannot verify plaintext message.")
os.Exit(1)
}
if flag.NArg() != 2 {
fmt.Println("[!] source and target must both be specified.")
fmt.Printf("\t%s [options] source target\n", os.Args[0])
os.Exit(1)
}
source := flag.Args()[0]
target := flag.Args()[1]
if *flKeyFile == "" {
fmt.Println("[!] no key was specified!\n")
os.Exit(1)
}
remote := remoteCheck.MatchString(*flKeyFile)
if remote {
if *flDecrypt {
fmt.Println("[+] remotely fetching private keys is not allowed.")
os.Exit(1)
}
fmt.Println("[+] will fetch key")
}
if *flEncrypt {
err := encrypt(source, target, *flKeyFile, *flSignKey, !remote, *flArmour)
if err != nil {
fmt.Println("[!] failed.")
os.Exit(1)
}
fmt.Println("[+] success")
os.Exit(0)
} else {
err := decrypt(source, target, *flKeyFile, *flVerifyKey, *flArmour)
if err != nil {
fmt.Println("[!] failed.")
os.Exit(1)
}
fmt.Println("[+] success.")
os.Exit(0)
}
}
// Generate a random box key, encrypt the key to the RSA public key,
// package the box appropriately, and write it out to a file.
func encrypt(in, out, keyfile, signkey string, local, armour bool) (err error) {
pub, err := sshkey.LoadPublicKeyFile(keyfile, local)
if err != nil {
fmt.Printf("[!] failed to load the public key:\n\t%s\n",
err.Error())
return
}
switch pub.Type {
case sshkey.KEY_RSA:
err = encryptRSA(in, out, pub.Key.(*rsa.PublicKey), signkey,
local, armour)
case sshkey.KEY_ECDSA:
err = encryptECDSA(in, out, pub.Key.(*ecdsa.PublicKey), signkey,
local, armour)
default:
err = sshkey.ErrInvalidPrivateKey
fmt.Println("[!]", err.Error())
}
return
}
func encryptRSA(in, out string, key *rsa.PublicKey, signkey string, local, armour bool) (err error) {
boxKey, ok := secretbox.GenerateKey()
if !ok {
fmt.Println("[!] failed to generate the box key.")
return
}
hash := sha256.New()
lockedKey, err := rsa.EncryptOAEP(hash, rand.Reader, key, boxKey, nil)
if err != nil {
fmt.Println("[!] RSA encryption failed:", err.Error())
return
}
message, err := ioutil.ReadFile(in)
if err != nil {
fmt.Println("[!]", err.Error())
return
}
box, ok := secretbox.Seal(message, boxKey)
if !ok {
fmt.Println("[!] failed to seal the message.")
err = fmt.Errorf("sealing failure")
return
}
pkg, err := packageBox(lockedKey, box, armour)
if err != nil {
return
}
err = ioutil.WriteFile(out, pkg, 0644)
if err != nil {
fmt.Println("[!]", err.Error())
}
if err != nil {
fmt.Println("[!]", err.Error())
}
return
}
func encryptECDSA(in, out string, key *ecdsa.PublicKey, signkey string, local, armour bool) (err error) {
message, err := ioutil.ReadFile(in)
if err != nil {
fmt.Println("[!]", err.Error())
return
}
eciesKey := ecies.ImportECDSAPublic(key)
eciesKey.Params = ecies.ParamsFromCurve(key.Curve)
box, error := ecies.Encrypt(rand.Reader, eciesKey, message, nil, nil)
if error != nil {
fmt.Println("[!]", err.Error())
return
}
pkg, err := packageBox(nil, box, armour)
if err != nil {
return
}
err = ioutil.WriteFile(out, pkg, 0644)
if err != nil {
fmt.Println("[!]", err.Error())
}
if err != nil {
fmt.Println("[!]", err.Error())
}
return
}
// packageBox actually handles boxing. It can output either PEM-encoded or
// DER-encoded boxes.
func packageBox(lockedKey, box []byte, armour bool) (pkg []byte, err error) {
var pkgBox = boxPackage{lockedKey, box}
pkg, err = asn1.Marshal(pkgBox)
if err != nil {
fmt.Println("[!] couldn't package the box")
return
}
if armour {
var block pem.Block
block.Type = "SSHBOX ENCRYPTED FILE"
block.Bytes = pkg
pkg = pem.EncodeToMemory(&block)
}
return
}
// Decrypt loads the box, recovers the key using the RSA private key, open
// the box, and write the message to a file.
func decrypt(in, out, keyfile, verifykey string, armour bool) (err error) {
key, keytype, err := sshkey.LoadPrivateKeyFile(keyfile)
if err != nil {
fmt.Printf("[!] failed to load the private key:\n\t%s\n",
err.Error())
return
}
switch keytype {
case sshkey.KEY_RSA:
return decryptRSA(in, out, key.(*rsa.PrivateKey), verifykey, armour)
case sshkey.KEY_ECDSA:
return decryptECDSA(in, out, key.(*ecdsa.PrivateKey), verifykey, armour)
default:
err = sshkey.ErrInvalidPublicKey
fmt.Println("[!]", err.Error())
return
}
}
func decryptRSA(in, out string, key *rsa.PrivateKey, verifykey string, armour bool) (err error) {
pkg, err := ioutil.ReadFile(in)
if err != nil {
fmt.Println("[!]", err.Error())
return
}
lockedKey, box, err := unpackageBox(pkg)
if err != nil {
return
}
hash := sha256.New()
boxKey, err := rsa.DecryptOAEP(hash, rand.Reader, key, lockedKey, nil)
if err != nil {
fmt.Println("[!] RSA decryption failed:", err.Error())
return
}
message, ok := secretbox.Open(box, boxKey)
if !ok {
fmt.Println("[!] failed to open box.")
err = fmt.Errorf("opening box failed")
return
}
err = ioutil.WriteFile(out, message, 0644)
return
}
func decryptECDSA(in, out string, key *ecdsa.PrivateKey, verifykey string, armour bool) (err error) {
pkg, err := ioutil.ReadFile(in)
if err != nil {
fmt.Println("[!]", err.Error())
return
}
_, box, err := unpackageBox(pkg)
if err != nil {
return
}
eciesKey := ecies.ImportECDSA(key)
eciesKey.PublicKey.Params = ecies.ParamsFromCurve(key.PublicKey.Curve)
message, err := eciesKey.Decrypt(rand.Reader, box, nil, nil)
if err != nil {
fmt.Println("[!]", err.Error())
return
}
err = ioutil.WriteFile(out, message, 0644)
return
}
// unpackageBox handles the loading of a box; it first attempts to decode the
// box as a DER-encoded box. If this fails, it attempts to decode the box as
// a PEM-encoded box.
func unpackageBox(pkg []byte) (lockedKey, box []byte, err error) {
var pkgStruct boxPackage
_, err = asn1.Unmarshal(pkg, &pkgStruct)
if err == nil {
return pkgStruct.LockedKey, pkgStruct.Box, nil
}
block, _ := pem.Decode(pkg)
if block == nil || block.Type != "SSHBOX ENCRYPTED FILE" {
fmt.Println("[!] invalid box.")
err = fmt.Errorf("invalid box")
return
}
_, err = asn1.Unmarshal(block.Bytes, &pkgStruct)
return pkgStruct.LockedKey, pkgStruct.Box, err
}