Author: nikou Date: Mon Mar 12 18:48:02 2012 New Revision: 2908 Log: format "pisa-cert-info.c" in Kernighan and Ritchie style. Modified: trunk/community-operator/client/pisa-cert-info.c Modified: trunk/community-operator/client/pisa-cert-info.c ============================================================================== --- trunk/community-operator/client/pisa-cert-info.c Mon Mar 12 15:48:06 2012 (r2907) +++ trunk/community-operator/client/pisa-cert-info.c Mon Mar 12 18:48:02 2012 (r2908) @@ -1,201 +1,239 @@ /** * @file * This programm will extract informations of X590 certificate - *@brief - *This programm will extract informations of X590 certificate * * @author Nikou Gholizadeh <Nikou.Gholizadeh@xxxxxxxxxxxxxx> */ + #include <inttypes.h> -#include <openssl/asn1.h> -#include <openssl/pem.h> -#include <openssl/x509.h> #include <stdbool.h> #include <stdio.h> #include <stdlib.h> #include <string.h> -#include <stdio.h> +#include <openssl/asn1.h> +#include <openssl/pem.h> +#include <openssl/x509.h> int X509_print_fp_cop(FILE *fp, X509 *x); int X509_print_cop(BIO *bp, X509 *x); -static void t1(BIO *bp) { BIO_write(bp,"<tr><td>",8); } -static void t2(BIO *bp) { BIO_write(bp,"</td><td>",9); } -static void t3(BIO *bp) { BIO_write(bp,"</td></tr>\n",11); } +static void t1(BIO *bp) +{ + BIO_write(bp, "<tr><td>", 8); +} + +static void t2(BIO *bp) +{ + BIO_write(bp, "</td><td>", 9); +} + +static void t3(BIO *bp) +{ + BIO_write(bp, "</td></tr>\n", 11); +} int X509_print_fp_cop(FILE *fp, X509 *x) { -BIO *b; -int ret; + BIO *b; + int ret; -if ((b=BIO_new(BIO_s_file())) == NULL) - { - return(0); - } -BIO_set_fp(b,fp,BIO_NOCLOSE); -ret=X509_print_cop(b, x); -BIO_free(b); -return(ret); + if ((b = BIO_new(BIO_s_file())) == NULL) { + return 0; + } + BIO_set_fp(b, fp, BIO_NOCLOSE); + ret = X509_print_cop(b, x); + BIO_free(b); + return ret; } + int X509_print_cop(BIO *bp, X509 *x) { -long l; -int ret=0,i,j,n; -char *s; -X509_CINF *ci; - ASN1_INTEGER *bs; -EVP_PKEY *pkey=NULL; - const char *neg; -X509_EXTENSION *ex; -ASN1_STRING *str=NULL; - -ci=x->cert_info; -BIO_printf(bp,"<h3><strong>@TR<<Certification Data>></strong></h3>\n"); -BIO_printf(bp,"<table style=\"width: 90%%; margin-left: 2.5em; text-align: left; font-size: 0.8em;\" border=\"0\" cellpadding=\"2\" cellspacing=\"1\" summary=\"@TR<<Certification>>\">\n"); - - -BIO_printf(bp,"<tr><td><table style=\"margin-left: 0.2em; text-align: left;\" border=\"0\" cellpadding=\"1\" cellspacing= \"8\" summary=\"@TR<<Certification>>\">\n"); -l=X509_get_version(x); -if (BIO_printf(bp,"<tr><td>Version</td><td>%lu (0x%lx)</td></tr>\n",l+1,l) <= 0) goto err; - -t1(bp); -if (BIO_write(bp,"Serial Number",13) <= 0) goto err; -t2(bp); - -bs=X509_get_serialNumber(x); -if (bs->length <= 4) - { - l=ASN1_INTEGER_get(bs); - if (l < 0) - { - l= -l; - neg="-"; - } - else - neg=""; - if (BIO_printf(bp," %s%lu (%s0x%lx)",neg,l,neg,l) <= 0) - goto err; + long l; + int ret = 0, i, j, n; + char *s; + X509_CINF *ci; + ASN1_INTEGER *bs; + EVP_PKEY *pkey = NULL; + const char *neg; + X509_EXTENSION *ex; + ASN1_STRING *str = NULL; + + ci = x->cert_info; + BIO_printf(bp, "<h3><strong>@TR<<Certification Data>></strong></h3>\n"); + BIO_printf(bp, "<table style=\"width: 90%%; margin-left: 2.5em; text-align: left; font-size: 0.8em;\" border=\"0\" cellpadding=\"2\" cellspacing=\"1\" summary=\"@TR<<Certification>>\">\n"); + + BIO_printf(bp, "<tr><td><table style=\"margin-left: 0.2em; text-align: left;\" border=\"0\" cellpadding=\"1\" cellspacing= \"8\" summary=\"@TR<<Certification>>\">\n"); + l = X509_get_version(x); + if (BIO_printf(bp, "<tr><td>Version</td><td>%lu (0x%lx)</td></tr>\n", + l + 1, l) <= 0) { + goto err; + } + + t1(bp); + if (BIO_write(bp, "Serial Number", 13) <= 0) { + goto err; + } + t2(bp); + + bs = X509_get_serialNumber(x); + if (bs->length <= 4) { + l = ASN1_INTEGER_get(bs); + if (l < 0) { + l = -l; + neg = "-"; + } else { + neg = ""; + } + if (BIO_printf(bp, " %s%lu (%s0x%lx)", neg, l, neg, l) <= 0) { + goto err; } -else - { - neg=(bs->type == V_ASN1_NEG_INTEGER)?" (Negative)":""; - if (BIO_printf(bp,"\n%12s%s","",neg) <= 0) goto err; - - for (i=0; i<bs->length; i++) - { - if (BIO_printf(bp,"%02x%c",bs->data[i], - ((i+1 == bs->length)?'\n':':')) <= 0) - goto err; - } + } else { + neg = (bs->type == V_ASN1_NEG_INTEGER) ? " (Negative)" : ""; + if (BIO_printf(bp, "\n%12s%s", "", neg) <= 0) { + goto err; } -t3(bp); -i=OBJ_obj2nid(ci->signature->algorithm); -if (BIO_printf(bp,"<tr><td>Signature Algorithm</td><td> %s</td></tr>\n", - (i == NID_undef)?"UNKNOWN":OBJ_nid2ln(i)) <= 0) - goto err; - -t1(bp); -if (BIO_write(bp,"Issuer",6) <= 0) goto err; -t2(bp); -if (!X509_NAME_print(bp,X509_get_issuer_name(x),16)) goto err; -t3(bp); - -t1(bp); -if (BIO_write(bp,"Valid Not Before",16) <= 0) goto err; -t2(bp); -if (!ASN1_TIME_print(bp,X509_get_notBefore(x))) goto err; -t3(bp); - -t1(bp); -if (BIO_write(bp,"Valid Not After",15) <= 0) goto err; -t2(bp); -if (!ASN1_TIME_print(bp,X509_get_notAfter(x))) goto err; -t3(bp); - -t1(bp); -if (BIO_write(bp,"Subject",7) <= 0) goto err; -t2(bp); -if (!X509_NAME_print(bp,X509_get_subject_name(x),16)) goto err; -t3(bp); - -i=OBJ_obj2nid(ci->key->algor->algorithm); -if (BIO_printf(bp,"<tr><td>Public Key Algorithm</td><td>%s<br>\n", -(i == NID_undef)?"UNKNOWN":OBJ_nid2ln(i)) <= 0) goto err; + for (i = 0; i < bs->length; i++) { + if (BIO_printf(bp, "%02x%c", bs->data[i], + ((i + 1 == bs->length) ? '\n' : ':')) <= 0) { + goto err; + } + } + } + t3(bp); -pkey=X509_get_pubkey(x); -if (pkey == NULL) -{ -BIO_printf(bp,"%12sUnable to load Public Key\n",""); -} -else + i = OBJ_obj2nid(ci->signature->algorithm); + if (BIO_printf(bp, "<tr><td>Signature Algorithm</td><td> %s</td></tr>\n", + (i == NID_undef) ? "UNKNOWN" : OBJ_nid2ln(i)) <= 0) { + goto err; + } + + t1(bp); + if (BIO_write(bp, "Issuer", 6) <= 0) { + goto err; + } + t2(bp); + if (!X509_NAME_print(bp, X509_get_issuer_name(x), 16)) { + goto err; + } + t3(bp); + + t1(bp); + if (BIO_write(bp, "Valid Not Before", 16) <= 0) { + goto err; + } + t2(bp); + if (!ASN1_TIME_print(bp, X509_get_notBefore(x))) { + goto err; + } + t3(bp); + + t1(bp); + if (BIO_write(bp, "Valid Not After", 15) <= 0) { + goto err; + } + t2(bp); + if (!ASN1_TIME_print(bp, X509_get_notAfter(x))) { + goto err; + } + t3(bp); + + t1(bp); + if (BIO_write(bp, "Subject", 7) <= 0) { + goto err; + } + t2(bp); + if (!X509_NAME_print(bp, X509_get_subject_name(x), 16)) { + goto err; + } + t3(bp); + + i = OBJ_obj2nid(ci->key->algor->algorithm); + if (BIO_printf(bp, "<tr><td>Public Key Algorithm</td><td>%s<br>\n", + (i == NID_undef) ? "UNKNOWN" : OBJ_nid2ln(i)) <= 0) { + goto err; + } + + pkey = X509_get_pubkey(x); + if (pkey == NULL) { + BIO_printf(bp, "%12sUnable to load Public Key\n", ""); + } else #ifndef 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); - } -else + 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); + } else #endif #ifndef NO_DSA -if (pkey->type == EVP_PKEY_DSA) - { - BIO_printf(bp,"%12sDSA Public Key:\n",""); - DSA_print(bp,pkey->pkey.dsa,16); - } -else + if (pkey->type == EVP_PKEY_DSA) { + BIO_printf(bp, "%12sDSA Public Key:\n", ""); + DSA_print(bp, pkey->pkey.dsa, 16); + } else #endif - BIO_printf(bp,"%12sUnknown Public Key:\n",""); -t3(bp); -BIO_printf(bp,"</table>\n"); -EVP_PKEY_free(pkey); - -n=X509_get_ext_count(x); -if (n > 0) - { - - BIO_printf(bp,"<h3><strong>@TR<<X509v3 extensions>></strong></h3>\n"); - BIO_printf(bp,"<table style=\"width: 90%%; margin-left: 2.5em; text-align: left; font-size: 1em;\" border=\"0\" cellpadding=\"2\" cellspacing=\"1\" summary=\"@TR<<Certification>>\">" ); - BIO_printf(bp,"<tr><td><table style=\"margin-left: 0.2em; text-align: left;\" border=\"0\" cellpadding=\"1\" cellspacing=\"8\" summary=\"@TR<<Certification>>\">\n "); - for (i=0; i<n; i++) - { - ASN1_OBJECT *obj; - ex=X509_get_ext(x,i); - t1(bp); - obj=X509_EXTENSION_get_object(ex); - i2a_ASN1_OBJECT(bp,obj); - j=X509_EXTENSION_get_critical(ex); - t2(bp); - if (BIO_printf(bp,"%1s",j?"critical":"") <= 0) goto err; - t3(bp); - } + { BIO_printf(bp, "%12sUnknown Public Key:\n", ""); + } + t3(bp); + BIO_printf(bp, "</table>\n"); + EVP_PKEY_free(pkey); + + n = X509_get_ext_count(x); + if (n > 0) { + BIO_printf(bp, "<h3><strong>@TR<<X509v3 extensions>></strong></h3>\n"); + BIO_printf(bp, "<table style=\"width: 90%%; margin-left: 2.5em; text-align: left; font-size: 1em;\" border=\"0\" cellpadding=\"2\" cellspacing=\"1\" summary=\"@TR<<Certification>>\">" ); + BIO_printf(bp, "<tr><td><table style=\"margin-left: 0.2em; text-align: left;\" border=\"0\" cellpadding=\"1\" cellspacing=\"8\" summary=\"@TR<<Certification>>\">\n "); + for (i = 0; i < n; i++) { + ASN1_OBJECT *obj; + ex = X509_get_ext(x, i); + t1(bp); + obj = X509_EXTENSION_get_object(ex); + i2a_ASN1_OBJECT(bp, obj); + j = X509_EXTENSION_get_critical(ex); + t2(bp); + if (BIO_printf(bp, "%1s", j ? "critical" : "") <= 0) { + goto err; + } + t3(bp); } + } -i=OBJ_obj2nid(x->sig_alg->algorithm); -if (BIO_printf(bp,"<tr><td>Signature Algorithm</td><td>%s<br>\n", - (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_CERT_AUX_print(bp, x->aux, 0)) goto err; - ret=1; -err: - if (str != NULL) ASN1_STRING_free(str); -t3(bp); -BIO_write(bp,"\n</table>",9); - return(ret); + i = OBJ_obj2nid(x->sig_alg->algorithm); + if (BIO_printf(bp, "<tr><td>Signature Algorithm</td><td>%s<br>\n", + (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_CERT_AUX_print(bp, x->aux, 0)) { + goto err; + } + ret = 1; + +err: + if (str != NULL) { + ASN1_STRING_free(str); + } + t3(bp); + BIO_write(bp, "\n</table>", 9); + return ret; +} //################################################################################################################ @@ -205,7 +243,6 @@ */ static void usage(FILE *output) { - fprintf(output, "pisa-cert-info - infos of an X.509 certificate\n\n"); fprintf(output, "Usage:\n"); @@ -232,7 +269,6 @@ FILE *cert_file = NULL; X509 *cert = NULL; - /* give usage help if required */ if (argc == 1 || (argc == 2 && strlen(argv[1]) == 2 && !strcmp(argv[1], "-h")) || @@ -242,8 +278,7 @@ } /* process command-line parameters */ - - if (argc == 2 ) { + if (argc == 2) { filename = argv[1]; } else { usage(stderr); @@ -259,19 +294,18 @@ if (!(cert = PEM_read_X509_AUX(cert_file, NULL, NULL, passphrase))) { rewind(cert_file); if (!(cert = d2i_X509_fp(cert_file, NULL))) { - fprintf(stderr, "%s: reading the certificate file failed\n", filename); + fprintf(stderr, "%s: reading the certificate file failed\n", + filename); fclose(cert_file); exit(EXIT_FAILURE); } } - X509_print_fp_cop(stdout,cert); - - + X509_print_fp_cop(stdout, cert); /* clean up */ X509_free(cert); fclose(cert_file); - exit(EXIT_SUCCESS); + exit(EXIT_SUCCESS); } -- This is the pisa developer mailing list. Please also subscribe to the main pisa list at: //www.freelists.org/list/pisa