From 8ce1c32ec4f2fd408cc0a105b007916bad842fe3 Mon Sep 17 00:00:00 2001 From: John Jones Date: Wed, 26 Apr 2017 23:56:18 -0500 Subject: [PATCH] Multiple memory fixes, mostly in tests --- conn/tcp_transport_dialer.c | 1 + net/multistream.c | 3 +- secio/secio.c | 32 +++++-- test/crypto/test_base58.h | 11 ++- test/crypto/test_key.h | 10 ++- test/crypto/test_rsa.h | 163 +++++++++++++++++++++++------------- test/test_conn.h | 23 ++++- test/test_multistream.h | 17 +++- test/test_peer.h | 14 +++- test/test_secio.h | 33 ++++++-- 10 files changed, 220 insertions(+), 87 deletions(-) diff --git a/conn/tcp_transport_dialer.c b/conn/tcp_transport_dialer.c index c728213..6715375 100644 --- a/conn/tcp_transport_dialer.c +++ b/conn/tcp_transport_dialer.c @@ -37,6 +37,7 @@ struct Connection* libp2p_conn_tcp_dial(const struct TransportDialer* transport_ if (!multiaddress_get_ip_address(addr, &ip)) return NULL; struct hostent* host = gethostbyname(ip); + free(ip); struct in_addr** addr_list = (struct in_addr**)host->h_addr_list; socket_connect4(conn->socket_handle, (*addr_list[0]).s_addr, port); conn->read = libp2p_conn_tcp_read; diff --git a/net/multistream.c b/net/multistream.c index feb0398..540a3a8 100644 --- a/net/multistream.c +++ b/net/multistream.c @@ -19,7 +19,8 @@ int libp2p_net_multistream_close(void* stream_context) { struct SessionContext* secure_context = (struct SessionContext*)stream_context; struct Stream* stream = secure_context->insecure_stream; - close( *((int*)stream->socket_descriptor)); + int socket_descriptor = *((int*)stream->socket_descriptor); + close(socket_descriptor); return 1; } diff --git a/secio/secio.c b/secio/secio.c index ba9d084..eb6db66 100644 --- a/secio/secio.c +++ b/secio/secio.c @@ -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_finish(&ctx, a_hash); } + mbedtls_md_free(&ctx); // now we have a big result. Cut it up into pieces 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) { unsigned char* buffer = NULL; - size_t buffer_size = 0; + size_t buffer_size = 0, original_buffer_size = 0; //TODO switch between ciphers mbedtls_cipher_context_t cipher_ctx; mbedtls_cipher_init(&cipher_ctx); 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); - buffer_size = incoming_size + mbedtls_cipher_get_block_size(&cipher_ctx) + 32; - buffer = malloc(buffer_size); - memset(buffer, 0, buffer_size); + original_buffer_size = incoming_size; + original_buffer_size += 32; + 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); + // 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); - // mac the data, and append it + // mac the data mbedtls_md_context_t ctx; 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_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_free(&ctx); // put it all in outgoing - *outgoing_size = buffer_size + 32; + *outgoing_size = original_buffer_size; *outgoing = malloc(*outgoing_size); - memcpy(*outgoing, buffer, *outgoing_size); + memset(*outgoing, 0, *outgoing_size); + memcpy(*outgoing, buffer, original_buffer_size); free(buffer); 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 = malloc(buffer_size); mbedtls_cipher_update(&cipher_ctx, incoming, data_section_size, buffer, &buffer_size); + mbedtls_cipher_free(&cipher_ctx); *outgoing = malloc(buffer_size); *outgoing_size = buffer_size; memcpy(*outgoing, buffer, buffer_size); + free(buffer); return *outgoing_size; } @@ -629,14 +638,19 @@ int libp2p_secio_decrypt(const struct SessionContext* session, const unsigned ch * @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 retVal = 0; struct SessionContext* session = (struct SessionContext*)stream_context; // reader uses the remote cipher and mac // read the data unsigned char* incoming = NULL; size_t incoming_size = 0; if (libp2p_secio_unencrypted_read(session, &incoming, &incoming_size, timeout_secs) <= 0) - return 0; - return libp2p_secio_decrypt(session, incoming, incoming_size, bytes, num_bytes); + goto exit; + retVal = libp2p_secio_decrypt(session, incoming, incoming_size, bytes, num_bytes); + exit: + if (incoming != NULL) + free(incoming); + return retVal; } /*** diff --git a/test/crypto/test_base58.h b/test/crypto/test_base58.h index 59a837f..828c33b 100644 --- a/test/crypto/test_base58.h +++ b/test/crypto/test_base58.h @@ -40,23 +40,26 @@ int test_base58_size() { 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; + } size_t encoded_max_length = 100; unsigned char encoded[encoded_max_length]; unsigned char* ptr = encoded; // 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); - 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 1; - } int test_base58_max_size() { diff --git a/test/crypto/test_key.h b/test/crypto/test_key.h index d8d31e4..f8604b6 100644 --- a/test/crypto/test_key.h +++ b/test/crypto/test_key.h @@ -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 // these were pulled from the GO version of ipfs char* orig_priv_key = "CAASqgkwggSmAgEAAoIBAQD0a4RI+bF/ov7IVOGSJ8dQfnK1DwM0gwVuJAd+3LXxIZEPZzsKIKia0TojDbTdLvOJ23wsaojTF/4bSzBK5otdAz8YSgez4vTRUV5pUqvCkK0dSJJ1DHTdrFUwvzlXuKbwNbvWyzjmKfeaE9a9YLzhrUIUTRvKyqhZXr++vMy3hw4fdtGUTJWeiqmoIuJWIZ1748Ff6LjcP7TdG7OvY4q+U9ilEJYdF4aM+TJY193zKp0GNWohunuVrtOUnL9VQaSSDbvGdFS1Mg9iCN6kRBQTHVQvFzvuEw/Y2LvoPH3yFG1zj6bDLWfOBhegy/6Zi6fi4E1UfgJNFN1sjWF+gZoHAgMBAAECggEBALTqBD9zuoMsJYQo99IT6X7WKZeE5i1vMYzF1Fp9iZpS6ylIkrW7WLFHXs3lblMtVoxazn2d2WrOXoLbU4KNROhy57fVy//FZMqufMBetc3NAqYjOmyy7KnLzj7Hu+0HO2GflEq3n4UV2TTNrGv+d7BfawLV1FV1TcjgzfKjkq/gMDCTPMgfT7lcF4TGSqv6Pgudp8RRn/R7EKOx+I8/XkJsZWP3XJ0zj4ciqDmKrX2j7wZMT8CH/8wfyg4NGk1+TN4xBB2CXgulIWJg5yhzu+JgbGnHEL/Ga+i40XJe+RnlKDpjQ+ZFyrOkmHpIldasjWNGFeKwLjzrDQfyDRwex5ECgYEA+fFGJ+zbi2YJwHDO/44PmvcoCWpho0ah7y+O5/LEVROTYEoNRqodLet+z+LMKDq/o2qTNaYJLiDGMBZzhqyJIFR5ZJ5MhgLloY1gL8s0a7KMWDbh7giiWSu5zqhB3Du8Tom+8bYZUxOL4zhzCGrFitRqiEIIjy1/c5qyRQZaZx8CgYEA+lf6tdO6kKiAOxm7sdZ3rEl4UGFY+tEqgivKwurLRIor0XDfhCKr1hCfsZggpR8SMLfjCuNEgKbceofcKMa8OtyDbMPRz0mYNkCELTUYA+r8Ib/LvleQApMcLn+TDNwEnGlglSrrF33RVAUK+i/WfSXUvZRVpLQpRmdAqHjJeBkCgYEA0+Zz/iFXOGW32slJFWxRWqYz8VeZk52saGY/l/I/9Yj1J2tgugo7VtUS3BiB0ZGNK3SNfaxYmBz9KYO/Sew5DYnQqTdz1SHboQ2FAMAcnznutlNBVFdJnKPvkX8g5yBV05gApFgoPECUFn2jOP2coMjZ0M97Bjgil9YNUWvDdS0CgYEA4beFs3+tzVRAGgl/tD7dNBgiRMchBTSmkSuO6+PrVmcGTxboUSk5qg7fDa9Ob9LuAcMrENwNHbpVPJ1WoeVePewpC14bxDxk4zWUd3ZRquaqYnud5obor4mYdUxNd+DAv447qQNDaLDmlkzdsuqDB9+eSzh9Z72RIYtjPwN5E7ECgYEAsbqkMZXfK1tTRfduX+7KOlPMfcSr29X6nuDglcna4dHec6FAOzp3xL2722nnFt6gygc7pErrm0m0Wd/6BMTb4T3+GYwkDiMjM2CsTZYjpzrUri/VfRR509rScxHVR0/1PTFWN0K0+VZbEAyXDbbs4opq40tW0dWtcKxaNlimMw8="; - struct PrivateKey* private_key = libp2p_crypto_private_key_new(); + struct PrivateKey* private_key = NULL; struct RsaPrivateKey rsa_private_key = {0}; char* orig_peer_id = "QmbTyKkUuv6yaSpTuCFq1Ft6Q3g4wTtFJk1BLGMPRdAEP8"; size_t orig_peer_id_size = strlen(orig_peer_id); @@ -57,5 +57,13 @@ int test_protobuf_private_key() { exit: if (decode_base64 != NULL) 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; } diff --git a/test/crypto/test_rsa.h b/test/crypto/test_rsa.h index f13f7ce..c12a49e 100644 --- a/test/crypto/test_rsa.h +++ b/test/crypto/test_rsa.h @@ -10,115 +10,159 @@ #include "libp2p/crypto/peerutils.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 */ int test_crypto_rsa_private_key_der() { - + int retVal = 0; struct RsaPrivateKey private_key; libp2p_crypto_rsa_generate_keypair(&private_key, 2048); if (private_key.der_length == 0) - return 0; + goto exit; 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 retVal = 0; // 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/PVB5TnpdF9qXYbaJoLCziRzzgPJ9HWItnPuYudY8CgYB4mZZ9SM7t7IoutB+gVA1jgAWAJO+vG/c0e6rA9WILmtJA908GPeTQguaumbcKsDXckoJAlwLOvYjR1aZJx89SiU78znMF3EesoR3vm9J/2rIU6p6AwQqjfUjiA/deP0uJqicb9E7yhXNrEp/0ziq6zgfYk8S2UjJcnhqll9pHQQKBgGdoyfxHmSFD/Wowpbh6Edr+LNgbHBM7kcvWeNA0oIbKL/3tIrc1xUnU4fzjw5ozTQI+FzaujX0YysbcxGc7QsUnr9iRd4WulyvLKDMhO97KVcJzt1RMwjqQy3fnURIOyJvGOML6+/CDisLzqlV9WwIGrHQeGGwwSqoSqJnxcDy1AoGAMVmdK7mTMBtayUXRpthNCUmRAubaUKAjhKYm0j7ciPKWmo+mBHITbUshti07HqYeqxZE3LAIw/j0nowPsf4HIOxc69E+ZQqvETQwGyCwyHKJNYSFJi3tkHzCGEoP0/k9l6Fu8Qqcs1xhNQu8AcjQ1QL17rs/r9N44GKS2iXI+PY="; size_t b64_length = libp2p_crypto_encoding_base64_decode_size(strlen(der)); unsigned char buffer[b64_length]; unsigned char* b = buffer; size_t ultimate_length; - int retVal = libp2p_crypto_encoding_base64_decode((unsigned char*)der, strlen(der), b, b64_length, &ultimate_length); - if (retVal == 0) - return 0; + if (!libp2p_crypto_encoding_base64_decode((unsigned char*)der, strlen(der), b, b64_length, &ultimate_length)) + goto exit; // we now have the bytes struct RsaPrivateKey private_key = {0}; - retVal = libp2p_crypto_encoding_x509_der_to_private_key(b, ultimate_length, &private_key); - if (retVal == 0) - return 0; - return private_key.D > 0; + if (!libp2p_crypto_encoding_x509_der_to_private_key(b, ultimate_length, &private_key)) + goto exit; + if (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 retVal = 0; // 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/MIxr2RldM5ukpN6qxGWKtJkXrAgD2dqJPrRoBpqKahzPxSHfIJ0Fw5dqDtjsrpYJvyt0oEDPmnDuZAbmFx4sJqnesPNhKxtRMBx1+yxGVuRVJjHcqAgqPqwNiuoMEaYMY+G9yzT6vza8ovCpbX7BBIgM5fAT9PD8TBG//Vu9THvj/ZomiVG2qv6RL0qQyVb+DUzPZz1amBsSvahtXCl72jA3JwAZ943RxSR66P934S0ashkVwLUi46z/EAbJ4QKBgQDojGIO07BEVL2+7VxlGL9XGZQp4Y3qlhh2zDDQGwkCq/KQ+BdNYWitPwqRl9GqFLgpmeQIhyHTOa/IThx+AXGKVQ24ROH+skUs4IbO6R3qY7BKtb5lkZE/Yln09x70BBngUYAzh/rtnsXO3cl1x2XDDqUbCwlGcDAs8Jh/6UnvQwKBgQDoVSQs7Uq9MJCGIUM2bixX89tHzSxq5mn9wMD3/XRVfT5Ua8YkYBuzcmlcT39N7L5BwuyFqX3Vi7lv/Ya/qaQP6XkrZ8W1OAaTlYewfE5ZgknJqSpXcNWhABKeNmqndvqyQ/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)); unsigned char buffer[b64_length]; unsigned char* b = buffer; size_t ultimate_length; - int retVal = libp2p_crypto_encoding_base64_decode((unsigned char*)der, strlen(der), b, b64_length, &ultimate_length); - if (retVal == 0) - return 0; + if (!libp2p_crypto_encoding_base64_decode((unsigned char*)der, strlen(der), b, b64_length, &ultimate_length)) + goto exit; // 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(); int bytesToStrip = 5; - retVal = libp2p_crypto_encoding_x509_der_to_private_key(&b[bytesToStrip], ultimate_length-bytesToStrip, private_key); - if (retVal == 0) - return 0; + if (!libp2p_crypto_encoding_x509_der_to_private_key(&b[bytesToStrip], ultimate_length-bytesToStrip, private_key)) + goto exit; retVal = private_key->D > 0; + exit: libp2p_crypto_rsa_rsa_private_key_free(private_key); return retVal; } -int test_public_der_to_private_der() { - struct RsaPrivateKey private_key; - int retVal = libp2p_crypto_rsa_generate_keypair(&private_key, 2048); - if (retVal == 0) - return 0; +void printKey(unsigned char* in, size_t in_size) { + for (int i = 0; i < in_size; i++) { + fprintf(stdout, "%02x ", in[i]); + } + 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; - 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; + } + 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. - 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]; memcpy(public_der_temp, private_key.public_key_der, private_key.public_key_length); free(private_key.public_key_der); private_key.public_key_length = 0; - retVal = libp2p_crypto_rsa_private_key_fill_public_key(&private_key); - if (retVal == 0) + if (!libp2p_crypto_rsa_private_key_fill_public_key(&private_key)) { + fprintf(stderr, "Fill_public_key returned FALSE\n"); 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; + } - /* - 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; + public_der_temp_length2 = private_key.public_key_length; unsigned char public_der_temp2[public_der_temp_length2]; memcpy(public_der_temp2, private_key.public_key_der, public_der_temp_length2); free(private_key.public_key_der); private_key.public_key_length = 0; - retVal = libp2p_crypto_rsa_private_key_fill_public_key(&private_key); - if (retVal == 0) + if (!libp2p_crypto_rsa_private_key_fill_public_key(&private_key)) { + fprintf(stderr, "Filling Public key 2 failed\n"); return 0; + } - for(int i = 0; i < public_der_temp_length2; i++) - if (private_key.public_key_der[i] != public_der_temp2[i]) + if (public_der_temp_length != public_der_temp_length2) { + fprintf(stderr, "The 2 public key lengths do not match: %lu vs %lu\n", public_der_temp_length, public_der_temp_length2); + return 0; + } + + for(int i = 0; i < public_der_temp_length2; i++) { + 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; + } + } - // well, at least it is consistent. - 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); + return retVal; } 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 //char* orig_priv_key = "CAASpwkwggSjAgEAAoIBAQDTDJBWjDzS/HxDNOHazvzH2bu9CPMVHUrrvKRdBUM5ansL6/CC3MVZ6HVm4O6QHRapN6EF2CbrTgI4KBOXIL125Xo8MlROnyfXYk3O5q2tgwL/MbW8kXjtkyCfBak7MUoLOdLU7Svg0gkl3l+uDAiDcCLnwJVcFfq9ch6z4wMOhYJqE5dtx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QfyDRwex5ECgYEA+fFGJ+zbi2YJwHDO/44PmvcoCWpho0ah7y+O5/LEVROTYEoNRqodLet+z+LMKDq/o2qTNaYJLiDGMBZzhqyJIFR5ZJ5MhgLloY1gL8s0a7KMWDbh7giiWSu5zqhB3Du8Tom+8bYZUxOL4zhzCGrFitRqiEIIjy1/c5qyRQZaZx8CgYEA+lf6tdO6kKiAOxm7sdZ3rEl4UGFY+tEqgivKwurLRIor0XDfhCKr1hCfsZggpR8SMLfjCuNEgKbceofcKMa8OtyDbMPRz0mYNkCELTUYA+r8Ib/LvleQApMcLn+TDNwEnGlglSrrF33RVAUK+i/WfSXUvZRVpLQpRmdAqHjJeBkCgYEA0+Zz/iFXOGW32slJFWxRWqYz8VeZk52saGY/l/I/9Yj1J2tgugo7VtUS3BiB0ZGNK3SNfaxYmBz9KYO/Sew5DYnQqTdz1SHboQ2FAMAcnznutlNBVFdJnKPvkX8g5yBV05gApFgoPECUFn2jOP2coMjZ0M97Bjgil9YNUWvDdS0CgYEA4beFs3+tzVRAGgl/tD7dNBgiRMchBTSmkSuO6+PrVmcGTxboUSk5qg7fDa9Ob9LuAcMrENwNHbpVPJ1WoeVePewpC14bxDxk4zWUd3ZRquaqYnud5obor4mYdUxNd+DAv447qQNDaLDmlkzdsuqDB9+eSzh9Z72RIYtjPwN5E7ECgYEAsbqkMZXfK1tTRfduX+7KOlPMfcSr29X6nuDglcna4dHec6FAOzp3xL2722nnFt6gygc7pErrm0m0Wd/6BMTb4T3+GYwkDiMjM2CsTZYjpzrUri/VfRR509rScxHVR0/1PTFWN0K0+VZbEAyXDbbs4opq40tW0dWtcKxaNlimMw8="; @@ -134,9 +178,9 @@ int test_crypto_rsa_public_key_to_peer_id() { memset(decode_base64, 0, decode_base64_size); 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 (retVal == 0) - return 0; + if (!libp2p_crypto_encoding_base64_decode((unsigned char*)orig_priv_key, strlen(orig_priv_key), ptr, decode_base64_size, &decode_base64_size)) { + goto exit; + } // the first 5 bytes [0-4] are protobuf metadata before the DER encoded private key // 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 // 2) take the bytes of the private key and turn it back into a private key struct - struct RsaPrivateKey private_key = {0}; - retVal = libp2p_crypto_encoding_x509_der_to_private_key(&decode_base64[5], decode_base64_size - 5, &private_key); - if (retVal == 0) - return 0; + if (!libp2p_crypto_encoding_x509_der_to_private_key(&decode_base64[5], decode_base64_size - 5, &private_key)) + goto exit; // 2b) take the private key and fill in the public key DER - retVal = libp2p_crypto_rsa_private_key_fill_public_key(&private_key); - if (retVal == 0) - return 0; + if (!libp2p_crypto_rsa_private_key_fill_public_key(&private_key)) + goto exit; // 3) grab the public key, hash it, then base58 it struct PublicKey public_key; public_key.type = KEYTYPE_RSA; public_key.data_size = private_key.public_key_length; public_key.data = private_key.public_key_der; - char* final_id; if (!libp2p_crypto_public_key_to_peer_id(&public_key, &final_id )) - return 0; + goto exit; // 4) compare results if (orig_peer_id_size != strlen(final_id)) - return 0; + goto exit; 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() { diff --git a/test/test_conn.h b/test/test_conn.h index 14d740a..d6079ab 100644 --- a/test/test_conn.h +++ b/test/test_conn.h @@ -5,13 +5,19 @@ #include "test_helper.h" int test_dialer_new() { + int retVal = 0; char* peer_id = "QmQSDGgxSVTkHmtT25rTzQtc5C1Yg8SpGK3BTws8YsJ4x3"; struct PrivateKey* private_key = libp2p_crypto_private_key_new(); struct Dialer* dialer = libp2p_conn_dialer_new(peer_id, private_key); if (dialer == NULL) - return 0; - libp2p_conn_dialer_free(dialer); - return 1; + goto exit; + retVal = 1; + exit: + if (dialer != NULL) + libp2p_conn_dialer_free(dialer); + if (private_key != NULL) + libp2p_crypto_private_key_free(private_key); + return retVal; } int test_dialer_dial() { @@ -84,6 +90,10 @@ int test_dialer_dial_multistream() { stream = libp2p_conn_dialer_get_stream(dialer, destination_address, "multistream"); if (stream == NULL) goto exit; + int socket_descriptor = *((int*)stream->socket_descriptor); + if ( socket_descriptor < 0 || socket_descriptor > 255) { + goto exit; + } // now ping @@ -96,6 +106,11 @@ int test_dialer_dial_multistream() { multiaddress_free(destination_address); libp2p_conn_dialer_free(dialer); 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; } diff --git a/test/test_multistream.h b/test/test_multistream.h index 85c64a8..88d7245 100644 --- a/test/test_multistream.h +++ b/test/test_multistream.h @@ -18,8 +18,14 @@ int test_multistream_connect() { retVal = 1; 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() { @@ -50,11 +56,18 @@ int test_multistream_get_list() { filtered[response_size] = 0; fprintf(stdout, "Response from multistream ls: %s", (char*)filtered); - free(filtered); retVal = 1; 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; } diff --git a/test/test_peer.h b/test/test_peer.h index ea1bd9c..fbe17bf 100644 --- a/test/test_peer.h +++ b/test/test_peer.h @@ -61,10 +61,11 @@ int test_peerstore() { } int test_peer_protobuf() { + int retVal = 0; struct Libp2pPeer *peer = NULL, *peer_result = NULL; struct MultiAddress* ma = NULL, *ma_result = NULL; char* peer_id = "QmW8CYQuoJhgfxTeNVFWktGFnTRzdUAimerSsHaE4rUXk8"; - unsigned char* protobuf; + unsigned char* protobuf = NULL; size_t protobuf_size; peer = libp2p_peer_new(); @@ -84,8 +85,15 @@ int test_peer_protobuf() { if (strcmp(ma->string, ma_result->string) != 0) { 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; } diff --git a/test/test_secio.h b/test/test_secio.h index 5dcc3e8..a8fb8ba 100644 --- a/test/test_secio.h +++ b/test/test_secio.h @@ -4,6 +4,7 @@ #include "libp2p/secio/exchange.h" #include "libp2p/net/multistream.h" #include "libp2p/net/p2pnet.h" +#include "libp2p/utils/logger.h" int test_secio_handshake() { int retVal = 0; @@ -70,18 +71,23 @@ int test_secio_handshake() { libp2p_crypto_ephemeral_stretched_key_free(secure_session.remote_stretched_key); if (secure_session.ephemeral_private_key != NULL) 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) free(secure_session.chosen_cipher); if (secure_session.chosen_curve != NULL) free(secure_session.chosen_curve); if (secure_session.chosen_hash != NULL) free(secure_session.chosen_hash); + if (secure_session.shared_key != NULL) + free(secure_session.shared_key); if (private_key != NULL) libp2p_crypto_private_key_free(private_key); if (decode_base64 != NULL) free(decode_base64); if (rsa_private_key != NULL) libp2p_crypto_rsa_rsa_private_key_free(rsa_private_key); + libp2p_logger_free(); return retVal; } @@ -97,29 +103,44 @@ int test_secio_encrypt_decrypt() { size_t results_size = 0; struct SessionContext secure_session; struct StretchedKey stretched_key; + secure_session.local_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_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; - - 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; + } - 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; + } - 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; + } - if (strcmp(original, results) != 0) + if (strncmp(original, results, strlen( (char*) original)) != 0) { + fprintf(stderr, "String comparison did not match\n"); goto exit; + } retVal = 1; exit: + if (results != NULL) + free(results); + if (encrypted != NULL) + free(encrypted); return retVal; }