| OLD | NEW |
| (Empty) |
| 1 /* This Source Code Form is subject to the terms of the Mozilla Public | |
| 2 * License, v. 2.0. If a copy of the MPL was not distributed with this | |
| 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | |
| 4 | |
| 5 /* | |
| 6 * certhtml.c --- convert a cert to html | |
| 7 */ | |
| 8 | |
| 9 #include "seccomon.h" | |
| 10 #include "secitem.h" | |
| 11 #include "sechash.h" | |
| 12 #include "cert.h" | |
| 13 #include "keyhi.h" | |
| 14 #include "secder.h" | |
| 15 #include "prprf.h" | |
| 16 #include "secport.h" | |
| 17 #include "secasn1.h" | |
| 18 #include "pk11func.h" | |
| 19 | |
| 20 static char *hex = "0123456789ABCDEF"; | |
| 21 | |
| 22 /* | |
| 23 ** Convert a der-encoded integer to a hex printable string form | |
| 24 */ | |
| 25 char * | |
| 26 CERT_Hexify(SECItem *i, int do_colon) | |
| 27 { | |
| 28 unsigned char *cp, *end; | |
| 29 char *rv, *o; | |
| 30 | |
| 31 if (!i->len) { | |
| 32 return PORT_Strdup("00"); | |
| 33 } | |
| 34 | |
| 35 rv = o = (char *)PORT_Alloc(i->len * 3); | |
| 36 if (!rv) | |
| 37 return rv; | |
| 38 | |
| 39 cp = i->data; | |
| 40 end = cp + i->len; | |
| 41 while (cp < end) { | |
| 42 unsigned char ch = *cp++; | |
| 43 *o++ = hex[(ch >> 4) & 0xf]; | |
| 44 *o++ = hex[ch & 0xf]; | |
| 45 if (cp != end) { | |
| 46 if (do_colon) { | |
| 47 *o++ = ':'; | |
| 48 } | |
| 49 } | |
| 50 } | |
| 51 *o = 0; /* Null terminate the string */ | |
| 52 return rv; | |
| 53 } | |
| 54 | |
| 55 #define BREAK "<br>" | |
| 56 #define BREAKLEN 4 | |
| 57 #define COMMA ", " | |
| 58 #define COMMALEN 2 | |
| 59 | |
| 60 #define MAX_OUS 20 | |
| 61 #define MAX_DC MAX_OUS | |
| 62 | |
| 63 char * | |
| 64 CERT_FormatName(CERTName *name) | |
| 65 { | |
| 66 CERTRDN **rdns; | |
| 67 CERTRDN *rdn; | |
| 68 CERTAVA **avas; | |
| 69 CERTAVA *ava; | |
| 70 char *buf = 0; | |
| 71 char *tmpbuf = 0; | |
| 72 SECItem *cn = 0; | |
| 73 SECItem *email = 0; | |
| 74 SECItem *org = 0; | |
| 75 SECItem *loc = 0; | |
| 76 SECItem *state = 0; | |
| 77 SECItem *country = 0; | |
| 78 SECItem *dq = 0; | |
| 79 | |
| 80 unsigned len = 0; | |
| 81 int tag; | |
| 82 int i; | |
| 83 int ou_count = 0; | |
| 84 int dc_count = 0; | |
| 85 PRBool first; | |
| 86 SECItem *orgunit[MAX_OUS]; | |
| 87 SECItem *dc[MAX_DC]; | |
| 88 | |
| 89 /* Loop over name components and gather the interesting ones */ | |
| 90 rdns = name->rdns; | |
| 91 while ((rdn = *rdns++) != 0) { | |
| 92 avas = rdn->avas; | |
| 93 while ((ava = *avas++) != 0) { | |
| 94 tag = CERT_GetAVATag(ava); | |
| 95 switch (tag) { | |
| 96 case SEC_OID_AVA_COMMON_NAME: | |
| 97 if (cn) { | |
| 98 break; | |
| 99 } | |
| 100 cn = CERT_DecodeAVAValue(&ava->value); | |
| 101 if (!cn) { | |
| 102 goto loser; | |
| 103 } | |
| 104 len += cn->len; | |
| 105 break; | |
| 106 case SEC_OID_AVA_COUNTRY_NAME: | |
| 107 if (country) { | |
| 108 break; | |
| 109 } | |
| 110 country = CERT_DecodeAVAValue(&ava->value); | |
| 111 if (!country) { | |
| 112 goto loser; | |
| 113 } | |
| 114 len += country->len; | |
| 115 break; | |
| 116 case SEC_OID_AVA_LOCALITY: | |
| 117 if (loc) { | |
| 118 break; | |
| 119 } | |
| 120 loc = CERT_DecodeAVAValue(&ava->value); | |
| 121 if (!loc) { | |
| 122 goto loser; | |
| 123 } | |
| 124 len += loc->len; | |
| 125 break; | |
| 126 case SEC_OID_AVA_STATE_OR_PROVINCE: | |
| 127 if (state) { | |
| 128 break; | |
| 129 } | |
| 130 state = CERT_DecodeAVAValue(&ava->value); | |
| 131 if (!state) { | |
| 132 goto loser; | |
| 133 } | |
| 134 len += state->len; | |
| 135 break; | |
| 136 case SEC_OID_AVA_ORGANIZATION_NAME: | |
| 137 if (org) { | |
| 138 break; | |
| 139 } | |
| 140 org = CERT_DecodeAVAValue(&ava->value); | |
| 141 if (!org) { | |
| 142 goto loser; | |
| 143 } | |
| 144 len += org->len; | |
| 145 break; | |
| 146 case SEC_OID_AVA_DN_QUALIFIER: | |
| 147 if (dq) { | |
| 148 break; | |
| 149 } | |
| 150 dq = CERT_DecodeAVAValue(&ava->value); | |
| 151 if (!dq) { | |
| 152 goto loser; | |
| 153 } | |
| 154 len += dq->len; | |
| 155 break; | |
| 156 case SEC_OID_AVA_ORGANIZATIONAL_UNIT_NAME: | |
| 157 if (ou_count < MAX_OUS) { | |
| 158 orgunit[ou_count] = CERT_DecodeAVAValue(&ava->value); | |
| 159 if (!orgunit[ou_count]) { | |
| 160 goto loser; | |
| 161 } | |
| 162 len += orgunit[ou_count++]->len; | |
| 163 } | |
| 164 break; | |
| 165 case SEC_OID_AVA_DC: | |
| 166 if (dc_count < MAX_DC) { | |
| 167 dc[dc_count] = CERT_DecodeAVAValue(&ava->value); | |
| 168 if (!dc[dc_count]) { | |
| 169 goto loser; | |
| 170 } | |
| 171 len += dc[dc_count++]->len; | |
| 172 } | |
| 173 break; | |
| 174 case SEC_OID_PKCS9_EMAIL_ADDRESS: | |
| 175 case SEC_OID_RFC1274_MAIL: | |
| 176 if (email) { | |
| 177 break; | |
| 178 } | |
| 179 email = CERT_DecodeAVAValue(&ava->value); | |
| 180 if (!email) { | |
| 181 goto loser; | |
| 182 } | |
| 183 len += email->len; | |
| 184 break; | |
| 185 default: | |
| 186 break; | |
| 187 } | |
| 188 } | |
| 189 } | |
| 190 | |
| 191 /* XXX - add some for formatting */ | |
| 192 len += 128; | |
| 193 | |
| 194 /* allocate buffer */ | |
| 195 buf = (char *)PORT_Alloc(len); | |
| 196 if (!buf) { | |
| 197 goto loser; | |
| 198 } | |
| 199 | |
| 200 tmpbuf = buf; | |
| 201 | |
| 202 if (cn) { | |
| 203 PORT_Memcpy(tmpbuf, cn->data, cn->len); | |
| 204 tmpbuf += cn->len; | |
| 205 PORT_Memcpy(tmpbuf, BREAK, BREAKLEN); | |
| 206 tmpbuf += BREAKLEN; | |
| 207 } | |
| 208 if (email) { | |
| 209 PORT_Memcpy(tmpbuf, email->data, email->len); | |
| 210 tmpbuf += (email->len); | |
| 211 PORT_Memcpy(tmpbuf, BREAK, BREAKLEN); | |
| 212 tmpbuf += BREAKLEN; | |
| 213 } | |
| 214 for (i = ou_count - 1; i >= 0; i--) { | |
| 215 PORT_Memcpy(tmpbuf, orgunit[i]->data, orgunit[i]->len); | |
| 216 tmpbuf += (orgunit[i]->len); | |
| 217 PORT_Memcpy(tmpbuf, BREAK, BREAKLEN); | |
| 218 tmpbuf += BREAKLEN; | |
| 219 } | |
| 220 if (dq) { | |
| 221 PORT_Memcpy(tmpbuf, dq->data, dq->len); | |
| 222 tmpbuf += (dq->len); | |
| 223 PORT_Memcpy(tmpbuf, BREAK, BREAKLEN); | |
| 224 tmpbuf += BREAKLEN; | |
| 225 } | |
| 226 if (org) { | |
| 227 PORT_Memcpy(tmpbuf, org->data, org->len); | |
| 228 tmpbuf += (org->len); | |
| 229 PORT_Memcpy(tmpbuf, BREAK, BREAKLEN); | |
| 230 tmpbuf += BREAKLEN; | |
| 231 } | |
| 232 for (i = dc_count - 1; i >= 0; i--) { | |
| 233 PORT_Memcpy(tmpbuf, dc[i]->data, dc[i]->len); | |
| 234 tmpbuf += (dc[i]->len); | |
| 235 PORT_Memcpy(tmpbuf, BREAK, BREAKLEN); | |
| 236 tmpbuf += BREAKLEN; | |
| 237 } | |
| 238 first = PR_TRUE; | |
| 239 if (loc) { | |
| 240 PORT_Memcpy(tmpbuf, loc->data, loc->len); | |
| 241 tmpbuf += (loc->len); | |
| 242 first = PR_FALSE; | |
| 243 } | |
| 244 if (state) { | |
| 245 if (!first) { | |
| 246 PORT_Memcpy(tmpbuf, COMMA, COMMALEN); | |
| 247 tmpbuf += COMMALEN; | |
| 248 } | |
| 249 PORT_Memcpy(tmpbuf, state->data, state->len); | |
| 250 tmpbuf += (state->len); | |
| 251 first = PR_FALSE; | |
| 252 } | |
| 253 if (country) { | |
| 254 if (!first) { | |
| 255 PORT_Memcpy(tmpbuf, COMMA, COMMALEN); | |
| 256 tmpbuf += COMMALEN; | |
| 257 } | |
| 258 PORT_Memcpy(tmpbuf, country->data, country->len); | |
| 259 tmpbuf += (country->len); | |
| 260 first = PR_FALSE; | |
| 261 } | |
| 262 if (!first) { | |
| 263 PORT_Memcpy(tmpbuf, BREAK, BREAKLEN); | |
| 264 tmpbuf += BREAKLEN; | |
| 265 } | |
| 266 | |
| 267 *tmpbuf = 0; | |
| 268 | |
| 269 /* fall through and clean */ | |
| 270 loser: | |
| 271 if (cn) { | |
| 272 SECITEM_FreeItem(cn, PR_TRUE); | |
| 273 } | |
| 274 if (email) { | |
| 275 SECITEM_FreeItem(email, PR_TRUE); | |
| 276 } | |
| 277 for (i = ou_count - 1; i >= 0; i--) { | |
| 278 SECITEM_FreeItem(orgunit[i], PR_TRUE); | |
| 279 } | |
| 280 if (dq) { | |
| 281 SECITEM_FreeItem(dq, PR_TRUE); | |
| 282 } | |
| 283 if (org) { | |
| 284 SECITEM_FreeItem(org, PR_TRUE); | |
| 285 } | |
| 286 for (i = dc_count - 1; i >= 0; i--) { | |
| 287 SECITEM_FreeItem(dc[i], PR_TRUE); | |
| 288 } | |
| 289 if (loc) { | |
| 290 SECITEM_FreeItem(loc, PR_TRUE); | |
| 291 } | |
| 292 if (state) { | |
| 293 SECITEM_FreeItem(state, PR_TRUE); | |
| 294 } | |
| 295 if (country) { | |
| 296 SECITEM_FreeItem(country, PR_TRUE); | |
| 297 } | |
| 298 | |
| 299 return (buf); | |
| 300 } | |
| OLD | NEW |