- added verify function to all payload types
[strongswan.git] / Source / charon / payloads / nonce_payload.c
1 /**
2 * @file nonce_payload.h
3 *
4 * @brief Declaration of the class nonce_payload_t.
5 *
6 * An object of this type represents an IKEv2 Nonce-Payload.
7 *
8 */
9
10 /*
11 * Copyright (C) 2005 Jan Hutter, Martin Willi
12 * Hochschule fuer Technik Rapperswil
13 *
14 * This program is free software; you can redistribute it and/or modify it
15 * under the terms of the GNU General Public License as published by the
16 * Free Software Foundation; either version 2 of the License, or (at your
17 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
18 *
19 * This program is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
21 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 * for more details.
23 */
24
25 /* offsetof macro */
26 #include <stddef.h>
27
28 #include "nonce_payload.h"
29
30 #include "encodings.h"
31 #include "../utils/allocator.h"
32
33
34
35 /**
36 * Private data of an nonce_payload_t' Object
37 *
38 */
39 typedef struct private_nonce_payload_s private_nonce_payload_t;
40
41 struct private_nonce_payload_s {
42 /**
43 * public nonce_payload_t interface
44 */
45 nonce_payload_t public;
46
47 /**
48 * next payload type
49 */
50 u_int8_t next_payload;
51
52 /**
53 * Critical flag
54 */
55 bool critical;
56
57 /**
58 * Length of this payload
59 */
60 u_int16_t payload_length;
61
62 /**
63 * the contained nonce value
64 */
65 chunk_t nonce;
66
67 /**
68 * @brief Computes the length of this payload.
69 *
70 * @param this calling private_nonce_payload_t object
71 * @return
72 * SUCCESS in any case
73 */
74 status_t (*compute_length) (private_nonce_payload_t *this);
75 };
76
77 /**
78 * Encoding rules to parse or generate a nonce payload
79 *
80 * The defined offsets are the positions in a object of type
81 * private_nonce_payload_t.
82 *
83 */
84 encoding_rule_t nonce_payload_encodings[] = {
85 /* 1 Byte next payload type, stored in the field next_payload */
86 { U_INT_8, offsetof(private_nonce_payload_t, next_payload) },
87 /* the critical bit */
88 { FLAG, offsetof(private_nonce_payload_t, critical) },
89 /* 7 Bit reserved bits, nowhere stored */
90 { RESERVED_BIT, 0 },
91 { RESERVED_BIT, 0 },
92 { RESERVED_BIT, 0 },
93 { RESERVED_BIT, 0 },
94 { RESERVED_BIT, 0 },
95 { RESERVED_BIT, 0 },
96 { RESERVED_BIT, 0 },
97 /* Length of the whole nonce payload*/
98 { PAYLOAD_LENGTH, offsetof(private_nonce_payload_t, payload_length) },
99 /* some nonce bytes, lenth is defined in PAYLOAD_LENGTH */
100 { NONCE_DATA, offsetof(private_nonce_payload_t, nonce) }
101 };
102
103 /* 1 2 3
104 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
105 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
106 ! Next Payload !C! RESERVED ! Payload Length !
107 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
108 ! !
109 ~ Nonce Data ~
110 ! !
111 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
112 */
113
114 /**
115 * Implements payload_t's verify function.
116 * See #payload_s.verify for description.
117 */
118 static status_t verify(private_nonce_payload_t *this)
119 {
120 if (this->critical)
121 {
122 /* critical bit is set! */
123 return FAILED;
124 }
125 if ((this->nonce.len < 16) || ((this->nonce.len > 256)))
126 {
127 /* nonce length is wrong */
128 return FAILED;
129 }
130
131 return SUCCESS;
132 }
133
134 /**
135 * Implements payload_t's and nonce_payload_t's destroy function.
136 * See #payload_s.destroy or nonce_payload_s.destroy for description.
137 */
138 static status_t destroy(private_nonce_payload_t *this)
139 {
140 if (this->nonce.ptr != NULL)
141 {
142 allocator_free(this->nonce.ptr);
143 }
144 allocator_free(this);
145
146 return SUCCESS;
147 }
148
149 /**
150 * Implements nonce_payload_t's set_nonce function.
151 * See #nonce_payload_t.set_nonce for description.
152 */
153 static status_t set_nonce(private_nonce_payload_t *this, chunk_t nonce)
154 {
155 if (nonce.len >= 16 && nonce.len <= 256)
156 {
157 this->nonce.len = nonce.len;
158 this->nonce.ptr = nonce.ptr;
159 this->payload_length = NONCE_PAYLOAD_HEADER_LENGTH + nonce.len;
160 return SUCCESS;
161 }
162 return INVALID_ARG;
163 }
164
165 /**
166 * Implements nonce_payload_t's get_nonce function.
167 * See #nonce_payload_t.get_nonce for description.
168 */
169 static status_t get_nonce(private_nonce_payload_t *this, chunk_t *nonce)
170 {
171 nonce->len = this->nonce.len;
172 nonce->ptr = this->nonce.ptr;
173 return SUCCESS;
174 }
175
176 /**
177 * Implements payload_t's get_encoding_rules function.
178 * See #payload_s.get_encoding_rules for description.
179 */
180 static status_t get_encoding_rules(private_nonce_payload_t *this, encoding_rule_t **rules, size_t *rule_count)
181 {
182 *rules = nonce_payload_encodings;
183 *rule_count = sizeof(nonce_payload_encodings) / sizeof(encoding_rule_t);
184
185 return SUCCESS;
186 }
187
188 /**
189 * Implements payload_t's get_type function.
190 * See #payload_s.get_type for description.
191 */
192 static payload_type_t get_type(private_nonce_payload_t *this)
193 {
194 return NONCE;
195 }
196
197 /**
198 * Implements payload_t's get_next_type function.
199 * See #payload_s.get_next_type for description.
200 */
201 static payload_type_t get_next_type(private_nonce_payload_t *this)
202 {
203 return (this->next_payload);
204 }
205
206 /**
207 * Implements payload_t's set_next_type function.
208 * See #payload_s.set_next_type for description.
209 */
210 static status_t set_next_type(private_nonce_payload_t *this,payload_type_t type)
211 {
212 this->next_payload = type;
213 return SUCCESS;
214 }
215
216 /**
217 * Implements payload_t's get_length function.
218 * See #payload_s.get_length for description.
219 */
220 static size_t get_length(private_nonce_payload_t *this)
221 {
222 this->compute_length(this);
223 return this->payload_length;
224 }
225
226 /*
227 * Described in header
228 */
229 nonce_payload_t *nonce_payload_create()
230 {
231 private_nonce_payload_t *this = allocator_alloc_thing(private_nonce_payload_t);
232 if (this == NULL)
233 {
234 return NULL;
235 }
236
237 this->public.payload_interface.verify = (status_t (*) (payload_t *))verify;
238 this->public.payload_interface.get_encoding_rules = (status_t (*) (payload_t *, encoding_rule_t **, size_t *) ) get_encoding_rules;
239 this->public.payload_interface.get_length = (size_t (*) (payload_t *)) get_length;
240 this->public.payload_interface.get_next_type = (payload_type_t (*) (payload_t *)) get_next_type;
241 this->public.payload_interface.set_next_type = (status_t (*) (payload_t *,payload_type_t)) set_next_type;
242 this->public.payload_interface.get_type = (payload_type_t (*) (payload_t *)) get_type;
243 this->public.payload_interface.destroy = (status_t (*) (payload_t *))destroy;
244 this->public.destroy = (status_t (*) (nonce_payload_t *)) destroy;
245 this->public.set_nonce = (status_t (*) (nonce_payload_t *,chunk_t)) set_nonce;
246 this->public.get_nonce = (status_t (*) (nonce_payload_t *,chunk_t*)) get_nonce;
247
248 this->critical = FALSE;
249 this->next_payload = NO_PAYLOAD;
250 this->payload_length = NONCE_PAYLOAD_HEADER_LENGTH;
251 this->nonce.ptr = NULL;
252 this->nonce.len = 0;
253
254 return (&(this->public));
255 }
256
257