| 1 | package awstoken |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "crypto" |
| 6 | "crypto/ecdsa" |
| 7 | "crypto/rsa" |
| 8 | "crypto/x509" |
| 9 | "fmt" |
| 10 | "io" |
| 11 | |
| 12 | awsconfig "github.com/aws/aws-sdk-go-v2/config" |
| 13 | "github.com/aws/aws-sdk-go-v2/service/kms" |
| 14 | "github.com/aws/aws-sdk-go-v2/service/kms/types" |
| 15 | |
| 16 | "github.com/sassoftware/relic/v7/config" |
| 17 | "github.com/sassoftware/relic/v7/lib/passprompt" |
| 18 | "github.com/sassoftware/relic/v7/token" |
| 19 | ) |
| 20 | |
| 21 | const tokenType = "aws" |
| 22 | |
| 23 | type awsToken struct { |
| 24 | config *config.Config |
| 25 | tconf *config.TokenConfig |
| 26 | cli *kms.Client |
| 27 | } |
| 28 | |
| 29 | type awsKey struct { |
| 30 | kconf *config.KeyConfig |
| 31 | cli *kms.Client |
| 32 | pub crypto.PublicKey |
| 33 | } |
| 34 | |
| 35 | func init() { |
| 36 | token.Openers[tokenType] = open |
| 37 | } |
| 38 | |
| 39 | func open(conf *config.Config, tokenName string, pinProvider passprompt.PasswordGetter) (token.Token, error) { |
| 40 | tconf, err := conf.GetToken(tokenName) |
| 41 | if err != nil { |
| 42 | return nil, err |
| 43 | } |
| 44 | cfg, err := awsconfig.LoadDefaultConfig(context.Background()) |
| 45 | if err != nil { |
| 46 | return nil, err |
| 47 | } |
| 48 | cli := kms.NewFromConfig(cfg) |
| 49 | return &awsToken{ |
| 50 | config: conf, |
| 51 | tconf: tconf, |
| 52 | cli: cli, |
| 53 | }, nil |
| 54 | } |
| 55 | |
| 56 | func (t *awsToken) Close() error { |
| 57 | return nil |
| 58 | } |
| 59 | |
| 60 | func (t *awsToken) Ping(ctx context.Context) error { |
| 61 | // TODO |
| 62 | return nil |
| 63 | } |
| 64 | |
| 65 | func (t *awsToken) Config() *config.TokenConfig { |
| 66 | return t.tconf |
| 67 | } |
| 68 | |
| 69 | func (t *awsToken) GetKey(ctx context.Context, keyName string) (token.Key, error) { |
| 70 | keyConf, err := t.config.GetKey(keyName) |
| 71 | if err != nil { |
| 72 | return nil, err |
| 73 | } |
| 74 | if keyConf.ID == "" { |
| 75 | return nil, fmt.Errorf("key %q must have \"id\" set to the ID or ARN of the key", keyName) |
| 76 | } |
| 77 | id := keyConf.ID |
| 78 | resp, err := t.cli.GetPublicKey(ctx, &kms.GetPublicKeyInput{KeyId: &id}) |
| 79 | if err != nil { |
| 80 | return nil, err |
| 81 | } |
| 82 | pub, err := x509.ParsePKIXPublicKey(resp.PublicKey) |
| 83 | if err != nil { |
| 84 | return nil, err |
| 85 | } |
| 86 | return &awsKey{ |
| 87 | kconf: keyConf, |
| 88 | cli: t.cli, |
| 89 | pub: pub, |
| 90 | }, nil |
| 91 | } |
| 92 | |
| 93 | func (t *awsToken) Import(keyName string, privKey crypto.PrivateKey) (token.Key, error) { |
| 94 | return nil, token.NotImplementedError{Op: "import-key", Type: tokenType} |
| 95 | } |
| 96 | |
| 97 | func (t *awsToken) ImportCertificate(cert *x509.Certificate, labelBase string) error { |
| 98 | return token.NotImplementedError{Op: "import-certificate", Type: tokenType} |
| 99 | } |
| 100 | |
| 101 | func (t *awsToken) Generate(keyName string, keyType token.KeyType, bits uint) (token.Key, error) { |
| 102 | return nil, token.NotImplementedError{Op: "generate-key", Type: tokenType} |
| 103 | } |
| 104 | |
| 105 | func (t *awsToken) ListKeys(opts token.ListOptions) error { |
| 106 | return token.NotImplementedError{Op: "list-keys", Type: tokenType} |
| 107 | } |
| 108 | |
| 109 | func (k *awsKey) Public() crypto.PublicKey { |
| 110 | return k.pub |
| 111 | } |
| 112 | |
| 113 | func (k *awsKey) Sign(rand io.Reader, digest []byte, opts crypto.SignerOpts) (signature []byte, err error) { |
| 114 | return k.SignContext(context.Background(), digest, opts) |
| 115 | } |
| 116 | |
| 117 | func (k *awsKey) SignContext(ctx context.Context, digest []byte, opts crypto.SignerOpts) (signature []byte, err error) { |
| 118 | alg, err := k.sigAlgorithm(opts) |
| 119 | if err != nil { |
| 120 | return nil, err |
| 121 | } |
| 122 | id := k.kconf.ID |
| 123 | resp, err := k.cli.Sign(ctx, &kms.SignInput{ |
| 124 | KeyId: &id, |
| 125 | Message: digest, |
| 126 | SigningAlgorithm: types.SigningAlgorithmSpec(alg), |
| 127 | MessageType: types.MessageTypeDigest, |
| 128 | }) |
| 129 | if err != nil { |
| 130 | return nil, err |
| 131 | } |
| 132 | return resp.Signature, nil |
| 133 | } |
| 134 | |
| 135 | func (k *awsKey) Config() *config.KeyConfig { |
| 136 | return k.kconf |
| 137 | } |
| 138 | |
| 139 | func (k *awsKey) Certificate() []byte { |
| 140 | return nil |
| 141 | } |
| 142 | |
| 143 | func (k *awsKey) GetID() []byte { |
| 144 | return nil |
| 145 | } |
| 146 | |
| 147 | func (k *awsKey) ImportCertificate(cert *x509.Certificate) error { |
| 148 | return token.NotImplementedError{Op: "import-certificate", Type: tokenType} |
| 149 | } |
| 150 | |
| 151 | func (k *awsKey) sigAlgorithm(opts crypto.SignerOpts) (string, error) { |
| 152 | var alg string |
| 153 | switch opts.HashFunc() { |
| 154 | case crypto.SHA256: |
| 155 | alg = "SHA_256" |
| 156 | case crypto.SHA384: |
| 157 | alg = "SHA_384" |
| 158 | case crypto.SHA512: |
| 159 | alg = "SHA_512" |
| 160 | default: |
| 161 | return "", token.KeyUsageError{ |
| 162 | Key: k.kconf.Name(), |
| 163 | Err: fmt.Errorf("unsupported digest algorithm %s", opts.HashFunc()), |
| 164 | } |
| 165 | } |
| 166 | switch k.pub.(type) { |
| 167 | case *rsa.PublicKey: |
| 168 | if _, ok := opts.(*rsa.PSSOptions); ok { |
| 169 | return "RSASSA_PSS_" + alg, nil |
| 170 | } else { |
| 171 | return "RSASSA_PKCS1_V1_5_" + alg, nil |
| 172 | } |
| 173 | case *ecdsa.PublicKey: |
| 174 | return "ECDSA_" + alg, nil |
| 175 | default: |
| 176 | return "", token.KeyUsageError{ |
| 177 | Key: k.kconf.Name(), |
| 178 | Err: fmt.Errorf("unsupported public key type %T", k.pub), |
| 179 | } |
| 180 | } |
| 181 | } |