summaryrefslogtreecommitdiff
path: root/src/pluto/xauth/xauth_manager.c
blob: 2e57ccefad64ac66c1fdfcf6c035a279dfb1196a (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
/*
 * Copyright (C) 2010 Andreas Steffen
 * 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 "xauth_manager.h"

typedef struct private_xauth_manager_t private_xauth_manager_t;

/**
 * private data of xauth_manager
 */
struct private_xauth_manager_t {

	/**
	 * public functions
	 */
	xauth_manager_t public;

	/**
	 * list of registered secret providers
	 */
	linked_list_t *providers;

	/**
	 * list of registered secret verifiers
	 */
	linked_list_t *verifiers;
};

METHOD(xauth_manager_t, get_secret, bool,
	private_xauth_manager_t *this, connection_t *c, chunk_t *secret)
{
	xauth_provider_t *provider;
	enumerator_t *enumerator;
 	bool success = FALSE;

	*secret = chunk_empty;

	enumerator = this->providers->create_enumerator(this->providers);
	while (enumerator->enumerate(enumerator, &provider))
	{
		if (provider->get_secret(provider, c, secret))
		{
			success = TRUE;
			break;
		}
	}
	enumerator->destroy(enumerator);
	return success;
}

METHOD(xauth_manager_t, verify_secret, bool,
	private_xauth_manager_t *this, connection_t *c, chunk_t secret)
{
	xauth_verifier_t *verifier;
	enumerator_t *enumerator;
	bool success = FALSE;

	enumerator = this->verifiers->create_enumerator(this->verifiers);
	while (enumerator->enumerate(enumerator, &verifier))
	{
		if (verifier->verify_secret(verifier, c, secret))
		{
			success = TRUE;
			break;
		}
	}
	enumerator->destroy(enumerator);
	return success;
}

METHOD(xauth_manager_t, add_provider, void,
	private_xauth_manager_t *this,	xauth_provider_t *provider)
{
	this->providers->insert_last(this->providers, provider);
}

METHOD(xauth_manager_t, add_verifier, void,
	private_xauth_manager_t *this,	xauth_verifier_t *verifier)
{
	this->verifiers->insert_last(this->verifiers, verifier);
}

METHOD(xauth_manager_t, destroy, void,
	private_xauth_manager_t *this)
{
	this->providers->destroy_offset(this->providers,
									offsetof(xauth_provider_t, destroy));
	this->verifiers->destroy_offset(this->verifiers,
									offsetof(xauth_verifier_t, destroy));
	free(this);
}

/*
 * Described in header.
 */
xauth_manager_t *xauth_manager_create()
{
	private_xauth_manager_t *this;

	INIT(this,
		.public = {
			.get_secret = _get_secret,
			.verify_secret = _verify_secret,
			.add_provider = _add_provider,
			.add_verifier = _add_verifier,
			.destroy = _destroy,
		 }
	);

	this->providers = linked_list_create();
	this->verifiers = linked_list_create();

	return &this->public;
}