Skip to content

Commit a0e5313

Browse files
committed
[x509] Fix some memory leaks when using OpenSSL APIs
1 parent b921bbf commit a0e5313

File tree

3 files changed

+8
-7
lines changed

3 files changed

+8
-7
lines changed

src/tools/x509/Certificate.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ void Certificate::convertCertificateRequest(void* request, const void* issuer, v
249249
// Set the public key
250250
EVP_PKEY* public_key = X509_REQ_get_pubkey(cert_request);
251251
X509_set_pubkey(cert, public_key);
252+
EVP_PKEY_free(public_key);
252253

253254
// Set the extensions
254255
STACK_OF(X509_EXTENSION)* extensions = X509_REQ_get_extensions(cert_request);
@@ -278,7 +279,7 @@ void Certificate::convertCertificateRequest(void* request, const void* issuer, v
278279
if (val)
279280
{
280281
X509_add1_ext_i2d(cert, NID_issuer_alt_name, val, crit, 0);
281-
OPENSSL_free(val);
282+
sk_GENERAL_NAME_pop_free((STACK_OF(GENERAL_NAME)*)val, GENERAL_NAME_free);
282283
}
283284
}
284285

@@ -388,13 +389,11 @@ void Certificate::readInfos(Certificate& certificate)
388389
{
389390
void* ext = X509_get_ext_d2i(cert, NID_issuer_alt_name, nullptr, nullptr);
390391
certificate.m_x509v3_extensions.issuer_alternate_names = convertGeneralNames(ext);
391-
OPENSSL_free(ext);
392392
}
393393
else if (extension_obj_nid == NID_subject_alt_name)
394394
{
395395
void* ext = X509_get_ext_d2i(cert, NID_subject_alt_name, nullptr, nullptr);
396396
certificate.m_x509v3_extensions.subject_alternate_names = convertGeneralNames(ext);
397-
OPENSSL_free(ext);
398397
}
399398
else if (extension_obj_nid == NID_basic_constraints)
400399
{
@@ -411,7 +410,7 @@ void Certificate::readInfos(Certificate& certificate)
411410
static_cast<unsigned int>(ASN1_INTEGER_get(basic_constraint->pathlen));
412411
}
413412
}
414-
OPENSSL_free(basic_constraint);
413+
BASIC_CONSTRAINTS_free(basic_constraint);
415414
}
416415
}
417416
else

src/tools/x509/X509Document.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,10 +174,10 @@ std::string X509Document::convertX509Name(const void* px509_name)
174174
}
175175

176176
/** @brief Convert a list of strings in GENERAL_NAMES format to a standard vector of strings representation */
177-
std::vector<std::string> X509Document::convertGeneralNames(const void* pgeneral_names)
177+
std::vector<std::string> X509Document::convertGeneralNames(void* pgeneral_names)
178178
{
179179
std::vector<std::string> names;
180-
const STACK_OF(GENERAL_NAME)* general_names = reinterpret_cast<const STACK_OF(GENERAL_NAME)*>(pgeneral_names);
180+
STACK_OF(GENERAL_NAME)* general_names = reinterpret_cast<STACK_OF(GENERAL_NAME)*>(pgeneral_names);
181181

182182
int names_count = sk_GENERAL_NAME_num(general_names);
183183
for (int i = 0; i < names_count; i++)
@@ -216,6 +216,8 @@ std::vector<std::string> X509Document::convertGeneralNames(const void* pgeneral_
216216
}
217217
}
218218

219+
sk_GENERAL_NAME_pop_free(general_names, GENERAL_NAME_free);
220+
219221
return names;
220222
}
221223

src/tools/x509/X509Document.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ class X509Document
232232
/** @brief Convert a string in X509_NAME format to a standard string representation */
233233
static std::string convertX509Name(const void* px509_name);
234234
/** @brief Convert a list of strings in GENERAL_NAMES format to a standard vector of strings representation */
235-
static std::vector<std::string> convertGeneralNames(const void* pgeneral_names);
235+
static std::vector<std::string> convertGeneralNames(void* pgeneral_names);
236236
/** @brief Parse a subject's string */
237237
static void parseSubjectString(const void* px509_name, Subject& subject);
238238
};

0 commit comments

Comments
 (0)