- STACK_OF(X509) *ocerts = NULL;
- X509 *x = NULL;
- /* Check for NULL PKCS12 structure */
-
- if(!p12)
- {
- PKCS12err(PKCS12_F_PKCS12_PARSE,PKCS12_R_INVALID_NULL_PKCS12_POINTER);
- return 0;
- }
-
- if(pkey)
- *pkey = NULL;
- if(cert)
- *cert = NULL;
-
- /* Check the mac */
-
- /* If password is zero length or NULL then try verifying both cases
- * to determine which password is correct. The reason for this is that
- * under PKCS#12 password based encryption no password and a zero length
- * password are two different things...
- */
-
- if(!pass || !*pass) {
- if(PKCS12_verify_mac(p12, NULL, 0)) pass = NULL;
- else if(PKCS12_verify_mac(p12, "", 0)) pass = "";
- else {
- PKCS12err(PKCS12_F_PKCS12_PARSE,PKCS12_R_MAC_VERIFY_FAILURE);
- goto err;
- }
- } else if (!PKCS12_verify_mac(p12, pass, -1)) {
- PKCS12err(PKCS12_F_PKCS12_PARSE,PKCS12_R_MAC_VERIFY_FAILURE);
- goto err;
- }
-
- /* Allocate stack for other certificates */
- ocerts = sk_X509_new_null();
-
- if (!ocerts)
- {
- PKCS12err(PKCS12_F_PKCS12_PARSE,ERR_R_MALLOC_FAILURE);
- return 0;
- }
-
- if (!parse_pk12 (p12, pass, -1, pkey, ocerts))
- {
- PKCS12err(PKCS12_F_PKCS12_PARSE,PKCS12_R_PARSE_ERROR);
- goto err;
- }
-
- while ((x = sk_X509_pop(ocerts)))
- {
- if (pkey && *pkey && cert && !*cert)
- {
- if (X509_check_private_key(x, *pkey))
- {
- *cert = x;
- x = NULL;
- }
- }
-
- if (ca && x)
- {
- if (!*ca)
- *ca = sk_X509_new_null();
- if (!*ca)
- goto err;
- if (!sk_X509_push(*ca, x))
- goto err;
- x = NULL;
- }
- if (x)
- X509_free(x);
- }
-
- if (ocerts)
- sk_X509_pop_free(ocerts, X509_free);
-
- return 1;
+ STACK_OF(X509) *ocerts = NULL;
+ X509 *x = NULL;
+ /* Check for NULL PKCS12 structure */
+
+ if (!p12) {
+ PKCS12err(PKCS12_F_PKCS12_PARSE,
+ PKCS12_R_INVALID_NULL_PKCS12_POINTER);
+ return 0;
+ }
+
+ if (pkey)
+ *pkey = NULL;
+ if (cert)
+ *cert = NULL;
+
+ /* Check the mac */
+
+ /*
+ * If password is zero length or NULL then try verifying both cases to
+ * determine which password is correct. The reason for this is that under
+ * PKCS#12 password based encryption no password and a zero length
+ * password are two different things...
+ */
+
+ if (!pass || !*pass) {
+ if (PKCS12_verify_mac(p12, NULL, 0))
+ pass = NULL;
+ else if (PKCS12_verify_mac(p12, "", 0))
+ pass = "";
+ else {
+ PKCS12err(PKCS12_F_PKCS12_PARSE, PKCS12_R_MAC_VERIFY_FAILURE);
+ goto err;
+ }
+ } else if (!PKCS12_verify_mac(p12, pass, -1)) {
+ PKCS12err(PKCS12_F_PKCS12_PARSE, PKCS12_R_MAC_VERIFY_FAILURE);
+ goto err;
+ }
+
+ /* Allocate stack for other certificates */
+ ocerts = sk_X509_new_null();
+
+ if (!ocerts) {
+ PKCS12err(PKCS12_F_PKCS12_PARSE, ERR_R_MALLOC_FAILURE);
+ return 0;
+ }
+
+ if (!parse_pk12(p12, pass, -1, pkey, ocerts)) {
+ PKCS12err(PKCS12_F_PKCS12_PARSE, PKCS12_R_PARSE_ERROR);
+ goto err;
+ }
+
+ while ((x = sk_X509_pop(ocerts))) {
+ if (pkey && *pkey && cert && !*cert) {
+ if (X509_check_private_key(x, *pkey)) {
+ *cert = x;
+ x = NULL;
+ }
+ }
+
+ if (ca && x) {
+ if (!*ca)
+ *ca = sk_X509_new_null();
+ if (!*ca)
+ goto err;
+ if (!sk_X509_push(*ca, x))
+ goto err;
+ x = NULL;
+ }
+ if (x)
+ X509_free(x);
+ }
+
+ if (ocerts)
+ sk_X509_pop_free(ocerts, X509_free);
+
+ return 1;