| 1 | // |
| 2 | // Copyright (c) SAS Institute Inc. |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | // |
| 16 | |
| 17 | package servecmd |
| 18 | |
| 19 | import ( |
| 20 | "crypto" |
| 21 | "crypto/ecdsa" |
| 22 | "crypto/rand" |
| 23 | "crypto/rsa" |
| 24 | "crypto/x509" |
| 25 | "encoding/pem" |
| 26 | "errors" |
| 27 | "fmt" |
| 28 | "io/ioutil" |
| 29 | "os" |
| 30 | |
| 31 | "github.com/spf13/cobra" |
| 32 | |
| 33 | "github.com/sassoftware/relic/v7/cmdline/shared" |
| 34 | "github.com/sassoftware/relic/v7/lib/certloader" |
| 35 | "github.com/sassoftware/relic/v7/lib/x509tools" |
| 36 | ) |
| 37 | |
| 38 | var SetupCmd = &cobra.Command{ |
| 39 | Use: "setup", |
| 40 | Short: "Generate private key and certificate request for server", |
| 41 | RunE: setupCmd, |
| 42 | } |
| 43 | |
| 44 | var ( |
| 45 | argRsaBits uint |
| 46 | argEcdsaBits uint |
| 47 | argSelfSign bool |
| 48 | ) |
| 49 | |
| 50 | func init() { |
| 51 | SetupCmd.Flags().UintVar(&argRsaBits, "generate-rsa", 0, "Generate a RSA key of the specified bit size, if needed") |
| 52 | SetupCmd.Flags().UintVar(&argEcdsaBits, "generate-ecdsa", 0, "Generate an ECDSA key of the specified curve size, if needed") |
| 53 | SetupCmd.Flags().BoolVar(&argSelfSign, "self-sign", false, "Make and store a self-signed certificate instead of a request") |
| 54 | x509tools.AddRequestFlags(SetupCmd) |
| 55 | ServeCmd.AddCommand(SetupCmd) |
| 56 | } |
| 57 | |
| 58 | func readKey(path string) (crypto.PrivateKey, error) { |
| 59 | pemData, err := ioutil.ReadFile(path) |
| 60 | if err != nil { |
| 61 | return nil, err |
| 62 | } |
| 63 | return certloader.ParseAnyPrivateKey(pemData, nil) |
| 64 | } |
| 65 | |
| 66 | func selectOrGenerate(path string) (crypto.PrivateKey, error) { |
| 67 | key, err := readKey(path) |
| 68 | if err == nil { |
| 69 | fmt.Fprintf(os.Stderr, "Using existing private key at %s\n", path) |
| 70 | return key, nil |
| 71 | } else if !os.IsNotExist(err) { |
| 72 | return nil, fmt.Errorf("reading private key: %w", err) |
| 73 | } |
| 74 | var block *pem.Block |
| 75 | if argRsaBits != 0 { |
| 76 | rsaKey, err := rsa.GenerateKey(rand.Reader, int(argRsaBits)) |
| 77 | if err != nil { |
| 78 | return nil, err |
| 79 | } |
| 80 | key = rsaKey |
| 81 | keyBytes := x509.MarshalPKCS1PrivateKey(rsaKey) |
| 82 | block = &pem.Block{Type: "RSA PRIVATE KEY", Bytes: keyBytes} |
| 83 | } else if argEcdsaBits != 0 { |
| 84 | curve, err := x509tools.CurveByBits(argEcdsaBits) |
| 85 | if err != nil { |
| 86 | return nil, err |
| 87 | } |
| 88 | ecKey, err := ecdsa.GenerateKey(curve.Curve, rand.Reader) |
| 89 | if err != nil { |
| 90 | return nil, err |
| 91 | } |
| 92 | key = ecKey |
| 93 | keyBytes, err := x509.MarshalECPrivateKey(ecKey) |
| 94 | if err != nil { |
| 95 | return nil, err |
| 96 | } |
| 97 | block = &pem.Block{Type: "EC PRIVATE KEY", Bytes: keyBytes} |
| 98 | } else { |
| 99 | return nil, errors.New("No matching key exists, specify --generate-rsa or --generate-ecdsa to generate one") |
| 100 | } |
| 101 | pemData := pem.EncodeToMemory(block) |
| 102 | if err := ioutil.WriteFile(path, pemData, 0600); err != nil { |
| 103 | return nil, fmt.Errorf("writing new private key: %w", err) |
| 104 | } |
| 105 | fmt.Fprintf(os.Stderr, "Wrote new private key to %s\n", path) |
| 106 | return key, nil |
| 107 | } |
| 108 | |
| 109 | func setupCmd(cmd *cobra.Command, args []string) error { |
| 110 | if err := shared.InitConfig(); err != nil { |
| 111 | return err |
| 112 | } |
| 113 | if shared.CurrentConfig.Server == nil { |
| 114 | return errors.New("Missing server section in configuration file") |
| 115 | } |
| 116 | if shared.CurrentConfig.Server.KeyFile == "" { |
| 117 | return errors.New("Missing keyfile option in server configuration file") |
| 118 | } |
| 119 | if argSelfSign && shared.CurrentConfig.Server.KeyFile == "" { |
| 120 | return errors.New("Missing certfile option in server configuration file") |
| 121 | } |
| 122 | if x509tools.ArgCommonName == "" { |
| 123 | return errors.New("--commonName is required") |
| 124 | } |
| 125 | if x509tools.ArgDNSNames == "" && x509tools.ArgEmailNames == "" { |
| 126 | fmt.Fprintf(os.Stderr, "Subject alternate names is empty; appending %s\n", x509tools.ArgCommonName) |
| 127 | x509tools.ArgDNSNames = x509tools.ArgCommonName |
| 128 | } |
| 129 | key, err := selectOrGenerate(shared.CurrentConfig.Server.KeyFile) |
| 130 | if err != nil { |
| 131 | return err |
| 132 | } |
| 133 | signer, ok := key.(crypto.Signer) |
| 134 | if !ok { |
| 135 | panic("expected a signer") |
| 136 | } |
| 137 | if argSelfSign { |
| 138 | x509tools.ArgExpireDays = 36525 |
| 139 | x509tools.ArgCertAuthority = true |
| 140 | cert, err := x509tools.MakeCertificate(rand.Reader, signer) |
| 141 | if err != nil { |
| 142 | return err |
| 143 | } |
| 144 | err = ioutil.WriteFile(shared.CurrentConfig.Server.CertFile, []byte(cert), 0600) |
| 145 | if err != nil { |
| 146 | return err |
| 147 | } |
| 148 | } else { |
| 149 | req, err := x509tools.MakeRequest(rand.Reader, signer) |
| 150 | if err != nil { |
| 151 | return err |
| 152 | } |
| 153 | os.Stdout.WriteString(req) |
| 154 | } |
| 155 | return nil |
| 156 | } |