Line | Count | Source |
1 | | /*************************************************************************** |
2 | | * _ _ ____ _ |
3 | | * Project ___| | | | _ \| | |
4 | | * / __| | | | |_) | | |
5 | | * | (__| |_| | _ <| |___ |
6 | | * \___|\___/|_| \_\_____| |
7 | | * |
8 | | * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al. |
9 | | * |
10 | | * This software is licensed as described in the file COPYING, which |
11 | | * you should have received as part of this distribution. The terms |
12 | | * are also available at https://curl.se/docs/copyright.html. |
13 | | * |
14 | | * You may opt to use, copy, modify, merge, publish, distribute and/or sell |
15 | | * copies of the Software, and permit persons to whom the Software is |
16 | | * furnished to do so, under the terms of the COPYING file. |
17 | | * |
18 | | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
19 | | * KIND, either express or implied. |
20 | | * |
21 | | * SPDX-License-Identifier: curl |
22 | | * |
23 | | ***************************************************************************/ |
24 | | |
25 | | /* |
26 | | * IDN conversions |
27 | | */ |
28 | | |
29 | | #include "curl_setup.h" |
30 | | #include "urldata.h" |
31 | | #include "idn.h" |
32 | | #include "sendf.h" |
33 | | #include "curlx/multibyte.h" |
34 | | #include "curlx/warnless.h" |
35 | | |
36 | | #ifdef USE_LIBIDN2 |
37 | | #include <idn2.h> |
38 | | |
39 | | #if defined(_WIN32) && defined(UNICODE) |
40 | | #define IDN2_LOOKUP(name, host, flags) \ |
41 | | idn2_lookup_u8((const uint8_t *)name, (uint8_t **)host, flags) |
42 | | #else |
43 | | #define IDN2_LOOKUP(name, host, flags) \ |
44 | 0 | idn2_lookup_ul((const char *)name, (char **)host, flags) |
45 | | #endif |
46 | | #endif /* USE_LIBIDN2 */ |
47 | | |
48 | | /* The last 2 #include files should be in this order */ |
49 | | #include "curl_memory.h" |
50 | | #include "memdebug.h" |
51 | | |
52 | | /* for macOS and iOS targets */ |
53 | | #ifdef USE_APPLE_IDN |
54 | | #include <unicode/uidna.h> |
55 | | #include <iconv.h> |
56 | | #include <langinfo.h> |
57 | | |
58 | | #define MAX_HOST_LENGTH 512 |
59 | | |
60 | | static CURLcode iconv_to_utf8(const char *in, size_t inlen, |
61 | | char **out, size_t *outlen) |
62 | | { |
63 | | iconv_t cd = iconv_open("UTF-8", nl_langinfo(CODESET)); |
64 | | if(cd != (iconv_t)-1) { |
65 | | size_t iconv_outlen = *outlen; |
66 | | char *iconv_in = (char *)CURL_UNCONST(in); |
67 | | size_t iconv_inlen = inlen; |
68 | | size_t iconv_result = iconv(cd, &iconv_in, &iconv_inlen, |
69 | | out, &iconv_outlen); |
70 | | *outlen -= iconv_outlen; |
71 | | iconv_close(cd); |
72 | | if(iconv_result == (size_t)-1) { |
73 | | /* !checksrc! disable ERRNOVAR 1 */ |
74 | | if(errno == ENOMEM) |
75 | | return CURLE_OUT_OF_MEMORY; |
76 | | else |
77 | | return CURLE_URL_MALFORMAT; |
78 | | } |
79 | | |
80 | | return CURLE_OK; |
81 | | } |
82 | | else { |
83 | | /* !checksrc! disable ERRNOVAR 1 */ |
84 | | if(errno == ENOMEM) |
85 | | return CURLE_OUT_OF_MEMORY; |
86 | | else |
87 | | return CURLE_FAILED_INIT; |
88 | | } |
89 | | } |
90 | | |
91 | | static CURLcode mac_idn_to_ascii(const char *in, char **out) |
92 | | { |
93 | | size_t inlen = strlen(in); |
94 | | if(inlen < MAX_HOST_LENGTH) { |
95 | | char iconv_buffer[MAX_HOST_LENGTH] = {0}; |
96 | | char *iconv_outptr = iconv_buffer; |
97 | | size_t iconv_outlen = sizeof(iconv_buffer); |
98 | | CURLcode iconv_result = iconv_to_utf8(in, inlen, |
99 | | &iconv_outptr, &iconv_outlen); |
100 | | if(!iconv_result) { |
101 | | UErrorCode err = U_ZERO_ERROR; |
102 | | UIDNA* idna = uidna_openUTS46( |
103 | | UIDNA_CHECK_BIDI|UIDNA_NONTRANSITIONAL_TO_ASCII, &err); |
104 | | if(!U_FAILURE(err)) { |
105 | | UIDNAInfo info = UIDNA_INFO_INITIALIZER; |
106 | | char buffer[MAX_HOST_LENGTH] = {0}; |
107 | | (void)uidna_nameToASCII_UTF8(idna, iconv_buffer, (int)iconv_outlen, |
108 | | buffer, sizeof(buffer) - 1, &info, &err); |
109 | | uidna_close(idna); |
110 | | if(!U_FAILURE(err) && !info.errors) { |
111 | | *out = strdup(buffer); |
112 | | if(*out) |
113 | | return CURLE_OK; |
114 | | else |
115 | | return CURLE_OUT_OF_MEMORY; |
116 | | } |
117 | | } |
118 | | } |
119 | | else |
120 | | return iconv_result; |
121 | | } |
122 | | return CURLE_URL_MALFORMAT; |
123 | | } |
124 | | |
125 | | static CURLcode mac_ascii_to_idn(const char *in, char **out) |
126 | | { |
127 | | size_t inlen = strlen(in); |
128 | | if(inlen < MAX_HOST_LENGTH) { |
129 | | UErrorCode err = U_ZERO_ERROR; |
130 | | UIDNA* idna = uidna_openUTS46( |
131 | | UIDNA_CHECK_BIDI|UIDNA_NONTRANSITIONAL_TO_UNICODE, &err); |
132 | | if(!U_FAILURE(err)) { |
133 | | UIDNAInfo info = UIDNA_INFO_INITIALIZER; |
134 | | char buffer[MAX_HOST_LENGTH] = {0}; |
135 | | (void)uidna_nameToUnicodeUTF8(idna, in, -1, buffer, |
136 | | sizeof(buffer) - 1, &info, &err); |
137 | | uidna_close(idna); |
138 | | if(!U_FAILURE(err)) { |
139 | | *out = strdup(buffer); |
140 | | if(*out) |
141 | | return CURLE_OK; |
142 | | else |
143 | | return CURLE_OUT_OF_MEMORY; |
144 | | } |
145 | | } |
146 | | } |
147 | | return CURLE_URL_MALFORMAT; |
148 | | } |
149 | | #endif |
150 | | |
151 | | #ifdef USE_WIN32_IDN |
152 | | /* using Windows kernel32 and normaliz libraries. */ |
153 | | |
154 | | #if (!defined(_WIN32_WINNT) || _WIN32_WINNT < _WIN32_WINNT_VISTA) && \ |
155 | | (!defined(WINVER) || WINVER < 0x600) |
156 | | WINBASEAPI int WINAPI IdnToAscii(DWORD dwFlags, |
157 | | const WCHAR *lpUnicodeCharStr, |
158 | | int cchUnicodeChar, |
159 | | WCHAR *lpASCIICharStr, |
160 | | int cchASCIIChar); |
161 | | WINBASEAPI int WINAPI IdnToUnicode(DWORD dwFlags, |
162 | | const WCHAR *lpASCIICharStr, |
163 | | int cchASCIIChar, |
164 | | WCHAR *lpUnicodeCharStr, |
165 | | int cchUnicodeChar); |
166 | | #endif |
167 | | |
168 | | #define IDN_MAX_LENGTH 255 |
169 | | |
170 | | static CURLcode win32_idn_to_ascii(const char *in, char **out) |
171 | | { |
172 | | wchar_t *in_w = curlx_convert_UTF8_to_wchar(in); |
173 | | *out = NULL; |
174 | | if(in_w) { |
175 | | wchar_t punycode[IDN_MAX_LENGTH]; |
176 | | int chars = IdnToAscii(0, in_w, (int)(wcslen(in_w) + 1), punycode, |
177 | | IDN_MAX_LENGTH); |
178 | | curlx_unicodefree(in_w); |
179 | | if(chars) { |
180 | | char *mstr = curlx_convert_wchar_to_UTF8(punycode); |
181 | | if(mstr) { |
182 | | *out = strdup(mstr); |
183 | | curlx_unicodefree(mstr); |
184 | | if(!*out) |
185 | | return CURLE_OUT_OF_MEMORY; |
186 | | } |
187 | | else |
188 | | return CURLE_OUT_OF_MEMORY; |
189 | | } |
190 | | else |
191 | | return CURLE_URL_MALFORMAT; |
192 | | } |
193 | | else |
194 | | return CURLE_URL_MALFORMAT; |
195 | | |
196 | | return CURLE_OK; |
197 | | } |
198 | | |
199 | | static CURLcode win32_ascii_to_idn(const char *in, char **output) |
200 | | { |
201 | | char *out = NULL; |
202 | | |
203 | | wchar_t *in_w = curlx_convert_UTF8_to_wchar(in); |
204 | | if(in_w) { |
205 | | WCHAR idn[IDN_MAX_LENGTH]; /* stores a UTF-16 string */ |
206 | | int chars = IdnToUnicode(0, in_w, (int)(wcslen(in_w) + 1), idn, |
207 | | IDN_MAX_LENGTH); |
208 | | if(chars) { |
209 | | /* 'chars' is "the number of characters retrieved" */ |
210 | | char *mstr = curlx_convert_wchar_to_UTF8(idn); |
211 | | if(mstr) { |
212 | | out = strdup(mstr); |
213 | | curlx_unicodefree(mstr); |
214 | | if(!out) |
215 | | return CURLE_OUT_OF_MEMORY; |
216 | | } |
217 | | } |
218 | | else |
219 | | return CURLE_URL_MALFORMAT; |
220 | | } |
221 | | else |
222 | | return CURLE_URL_MALFORMAT; |
223 | | *output = out; |
224 | | return CURLE_OK; |
225 | | } |
226 | | |
227 | | #endif /* USE_WIN32_IDN */ |
228 | | |
229 | | /* |
230 | | * Helpers for IDNA conversions. |
231 | | */ |
232 | | bool Curl_is_ASCII_name(const char *hostname) |
233 | 0 | { |
234 | | /* get an UNSIGNED local version of the pointer */ |
235 | 0 | const unsigned char *ch = (const unsigned char *)hostname; |
236 | |
|
237 | 0 | if(!hostname) /* bad input, consider it ASCII! */ |
238 | 0 | return TRUE; |
239 | | |
240 | 0 | while(*ch) { |
241 | 0 | if(*ch++ & 0x80) |
242 | 0 | return FALSE; |
243 | 0 | } |
244 | 0 | return TRUE; |
245 | 0 | } |
246 | | |
247 | | #ifdef USE_IDN |
248 | | /* |
249 | | * Curl_idn_decode() returns an allocated IDN decoded string if it was |
250 | | * possible. NULL on error. |
251 | | * |
252 | | * CURLE_URL_MALFORMAT - the hostname could not be converted |
253 | | * CURLE_OUT_OF_MEMORY - memory problem |
254 | | * |
255 | | */ |
256 | | static CURLcode idn_decode(const char *input, char **output) |
257 | 0 | { |
258 | 0 | char *decoded = NULL; |
259 | 0 | CURLcode result = CURLE_OK; |
260 | 0 | #ifdef USE_LIBIDN2 |
261 | 0 | if(idn2_check_version(IDN2_VERSION)) { |
262 | 0 | int flags = IDN2_NFC_INPUT |
263 | 0 | #if IDN2_VERSION_NUMBER >= 0x00140000 |
264 | | /* IDN2_NFC_INPUT: Normalize input string using normalization form C. |
265 | | IDN2_NONTRANSITIONAL: Perform Unicode TR46 non-transitional |
266 | | processing. */ |
267 | 0 | | IDN2_NONTRANSITIONAL |
268 | 0 | #endif |
269 | 0 | ; |
270 | 0 | int rc = IDN2_LOOKUP(input, &decoded, flags); |
271 | 0 | if(rc != IDN2_OK) |
272 | | /* fallback to TR46 Transitional mode for better IDNA2003 |
273 | | compatibility */ |
274 | 0 | rc = IDN2_LOOKUP(input, &decoded, IDN2_TRANSITIONAL); |
275 | 0 | if(rc != IDN2_OK) |
276 | 0 | result = CURLE_URL_MALFORMAT; |
277 | 0 | } |
278 | 0 | else |
279 | | /* a too old libidn2 version */ |
280 | 0 | result = CURLE_NOT_BUILT_IN; |
281 | | #elif defined(USE_WIN32_IDN) |
282 | | result = win32_idn_to_ascii(input, &decoded); |
283 | | #elif defined(USE_APPLE_IDN) |
284 | | result = mac_idn_to_ascii(input, &decoded); |
285 | | #endif |
286 | 0 | if(!result) |
287 | 0 | *output = decoded; |
288 | 0 | return result; |
289 | 0 | } |
290 | | |
291 | | static CURLcode idn_encode(const char *puny, char **output) |
292 | 0 | { |
293 | 0 | char *enc = NULL; |
294 | 0 | #ifdef USE_LIBIDN2 |
295 | 0 | int rc = idn2_to_unicode_8z8z(puny, &enc, 0); |
296 | 0 | if(rc != IDNA_SUCCESS) |
297 | 0 | return rc == IDNA_MALLOC_ERROR ? CURLE_OUT_OF_MEMORY : CURLE_URL_MALFORMAT; |
298 | | #elif defined(USE_WIN32_IDN) |
299 | | CURLcode result = win32_ascii_to_idn(puny, &enc); |
300 | | if(result) |
301 | | return result; |
302 | | #elif defined(USE_APPLE_IDN) |
303 | | CURLcode result = mac_ascii_to_idn(puny, &enc); |
304 | | if(result) |
305 | | return result; |
306 | | #endif |
307 | 0 | *output = enc; |
308 | 0 | return CURLE_OK; |
309 | 0 | } |
310 | | |
311 | | CURLcode Curl_idn_decode(const char *input, char **output) |
312 | 0 | { |
313 | 0 | char *d = NULL; |
314 | 0 | CURLcode result = idn_decode(input, &d); |
315 | 0 | #ifdef USE_LIBIDN2 |
316 | 0 | if(!result) { |
317 | 0 | char *c = strdup(d); |
318 | 0 | idn2_free(d); |
319 | 0 | if(c) |
320 | 0 | d = c; |
321 | 0 | else |
322 | 0 | result = CURLE_OUT_OF_MEMORY; |
323 | 0 | } |
324 | 0 | #endif |
325 | 0 | if(!result) { |
326 | 0 | if(!d[0]) { /* ended up zero length, not acceptable */ |
327 | 0 | result = CURLE_URL_MALFORMAT; |
328 | 0 | free(d); |
329 | 0 | } |
330 | 0 | else |
331 | 0 | *output = d; |
332 | 0 | } |
333 | 0 | return result; |
334 | 0 | } |
335 | | |
336 | | CURLcode Curl_idn_encode(const char *puny, char **output) |
337 | 0 | { |
338 | 0 | char *d = NULL; |
339 | 0 | CURLcode result = idn_encode(puny, &d); |
340 | 0 | #ifdef USE_LIBIDN2 |
341 | 0 | if(!result) { |
342 | 0 | char *c = strdup(d); |
343 | 0 | idn2_free(d); |
344 | 0 | if(c) |
345 | 0 | d = c; |
346 | 0 | else |
347 | 0 | result = CURLE_OUT_OF_MEMORY; |
348 | 0 | } |
349 | 0 | #endif |
350 | 0 | if(!result) |
351 | 0 | *output = d; |
352 | 0 | return result; |
353 | 0 | } |
354 | | |
355 | | /* |
356 | | * Frees data allocated by idnconvert_hostname() |
357 | | */ |
358 | | void Curl_free_idnconverted_hostname(struct hostname *host) |
359 | 0 | { |
360 | 0 | Curl_safefree(host->encalloc); |
361 | 0 | } |
362 | | |
363 | | #endif /* USE_IDN */ |
364 | | |
365 | | /* |
366 | | * Perform any necessary IDN conversion of hostname |
367 | | */ |
368 | | CURLcode Curl_idnconvert_hostname(struct hostname *host) |
369 | 0 | { |
370 | | /* set the name we use to display the hostname */ |
371 | 0 | host->dispname = host->name; |
372 | |
|
373 | 0 | #ifdef USE_IDN |
374 | | /* Check name for non-ASCII and convert hostname if we can */ |
375 | 0 | if(!Curl_is_ASCII_name(host->name)) { |
376 | 0 | char *decoded; |
377 | 0 | CURLcode result = Curl_idn_decode(host->name, &decoded); |
378 | 0 | if(result) |
379 | 0 | return result; |
380 | | /* successful */ |
381 | 0 | host->name = host->encalloc = decoded; |
382 | 0 | } |
383 | 0 | #endif |
384 | 0 | return CURLE_OK; |
385 | 0 | } |