| 1 | package gcloudtoken |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "crypto" |
| 6 | "crypto/rsa" |
| 7 | "crypto/x509" |
| 8 | "encoding/pem" |
| 9 | "errors" |
| 10 | "fmt" |
| 11 | "io" |
| 12 | "time" |
| 13 | |
| 14 | kms "cloud.google.com/go/kms/apiv1" |
| 15 | "google.golang.org/api/option" |
| 16 | kmspb "google.golang.org/genproto/googleapis/cloud/kms/v1" |
| 17 | |
| 18 | "github.com/sassoftware/relic/v7/config" |
| 19 | "github.com/sassoftware/relic/v7/lib/passprompt" |
| 20 | "github.com/sassoftware/relic/v7/token" |
| 21 | ) |
| 22 | |
| 23 | const tokenType = "gcloud" |
| 24 | |
| 25 | type gcloudToken struct { |
| 26 | config *config.Config |
| 27 | tconf *config.TokenConfig |
| 28 | cli *kms.KeyManagementClient |
| 29 | } |
| 30 | |
| 31 | type gcloudKey struct { |
| 32 | kconf *config.KeyConfig |
| 33 | cli *kms.KeyManagementClient |
| 34 | pub crypto.PublicKey |
| 35 | hash crypto.Hash |
| 36 | pss bool |
| 37 | } |
| 38 | |
| 39 | func init() { |
| 40 | token.Openers[tokenType] = open |
| 41 | } |
| 42 | |
| 43 | func open(conf *config.Config, tokenName string, pinProvider passprompt.PasswordGetter) (token.Token, error) { |
| 44 | tconf, err := conf.GetToken(tokenName) |
| 45 | if err != nil { |
| 46 | return nil, err |
| 47 | } |
| 48 | ctx, cancel := context.WithTimeout(context.Background(), time.Minute) |
| 49 | defer cancel() |
| 50 | var opts []option.ClientOption |
| 51 | if tconf.Pin != nil { |
| 52 | opts = append(opts, option.WithCredentialsFile(*tconf.Pin)) |
| 53 | } |
| 54 | cli, err := kms.NewKeyManagementClient(ctx, opts...) |
| 55 | if err != nil { |
| 56 | return nil, err |
| 57 | } |
| 58 | return &gcloudToken{ |
| 59 | config: conf, |
| 60 | tconf: tconf, |
| 61 | cli: cli, |
| 62 | }, nil |
| 63 | } |
| 64 | |
| 65 | func (t *gcloudToken) Close() error { |
| 66 | return t.cli.Close() |
| 67 | } |
| 68 | |
| 69 | func (t *gcloudToken) Ping(ctx context.Context) error { |
| 70 | // TODO |
| 71 | return nil |
| 72 | } |
| 73 | |
| 74 | func (t *gcloudToken) Config() *config.TokenConfig { |
| 75 | return t.tconf |
| 76 | } |
| 77 | |
| 78 | func (t *gcloudToken) GetKey(ctx context.Context, keyName string) (token.Key, error) { |
| 79 | keyConf, err := t.config.GetKey(keyName) |
| 80 | if err != nil { |
| 81 | return nil, err |
| 82 | } |
| 83 | if keyConf.ID == "" { |
| 84 | return nil, fmt.Errorf("key %q must have \"id\" set to the fully-quaified resource name of a Cloud KMS key version", keyName) |
| 85 | } |
| 86 | resp, err := t.cli.GetPublicKey(ctx, &kmspb.GetPublicKeyRequest{Name: keyConf.ID}) |
| 87 | if err != nil { |
| 88 | return nil, err |
| 89 | } |
| 90 | hashFunc, pss := pubKeyAlgorithm(resp) |
| 91 | if hashFunc == 0 { |
| 92 | return nil, fmt.Errorf("key %q: unsupported type %q", keyName, resp.Algorithm.String()) |
| 93 | } |
| 94 | block, _ := pem.Decode([]byte(resp.Pem)) |
| 95 | if block == nil { |
| 96 | return nil, errors.New("expected PEM in public key response") |
| 97 | } else if block.Type != "PUBLIC KEY" { |
| 98 | return nil, fmt.Errorf("expected PUBLIC KEY in response but got %q", block.Type) |
| 99 | } |
| 100 | pub, err := x509.ParsePKIXPublicKey(block.Bytes) |
| 101 | if err != nil { |
| 102 | return nil, err |
| 103 | } |
| 104 | return &gcloudKey{ |
| 105 | kconf: keyConf, |
| 106 | cli: t.cli, |
| 107 | pub: pub, |
| 108 | hash: hashFunc, |
| 109 | pss: pss, |
| 110 | }, nil |
| 111 | } |
| 112 | |
| 113 | func (t *gcloudToken) Import(keyName string, privKey crypto.PrivateKey) (token.Key, error) { |
| 114 | return nil, token.NotImplementedError{Op: "import-key", Type: tokenType} |
| 115 | } |
| 116 | |
| 117 | func (t *gcloudToken) ImportCertificate(cert *x509.Certificate, labelBase string) error { |
| 118 | return token.NotImplementedError{Op: "import-certificate", Type: tokenType} |
| 119 | } |
| 120 | |
| 121 | func (t *gcloudToken) Generate(keyName string, keyType token.KeyType, bits uint) (token.Key, error) { |
| 122 | return nil, token.NotImplementedError{Op: "generate-key", Type: tokenType} |
| 123 | } |
| 124 | |
| 125 | func (t *gcloudToken) ListKeys(opts token.ListOptions) error { |
| 126 | return token.NotImplementedError{Op: "list-keys", Type: tokenType} |
| 127 | } |
| 128 | |
| 129 | func (k *gcloudKey) Public() crypto.PublicKey { |
| 130 | return k.pub |
| 131 | } |
| 132 | |
| 133 | func (k *gcloudKey) Sign(rand io.Reader, digest []byte, opts crypto.SignerOpts) (signature []byte, err error) { |
| 134 | return k.SignContext(context.Background(), digest, opts) |
| 135 | } |
| 136 | |
| 137 | func (k *gcloudKey) SignContext(ctx context.Context, digest []byte, opts crypto.SignerOpts) (signature []byte, err error) { |
| 138 | if opts.HashFunc() != k.hash { |
| 139 | return nil, token.KeyUsageError{ |
| 140 | Key: k.kconf.Name(), |
| 141 | Err: fmt.Errorf("tried to use digest %s but key requires digest %s", opts.HashFunc(), k.hash), |
| 142 | } |
| 143 | } |
| 144 | if _, ok := opts.(*rsa.PSSOptions); ok && !k.pss { |
| 145 | return nil, token.KeyUsageError{ |
| 146 | Key: k.kconf.Name(), |
| 147 | Err: errors.New("tried to use RSA-PSS signature but key uses PKCS#1"), |
| 148 | } |
| 149 | } else if k.pss && !ok { |
| 150 | return nil, token.KeyUsageError{ |
| 151 | Key: k.kconf.Name(), |
| 152 | Err: errors.New("tried to use PKCS#1 signature but key uses RSA-PSS"), |
| 153 | } |
| 154 | } |
| 155 | req := &kmspb.AsymmetricSignRequest{ |
| 156 | Name: k.kconf.ID, |
| 157 | Digest: &kmspb.Digest{}, |
| 158 | } |
| 159 | switch k.hash { |
| 160 | case crypto.SHA256: |
| 161 | req.Digest.Digest = &kmspb.Digest_Sha256{Sha256: digest} |
| 162 | case crypto.SHA384: |
| 163 | req.Digest.Digest = &kmspb.Digest_Sha384{Sha384: digest} |
| 164 | case crypto.SHA512: |
| 165 | req.Digest.Digest = &kmspb.Digest_Sha512{Sha512: digest} |
| 166 | default: |
| 167 | return nil, token.KeyUsageError{ |
| 168 | Key: k.kconf.Name(), |
| 169 | Err: fmt.Errorf("unsupported digest algorithm %s", k.hash), |
| 170 | } |
| 171 | } |
| 172 | resp, err := k.cli.AsymmetricSign(ctx, req) |
| 173 | if err != nil { |
| 174 | return nil, err |
| 175 | } |
| 176 | return resp.Signature, nil |
| 177 | } |
| 178 | |
| 179 | func (k *gcloudKey) Config() *config.KeyConfig { |
| 180 | return k.kconf |
| 181 | } |
| 182 | |
| 183 | func (k *gcloudKey) Certificate() []byte { |
| 184 | return nil |
| 185 | } |
| 186 | |
| 187 | func (k *gcloudKey) GetID() []byte { |
| 188 | return nil |
| 189 | } |
| 190 | |
| 191 | func (k *gcloudKey) ImportCertificate(cert *x509.Certificate) error { |
| 192 | return token.NotImplementedError{Op: "import-certificate", Type: tokenType} |
| 193 | } |
| 194 | |
| 195 | func pubKeyAlgorithm(pub *kmspb.PublicKey) (h crypto.Hash, pss bool) { |
| 196 | switch pub.Algorithm { |
| 197 | case kmspb.CryptoKeyVersion_RSA_SIGN_PSS_2048_SHA256: |
| 198 | return crypto.SHA256, true |
| 199 | case kmspb.CryptoKeyVersion_RSA_SIGN_PSS_3072_SHA256: |
| 200 | return crypto.SHA256, true |
| 201 | case kmspb.CryptoKeyVersion_RSA_SIGN_PSS_4096_SHA256: |
| 202 | return crypto.SHA256, true |
| 203 | case kmspb.CryptoKeyVersion_RSA_SIGN_PSS_4096_SHA512: |
| 204 | return crypto.SHA512, true |
| 205 | case kmspb.CryptoKeyVersion_RSA_SIGN_PKCS1_2048_SHA256: |
| 206 | return crypto.SHA256, false |
| 207 | case kmspb.CryptoKeyVersion_RSA_SIGN_PKCS1_3072_SHA256: |
| 208 | return crypto.SHA256, false |
| 209 | case kmspb.CryptoKeyVersion_RSA_SIGN_PKCS1_4096_SHA256: |
| 210 | return crypto.SHA256, false |
| 211 | case kmspb.CryptoKeyVersion_RSA_SIGN_PKCS1_4096_SHA512: |
| 212 | return crypto.SHA512, false |
| 213 | case kmspb.CryptoKeyVersion_EC_SIGN_P256_SHA256: |
| 214 | return crypto.SHA256, false |
| 215 | case kmspb.CryptoKeyVersion_EC_SIGN_P384_SHA384: |
| 216 | return crypto.SHA384, false |
| 217 | } |
| 218 | return 0, false |
| 219 | } |