gpt4 book ai didi

ios - PKCS12_newpass 中的内存泄漏

转载 作者:行者123 更新时间:2023-11-29 12:03:18 26 4
gpt4 key购买 nike

我编写了更改 PKCS12 证书密码的函数。

我注意到 PKCS12_newpass 函数会泄漏内存。注释掉这一行时,不会产生内存泄漏。

我该如何修复此内存泄漏?

- (NSData*)changePKCS12:(NSData*)p12Data
oldPassphrase:(NSString*)oldPassphrase
newPassphrase:(NSString*)newPassphrase {
OpenSSL_add_all_algorithms();
BIO *bp = NULL;
PKCS12 *p12 = NULL;
int status = 0;
do {
bp = BIO_new_mem_buf((void *)[p12Data bytes], (int)[p12Data length]);
p12 = d2i_PKCS12_bio(bp, NULL);

// MEMORY LEAK in PKCS12_newpass
status = PKCS12_newpass(p12, (char *)[oldPassphrase UTF8String], (char *)[newPassphrase UTF8String]);
} while (false);

if (p12) {
PKCS12_free(p12);
p12 = NULL;
}
if (bp) {
BIO_free_all(bp);
bp = NULL;
}
EVP_cleanup();
return NULL;
}

最佳答案

这是 Valgrind 报告的两个泄漏:

$ valgrind --leak-check=full ./test.exe 
==32547== Memcheck, a memory error detector
==32547== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==32547== Using Valgrind-3.10.1 and LibVEX; rerun with -h for copyright info
==32547== Command: ./test.exe
==32547==
==32547==
==32547== HEAP SUMMARY:
==32547== in use at exit: 4,044 bytes in 25 blocks
==32547== total heap usage: 3,273 allocs, 3,248 frees, 149,992 bytes allocated
==32547==
==32547== 1,307 (32 direct, 1,275 indirect) bytes in 1 blocks are definitely lost in loss record 22 of 24
==32547== at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==32547== by 0x408A76: CRYPTO_malloc (mem.c:140)
==32547== by 0x408AA9: CRYPTO_zalloc (mem.c:148)
==32547== by 0x447104: asn1_item_embed_new (tasn_new.c:171)
==32547== by 0x446E66: ASN1_item_ex_new (tasn_new.c:88)
==32547== by 0x4439AA: asn1_item_embed_d2i (tasn_dec.c:333)
==32547== by 0x4431B7: ASN1_item_ex_d2i (tasn_dec.c:162)
==32547== by 0x44314A: ASN1_item_d2i (tasn_dec.c:152)
==32547== by 0x4AB8BA: PKCS12_item_decrypt_d2i (p12_decr.c:159)
==32547== by 0x40CA79: PKCS8_decrypt (p12_p8d.c:69)
==32547== by 0x40C8DC: newpass_bag (p12_npas.c:206)
==32547== by 0x40C868: newpass_bags (p12_npas.c:188)
==32547==
==32547== 2,625 (32 direct, 2,593 indirect) bytes in 1 blocks are definitely lost in loss record 24 of 24
==32547== at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==32547== by 0x408A76: CRYPTO_malloc (mem.c:140)
==32547== by 0x408AA9: CRYPTO_zalloc (mem.c:148)
==32547== by 0x41258E: sk_new (stack.c:153)
==32547== by 0x41256A: sk_new_null (stack.c:146)
==32547== by 0x40C27E: sk_PKCS7_new_null (pkcs7.h:199)
==32547== by 0x40C489: newpass_p12 (p12_npas.c:118)
==32547== by 0x40C3CC: PKCS12_newpass (p12_npas.c:96)
==32547== by 0x40315F: main (in /home/openssl/test.exe)

第一个是由于0x40C8DC: newpass_bag (p12_npas.c:206) :

X509_SIG_get0(&shalg, NULL, bag->value.shkeybag);

然而,get0X509_SIG_get0不会影响引用计数,所以我认为它确实是它之前的那一行(或者是 X509_SIG_get0 中的错误):

if (PKCS12_SAFEBAG_get_nid(bag) != NID_pkcs8ShroudedKeyBag)

PKCS12_SAFEBAG_get_nid没有记录。这意味着它是一个私有(private) API,因此 OpenSSL 开发人员必须修复由此引起的泄漏。 (我认为它实际上是由于 PKCS12_item_decrypt_d2i 在堆栈中更深一点,但由于 PKCS12_SAFEBAG_get_nid 而无法触及)。

第二个是由于0x40C489: newpass_p12 (p12_npas.c:118) :

if ((newsafes = sk_PKCS7_new_null()) == NULL)
return 0;

sk_PKCS7_new_null没有记录。这意味着它是一个私有(private) API,因此 OpenSSL 开发人员必须修复由此引起的泄漏。


How I could fix this memory leak?

不幸的是,您不能这样做,因为这两个违规者都是私有(private) API。尽你所能在 RT 报告它们,这是 OpenSSL bug tracker .

关于文档和私有(private) API 等,有一些出乎意料的“大事”。参见 EC_KEY_priv2buf(): check parameter sanity进行讨论和新规则。


根据来源搜索,PKCS12_newpass缺少文档,所以它也是一个私有(private) API(没有用于构建手册页的 POD 文件):

$ grep -IR PKCS12_newpass *
CHANGES: *) New function PKCS12_newpass() which changes the password of a
crypto/pkcs12/pk12err.c: {ERR_FUNC(PKCS12_F_PKCS12_NEWPASS), "PKCS12_newpass"},
crypto/pkcs12/p12_npas.c:int PKCS12_newpass(PKCS12 *p12, const char *oldpass, const char *newpass)
crypto.map: PKCS12_newpass;
include/openssl/pkcs12.h:int PKCS12_newpass(PKCS12 *p12, const char *oldpass, const char *newpass);
util/libcrypto.num:PKCS12_newpass 3204 1_1_0 EXIST::FUNCTION:

Issue 4478: DOCUMENTATION PKCS12_newpass 提交了错误报告和文档.它应该有助于超越“文档其他为私有(private)”规则。


下面是一个cat test.cc :

#include "openssl/pkcs12.h"
#include "openssl/bio.h"
#include "openssl/engine.h"
#include "openssl/conf.h"
#include "openssl/err.h"

/* openssl req -x509 -newkey rsa:1024 -keyout key.pem -nodes -out cert.pem -days 365 */
/* openssl pkcs12 -export -out pkcs12.p12 -inkey key.pem -in cert.pem */

/* gcc -ansi -I . -I ./include test.cc ./libcrypto.a -o test.exe */

int main(int argc, char* argv[])
{
OpenSSL_add_all_algorithms();

BIO *bp = NULL;
PKCS12 *p12 = NULL;
int rc = -1;
unsigned long err = 0;
char password[] = "passphrase";

bp = BIO_new_file("pkcs12.p12", "r");
if (bp == NULL) goto cleanup;

p12 = d2i_PKCS12_bio(bp, NULL);
if (p12 == NULL) goto cleanup;

/* Use empty string when no password was applies with 'openssl pkcs12' */
rc = PKCS12_newpass(p12, password, password);

cleanup:

if (rc == 1)
{
fprintf(stdout, "Sucessfully changed password\n");
}
else
{
err = ERR_get_error();
fprintf(stderr, "Failed to change password, error %lu\n", err);
}

if (p12)
PKCS12_free(p12);

if (bp)
BIO_free_all(bp);

/* http://wiki.openssl.org/index.php/Library_Initialization#Cleanup */
ENGINE_cleanup();
CONF_modules_unload(1);
EVP_cleanup();
CRYPTO_cleanup_all_ex_data();
#if OPENSSL_API_COMPAT < 0x10000000L
ERR_remove_state(0);
#else
ERR_remove_thread_state();
#endif
ERR_free_strings();

return 0;
}

关于ios - PKCS12_newpass 中的内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36079538/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com