简体   繁体   中英

DataStax C/C++ Driver for Apache Cassandra: Blob Conversion (GCrypt Crypto Key) Issue

I successfully add a GCrypt Crypto Key (0x135aa10) as a CassBytes structure to a Cassandra Cluster. See Cassandra C++ include: https://github.com/datastax/cpp-driver/blob/1.0/include/cassandra.h .

// Create PSK Blob
CassBytes pskey_blob;
pskey_blob = cass_bytes_init((const cass_byte_t*) pskey, sizeof(pskey));

// Check that key BLOBED correctly
gcry_sexp_t tmpkey = (gcry_sexp_t)pskey_blob.data; //This correctly returns GCrypt Crypto Key (0x135aa10)

// Bind PSK Blob & Execute Statment
cass_statement_bind_bytes(statement, 0, pskey_blob);
future = cass_session_execute(session, statement);
cass_future_wait(future);

I then retrieve GCrypt Crypto Key from Cassandra as follows:

// Get Payload Secret Key
CassBytes pskey_blob;
cass_value_get_bytes(cass_row_get_column(row, 0), &pskey_blob);

if (pskey_blob.size != 0) {
  pskey = (gcry_sexp_t)pskey_blob.data;
  std::cout << "pskey: " << pskey << std::endl;
}

The blob data is returned within the CassBytes structure (pskey_blob.data) as 0x7f7fb00028a4 with the correct size of 8 (pskey_blob.size). It seems that Cassandra returns a HEX representation of my original unit8_t type GCrypt Crypto Key (0x135aa10) as HEX (0x7f7fb00028a4).

Does anyone know how to convert this from the Cassandra byte representation to the GCrypt Crypto Key?

In the code above it looks like you're only copying the address of the pskey to Cassandra not the content of the key.

I've only superficially looked at the GCrypt docs, but it looks like you can print/scan the GCrypt S-Expr to a string buffer using gcry_sexp_sprint() and one of gcry_sexp_new() , gcry_sexp_create() , gcry_sexp_scan() .

Docs found here: https://www.gnupg.org/documentation/manuals/gcrypt/Working-with-S_002dexpressions.html#Working-with-S_002dexpressions

Disclaimer: I haven't compiled or tested this code. It's just a rough outline of what might work.

To INSERT the key might look like this:

size_t length = gcry_sexp_sprint(pskey, GCRYSEXP_FMT_DEFAULT, NULL, 0); // Needed to get the length
char* buffer = (char*)malloc(length);
gcry_sexp_sprint(pskey, GCRYSEXP_FMT_DEFAULT, buffer, length);

cass_statement_bind_string(statement, 0, cass_string_init2(buffer, length));
future = cass_session_execute(session, statement);
cass_future_wait(future);

free(buffer);

To SELECT the key might look like this:

CassString pskey_blob;
cass_value_get_string(cass_row_get_column(row, 0), &pskey_blob);
gcry_sexp_new(&pskey, pskey_blob.data, pskey_blog.length, 1); // Need to check the error here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM