]> git.ipfire.org Git - thirdparty/strongswan.git/blame - src/libcharon/encoding/payloads/nonce_payload.c
Update copyright headers after acquisition by secunet
[thirdparty/strongswan.git] / src / libcharon / encoding / payloads / nonce_payload.c
CommitLineData
1071642c 1/*
e3c4c6a5 2 * Copyright (C) 2005-2010 Martin Willi
c71d53ba 3 * Copyright (C) 2005 Jan Hutter
19ef2aec
TB
4 *
5 * Copyright (C) secunet Security Networks AG
1071642c
MW
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 */
7daf5226 17
1071642c
MW
18#include <stddef.h>
19
20#include "nonce_payload.h"
21
31fc14e3 22#include <daemon.h>
4a962238 23#include <encoding/payloads/encodings.h>
1071642c 24
95c61cb9 25typedef struct private_nonce_payload_t private_nonce_payload_t;
1071642c
MW
26
27/**
3fe05870 28 * Private data of an nonce_payload_t object.
1071642c 29 */
95c61cb9 30struct private_nonce_payload_t {
e3c4c6a5 31
1071642c 32 /**
3fe05870 33 * Public nonce_payload_t interface.
1071642c
MW
34 */
35 nonce_payload_t public;
7daf5226 36
1071642c 37 /**
3fe05870 38 * Next payload type.
1071642c 39 */
b12c53ce 40 uint8_t next_payload;
1071642c
MW
41
42 /**
3fe05870 43 * Critical flag.
1071642c
MW
44 */
45 bool critical;
7daf5226 46
c93c7a75
MW
47 /**
48 * Reserved bits
49 */
50 bool reserved[7];
51
1071642c 52 /**
3fe05870 53 * Length of this payload.
1071642c 54 */
b12c53ce 55 uint16_t payload_length;
7daf5226 56
1071642c 57 /**
3fe05870 58 * The contained nonce value.
1071642c
MW
59 */
60 chunk_t nonce;
bcfb0f40
MW
61
62 /**
3ecfc83c 63 * Payload type, PLV2_NONCE or PLV1_NONCE
bcfb0f40
MW
64 */
65 payload_type_t type;
1071642c
MW
66};
67
68/**
69 * Encoding rules to parse or generate a nonce payload
7daf5226
MW
70 *
71 * The defined offsets are the positions in a object of type
1071642c 72 * private_nonce_payload_t.
1071642c 73 */
e9b55b83 74static encoding_rule_t encodings[] = {
7b3814f7 75 /* 1 Byte next payload type, stored in the field next_payload */
e3c4c6a5 76 { U_INT_8, offsetof(private_nonce_payload_t, next_payload) },
1071642c 77 /* the critical bit */
e3c4c6a5 78 { FLAG, offsetof(private_nonce_payload_t, critical) },
c93c7a75
MW
79 /* 7 Bit reserved bits */
80 { RESERVED_BIT, offsetof(private_nonce_payload_t, reserved[0]) },
81 { RESERVED_BIT, offsetof(private_nonce_payload_t, reserved[1]) },
82 { RESERVED_BIT, offsetof(private_nonce_payload_t, reserved[2]) },
83 { RESERVED_BIT, offsetof(private_nonce_payload_t, reserved[3]) },
84 { RESERVED_BIT, offsetof(private_nonce_payload_t, reserved[4]) },
85 { RESERVED_BIT, offsetof(private_nonce_payload_t, reserved[5]) },
86 { RESERVED_BIT, offsetof(private_nonce_payload_t, reserved[6]) },
1071642c 87 /* Length of the whole nonce payload*/
e3c4c6a5 88 { PAYLOAD_LENGTH, offsetof(private_nonce_payload_t, payload_length) },
527b3f0c 89 /* some nonce bytes, length is defined in PAYLOAD_LENGTH */
95a26523 90 { CHUNK_DATA, offsetof(private_nonce_payload_t, nonce) },
1071642c
MW
91};
92
e31eb71e
JH
93/* 1 2 3
94 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
95 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
96 ! Next Payload !C! RESERVED ! Payload Length !
97 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
98 ! !
99 ~ Nonce Data ~
100 ! !
101 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
102*/
103
e3c4c6a5
MW
104METHOD(payload_t, verify, status_t,
105 private_nonce_payload_t *this)
e31eb71e 106{
31fc14e3
MW
107 bool bad_length = FALSE;
108
109 if (this->nonce.len > 256)
110 {
111 bad_length = TRUE;
112 }
3ecfc83c 113 if (this->type == PLV2_NONCE &&
31fc14e3
MW
114 this->nonce.len < 16)
115 {
116 bad_length = TRUE;
117 }
3ecfc83c 118 if (this->type == PLV1_NONCE &&
31fc14e3
MW
119 this->nonce.len < 8)
120 {
121 bad_length = TRUE;
122 }
123 if (bad_length)
e31eb71e 124 {
31fc14e3
MW
125 DBG1(DBG_ENC, "%N payload has invalid length (%d bytes)",
126 payload_type_names, this->type, this->nonce.len);
e31eb71e
JH
127 return FAILED;
128 }
e31eb71e
JH
129 return SUCCESS;
130}
131
e9b55b83
MW
132METHOD(payload_t, get_encoding_rules, int,
133 private_nonce_payload_t *this, encoding_rule_t **rules)
1071642c 134{
e9b55b83
MW
135 *rules = encodings;
136 return countof(encodings);
1071642c
MW
137}
138
38fb67fb
MW
139METHOD(payload_t, get_header_length, int,
140 private_nonce_payload_t *this)
141{
142 return 4;
143}
144
e3c4c6a5
MW
145METHOD(payload_t, get_type, payload_type_t,
146 private_nonce_payload_t *this)
1071642c 147{
bcfb0f40 148 return this->type;
1071642c
MW
149}
150
e3c4c6a5
MW
151METHOD(payload_t, get_next_type, payload_type_t,
152 private_nonce_payload_t *this)
1071642c 153{
e3c4c6a5 154 return this->next_payload;
1071642c
MW
155}
156
e3c4c6a5
MW
157METHOD(payload_t, set_next_type, void,
158 private_nonce_payload_t *this, payload_type_t type)
1071642c 159{
e3c4c6a5 160 this->next_payload = type;
1071642c
MW
161}
162
e3c4c6a5
MW
163METHOD(payload_t, get_length, size_t,
164 private_nonce_payload_t *this)
1071642c 165{
e3c4c6a5 166 return this->payload_length;
1071642c
MW
167}
168
e3c4c6a5
MW
169METHOD(nonce_payload_t, set_nonce, void,
170 private_nonce_payload_t *this, chunk_t nonce)
1071642c 171{
e3c4c6a5 172 this->nonce = chunk_clone(nonce);
38fb67fb 173 this->payload_length = get_header_length(this) + nonce.len;
1071642c
MW
174}
175
e3c4c6a5
MW
176METHOD(nonce_payload_t, get_nonce, chunk_t,
177 private_nonce_payload_t *this)
79b9c1d6 178{
e3c4c6a5 179 return chunk_clone(this->nonce);
79b9c1d6
JH
180}
181
e3c4c6a5
MW
182METHOD2(payload_t, nonce_payload_t, destroy, void,
183 private_nonce_payload_t *this)
b2c259ba 184{
e3c4c6a5 185 free(this->nonce.ptr);
7daf5226 186 free(this);
b2c259ba
JH
187}
188
1071642c
MW
189/*
190 * Described in header
191 */
bcfb0f40 192nonce_payload_t *nonce_payload_create(payload_type_t type)
1071642c 193{
e3c4c6a5
MW
194 private_nonce_payload_t *this;
195
196 INIT(this,
197 .public = {
198 .payload_interface = {
199 .verify = _verify,
200 .get_encoding_rules = _get_encoding_rules,
38fb67fb 201 .get_header_length = _get_header_length,
e3c4c6a5
MW
202 .get_length = _get_length,
203 .get_next_type = _get_next_type,
204 .set_next_type = _set_next_type,
205 .get_type = _get_type,
206 .destroy = _destroy,
207 },
208 .set_nonce = _set_nonce,
209 .get_nonce = _get_nonce,
210 .destroy = _destroy,
211 },
3ecfc83c 212 .next_payload = PL_NONE,
38fb67fb 213 .payload_length = get_header_length(this),
bcfb0f40 214 .type = type,
e3c4c6a5
MW
215 );
216 return &this->public;
1071642c 217}