Skip to content

Commit

Permalink
memleak fix: X509 and X509_REQ structures not disposed off properly (#53
Browse files Browse the repository at this point in the history
)
  • Loading branch information
keldonin authored Feb 2, 2024
1 parent 771d9d5 commit 403fdfa
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 1 deletion.
4 changes: 3 additions & 1 deletion include/pkcs11lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,7 @@ CK_VOID_PTR pkcs11_create_X509_REQ(pkcs11Context *p11Context,
pkcs11AttrList *attrlist) ;

void write_X509_REQ(CK_VOID_PTR req, char *filename, bool verbose);
void pkcs11_free_X509_REQ(CK_VOID_PTR req);

/* pkcs11_cert.c */
CK_VOID_PTR pkcs11_create_X509_CERT(pkcs11Context *p11Context,
Expand All @@ -600,7 +601,8 @@ CK_VOID_PTR pkcs11_create_X509_CERT(pkcs11Context *p11Context,
CK_OBJECT_HANDLE hprivkey,
pkcs11AttrList *attrlist);

void write_X509_CERT(CK_VOID_PTR req, char *filename, bool verbose);
void write_X509_CERT(CK_VOID_PTR crt, char *filename, bool verbose);
void pkcs11_free_X509_CERT(CK_VOID_PTR crt);


// CK_ULONG pkcs11_allocate_and_hash_sha1(CK_BYTE_PTR data, CK_ULONG datalen, CK_VOID_PTR_PTR buf);
Expand Down
9 changes: 9 additions & 0 deletions lib/pkcs11_cert.c
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,15 @@ CK_VOID_PTR pkcs11_create_X509_CERT(pkcs11Context *p11Context,
}


void pkcs11_free_X509_CERT(CK_VOID_PTR crt) {
X509 *xcrt = (X509 *)crt;

if(xcrt) {
X509_free(xcrt);
}
}


void write_X509_CERT(CK_VOID_PTR crt, char *filename, bool verbose)
{

Expand Down
10 changes: 10 additions & 0 deletions lib/pkcs11_req.c
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,16 @@ CK_VOID_PTR pkcs11_create_X509_REQ(pkcs11Context *p11Context,
return retval;
}


void pkcs11_free_X509_REQ(CK_VOID_PTR req) {
X509_REQ *xreq = (X509_REQ *)req;

if(xreq) {
X509_REQ_free(xreq);
}
}


void write_X509_REQ(CK_VOID_PTR req, char *filename, bool verbose)
{

Expand Down
1 change: 1 addition & 0 deletions src/p11mkcert.c
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,7 @@ int main( int argc, char ** argv )
fprintf(stderr, "importing certificate succeeded.\n");
}
}
pkcs11_free_X509_CERT(x509); /* free cert structure */
} else {
fprintf(stderr, "Error: Unable to generate certificate\n");
}
Expand Down
1 change: 1 addition & 0 deletions src/p11req.c
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,7 @@ int main( int argc, char ** argv )

if(req) {
write_X509_REQ(req, filename, verbose);
pkcs11_free_X509_REQ(req);
} else {
fprintf(stderr, "Error: Unable to generate certificate request\n");
}
Expand Down

0 comments on commit 403fdfa

Please sign in to comment.