Skip to content

Commit cfa9e53

Browse files
authored
Fix memory management and API usage in R_cert_info. (#137)
1. Pass NULL for `d2i_X509`'s first parameter to get the parsed certificate as its return value, rather than using the object reuse mode which is "strongly discouraged" -- see https://docs.openssl.org/3.3/man3/d2i_X509/#synopsis. 2. Remove calls to `X509_NAME_free` as it's an error to free the non-owning pointers. 3. Remember to free the `X509` object allocated by the function. 4. Also stop leaking `subjectAltNames`.
1 parent 62e3e95 commit cfa9e53

1 file changed

Lines changed: 5 additions & 4 deletions

File tree

src/cert.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
#include "compatibility.h"
1010

1111
SEXP R_cert_info(SEXP bin, SEXP name_format){
12-
X509 *cert = X509_new();
1312
const unsigned char *ptr = RAW(bin);
14-
bail(!!d2i_X509(&cert, &ptr, LENGTH(bin)));
13+
X509 *cert = d2i_X509(NULL, &ptr, LENGTH(bin));
14+
bail(!!cert);
1515

1616
//out list
1717
int bufsize = 8192;
@@ -32,7 +32,6 @@ SEXP R_cert_info(SEXP bin, SEXP name_format){
3232
BIO_free(b);
3333
SET_VECTOR_ELT(out, 0, Rf_allocVector(STRSXP, 1));
3434
SET_STRING_ELT(VECTOR_ELT(out, 0), 0, Rf_mkCharLenCE(buf, len, CE_UTF8));
35-
X509_NAME_free(name);
3635

3736
//issuer name name
3837
name = X509_get_issuer_name(cert);
@@ -42,7 +41,6 @@ SEXP R_cert_info(SEXP bin, SEXP name_format){
4241
BIO_free(b);
4342
SET_VECTOR_ELT(out, 1, Rf_allocVector(STRSXP, 1));
4443
SET_STRING_ELT(VECTOR_ELT(out, 1), 0, Rf_mkCharLenCE(buf, len, CE_UTF8));
45-
X509_NAME_free(name);
4644

4745
//sign algorithm
4846
const ASN1_BIT_STRING *signature;
@@ -88,6 +86,9 @@ SEXP R_cert_info(SEXP bin, SEXP name_format){
8886
}
8987
}
9088
}
89+
GENERAL_NAMES_free(subjectAltNames);
90+
91+
X509_free(cert);
9192

9293
//return
9394
UNPROTECT(1);

0 commit comments

Comments
 (0)