-
Notifications
You must be signed in to change notification settings - Fork 18
/
encoder.go
450 lines (390 loc) · 11.2 KB
/
encoder.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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package crypto
import (
"crypto"
"crypto/ecdsa"
"crypto/ed25519"
"crypto/elliptic"
"crypto/rsa"
"crypto/sha256"
"crypto/sha512"
"crypto/x509"
"encoding/asn1"
"encoding/base64"
"encoding/hex"
"encoding/json"
"encoding/pem"
"fmt"
"github.com/pkg/errors"
"go.step.sm/crypto/pemutil"
// Import Blake2b
_ "golang.org/x/crypto/blake2b"
"golang.org/x/crypto/ssh"
jose "gopkg.in/square/go-jose.v2"
"gopkg.in/square/go-jose.v2/jwt"
"github.com/elastic/harp/build/fips"
"github.com/elastic/harp/pkg/sdk/security/crypto/bech32"
"github.com/elastic/harp/pkg/sdk/types"
)
// ToJWK encodes given key using JWK.
func ToJWK(key interface{}) (string, error) {
// Check key
if types.IsNil(key) {
return "", fmt.Errorf("unable to encode nil key")
}
// Wrap key
keyWrapper := jose.JSONWebKey{Key: key, KeyID: ""}
// Don't process Ed25519 keys
if fips.Enabled() {
switch key.(type) {
case ed25519.PrivateKey, ed25519.PublicKey:
return "", errors.New("ed25519 key processing is disabled in FIPS Mode")
}
}
// Generate thumbprint
thumb, err := keyWrapper.Thumbprint(crypto.SHA512_256)
if err != nil {
return "", err
}
// Assign thumbprint
keyWrapper.KeyID = base64.URLEncoding.EncodeToString(thumb)
// Marshal private as JSON
payload, err := keyWrapper.MarshalJSON()
if err != nil {
return "", err
}
// No error
return string(payload), nil
}
// FromJWK parses a JWK and return wrapped keys.
func FromJWK(jwk string) (interface{}, error) {
var k jose.JSONWebKey
// Decode JWK
if err := json.Unmarshal([]byte(jwk), &k); err != nil {
return nil, fmt.Errorf("unable to decode JWK: %w", err)
}
// Don't process Ed25519 keys
if fips.Enabled() {
switch k.Key.(type) {
case ed25519.PrivateKey, ed25519.PublicKey:
return "", errors.New("ed25519 key processing is disabled in FIPS Mode")
}
}
if k.IsPublic() {
// No error
return struct {
Public interface{}
}{
Public: k.Key,
}, nil
}
// No error
return struct {
Private interface{}
Public interface{}
}{
Private: k.Key,
Public: k.Public().Key,
}, nil
}
// ToPEM encodes the given key using PEM.
func ToPEM(key interface{}) (string, error) {
// Check key
if types.IsNil(key) {
return "", fmt.Errorf("unable to encode nil key")
}
// Don't process Ed25519 keys
if fips.Enabled() {
switch key.(type) {
case ed25519.PrivateKey, ed25519.PublicKey:
return "", errors.New("ed25519 key processing is disabled in FIPS Mode")
}
}
// Delegate to smallstep library
pemBlock, err := pemutil.Serialize(key, pemutil.WithPKCS8(true))
if err != nil {
return "", fmt.Errorf("unable to serialize input as PEM: %w", err)
}
return string(pem.EncodeToMemory(pemBlock)), nil
}
// KeyToBytes encodes the given crypto key as a byte array.
func KeyToBytes(key interface{}) ([]byte, error) {
// Check key
if types.IsNil(key) {
return nil, fmt.Errorf("unable to encode nil key")
}
var (
out []byte
err error
)
switch k := key.(type) {
// Private keys ------------------------------------------------------------
case *rsa.PrivateKey, *ecdsa.PrivateKey:
out, err = x509.MarshalPKCS8PrivateKey(k)
if err != nil {
return nil, err
}
case ed25519.PrivateKey:
if fips.Enabled() {
return nil, errors.New("ed25519 private key processing is disabled in FIPS Mode")
}
out = []byte(k)
// Public keys ------------------------------------------------------------
case *rsa.PublicKey:
out, err = x509.MarshalPKIXPublicKey(k)
if err != nil {
return nil, err
}
case *ecdsa.PublicKey:
out = elliptic.MarshalCompressed(k.Curve, k.X, k.Y)
case ed25519.PublicKey:
if fips.Enabled() {
return nil, errors.New("ed25519 private key processing is disabled in FIPS Mode")
}
out = []byte(k)
default:
return nil, fmt.Errorf("given key type is not supported")
}
return out, nil
}
// EncryptPEM returns an encrypted PEM block using the given passphrase.
func EncryptPEM(pemData, passphrase string) (string, error) {
// Check passphrase
if len(passphrase) < 32 {
return "", fmt.Errorf("passphrase must contains more than 32 characters, usage of a diceware passphrase is recommended")
}
// Decode PEM
out, err := pemutil.Parse([]byte(pemData))
if err != nil {
return "", fmt.Errorf("unable to parse input PEM data: %w", err)
}
// Encrypt PEM
encryptedBlock, err := pemutil.Serialize(out, pemutil.WithPKCS8(true), pemutil.WithPassword([]byte(passphrase)))
if err != nil {
return "", fmt.Errorf("unable to export encrypted PEM: %w", err)
}
// Build output
outPem := pem.EncodeToMemory(encryptedBlock)
// No error
return string(outPem), nil
}
// ToSSH encodes the given key as SSH key.
func ToSSH(key interface{}) (string, error) {
var result []byte
// Check key
if types.IsNil(key) {
return "", fmt.Errorf("unable to encode nil key")
}
switch k := key.(type) {
// Public keys ------------------------------------------------------------
case *rsa.PublicKey, *ecdsa.PublicKey, ed25519.PublicKey:
if _, ok := k.(ed25519.PublicKey); ok && fips.Enabled() {
return "", errors.New("ed25519 public key processing is disabled in FIPS Mode")
}
pubKey, err := ssh.NewPublicKey(k)
if err != nil {
return "", fmt.Errorf("unable to convert key as ssh public key: %w", err)
}
result = ssh.MarshalAuthorizedKey(pubKey)
// Private keys --------------------------------------------------------
default:
if _, ok := k.(ed25519.PrivateKey); ok && fips.Enabled() {
return "", errors.New("ed25519 private key processing is disabled in FIPS Mode")
}
pemBlock, err := pemutil.Serialize(key, pemutil.WithOpenSSH(true))
if err != nil {
return "", fmt.Errorf("unable to encode SSH key: %w", err)
}
result = pem.EncodeToMemory(pemBlock)
}
// No error
return string(result), nil
}
// EncryptJWE encrypts input as JWE token.
func EncryptJWE(key string, payload interface{}) (string, error) {
// Prepare encrypter
encrypter, err := jose.NewEncrypter(jose.A128GCM, jose.Recipient{Algorithm: jose.PBES2_HS256_A128KW, Key: key}, nil)
if err != nil {
return "", fmt.Errorf("unable to initialize JWE encrypter: %w", err)
}
// Marshal payload
payloadBytes, err := json.Marshal(payload)
if err != nil {
return "", fmt.Errorf("unable to marshal content: %w", err)
}
// Encrypt
object, err := encrypter.Encrypt(payloadBytes)
if err != nil {
return "", fmt.Errorf("unable to encrypt payload: %w", err)
}
// Return JWE
return object.CompactSerialize()
}
// DecryptJWE decrypt a JWE token.
func DecryptJWE(key, token string) (interface{}, error) {
// Parse JWE token
object, err := jose.ParseEncrypted(token)
if err != nil {
return "", fmt.Errorf("unable to parse JWE assertion: %w", err)
}
// Decrypt token using given key.
payloadBytes, err := object.Decrypt(key)
if err != nil {
return "", fmt.Errorf("unable to decrypt JWE: %w", err)
}
// Decode payload
var data interface{}
if err := json.Unmarshal(payloadBytes, &data); err != nil {
return "", fmt.Errorf("unable to decode payload: %w", err)
}
// No error
return data, nil
}
// ToJWS returns a JWT token.
func ToJWS(payload, privkey interface{}) (string, error) {
var alg jose.SignatureAlgorithm
// Select appropriate algorithm
switch k := privkey.(type) {
case *rsa.PrivateKey:
alg = jose.RS256
case *ecdsa.PrivateKey:
switch k.Curve {
case elliptic.P256():
alg = jose.ES256
case elliptic.P384():
alg = jose.ES384
case elliptic.P521():
alg = jose.ES512
}
case ed25519.PrivateKey:
if fips.Enabled() {
return "", errors.New("signature with Ed25519 key is disabled in FIPS Mode")
}
alg = jose.EdDSA
default:
return "", fmt.Errorf("this private key type is not supported '%T'", privkey)
}
// Create a signer
signer, err := jose.NewSigner(jose.SigningKey{Algorithm: alg, Key: privkey}, nil)
if err != nil {
return "", fmt.Errorf("unable to initialize signer: %w", err)
}
// Encode payload
payloadBytes, err := json.Marshal(payload)
if err != nil {
return "", fmt.Errorf("unable to marshal payload as json: %w", err)
}
// Sign the token
object, err := signer.Sign(payloadBytes)
if err != nil {
return "", fmt.Errorf("unable to sign payload: %w", err)
}
// Serialize final token
serialize, err := object.CompactSerialize()
if err != nil {
return "", fmt.Errorf("unable to generate final token: %w", err)
}
// No error
return serialize, nil
}
// ParseJWT unpack a JWT without signature verification.
func ParseJWT(token string) (interface{}, error) {
// Parse token
t, err := jwt.ParseSigned(token)
if err != nil {
return nil, fmt.Errorf("unable to parse input token: %w", err)
}
// Extract claims without verification
var claims map[string]interface{}
if err := t.UnsafeClaimsWithoutVerification(&claims); err != nil {
return nil, fmt.Errorf("unable to extract claims from token: %w", err)
}
return struct {
Headers []jose.Header
Claims map[string]interface{}
}{
Headers: t.Headers,
Claims: claims,
}, nil
}
func VerifyJWT(token string, key interface{}) (interface{}, error) {
// Parse token
t, err := jwt.ParseSigned(token)
if err != nil {
return nil, fmt.Errorf("unable to parse input token: %w", err)
}
// Extract claims without verification
var claims map[string]interface{}
if err := t.Claims(key, &claims); err != nil {
return nil, fmt.Errorf("unable to extract claims from token: %w", err)
}
return struct {
Headers []jose.Header
Claims map[string]interface{}
}{
Headers: t.Headers,
Claims: claims,
}, nil
}
// Bech32Decode decodes given bech32 encoded string.
func Bech32Decode(in string) (interface{}, error) {
hrp, data, err := bech32.Decode(in)
if err != nil {
return nil, fmt.Errorf("unbale to decode Bech32 encoding string: %w", err)
}
return struct {
Hrp string
Data []byte
}{
Hrp: hrp,
Data: data,
}, nil
}
// ToTLSA encodes the given certificate using TLSA encoding strategy.
// selector 0 - Raw / 1 - Public Key
// mtype 0 - Raw / 1 - SHA256 / 2 - SHA512
func ToTLSA(selector, mtype uint8, cert *x509.Certificate) (string, error) {
var (
preimage asn1.RawContent
output []byte
tmp256 [32]byte
tmp512 [64]byte
)
switch selector {
case 0:
preimage = cert.Raw
case 1:
preimage = cert.RawSubjectPublicKeyInfo
default:
return "", fmt.Errorf("unknown TLSA selector: %d", selector)
}
switch mtype {
case 0:
output = preimage
case 1:
tmp256 = sha256.Sum256(preimage)
output = tmp256[:]
case 2:
tmp512 = sha512.Sum512(preimage)
output = tmp512[:]
default:
return "", fmt.Errorf("unknown TLSA matching type: %d", mtype)
}
// No error
return hex.EncodeToString(output), nil
}