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