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