1 /*
2 * Copyright (C) 2000-2012 Free Software Foundation, Inc.
3 * Copyright (C) 2016-2017 Red Hat, Inc.
4 *
5 * Author: Nikos Mavrogiannopoulos
6 *
7 * This file is part of GnuTLS.
8 *
9 * The GnuTLS is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2.1 of
12 * the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program. If not, see <https://www.gnu.org/licenses/>
21 *
22 */
23
24 #ifndef GNUTLS_LIB_STR_H
25 #define GNUTLS_LIB_STR_H
26
27 #include <config.h>
28 #include "gnutls_int.h"
29 #include "errors.h"
30 #include <datum.h>
31 #include <c-ctype.h>
32 #include "errors.h"
33
34 #ifdef HAVE_DCGETTEXT
35 # include "gettext.h"
36 # define _(String) dgettext (PACKAGE, String)
37 # define N_(String) gettext_noop (String)
38 #else
39 # define _(String) String
40 # define N_(String) String
41 #endif
42
43 int gnutls_utf8_password_normalize(const uint8_t *password, unsigned password_len,
44 gnutls_datum_t *out, unsigned flags);
45
46 #define _gnutls_utf8_password_normalize(p, plen, out, ignore_errs) \
47 gnutls_utf8_password_normalize((unsigned char*)p, plen, out, \
48 ignore_errs?(GNUTLS_UTF8_IGNORE_ERRS):0)
49
50 int _gnutls_idna_email_map(const char *input, unsigned ilen, gnutls_datum_t *output);
51 int _gnutls_idna_email_reverse_map(const char *input, unsigned ilen, gnutls_datum_t *output);
52
_gnutls_str_is_print(const char * str,unsigned size)53 inline static unsigned _gnutls_str_is_print(const char *str, unsigned size)
54 {
55 unsigned i;
56 for (i=0;i<size;i++) {
57 if (!c_isprint(str[i]))
58 return 0;
59 }
60 return 1;
61 }
62
_gnutls_dnsname_is_valid(const char * str,unsigned size)63 inline static unsigned _gnutls_dnsname_is_valid(const char *str, unsigned size)
64 {
65 unsigned i;
66 for (i=0;i<size;i++) {
67 if (!(c_isalnum(str[i]) || str[i] == '-' || str[i] == '.'))
68 return 0;
69 }
70 return 1;
71 }
72
_gnutls_has_embedded_null(const char * str,unsigned size)73 inline static bool _gnutls_has_embedded_null(const char *str, unsigned size)
74 {
75 if (strlen(str) != size)
76 return true;
77 return false;
78 }
79
80 void _gnutls_str_cpy(char *dest, size_t dest_tot_size, const char *src);
81 void _gnutls_str_cat(char *dest, size_t dest_tot_size, const char *src);
82
83 typedef struct gnutls_buffer_st {
84 uint8_t *allocd; /* pointer to allocated data */
85 uint8_t *data; /* API: pointer to data to copy from */
86 size_t max_length;
87 size_t length; /* API: current length */
88 } gnutls_buffer_st;
89
90 /* Initialize a buffer */
91 void _gnutls_buffer_init(gnutls_buffer_st *);
92
93 /* Free the data in a buffer */
94 void _gnutls_buffer_clear(gnutls_buffer_st *);
95
96 /* Set the buffer data to be of zero length */
_gnutls_buffer_reset(gnutls_buffer_st * buf)97 inline static void _gnutls_buffer_reset(gnutls_buffer_st * buf)
98 {
99 buf->data = buf->allocd;
100 buf->length = 0;
101 }
102
103 int _gnutls_buffer_resize(gnutls_buffer_st *, size_t new_size);
104
105 int _gnutls_buffer_append_str(gnutls_buffer_st *, const char *str);
106
107 #define _gnutls_buffer_append_data gnutls_buffer_append_data
108
109 #include <num.h>
110
111 int _gnutls_buffer_append_prefix(gnutls_buffer_st * buf, int pfx_size,
112 size_t data_size);
113
114 int _gnutls_buffer_append_mpi(gnutls_buffer_st * buf, int pfx_size,
115 bigint_t, int lz);
116
117 int _gnutls_buffer_append_fixed_mpi(gnutls_buffer_st * buf,
118 bigint_t mpi, unsigned size);
119
120 int _gnutls_buffer_append_data_prefix(gnutls_buffer_st * buf, int pfx_size,
121 const void *data, size_t data_size);
122 int _gnutls_buffer_pop_data(gnutls_buffer_st *, void *, size_t size);
123 void _gnutls_buffer_pop_datum(gnutls_buffer_st *, gnutls_datum_t *,
124 size_t max_size);
125
126 int _gnutls_buffer_pop_prefix8(gnutls_buffer_st *, uint8_t *, int check);
127
128 /* 32-bit prefix */
129 int _gnutls_buffer_pop_prefix32(gnutls_buffer_st * buf, size_t * data_size,
130 int check);
131
132 int _gnutls_buffer_pop_prefix24(gnutls_buffer_st * buf, size_t * data_size,
133 int check);
134
135 /* 32-bit prefix */
136 int _gnutls_buffer_pop_datum_prefix32(gnutls_buffer_st * buf,
137 gnutls_datum_t * data);
138
139 /* 16-bit prefix */
140 int _gnutls_buffer_pop_datum_prefix16(gnutls_buffer_st * buf,
141 gnutls_datum_t * data);
142
143 /* 8-bit prefix */
144 int _gnutls_buffer_pop_datum_prefix8(gnutls_buffer_st * buf,
145 gnutls_datum_t * data);
146
147 int _gnutls_buffer_to_datum(gnutls_buffer_st * str, gnutls_datum_t * data, unsigned is_str);
148
149 inline static
_gnutls_ro_buffer_from_datum(gnutls_buffer_st * str,gnutls_datum_t * data)150 void _gnutls_ro_buffer_from_datum(gnutls_buffer_st * str, gnutls_datum_t * data)
151 {
152 _gnutls_buffer_init(str);
153 str->length = data->size;
154 str->max_length = data->size;
155 str->data = data->data;
156 }
157
158 int
159 _gnutls_buffer_append_escape(gnutls_buffer_st * dest, const void *data,
160 size_t data_size, const char *invalid_chars);
161 int _gnutls_buffer_unescape(gnutls_buffer_st * dest);
162
163 #ifndef __attribute__
164 /* This feature is available in gcc versions 2.5 and later. */
165 #if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 5)
166 #define __attribute__(Spec) /* empty */
167 #endif
168 #endif
169
170 int _gnutls_buffer_append_printf(gnutls_buffer_st * dest, const char *fmt,
171 ...)
172 __attribute__ ((format(printf, 2, 3)));
173
174 void _gnutls_buffer_hexprint(gnutls_buffer_st * str,
175 const void *data, size_t len);
176 int _gnutls_buffer_base64print(gnutls_buffer_st * str,
177 const void *data, size_t len);
178 void _gnutls_buffer_hexdump(gnutls_buffer_st * str, const void *data,
179 size_t len, const char *spc);
180 void _gnutls_buffer_asciiprint(gnutls_buffer_st * str,
181 const char *data, size_t len);
182
183 char *_gnutls_bin2hex(const void *old, size_t oldlen, char *buffer,
184 size_t buffer_size, const char *separator);
185 int _gnutls_hex2bin(const char *hex_data, size_t hex_size,
186 uint8_t * bin_data, size_t * bin_size);
187
188 int _gnutls_hostname_compare(const char *certname, size_t certnamesize,
189 const char *hostname, unsigned vflags);
190
191 #define MAX_CN 256
192 #define MAX_DN 1024
193
194 #define BUFFER_APPEND(b, x, s) { \
195 ret = _gnutls_buffer_append_data(b, x, s); \
196 if (ret < 0) { \
197 gnutls_assert(); \
198 return ret; \
199 } \
200 }
201
202 /* append data prefixed with 4-bytes length field*/
203 #define BUFFER_APPEND_PFX4(b, x, s) { \
204 ret = _gnutls_buffer_append_data_prefix(b, 32, x, s); \
205 if (ret < 0) { \
206 gnutls_assert(); \
207 return ret; \
208 } \
209 }
210
211 #define BUFFER_APPEND_PFX3(b, x, s) { \
212 ret = _gnutls_buffer_append_data_prefix(b, 24, x, s); \
213 if (ret < 0) { \
214 gnutls_assert(); \
215 return ret; \
216 } \
217 }
218
219 #define BUFFER_APPEND_PFX2(b, x, s) { \
220 ret = _gnutls_buffer_append_data_prefix(b, 16, x, s); \
221 if (ret < 0) { \
222 gnutls_assert(); \
223 return ret; \
224 } \
225 }
226
227 #define BUFFER_APPEND_PFX1(b, x, s) { \
228 ret = _gnutls_buffer_append_data_prefix(b, 8, x, s); \
229 if (ret < 0) { \
230 gnutls_assert(); \
231 return ret; \
232 } \
233 }
234
235 #define BUFFER_APPEND_NUM(b, s) { \
236 ret = _gnutls_buffer_append_prefix(b, 32, s); \
237 if (ret < 0) { \
238 gnutls_assert(); \
239 return ret; \
240 } \
241 }
242
243 #define BUFFER_APPEND_TS(b, s) { \
244 ret = _gnutls_buffer_append_prefix(b, 32, (uint64_t) s.tv_sec >> 32); \
245 if (ret < 0) { \
246 gnutls_assert(); \
247 return ret; \
248 } \
249 ret = _gnutls_buffer_append_prefix(b, 32, s.tv_sec & 0xFFFFFFFF); \
250 if (ret < 0) { \
251 gnutls_assert(); \
252 return ret; \
253 } \
254 ret = _gnutls_buffer_append_prefix(b, 32, s.tv_nsec); \
255 if (ret < 0) { \
256 gnutls_assert(); \
257 return ret; \
258 } \
259 }
260
261 #define BUFFER_POP(b, x, s) { \
262 ret = _gnutls_buffer_pop_data(b, x, s); \
263 if (ret < 0) { \
264 ret = GNUTLS_E_PARSING_ERROR; \
265 gnutls_assert(); \
266 goto error; \
267 } \
268 }
269
270 #define BUFFER_POP_DATUM(b, o) { \
271 gnutls_datum_t d; \
272 ret = _gnutls_buffer_pop_datum_prefix32(b, &d); \
273 if (ret >= 0) \
274 ret = _gnutls_set_datum (o, d.data, d.size); \
275 if (ret < 0) { \
276 gnutls_assert(); \
277 goto error; \
278 } \
279 }
280
281 #define BUFFER_POP_NUM(b, o) { \
282 size_t s; \
283 ret = _gnutls_buffer_pop_prefix32(b, &s, 0); \
284 if (ret < 0) { \
285 gnutls_assert(); \
286 goto error; \
287 } \
288 o = s; \
289 }
290
291 #define BUFFER_POP_CAST_NUM(b, o) { \
292 size_t s; \
293 ret = _gnutls_buffer_pop_prefix32(b, &s, 0); \
294 if (ret < 0) { \
295 gnutls_assert(); \
296 goto error; \
297 } \
298 o = (void *) (intptr_t)(s); \
299 }
300
301 #define BUFFER_POP_TS(b, o) { \
302 size_t s; \
303 uint64_t v; \
304 ret = _gnutls_buffer_pop_prefix32(b, &s, 0); \
305 if (ret < 0) { \
306 gnutls_assert(); \
307 goto error; \
308 } \
309 v = s; \
310 ret = _gnutls_buffer_pop_prefix32(b, &s, 0); \
311 if (ret < 0) { \
312 gnutls_assert(); \
313 goto error; \
314 } \
315 v = (v << 32) | s; \
316 ret = _gnutls_buffer_pop_prefix32(b, &s, 0); \
317 if (ret < 0) { \
318 gnutls_assert(); \
319 goto error; \
320 } \
321 o.tv_sec = v; \
322 o.tv_nsec = s; \
323 }
324
325 #endif /* GNUTLS_LIB_STR_H */
326