aboutsummaryrefslogtreecommitdiffstats
path: root/src/libcharon/sa/ikev1/tasks/isakmp_cert_pre.c
blob: d48484f090a4253cdbf3d37963acd09995609c97 (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
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
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
/*
 * Copyright (C) 2011 Martin Willi
 * Copyright (C) 2011 revosec AG
 *
 * 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 "isakmp_cert_pre.h"

#include <daemon.h>
#include <sa/ike_sa.h>
#include <encoding/payloads/cert_payload.h>
#include <encoding/payloads/sa_payload.h>
#include <encoding/payloads/certreq_payload.h>
#include <credentials/certificates/x509.h>


typedef struct private_isakmp_cert_pre_t private_isakmp_cert_pre_t;

/**
 * Private members of a isakmp_cert_pre_t task.
 */
struct private_isakmp_cert_pre_t {

	/**
	 * Public methods and task_t interface.
	 */
	isakmp_cert_pre_t public;

	/**
	 * Assigned IKE_SA.
	 */
	ike_sa_t *ike_sa;

	/**
	 * Are we the initiator?
	 */
	bool initiator;

	/**
	 * Send certificate requests?
	 */
	bool send_req;

	/** next message we expect */
	enum {
		CR_SA,
		CR_KE,
		CR_AUTH,
	} state;
};

/**
 * Find the CA certificate for a given certreq payload
 */
static certificate_t* find_certificate(private_isakmp_cert_pre_t *this,
									   certreq_payload_t *certreq)
{
	identification_t *id;
	certificate_t *cert;

	if (certreq->get_cert_type(certreq) != CERT_X509)
	{
		DBG1(DBG_IKE, "%N CERTREQ not supported - ignored",
			 certificate_type_names, certreq->get_cert_type(certreq));
		return NULL;
	}
	id = certreq->get_dn(certreq);
	if (!id)
	{
		DBG1(DBG_IKE, "ignoring certificate request without data",
			 certificate_type_names, certreq->get_cert_type(certreq));
		return NULL;
	}
	cert = lib->credmgr->get_cert(lib->credmgr, CERT_X509, KEY_ANY, id, TRUE);
	if (cert)
	{
		DBG1(DBG_IKE, "received cert request for '%Y'",
			 cert->get_subject(cert));
	}
	else
	{
		DBG1(DBG_IKE, "received cert request for unknown ca '%Y'", id);
	}
	id->destroy(id);

	return cert;
}

/**
 * read certificate requests
 */
static void process_certreqs(private_isakmp_cert_pre_t *this, message_t *message)
{
	enumerator_t *enumerator;
	payload_t *payload;
	auth_cfg_t *auth;

	auth = this->ike_sa->get_auth_cfg(this->ike_sa, TRUE);

	enumerator = message->create_payload_enumerator(message);
	while (enumerator->enumerate(enumerator, &payload))
	{
		switch (payload->get_type(payload))
		{
			case CERTIFICATE_REQUEST_V1:
			{
				certificate_t *cert;

				this->ike_sa->set_condition(this->ike_sa,
											COND_CERTREQ_SEEN, TRUE);
				cert = find_certificate(this, (certreq_payload_t*)payload);
				if (cert)
				{
					auth->add(auth, AUTH_RULE_CA_CERT, cert);
				}
				break;
			}
			default:
				break;
		}
	}
	enumerator->destroy(enumerator);
}

/**
 * Import receuved certificates
 */
static void process_certs(private_isakmp_cert_pre_t *this, message_t *message)
{
	enumerator_t *enumerator;
	payload_t *payload;
	auth_cfg_t *auth;
	bool first = TRUE;

	auth = this->ike_sa->get_auth_cfg(this->ike_sa, FALSE);

	enumerator = message->create_payload_enumerator(message);
	while (enumerator->enumerate(enumerator, &payload))
	{
		if (payload->get_type(payload) == CERTIFICATE_V1)
		{
			cert_payload_t *cert_payload;
			cert_encoding_t encoding;
			certificate_t *cert;

			cert_payload = (cert_payload_t*)payload;
			encoding = cert_payload->get_cert_encoding(cert_payload);

			switch (encoding)
			{
				case ENC_X509_SIGNATURE:
				{
					cert = cert_payload->get_cert(cert_payload);
					if (cert)
					{
						if (first)
						{	/* the first is an end entity certificate */
							DBG1(DBG_IKE, "received end entity cert \"%Y\"",
								 cert->get_subject(cert));
							auth->add(auth, AUTH_HELPER_SUBJECT_CERT, cert);
							first = FALSE;
						}
						else
						{
							DBG1(DBG_IKE, "received issuer cert \"%Y\"",
								 cert->get_subject(cert));
							auth->add(auth, AUTH_HELPER_IM_CERT, cert);
						}
					}
					break;
				}
				case ENC_CRL:
					cert = cert_payload->get_cert(cert_payload);
					if (cert)
					{
						DBG1(DBG_IKE, "received CRL \"%Y\"",
							 cert->get_subject(cert));
						auth->add(auth, AUTH_HELPER_REVOCATION_CERT, cert);
					}
					break;
				case ENC_PKCS7_WRAPPED_X509:
				case ENC_PGP:
				case ENC_DNS_SIGNED_KEY:
				case ENC_KERBEROS_TOKEN:
				case ENC_ARL:
				case ENC_SPKI:
				case ENC_X509_ATTRIBUTE:
				case ENC_RAW_RSA_KEY:
				case ENC_X509_HASH_AND_URL_BUNDLE:
				case ENC_OCSP_CONTENT:
				default:
					DBG1(DBG_ENC, "certificate encoding %N not supported",
						 cert_encoding_names, encoding);
			}
		}
	}
	enumerator->destroy(enumerator);
}

/**
 * Add the subject of a CA certificate a message
 */
static void add_certreq(private_isakmp_cert_pre_t *this, message_t *message,
						certificate_t *cert)
{
	if (cert->get_type(cert) == CERT_X509)
	{
		x509_t *x509 = (x509_t*)cert;

		if (x509->get_flags(x509) & X509_CA)
		{
			DBG1(DBG_IKE, "sending cert request for \"%Y\"",
				 cert->get_subject(cert));
			message->add_payload(message, (payload_t*)
							certreq_payload_create_dn(cert->get_subject(cert)));
		}
	}
}

/**
 * Add auth_cfg's CA certificates to the certificate request
 */
static void add_certreqs(private_isakmp_cert_pre_t *this,
						 auth_cfg_t *auth, message_t *message)
{
	enumerator_t *enumerator;
	auth_rule_t type;
	void *value;

	enumerator = auth->create_enumerator(auth);
	while (enumerator->enumerate(enumerator, &type, &value))
	{
		switch (type)
		{
			case AUTH_RULE_CA_CERT:
				add_certreq(this, message, (certificate_t*)value);
				break;
			default:
				break;
		}
	}
	enumerator->destroy(enumerator);
}

/**
 * Build certificate requests
 */
static void build_certreqs(private_isakmp_cert_pre_t *this, message_t *message)
{
	enumerator_t *enumerator;
	ike_cfg_t *ike_cfg;
	peer_cfg_t *peer_cfg;
	certificate_t *cert;
	auth_cfg_t *auth;

	ike_cfg = this->ike_sa->get_ike_cfg(this->ike_sa);
	if (!ike_cfg->send_certreq(ike_cfg))
	{
		return;
	}
	/* check if we require a specific CA for that peer */
	peer_cfg = this->ike_sa->get_peer_cfg(this->ike_sa);
	if (peer_cfg)
	{
		enumerator = peer_cfg->create_auth_cfg_enumerator(peer_cfg, FALSE);
		if (enumerator->enumerate(enumerator, &auth))
		{
			add_certreqs(this, auth, message);
		}
		enumerator->destroy(enumerator);
	}
	if (!message->get_payload(message, CERTIFICATE_REQUEST_V1))
	{
		/* otherwise add all trusted CA certificates */
		enumerator = lib->credmgr->create_cert_enumerator(lib->credmgr,
												CERT_ANY, KEY_ANY, NULL, TRUE);
		while (enumerator->enumerate(enumerator, &cert))
		{
			add_certreq(this, message, cert);
		}
		enumerator->destroy(enumerator);
	}
}

/**
 * Check if we actually use certificates for authentication
 */
static bool use_certs(private_isakmp_cert_pre_t *this, message_t *message)
{
	enumerator_t *enumerator;
	payload_t *payload;
	bool use = FALSE;

	enumerator = message->create_payload_enumerator(message);
	while (enumerator->enumerate(enumerator, &payload))
	{
		if (payload->get_type(payload) == SECURITY_ASSOCIATION_V1)
		{
			sa_payload_t *sa_payload = (sa_payload_t*)payload;

			switch (sa_payload->get_auth_method(sa_payload))
			{
				case AUTH_HYBRID_INIT_RSA:
				case AUTH_HYBRID_RESP_RSA:
					if (!this->initiator)
					{
						this->send_req = FALSE;
					}
					/* FALL */
				case AUTH_RSA:
				case AUTH_ECDSA_256:
				case AUTH_ECDSA_384:
				case AUTH_ECDSA_521:
				case AUTH_XAUTH_INIT_RSA:
				case AUTH_XAUTH_RESP_RSA:
					use = TRUE;
					break;
				default:
					break;
			}
			break;
		}
	}
	enumerator->destroy(enumerator);

	return use;
}

/**
 * Check if we should send a certificate request
 */
static bool send_certreq(private_isakmp_cert_pre_t *this)
{
	enumerator_t *enumerator;
	peer_cfg_t *peer_cfg;
	auth_cfg_t *auth;
	bool req = FALSE;
	auth_class_t class;

	peer_cfg = this->ike_sa->get_peer_cfg(this->ike_sa);
	if (peer_cfg)
	{
		enumerator = peer_cfg->create_auth_cfg_enumerator(peer_cfg, FALSE);
		if (enumerator->enumerate(enumerator, &auth))
		{
			class = (intptr_t)auth->get(auth, AUTH_RULE_AUTH_CLASS);
			if (class == AUTH_CLASS_PUBKEY)
			{
				req = TRUE;
			}
		}
		enumerator->destroy(enumerator);
	}
	return req;
}

METHOD(task_t, build_i, status_t,
	private_isakmp_cert_pre_t *this, message_t *message)
{
	switch (message->get_exchange_type(message))
	{
		case ID_PROT:
			if (this->state == CR_AUTH)
			{
				build_certreqs(this, message);
			}
			return NEED_MORE;
		case AGGRESSIVE:
			if (this->state == CR_SA)
			{
				if (send_certreq(this))
				{
					build_certreqs(this, message);
				}
			}
			return NEED_MORE;
		default:
			return FAILED;
	}
}

METHOD(task_t, process_r, status_t,
	private_isakmp_cert_pre_t *this, message_t *message)
{
	switch (message->get_exchange_type(message))
	{
		case ID_PROT:
		{
			switch (this->state)
			{
				case CR_SA:
					if (!use_certs(this, message))
					{
						return SUCCESS;
					}
					return NEED_MORE;
				case CR_KE:
					process_certreqs(this, message);
					return NEED_MORE;
				case CR_AUTH:
					process_certreqs(this, message);
					process_certs(this, message);
					return SUCCESS;
				default:
					return FAILED;
			}
		}
		case AGGRESSIVE:
		{
			switch (this->state)
			{
				case CR_SA:
					if (!use_certs(this, message))
					{
						return SUCCESS;
					}
					process_certreqs(this, message);
					return NEED_MORE;
				case CR_AUTH:
					process_certs(this, message);
					return SUCCESS;
				default:
					return FAILED;
			}
		}
		default:
			return FAILED;
	}
}

METHOD(task_t, build_r, status_t,
	private_isakmp_cert_pre_t *this, message_t *message)
{
	switch (message->get_exchange_type(message))
	{
		case ID_PROT:
			switch (this->state)
			{
				case CR_SA:
					this->state = CR_KE;
					return NEED_MORE;
				case CR_KE:
					if (this->send_req)
					{
						build_certreqs(this, message);
					}
					this->state = CR_AUTH;
					return NEED_MORE;
				case CR_AUTH:
					return NEED_MORE;
				default:
					return FAILED;
			}
		case AGGRESSIVE:
			switch (this->state)
			{
				case CR_SA:
					if (this->send_req)
					{
						build_certreqs(this, message);
					}
					this->state = CR_AUTH;
					return NEED_MORE;
				case CR_AUTH:
					return SUCCESS;
				default:
					return FAILED;
			}
		default:
			return FAILED;
	}
}

METHOD(task_t, process_i, status_t,
	private_isakmp_cert_pre_t *this, message_t *message)
{
	switch (message->get_exchange_type(message))
	{
		case ID_PROT:
		{
			switch (this->state)
			{
				case CR_SA:
					if (!use_certs(this, message))
					{
						return SUCCESS;
					}
					this->state = CR_KE;
					return NEED_MORE;
				case CR_KE:
					process_certreqs(this, message);
					this->state = CR_AUTH;
					return NEED_MORE;
				case CR_AUTH:
					process_certs(this, message);
					return SUCCESS;
				default:
					return FAILED;
			}
			break;
		}
		case AGGRESSIVE:
		{
			if (!use_certs(this, message))
			{
				return SUCCESS;
			}
			process_certreqs(this, message);
			process_certs(this, message);
			this->state = CR_AUTH;
			return SUCCESS;
		}
		default:
			return FAILED;
	}
}

METHOD(task_t, get_type, task_type_t,
	private_isakmp_cert_pre_t *this)
{
	return TASK_ISAKMP_CERT_PRE;
}

METHOD(task_t, migrate, void,
	private_isakmp_cert_pre_t *this, ike_sa_t *ike_sa)
{
	this->ike_sa = ike_sa;
	this->state = CR_SA;
	this->send_req = TRUE;
}

METHOD(task_t, destroy, void,
	private_isakmp_cert_pre_t *this)
{
	free(this);
}

/*
 * Described in header.
 */
isakmp_cert_pre_t *isakmp_cert_pre_create(ike_sa_t *ike_sa, bool initiator)
{
	private_isakmp_cert_pre_t *this;

	INIT(this,
		.public = {
			.task = {
				.get_type = _get_type,
				.migrate = _migrate,
				.destroy = _destroy,
			},
		},
		.ike_sa = ike_sa,
		.initiator = initiator,
		.state = CR_SA,
		.send_req = TRUE,
	);
	if (initiator)
	{
		this->public.task.build = _build_i;
		this->public.task.process = _process_i;
	}
	else
	{
		this->public.task.build = _build_r;
		this->public.task.process = _process_r;
	}
	return &this->public;
}