Merge branch 'ikev1-clean' into ikev1-master
[strongswan.git] / src / conftest / hooks / rebuild_auth.c
1 /*
2 * Copyright (C) 2010 Martin Willi
3 * Copyright (C) 2010 revosec AG
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include "hook.h"
17
18 #include <sa/ikev2/keymat_v2.h>
19 #include <encoding/generator.h>
20 #include <encoding/payloads/nonce_payload.h>
21 #include <encoding/payloads/auth_payload.h>
22 #include <encoding/payloads/id_payload.h>
23
24 typedef struct private_rebuild_auth_t private_rebuild_auth_t;
25
26 /**
27 * Private data of an rebuild_auth_t object.
28 */
29 struct private_rebuild_auth_t {
30
31 /**
32 * Implements the hook_t interface.
33 */
34 hook_t hook;
35
36 /**
37 * Our IKE_SA_INIT data, required to rebuild AUTH
38 */
39 chunk_t ike_init;
40
41 /**
42 * Received NONCE, required to rebuild AUTH
43 */
44 chunk_t nonce;
45
46 /**
47 * ID to use for key lookup, if not from IDi
48 */
49 identification_t *id;
50 };
51
52 /**
53 * Rebuild our AUTH data
54 */
55 static bool rebuild_auth(private_rebuild_auth_t *this, ike_sa_t *ike_sa,
56 message_t *message)
57 {
58 enumerator_t *enumerator;
59 chunk_t octets, auth_data;
60 private_key_t *private;
61 payload_t *payload;
62 auth_payload_t *auth_payload;
63 auth_method_t auth_method;
64 signature_scheme_t scheme;
65 keymat_v2_t *keymat;
66 identification_t *id;
67 char reserved[3];
68 generator_t *generator;
69 chunk_t data;
70 u_int32_t *lenpos;
71
72 payload = message->get_payload(message,
73 message->get_request(message) ? ID_INITIATOR : ID_RESPONDER);
74 if (!payload)
75 {
76 DBG1(DBG_CFG, "ID payload not found to rebuild AUTH");
77 return FALSE;
78 }
79
80 generator = generator_create();
81 generator->generate_payload(generator, payload);
82 data = generator->get_chunk(generator, &lenpos);
83 if (data.len < 8)
84 {
85 DBG1(DBG_CFG, "ID payload invalid to rebuild AUTH");
86 generator->destroy(generator);
87 return FALSE;
88 }
89 memcpy(reserved, data.ptr + 5, 3);
90 id = identification_create_from_encoding(data.ptr[4], chunk_skip(data, 8));
91 generator->destroy(generator);
92
93 private = lib->credmgr->get_private(lib->credmgr, KEY_ANY,
94 this->id ?: id, NULL);
95 if (private == NULL)
96 {
97 DBG1(DBG_CFG, "no private key found for '%Y' to rebuild AUTH",
98 this->id ?: id);
99 id->destroy(id);
100 return FALSE;
101 }
102
103 switch (private->get_type(private))
104 {
105 case KEY_RSA:
106 scheme = SIGN_RSA_EMSA_PKCS1_SHA1;
107 auth_method = AUTH_RSA;
108 break;
109 case KEY_ECDSA:
110 /* we try to deduct the signature scheme from the keysize */
111 switch (private->get_keysize(private))
112 {
113 case 256:
114 scheme = SIGN_ECDSA_256;
115 auth_method = AUTH_ECDSA_256;
116 break;
117 case 384:
118 scheme = SIGN_ECDSA_384;
119 auth_method = AUTH_ECDSA_384;
120 break;
121 case 521:
122 scheme = SIGN_ECDSA_521;
123 auth_method = AUTH_ECDSA_521;
124 break;
125 default:
126 DBG1(DBG_CFG, "%d bit ECDSA private key size not supported",
127 private->get_keysize(private));
128 id->destroy(id);
129 return FALSE;
130 }
131 break;
132 default:
133 DBG1(DBG_CFG, "private key of type %N not supported",
134 key_type_names, private->get_type(private));
135 id->destroy(id);
136 return FALSE;
137 }
138 keymat = (keymat_v2_t*)ike_sa->get_keymat(ike_sa);
139 octets = keymat->get_auth_octets(keymat, FALSE, this->ike_init,
140 this->nonce, id, reserved);
141 if (!private->sign(private, scheme, octets, &auth_data))
142 {
143 chunk_free(&octets);
144 private->destroy(private);
145 id->destroy(id);
146 return FALSE;
147 }
148 auth_payload = auth_payload_create();
149 auth_payload->set_auth_method(auth_payload, auth_method);
150 auth_payload->set_data(auth_payload, auth_data);
151 chunk_free(&auth_data);
152 chunk_free(&octets);
153 private->destroy(private);
154
155 enumerator = message->create_payload_enumerator(message);
156 while (enumerator->enumerate(enumerator, &payload))
157 {
158 if (payload->get_type(payload) == AUTHENTICATION)
159 {
160 message->remove_payload_at(message, enumerator);
161 payload->destroy(payload);
162 }
163 }
164 enumerator->destroy(enumerator);
165
166 message->add_payload(message, (payload_t*)auth_payload);
167 DBG1(DBG_CFG, "rebuilding AUTH payload for '%Y' with %N",
168 id, auth_method_names, auth_method);
169 id->destroy(id);
170 return TRUE;
171 }
172
173 METHOD(listener_t, message, bool,
174 private_rebuild_auth_t *this, ike_sa_t *ike_sa, message_t *message,
175 bool incoming, bool plain)
176 {
177 if (plain)
178 {
179 if (!incoming && message->get_message_id(message) == 1)
180 {
181 rebuild_auth(this, ike_sa, message);
182 }
183 if (message->get_exchange_type(message) == IKE_SA_INIT)
184 {
185 if (incoming)
186 {
187 nonce_payload_t *nonce;
188
189 nonce = (nonce_payload_t*)message->get_payload(message, NONCE);
190 if (nonce)
191 {
192 free(this->nonce.ptr);
193 this->nonce = nonce->get_nonce(nonce);
194 }
195 }
196 else
197 {
198 packet_t *packet;
199
200 if (message->generate(message, NULL, &packet) == SUCCESS)
201 {
202 free(this->ike_init.ptr);
203 this->ike_init = chunk_clone(packet->get_data(packet));
204 packet->destroy(packet);
205 }
206 }
207 }
208 }
209 return TRUE;
210 }
211
212 METHOD(hook_t, destroy, void,
213 private_rebuild_auth_t *this)
214 {
215 free(this->ike_init.ptr);
216 free(this->nonce.ptr);
217 DESTROY_IF(this->id);
218 free(this);
219 }
220
221 /**
222 * Create the IKE_AUTH fill hook
223 */
224 hook_t *rebuild_auth_hook_create(char *name)
225 {
226 private_rebuild_auth_t *this;
227 char *id;
228
229 INIT(this,
230 .hook = {
231 .listener = {
232 .message = _message,
233 },
234 .destroy = _destroy,
235 },
236 );
237 id = conftest->test->get_str(conftest->test, "hooks.%s.key", NULL, name);
238 if (id)
239 {
240 this->id = identification_create_from_string(id);
241 }
242
243 return &this->hook;
244 }