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