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