/
crypto.go
149 lines (120 loc) · 3.5 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
package yoti
import (
"crypto"
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"crypto/rsa"
"crypto/sha256"
"crypto/x509"
"encoding/pem"
"errors"
"fmt"
)
func loadRsaKey(keyBytes []byte) (*rsa.PrivateKey, error) {
// Extract the PEM-encoded data
block, _ := pem.Decode(keyBytes)
if block == nil {
return nil, errors.New("not PEM-encoded")
}
if block.Type != "RSA PRIVATE KEY" {
return nil, errors.New("not RSA private key")
}
key, err := x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
return nil, errors.New("bad RSA private key")
}
return key, nil
}
func decryptRsa(cipherBytes []byte, key *rsa.PrivateKey) ([]byte, error) {
return rsa.DecryptPKCS1v15(rand.Reader, key, cipherBytes)
}
func decipherAes(key, iv, cipherBytes []byte) ([]byte, error) {
block, err := aes.NewCipher(key)
if err != nil {
return []byte{}, err
}
// CBC mode always works in whole blocks.
if (len(cipherBytes) % aes.BlockSize) != 0 {
return []byte{}, errors.New("ciphertext is not a multiple of the block size")
}
mode := cipher.NewCBCDecrypter(block, iv)
decipheredBytes := make([]byte, len(cipherBytes))
mode.CryptBlocks(decipheredBytes, cipherBytes)
return pkcs7Unpad(decipheredBytes, aes.BlockSize)
}
func pkcs7Unpad(ciphertext []byte, blocksize int) (result []byte, err error) {
if blocksize <= 0 {
err = fmt.Errorf("blocksize %d is not valid for padding removal", blocksize)
return
}
if len(ciphertext) == 0 {
err = errors.New("Cannot remove padding on empty byte array")
return
}
if len(ciphertext)%blocksize != 0 {
err = errors.New("ciphertext is not a multiple of the block size")
return
}
c := ciphertext[len(ciphertext)-1]
n := int(c)
if n == 0 || n > len(ciphertext) {
err = errors.New("ciphertext is not padded with PKCS#7 padding")
return
}
// verify all padding bytes are correct
for i := 0; i < n; i++ {
if ciphertext[len(ciphertext)-n+i] != c {
err = errors.New("ciphertext is not padded with PKCS#7 padding")
return
}
}
return ciphertext[:len(ciphertext)-n], nil
}
func signDigest(digest []byte, key *rsa.PrivateKey) ([]byte, error) {
hashed := sha256.Sum256(digest)
signedDigest, err := rsa.SignPKCS1v15(rand.Reader, key, crypto.SHA256, hashed[:])
if err != nil {
return []byte{}, err
}
return signedDigest, nil
}
func getAuthKey(key *rsa.PrivateKey) (string, error) {
return getDerEncodedPublicKey(key)
}
func getDerEncodedPublicKey(key *rsa.PrivateKey) (result string, err error) {
var derEncodedBytes []byte
if derEncodedBytes, err = x509.MarshalPKIXPublicKey(key.Public()); err != nil {
return
}
result = bytesToBase64(derEncodedBytes)
return
}
func generateNonce() (string, error) {
b := make([]byte, 16)
_, err := rand.Read(b)
if err != nil {
return "", err
}
uuid := fmt.Sprintf("%X-%X-%X-%X-%X", b[0:4], b[4:6], b[6:8], b[8:10], b[10:])
return uuid, nil
}
func decryptToken(encryptedConnectToken string, key *rsa.PrivateKey) (result string, err error) {
// token was encoded as a urlsafe base64 so it can be transfered in a url
var cipherBytes []byte
if cipherBytes, err = urlSafeBase64ToBytes(encryptedConnectToken); err != nil {
return "", err
}
var decipheredBytes []byte
if decipheredBytes, err = decryptRsa(cipherBytes, key); err != nil {
return "", err
}
return bytesToUtf8(decipheredBytes), nil
}
func unwrapKey(wrappedKey string, key *rsa.PrivateKey) (result []byte, err error) {
var cipherBytes []byte
if cipherBytes, err = base64ToBytes(wrappedKey); err != nil {
return nil, err
}
return decryptRsa(cipherBytes, key)
}