summaryrefslogtreecommitdiff
path: root/desktop/sslcert.c
blob: b7a42446591e3135e68c2d83e3fd163751c0aece (plain)
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
/*
 * Copyright 2009 Paul Blokus <paul_pl@users.sourceforge.net>
 *
 * 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 "utils/config.h"

#include <assert.h>
#include <stdbool.h>
#include <string.h>
#include "content/content.h"
#include "content/fetch.h"
#include "content/hlcache.h"
#include "content/urldb.h"
#include "desktop/browser.h"
#include "desktop/sslcert.h"
#include "desktop/tree.h"
#include "utils/log.h"
#include "utils/messages.h"
#include "utils/nsurl.h"
#include "utils/utils.h"

/** Flags for each type of ssl tree node. */
enum tree_element_ssl {
	TREE_ELEMENT_SSL_VERSION = 0x01,
	TREE_ELEMENT_SSL_VALID_FROM = 0x02,
	TREE_ELEMENT_SSL_VALID_TO = 0x03,
	TREE_ELEMENT_SSL_CERT_TYPE = 0x04,
	TREE_ELEMENT_SSL_SERIAL = 0x05,
	TREE_ELEMENT_SSL_ISSUER = 0x06,
};

/** ssl certificate verification context. */
struct sslcert_session_data {
	unsigned long num; /**< The number of ssl certificates in the chain */
	nsurl *url; /**< The url of the certificate */
	struct tree *tree; /**< The root of the treeview */
	llcache_query_response cb; /**< callback when cert is accepted or rejected */
	void *cbpw; /**< context passed to callback */
};

/** Handle for the window icon. */
static hlcache_handle *sslcert_icon = NULL;

/** Initialise ssl certificate window. */
void sslcert_init(const char* icon_name)
{
	sslcert_icon = tree_load_icon(icon_name);
}


/**
 * Get flags with which the sslcert tree should be created;
 *
 * \return the flags
 */
unsigned int sslcert_get_tree_flags(void)
{
	return TREE_NO_DRAGS  | TREE_NO_SELECT;
}


void sslcert_cleanup(void)
{
	if (sslcert_icon != NULL)
		hlcache_handle_release(sslcert_icon);
}

struct sslcert_session_data *
sslcert_create_session_data(unsigned long num,
			    nsurl *url, 
			    llcache_query_response cb, 
			    void *cbpw)
{
	struct sslcert_session_data *data;

	data = malloc(sizeof(struct sslcert_session_data));
	if (data == NULL) {
		warn_user("NoMemory", 0);
		return NULL;
	}
	data->url = nsurl_ref(url);
	if (data->url == NULL) {
		free(data);
		warn_user("NoMemory", 0);
		return NULL;
	}
	data->num = num;
	data->cb = cb;
	data->cbpw = cbpw;

	return data;
}

static node_callback_resp sslcert_node_callback(void *user_data,
						struct node_msg_data *msg_data)
{
	if (msg_data->msg == NODE_DELETE_ELEMENT_IMG)
		return NODE_CALLBACK_HANDLED;
	return NODE_CALLBACK_NOT_HANDLED;
}

static struct node *sslcert_create_node(const struct ssl_cert_info *cert)
{
	struct node *node;
	struct node_element *element;
	char *text;

	text = messages_get_buff("SSL_Certificate_Subject", cert->subject);
	if (text == NULL)
		return NULL;

	node = tree_create_leaf_node(NULL, NULL, text, false, false, false);
	if (node == NULL) {
		free(text);
		return NULL;
	}
	tree_set_node_user_callback(node, sslcert_node_callback, NULL);

	/* add issuer node */
	element = tree_create_node_element(node, NODE_ELEMENT_TEXT,
					   TREE_ELEMENT_SSL_ISSUER, false);
	if (element != NULL) {
		text = messages_get_buff("SSL_Certificate_Issuer", cert->issuer);
		if (text == NULL) {
			tree_delete_node(NULL, node, false);
			return NULL;
		}
		tree_update_node_element(NULL, element, text, NULL);
	}

	/* add version node */
	element = tree_create_node_element(node, NODE_ELEMENT_TEXT,
					   TREE_ELEMENT_SSL_VERSION, false);
	if (element != NULL) {
		text = messages_get_buff("SSL_Certificate_Version", cert->version);
		if (text == NULL) {
			tree_delete_node(NULL, node, false);
			return NULL;
		}
		tree_update_node_element(NULL, element, text, NULL);
	}

	/* add valid from node */
	element = tree_create_node_element(node, NODE_ELEMENT_TEXT,
					   TREE_ELEMENT_SSL_VALID_FROM, false);
	if (element != NULL) {
		text = messages_get_buff("SSL_Certificate_ValidFrom", cert->not_before);
		if (text == NULL) {
			tree_delete_node(NULL, node, false);
			return NULL;
		}
		tree_update_node_element(NULL, element, text, NULL);
	}


	/* add valid to node */
	element = tree_create_node_element(node, NODE_ELEMENT_TEXT,
					   TREE_ELEMENT_SSL_VALID_TO, false);
	if (element != NULL) {
		text = messages_get_buff("SSL_Certificate_ValidTo", cert->not_after);
		if (text == NULL) {
			tree_delete_node(NULL, node, false);
			return NULL;
		}
		tree_update_node_element(NULL, element, text, NULL);
	}

	/* add certificate type */
	element = tree_create_node_element(node, NODE_ELEMENT_TEXT,
					   TREE_ELEMENT_SSL_CERT_TYPE, false);
	if (element != NULL) {
		text = messages_get_buff("SSL_Certificate_Type", cert->cert_type);
		if (text == NULL) {
			tree_delete_node(NULL, node, false);
			return NULL;
		}
		tree_update_node_element(NULL, element, text, NULL);
	}

	/* add serial node */
	element = tree_create_node_element(node, NODE_ELEMENT_TEXT,
					   TREE_ELEMENT_SSL_SERIAL, false);
	if (element != NULL) {
		text = messages_get_buff("SSL_Certificate_Serial", cert->serial);
		if (text == NULL) {
			tree_delete_node(NULL, node, false);
			return NULL;
		}
		tree_update_node_element(NULL, element, text, NULL);
	}

	/* set the display icon */
	tree_set_node_icon(NULL, node, sslcert_icon);

	return node;
}

bool sslcert_load_tree(struct tree *tree, 
		       const struct ssl_cert_info *certs,
		       struct sslcert_session_data *data)
{
	struct node *tree_root;
	struct node *node;
	unsigned long cert_loop;

	assert(data != NULL && certs != NULL && tree != NULL);

	tree_root = tree_get_root(tree);

	for (cert_loop = 0; cert_loop < data->num; cert_loop++) {
		node = sslcert_create_node(&(certs[cert_loop]));
		if (node != NULL) {
			/* There is no problem creating the node
			 * add an entry for it in the root of the
			 * treeview .
			 */
			tree_link_node(tree, tree_root, node, false);
		}
	}

	data->tree = tree;

	return true;

}


static void sslcert_cleanup_session(struct sslcert_session_data *session)
{
	assert(session != NULL);

	if (session->url)
		nsurl_unref(session->url);

	free(session);
}



bool sslcert_reject(struct sslcert_session_data *session)
{
	session->cb(false, session->cbpw);
	sslcert_cleanup_session(session);
	return true;
}


/**
 * Handle acceptance of certificate
 */
bool sslcert_accept(struct sslcert_session_data *session)
{
	assert(session != NULL);

	urldb_set_cert_permissions(session->url, true);

	session->cb(true, session->cbpw);

	sslcert_cleanup_session(session);

	return true;
}