From: Tobias Brunner Date: Wed, 10 Feb 2021 16:55:06 +0000 (+0100) Subject: openssl: Allocate our own buffer for i2d_* wrapper macro X-Git-Tag: 5.9.2dr2~6 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=781ad0b93b13c9e7234d2f86711e3aaf8fce38c3;p=thirdparty%2Fstrongswan.git openssl: Allocate our own buffer for i2d_* wrapper macro If we pass a pointer to NULL, the memory allocated by OpenSSL has to be freed with OPENSSL_free(). Otherwise, this can lead to random crashes/freezes for Windows builds as seen on AppVeyor. To not complicate things for callers of this macro, we allocate our own memory, which we already do for other i2d_* calls. --- diff --git a/src/libstrongswan/plugins/openssl/openssl_util.h b/src/libstrongswan/plugins/openssl/openssl_util.h index eb2a3788bb..f226fef0e4 100644 --- a/src/libstrongswan/plugins/openssl/openssl_util.h +++ b/src/libstrongswan/plugins/openssl/openssl_util.h @@ -101,9 +101,14 @@ bool openssl_bn2chunk(const BIGNUM *bn, chunk_t *chunk); * @returns allocated chunk of the object, or chunk_empty */ #define openssl_i2chunk(type, obj) ({ \ - unsigned char *ptr = NULL; \ - int len = i2d_##type(obj, &ptr); \ - len < 0 ? chunk_empty : chunk_create(ptr, len);}) + chunk_t chunk = chunk_empty; \ + int len = i2d_##type(obj, NULL); \ + if (len >= 0) { \ + chunk = chunk_alloc(len); \ + u_char *p = chunk.ptr; \ + i2d_##type(obj, &p); \ + } \ + chunk; }) /** * Convert an OpenSSL ASN1_OBJECT to a chunk.