-
Notifications
You must be signed in to change notification settings - Fork 90
/
signature.go
264 lines (222 loc) · 8.74 KB
/
signature.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
package pull
import (
"crypto"
"crypto/rsa"
"crypto/x509"
"encoding/json"
"encoding/pem"
"fmt"
"github.com/pkg/errors"
kotsv1beta1 "github.com/replicatedhq/kots/kotskinds/apis/kots/v1beta1"
)
var (
ErrSignatureInvalid = errors.New("signature is invalid")
ErrSignatureMissing = errors.New("signature is missing")
)
type InnerSignature struct {
LicenseSignature []byte `json:"licenseSignature"`
PublicKey string `json:"publicKey"`
KeySignature []byte `json:"keySignature"`
}
type OuterSignature struct {
LicenseData []byte `json:"licenseData"`
InnerSignature []byte `json:"innerSignature"`
}
type KeySignature struct {
Signature []byte `json:"signature"`
GlobalKeyId string `json:"globalKeyId"`
}
func VerifySignature(license *kotsv1beta1.License) (*kotsv1beta1.License, error) {
outerSignature := &OuterSignature{}
if err := json.Unmarshal(license.Spec.Signature, outerSignature); err != nil {
return nil, errors.Wrap(err, "failed to unmarshal license outer signature")
}
isOldFormat := len(outerSignature.InnerSignature) == 0
if isOldFormat {
return VerifyOldSignature(license)
}
innerSignature := &InnerSignature{}
if err := json.Unmarshal(outerSignature.InnerSignature, innerSignature); err != nil {
return nil, errors.Wrap(err, "failed to unmarshal license inner signature")
}
keySignature := &KeySignature{}
if err := json.Unmarshal(innerSignature.KeySignature, keySignature); err != nil {
return nil, errors.Wrap(err, "failed to unmarshal key signature")
}
globalKeyPEM, ok := publicKeys[keySignature.GlobalKeyId]
if !ok {
return nil, errors.New("unknown global key")
}
if err := verify([]byte(innerSignature.PublicKey), keySignature.Signature, globalKeyPEM); err != nil {
return nil, errors.Wrap(err, "failed to verify key signature")
}
if err := verify(outerSignature.LicenseData, innerSignature.LicenseSignature, []byte(innerSignature.PublicKey)); err != nil {
return nil, errors.Wrap(err, "failed to verify license signature")
}
verifiedLicense := &kotsv1beta1.License{}
if err := json.Unmarshal(outerSignature.LicenseData, verifiedLicense); err != nil {
return nil, errors.Wrap(err, "failed to unmarshal license data")
}
if err := verifyLicenseData(license, verifiedLicense); err != nil {
return nil, errors.Wrap(err, "failed to verify license data")
}
verifiedLicense.Spec.Endpoint = license.Spec.Endpoint
verifiedLicense.Spec.Signature = license.Spec.Signature
return verifiedLicense, nil
}
func verify(message, signature, publicKeyPEM []byte) error {
pubBlock, _ := pem.Decode(publicKeyPEM)
publicKey, err := x509.ParsePKIXPublicKey(pubBlock.Bytes)
if err != nil {
return errors.Wrap(err, "failed to load public key from PEM")
}
var opts rsa.PSSOptions
opts.SaltLength = rsa.PSSSaltLengthAuto
newHash := crypto.MD5
pssh := newHash.New()
pssh.Write(message)
hashed := pssh.Sum(nil)
err = rsa.VerifyPSS(publicKey.(*rsa.PublicKey), newHash, hashed, signature, &opts)
if err != nil {
// this ordering makes errors.Cause a little more useful
return errors.Wrap(ErrSignatureInvalid, err.Error())
}
return nil
}
func verifyLicenseData(outerLicense *kotsv1beta1.License, innerLicense *kotsv1beta1.License) error {
if outerLicense.Spec.AppSlug != innerLicense.Spec.AppSlug {
return errors.New("\"appSlug\" field has changed")
}
if outerLicense.Spec.ChannelName != innerLicense.Spec.ChannelName {
return errors.New("\"channelName\" field has changed")
}
if outerLicense.Spec.LicenseSequence != innerLicense.Spec.LicenseSequence {
return errors.New("\"licenseSequence\" field has changed")
}
if outerLicense.Spec.LicenseID != innerLicense.Spec.LicenseID {
return errors.New("\"licenseID\" field has changed")
}
if outerLicense.Spec.LicenseType != innerLicense.Spec.LicenseType {
return errors.New("\"LicenseType\" field has changed")
}
if outerLicense.Spec.IsAirgapSupported != innerLicense.Spec.IsAirgapSupported {
return errors.New("\"IsAirgapSupported\" field has changed")
}
if outerLicense.Spec.IsGitOpsSupported != innerLicense.Spec.IsGitOpsSupported {
return errors.New("\"IsGitOpsSupported\" field has changed")
}
if outerLicense.Spec.IsSnapshotSupported != innerLicense.Spec.IsSnapshotSupported {
return errors.New("\"IsSnapshotSupported\" field has changed")
}
// Check entitlements
if len(outerLicense.Spec.Entitlements) != len(innerLicense.Spec.Entitlements) {
return errors.New("\"entitlements\" field has changed")
}
for k, outerEntitlement := range outerLicense.Spec.Entitlements {
innerEntitlement, ok := innerLicense.Spec.Entitlements[k]
if !ok {
return errors.New("entitlement not found in the inner license")
}
if outerEntitlement.Value.Value() != innerEntitlement.Value.Value() {
return errors.New("one or more of the entitlements values have changed")
}
if outerEntitlement.Title != innerEntitlement.Title {
return errors.New("one or more of the entitlements titles have changed")
}
if outerEntitlement.Description != innerEntitlement.Description {
return errors.New("one or more of the entitlements descriptions have changed")
}
if outerEntitlement.IsHidden != innerEntitlement.IsHidden {
return errors.New("one or more of the entitlements hidden flags have changed")
}
if outerEntitlement.ValueType != innerEntitlement.ValueType {
return errors.New("one or more of the entitlements value types have changed")
}
}
return nil
}
func VerifyOldSignature(license *kotsv1beta1.License) (*kotsv1beta1.License, error) {
signature := &InnerSignature{}
if err := json.Unmarshal(license.Spec.Signature, signature); err != nil {
// old licenses's signature is a single space character
if len(license.Spec.Signature) == 0 || len(license.Spec.Signature) == 1 {
return nil, ErrSignatureMissing
}
return nil, errors.Wrap(err, "failed to unmarshal license signature")
}
keySignature := &KeySignature{}
if err := json.Unmarshal(signature.KeySignature, keySignature); err != nil {
return nil, errors.Wrap(err, "failed to unmarshal key signature")
}
globalKeyPEM, ok := publicKeys[keySignature.GlobalKeyId]
if !ok {
return nil, errors.New("unknown global key")
}
if err := verify([]byte(signature.PublicKey), keySignature.Signature, globalKeyPEM); err != nil {
return nil, errors.Wrap(err, "failed to verify key signature")
}
licenseMessage, err := getMessageFromLicense(license)
if err != nil {
return nil, errors.Wrap(err, "failed to convert license to message")
}
if err := verify(licenseMessage, signature.LicenseSignature, []byte(signature.PublicKey)); err != nil {
return nil, errors.Wrap(err, "failed to verify license signature")
}
return license, nil
}
func getMessageFromLicense(license *kotsv1beta1.License) ([]byte, error) {
// JSON marshaller will sort map keys automatically.
fields := map[string]string{
"apiVersion": license.APIVersion,
"kind": license.Kind,
"metadata.name": license.GetObjectMeta().GetName(),
"spec.licenseID": license.Spec.LicenseID,
"spec.appSlug": license.Spec.AppSlug,
"spec.channelName": license.Spec.ChannelName,
"spec.endpoint": license.Spec.Endpoint,
"spec.isAirgapSupported": fmt.Sprintf("%t", license.Spec.IsAirgapSupported),
}
if license.Spec.LicenseSequence > 0 {
fields["spec.licenseSequence"] = fmt.Sprintf("%d", license.Spec.LicenseSequence)
}
for k, v := range license.Spec.Entitlements {
key := fmt.Sprintf("spec.entitlements.%s", k)
val := map[string]string{
"title": v.Title,
"description": v.Description,
"value": fmt.Sprintf("%v", v.Value.Value()),
}
valStr, err := json.Marshal(val)
if err != nil {
return nil, errors.Wrapf(err, "failed to marshal entitlement value: %s", k)
}
fields[key] = string(valStr)
}
message, err := json.Marshal(fields)
if err != nil {
return nil, errors.Wrap(err, "failed to marshal message JSON")
}
return message, err
}
func GetAppPublicKey(license *kotsv1beta1.License) ([]byte, error) {
// old licenses's signature is a single space character
if len(license.Spec.Signature) == 0 || len(license.Spec.Signature) == 1 {
return nil, ErrSignatureMissing
}
innerSignature := &InnerSignature{}
outerSignature := &OuterSignature{}
if err := json.Unmarshal(license.Spec.Signature, outerSignature); err != nil {
return nil, errors.Wrap(err, "failed to unmarshal license outer signature")
}
isOldFormat := len(outerSignature.InnerSignature) == 0
if isOldFormat {
if err := json.Unmarshal(license.Spec.Signature, innerSignature); err != nil {
return nil, errors.Wrap(err, "failed to unmarshal license signature")
}
} else {
if err := json.Unmarshal(outerSignature.InnerSignature, innerSignature); err != nil {
return nil, errors.Wrap(err, "failed to unmarshal license inner signature")
}
}
return []byte(innerSignature.PublicKey), nil
}