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