Multiple memory fixes, mostly in tests

This commit is contained in:
John Jones 2017-04-26 23:56:18 -05:00
parent 3463196f9c
commit 8ce1c32ec4
10 changed files with 220 additions and 87 deletions

View file

@ -37,6 +37,7 @@ struct Connection* libp2p_conn_tcp_dial(const struct TransportDialer* transport_
if (!multiaddress_get_ip_address(addr, &ip)) if (!multiaddress_get_ip_address(addr, &ip))
return NULL; return NULL;
struct hostent* host = gethostbyname(ip); struct hostent* host = gethostbyname(ip);
free(ip);
struct in_addr** addr_list = (struct in_addr**)host->h_addr_list; struct in_addr** addr_list = (struct in_addr**)host->h_addr_list;
socket_connect4(conn->socket_handle, (*addr_list[0]).s_addr, port); socket_connect4(conn->socket_handle, (*addr_list[0]).s_addr, port);
conn->read = libp2p_conn_tcp_read; conn->read = libp2p_conn_tcp_read;

View file

@ -19,7 +19,8 @@
int libp2p_net_multistream_close(void* stream_context) { int libp2p_net_multistream_close(void* stream_context) {
struct SessionContext* secure_context = (struct SessionContext*)stream_context; struct SessionContext* secure_context = (struct SessionContext*)stream_context;
struct Stream* stream = secure_context->insecure_stream; struct Stream* stream = secure_context->insecure_stream;
close( *((int*)stream->socket_descriptor)); int socket_descriptor = *((int*)stream->socket_descriptor);
close(socket_descriptor);
return 1; return 1;
} }

View file

@ -323,6 +323,7 @@ int libp2p_secio_stretch_keys(char* cipherType, char* hashType, unsigned char* s
mbedtls_md_hmac_update(&ctx, a_hash, 32); mbedtls_md_hmac_update(&ctx, a_hash, 32);
mbedtls_md_hmac_finish(&ctx, a_hash); mbedtls_md_hmac_finish(&ctx, a_hash);
} }
mbedtls_md_free(&ctx);
// now we have a big result. Cut it up into pieces // now we have a big result. Cut it up into pieces
if (temp != NULL) if (temp != NULL)
@ -528,31 +529,37 @@ int libp2p_secio_unencrypted_read(struct SessionContext* session, unsigned char*
*/ */
int libp2p_secio_encrypt(const struct SessionContext* session, const unsigned char* incoming, size_t incoming_size, unsigned char** outgoing, size_t* outgoing_size) { int libp2p_secio_encrypt(const struct SessionContext* session, const unsigned char* incoming, size_t incoming_size, unsigned char** outgoing, size_t* outgoing_size) {
unsigned char* buffer = NULL; unsigned char* buffer = NULL;
size_t buffer_size = 0; size_t buffer_size = 0, original_buffer_size = 0;
//TODO switch between ciphers //TODO switch between ciphers
mbedtls_cipher_context_t cipher_ctx; mbedtls_cipher_context_t cipher_ctx;
mbedtls_cipher_init(&cipher_ctx); mbedtls_cipher_init(&cipher_ctx);
mbedtls_cipher_setup(&cipher_ctx, mbedtls_cipher_info_from_type(MBEDTLS_CIPHER_AES_256_CTR)); mbedtls_cipher_setup(&cipher_ctx, mbedtls_cipher_info_from_type(MBEDTLS_CIPHER_AES_256_CTR));
mbedtls_cipher_setkey(&cipher_ctx, session->local_stretched_key->cipher_key, session->local_stretched_key->cipher_size * 8, MBEDTLS_ENCRYPT); mbedtls_cipher_setkey(&cipher_ctx, session->local_stretched_key->cipher_key, session->local_stretched_key->cipher_size * 8, MBEDTLS_ENCRYPT);
buffer_size = incoming_size + mbedtls_cipher_get_block_size(&cipher_ctx) + 32; original_buffer_size = incoming_size;
buffer = malloc(buffer_size); original_buffer_size += 32;
memset(buffer, 0, buffer_size); buffer_size = original_buffer_size;
buffer = malloc(original_buffer_size);
memset(buffer, 0, original_buffer_size);
mbedtls_cipher_crypt(&cipher_ctx, session->local_stretched_key->iv, session->local_stretched_key->iv_size, incoming, incoming_size, buffer, &buffer_size); mbedtls_cipher_crypt(&cipher_ctx, session->local_stretched_key->iv, session->local_stretched_key->iv_size, incoming, incoming_size, buffer, &buffer_size);
// Now, buffer size may be set differently than original_buffer_size
// The "incoming" is now encrypted, and is in the first part of the buffer
mbedtls_cipher_free(&cipher_ctx); mbedtls_cipher_free(&cipher_ctx);
// mac the data, and append it // mac the data
mbedtls_md_context_t ctx; mbedtls_md_context_t ctx;
mbedtls_md_setup(&ctx, &mbedtls_sha256_info, 1); mbedtls_md_setup(&ctx, &mbedtls_sha256_info, 1);
mbedtls_md_hmac_starts(&ctx, session->local_stretched_key->mac_key, session->local_stretched_key->mac_size); mbedtls_md_hmac_starts(&ctx, session->local_stretched_key->mac_key, session->local_stretched_key->mac_size);
mbedtls_md_hmac_update(&ctx, buffer, buffer_size); mbedtls_md_hmac_update(&ctx, buffer, buffer_size);
// this will tack the mac onto the end of the buffer
mbedtls_md_hmac_finish(&ctx, &buffer[buffer_size]); mbedtls_md_hmac_finish(&ctx, &buffer[buffer_size]);
mbedtls_md_free(&ctx); mbedtls_md_free(&ctx);
// put it all in outgoing // put it all in outgoing
*outgoing_size = buffer_size + 32; *outgoing_size = original_buffer_size;
*outgoing = malloc(*outgoing_size); *outgoing = malloc(*outgoing_size);
memcpy(*outgoing, buffer, *outgoing_size); memset(*outgoing, 0, *outgoing_size);
memcpy(*outgoing, buffer, original_buffer_size);
free(buffer); free(buffer);
return 1; return 1;
@ -615,9 +622,11 @@ int libp2p_secio_decrypt(const struct SessionContext* session, const unsigned ch
buffer_size = data_section_size + mbedtls_cipher_get_block_size(&cipher_ctx); buffer_size = data_section_size + mbedtls_cipher_get_block_size(&cipher_ctx);
buffer = malloc(buffer_size); buffer = malloc(buffer_size);
mbedtls_cipher_update(&cipher_ctx, incoming, data_section_size, buffer, &buffer_size); mbedtls_cipher_update(&cipher_ctx, incoming, data_section_size, buffer, &buffer_size);
mbedtls_cipher_free(&cipher_ctx);
*outgoing = malloc(buffer_size); *outgoing = malloc(buffer_size);
*outgoing_size = buffer_size; *outgoing_size = buffer_size;
memcpy(*outgoing, buffer, buffer_size); memcpy(*outgoing, buffer, buffer_size);
free(buffer);
return *outgoing_size; return *outgoing_size;
} }
@ -629,14 +638,19 @@ int libp2p_secio_decrypt(const struct SessionContext* session, const unsigned ch
* @returns the number of bytes read * @returns the number of bytes read
*/ */
int libp2p_secio_encrypted_read(void* stream_context, unsigned char** bytes, size_t* num_bytes, int timeout_secs) { int libp2p_secio_encrypted_read(void* stream_context, unsigned char** bytes, size_t* num_bytes, int timeout_secs) {
int retVal = 0;
struct SessionContext* session = (struct SessionContext*)stream_context; struct SessionContext* session = (struct SessionContext*)stream_context;
// reader uses the remote cipher and mac // reader uses the remote cipher and mac
// read the data // read the data
unsigned char* incoming = NULL; unsigned char* incoming = NULL;
size_t incoming_size = 0; size_t incoming_size = 0;
if (libp2p_secio_unencrypted_read(session, &incoming, &incoming_size, timeout_secs) <= 0) if (libp2p_secio_unencrypted_read(session, &incoming, &incoming_size, timeout_secs) <= 0)
return 0; goto exit;
return libp2p_secio_decrypt(session, incoming, incoming_size, bytes, num_bytes); retVal = libp2p_secio_decrypt(session, incoming, incoming_size, bytes, num_bytes);
exit:
if (incoming != NULL)
free(incoming);
return retVal;
} }
/*** /***

View file

@ -40,23 +40,26 @@ int test_base58_size() {
size_t encoded_length = libp2p_crypto_encoding_base58_encode_size(string_length); size_t encoded_length = libp2p_crypto_encoding_base58_encode_size(string_length);
if (encoded_length != 20) if (encoded_length != 20) {
fprintf(stderr, "Encoded length incorrect. Should have been 20 and is %lu\n", encoded_length);
return 0; return 0;
}
size_t encoded_max_length = 100; size_t encoded_max_length = 100;
unsigned char encoded[encoded_max_length]; unsigned char encoded[encoded_max_length];
unsigned char* ptr = encoded; unsigned char* ptr = encoded;
// now encode it // now encode it
libp2p_crypto_encoding_base58_encode(unencoded, string_length, &ptr, &encoded_max_length); libp2p_crypto_encoding_base58_encode(unencoded, string_length-1, &ptr, &encoded_max_length);
size_t decoded_length = libp2p_crypto_encoding_base58_decode_size(encoded_max_length); size_t decoded_length = libp2p_crypto_encoding_base58_decode_size(encoded_max_length);
if (decoded_length != string_length) if (decoded_length != string_length) {
fprintf(stderr, "String length and decoded length are different. Decoded length = %lu and string length is %lu\n", decoded_length, string_length);
return 0; return 0;
}
return 1; return 1;
} }
int test_base58_max_size() { int test_base58_max_size() {

View file

@ -10,7 +10,7 @@ int test_protobuf_private_key() {
// this is a base64 encoded private key. It makes it easier to test if it is in base64 form // this is a base64 encoded private key. It makes it easier to test if it is in base64 form
// these were pulled from the GO version of ipfs // these were pulled from the GO version of ipfs
char* or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dWtcKxaNlimMw8=";
struct PrivateKey* private_key = libp2p_crypto_private_key_new(); struct PrivateKey* private_key = NULL;
struct RsaPrivateKey rsa_private_key = {0}; struct RsaPrivateKey rsa_private_key = {0};
char* orig_peer_id = "QmbTyKkUuv6yaSpTuCFq1Ft6Q3g4wTtFJk1BLGMPRdAEP8"; char* orig_peer_id = "QmbTyKkUuv6yaSpTuCFq1Ft6Q3g4wTtFJk1BLGMPRdAEP8";
size_t orig_peer_id_size = strlen(orig_peer_id); size_t orig_peer_id_size = strlen(orig_peer_id);
@ -57,5 +57,13 @@ int test_protobuf_private_key() {
exit: exit:
if (decode_base64 != NULL) if (decode_base64 != NULL)
free(decode_base64); free(decode_base64);
if (private_key != NULL)
libp2p_crypto_private_key_free(private_key);
if (rsa_private_key.der != NULL)
free(rsa_private_key.der);
if (rsa_private_key.public_key_der != NULL)
free(rsa_private_key.public_key_der);
if (final_id != NULL)
free(final_id);
return retVal; return retVal;
} }

View file

@ -10,115 +10,159 @@
#include "libp2p/crypto/peerutils.h" #include "libp2p/crypto/peerutils.h"
#include "libp2p/crypto/key.h" #include "libp2p/crypto/key.h"
void free_private_key_ders(struct RsaPrivateKey* pk) {
if (pk->der != NULL)
free(pk->der);
if (pk->public_key_der != NULL)
free(pk->public_key_der);
}
/** /**
* make sure we can get a DER formatted result * make sure we can get a DER formatted result
*/ */
int test_crypto_rsa_private_key_der() { int test_crypto_rsa_private_key_der() {
int retVal = 0;
struct RsaPrivateKey private_key; struct RsaPrivateKey private_key;
libp2p_crypto_rsa_generate_keypair(&private_key, 2048); libp2p_crypto_rsa_generate_keypair(&private_key, 2048);
if (private_key.der_length == 0) if (private_key.der_length == 0)
return 0; goto exit;
if (private_key.der == NULL) if (private_key.der == NULL)
return 0; goto exit;
return 1; retVal = 1;
exit:
free_private_key_ders(&private_key);
return retVal;
} }
int test_crypto_x509_der_to_private2() { int test_crypto_x509_der_to_private2() {
int retVal = 0;
// this is an example private key. The type is not added. Therefore, it is not compatible with the go version // this is an example private key. The type is not added. Therefore, it is not compatible with the go version
char* der = "MIIEogIBAAKCAQEAmpDhRGecWN+MwfxW9UqsyEsZwnROHb2hCRBUATh7e0thgyi5Te6lT9x2++89x1sC7B9Egma1AJvY7BinIJYeFiOwzkKDe6/JxCle6TmLtCO1qmb8aGOIlu8NzO3L8EjcriYcgp0J5sZn5I3B4iU6q78u5jJQFpi2V7wsasanmfFwfF9ZUSxAnuwkcRkuhGnp5sBauHsQ4dn3IaiutiZDxPsAdxQyXX0SdrB4ew72UVAI2UsMAj5fRYy3plrsV/zZsGpeT6SDJI28weqOoSGvLzYaowf583MrH8O8dFoOWfXHq8hXy6qCDFOa7UYICCkb4QU9SLP7embmtfnB5/bVKwIDAQABAoIBADAwGwsIgmXyzB9uXG386gFH6LAHMpNzG1GIFaiLw3Oc/Lm3aI4zaLaNNUs2Ozx7011qIiHFg4i9DdQNm409QAQG/IhRlExrcawGeeCcYEG5IFoP4YFqBpuHy/Wn7XzsOmDQ4PKXow6frKREzb2Dfdcts6Fw7icdVTvlHrPrWzVS4azROsKy1spaCG7gYGd968f14Q2NBJmmlO75+dhjliI+cmMq5TaEvuCilXfSK4v6RWRGx+Mjl+ATSN1V7ksgzYsjNE15fdguxVWEIW5x5IsrpVviZdfGsDNWGsW9upoHoP118i2HKgrHRgbNcvTvWN/vCqJ/wDGz2FkYaa/3sYECgYEA9mBkSMW/Uc6m9/FNqlsEqCKLryI3ksxW9Ea6pHQ2p79gXYnlKuBLs7VdOA4XBIbV1vQdxjwJfojOdG7DmjklXYf+AYL/x26NSTszVV5feIIfR1jko67izBjeahPdizmpgQDFstgfdsc6vMKsbamvHzlZjpWeyanBz1t6OFPB8KUCgYEAoJpw2JB3o6ws4dASsSs4gthusY7e1Gcp0d7z8ZH0o8Rk2jxnQZuKHFUNnLY+qwxb6CP4zCpMMeGwuBVTapCE+7UkzUEk5GKmEgtO5G4Eu2yJFib1aGITlDnvxRkDOUGMSc/PVB5TnpdF9qXYbaJoLCziRzzgPJ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der)); size_t b64_length = libp2p_crypto_encoding_base64_decode_size(strlen(der));
unsigned char buffer[b64_length]; unsigned char buffer[b64_length];
unsigned char* b = buffer; unsigned char* b = buffer;
size_t ultimate_length; size_t ultimate_length;
int retVal = libp2p_crypto_encoding_base64_decode((unsigned char*)der, strlen(der), b, b64_length, &ultimate_length); if (!libp2p_crypto_encoding_base64_decode((unsigned char*)der, strlen(der), b, b64_length, &ultimate_length))
if (retVal == 0) goto exit;
return 0;
// we now have the bytes // we now have the bytes
struct RsaPrivateKey private_key = {0}; struct RsaPrivateKey private_key = {0};
retVal = libp2p_crypto_encoding_x509_der_to_private_key(b, ultimate_length, &private_key); if (!libp2p_crypto_encoding_x509_der_to_private_key(b, ultimate_length, &private_key))
if (retVal == 0) goto exit;
return 0; if (private_key.D <= 0)
return private_key.D > 0; goto exit;
retVal = 1;
exit:
free_private_key_ders(&private_key);
return retVal;
} }
int test_crypto_x509_der_to_private() { int test_crypto_x509_der_to_private() {
int retVal = 0;
// this is a base64 encoded string from the go version of ipfs // this is a base64 encoded string from the go version of ipfs
char* der = "CAASpwkwggSjAgEAAoIBAQDTDJBWjDzS/HxDNOHazvzH2bu9CPMVHUrrvKRdBUM5ansL6/CC3MVZ6HVm4O6QHRapN6EF2CbrTgI4KBOXIL125Xo8MlROnyfXYk3O5q2tgwL/MbW8kXjtkyCfBak7MUoLOdLU7Svg0gkl3l+uDAiDcCLnwJVcFfq9ch6z4wMOhYJqE5dtx0uXxn6IuKWl1B69FTvBXCc0thw8Rw54b941FDcsBH5ttV9mRNCJym3poZ5qalNgXlxoIIB+PUx5QD+aq7KMJdpAX8HkapBntCOahP/QUceRmma0grlZLeYkH6/oi/hIrM6se3KUZ+F6tBuDFys8UAZy/X2BCUbKjbxtAgMBAAECggEANWfQfpYuLhXGPBt9q6kFPm1SnJtPJ+CpvM2XqhJS2IyhZnrl+bd0GTRBwS7aL42s1lVFYf04nAK5fQxnKK8YQqX/MIxr2RldM5ukpN6qxGWKtJkXrAgD2d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pXcNWhABKeNmqndvqyQ/8HNCv/j8AdraGB2DGO57Xso5J0CQ43W/U9+QIyjwKBgHLL2hw3o+wXaRO3WMUPUmVM2zdRgR0suybp5a7Vqb0H5NZrohUw4NulIzJ8H6Q2VjMzJL6Q9sGu2HepF6ecTtBa7ErqtiVlG4Dr1aCOs5XhYEWBMlwxX+JKSt4Cn+UVoTB7Cy5lEhn7JurX0Xuy0ylXMWoIKKv89cs5eg6quzTBAoGAaq9eEztLjKCWXOE9SetBdYnG8aunb9cqaJlwgu/h0bfXPVDYBbAUSEyLURY4MQI7Q1tM3Pu9iqfEmUZj7/LoIV5mg6X9RX/alT6etk3+dF+9nlqN1OU9U9cCtZ/rTcb2y5EptJcidRH/eCFY/pTV/PcttOJPx/S4kHcroC+N8MUCgYEA6DA5QHxHfNN6Nxv+pEzy2DIxFe9RrBxS+KPBsra1C8jgdeMf4EmfU0Nox92V0q0bRrD5ztqQwSONI0hSRb1iiMWR6MuFnAFajUJfASjjIlZ6nIQjQslI7vjlvYyyHS/p/Codxap+yJlTLWwVEOXp2D9pWwiMq1xEyf0TH1BosvM=";
size_t b64_length = libp2p_crypto_encoding_base64_decode_size(strlen(der)); size_t b64_length = libp2p_crypto_encoding_base64_decode_size(strlen(der));
unsigned char buffer[b64_length]; unsigned char buffer[b64_length];
unsigned char* b = buffer; unsigned char* b = buffer;
size_t ultimate_length; size_t ultimate_length;
int retVal = libp2p_crypto_encoding_base64_decode((unsigned char*)der, strlen(der), b, b64_length, &ultimate_length); if (!libp2p_crypto_encoding_base64_decode((unsigned char*)der, strlen(der), b, b64_length, &ultimate_length))
if (retVal == 0) goto exit;
return 0;
// we now have the bytes, but we must strip off the type (5 bytes) // we now have the bytes, but we must strip off the type (5 bytes)
struct RsaPrivateKey* private_key = libp2p_crypto_rsa_rsa_private_key_new(); struct RsaPrivateKey* private_key = libp2p_crypto_rsa_rsa_private_key_new();
int bytesToStrip = 5; int bytesToStrip = 5;
retVal = libp2p_crypto_encoding_x509_der_to_private_key(&b[bytesToStrip], ultimate_length-bytesToStrip, private_key); if (!libp2p_crypto_encoding_x509_der_to_private_key(&b[bytesToStrip], ultimate_length-bytesToStrip, private_key))
if (retVal == 0) goto exit;
return 0;
retVal = private_key->D > 0; retVal = private_key->D > 0;
exit:
libp2p_crypto_rsa_rsa_private_key_free(private_key); libp2p_crypto_rsa_rsa_private_key_free(private_key);
return retVal; return retVal;
} }
int test_public_der_to_private_der() { void printKey(unsigned char* in, size_t in_size) {
struct RsaPrivateKey private_key; for (int i = 0; i < in_size; i++) {
int retVal = libp2p_crypto_rsa_generate_keypair(&private_key, 2048); fprintf(stdout, "%02x ", in[i]);
if (retVal == 0) }
return 0; fprintf(stdout, "\n");
return;
}
if (private_key.der_length == 0) int test_public_der_to_private_der() {
int retVal = 0;
struct RsaPrivateKey private_key;
size_t public_der_temp_length = 0;
size_t public_der_temp_length2 = 0;
if (!libp2p_crypto_rsa_generate_keypair(&private_key, 2048)) {
fprintf(stderr, "Unable to generate keypair\n");
return 0; return 0;
if (private_key.der == NULL) }
if (private_key.der_length == 0) {
fprintf(stderr, "Private Key's DER length should not be zero\n");
return 0; return 0;
}
if (private_key.der == NULL) {
fprintf(stderr, "Private Key's DER should not be null\n");
return 0;
}
// copy the public DER to a temporary area, then erase it, then try to generate it again. // copy the public DER to a temporary area, then erase it, then try to generate it again.
size_t public_der_temp_length = private_key.public_key_length; public_der_temp_length = private_key.public_key_length;
unsigned char public_der_temp[private_key.public_key_length]; unsigned char public_der_temp[private_key.public_key_length];
memcpy(public_der_temp, private_key.public_key_der, private_key.public_key_length); memcpy(public_der_temp, private_key.public_key_der, private_key.public_key_length);
free(private_key.public_key_der); free(private_key.public_key_der);
private_key.public_key_length = 0; private_key.public_key_length = 0;
retVal = libp2p_crypto_rsa_private_key_fill_public_key(&private_key); if (!libp2p_crypto_rsa_private_key_fill_public_key(&private_key)) {
if (retVal == 0) fprintf(stderr, "Fill_public_key returned FALSE\n");
return 0; return 0;
}
if (public_der_temp_length != private_key.public_key_length) if (public_der_temp_length != private_key.public_key_length) {
fprintf(stderr, "Public key's lengths do not match\n");
return 0; return 0;
}
/* public_der_temp_length2 = private_key.public_key_length;
for(int i = 0; i < public_der_temp_length; i++)
if (public_der_temp[i] != private_key.public_key_der[i])
return 0;
*/
// that didn't work... so let's try it again, to see if it is consistent
size_t public_der_temp_length2 = private_key.public_key_length;
unsigned char public_der_temp2[public_der_temp_length2]; unsigned char public_der_temp2[public_der_temp_length2];
memcpy(public_der_temp2, private_key.public_key_der, public_der_temp_length2); memcpy(public_der_temp2, private_key.public_key_der, public_der_temp_length2);
free(private_key.public_key_der); free(private_key.public_key_der);
private_key.public_key_length = 0; private_key.public_key_length = 0;
retVal = libp2p_crypto_rsa_private_key_fill_public_key(&private_key); if (!libp2p_crypto_rsa_private_key_fill_public_key(&private_key)) {
if (retVal == 0) fprintf(stderr, "Filling Public key 2 failed\n");
return 0; return 0;
}
for(int i = 0; i < public_der_temp_length2; i++) if (public_der_temp_length != public_der_temp_length2) {
if (private_key.public_key_der[i] != public_der_temp2[i]) fprintf(stderr, "The 2 public key lengths do not match: %lu vs %lu\n", public_der_temp_length, public_der_temp_length2);
return 0; return 0;
}
// well, at least it is consistent. for(int i = 0; i < public_der_temp_length2; i++) {
return 1; if ( ((unsigned char)private_key.public_key_der[i]) != public_der_temp2[i]) {
fprintf(stderr, "Key mismatch at position %d. 1 is %02x and the other is %02x\n", i, private_key.public_key_der[i], public_der_temp2[i]);
return 0;
}
}
retVal = 1;
exit:
if (private_key.der != NULL)
free(private_key.der);
if (private_key.public_key_der != NULL)
free(private_key.public_key_der);
return retVal;
} }
int test_crypto_rsa_public_key_to_peer_id() { int test_crypto_rsa_public_key_to_peer_id() {
int retVal = 0;
struct RsaPrivateKey private_key = {0};
char* final_id = NULL;
// this is the base64 encoded private key from the config file // this is the base64 encoded private key from the config file
//char* orig_priv_key = "CAASpwkwggSjAgEAAoIBAQDTDJBWjDzS/HxDNOHazvzH2bu9CPMVHUrrvKR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 int test_crypto_rsa_public_key_to_peer_id() {
memset(decode_base64, 0, decode_base64_size); memset(decode_base64, 0, decode_base64_size);
unsigned char* ptr = decode_base64; unsigned char* ptr = decode_base64;
int retVal = libp2p_crypto_encoding_base64_decode((unsigned char*)orig_priv_key, strlen(orig_priv_key), ptr, decode_base64_size, &decode_base64_size); if (!libp2p_crypto_encoding_base64_decode((unsigned char*)orig_priv_key, strlen(orig_priv_key), ptr, decode_base64_size, &decode_base64_size)) {
if (retVal == 0) goto exit;
return 0; }
// the first 5 bytes [0-4] are protobuf metadata before the DER encoded private key // the first 5 bytes [0-4] are protobuf metadata before the DER encoded private key
// byte 0 is "Tag 1 which is a varint" // byte 0 is "Tag 1 which is a varint"
@ -145,33 +189,38 @@ int test_crypto_rsa_public_key_to_peer_id() {
// bytes 3 & 4 is a varint with the value of 1191, which is the number of bytes that follow // bytes 3 & 4 is a varint with the value of 1191, which is the number of bytes that follow
// 2) take the bytes of the private key and turn it back into a private key struct // 2) take the bytes of the private key and turn it back into a private key struct
struct RsaPrivateKey private_key = {0}; if (!libp2p_crypto_encoding_x509_der_to_private_key(&decode_base64[5], decode_base64_size - 5, &private_key))
retVal = libp2p_crypto_encoding_x509_der_to_private_key(&decode_base64[5], decode_base64_size - 5, &private_key); goto exit;
if (retVal == 0)
return 0;
// 2b) take the private key and fill in the public key DER // 2b) take the private key and fill in the public key DER
retVal = libp2p_crypto_rsa_private_key_fill_public_key(&private_key); if (!libp2p_crypto_rsa_private_key_fill_public_key(&private_key))
if (retVal == 0) goto exit;
return 0;
// 3) grab the public key, hash it, then base58 it // 3) grab the public key, hash it, then base58 it
struct PublicKey public_key; struct PublicKey public_key;
public_key.type = KEYTYPE_RSA; public_key.type = KEYTYPE_RSA;
public_key.data_size = private_key.public_key_length; public_key.data_size = private_key.public_key_length;
public_key.data = private_key.public_key_der; public_key.data = private_key.public_key_der;
char* final_id;
if (!libp2p_crypto_public_key_to_peer_id(&public_key, &final_id )) if (!libp2p_crypto_public_key_to_peer_id(&public_key, &final_id ))
return 0; goto exit;
// 4) compare results // 4) compare results
if (orig_peer_id_size != strlen(final_id)) if (orig_peer_id_size != strlen(final_id))
return 0; goto exit;
if (strncmp(orig_peer_id, (char*)final_id, strlen(final_id)) != 0) if (strncmp(orig_peer_id, (char*)final_id, strlen(final_id)) != 0)
return 0; goto exit;
return 1; retVal = 1;
exit:
if (private_key.der != NULL)
free(private_key.der);
if (private_key.public_key_der != NULL)
free(private_key.public_key_der);
if (final_id != NULL)
free(final_id);
return retVal;
} }
int test_crypto_rsa_signing() { int test_crypto_rsa_signing() {

View file

@ -5,13 +5,19 @@
#include "test_helper.h" #include "test_helper.h"
int test_dialer_new() { int test_dialer_new() {
int retVal = 0;
char* peer_id = "QmQSDGgxSVTkHmtT25rTzQtc5C1Yg8SpGK3BTws8YsJ4x3"; char* peer_id = "QmQSDGgxSVTkHmtT25rTzQtc5C1Yg8SpGK3BTws8YsJ4x3";
struct PrivateKey* private_key = libp2p_crypto_private_key_new(); struct PrivateKey* private_key = libp2p_crypto_private_key_new();
struct Dialer* dialer = libp2p_conn_dialer_new(peer_id, private_key); struct Dialer* dialer = libp2p_conn_dialer_new(peer_id, private_key);
if (dialer == NULL) if (dialer == NULL)
return 0; goto exit;
retVal = 1;
exit:
if (dialer != NULL)
libp2p_conn_dialer_free(dialer); libp2p_conn_dialer_free(dialer);
return 1; if (private_key != NULL)
libp2p_crypto_private_key_free(private_key);
return retVal;
} }
int test_dialer_dial() { int test_dialer_dial() {
@ -84,6 +90,10 @@ int test_dialer_dial_multistream() {
stream = libp2p_conn_dialer_get_stream(dialer, destination_address, "multistream"); stream = libp2p_conn_dialer_get_stream(dialer, destination_address, "multistream");
if (stream == NULL) if (stream == NULL)
goto exit; goto exit;
int socket_descriptor = *((int*)stream->socket_descriptor);
if ( socket_descriptor < 0 || socket_descriptor > 255) {
goto exit;
}
// now ping // now ping
@ -96,6 +106,11 @@ int test_dialer_dial_multistream() {
multiaddress_free(destination_address); multiaddress_free(destination_address);
libp2p_conn_dialer_free(dialer); libp2p_conn_dialer_free(dialer);
libp2p_crypto_private_key_free(private_key); libp2p_crypto_private_key_free(private_key);
stream->close(stream); if (stream != NULL) {
struct SessionContext session_context;
session_context.insecure_stream = stream;
stream->close(&session_context);
libp2p_net_multistream_stream_free(stream);
}
return retVal; return retVal;
} }

View file

@ -18,8 +18,14 @@ int test_multistream_connect() {
retVal = 1; retVal = 1;
exit: exit:
if (stream != NULL) {
struct SessionContext ctx;
ctx.insecure_stream = stream;
stream->close(&ctx);
libp2p_net_multistream_stream_free(stream);
}
return retVal > 0; return retVal;
} }
int test_multistream_get_list() { int test_multistream_get_list() {
@ -50,11 +56,18 @@ int test_multistream_get_list() {
filtered[response_size] = 0; filtered[response_size] = 0;
fprintf(stdout, "Response from multistream ls: %s", (char*)filtered); fprintf(stdout, "Response from multistream ls: %s", (char*)filtered);
free(filtered);
retVal = 1; retVal = 1;
exit: exit:
if (session.insecure_stream != NULL) {
session.insecure_stream->close(&session);
libp2p_net_multistream_stream_free(session.insecure_stream);
}
if (response != NULL)
free(response);
if (filtered != NULL)
free(filtered);
return retVal > 0; return retVal > 0;
} }

View file

@ -61,10 +61,11 @@ int test_peerstore() {
} }
int test_peer_protobuf() { int test_peer_protobuf() {
int retVal = 0;
struct Libp2pPeer *peer = NULL, *peer_result = NULL; struct Libp2pPeer *peer = NULL, *peer_result = NULL;
struct MultiAddress* ma = NULL, *ma_result = NULL; struct MultiAddress* ma = NULL, *ma_result = NULL;
char* peer_id = "QmW8CYQuoJhgfxTeNVFWktGFnTRzdUAimerSsHaE4rUXk8"; char* peer_id = "QmW8CYQuoJhgfxTeNVFWktGFnTRzdUAimerSsHaE4rUXk8";
unsigned char* protobuf; unsigned char* protobuf = NULL;
size_t protobuf_size; size_t protobuf_size;
peer = libp2p_peer_new(); peer = libp2p_peer_new();
@ -84,8 +85,15 @@ int test_peer_protobuf() {
if (strcmp(ma->string, ma_result->string) != 0) { if (strcmp(ma->string, ma_result->string) != 0) {
fprintf(stderr, "Results to not match: %s vs %s\n", ma->string, ma_result->string); fprintf(stderr, "Results to not match: %s vs %s\n", ma->string, ma_result->string);
return 0; goto exit;
} }
return 1; retVal = 1;
exit:
multiaddress_free(ma);
libp2p_peer_free(peer);
libp2p_peer_free(peer_result);
if (protobuf != NULL)
free(protobuf);
return retVal;
} }

View file

@ -4,6 +4,7 @@
#include "libp2p/secio/exchange.h" #include "libp2p/secio/exchange.h"
#include "libp2p/net/multistream.h" #include "libp2p/net/multistream.h"
#include "libp2p/net/p2pnet.h" #include "libp2p/net/p2pnet.h"
#include "libp2p/utils/logger.h"
int test_secio_handshake() { int test_secio_handshake() {
int retVal = 0; int retVal = 0;
@ -70,18 +71,23 @@ int test_secio_handshake() {
libp2p_crypto_ephemeral_stretched_key_free(secure_session.remote_stretched_key); libp2p_crypto_ephemeral_stretched_key_free(secure_session.remote_stretched_key);
if (secure_session.ephemeral_private_key != NULL) if (secure_session.ephemeral_private_key != NULL)
libp2p_crypto_ephemeral_key_free(secure_session.ephemeral_private_key); libp2p_crypto_ephemeral_key_free(secure_session.ephemeral_private_key);
if (secure_session.remote_ephemeral_public_key != NULL)
free(secure_session.remote_ephemeral_public_key);
if (secure_session.chosen_cipher != NULL) if (secure_session.chosen_cipher != NULL)
free(secure_session.chosen_cipher); free(secure_session.chosen_cipher);
if (secure_session.chosen_curve != NULL) if (secure_session.chosen_curve != NULL)
free(secure_session.chosen_curve); free(secure_session.chosen_curve);
if (secure_session.chosen_hash != NULL) if (secure_session.chosen_hash != NULL)
free(secure_session.chosen_hash); free(secure_session.chosen_hash);
if (secure_session.shared_key != NULL)
free(secure_session.shared_key);
if (private_key != NULL) if (private_key != NULL)
libp2p_crypto_private_key_free(private_key); libp2p_crypto_private_key_free(private_key);
if (decode_base64 != NULL) if (decode_base64 != NULL)
free(decode_base64); free(decode_base64);
if (rsa_private_key != NULL) if (rsa_private_key != NULL)
libp2p_crypto_rsa_rsa_private_key_free(rsa_private_key); libp2p_crypto_rsa_rsa_private_key_free(rsa_private_key);
libp2p_logger_free();
return retVal; return retVal;
} }
@ -97,29 +103,44 @@ int test_secio_encrypt_decrypt() {
size_t results_size = 0; size_t results_size = 0;
struct SessionContext secure_session; struct SessionContext secure_session;
struct StretchedKey stretched_key; struct StretchedKey stretched_key;
secure_session.local_stretched_key = &stretched_key; secure_session.local_stretched_key = &stretched_key;
secure_session.remote_stretched_key = &stretched_key; secure_session.remote_stretched_key = &stretched_key;
secure_session.local_stretched_key->cipher_key = (unsigned char*)"abcdefghijklmnopqrstuvwxyzabcdef"; secure_session.local_stretched_key->cipher_key = (unsigned char*)"abcdefghijklmnopqrstuvwxyzabcdef";
secure_session.local_stretched_key->cipher_size = 32; secure_session.local_stretched_key->cipher_size = 32;
secure_session.local_stretched_key->mac_size = 0; secure_session.local_stretched_key->mac_size = 40;
secure_session.local_stretched_key->mac_key = "abcdefghijklmnopqrstuvwxyzabcdefghijklmn";
secure_session.local_stretched_key->iv_size = 16;
secure_session.local_stretched_key->iv = "abcdefghijklmnop";
secure_session.mac_function = NULL; secure_session.mac_function = NULL;
if (!libp2p_secio_encrypt(&secure_session, original, strlen((char*)original), &encrypted, &encrypted_size)) {
if (!libp2p_secio_encrypt(&secure_session, original, strlen((char*)original), &encrypted, &encrypted_size)) fprintf(stderr, "Unable to encrypt\n");
goto exit; goto exit;
}
if (!libp2p_secio_decrypt(&secure_session, encrypted, encrypted_size, &results, &results_size)) if (!libp2p_secio_decrypt(&secure_session, encrypted, encrypted_size, &results, &results_size)) {
fprintf(stderr, "Unable to decrypt\n");
goto exit; goto exit;
}
if (results_size != strlen((char*)original)) if (results_size != strlen((char*)original)) {
fprintf(stderr, "Results size are different. Results size = %lu and original is %lu\n", results_size, strlen((char*)original));
goto exit; goto exit;
}
if (strcmp(original, results) != 0) if (strncmp(original, results, strlen( (char*) original)) != 0) {
fprintf(stderr, "String comparison did not match\n");
goto exit; goto exit;
}
retVal = 1; retVal = 1;
exit: exit:
if (results != NULL)
free(results);
if (encrypted != NULL)
free(encrypted);
return retVal; return retVal;
} }