Uhmmm, if we use && after having tested for the presence of the certificate,
[openssl.git] / crypto / pem / pem_info.c
index b65239a9200fce87cbb95ea8bc427346f1c5a63e..27bcc25177539821d12c1616da341e269a0a56e6 100644 (file)
@@ -64,7 +64,7 @@
 #include <openssl/x509.h>
 #include <openssl/pem.h>
 
-#ifndef NO_FP_API
+#ifndef OPENSSL_NO_FP_API
 STACK_OF(X509_INFO) *PEM_X509_INFO_read(FILE *fp, STACK_OF(X509_INFO) *sk, pem_password_cb *cb, void *u)
        {
         BIO *b;
@@ -111,7 +111,7 @@ STACK_OF(X509_INFO) *PEM_X509_INFO_read_bio(BIO *bp, STACK_OF(X509_INFO) *sk, pe
                i=PEM_read_bio(bp,&name,&header,&data,&len);
                if (i == 0)
                        {
-                       error=ERR_GET_REASON(ERR_peek_error());
+                       error=ERR_GET_REASON(ERR_peek_last_error());
                        if (error == PEM_R_NO_START_LINE)
                                {
                                ERR_clear_error();
@@ -155,7 +155,7 @@ start:
                        pp=(char **)&(xi->crl);
                        }
                else
-#ifndef NO_RSA
+#ifndef OPENSSL_NO_RSA
                        if (strcmp(name,PEM_STRING_RSA) == 0)
                        {
                        d2i=(char *(*)())d2i_RSAPrivateKey;
@@ -179,7 +179,7 @@ start:
                        }
                else
 #endif
-#ifndef NO_DSA
+#ifndef OPENSSL_NO_DSA
                        if (strcmp(name,PEM_STRING_DSA) == 0)
                        {
                        d2i=(char *(*)())d2i_DSAPrivateKey;
@@ -202,6 +202,30 @@ start:
                                raw=1;
                        }
                else
+#endif
+#ifndef OPENSSL_NO_ECDSA
+                       if (strcmp(name,PEM_STRING_ECDSA) == 0)
+                       {
+                               d2i=(char *(*)())d2i_ECDSAPrivateKey;
+                               if (xi->x_pkey != NULL) 
+                               {
+                                       if (!sk_X509_INFO_push(ret,xi)) goto err;
+                                       if ((xi=X509_INFO_new()) == NULL) goto err;
+                                               goto start;
+                               }
+                       xi->enc_data=NULL;
+                       xi->enc_len=0;
+                       xi->x_pkey=X509_PKEY_new();
+                       if ((xi->x_pkey->dec_pkey=EVP_PKEY_new()) == NULL)
+                               goto err;
+                       xi->x_pkey->dec_pkey->type=EVP_PKEY_ECDSA;
+                       pp=(char **)&(xi->x_pkey->dec_pkey->pkey.ecdsa);
+                       if ((int)strlen(header) > 10) /* assume encrypted */
+                               raw=1;
+                       }
+               else
 #endif
                        {
                        d2i=NULL;
@@ -237,9 +261,9 @@ start:
                else    {
                        /* unknown */
                        }
-               if (name != NULL) Free(name);
-               if (header != NULL) Free(header);
-               if (data != NULL) Free(data);
+               if (name != NULL) OPENSSL_free(name);
+               if (header != NULL) OPENSSL_free(header);
+               if (data != NULL) OPENSSL_free(data);
                name=NULL;
                header=NULL;
                data=NULL;
@@ -268,9 +292,9 @@ err:
                ret=NULL;
                }
                
-       if (name != NULL) Free(name);
-       if (header != NULL) Free(header);
-       if (data != NULL) Free(data);
+       if (name != NULL) OPENSSL_free(name);
+       if (header != NULL) OPENSSL_free(header);
+       if (data != NULL) OPENSSL_free(data);
        return(ret);
        }
 
@@ -305,7 +329,7 @@ int PEM_X509_INFO_write_bio(BIO *bp, X509_INFO *xi, EVP_CIPHER *enc,
                {
                if ( (xi->enc_data!=NULL) && (xi->enc_len>0) )
                        {
-                       /* copy from wierdo names into more normal things */
+                       /* copy from weirdo names into more normal things */
                        iv=xi->enc_cipher.iv;
                        data=(unsigned char *)xi->enc_data;
                        i=xi->enc_len;
@@ -326,7 +350,7 @@ int PEM_X509_INFO_write_bio(BIO *bp, X509_INFO *xi, EVP_CIPHER *enc,
                        /* create the right magic header stuff */
                        buf[0]='\0';
                        PEM_proc_type(buf,PEM_TYPE_ENCRYPTED);
-                       PEM_dek_info(buf,objstr,8,(char *)iv);
+                       PEM_dek_info(buf,objstr,enc->iv_len,(char *)iv);
 
                        /* use the normal code to write things out */
                        i=PEM_write_bio(bp,PEM_STRING_RSA,buf,data,i);
@@ -335,7 +359,7 @@ int PEM_X509_INFO_write_bio(BIO *bp, X509_INFO *xi, EVP_CIPHER *enc,
                else
                        {
                        /* Add DSA/DH */
-#ifndef NO_RSA
+#ifndef OPENSSL_NO_RSA
                        /* normal optionally encrypted stuff */
                        if (PEM_write_bio_RSAPrivateKey(bp,
                                xi->x_pkey->dec_pkey->pkey.rsa,
@@ -346,7 +370,7 @@ int PEM_X509_INFO_write_bio(BIO *bp, X509_INFO *xi, EVP_CIPHER *enc,
                }
 
        /* if we have a certificate then write it out now */
-       if ((xi->x509 != NULL) || (PEM_write_bio_X509(bp,xi->x509) <= 0))
+       if ((xi->x509 != NULL) && (PEM_write_bio_X509(bp,xi->x509) <= 0))
                goto err;
 
        /* we are ignoring anything else that is loaded into the X509_INFO