Removed strayed code fragment
[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
17 /* offsetof macro */
18 #include <stddef.h>
19
20 #include "nonce_payload.h"
21
22 #include <encoding/payloads/encodings.h>
23
24
25 typedef struct private_nonce_payload_t private_nonce_payload_t;
26
27 /**
28 * Private data of an nonce_payload_t object.
29 *
30 */
31 struct private_nonce_payload_t {
32 /**
33 * Public nonce_payload_t interface.
34 */
35 nonce_payload_t public;
36
37 /**
38 * Next payload type.
39 */
40 u_int8_t next_payload;
41
42 /**
43 * Critical flag.
44 */
45 bool critical;
46
47 /**
48 * Length of this payload.
49 */
50 u_int16_t payload_length;
51
52 /**
53 * The contained nonce value.
54 */
55 chunk_t nonce;
56 };
57
58 /**
59 * Encoding rules to parse or generate a nonce payload
60 *
61 * The defined offsets are the positions in a object of type
62 * private_nonce_payload_t.
63 *
64 */
65 encoding_rule_t nonce_payload_encodings[] = {
66 /* 1 Byte next payload type, stored in the field next_payload */
67 { U_INT_8, offsetof(private_nonce_payload_t, next_payload) },
68 /* the critical bit */
69 { FLAG, offsetof(private_nonce_payload_t, critical) },
70 /* 7 Bit reserved bits, nowhere stored */
71 { RESERVED_BIT, 0 },
72 { RESERVED_BIT, 0 },
73 { RESERVED_BIT, 0 },
74 { RESERVED_BIT, 0 },
75 { RESERVED_BIT, 0 },
76 { RESERVED_BIT, 0 },
77 { RESERVED_BIT, 0 },
78 /* Length of the whole nonce payload*/
79 { PAYLOAD_LENGTH, offsetof(private_nonce_payload_t, payload_length) },
80 /* some nonce bytes, lenth is defined in PAYLOAD_LENGTH */
81 { NONCE_DATA, offsetof(private_nonce_payload_t, nonce) }
82 };
83
84 /* 1 2 3
85 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
86 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
87 ! Next Payload !C! RESERVED ! Payload Length !
88 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
89 ! !
90 ~ Nonce Data ~
91 ! !
92 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
93 */
94
95 /**
96 * Implementation of payload_t.verify.
97 */
98 static status_t verify(private_nonce_payload_t *this)
99 {
100 if ((this->nonce.len < 16) || ((this->nonce.len > 256)))
101 {
102 /* nonce length is wrong */
103 return FAILED;
104 }
105
106 return SUCCESS;
107 }
108
109 /**
110 * Implementation of nonce_payload_t.set_nonce.
111 */
112 static status_t set_nonce(private_nonce_payload_t *this, chunk_t nonce)
113 {
114 this->nonce.ptr = clalloc(nonce.ptr, nonce.len);
115 this->nonce.len = nonce.len;
116 this->payload_length = NONCE_PAYLOAD_HEADER_LENGTH + nonce.len;
117 return SUCCESS;
118 }
119
120 /**
121 * Implementation of nonce_payload_t.get_nonce.
122 */
123 static chunk_t get_nonce(private_nonce_payload_t *this)
124 {
125 chunk_t nonce;
126 nonce.ptr = clalloc(this->nonce.ptr,this->nonce.len);
127 nonce.len = this->nonce.len;
128 return nonce;
129 }
130
131 /**
132 * Implementation of nonce_payload_t.get_encoding_rules.
133 */
134 static void get_encoding_rules(private_nonce_payload_t *this, encoding_rule_t **rules, size_t *rule_count)
135 {
136 *rules = nonce_payload_encodings;
137 *rule_count = sizeof(nonce_payload_encodings) / sizeof(encoding_rule_t);
138 }
139
140 /**
141 * Implementation of payload_t.get_type.
142 */
143 static payload_type_t get_type(private_nonce_payload_t *this)
144 {
145 return NONCE;
146 }
147
148 /**
149 * Implementation of payload_t.get_next_type.
150 */
151 static payload_type_t get_next_type(private_nonce_payload_t *this)
152 {
153 return (this->next_payload);
154 }
155
156 /**
157 * Implementation of payload_t.set_next_type.
158 */
159 static void set_next_type(private_nonce_payload_t *this,payload_type_t type)
160 {
161 this->next_payload = type;
162 }
163
164 /**
165 * recompute the length of the payload.
166 */
167 static void compute_length(private_nonce_payload_t *this)
168 {
169 this->payload_length = NONCE_PAYLOAD_HEADER_LENGTH + this->nonce.len;
170 }
171
172 /**
173 * Implementation of payload_t.get_length.
174 */
175 static size_t get_length(private_nonce_payload_t *this)
176 {
177 compute_length(this);
178 return this->payload_length;
179 }
180
181 /**
182 * Implementation of payload_t.destroy and nonce_payload_t.destroy.
183 */
184 static void destroy(private_nonce_payload_t *this)
185 {
186 if (this->nonce.ptr != NULL)
187 {
188 free(this->nonce.ptr);
189 }
190
191 free(this);
192 }
193
194 /*
195 * Described in header
196 */
197 nonce_payload_t *nonce_payload_create()
198 {
199 private_nonce_payload_t *this = malloc_thing(private_nonce_payload_t);
200
201 /* interface functions */
202 this->public.payload_interface.verify = (status_t (*) (payload_t *))verify;
203 this->public.payload_interface.get_encoding_rules = (void (*) (payload_t *, encoding_rule_t **, size_t *) ) get_encoding_rules;
204 this->public.payload_interface.get_length = (size_t (*) (payload_t *)) get_length;
205 this->public.payload_interface.get_next_type = (payload_type_t (*) (payload_t *)) get_next_type;
206 this->public.payload_interface.set_next_type = (void (*) (payload_t *,payload_type_t)) set_next_type;
207 this->public.payload_interface.get_type = (payload_type_t (*) (payload_t *)) get_type;
208 this->public.payload_interface.destroy = (void (*) (payload_t *))destroy;
209
210 /* public functions */
211 this->public.destroy = (void (*) (nonce_payload_t *)) destroy;
212 this->public.set_nonce = (void (*) (nonce_payload_t *,chunk_t)) set_nonce;
213 this->public.get_nonce = (chunk_t (*) (nonce_payload_t *)) get_nonce;
214
215 /* private variables */
216 this->critical = FALSE;
217 this->next_payload = NO_PAYLOAD;
218 this->payload_length = NONCE_PAYLOAD_HEADER_LENGTH;
219 this->nonce.ptr = NULL;
220 this->nonce.len = 0;
221
222 return (&(this->public));
223 }
224
225