Extended nonce payload for IKEv1 support
[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 * Payload type, NONCE or NONCE_V1
63 */
64 payload_type_t type;
65 };
66
67 /**
68 * Encoding rules to parse or generate a nonce payload
69 *
70 * The defined offsets are the positions in a object of type
71 * private_nonce_payload_t.
72 */
73 encoding_rule_t nonce_payload_encodings[] = {
74 /* 1 Byte next payload type, stored in the field next_payload */
75 { U_INT_8, offsetof(private_nonce_payload_t, next_payload) },
76 /* the critical bit */
77 { FLAG, offsetof(private_nonce_payload_t, critical) },
78 /* 7 Bit reserved bits */
79 { RESERVED_BIT, offsetof(private_nonce_payload_t, reserved[0]) },
80 { RESERVED_BIT, offsetof(private_nonce_payload_t, reserved[1]) },
81 { RESERVED_BIT, offsetof(private_nonce_payload_t, reserved[2]) },
82 { RESERVED_BIT, offsetof(private_nonce_payload_t, reserved[3]) },
83 { RESERVED_BIT, offsetof(private_nonce_payload_t, reserved[4]) },
84 { RESERVED_BIT, offsetof(private_nonce_payload_t, reserved[5]) },
85 { RESERVED_BIT, offsetof(private_nonce_payload_t, reserved[6]) },
86 /* Length of the whole nonce payload*/
87 { PAYLOAD_LENGTH, offsetof(private_nonce_payload_t, payload_length) },
88 /* some nonce bytes, lenth is defined in PAYLOAD_LENGTH */
89 { NONCE_DATA, offsetof(private_nonce_payload_t, nonce) },
90 };
91
92 /* 1 2 3
93 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
94 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
95 ! Next Payload !C! RESERVED ! Payload Length !
96 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
97 ! !
98 ~ Nonce Data ~
99 ! !
100 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
101 */
102
103 METHOD(payload_t, verify, status_t,
104 private_nonce_payload_t *this)
105 {
106 if (this->nonce.len < 16 || this->nonce.len > 256)
107 {
108 return FAILED;
109 }
110 return SUCCESS;
111 }
112
113 METHOD(payload_t, get_encoding_rules, void,
114 private_nonce_payload_t *this, encoding_rule_t **rules, size_t *rule_count)
115 {
116 *rules = nonce_payload_encodings;
117 *rule_count = countof(nonce_payload_encodings);
118 }
119
120 METHOD(payload_t, get_type, payload_type_t,
121 private_nonce_payload_t *this)
122 {
123 return this->type;
124 }
125
126 METHOD(payload_t, get_next_type, payload_type_t,
127 private_nonce_payload_t *this)
128 {
129 return this->next_payload;
130 }
131
132 METHOD(payload_t, set_next_type, void,
133 private_nonce_payload_t *this, payload_type_t type)
134 {
135 this->next_payload = type;
136 }
137
138 METHOD(payload_t, get_length, size_t,
139 private_nonce_payload_t *this)
140 {
141 return this->payload_length;
142 }
143
144 METHOD(nonce_payload_t, set_nonce, void,
145 private_nonce_payload_t *this, chunk_t nonce)
146 {
147 this->nonce = chunk_clone(nonce);
148 this->payload_length = NONCE_PAYLOAD_HEADER_LENGTH + nonce.len;
149 }
150
151 METHOD(nonce_payload_t, get_nonce, chunk_t,
152 private_nonce_payload_t *this)
153 {
154 return chunk_clone(this->nonce);
155 }
156
157 METHOD2(payload_t, nonce_payload_t, destroy, void,
158 private_nonce_payload_t *this)
159 {
160 free(this->nonce.ptr);
161 free(this);
162 }
163
164 /*
165 * Described in header
166 */
167 nonce_payload_t *nonce_payload_create(payload_type_t type)
168 {
169 private_nonce_payload_t *this;
170
171 INIT(this,
172 .public = {
173 .payload_interface = {
174 .verify = _verify,
175 .get_encoding_rules = _get_encoding_rules,
176 .get_length = _get_length,
177 .get_next_type = _get_next_type,
178 .set_next_type = _set_next_type,
179 .get_type = _get_type,
180 .destroy = _destroy,
181 },
182 .set_nonce = _set_nonce,
183 .get_nonce = _get_nonce,
184 .destroy = _destroy,
185 },
186 .next_payload = NO_PAYLOAD,
187 .payload_length = NONCE_PAYLOAD_HEADER_LENGTH,
188 .type = type,
189 );
190 return &this->public;
191 }