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