1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
|
/*
* Copyright 2009 Paul Blokus <paul_pl@users.sourceforge.net>
* Copyright 2013 Michael Drake <tlsa@netsurf-browser.org>
*
* This file is part of NetSurf, http://www.netsurf-browser.org/
*
* NetSurf is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License.
*
* NetSurf is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/** \file
* SSL Certificate verification UI (implementation)
*/
#include <assert.h>
#include <stdlib.h>
#include "content/fetch.h"
#include "content/urldb.h"
#include "content/hlcache.h"
#include "desktop/sslcert_viewer.h"
#include "desktop/treeview.h"
#include "utils/messages.h"
#include "utils/log.h"
#include "utils/utils.h"
enum sslcert_viewer_field {
SSLCERT_V_SUBJECT,
SSLCERT_V_SERIAL,
SSLCERT_V_TYPE,
SSLCERT_V_VALID_UNTIL,
SSLCERT_V_VALID_FROM,
SSLCERT_V_VERSION,
SSLCERT_V_ISSUER,
SSLCERT_V_CERTIFICATES,
SSLCERT_V_N_FIELDS
};
/** ssl certificate verification context. */
struct sslcert_session_data {
struct ssl_cert_info *certs; /**< Certificates */
unsigned long num; /**< Number of certificates in chain */
nsurl *url; /**< The url of the certificate */
llcache_query_response cb; /**< Cert accept/reject callback */
void *cbpw; /**< Context passed to callback */
treeview *tree; /**< The treeview object */
struct treeview_field_desc fields[SSLCERT_V_N_FIELDS];
};
struct sslcert_entry {
treeview_node *entry;
char version[24];
char serial[24];
char type[24];
struct treeview_field_data data[SSLCERT_V_N_FIELDS - 1];
};
/**
* Free a sll certificate viewer entry's treeview field data.
*
* \param e Entry to free data from
*/
static void sslcert_viewer_free_treeview_field_data(
struct sslcert_entry *e)
{
}
/**
* Build a sslcert viewer treeview field from given text
*
* \param field SSL certificate treeview field to build
* \param data SSL certificate entry field data to set
* \param value Text to set in field, ownership yielded
* \param ssl_d SSL certificate session data
* \return NSERROR_OK on success, appropriate error otherwise
*/
static inline nserror sslcert_viewer_field_builder(
enum sslcert_viewer_field field,
struct treeview_field_data *data,
const char *value,
struct sslcert_session_data *ssl_d)
{
data->field = ssl_d->fields[field].field;
data->value = value;
data->value_len = (value != NULL) ? strlen(value) : 0;
return NSERROR_OK;
}
/**
* Set a sslcert viewer entry's data from the certificate.
*
* \param e Entry to set up
* \param cert Data associated with entry's certificate
* \param ssl_d SSL certificate session data
* \return NSERROR_OK on success, appropriate error otherwise
*/
static nserror sslcert_viewer_set_treeview_field_data(
struct sslcert_entry *e,
const struct ssl_cert_info *cert,
struct sslcert_session_data *ssl_d)
{
unsigned int written;
assert(e != NULL);
assert(cert != NULL);
assert(ssl_d != NULL);
/* Set the fields up */
sslcert_viewer_field_builder(SSLCERT_V_SUBJECT,
&e->data[SSLCERT_V_SUBJECT],
cert->subject, ssl_d);
written = snprintf(e->serial, sizeof(e->serial), "%li", cert->serial);
assert(written < sizeof(e->serial));
sslcert_viewer_field_builder(SSLCERT_V_SERIAL,
&e->data[SSLCERT_V_SERIAL],
e->serial, ssl_d);
written = snprintf(e->type, sizeof(e->type), "%i", cert->cert_type);
assert(written < sizeof(e->type));
sslcert_viewer_field_builder(SSLCERT_V_TYPE,
&e->data[SSLCERT_V_TYPE],
e->type, ssl_d);
sslcert_viewer_field_builder(SSLCERT_V_VALID_UNTIL,
&e->data[SSLCERT_V_VALID_UNTIL],
cert->not_after, ssl_d);
sslcert_viewer_field_builder(SSLCERT_V_VALID_FROM,
&e->data[SSLCERT_V_VALID_FROM],
cert->not_before, ssl_d);
written = snprintf(e->version, sizeof(e->version),
"%li", cert->version);
assert(written < sizeof(e->version));
sslcert_viewer_field_builder(SSLCERT_V_VERSION,
&e->data[SSLCERT_V_VERSION],
e->version, ssl_d);
sslcert_viewer_field_builder(SSLCERT_V_ISSUER,
&e->data[SSLCERT_V_ISSUER],
cert->issuer, ssl_d);
return NSERROR_OK;
}
/**
* Create a treeview node for a certificate
*
* \param ssl_d SSL certificate session data
* \param n Number of SSL certificate in chain, to make node for
* \return true on success, false on memory exhaustion
*/
static nserror sslcert_viewer_create_node(
struct sslcert_session_data *ssl_d, int n)
{
struct sslcert_entry *e;
const struct ssl_cert_info *cert = &(ssl_d->certs[n]);
nserror err;
/* Create new certificate viewer entry */
e = malloc(sizeof(struct sslcert_entry));
if (e == NULL) {
return NSERROR_NOMEM;
}
err = sslcert_viewer_set_treeview_field_data(e, cert, ssl_d);
if (err != NSERROR_OK) {
free(e);
return err;
}
/* Create the new treeview node */
err = treeview_create_node_entry(ssl_d->tree, &(e->entry),
NULL, TREE_REL_FIRST_CHILD,
e->data, e, TREE_OPTION_NONE);
if (err != NSERROR_OK) {
sslcert_viewer_free_treeview_field_data(e);
free(e);
return err;
}
return NSERROR_OK;
}
/**
* Initialise the treeview entry feilds
*
* \param ssl_d SSL certificate session data
* \return true on success, false on memory exhaustion
*/
static nserror sslcert_init_entry_fields(struct sslcert_session_data *ssl_d)
{
int i;
const char *label;
for (i = 0; i < SSLCERT_V_N_FIELDS; i++)
ssl_d->fields[i].field = NULL;
ssl_d->fields[SSLCERT_V_SUBJECT].flags = TREE_FLAG_DEFAULT;
label = "TreeviewLabelSubject";
label = messages_get(label);
if (lwc_intern_string(label, strlen(label),
&ssl_d->fields[SSLCERT_V_SUBJECT].field) !=
lwc_error_ok) {
goto error;
}
ssl_d->fields[SSLCERT_V_SERIAL].flags = TREE_FLAG_SHOW_NAME;
label = "TreeviewLabelSerial";
label = messages_get(label);
if (lwc_intern_string(label, strlen(label),
&ssl_d->fields[SSLCERT_V_SERIAL].field) !=
lwc_error_ok) {
goto error;
}
ssl_d->fields[SSLCERT_V_TYPE].flags = TREE_FLAG_SHOW_NAME;
label = "TreeviewLabelType";
label = messages_get(label);
if (lwc_intern_string(label, strlen(label),
&ssl_d->fields[SSLCERT_V_TYPE].field) !=
lwc_error_ok) {
goto error;
}
ssl_d->fields[SSLCERT_V_VALID_UNTIL].flags = TREE_FLAG_SHOW_NAME;
label = "TreeviewLabelValidUntil";
label = messages_get(label);
if (lwc_intern_string(label, strlen(label),
&ssl_d->fields[SSLCERT_V_VALID_UNTIL].field) !=
lwc_error_ok) {
goto error;
}
ssl_d->fields[SSLCERT_V_VALID_FROM].flags = TREE_FLAG_SHOW_NAME;
label = "TreeviewLabelValidFrom";
label = messages_get(label);
if (lwc_intern_string(label, strlen(label),
&ssl_d->fields[SSLCERT_V_VALID_FROM].field) !=
lwc_error_ok) {
goto error;
}
ssl_d->fields[SSLCERT_V_VERSION].flags = TREE_FLAG_SHOW_NAME;
label = "TreeviewLabelVersion";
label = messages_get(label);
if (lwc_intern_string(label, strlen(label),
&ssl_d->fields[SSLCERT_V_VERSION].field) !=
lwc_error_ok) {
goto error;
}
ssl_d->fields[SSLCERT_V_ISSUER].flags = TREE_FLAG_SHOW_NAME;
label = "TreeviewLabelIssuer";
label = messages_get(label);
if (lwc_intern_string(label, strlen(label),
&ssl_d->fields[SSLCERT_V_ISSUER].field) !=
lwc_error_ok) {
goto error;
}
ssl_d->fields[SSLCERT_V_CERTIFICATES].flags = TREE_FLAG_DEFAULT;
label = "TreeviewLabelCertificates";
label = messages_get(label);
if (lwc_intern_string(label, strlen(label),
&ssl_d->fields[SSLCERT_V_CERTIFICATES].field) !=
lwc_error_ok) {
return false;
}
return NSERROR_OK;
error:
for (i = 0; i < SSLCERT_V_N_FIELDS; i++)
if (ssl_d->fields[i].field != NULL)
lwc_string_unref(ssl_d->fields[i].field);
return NSERROR_UNKNOWN;
}
/**
* Delete ssl certificate viewer entries
*
* \param e Entry to delete.
*/
static void sslcert_viewer_delete_entry(struct sslcert_entry *e)
{
sslcert_viewer_free_treeview_field_data(e);
free(e);
}
static nserror sslcert_viewer_tree_node_folder_cb(
struct treeview_node_msg msg, void *data)
{
switch (msg.msg) {
case TREE_MSG_NODE_DELETE:
case TREE_MSG_NODE_EDIT:
case TREE_MSG_NODE_LAUNCH:
break;
}
return NSERROR_OK;
}
static nserror sslcert_viewer_tree_node_entry_cb(
struct treeview_node_msg msg, void *data)
{
struct sslcert_entry *e = data;
switch (msg.msg) {
case TREE_MSG_NODE_DELETE:
e->entry = NULL;
sslcert_viewer_delete_entry(e);
break;
case TREE_MSG_NODE_EDIT:
case TREE_MSG_NODE_LAUNCH:
break;
}
return NSERROR_OK;
}
struct treeview_callback_table sslv_tree_cb_t = {
.folder = sslcert_viewer_tree_node_folder_cb,
.entry = sslcert_viewer_tree_node_entry_cb
};
/* Exported interface, documented in sslcert_viewer.h */
nserror sslcert_viewer_init(struct core_window_callback_table *cw_t,
void *core_window_handle, struct sslcert_session_data *ssl_d)
{
nserror err;
int cert_loop;
assert(ssl_d != NULL);
LOG(("Building certificate viewer"));
/* Init. certificate chain treeview entry fields */
err = sslcert_init_entry_fields(ssl_d);
if (err != NSERROR_OK) {
ssl_d->tree = NULL;
return err;
}
/* Create the certificate treeview */
err = treeview_create(&ssl_d->tree, &sslv_tree_cb_t,
SSLCERT_V_N_FIELDS, ssl_d->fields,
cw_t, core_window_handle, TREEVIEW_READ_ONLY);
if (err != NSERROR_OK) {
ssl_d->tree = NULL;
return err;
}
/* Build treeview nodes from certificate chain */
for (cert_loop = ssl_d->num - 1; cert_loop >= 0; cert_loop--) {
err = sslcert_viewer_create_node(ssl_d, cert_loop);
if (err != NSERROR_OK) {
return err;
}
}
LOG(("Built certificate viewer"));
return NSERROR_OK;
}
/**
* Free SSL certificate session data
*
* \param ssl_d SSL certificate session data
*/
static void sslcert_cleanup_session(struct sslcert_session_data *ssl_d)
{
assert(ssl_d != NULL);
if (ssl_d->url) {
nsurl_unref(ssl_d->url);
ssl_d->url = NULL;
}
if (ssl_d->certs) {
free(ssl_d->certs);
ssl_d->certs = NULL;
}
free(ssl_d);
}
/* Exported interface, documented in sslcert_viewer.h */
nserror sslcert_viewer_fini(struct sslcert_session_data *ssl_d)
{
int i;
nserror err;
LOG(("Finalising ssl certificate viewer"));
/* Destroy the treeview */
err = treeview_destroy(ssl_d->tree);
/* Free treeview entry fields */
for (i = 0; i < SSLCERT_V_N_FIELDS; i++)
if (ssl_d->fields[i].field != NULL)
lwc_string_unref(ssl_d->fields[i].field);
/* Destroy the sslcert_session_data */
sslcert_cleanup_session(ssl_d);
LOG(("Finalised ssl certificate viewer"));
return err;
}
/* Exported interface, documented in sslcert_viewer.h */
nserror sslcert_viewer_create_session_data(unsigned long num, nsurl *url,
llcache_query_response cb, void *cbpw,
const struct ssl_cert_info *certs,
struct sslcert_session_data **ssl_d)
{
struct sslcert_session_data *data;
assert(url != NULL);
assert(certs != NULL);
data = malloc(sizeof(struct sslcert_session_data));
if (data == NULL) {
*ssl_d = NULL;
return NSERROR_NOMEM;
}
/* copy certificate data */
data->certs = malloc(num * sizeof(struct ssl_cert_info));
if (data->certs == NULL) {
free(data);
*ssl_d = NULL;
return NSERROR_NOMEM;
}
memcpy(data->certs, certs, num * sizeof(struct ssl_cert_info));
data->url = nsurl_ref(url);
data->num = num;
data->cb = cb;
data->cbpw = cbpw;
data->tree = NULL;
*ssl_d = data;
return NSERROR_OK;
}
/* Exported interface, documented in sslcert_viewer.h */
nserror sslcert_viewer_reject(struct sslcert_session_data *ssl_d)
{
assert(ssl_d != NULL);
ssl_d->cb(false, ssl_d->cbpw);
return NSERROR_OK;
}
/* Exported interface, documented in sslcert_viewer.h */
nserror sslcert_viewer_accept(struct sslcert_session_data *ssl_d)
{
assert(ssl_d != NULL);
urldb_set_cert_permissions(ssl_d->url, true);
ssl_d->cb(true, ssl_d->cbpw);
return NSERROR_OK;
}
/* Exported interface, documented in sslcert_viewer.h */
void sslcert_viewer_redraw(struct sslcert_session_data *ssl_d,
int x, int y, struct rect *clip,
const struct redraw_context *ctx)
{
assert(ssl_d != NULL &&
"sslcert_viewer_redraw() given bad session data");
treeview_redraw(ssl_d->tree, x, y, clip, ctx);
}
/* Exported interface, documented in sslcert_viewer.h */
void sslcert_viewer_mouse_action(struct sslcert_session_data *ssl_d,
browser_mouse_state mouse, int x, int y)
{
treeview_mouse_action(ssl_d->tree, mouse, x, y);
}
/* Exported interface, documented in sslcert_viewer.h */
void sslcert_viewer_keypress(struct sslcert_session_data *ssl_d,
uint32_t key)
{
treeview_keypress(ssl_d->tree, key);
}
|