free() -> OPENSSL_free()
[openssl.git] / crypto / asn1 / t_x509.c
index 823c20860ce96280a2f590a9141e24494f9d5660..36cef4605db72e938c6ca5673339c40feec1cfc4 100644 (file)
 #include "cryptlib.h"
 #include <openssl/buffer.h>
 #include <openssl/bn.h>
-#ifndef NO_RSA
+#ifndef OPENSSL_NO_RSA
 #include <openssl/rsa.h>
 #endif
-#ifndef NO_DSA
+#ifndef OPENSSL_NO_DSA
 #include <openssl/dsa.h>
 #endif
+#ifndef OPENSSL_NO_EC
+#include <openssl/ec.h>
+#endif
 #include <openssl/objects.h>
 #include <openssl/x509.h>
 #include <openssl/x509v3.h>
 
-#ifndef NO_FP_API
+#ifndef OPENSSL_NO_FP_API
 int X509_print_fp(FILE *fp, X509 *x)
        {
        return X509_print_ex_fp(fp, x, XN_FLAG_COMPAT, X509_FLAG_COMPAT);
@@ -101,19 +104,18 @@ int X509_print(BIO *bp, X509 *x)
 int X509_print_ex(BIO *bp, X509 *x, unsigned long nmflags, unsigned long cflag)
        {
        long l;
-       int ret=0,i,j,n;
-       char *m=NULL,*s, mlch = ' ';
+       int ret=0,i;
+       char *m=NULL,mlch = ' ';
        int nmindent = 0;
        X509_CINF *ci;
        ASN1_INTEGER *bs;
        EVP_PKEY *pkey=NULL;
        const char *neg;
-       X509_EXTENSION *ex;
        ASN1_STRING *str=NULL;
 
        if((nmflags & XN_FLAG_SEP_MASK) == XN_FLAG_SEP_MULTILINE) {
                        mlch = '\n';
-                       nmindent = 16;
+                       nmindent = 12;
        }
 
        if(nmflags == X509_FLAG_COMPAT)
@@ -166,20 +168,23 @@ int X509_print_ex(BIO *bp, X509 *x, unsigned long nmflags, unsigned long cflag)
 
        if(!(cflag & X509_FLAG_NO_SIGNAME))
                {
-               i=OBJ_obj2nid(ci->signature->algorithm);
-               if (BIO_printf(bp,"%8sSignature Algorithm: %s\n","",
-                       (i == NID_undef)?"UNKNOWN":OBJ_nid2ln(i)) <= 0)
+               if (BIO_printf(bp,"%8sSignature Algorithm: ","") <= 0) 
+                       goto err;
+               if (i2a_ASN1_OBJECT(bp, ci->signature->algorithm) <= 0)
+                       goto err;
+               if (BIO_puts(bp, "\n") <= 0)
                        goto err;
                }
 
        if(!(cflag & X509_FLAG_NO_ISSUER))
                {
                if (BIO_printf(bp,"        Issuer:%c",mlch) <= 0) goto err;
-               if (!X509_NAME_print_ex(bp,X509_get_issuer_name(x),nmindent, nmflags)) goto err;
+               if (X509_NAME_print_ex(bp,X509_get_issuer_name(x),nmindent, nmflags) < 0) goto err;
+               if (BIO_write(bp,"\n",1) <= 0) goto err;
                }
        if(!(cflag & X509_FLAG_NO_VALIDITY))
                {
-               if (BIO_write(bp,"\n        Validity\n",18) <= 0) goto err;
+               if (BIO_write(bp,"        Validity\n",17) <= 0) goto err;
                if (BIO_write(bp,"            Not Before: ",24) <= 0) goto err;
                if (!ASN1_TIME_print(bp,X509_get_notBefore(x))) goto err;
                if (BIO_write(bp,"\n            Not After : ",25) <= 0) goto err;
@@ -189,15 +194,19 @@ int X509_print_ex(BIO *bp, X509 *x, unsigned long nmflags, unsigned long cflag)
        if(!(cflag & X509_FLAG_NO_SUBJECT))
                {
                if (BIO_printf(bp,"        Subject:%c",mlch) <= 0) goto err;
-               if (!X509_NAME_print_ex(bp,X509_get_subject_name(x),nmindent, nmflags)) goto err;
+               if (X509_NAME_print_ex(bp,X509_get_subject_name(x),nmindent, nmflags) < 0) goto err;
+               if (BIO_write(bp,"\n",1) <= 0) goto err;
                }
        if(!(cflag & X509_FLAG_NO_PUBKEY))
                {
-               if (BIO_write(bp,"\n        Subject Public Key Info:\n",34) <= 0)
+               if (BIO_write(bp,"        Subject Public Key Info:\n",33) <= 0)
+                       goto err;
+               if (BIO_printf(bp,"%12sPublic Key Algorithm: ","") <= 0)
+                       goto err;
+               if (i2a_ASN1_OBJECT(bp, ci->key->algor->algorithm) <= 0)
+                       goto err;
+               if (BIO_puts(bp, "\n") <= 0)
                        goto err;
-               i=OBJ_obj2nid(ci->key->algor->algorithm);
-               if (BIO_printf(bp,"%12sPublic Key Algorithm: %s\n","",
-                       (i == NID_undef)?"UNKNOWN":OBJ_nid2ln(i)) <= 0) goto err;
 
                pkey=X509_get_pubkey(x);
                if (pkey == NULL)
@@ -206,7 +215,7 @@ int X509_print_ex(BIO *bp, X509 *x, unsigned long nmflags, unsigned long cflag)
                        ERR_print_errors(bp);
                        }
                else
-#ifndef NO_RSA
+#ifndef OPENSSL_NO_RSA
                if (pkey->type == EVP_PKEY_RSA)
                        {
                        BIO_printf(bp,"%12sRSA Public Key: (%d bit)\n","",
@@ -215,61 +224,34 @@ int X509_print_ex(BIO *bp, X509 *x, unsigned long nmflags, unsigned long cflag)
                        }
                else
 #endif
-#ifndef NO_DSA
+#ifndef OPENSSL_NO_DSA
                if (pkey->type == EVP_PKEY_DSA)
                        {
                        BIO_printf(bp,"%12sDSA Public Key:\n","");
                        DSA_print(bp,pkey->pkey.dsa,16);
                        }
                else
+#endif
+#ifndef OPENSSL_NO_EC
+               if (pkey->type == EVP_PKEY_EC)
+                       {
+                       BIO_printf(bp, "%12sEC Public Key:\n","");
+                       EC_KEY_print(bp, pkey->pkey.eckey, 16);
+                       }
+               else
 #endif
                        BIO_printf(bp,"%12sUnknown Public Key:\n","");
 
                EVP_PKEY_free(pkey);
                }
 
-       if (cflag & X509_FLAG_NO_EXTENSIONS)
-               n = 0;
-       else
-               n=X509_get_ext_count(x);
-       if (n > 0)
-               {
-               BIO_printf(bp,"%8sX509v3 extensions:\n","");
-               for (i=0; i<n; i++)
-                       {
-                       ASN1_OBJECT *obj;
-                       ex=X509_get_ext(x,i);
-                       if (BIO_printf(bp,"%12s","") <= 0) goto err;
-                       obj=X509_EXTENSION_get_object(ex);
-                       i2a_ASN1_OBJECT(bp,obj);
-                       j=X509_EXTENSION_get_critical(ex);
-                       if (BIO_printf(bp,": %s\n",j?"critical":"","") <= 0)
-                               goto err;
-                       if(!X509V3_EXT_print(bp, ex, cflag, 16))
-                               {
-                               BIO_printf(bp, "%16s", "");
-                               M_ASN1_OCTET_STRING_print(bp,ex->value);
-                               }
-                       if (BIO_write(bp,"\n",1) <= 0) goto err;
-                       }
-               }
+       if (!(cflag & X509_FLAG_NO_EXTENSIONS))
+               X509V3_extensions_print(bp, "X509v3 extensions",
+                                       ci->extensions, cflag, 8);
 
        if(!(cflag & X509_FLAG_NO_SIGDUMP))
                {
-               i=OBJ_obj2nid(x->sig_alg->algorithm);
-               if (BIO_printf(bp,"%4sSignature Algorithm: %s","",
-                       (i == NID_undef)?"UNKNOWN":OBJ_nid2ln(i)) <= 0) goto err;
-
-               n=x->signature->length;
-               s=(char *)x->signature->data;
-               for (i=0; i<n; i++)
-                       {
-                       if ((i%18) == 0)
-                               if (BIO_write(bp,"\n        ",9) <= 0) goto err;
-                       if (BIO_printf(bp,"%02x%s",(unsigned char)s[i],
-                               ((i+1) == n)?"":":") <= 0) goto err;
-                       }
-               if (BIO_write(bp,"\n",1) != 1) goto err;
+               if(X509_signature_print(bp, x->sig_alg, x->signature) <= 0) goto err;
                }
        if(!(cflag & X509_FLAG_NO_AUX))
                {
@@ -288,7 +270,6 @@ int X509_ocspid_print (BIO *bp, X509 *x)
        unsigned char *dertmp;
        int derlen;
        int i;
-       SHA_CTX SHA1buf ;
        unsigned char SHA1md[SHA_DIGEST_LENGTH];
 
        /* display the hash of the subject as it would appear
@@ -300,9 +281,7 @@ int X509_ocspid_print (BIO *bp, X509 *x)
                goto err;
        i2d_X509_NAME(x->cert_info->subject, &dertmp);
 
-       SHA1_Init(&SHA1buf);
-       SHA1_Update(&SHA1buf, der, derlen);
-       SHA1_Final(SHA1md,&SHA1buf);
+       EVP_Digest(der, derlen, SHA1md, NULL, EVP_sha1(), NULL);
        for (i=0; i < SHA_DIGEST_LENGTH; i++)
                {
                if (BIO_printf(bp,"%02X",SHA1md[i]) <= 0) goto err;
@@ -315,10 +294,8 @@ int X509_ocspid_print (BIO *bp, X509 *x)
        if (BIO_printf(bp,"\n        Public key OCSP hash: ") <= 0)
                goto err;
 
-       SHA1_Init(&SHA1buf);
-       SHA1_Update(&SHA1buf, x->cert_info->key->public_key->data,
-               x->cert_info->key->public_key->length);
-       SHA1_Final(SHA1md,&SHA1buf);
+       EVP_Digest(x->cert_info->key->public_key->data,
+               x->cert_info->key->public_key->length, SHA1md, NULL, EVP_sha1(), NULL);
        for (i=0; i < SHA_DIGEST_LENGTH; i++)
                {
                if (BIO_printf(bp,"%02X",SHA1md[i]) <= 0)
@@ -332,6 +309,26 @@ err:
        return(0);
        }
 
+int X509_signature_print(BIO *bp, X509_ALGOR *sigalg, ASN1_STRING *sig)
+{
+       unsigned char *s;
+       int i, n;
+       if (BIO_puts(bp,"    Signature Algorithm: ") <= 0) return 0;
+       if (i2a_ASN1_OBJECT(bp, sigalg->algorithm) <= 0) return 0;
+
+       n=sig->length;
+       s=sig->data;
+       for (i=0; i<n; i++)
+               {
+               if ((i%18) == 0)
+                       if (BIO_write(bp,"\n        ",9) <= 0) return 0;
+                       if (BIO_printf(bp,"%02x%s",s[i],
+                               ((i+1) == n)?"":":") <= 0) return 0;
+               }
+       if (BIO_write(bp,"\n",1) != 1) return 0;
+       return 1;
+}
+
 int ASN1_STRING_print(BIO *bp, ASN1_STRING *v)
        {
        int i,n;
@@ -447,13 +444,17 @@ err:
 
 int X509_NAME_print(BIO *bp, X509_NAME *name, int obase)
        {
-       char *s,*c;
+       char *s,*c,*b;
        int ret=0,l,ll,i,first=1;
-       char buf[256];
 
        ll=80-2-obase;
 
-       s=X509_NAME_oneline(name,buf,256);
+       b=s=X509_NAME_oneline(name,NULL,0);
+       if (!*s)
+               {
+               OPENSSL_free(b);
+               return 1;
+               }
        s++; /* skip the first slash */
 
        l=ll;
@@ -509,6 +510,7 @@ int X509_NAME_print(BIO *bp, X509_NAME *name, int obase)
 err:
                X509err(X509_F_X509_NAME_PRINT,ERR_R_BUF_LIB);
                }
+       OPENSSL_free(b);
        return(ret);
        }