Jump to Content

New API Documentation - Developer Preview Available

We are excited to announce the developer preview of our new API documentation for AWS SDK for JavaScript v3. Please follow instructions on the landing page to leave us your feedback.

Class GetPublicKeyCommandProtected

Returns the public key of an asymmetric KMS key. Unlike the private key of a asymmetric KMS key, which never leaves KMS unencrypted, callers with kms:GetPublicKey permission can download the public key of an asymmetric KMS key. You can share the public key to allow others to encrypt messages and verify signatures outside of KMS. For information about asymmetric KMS keys, see Asymmetric KMS keys in the Key Management Service Developer Guide.

You do not need to download the public key. Instead, you can use the public key within KMS by calling the Encrypt, ReEncrypt, or Verify operations with the identifier of an asymmetric KMS key. When you use the public key within KMS, you benefit from the authentication, authorization, and logging that are part of every KMS operation. You also reduce of risk of encrypting data that cannot be decrypted. These features are not effective outside of KMS.

To help you use the public key safely outside of KMS, GetPublicKey returns important information about the public key in the response, including:

  • KeySpec: The type of key material in the public key, such as RSA_4096 or ECC_NIST_P521.

  • KeyUsage: Whether the key is used for encryption or signing.

  • EncryptionAlgorithms or SigningAlgorithms: A list of the encryption algorithms or the signing algorithms for the key.

Although KMS cannot enforce these restrictions on external operations, it is crucial that you use this information to prevent the public key from being used improperly. For example, you can prevent a public signing key from being used encrypt data, or prevent a public key from being used with an encryption algorithm that is not supported by KMS. You can also avoid errors, such as using the wrong signing algorithm in a verification operation.

To verify a signature outside of KMS with an SM2 public key (China Regions only), you must specify the distinguishing ID. By default, KMS uses 1234567812345678 as the distinguishing ID. For more information, see Offline verification with SM2 key pairs.

The KMS key that you use for this operation must be in a compatible key state. For details, see Key states of KMS keys in the Key Management Service Developer Guide.

Cross-account use: Yes. To perform this operation with a KMS key in a different Amazon Web Services account, specify the key ARN or alias ARN in the value of the KeyId parameter.

Required permissions: kms:GetPublicKey (key policy)

Related operations: CreateKey

Example

Use a bare-bones client and the command you need to make an API call.

import { KMSClient, GetPublicKeyCommand } from "@aws-sdk/client-kms"; // ES Modules import
// const { KMSClient, GetPublicKeyCommand } = require("@aws-sdk/client-kms"); // CommonJS import
const client = new KMSClient(config);
const input = { // GetPublicKeyRequest
KeyId: "STRING_VALUE", // required
GrantTokens: [ // GrantTokenList
"STRING_VALUE",
],
};
const command = new GetPublicKeyCommand(input);
const response = await client.send(command);
// { // GetPublicKeyResponse
// KeyId: "STRING_VALUE",
// PublicKey: "BLOB_VALUE",
// CustomerMasterKeySpec: "RSA_2048" || "RSA_3072" || "RSA_4096" || "ECC_NIST_P256" || "ECC_NIST_P384" || "ECC_NIST_P521" || "ECC_SECG_P256K1" || "SYMMETRIC_DEFAULT" || "HMAC_224" || "HMAC_256" || "HMAC_384" || "HMAC_512" || "SM2",
// KeySpec: "RSA_2048" || "RSA_3072" || "RSA_4096" || "ECC_NIST_P256" || "ECC_NIST_P384" || "ECC_NIST_P521" || "ECC_SECG_P256K1" || "SYMMETRIC_DEFAULT" || "HMAC_224" || "HMAC_256" || "HMAC_384" || "HMAC_512" || "SM2",
// KeyUsage: "SIGN_VERIFY" || "ENCRYPT_DECRYPT" || "GENERATE_VERIFY_MAC",
// EncryptionAlgorithms: [ // EncryptionAlgorithmSpecList
// "SYMMETRIC_DEFAULT" || "RSAES_OAEP_SHA_1" || "RSAES_OAEP_SHA_256" || "SM2PKE",
// ],
// SigningAlgorithms: [ // SigningAlgorithmSpecList
// "RSASSA_PSS_SHA_256" || "RSASSA_PSS_SHA_384" || "RSASSA_PSS_SHA_512" || "RSASSA_PKCS1_V1_5_SHA_256" || "RSASSA_PKCS1_V1_5_SHA_384" || "RSASSA_PKCS1_V1_5_SHA_512" || "ECDSA_SHA_256" || "ECDSA_SHA_384" || "ECDSA_SHA_512" || "SM2DSA",
// ],
// };

Param

GetPublicKeyCommandInput

Returns

GetPublicKeyCommandOutput

See

Throws

DependencyTimeoutException (server fault)

The system timed out while trying to fulfill the request. You can retry the request.

Throws

DisabledException (client fault)

The request was rejected because the specified KMS key is not enabled.

Throws

InvalidArnException (client fault)

The request was rejected because a specified ARN, or an ARN in a key policy, is not valid.

Throws

InvalidGrantTokenException (client fault)

The request was rejected because the specified grant token is not valid.

Throws

InvalidKeyUsageException (client fault)

The request was rejected for one of the following reasons:

  • The KeyUsage value of the KMS key is incompatible with the API operation.

  • The encryption algorithm or signing algorithm specified for the operation is incompatible with the type of key material in the KMS key (KeySpec).

For encrypting, decrypting, re-encrypting, and generating data keys, the KeyUsage must be ENCRYPT_DECRYPT. For signing and verifying messages, the KeyUsage must be SIGN_VERIFY. For generating and verifying message authentication codes (MACs), the KeyUsage must be GENERATE_VERIFY_MAC. To find the KeyUsage of a KMS key, use the DescribeKey operation.

To find the encryption or signing algorithms supported for a particular KMS key, use the DescribeKey operation.

Throws

KeyUnavailableException (server fault)

The request was rejected because the specified KMS key was not available. You can retry the request.

Throws

KMSInternalException (server fault)

The request was rejected because an internal exception occurred. The request can be retried.

Throws

KMSInvalidStateException (client fault)

The request was rejected because the state of the specified resource is not valid for this request.

This exceptions means one of the following:

  • The key state of the KMS key is not compatible with the operation.

    To find the key state, use the DescribeKey operation. For more information about which key states are compatible with each KMS operation, see Key states of KMS keys in the Key Management Service Developer Guide .

  • For cryptographic operations on KMS keys in custom key stores, this exception represents a general failure with many possible causes. To identify the cause, see the error message that accompanies the exception.

Throws

NotFoundException (client fault)

The request was rejected because the specified entity or resource could not be found.

Throws

UnsupportedOperationException (client fault)

The request was rejected because a specified parameter is not supported or a specified resource is not valid for this operation.

Throws

KMSServiceException

Base exception class for all service exceptions from KMS service.

Example

To download the public key of an asymmetric KMS key

// This example gets the public key of an asymmetric RSA KMS key used for encryption and decryption. The operation returns the key spec, key usage, and encryption or signing algorithms to help you use the public key correctly outside of AWS KMS.
const input = {
"KeyId": "arn:aws:kms:us-west-2:111122223333:key/0987dcba-09fe-87dc-65ba-ab0987654321"
};
const command = new GetPublicKeyCommand(input);
const response = await client.send(command);
/* response ==
{
"CustomerMasterKeySpec": "RSA_4096",
"EncryptionAlgorithms": [
"RSAES_OAEP_SHA_1",
"RSAES_OAEP_SHA_256"
],
"KeyId": "arn:aws:kms:us-west-2:111122223333:key/0987dcba-09fe-87dc-65ba-ab0987654321",
"KeyUsage": "ENCRYPT_DECRYPT",
"PublicKey": "<binary data>"
}
*/
// example id: to-download-the-public-key-of-an-asymmetric-kms-key-1628621691873

Hierarchy

Constructors

Properties

middlewareStack: MiddlewareStack<GetPublicKeyCommandInput, GetPublicKeyCommandOutput>

Methods