Do not update payload length during generation, allows hooks override payload length
[strongswan.git] / src / libcharon / 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 return FAILED;
103 }
104 return SUCCESS;
105 }
106
107 /**
108 * Implementation of nonce_payload_t.set_nonce.
109 */
110 static status_t set_nonce(private_nonce_payload_t *this, chunk_t nonce)
111 {
112 this->nonce = chunk_clone(nonce);
113 this->payload_length = NONCE_PAYLOAD_HEADER_LENGTH + nonce.len;
114 return SUCCESS;
115 }
116
117 /**
118 * Implementation of nonce_payload_t.get_nonce.
119 */
120 static chunk_t get_nonce(private_nonce_payload_t *this)
121 {
122 return chunk_clone(this->nonce);
123 }
124
125 /**
126 * Implementation of nonce_payload_t.get_encoding_rules.
127 */
128 static void get_encoding_rules(private_nonce_payload_t *this, encoding_rule_t **rules, size_t *rule_count)
129 {
130 *rules = nonce_payload_encodings;
131 *rule_count = sizeof(nonce_payload_encodings) / sizeof(encoding_rule_t);
132 }
133
134 /**
135 * Implementation of payload_t.get_type.
136 */
137 static payload_type_t get_type(private_nonce_payload_t *this)
138 {
139 return NONCE;
140 }
141
142 /**
143 * Implementation of payload_t.get_next_type.
144 */
145 static payload_type_t get_next_type(private_nonce_payload_t *this)
146 {
147 return this->next_payload;
148 }
149
150 /**
151 * Implementation of payload_t.set_next_type.
152 */
153 static void set_next_type(private_nonce_payload_t *this,payload_type_t type)
154 {
155 this->next_payload = type;
156 }
157
158 /**
159 * Implementation of payload_t.get_length.
160 */
161 static size_t get_length(private_nonce_payload_t *this)
162 {
163 return this->payload_length;
164 }
165
166 /**
167 * Implementation of payload_t.destroy and nonce_payload_t.destroy.
168 */
169 static void destroy(private_nonce_payload_t *this)
170 {
171 if (this->nonce.ptr != NULL)
172 {
173 free(this->nonce.ptr);
174 }
175 free(this);
176 }
177
178 /*
179 * Described in header
180 */
181 nonce_payload_t *nonce_payload_create()
182 {
183 private_nonce_payload_t *this = malloc_thing(private_nonce_payload_t);
184
185 /* interface functions */
186 this->public.payload_interface.verify = (status_t (*) (payload_t *))verify;
187 this->public.payload_interface.get_encoding_rules = (void (*) (payload_t *, encoding_rule_t **, size_t *) ) get_encoding_rules;
188 this->public.payload_interface.get_length = (size_t (*) (payload_t *)) get_length;
189 this->public.payload_interface.get_next_type = (payload_type_t (*) (payload_t *)) get_next_type;
190 this->public.payload_interface.set_next_type = (void (*) (payload_t *,payload_type_t)) set_next_type;
191 this->public.payload_interface.get_type = (payload_type_t (*) (payload_t *)) get_type;
192 this->public.payload_interface.destroy = (void (*) (payload_t *))destroy;
193
194 /* public functions */
195 this->public.destroy = (void (*) (nonce_payload_t *)) destroy;
196 this->public.set_nonce = (void (*) (nonce_payload_t *,chunk_t)) set_nonce;
197 this->public.get_nonce = (chunk_t (*) (nonce_payload_t *)) get_nonce;
198
199 /* private variables */
200 this->critical = FALSE;
201 this->next_payload = NO_PAYLOAD;
202 this->payload_length = NONCE_PAYLOAD_HEADER_LENGTH;
203 this->nonce.ptr = NULL;
204 this->nonce.len = 0;
205
206 return (&(this->public));
207 }
208
209