print out issuer and subject unique identifier fields in certificates
[openssl.git] / crypto / asn1 / t_x509.c
index 01cf9e427a14233929aeb0c8a1488f873344a4ab..8eb0b79a9126a6f915d4c30f53a4b0a10dcabff7 100644 (file)
@@ -72,6 +72,7 @@
 #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)
@@ -137,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="-";
@@ -167,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))
@@ -220,6 +225,24 @@ int X509_print_ex(BIO *bp, X509 *x, unsigned long nmflags, unsigned long cflag)
                        }
                }
 
+       if(!(cflag & X509_FLAG_NO_IDS))
+               {
+               if (ci->issuerUID)
+                       {
+                       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;
+                       }
+               }
+
        if (!(cflag & X509_FLAG_NO_EXTENSIONS))
                X509V3_extensions_print(bp, "X509v3 extensions",
                                        ci->extensions, cflag, 8);
@@ -286,23 +309,50 @@ 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;
 }