Back to index

lightning-sunbird  0.9+nobinonly
Defines | Functions | Variables
certhtml.c File Reference
#include "seccomon.h"
#include "secitem.h"
#include "sechash.h"
#include "cert.h"
#include "keyhi.h"
#include "secder.h"
#include "prprf.h"
#include "secport.h"
#include "secasn1.h"
#include "pk11func.h"

Go to the source code of this file.

Defines

#define BREAK   "<br>"
#define BREAKLEN   4
#define COMMA   ", "
#define COMMALEN   2
#define MAX_OUS   20
#define MAX_DC   MAX_OUS

Functions

char * CERT_Hexify (SECItem *i, int do_colon)
char * CERT_FormatName (CERTName *name)

Variables

static char * hex = "0123456789ABCDEF"

Define Documentation

#define BREAK   "<br>"

Definition at line 87 of file certhtml.c.

#define BREAKLEN   4

Definition at line 88 of file certhtml.c.

#define COMMA   ", "

Definition at line 89 of file certhtml.c.

#define COMMALEN   2

Definition at line 90 of file certhtml.c.

Definition at line 93 of file certhtml.c.

#define MAX_OUS   20

Definition at line 92 of file certhtml.c.


Function Documentation

char* CERT_FormatName ( CERTName *  name)

Definition at line 96 of file certhtml.c.

{
    CERTRDN** rdns;
    CERTRDN * rdn;
    CERTAVA** avas;
    CERTAVA*  ava;
    char *    buf    = 0;
    char *    tmpbuf = 0;
    SECItem * cn     = 0;
    SECItem * email  = 0;
    SECItem * org    = 0;
    SECItem * loc    = 0;
    SECItem * state  = 0;
    SECItem * country       = 0;
    SECItem * dq            = 0;

    unsigned  len    = 0;
    int       tag;
    int       i;
    int       ou_count = 0;
    int       dc_count = 0;
    PRBool    first;
    SECItem * orgunit[MAX_OUS];
    SECItem * dc[MAX_DC];

    /* Loop over name components and gather the interesting ones */
    rdns = name->rdns;
    while ((rdn = *rdns++) != 0) {
       avas = rdn->avas;
       while ((ava = *avas++) != 0) {
           tag = CERT_GetAVATag(ava);
           switch(tag) {
             case SEC_OID_AVA_COMMON_NAME:
              cn = CERT_DecodeAVAValue(&ava->value);
              len += cn->len;
              break;
             case SEC_OID_AVA_COUNTRY_NAME:
              country = CERT_DecodeAVAValue(&ava->value);
              len += country->len;
              break;
             case SEC_OID_AVA_LOCALITY:
              loc = CERT_DecodeAVAValue(&ava->value);
              len += loc->len;
              break;
             case SEC_OID_AVA_STATE_OR_PROVINCE:
              state = CERT_DecodeAVAValue(&ava->value);
              len += state->len;
              break;
             case SEC_OID_AVA_ORGANIZATION_NAME:
              org = CERT_DecodeAVAValue(&ava->value);
              len += org->len;
              break;
             case SEC_OID_AVA_DN_QUALIFIER:
              dq = CERT_DecodeAVAValue(&ava->value);
              len += dq->len;
              break;
             case SEC_OID_AVA_ORGANIZATIONAL_UNIT_NAME:
              if (ou_count < MAX_OUS) {
                     orgunit[ou_count] = CERT_DecodeAVAValue(&ava->value);
                     len += orgunit[ou_count++]->len;
              }
              break;
             case SEC_OID_AVA_DC:
              if (dc_count < MAX_DC) {
                     dc[dc_count] = CERT_DecodeAVAValue(&ava->value);
                     len += dc[dc_count++]->len;
              }
              break;
             case SEC_OID_PKCS9_EMAIL_ADDRESS:
             case SEC_OID_RFC1274_MAIL:
              email = CERT_DecodeAVAValue(&ava->value);
              len += email->len;
              break;
             default:
              break;
           }
       }
    }

    /* XXX - add some for formatting */
    len += 128;

    /* allocate buffer */
    buf = (char *)PORT_Alloc(len);
    if ( !buf ) {
       return(0);
    }

    tmpbuf = buf;
    
    if ( cn ) {
       PORT_Memcpy(tmpbuf, cn->data, cn->len);
       tmpbuf += cn->len;
       PORT_Memcpy(tmpbuf, BREAK, BREAKLEN);
       tmpbuf += BREAKLEN;
       SECITEM_FreeItem(cn, PR_TRUE);
    }
    if ( email ) {
       PORT_Memcpy(tmpbuf, email->data, email->len);
       tmpbuf += ( email->len );
       PORT_Memcpy(tmpbuf, BREAK, BREAKLEN);
       tmpbuf += BREAKLEN;
       SECITEM_FreeItem(email, PR_TRUE);
    }
    for (i=ou_count-1; i >= 0; i--) {
       PORT_Memcpy(tmpbuf, orgunit[i]->data, orgunit[i]->len);
       tmpbuf += ( orgunit[i]->len );
       PORT_Memcpy(tmpbuf, BREAK, BREAKLEN);
       tmpbuf += BREAKLEN;
       SECITEM_FreeItem(orgunit[i], PR_TRUE);
    }
    if ( dq ) {
       PORT_Memcpy(tmpbuf, dq->data, dq->len);
       tmpbuf += ( dq->len );
       PORT_Memcpy(tmpbuf, BREAK, BREAKLEN);
       tmpbuf += BREAKLEN;
       SECITEM_FreeItem(dq, PR_TRUE);
    }
    if ( org ) {
       PORT_Memcpy(tmpbuf, org->data, org->len);
       tmpbuf += ( org->len );
       PORT_Memcpy(tmpbuf, BREAK, BREAKLEN);
       tmpbuf += BREAKLEN;
       SECITEM_FreeItem(org, PR_TRUE);
    }
    for (i=dc_count-1; i >= 0; i--) {
       PORT_Memcpy(tmpbuf, dc[i]->data, dc[i]->len);
       tmpbuf += ( dc[i]->len );
       PORT_Memcpy(tmpbuf, BREAK, BREAKLEN);
       tmpbuf += BREAKLEN;
       SECITEM_FreeItem(dc[i], PR_TRUE);
    }
    first = PR_TRUE;
    if ( loc ) {
       PORT_Memcpy(tmpbuf, loc->data,  loc->len);
       tmpbuf += ( loc->len );
       first = PR_FALSE;
       SECITEM_FreeItem(loc, PR_TRUE);
    }
    if ( state ) {
       if ( !first ) {
           PORT_Memcpy(tmpbuf, COMMA, COMMALEN);
           tmpbuf += COMMALEN;
       }
       PORT_Memcpy(tmpbuf, state->data, state->len);
       tmpbuf += ( state->len );
       first = PR_FALSE;
       SECITEM_FreeItem(state, PR_TRUE);
    }
    if ( country ) {
       if ( !first ) {
           PORT_Memcpy(tmpbuf, COMMA, COMMALEN);
           tmpbuf += COMMALEN;
       }
       PORT_Memcpy(tmpbuf, country->data, country->len);
       tmpbuf += ( country->len );
       first = PR_FALSE;
       SECITEM_FreeItem(country, PR_TRUE);
    }
    if ( !first ) {
       PORT_Memcpy(tmpbuf, BREAK, BREAKLEN);
       tmpbuf += BREAKLEN;
    }

    *tmpbuf = 0;

    return(buf);
}
char* CERT_Hexify ( SECItem *  i,
int  do_colon 
)

Definition at line 59 of file certhtml.c.

{
    unsigned char *cp, *end;
    char *rv, *o;

    if (!i->len) {
       return PORT_Strdup("00");
    }

    rv = o = (char*) PORT_Alloc(i->len * 3);
    if (!rv) return rv;

    cp = i->data;
    end = cp + i->len;
    while (cp < end) {
       unsigned char ch = *cp++;
       *o++ = hex[(ch >> 4) & 0xf];
       *o++ = hex[ch & 0xf];
       if (cp != end) {
           if (do_colon) {
              *o++ = ':';
           }
       } 
    }
    *o = 0;           /* Null terminate the string */
    return rv;
}

Variable Documentation

char* hex = "0123456789ABCDEF" [static]

Definition at line 54 of file certhtml.c.