Fix $regex<n> use-after-free. Bug 2915
[exim.git] / src / src / utf8.c
1 /*************************************************
2 *     Exim - an Internet mail transport agent    *
3 *************************************************/
4
5 /* Copyright (c) The Exim Maintainers 2022 */
6 /* Copyright (c) Jeremy Harris 2015 - 2018 */
7 /* See the file NOTICE for conditions of use and distribution. */
8
9
10 #include "exim.h"
11
12 #ifdef SUPPORT_I18N
13
14 #ifdef SUPPORT_I18N_2008
15 # include <idn2.h>
16 #else
17 # include <idna.h>
18 #endif
19
20 #include <punycode.h>
21 #include <stringprep.h>
22
23 static uschar *
24 string_localpart_alabel_to_utf8_(const uschar * alabel, uschar ** err);
25
26 /**************************************************/
27
28 BOOL
29 string_is_utf8(const uschar * s)
30 {
31 uschar c;
32 if (s) while ((c = *s++)) if (c & 0x80) return TRUE;
33 return FALSE;
34 }
35
36 static BOOL
37 string_is_alabel(const uschar * s)
38 {
39 return s[0] == 'x' && s[1] == 'n' && s[2] == '-' && s[3] == '-';
40 }
41
42 /**************************************************/
43 /* Domain conversions.
44 The *err string pointer should be null before the call
45
46 Return NULL for error, with optional errstr pointer filled in
47 */
48
49 uschar *
50 string_domain_utf8_to_alabel(const uschar * utf8, uschar ** err)
51 {
52 uschar * s1, * s;
53 int rc;
54
55 #ifdef SUPPORT_I18N_2008
56 /* Avoid lowercasing plain-ascii domains */
57 if (!string_is_utf8(utf8))
58   return string_copy(utf8);
59
60 /* Only lowercase is accepted by the library call.  A pity since we lose
61 any mixed-case annotation.  This does not really matter for a domain. */
62   {
63   uschar c;
64   for (s1 = s = US utf8; (c = *s1); s1++) if (!(c & 0x80) && isupper(c))
65     {
66     s = string_copy(utf8);
67     for (s1 = s + (s1 - utf8); (c = *s1); s1++) if (!(c & 0x80) && isupper(c))
68       *s1 = tolower(c);
69     break;
70     }
71   }
72 if ((rc = idn2_lookup_u8((const uint8_t *) s, &s1, IDN2_NFC_INPUT)) != IDN2_OK)
73   {
74   if (err) *err = US idn2_strerror(rc);
75   return NULL;
76   }
77 #else
78 s = US stringprep_utf8_nfkc_normalize(CCS utf8, -1);
79 if (  (rc = idna_to_ascii_8z(CCS s, CSS &s1, IDNA_ALLOW_UNASSIGNED))
80    != IDNA_SUCCESS)
81   {
82   free(s);
83   if (err) *err = US idna_strerror(rc);
84   return NULL;
85   }
86 free(s);
87 #endif
88 s = string_copy(s1);
89 free(s1);
90 return s;
91 }
92
93
94
95 uschar *
96 string_domain_alabel_to_utf8(const uschar * alabel, uschar ** err)
97 {
98 #ifdef SUPPORT_I18N_2008
99 const uschar * label;
100 int sep = '.';
101 gstring * g = NULL;
102
103 while (label = string_nextinlist(&alabel, &sep, NULL, 0))
104   if (  string_is_alabel(label)
105      && !(label = string_localpart_alabel_to_utf8_(label, err))
106      )
107     return NULL;
108   else
109     g = string_append_listele(g, '.', label);
110 return string_from_gstring(g);
111
112 #else
113
114 uschar * s1, * s;
115 int rc;
116
117 if (  (rc = idna_to_unicode_8z8z(CCS alabel, CSS &s1, IDNA_USE_STD3_ASCII_RULES))
118    != IDNA_SUCCESS)
119   {
120   if (err) *err = US idna_strerror(rc);
121   return NULL;
122   }
123 s = string_copy(s1);
124 free(s1);
125 return s;
126 #endif
127 }
128
129 /**************************************************/
130 /* localpart conversions */
131 /* the *err string pointer should be null before the call */
132
133
134 uschar *
135 string_localpart_utf8_to_alabel(const uschar * utf8, uschar ** err)
136 {
137 size_t ucs4_len = 0;
138 punycode_uint * p;
139 size_t p_len;
140 uschar * res;
141 int rc;
142
143 if (!string_is_utf8(utf8)) return string_copy(utf8);
144
145 p = (punycode_uint *) stringprep_utf8_to_ucs4(CCS utf8, -1, &ucs4_len);
146 if (!p || !ucs4_len)
147   {
148   if (err) *err = US"l_u2a: bad UTF-8 input";
149   return NULL;
150   }
151 p_len = ucs4_len*4;     /* this multiplier is pure guesswork */
152 res = store_get(p_len+5, utf8);
153
154 res[0] = 'x'; res[1] = 'n'; res[2] = res[3] = '-';
155
156 if ((rc = punycode_encode(ucs4_len, p, NULL, &p_len, CS res+4)) != PUNYCODE_SUCCESS)
157   {
158   DEBUG(D_expand) debug_printf("l_u2a: bad '%s'\n", punycode_strerror(rc));
159   free(p);
160   if (err) *err = US punycode_strerror(rc);
161   return NULL;
162   }
163 p_len += 4;
164 free(p);
165 res[p_len] = '\0';
166 return res;
167 }
168
169
170 static uschar *
171 string_localpart_alabel_to_utf8_(const uschar * alabel, uschar ** err)
172 {
173 size_t p_len;
174 punycode_uint * p;
175 int rc;
176 uschar * s, * res;
177
178 DEBUG(D_expand) debug_printf("l_a2u: '%s'\n", alabel);
179 alabel += 4;
180 p_len = Ustrlen(alabel);
181 p = store_get((p_len+1) * sizeof(*p), alabel);
182
183 if ((rc = punycode_decode(p_len, CCS alabel, &p_len, p, NULL)) != PUNYCODE_SUCCESS)
184   {
185   if (err) *err = US punycode_strerror(rc);
186   return NULL;
187   }
188
189 s = US stringprep_ucs4_to_utf8(p, p_len, NULL, &p_len);
190 res = string_copyn(s, p_len);
191 free(s);
192 return res;
193 }
194
195
196 uschar *
197 string_localpart_alabel_to_utf8(const uschar * alabel, uschar ** err)
198 {
199 if (string_is_alabel(alabel))
200   return string_localpart_alabel_to_utf8_(alabel, err);
201
202 if (err) *err = US"bad alabel prefix";
203 return NULL;
204 }
205
206
207 /**************************************************/
208 /* Whole address conversion.
209 The *err string pointer should be null before the call.
210
211 Return NULL on error, with (optional) errstring pointer filled in
212 */
213
214 uschar *
215 string_address_utf8_to_alabel(const uschar * utf8, uschar ** err)
216 {
217 uschar * l, * d;
218
219 if (!*utf8) return string_copy(utf8);
220
221 DEBUG(D_expand) debug_printf("addr from utf8 <%s>", utf8);
222
223 for (const uschar * s = utf8; *s; s++)
224   if (*s == '@')
225     {
226     l = string_copyn(utf8, s - utf8);
227     if (  !(l = string_localpart_utf8_to_alabel(l, err))
228        || !(d = string_domain_utf8_to_alabel(++s, err))
229        )
230       return NULL;
231     l = string_sprintf("%s@%s", l, d);
232     DEBUG(D_expand) debug_printf(" -> <%s>\n", l);
233     return l;
234     }
235
236 l =  string_localpart_utf8_to_alabel(utf8, err);
237 DEBUG(D_expand) debug_printf(" -> <%s>\n", l);
238 return l;
239 }
240
241
242
243 /*************************************************
244 *         Report the library versions.           *
245 *************************************************/
246
247 /* See a description in tls-openssl.c for an explanation of why this exists.
248
249 Arguments:   string to append to
250 Returns:     string
251 */
252
253 gstring *
254 utf8_version_report(gstring * g)
255 {
256 #ifdef SUPPORT_I18N_2008
257 g = string_fmt_append(g, "Library version: IDN2: Compile: %s\n"
258            "                       Runtime: %s\n",
259         IDN2_VERSION,
260         idn2_check_version(NULL));
261 g = string_fmt_append(g, "Library version: Stringprep: Compile: %s\n"
262            "                             Runtime: %s\n",
263         STRINGPREP_VERSION,
264         stringprep_check_version(NULL));
265 #else
266 g = string_fmt_append(g, "Library version: IDN: Compile: %s\n"
267            "                      Runtime: %s\n",
268         STRINGPREP_VERSION,
269         stringprep_check_version(NULL));
270 #endif
271 return g;
272 }
273
274 #endif  /* whole file */
275
276 /* vi: aw ai sw=2
277 */
278 /* End of utf8.c */