int aws_kms_encrypt_blocking()

in source/kms.c [2668:2724]


int aws_kms_encrypt_blocking(
    struct aws_nitro_enclaves_kms_client *client,
    const struct aws_string *key_id,
    const struct aws_byte_buf *plaintext,
    struct aws_byte_buf *ciphertext_blob
    /* TODO: err_reason */) {
    AWS_PRECONDITION(client != NULL);
    AWS_PRECONDITION(key_id != NULL);
    AWS_PRECONDITION(ciphertext_blob != NULL);
    AWS_PRECONDITION(plaintext != NULL);

    struct aws_string *response = NULL;
    struct aws_string *request = NULL;
    struct aws_kms_encrypt_response *response_structure = NULL;
    struct aws_kms_encrypt_request *request_structure = NULL;
    int rc = 0;

    request_structure = aws_kms_encrypt_request_new(client->allocator);
    if (request_structure == NULL) {
        return AWS_OP_ERR;
    }

    aws_byte_buf_init_copy(&request_structure->plaintext, client->allocator, plaintext);
    request_structure->key_id = aws_string_clone_or_reuse(client->allocator, key_id);

    request = aws_kms_encrypt_request_to_json(request_structure);
    if (request == NULL) {
        goto err_clean;
    }

    rc = s_aws_nitro_enclaves_kms_client_call_blocking(client, kms_target_encrypt, request, &response);
    if (rc != 200) {
        fprintf(stderr, "Got non-200 answer from KMS: %d\n", rc);
        goto err_clean;
    }

    response_structure = aws_kms_encrypt_response_from_json(client->allocator, response);
    if (response_structure == NULL) {
        fprintf(stderr, "Could not read response from KMS: %d\n", rc);
        goto err_clean;
    }

    aws_byte_buf_init_copy(ciphertext_blob, client->allocator, &response_structure->ciphertext_blob);

    aws_kms_encrypt_request_destroy(request_structure);
    aws_kms_encrypt_response_destroy(response_structure);
    aws_string_destroy(request);
    aws_string_destroy(response);

    return AWS_OP_SUCCESS;
err_clean:
    aws_kms_encrypt_request_destroy(request_structure);
    aws_kms_encrypt_response_destroy(response_structure);
    aws_string_destroy(request);
    aws_string_destroy(response);
    return AWS_OP_ERR;
}