diff options
author | Tobias Weimer <wishmaster51@googlemail.com> | 2016-01-25 21:20:35 +0000 |
---|---|---|
committer | Tobias Weimer <wishmaster51@googlemail.com> | 2016-01-25 21:20:35 +0000 |
commit | 80148955f82c205cc94f0112e0fbfe8f91bc4330 (patch) | |
tree | 252a0ae41cdb2ad3d798e311fb728b04e5bdbdbd /plugins/CryptoPP | |
parent | a289103f2834d2219799f8d84e29a6d69c00574a (diff) |
Crypto++: Fix for many memory leaks introduced in [6593].
Note that replaceStr already does mir_strdup the argument!
git-svn-id: http://svn.miranda-ng.org/main/trunk@16168 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c
Diffstat (limited to 'plugins/CryptoPP')
-rw-r--r-- | plugins/CryptoPP/src/cpp_cntx.cpp | 3 | ||||
-rw-r--r-- | plugins/CryptoPP/src/cpp_gpgw.cpp | 46 | ||||
-rw-r--r-- | plugins/CryptoPP/src/cpp_keys.cpp | 10 | ||||
-rw-r--r-- | plugins/CryptoPP/src/cpp_misc.cpp | 52 | ||||
-rw-r--r-- | plugins/CryptoPP/src/cpp_pgpw.cpp | 47 | ||||
-rw-r--r-- | plugins/CryptoPP/src/cpp_rsam.cpp | 12 | ||||
-rw-r--r-- | plugins/CryptoPP/src/cpp_svcs.cpp | 28 |
7 files changed, 124 insertions, 74 deletions
diff --git a/plugins/CryptoPP/src/cpp_cntx.cpp b/plugins/CryptoPP/src/cpp_cntx.cpp index 867060daba..327890f4ea 100644 --- a/plugins/CryptoPP/src/cpp_cntx.cpp +++ b/plugins/CryptoPP/src/cpp_cntx.cpp @@ -70,7 +70,8 @@ PBYTE cpp_alloc_pdata(pCNTX ptr) // free memory from keys
void cpp_free_keys(pCNTX ptr)
{
- replaceStr(ptr->tmp, 0);
+ mir_free(ptr->tmp);
+ ptr->tmp = 0;
cpp_alloc_pdata(ptr);
if (ptr->mode & MODE_PGP) {
pPGPDATA p = (pPGPDATA)ptr->pdata;
diff --git a/plugins/CryptoPP/src/cpp_gpgw.cpp b/plugins/CryptoPP/src/cpp_gpgw.cpp index 2e62b77deb..a86d2219cd 100644 --- a/plugins/CryptoPP/src/cpp_gpgw.cpp +++ b/plugins/CryptoPP/src/cpp_gpgw.cpp @@ -65,16 +65,16 @@ LPSTR __cdecl gpg_encrypt(pCNTX ptr, LPCSTR szPlainMsg) ptr->error = ERROR_NONE;
pGPGDATA p = (pGPGDATA)ptr->pdata;
- LPSTR szEncMsg;
- szEncMsg = _gpg_encrypt(szPlainMsg, (LPCSTR)p->gpgKeyID);
+ LPSTR szEncMsg = _gpg_encrypt(szPlainMsg, (LPCSTR)p->gpgKeyID);
+ mir_free(ptr->tmp);
if (!szEncMsg) {
- replaceStr(ptr->tmp, 0);
- return 0;
+ return ptr->tmp = 0;
+ }
+ else {
+ ptr->tmp = mir_strdup(szEncMsg);
+ LocalFree((LPVOID)szEncMsg);
+ return ptr->tmp;
}
-
- replaceStr(ptr->tmp, mir_strdup(szEncMsg));
- LocalFree((LPVOID)szEncMsg);
- return ptr->tmp;
}
LPSTR __cdecl gpg_decrypt(pCNTX ptr, LPCSTR szEncMsg)
@@ -82,7 +82,8 @@ LPSTR __cdecl gpg_decrypt(pCNTX ptr, LPCSTR szEncMsg) ptr->error = ERROR_NONE;
LPSTR szPlainMsg = _gpg_decrypt(szEncMsg);
- replaceStr(ptr->tmp, mir_strdup(szPlainMsg));
+ mir_free(ptr->tmp);
+ ptr->tmp = mir_strdup(szPlainMsg);
LocalFree((LPVOID)szPlainMsg);
return ptr->tmp;
@@ -90,9 +91,14 @@ LPSTR __cdecl gpg_decrypt(pCNTX ptr, LPCSTR szEncMsg) LPSTR __cdecl gpg_encode(HANDLE context, LPCSTR szPlainMsg)
{
- pCNTX ptr = get_context_on_id(context); if (!ptr) return NULL;
+ pCNTX ptr = get_context_on_id(context);
+ if (!ptr)
+ return NULL;
pGPGDATA p = (pGPGDATA)cpp_alloc_pdata(ptr);
- if (!p->gpgKeyID) { ptr->error = ERROR_NO_GPG_KEY; return NULL; }
+ if (!p->gpgKeyID) {
+ ptr->error = ERROR_NO_GPG_KEY;
+ return NULL;
+ }
// utf8 message: encrypt.
LPSTR szUtfMsg;
@@ -112,24 +118,22 @@ LPSTR __cdecl gpg_encode(HANDLE context, LPCSTR szPlainMsg) LPSTR __cdecl gpg_decode(HANDLE context, LPCSTR szEncMsg)
{
pCNTX ptr = get_context_on_id(context);
- if (!ptr) return NULL;
+ if (!ptr)
+ return NULL;
LPSTR szNewMsg = NULL;
LPSTR szOldMsg = gpg_decrypt(ptr, szEncMsg);
if (szOldMsg) {
if (!is_7bit_string(szOldMsg) && !is_utf8_string(szOldMsg)) {
- int slen = (int)strlen(szOldMsg) + 1;
- LPWSTR wszMsg = (LPWSTR)alloca(slen*sizeof(WCHAR));
- MultiByteToWideChar(CP_ACP, 0, szOldMsg, -1, wszMsg, slen*sizeof(WCHAR));
- szNewMsg = _strdup(utf8encode(wszMsg));
+ szNewMsg = mir_utf8encode(szOldMsg);
}
else {
- szNewMsg = _strdup(szOldMsg);
+ szNewMsg = mir_strdup(szOldMsg);
}
}
- replaceStr(ptr->tmp, szNewMsg);
- return szNewMsg;
+ mir_free(ptr->tmp);
+ return ptr->tmp = szNewMsg;
}
int __cdecl gpg_set_key(HANDLE context, LPCSTR RemoteKey)
@@ -152,7 +156,9 @@ int __cdecl gpg_set_key(HANDLE context, LPCSTR RemoteKey) int __cdecl gpg_set_keyid(HANDLE context, LPCSTR RemoteKeyID)
{
- pCNTX ptr = get_context_on_id(context); if (!ptr) return 0;
+ pCNTX ptr = get_context_on_id(context);
+ if (!ptr)
+ return 0;
pGPGDATA p = (pGPGDATA)cpp_alloc_pdata(ptr);
ptr->error = ERROR_NONE;
diff --git a/plugins/CryptoPP/src/cpp_keys.cpp b/plugins/CryptoPP/src/cpp_keys.cpp index ec1659f179..37a197d70f 100644 --- a/plugins/CryptoPP/src/cpp_keys.cpp +++ b/plugins/CryptoPP/src/cpp_keys.cpp @@ -56,10 +56,14 @@ LPSTR __cdecl cpp_init_keya(HANDLE context, int features) }
memcpy((PVOID)&publ1[KEYSIZE], (PVOID)&send_features, 2);
+ mir_free(ptr->tmp);
if (ptr->mode & MODE_BASE64 || features & FEATURES_NEWPG)
- replaceStr(ptr->tmp, mir_base64_encode(publ1, KEYSIZE + 2));
- else
- replaceStr(ptr->tmp, base16encode((LPSTR)&publ1, KEYSIZE + 2));
+ ptr->tmp = mir_base64_encode(publ1, KEYSIZE + 2);
+ else {
+ char *base16 = base16encode((LPSTR)&publ1, KEYSIZE + 2);
+ ptr->tmp = mir_strdup(base16);
+ free(base16);
+ }
return ptr->tmp;
}
diff --git a/plugins/CryptoPP/src/cpp_misc.cpp b/plugins/CryptoPP/src/cpp_misc.cpp index a0721ed5f3..6f9ca08ab4 100644 --- a/plugins/CryptoPP/src/cpp_misc.cpp +++ b/plugins/CryptoPP/src/cpp_misc.cpp @@ -2,13 +2,17 @@ int __cdecl cpp_get_features(HANDLE context)
{
- pCNTX ptr = get_context_on_id(context); if (!ptr) return 0;
+ pCNTX ptr = get_context_on_id(context);
+ if (!ptr)
+ return 0;
return ptr->features;
}
int __cdecl cpp_get_error(HANDLE context)
{
- pCNTX ptr = get_context_on_id(context); if (!ptr) return 0;
+ pCNTX ptr = get_context_on_id(context);
+ if (!ptr)
+ return 0;
return ptr->error;
}
@@ -19,7 +23,9 @@ int __cdecl cpp_get_version(void) BOOL cpp_get_simdata(HANDLE context, pCNTX *ptr, pSIMDATA *p)
{
- *ptr = get_context_on_id(context); if (!ptr) return 0;
+ if (!ptr)
+ return 0;
+ *ptr = get_context_on_id(context);
if (!*ptr || (*ptr)->mode&(MODE_PGP | MODE_GPG | MODE_RSA)) return FALSE;
*p = (pSIMDATA)cpp_alloc_pdata(*ptr);
return TRUE;
@@ -32,14 +38,20 @@ int __cdecl cpp_size_keyx(void) void __cdecl cpp_get_keyx(HANDLE context, BYTE *key)
{
- pCNTX ptr; pSIMDATA p; if (!cpp_get_simdata(context, &ptr, &p)) return;
+ pCNTX ptr;
+ pSIMDATA p;
+ if (!cpp_get_simdata(context, &ptr, &p))
+ return;
memcpy(key, p->KeyX, Tiger::DIGESTSIZE);
memcpy(key + Tiger::DIGESTSIZE, &ptr->features, 2);
}
void __cdecl cpp_set_keyx(HANDLE context, BYTE *key)
{
- pCNTX ptr; pSIMDATA p; if (!cpp_get_simdata(context, &ptr, &p)) return;
+ pCNTX ptr;
+ pSIMDATA p;
+ if (!cpp_get_simdata(context, &ptr, &p))
+ return;
SAFE_FREE(p->PubA);
SAFE_FREE(p->KeyA);
mir_free(p->KeyB); p->KeyB = 0;
@@ -51,7 +63,10 @@ void __cdecl cpp_set_keyx(HANDLE context, BYTE *key) void __cdecl cpp_get_keyp(HANDLE context, BYTE *key)
{
- pCNTX ptr; pSIMDATA p; if (!cpp_get_simdata(context, &ptr, &p)) return;
+ pCNTX ptr;
+ pSIMDATA p;
+ if (!cpp_get_simdata(context, &ptr, &p))
+ return;
memcpy(key, p->KeyP, Tiger::DIGESTSIZE);
}
@@ -62,7 +77,10 @@ int __cdecl cpp_size_keyp(void) void __cdecl cpp_set_keyp(HANDLE context, BYTE *key)
{
- pCNTX ptr; pSIMDATA p; if (!cpp_get_simdata(context, &ptr, &p)) return;
+ pCNTX ptr;
+ pSIMDATA p;
+ if (!cpp_get_simdata(context, &ptr, &p))
+ return;
SAFE_FREE(p->KeyP);
p->KeyP = (PBYTE)malloc(Tiger::DIGESTSIZE);
memcpy(p->KeyP, key, Tiger::DIGESTSIZE);
@@ -70,24 +88,36 @@ void __cdecl cpp_set_keyp(HANDLE context, BYTE *key) int __cdecl cpp_keya(HANDLE context)
{
- pCNTX ptr; pSIMDATA p; if (!cpp_get_simdata(context, &ptr, &p)) return 0;
+ pCNTX ptr;
+ pSIMDATA p;
+ if (!cpp_get_simdata(context, &ptr, &p))
+ return 0;
return p->KeyA != NULL;
}
int __cdecl cpp_keyb(HANDLE context)
{
- pCNTX ptr; pSIMDATA p; if (!cpp_get_simdata(context, &ptr, &p)) return 0;
+ pCNTX ptr;
+ pSIMDATA p;
+ if (!cpp_get_simdata(context, &ptr, &p))
+ return 0;
return p->KeyB != NULL;
}
int __cdecl cpp_keyx(HANDLE context)
{
- pCNTX ptr; pSIMDATA p; if (!cpp_get_simdata(context, &ptr, &p)) return 0;
+ pCNTX ptr;
+ pSIMDATA p;
+ if (!cpp_get_simdata(context, &ptr, &p))
+ return 0;
return p->KeyX != NULL;
}
int __cdecl cpp_keyp(HANDLE context)
{
- pCNTX ptr; pSIMDATA p; if (!cpp_get_simdata(context, &ptr, &p)) return 0;
+ pCNTX ptr;
+ pSIMDATA p;
+ if (!cpp_get_simdata(context, &ptr, &p))
+ return 0;
return p->KeyP != NULL;
}
diff --git a/plugins/CryptoPP/src/cpp_pgpw.cpp b/plugins/CryptoPP/src/cpp_pgpw.cpp index 85de0513f0..366a3776de 100644 --- a/plugins/CryptoPP/src/cpp_pgpw.cpp +++ b/plugins/CryptoPP/src/cpp_pgpw.cpp @@ -135,27 +135,26 @@ LPSTR __cdecl pgp_get_error() LPSTR __cdecl pgp_encrypt(pCNTX ptr, LPCSTR szPlainMsg)
{
ptr->error = ERROR_NONE;
+ mir_free(ptr->tmp);
pPGPDATA p = (pPGPDATA)ptr->pdata;
- LPSTR szEncMsg;
- if (p->pgpKey)
- szEncMsg = p_pgp_encrypt_key(szPlainMsg, (LPCSTR)p->pgpKey);
- else
- szEncMsg = p_pgp_encrypt_keydb(szPlainMsg, p->pgpKeyID);
+ LPSTR szEncMsg = (p->pgpKey ? p_pgp_encrypt_key(szPlainMsg, (LPCSTR)p->pgpKey) : p_pgp_encrypt_keydb(szPlainMsg, p->pgpKeyID));
if (!szEncMsg) {
- replaceStr(ptr->tmp, NULL);
- return 0;
+ return ptr->tmp = 0;
}
+ else {
+ ptr->tmp = mir_strdup(szEncMsg);
+ LocalFree((LPVOID)szEncMsg);
- replaceStr(ptr->tmp, mir_strdup(szEncMsg));
- LocalFree((LPVOID)szEncMsg);
-
- return ptr->tmp;
+ return ptr->tmp;
+ }
}
LPSTR __cdecl pgp_decrypt(pCNTX ptr, LPCSTR szEncMsg)
{
ptr->error = ERROR_NONE;
+ mir_free(ptr->tmp);
+ ptr->tmp = NULL;
LPSTR szPlainMsg = p_pgp_decrypt_keydb(szEncMsg);
if (!szPlainMsg) {
@@ -166,12 +165,11 @@ LPSTR __cdecl pgp_decrypt(pCNTX ptr, LPCSTR szEncMsg) szPlainMsg = p_pgp_decrypt_key(szEncMsg, (LPCSTR)p->pgpKey);
}
if (!szPlainMsg) {
- replaceStr(ptr->tmp, NULL);
return NULL;
}
}
- replaceStr(ptr->tmp, mir_strdup(szPlainMsg));
+ ptr->tmp = mir_strdup(szPlainMsg);
LocalFree((LPVOID)szPlainMsg);
return ptr->tmp;
}
@@ -192,22 +190,21 @@ LPSTR __cdecl pgp_encode(HANDLE context, LPCSTR szPlainMsg) LPSTR __cdecl pgp_decode(HANDLE context, LPCSTR szEncMsg)
{
pCNTX ptr = get_context_on_id(context);
- if (!ptr) return NULL;
+ if (!ptr)
+ return NULL;
+ mir_free(ptr->tmp);
LPSTR szNewMsg = NULL;
LPSTR szOldMsg = pgp_decrypt(ptr, szEncMsg);
if (szOldMsg) {
if (!is_7bit_string(szOldMsg) && !is_utf8_string(szOldMsg)) {
- int slen = (int)strlen(szOldMsg) + 1;
- LPWSTR wszMsg = (LPWSTR)alloca(slen*sizeof(WCHAR));
- MultiByteToWideChar(CP_ACP, 0, szOldMsg, -1, wszMsg, slen*sizeof(WCHAR));
- szNewMsg = _strdup(utf8encode(wszMsg));
+ szNewMsg = mir_utf8encode(szOldMsg);
}
- else szNewMsg = _strdup(szOldMsg);
+ else
+ szNewMsg = mir_strdup(szOldMsg);
}
- replaceStr(ptr->tmp, szNewMsg);
- return szNewMsg;
+ return ptr->tmp = szNewMsg;
}
int __cdecl pgp_set_priv_key(LPCSTR LocalKey)
@@ -217,7 +214,9 @@ int __cdecl pgp_set_priv_key(LPCSTR LocalKey) int __cdecl pgp_set_key(HANDLE context, LPCSTR RemoteKey)
{
- pCNTX ptr = get_context_on_id(context); if (!ptr) return 0;
+ pCNTX ptr = get_context_on_id(context);
+ if (!ptr)
+ return 0;
pPGPDATA p = (pPGPDATA)cpp_alloc_pdata(ptr);
ptr->error = ERROR_NONE;
@@ -229,7 +228,9 @@ int __cdecl pgp_set_key(HANDLE context, LPCSTR RemoteKey) int __cdecl pgp_set_keyid(HANDLE context, PVOID RemoteKeyID)
{
- pCNTX ptr = get_context_on_id(context); if (!ptr) return 0;
+ pCNTX ptr = get_context_on_id(context);
+ if (!ptr)
+ return 0;
pPGPDATA p = (pPGPDATA)cpp_alloc_pdata(ptr);
ptr->error = ERROR_NONE;
diff --git a/plugins/CryptoPP/src/cpp_rsam.cpp b/plugins/CryptoPP/src/cpp_rsam.cpp index f20d1c399c..ebf0564bc6 100644 --- a/plugins/CryptoPP/src/cpp_rsam.cpp +++ b/plugins/CryptoPP/src/cpp_rsam.cpp @@ -408,27 +408,27 @@ LPSTR __cdecl rsa_recv(HANDLE context, LPCSTR msg) case 0x70: // получили AES сообщение, декодируем
{
+ mir_free(ptr->tmp);
string msg = decode_msg(p, data);
if (msg.length())
- replaceStr(ptr->tmp, mir_strdup(msg.c_str()));
+ return ptr->tmp = mir_strdup(msg.c_str());
else {
imp->rsa_notify(context, -5); // ошибка декодирования AES сообщения
- replaceStr(ptr->tmp, NULL);
+ return ptr->tmp = NULL;
}
}
- return ptr->tmp;
case 0xE0: // получили RSA сообщение, декодируем
{
+ mir_free(ptr->tmp);
string msg = decode_rsa(p, r, data);
if (msg.length())
- replaceStr(ptr->tmp, mir_strdup(msg.c_str()));
+ return ptr->tmp = mir_strdup(msg.c_str());
else {
imp->rsa_notify(context, -6); // ошибка декодирования RSA сообщения
- replaceStr(ptr->tmp, NULL);
+ return ptr->tmp = NULL;
}
}
- return ptr->tmp;
case 0xF0: // разрыв соединения вручную
{
diff --git a/plugins/CryptoPP/src/cpp_svcs.cpp b/plugins/CryptoPP/src/cpp_svcs.cpp index 01b95bc2d9..ed5af8bdbb 100644 --- a/plugins/CryptoPP/src/cpp_svcs.cpp +++ b/plugins/CryptoPP/src/cpp_svcs.cpp @@ -48,10 +48,14 @@ LPSTR __cdecl cpp_encrypt(pCNTX ptr, LPCSTR szPlainMsg) ciphered.insert(0, (LPSTR)&dataflag, 1);
clen = (unsigned)ciphered.length();
+ mir_free(ptr->tmp);
if (ptr->features & FEATURES_BASE64)
- replaceStr(ptr->tmp, mir_base64_encode((PBYTE)ciphered.data(), clen));
- else
- replaceStr(ptr->tmp, base16encode(ciphered.data(), clen));
+ ptr->tmp = mir_base64_encode((PBYTE)ciphered.data(), clen);
+ else {
+ char *base16 = base16encode(ciphered.data(), clen);
+ ptr->tmp = mir_strdup(base16);
+ free(base16);
+ }
return ptr->tmp;
}
@@ -116,13 +120,16 @@ LPSTR __cdecl cpp_decrypt(pCNTX ptr, LPCSTR szEncMsg) cbcDecryptor.Put((PBYTE)bciphered, clen);
cbcDecryptor.MessageEnd();
+ mir_free(ptr->tmp);
+
if (dataflag & DATA_GZIP) {
size_t clen2 = clen;
LPSTR res = (LPSTR)cpp_gunzip((PBYTE)unciphered.data(), unciphered.length(), clen2);
- replaceStr(ptr->tmp, mir_strndup(res, clen2));
+ ptr->tmp = mir_strndup(res, clen2);
free(res);
}
- else replaceStr(ptr->tmp, mir_strdup(unciphered.c_str()));
+ else
+ ptr->tmp = mir_strdup(unciphered.c_str());
ptr->error = ERROR_NONE;
return ptr->tmp;
@@ -249,15 +256,17 @@ LPSTR __cdecl cpp_decode(HANDLE context, LPCSTR szEncMsg) memcpy(szNewMsg + slen, wstring, slen*sizeof(WCHAR));
}
}
- replaceStr(ptr->tmp, szNewMsg);
- return szNewMsg;
+ mir_free(ptr->tmp);
+ return ptr->tmp = szNewMsg;
}
// decode message return UTF8z
LPSTR __cdecl cpp_decodeU(HANDLE context, LPCSTR szEncMsg)
{
pCNTX ptr = get_context_on_id(context);
- if (!ptr) return NULL;
+ if (!ptr)
+ return NULL;
+ mir_free(ptr->tmp);
cpp_alloc_pdata(ptr); pSIMDATA p = (pSIMDATA)ptr->pdata;
if (!p->KeyX) { ptr->error = ERROR_NO_KEYX; return NULL; }
@@ -277,8 +286,7 @@ LPSTR __cdecl cpp_decodeU(HANDLE context, LPCSTR szEncMsg) szNewMsg = mir_strdup(utf8encode(wstring));
}
}
- replaceStr(ptr->tmp, szNewMsg);
- return szNewMsg;
+ return ptr->tmp = szNewMsg;
}
int __cdecl cpp_encrypt_file(HANDLE context, LPCSTR file_in, LPCSTR file_out)
|