diff --git a/kmippp/CMakeLists.txt b/kmippp/CMakeLists.txt index 5f7183b..ad5dc8d 100644 --- a/kmippp/CMakeLists.txt +++ b/kmippp/CMakeLists.txt @@ -43,6 +43,7 @@ add_pp_demo(get_name) add_pp_demo(register) add_pp_demo(locate) add_pp_demo(all) +add_pp_demo(all_secrets) add_pp_demo(revoke) add_pp_demo(get_secret) add_pp_demo(register_secret) diff --git a/kmippp/demo_all_secrets.cpp b/kmippp/demo_all_secrets.cpp new file mode 100644 index 0000000..5b4532a --- /dev/null +++ b/kmippp/demo_all_secrets.cpp @@ -0,0 +1,41 @@ + + +#include "kmippp.h" +#include + +int +main (int argc, char **argv) +{ + + if (argc < 6) + { + std::cerr << "Usage: demo_locate " + " [group_name]" + << std::endl; + return -1; + } + + kmippp::context ctx (argv[1], argv[2], argv[3], argv[4], argv[5]); + // auto keys = ctx.op_all_secrets(); + const std::string group = argv[6]!=nullptr? argv[6] : "TestGroup"; + auto keys = ctx.op_locate_secrets_by_group (group); + if(keys.empty ()) + { + std::cerr << "No Secret Data found" << std::endl; + std::cerr << ctx.get_last_result () << std::endl; + return 1; + } + for (auto id : keys) + { + std::cout << "Key: " << id << " "; + auto secret = ctx.op_get_secret (id); + auto secret_name = ctx.op_get_name_attr (id); + std::cout << secret_name << " 0x"; + for (auto const &c : secret) + { + std::cout << std::hex << ((int)c); + } + std::cout << std::endl; + } + return 0; +} diff --git a/kmippp/kmippp.cpp b/kmippp/kmippp.cpp index ec76ca5..9157829 100644 --- a/kmippp/kmippp.cpp +++ b/kmippp/kmippp.cpp @@ -380,6 +380,67 @@ context::op_locate_by_group (context::name_t group) return ret; } +context::ids_t +context::op_locate_secrets_by_group (context::name_t group) +{ + Attribute a[2]; + for (int i = 0; i < 2; i++) + { + kmip_init_attribute (&a[i]); + } + + object_type loctype = KMIP_OBJTYPE_SECRET_DATA; + a[0].type = KMIP_ATTR_OBJECT_TYPE; + a[0].value = &loctype; + + TextString ts2 = { 0, 0 }; + ts2.value = const_cast (group.c_str ()); + ts2.size = kmip_strnlen_s (ts2.value, 250); + a[1].type = KMIP_ATTR_OBJECT_GROUP; + a[1].value = &ts2; + + TemplateAttribute ta = { 0 }; + ta.attributes = a; + ta.attribute_count = ARRAY_LENGTH (a); + + int upto = 0; + int all = 1; // TMP + ids_t ret; + + LocateResponse locate_result; + + while (upto < all) + { + int result = kmip_bio_locate (bio_, a, 2, &locate_result, 16, upto); + + if (result != 0) + { + return {}; + } + + for (int i = 0; i < locate_result.ids_size; ++i) + { + ret.push_back (locate_result.ids[i]); + } + if (locate_result.located_items != 0) + { + all = locate_result.located_items; // shouldn't change after its != 1 + } + else + { + // Dummy server sometimes returns 0 for located_items + all += locate_result.ids_size; + if (locate_result.ids_size == 0) + { + --all; + } + } + upto += locate_result.ids_size; + } + + return ret; +} + context::ids_t context::op_all () { @@ -431,6 +492,57 @@ context::op_all () return ret; } +context::ids_t +context::op_all_secrets () +{ + Attribute a[1]; + for (int i = 0; i < 1; i++) + { + kmip_init_attribute (&a[i]); + } + + object_type loctype = KMIP_OBJTYPE_SECRET_DATA; + a[0].type = KMIP_ATTR_OBJECT_TYPE; + a[0].value = &loctype; + + LocateResponse locate_result; + + int upto = 0; + int all = 1; // TMP + ids_t ret; + + while (upto < all) + { + int result = kmip_bio_locate (bio_, a, 1, &locate_result, 16, upto); + + if (result != 0) + { + return {}; + } + + for (int i = 0; i < locate_result.ids_size; ++i) + { + ret.push_back (locate_result.ids[i]); + } + if (locate_result.located_items != 0) + { + all = locate_result.located_items; // shouldn't change after its != 1 + } + else + { + // Dummy server sometimes returns 0 for located_items + all += locate_result.ids_size; + if (locate_result.ids_size == 0) + { + --all; + } + } + upto += locate_result.ids_size; + } + + return ret; +} + bool context::op_revoke (id_t id, int reason, name_t message, time_t occurrence_time) { diff --git a/kmippp/kmippp.h b/kmippp/kmippp.h index ac595c7..72031d2 100644 --- a/kmippp/kmippp.h +++ b/kmippp/kmippp.h @@ -55,12 +55,16 @@ class context ids_t op_locate_by_group (name_t group); + ids_t op_locate_secrets_by_group (name_t group); + bool op_destroy (id_t id); // KMIP::locate operation, retrieve all symmetric keys // note: name can be empty, and will retrieve all keys ids_t op_all (); + ids_t op_all_secrets (); + // KMIP::revoke operation, revoke activated or not activated key. Deactivates // active key bool op_revoke (id_t id, int reason, name_t message, time_t occurrence_time); diff --git a/libkmip/include/libkmip_version.h b/libkmip/include/libkmip_version.h index 92f7d09..a574dff 100644 --- a/libkmip/include/libkmip_version.h +++ b/libkmip/include/libkmip_version.h @@ -4,7 +4,7 @@ #define KMIP_LIB_VERSION_MAJOR 0 #define KMIP_LIB_VERSION_MINOR 3 -#define KMIP_LIB_VERSION_PATCH 1 +#define KMIP_LIB_VERSION_PATCH 2 #define KMIP_LIB_STRINGIFY_I(x) #x #define KMIP_LIB_TOSTRING_I(x) KMIP_LIB_STRINGIFY_I (x)