-
Notifications
You must be signed in to change notification settings - Fork 1
/
licensefile.go
378 lines (334 loc) · 13 KB
/
licensefile.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
package licensefile
import (
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
"encoding/base64"
"errors"
"fmt"
"io"
"math"
"os"
"strings"
"time"
"golang.org/x/exp/slices"
)
// Errors when validating a license key file.
var (
// ErrBadSignature is returned from Verify() or Verify...() when a File's Signature
// cannot be verified with the given public key.
ErrBadSignature = errors.New("signature invalid")
// ErrMissingExpireDate is returned when trying to check if a license is expires
// or in how long it expires via the Expired() or ExpiresIn() funcs. This error
//should really never be returned since the only time these funcs are used are
//with an existing license's data.
ErrMissingExpireDate = errors.New("missing expire date")
)
// File defines the format of data stored in a license key file. This is the body of
// the text file.
//
// Struct tags are needed for YAML since otherwise when marshalling the field names
// will be converted to lowercase. We want to maintain camel case since that matches
// the format used when marshalling to JSON.
//
// We use a struct with a map, instead of just map, so that we can more easily interact
// with common fields and store some non-marshalled license data. More simply, having
// a struct is just nicer for interacting with.
type File struct {
//Optionally displayed fields per app. These are at the top of the struct
//definition so that they will be displayed at the top of the marshalled data just
//for ease of human reading of the license key file.
LicenseID int64 `json:"LicenseID,omitempty" yaml:"LicenseID,omitempty"`
AppName string `json:"AppName,omitempty" yaml:"AppName,omitempty"`
//This data copied from db-license.go and always included in each license key file.
CompanyName string `yaml:"CompanyName"`
ContactName string `yaml:"ContactName"`
PhoneNumber string `yaml:"PhoneNumber"`
Email string `yaml:"Email"`
IssueDate string `yaml:"IssueDate"` //YYYY-MM-DD
IssueTimestamp int64 `yaml:"IssueTimestamp"` //unix timestamp in seconds
ExpireDate string `yaml:"ExpireDate"` //YYYY-MM-DD, in UTC timezone for easiest comparison in DaysUntilExpired()
//The name and value for each custom field result. This is stored as a key
//value pair and we use an interface since custom fields can have many types and
//this is just easier.
Extras map[string]interface{} `json:"Extras,omitempty" yaml:"Extras,omitempty"`
//Signature is the result of signing the hash of File (all of the above fields)
//using the private key. The result is stored here and File is output to a text
//file known as the complete license key file. This file is distributed to and
//imported into your app by the end-user to allow the app's use.
Signature string `yaml:"Signature"`
//Stuff used for signing or verifying a license file. These are never included in
//the license key file that is distributed.
//
//During verification, these fields are populated just for debugging.
fileFormat FileFormat //the format a file was unmarshaled from.
readFromPath string //path a license file was read from.
publicKey []byte //the public key used to verify a license.
keyPairAlgo KeyPairAlgoType //algorithm type of the public key.
}
// GenerateKeyPair creates and returns a new private and public key.
func GenerateKeyPair(k KeyPairAlgoType) (private, public []byte, err error) {
//Make sure a valid key pair type was provided.
if !slices.Contains(keyPairAlgoTypes, k) {
err = fmt.Errorf("invalid key pair type, should be one of '%s', got '%s'", keyPairAlgoTypes, k)
return
}
//Generate the key pair.
switch k {
case KeyPairAlgoECDSAP256, KeyPairAlgoECDSAP384, KeyPairAlgoECDSAP521:
private, public, err = GenerateKeyPairECDSA(k)
case KeyPairAlgoRSA2048, KeyPairAlgoRSA4096:
private, public, err = GenerateKeyPairRSA(k)
case KeyPairAlgoED25519:
private, public, err = GenerateKeyPairED25519()
}
return
}
//***********************************************************************************
//The below funcs are used when generating a license key file.
// Sign creates a signature for a license file. The signature is set in the provided
// File's Signature field. The private key must be decrypted, if needed, prior to
// being provided. The signature will be encoded per the File's EncodingType.
func (f *File) Sign(privateKey []byte, keyPairAlgo KeyPairAlgoType) (err error) {
err = keyPairAlgo.Valid()
if err != nil {
return
}
switch keyPairAlgo {
case KeyPairAlgoECDSAP256, KeyPairAlgoECDSAP384, KeyPairAlgoECDSAP521:
err = f.SignECDSA(privateKey, keyPairAlgo)
case KeyPairAlgoRSA2048, KeyPairAlgoRSA4096:
err = f.SignRSA(privateKey, keyPairAlgo)
case KeyPairAlgoED25519:
err = f.SignED25519(privateKey)
}
return
}
// hash generates a checksum of the marshalled File's data per the key pair algorithm
// that will be used to sign the hash. This is used as part of the signing process
// since we sign a hash, not the underlying File. This is also used when verifying
// the license key file since we compare the hash against the signature with a public
// key.
func (f *File) hash(keyPairAlgo KeyPairAlgoType) (hash []byte, err error) {
//Make sure the Signature field is blank prior hashing since if the Signature
//field is present, it will add a source of randomness and will be replaced
//anyway by the signature generated within this func.
f.Signature = ""
//Encode the struct as bytes per the File's FileFormat. We reuse the FileFormat
//here since if a third-party app is validating a license file, it already has
//support for the provided FileFormat (to decode the file's data into a File
//struct) and reusing the same format for marshalling before hashing just makes
//sense.
//
//We tried marshalling with gob.NewEncoder() and Encode() but this doesn't
//ignore non-exported struct fields nor fields we don't want included in the
//license file (i.e.: fields with `json:"-"`).
b, err := f.Marshal()
if err != nil {
err = errors.New(err.Error() + " file format required to marshal data before hashing")
return
}
//Calculate the hash. The hash algorithm is determined by the key pair algorithm.
err = keyPairAlgo.Valid()
if err != nil {
return
}
//Use the correct hash algorithm per the key pair algorithm.
//https://www.rfc-editor.org/rfc/rfc5656#section-6.2.1
//Default to SHA1 for RSA.
//Default to SHA512 for ED25519.
switch keyPairAlgo {
case KeyPairAlgoECDSAP256:
h := sha256.Sum256(b)
hash = []byte(h[:])
case KeyPairAlgoECDSAP384:
h := sha512.Sum384(b)
hash = []byte(h[:])
case KeyPairAlgoECDSAP521:
h := sha512.Sum512(b)
hash = []byte(h[:])
case KeyPairAlgoRSA2048:
h := sha1.Sum(b)
hash = []byte(h[:])
case KeyPairAlgoRSA4096:
h := sha1.Sum(b)
hash = []byte(h[:])
case KeyPairAlgoED25519:
h := sha512.Sum512(b)
hash = []byte(h[:])
}
return
}
// encodeSignature returns the generated signature encoded as a string. The returned
// value is the signature that will be set in the File's Signature field.
//
// base64 is used because it will generate shorter signatures than base32 or hex.
func (f *File) encodeSignature(b []byte) {
f.Signature = base64.StdEncoding.EncodeToString(b)
}
// Write writes a File to out. This is used to output the complete license key file.
// This can be used to write the File to a buffer, as is done when creating a license
// key file, write the File back to the browser as html, or write the File to an actual
// filesystem file.
//
// For use with a buffer:
//
// //b := bytes.Buffer{}
// //err := f.Write(&b)
//
// Writing to an http.ResponseWriter:
//
// //func handler(w http.ResponseWriter, r *http.Request) {
// // //...
// // err := f.Write(w)
// //}
func (f *File) Write(out io.Writer) (err error) {
//Marshal to bytes.
b, err := f.Marshal()
if err != nil {
return
}
//Write.
_, err = out.Write(b)
return
}
//***********************************************************************************
//The below funcs are used when using a license key file in a third-party app.
// Read reads a license key file from the given path, unmarshals it, and returns it's
// data as a File. This checks if the file exists and the data is of the correct
// format.
//
// This DOES NOT check if the license key file itself (the contents of the file and
// the signature) is valid nor does this check if the license is expired. You should
// call VerifySignature() and Expired() on the returned File immediately after calling
// this func.
func Read(path string, format FileFormat) (f File, err error) {
//Check if a file exists at the provided path.
_, err = os.Stat(path)
if os.IsNotExist(err) {
return
}
//Read the file at the provided path.
contents, err := os.ReadFile(path)
if err != nil {
return
}
//Unmarshal the file's contents. Upon success, this will set the File's FileFormat
//field accordingly.
f, err = Unmarshal(contents, format)
//If unmarshalling was successful, save the path to the license file since we know
//the file exists.
if err == nil {
f.readFromPath = path
} else {
f.readFromPath = "unknown"
}
return
}
// decodeSignature returns the File's Signature field as a []byte for use when
// verifying the license key file with a public key.
//
// base64 is used because it will generate shorter signatures than base32 or hex.
func (f *File) decodeSignature() (b []byte, err error) {
b, err = base64.StdEncoding.DecodeString(f.Signature)
return
}
// VerifySignature checks if a File's signature is valid by checking it against the
// publicKey.
//
// This DOES NOT check if a File is expired. You should call Expired() on the File
// after calling this func.
//
// Signature verification and expiration date checking were kept separate on purpose
// so that each step can be handled more deliberately with specific handling of
// invalid states (i.e.: for more graceful handling).
func (f *File) VerifySignature(publicKey []byte, keyPairAlgo KeyPairAlgoType) (err error) {
//Make sure a valid algo type was provided.
err = keyPairAlgo.Valid()
if err != nil {
return
}
//Handle verification of signature based on algo type.
switch keyPairAlgo {
case KeyPairAlgoECDSAP256, KeyPairAlgoECDSAP384, KeyPairAlgoECDSAP521:
err = f.VerifySignatureECDSA(publicKey, keyPairAlgo)
case KeyPairAlgoRSA2048, KeyPairAlgoRSA4096:
err = f.VerifySignatureRSA(publicKey, keyPairAlgo)
case KeyPairAlgoED25519:
err = f.VerifySignatureED25519(publicKey)
}
//If verification was successful, save the public key info to the license file
//since we know the information was correct.
if err == nil {
f.publicKey = publicKey
f.keyPairAlgo = keyPairAlgo
}
return
}
// Verify calls VerifySignature().
//
// Deprecated: This func is here just for legacy situations since the old Verify()
// func was renamed to VerifySignature() for better clarity. Use VerifySignature()
// instead.
func (f *File) Verify(publicKey []byte, keyPairAlgo KeyPairAlgoType) (err error) {
return f.VerifySignature(publicKey, keyPairAlgo)
}
// Expired returns if a lincense File's expiration date is in the past.
//
// You should only call this AFTER calling VerifySignature() otherwise the expiration
// date in the File is untrustworthy and could have been modified.
//
// Signature verification and expiration date checking were kept separate on purpose
// so that each step can be handled more deliberately with specific handling of
// invalid states (i.e.: for more graceful handling).
func (f *File) Expired() (yes bool, err error) {
//Make sure a expiration data is provided. It should always be provided since
//you would call this func after reading a license file and verifying it's
//signature.
if strings.TrimSpace(f.ExpireDate) == "" {
return false, ErrMissingExpireDate
}
//Check if license is expired.
expDate, err := time.Parse("2006-01-02", f.ExpireDate)
if err != nil {
return
}
yes = expDate.Before(time.Now())
return
}
// ExpiresIn calculates duration until a license File expires. The returned duration
// will be negative for an expired license.
//
// You should only call this AFTER calling VerifySignature() otherwise the expiration
// date in the File is untrustworthy and could have been modified.
func (f *File) ExpiresIn() (d time.Duration, err error) {
//Make sure a expiration data is provided. It should always be provided since
//you would call this func after reading a license file and verifying it's
//signature.
if strings.TrimSpace(f.ExpireDate) == "" {
return 0, ErrMissingExpireDate
}
//Get duration until license is expired.
expDate, err := time.Parse("2006-01-02", f.ExpireDate)
if err != nil {
return
}
d = time.Until(expDate)
return
}
// ExpiresInDays is a wrapper around ExpiresIn that returns the number of days a
// license File expires in. The returned days will be negative for an expired
// license.
//
// You should only call this AFTER calling VerifySignature() otherwise the expiration
// date in the File is untrustworthy and could have been modified.
func (f *File) ExpiresInDays() (days int, err error) {
//Get duration license will expire in.
dur, err := f.ExpiresIn()
if err != nil {
return
}
//Convert to days.
days = int(math.Floor(dur.Hours() / 24))
return
}