Moved TLS stack to its own library
[strongswan.git] / src / libtls / tls_reader.c
1 /*
2 * Copyright (C) 2010 Martin Willi
3 * Copyright (C) 2010 revosec AG
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include "tls_reader.h"
17
18 #include <debug.h>
19
20 typedef struct private_tls_reader_t private_tls_reader_t;
21
22 /**
23 * Private data of an tls_reader_t object.
24 */
25 struct private_tls_reader_t {
26
27 /**
28 * Public tls_reader_t interface.
29 */
30 tls_reader_t public;
31
32 /**
33 * Remaining data to process
34 */
35 chunk_t buf;
36 };
37
38 METHOD(tls_reader_t, remaining, u_int32_t,
39 private_tls_reader_t *this)
40 {
41 return this->buf.len;
42 }
43
44 METHOD(tls_reader_t, peek, chunk_t,
45 private_tls_reader_t *this)
46 {
47 return this->buf;
48 }
49
50 METHOD(tls_reader_t, read_uint8, bool,
51 private_tls_reader_t *this, u_int8_t *res)
52 {
53 if (this->buf.len < 1)
54 {
55 DBG1(DBG_IKE, "%d bytes insufficient to parse uint%d TLS data",
56 this->buf.len, 8);
57 return FALSE;
58 }
59 *res = this->buf.ptr[0];
60 this->buf = chunk_skip(this->buf, 1);
61 return TRUE;
62 }
63
64 METHOD(tls_reader_t, read_uint16, bool,
65 private_tls_reader_t *this, u_int16_t *res)
66 {
67 if (this->buf.len < 2)
68 {
69 DBG1(DBG_IKE, "%d bytes insufficient to parse uint%d TLS data",
70 this->buf.len, 16);
71 return FALSE;
72 }
73 *res = untoh16(this->buf.ptr);
74 this->buf = chunk_skip(this->buf, 2);
75 return TRUE;
76 }
77
78 METHOD(tls_reader_t, read_uint24, bool,
79 private_tls_reader_t *this, u_int32_t *res)
80 {
81 if (this->buf.len < 3)
82 {
83 DBG1(DBG_IKE, "%d bytes insufficient to parse uint%d TLS data",
84 this->buf.len, 24);
85 return FALSE;
86 }
87 *res = untoh32(this->buf.ptr) >> 8;
88 this->buf = chunk_skip(this->buf, 3);
89 return TRUE;
90 }
91
92 METHOD(tls_reader_t, read_uint32, bool,
93 private_tls_reader_t *this, u_int32_t *res)
94 {
95 if (this->buf.len < 4)
96 {
97 DBG1(DBG_IKE, "%d bytes insufficient to parse uint%d TLS data",
98 this->buf.len, 32);
99 return FALSE;
100 }
101 *res = untoh32(this->buf.ptr);
102 this->buf = chunk_skip(this->buf, 4);
103 return TRUE;
104 }
105
106 METHOD(tls_reader_t, read_data, bool,
107 private_tls_reader_t *this, u_int32_t len, chunk_t *res)
108 {
109 if (this->buf.len < len)
110 {
111 DBG1(DBG_IKE, "%d bytes insufficient to parse %d bytes TLS data",
112 this->buf.len, len);
113 return FALSE;
114 }
115 *res = chunk_create(this->buf.ptr, len);
116 this->buf = chunk_skip(this->buf, len);
117 return TRUE;
118 }
119
120 METHOD(tls_reader_t, read_data8, bool,
121 private_tls_reader_t *this, chunk_t *res)
122 {
123 u_int8_t len;
124
125 if (!read_uint8(this, &len))
126 {
127 return FALSE;
128 }
129 return read_data(this, len, res);
130 }
131
132 METHOD(tls_reader_t, read_data16, bool,
133 private_tls_reader_t *this, chunk_t *res)
134 {
135 u_int16_t len;
136
137 if (!read_uint16(this, &len))
138 {
139 return FALSE;
140 }
141 return read_data(this, len, res);
142 }
143
144 METHOD(tls_reader_t, read_data24, bool,
145 private_tls_reader_t *this, chunk_t *res)
146 {
147 u_int32_t len;
148
149 if (!read_uint24(this, &len))
150 {
151 return FALSE;
152 }
153 return read_data(this, len, res);
154 }
155
156 METHOD(tls_reader_t, read_data32, bool,
157 private_tls_reader_t *this, chunk_t *res)
158 {
159 u_int32_t len;
160
161 if (!read_uint32(this, &len))
162 {
163 return FALSE;
164 }
165 return read_data(this, len, res);
166 }
167
168 METHOD(tls_reader_t, destroy, void,
169 private_tls_reader_t *this)
170 {
171 free(this);
172 }
173
174 /**
175 * See header
176 */
177 tls_reader_t *tls_reader_create(chunk_t data)
178 {
179 private_tls_reader_t *this;
180
181 INIT(this,
182 .public = {
183 .remaining = _remaining,
184 .peek = _peek,
185 .read_uint8 = _read_uint8,
186 .read_uint16 = _read_uint16,
187 .read_uint24 = _read_uint24,
188 .read_uint32 = _read_uint32,
189 .read_data = _read_data,
190 .read_data8 = _read_data8,
191 .read_data16 = _read_data16,
192 .read_data24 = _read_data24,
193 .read_data32 = _read_data32,
194 .destroy = _destroy,
195 },
196 .buf = data,
197 );
198
199 return &this->public;
200 }