This repository has been archived by the owner on Nov 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crypto.go
552 lines (486 loc) · 13.7 KB
/
crypto.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
/*
* Copyright 2020 Aletheia Ware LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cryptogo
import (
"bytes"
"crypto"
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"crypto/rsa"
"crypto/sha512"
"crypto/x509"
"encoding/base64"
"encoding/pem"
"fmt"
"github.com/golang/protobuf/proto"
"golang.org/x/crypto/ssh/terminal"
"io"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"path"
"strings"
"syscall"
)
const (
AES_128_KEY_SIZE_BITS = 128
AES_128_KEY_SIZE_BYTES = AES_128_KEY_SIZE_BITS / 8
AES_256_KEY_SIZE_BITS = 256
AES_256_KEY_SIZE_BYTES = AES_256_KEY_SIZE_BITS / 8
MIN_PASSWORD = 12
privateKeyFileExtension = ".go.private"
)
func RandomString(size uint) (string, error) {
buffer := make([]byte, size)
_, err := rand.Read(buffer)
if err != nil {
return "", err
}
return base64.RawURLEncoding.EncodeToString(buffer), nil
}
func Hash(data []byte) []byte {
hash := sha512.Sum512(data)
return hash[:]
}
func HashProtobuf(protobuf proto.Message) ([]byte, error) {
data, err := proto.Marshal(protobuf)
if err != nil {
return nil, err
}
return Hash(data), nil
}
func RSAPublicKeyToPKCS1Bytes(publicKey *rsa.PublicKey) []byte {
return x509.MarshalPKCS1PublicKey(publicKey)
}
func RSAPublicKeyToPKIXBytes(publicKey *rsa.PublicKey) ([]byte, error) {
bytes, err := x509.MarshalPKIXPublicKey(publicKey)
if err != nil {
return nil, err
}
return bytes, nil
}
func RSAPublicKeyFromPKCS1Bytes(data []byte) (*rsa.PublicKey, error) {
pub, err := x509.ParsePKCS1PublicKey(data)
if err != nil {
return nil, err
}
return PublicKeyToRSAPublicKey(pub)
}
func RSAPublicKeyFromPKIXBytes(data []byte) (*rsa.PublicKey, error) {
pub, err := x509.ParsePKIXPublicKey(data)
if err != nil {
return nil, err
}
return PublicKeyToRSAPublicKey(pub)
}
func PublicKeyToRSAPublicKey(key interface{}) (*rsa.PublicKey, error) {
switch k := key.(type) {
case *rsa.PublicKey:
return k, nil
default:
return nil, ErrUnsupportedPublicKeyType{Type: fmt.Sprintf("%v", k)}
}
}
func RSAPublicKeyToPEM(publicKey *rsa.PublicKey) (*pem.Block, error) {
// Marshal public key into PKIX
bytes, err := x509.MarshalPKIXPublicKey(publicKey)
if err != nil {
return nil, err
}
// Create PEM block
return &pem.Block{
Type: "PUBLIC KEY",
Bytes: bytes,
}, nil
}
func RSAPrivateKeyToPKCS1Bytes(privateKey *rsa.PrivateKey) []byte {
return x509.MarshalPKCS1PrivateKey(privateKey)
}
func RSAPrivateKeyToPKCS8Bytes(privateKey *rsa.PrivateKey) ([]byte, error) {
bytes, err := x509.MarshalPKCS8PrivateKey(privateKey)
if err != nil {
return nil, err
}
return bytes, nil
}
func RSAPrivateKeyFromPKCS1Bytes(data []byte) (*rsa.PrivateKey, error) {
priv, err := x509.ParsePKCS1PrivateKey(data)
if err != nil {
return nil, err
}
return PrivateKeyToRSAPrivateKey(priv)
}
func RSAPrivateKeyFromPKCS8Bytes(data []byte) (*rsa.PrivateKey, error) {
priv, err := x509.ParsePKCS8PrivateKey(data)
if err != nil {
return nil, err
}
return PrivateKeyToRSAPrivateKey(priv)
}
func PrivateKeyToRSAPrivateKey(key interface{}) (*rsa.PrivateKey, error) {
switch k := key.(type) {
case *rsa.PrivateKey:
return k, nil
default:
return nil, ErrUnsupportedPrivateKeyType{Type: fmt.Sprintf("%v", k)}
}
}
func RSAPrivateKeyToPEM(privateKey *rsa.PrivateKey, password []byte) (*pem.Block, error) {
// Create encrypted PEM block with private key marshalled into PKCS8
data, err := x509.MarshalPKCS8PrivateKey(privateKey)
if err != nil {
return nil, err
}
return x509.EncryptPEMBlock(rand.Reader, "ENCRYPTED PRIVATE KEY", data, password, x509.PEMCipherAES128)
}
func ParseRSAPublicKey(publicKey []byte, format PublicKeyFormat) (*rsa.PublicKey, error) {
switch format {
case PublicKeyFormat_PKCS1_PUBLIC:
return RSAPublicKeyFromPKCS1Bytes(publicKey)
case PublicKeyFormat_PKIX:
fallthrough
case PublicKeyFormat_X509:
return RSAPublicKeyFromPKIXBytes(publicKey)
case PublicKeyFormat_UNKNOWN_PUBLIC_KEY_FORMAT:
fallthrough
default:
return nil, ErrUnsupportedPublicKeyFormat{Format: format.String()}
}
}
func ParseRSAPrivateKey(privateKey []byte, format PrivateKeyFormat) (*rsa.PrivateKey, error) {
switch format {
case PrivateKeyFormat_PKCS1_PRIVATE:
return RSAPrivateKeyFromPKCS1Bytes(privateKey)
case PrivateKeyFormat_PKCS8:
return RSAPrivateKeyFromPKCS8Bytes(privateKey)
case PrivateKeyFormat_UNKNOWN_PRIVATE_KEY_FORMAT:
fallthrough
default:
return nil, ErrUnsupportedPrivateKeyFormat{Format: format.String()}
}
}
func HasRSAPrivateKey(directory, name string) bool {
_, err := os.Stat(path.Join(directory, name+privateKeyFileExtension))
if err != nil {
if os.IsNotExist(err) {
return false
}
}
return true
}
func ListRSAPrivateKeys(directory string) ([]string, error) {
files, err := ioutil.ReadDir(directory)
if err != nil {
return nil, err
}
var keys []string
for _, f := range files {
name := f.Name()
if strings.HasSuffix(name, privateKeyFileExtension) {
keys = append(keys, strings.TrimSuffix(name, privateKeyFileExtension))
}
}
return keys, nil
}
func CreateRSAPrivateKey(directory, name string, password []byte) (*rsa.PrivateKey, error) {
// Create directory
if err := os.MkdirAll(directory, os.ModePerm); err != nil {
return nil, err
}
log.Println("Generating RSA-4096bit Public/Private Key Pair")
privateKey, err := rsa.GenerateKey(rand.Reader, 4096)
if err != nil {
return nil, err
}
if err := WriteRSAPrivateKey(privateKey, directory, name, password); err != nil {
return nil, err
}
return privateKey, nil
}
func WriteRSAPrivateKey(privateKey *rsa.PrivateKey, directory, name string, password []byte) error {
// Encode Private Key to PEM block
privateKeyPEM, err := RSAPrivateKeyToPEM(privateKey, password)
if err != nil {
return err
}
// Write Private Key PEM block to file
if err := WritePEM(privateKeyPEM, path.Join(directory, name+privateKeyFileExtension)); err != nil {
return err
}
return nil
}
func RSAPrivateKey(directory, name string, password []byte) (*rsa.PrivateKey, error) {
privateKeyPEM, err := ReadPEM(path.Join(directory, name+privateKeyFileExtension))
if err != nil {
return nil, err
}
decrypted, err := x509.DecryptPEMBlock(privateKeyPEM, password)
if err != nil {
return nil, err
}
priv, err := x509.ParsePKCS8PrivateKey(decrypted)
if err != nil {
return nil, err
}
return PrivateKeyToRSAPrivateKey(priv)
}
func Password() ([]byte, error) {
pwd, ok := os.LookupEnv("PASSWORD")
if ok {
return []byte(pwd), nil
} else {
return ReadPassword("Enter password: ")
}
}
func ReadPassword(prompt string) ([]byte, error) {
log.Print(prompt)
password, err := terminal.ReadPassword(int(syscall.Stdin))
if err != nil {
return nil, err
}
log.Println()
return password, nil
}
func LoadRSAPrivateKey(directory, name string) (*rsa.PrivateKey, error) {
log.Println("Alias:", name)
password, err := Password()
if err != nil {
return nil, err
}
if HasRSAPrivateKey(directory, name) {
key, err := RSAPrivateKey(directory, name, password)
if err != nil {
return nil, err
}
return key, nil
} else {
log.Println("Creating key in " + directory + " for " + name)
confirm, err := ReadPassword("Confirm password: ")
if err != nil {
return nil, err
}
if !bytes.Equal(password, confirm) {
return nil, ErrPasswordsDoNotMatch{}
}
key, err := CreateRSAPrivateKey(directory, name, password)
if err != nil {
return nil, err
}
log.Println("Successfully Created Key Pair")
return key, nil
}
}
func ExportKeys(host, keystore, name string, password []byte) (string, error) {
privateKey, err := RSAPrivateKey(keystore, name, password)
if err != nil {
return "", err
}
// Generate a random access code
accessCode, err := GenerateRandomKey(AES_128_KEY_SIZE_BYTES)
if err != nil {
return "", err
}
data, err := x509.MarshalPKCS8PrivateKey(privateKey)
if err != nil {
return "", err
}
encryptedPrivateKeyBytes, err := EncryptAESGCM(accessCode, data)
if err != nil {
return "", err
}
publicKeyBytes, err := RSAPublicKeyToPKIXBytes(&privateKey.PublicKey)
if err != nil {
return "", err
}
encryptedPassword, err := EncryptAESGCM(accessCode, password)
if err != nil {
return "", err
}
response, err := http.PostForm(host+"/keys", url.Values{
"name": {name},
"publicKey": {base64.RawURLEncoding.EncodeToString(publicKeyBytes)},
"publicKeyFormat": {"PKIX"},
"privateKey": {base64.RawURLEncoding.EncodeToString(encryptedPrivateKeyBytes)},
"privateKeyFormat": {"PKCS8"},
"password": {base64.RawURLEncoding.EncodeToString(encryptedPassword)},
})
if err != nil {
return "", err
}
switch response.StatusCode {
case http.StatusOK:
log.Println("Keys exported")
return base64.RawURLEncoding.EncodeToString(accessCode), nil
default:
return "", ErrExportFailed{StatusCode: response.StatusCode, Status: response.Status}
}
}
func ImportKeys(host, keystore, name, accessCode string) error {
response, err := http.Get(host + "/keys?name=" + name)
if err != nil {
return err
}
data, err := ioutil.ReadAll(response.Body)
if err != nil {
return err
}
if len(data) <= 0 {
return fmt.Errorf("Could not get KeyShare for %s", name)
}
keyShare := &KeyShare{}
if err = proto.Unmarshal(data, keyShare); err != nil {
return err
}
if name != keyShare.Name {
return fmt.Errorf("Incorrect KeyShare Name: %s vs %s", name, keyShare.Name)
}
// Decode Access Code
decodedAccessCode, err := base64.RawURLEncoding.DecodeString(accessCode)
if err != nil {
return err
}
// Decrypt Private Key
decryptedPrivateKey, err := DecryptAESGCM(decodedAccessCode, keyShare.PrivateKey)
if err != nil {
return err
}
// Parse Private Key
privateKey, err := ParseRSAPrivateKey(decryptedPrivateKey, keyShare.PrivateFormat)
if err != nil {
return err
}
// Decrypt Password
decryptedPassword, err := DecryptAESGCM(decodedAccessCode, keyShare.Password)
if err != nil {
return err
}
// Write Private Key
if err := WriteRSAPrivateKey(privateKey, keystore, name, decryptedPassword); err != nil {
return err
}
log.Println("Keys imported")
return nil
}
func DecryptKey(algorithm EncryptionAlgorithm, secret []byte, key *rsa.PrivateKey) ([]byte, error) {
switch algorithm {
case EncryptionAlgorithm_RSA_ECB_OAEPPADDING:
// Decrypt a shared key
return rsa.DecryptOAEP(sha512.New(), rand.Reader, key, secret, nil)
case EncryptionAlgorithm_UNKNOWN_ENCRYPTION:
return secret, nil
default:
return nil, ErrUnsupportedEncryption{Algorithm: algorithm.String()}
}
}
func DecryptPayload(algorithm EncryptionAlgorithm, key []byte, payload []byte) ([]byte, error) {
switch algorithm {
case EncryptionAlgorithm_AES_128_GCM_NOPADDING:
fallthrough
case EncryptionAlgorithm_AES_256_GCM_NOPADDING:
return DecryptAESGCM(key, payload)
case EncryptionAlgorithm_UNKNOWN_ENCRYPTION:
return payload, nil
default:
return nil, ErrUnsupportedEncryption{Algorithm: algorithm.String()}
}
}
func GenerateRandomKey(bytes int) ([]byte, error) {
key := make([]byte, bytes)
if _, err := io.ReadFull(rand.Reader, key); err != nil {
return nil, err
}
return key, nil
}
func EncryptAESGCM(key, payload []byte) ([]byte, error) {
// Create cipher
c, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
// Create galois counter mode
gcm, err := cipher.NewGCM(c)
if err != nil {
return nil, err
}
// Generate nonce
nonce := make([]byte, gcm.NonceSize())
if _, err := io.ReadFull(rand.Reader, nonce); err != nil {
return nil, err
}
// Encrypt payload
encrypted := append(nonce, gcm.Seal(nil, nonce, payload, nil)...)
return encrypted, nil
}
func DecryptAESGCM(key, encrypted []byte) ([]byte, error) {
// Create cipher
c, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
// Create galois counter mode
gcm, err := cipher.NewGCM(c)
if err != nil {
return nil, err
}
// Get nonce
nonce := encrypted[:gcm.NonceSize()]
// Get payload
payload := encrypted[gcm.NonceSize():]
// Decrypt payload
return gcm.Open(nil, nonce, payload, nil)
}
func CreateSignature(privateKey *rsa.PrivateKey, data []byte, algorithm SignatureAlgorithm) ([]byte, error) {
switch algorithm {
case SignatureAlgorithm_SHA512WITHRSA:
return rsa.SignPKCS1v15(rand.Reader, privateKey, crypto.SHA512, data)
case SignatureAlgorithm_SHA512WITHRSA_PSS:
var options rsa.PSSOptions
options.SaltLength = rsa.PSSSaltLengthAuto
return rsa.SignPSS(rand.Reader, privateKey, crypto.SHA512, data, &options)
case SignatureAlgorithm_UNKNOWN_SIGNATURE:
fallthrough
default:
return nil, ErrUnsupportedSignature{Algorithm: algorithm.String()}
}
}
func VerifySignature(publicKey *rsa.PublicKey, data, signature []byte, algorithm SignatureAlgorithm) error {
switch algorithm {
case SignatureAlgorithm_SHA512WITHRSA:
return rsa.VerifyPKCS1v15(publicKey, crypto.SHA512, data, signature)
case SignatureAlgorithm_SHA512WITHRSA_PSS:
var options rsa.PSSOptions
options.SaltLength = rsa.PSSSaltLengthAuto
return rsa.VerifyPSS(publicKey, crypto.SHA512, data, signature, &options)
case SignatureAlgorithm_UNKNOWN_SIGNATURE:
fallthrough
default:
return ErrUnsupportedSignature{Algorithm: algorithm.String()}
}
}
func ReadPEM(filename string) (*pem.Block, error) {
data, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
block, _ := pem.Decode(data)
return block, nil
}
func WritePEM(key *pem.Block, filename string) error {
return ioutil.WriteFile(filename, pem.EncodeToMemory(key), os.ModePerm)
}