-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlicense.go
120 lines (101 loc) · 3.24 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
package main
import (
"crypto"
"crypto/rand"
"crypto/rsa"
"crypto/sha1"
"crypto/x509"
"encoding/base64"
"encoding/json"
"encoding/pem"
"fmt"
"math/big"
"os"
)
var (
privateKey *rsa.PrivateKey
certificate *x509.Certificate
)
type License struct {
Products []Product `json:"products"`
LicenseID string `json:"licenseId"`
LicenseeName string `json:"licenseeName"`
AssigneeName string `json:"assigneeName"`
AssigneeEmail string `json:"assigneeEmail"`
LicenseRestriction string `json:"licenseRestriction"`
Metadata string `json:"metadata"`
Hash string `json:"hash"`
GracePeriodDays int `json:"gracePeriodDays"`
CheckConcurrentUse bool `json:"checkConcurrentUse"`
AutoProlongated bool `json:"autoProlongated"`
IsAutoProlongated bool `json:"isAutoProlongated"`
}
type Product struct {
Code string `json:"code"`
FallbackDate string `json:"fallbackDate"`
PaidUpTo string `json:"paidUpTo"`
Extended bool `json:"extended"`
}
func init() {
loadPrivateKey()
loadCertificate()
}
func loadPrivateKey() (*rsa.PrivateKey, error) {
private, err := os.ReadFile(keyFileName)
if err != nil {
return nil, fmt.Errorf("failed to read private key file (jetbra.key): %v", err)
}
block, _ := pem.Decode(private)
if block == nil {
return nil, fmt.Errorf("failed to decode PEM block from private key file (jetbra.key)")
}
privateKey, err = x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
return nil, fmt.Errorf("failed to parse private key (jetbra.key): %v", err)
}
return privateKey, nil
}
func loadCertificate() (*x509.Certificate, error) {
cert, err := os.ReadFile(certificateFile)
if err != nil {
return nil, fmt.Errorf("failed to read certificate file (jetbra.pem): %v", err)
}
block, _ := pem.Decode(cert)
if block == nil {
return nil, fmt.Errorf("failed to decode PEM block from certificate file (jetbra.pem)")
}
certificate, err = x509.ParseCertificate(block.Bytes)
if err != nil {
return nil, fmt.Errorf("failed to parse certificate (jetbra.pem): %v", err)
}
return certificate, nil
}
func generateLicenseID() string {
const allowedCharacters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
const licenseLength = 16
b := make([]byte, licenseLength)
for i := range b {
index, _ := rand.Int(rand.Reader, big.NewInt(int64(len(allowedCharacters))))
b[i] = allowedCharacters[index.Int64()]
}
return string(b)
}
func GenerateLicense(body string) string {
var license License
err := json.Unmarshal([]byte(body), &license)
if err != nil {
return err.Error()
}
license.LicenseID = generateLicenseID()
licenseStr, _ := json.Marshal(license)
fmt.Println(string(licenseStr))
// Sign the license using SHA1withRSA
hashed := sha1.Sum(licenseStr)
signature, _ := rsa.SignPKCS1v15(rand.Reader, privateKey, crypto.SHA1, hashed[:])
licensePartBase64 := base64.StdEncoding.EncodeToString(licenseStr)
signatureBase64 := base64.StdEncoding.EncodeToString(signature)
certBase64 := base64.StdEncoding.EncodeToString(certificate.Raw)
licenseResult := fmt.Sprintf("%s-%s-%s-%s", license.LicenseID, licensePartBase64, signatureBase64, certBase64)
// fmt.Printf("licenseResult:%s\n", licenseResult)
return licenseResult
}