68ee8512e46f7ea2c9323f9d62cce5c4c96512f0
[strongswan.git] / src / libcharon / sa / tasks / ike_vendor_v1.c
1 /*
2 * Copyright (C) 2009 Martin Willi
3 * Hochschule fuer Technik Rapperswil
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 "ike_vendor_v1.h"
17
18 #include <daemon.h>
19 #include <encoding/payloads/vendor_id_payload.h>
20
21 typedef struct private_ike_vendor_v1_t private_ike_vendor_v1_t;
22
23 /**
24 * Private data of an ike_vendor_v1_t object.
25 */
26 struct private_ike_vendor_v1_t {
27
28 /**
29 * Public ike_vendor_v1_t interface.
30 */
31 ike_vendor_v1_t public;
32
33 /**
34 * Associated IKE_SA
35 */
36 ike_sa_t *ike_sa;
37
38 /**
39 * Are we the inititator of this task
40 */
41 bool initiator;
42 };
43
44 /**
45 * IKEv1 Vendor ID database
46 */
47 static struct {
48 /* Description */
49 char *desc;
50 /* extension flag negotiated with vendor ID, if any */
51 ike_extension_t extension;
52 /* send yourself? */
53 bool send;
54 /* length of vendor ID string */
55 int len;
56 /* vendor ID string */
57 char *id;
58 } vendor_ids[] = {
59
60 /* strongSwan MD5("strongSwan") */
61 { "strongSwan", EXT_STRONGSWAN, FALSE, 16,
62 "\x88\x2f\xe5\x6d\x6f\xd2\x0d\xbc\x22\x51\x61\x3b\x2e\xbe\x5b\xeb"},
63
64 /* XAuth, MD5("draft-ietf-ipsra-isakmp-xauth-06.txt") */
65 { "XAuth", EXT_XAUTH, TRUE, 8,
66 "\x09\x00\x26\x89\xdf\xd6\xb7\x12"},
67
68 /* NAT-Traversal, MD5("RFC 3947") */
69 { "NAT-T (RFC 3947)", EXT_NATT, TRUE, 16,
70 "\x4a\x13\x1c\x81\x07\x03\x58\x45\x5c\x57\x28\xf2\x0e\x95\x45\x2f"},
71 };
72
73 METHOD(task_t, build, status_t,
74 private_ike_vendor_v1_t *this, message_t *message)
75 {
76 vendor_id_payload_t *vid_payload;
77 bool strongswan;
78 int i;
79
80 strongswan = lib->settings->get_bool(lib->settings,
81 "charon.send_vendor_id", FALSE);
82 for (i = 0; i < countof(vendor_ids); i++)
83 {
84 if (vendor_ids[i].send ||
85 (vendor_ids[i].extension == EXT_STRONGSWAN && strongswan))
86 {
87 vid_payload = vendor_id_payload_create_data(VENDOR_ID_V1,
88 chunk_clone(chunk_create(vendor_ids[i].id, vendor_ids[i].len)));
89 message->add_payload(message, &vid_payload->payload_interface);
90 }
91 }
92 return this->initiator ? NEED_MORE : SUCCESS;
93 }
94
95 METHOD(task_t, process, status_t,
96 private_ike_vendor_v1_t *this, message_t *message)
97 {
98 enumerator_t *enumerator;
99 payload_t *payload;
100 int i;
101
102 enumerator = message->create_payload_enumerator(message);
103 while (enumerator->enumerate(enumerator, &payload))
104 {
105 if (payload->get_type(payload) == VENDOR_ID_V1)
106 {
107 vendor_id_payload_t *vid;
108 bool found = FALSE;
109 chunk_t data;
110
111 vid = (vendor_id_payload_t*)payload;
112 data = vid->get_data(vid);
113
114 for (i = 0; i < countof(vendor_ids); i++)
115 {
116 if (chunk_equals(data, chunk_create(vendor_ids[i].id,
117 vendor_ids[i].len)))
118 {
119 DBG1(DBG_IKE, "received %s vendor id", vendor_ids[i].desc);
120 if (vendor_ids[i].extension)
121 {
122 this->ike_sa->enable_extension(this->ike_sa,
123 vendor_ids[i].extension);
124 }
125 found = TRUE;
126 }
127 }
128 if (!found)
129 {
130 DBG1(DBG_ENC, "received unknown vendor id: %#B", &data);
131 }
132 }
133 }
134 enumerator->destroy(enumerator);
135
136 return this->initiator ? SUCCESS : NEED_MORE;
137 }
138
139 METHOD(task_t, migrate, void,
140 private_ike_vendor_v1_t *this, ike_sa_t *ike_sa)
141 {
142 this->ike_sa = ike_sa;
143 }
144
145 METHOD(task_t, get_type, task_type_t,
146 private_ike_vendor_v1_t *this)
147 {
148 return TASK_VENDOR_V1;
149 }
150
151 METHOD(task_t, destroy, void,
152 private_ike_vendor_v1_t *this)
153 {
154 free(this);
155 }
156
157 /**
158 * See header
159 */
160 ike_vendor_v1_t *ike_vendor_v1_create(ike_sa_t *ike_sa, bool initiator)
161 {
162 private_ike_vendor_v1_t *this;
163
164 INIT(this,
165 .public = {
166 .task = {
167 .build = _build,
168 .process = _process,
169 .migrate = _migrate,
170 .get_type = _get_type,
171 .destroy = _destroy,
172 },
173 },
174 .initiator = initiator,
175 .ike_sa = ike_sa,
176 );
177
178 return &this->public;
179 }