Debian Codegraph @

cmd/nebula-cert/keygen.go

raw
1 package main
2
3 import (
4 "flag"
5 "fmt"
6 "io"
7 "os"
8
9 "github.com/slackhq/nebula/pkclient"
10
11 "github.com/slackhq/nebula/cert"
12 )
13
14 type keygenFlags struct {
15 set *flag.FlagSet
16 outKeyPath *string
17 outPubPath *string
18 curve *string
19 p11url *string
20 }
21
22 func newKeygenFlags() *keygenFlags {
23 cf := keygenFlags{set: flag.NewFlagSet("keygen", flag.ContinueOnError)}
24 cf.set.Usage = func() {}
25 cf.outPubPath = cf.set.String("out-pub", "", "Required: path to write the public key to")
26 cf.outKeyPath = cf.set.String("out-key", "", "Required: path to write the private key to")
27 cf.curve = cf.set.String("curve", "25519", "ECDH Curve (25519, P256)")
28 cf.p11url = p11Flag(cf.set)
29 return &cf
30 }
31
32 func keygen(args []string, out io.Writer, errOut io.Writer) error {
33 cf := newKeygenFlags()
34 err := cf.set.Parse(args)
35 if err != nil {
36 return err
37 }
38
39 isP11 := len(*cf.p11url) > 0
40
41 if !isP11 {
42 if err = mustFlagString("out-key", cf.outKeyPath); err != nil {
43 return err
44 }
45 } else if *cf.outKeyPath != "" {
46 return newHelpErrorf("cannot set -out-key with -pkcs11")
47 }
48 if err = mustFlagString("out-pub", cf.outPubPath); err != nil {
49 return err
50 }
51
52 var pub, rawPriv []byte
53 var curve cert.Curve
54 if isP11 {
55 switch *cf.curve {
56 case "P256":
57 curve = cert.Curve_P256
58 default:
59 return fmt.Errorf("invalid curve for PKCS#11: %s", *cf.curve)
60 }
61 } else {
62 switch *cf.curve {
63 case "25519", "X25519", "Curve25519", "CURVE25519":
64 pub, rawPriv = x25519Keypair()
65 curve = cert.Curve_CURVE25519
66 case "P256":
67 pub, rawPriv = p256Keypair()
68 curve = cert.Curve_P256
69 default:
70 return fmt.Errorf("invalid curve: %s", *cf.curve)
71 }
72 }
73
74 var claims ioClaims
75 if err := reserveOutputs(&claims,
76 "out-key", *cf.outKeyPath,
77 "out-pub", *cf.outPubPath,
78 ); err != nil {
79 return err
80 }
81
82 if isP11 {
83 p11Client, err := pkclient.FromUrl(*cf.p11url)
84 if err != nil {
85 return fmt.Errorf("error while creating PKCS#11 client: %w", err)
86 }
87 defer func(client *pkclient.PKClient) {
88 _ = client.Close()
89 }(p11Client)
90 pub, err = p11Client.GetPubKey()
91 if err != nil {
92 return fmt.Errorf("error while getting public key: %w", err)
93 }
94 } else {
95 err = writeOutput(*cf.outKeyPath, cert.MarshalPrivateKeyToPEM(curve, rawPriv), 0600, out)
96 if err != nil {
97 return fmt.Errorf("error while writing out-key: %s", err)
98 }
99 }
100 err = writeOutput(*cf.outPubPath, cert.MarshalPublicKeyToPEM(curve, pub), 0600, out)
101 if err != nil {
102 return fmt.Errorf("error while writing out-pub: %s", err)
103 }
104
105 return nil
106 }
107
108 func keygenSummary() string {
109 return "keygen <flags>: create a public/private key pair. the public key can be passed to `nebula-cert sign`"
110 }
111
112 func keygenHelp(out io.Writer) {
113 cf := newKeygenFlags()
114 _, _ = out.Write([]byte("Usage of " + os.Args[0] + " " + keygenSummary() + "\n"))
115 _, _ = out.Write([]byte(stdioHelpText))
116 cf.set.SetOutput(out)
117 cf.set.PrintDefaults()
118 }