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