Retrieve a public key

This page shows you how to retrieve the public key portion of an enabled asymmetric key version.

The format of the public key depends on whether it is a post-quantum computing (PQC) or conventional algorithm:

  • For non-PQC algorithms, the default format of the public key is the Privacy-enhanced Electronic Mail (PEM) format. You can also retrieve non-PQC public keys in the Distinguished Encoding Rules (DER) format. For more information, see the RFC 7468, particularly the sections "General Considerations" and "Textual Encoding of Subject Public Key Info".

  • For PQC algorithms standardized by NIST, you can retrieve the public key in the format identified in the NIST PCQ standards for that algorithm. For more information, see FIPS-203, FIPS-204, and FIPS-205. The PEM and DER formats are only supported for ML-DSA keys.

  • For X-Wing, you can retrieve the public key in the raw bytes format specified by the X-Wing standard. PEM and DER formats are not supported for these keys.

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

You can specify the format in which you want to retrieve the public key. If the format is specified, the key will be returned in the specified format in the public_key field of the response. Otherwise, it is returned in the pem field of the response.

To download the public key for an enabled asymmetric key version:

Console

  1. In the Google Cloud console, go to the Key Management page.

    Go to Key Management

  2. Click the name of the key ring that contains the asymmetric key for which you want to retrieve the public key.

  3. Click the name of the key for which you want to retrieve the public key.

  4. On the row corresponding to the key version for which you want to retrieve the public key, click View More .

  5. Click Get public key.

  6. 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 \
    --public-key-format PUBLIC_KEY_FORMAT \
    --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.
  • PUBLIC_KEY_FORMAT: the format in which you want to export the public key. For NIST PQC algorithms, use nist-pqc and for X-Wing use xwing-raw-bytes. For all other keys, you can use pem, der, or omit this parameter.
  • 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.


using Google.Cloud.Kms.V1;

public class GetPublicKeySample
{
    public PublicKey GetPublicKey(string projectId = "my-project", string locationId = "us-east1", string keyRingId = "my-key-ring", string keyId = "my-key", string keyVersionId = "123")
    {
        // Create the client.
        KeyManagementServiceClient client = KeyManagementServiceClient.Create();

        // Build the key version name.
        CryptoKeyVersionName keyVersionName = new CryptoKeyVersionName(projectId, locationId, keyRingId, keyId, keyVersionId);

        // Call the API.
        PublicKey result = client.GetPublicKey(keyVersionName);

        // Return the ciphertext.
        return result;
    }
}

Go

To run this code, first set up a Go development environment and install the Cloud KMS Go SDK.

import (
	"context"
	"crypto/x509"
	"encoding/pem"
	"fmt"
	"hash/crc32"
	"io"

	kms "cloud.google.com/go/kms/apiv1"
	"cloud.google.com/go/kms/apiv1/kmspb"
)

// getPublicKey retrieves the public key from an asymmetric key pair on
// Cloud KMS.
func getPublicKey(