Wrapping di una chiave utilizzando OpenSSL su Linux
Mantieni tutto organizzato con le raccolte
Salva e classifica i contenuti in base alle tue preferenze.
Questo argomento mostra come eseguire il wrapping manuale di una chiave prima di importarla in Cloud KMS. Devi seguire le istruzioni riportate in questo argomento solo se non vuoi utilizzare l'interfaccia a riga di comando Google Cloud per eseguire automaticamente l'arrotondamento della chiave prima di importarla. Per una panoramica delle differenze, consulta Come funziona l'importazione delle chiavi.
Puoi completare i passaggi descritti in questo argomento in 5-10 minuti, esclusi i passaggi di Prima di iniziare.
Prima di iniziare
Prima di poter eseguire l'incapsulamento di una chiave, devi completare i seguenti prerequisiti.
Questa sezione mostra come recuperare la chiave di wrapping dal job di importazione creato in Prima di iniziare. ร consigliato utilizzare la Google Cloud console.
Console
Vai alla pagina Gestione chiavi nella Google Cloud console.
Recupera la chiave pubblica tramite il campo publicKey della risposta ImportJob.get. Questo valore รจ di tipo WrappingPublicKey.
Il campo pem di tipo WrappingPublicKey รจ la chiave pubblica codificata in formato PEM (Privacy Enhanced Mail).
I comandi OpenSSL richiedono diversi percorsi file come valori di input. Definisci
variabili di ambiente per i percorsi file per semplificare l'esecuzione dei comandi.
Assicurati di avere accesso in scrittura alle directory che definisci di seguito.
Imposta la variabile PUB_WRAPPING_KEY sul percorso completo della chiave di wrapping che hai scaricato dal job di importazione. La chiave di wrapping termina in .pem.
PUB_WRAPPING_KEY="WRAPPING_KEY_PATH"
Imposta la variabile TARGET_KEY sul percorso completo della chiave scomposta (target).
TARGET_KEY=TARGET_KEY_PATH
Sostituisci TARGET_KEY_PATH con il percorso del file .bin per le chiavi simmetriche o con il percorso del file .der per le chiavi asimmetriche.
Se esegui il wrapping con RSA-AES, imposta la variabile TEMP_AES_KEY sul percorso completo della chiave AES temporanea.
TEMP_AES_KEY=TEMP_AES_KEY_PATH
Imposta la variabile WRAPPED_KEY sul percorso completo in cui vuoi salvare la chiave di destinazione con wrapping pronta per l'importazione.
WRAPPED_KEY=WRAPPED_KEY_PATH
Verifica che tutte le variabili di ambiente siano impostate correttamente utilizzando i seguenti comandi:
Quando le variabili sono impostate correttamente, puoi eseguire l'arrotondamento della chiave. Esistono due approcci, come descritto di seguito: con solo RSA o con RSA-AES.
Esegui il wrapping della chiave
Esegui il wrapping della chiave con RSA
In questo approccio, la chiave di destinazione viene inserita in un blocco RSA. La dimensione della chiave di destinazione รจ quindi limitata. Ad esempio, non puoi utilizzare questo metodo per eseguire l'incapsulamento di un'altra chiave RSA. I metodi di importazione supportati sono rsa-oaep-3072-sha256 e
rsa-oaep-4096-sha256.
Esegui il wrapping della chiave di destinazione con la chiave pubblica di wrapping utilizzando
l'algoritmo CKM_RSA_PKCS_OAEP:
In questo approccio, la chiave di destinazione viene sottoposta a wrapping con una chiave AES temporanea. La chiave AES temporanea viene poi sottoposta a wrapping dalla chiave RSA. Queste due chiavi con wrapping vengono concatenate e importate. Poichรฉ la chiave di destinazione viene sottoposta a wrapping utilizzando AES anzichรฉ RSA, questo approccio puรฒ essere utilizzato per sottoporre a wrapping chiavi di grandi dimensioni. I metodi di importazione supportati sono rsa-oaep-3072-sha1-aes-256, rsa-oaep-4096-sha1-aes-256,
rsa-oaep-3072-sha256-aes-256 e rsa-oaep-4096-sha256-aes-256.
Genera una chiave AES casuale temporanea di 32 byte e salvala nella posizione identificata da ${TEMP_AES_KEY}:
openssl rand -out "${TEMP_AES_KEY}" 32
Esegui il wrapping della chiave AES temporanea con la chiave pubblica di wrapping utilizzando
l'algoritmo CKM_RSA_PKCS_OAEP. Se il metodo di importazione รจ rsa-oaep-3072-sha1-aes-256 o rsa-oaep-4096-sha1-aes-256, utilizza sha1 per rsa_oaep_md e rsa_mgf1_md. Utilizza sha256 per
rsa-oaep-3072-sha256-aes-256 e rsa-oaep-4096-sha256-aes-256.
Imposta la variabile OpenSSL_V110 sul percorso dello script openssl.sh. Se
hai seguito esattamente le istruzioni per
eseguire il patching e la ricompilazione di OpenSSL,
puoi utilizzare questo comando senza modificare il valore della variabile.
OPENSSL_V110="${HOME}/local/bin/openssl.sh"
Avvolgi la chiave di destinazione con la chiave AES temporanea utilizzando l'algoritmo CKM_AES_KEY_WRAP_PAD e aggiungila a WRAPPED_KEY.
Il flag -iv A65959A6 imposta A65959A6 come valore iniziale alternativo. Questo รจ richiesto dalla specifica RFC 5649.
Passaggi successivi
La chiave con wrapping salvata in WRAPPED_KEY รจ ora pronta per essere importata.
Per importare la chiave, segui le istruzioni riportate in Importazione di una chiave con wrapping manuale.
[[["Facile da capire","easyToUnderstand","thumb-up"],["Il problema รจ stato risolto","solvedMyProblem","thumb-up"],["Altra","otherUp","thumb-up"]],[["Difficile da capire","hardToUnderstand","thumb-down"],["Informazioni o codice di esempio errati","incorrectInformationOrSampleCode","thumb-down"],["Mancano le informazioni o gli esempi di cui ho bisogno","missingTheInformationSamplesINeed","thumb-down"],["Problema di traduzione","translationIssue","thumb-down"],["Altra","otherDown","thumb-down"]],["Ultimo aggiornamento 2025-09-02 UTC."],[],[],null,["# Wrapping a key using OpenSSL on Linux\n\nThis topic shows how to manually wrap a key before\n[importing the key](/kms/docs/importing-a-key) into Cloud KMS. You\nonly need to follow the instructions in this topic if you do not want to use the\nGoogle Cloud CLI to automatically wrap the key before importing it. For an\noverview of the differences, refer to\n[How key import works](/kms/docs/key-import#key_import_flow).\n\nYou can complete the steps in this topic in 5 to 10 minutes, not including the\n[Before you begin](#before_you_begin) steps.\n\nBefore you begin\n----------------\n\nBefore you can wrap a key, you must complete the following prerequisites.\n\n1. [Create a target key ring and key, and create an import job](/kms/docs/importing-a-key).\n2. [Verify that your key is available locally and formatted correctly](/kms/docs/formatting-keys-for-import) for import into Cloud KMS.\n3. [Patch and recompile OpenSSL](/kms/docs/configuring-openssl-for-manual-key-wrapping)\n\nRetrieve the wrapping key\n-------------------------\n\nThis section shows how to retrieve the wrapping key from the import job you\ncreated in [Before you begin](#before_you_begin). Using the Google Cloud console is\nrecommended. \n\n### Console\n\n1. Go to the **Key Management** page in the Google Cloud console.\n\n [Go to the Key Management page](https://console.cloud.google.com/security/kms)\n2. Click the name of the key ring that contains your import job.\n\n3. Click the **Import Jobs** tab at the top of the page.\n\n4. Click **More** *more_vert* ,\n then **Download wrapping key** in the pop-up menu.\n\n### gcloud CLI\n\nTo verify that the import job is active, run the\n`gcloud kms import-jobs describe` command: \n\n```\ngcloud kms import-jobs describe IMPORT_JOB \\\n --location LOCATION \\\n --keyring KEY_RING \\\n --format=\"value(state)\"\n``` \n\n```\nstate: ACTIVE\n```\n\n\u003cbr /\u003e\n\nRun the following command to save the public key from the import job to\n`${HOME}/wrapping-key.pem` \n\n```\ngcloud kms import-jobs describe \\\n --location=LOCATION \\\n --keyring=KEY_RING \\\n --format=\"value(publicKey.pem)\" \\\n IMPORT_JOB \u003e ${HOME}/wrapping-key.pem\n```\n\n\u003cbr /\u003e\n\n| **Note:** You only need to download the public key once and can use it until the import job expires.\n\n### API\n\n1. Call the [`ImportJob.get`](/kms/docs/reference/rest/v1/projects.locations.keyRings.importJobs/get) method.\n\n2. Retrieve the public key via the [`publicKey`](/kms/docs/reference/rest/v1/projects.locations.keyRings.importJobs#ImportJob.FIELDS.public_key) field of the\n `ImportJob.get` response. This value is of type [`WrappingPublicKey`](/kms/docs/reference/rest/v1/projects.locations.keyRings.importJobs#WrappingPublicKey).\n The [`pem`](/kms/docs/reference/rest/v1/projects.locations.keyRings.importJobs#WrappingPublicKey.FIELDS.pem) field of the `WrappingPublicKey` type is the public\n key encoded in Privacy Enhanced Mail (PEM) format.\n\n| **Note:** Remember to verify that your key's [algorithm and length](/kms/docs/algorithms) are supported. Allowable algorithms for a key depend upon whether the key is used for symmetric encryption, asymmetric encryption or asymmetric signing, as well as whether the key is stored in software or a Hardware Security Module (HSM).\n\nFor more information about the PEM-encoded format, see\n[RFC 7468](https://tools.ietf.org/html/rfc7468), especially the\n[General Considerations](https://tools.ietf.org/html/rfc7468#section-2)\nand\n[Textual Encoding of Subject Public Key Info](https://tools.ietf.org/html/rfc7468#section-2)\nsections.\n\nSet up environment variables\n----------------------------\n\nThe OpenSSL commands require several file paths as input values. Define\nenvironment variables for the file paths to make it easier to run the commands.\nMake sure you have access to write to the directories you define below.\n| **Note:** If desired, you can save all of these commands to a shell script and run the script to set the variables all at once. You may need to adjust some of the values for each key you want to wrap.\n\n1. Set the `PUB_WRAPPING_KEY` variable to the full path to the wrapping key you\n downloaded from the import job. The wrapping key ends in `.pem`.\n\n ```\n PUB_WRAPPING_KEY=\"WRAPPING_KEY_PATH\"\n ```\n\n \u003cbr /\u003e\n\n2. Set the `TARGET_KEY` variable to the full path to the unwrapped (target) key.\n\n ```\n TARGET_KEY=TARGET_KEY_PATH\n ```\n\n \u003cbr /\u003e\n\n Replace `TARGET_KEY_PATH` with the path to the `.bin` file for\n symmetric keys or the path to the `.der` file for asymmetric keys.\n3. If wrapping with RSA-AES, set the `TEMP_AES_KEY` variable to the full path\n to the temporary AES key.\n\n ```\n TEMP_AES_KEY=TEMP_AES_KEY_PATH\n ```\n\n \u003cbr /\u003e\n\n4. Set the `WRAPPED_KEY` variable to the full path where you\n want to save the wrapped target key that is ready for\n import.\n\n ```\n WRAPPED_KEY=WRAPPED_KEY_PATH\n ```\n\n \u003cbr /\u003e\n\n5. Verify that all the environment variables are set correctly using the\n following commands:\n\n ```\n echo \"PUB_WRAPPING_KEY: \" ${PUB_WRAPPING_KEY}; \\\n echo \"TARGET_KEY: \" ${TARGET_KEY}; \\\n echo \"TEMP_AES_KEY: \" ${TEMP_AES_KEY}; \\\n echo \"WRAPPED_KEY: \" ${WRAPPED_KEY}\n ```\n\n \u003cbr /\u003e\n\nWhen the variables are set correctly, you are ready to wrap the key. There are\ntwo approaches as described below: with [RSA only](#rsa_wrap) or with\n[RSA-AES](#rsa_aes_wrap).\n\nWrap the key\n------------\n\n### Wrap the key with RSA\n\nIn this approach, the target key is wrapped in an RSA block. The target key size\nis therefore limited. For example, you can't use this method to wrap another RSA\nkey. The supported import methods are `rsa-oaep-3072-sha256` and\n`rsa-oaep-4096-sha256`.\n\n- Wrap the target key with the wrapping public key using\n the `CKM_RSA_PKCS_OAEP` algorithm:\n\n ```\n openssl pkeyutl \\\n -encrypt \\\n -pubin \\\n -inkey ${PUB_WRAPPING_KEY} \\\n -in ${TARGET_KEY} \\\n -out ${WRAPPED_KEY} \\\n -pkeyopt rsa_padding_mode:oaep \\\n -pkeyopt rsa_oaep_md:sha256 \\\n -pkeyopt rsa_mgf1_md:sha256\n ```\n\n \u003cbr /\u003e\n\n### Wrap the key with RSA-AES\n\nIn this approach, the target key is wrapped with a temporary AES key. The\ntemporary AES key is then wrapped by the RSA key. These two wrapped keys are\nconcatenated and imported. Because the target key is wrapped using AES rather\nthan RSA, this approach can be used to wrap large keys. The supported import\nmethods are `rsa-oaep-3072-sha1-aes-256`, `rsa-oaep-4096-sha1-aes-256`,\n`rsa-oaep-3072-sha256-aes-256` and `rsa-oaep-4096-sha256-aes-256`.\n\n1. Generate a temporary random AES key that is 32 bytes long, and save it to\n the location identified by `${TEMP_AES_KEY}`:\n\n ```\n openssl rand -out \"${TEMP_AES_KEY}\" 32\n ```\n\n \u003cbr /\u003e\n\n2. Wrap the temporary AES key with the wrapping public key using\n the `CKM_RSA_PKCS_OAEP` algorithm. If the import method is either\n `rsa-oaep-3072-sha1-aes-256` or `rsa-oaep-4096-sha1-aes-256`, use `sha1` for\n `rsa_oaep_md` and `rsa_mgf1_md`. Use `sha256` for\n `rsa-oaep-3072-sha256-aes-256` and `rsa-oaep-4096-sha256-aes-256`.\n\n ```\n openssl pkeyutl \\\n -encrypt \\\n -pubin \\\n -inkey ${PUB_WRAPPING_KEY} \\\n -in ${TEMP_AES_KEY} \\\n -out ${WRAPPED_KEY} \\\n -pkeyopt rsa_padding_mode:oaep \\\n -pkeyopt rsa_oaep_md:{sha1|sha256} \\\n -pkeyopt rsa_mgf1_md:{sha1|sha256}\n ```\n\n \u003cbr /\u003e\n\n3. Set the `OpenSSL_V110` variable to the path of your `openssl.sh` script. If\n you followed the instructions for\n [patching and recompiling OpenSSL](/kms/docs/configuring-openssl-for-manual-key-wrapping) exactly,\n you can use this command without modifying the value of the variable.\n\n ```\n OPENSSL_V110=\"${HOME}/local/bin/openssl.sh\"\n ```\n\n \u003cbr /\u003e\n\n4. Wrap the target key with the temporary AES key using the\n `CKM_AES_KEY_WRAP_PAD` algorithm, and append it to the `WRAPPED_KEY`.\n\n ```\n \"${OPENSSL_V110}\" enc \\\n -id-aes256-wrap-pad \\\n -iv A65959A6 \\\n -K $( hexdump -v -e '/1 \"%02x\"' \u003c \"${TEMP_AES_KEY}\" ) \\\n -in \"${TARGET_KEY}\" \u003e\u003e \"${WRAPPED_KEY}\"\n ```\n\n \u003cbr /\u003e\n\n The `-iv A65959A6` flag sets A65959A6 as the Alternate Initial Value. This\n is required by the [RFC 5649](https://tools.ietf.org/html/rfc5649)\n specification.\n\nWhat's next\n-----------\n\n- The wrapped key saved at `WRAPPED_KEY` is now ready to be imported. To import the key, follow the instructions in [Importing a\n manually-wrapped key](/kms/docs/importing-a-key#importing_a_manually-wrapped_key)."]]