This page shows you how to retrieve the public key portion of an enabled asymmetric key version.
The public key is in the Privacy-enhanced Electronic Mail (PEM) format. For more information, see the RFC 7468 sections for General Considerations and Textual Encoding of Subject Public Key Info.
Required roles
To get the permissions that you need to retrieve a public key,
ask your administrator to grant you the
Cloud KMS CryptoKey Public Key Viewer (roles/cloudkms.publicKeyViewer
) IAM role on your key or a parent resource.
For more information about granting roles, see Manage access to projects, folders, and organizations.
This predefined role contains the permissions required to retrieve a public key. To see the exact permissions that are required, expand the Required permissions section:
Required permissions
The following permissions are required to retrieve a public key:
-
cloudkms.cryptoKeyVersions.viewPublicKey
-
cloudkms.locations.get
-
cloudkms.locations.list
-
resourcemanager.projects.get
You might also be able to get these permissions with custom roles or other predefined roles.
Retrieve a public key
To download the public key for an enabled asymmetric key version:
Console
In the Google Cloud console, go to the Key Management page.
Click the name of the key ring that contains the asymmetric key for which you want to retrieve the public key.
Click the name of the key for which you want to retrieve the public key.
On the row corresponding to the key version for which you want to retrieve the public key, click View More
.Click Get public key.
The public key is displayed in the prompt. You can copy the public key to your clipboard. To download the public key, click Download.
If you do not see the Get public key option, verify the following:
- The key is an asymmetric key.
- The key version is enabled.
- You have the
cloudkms.cryptoKeyVersions.viewPublicKey
permission.
The filename of a public key downloaded from the Google Cloud console is of the form:
KEY_RING-KEY_NAME-KEY_VERSION.pub
Each portion of the filename is separated by a hyphen, for example
ringname-keyname-version.pub
gcloud
To use Cloud KMS on the command line, first Install or upgrade to the latest version of Google Cloud CLI.
gcloud kms keys versions get-public-key KEY_VERSION \ --key KEY_NAME \ --keyring KEY_RING \ --location LOCATION \ --output-file OUTPUT_FILE_PATH
Replace the following:
KEY_VERSION
: the key version number.KEY_NAME
: the name of the key.KEY_RING
: the name of the key ring that contains the key.LOCATION
: the Cloud KMS location of the key ring.OUTPUT_FILE_PATH
: the path where you want to save the public key file—for example,public-key.pub
.
For information on all flags and possible values, run the command with the
--help
flag.
C#
To run this code, first set up a C# development environment and install the Cloud KMS C# SDK.
Go
To run this code, first set up a Go development environment and install the Cloud KMS Go SDK.
Java
To run this code, first set up a Java development environment and install the Cloud KMS Java SDK.
Node.js
To run this code, first set up a Node.js development environment and install the Cloud KMS Node.js SDK.
PHP
To run this code, first learn about using PHP on Google Cloud and install the Cloud KMS PHP SDK.
Python
To run this code, first set up a Python development environment and install the Cloud KMS Python SDK.
Ruby
To run this code, first set up a Ruby development environment and install the Cloud KMS Ruby SDK.
API
These examples use curl as an HTTP client to demonstrate using the API. For more information about access control, see Accessing the Cloud KMS API.
Retrieve the public key by calling the CryptoKeyVersions.getPublicKey method.
curl "https://cloudkms.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/keyRings/KEY_RING/cryptoKeys/KEY_NAME/cryptoKeyVersions/KEY_VERSION/publicKey" \ --request "GET" \ --header "authorization: Bearer TOKEN"
Replace the following:
PROJECT_ID
: the ID of the project that contains the key ring.LOCATION
: the Cloud KMS location of the key ring.KEY_RING
: the name of the key ring that contains the key.KEY_NAME
: the name of the key.KEY_VERSION
: the key version number.
The output should look similar to the following:
{ "pem": "-----BEGIN PUBLIC KEY-----\nQ29uZ3JhdHVsYXRpb25zLCB5b3UndmUgZGlzY292ZX JlZCB0aGF0IHRoaXMgaXNuJ3QgYWN0dWFsbHkgYSBwdWJsaWMga2V5ISBIYXZlIGEgbmlj ZSBkYXkgOik=\n-----END PUBLIC KEY-----\n", "algorithm": "ALGORITHM", "pemCrc32c": "2561089887", "name": "projects/PROJECT_ID/locations/LOCATION/keyRings/ KEY_RING/cryptoKeys/KEY_NAME/cryptoKeyVersions/ KEY_VERSION", "protectionLevel": "PROTECTION_LEVEL" }