Check PKCS7 structures in PKCS#12 files are of type data.
[openssl.git] / crypto / pkcs12 / p12_init.c
index 6ec6fb4..6bdc132 100644 (file)
 
 #include <stdio.h>
 #include "cryptlib.h"
-#include "pkcs12.h"
+#include <openssl/pkcs12.h>
 
 /* Initialise a PKCS12 structure to take data */
 
-PKCS12 *PKCS12_init (mode)
-int mode;
+PKCS12 *PKCS12_init(int mode)
 {
        PKCS12 *pkcs12;
        if (!(pkcs12 = PKCS12_new())) {
                PKCS12err(PKCS12_F_PKCS12_INIT,ERR_R_MALLOC_FAILURE);
                return NULL;
        }
-       if (!(pkcs12->version = ASN1_INTEGER_new ())) {
-               PKCS12err(PKCS12_F_PKCS12_INIT,ERR_R_MALLOC_FAILURE);
-               return NULL;
-       }
-       ASN1_INTEGER_set (pkcs12->version, 3);
-       if (!(pkcs12->authsafes = PKCS7_new())) {
-               PKCS12err(PKCS12_F_PKCS12_INIT,ERR_R_MALLOC_FAILURE);
-               return NULL;
-       }
+       ASN1_INTEGER_set(pkcs12->version, 3);
        pkcs12->authsafes->type = OBJ_nid2obj(mode);
        switch (mode) {
                case NID_pkcs7_data:
                        if (!(pkcs12->authsafes->d.data =
-                                ASN1_OCTET_STRING_new())) {
+                                M_ASN1_OCTET_STRING_new())) {
                        PKCS12err(PKCS12_F_PKCS12_INIT,ERR_R_MALLOC_FAILURE);
-                       return NULL;
+                       goto err;
                }
                break;
                default:
-                       PKCS12err(PKCS12_F_PKCS12_INIT,PKCS12_R_UNSUPPORTED_PKCS12_MODE);
-                       PKCS12_free(pkcs12);
-                       return NULL;
-               break;
+                       PKCS12err(PKCS12_F_PKCS12_INIT,
+                               PKCS12_R_UNSUPPORTED_PKCS12_MODE);
+                       goto err;
        }
                
        return pkcs12;
+err:
+       if (pkcs12 != NULL) PKCS12_free(pkcs12);
+       return NULL;
 }