Fix memory leaks in pkread.c demo file

Also make various changes to bring the file into line with current coding
style.

Fixes #8456

Reviewed-by: Paul Dale <paul.dale@oracle.com>
Reviewed-by: Paul Yang <yang.yang@baishancloud.com>
(Merged from https://github.com/openssl/openssl/pull/8457)
This commit is contained in:
Matt Caswell 2019-03-12 10:21:39 +00:00
parent 8267becb8b
commit 81cd023f1d

View File

@ -17,7 +17,7 @@
static char *find_friendly_name(PKCS12 *p12) static char *find_friendly_name(PKCS12 *p12)
{ {
STACK_OF(PKCS7) *safes = PKCS12_unpack_authsafes(p12); STACK_OF(PKCS7) *safes;
int n, m; int n, m;
char *name = NULL; char *name = NULL;
PKCS7 *safe; PKCS7 *safe;
@ -48,56 +48,64 @@ static char *find_friendly_name(PKCS12 *p12)
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
FILE *fp; FILE *fp;
EVP_PKEY *pkey; EVP_PKEY *pkey = NULL;
X509 *cert; X509 *cert = NULL;
STACK_OF(X509) *ca = NULL; STACK_OF(X509) *ca = NULL;
PKCS12 *p12; PKCS12 *p12 = NULL;
const char *name; char *name = NULL;
int i; int i, ret = EXIT_FAILURE;
if (argc != 4) { if (argc != 4) {
fprintf(stderr, "Usage: pkread p12file password opfile\n"); fprintf(stderr, "Usage: pkread p12file password opfile\n");
exit(1); exit(EXIT_FAILURE);
} }
OpenSSL_add_all_algorithms();
ERR_load_crypto_strings();
if ((fp = fopen(argv[1], "rb")) == NULL) { if ((fp = fopen(argv[1], "rb")) == NULL) {
fprintf(stderr, "Error opening file %s\n", argv[1]); fprintf(stderr, "Error opening file %s\n", argv[1]);
exit(1); exit(EXIT_FAILURE);
} }
p12 = d2i_PKCS12_fp(fp, NULL); p12 = d2i_PKCS12_fp(fp, NULL);
fclose(fp); fclose(fp);
if (!p12) { if (p12 == NULL) {
fprintf(stderr, "Error reading PKCS#12 file\n"); fprintf(stderr, "Error reading PKCS#12 file\n");
ERR_print_errors_fp(stderr); ERR_print_errors_fp(stderr);
exit(1); goto err;
} }
if (!PKCS12_parse(p12, argv[2], &pkey, &cert, &ca)) { if (!PKCS12_parse(p12, argv[2], &pkey, &cert, &ca)) {
fprintf(stderr, "Error parsing PKCS#12 file\n"); fprintf(stderr, "Error parsing PKCS#12 file\n");
ERR_print_errors_fp(stderr); ERR_print_errors_fp(stderr);
exit(1); goto err;
} }
name = find_friendly_name(p12); name = find_friendly_name(p12);
PKCS12_free(p12); PKCS12_free(p12);
if ((fp = fopen(argv[3], "w")) == NULL) { if ((fp = fopen(argv[3], "w")) == NULL) {
fprintf(stderr, "Error opening file %s\n", argv[1]); fprintf(stderr, "Error opening file %s\n", argv[1]);
exit(1); goto err;
} }
if (name) if (name != NULL)
fprintf(fp, "***Friendly Name***\n%s\n", name); fprintf(fp, "***Friendly Name***\n%s\n", name);
if (pkey) { if (pkey != NULL) {
fprintf(fp, "***Private Key***\n"); fprintf(fp, "***Private Key***\n");
PEM_write_PrivateKey(fp, pkey, NULL, NULL, 0, NULL, NULL); PEM_write_PrivateKey(fp, pkey, NULL, NULL, 0, NULL, NULL);
} }
if (cert) { if (cert != NULL) {
fprintf(fp, "***User Certificate***\n"); fprintf(fp, "***User Certificate***\n");
PEM_write_X509_AUX(fp, cert); PEM_write_X509_AUX(fp, cert);
} }
if (ca && sk_X509_num(ca)) { if (ca != NULL && sk_X509_num(ca) > 0) {
fprintf(fp, "***Other Certificates***\n"); fprintf(fp, "***Other Certificates***\n");
for (i = 0; i < sk_X509_num(ca); i++) for (i = 0; i < sk_X509_num(ca); i++)
PEM_write_X509_AUX(fp, sk_X509_value(ca, i)); PEM_write_X509_AUX(fp, sk_X509_value(ca, i));
} }
fclose(fp); fclose(fp);
return 0;
ret = EXIT_SUCCESS;
err:
OPENSSL_free(name);
X509_free(cert);
EVP_PKEY_free(pkey);
sk_X509_pop_free(ca, X509_free);
return ret;
} }