-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
machine_file.go
178 lines (145 loc) · 4.8 KB
/
machine_file.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
package keygen
import (
"encoding/base64"
"encoding/json"
"strings"
"time"
"github.com/keygen-sh/jsonapi-go"
)
// MachineFile represents a Keygen license file.
type MachineFile struct {
ID string `json:"-"`
Type string `json:"-"`
Certificate string `json:"certificate"`
Issued time.Time `json:"issued"`
Expiry time.Time `json:"expiry"`
TTL int `json:"ttl"`
MachineID string `json:"-"`
LicenseID string `json:"-"`
}
// SetID implements the jsonapi.UnmarshalResourceIdentifier interface.
func (lic *MachineFile) SetID(id string) error {
lic.ID = id
return nil
}
// SetType implements the jsonapi.UnmarshalResourceIdentifier interface.
func (lic *MachineFile) SetType(t string) error {
lic.Type = t
return nil
}
// SetData implements the jsonapi.UnmarshalData interface.
func (lic *MachineFile) SetData(to func(target interface{}) error) error {
return to(lic)
}
// SetRelationships implements the jsonapi.UnmarshalRelationship interface.
func (lic *MachineFile) SetRelationships(relationships map[string]interface{}) error {
if relationship, ok := relationships["machine"]; ok {
lic.MachineID = relationship.(*jsonapi.ResourceObjectIdentifier).ID
}
if relationship, ok := relationships["license"]; ok {
lic.LicenseID = relationship.(*jsonapi.ResourceObjectIdentifier).ID
}
return nil
}
// Decrypt verifies the machine file's signature. It returns any errors
// that occurred during verification, e.g. ErrMachineFileInvalid.
func (lic *MachineFile) Verify() error {
verifier := &verifier{PublicKey: PublicKey}
if err := verifier.VerifyMachineFile(lic); err != nil {
return &MachineFileError{err}
}
return nil
}
// Decrypt decrypts the machine file's encrypted dataset. It returns the decrypted dataset
// and any errors that occurred during decryption, e.g. ErrMachineFileNotEncrypted.
func (lic *MachineFile) Decrypt(key string) (*MachineFileDataset, error) {
cert, err := lic.certificate()
if err != nil {
return nil, err
}
switch {
case cert.Alg == "aes-256-gcm+rsa-pss-sha256" || cert.Alg == "aes-256-gcm+rsa-sha256":
return nil, ErrMachineFileNotSupported
case cert.Alg != "aes-256-gcm+ed25519":
return nil, ErrMachineFileNotEncrypted
}
// Decrypt
decryptor := &decryptor{key}
data, err := decryptor.DecryptCertificate(cert)
if err != nil {
return nil, &MachineFileError{err}
}
// Unmarshal
dataset := &MachineFileDataset{}
if _, err := jsonapi.Unmarshal(data, dataset); err != nil {
return nil, &MachineFileError{err}
}
if MaxClockDrift >= 0 && time.Until(dataset.Issued) > MaxClockDrift {
return dataset, ErrSystemClockUnsynced
}
if dataset.TTL != 0 && time.Now().After(dataset.Expiry) {
return dataset, ErrMachineFileExpired
}
return dataset, nil
}
func (lic *MachineFile) certificate() (*certificate, error) {
payload := lic.Certificate
// Remove header and footer
payload = strings.TrimPrefix(payload, "-----BEGIN MACHINE FILE-----\n")
payload = strings.TrimSuffix(payload, "-----END MACHINE FILE-----\n")
// Decode
dec, err := base64.StdEncoding.DecodeString(payload)
if err != nil {
return nil, &MachineFileError{err}
}
// Unmarshal
var cert *certificate
if err := json.Unmarshal(dec, &cert); err != nil {
return nil, &MachineFileError{err}
}
return cert, nil
}
// MachineFileDataset represents a decrypted machine file object.
type MachineFileDataset struct {
Machine Machine `json:"-"`
License License `json:"-"`
Entitlements Entitlements `json:"-"`
Components Components `json:"-"`
Issued time.Time `json:"issued"`
Expiry time.Time `json:"expiry"`
TTL int `json:"ttl"`
}
// SetData implements the jsonapi.UnmarshalData interface.
func (lic *MachineFileDataset) SetData(to func(target interface{}) error) error {
return to(&lic.Machine)
}
// SetMeta implements jsonapi.UnmarshalMeta interface.
func (lic *MachineFileDataset) SetMeta(to func(target interface{}) error) error {
return to(&lic)
}
// SetIncluded implements jsonapi.UnmarshalIncluded interface.
func (lic *MachineFileDataset) SetIncluded(relationships []*jsonapi.ResourceObject, unmarshal func(res *jsonapi.ResourceObject, target interface{}) error) error {
for _, relationship := range relationships {
switch relationship.Type {
case "components":
component := &Component{}
if err := unmarshal(relationship, component); err != nil {
return err
}
lic.Components = append(lic.Components, *component)
case "entitlements":
entitlement := &Entitlement{}
if err := unmarshal(relationship, entitlement); err != nil {
return err
}
lic.Entitlements = append(lic.Entitlements, *entitlement)
case "licenses":
license := &License{}
if err := unmarshal(relationship, license); err != nil {
return err
}
lic.License = *license
}
}
return nil
}