New function X509_signature_print() to remove some duplicate
[openssl.git] / crypto / asn1 / t_x509.c
index f2979bf5a75a8013a728fc01d53aadc9981e72cc..beba47c6651f46b90c1646ce06724691e7649f53 100644 (file)
@@ -102,7 +102,7 @@ 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 = ' ';
+       char *m=NULL,mlch = ' ';
        int nmindent = 0;
        X509_CINF *ci;
        ASN1_INTEGER *bs;
@@ -256,20 +256,7 @@ int X509_print_ex(BIO *bp, X509 *x, unsigned long nmflags, unsigned long cflag)
 
        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))
                {
@@ -282,6 +269,76 @@ err:
        return(ret);
        }
 
+int X509_ocspid_print (BIO *bp, X509 *x)
+       {
+       unsigned char *der=NULL ;
+       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
+          in OCSP requests */
+       if (BIO_printf(bp,"        Subject OCSP hash: ") <= 0)
+               goto err;
+       derlen = i2d_X509_NAME(x->cert_info->subject, NULL);
+       if ((der = dertmp = (unsigned char *)OPENSSL_malloc (derlen)) == NULL)
+               goto err;
+       i2d_X509_NAME(x->cert_info->subject, &dertmp);
+
+       SHA1_Init(&SHA1buf);
+       SHA1_Update(&SHA1buf, der, derlen);
+       SHA1_Final(SHA1md,&SHA1buf);
+       for (i=0; i < SHA_DIGEST_LENGTH; i++)
+               {
+               if (BIO_printf(bp,"%02X",SHA1md[i]) <= 0) goto err;
+               }
+       OPENSSL_free (der);
+       der=NULL;
+
+       /* display the hash of the public key as it would appear
+          in OCSP requests */
+       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);
+       for (i=0; i < SHA_DIGEST_LENGTH; i++)
+               {
+               if (BIO_printf(bp,"%02X",SHA1md[i]) <= 0)
+                       goto err;
+               }
+       BIO_printf(bp,"\n");
+
+       return (1);
+err:
+       if (der != NULL) OPENSSL_free(der);
+       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;