Migrated ke_payload to INIT/METHOD macros
[strongswan.git] / src / libcharon / encoding / payloads / ke_payload.c
1 /*
2 * Copyright (C) 2005-2010 Martin Willi
3 * Copyright (C) 2010 revosec AG
4 * Copyright (C) 2005 Jan Hutter
5 * Hochschule fuer Technik Rapperswil
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 * for more details.
16 */
17
18 #include <stddef.h>
19
20 #include "ke_payload.h"
21
22 #include <encoding/payloads/encodings.h>
23
24 typedef struct private_ke_payload_t private_ke_payload_t;
25
26 /**
27 * Private data of an ke_payload_t object.
28 */
29 struct private_ke_payload_t {
30
31 /**
32 * Public ke_payload_t interface.
33 */
34 ke_payload_t public;
35
36 /**
37 * Next payload type.
38 */
39 u_int8_t next_payload;
40
41 /**
42 * Critical flag.
43 */
44 bool critical;
45
46 /**
47 * Length of this payload.
48 */
49 u_int16_t payload_length;
50
51 /**
52 * DH Group Number.
53 */
54 u_int16_t dh_group_number;
55
56 /**
57 * Key Exchange Data of this KE payload.
58 */
59 chunk_t key_exchange_data;
60 };
61
62 /**
63 * Encoding rules to parse or generate a IKEv2-KE Payload.
64 *
65 * The defined offsets are the positions in a object of type
66 * private_ke_payload_t.
67 */
68 encoding_rule_t ke_payload_encodings[] = {
69 /* 1 Byte next payload type, stored in the field next_payload */
70 { U_INT_8, offsetof(private_ke_payload_t, next_payload) },
71 /* the critical bit */
72 { FLAG, offsetof(private_ke_payload_t, critical) },
73 /* 7 Bit reserved bits, nowhere stored */
74 { RESERVED_BIT, 0 },
75 { RESERVED_BIT, 0 },
76 { RESERVED_BIT, 0 },
77 { RESERVED_BIT, 0 },
78 { RESERVED_BIT, 0 },
79 { RESERVED_BIT, 0 },
80 { RESERVED_BIT, 0 },
81 /* Length of the whole payload*/
82 { PAYLOAD_LENGTH, offsetof(private_ke_payload_t, payload_length) },
83 /* DH Group number as 16 bit field*/
84 { U_INT_16, offsetof(private_ke_payload_t, dh_group_number) },
85 { RESERVED_BYTE, 0 },
86 { RESERVED_BYTE, 0 },
87 /* Key Exchange Data is from variable size */
88 { KEY_EXCHANGE_DATA, offsetof(private_ke_payload_t, key_exchange_data)}
89 };
90
91 /*
92 1 2 3
93 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
94 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
95 ! Next Payload !C! RESERVED ! Payload Length !
96 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
97 ! DH Group # ! RESERVED !
98 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
99 ! !
100 ~ Key Exchange Data ~
101 ! !
102 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
103 */
104
105 METHOD(payload_t, verify, status_t,
106 private_ke_payload_t *this)
107 {
108 return SUCCESS;
109 }
110
111 METHOD(payload_t, get_encoding_rules, void,
112 private_ke_payload_t *this, encoding_rule_t **rules, size_t *rule_count)
113 {
114 *rules = ke_payload_encodings;
115 *rule_count = countof(ke_payload_encodings);
116 }
117
118 METHOD(payload_t, get_type, payload_type_t,
119 private_ke_payload_t *this)
120 {
121 return KEY_EXCHANGE;
122 }
123
124 METHOD(payload_t, get_next_type, payload_type_t,
125 private_ke_payload_t *this)
126 {
127 return this->next_payload;
128 }
129
130 METHOD(payload_t, set_next_type, void,
131 private_ke_payload_t *this,payload_type_t type)
132 {
133 this->next_payload = type;
134 }
135
136 METHOD(payload_t, get_length, size_t,
137 private_ke_payload_t *this)
138 {
139 return this->payload_length;
140 }
141
142 METHOD(ke_payload_t, get_key_exchange_data, chunk_t,
143 private_ke_payload_t *this)
144 {
145 return this->key_exchange_data;
146 }
147
148 METHOD(ke_payload_t, get_dh_group_number, diffie_hellman_group_t,
149 private_ke_payload_t *this)
150 {
151 return this->dh_group_number;
152 }
153
154 METHOD2(payload_t, ke_payload_t, destroy, void,
155 private_ke_payload_t *this)
156 {
157 free(this->key_exchange_data.ptr);
158 free(this);
159 }
160
161 /*
162 * Described in header
163 */
164 ke_payload_t *ke_payload_create()
165 {
166 private_ke_payload_t *this;
167
168 INIT(this,
169 .public = {
170 .payload_interface = {
171 .verify = _verify,
172 .get_encoding_rules = _get_encoding_rules,
173 .get_length = _get_length,
174 .get_next_type = _get_next_type,
175 .set_next_type = _set_next_type,
176 .get_type = _get_type,
177 .destroy = _destroy,
178 },
179 .get_key_exchange_data = _get_key_exchange_data,
180 .get_dh_group_number = _get_dh_group_number,
181 .destroy = _destroy,
182 },
183 .next_payload = NO_PAYLOAD,
184 .payload_length = KE_PAYLOAD_HEADER_LENGTH,
185 .dh_group_number = MODP_NONE,
186 );
187 return &this->public;
188 }
189
190 /*
191 * Described in header
192 */
193 ke_payload_t *ke_payload_create_from_diffie_hellman(diffie_hellman_t *dh)
194 {
195 private_ke_payload_t *this = (private_ke_payload_t*)ke_payload_create();
196
197 dh->get_my_public_value(dh, &this->key_exchange_data);
198 this->dh_group_number = dh->get_dh_group(dh);
199 this->payload_length = this->key_exchange_data.len + KE_PAYLOAD_HEADER_LENGTH;
200
201 return &this->public;
202 }