-
Notifications
You must be signed in to change notification settings - Fork 295
Expand file tree
/
Copy pathtoken_generator.go
More file actions
544 lines (486 loc) · 16.8 KB
/
token_generator.go
File metadata and controls
544 lines (486 loc) · 16.8 KB
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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
package cautils
import (
"crypto"
"crypto/ecdsa"
"crypto/ed25519"
"crypto/elliptic"
"crypto/rsa"
"encoding/base64"
"encoding/json"
"fmt"
"os"
"strings"
"time"
"github.com/pkg/errors"
"github.com/urfave/cli"
"github.com/smallstep/certificates/authority/provisioner"
"github.com/smallstep/certificates/pki"
"github.com/smallstep/cli-utils/errs"
"github.com/smallstep/cli-utils/ui"
"go.step.sm/crypto/jose"
"go.step.sm/crypto/pemutil"
"go.step.sm/crypto/randutil"
"github.com/smallstep/cli/exec"
"github.com/smallstep/cli/internal/cryptoutil"
"github.com/smallstep/cli/token"
"github.com/smallstep/cli/token/provision"
)
// TokenGenerator is a helper used to generate different types of tokens used in
// the CA.
type TokenGenerator struct {
kid, iss, aud string
root string
notBefore, notAfter time.Time
jwk *jose.JSONWebKey
}
// NewTokenGenerator initializes a new token generator with the common fields.
func NewTokenGenerator(kid, iss, aud, root string, notBefore, notAfter time.Time, jwk *jose.JSONWebKey) *TokenGenerator {
return &TokenGenerator{
kid: kid,
iss: iss,
aud: aud,
root: root,
notBefore: notBefore,
notAfter: notAfter,
jwk: jwk,
}
}
// Token generates a generic token with the given subject and options.
func (t *TokenGenerator) Token(sub string, opts ...token.Options) (string, error) {
// A random jwt id will be used to identify duplicated tokens
jwtID, err := randutil.Hex(64) // 256 bits
if err != nil {
return "", err
}
tokOptions := []token.Options{
token.WithJWTID(jwtID),
token.WithKid(t.kid),
token.WithIssuer(t.iss),
token.WithAudience(t.aud),
}
if t.root != "" {
tokOptions = append(tokOptions, token.WithRootCA(t.root))
}
// Add custom options
tokOptions = append(tokOptions, opts...)
// Add token validity
notBefore, notAfter := t.notBefore, t.notAfter
if !notBefore.IsZero() || !notAfter.IsZero() {
if notBefore.IsZero() {
notBefore = time.Now()
}
if notAfter.IsZero() {
notAfter = notBefore.Add(token.DefaultValidity)
}
tokOptions = append(tokOptions, token.WithValidity(notBefore, notAfter))
}
tok, err := provision.New(sub, tokOptions...)
if err != nil {
return "", err
}
return tok.SignedString(t.jwk.Algorithm, t.jwk.Key)
}
// SignToken generates a X.509 certificate signing token. If sans is empty, we
// will use the subject (common name) as the only SAN.
func (t *TokenGenerator) SignToken(sub string, sans []string, certNotBefore, certNotAfter provisioner.TimeDuration, opts ...token.Options) (string, error) {
if len(sans) == 0 {
sans = []string{sub}
}
opts = append(opts, token.WithSANS(sans))
// Add validity options for the certificate
if !certNotBefore.IsZero() || !certNotAfter.IsZero() {
opts = append(opts, token.WithValidityOptions(certNotBefore, certNotAfter))
}
// Tie certificate request to the token used in the JWK and X5C provisioners
if sharedContext.CertificateRequest != nil {
opts = append(opts, token.WithFingerprint(sharedContext.CertificateRequest))
} else if sharedContext.ConfirmationFingerprint != "" {
opts = append(opts, token.WithConfirmationFingerprint(sharedContext.ConfirmationFingerprint))
}
// Add custom user data, if set.
if sharedContext.CustomAttributes != nil {
opts = append(opts, token.WithUserData(sharedContext.CustomAttributes))
}
return t.Token(sub, opts...)
}
// RevokeToken generates a X.509 certificate revoke token.
func (t *TokenGenerator) RevokeToken(sub string, opts ...token.Options) (string, error) {
return t.Token(sub, opts...)
}
// SignSSHToken generates a SSH certificate signing token.
func (t *TokenGenerator) SignSSHToken(sub, certType string, principals []string, notBefore, notAfter provisioner.TimeDuration, opts ...token.Options) (string, error) {
opts = append([]token.Options{token.WithSSH(provisioner.SignSSHOptions{
CertType: certType,
KeyID: sub,
Principals: principals,
ValidAfter: notBefore,
ValidBefore: notAfter,
})}, opts...)
// Add custom user data, if set.
if sharedContext.CustomAttributes != nil {
opts = append(opts, token.WithUserData(sharedContext.CustomAttributes))
}
return t.Token(sub, opts...)
}
// generateOIDCToken performs the necessary protocol to retrieve an OIDC token
// using a configured provisioner.
func generateOIDCToken(ctx *cli.Context, p *provisioner.OIDC) (string, error) {
args := []string{"oauth", "--oidc", "--bare",
"--provider", p.ConfigurationEndpoint,
"--client-id", p.ClientID, "--client-secret", p.ClientSecret}
if len(p.Scopes) != 0 {
for _, keyval := range p.Scopes {
args = append(args, "--scope", keyval)
}
}
if len(p.AuthParams) != 0 {
for _, keyval := range p.AuthParams {
args = append(args, "--auth-param", keyval)
}
}
if ctx.Bool("console") {
args = append(args, "--console")
}
if p.ListenAddress != "" && os.Getenv("STEP_LISTEN") == "" {
args = append(args, "--listen", p.ListenAddress)
}
out, err := exec.Step(args...)
if err != nil {
return "", fmt.Errorf(`error generating OIDC token: exec "step oauth" failed`)
}
return strings.TrimSpace(string(out)), nil
}
type tokenAttrs struct {
subject string
root string
caURL string
audience string
provisionerName string
kid string
sans []string
notBefore, notAfter time.Time
certNotBefore, certNotAfter provisioner.TimeDuration
}
func generateK8sSAToken(ctx *cli.Context) (string, error) {
path := ctx.String("k8ssa-token-path")
if path == "" {
path = "/var/run/secrets/kubernetes.io/serviceaccount/token"
}
tokBytes, err := os.ReadFile(path)
if err != nil {
return "", errors.Wrap(err, "error reading kubernetes service account token")
}
return string(tokBytes), nil
}
func generateX5CToken(ctx *cli.Context, p *provisioner.X5C, tokType int, tokAttrs tokenAttrs) (string, error) {
x5cCertFile := ctx.String("x5c-cert")
x5cKeyFile := ctx.String("x5c-key")
x5cChainFiles := ctx.StringSlice("x5c-chain")
kmsURI := ctx.String("kms")
if x5cCertFile == "" {
return "", errs.RequiredWithProvisionerTypeFlag(ctx, "X5C", "x5c-cert")
}
if x5cKeyFile == "" {
return "", errs.RequiredWithProvisionerTypeFlag(ctx, "X5C", "x5c-key")
}
var jwk *jose.JSONWebKey
var err error
var opts []pemutil.Options
if passOpt := getProvisionerPasswordPEMOption(ctx); passOpt != nil {
opts = append(opts, passOpt)
}
var kmsSigner crypto.Signer
kmsSigner, err = cryptoutil.CreateSigner(kmsURI, x5cKeyFile, opts...)
if err != nil {
return "", err
}
joseSigner := jose.NewOpaqueSigner(kmsSigner)
alg, err := getSigningAlgorithm(kmsSigner.Public())
if err != nil {
return "", err
}
jwk = &jose.JSONWebKey{
Key: joseSigner,
KeyID: x5cKeyFile,
Algorithm: alg,
}
tokenGen := NewTokenGenerator(jwk.KeyID, p.Name,
fmt.Sprintf("%s#%s", tokAttrs.audience, p.GetIDForToken()), tokAttrs.root,
tokAttrs.notBefore, tokAttrs.notAfter, jwk)
var tokenOpts []token.Options
x5cCerts, err := cryptoutil.LoadCertificate(kmsURI, x5cCertFile)
if err != nil {
return "", fmt.Errorf("could not load x5c certificate: %w", err)
}
for _, chainPath := range x5cChainFiles {
x5cChainCerts, err := cryptoutil.LoadCertificate(kmsURI, chainPath)
if err != nil {
return "", fmt.Errorf("could not load x5c chain certificate %s: %w", chainPath, err)
}
x5cCerts = append(x5cCerts, x5cChainCerts...)
}
if ctx.Bool("x5c-insecure") {
tokenOpts = append(tokenOpts, token.WithX5CInsecureCerts(x5cCerts, jwk.Key))
} else {
tokenOpts = append(tokenOpts, token.WithX5CCerts(x5cCerts, jwk.Key))
}
switch tokType {
case SignType:
return tokenGen.SignToken(tokAttrs.subject, tokAttrs.sans, tokAttrs.certNotBefore, tokAttrs.certNotAfter, tokenOpts...)
case RevokeType:
return tokenGen.RevokeToken(tokAttrs.subject, tokenOpts...)
case SSHUserSignType:
return tokenGen.SignSSHToken(tokAttrs.subject, provisioner.SSHUserCert, tokAttrs.sans,
tokAttrs.certNotBefore, tokAttrs.certNotAfter, tokenOpts...)
case SSHHostSignType:
return tokenGen.SignSSHToken(tokAttrs.subject, provisioner.SSHHostCert, tokAttrs.sans,
tokAttrs.certNotBefore, tokAttrs.certNotAfter, tokenOpts...)
default:
return tokenGen.Token(tokAttrs.subject, tokenOpts...)
}
}
func generateNebulaToken(ctx *cli.Context, p *provisioner.Nebula, tokType int, tokAttrs tokenAttrs) (string, error) {
certFile := ctx.String("nebula-cert")
keyFile := ctx.String("nebula-key")
if certFile == "" {
return "", errs.RequiredWithProvisionerTypeFlag(ctx, "Nebula", "nebula-cert")
}
if keyFile == "" {
return "", errs.RequiredWithProvisionerTypeFlag(ctx, "Nebula", "nebula-key")
}
// Get private key from given key file, nebula CAs uses ed25519 keys while
// nebula leafs uses X25519 keys.
jwk, err := jose.ReadKey(keyFile)
if err != nil {
return "", err
}
key := jwk.Key
tokenGen := NewTokenGenerator(jwk.KeyID, p.Name,
fmt.Sprintf("%s#%s", tokAttrs.audience, p.GetIDForToken()), tokAttrs.root,
tokAttrs.notBefore, tokAttrs.notAfter, jwk)
switch tokType {
case SignType:
return tokenGen.SignToken(tokAttrs.subject, tokAttrs.sans, tokAttrs.certNotBefore, tokAttrs.certNotAfter, token.WithNebulaCert(certFile, key))
case RevokeType:
return tokenGen.RevokeToken(tokAttrs.subject, token.WithNebulaCert(certFile, key))
case SSHUserSignType:
return tokenGen.SignSSHToken(tokAttrs.subject, provisioner.SSHUserCert, tokAttrs.sans,
tokAttrs.certNotBefore, tokAttrs.certNotAfter, token.WithNebulaCert(certFile, key))
case SSHHostSignType:
return tokenGen.SignSSHToken(tokAttrs.subject, provisioner.SSHHostCert, tokAttrs.sans,
tokAttrs.certNotBefore, tokAttrs.certNotAfter, token.WithNebulaCert(certFile, key))
default:
return tokenGen.Token(tokAttrs.subject, token.WithNebulaCert(certFile, key))
}
}
func generateSSHPOPToken(ctx *cli.Context, p *provisioner.SSHPOP, tokType int, tokAttrs tokenAttrs) (string, error) {
sshPOPCertFile := ctx.String("sshpop-cert")
sshPOPKeyFile := ctx.String("sshpop-key")
if sshPOPCertFile == "" {
return "", errs.RequiredWithProvisionerTypeFlag(ctx, "SSHPOP", "sshpop-cert")
}
if sshPOPKeyFile == "" {
return "", errs.RequiredWithProvisionerTypeFlag(ctx, "SSHPOP", "sshpop-key")
}
// Get private key from given key file
var opts []jose.Option
if passOpt := getProvisionerPasswordOption(ctx); passOpt != nil {
opts = append(opts, passOpt)
}
jwk, err := cryptoutil.LoadJSONWebKey(ctx.String("kms"), sshPOPKeyFile, opts...)
if err != nil {
return "", err
}
tokenGen := NewTokenGenerator(jwk.KeyID, p.Name,
fmt.Sprintf("%s#%s", tokAttrs.audience, p.GetIDForToken()), tokAttrs.root,
tokAttrs.notBefore, tokAttrs.notAfter, jwk)
switch tokType {
case SSHRevokeType:
return tokenGen.Token(tokAttrs.subject, token.WithSSHPOPFile(sshPOPCertFile, jwk.Key))
case SSHRenewType:
return tokenGen.Token(tokAttrs.subject, token.WithSSHPOPFile(sshPOPCertFile, jwk.Key))
case SSHRekeyType:
return tokenGen.Token(tokAttrs.subject, token.WithSSHPOPFile(sshPOPCertFile, jwk.Key))
default:
return "", errors.Errorf("unexpected requested token type for SSHPOP token: %d", tokType)
}
}
func getProvisionerPasswordOption(ctx *cli.Context) jose.Option {
switch {
case ctx.String("provisioner-password-file") != "":
return jose.WithPasswordFile(ctx.String("provisioner-password-file"))
case ctx.String("admin-password-file") != "":
return jose.WithPasswordFile(ctx.String("admin-password-file"))
case ctx.String("password-file") != "":
return jose.WithPasswordFile(ctx.String("password-file"))
default:
return nil
}
}
func getProvisionerPasswordPEMOption(ctx *cli.Context) pemutil.Options {
switch {
case ctx.String("provisioner-password-file") != "":
return pemutil.WithPasswordFile(ctx.String("provisioner-password-file"))
case ctx.String("admin-password-file") != "":
return pemutil.WithPasswordFile(ctx.String("admin-password-file"))
case ctx.String("password-file") != "":
return pemutil.WithPasswordFile(ctx.String("password-file"))
default:
return nil
}
}
// loadJWK loads a JWK based on the following system:
// 1. If a private key is specified on the command line, then load the JWK from
// that private key.
// 2. No private key was given on the command line. We'll need to use the
// provided provisioner to load a signing key.
// a) Offline-mode: load the JWK directly from the provisioner in the CA-config.
// b) Online-mode: get the provisioner private key from the CA.
func loadJWK(ctx *cli.Context, p *provisioner.JWK, tokAttrs tokenAttrs) (jwk *jose.JSONWebKey, kid string, err error) {
var opts []jose.Option
if passOpt := getProvisionerPasswordOption(ctx); passOpt != nil {
opts = append(opts, passOpt)
}
if keyFile := ctx.String("key"); keyFile == "" {
if p == nil {
return nil, "", errors.New("no provisioner selected")
}
kid = p.Key.KeyID
// If provisioner is not nil then we must be using the offlineCA.
var encryptedKey string
if ctx.IsSet("offline") {
encryptedKey = p.EncryptedKey
if encryptedKey == "" {
return nil, "", errors.Errorf("provisioner '%s' does not have an 'encryptedKey' property", kid)
}
} else {
// Get private key from CA.
encryptedKey, err = pki.GetProvisionerKey(tokAttrs.caURL, tokAttrs.root, kid)
if err != nil {
return nil, "", err
}
}
opts = append(opts, jose.WithPasswordPrompter("Please enter the password to decrypt the provisioner key",
func(s string) ([]byte, error) {
return ui.PromptPassword(s)
}),
)
decrypted, err := jose.Decrypt([]byte(encryptedKey), opts...)
if err != nil {
return nil, "", err
}
jwk = new(jose.JSONWebKey)
if err := json.Unmarshal(decrypted, jwk); err != nil {
return nil, "", errors.Wrap(err, "error unmarshaling provisioning key")
}
} else {
jwk, err = cryptoutil.LoadJSONWebKey(ctx.String("kms"), keyFile, opts...)
if err != nil {
return nil, "", err
}
switch {
case p != nil:
kid = p.Key.KeyID
case tokAttrs.kid != "":
kid = tokAttrs.kid
default:
if kid, err = jose.Thumbprint(jwk); err != nil {
return nil, "", err
}
}
}
return
}
func generateJWKToken(ctx *cli.Context, p *provisioner.JWK, tokType int, tokAttrs tokenAttrs) (string, error) {
jwk, kid, err := loadJWK(ctx, p, tokAttrs)
if err != nil {
return "", err
}
issuer := tokAttrs.provisionerName
if p != nil {
issuer = p.Name
}
// Generate token
tokenGen := NewTokenGenerator(kid, issuer, tokAttrs.audience, tokAttrs.root,
tokAttrs.notBefore, tokAttrs.notAfter, jwk)
switch tokType {
case SignType:
return tokenGen.SignToken(tokAttrs.subject, tokAttrs.sans, tokAttrs.certNotBefore, tokAttrs.certNotAfter)
case RevokeType:
return tokenGen.RevokeToken(tokAttrs.subject)
case SSHUserSignType:
return tokenGen.SignSSHToken(tokAttrs.subject, provisioner.SSHUserCert,
tokAttrs.sans, tokAttrs.certNotBefore, tokAttrs.certNotAfter)
case SSHHostSignType:
return tokenGen.SignSSHToken(tokAttrs.subject, provisioner.SSHHostCert,
tokAttrs.sans, tokAttrs.certNotBefore, tokAttrs.certNotAfter)
default:
return tokenGen.Token(tokAttrs.subject)
}
}
func generateRenewToken(ctx *cli.Context, aud, sub string) (string, error) {
renewCert := ctx.String("x5c-cert")
if renewCert == "" {
return "", errs.RequiredFlag(ctx, "x5c-cert")
}
renewKey := ctx.String("x5c-key")
if renewKey == "" {
return "", errs.RequiredFlag(ctx, "x5c-key")
}
bundle, err := pemutil.ReadCertificateBundle(renewCert)
if err != nil {
return "", err
}
if len(bundle) == 0 {
return "", errs.InvalidFlagValueMsg(ctx, "--x5c-cert", renewCert, "certificate not found")
}
key, err := pemutil.Read(renewKey)
if err != nil {
return "", err
}
if sub != "" && sub != bundle[0].Subject.CommonName {
return "", errors.Errorf("positional argument <subject> must match the certificate common name")
}
claims, err := token.NewClaims(
token.WithAudience(aud),
token.WithIssuer("step-ca-client/1.0"),
token.WithSubject(bundle[0].Subject.CommonName),
)
if err != nil {
return "", errors.Wrap(err, "error creating renew token")
}
var x5c []string
for _, crt := range bundle {
x5c = append(x5c, base64.StdEncoding.EncodeToString(crt.Raw))
}
if claims.ExtraHeaders == nil {
claims.ExtraHeaders = make(map[string]interface{})
}
claims.ExtraHeaders[jose.X5cInsecureKey] = x5c
tok, err := claims.Sign("", key)
if err != nil {
return "", errors.Wrap(err, "error creating renew token")
}
return tok, nil
}
func getSigningAlgorithm(pub crypto.PublicKey) (string, error) {
switch k := pub.(type) {
case *ecdsa.PublicKey:
switch k.Curve {
case elliptic.P256():
return jose.ES256, nil
case elliptic.P384():
return jose.ES384, nil
case elliptic.P521():
return jose.ES512, nil
default:
return "", fmt.Errorf("unsupported public key type ECDSA %s", k.Curve.Params().Name)
}
case *rsa.PublicKey:
return jose.DefaultRSASigAlgorithm, nil
case ed25519.PublicKey:
return jose.EdDSA, nil
default:
return "", fmt.Errorf("unsupported public key type %T", k)
}
}