Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rj all 2022 04 13 #66

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ CFLAGS = -std=c11 -pedantic -g3 -Wall -Wextra
LOFLAGS = -fPIC
SOFLAGS = -shared -Wl,-soname,$(SO_NAME)
SOCOREFLAGS = -shared -Wl,-soname,$(SO_CORE_NAME)
LDFLAGS = -L/usr/local/lib64 -L/usr/local/lib
#LDFLAGS = -L/usr/local/lib64 -L/usr/local/lib
LDFLAGS = -L/usr/local/lib
LDLIBS = -lssl -lcrypto

INC_FLAGS = -I$(INC_DIR)
Expand All @@ -70,6 +71,8 @@ DEMO_O_FILES = $(OBJ_DIR)/demo_get.o
DEMO_O_FILES += $(OBJ_DIR)/demo_create.o
DEMO_O_FILES += $(OBJ_DIR)/demo_destroy.o
DEMO_O_FILES += $(OBJ_DIR)/demo_query.o
DEMO_O_FILES += $(OBJ_DIR)/demo_activate.o
DEMO_O_FILES += $(OBJ_DIR)/demo_locate.o

TEST_O_FILES = $(OBJ_DIR)/tests.o

Expand All @@ -86,7 +89,9 @@ demos: objs \
$(BIN_DIR)/demo_get \
$(BIN_DIR)/demo_create \
$(BIN_DIR)/demo_destroy \
$(BIN_DIR)/demo_query
$(BIN_DIR)/demo_query \
$(BIN_DIR)/demo_activate \
$(BIN_DIR)/demo_locate

tests: objs \
$(TEST_O_FILES) \
Expand All @@ -104,6 +109,10 @@ $(BIN_DIR)/demo_destroy: $(OBJ_DIR)/demo_destroy.o $(SRC_O_FILES)
$(CC) $(LDFLAGS) -o $@ $^ $(LDLIBS)
$(BIN_DIR)/demo_query: $(OBJ_DIR)/demo_query.o $(SRC_O_FILES)
$(CC) $(LDFLAGS) -o $@ $^ $(LDLIBS)
$(BIN_DIR)/demo_activate: $(OBJ_DIR)/demo_activate.o $(SRC_O_FILES)
$(CC) $(LDFLAGS) -o $@ $^ $(LDLIBS)
$(BIN_DIR)/demo_locate: $(OBJ_DIR)/demo_locate.o $(SRC_O_FILES)
$(CC) $(LDFLAGS) -o $@ $^ $(LDLIBS)

$(BIN_DIR)/tests: $(TEST_O_FILES) $(OBJ_DIR)/kmip.o $(OBJ_DIR)/kmip_io.o $(OBJ_DIR)/kmip_memset.o
$(CC) $(LDFLAGS) -o $@ $^
Expand All @@ -126,6 +135,10 @@ $(OBJ_DIR)/demo_destroy.o: $(DEMO_DIR)/demo_destroy.c $(H_FILES)
$(CC) $(CFLAGS) $(INC_FLAGS) -c $< -o $@
$(OBJ_DIR)/demo_query.o: $(DEMO_DIR)/demo_query.c $(H_FILES)
$(CC) $(CFLAGS) $(INC_FLAGS) -c $< -o $@
$(OBJ_DIR)/demo_activate.o: $(DEMO_DIR)/demo_activate.c $(H_FILES)
$(CC) $(CFLAGS) $(INC_FLAGS) -c $< -o $@
$(OBJ_DIR)/demo_locate.o: $(DEMO_DIR)/demo_locate.c $(H_FILES)
$(CC) $(CFLAGS) $(INC_FLAGS) -c $< -o $@

$(OBJ_DIR)/tests.o: $(TEST_DIR)/tests.c $(INC_DIR)/kmip.h $(INC_DIR)/kmip_io.h $(INC_DIR)/kmip_memset.h
$(CC) $(CFLAGS) $(INC_FLAGS) -c $< -o $@
Expand Down Expand Up @@ -175,6 +188,8 @@ install: all
cp $(BIN_DIR)/demo_get $(DEST_DIR)$(PREFIX)/bin/$(KMIP)
cp $(BIN_DIR)/demo_destroy $(DEST_DIR)$(PREFIX)/bin/$(KMIP)
cp $(BIN_DIR)/demo_query $(DEST_DIR)$(PREFIX)/bin/$(KMIP)
cp $(BIN_DIR)/demo_activate $(DEST_DIR)$(PREFIX)/bin/$(KMIP)
cp $(BIN_DIR)/demo_locate $(DEST_DIR)$(PREFIX)/bin/$(KMIP)
cp -r $(DOCS_DIR)/source/. $(DEST_DIR)$(PREFIX)/share/doc/$(KMIP)/src
cp $(SRC_DIR)/*.c $(DEST_DIR)$(PREFIX)/src/$(KMIP)
cp $(INC_DIR)/*.h $(DEST_DIR)$(PREFIX)/include/$(KMIP)
Expand Down
202 changes: 202 additions & 0 deletions demos/demo_activate.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
/* Copyright (c) 2018 The Johns Hopkins University/Applied Physics Laboratory
* All Rights Reserved.
*
* This file is dual licensed under the terms of the Apache 2.0 License and
* the BSD 3-Clause License. See the LICENSE file in the root of this
* repository for more information.
*/

#include <openssl/err.h>
#include <openssl/ssl.h>
#include <stdio.h>
#include <string.h>
#include <time.h>

#include "kmip.h"
#include "kmip_io.h"
#include "kmip_bio.h"

void
print_help(const char *app)
{
printf("Usage: %s [flag value | flag] ...\n\n", app);
printf("Flags:\n");
printf("-a addr : the IP address of the KMIP server\n");
printf("-c path : path to client certificate file\n");
printf("-h : print this help info\n");
printf("-i id : the ID of the symmetric key to activate\n");
printf("-k path : path to client key file\n");
printf("-p port : the port number of the KMIP server\n");
printf("-r path : path to CA certificate file\n");
}

int
parse_arguments(int argc, char **argv,
char **server_address, char **server_port,
char **client_certificate, char **client_key, char **ca_certificate,
char **id,
int *print_usage)
{
if(argc <= 1)
{
print_help(argv[0]);
return(-1);
}

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], "-i", 2) == 0)
{
*id = argv[++i];
}
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]);
print_help(argv[0]);
return(-1);
}
}

return(0);
}

int
use_high_level_api(BIO* bio,
char *id)
{
/* Send the request message. */
int result = kmip_bio_activate_symmetric_key(bio, id, kmip_strnlen_s(id, 128));

/* Handle the response results. */
printf("\n");
if(result < 0)
{
printf("An error occurred while activating object: %s\n", id);
printf("Error Code: %d\n", result);
}
else
{
printf("The KMIP operation was executed with no errors.\n");
printf("Result: ");
kmip_print_result_status_enum(stdout, result);
printf(" (%d)\n", result);
}

return(result);
}

int
main(int argc, char **argv)
{
char *server_address = NULL;
char *server_port = NULL;
char *client_certificate = NULL;
char *client_key = NULL;
char *ca_certificate = NULL;
char *id = NULL;
int help = 0;

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

/* Set up the TLS connection to the KMIP server. */
SSL_CTX *ctx = NULL;
SSL *ssl = NULL;
#if (OPENSSL_VERSION_NUMBER >= 0x10100000L)
OPENSSL_init_ssl(0, NULL);
ctx = SSL_CTX_new(TLS_client_method());
#else
SSL_library_init();
ctx = SSL_CTX_new(SSLv23_client_method());
#endif

printf("\n");
printf("Loading the client certificate: %s\n", client_certificate);
if(SSL_CTX_use_certificate_file(ctx, client_certificate, SSL_FILETYPE_PEM) != 1)
{
fprintf(stderr, "Loading the client certificate failed\n");
ERR_print_errors_fp(stderr);
SSL_CTX_free(ctx);
return(-1);
}

printf("Loading the client key: %s\n", client_key);
if(SSL_CTX_use_PrivateKey_file(ctx, client_key, SSL_FILETYPE_PEM) != 1)
{
fprintf(stderr, "Loading the client key failed\n");
ERR_print_errors_fp(stderr);
SSL_CTX_free(ctx);
return(-1);
}

printf("Loading the CA certificate: %s\n", ca_certificate);
if(SSL_CTX_load_verify_locations(ctx, ca_certificate, NULL) != 1)
{
fprintf(stderr, "Loading the CA file failed\n");
ERR_print_errors_fp(stderr);
SSL_CTX_free(ctx);
return(-1);
}

BIO *bio = NULL;
bio = BIO_new_ssl_connect(ctx);
if(bio == NULL)
{
fprintf(stderr, "BIO_new_ssl_connect failed\n");
ERR_print_errors_fp(stderr);
SSL_CTX_free(ctx);
return(-1);
}

BIO_get_ssl(bio, &ssl);
SSL_set_mode(ssl, SSL_MODE_AUTO_RETRY);
BIO_set_conn_hostname(bio, server_address);
BIO_set_conn_port(bio, server_port);
if(BIO_do_connect(bio) != 1)
{
fprintf(stderr, "BIO_do_connect failed\n");
ERR_print_errors_fp(stderr);
BIO_free_all(bio);
SSL_CTX_free(ctx);
return(-1);
}

int result = use_high_level_api(bio, id);

BIO_free_all(bio);
SSL_CTX_free(ctx);

return(result);
}
Loading