| 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 token |
| 18 | |
| 19 | import ( |
| 20 | "context" |
| 21 | "errors" |
| 22 | "fmt" |
| 23 | "io/ioutil" |
| 24 | "os" |
| 25 | |
| 26 | "github.com/spf13/cobra" |
| 27 | |
| 28 | "github.com/sassoftware/relic/v7/cmdline/shared" |
| 29 | "github.com/sassoftware/relic/v7/lib/certloader" |
| 30 | "github.com/sassoftware/relic/v7/lib/passprompt" |
| 31 | "github.com/sassoftware/relic/v7/lib/x509tools" |
| 32 | "github.com/sassoftware/relic/v7/signers/sigerrors" |
| 33 | ) |
| 34 | |
| 35 | var ImportKeyCmd = &cobra.Command{ |
| 36 | Use: "import-key", |
| 37 | Short: "Import a private key to a token", |
| 38 | RunE: importKeyCmd, |
| 39 | } |
| 40 | |
| 41 | var argPkcs12 bool |
| 42 | |
| 43 | func init() { |
| 44 | shared.RootCmd.AddCommand(ImportKeyCmd) |
| 45 | addKeyFlags(ImportKeyCmd) |
| 46 | ImportKeyCmd.Flags().StringVarP(&argToken, "token", "t", "", "Name of token to import key to") |
| 47 | ImportKeyCmd.Flags().StringVarP(&argLabel, "label", "l", "", "Label to attach to imported key") |
| 48 | ImportKeyCmd.Flags().StringVarP(&argFile, "file", "f", "", "Private key file to import: PEM, DER, or PGP") |
| 49 | ImportKeyCmd.Flags().BoolVar(&argPkcs12, "pkcs12", false, "Import a PKCS12 key and certificate chain") |
| 50 | } |
| 51 | |
| 52 | func importKeyCmd(cmd *cobra.Command, args []string) error { |
| 53 | if argFile == "" { |
| 54 | return errors.New("--file is required") |
| 55 | } |
| 56 | blob, err := ioutil.ReadFile(argFile) |
| 57 | if err != nil { |
| 58 | return shared.Fail(err) |
| 59 | } |
| 60 | prompt := new(passprompt.PasswordPrompt) |
| 61 | var cert *certloader.Certificate |
| 62 | if argPkcs12 { |
| 63 | var err error |
| 64 | cert, err = certloader.ParsePKCS12(blob, prompt) |
| 65 | if err != nil { |
| 66 | return shared.Fail(err) |
| 67 | } |
| 68 | } else { |
| 69 | privKey, err := certloader.ParseAnyPrivateKey(blob, prompt) |
| 70 | if err != nil { |
| 71 | return shared.Fail(err) |
| 72 | } |
| 73 | cert = &certloader.Certificate{PrivateKey: privKey} |
| 74 | } |
| 75 | keyConf, err := newKeyConfig() |
| 76 | if err != nil { |
| 77 | return err |
| 78 | } |
| 79 | tok, err := openToken(keyConf.Token) |
| 80 | if err != nil { |
| 81 | return shared.Fail(err) |
| 82 | } |
| 83 | var didSomething bool |
| 84 | key, err := tok.GetKey(context.Background(), argKeyName) |
| 85 | if err == nil { |
| 86 | if cert.Leaf == nil { |
| 87 | return errors.New("An object with that label already exists in the token") |
| 88 | } |
| 89 | fmt.Fprintln(os.Stderr, "Private key already exists. Attempting to import certificates.") |
| 90 | } else if _, ok := err.(sigerrors.KeyNotFoundError); !ok { |
| 91 | return err |
| 92 | } else { |
| 93 | key, err = tok.Import(argKeyName, cert.PrivateKey) |
| 94 | if err != nil { |
| 95 | return err |
| 96 | } |
| 97 | didSomething = true |
| 98 | } |
| 99 | if cert.Leaf != nil { |
| 100 | name := x509tools.FormatSubject(cert.Leaf) |
| 101 | err := key.ImportCertificate(cert.Leaf) |
| 102 | if err == sigerrors.ErrExist { |
| 103 | fmt.Fprintln(os.Stderr, "Certificate already exists:", name) |
| 104 | } else if err != nil { |
| 105 | return shared.Fail(fmt.Errorf("importing %s: %w", name, err)) |
| 106 | } else { |
| 107 | fmt.Fprintln(os.Stderr, "Imported", name) |
| 108 | didSomething = true |
| 109 | } |
| 110 | for _, chain := range cert.Chain() { |
| 111 | if chain == cert.Leaf { |
| 112 | continue |
| 113 | } |
| 114 | name = x509tools.FormatSubject(chain) |
| 115 | err = tok.ImportCertificate(chain, keyConf.Label) |
| 116 | if err == sigerrors.ErrExist { |
| 117 | fmt.Fprintln(os.Stderr, "Certificate already exists:", name) |
| 118 | } else if err != nil { |
| 119 | return shared.Fail(fmt.Errorf("importing %s: %w", name, err)) |
| 120 | } else { |
| 121 | fmt.Fprintln(os.Stderr, "Imported", name) |
| 122 | didSomething = true |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | if !didSomething { |
| 127 | return shared.Fail(errors.New("nothing imported")) |
| 128 | } |
| 129 | fmt.Fprintln(os.Stderr, "Token CKA_ID: ", formatKeyID(key.GetID())) |
| 130 | return nil |
| 131 | } |