2 * @file nonce_payload.h
4 * @brief Declaration of the class nonce_payload_t.
6 * An object of this type represents an IKEv2 Nonce-Payload.
11 * Copyright (C) 2005 Jan Hutter, Martin Willi
12 * Hochschule fuer Technik Rapperswil
14 * This program is free software; you can redistribute it and/or modify it
15 * under the terms of the GNU General Public License as published by the
16 * Free Software Foundation; either version 2 of the License, or (at your
17 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
19 * This program is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
21 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
28 #include "nonce_payload.h"
30 #include "encodings.h"
31 #include "../utils/allocator.h"
36 * Private data of an nonce_payload_t' Object
39 typedef struct private_nonce_payload_s private_nonce_payload_t
;
41 struct private_nonce_payload_s
{
43 * public nonce_payload_t interface
45 nonce_payload_t
public;
50 u_int8_t next_payload
;
58 * Length of this payload
60 u_int16_t payload_length
;
63 * the contained nonce value
68 * @brief Computes the length of this payload.
70 * @param this calling private_nonce_payload_t object
74 status_t (*compute_length
) (private_nonce_payload_t
*this);
78 * Encoding rules to parse or generate a nonce payload
80 * The defined offsets are the positions in a object of type
81 * private_nonce_payload_t.
84 encoding_rule_t nonce_payload_encodings
[] = {
85 /* 1 Byte next payload type, stored in the field next_payload */
86 { U_INT_8
, offsetof(private_nonce_payload_t
, next_payload
) },
87 /* the critical bit */
88 { FLAG
, offsetof(private_nonce_payload_t
, critical
) },
89 /* 7 Bit reserved bits, nowhere stored */
97 /* Length of the whole nonce payload*/
98 { PAYLOAD_LENGTH
, offsetof(private_nonce_payload_t
, payload_length
) },
99 /* some nonce bytes, lenth is defined in PAYLOAD_LENGTH */
100 { NONCE_DATA
, offsetof(private_nonce_payload_t
, nonce
) }
104 * length of a nonce payload without a nonce in int
106 #define NONCE_PAYLOAD_HEADER_LENGTH 4
109 * Implements payload_t's and nonce_payload_t's destroy function.
110 * See #payload_s.destroy or nonce_payload_s.destroy for description.
112 static status_t
destroy(private_nonce_payload_t
*this)
116 allocator_free(this->nonce
.ptr
);
118 allocator_free(this);
124 * Implements nonce_payload_t's set_nonce function.
125 * See #nonce_payload_t.set_nonce for description.
127 static status_t
set_nonce(private_nonce_payload_t
*this, chunk_t nonce
)
129 if (nonce
.len
>= 16 && nonce
.len
<= 256)
131 this->nonce
.len
= nonce
.len
;
132 this->nonce
.ptr
= allocator_clone_bytes(nonce
.ptr
, nonce
.len
);
133 if (this->nonce
.ptr
== NULL
)
137 this->payload_length
= NONCE_PAYLOAD_HEADER_LENGTH
+ nonce
.len
;
144 * Implements nonce_payload_t's get_nonce function.
145 * See #nonce_payload_t.get_nonce for description.
147 static status_t
get_nonce(private_nonce_payload_t
*this, chunk_t
*nonce
)
149 nonce
->len
= this->nonce
.len
;
150 nonce
->ptr
= allocator_clone_bytes(this->nonce
.ptr
, this->nonce
.len
);
151 if (nonce
->ptr
== NULL
)
159 * Implements payload_t's get_encoding_rules function.
160 * See #payload_s.get_encoding_rules for description.
162 static status_t
get_encoding_rules(private_nonce_payload_t
*this, encoding_rule_t
**rules
, size_t *rule_count
)
164 *rules
= nonce_payload_encodings
;
165 *rule_count
= sizeof(nonce_payload_encodings
) / sizeof(encoding_rule_t
);
171 * Implements payload_t's get_type function.
172 * See #payload_s.get_type for description.
174 static payload_type_t
get_type(private_nonce_payload_t
*this)
176 return SECURITY_ASSOCIATION
;
180 * Implements payload_t's get_next_type function.
181 * See #payload_s.get_next_type for description.
183 static payload_type_t
get_next_type(private_nonce_payload_t
*this)
185 return (this->next_payload
);
189 * Implements payload_t's set_next_type function.
190 * See #payload_s.set_next_type for description.
192 static status_t
set_next_type(private_nonce_payload_t
*this,payload_type_t type
)
194 this->next_payload
= type
;
199 * Implements payload_t's get_length function.
200 * See #payload_s.get_length for description.
202 static size_t get_length(private_nonce_payload_t
*this)
204 this->compute_length(this);
205 return this->payload_length
;
209 * Described in header
211 nonce_payload_t
*nonce_payload_create()
213 private_nonce_payload_t
*this = allocator_alloc_thing(private_nonce_payload_t
);
219 this->public.payload_interface
.get_encoding_rules
= (status_t (*) (payload_t
*, encoding_rule_t
**, size_t *) ) get_encoding_rules
;
220 this->public.payload_interface
.get_length
= (size_t (*) (payload_t
*)) get_length
;
221 this->public.payload_interface
.get_next_type
= (payload_type_t (*) (payload_t
*)) get_next_type
;
222 this->public.payload_interface
.set_next_type
= (status_t (*) (payload_t
*,payload_type_t
)) set_next_type
;
223 this->public.payload_interface
.get_type
= (payload_type_t (*) (payload_t
*)) get_type
;
224 this->public.payload_interface
.destroy
= (status_t (*) (payload_t
*))destroy
;
225 this->public.destroy
= (status_t (*) (nonce_payload_t
*)) destroy
;
226 this->public.set_nonce
= (status_t (*) (nonce_payload_t
*,chunk_t
)) set_nonce
;
227 this->public.get_nonce
= (status_t (*) (nonce_payload_t
*,chunk_t
*)) get_nonce
;
229 this->critical
= FALSE
;
230 this->next_payload
= NO_PAYLOAD
;
231 this->payload_length
= NONCE_PAYLOAD_HEADER_LENGTH
;
233 return (&(this->public));