e6ecce0b5c6b5b1e93d3dc2e885dd4ee7fc187a2
[strongswan.git] / src / charon / sa / tasks / ike_cert_post.c
1 /*
2 * Copyright (C) 2008 Tobias Brunner
3 * Copyright (C) 2006-2009 Martin Willi
4 * Hochschule fuer Technik Rapperswil
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 */
16
17 #include "ike_cert_post.h"
18
19 #include <daemon.h>
20 #include <sa/ike_sa.h>
21 #include <encoding/payloads/cert_payload.h>
22 #include <encoding/payloads/certreq_payload.h>
23 #include <encoding/payloads/auth_payload.h>
24 #include <credentials/certificates/x509.h>
25
26
27 typedef struct private_ike_cert_post_t private_ike_cert_post_t;
28
29 /**
30 * Private members of a ike_cert_post_t task.
31 */
32 struct private_ike_cert_post_t {
33
34 /**
35 * Public methods and task_t interface.
36 */
37 ike_cert_post_t public;
38
39 /**
40 * Assigned IKE_SA.
41 */
42 ike_sa_t *ike_sa;
43
44 /**
45 * Are we the initiator?
46 */
47 bool initiator;
48 };
49
50 /**
51 * Generates the cert payload, if possible with "Hash and URL"
52 */
53 static cert_payload_t *build_cert_payload(private_ike_cert_post_t *this,
54 certificate_t *cert)
55 {
56 hasher_t *hasher;
57 identification_t *id;
58 chunk_t hash, encoded ;
59 enumerator_t *enumerator;
60 char *url;
61
62 if (!this->ike_sa->supports_extension(this->ike_sa, EXT_HASH_AND_URL))
63 {
64 return cert_payload_create_from_cert(cert);
65 }
66
67 hasher = lib->crypto->create_hasher(lib->crypto, HASH_SHA1);
68 if (!hasher)
69 {
70 DBG1(DBG_IKE, "unable to use hash-and-url: sha1 not supported");
71 return cert_payload_create_from_cert(cert);
72 }
73
74 encoded = cert->get_encoding(cert);
75 hasher->allocate_hash(hasher, encoded, &hash);
76 id = identification_create_from_encoding(ID_KEY_ID, hash);
77
78 enumerator = charon->credentials->create_cdp_enumerator(
79 charon->credentials, CERT_X509, id);
80 if (!enumerator->enumerate(enumerator, &url))
81 {
82 url = NULL;
83 }
84 enumerator->destroy(enumerator);
85
86 id->destroy(id);
87 chunk_free(&hash);
88 chunk_free(&encoded);
89 hasher->destroy(hasher);
90 if (url)
91 {
92 return cert_payload_create_from_hash_and_url(hash, url);
93 }
94 return cert_payload_create_from_cert(cert);
95 }
96
97 /**
98 * add certificates to message
99 */
100 static void build_certs(private_ike_cert_post_t *this, message_t *message)
101 {
102 peer_cfg_t *peer_cfg;
103 auth_payload_t *payload;
104
105 payload = (auth_payload_t*)message->get_payload(message, AUTHENTICATION);
106 peer_cfg = this->ike_sa->get_peer_cfg(this->ike_sa);
107 if (!peer_cfg || !payload || payload->get_auth_method(payload) == AUTH_PSK)
108 { /* no CERT payload for EAP/PSK */
109 return;
110 }
111
112 switch (peer_cfg->get_cert_policy(peer_cfg))
113 {
114 case CERT_NEVER_SEND:
115 break;
116 case CERT_SEND_IF_ASKED:
117 if (!this->ike_sa->has_condition(this->ike_sa, COND_CERTREQ_SEEN))
118 {
119 break;
120 }
121 /* FALL */
122 case CERT_ALWAYS_SEND:
123 {
124 cert_payload_t *payload;
125 enumerator_t *enumerator;
126 certificate_t *cert;
127 auth_rule_t type;
128 auth_cfg_t *auth;
129
130 auth = this->ike_sa->get_auth_cfg(this->ike_sa, TRUE);
131
132 /* get subject cert first, then issuing certificates */
133 cert = auth->get(auth, AUTH_RULE_SUBJECT_CERT);
134 if (!cert)
135 {
136 break;
137 }
138 payload = build_cert_payload(this, cert);
139 if (!payload)
140 {
141 break;
142 }
143 DBG1(DBG_IKE, "sending end entity cert \"%Y\"",
144 cert->get_subject(cert));
145 message->add_payload(message, (payload_t*)payload);
146
147 enumerator = auth->create_enumerator(auth);
148 while (enumerator->enumerate(enumerator, &type, &cert))
149 {
150 if (type == AUTH_RULE_IM_CERT)
151 {
152 payload = cert_payload_create_from_cert(cert);
153 if (payload)
154 {
155 DBG1(DBG_IKE, "sending issuer cert \"%Y\"",
156 cert->get_subject(cert));
157 message->add_payload(message, (payload_t*)payload);
158 }
159 }
160 }
161 enumerator->destroy(enumerator);
162 }
163 }
164 }
165
166 /**
167 * Implementation of task_t.process for initiator
168 */
169 static status_t build_i(private_ike_cert_post_t *this, message_t *message)
170 {
171 build_certs(this, message);
172
173 return NEED_MORE;
174 }
175
176 /**
177 * Implementation of task_t.process for responder
178 */
179 static status_t process_r(private_ike_cert_post_t *this, message_t *message)
180 {
181 return NEED_MORE;
182 }
183
184 /**
185 * Implementation of task_t.build for responder
186 */
187 static status_t build_r(private_ike_cert_post_t *this, message_t *message)
188 {
189 build_certs(this, message);
190
191 if (this->ike_sa->get_state(this->ike_sa) != IKE_ESTABLISHED)
192 { /* stay alive, we might have additional rounds with certs */
193 return NEED_MORE;
194 }
195 return SUCCESS;
196 }
197
198 /**
199 * Implementation of task_t.process for initiator
200 */
201 static status_t process_i(private_ike_cert_post_t *this, message_t *message)
202 {
203 if (this->ike_sa->get_state(this->ike_sa) != IKE_ESTABLISHED)
204 { /* stay alive, we might have additional rounds with CERTS */
205 return NEED_MORE;
206 }
207 return SUCCESS;
208 }
209
210 /**
211 * Implementation of task_t.get_type
212 */
213 static task_type_t get_type(private_ike_cert_post_t *this)
214 {
215 return IKE_CERT_POST;
216 }
217
218 /**
219 * Implementation of task_t.migrate
220 */
221 static void migrate(private_ike_cert_post_t *this, ike_sa_t *ike_sa)
222 {
223 this->ike_sa = ike_sa;
224 }
225
226 /**
227 * Implementation of task_t.destroy
228 */
229 static void destroy(private_ike_cert_post_t *this)
230 {
231 free(this);
232 }
233
234 /*
235 * Described in header.
236 */
237 ike_cert_post_t *ike_cert_post_create(ike_sa_t *ike_sa, bool initiator)
238 {
239 private_ike_cert_post_t *this = malloc_thing(private_ike_cert_post_t);
240
241 this->public.task.get_type = (task_type_t(*)(task_t*))get_type;
242 this->public.task.migrate = (void(*)(task_t*,ike_sa_t*))migrate;
243 this->public.task.destroy = (void(*)(task_t*))destroy;
244
245 if (initiator)
246 {
247 this->public.task.build = (status_t(*)(task_t*,message_t*))build_i;
248 this->public.task.process = (status_t(*)(task_t*,message_t*))process_i;
249 }
250 else
251 {
252 this->public.task.build = (status_t(*)(task_t*,message_t*))build_r;
253 this->public.task.process = (status_t(*)(task_t*,message_t*))process_r;
254 }
255
256 this->ike_sa = ike_sa;
257 this->initiator = initiator;
258
259 return &this->public;
260 }
261