| 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 filetoken |
| 18 | |
| 19 | import ( |
| 20 | "context" |
| 21 | "crypto" |
| 22 | "crypto/rand" |
| 23 | "crypto/x509" |
| 24 | "fmt" |
| 25 | "io" |
| 26 | "io/ioutil" |
| 27 | |
| 28 | "github.com/sassoftware/relic/v7/config" |
| 29 | "github.com/sassoftware/relic/v7/lib/certloader" |
| 30 | "github.com/sassoftware/relic/v7/lib/passprompt" |
| 31 | "github.com/sassoftware/relic/v7/token" |
| 32 | ) |
| 33 | |
| 34 | const tokenType = "file" |
| 35 | |
| 36 | func init() { |
| 37 | token.Openers[tokenType] = Open |
| 38 | } |
| 39 | |
| 40 | type fileToken struct { |
| 41 | config *config.Config |
| 42 | tokenConf *config.TokenConfig |
| 43 | prompt passprompt.PasswordGetter |
| 44 | } |
| 45 | |
| 46 | type fileKey struct { |
| 47 | keyConf *config.KeyConfig |
| 48 | signer crypto.Signer |
| 49 | cert []byte |
| 50 | } |
| 51 | |
| 52 | func Open(conf *config.Config, tokenName string, prompt passprompt.PasswordGetter) (token.Token, error) { |
| 53 | tconf, err := conf.GetToken(tokenName) |
| 54 | if err != nil { |
| 55 | return nil, err |
| 56 | } |
| 57 | return &fileToken{ |
| 58 | config: conf, |
| 59 | tokenConf: tconf, |
| 60 | prompt: prompt, |
| 61 | }, nil |
| 62 | } |
| 63 | |
| 64 | func (tok *fileToken) Ping(context.Context) error { |
| 65 | return nil |
| 66 | } |
| 67 | |
| 68 | func (tok *fileToken) Close() error { |
| 69 | return nil |
| 70 | } |
| 71 | |
| 72 | func (tok *fileToken) Config() *config.TokenConfig { |
| 73 | return tok.tokenConf |
| 74 | } |
| 75 | |
| 76 | func (tok *fileToken) ListKeys(opts token.ListOptions) error { |
| 77 | return token.NotImplementedError{Op: "list-keys", Type: tokenType} |
| 78 | } |
| 79 | |
| 80 | func (tok *fileToken) GetKey(ctx context.Context, keyName string) (token.Key, error) { |
| 81 | keyConf, err := tok.config.GetKey(keyName) |
| 82 | if err != nil { |
| 83 | return nil, err |
| 84 | } |
| 85 | if keyConf.KeyFile == "" { |
| 86 | return nil, fmt.Errorf("key \"%s\" needs a KeyFile setting", keyName) |
| 87 | } |
| 88 | blob, err := ioutil.ReadFile(keyConf.KeyFile) |
| 89 | if err != nil { |
| 90 | return nil, err |
| 91 | } |
| 92 | /* TODO: keyring support |
| 93 | loginFunc := func(pin string) (bool, error) { |
| 94 | keyringUser := fmt.Sprintf("%s.%s", tok.tokenConf.Name(), keyName) |
| 95 | if savedPass != "" { |
| 96 | tok.mu.Lock() |
| 97 | defer tok.mu.Unlock() |
| 98 | if err := token.Login(tok.tokenConf, tok.prompt, loginFunc, keyringUser, ""); err != nil { |
| 99 | return nil, err |
| 100 | } |
| 101 | } |
| 102 | */ |
| 103 | var privateKey crypto.PrivateKey |
| 104 | var certBlob []byte |
| 105 | if keyConf.IsPkcs12 { |
| 106 | cert, err := certloader.ParsePKCS12(blob, tok.prompt) |
| 107 | if err != nil { |
| 108 | return nil, err |
| 109 | } |
| 110 | privateKey = cert.PrivateKey |
| 111 | for _, oneCert := range cert.Chain() { |
| 112 | certBlob = append(certBlob, oneCert.Raw...) |
| 113 | } |
| 114 | } else { |
| 115 | var err error |
| 116 | privateKey, err = certloader.ParseAnyPrivateKey(blob, tok.prompt) |
| 117 | if err != nil { |
| 118 | return nil, err |
| 119 | } |
| 120 | } |
| 121 | return &fileKey{ |
| 122 | keyConf: keyConf, |
| 123 | signer: privateKey.(crypto.Signer), |
| 124 | cert: certBlob, |
| 125 | }, nil |
| 126 | } |
| 127 | |
| 128 | func (key *fileKey) Public() crypto.PublicKey { |
| 129 | return key.signer.Public() |
| 130 | } |
| 131 | |
| 132 | func (key *fileKey) Sign(rand io.Reader, digest []byte, opts crypto.SignerOpts) ([]byte, error) { |
| 133 | return key.signer.Sign(rand, digest, opts) |
| 134 | } |
| 135 | |
| 136 | func (key *fileKey) SignContext(ctx context.Context, digest []byte, opts crypto.SignerOpts) ([]byte, error) { |
| 137 | return key.signer.Sign(rand.Reader, digest, opts) |
| 138 | } |
| 139 | |
| 140 | func (key *fileKey) Config() *config.KeyConfig { |
| 141 | return key.keyConf |
| 142 | } |
| 143 | |
| 144 | func (key *fileKey) Certificate() []byte { |
| 145 | return key.cert |
| 146 | } |
| 147 | |
| 148 | func (key *fileKey) GetID() []byte { |
| 149 | return nil |
| 150 | } |
| 151 | |
| 152 | func (tok *fileToken) Import(keyName string, privKey crypto.PrivateKey) (token.Key, error) { |
| 153 | return nil, token.NotImplementedError{Op: "import-key", Type: tokenType} |
| 154 | } |
| 155 | |
| 156 | func (tok *fileToken) ImportCertificate(cert *x509.Certificate, labelBase string) error { |
| 157 | return token.NotImplementedError{Op: "import-certificate", Type: tokenType} |
| 158 | } |
| 159 | |
| 160 | func (tok *fileToken) Generate(keyName string, keyType token.KeyType, bits uint) (token.Key, error) { |
| 161 | // TODO - probably useful |
| 162 | return nil, token.NotImplementedError{Op: "generate-key", Type: tokenType} |
| 163 | } |
| 164 | |
| 165 | func (key *fileKey) ImportCertificate(cert *x509.Certificate) error { |
| 166 | return token.NotImplementedError{Op: "import-certificate", Type: tokenType} |
| 167 | } |