Back to index

lightning-sunbird  0.9+nobinonly
certhtml.c
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is the Netscape security libraries.
00015  *
00016  * The Initial Developer of the Original Code is
00017  * Netscape Communications Corporation.
00018  * Portions created by the Initial Developer are Copyright (C) 1994-2000
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *
00023  * Alternatively, the contents of this file may be used under the terms of
00024  * either the GNU General Public License Version 2 or later (the "GPL"), or
00025  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00026  * in which case the provisions of the GPL or the LGPL are applicable instead
00027  * of those above. If you wish to allow use of your version of this file only
00028  * under the terms of either the GPL or the LGPL, and not to allow others to
00029  * use your version of this file under the terms of the MPL, indicate your
00030  * decision by deleting the provisions above and replace them with the notice
00031  * and other provisions required by the GPL or the LGPL. If you do not delete
00032  * the provisions above, a recipient may use your version of this file under
00033  * the terms of any one of the MPL, the GPL or the LGPL.
00034  *
00035  * ***** END LICENSE BLOCK ***** */
00036 
00037 /*
00038  * certhtml.c --- convert a cert to html
00039  *
00040  * $Id: certhtml.c,v 1.6.2.1 2007/05/15 23:25:16 julien.pierre.bugs%sun.com Exp $
00041  */
00042 
00043 #include "seccomon.h"
00044 #include "secitem.h"
00045 #include "sechash.h"
00046 #include "cert.h"
00047 #include "keyhi.h"
00048 #include "secder.h"
00049 #include "prprf.h"
00050 #include "secport.h"
00051 #include "secasn1.h"
00052 #include "pk11func.h"
00053 
00054 static char *hex = "0123456789ABCDEF";
00055 
00056 /*
00057 ** Convert a der-encoded integer to a hex printable string form
00058 */
00059 char *CERT_Hexify (SECItem *i, int do_colon)
00060 {
00061     unsigned char *cp, *end;
00062     char *rv, *o;
00063 
00064     if (!i->len) {
00065        return PORT_Strdup("00");
00066     }
00067 
00068     rv = o = (char*) PORT_Alloc(i->len * 3);
00069     if (!rv) return rv;
00070 
00071     cp = i->data;
00072     end = cp + i->len;
00073     while (cp < end) {
00074        unsigned char ch = *cp++;
00075        *o++ = hex[(ch >> 4) & 0xf];
00076        *o++ = hex[ch & 0xf];
00077        if (cp != end) {
00078            if (do_colon) {
00079               *o++ = ':';
00080            }
00081        } 
00082     }
00083     *o = 0;           /* Null terminate the string */
00084     return rv;
00085 }
00086 
00087 #define BREAK "<br>"
00088 #define BREAKLEN 4
00089 #define COMMA ", "
00090 #define COMMALEN 2
00091 
00092 #define MAX_OUS 20
00093 #define MAX_DC MAX_OUS
00094 
00095 
00096 char *CERT_FormatName (CERTName *name)
00097 {
00098     CERTRDN** rdns;
00099     CERTRDN * rdn;
00100     CERTAVA** avas;
00101     CERTAVA*  ava;
00102     char *    buf    = 0;
00103     char *    tmpbuf = 0;
00104     SECItem * cn     = 0;
00105     SECItem * email  = 0;
00106     SECItem * org    = 0;
00107     SECItem * loc    = 0;
00108     SECItem * state  = 0;
00109     SECItem * country       = 0;
00110     SECItem * dq            = 0;
00111 
00112     unsigned  len    = 0;
00113     int       tag;
00114     int       i;
00115     int       ou_count = 0;
00116     int       dc_count = 0;
00117     PRBool    first;
00118     SECItem * orgunit[MAX_OUS];
00119     SECItem * dc[MAX_DC];
00120 
00121     /* Loop over name components and gather the interesting ones */
00122     rdns = name->rdns;
00123     while ((rdn = *rdns++) != 0) {
00124        avas = rdn->avas;
00125        while ((ava = *avas++) != 0) {
00126            tag = CERT_GetAVATag(ava);
00127            switch(tag) {
00128              case SEC_OID_AVA_COMMON_NAME:
00129               cn = CERT_DecodeAVAValue(&ava->value);
00130               len += cn->len;
00131               break;
00132              case SEC_OID_AVA_COUNTRY_NAME:
00133               country = CERT_DecodeAVAValue(&ava->value);
00134               len += country->len;
00135               break;
00136              case SEC_OID_AVA_LOCALITY:
00137               loc = CERT_DecodeAVAValue(&ava->value);
00138               len += loc->len;
00139               break;
00140              case SEC_OID_AVA_STATE_OR_PROVINCE:
00141               state = CERT_DecodeAVAValue(&ava->value);
00142               len += state->len;
00143               break;
00144              case SEC_OID_AVA_ORGANIZATION_NAME:
00145               org = CERT_DecodeAVAValue(&ava->value);
00146               len += org->len;
00147               break;
00148              case SEC_OID_AVA_DN_QUALIFIER:
00149               dq = CERT_DecodeAVAValue(&ava->value);
00150               len += dq->len;
00151               break;
00152              case SEC_OID_AVA_ORGANIZATIONAL_UNIT_NAME:
00153               if (ou_count < MAX_OUS) {
00154                      orgunit[ou_count] = CERT_DecodeAVAValue(&ava->value);
00155                      len += orgunit[ou_count++]->len;
00156               }
00157               break;
00158              case SEC_OID_AVA_DC:
00159               if (dc_count < MAX_DC) {
00160                      dc[dc_count] = CERT_DecodeAVAValue(&ava->value);
00161                      len += dc[dc_count++]->len;
00162               }
00163               break;
00164              case SEC_OID_PKCS9_EMAIL_ADDRESS:
00165              case SEC_OID_RFC1274_MAIL:
00166               email = CERT_DecodeAVAValue(&ava->value);
00167               len += email->len;
00168               break;
00169              default:
00170               break;
00171            }
00172        }
00173     }
00174 
00175     /* XXX - add some for formatting */
00176     len += 128;
00177 
00178     /* allocate buffer */
00179     buf = (char *)PORT_Alloc(len);
00180     if ( !buf ) {
00181        return(0);
00182     }
00183 
00184     tmpbuf = buf;
00185     
00186     if ( cn ) {
00187        PORT_Memcpy(tmpbuf, cn->data, cn->len);
00188        tmpbuf += cn->len;
00189        PORT_Memcpy(tmpbuf, BREAK, BREAKLEN);
00190        tmpbuf += BREAKLEN;
00191        SECITEM_FreeItem(cn, PR_TRUE);
00192     }
00193     if ( email ) {
00194        PORT_Memcpy(tmpbuf, email->data, email->len);
00195        tmpbuf += ( email->len );
00196        PORT_Memcpy(tmpbuf, BREAK, BREAKLEN);
00197        tmpbuf += BREAKLEN;
00198        SECITEM_FreeItem(email, PR_TRUE);
00199     }
00200     for (i=ou_count-1; i >= 0; i--) {
00201        PORT_Memcpy(tmpbuf, orgunit[i]->data, orgunit[i]->len);
00202        tmpbuf += ( orgunit[i]->len );
00203        PORT_Memcpy(tmpbuf, BREAK, BREAKLEN);
00204        tmpbuf += BREAKLEN;
00205        SECITEM_FreeItem(orgunit[i], PR_TRUE);
00206     }
00207     if ( dq ) {
00208        PORT_Memcpy(tmpbuf, dq->data, dq->len);
00209        tmpbuf += ( dq->len );
00210        PORT_Memcpy(tmpbuf, BREAK, BREAKLEN);
00211        tmpbuf += BREAKLEN;
00212        SECITEM_FreeItem(dq, PR_TRUE);
00213     }
00214     if ( org ) {
00215        PORT_Memcpy(tmpbuf, org->data, org->len);
00216        tmpbuf += ( org->len );
00217        PORT_Memcpy(tmpbuf, BREAK, BREAKLEN);
00218        tmpbuf += BREAKLEN;
00219        SECITEM_FreeItem(org, PR_TRUE);
00220     }
00221     for (i=dc_count-1; i >= 0; i--) {
00222        PORT_Memcpy(tmpbuf, dc[i]->data, dc[i]->len);
00223        tmpbuf += ( dc[i]->len );
00224        PORT_Memcpy(tmpbuf, BREAK, BREAKLEN);
00225        tmpbuf += BREAKLEN;
00226        SECITEM_FreeItem(dc[i], PR_TRUE);
00227     }
00228     first = PR_TRUE;
00229     if ( loc ) {
00230        PORT_Memcpy(tmpbuf, loc->data,  loc->len);
00231        tmpbuf += ( loc->len );
00232        first = PR_FALSE;
00233        SECITEM_FreeItem(loc, PR_TRUE);
00234     }
00235     if ( state ) {
00236        if ( !first ) {
00237            PORT_Memcpy(tmpbuf, COMMA, COMMALEN);
00238            tmpbuf += COMMALEN;
00239        }
00240        PORT_Memcpy(tmpbuf, state->data, state->len);
00241        tmpbuf += ( state->len );
00242        first = PR_FALSE;
00243        SECITEM_FreeItem(state, PR_TRUE);
00244     }
00245     if ( country ) {
00246        if ( !first ) {
00247            PORT_Memcpy(tmpbuf, COMMA, COMMALEN);
00248            tmpbuf += COMMALEN;
00249        }
00250        PORT_Memcpy(tmpbuf, country->data, country->len);
00251        tmpbuf += ( country->len );
00252        first = PR_FALSE;
00253        SECITEM_FreeItem(country, PR_TRUE);
00254     }
00255     if ( !first ) {
00256        PORT_Memcpy(tmpbuf, BREAK, BREAKLEN);
00257        tmpbuf += BREAKLEN;
00258     }
00259 
00260     *tmpbuf = 0;
00261 
00262     return(buf);
00263 }
00264