From d13a47d7d5c997892ebf34b148a905824119addd Mon Sep 17 00:00:00 2001 From: John Jones Date: Mon, 23 Jan 2017 12:07:48 -0500 Subject: [PATCH] Fixed possible memory leak in signature routine --- .cproject | 61 ++++++++++++++++------------- Makefile | 2 + crypto/rsa.c | 2 + test/testit.c | 106 ++++++++++++++++++++++++++++++++++++++++---------- 4 files changed, 125 insertions(+), 46 deletions(-) diff --git a/.cproject b/.cproject index e7bc0e7..bd43fbf 100644 --- a/.cproject +++ b/.cproject @@ -51,32 +51,6 @@ - - - - make - all - true - false - true - - - make - clean - true - false - true - - - make - - test - true - false - true - - - @@ -86,4 +60,39 @@ + + + + make + + all + true + false + true + + + make + + clean + true + false + true + + + make + test + true + false + true + + + make + + rebuild + true + true + true + + + diff --git a/Makefile b/Makefile index 87b5aad..79a487e 100644 --- a/Makefile +++ b/Makefile @@ -15,6 +15,8 @@ compile: test: compile cd test; make all; +rebuild: clean all + all: test clean: diff --git a/crypto/rsa.c b/crypto/rsa.c index ec5976f..42257de 100644 --- a/crypto/rsa.c +++ b/crypto/rsa.c @@ -252,6 +252,8 @@ int libp2p_crypto_rsa_sign(struct RsaPrivateKey* private_key, unsigned char* mes 32, output, result ); + mbedtls_ctr_drbg_free(&ctr_drbg); + //mbetdls_rsa_free(ctx); return retVal == 0; } diff --git a/test/testit.c b/test/testit.c index 30f8815..d46faa2 100644 --- a/test/testit.c +++ b/test/testit.c @@ -7,6 +7,50 @@ #include "crypto/test_base32.h" #include "test_mbedtls.h" +const char* names[] = { + "test_public_der_to_private_der", + "test_mbedtls_varint_128_binary", + "test_mbedtls_varint_128_string", + "test_crypto_rsa_private_key_der", + "test_crypto_rsa_public_key_to_peer_id", + "test_crypto_x509_der_to_private2", + "test_crypto_x509_der_to_private", + //"test_multihash_encode", + //"test_multihash_decode", + //"test_multihash_base58_encode_decode", + //"test_multihash_base58_decode", + //"test_multihash_size", + "test_base58_encode_decode", + "test_base58_size", + "test_base58_max_size", + "test_base58_peer_address", + //"test_mbedtls_pk_write_key_der", + //"test_crypto_rsa_sign", + "test_crypto_encoding_base32_encode" +}; + +int (*funcs[])(void) = { + test_public_der_to_private_der, + test_mbedtls_varint_128_binary, + test_mbedtls_varint_128_string, + test_crypto_rsa_private_key_der, + test_crypto_rsa_public_key_to_peer_id, + test_crypto_x509_der_to_private2, + test_crypto_x509_der_to_private, + //test_multihash_encode, + //test_multihash_decode, + //test_multihash_base58_encode_decode, + //test_multihash_base58_decode, + //test_multihash_size, + test_base58_encode_decode, + test_base58_size, + test_base58_max_size, + test_base58_peer_address, + //test_mbedtls_pk_write_key_der, + //test_crypto_rsa_sign, + test_crypto_encoding_base32_encode +}; + int testit(const char* name, int (*func)(void)) { printf("Testing %s...\n", name); int retVal = func(); @@ -18,26 +62,48 @@ int testit(const char* name, int (*func)(void)) { } int main(int argc, char** argv) { - /* - testit("test_public_der_to_private_der", test_public_der_to_private_der); - testit("test_mbedtls_varint_128_binary", test_mbedtls_varint_128_binary); - testit("test_mbedtls_varint_128_string", test_mbedtls_varint_128_string); - testit("test_crypto_rsa_private_key_der", test_crypto_rsa_private_key_der); - testit("test_crypto_rsa_public_key_to_peer_id", test_crypto_rsa_public_key_to_peer_id); - testit("test_crypto_x509_der_to_private2", test_crypto_x509_der_to_private2); - testit("test_crypto_x509_der_to_private", test_crypto_x509_der_to_private); - //testit("test_multihash_encode", test_multihash_encode); - //testit("test_multihash_decode", test_multihash_decode); - //testit("test_multihash_base58_encode_decode", test_multihash_base58_encode_decode); - //testit("test_multihash_base58_decode", test_multihash_base58_decode); - //testit("test_multihash_size", test_multihash_size); - testit("test_base58_encode_decode", test_base58_encode_decode); - testit("test_base58_size", test_base58_size); - testit("test_base58_max_size", test_base58_max_size); - testit("test_base58_peer_address", test_base58_peer_address); - //testit("test_mbedtls_pk_write_key_der", test_mbedtls_pk_write_key_der); - */ - testit("test_crypto_encoding_base32_encode", test_crypto_encoding_base32_encode); + int counter = 0; + int tests_ran = 0; + char* test_wanted; + int only_one = 0; + if(argc > 1) { + only_one = 1; + if (argv[1][0] == '\'') { // some shells put quotes around arguments + argv[1][strlen(argv[1])-1] = 0; + test_wanted = &(argv[1][1]); + } + else + test_wanted = argv[1]; + } + int array_length = sizeof(funcs) / sizeof(funcs[0]); + int array2_length = sizeof(names) / sizeof(names[0]); + if (array_length != array2_length) { + printf("Test arrays are not of the same length. Funcs: %d, Names: %d\n", array_length, array2_length); + } + for (int i = 0; i < array_length; i++) { + if (only_one) { + const char* currName = names[i]; + if (strcmp(currName, test_wanted) == 0) { + tests_ran++; + counter += testit(names[i], funcs[i]); + } + } + else + if (!only_one) { + tests_ran++; + counter += testit(names[i], funcs[i]); + } + } + + if (tests_ran == 0) + printf("***** No tests found *****\n"); + else { + if (counter > 0) { + printf("***** There were %d failed test(s) *****\n", counter); + } else { + printf("All %d tests passed\n", tests_ran); + } + } return 1; }