When you need to manage and control key material on your own, you can create a key with an external key material origin in a KMS instance and then import your own key material. This topic describes how to import key material for a symmetric key.
If your software key management instance or hardware key management instance does not support importing key material, or if importing key material returns a failure, contact Alibaba Cloud technical support to upgrade the instance.
Features
A key is a basic resource in KMS, consisting of a key ID, basic metadata (such as key status), and key material. When you create a key, you can choose to let KMS generate the key material, or choose an external key material origin. If you choose an external key material origin, you need to import the external key material into the key. This feature is commonly referred to as Bring Your Own Key (BYOK). For the support of different KMS key management types for importing key material, see the following table. For more information about key management types, see Key management types and key specifications.
Key management type | Import symmetric key material |
Default key |
|
Software-protected key | |
Hardware-protected key |
Usage notes
Make sure you use a random number generator that meets the requirements to generate key material.
After you import key material into a key for the first time, the key is bound to that key material and no longer supports importing other key material.
You can import the same key material into a KMS key multiple times as needed, but you cannot import different key material into the same KMS key.
If the key material of a key expires or is deleted, you can import the same key material again to make the key usable again. Imported key material cannot be exported. Store your key material securely.
Applicable scope
A KMS instance has been purchased and enabled. For more information, see Purchase and enable a KMS instance.
A KMS instance is not required to import key material for a default key (customer master key).
Step 1: Create a symmetric key with an external key material origin
Before importing key material, create a symmetric key with an external key material origin.
Default key (customer master key)
Log on to the Key Management Service console. In the top navigation bar, select a region. In the left-side navigation pane, choose .
Click the Default Keys tab, click Enable in the Actions column for a customer master key, and complete the settings on the Create Key tab, then click OK.
Parameter
Description
Key Alias
An identifier for the key. Allowed characters: letters, digits, underscores (
_), hyphens (-), and forward slashes (/).Description
A description of the key.
Advanced Settings
Key Material Origin. Select External (Import Key Material). Read carefully and select I understand the implications of using external key material..
Software-protected key
Log on to the Key Management Service console. In the top navigation bar, select a region. In the left-side navigation pane, choose .
On the Customer Master Keys tab, select a software key management instance from the Instance ID list, and click Create Key.
In the Create Key panel, complete the settings and click OK.
Parameter
Description
Key Type
Select Symmetric Key. If you are creating a key to encrypt a secret value, select Symmetric Key.
Key Specifications
AES_256, SM4_128, HMAC_224, HMAC_256, HMAC_384, HMAC_512.
Key Usage
The purpose of the key. Valid values:
ENCRYPT/DECRYPT: data encryption and decryption. If you select AES_256 or SM4_128, the key usage is fixed to ENCRYPT/DECRYPT.
SIGN/VERIFY: generate and verify message authentication codes (MACs). If you select HMAC_224, HMAC_256, HMAC_384, or HMAC_512, the key usage is automatically set to SIGN/VERIFY and cannot be changed.
Key Alias
An alias for the key. Allowed characters: letters, digits, underscores (
_), hyphens (-), and forward slashes (/).Tag
Tags for the key, which help you categorize and manage keys. Each tag is a key-value pair (Key:Value), consisting of a tag key (Key) and a tag value (Value).
NoteFormat of a tag key and a tag value: A tag key or a tag value can be up to 128 characters in length and can contain letters, digits, forward slashes (/), backslashes (\), underscores (_), hyphens (-), periods (.), plus signs (+), equal signs (=), colons (:), at signs (@), and spaces.
A tag key cannot start with
aliyunoracs:.Each key supports up to 20 tag key-value pairs.
Description
A description of the key.
Advanced Settings
Policy Settings: For more information, see Key policy overview.
Key Material Origin: Select External (Import Key Material). Read carefully and select I understand the implications of using external key material..
Hardware-protected key
Log on to the Key Management Service console. In the top navigation bar, select a region. In the left-side navigation pane, choose .
On the Customer Master Keys tab, select a hardware key management instance from the Instance ID list, and click Create Key.
In the Create Key panel, complete the settings and click OK.
Parameter
Description
Key Type
Select Symmetric Key. If you are creating a key to encrypt a secret value, select Symmetric Key.
Key Specifications
AES_256, HMAC_224, HMAC_256, HMAC_384, HMAC_512.
Key Usage
The purpose of the key. Valid values:
Encrypt/Decrypt: data encryption and decryption.
Sign/Verify: generate and verify digital signatures.
Key Alias
An alias for the key. Allowed characters: letters, digits, underscores (
_), hyphens (-), and forward slashes (/).Tag
Tags for the key, which help you categorize and manage keys. Each tag is a key-value pair (Key:Value), consisting of a tag key (Key) and a tag value (Value).
NoteFormat of a tag key and a tag value: A tag key or a tag value can be up to 128 characters in length and can contain letters, digits, forward slashes (/), backslashes (\), underscores (_), hyphens (-), periods (.), plus signs (+), equal signs (=), colons (:), at signs (@), and spaces.
A tag key cannot start with
aliyunoracs:.Each key supports up to 20 tag key-value pairs.
Description
A description of the key.
Advanced Settings
Policy Settings: For more information, see Key policy overview.
Key Material Origin: Select External (Import Key Material). Read carefully and select I understand the implications of using external key material..
After creation, the key status is Pending Import.
Step 2: Download a wrapping public key and an import token
The parameters for importing key material include a wrapping public key and an import token. The wrapping public key is used to encrypt the key material and protect your key material during the import process. The import token is used to import the key material.
Find the target key and click Details in the Actions column. In the Key Material section, click Obtain Parameters for Import.
In the Obtain Parameters to Import Key Material dialog box, select Public Key Type and Encryption Algorithm, and click Next.
Key management type
Wrapping public key type
Encryption algorithm
Default key (customer master key)
RSA_2048
RSAES_OAEP_SHA_1, RSAES_OAEP_SHA_256, RSAES_PKCS1_V1_5 (not recommended)
Software-protected key
RSA_2048
RSAES_OAEP_SHA_256, RSAES_PKCS1_V1_5 (not recommended)
Hardware-protected key
RSA_2048
RSAES_OAEP_SHA_256, RSAES_PKCS1_V1_5 (not recommended)
Download the wrapping public key and import token, and store them securely.
Public Key Format:
DER Format: The downloaded file is named
WrappingPublicKey**.binby default.PEM Format: The downloaded file is named
WrappingPublicKey**.pemby default.
Import Token: The downloaded file is named
ImportToken***.txtby default.ImportantAn import token is valid for 24 hours and can be reused within this period. After it expires, you must obtain a new import token and public key.
The wrapping public key and the import token must be used as a pair. You cannot use a wrapping public key from one download with an import token from another.
Step 3: Encrypt the key material using the wrapping public key
Use the wrapping public key and specified wrapping algorithm downloaded in Step 2: Download a wrapping public key and an import token to encrypt the key material.
Use OpenSSL to generate a 32-byte random number as key material. Skip this step if you already have key material.
NoteIf you are creating an HMAC key, refer to HMAC key material length for the required number of bytes, replace the byte count, and then generate the key material.
openssl rand -out KeyMaterial.bin 32Encrypt the key material based on the specified encryption algorithm.
NoteThe encryption algorithm in the example uses RSAES_OAEP_SHA_256.
The example uses DER format for the public key. If you selected PEM format when downloading the public key file, replace
-keyform DERwith-keyform PEMin the example.Replace
PublicKey.binwith the name of the public key file downloaded in Step 2: Download a wrapping public key and an import token.openssl pkeyutl -encrypt \ -in KeyMaterial.bin \ -inkey PublicKey.bin \ -keyform DER \ -pubin \ -out EncryptedKeyMaterial.bin \ -pkeyopt rsa_padding_mode:oaep \ -pkeyopt rsa_oaep_md:sha256 \ -pkeyopt rsa_mgf1_md:sha256Base64-encode the encrypted key material and save it to a text file.
openssl enc -e -base64 -A -in EncryptedKeyMaterial.bin -out EncryptedKeyMaterial_base64.txtNoteEncryptedKeyMaterial_base64.txt is the key material file that can be imported into KMS.
Step 4: Import the key material
On the key details page, click Import Key Material. In the Import Wrapped Key Material dialog box, complete the settings and click OK.
Parameter | Description |
Wrapped Key Material | Upload the key material file generated in Step 3: Encrypt the key material using the wrapping public key. |
Import Token | Upload the token file downloaded in Step 2: Download a wrapping public key and an import token. |
Key Material Expired On | You can select Never Expire, or specify a custom expiration time. Note If you set an expiration time for the key material, KMS will delete the expired key material after the specified time. You will not be able to use the key material. To restore usage, you can import the same key material into the key again. |
After the key material is successfully imported, the key status changes from Pending Import to Enabling.
Appendix
HMAC key material length
Key specification | Required key material length (bytes) |
HMAC_224 | 28 |
HMAC_256 | 32 |
HMAC_384 | 48 |
HMAC_512 | 64 |
Encryption algorithm
RSAES_OAEP_SHA_1: RSA encryption using the RSAES-OAEP scheme defined in RFC 3447/PKCS#1, with MGF1 and SHA-1.
RSAES_OAEP_SHA_256: RSA encryption using the RSAES-OAEP mode as defined in RFC 3447/PKCS#1, with MGF1 and SHA-256.
- Important
RSAES_PKCS1_V1_5: The National Institute of Standards and Technology (NIST) guidance on cryptographic algorithms and key lengths Transitioning the Use of Cryptographic Algorithms and Key Lengths has clearly stated that the use of this encryption algorithm for encrypting transport keys should be discontinued after December 31, 2023.
FAQ
Can I delete key material?
Yes.
After imported key material expires or is deleted, the key becomes unusable. You need to import the same key material again to use the key normally.
Delete key material directly
Console: On the key details page, in the Key Material section, click Delete Key Material.
API: Call the DeleteKeyMaterial operation to delete the key material. This operation does not delete your key.
Automatic deletion by KMS after expiration
Set an expiration time when importing key material. KMS will delete the expired key material after the specified time.
How do I re-import the same key material?
After key material expires or is deleted, you can import the same key material again to continue using the key.
Delete the expired key material.
On the key details page, click the Key Material tab and click Delete Key Material.
Download a new wrapping public key and import token. For more information, see Step 2: Download a wrapping public key and an import token.
The key wrapping process does not affect the content of the key material. Therefore, you can use a different wrapping public key and wrapping algorithm to import the same key material.
Encrypt the key material using the wrapping public key. For more information, see .
The key material must be the same as the previously expired key material.
Import the encrypted key material using the import token. For more information, see Step 4: Import the key material.
How do I check whether the key material was imported from an external source or generated by KMS?
Method 1: Check in the KMS console.
Default key: On the Keys page, click the Default Keys tab, find the target key, and click Details in the Actions column. On the details page, check Key Material Origin.
Software-protected key, Hardware-protected key: On the Keys page, click the Customer Master Keys tab, select the Instance ID, find the target key, and click Details in the Actions column. On the details page, check Key Material Origin.
Method 2: Call the DescribeKey operation.
If the
Originvalue isEXTERNAL, the key material was imported from an external source. If theOriginvalue isAliyun_KMS, the key material was generated by KMS.
How do I rotate a key that uses imported key material?
Software-protected symmetric keys with imported external key material support only immediate manual rotation and do not support automatic periodic rotation. For more information, see BYOK key rotation.
Software-protected asymmetric keys with imported external key material do not support rotation.
Hardware-protected keys (both symmetric and asymmetric) with imported external key material do not support rotation.