View | Details | Raw Unified | Return to bug 2058 | Differences between
and this patch

Collapse All | Expand All

(-)sshconnect2.c (-2 / +34 lines)
Lines 39-44 Link Here
39
#include <pwd.h>
39
#include <pwd.h>
40
#include <unistd.h>
40
#include <unistd.h>
41
#include <vis.h>
41
#include <vis.h>
42
#include <locale.h>
43
#include <langinfo.h>
42
44
43
#include "xmalloc.h"
45
#include "xmalloc.h"
44
#include "ssh.h"
46
#include "ssh.h"
Lines 462-482 input_userauth_error(int type, u_int32_t Link Here
462
	    "type %d", type);
464
	    "type %d", type);
463
}
465
}
464
466
467
/* Check whether we can display UTF-8 safely */
468
static int
469
utf8_ok(void)
470
{
471
	static int ret = -1;
472
	char *cp;
473
474
	if (ret == -1) {
475
		setlocale(LC_CTYPE, "");
476
		cp = nl_langinfo(CODESET);
477
		ret = strcmp(cp, "UTF-8") == 0;
478
	}
479
	return ret;
480
}
481
465
/* ARGSUSED */
482
/* ARGSUSED */
466
void
483
void
467
input_userauth_banner(int type, u_int32_t seq, void *ctxt)
484
input_userauth_banner(int type, u_int32_t seq, void *ctxt)
468
{
485
{
469
	char *msg, *raw, *lang;
486
	char *msg, *raw, *lang;
470
	u_int len;
487
	u_int done, len;
471
488
472
	debug3("input_userauth_banner");
489
	debug3("input_userauth_banner");
490
473
	raw = packet_get_string(&len);
491
	raw = packet_get_string(&len);
474
	lang = packet_get_string(NULL);
492
	lang = packet_get_string(NULL);
475
	if (len > 0 && options.log_level >= SYSLOG_LEVEL_INFO) {
493
	if (len > 0 && options.log_level >= SYSLOG_LEVEL_INFO) {
476
		if (len > 65536)
494
		if (len > 65536)
477
			len = 65536;
495
			len = 65536;
478
		msg = xmalloc(len * 4 + 1); /* max expansion from strnvis() */
496
		msg = xmalloc(len * 4 + 1); /* max expansion from strnvis() */
479
		strnvis(msg, raw, len * 4 + 1, VIS_SAFE|VIS_OCTAL|VIS_NOSLASH);
497
		done = 0;
498
		if (utf8_ok()) {
499
			if (ssh_utf8_stringprep(raw, msg, len * 4 + 1) == 0)
500
				done = 1;
501
			else
502
				debug2("%s: UTF8 stringprep failed", __func__);
503
		}
504
		/*
505
		 * Fallback to strnvis if UTF8 display not supported or
506
		 * conversion failed.
507
		 */
508
		if (!done) {
509
			strnvis(msg, raw, len * 4 + 1,
510
			    VIS_SAFE|VIS_OCTAL|VIS_NOSLASH);
511
		}
480
		fprintf(stderr, "%s", msg);
512
		fprintf(stderr, "%s", msg);
481
		free(msg);
513
		free(msg);
482
	}
514
	}
(-)misc.h (+3 lines)
Lines 102-105 char *read_passphrase(const char *, int) Link Here
102
int	 ask_permission(const char *, ...) __attribute__((format(printf, 1, 2)));
102
int	 ask_permission(const char *, ...) __attribute__((format(printf, 1, 2)));
103
int	 read_keyfile_line(FILE *, const char *, char *, size_t, u_long *);
103
int	 read_keyfile_line(FILE *, const char *, char *, size_t, u_long *);
104
104
105
/* stringprep.c */
106
int ssh_utf8_stringprep(const char *, char *, size_t);
107
105
#endif /* _MISC_H */
108
#endif /* _MISC_H */
(-)stringprep.c (+949 lines)
Added Link Here
1
/*
2
 * Copyright (c) 2013 Damien Miller <djm@mindrot.org>
3
 *
4
 * Permission to use, copy, modify, and distribute this software for any
5
 * purpose with or without fee is hereby granted, provided that the above
6
 * copyright notice and this permission notice appear in all copies.
7
 *
8
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15
 */
16
17
/*
18
 * This is a simple RFC3454 stringprep profile to sanitise UTF-8 strings
19
 * from untrusted sources.
20
 *
21
 * It is intended to be used prior to display of untrusted strings only.
22
 * It should not be used for logging because of bi-di ambiguity. It
23
 * should also not be used in any case where lack of normalisation may
24
 * cause problems.
25
 *
26
 * This profile uses the prohibition and mapping tables from RFC3454
27
 * (listed below) but the unassigned character table has been updated to
28
 * Unicode 6.2. It uses a local whitelist of whitespace characters (\n,
29
 * \a and \t). Unicode normalisation and bi-di testing are not used.
30
 *
31
 * XXX: implement bi-di handling (needed for logs)
32
 * XXX: implement KC normalisation (needed for passing to libs/syscalls)
33
 */
34
35
#include <sys/types.h>
36
#include <stdio.h>
37
#include <stdlib.h>
38
#include <string.h>
39
#include <limits.h>
40
#include <ctype.h>
41
42
#include "misc.h"
43
44
#define BANNED_RFC3629 0
45
#define UTF8_DEBUG 0
46
47
#if UTF8_DEBUG
48
# define DBG(x) \
49
	do { \
50
		printf("%s:%d %s: ", __FILE__, __LINE__, __func__); \
51
		printf x; \
52
		printf("\n"); \
53
		fflush(stdout); \
54
	} while (0)
55
#else
56
# define DBG(x)
57
#endif
58
59
struct u32_range {
60
	u_int32_t lo, hi;  /* Inclusive */
61
};
62
63
/* RFC3454 Table A.1 */
64
static const struct u32_range unassigned[] = {
65
	{ 0x0378, 0x0379 },
66
	{ 0x037F, 0x0383 },
67
	{ 0x038B, 0x038B },
68
	{ 0x038D, 0x038D },
69
	{ 0x03A2, 0x03A2 },
70
	{ 0x0528, 0x0530 },
71
	{ 0x0557, 0x0558 },
72
	{ 0x0560, 0x0560 },
73
	{ 0x0588, 0x0588 },
74
	{ 0x058B, 0x058E },
75
	{ 0x0590, 0x0590 },
76
	{ 0x05C8, 0x05CF },
77
	{ 0x05EB, 0x05EF },
78
	{ 0x05F5, 0x05FF },
79
	{ 0x0605, 0x0605 },
80
	{ 0x061C, 0x061D },
81
	{ 0x070E, 0x070E },
82
	{ 0x074B, 0x074C },
83
	{ 0x07B2, 0x07BF },
84
	{ 0x07FB, 0x07FF },
85
	{ 0x082E, 0x082F },
86
	{ 0x083F, 0x083F },
87
	{ 0x085C, 0x085D },
88
	{ 0x085F, 0x089F },
89
	{ 0x08A1, 0x08A1 },
90
	{ 0x08AD, 0x08E3 },
91
	{ 0x08FF, 0x08FF },
92
	{ 0x0978, 0x0978 },
93
	{ 0x0980, 0x0980 },
94
	{ 0x0984, 0x0984 },
95
	{ 0x098D, 0x098E },
96
	{ 0x0991, 0x0992 },
97
	{ 0x09A9, 0x09A9 },
98
	{ 0x09B1, 0x09B1 },
99
	{ 0x09B3, 0x09B5 },
100
	{ 0x09BA, 0x09BB },
101
	{ 0x09C5, 0x09C6 },
102
	{ 0x09C9, 0x09CA },
103
	{ 0x09CF, 0x09D6 },
104
	{ 0x09D8, 0x09DB },
105
	{ 0x09DE, 0x09DE },
106
	{ 0x09E4, 0x09E5 },
107
	{ 0x09FC, 0x0A00 },
108
	{ 0x0A04, 0x0A04 },
109
	{ 0x0A0B, 0x0A0E },
110
	{ 0x0A11, 0x0A12 },
111
	{ 0x0A29, 0x0A29 },
112
	{ 0x0A31, 0x0A31 },
113
	{ 0x0A34, 0x0A34 },
114
	{ 0x0A37, 0x0A37 },
115
	{ 0x0A3A, 0x0A3B },
116
	{ 0x0A3D, 0x0A3D },
117
	{ 0x0A43, 0x0A46 },
118
	{ 0x0A49, 0x0A4A },
119
	{ 0x0A4E, 0x0A50 },
120
	{ 0x0A52, 0x0A58 },
121
	{ 0x0A5D, 0x0A5D },
122
	{ 0x0A5F, 0x0A65 },
123
	{ 0x0A76, 0x0A80 },
124
	{ 0x0A84, 0x0A84 },
125
	{ 0x0A8E, 0x0A8E },
126
	{ 0x0A92, 0x0A92 },
127
	{ 0x0AA9, 0x0AA9 },
128
	{ 0x0AB1, 0x0AB1 },
129
	{ 0x0AB4, 0x0AB4 },
130
	{ 0x0ABA, 0x0ABB },
131
	{ 0x0AC6, 0x0AC6 },
132
	{ 0x0ACA, 0x0ACA },
133
	{ 0x0ACE, 0x0ACF },
134
	{ 0x0AD1, 0x0ADF },
135
	{ 0x0AE4, 0x0AE5 },
136
	{ 0x0AF2, 0x0B00 },
137
	{ 0x0B04, 0x0B04 },
138
	{ 0x0B0D, 0x0B0E },
139
	{ 0x0B11, 0x0B12 },
140
	{ 0x0B29, 0x0B29 },
141
	{ 0x0B31, 0x0B31 },
142
	{ 0x0B34, 0x0B34 },
143
	{ 0x0B3A, 0x0B3B },
144
	{ 0x0B45, 0x0B46 },
145
	{ 0x0B49, 0x0B4A },
146
	{ 0x0B4E, 0x0B55 },
147
	{ 0x0B58, 0x0B5B },
148
	{ 0x0B5E, 0x0B5E },
149
	{ 0x0B64, 0x0B65 },
150
	{ 0x0B78, 0x0B81 },
151
	{ 0x0B84, 0x0B84 },
152
	{ 0x0B8B, 0x0B8D },
153
	{ 0x0B91, 0x0B91 },
154
	{ 0x0B96, 0x0B98 },
155
	{ 0x0B9B, 0x0B9B },
156
	{ 0x0B9D, 0x0B9D },
157
	{ 0x0BA0, 0x0BA2 },
158
	{ 0x0BA5, 0x0BA7 },
159
	{ 0x0BAB, 0x0BAD },
160
	{ 0x0BBA, 0x0BBD },
161
	{ 0x0BC3, 0x0BC5 },
162
	{ 0x0BC9, 0x0BC9 },
163
	{ 0x0BCE, 0x0BCF },
164
	{ 0x0BD1, 0x0BD6 },
165
	{ 0x0BD8, 0x0BE5 },
166
	{ 0x0BFB, 0x0C00 },
167
	{ 0x0C04, 0x0C04 },
168
	{ 0x0C0D, 0x0C0D },
169
	{ 0x0C11, 0x0C11 },
170
	{ 0x0C29, 0x0C29 },
171
	{ 0x0C34, 0x0C34 },
172
	{ 0x0C3A, 0x0C3C },
173
	{ 0x0C45, 0x0C45 },
174
	{ 0x0C49, 0x0C49 },
175
	{ 0x0C4E, 0x0C54 },
176
	{ 0x0C57, 0x0C57 },
177
	{ 0x0C5A, 0x0C5F },
178
	{ 0x0C64, 0x0C65 },
179
	{ 0x0C70, 0x0C77 },
180
	{ 0x0C80, 0x0C81 },
181
	{ 0x0C84, 0x0C84 },
182
	{ 0x0C8D, 0x0C8D },
183
	{ 0x0C91, 0x0C91 },
184
	{ 0x0CA9, 0x0CA9 },
185
	{ 0x0CB4, 0x0CB4 },
186
	{ 0x0CBA, 0x0CBB },
187
	{ 0x0CC5, 0x0CC5 },
188
	{ 0x0CC9, 0x0CC9 },
189
	{ 0x0CCE, 0x0CD4 },
190
	{ 0x0CD7, 0x0CDD },
191
	{ 0x0CDF, 0x0CDF },
192
	{ 0x0CE4, 0x0CE5 },
193
	{ 0x0CF0, 0x0CF0 },
194
	{ 0x0CF3, 0x0D01 },
195
	{ 0x0D04, 0x0D04 },
196
	{ 0x0D0D, 0x0D0D },
197
	{ 0x0D11, 0x0D11 },
198
	{ 0x0D3B, 0x0D3C },
199
	{ 0x0D45, 0x0D45 },
200
	{ 0x0D49, 0x0D49 },
201
	{ 0x0D4F, 0x0D56 },
202
	{ 0x0D58, 0x0D5F },
203
	{ 0x0D64, 0x0D65 },
204
	{ 0x0D76, 0x0D78 },
205
	{ 0x0D80, 0x0D81 },
206
	{ 0x0D84, 0x0D84 },
207
	{ 0x0D97, 0x0D99 },
208
	{ 0x0DB2, 0x0DB2 },
209
	{ 0x0DBC, 0x0DBC },
210
	{ 0x0DBE, 0x0DBF },
211
	{ 0x0DC7, 0x0DC9 },
212
	{ 0x0DCB, 0x0DCE },
213
	{ 0x0DD5, 0x0DD5 },
214
	{ 0x0DD7, 0x0DD7 },
215
	{ 0x0DE0, 0x0DF1 },
216
	{ 0x0DF5, 0x0E00 },
217
	{ 0x0E3B, 0x0E3E },
218
	{ 0x0E5C, 0x0E80 },
219
	{ 0x0E83, 0x0E83 },
220
	{ 0x0E85, 0x0E86 },
221
	{ 0x0E89, 0x0E89 },
222
	{ 0x0E8B, 0x0E8C },
223
	{ 0x0E8E, 0x0E93 },
224
	{ 0x0E98, 0x0E98 },
225
	{ 0x0EA0, 0x0EA0 },
226
	{ 0x0EA4, 0x0EA4 },
227
	{ 0x0EA6, 0x0EA6 },
228
	{ 0x0EA8, 0x0EA9 },
229
	{ 0x0EAC, 0x0EAC },
230
	{ 0x0EBA, 0x0EBA },
231
	{ 0x0EBE, 0x0EBF },
232
	{ 0x0EC5, 0x0EC5 },
233
	{ 0x0EC7, 0x0EC7 },
234
	{ 0x0ECE, 0x0ECF },
235
	{ 0x0EDA, 0x0EDB },
236
	{ 0x0EE0, 0x0EFF },
237
	{ 0x0F48, 0x0F48 },
238
	{ 0x0F6D, 0x0F70 },
239
	{ 0x0F98, 0x0F98 },
240
	{ 0x0FBD, 0x0FBD },
241
	{ 0x0FCD, 0x0FCD },
242
	{ 0x0FDB, 0x0FFF },
243
	{ 0x10C6, 0x10C6 },
244
	{ 0x10C8, 0x10CC },
245
	{ 0x10CE, 0x10CF },
246
	{ 0x1249, 0x1249 },
247
	{ 0x124E, 0x124F },
248
	{ 0x1257, 0x1257 },
249
	{ 0x1259, 0x1259 },
250
	{ 0x125E, 0x125F },
251
	{ 0x1289, 0x1289 },
252
	{ 0x128E, 0x128F },
253
	{ 0x12B1, 0x12B1 },
254
	{ 0x12B6, 0x12B7 },
255
	{ 0x12BF, 0x12BF },
256
	{ 0x12C1, 0x12C1 },
257
	{ 0x12C6, 0x12C7 },
258
	{ 0x12D7, 0x12D7 },
259
	{ 0x1311, 0x1311 },
260
	{ 0x1316, 0x1317 },
261
	{ 0x135B, 0x135C },
262
	{ 0x137D, 0x137F },
263
	{ 0x139A, 0x139F },
264
	{ 0x13F5, 0x13FF },
265
	{ 0x169D, 0x169F },
266
	{ 0x16F1, 0x16FF },
267
	{ 0x170D, 0x170D },
268
	{ 0x1715, 0x171F },
269
	{ 0x1737, 0x173F },
270
	{ 0x1754, 0x175F },
271
	{ 0x176D, 0x176D },
272
	{ 0x1771, 0x1771 },
273
	{ 0x1774, 0x177F },
274
	{ 0x17DE, 0x17DF },
275
	{ 0x17EA, 0x17EF },
276
	{ 0x17FA, 0x17FF },
277
	{ 0x180F, 0x180F },
278
	{ 0x181A, 0x181F },
279
	{ 0x1878, 0x187F },
280
	{ 0x18AB, 0x18AF },
281
	{ 0x18F6, 0x18FF },
282
	{ 0x191D, 0x191F },
283
	{ 0x192C, 0x192F },
284
	{ 0x193C, 0x193F },
285
	{ 0x1941, 0x1943 },
286
	{ 0x196E, 0x196F },
287
	{ 0x1975, 0x197F },
288
	{ 0x19AC, 0x19AF },
289
	{ 0x19CA, 0x19CF },
290
	{ 0x19DB, 0x19DD },
291
	{ 0x1A1C, 0x1A1D },
292
	{ 0x1A5F, 0x1A5F },
293
	{ 0x1A7D, 0x1A7E },
294
	{ 0x1A8A, 0x1A8F },
295
	{ 0x1A9A, 0x1A9F },
296
	{ 0x1AAE, 0x1AFF },
297
	{ 0x1B4C, 0x1B4F },
298
	{ 0x1B7D, 0x1B7F },
299
	{ 0x1BF4, 0x1BFB },
300
	{ 0x1C38, 0x1C3A },
301
	{ 0x1C4A, 0x1C4C },
302
	{ 0x1C80, 0x1CBF },
303
	{ 0x1CC8, 0x1CCF },
304
	{ 0x1CF7, 0x1CFF },
305
	{ 0x1DE7, 0x1DFB },
306
	{ 0x1F16, 0x1F17 },
307
	{ 0x1F1E, 0x1F1F },
308
	{ 0x1F46, 0x1F47 },
309
	{ 0x1F4E, 0x1F4F },
310
	{ 0x1F58, 0x1F58 },
311
	{ 0x1F5A, 0x1F5A },
312
	{ 0x1F5C, 0x1F5C },
313
	{ 0x1F5E, 0x1F5E },
314
	{ 0x1F7E, 0x1F7F },
315
	{ 0x1FB5, 0x1FB5 },
316
	{ 0x1FC5, 0x1FC5 },
317
	{ 0x1FD4, 0x1FD5 },
318
	{ 0x1FDC, 0x1FDC },
319
	{ 0x1FF0, 0x1FF1 },
320
	{ 0x1FF5, 0x1FF5 },
321
	{ 0x1FFF, 0x1FFF },
322
	{ 0x2065, 0x2069 },
323
	{ 0x2072, 0x2073 },
324
	{ 0x208F, 0x208F },
325
	{ 0x209D, 0x209F },
326
	{ 0x20BB, 0x20CF },
327
	{ 0x20F1, 0x20FF },
328
	{ 0x218A, 0x218F },
329
	{ 0x23F4, 0x23FF },
330
	{ 0x2427, 0x243F },
331
	{ 0x244B, 0x245F },
332
	{ 0x2700, 0x2700 },
333
	{ 0x2B4D, 0x2B4F },
334
	{ 0x2B5A, 0x2BFF },
335
	{ 0x2C2F, 0x2C2F },
336
	{ 0x2C5F, 0x2C5F },
337
	{ 0x2CF4, 0x2CF8 },
338
	{ 0x2D26, 0x2D26 },
339
	{ 0x2D28, 0x2D2C },
340
	{ 0x2D2E, 0x2D2F },
341
	{ 0x2D68, 0x2D6E },
342
	{ 0x2D71, 0x2D7E },
343
	{ 0x2D97, 0x2D9F },
344
	{ 0x2DA7, 0x2DA7 },
345
	{ 0x2DAF, 0x2DAF },
346
	{ 0x2DB7, 0x2DB7 },
347
	{ 0x2DBF, 0x2DBF },
348
	{ 0x2DC7, 0x2DC7 },
349
	{ 0x2DCF, 0x2DCF },
350
	{ 0x2DD7, 0x2DD7 },
351
	{ 0x2DDF, 0x2DDF },
352
	{ 0x2E3C, 0x2E7F },
353
	{ 0x2E9A, 0x2E9A },
354
	{ 0x2EF4, 0x2EFF },
355
	{ 0x2FD6, 0x2FEF },
356
	{ 0x2FFC, 0x2FFF },
357
	{ 0x3040, 0x3040 },
358
	{ 0x3097, 0x3098 },
359
	{ 0x3100, 0x3104 },
360
	{ 0x312E, 0x3130 },
361
	{ 0x318F, 0x318F },
362
	{ 0x31BB, 0x31BF },
363
	{ 0x31E4, 0x31EF },
364
	{ 0x321F, 0x321F },
365
	{ 0x32FF, 0x32FF },
366
	{ 0x4DB6, 0x4DBF },
367
	{ 0x9FA6, 0x9FCB },
368
	{ 0x9FCD, 0x9FFF },
369
	{ 0xA48D, 0xA48F },
370
	{ 0xA4C7, 0xA4CF },
371
	{ 0xA62C, 0xA63F },
372
	{ 0xA698, 0xA69E },
373
	{ 0xA6F8, 0xA6FF },
374
	{ 0xA78F, 0xA78F },
375
	{ 0xA794, 0xA79F },
376
	{ 0xA7AB, 0xA7F7 },
377
	{ 0xA82C, 0xA82F },
378
	{ 0xA83A, 0xA83F },
379
	{ 0xA878, 0xA87F },
380
	{ 0xA8C5, 0xA8CD },
381
	{ 0xA8DA, 0xA8DF },
382
	{ 0xA8FC, 0xA8FF },
383
	{ 0xA954, 0xA95E },
384
	{ 0xA97D, 0xA97F },
385
	{ 0xA9CE, 0xA9CE },
386
	{ 0xA9DA, 0xA9DD },
387
	{ 0xA9E0, 0xA9FF },
388
	{ 0xAA37, 0xAA3F },
389
	{ 0xAA4E, 0xAA4F },
390
	{ 0xAA5A, 0xAA5B },
391
	{ 0xAA7C, 0xAA7F },
392
	{ 0xAAC3, 0xAADA },
393
	{ 0xAAF7, 0xAB00 },
394
	{ 0xAB07, 0xAB08 },
395
	{ 0xAB0F, 0xAB10 },
396
	{ 0xAB17, 0xAB1F },
397
	{ 0xAB27, 0xAB27 },
398
	{ 0xAB2F, 0xABBF },
399
	{ 0xABEE, 0xABEF },
400
	{ 0xABFA, 0xABFF },
401
	{ 0xD7A4, 0xD7AF },
402
	{ 0xD7C7, 0xD7CA },
403
	{ 0xD7FC, 0xD7FF },
404
	{ 0xFA6E, 0xFA6F },
405
	{ 0xFADA, 0xFAFF },
406
	{ 0xFB07, 0xFB12 },
407
	{ 0xFB18, 0xFB1C },
408
	{ 0xFB37, 0xFB37 },
409
	{ 0xFB3D, 0xFB3D },
410
	{ 0xFB3F, 0xFB3F },
411
	{ 0xFB42, 0xFB42 },
412
	{ 0xFB45, 0xFB45 },
413
	{ 0xFBC2, 0xFBD2 },
414
	{ 0xFD40, 0xFD4F },
415
	{ 0xFD90, 0xFD91 },
416
	{ 0xFDC8, 0xFDCF },
417
	{ 0xFDFE, 0xFDFF },
418
	{ 0xFE1A, 0xFE1F },
419
	{ 0xFE27, 0xFE2F },
420
	{ 0xFE53, 0xFE53 },
421
	{ 0xFE67, 0xFE67 },
422
	{ 0xFE6C, 0xFE6F },
423
	{ 0xFE75, 0xFE75 },
424
	{ 0xFEFD, 0xFEFE },
425
	{ 0xFF00, 0xFF00 },
426
	{ 0xFFBF, 0xFFC1 },
427
	{ 0xFFC8, 0xFFC9 },
428
	{ 0xFFD0, 0xFFD1 },
429
	{ 0xFFD8, 0xFFD9 },
430
	{ 0xFFDD, 0xFFDF },
431
	{ 0xFFE7, 0xFFE7 },
432
	{ 0xFFEF, 0xFFF8 },
433
	{ 0x1000C, 0x1000C },
434
	{ 0x10027, 0x10027 },
435
	{ 0x1003B, 0x1003B },
436
	{ 0x1003E, 0x1003E },
437
	{ 0x1004E, 0x1004F },
438
	{ 0x1005E, 0x1007F },
439
	{ 0x100FB, 0x100FF },
440
	{ 0x10103, 0x10106 },
441
	{ 0x10134, 0x10136 },
442
	{ 0x1018B, 0x1018F },
443
	{ 0x1019C, 0x101CF },
444
	{ 0x101FE, 0x1027F },
445
	{ 0x1029D, 0x1029F },
446
	{ 0x102D1, 0x102FF },
447
	{ 0x1031F, 0x1031F },
448
	{ 0x10324, 0x1032F },
449
	{ 0x1034B, 0x1037F },
450
	{ 0x1039E, 0x1039E },
451
	{ 0x103C4, 0x103C7 },
452
	{ 0x103D6, 0x103FF },
453
	{ 0x1049E, 0x1049F },
454
	{ 0x104AA, 0x107FF },
455
	{ 0x10806, 0x10807 },
456
	{ 0x10809, 0x10809 },
457
	{ 0x10836, 0x10836 },
458
	{ 0x10839, 0x1083B },
459
	{ 0x1083D, 0x1083E },
460
	{ 0x10856, 0x10856 },
461
	{ 0x10860, 0x108FF },
462
	{ 0x1091C, 0x1091E },
463
	{ 0x1093A, 0x1093E },
464
	{ 0x10940, 0x1097F },
465
	{ 0x109B8, 0x109BD },
466
	{ 0x109C0, 0x109FF },
467
	{ 0x10A04, 0x10A04 },
468
	{ 0x10A07, 0x10A0B },
469
	{ 0x10A14, 0x10A14 },
470
	{ 0x10A18, 0x10A18 },
471
	{ 0x10A34, 0x10A37 },
472
	{ 0x10A3B, 0x10A3E },
473
	{ 0x10A48, 0x10A4F },
474
	{ 0x10A59, 0x10A5F },
475
	{ 0x10A80, 0x10AFF },
476
	{ 0x10B36, 0x10B38 },
477
	{ 0x10B56, 0x10B57 },
478
	{ 0x10B73, 0x10B77 },
479
	{ 0x10B80, 0x10BFF },
480
	{ 0x10C49, 0x10E5F },
481
	{ 0x10E7F, 0x10FFF },
482
	{ 0x1104E, 0x11051 },
483
	{ 0x11070, 0x1107F },
484
	{ 0x110C2, 0x110CF },
485
	{ 0x110E9, 0x110EF },
486
	{ 0x110FA, 0x110FF },
487
	{ 0x11135, 0x11135 },
488
	{ 0x11144, 0x1117F },
489
	{ 0x111C9, 0x111CF },
490
	{ 0x111DA, 0x1167F },
491
	{ 0x116B8, 0x116BF },
492
	{ 0x116CA, 0x11FFF },
493
	{ 0x1236F, 0x123FF },
494
	{ 0x12463, 0x1246F },
495
	{ 0x12474, 0x12FFF },
496
	{ 0x1342F, 0x167FF },
497
	{ 0x16A39, 0x16EFF },
498
	{ 0x16F45, 0x16F4F },
499
	{ 0x16F7F, 0x16F8E },
500
	{ 0x16FA0, 0x1AFFF },
501
	{ 0x1B002, 0x1CFFF },
502
	{ 0x1D0F6, 0x1D0FF },
503
	{ 0x1D127, 0x1D128 },
504
	{ 0x1D1DE, 0x1D1FF },
505
	{ 0x1D246, 0x1D2FF },
506
	{ 0x1D357, 0x1D35F },
507
	{ 0x1D372, 0x1D3FF },
508
	{ 0x1D455, 0x1D455 },
509
	{ 0x1D49D, 0x1D49D },
510
	{ 0x1D4A0, 0x1D4A1 },
511
	{ 0x1D4A3, 0x1D4A4 },
512
	{ 0x1D4A7, 0x1D4A8 },
513
	{ 0x1D4AD, 0x1D4AD },
514
	{ 0x1D4BA, 0x1D4BA },
515
	{ 0x1D4BC, 0x1D4BC },
516
	{ 0x1D4C4, 0x1D4C4 },
517
	{ 0x1D506, 0x1D506 },
518
	{ 0x1D50B, 0x1D50C },
519
	{ 0x1D515, 0x1D515 },
520
	{ 0x1D51D, 0x1D51D },
521
	{ 0x1D53A, 0x1D53A },
522
	{ 0x1D53F, 0x1D53F },
523
	{ 0x1D545, 0x1D545 },
524
	{ 0x1D547, 0x1D549 },
525
	{ 0x1D551, 0x1D551 },
526
	{ 0x1D6A6, 0x1D6A7 },
527
	{ 0x1D7CC, 0x1D7CD },
528
	{ 0x1D800, 0x1EDFF },
529
	{ 0x1EE04, 0x1EE04 },
530
	{ 0x1EE20, 0x1EE20 },
531
	{ 0x1EE23, 0x1EE23 },
532
	{ 0x1EE25, 0x1EE26 },
533
	{ 0x1EE28, 0x1EE28 },
534
	{ 0x1EE33, 0x1EE33 },
535
	{ 0x1EE38, 0x1EE38 },
536
	{ 0x1EE3A, 0x1EE3A },
537
	{ 0x1EE3C, 0x1EE41 },
538
	{ 0x1EE43, 0x1EE46 },
539
	{ 0x1EE48, 0x1EE48 },
540
	{ 0x1EE4A, 0x1EE4A },
541
	{ 0x1EE4C, 0x1EE4C },
542
	{ 0x1EE50, 0x1EE50 },
543
	{ 0x1EE53, 0x1EE53 },
544
	{ 0x1EE55, 0x1EE56 },
545
	{ 0x1EE58, 0x1EE58 },
546
	{ 0x1EE5A, 0x1EE5A },
547
	{ 0x1EE5C, 0x1EE5C },
548
	{ 0x1EE5E, 0x1EE5E },
549
	{ 0x1EE60, 0x1EE60 },
550
	{ 0x1EE63, 0x1EE63 },
551
	{ 0x1EE65, 0x1EE66 },
552
	{ 0x1EE6B, 0x1EE6B },
553
	{ 0x1EE73, 0x1EE73 },
554
	{ 0x1EE78, 0x1EE78 },
555
	{ 0x1EE7D, 0x1EE7D },
556
	{ 0x1EE7F, 0x1EE7F },
557
	{ 0x1EE8A, 0x1EE8A },
558
	{ 0x1EE9C, 0x1EEA0 },
559
	{ 0x1EEA4, 0x1EEA4 },
560
	{ 0x1EEAA, 0x1EEAA },
561
	{ 0x1EEBC, 0x1EEEF },
562
	{ 0x1EEF2, 0x1EFFF },
563
	{ 0x1F02C, 0x1F02F },
564
	{ 0x1F094, 0x1F09F },
565
	{ 0x1F0AF, 0x1F0B0 },
566
	{ 0x1F0BF, 0x1F0C0 },
567
	{ 0x1F0D0, 0x1F0D0 },
568
	{ 0x1F0E0, 0x1F0FF },
569
	{ 0x1F10B, 0x1F10F },
570
	{ 0x1F12F, 0x1F12F },
571
	{ 0x1F16C, 0x1F16F },
572
	{ 0x1F19B, 0x1F1E5 },
573
	{ 0x1F203, 0x1F20F },
574
	{ 0x1F23B, 0x1F23F },
575
	{ 0x1F249, 0x1F24F },
576
	{ 0x1F252, 0x1F2FF },
577
	{ 0x1F321, 0x1F32F },
578
	{ 0x1F336, 0x1F336 },
579
	{ 0x1F37D, 0x1F37F },
580
	{ 0x1F394, 0x1F39F },
581
	{ 0x1F3C5, 0x1F3C5 },
582
	{ 0x1F3CB, 0x1F3DF },
583
	{ 0x1F3F1, 0x1F3FF },
584
	{ 0x1F43F, 0x1F43F },
585
	{ 0x1F441, 0x1F441 },
586
	{ 0x1F4F8, 0x1F4F8 },
587
	{ 0x1F4FD, 0x1F4FF },
588
	{ 0x1F53E, 0x1F53F },
589
	{ 0x1F544, 0x1F54F },
590
	{ 0x1F568, 0x1F5FA },
591
	{ 0x1F641, 0x1F644 },
592
	{ 0x1F650, 0x1F67F },
593
	{ 0x1F6C6, 0x1F6FF },
594
	{ 0x1F774, 0x1FFFD },
595
	{ 0x2A6D7, 0x2A6FF },
596
	{ 0x2A701, 0x2B733 },
597
	{ 0x2B735, 0x2B73F },
598
	{ 0x2B741, 0x2B81C },
599
	{ 0x2B81E, 0x2F7FF },
600
	{ 0x2FA1E, 0x2FFFD },
601
	{ 0x30000, 0x3FFFD },
602
	{ 0x40000, 0x4FFFD },
603
	{ 0x50000, 0x5FFFD },
604
	{ 0x60000, 0x6FFFD },
605
	{ 0x70000, 0x7FFFD },
606
	{ 0x80000, 0x8FFFD },
607
	{ 0x90000, 0x9FFFD },
608
	{ 0xA0000, 0xAFFFD },
609
	{ 0xB0000, 0xBFFFD },
610
	{ 0xC0000, 0xCFFFD },
611
	{ 0xD0000, 0xDFFFD },
612
	{ 0xE0000, 0xE0000 },
613
	{ 0xE0002, 0xE001F },
614
	{ 0xE0080, 0xE00FF },
615
	{ 0xE01F0, 0xEFFFD },
616
};
617
618
/* RFC3454 Table B.1 */
619
static const struct u32_range map_to_nothing[] = {
620
	{ 0x00AD, 0x00AD },
621
	{ 0x034F, 0x034F },
622
	{ 0x1806, 0x1806 },
623
	{ 0x180B, 0x180D },
624
	{ 0x200B, 0x200D },
625
	{ 0x2060, 0x2060 },
626
	{ 0xFE00, 0xFE0F },
627
	{ 0xFEFF, 0xFEFF },
628
};
629
630
/* Local: allow tab, CR and LF */
631
static const struct u32_range whitelist[] = {
632
	{ 0x09, 0x00 },
633
	{ 0x0a, 0x0a },
634
	{ 0x0d, 0x0d },
635
};
636
637
/* RFC3454 Tables in appendix C */
638
static const struct u32_range prohibited[] = {
639
	/* C.2.1 ASCII control characters */
640
	{ 0x0000, 0x001F },
641
	{ 0x007F, 0x007F },
642
	/* C.2.2 Non-ASCII control characters */
643
	{ 0x0080, 0x009F },
644
	{ 0x06DD, 0x06DD },
645
	{ 0x070F, 0x070F },
646
	{ 0x180E, 0x180E },
647
	{ 0x200C, 0x200C },
648
	{ 0x200D, 0x200D },
649
	{ 0x2028, 0x2028 },
650
	{ 0x2029, 0x2029 },
651
	{ 0x2060, 0x2060 },
652
	{ 0x2061, 0x2061 },
653
	{ 0x2062, 0x2062 },
654
	{ 0x2063, 0x2063 },
655
	{ 0x206A, 0x206F },
656
	{ 0xFEFF, 0xFEFF },
657
	{ 0xFFF9, 0xFFFC },
658
	{ 0x1D173, 0x1D17A },
659
	/* C.3 Private use */
660
	{ 0xE000, 0xF8FF },
661
	{ 0xF0000, 0xFFFFD },
662
	{ 0x100000, 0x10FFFD },
663
	/* C.4 Non-character code points */
664
	{ 0xFDD0, 0xFDEF },
665
	{ 0xFFFE, 0xFFFF },
666
	{ 0x1FFFE, 0x1FFFF },
667
	{ 0x2FFFE, 0x2FFFF },
668
	{ 0x3FFFE, 0x3FFFF },
669
	{ 0x4FFFE, 0x4FFFF },
670
	{ 0x5FFFE, 0x5FFFF },
671
	{ 0x6FFFE, 0x6FFFF },
672
	{ 0x7FFFE, 0x7FFFF },
673
	{ 0x8FFFE, 0x8FFFF },
674
	{ 0x9FFFE, 0x9FFFF },
675
	{ 0xAFFFE, 0xAFFFF },
676
	{ 0xBFFFE, 0xBFFFF },
677
	{ 0xCFFFE, 0xCFFFF },
678
	{ 0xDFFFE, 0xDFFFF },
679
	{ 0xEFFFE, 0xEFFFF },
680
	{ 0xFFFFE, 0xFFFFF },
681
	{ 0x10FFFE, 0x10FFFF },
682
	/* C.5 Surrogate codes */
683
	{ 0xD800, 0xDFFF },
684
	/* C.6 Inappropriate for plain text */
685
	{ 0xFFF9, 0xFFF9 },
686
	{ 0xFFFA, 0xFFFA },
687
	{ 0xFFFB, 0xFFFB },
688
	{ 0xFFFC, 0xFFFC },
689
	{ 0xFFFD, 0xFFFD },
690
	/* C.7 Inappropriate for canonical representation */
691
	{ 0x2FF0, 0x2FFB },
692
	/* C.8 Change display properties or are deprecated */
693
	{ 0x0340, 0x0340 },
694
	{ 0x0341, 0x0341 },
695
	{ 0x200E, 0x200E },
696
	{ 0x200F, 0x200F },
697
	{ 0x202A, 0x202A },
698
	{ 0x202B, 0x202B },
699
	{ 0x202C, 0x202C },
700
	{ 0x202D, 0x202D },
701
	{ 0x202E, 0x202E },
702
	{ 0x206A, 0x206A },
703
	{ 0x206B, 0x206B },
704
	{ 0x206C, 0x206C },
705
	{ 0x206D, 0x206D },
706
	{ 0x206E, 0x206E },
707
	{ 0x206F, 0x206F },
708
	/* C.9 Tagging characters */
709
	{ 0xE0001, 0xE0001 },
710
	{ 0xE0020, 0xE007F },
711
};
712
713
static int
714
encode_utf8(u_int32_t c, char *s, size_t slen)
715
{
716
	DBG(("encode CP 0x%x", c));
717
	if (c < 0x80) {
718
		if (slen >= 1) {
719
			s[0] = (char)c;
720
		}
721
		DBG(("CP 0x%x => %02x", c, (u_char)s[0]));
722
		return 1;
723
	} else if (c < 0x800) {
724
		if (slen >= 2) {
725
			s[0] = (char)(0xc0 | (c >> 6));
726
			s[1] = (char)(0x80 | (c & 0x3f));
727
		}
728
		DBG(("CP 0x%x => %02x %02x", c, (u_char)s[0], (u_char)s[1]));
729
		return 2;
730
	} else if (c < 0x10000) {
731
		if (slen >= 3) {
732
			s[0] = (char)(0xe0 | (c >> 12));
733
			s[1] = (char)(0x80 | ((c >> 6) & 0x3f));
734
			s[2] = (char)(0x80 | (c & 0x3f));
735
		}
736
		DBG(("CP 0x%x => %02x %02x %02x", c, s[0], s[1], s[2]));
737
		return 3;
738
	} else if (c < 0x200000) {
739
		if (slen >= 4) {
740
			s[0] = (char)(0xf0 | (c >> 18));
741
			s[1] = (char)(0x80 | ((c >> 12) & 0x3f));
742
			s[2] = (char)(0x80 | ((c >> 6) & 0x3f));
743
			s[3] = (char)(0x80 | (c & 0x3f));
744
		}
745
		DBG(("CP 0x%x => %02x %02x %02x %02x", c,
746
		    s[0], s[1], s[2], s[3]));
747
		return 4;
748
#if BANED_RFC3629
749
	} else if (c < 0x4000000) {
750
		if (slen >= 5) {
751
			s[0] = (char)(0xf8 | (c >> 24));
752
			s[1] = (char)(0x80 | ((c >> 18) & 0x3f));
753
			s[2] = (char)(0x80 | ((c >> 12) & 0x3f));
754
			s[3] = (char)(0x80 | ((c >> 6) & 0x3f));
755
			s[4] = (char)(0x80 | (c & 0x3f));
756
		}
757
		DBG(("CP 0x%x => %02x %02x %02x %02x %02x", c,
758
		    s[0], s[1], s[2], s[3], s[4], s[5]));
759
		return 5;
760
	} else if (c < 0x80000000) {
761
		if (slen >= 6) {
762
			s[0] = (char)(0xfc | (c >> 30));
763
			s[1] = (char)(0x80 | ((c >> 24) & 0x3f));
764
			s[2] = (char)(0x80 | ((c >> 18) & 0x3f));
765
			s[3] = (char)(0x80 | ((c >> 12) & 0x3f));
766
			s[4] = (char)(0x80 | ((c >> 6) & 0x3f));
767
			s[5] = (char)(0x80 | (c & 0x3f));
768
		}
769
		DBG(("CP 0x%x => %02x %02x %02x %02x %02x %02x", c,
770
		    s[0], s[1], s[2], s[3], s[4], s[5], s[6]));
771
		return 6;
772
#endif
773
	}
774
	DBG(("INTERNAL ERROR: CP 0x%x not encodeable", c));
775
	return -1;
776
}
777
778
static int
779
code_in_table(u_int32_t c, const struct u32_range *table, size_t tlen)
780
{
781
	const struct u32_range *e, *end = (void *)(tlen + (char *)table);
782
783
	for (e = table; e < end; e++) {
784
		if (c >= e->lo && c <= e->hi)
785
			return 1;
786
	}
787
	return 0;
788
}
789
790
791
int
792
ssh_utf8_stringprep(const char *in, char *_out, size_t olen)
793
{
794
	u_char *out = (u_char *)_out;
795
	int r, state = 0;
796
	size_t i, o, ilen = strlen(in);
797
	u_int32_t c, e;
798
799
	if (olen < 1)
800
		return -1;
801
802
	e = c = 0;
803
	for (i = o = 0; i < ilen; i++) {
804
		e = (u_char)in[i];
805
		DBG(("top: i=%lu c=0x%02x e=0x%02x '%c' state=%d",
806
		    (u_long)i, c, e, isprint(e) ? e : ' ', state));
807
		/* Invalid code point state */
808
		if (state == -1) {
809
			/*
810
			 * Continue eating continuation characters until
811
			 * a new start character comes along.
812
			 */
813
			if ((e & 0xc0) == 0x80)
814
				continue;
815
			state = 0;
816
		}
817
818
		/* New code point state */
819
		if (state == 0) {
820
			DBG(("new code point"));
821
			if ((e & 0x80) == 0) {
822
				/* 7 bit code point: skip to output */
823
				c = e & 0x7f;
824
				DBG(("7 bit CP: c=0x%02x '%c'",
825
				    c, isprint(c) ? c : '.'));
826
				goto have_code;
827
			}
828
			if ((e & 0xe0) == 0xc0) {
829
				/* 11 bit code point */
830
				state = 1;
831
				c = (e & 0x1f) << 6;
832
				DBG(("start 11 bit CP: c=0x%02x", c));
833
				if (c == 0)
834
					goto bad_encoding;
835
				continue;
836
			}
837
			if ((e & 0xf0) == 0xe0) {
838
				/* 16 bit code point */
839
				state = 2;
840
				c = (e & 0xf) << 12;
841
				DBG(("start 16 bit CP: c=0x%02x", c));
842
				if (c == 0)
843
					goto bad_encoding;
844
				continue;
845
			}
846
			if ((e & 0xf8) == 0xf0) {
847
				/* 21 bit code point */
848
				state = 3;
849
				c = (e & 0x7) << 18;
850
				DBG(("start 21 bit CP: c=0x%02x", c));
851
				if (c == 0)
852
					goto bad_encoding;
853
				continue;
854
			}
855
#if BANNED_RFC3629
856
			if ((e & 0xfc) == 0xf8) {
857
				/* 26 bit code point */
858
				state = 4;
859
				c = (e & 0x3) << 24;
860
				DBG(("start 26 bit CP: c=0x%02x", c));
861
				if (c == 0)
862
					goto bad_encoding;
863
				continue;
864
			}
865
			if ((e & 0xfe) == 0xfc) {
866
				/* 31 bit code point */
867
				state = 5;
868
				c = (e & 0x1) << 30;
869
				DBG(("start 31 bit CP: c=0x%02x", c));
870
				if (c == 0)
871
					goto bad_encoding;
872
				continue;
873
			}
874
#endif
875
 bad_encoding:
876
			DBG(("bad encoding"));
877
			c = 0;
878
			state = -1;
879
			continue;
880
		}
881
882
		/* In multibyte code point state */
883
		if (state < 1 || state > 5) {
884
			DBG(("INTERNAL ERROR: invalid state %d", state));
885
			return -1;
886
		}
887
		DBG(("CP continuation; before c=0x%02x state=%d", c, state));
888
		state--;
889
		c |= (e & 0x3f) << (state * 6);	
890
		DBG(("CP continuation; after c=0x%02x state=%d", c, state));
891
		if (state > 0)
892
			continue;
893
894
 have_code:
895
		DBG(("CP done; final c=0x%02x", c));
896
		/* Character finished. prepare, encode and output */
897
		if (c == 0)
898
			break; /* shouldn't happen */
899
900
		/* RFC3629 bans codepoints > U+10FFFF */
901
		if (c > 0x10FFFF) {
902
			DBG(("CP 0x%x > 0x10FFFF", c));
903
			goto bad_encoding;
904
		}
905
		/* Mapping */
906
		if (code_in_table(c, map_to_nothing, sizeof(map_to_nothing))) {
907
			DBG(("CP 0x%x mapped to nothing", c));
908
			continue;
909
		}
910
		/* Prohibitied output */
911
		if (code_in_table(c, prohibited, sizeof(prohibited)) &&
912
		    !code_in_table(c, whitelist, sizeof(whitelist))) {
913
			DBG(("CP 0x%x is in prohibited list", c));
914
			return -1;
915
		}
916
		/* Map unassigned code points to U+FFFD */
917
		if (code_in_table(c, unassigned, sizeof(unassigned))) {
918
			DBG(("CP 0x%x is unassigned", c));
919
			c = 0xFFFD;
920
		}
921
922
		/* Encode the character */
923
		r = encode_utf8(c, out + o, olen - o - 1);
924
		DBG(("CP 0x%x encode returned %d o=%lu of %lu",
925
		    c, r, (u_long)o, (u_long)olen));
926
		if (r < 0)
927
			return -1;
928
		o += r;
929
	}
930
	out[o] = '\0';
931
	return 0;
932
}
933
934
#if 0
935
#include <err.h>
936
937
int
938
main(int argc, char **argv)
939
{
940
	char in[8192], out[8192];
941
	
942
	while (fgets(in, sizeof(in), stdin) != NULL) {
943
		if (ssh_utf8_stringprep(in, out, sizeof(out)) != 0)
944
			errx(1, "bad input");
945
		fputs(out, stdout);
946
	}
947
	return 0;
948
}
949
#endif
(-)lib/Makefile (-1 / +1 lines)
Lines 13-19 SRCS= authfd.c authfile.c bufaux.c bufec Link Here
13
	ssh-dss.c ssh-rsa.c ssh-ecdsa.c dh.c kexdh.c kexgex.c kexecdh.c \
13
	ssh-dss.c ssh-rsa.c ssh-ecdsa.c dh.c kexdh.c kexgex.c kexecdh.c \
14
	kexdhc.c kexgexc.c kexecdhc.c msg.c progressmeter.c dns.c \
14
	kexdhc.c kexgexc.c kexecdhc.c msg.c progressmeter.c dns.c \
15
	monitor_fdpass.c umac.c addrmatch.c schnorr.c jpake.c ssh-pkcs11.c \
15
	monitor_fdpass.c umac.c addrmatch.c schnorr.c jpake.c ssh-pkcs11.c \
16
	krl.c
16
	krl.c stringprep.c
17
17
18
SRCS+=	umac128.c
18
SRCS+=	umac128.c
19
CLEANFILES+=   umac128.c
19
CLEANFILES+=   umac128.c

Return to bug 2058