我需要从内存中添加 PEM 类型证书,这意味着我无法使用内置的从文件读取帮助程序。
我的问题是没有关于如何释放内存的文档。现在我最好的猜测如下:
SSL_CTX_use_certificate(): //X509 structure SHOULD be freed using X509_free(), as in SSL_CTX_use_certificate_file()
SSL_CTX_use_PrivateKey() // EVP_KEY structure SHOULD be freed using EVP_KEY_free(), as in SSL_CTX_use_PrivateKey_file()
SSL_CTX_add_extra_chain_cert() // X509 structure SHOULD NOT be freed, as in SSL_CTX_use_certificate_chain_file()
有时 grep 来源似乎表明SSL_CTX_use_certificate()
增加引用计数,而SSL_CTX_add_extra_chain_cert()
没有。
谁能证实或否认我的怀疑?
使用和不使用以下命令运行该程序X509_free
在瓦尔格林德的领导下。
#include <stdio.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
#include <openssl/x509.h>
int main(int argc, char* argv[])
{
unsigned long err;
SSL_library_init();
OpenSSL_add_all_algorithms();
SSL_CTX* ctx = SSL_CTX_new(SSLv23_method());
err = ERR_get_error();
if(ctx == NULL)
{
printf("SSL_CTX_new failed: 0x%lx\n", err);
exit (1);
}
X509* x509 = X509_new();
err = ERR_get_error();
if(x509 == NULL)
{
printf("X509_new failed: 0x%lx\n", err);
exit (1);
}
long res = SSL_CTX_add_extra_chain_cert(ctx, x509);
err = ERR_get_error();
if(res != 1)
{
printf("SSL_CTX_add_extra_chain_cert failed: 0x%lx\n", err);
exit (1);
}
X509_free(x509);
SSL_CTX_free(ctx);
return 0;
}
随着X509_free(x509)
未注释的:
$ valgrind ./openssl-test.exe
==23505== Memcheck, a memory error detector
==23505== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==23505== Using Valgrind-3.9.0 and LibVEX; rerun with -h for copyright info
==23505== Command: ./openssl-test.exe
==23505==
==23505== WARNING: Support on MacOS 10.8 is experimental and mostly broken.
==23505== WARNING: Expect incorrect results, assertions and crashes.
==23505== WARNING: In particular, Memcheck on 32-bit programs will fail to
==23505== WARNING: detect any errors associated with heap-allocated data.
==23505==
==23505== Invalid read of size 4
==23505== at 0x100001AD9: CRYPTO_add_lock (in ./openssl-test.exe)
==23505== by 0x1000BC62A: asn1_item_combine_free (in ./openssl-test.exe)
==23505== by 0x1000BC5A6: ASN1_item_free (in ./openssl-test.exe)
==23505== by 0x10009CC5F: sk_pop_free (in ./openssl-test.exe)
==23505== by 0x100114862: SSL_CTX_free (in ./openssl-test.exe)
==23505== by 0x100001213: main (openssl-test.c:43)
==23505== Address 0x100202dac is 28 bytes inside a block of size 184 free'd
==23505== at 0x7517: free (vg_replace_malloc.c:472)
==23505== by 0x100002634: CRYPTO_free (in ./openssl-test.exe)
==23505== by 0x1000BC95C: asn1_item_combine_free (in ./openssl-test.exe)
==23505== by 0x1000BC5A6: ASN1_item_free (in ./openssl-test.exe)
==23505== by 0x10000120A: main (openssl-test.c:42)
...
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)