2 * @file nonce_payload.h
4 * @brief Implementation of nonce_payload_t.
9 * Copyright (C) 2005 Jan Hutter, Martin Willi
10 * Hochschule fuer Technik Rapperswil
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
26 #include "nonce_payload.h"
28 #include <encoding/payloads/encodings.h>
29 #include <utils/allocator.h>
32 typedef struct private_nonce_payload_t private_nonce_payload_t
;
35 * Private data of an nonce_payload_t object.
38 struct private_nonce_payload_t
{
40 * Public nonce_payload_t interface.
42 nonce_payload_t
public;
47 u_int8_t next_payload
;
55 * Length of this payload.
57 u_int16_t payload_length
;
60 * The contained nonce value.
65 * @brief Computes the length of this payload.
67 * @param this calling private_nonce_payload_t object
69 void (*compute_length
) (private_nonce_payload_t
*this);
73 * Encoding rules to parse or generate a nonce payload
75 * The defined offsets are the positions in a object of type
76 * private_nonce_payload_t.
79 encoding_rule_t nonce_payload_encodings
[] = {
80 /* 1 Byte next payload type, stored in the field next_payload */
81 { U_INT_8
, offsetof(private_nonce_payload_t
, next_payload
) },
82 /* the critical bit */
83 { FLAG
, offsetof(private_nonce_payload_t
, critical
) },
84 /* 7 Bit reserved bits, nowhere stored */
92 /* Length of the whole nonce payload*/
93 { PAYLOAD_LENGTH
, offsetof(private_nonce_payload_t
, payload_length
) },
94 /* some nonce bytes, lenth is defined in PAYLOAD_LENGTH */
95 { NONCE_DATA
, offsetof(private_nonce_payload_t
, nonce
) }
99 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
100 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
101 ! Next Payload !C! RESERVED ! Payload Length !
102 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
106 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
110 * Implementation of payload_t.verify.
112 static status_t
verify(private_nonce_payload_t
*this)
116 /* critical bit is set! */
119 if ((this->nonce
.len
< 16) || ((this->nonce
.len
> 256)))
121 /* nonce length is wrong */
129 * Implementation of nonce_payload_t.set_nonce.
131 static status_t
set_nonce(private_nonce_payload_t
*this, chunk_t nonce
)
133 if (nonce
.len
>= 16 && nonce
.len
<= 256)
136 this->nonce
.ptr
= allocator_clone_bytes(nonce
.ptr
, nonce
.len
);
137 this->nonce
.len
= nonce
.len
;
138 this->payload_length
= NONCE_PAYLOAD_HEADER_LENGTH
+ nonce
.len
;
145 * Implementation of nonce_payload_t.get_nonce.
147 static void get_nonce(private_nonce_payload_t
*this, chunk_t
*nonce
)
149 nonce
->ptr
= allocator_clone_bytes(this->nonce
.ptr
,this->nonce
.len
);
150 nonce
->len
= this->nonce
.len
;
154 * Implementation of nonce_payload_t.get_encoding_rules.
156 static void get_encoding_rules(private_nonce_payload_t
*this, encoding_rule_t
**rules
, size_t *rule_count
)
158 *rules
= nonce_payload_encodings
;
159 *rule_count
= sizeof(nonce_payload_encodings
) / sizeof(encoding_rule_t
);
163 * Implementation of payload_t.get_type.
165 static payload_type_t
get_type(private_nonce_payload_t
*this)
171 * Implementation of payload_t.get_next_type.
173 static payload_type_t
get_next_type(private_nonce_payload_t
*this)
175 return (this->next_payload
);
179 * Implementation of payload_t.set_next_type.
181 static void set_next_type(private_nonce_payload_t
*this,payload_type_t type
)
183 this->next_payload
= type
;
187 * Implementation of payload_t.get_length.
189 static size_t get_length(private_nonce_payload_t
*this)
191 this->compute_length(this);
192 return this->payload_length
;
196 * Implementation of payload_t.destroy and nonce_payload_t.destroy.
198 static void destroy(private_nonce_payload_t
*this)
200 if (this->nonce
.ptr
!= NULL
)
202 allocator_free(this->nonce
.ptr
);
205 allocator_free(this);
209 * Described in header
211 nonce_payload_t
*nonce_payload_create()
213 private_nonce_payload_t
*this = allocator_alloc_thing(private_nonce_payload_t
);
215 /* interface functions */
216 this->public.payload_interface
.verify
= (status_t (*) (payload_t
*))verify
;
217 this->public.payload_interface
.get_encoding_rules
= (void (*) (payload_t
*, encoding_rule_t
**, size_t *) ) get_encoding_rules
;
218 this->public.payload_interface
.get_length
= (size_t (*) (payload_t
*)) get_length
;
219 this->public.payload_interface
.get_next_type
= (payload_type_t (*) (payload_t
*)) get_next_type
;
220 this->public.payload_interface
.set_next_type
= (void (*) (payload_t
*,payload_type_t
)) set_next_type
;
221 this->public.payload_interface
.get_type
= (payload_type_t (*) (payload_t
*)) get_type
;
222 this->public.payload_interface
.destroy
= (void (*) (payload_t
*))destroy
;
224 /* public functions */
225 this->public.destroy
= (void (*) (nonce_payload_t
*)) destroy
;
226 this->public.set_nonce
= (status_t (*) (nonce_payload_t
*,chunk_t
)) set_nonce
;
227 this->public.get_nonce
= (void (*) (nonce_payload_t
*,chunk_t
*)) get_nonce
;
229 /* private variables */
230 this->critical
= FALSE
;
231 this->next_payload
= NO_PAYLOAD
;
232 this->payload_length
= NONCE_PAYLOAD_HEADER_LENGTH
;
233 this->nonce
.ptr
= NULL
;
236 return (&(this->public));