print out issuer and subject unique identifier fields in certificates
[openssl.git] / crypto / asn1 / t_x509.c
index 454c695eb2aa43d62fb07880b730fc067c75df59..8eb0b79a9126a6f915d4c30f53a4b0a10dcabff7 100644 (file)
 #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>
+#include "asn1_locl.h"
 
 #ifndef OPENSSL_NO_FP_API
 int X509_print_fp(FILE *fp, X509 *x)
@@ -83,7 +87,7 @@ int X509_print_ex_fp(FILE *fp, X509 *x, unsigned long nmflag, unsigned long cfla
 
         if ((b=BIO_new(BIO_s_file())) == NULL)
                {
-               X509err(X509_F_X509_PRINT_FP,ERR_R_BUF_LIB);
+               X509err(X509_F_X509_PRINT_EX_FP,ERR_R_BUF_LIB);
                 return(0);
                }
         BIO_set_fp(b,fp,BIO_NOCLOSE);
@@ -108,7 +112,6 @@ int X509_print_ex(BIO *bp, X509 *x, unsigned long nmflags, unsigned long cflag)
        ASN1_INTEGER *bs;
        EVP_PKEY *pkey=NULL;
        const char *neg;
-       ASN1_STRING *str=NULL;
 
        if((nmflags & XN_FLAG_SEP_MASK) == XN_FLAG_SEP_MULTILINE) {
                        mlch = '\n';
@@ -135,10 +138,10 @@ int X509_print_ex(BIO *bp, X509 *x, unsigned long nmflags, unsigned long cflag)
                if (BIO_write(bp,"        Serial Number:",22) <= 0) goto err;
 
                bs=X509_get_serialNumber(x);
-               if (bs->length <= 4)
+               if (bs->length <= (int)sizeof(long))
                        {
                        l=ASN1_INTEGER_get(bs);
-                       if (l < 0)
+                       if (bs->type == V_ASN1_NEG_INTEGER)
                                {
                                l= -l;
                                neg="-";
@@ -165,12 +168,16 @@ int X509_print_ex(BIO *bp, X509 *x, unsigned long nmflags, unsigned long cflag)
 
        if(!(cflag & X509_FLAG_NO_SIGNAME))
                {
+               if(X509_signature_print(bp, x->sig_alg, NULL) <= 0)
+                       goto err;
+#if 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;
+#endif
                }
 
        if(!(cflag & X509_FLAG_NO_ISSUER))
@@ -212,26 +219,28 @@ int X509_print_ex(BIO *bp, X509 *x, unsigned long nmflags, unsigned long cflag)
                        ERR_print_errors(bp);
                        }
                else
-#ifndef OPENSSL_NO_RSA
-               if (pkey->type == EVP_PKEY_RSA)
                        {
-                       BIO_printf(bp,"%12sRSA Public Key: (%d bit)\n","",
-                       BN_num_bits(pkey->pkey.rsa->n));
-                       RSA_print(bp,pkey->pkey.rsa,16);
+                       EVP_PKEY_print_public(bp, pkey, 16, NULL);
+                       EVP_PKEY_free(pkey);
                        }
-               else
-#endif
-#ifndef OPENSSL_NO_DSA
-               if (pkey->type == EVP_PKEY_DSA)
+               }
+
+       if(!(cflag & X509_FLAG_NO_IDS))
+               {
+               if (ci->issuerUID)
                        {
-                       BIO_printf(bp,"%12sDSA Public Key:\n","");
-                       DSA_print(bp,pkey->pkey.dsa,16);
+                       if (BIO_printf(bp,"%8sIssuer Unique ID: ","") <= 0) 
+                               goto err;
+                       if (!X509_signature_dump(bp, ci->issuerUID, 12))
+                               goto err;
+                       }
+               if (ci->subjectUID)
+                       {
+                       if (BIO_printf(bp,"%8sSubject Unique ID: ","") <= 0) 
+                               goto err;
+                       if (!X509_signature_dump(bp, ci->subjectUID, 12))
+                               goto err;
                        }
-               else
-#endif
-                       BIO_printf(bp,"%12sUnknown Public Key:\n","");
-
-               EVP_PKEY_free(pkey);
                }
 
        if (!(cflag & X509_FLAG_NO_EXTENSIONS))
@@ -248,7 +257,6 @@ int X509_print_ex(BIO *bp, X509 *x, unsigned long nmflags, unsigned long cflag)
                }
        ret=1;
 err:
-       if (str != NULL) ASN1_STRING_free(str);
        if (m != NULL) OPENSSL_free(m);
        return(ret);
        }
@@ -270,7 +278,8 @@ int X509_ocspid_print (BIO *bp, X509 *x)
                goto err;
        i2d_X509_NAME(x->cert_info->subject, &dertmp);
 
-       EVP_Digest(der, derlen, SHA1md, NULL, EVP_sha1());
+       if (!EVP_Digest(der, derlen, SHA1md, NULL, EVP_sha1(), NULL))
+               goto err;
        for (i=0; i < SHA_DIGEST_LENGTH; i++)
                {
                if (BIO_printf(bp,"%02X",SHA1md[i]) <= 0) goto err;
@@ -283,8 +292,10 @@ int X509_ocspid_print (BIO *bp, X509 *x)
        if (BIO_printf(bp,"\n        Public key OCSP hash: ") <= 0)
                goto err;
 
-       EVP_Digest(x->cert_info->key->public_key->data,
-               x->cert_info->key->public_key->length, SHA1md, NULL, EVP_sha1());
+       if (!EVP_Digest(x->cert_info->key->public_key->data,
+                       x->cert_info->key->public_key->length,
+                       SHA1md, NULL, EVP_sha1(), NULL))
+               goto err;
        for (i=0; i < SHA_DIGEST_LENGTH; i++)
                {
                if (BIO_printf(bp,"%02X",SHA1md[i]) <= 0)
@@ -298,34 +309,62 @@ err:
        return(0);
        }
 
-int X509_signature_print(BIO *bp, X509_ALGOR *sigalg, ASN1_STRING *sig)
+int X509_signature_dump(BIO *bp, const ASN1_STRING *sig, int indent)
 {
-       unsigned char *s;
+       const 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_write(bp,"\n",1) <= 0) return 0;
+                       if (BIO_indent(bp, indent, indent) <= 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 X509_signature_print(BIO *bp, X509_ALGOR *sigalg, ASN1_STRING *sig)
+{
+       int sig_nid;
+       if (BIO_puts(bp,"    Signature Algorithm: ") <= 0) return 0;
+       if (i2a_ASN1_OBJECT(bp, sigalg->algorithm) <= 0) return 0;
+
+       sig_nid = OBJ_obj2nid(sigalg->algorithm);
+       if (sig_nid != NID_undef)
+               {
+               int pkey_nid, dig_nid;
+               const EVP_PKEY_ASN1_METHOD *ameth;
+               if (OBJ_find_sigid_algs(sig_nid, &dig_nid, &pkey_nid))
+                       {
+                       ameth = EVP_PKEY_asn1_find(NULL, pkey_nid);
+                       if (ameth && ameth->sig_print)
+                               return ameth->sig_print(bp, sigalg, sig, 9, 0);
+                       }
+               }
+       if (sig)
+               return X509_signature_dump(bp, sig, 9);
+       else if (BIO_puts(bp, "\n") <= 0)
+               return 0;
        return 1;
 }
 
-int ASN1_STRING_print(BIO *bp, ASN1_STRING *v)
+int ASN1_STRING_print(BIO *bp, const ASN1_STRING *v)
        {
        int i,n;
-       char buf[80],*p;;
+       char buf[80];
+       const char *p;
 
        if (v == NULL) return(0);
        n=0;
-       p=(char *)v->data;
+       p=(const char *)v->data;
        for (i=0; i<v->length; i++)
                {
                if ((p[i] > '~') || ((p[i] < ' ') &&
@@ -347,7 +386,7 @@ int ASN1_STRING_print(BIO *bp, ASN1_STRING *v)
        return(1);
        }
 
-int ASN1_TIME_print(BIO *bp, ASN1_TIME *tm)
+int ASN1_TIME_print(BIO *bp, const ASN1_TIME *tm)
 {
        if(tm->type == V_ASN1_UTCTIME) return ASN1_UTCTIME_print(bp, tm);
        if(tm->type == V_ASN1_GENERALIZEDTIME)
@@ -362,12 +401,14 @@ static const char *mon[12]=
     "Jul","Aug","Sep","Oct","Nov","Dec"
     };
 
-int ASN1_GENERALIZEDTIME_print(BIO *bp, ASN1_GENERALIZEDTIME *tm)
+int ASN1_GENERALIZEDTIME_print(BIO *bp, const ASN1_GENERALIZEDTIME *tm)
        {
        char *v;
        int gmt=0;
        int i;
        int y=0,M=0,d=0,h=0,m=0,s=0;
+       char *f = NULL;
+       int f_len = 0;
 
        i=tm->length;
        v=(char *)tm->data;
@@ -382,12 +423,24 @@ int ASN1_GENERALIZEDTIME_print(BIO *bp, ASN1_GENERALIZEDTIME *tm)
        d= (v[6]-'0')*10+(v[7]-'0');
        h= (v[8]-'0')*10+(v[9]-'0');
        m=  (v[10]-'0')*10+(v[11]-'0');
-       if (    (v[12] >= '0') && (v[12] <= '9') &&
-               (v[13] >= '0') && (v[13] <= '9'))
+       if (tm->length >= 14 &&
+           (v[12] >= '0') && (v[12] <= '9') &&
+           (v[13] >= '0') && (v[13] <= '9'))
+               {
                s=  (v[12]-'0')*10+(v[13]-'0');
+               /* Check for fractions of seconds. */
+               if (tm->length >= 15 && v[14] == '.')
+                       {
+                       int l = tm->length;
+                       f = &v[14];     /* The decimal point. */
+                       f_len = 1;
+                       while (14 + f_len < l && f[f_len] >= '0' && f[f_len] <= '9')
+                               ++f_len;
+                       }
+               }
 
-       if (BIO_printf(bp,"%s %2d %02d:%02d:%02d %d%s",
-               mon[M-1],d,h,m,s,y,(gmt)?" GMT":"") <= 0)
+       if (BIO_printf(bp,"%s %2d %02d:%02d:%02d%.*s %d%s",
+               mon[M-1],d,h,m,s,f_len,f,y,(gmt)?" GMT":"") <= 0)
                return(0);
        else
                return(1);
@@ -396,15 +449,15 @@ err:
        return(0);
        }
 
-int ASN1_UTCTIME_print(BIO *bp, ASN1_UTCTIME *tm)
+int ASN1_UTCTIME_print(BIO *bp, const ASN1_UTCTIME *tm)
        {
-       char *v;
+       const char *v;
        int gmt=0;
        int i;
        int y=0,M=0,d=0,h=0,m=0,s=0;
 
        i=tm->length;
-       v=(char *)tm->data;
+       v=(const char *)tm->data;
 
        if (i < 10) goto err;
        if (v[i-1] == 'Z') gmt=1;
@@ -417,8 +470,9 @@ int ASN1_UTCTIME_print(BIO *bp, ASN1_UTCTIME *tm)
        d= (v[4]-'0')*10+(v[5]-'0');
        h= (v[6]-'0')*10+(v[7]-'0');
        m=  (v[8]-'0')*10+(v[9]-'0');
-       if (    (v[10] >= '0') && (v[10] <= '9') &&
-               (v[11] >= '0') && (v[11] <= '9'))
+       if (tm->length >=12 &&
+           (v[10] >= '0') && (v[10] <= '9') &&
+           (v[11] >= '0') && (v[11] <= '9'))
                s=  (v[10]-'0')*10+(v[11]-'0');
 
        if (BIO_printf(bp,"%s %2d %02d:%02d:%02d %d%s",
@@ -433,18 +487,19 @@ err:
 
 int X509_NAME_print(BIO *bp, X509_NAME *name, int obase)
        {
-       char *s,*c;
-       int ret=0,l,ll,i,first=1;
-       char buf[256];
+       char *s,*c,*b;
+       int ret=0,l,i;
 
-       ll=80-2-obase;
+       l=80-2-obase;
 
-       s=X509_NAME_oneline(name,buf,256);
-       if (!*s)
+       b=X509_NAME_oneline(name,NULL,0);
+       if (!*b)
+               {
+               OPENSSL_free(b);
                return 1;
-       s++; /* skip the first slash */
+               }
+       s=b+1; /* skip the first slash */
 
-       l=ll;
        c=s;
        for (;;)
                {
@@ -466,20 +521,9 @@ int X509_NAME_print(BIO *bp, X509_NAME *name, int obase)
                        (*s == '\0'))
 #endif
                        {
-                       if ((l <= 0) && !first)
-                               {
-                               first=0;
-                               if (BIO_write(bp,"\n",1) != 1) goto err;
-                               for (i=0; i<obase; i++)
-                                       {
-                                       if (BIO_write(bp," ",1) != 1) goto err;
-                                       }
-                               l=ll;
-                               }
                        i=s-c;
                        if (BIO_write(bp,c,i) != i) goto err;
-                       c+=i;
-                       c++;
+                       c=s+1;  /* skip following slash */
                        if (*s != '\0')
                                {
                                if (BIO_write(bp,", ",2) != 2) goto err;
@@ -497,6 +541,6 @@ 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);
        }
-