forked from unidoc/unioffice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
license.go
185 lines (164 loc) · 4.57 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
// Copyright 2017 Baliance. All rights reserved.
//
// Use of this source code is governed by the terms of the Affero GNU General
// Public License version 3.0 as published by the Free Software Foundation and
// appearing in the file LICENSE included in the packaging of this file. A
// commercial license can be purchased by contacting [email protected].
package gooxml
import (
"bytes"
"compress/gzip"
"crypto"
"crypto/rand"
"crypto/rsa"
"crypto/sha256"
"crypto/x509"
"encoding/base64"
"encoding/hex"
"encoding/json"
"errors"
"io"
"log"
"strings"
"time"
)
// OpenSourceLicense is the AGPL open source license. It is installed by default
// if no license has previously been installed.
const OpenSourceLicense = `
H4sIAAAAAAAC_xzJsU7DQAwA0J2vqDynks--y9nZ
2RAMLQubfeegDKRR2kogxL8jkN72vuHZPgImeNli
PZwu973F4fUaOwxwWt5Xu933v86Fqjfnlq1mKWzU
SXzEwCKoqWc0sREtvCYlsdlV2b12CwqyntVk7orZ
eTQRtMhMoonHaDh3DMsYVEir10JzbV3CW1FmyrM4
wQCPn9uy2225rDABI-Ix0ZHTGXH69wYDPC0t1muc
v7aAKf08_AYAAP__z2E3oN8A`
var license *License
const pubKeyHex = `305c300d06092a864886f70d0101010500034b003048024100b87eafb6c07499eb97cc9d3565ecf3168196301907c841addc665086bb3ed8eb12d9da26cafa96450146da8bd0ccf155fcacc686955ef0302fa44aa3ec89417b0203010001`
var pubKey *rsa.PublicKey
func init() {
pubKeyBytes, err := hex.DecodeString(pubKeyHex)
if err != nil {
log.Fatalf("error reading key: %s", err)
}
pkRaw, err := x509.ParsePKIXPublicKey(pubKeyBytes)
if err != nil {
log.Fatalf("error reading key: %s", err)
}
pubKey = pkRaw.(*rsa.PublicKey)
}
// LicenseType is the type of license
//go:generate stringer -type=LicenseType
type LicenseType byte
// LicenseType constants
const (
LicenseTypeInvalid LicenseType = iota
LicenseTypeAGPL
LicenseTypeCommercial
)
// License holds the gooxml license information.
type License struct {
Name string
Signature string `json:",omitempty"`
Expiration time.Time
LicenseType LicenseType
}
// Sign signs a license with a private key, setting the license's signature
// value
func (l *License) Sign(privKey *rsa.PrivateKey) error {
l.Signature = ""
buf := bytes.Buffer{}
enc := json.NewEncoder(&buf)
if err := enc.Encode(l); err != nil {
return err
}
hashed := sha256.Sum256(buf.Bytes())
signature, err := rsa.SignPKCS1v15(rand.Reader, privKey, crypto.SHA256, hashed[:])
if err != nil {
return err
}
l.Signature = hex.EncodeToString(signature)
return nil
}
// Verify verifies a license by checking the license content and signature
// against a public key.
func (l License) Verify(pubKey *rsa.PublicKey) error {
cp := l
cp.Signature = ""
buf := bytes.Buffer{}
enc := json.NewEncoder(&buf)
if err := enc.Encode(cp); err != nil {
return err
}
sig, err := hex.DecodeString(l.Signature)
if err != nil {
return err
}
hashed := sha256.Sum256(buf.Bytes())
err = rsa.VerifyPKCS1v15(pubKey, crypto.SHA256, hashed[:], sig)
return err
}
func (l License) String() string {
buf := bytes.Buffer{}
enc := json.NewEncoder(&buf)
enc.Encode(l)
return buf.String()
}
// Encoded returns a base64 encoded version of the license for use with
// InstallLicense.
func (l License) Encoded() string {
buf := bytes.Buffer{}
w := base64.NewEncoder(base64.RawURLEncoding, &buf)
gz, _ := gzip.NewWriterLevel(w, gzip.BestCompression)
enc := json.NewEncoder(gz)
enc.Encode(l)
gz.Close()
rsp := bytes.Buffer{}
const maxLen = 40
raw := buf.Bytes()
for i := 0; i < buf.Len(); i += maxLen {
rsp.Write(raw[i : i+maxLen])
rsp.WriteByte('\r')
rsp.WriteByte('\n')
}
return rsp.String()
}
// InstallLicense installs a license, returning an error if the license is
// invalid or expired. Expiration checks the ReleaseDate variable in version.go.
func InstallLicense(s string) error {
s = strings.Replace(s, "\r", "", -1)
s = strings.Replace(s, "\n", "", -1)
var r io.Reader
r = strings.NewReader(s)
r = base64.NewDecoder(base64.RawURLEncoding, r)
r, err := gzip.NewReader(r)
if err != nil {
return err
}
dec := json.NewDecoder(r)
l := &License{}
if err := dec.Decode(l); err != nil {
return err
}
// check signature
if err := l.Verify(pubKey); err != nil {
return errors.New("license validatin error")
}
if l.Expiration.Before(ReleaseDate) {
return errors.New("license expired")
}
license = l
return nil
}
// GetLicense returns the current license. This can be used by commercial
// customers to assist in ensuring that their license hasn't expired.
func GetLicense() License {
if license == nil {
if err := InstallLicense(OpenSourceLicense); err != nil {
log.Printf("open source license error: %s", err)
}
}
if license != nil {
return *license
}
return License{}
}