Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clear outputs in PKCS12_parse error handling #4145

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 13 additions & 8 deletions crypto/pkcs12/p12_kiss.c
Expand Up @@ -34,6 +34,12 @@ int PKCS12_parse(PKCS12 *p12, const char *pass, EVP_PKEY **pkey, X509 **cert,
{
STACK_OF(X509) *ocerts = NULL;
X509 *x = NULL;

if (pkey)
*pkey = NULL;
if (cert)
*cert = NULL;

/* Check for NULL PKCS12 structure */

if (!p12) {
Expand All @@ -42,11 +48,6 @@ int PKCS12_parse(PKCS12 *p12, const char *pass, EVP_PKEY **pkey, X509 **cert,
return 0;
}

if (pkey)
*pkey = NULL;
if (cert)
*cert = NULL;

/* Check the mac */

/*
Expand Down Expand Up @@ -75,7 +76,7 @@ int PKCS12_parse(PKCS12 *p12, const char *pass, EVP_PKEY **pkey, X509 **cert,

if (!ocerts) {
PKCS12err(PKCS12_F_PKCS12_PARSE, ERR_R_MALLOC_FAILURE);
return 0;
goto err;
}

if (!parse_pk12(p12, pass, -1, pkey, ocerts)) {
Expand Down Expand Up @@ -111,10 +112,14 @@ int PKCS12_parse(PKCS12 *p12, const char *pass, EVP_PKEY **pkey, X509 **cert,

err:

if (pkey)
if (pkey) {
EVP_PKEY_free(*pkey);
if (cert)
*pkey = NULL;
}
if (cert) {
X509_free(*cert);
*cert = NULL;
}
X509_free(x);
sk_X509_pop_free(ocerts, X509_free);
return 0;
Expand Down
3 changes: 3 additions & 0 deletions doc/man3/PKCS12_parse.pod
Expand Up @@ -42,6 +42,9 @@ L<UI_OpenSSL(3)>, for example.

PKCS12_parse() returns 1 for success and zero if an error occurred.

If an error occurred B<*pkey> and B<*cert> are set to B<NULL>,
but B<*ca> is not and may need to be cleaned up.

The error can be obtained from L<ERR_get_error(3)>

=head1 BUGS
Expand Down