Search in sources :

Example 1 with GenerateDataKeyResponse

use of com.tencentcloudapi.kms.v20190118.models.GenerateDataKeyResponse in project cos-java-sdk-v5 by tencentyun.

the class COSCryptoModuleBase method buildContentCryptoMaterial.

/**
 * @param materials a non-null encryption material
 */
private ContentCryptoMaterial buildContentCryptoMaterial(EncryptionMaterials materials, Provider provider, CosServiceRequest req) {
    byte[] iv = contentCryptoScheme.getIV();
    if (iv == null) {
        // Randomly generate the IV
        iv = new byte[contentCryptoScheme.getIVLengthInBytes()];
        cryptoScheme.getSecureRandom().nextBytes(iv);
    }
    if (materials.isKMSEnabled()) {
        final Map<String, String> encryptionContext = ContentCryptoMaterial.mergeMaterialDescriptions(materials, req);
        GenerateDataKeyRequest keyGenReq = new GenerateDataKeyRequest();
        try {
            ObjectMapper mapper = new ObjectMapper();
            keyGenReq.setEncryptionContext(mapper.writeValueAsString(encryptionContext));
        } catch (JsonProcessingException e) {
            throw new CosClientException("generate datakey request set encryption context got json processing exception", e);
        }
        keyGenReq.setKeyId(materials.getCustomerMasterKeyId());
        keyGenReq.setKeySpec(contentCryptoScheme.getKeySpec());
        GenerateDataKeyResponse keyGenRes = kms.generateDataKey(keyGenReq);
        byte[] key = Base64.decode(keyGenRes.getPlaintext());
        final SecretKey cek = new SecretKeySpec(key, contentCryptoScheme.getKeyGeneratorAlgorithm());
        byte[] keyBlob = keyGenRes.getCiphertextBlob().getBytes();
        byte[] securedIV = ContentCryptoMaterial.encryptIV(iv, materials, cryptoScheme.getKeyWrapScheme(), cryptoScheme.getSecureRandom(), provider, kms, req);
        return ContentCryptoMaterial.wrap(cek, iv, contentCryptoScheme, provider, new KMSSecuredCEK(keyBlob, encryptionContext), securedIV);
    } else {
        // Generate a one-time use symmetric key and initialize a cipher to encrypt object data
        return ContentCryptoMaterial.create(generateCEK(materials, provider), iv, materials, cryptoScheme, provider, kms, req);
    }
}
Also used : GenerateDataKeyRequest(com.tencentcloudapi.kms.v20190118.models.GenerateDataKeyRequest) SecretKey(javax.crypto.SecretKey) CosClientException(com.qcloud.cos.exception.CosClientException) SecretKeySpec(javax.crypto.spec.SecretKeySpec) GenerateDataKeyResponse(com.tencentcloudapi.kms.v20190118.models.GenerateDataKeyResponse) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 CosClientException (com.qcloud.cos.exception.CosClientException)1 GenerateDataKeyRequest (com.tencentcloudapi.kms.v20190118.models.GenerateDataKeyRequest)1 GenerateDataKeyResponse (com.tencentcloudapi.kms.v20190118.models.GenerateDataKeyResponse)1 SecretKey (javax.crypto.SecretKey)1 SecretKeySpec (javax.crypto.spec.SecretKeySpec)1