From 715c17c469f6c463dd511a5deb229da4de9ee100 Mon Sep 17 00:00:00 2001
From: Jakub Jelen <jjelen@redhat.com>
Date: Wed, 17 Mar 2021 20:17:34 +0100
Subject: [PATCH] oberthur: Fix memory leaks

Thanks oss-fuzz

https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=32149
---
 src/libopensc/pkcs15-oberthur.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/src/libopensc/pkcs15-oberthur.c b/src/libopensc/pkcs15-oberthur.c
index 314a7bd..4ba201f 100644
--- a/src/libopensc/pkcs15-oberthur.c
+++ b/src/libopensc/pkcs15-oberthur.c
@@ -211,6 +211,7 @@ sc_oberthur_get_certificate_authority(struct sc_pkcs15_der *der, int *out_author
 
 	BIO_set_mem_buf(bio, &buf_mem, BIO_NOCLOSE);
 	x = d2i_X509_bio(bio, 0);
+	free(buf_mem.data);
 	BIO_free(bio);
 	if (!x)
 		return SC_ERROR_INVALID_DATA;
@@ -729,7 +730,10 @@ sc_pkcs15emu_oberthur_add_cert(struct sc_pkcs15_card *p15card, unsigned int file
 	cinfo.value.len = cert_len;
 
 	rv = sc_oberthur_get_certificate_authority(&cinfo.value, &cinfo.authority);
-	LOG_TEST_RET(ctx, rv, "Failed to add certificate: get certificate attributes error");
+	if (rv != SC_SUCCESS) {
+		free(cinfo.value.value);
+		LOG_TEST_RET(ctx, rv, "Failed to add certificate: get certificate attributes error");
+	}
 
 	if (flags & OBERTHUR_ATTR_MODIFIABLE)
 		cobj.flags |= SC_PKCS15_CO_FLAG_MODIFIABLE;
-- 
1.8.3.1

