123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347 |
- package main
- import (
- "crypto/ecdh"
- "crypto/rand"
- "flag"
- "fmt"
- "io"
- "net"
- "os"
- "strings"
- "time"
- "github.com/skip2/go-qrcode"
- "github.com/slackhq/nebula/cert"
- "github.com/slackhq/nebula/pkclient"
- "golang.org/x/crypto/curve25519"
- )
- type signFlags struct {
- set *flag.FlagSet
- caKeyPath *string
- caCertPath *string
- name *string
- ip *string
- duration *time.Duration
- inPubPath *string
- outKeyPath *string
- outCertPath *string
- outQRPath *string
- groups *string
- subnets *string
- p11url *string
- }
- func newSignFlags() *signFlags {
- sf := signFlags{set: flag.NewFlagSet("sign", flag.ContinueOnError)}
- sf.set.Usage = func() {}
- sf.caKeyPath = sf.set.String("ca-key", "ca.key", "Optional: path to the signing CA key")
- sf.caCertPath = sf.set.String("ca-crt", "ca.crt", "Optional: path to the signing CA cert")
- sf.name = sf.set.String("name", "", "Required: name of the cert, usually a hostname")
- sf.ip = sf.set.String("ip", "", "Required: ipv4 address and network in CIDR notation to assign the cert")
- sf.duration = sf.set.Duration("duration", 0, "Optional: how long the cert should be valid for. The default is 1 second before the signing cert expires. Valid time units are seconds: \"s\", minutes: \"m\", hours: \"h\"")
- sf.inPubPath = sf.set.String("in-pub", "", "Optional (if out-key not set): path to read a previously generated public key")
- sf.outKeyPath = sf.set.String("out-key", "", "Optional (if in-pub not set): path to write the private key to")
- sf.outCertPath = sf.set.String("out-crt", "", "Optional: path to write the certificate to")
- sf.outQRPath = sf.set.String("out-qr", "", "Optional: output a qr code image (png) of the certificate")
- sf.groups = sf.set.String("groups", "", "Optional: comma separated list of groups")
- sf.subnets = sf.set.String("subnets", "", "Optional: comma separated list of ipv4 address and network in CIDR notation. Subnets this cert can serve for")
- sf.p11url = p11Flag(sf.set)
- return &sf
- }
- func signCert(args []string, out io.Writer, errOut io.Writer, pr PasswordReader) error {
- sf := newSignFlags()
- err := sf.set.Parse(args)
- if err != nil {
- return err
- }
- isP11 := len(*sf.p11url) > 0
- if !isP11 {
- if err := mustFlagString("ca-key", sf.caKeyPath); err != nil {
- return err
- }
- }
- if err := mustFlagString("ca-crt", sf.caCertPath); err != nil {
- return err
- }
- if err := mustFlagString("name", sf.name); err != nil {
- return err
- }
- if err := mustFlagString("ip", sf.ip); err != nil {
- return err
- }
- if !isP11 && *sf.inPubPath != "" && *sf.outKeyPath != "" {
- return newHelpErrorf("cannot set both -in-pub and -out-key")
- }
- var curve cert.Curve
- var caKey []byte
- if !isP11 {
- var rawCAKey []byte
- rawCAKey, err := os.ReadFile(*sf.caKeyPath)
- if err != nil {
- return fmt.Errorf("error while reading ca-key: %s", err)
- }
- // naively attempt to decode the private key as though it is not encrypted
- caKey, _, curve, err = cert.UnmarshalSigningPrivateKey(rawCAKey)
- if err == cert.ErrPrivateKeyEncrypted {
- // ask for a passphrase until we get one
- var passphrase []byte
- for i := 0; i < 5; i++ {
- out.Write([]byte("Enter passphrase: "))
- passphrase, err = pr.ReadPassword()
- if err == ErrNoTerminal {
- return fmt.Errorf("ca-key is encrypted and must be decrypted interactively")
- } else if err != nil {
- return fmt.Errorf("error reading password: %s", err)
- }
- if len(passphrase) > 0 {
- break
- }
- }
- if len(passphrase) == 0 {
- return fmt.Errorf("cannot open encrypted ca-key without passphrase")
- }
- curve, caKey, _, err = cert.DecryptAndUnmarshalSigningPrivateKey(passphrase, rawCAKey)
- if err != nil {
- return fmt.Errorf("error while parsing encrypted ca-key: %s", err)
- }
- } else if err != nil {
- return fmt.Errorf("error while parsing ca-key: %s", err)
- }
- }
- rawCACert, err := os.ReadFile(*sf.caCertPath)
- if err != nil {
- return fmt.Errorf("error while reading ca-crt: %s", err)
- }
- caCert, _, err := cert.UnmarshalNebulaCertificateFromPEM(rawCACert)
- if err != nil {
- return fmt.Errorf("error while parsing ca-crt: %s", err)
- }
- if !isP11 {
- if err := caCert.VerifyPrivateKey(curve, caKey); err != nil {
- return fmt.Errorf("refusing to sign, root certificate does not match private key")
- }
- }
- issuer, err := caCert.Sha256Sum()
- if err != nil {
- return fmt.Errorf("error while getting -ca-crt fingerprint: %s", err)
- }
- if caCert.Expired(time.Now()) {
- return fmt.Errorf("ca certificate is expired")
- }
- // if no duration is given, expire one second before the root expires
- if *sf.duration <= 0 {
- *sf.duration = time.Until(caCert.Details.NotAfter) - time.Second*1
- }
- ip, ipNet, err := net.ParseCIDR(*sf.ip)
- if err != nil {
- return newHelpErrorf("invalid ip definition: %s", err)
- }
- if ip.To4() == nil {
- return newHelpErrorf("invalid ip definition: can only be ipv4, have %s", *sf.ip)
- }
- ipNet.IP = ip
- groups := []string{}
- if *sf.groups != "" {
- for _, rg := range strings.Split(*sf.groups, ",") {
- g := strings.TrimSpace(rg)
- if g != "" {
- groups = append(groups, g)
- }
- }
- }
- subnets := []*net.IPNet{}
- if *sf.subnets != "" {
- for _, rs := range strings.Split(*sf.subnets, ",") {
- rs := strings.Trim(rs, " ")
- if rs != "" {
- _, s, err := net.ParseCIDR(rs)
- if err != nil {
- return newHelpErrorf("invalid subnet definition: %s", err)
- }
- if s.IP.To4() == nil {
- return newHelpErrorf("invalid subnet definition: can only be ipv4, have %s", rs)
- }
- subnets = append(subnets, s)
- }
- }
- }
- var pub, rawPriv []byte
- var p11Client *pkclient.PKClient
- if isP11 {
- curve = cert.Curve_P256
- p11Client, err = pkclient.FromUrl(*sf.p11url)
- if err != nil {
- return fmt.Errorf("error while creating PKCS#11 client: %w", err)
- }
- defer func(client *pkclient.PKClient) {
- _ = client.Close()
- }(p11Client)
- }
- if *sf.inPubPath != "" {
- var pubCurve cert.Curve
- rawPub, err := os.ReadFile(*sf.inPubPath)
- if err != nil {
- return fmt.Errorf("error while reading in-pub: %s", err)
- }
- pub, _, pubCurve, err = cert.UnmarshalPublicKey(rawPub)
- if err != nil {
- return fmt.Errorf("error while parsing in-pub: %s", err)
- }
- if pubCurve != curve {
- return fmt.Errorf("curve of in-pub does not match ca")
- }
- } else if isP11 {
- pub, err = p11Client.GetPubKey()
- if err != nil {
- return fmt.Errorf("error while getting public key with PKCS#11: %w", err)
- }
- } else {
- pub, rawPriv = newKeypair(curve)
- }
- nc := cert.NebulaCertificate{
- Details: cert.NebulaCertificateDetails{
- Name: *sf.name,
- Ips: []*net.IPNet{ipNet},
- Groups: groups,
- Subnets: subnets,
- NotBefore: time.Now(),
- NotAfter: time.Now().Add(*sf.duration),
- PublicKey: pub,
- IsCA: false,
- Issuer: issuer,
- Curve: curve,
- },
- Pkcs11Backed: isP11,
- }
- if p11Client == nil {
- err = nc.Sign(curve, caKey)
- if err != nil {
- return fmt.Errorf("error while signing: %w", err)
- }
- } else {
- err = nc.SignPkcs11(curve, p11Client)
- if err != nil {
- return fmt.Errorf("error while signing with PKCS#11: %w", err)
- }
- }
- if err := nc.CheckRootConstrains(caCert); err != nil {
- return fmt.Errorf("refusing to sign, root certificate constraints violated: %s", err)
- }
- if *sf.outKeyPath == "" {
- *sf.outKeyPath = *sf.name + ".key"
- }
- if *sf.outCertPath == "" {
- *sf.outCertPath = *sf.name + ".crt"
- }
- if _, err := os.Stat(*sf.outCertPath); err == nil {
- return fmt.Errorf("refusing to overwrite existing cert: %s", *sf.outCertPath)
- }
- if !isP11 && *sf.inPubPath == "" {
- if _, err := os.Stat(*sf.outKeyPath); err == nil {
- return fmt.Errorf("refusing to overwrite existing key: %s", *sf.outKeyPath)
- }
- err = os.WriteFile(*sf.outKeyPath, cert.MarshalPrivateKey(curve, rawPriv), 0600)
- if err != nil {
- return fmt.Errorf("error while writing out-key: %s", err)
- }
- }
- b, err := nc.MarshalToPEM()
- if err != nil {
- return fmt.Errorf("error while marshalling certificate: %s", err)
- }
- err = os.WriteFile(*sf.outCertPath, b, 0600)
- if err != nil {
- return fmt.Errorf("error while writing out-crt: %s", err)
- }
- if *sf.outQRPath != "" {
- b, err = qrcode.Encode(string(b), qrcode.Medium, -5)
- if err != nil {
- return fmt.Errorf("error while generating qr code: %s", err)
- }
- err = os.WriteFile(*sf.outQRPath, b, 0600)
- if err != nil {
- return fmt.Errorf("error while writing out-qr: %s", err)
- }
- }
- return nil
- }
- func newKeypair(curve cert.Curve) ([]byte, []byte) {
- switch curve {
- case cert.Curve_CURVE25519:
- return x25519Keypair()
- case cert.Curve_P256:
- return p256Keypair()
- default:
- return nil, nil
- }
- }
- func x25519Keypair() ([]byte, []byte) {
- privkey := make([]byte, 32)
- if _, err := io.ReadFull(rand.Reader, privkey); err != nil {
- panic(err)
- }
- pubkey, err := curve25519.X25519(privkey, curve25519.Basepoint)
- if err != nil {
- panic(err)
- }
- return pubkey, privkey
- }
- func p256Keypair() ([]byte, []byte) {
- privkey, err := ecdh.P256().GenerateKey(rand.Reader)
- if err != nil {
- panic(err)
- }
- pubkey := privkey.PublicKey()
- return pubkey.Bytes(), privkey.Bytes()
- }
- func signSummary() string {
- return "sign <flags>: create and sign a certificate"
- }
- func signHelp(out io.Writer) {
- sf := newSignFlags()
- out.Write([]byte("Usage of " + os.Args[0] + " " + signSummary() + "\n"))
- sf.set.SetOutput(out)
- sf.set.PrintDefaults()
- }
|