summaryrefslogtreecommitdiff
path: root/src/libcharon/plugins/sql/sql_cred.c
blob: 117eec9210229aa9ead7a8b551322afccb39b96a (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
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
/*
 * Copyright (C) 2010 Tobias Brunner
 * Copyright (C) 2008 Martin Willi
 * 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.
 */

#include <string.h>

#include "sql_cred.h"

#include <daemon.h>

typedef struct private_sql_cred_t private_sql_cred_t;

/**
 * Private data of an sql_cred_t object
 */
struct private_sql_cred_t {

	/**
	 * Public part
	 */
	sql_cred_t public;

	/**
	 * database connection
	 */
	database_t *db;
};


/**
 * enumerator over private keys
 */
typedef struct {
	/** implements enumerator */
	enumerator_t public;
	/** inner SQL enumerator */
	enumerator_t *inner;
	/** currently enumerated private key */
	private_key_t *current;
} private_enumerator_t;

METHOD(enumerator_t, private_enumerator_enumerate, bool,
	   private_enumerator_t *this, private_key_t **key)
{
	chunk_t blob;
	int type;

	DESTROY_IF(this->current);
	while (this->inner->enumerate(this->inner, &type, &blob))
	{
		this->current = lib->creds->create(lib->creds, CRED_PRIVATE_KEY, type,
										   BUILD_BLOB_PEM, blob,
										   BUILD_END);
		if (this->current)
		{
			*key = this->current;
			return TRUE;
		}
	}
	this->current = NULL;
	return FALSE;
}

METHOD(enumerator_t, private_enumerator_destroy, void,
	   private_enumerator_t *this)
{
	DESTROY_IF(this->current);
	this->inner->destroy(this->inner);
	free(this);
}

METHOD(credential_set_t, create_private_enumerator, enumerator_t*,
	   private_sql_cred_t *this, key_type_t type, identification_t *id)
{
	private_enumerator_t *e;

	INIT(e,
		.public = {
			.enumerate = (void*)_private_enumerator_enumerate,
			.destroy = _private_enumerator_destroy,
		},
	);
	if (id && id->get_type(id) != ID_ANY)
	{
		e->inner = this->db->query(this->db,
				"SELECT p.type, p.data FROM private_keys AS p "
				"JOIN private_key_identity AS pi ON p.id = pi.private_key "
				"JOIN identities AS i ON pi.identity = i.id "
				"WHERE i.type = ? AND i.data = ? AND (? OR p.type = ?)",
				DB_INT, id->get_type(id), DB_BLOB, id->get_encoding(id),
				DB_INT, type == KEY_ANY, DB_INT, type,
				DB_INT, DB_BLOB);
	}
	else
	{
		e->inner = this->db->query(this->db,
				"SELECT type, data FROM private_keys WHERE (? OR type = ?)",
				DB_INT, type == KEY_ANY, DB_INT, type,
				DB_INT, DB_BLOB);
	}
	if (!e->inner)
	{
		free(e);
		return NULL;
	}
	return &e->public;
}


/**
 * enumerator over certificates
 */
typedef struct {
	/** implements enumerator */
	enumerator_t public;
	/** inner SQL enumerator */
	enumerator_t *inner;
	/** currently enumerated cert */
	certificate_t *current;
} cert_enumerator_t;

METHOD(enumerator_t, cert_enumerator_enumerate, bool,
	   cert_enumerator_t *this, certificate_t **cert)
{
	chunk_t blob;
	int type;

	DESTROY_IF(this->current);
	while (this->inner->enumerate(this->inner, &type, &blob))
	{
		this->current = lib->creds->create(lib->creds, CRED_CERTIFICATE, type,
										   BUILD_BLOB_PEM, blob,
										   BUILD_END);
		if (this->current)
		{
			*cert = this->current;
			return TRUE;
		}
	}
	this->current = NULL;
	return FALSE;
}

METHOD(enumerator_t, cert_enumerator_destroy, void,
	   cert_enumerator_t *this)
{
	DESTROY_IF(this->current);
	this->inner->destroy(this->inner);
	free(this);
}

METHOD(credential_set_t, create_cert_enumerator, enumerator_t*,
	   private_sql_cred_t *this, certificate_type_t cert, key_type_t key,
	   identification_t *id, bool trusted)
{
	cert_enumerator_t *e;

	INIT(e,
		.public = {
			.enumerate = (void*)_cert_enumerator_enumerate,
			.destroy = _cert_enumerator_destroy,
		},
	);
	if (id && id->get_type(id) != ID_ANY)
	{
		e->inner = this->db->query(this->db,
				"SELECT c.type, c.data FROM certificates AS c "
				"JOIN certificate_identity AS ci ON c.id = ci.certificate "
				"JOIN identities AS i ON ci.identity = i.id "
				"WHERE i.type = ? AND i.data = ? AND "
				"(? OR c.type = ?) AND (? OR c.keytype = ?)",
				DB_INT, id->get_type(id), DB_BLOB, id->get_encoding(id),
				DB_INT, cert == CERT_ANY, DB_INT, cert,
				DB_INT, key == KEY_ANY, DB_INT, key,
				DB_INT, DB_BLOB);
	}
	else
	{
		e->inner = this->db->query(this->db,
				"SELECT type, data FROM certificates WHERE "
				"(? OR type = ?) AND (? OR keytype = ?)",
				DB_INT, cert == CERT_ANY, DB_INT, cert,
				DB_INT, key == KEY_ANY, DB_INT, key,
				DB_INT, DB_BLOB);
	}
	if (!e->inner)
	{
		free(e);
		return NULL;
	}
	return &e->public;
}


/**
 * enumerator over shared keys
 */
typedef struct {
	/** implements enumerator */
	enumerator_t public;
	/** inner SQL enumerator */
	enumerator_t *inner;
	/** own identity */
	identification_t *me;
	/** remote identity */
	identification_t *other;
	/** currently enumerated private key */
	shared_key_t *current;
} shared_enumerator_t;

METHOD(enumerator_t, shared_enumerator_enumerate, bool,
	   shared_enumerator_t *this, shared_key_t **shared,
	   id_match_t *me, id_match_t *other)
{
	chunk_t blob;
	int type;

	DESTROY_IF(this->current);
	while (this->inner->enumerate(this->inner, &type, &blob))
	{
		this->current = shared_key_create(type, chunk_clone(blob));
		if (this->current)
		{
			*shared = this->current;
			if (me)
			{
				*me = this->me ? ID_MATCH_PERFECT : ID_MATCH_ANY;
			}
			if (other)
			{
				*other = this->other ? ID_MATCH_PERFECT : ID_MATCH_ANY;
			}
			return TRUE;
		}
	}
	this->current = NULL;
	return FALSE;
}

METHOD(enumerator_t, shared_enumerator_destroy, void,
	   shared_enumerator_t *this)
{
	DESTROY_IF(this->current);
	this->inner->destroy(this->inner);
	free(this);
}

METHOD(credential_set_t, create_shared_enumerator, enumerator_t*,
	   private_sql_cred_t *this, shared_key_type_t type,
	   identification_t *me, identification_t *other)
{
	shared_enumerator_t *e;

	INIT(e,
		.public = {
			.enumerate = (void*)_shared_enumerator_enumerate,
			.destroy = _shared_enumerator_destroy,
		},
		.me = me,
		.other = other,
	);
	if (!me && !other)
	{
		e->inner = this->db->query(this->db,
				"SELECT type, data FROM shared_secrets WHERE  (? OR type = ?)",
				DB_INT, type == SHARED_ANY, DB_INT, type,
				DB_INT, DB_BLOB);
	}
	else if (me && other)
	{
		e->inner = this->db->query(this->db,
				"SELECT s.type, s.data FROM shared_secrets AS s "
				"JOIN shared_secret_identity AS sm ON s.id = sm.shared_secret "
				"JOIN identities AS m ON sm.identity = m.id "
				"JOIN shared_secret_identity AS so ON s.id = so.shared_secret "
				"JOIN identities AS o ON so.identity = o.id "
				"WHERE m.type = ? AND m.data = ? AND o.type = ? AND o.data = ? "
				"AND (? OR s.type = ?)",
				DB_INT, me->get_type(me), DB_BLOB, me->get_encoding(me),
				DB_INT, other->get_type(other), DB_BLOB, other->get_encoding(other),
				DB_INT, type == SHARED_ANY, DB_INT, type,
				DB_INT, DB_BLOB);
	}
	else
	{
		identification_t *id = me ? me : other;

		e->inner = this->db->query(this->db,
				"SELECT s.type, s.data FROM shared_secrets AS s "
				"JOIN shared_secret_identity AS si ON s.id = si.shared_secret "
				"JOIN identities AS i ON si.identity = i.id "
				"WHERE i.type = ? AND i.data = ? AND (? OR s.type = ?)",
				DB_INT, id->get_type(id), DB_BLOB, id->get_encoding(id),
				DB_INT, type == SHARED_ANY, DB_INT, type,
				DB_INT, DB_BLOB);
	}
	if (!e->inner)
	{
		free(e);
		return NULL;
	}
	return &e->public;
}


/**
 * enumerator over CDPs
 */
typedef struct {
	/** implements enumerator_t */
	enumerator_t public;
	/** inner SQL enumerator */
	enumerator_t *inner;
	/** currently enumerated string */
	char *current;
} cdp_enumerator_t;

/**
 * types of CDPs
 */
typedef enum {
	/** any available CDP */
	CDP_TYPE_ANY = 0,
	/** CRL */
	CDP_TYPE_CRL,
	/** OCSP Responder */
	CDP_TYPE_OCSP,
} cdp_type_t;

METHOD(enumerator_t, cdp_enumerator_enumerate, bool,
	   cdp_enumerator_t *this, char **uri)
{
	char *text;

	free(this->current);
	while (this->inner->enumerate(this->inner, &text))
	{
		*uri = this->current = strdup(text);
		return TRUE;
	}
	this->current = NULL;
	return FALSE;
}

METHOD(enumerator_t, cdp_enumerator_destroy, void,
	   cdp_enumerator_t *this)
{
	free(this->current);
	this->inner->destroy(this->inner);
	free(this);
}

METHOD(credential_set_t, create_cdp_enumerator, enumerator_t*,
	   private_sql_cred_t *this, certificate_type_t type, identification_t *id)
{
	cdp_enumerator_t *e;
	cdp_type_t cdp_type;

	switch (type)
	{	/* we serve CRLs and OCSP responders */
		case CERT_X509_CRL:
			cdp_type = CDP_TYPE_CRL;
			break;
		case CERT_X509_OCSP_RESPONSE:
			cdp_type = CDP_TYPE_OCSP;
			break;
		case CERT_ANY:
			cdp_type = CDP_TYPE_ANY;
			break;
		default:
			return NULL;
	}
	INIT(e,
		.public = {
			.enumerate = (void*)_cdp_enumerator_enumerate,
			.destroy = _cdp_enumerator_destroy,
		},
	);
	if (id && id->get_type(id) != ID_ANY)
	{
		e->inner = this->db->query(this->db,
				"SELECT dp.uri FROM certificate_distribution_points AS dp "
				"JOIN certificate_authorities AS ca ON ca.id = dp.ca "
				"JOIN certificates AS c ON c.id = ca.certificate "
				"JOIN certificate_identity AS ci ON c.id = ci.certificate "
				"JOIN identities AS i ON ci.identity = i.id "
				"WHERE i.type = ? AND i.data = ? AND (? OR dp.type = ?)",
				DB_INT, id->get_type(id), DB_BLOB, id->get_encoding(id),
				DB_INT, cdp_type == CDP_TYPE_ANY, DB_INT, cdp_type,
				DB_TEXT);
	}
	else
	{
		e->inner = this->db->query(this->db,
				"SELECT dp.uri FROM certificate_distribution_points AS dp "
				"WHERE (? OR dp.type = ?)",
				DB_INT, cdp_type == CDP_TYPE_ANY, DB_INT, cdp_type,
				DB_TEXT);
	}
	if (!e->inner)
	{
		free(e);
		return NULL;
	}
	return &e->public;
}

METHOD(credential_set_t, cache_cert, void,
	   private_sql_cred_t *this, certificate_t *cert)
{
	/* TODO: implement CRL caching to database */
}

METHOD(sql_cred_t, destroy, void,
	   private_sql_cred_t *this)
{
	free(this);
}

/**
 * Described in header.
 */
sql_cred_t *sql_cred_create(database_t *db)
{
	private_sql_cred_t *this;

	INIT(this,
		.public = {
			.set = {
				.create_private_enumerator = _create_private_enumerator,
				.create_cert_enumerator = _create_cert_enumerator,
				.create_shared_enumerator = _create_shared_enumerator,
				.create_cdp_enumerator = _create_cdp_enumerator,
				.cache_cert = _cache_cert,
			},
			.destroy = _destroy,
		},
		.db = db,
	);

	return &this->public;
}