Skip to content

Commit

Permalink
Update Create request encoding to convert template attributes
Browse files Browse the repository at this point in the history
This change updates how Create request payloads are encoded,
dynamically converting an existing TemplateAttribute structure
in the payload to the KMIP 2.0-style Attributes structure if
only the TemplateAttribute structure is present at encoding time
for KMIP 2.0 requests. This allows for seemless interoperability
with the existing BIO api, preserving the existing demo
applications while still supporting KMIP 2.0 requests.
  • Loading branch information
PeterHamilton committed Jul 12, 2019
1 parent 3dc3b08 commit 875da27
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 39 deletions.
47 changes: 25 additions & 22 deletions demo_create.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,29 +44,17 @@ parse_arguments(int argc, char **argv,
for(int i = 1; i < argc; i++)
{
if(strncmp(argv[i], "-a", 2) == 0)
{
*server_address = argv[++i];
}
else if(strncmp(argv[i], "-c", 2) == 0)
{
*client_certificate = argv[++i];
}
else if(strncmp(argv[i], "-h", 2) == 0)
{
*print_usage = 1;
}
else if(strncmp(argv[i], "-k", 2) == 0)
{
*client_key = argv[++i];
}
else if(strncmp(argv[i], "-p", 2) == 0)
{
*server_port = argv[++i];
}
else if(strncmp(argv[i], "-r", 2) == 0)
{
*ca_certificate = argv[++i];
}
else
{
printf("Invalid option: '%s'\n", argv[i]);
Expand Down Expand Up @@ -165,9 +153,7 @@ use_low_level_api(const char *server_address,
/* Build the request message. */
Attribute a[3] = {0};
for(int i = 0; i < 3; i++)
{
kmip_init_attribute(&a[i]);
}

enum cryptographic_algorithm algorithm = KMIP_CRYPTOALG_AES;
a[0].type = KMIP_ATTR_CRYPTOGRAPHIC_ALGORITHM;
Expand Down Expand Up @@ -199,7 +185,7 @@ use_low_level_api(const char *server_address,
CreateRequestPayload crp = {0};
crp.object_type = KMIP_OBJTYPE_SYMMETRIC_KEY;
crp.template_attribute = &ta;

RequestBatchItem rbi = {0};
kmip_init_request_batch_item(&rbi);
rbi.operation = KMIP_OP_CREATE;
Expand All @@ -209,7 +195,7 @@ use_low_level_api(const char *server_address,
rm.request_header = &rh;
rm.batch_items = &rbi;
rm.batch_count = 1;

/* Encode the request message. Dynamically resize the encoding buffer */
/* if it's not big enough. Once encoding succeeds, send the request */
/* message. */
Expand All @@ -225,6 +211,9 @@ use_low_level_api(const char *server_address,
encoding = kmip_context.calloc_func(kmip_context.state, buffer_blocks, buffer_block_size);
if(encoding == NULL)
{
printf("Failure: Could not automatically enlarge the encoding ");
printf("buffer for the Create request.\n");

kmip_destroy(&kmip_context);
BIO_free_all(bio);
SSL_CTX_free(ctx);
Expand All @@ -237,6 +226,15 @@ use_low_level_api(const char *server_address,

if(encode_result != KMIP_OK)
{
printf("An error occurred while encoding the Create request.\n");
printf("Error Code: %d\n", encode_result);
printf("Error Name: ");
kmip_print_error_string(encode_result);
printf("\n");
printf("Context Error: %s\n", kmip_context.error_message);
printf("Stack trace:\n");
kmip_print_stack_trace(&kmip_context);

kmip_free_buffer(&kmip_context, encoding, buffer_total_size);
encoding = NULL;
kmip_set_buffer(&kmip_context, NULL, 0);
Expand All @@ -260,7 +258,7 @@ use_low_level_api(const char *server_address,
printf("\n");
if(result < 0)
{
printf("An error occurred while creating the symmetric key.");
printf("An error occurred while creating the symmetric key.\n");
printf("Error Code: %d\n", result);
printf("Error Name: ");
kmip_print_error_string(result);
Expand All @@ -287,6 +285,15 @@ use_low_level_api(const char *server_address,
int decode_result = kmip_decode_response_message(&kmip_context, &resp_m);
if(decode_result != KMIP_OK)
{
printf("An error occurred while decoding the Create response.\n");
printf("Error Code: %d\n", decode_result);
printf("Error Name: ");
kmip_print_error_string(decode_result);
printf("\n");
printf("Context Error: %s\n", kmip_context.error_message);
printf("Stack trace:\n");
kmip_print_stack_trace(&kmip_context);

kmip_free_response_message(&kmip_context, &resp_m);
kmip_free_buffer(&kmip_context, response, response_size);
response = NULL;
Expand All @@ -300,6 +307,7 @@ use_low_level_api(const char *server_address,

if(resp_m.batch_count != 1 || resp_m.batch_items == NULL)
{
printf("Expected to find one batch item in the Create response.\n");
kmip_free_response_message(&kmip_context, &resp_m);
kmip_free_buffer(&kmip_context, response, response_size);
response = NULL;
Expand All @@ -324,9 +332,7 @@ use_low_level_api(const char *server_address,
TextString *uuid = pld->unique_identifier;

if(uuid != NULL)
{
printf("Symmetric Key ID: %.*s\n", (int)uuid->size, uuid->value);
}
}
}

Expand All @@ -353,16 +359,13 @@ main(int argc, char **argv)

int error = parse_arguments(argc, argv, &server_address, &server_port, &client_certificate, &client_key, &ca_certificate, &help);
if(error)
{
return(error);
}
if(help)
{
print_help(argv[0]);
return(0);
}

use_low_level_api(server_address, server_port, client_certificate, client_key, ca_certificate);

return(0);
}
26 changes: 24 additions & 2 deletions kmip.c
Original file line number Diff line number Diff line change
Expand Up @@ -9187,8 +9187,30 @@ kmip_encode_create_request_payload(KMIP *ctx, const CreateRequestPayload *value)
}
else
{
result = kmip_encode_attributes(ctx, value->attributes);
CHECK_RESULT(ctx, result);
if(value->attributes)
{
result = kmip_encode_attributes(ctx, value->attributes);
CHECK_RESULT(ctx, result);
}
else if(value->template_attribute)
{
Attributes *attributes = ctx->calloc_func(ctx->state, 1, sizeof(Attributes));
LinkedList *list = ctx->calloc_func(ctx->state, 1, sizeof(LinkedList));
attributes->attribute_list = list;
for(size_t i = 0; i < value->template_attribute->attribute_count; i++)
{
LinkedListItem *item = ctx->calloc_func(ctx->state, 1, sizeof(LinkedListItem));
item->data = kmip_deep_copy_attribute(ctx, &value->template_attribute->attributes[i]);
kmip_linked_list_enqueue(list, item);
}

result = kmip_encode_attributes(ctx, attributes);

kmip_free_attributes(ctx, attributes);
ctx->free_func(ctx->state, attributes);

CHECK_RESULT(ctx, result);
}

if(value->protection_storage_masks != NULL)
{
Expand Down
18 changes: 3 additions & 15 deletions kmip_bio.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ int kmip_bio_create_symmetric_key(BIO *bio,
char **id, int *id_size)
{
if(bio == NULL || template_attribute == NULL || id == NULL || id_size == NULL)
{
return(KMIP_ARG_INVALID);
}

/* Set up the KMIP context and the initial encoding buffer. */
KMIP ctx = {0};
Expand Down Expand Up @@ -158,9 +156,7 @@ int kmip_bio_create_symmetric_key(BIO *bio,
kmip_set_buffer(&ctx, NULL, 0);
uint8 *extended = ctx.realloc_func(ctx.state, encoding, buffer_total_size + length);
if(encoding != extended)
{
encoding = extended;
}
ctx.memset_func(encoding + buffer_total_size, 0, length);

buffer_block_size += length;
Expand Down Expand Up @@ -212,9 +208,7 @@ int kmip_bio_create_symmetric_key(BIO *bio,
unique_identifier->size + 1);
*id_size = unique_identifier->size;
for(int i = 0; i < *id_size; i++)
{
result_id[i] = unique_identifier->value[i];
}
*id = result_id;

/* Clean up the response message, the encoding buffer, and the KMIP */
Expand Down Expand Up @@ -682,12 +676,9 @@ int kmip_bio_create_symmetric_key_with_context(KMIP *ctx, BIO *bio,
size_t buffer_block_size = 1024;
size_t buffer_total_size = buffer_blocks * buffer_block_size;

uint8 *encoding = ctx->calloc_func(ctx->state, buffer_blocks,
buffer_block_size);
uint8 *encoding = ctx->calloc_func(ctx->state, buffer_blocks, buffer_block_size);
if(encoding == NULL)
{
return(KMIP_MEMORY_ALLOC_FAILED);
}
kmip_set_buffer(ctx, encoding, buffer_total_size);

/* Build the request message. */
Expand All @@ -705,7 +696,7 @@ int kmip_bio_create_symmetric_key_with_context(KMIP *ctx, BIO *bio,
CreateRequestPayload crp = {0};
crp.object_type = KMIP_OBJTYPE_SYMMETRIC_KEY;
crp.template_attribute = template_attribute;

RequestBatchItem rbi = {0};
kmip_init_request_batch_item(&rbi);
rbi.operation = KMIP_OP_CREATE;
Expand Down Expand Up @@ -741,8 +732,7 @@ int kmip_bio_create_symmetric_key_with_context(KMIP *ctx, BIO *bio,
buffer_blocks += 1;
buffer_total_size = buffer_blocks * buffer_block_size;

encoding = ctx->calloc_func(ctx->state, buffer_blocks,
buffer_block_size);
encoding = ctx->calloc_func(ctx->state, buffer_blocks, buffer_block_size);
if(encoding == NULL)
{
kmip_set_buffer(ctx, NULL, 0);
Expand Down Expand Up @@ -786,9 +776,7 @@ int kmip_bio_create_symmetric_key_with_context(KMIP *ctx, BIO *bio,

encoding = ctx->calloc_func(ctx->state, buffer_blocks, buffer_block_size);
if(encoding == NULL)
{
return(KMIP_MEMORY_ALLOC_FAILED);
}

int recv = BIO_read(bio, encoding, buffer_total_size);
if((size_t)recv != buffer_total_size)
Expand Down

0 comments on commit 875da27

Please sign in to comment.