2 * Copyright (C) 2005-2010 Martin Willi
3 * Copyright (C) 2010 revosec AG
4 * Copyright (C) 2005 Jan Hutter
5 * Hochschule fuer Technik Rapperswil
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>.
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
20 #include "nonce_payload.h"
22 #include <encoding/payloads/encodings.h>
24 typedef struct private_nonce_payload_t private_nonce_payload_t
;
27 * Private data of an nonce_payload_t object.
29 struct private_nonce_payload_t
{
32 * Public nonce_payload_t interface.
34 nonce_payload_t
public;
39 u_int8_t next_payload
;
52 * Length of this payload.
54 u_int16_t payload_length
;
57 * The contained nonce value.
62 * Payload type, NONCE or NONCE_V1
68 * Encoding rules to parse or generate a nonce payload
70 * The defined offsets are the positions in a object of type
71 * private_nonce_payload_t.
73 static encoding_rule_t 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
) },
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 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
100 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
103 METHOD(payload_t
, verify
, status_t
,
104 private_nonce_payload_t
*this)
106 if (this->nonce
.len
< 16 || this->nonce
.len
> 256)
113 METHOD(payload_t
, get_encoding_rules
, int,
114 private_nonce_payload_t
*this, encoding_rule_t
**rules
)
117 return countof(encodings
);
120 METHOD(payload_t
, get_type
, payload_type_t
,
121 private_nonce_payload_t
*this)
126 METHOD(payload_t
, get_next_type
, payload_type_t
,
127 private_nonce_payload_t
*this)
129 return this->next_payload
;
132 METHOD(payload_t
, set_next_type
, void,
133 private_nonce_payload_t
*this, payload_type_t type
)
135 this->next_payload
= type
;
138 METHOD(payload_t
, get_length
, size_t,
139 private_nonce_payload_t
*this)
141 return this->payload_length
;
144 METHOD(nonce_payload_t
, set_nonce
, void,
145 private_nonce_payload_t
*this, chunk_t nonce
)
147 this->nonce
= chunk_clone(nonce
);
148 this->payload_length
= NONCE_PAYLOAD_HEADER_LENGTH
+ nonce
.len
;
151 METHOD(nonce_payload_t
, get_nonce
, chunk_t
,
152 private_nonce_payload_t
*this)
154 return chunk_clone(this->nonce
);
157 METHOD2(payload_t
, nonce_payload_t
, destroy
, void,
158 private_nonce_payload_t
*this)
160 free(this->nonce
.ptr
);
165 * Described in header
167 nonce_payload_t
*nonce_payload_create(payload_type_t type
)
169 private_nonce_payload_t
*this;
173 .payload_interface
= {
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
,
182 .set_nonce
= _set_nonce
,
183 .get_nonce
= _get_nonce
,
186 .next_payload
= NO_PAYLOAD
,
187 .payload_length
= NONCE_PAYLOAD_HEADER_LENGTH
,
190 return &this->public;