forked from neuvector/neuvector
/
license.go
222 lines (186 loc) · 5.84 KB
/
license.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
package licenseinfo
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/sha256"
"encoding/base64"
"encoding/gob"
"errors"
"fmt"
"io"
"math/big"
)
const stdLicensePubKeyString = "BAEg6UBZkrnVy3BRMEUj1owyS/ApFrDt7lW2f2xyxHRPjjWtMfXmO0eaQwXcer46U5ZqHJ13TYhh2yH81Cqc3kLaDAEKraOw8xicvI/n2DtwTwMUzCQnRrycZrIP1NPqCzVAGBbffBdwRErz5fOzDcOve5C9/ZYnxB0fCCxWaTgR7fc0sA=="
// signedLicenseInfo represents a signed license
// signedLicenseInfo struct is shared with the NeuVector controller
// signedLicenseInfo struct MUST not be modified unless coordinated with the implementation in the NeuVector controller because the struct is encoded with gob during license issuing
type signedLicenseInfo struct {
Data []byte
R *big.Int
S *big.Int
}
func encrypt(encryptionKey, text []byte) ([]byte, error) {
block, err := aes.NewCipher(encryptionKey)
if err != nil {
return nil, err
}
ciphertext := make([]byte, aes.BlockSize+len(text))
iv := ciphertext[:aes.BlockSize]
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
return nil, err
}
cfb := cipher.NewCFBEncrypter(block, iv)
cfb.XORKeyStream(ciphertext[aes.BlockSize:], text)
return ciphertext, nil
}
func EncryptToBase64(encryptionKey, text []byte) (string, error) {
if ciphertext, err := encrypt(encryptionKey, text); err == nil {
return base64.StdEncoding.EncodeToString(ciphertext), nil
} else {
return "", err
}
}
func decrypt(encryptionKey, text []byte) ([]byte, error) {
block, err := aes.NewCipher(encryptionKey)
if err != nil {
return nil, err
}
if len(text) < aes.BlockSize {
return nil, errors.New("ciphertext too short")
}
iv := text[:aes.BlockSize]
text = text[aes.BlockSize:]
cfb := cipher.NewCFBDecrypter(block, iv)
cfb.XORKeyStream(text, text)
return text, nil
}
func DecryptFromBase64(encryptionKey []byte, b64 string) (string, error) {
text, err := base64.StdEncoding.DecodeString(b64)
if err != nil {
return "", err
}
if text, err = decrypt(encryptionKey, text); err == nil {
return string(text), nil
} else {
return "", err
}
}
// to verify whether an encrypted signed license code is valid or not. Caller provides the public signing key & encryption key
func VerifyLicenseSignature(licCode string, pubKey *ecdsa.PublicKey, symmetricKey []byte) (string, error) { // returns license json string
var err error
if pubKey == nil || len(symmetricKey) == 0 {
return "", fmt.Errorf("decrypt error!")
}
decrypted, err := DecryptFromBase64(symmetricKey, licCode)
if err != nil || len(decrypted) == 0 {
return "", fmt.Errorf("decrypt error! error=%v", err)
}
l, err := LicenseFromB64String(decrypted)
if err != nil {
return "", fmt.Errorf("get license error! error=%v", err)
}
if ok, err := l.Verify(pubKey); ok {
return string(l.Data), nil
} else {
return "", fmt.Errorf("license invalid! data=%s, error=%v", string(l.Data), err)
}
}
func GetLicenseInfo(license string, symmetricKey []byte) (string, error) { // returns license json string
var err error
var licValue string
var pubKey *ecdsa.PublicKey
pubKey, err = PublicKeyFromB64String(stdLicensePubKeyString, false)
if err == nil {
licValue, err = VerifyLicenseSignature(license, pubKey, symmetricKey)
}
return licValue, err
}
//--------------------------------------------------------------------------------------------------------------
func ObjToBytes(obj interface{}) ([]byte, error) {
var buffBin bytes.Buffer
encoderBin := gob.NewEncoder(&buffBin)
if err := encoderBin.Encode(obj); err != nil {
return nil, err
}
return buffBin.Bytes(), nil
}
func ObjToB64String(obj interface{}) (string, error) {
b, err := ObjToBytes(obj)
if err != nil {
return "", err
}
return base64.StdEncoding.EncodeToString(b), nil
}
func ObjFromBytes(obj interface{}, b []byte) error {
buffBin := bytes.NewBuffer(b)
decoder := gob.NewDecoder(buffBin)
return decoder.Decode(obj)
}
func ObjFromB64String(obj interface{}, s string) error {
b, err := base64.StdEncoding.DecodeString(s)
if err != nil {
return err
}
return ObjFromBytes(obj, b)
}
// LicenseFromB64String returns a signedLicenseInfo from a non-encrypted base64 encoded string.
func LicenseFromB64String(str string) (*signedLicenseInfo, error) {
l := &signedLicenseInfo{}
return l, ObjFromB64String(l, str)
}
//--------------------------------------------------------------------------------------------------------------
// PublicKeyFromBytes returns a public key from a []byte.
func PublicKeyFromBytes(b []byte, metered bool) (*ecdsa.PublicKey, error) {
var curve elliptic.Curve
if metered {
curve = elliptic.P256()
} else {
curve = elliptic.P521()
}
x, y := elliptic.Unmarshal(curve, b)
if x == nil {
return nil, errors.New("Invalid key.")
}
k := ecdsa.PublicKey{
Curve: curve,
X: x,
Y: y,
}
return &k, nil
}
// PublicKeyFromB64String returns a public key from a base64 encoded string.
func PublicKeyFromB64String(str string, metered bool) (*ecdsa.PublicKey, error) {
b, err := base64.StdEncoding.DecodeString(str)
if err != nil {
return nil, err
}
return PublicKeyFromBytes(b, metered)
}
//--------------------------------------------------------------------------------------------------------------
func (l *signedLicenseInfo) hash() ([]byte, error) {
h256 := sha256.New()
if _, err := h256.Write(l.Data); err != nil {
return nil, err
}
return h256.Sum(nil), nil
}
// Verify the signedLicenseInfo with the public key
func (l *signedLicenseInfo) Verify(k *ecdsa.PublicKey) (bool, error) {
h, err := l.hash()
if err != nil {
return false, err
}
return ecdsa.Verify(k, h, l.R, l.S), nil
}
// ToBytes transforms the public key to a base64 []byte.
func (l *signedLicenseInfo) ToBytes() ([]byte, error) {
return ObjToBytes(l)
}
// ToB64String transforms the public key to a base64 string.
func (l *signedLicenseInfo) ToB64String() (string, error) {
return ObjToB64String(l)
}