1 /* Prosody IM
2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
4 -- Copyright (C) 1994-2015 Lua.org, PUC-Rio.
5 --
6 -- This project is MIT/X11 licensed. Please see the
7 -- COPYING file in the source package for more information.
8 --
9 */
10 
11 /*
12 * encodings.c
13 * Lua library for base64, stringprep and idna encodings
14 */
15 
16 /* Newer MSVC compilers deprecate strcpy as unsafe, but we use it in a safe way */
17 #define _CRT_SECURE_NO_DEPRECATE
18 
19 #include <string.h>
20 #include <stdlib.h>
21 #include "lua.h"
22 #include "lauxlib.h"
23 
24 #if (LUA_VERSION_NUM == 501)
25 #define luaL_setfuncs(L, R, N) luaL_register(L, NULL, R)
26 #endif
27 
28 /***************** BASE64 *****************/
29 
30 static const char code[] =
31     "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
32 
base64_encode(luaL_Buffer * b,unsigned int c1,unsigned int c2,unsigned int c3,int n)33 static void base64_encode(luaL_Buffer *b, unsigned int c1, unsigned int c2, unsigned int c3, int n) {
34 	unsigned long tuple = c3 + 256UL * (c2 + 256UL * c1);
35 	int i;
36 	char s[4];
37 
38 	for(i = 0; i < 4; i++) {
39 		s[3 - i] = code[tuple % 64];
40 		tuple /= 64;
41 	}
42 
43 	for(i = n + 1; i < 4; i++) {
44 		s[i] = '=';
45 	}
46 
47 	luaL_addlstring(b, s, 4);
48 }
49 
Lbase64_encode(lua_State * L)50 static int Lbase64_encode(lua_State *L) {	/** encode(s) */
51 	size_t l;
52 	const unsigned char *s = (const unsigned char *)luaL_checklstring(L, 1, &l);
53 	luaL_Buffer b;
54 	int n;
55 	luaL_buffinit(L, &b);
56 
57 	for(n = l / 3; n--; s += 3) {
58 		base64_encode(&b, s[0], s[1], s[2], 3);
59 	}
60 
61 	switch(l % 3) {
62 		case 1:
63 			base64_encode(&b, s[0], 0, 0, 1);
64 			break;
65 
66 		case 2:
67 			base64_encode(&b, s[0], s[1], 0, 2);
68 			break;
69 	}
70 
71 	luaL_pushresult(&b);
72 	return 1;
73 }
74 
base64_decode(luaL_Buffer * b,int c1,int c2,int c3,int c4,int n)75 static void base64_decode(luaL_Buffer *b, int c1, int c2, int c3, int c4, int n) {
76 	unsigned long tuple = c4 + 64L * (c3 + 64L * (c2 + 64L * c1));
77 	char s[3];
78 
79 	switch(--n) {
80 		case 3:
81 			s[2] = (char) tuple;
82 			/* Falls through. */
83 
84 		case 2:
85 			s[1] = (char)(tuple >> 8);
86 			/* Falls through. */
87 
88 		case 1:
89 			s[0] = (char)(tuple >> 16);
90 	}
91 
92 	luaL_addlstring(b, s, n);
93 }
94 
Lbase64_decode(lua_State * L)95 static int Lbase64_decode(lua_State *L) {	/** decode(s) */
96 	size_t l;
97 	const char *s = luaL_checklstring(L, 1, &l);
98 	luaL_Buffer b;
99 	int n = 0;
100 	char t[4];
101 	luaL_buffinit(L, &b);
102 
103 	for(;;) {
104 		int c = *s++;
105 
106 		switch(c) {
107 				const char *p;
108 
109 			default:
110 				p = strchr(code, c);
111 
112 				if(p == NULL) {
113 					return 0;
114 				}
115 
116 				t[n++] = (char)(p - code);
117 
118 				if(n == 4) {
119 					base64_decode(&b, t[0], t[1], t[2], t[3], 4);
120 					n = 0;
121 				}
122 
123 				break;
124 
125 			case '=':
126 
127 				switch(n) {
128 					case 1:
129 						base64_decode(&b, t[0], 0, 0, 0, 1);
130 						break;
131 
132 					case 2:
133 						base64_decode(&b, t[0], t[1], 0, 0, 2);
134 						break;
135 
136 					case 3:
137 						base64_decode(&b, t[0], t[1], t[2], 0, 3);
138 						break;
139 				}
140 
141 				n = 0;
142 				break;
143 
144 			case 0:
145 				luaL_pushresult(&b);
146 				return 1;
147 
148 			case '\n':
149 			case '\r':
150 			case '\t':
151 			case ' ':
152 			case '\f':
153 			case '\b':
154 				break;
155 		}
156 	}
157 }
158 
159 static const luaL_Reg Reg_base64[] = {
160 	{ "encode",	Lbase64_encode	},
161 	{ "decode",	Lbase64_decode	},
162 	{ NULL,		NULL	}
163 };
164 
165 /******************* UTF-8 ********************/
166 
167 /*
168  * Adapted from Lua 5.3
169  * Needed because libidn does not validate that input is valid UTF-8
170  */
171 
172 #define MAXUNICODE	0x10FFFF
173 
174 /*
175  * Decode one UTF-8 sequence, returning NULL if byte sequence is invalid.
176  */
utf8_decode(const char * o,int * val)177 static const char *utf8_decode(const char *o, int *val) {
178 	static const unsigned int limits[] = {0xFF, 0x7F, 0x7FF, 0xFFFF};
179 	const unsigned char *s = (const unsigned char *)o;
180 	unsigned int c = s[0];
181 	unsigned int res = 0;  /* final result */
182 
183 	if(c < 0x80) { /* ascii? */
184 		res = c;
185 	} else {
186 		int count = 0;  /* to count number of continuation bytes */
187 
188 		while(c & 0x40) {   /* still have continuation bytes? */
189 			int cc = s[++count];  /* read next byte */
190 
191 			if((cc & 0xC0) != 0x80) { /* not a continuation byte? */
192 				return NULL;    /* invalid byte sequence */
193 			}
194 
195 			res = (res << 6) | (cc & 0x3F);  /* add lower 6 bits from cont. byte */
196 			c <<= 1;  /* to test next bit */
197 		}
198 
199 		res |= ((c & 0x7F) << (count * 5));  /* add first byte */
200 
201 		if(count > 3 || res > MAXUNICODE || res <= limits[count] || (0xd800 <= res && res <= 0xdfff)) {
202 			return NULL;    /* invalid byte sequence */
203 		}
204 
205 		s += count;  /* skip continuation bytes read */
206 	}
207 
208 	if(val) {
209 		*val = res;
210 	}
211 
212 	return (const char *)s + 1;  /* +1 to include first byte */
213 }
214 
215 /*
216  * Check that a string is valid UTF-8
217  * Returns NULL if not
218  */
check_utf8(lua_State * L,int idx,size_t * l)219 const char *check_utf8(lua_State *L, int idx, size_t *l) {
220 	size_t pos, len;
221 	const char *s = luaL_checklstring(L, idx, &len);
222 	pos = 0;
223 
224 	while(pos <= len) {
225 		const char *s1 = utf8_decode(s + pos, NULL);
226 
227 		if(s1 == NULL) {   /* conversion error? */
228 			return NULL;
229 		}
230 
231 		pos = s1 - s;
232 	}
233 
234 	if(l != NULL) {
235 		*l = len;
236 	}
237 
238 	return s;
239 }
240 
Lutf8_valid(lua_State * L)241 static int Lutf8_valid(lua_State *L) {
242 	lua_pushboolean(L, check_utf8(L, 1, NULL) != NULL);
243 	return 1;
244 }
245 
Lutf8_length(lua_State * L)246 static int Lutf8_length(lua_State *L) {
247 	size_t len;
248 
249 	if(!check_utf8(L, 1, &len)) {
250 		lua_pushnil(L);
251 		lua_pushliteral(L, "invalid utf8");
252 		return 2;
253 	}
254 
255 	lua_pushinteger(L, len);
256 	return 1;
257 }
258 
259 static const luaL_Reg Reg_utf8[] = {
260 	{ "valid",	Lutf8_valid	},
261 	{ "length",	Lutf8_length	},
262 	{ NULL,		NULL	}
263 };
264 
265 /***************** STRINGPREP *****************/
266 #ifdef USE_STRINGPREP_ICU
267 
268 #include <unicode/usprep.h>
269 #include <unicode/ustring.h>
270 #include <unicode/utrace.h>
271 
icu_stringprep_prep(lua_State * L,const UStringPrepProfile * profile)272 static int icu_stringprep_prep(lua_State *L, const UStringPrepProfile *profile) {
273 	size_t input_len;
274 	int32_t unprepped_len, prepped_len, output_len;
275 	const char *input;
276 	char output[1024];
277 
278 	UChar unprepped[1024]; /* Temporary unicode buffer (1024 characters) */
279 	UChar prepped[1024];
280 
281 	UErrorCode err = U_ZERO_ERROR;
282 
283 	if(!lua_isstring(L, 1)) {
284 		lua_pushnil(L);
285 		return 1;
286 	}
287 
288 	input = lua_tolstring(L, 1, &input_len);
289 
290 	if(input_len >= 1024) {
291 		lua_pushnil(L);
292 		return 1;
293 	}
294 
295 	u_strFromUTF8(unprepped, 1024, &unprepped_len, input, input_len, &err);
296 
297 	if(U_FAILURE(err)) {
298 		lua_pushnil(L);
299 		return 1;
300 	}
301 
302 	prepped_len = usprep_prepare(profile, unprepped, unprepped_len, prepped, 1024, USPREP_ALLOW_UNASSIGNED, NULL, &err);
303 
304 	if(U_FAILURE(err)) {
305 		lua_pushnil(L);
306 		return 1;
307 	} else {
308 		u_strToUTF8(output, 1024, &output_len, prepped, prepped_len, &err);
309 
310 		if(U_SUCCESS(err) && output_len < 1024) {
311 			lua_pushlstring(L, output, output_len);
312 		} else {
313 			lua_pushnil(L);
314 		}
315 
316 		return 1;
317 	}
318 }
319 
320 UStringPrepProfile *icu_nameprep;
321 UStringPrepProfile *icu_nodeprep;
322 UStringPrepProfile *icu_resourceprep;
323 UStringPrepProfile *icu_saslprep;
324 
325 /* initialize global ICU stringprep profiles */
init_icu()326 void init_icu() {
327 	UErrorCode err = U_ZERO_ERROR;
328 	utrace_setLevel(UTRACE_VERBOSE);
329 	icu_nameprep = usprep_openByType(USPREP_RFC3491_NAMEPREP, &err);
330 	icu_nodeprep = usprep_openByType(USPREP_RFC3920_NODEPREP, &err);
331 	icu_resourceprep = usprep_openByType(USPREP_RFC3920_RESOURCEPREP, &err);
332 	icu_saslprep = usprep_openByType(USPREP_RFC4013_SASLPREP, &err);
333 
334 	if(U_FAILURE(err)) {
335 		fprintf(stderr, "[c] util.encodings: error: %s\n", u_errorName((UErrorCode)err));
336 	}
337 }
338 
339 #define MAKE_PREP_FUNC(myFunc, prep) \
340 static int myFunc(lua_State *L) { return icu_stringprep_prep(L, prep); }
341 
342 MAKE_PREP_FUNC(Lstringprep_nameprep, icu_nameprep)		/** stringprep.nameprep(s) */
343 MAKE_PREP_FUNC(Lstringprep_nodeprep, icu_nodeprep)		/** stringprep.nodeprep(s) */
344 MAKE_PREP_FUNC(Lstringprep_resourceprep, icu_resourceprep)		/** stringprep.resourceprep(s) */
345 MAKE_PREP_FUNC(Lstringprep_saslprep, icu_saslprep)		/** stringprep.saslprep(s) */
346 
347 static const luaL_Reg Reg_stringprep[] = {
348 	{ "nameprep",	Lstringprep_nameprep	},
349 	{ "nodeprep",	Lstringprep_nodeprep	},
350 	{ "resourceprep",	Lstringprep_resourceprep	},
351 	{ "saslprep",	Lstringprep_saslprep	},
352 	{ NULL,		NULL	}
353 };
354 #else /* USE_STRINGPREP_ICU */
355 
356 /****************** libidn ********************/
357 
358 #include <stringprep.h>
359 
stringprep_prep(lua_State * L,const Stringprep_profile * profile)360 static int stringprep_prep(lua_State *L, const Stringprep_profile *profile) {
361 	size_t len;
362 	const char *s;
363 	char string[1024];
364 	int ret;
365 
366 	if(!lua_isstring(L, 1)) {
367 		lua_pushnil(L);
368 		return 1;
369 	}
370 
371 	s = check_utf8(L, 1, &len);
372 
373 	if(s == NULL || len >= 1024 || len != strlen(s)) {
374 		lua_pushnil(L);
375 		return 1; /* TODO return error message */
376 	}
377 
378 	strcpy(string, s);
379 	ret = stringprep(string, 1024, (Stringprep_profile_flags)0, profile);
380 
381 	if(ret == STRINGPREP_OK) {
382 		lua_pushstring(L, string);
383 		return 1;
384 	} else {
385 		lua_pushnil(L);
386 		return 1; /* TODO return error message */
387 	}
388 }
389 
390 #define MAKE_PREP_FUNC(myFunc, prep) \
391 static int myFunc(lua_State *L) { return stringprep_prep(L, prep); }
392 
393 MAKE_PREP_FUNC(Lstringprep_nameprep, stringprep_nameprep)		/** stringprep.nameprep(s) */
394 MAKE_PREP_FUNC(Lstringprep_nodeprep, stringprep_xmpp_nodeprep)		/** stringprep.nodeprep(s) */
395 MAKE_PREP_FUNC(Lstringprep_resourceprep, stringprep_xmpp_resourceprep)		/** stringprep.resourceprep(s) */
396 MAKE_PREP_FUNC(Lstringprep_saslprep, stringprep_saslprep)		/** stringprep.saslprep(s) */
397 
398 static const luaL_Reg Reg_stringprep[] = {
399 	{ "nameprep",	Lstringprep_nameprep	},
400 	{ "nodeprep",	Lstringprep_nodeprep	},
401 	{ "resourceprep",	Lstringprep_resourceprep	},
402 	{ "saslprep",	Lstringprep_saslprep	},
403 	{ NULL,		NULL	}
404 };
405 #endif
406 
407 /***************** IDNA *****************/
408 #ifdef USE_STRINGPREP_ICU
409 #include <unicode/ustdio.h>
410 #include <unicode/uidna.h>
411 /* IDNA2003 or IDNA2008 ? ? ? */
Lidna_to_ascii(lua_State * L)412 static int Lidna_to_ascii(lua_State *L) {	/** idna.to_ascii(s) */
413 	size_t len;
414 	int32_t ulen, dest_len, output_len;
415 	const char *s = luaL_checklstring(L, 1, &len);
416 	UChar ustr[1024];
417 	UErrorCode err = U_ZERO_ERROR;
418 	UChar dest[1024];
419 	char output[1024];
420 
421 	u_strFromUTF8(ustr, 1024, &ulen, s, len, &err);
422 
423 	if(U_FAILURE(err)) {
424 		lua_pushnil(L);
425 		return 1;
426 	}
427 
428 	dest_len = uidna_IDNToASCII(ustr, ulen, dest, 1024, UIDNA_USE_STD3_RULES, NULL, &err);
429 
430 	if(U_FAILURE(err)) {
431 		lua_pushnil(L);
432 		return 1;
433 	} else {
434 		u_strToUTF8(output, 1024, &output_len, dest, dest_len, &err);
435 
436 		if(U_SUCCESS(err) && output_len < 1024) {
437 			lua_pushlstring(L, output, output_len);
438 		} else {
439 			lua_pushnil(L);
440 		}
441 
442 		return 1;
443 	}
444 }
445 
Lidna_to_unicode(lua_State * L)446 static int Lidna_to_unicode(lua_State *L) {	/** idna.to_unicode(s) */
447 	size_t len;
448 	int32_t ulen, dest_len, output_len;
449 	const char *s = luaL_checklstring(L, 1, &len);
450 	UChar ustr[1024];
451 	UErrorCode err = U_ZERO_ERROR;
452 	UChar dest[1024];
453 	char output[1024];
454 
455 	u_strFromUTF8(ustr, 1024, &ulen, s, len, &err);
456 
457 	if(U_FAILURE(err)) {
458 		lua_pushnil(L);
459 		return 1;
460 	}
461 
462 	dest_len = uidna_IDNToUnicode(ustr, ulen, dest, 1024, UIDNA_USE_STD3_RULES, NULL, &err);
463 
464 	if(U_FAILURE(err)) {
465 		lua_pushnil(L);
466 		return 1;
467 	} else {
468 		u_strToUTF8(output, 1024, &output_len, dest, dest_len, &err);
469 
470 		if(U_SUCCESS(err) && output_len < 1024) {
471 			lua_pushlstring(L, output, output_len);
472 		} else {
473 			lua_pushnil(L);
474 		}
475 
476 		return 1;
477 	}
478 }
479 
480 #else /* USE_STRINGPREP_ICU */
481 /****************** libidn ********************/
482 
483 #include <idna.h>
484 #include <idn-free.h>
485 
Lidna_to_ascii(lua_State * L)486 static int Lidna_to_ascii(lua_State *L) {	/** idna.to_ascii(s) */
487 	size_t len;
488 	const char *s = check_utf8(L, 1, &len);
489 	char *output = NULL;
490 	int ret;
491 
492 	if(s == NULL || len != strlen(s)) {
493 		lua_pushnil(L);
494 		return 1; /* TODO return error message */
495 	}
496 
497 	ret = idna_to_ascii_8z(s, &output, IDNA_USE_STD3_ASCII_RULES);
498 
499 	if(ret == IDNA_SUCCESS) {
500 		lua_pushstring(L, output);
501 		idn_free(output);
502 		return 1;
503 	} else {
504 		lua_pushnil(L);
505 		idn_free(output);
506 		return 1; /* TODO return error message */
507 	}
508 }
509 
Lidna_to_unicode(lua_State * L)510 static int Lidna_to_unicode(lua_State *L) {	/** idna.to_unicode(s) */
511 	size_t len;
512 	const char *s = luaL_checklstring(L, 1, &len);
513 	char *output = NULL;
514 	int ret = idna_to_unicode_8z8z(s, &output, 0);
515 
516 	if(ret == IDNA_SUCCESS) {
517 		lua_pushstring(L, output);
518 		idn_free(output);
519 		return 1;
520 	} else {
521 		lua_pushnil(L);
522 		idn_free(output);
523 		return 1; /* TODO return error message */
524 	}
525 }
526 #endif
527 
528 static const luaL_Reg Reg_idna[] = {
529 	{ "to_ascii",	Lidna_to_ascii	},
530 	{ "to_unicode",	Lidna_to_unicode	},
531 	{ NULL,		NULL	}
532 };
533 
534 /***************** end *****************/
535 
luaopen_util_encodings(lua_State * L)536 LUALIB_API int luaopen_util_encodings(lua_State *L) {
537 #if (LUA_VERSION_NUM > 501)
538 	luaL_checkversion(L);
539 #endif
540 #ifdef USE_STRINGPREP_ICU
541 	init_icu();
542 #endif
543 	lua_newtable(L);
544 
545 	lua_newtable(L);
546 	luaL_setfuncs(L, Reg_base64, 0);
547 	lua_setfield(L, -2, "base64");
548 
549 	lua_newtable(L);
550 	luaL_setfuncs(L, Reg_stringprep, 0);
551 	lua_setfield(L, -2, "stringprep");
552 
553 	lua_newtable(L);
554 	luaL_setfuncs(L, Reg_idna, 0);
555 	lua_setfield(L, -2, "idna");
556 
557 	lua_newtable(L);
558 	luaL_setfuncs(L, Reg_utf8, 0);
559 	lua_setfield(L, -2, "utf8");
560 
561 	lua_pushliteral(L, "-3.14");
562 	lua_setfield(L, -2, "version");
563 	return 1;
564 }
565