fix memory leaks in RSA verification function

This commit is contained in:
Sergio R. Caprile committed 2025-07-02 16:40:36 -03:00
1 parent e72b95fb1c
commit ccd2b2699f
3 files changed
+102 -27

No files matched your search

+20 -11
View File
@@ -14707,10 +14707,10 @@ typedef struct _BI_CTX BI_CTX;
*/
NS_INTERNAL BI_CTX *bi_initialize(void);
//NS_INTERNAL void bi_terminate(BI_CTX *ctx);
NS_INTERNAL void bi_terminate(BI_CTX *ctx);
NS_INTERNAL void bi_permanent(bigint *bi);
NS_INTERNAL void bi_depermanent(bigint *bi);
//NS_INTERNAL void bi_clear_cache(BI_CTX *ctx);
NS_INTERNAL void bi_clear_cache(BI_CTX *ctx);
NS_INTERNAL void bi_free(BI_CTX *ctx, bigint *bi);
NS_INTERNAL bigint *bi_copy(bigint *bi);
NS_INTERNAL bigint *bi_clone(BI_CTX *ctx, const bigint *bi);
@@ -14732,7 +14732,7 @@ NS_INTERNAL bigint *bi_mod_power2(BI_CTX *ctx, bigint *bi,
#endif
NS_INTERNAL int bi_compare(bigint *bia, bigint *bib);
NS_INTERNAL void bi_set_mod(BI_CTX *ctx, bigint *bim, int mod_offset);
//NS_INTERNAL void bi_free_mod(BI_CTX *ctx, int mod_offset);
NS_INTERNAL void bi_free_mod(BI_CTX *ctx, int mod_offset);
#ifdef CONFIG_SSL_FULL_MODE
NS_INTERNAL void bi_print(const char *label, bigint *bi);
@@ -14869,7 +14869,6 @@ NS_INTERNAL BI_CTX *bi_initialize(void) {
return ctx;
}
#if 0
/**
* @brief Close the bigint context and free any resources.
*
@@ -14909,7 +14908,6 @@ NS_INTERNAL void bi_clear_cache(BI_CTX *ctx) {
ctx->free_count = 0;
ctx->free_list = NULL;
}
#endif
/**
* @brief Increment the number of references to this object.
@@ -15479,7 +15477,6 @@ NS_INTERNAL void bi_set_mod(BI_CTX *ctx, bigint *bim, int mod_offset) {
#endif
}
#if 0
/**
* @brief Used when cleaning various bigints at the end of a session.
* @param ctx [in] The bigint session context.
@@ -15501,7 +15498,6 @@ void bi_free_mod(BI_CTX *ctx, int mod_offset) {
bi_depermanent(ctx->bi_normalised_mod[mod_offset]);
bi_free(ctx, ctx->bi_normalised_mod[mod_offset]);
}
#endif
/**
* Perform a standard multiplication between two bigints.
@@ -16111,6 +16107,21 @@ NS_INTERNAL bigint *bi_crt(BI_CTX *ctx, bigint *bi, bigint *dP, bigint *dQ,
}
#endif
// Proper lib usage:
// - BI_CTX *c = bi_initialize()
// - allocate bigints (e.g.: calling bi_import(), int_to_bi(), ...)
// - function calls, allocate bigints, etc.
// - bigint *n = bi_import(c, indata, insize)
// - bi_set_mod(c, n, x)
// - mod function calls
// - bigint *nn = bi_mod_pwr(c, m, e) <-- frees m, e
// - bi_free_mod(c, x) <-- frees n
// - bi_export(c, nn, outdata, outsize) <-- frees nn
// - function calls
// - free bigints calling bi_free()
// - bi_terminate(c) <-- frees c
int mg_rsa_mod_pow(const uint8_t *mod, size_t modsz, const uint8_t *exp, size_t expsz, const uint8_t *msg, size_t msgsz, uint8_t *out, size_t outsz) {
BI_CTX *bi_ctx = bi_initialize();
bigint *n = bi_import(bi_ctx, mod, (int) modsz);
@@ -16118,11 +16129,9 @@ int mg_rsa_mod_pow(const uint8_t *mod, size_t modsz, const uint8_t *exp, size_t
bigint *h = bi_import(bi_ctx, msg, (int) msgsz);
bi_set_mod(bi_ctx, n, 0);
bigint *m1 = bi_mod_power(bi_ctx, h, e);
bi_free_mod(bi_ctx, 0);
bi_export(bi_ctx, m1, out, (int) outsz);
bi_free(bi_ctx, n);
bi_free(bi_ctx, e);
bi_free(bi_ctx, h);
bi_free(bi_ctx, m1);
bi_terminate(bi_ctx);
return 0;
}
+20 -11
View File
@@ -159,10 +159,10 @@ typedef struct _BI_CTX BI_CTX;
*/
NS_INTERNAL BI_CTX *bi_initialize(void);
//NS_INTERNAL void bi_terminate(BI_CTX *ctx);
NS_INTERNAL void bi_terminate(BI_CTX *ctx);
NS_INTERNAL void bi_permanent(bigint *bi);
NS_INTERNAL void bi_depermanent(bigint *bi);
//NS_INTERNAL void bi_clear_cache(BI_CTX *ctx);
NS_INTERNAL void bi_clear_cache(BI_CTX *ctx);
NS_INTERNAL void bi_free(BI_CTX *ctx, bigint *bi);
NS_INTERNAL bigint *bi_copy(bigint *bi);
NS_INTERNAL bigint *bi_clone(BI_CTX *ctx, const bigint *bi);
@@ -184,7 +184,7 @@ NS_INTERNAL bigint *bi_mod_power2(BI_CTX *ctx, bigint *bi,
#endif
NS_INTERNAL int bi_compare(bigint *bia, bigint *bib);
NS_INTERNAL void bi_set_mod(BI_CTX *ctx, bigint *bim, int mod_offset);
//NS_INTERNAL void bi_free_mod(BI_CTX *ctx, int mod_offset);
NS_INTERNAL void bi_free_mod(BI_CTX *ctx, int mod_offset);
#ifdef CONFIG_SSL_FULL_MODE
NS_INTERNAL void bi_print(const char *label, bigint *bi);
@@ -321,7 +321,6 @@ NS_INTERNAL BI_CTX *bi_initialize(void) {
return ctx;
}
#if 0
/**
* @brief Close the bigint context and free any resources.
*
@@ -361,7 +360,6 @@ NS_INTERNAL void bi_clear_cache(BI_CTX *ctx) {
ctx->free_count = 0;
ctx->free_list = NULL;
}
#endif
/**
* @brief Increment the number of references to this object.
@@ -931,7 +929,6 @@ NS_INTERNAL void bi_set_mod(BI_CTX *ctx, bigint *bim, int mod_offset) {
#endif
}
#if 0
/**
* @brief Used when cleaning various bigints at the end of a session.
* @param ctx [in] The bigint session context.
@@ -953,7 +950,6 @@ void bi_free_mod(BI_CTX *ctx, int mod_offset) {
bi_depermanent(ctx->bi_normalised_mod[mod_offset]);
bi_free(ctx, ctx->bi_normalised_mod[mod_offset]);
}
#endif
/**
* Perform a standard multiplication between two bigints.
@@ -1563,6 +1559,21 @@ NS_INTERNAL bigint *bi_crt(BI_CTX *ctx, bigint *bi, bigint *dP, bigint *dQ,
}
#endif
// Proper lib usage:
// - BI_CTX *c = bi_initialize()
// - allocate bigints (e.g.: calling bi_import(), int_to_bi(), ...)
// - function calls, allocate bigints, etc.
// - bigint *n = bi_import(c, indata, insize)
// - bi_set_mod(c, n, x)
// - mod function calls
// - bigint *nn = bi_mod_pwr(c, m, e) <-- frees m, e
// - bi_free_mod(c, x) <-- frees n
// - bi_export(c, nn, outdata, outsize) <-- frees nn
// - function calls
// - free bigints calling bi_free()
// - bi_terminate(c) <-- frees c
int mg_rsa_mod_pow(const uint8_t *mod, size_t modsz, const uint8_t *exp, size_t expsz, const uint8_t *msg, size_t msgsz, uint8_t *out, size_t outsz) {
BI_CTX *bi_ctx = bi_initialize();
bigint *n = bi_import(bi_ctx, mod, (int) modsz);
@@ -1570,11 +1581,9 @@ int mg_rsa_mod_pow(const uint8_t *mod, size_t modsz, const uint8_t *exp, size_t
bigint *h = bi_import(bi_ctx, msg, (int) msgsz);
bi_set_mod(bi_ctx, n, 0);
bigint *m1 = bi_mod_power(bi_ctx, h, e);
bi_free_mod(bi_ctx, 0);
bi_export(bi_ctx, m1, out, (int) outsz);
bi_free(bi_ctx, n);
bi_free(bi_ctx, e);
bi_free(bi_ctx, h);
bi_free(bi_ctx, m1);
bi_terminate(bi_ctx);
return 0;
}
+62 -5
View File
@@ -440,9 +440,9 @@ static void mqtt_cb(struct mg_connection *c, int ev, void *ev_data) {
opts.name = mg_url_host(MQTTS_URL);
mg_tls_init(c, &opts);
} else
} else
#endif
if (ev == MG_EV_MQTT_OPEN) {
if (ev == MG_EV_MQTT_OPEN) {
buf[0] = *(int *) ev_data == 0 ? 'X' : 'Y';
} else if (ev == MG_EV_CLOSE) {
buf[0] = 0;
@@ -570,7 +570,7 @@ static void test_mqtt_basic(void) {
struct mg_mqtt_opts opts;
#if MG_TLS
const char *url = MQTTS_URL;
#else
#else
const char *url = "mqtt://broker.hivemq.com:1883";
#endif
int i, retries;
@@ -619,14 +619,14 @@ static void test_mqtt_basic(void) {
#if MG_TLS
// send more than 1 record, content is not relevant
opts.message = mg_str_n((char *)(size_t) test_mqtt_basic, 21098);
opts.message = mg_str_n((char *) (size_t) test_mqtt_basic, 21098);
opts.qos = 1, opts.retain = false, opts.retransmit_id = 0;
mg_mqtt_pub(c, &opts);
tbuf[0] = 0;
for (i = 0; i < 1000 && test_data.flags == 0; i++) mg_mgr_poll(&mgr, 10);
ASSERT(test_data.flags == flags_published);
for (i = 0; i < 1000 && tbuf[0] == 0; i++) mg_mgr_poll(&mgr, 10);
ASSERT(tbuf[0] != 0); // just check we were able to send and receive
ASSERT(tbuf[0] != 0); // just check we were able to send and receive
memset(mbuf + 1, 0, sizeof(mbuf) - 1);
test_data.flags = 0;
opts.retransmit_id = 0;
@@ -3846,12 +3846,69 @@ static void test_x25519(void) {
#endif
}
static void test_rsa(void) {
#if MG_TLS == MG_TLS_BUILTIN
const unsigned char mod[] = {
0x00, 0xba, 0xee, 0x3b, 0x0b, 0x89, 0x58, 0xa6, 0x19, 0x0d, 0x4c, 0x89,
0x1a, 0x85, 0x9a, 0xf4, 0x55, 0xc2, 0xdd, 0x0d, 0xd4, 0x4a, 0xf5, 0xed,
0xda, 0x28, 0x55, 0x2f, 0x64, 0x46, 0x21, 0x9f, 0x46, 0x5c, 0xfa, 0x37,
0x88, 0x11, 0xdf, 0xcb, 0x51, 0x73, 0x42, 0x3d, 0x5e, 0x50, 0xde, 0x11,
0x30, 0x61, 0x04, 0x59, 0xd0, 0xf4, 0x57, 0xed, 0x13, 0x90, 0x32, 0xc5,
0x3f, 0xe6, 0x66, 0xfc, 0x2a, 0x12, 0xa3, 0x1f, 0xd1, 0x77, 0x21, 0x65,
0xdf, 0x9a, 0xcf, 0x04, 0x05, 0xc3, 0x1c, 0xf8, 0x79, 0xb5, 0xf5, 0x97,
0x68, 0x98, 0x2e, 0x96, 0x85, 0x3f, 0xee, 0x71, 0x91, 0xc1, 0x54, 0x71,
0x9a, 0x80, 0x1f, 0xbe, 0x21, 0xd9, 0xc1, 0x80, 0x9b, 0xd0, 0x5d, 0xb3,
0x76, 0x3e, 0xcc, 0x14, 0x3d, 0xec, 0xb7, 0x18, 0x74, 0xfb, 0xc4, 0x0e,
0x56, 0x8d, 0x3d, 0x78, 0xe6, 0xca, 0xcd, 0x9d, 0xc6, 0x20, 0x5a, 0xeb,
0x9b, 0xc8, 0x19, 0x5e, 0xeb, 0x80, 0xd2, 0xb2, 0xfe, 0x88, 0x15, 0x5c,
0x7c, 0x6b, 0x26, 0xe0, 0x43, 0xda, 0xa4, 0x07, 0x85, 0x73, 0xc4, 0x80,
0x28, 0xcb, 0xda, 0x18, 0x56, 0x37, 0x91, 0xd6, 0x41, 0xa1, 0x0b, 0xa2,
0x77, 0xd0, 0x62, 0x31, 0xc7, 0xc2, 0x67, 0x6d, 0x75, 0x08, 0x80, 0xe7,
0xb6, 0xbe, 0xc2, 0x25, 0xc9, 0xe0, 0x2c, 0x02, 0xbf, 0x39, 0x61, 0x7e,
0x32, 0xa4, 0xc9, 0xe7, 0x91, 0xe3, 0xa0, 0xcd, 0x94, 0x24, 0xbf, 0x8c,
0xeb, 0x47, 0x76, 0x53, 0x85, 0xb3, 0xb7, 0x31, 0x80, 0x3c, 0x77, 0x10,
0x69, 0xc3, 0x04, 0xd1, 0x60, 0x4c, 0x74, 0xda, 0x15, 0x18, 0x0b, 0x20,
0x6f, 0xb3, 0x03, 0x58, 0x4a, 0xfc, 0xd1, 0xd2, 0xcf, 0x37, 0x15, 0x0a,
0x63, 0xc8, 0xe9, 0xd5, 0x7d, 0xd5, 0xf2, 0x90, 0x78, 0x53, 0x49, 0xa9,
0xc5, 0x25, 0x65, 0x5c, 0x01};
const unsigned char exp[] = {1, 0, 1}; // 65537
const unsigned char sig[] = {
0x1e, 0xb1, 0x6a, 0xcb, 0x39, 0x63, 0x12, 0xed, 0x85, 0x62, 0x4b, 0x85,
0x47, 0x25, 0x67, 0xbd, 0xbd, 0x0e, 0xaa, 0x73, 0x34, 0x5f, 0x07, 0x2b,
0xbb, 0x4f, 0xf5, 0x21, 0x88, 0xb1, 0x04, 0x2c, 0xbb, 0x52, 0x72, 0x64,
0x89, 0x45, 0x50, 0x41, 0x73, 0xca, 0xda,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b7, 0x80, 0xda, 0x32, 0x69, 0x28, 0xa3, 0xee, 0xb9, 0xd4, 0x90, 0xa6,
0xab, 0x34, 0x17, 0x5e, 0xa0, 0xd6, 0xc1, 0x54, 0xc6, 0x9c, 0x58, 0x3a,
0xaf, 0xbf, 0xdc, 0x64};
unsigned char v[256]; // 2048 bits
mg_rsa_mod_pow(mod, sizeof(mod), exp, sizeof(exp), sig, sizeof(sig), v,
sizeof(v));
ASSERT(v[sizeof(v) - 1] == 0xbc);
#endif
}
static void test_crypto(void) {
test_md5();
test_sha1();
test_sha256();
test_sha384();
test_x25519();
test_rsa();
}
int main(void) {