| 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 | "crypto" |
| 22 | "crypto/x509" |
| 23 | "fmt" |
| 24 | "io" |
| 25 | |
| 26 | "github.com/sassoftware/relic/v7/config" |
| 27 | ) |
| 28 | |
| 29 | type KeyType uint |
| 30 | |
| 31 | const ( |
| 32 | // Values match CKK_RSA etc. |
| 33 | KeyTypeRsa KeyType = 0 |
| 34 | KeyTypeEcdsa KeyType = 3 |
| 35 | ) |
| 36 | |
| 37 | type Token interface { |
| 38 | io.Closer |
| 39 | // Check that the token is still alive |
| 40 | Ping(ctx context.Context) error |
| 41 | // Return the token config object used to instantiate this token |
| 42 | Config() *config.TokenConfig |
| 43 | // Get a key from the token by its config alias |
| 44 | GetKey(ctx context.Context, keyName string) (Key, error) |
| 45 | // Import a public+private keypair into the token |
| 46 | Import(keyName string, privKey crypto.PrivateKey) (Key, error) |
| 47 | // Import an issuer certificate into the token. The new object label will |
| 48 | // be labelBase plus the fingerprint of the certificate. |
| 49 | ImportCertificate(cert *x509.Certificate, labelBase string) error |
| 50 | // Generate a new key in the token |
| 51 | Generate(keyName string, keyType KeyType, bits uint) (Key, error) |
| 52 | // Print key info |
| 53 | ListKeys(opts ListOptions) error |
| 54 | } |
| 55 | |
| 56 | type Key interface { |
| 57 | crypto.Signer |
| 58 | SignContext(context.Context, []byte, crypto.SignerOpts) ([]byte, error) |
| 59 | // Return the key config object used to instantiate this key |
| 60 | Config() *config.KeyConfig |
| 61 | // Return the X509 certificate chain stored in the token, if any |
| 62 | Certificate() []byte |
| 63 | // Get the CKK_ID or equivalent for the key |
| 64 | GetID() []byte |
| 65 | // Import a leaf certificate for this key |
| 66 | ImportCertificate(cert *x509.Certificate) error |
| 67 | } |
| 68 | |
| 69 | type ListOptions struct { |
| 70 | // Destination stream |
| 71 | Output io.Writer |
| 72 | // Filter by attributes |
| 73 | Label string |
| 74 | ID string |
| 75 | // Print key and certificate contents |
| 76 | Values bool |
| 77 | } |
| 78 | |
| 79 | type NotImplementedError struct { |
| 80 | Op, Type string |
| 81 | } |
| 82 | |
| 83 | func (e NotImplementedError) Error() string { |
| 84 | return fmt.Sprintf("operation %s not implemented for tokens of type %s", e.Op, e.Type) |
| 85 | } |
| 86 | |
| 87 | type KeyUsageError struct { |
| 88 | Key string |
| 89 | Err error |
| 90 | } |
| 91 | |
| 92 | func (e KeyUsageError) Error() string { |
| 93 | return fmt.Sprintf("key %q: %+v", e.Key, e.Err) |
| 94 | } |
| 95 | |
| 96 | func (e KeyUsageError) Unwrap() error { |
| 97 | return e.Err |
| 98 | } |