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
|
/*
* Copyright (C) 2008 Martin Willi
* Copyright (C) 2008 Philip Boetschi, Adrian Doerig
* Hochschule fuer Technik Rapperswil
*
* This program 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; either version 2 of the License, or (at your
* option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
*
* This program 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.
*/
#define _GNU_SOURCE
#include <string.h>
#include "peer_controller.h"
#include <library.h>
#include <debug.h>
#include <asn1/asn1.h>
#include <asn1/oid.h>
#include <utils/identification.h>
#include <credentials/keys/public_key.h>
typedef struct private_peer_controller_t private_peer_controller_t;
/**
* private data of the peer_controller
*/
struct private_peer_controller_t {
/**
* public functions
*/
peer_controller_t public;
/**
* active user session
*/
user_t *user;
/**
* underlying database
*/
database_t *db;
};
/**
* list the configured peer configs
*/
static void list(private_peer_controller_t *this, request_t *request)
{
enumerator_t *query;
query = this->db->query(this->db,
"SELECT id, alias, keyid FROM peer WHERE user = ? ORDER BY alias",
DB_UINT, this->user->get_user(this->user),
DB_UINT, DB_TEXT, DB_BLOB);
if (query)
{
u_int id;
char *alias;
chunk_t keyid;
identification_t *identifier;
while (query->enumerate(query, &id, &alias, &keyid))
{
request->setf(request, "peers.%d.alias=%s", id, alias);
identifier = identification_create_from_encoding(ID_KEY_ID, keyid);
request->setf(request, "peers.%d.identifier=%Y", id, identifier);
identifier->destroy(identifier);
}
query->destroy(query);
}
request->render(request, "templates/peer/list.cs");
}
/**
* verify a peer alias
*/
static bool verify_alias(private_peer_controller_t *this, request_t *request,
char *alias)
{
if (!alias || *alias == '\0')
{
request->setf(request, "error=Alias is missing.");
return FALSE;
}
while (*alias != '\0')
{
switch (*alias)
{
case 'a' ... 'z':
case 'A' ... 'Z':
case '0' ... '9':
case '-':
case '_':
case '@':
case '.':
alias++;
continue;
default:
request->setf(request, "error=Alias invalid, "
"valid characters: A-Z a-z 0-9 - _ @ .");
return FALSE;
}
}
return TRUE;
}
/**
* parse and verify a public key
*/
static bool parse_public_key(private_peer_controller_t *this,
request_t *request, char *public_key,
chunk_t *encoding, chunk_t *keyid)
{
public_key_t *public;
identification_t *id;
if (!public_key || *public_key == '\0')
{
request->setf(request, "error=Public key is missing.");
return FALSE;
}
public = lib->creds->create(lib->creds, CRED_PUBLIC_KEY, KEY_ANY,
BUILD_BLOB_ASN1_PEM, public_key,
BUILD_END);
if (!public)
{
request->setf(request, "error=Parsing public key failed.");
return FALSE;
}
/* TODO: use get_encoding() with an encoding type */
*encoding = asn1_wrap(ASN1_SEQUENCE, "cm",
asn1_algorithmIdentifier(OID_RSA_ENCRYPTION),
asn1_bitstring("m", public->get_encoding(public)));
id = public->get_id(public, ID_PUBKEY_SHA1);
*keyid = chunk_clone(id->get_encoding(id));
public->destroy(public);
return TRUE;
}
/**
* register a new peer
*/
static void add(private_peer_controller_t *this, request_t *request)
{
char *alias = "", *public_key = "";
if (request->get_query_data(request, "back"))
{
return request->redirect(request, "peer/list");
}
while (request->get_query_data(request, "add"))
{
chunk_t encoding, keyid;
alias = request->get_query_data(request, "alias");
public_key = request->get_query_data(request, "public_key");
if (!verify_alias(this, request, alias))
{
break;
}
if (!parse_public_key(this, request, public_key, &encoding, &keyid))
{
break;
}
if (this->db->execute(this->db, NULL,
"INSERT INTO peer (user, alias, public_key, keyid) "
"VALUES (?, ?, ?, ?)",
DB_UINT, this->user->get_user(this->user),
DB_TEXT, alias, DB_BLOB, encoding,
DB_BLOB, keyid) <= 0)
{
request->setf(request, "error=Peer already exists.");
free(keyid.ptr);
free(encoding.ptr);
break;
}
free(keyid.ptr);
free(encoding.ptr);
return request->redirect(request, "peer/list");
}
request->set(request, "alias", alias);
request->set(request, "public_key", public_key);
return request->render(request, "templates/peer/add.cs");
}
/**
* pem encode a public key into an allocated string
*/
char* pem_encode(chunk_t der)
{
static const char *begin = "-----BEGIN PUBLIC KEY-----\n";
static const char *end = "-----END PUBLIC KEY-----";
size_t len;
char *pem;
chunk_t base64;
int i = 0;
base64 = chunk_to_base64(der, NULL);
len = strlen(begin) + base64.len + base64.len/64 + strlen(end) + 2;
pem = malloc(len + 1);
strcpy(pem, begin);
do
{
strncat(pem, base64.ptr + i, 64);
strcat(pem, "\n");
i += 64;
}
while (i < base64.len - 2);
strcat(pem, end);
free(base64.ptr);
return pem;
}
/**
* edit a peer
*/
static void edit(private_peer_controller_t *this, request_t *request, int id)
{
char *alias = "", *public_key = "", *pem;
chunk_t encoding, keyid;
if (request->get_query_data(request, "back"))
{
return request->redirect(request, "peer/list");
}
if (request->get_query_data(request, "delete"))
{
this->db->execute(this->db, NULL,
"DELETE FROM peer WHERE id = ? AND user = ?",
DB_INT, id, DB_UINT, this->user->get_user(this->user));
return request->redirect(request, "peer/list");
}
if (request->get_query_data(request, "save"))
{
while (TRUE)
{
alias = request->get_query_data(request, "alias");
public_key = request->get_query_data(request, "public_key");
if (!verify_alias(this, request, alias))
{
break;
}
if (!parse_public_key(this, request, public_key, &encoding, &keyid))
{
break;
}
if (this->db->execute(this->db, NULL,
"UPDATE peer SET alias = ?, public_key = ?, keyid = ? "
"WHERE id = ? AND user = ?",
DB_TEXT, alias, DB_BLOB, encoding, DB_BLOB, keyid,
DB_INT, id, DB_UINT, this->user->get_user(this->user)) < 0)
{
request->setf(request, "error=Peer already exists.");
free(keyid.ptr);
free(encoding.ptr);
break;
}
free(keyid.ptr);
free(encoding.ptr);
return request->redirect(request, "peer/list");
}
}
else
{
enumerator_t *query = this->db->query(this->db,
"SELECT alias, public_key FROM peer WHERE id = ? AND user = ?",
DB_INT, id, DB_UINT, this->user->get_user(this->user),
DB_TEXT, DB_BLOB);
if (query && query->enumerate(query, &alias, &encoding))
{
alias = strdupa(alias);
pem = pem_encode(encoding);
public_key = strdupa(pem);
free(pem);
}
else
{
return request->redirect(request, "peer/list");
}
DESTROY_IF(query);
}
request->set(request, "alias", alias);
request->set(request, "public_key", public_key);
return request->render(request, "templates/peer/edit.cs");
}
/**
* delete a peer from the database
*/
static void delete(private_peer_controller_t *this, request_t *request, int id)
{
this->db->execute(this->db, NULL,
"DELETE FROM peer WHERE id = ? AND user = ?",
DB_INT, id, DB_UINT, this->user->get_user(this->user));
}
/**
* Implementation of controller_t.get_name
*/
static char* get_name(private_peer_controller_t *this)
{
return "peer";
}
/**
* Implementation of controller_t.handle
*/
static void handle(private_peer_controller_t *this, request_t *request,
char *action, char *idstr)
{
if (action)
{
int id = 0;
if (idstr)
{
id = atoi(idstr);
}
if (streq(action, "list"))
{
return list(this, request);
}
else if (streq(action, "add"))
{
return add(this, request);
}
else if (streq(action, "edit") && id)
{
return edit(this, request, id);
}
else if (streq(action, "delete") && id)
{
delete(this, request, id);
}
}
request->redirect(request, "peer/list");
}
/**
* Implementation of controller_t.destroy
*/
static void destroy(private_peer_controller_t *this)
{
free(this);
}
/*
* see header file
*/
controller_t *peer_controller_create(user_t *user, database_t *db)
{
private_peer_controller_t *this= malloc_thing(private_peer_controller_t);
this->public.controller.get_name = (char*(*)(controller_t*))get_name;
this->public.controller.handle = (void(*)(controller_t*, request_t*, char*, char*, char*, char*, char*))handle;
this->public.controller.destroy = (void(*)(controller_t*))destroy;
this->user = user;
this->db = db;
return &this->public.controller;
}
|