textbuf.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. /*
  2. * OpenConnect (SSL + DTLS) VPN client
  3. *
  4. * Copyright © 2008-2016 Intel Corporation.
  5. * Copyright © 2016-2021 David Woodhouse.
  6. *
  7. * Author: David Woodhouse <dwmw2@infradead.org>
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public License
  11. * version 2.1, as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. */
  18. #include <config.h>
  19. #include "openconnect-internal.h"
  20. #include <unistd.h>
  21. #include <string.h>
  22. #include <ctype.h>
  23. #include <errno.h>
  24. #include <stdlib.h>
  25. #include <stdio.h>
  26. #include <stddef.h>
  27. #include <limits.h>
  28. #include <stdarg.h>
  29. #define BUF_CHUNK_SIZE 4096
  30. #define OC_BUF_MAX ((unsigned)(16*1024*1024))
  31. struct oc_text_buf *buf_alloc(void)
  32. {
  33. return calloc(1, sizeof(struct oc_text_buf));
  34. }
  35. int buf_error(struct oc_text_buf *buf)
  36. {
  37. return buf ? buf->error : -ENOMEM;
  38. }
  39. void buf_truncate(struct oc_text_buf *buf)
  40. {
  41. if (!buf)
  42. return;
  43. if (buf->data)
  44. memset(buf->data, 0, buf->pos);
  45. buf->pos = 0;
  46. }
  47. int buf_free(struct oc_text_buf *buf)
  48. {
  49. int error = buf_error(buf);
  50. if (buf) {
  51. buf_truncate(buf);
  52. if (buf->data)
  53. free(buf->data);
  54. free(buf);
  55. }
  56. return error;
  57. }
  58. int buf_ensure_space(struct oc_text_buf *buf, int len)
  59. {
  60. unsigned int new_buf_len;
  61. if (!buf)
  62. return -ENOMEM;
  63. new_buf_len = (buf->pos + len + BUF_CHUNK_SIZE - 1) & ~(BUF_CHUNK_SIZE - 1);
  64. if (new_buf_len <= buf->buf_len)
  65. return 0;
  66. if (new_buf_len > OC_BUF_MAX) {
  67. buf->error = -E2BIG;
  68. return buf->error;
  69. } else {
  70. realloc_inplace(buf->data, new_buf_len);
  71. if (!buf->data)
  72. buf->error = -ENOMEM;
  73. else
  74. buf->buf_len = new_buf_len;
  75. }
  76. return buf->error;
  77. }
  78. void buf_append_bytes(struct oc_text_buf *buf, const void *bytes, int len)
  79. {
  80. if (!buf || buf->error)
  81. return;
  82. if (buf_ensure_space(buf, len + 1))
  83. return;
  84. memcpy(buf->data + buf->pos, bytes, len);
  85. buf->pos += len;
  86. buf->data[buf->pos] = 0;
  87. }
  88. void __attribute__ ((format (printf, 2, 3)))
  89. buf_append(struct oc_text_buf *buf, const char *fmt, ...)
  90. {
  91. va_list ap;
  92. if (!buf || buf->error)
  93. return;
  94. if (buf_ensure_space(buf, 1))
  95. return;
  96. while (1) {
  97. int max_len = buf->buf_len - buf->pos, ret;
  98. va_start(ap, fmt);
  99. ret = vsnprintf(buf->data + buf->pos, max_len, fmt, ap);
  100. va_end(ap);
  101. if (ret < 0) {
  102. buf->error = -EIO;
  103. break;
  104. } else if (ret < max_len) {
  105. buf->pos += ret;
  106. break;
  107. } else if (buf_ensure_space(buf, ret))
  108. break;
  109. }
  110. }
  111. void buf_append_urlencoded(struct oc_text_buf *buf, const char *str)
  112. {
  113. while (str && *str) {
  114. unsigned char c = *str;
  115. if (c < 0x80 && (isalnum((int)(c)) || c=='-' || c=='_' || c=='.' || c=='~'))
  116. buf_append_bytes(buf, str, 1);
  117. else
  118. buf_append(buf, "%%%02x", c);
  119. str++;
  120. }
  121. }
  122. void buf_append_xmlescaped(struct oc_text_buf *buf, const char *str)
  123. {
  124. while (str && *str) {
  125. unsigned char c = *str;
  126. if (c=='<' || c=='>' || c=='&' || c=='"' || c=='\'')
  127. buf_append(buf, "&#x%02x;", c);
  128. else
  129. buf_append_bytes(buf, str, 1);
  130. str++;
  131. }
  132. }
  133. void buf_append_be16(struct oc_text_buf *buf, uint16_t val)
  134. {
  135. unsigned char b[2];
  136. store_be16(b, val);
  137. buf_append_bytes(buf, b, 2);
  138. }
  139. void buf_append_be32(struct oc_text_buf *buf, uint32_t val)
  140. {
  141. unsigned char b[4];
  142. store_be32(b, val);
  143. buf_append_bytes(buf, b, 4);
  144. }
  145. void buf_append_le16(struct oc_text_buf *buf, uint16_t val)
  146. {
  147. unsigned char b[2];
  148. store_le16(b, val);
  149. buf_append_bytes(buf, b, 2);
  150. }
  151. void buf_append_hex(struct oc_text_buf *buf, const void *str, unsigned len)
  152. {
  153. const unsigned char *data = str;
  154. unsigned i;
  155. for (i = 0; i < len; i++)
  156. buf_append(buf, "%02x", (unsigned)data[i]);
  157. }
  158. void buf_append_from_utf16le(struct oc_text_buf *buf, const void *_utf16)
  159. {
  160. const unsigned char *utf16 = _utf16;
  161. unsigned char utf8[4];
  162. int c;
  163. if (!utf16)
  164. return;
  165. while (utf16[0] || utf16[1]) {
  166. if ((utf16[1] & 0xfc) == 0xd8 && (utf16[3] & 0xfc) == 0xdc) {
  167. c = ((load_le16(utf16) & 0x3ff) << 10)|
  168. (load_le16(utf16 + 2) & 0x3ff);
  169. c += 0x10000;
  170. utf16 += 4;
  171. } else {
  172. c = load_le16(utf16);
  173. utf16 += 2;
  174. }
  175. if (c < 0x80) {
  176. utf8[0] = c;
  177. buf_append_bytes(buf, utf8, 1);
  178. } else if (c < 0x800) {
  179. utf8[0] = 0xc0 | (c >> 6);
  180. utf8[1] = 0x80 | (c & 0x3f);
  181. buf_append_bytes(buf, utf8, 2);
  182. } else if (c < 0x10000) {
  183. utf8[0] = 0xe0 | (c >> 12);
  184. utf8[1] = 0x80 | ((c >> 6) & 0x3f);
  185. utf8[2] = 0x80 | (c & 0x3f);
  186. buf_append_bytes(buf, utf8, 3);
  187. } else {
  188. utf8[0] = 0xf0 | (c >> 18);
  189. utf8[1] = 0x80 | ((c >> 12) & 0x3f);
  190. utf8[2] = 0x80 | ((c >> 6) & 0x3f);
  191. utf8[3] = 0x80 | (c & 0x3f);
  192. buf_append_bytes(buf, utf8, 4);
  193. }
  194. }
  195. utf8[0] = 0;
  196. buf_append_bytes(buf, utf8, 1);
  197. }
  198. int get_utf8char(const char **p)
  199. {
  200. const char *utf8 = *p;
  201. unsigned char c;
  202. int utfchar, nr_extra, min;
  203. c = *(utf8++);
  204. if (c < 128) {
  205. utfchar = c;
  206. nr_extra = 0;
  207. min = 0;
  208. } else if ((c & 0xe0) == 0xc0) {
  209. utfchar = c & 0x1f;
  210. nr_extra = 1;
  211. min = 0x80;
  212. } else if ((c & 0xf0) == 0xe0) {
  213. utfchar = c & 0x0f;
  214. nr_extra = 2;
  215. min = 0x800;
  216. } else if ((c & 0xf8) == 0xf0) {
  217. utfchar = c & 0x07;
  218. nr_extra = 3;
  219. min = 0x10000;
  220. } else {
  221. return -EILSEQ;
  222. }
  223. while (nr_extra--) {
  224. c = *(utf8++);
  225. if ((c & 0xc0) != 0x80)
  226. return -EILSEQ;
  227. utfchar <<= 6;
  228. utfchar |= (c & 0x3f);
  229. }
  230. if (utfchar > 0x10ffff || utfchar < min)
  231. return -EILSEQ;
  232. *p = utf8;
  233. return utfchar;
  234. }
  235. int buf_append_utf16le(struct oc_text_buf *buf, const char *utf8)
  236. {
  237. int utfchar, len = 0;
  238. if (!utf8)
  239. return 0;
  240. /* Ick. Now I'm implementing my own UTF8 handling too. Perhaps it's
  241. time to bite the bullet and start requiring something like glib? */
  242. while (*utf8) {
  243. utfchar = get_utf8char(&utf8);
  244. if (utfchar < 0) {
  245. if (buf)
  246. buf->error = utfchar;
  247. return utfchar;
  248. }
  249. if (!buf)
  250. continue;
  251. if (utfchar >= 0x10000) {
  252. utfchar -= 0x10000;
  253. if (buf_ensure_space(buf, 4))
  254. return buf_error(buf);
  255. store_le16(buf->data + buf->pos, (utfchar >> 10) | 0xd800);
  256. store_le16(buf->data + buf->pos + 2, (utfchar & 0x3ff) | 0xdc00);
  257. buf->pos += 4;
  258. len += 4;
  259. } else {
  260. if (buf_ensure_space(buf, 2))
  261. return buf_error(buf);
  262. store_le16(buf->data + buf->pos, utfchar);
  263. buf->pos += 2;
  264. len += 2;
  265. }
  266. }
  267. /* We were only being used for validation */
  268. if (!buf)
  269. return 0;
  270. /* Ensure UTF16 is NUL-terminated */
  271. if (buf_ensure_space(buf, 2))
  272. return buf_error(buf);
  273. buf->data[buf->pos] = buf->data[buf->pos + 1] = 0;
  274. return len;
  275. }
  276. /* Ick. Yet another wheel to reinvent. But although we could pull it
  277. in from OpenSSL, we can't from GnuTLS */
  278. static inline int b64_char(char c)
  279. {
  280. if (c >= 'A' && c <= 'Z')
  281. return c - 'A';
  282. if (c >= 'a' && c <= 'z')
  283. return c - 'a' + 26;
  284. if (c >= '0' && c <= '9')
  285. return c - '0' + 52;
  286. if (c == '+')
  287. return 62;
  288. if (c == '/')
  289. return 63;
  290. return -1;
  291. }
  292. void *openconnect_base64_decode(int *ret_len, const char *in)
  293. {
  294. unsigned char *buf;
  295. int b[4];
  296. int len = strlen(in);
  297. if (len & 3) {
  298. *ret_len = -EINVAL;
  299. return NULL;
  300. }
  301. len = (len * 3) / 4;
  302. buf = malloc(len);
  303. if (!buf) {
  304. *ret_len = -ENOMEM;
  305. return NULL;
  306. }
  307. len = 0;
  308. while (*in) {
  309. if (!in[1] || !in[2] || !in[3])
  310. goto err;
  311. b[0] = b64_char(in[0]);
  312. b[1] = b64_char(in[1]);
  313. if (b[0] < 0 || b[1] < 0)
  314. goto err;
  315. buf[len++] = (b[0] << 2) | (b[1] >> 4);
  316. if (in[2] == '=') {
  317. if (in[3] != '=' || in[4] != 0)
  318. goto err;
  319. break;
  320. }
  321. b[2] = b64_char(in[2]);
  322. if (b[2] < 0)
  323. goto err;
  324. buf[len++] = (b[1] << 4) | (b[2] >> 2);
  325. if (in[3] == '=') {
  326. if (in[4] != 0)
  327. goto err;
  328. break;
  329. }
  330. b[3] = b64_char(in[3]);
  331. if (b[3] < 0)
  332. goto err;
  333. buf[len++] = (b[2] << 6) | b[3];
  334. in += 4;
  335. }
  336. *ret_len = len;
  337. return buf;
  338. err:
  339. free(buf);
  340. *ret_len = -EINVAL;
  341. return NULL;
  342. }
  343. static const char b64_table[] = {
  344. 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
  345. 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
  346. 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
  347. 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'
  348. };
  349. void buf_append_base64(struct oc_text_buf *buf, const void *bytes, int len,
  350. int line_len)
  351. {
  352. const unsigned char *in = bytes;
  353. int hibits;
  354. if (!buf || buf->error)
  355. return;
  356. if (len < 0 || line_len < 0 || (line_len & 3)) {
  357. buf->error = -EINVAL;
  358. return;
  359. }
  360. unsigned int needed = ((len + 2u) / 3) * 4;
  361. /* Line endings, but not for the last line if it reaches line_len */
  362. if (line_len && needed)
  363. needed += (needed - 1) / line_len;
  364. needed++; /* Allow for the trailing NUL */
  365. if (needed >= (unsigned)(OC_BUF_MAX - buf->pos)) {
  366. buf->error = -E2BIG;
  367. return;
  368. }
  369. if (buf_ensure_space(buf, needed))
  370. return;
  371. #ifdef BUFTEST
  372. int orig_len = len, orig_pos = buf->pos;
  373. #endif
  374. int ll = 0;
  375. while (len > 0) {
  376. if (line_len) {
  377. if (ll >= line_len) {
  378. ll = 0;
  379. buf->data[buf->pos++] = '\n';
  380. }
  381. ll += 4;
  382. }
  383. buf->data[buf->pos++] = b64_table[in[0] >> 2];
  384. hibits = (in[0] << 4) & 0x30;
  385. if (len == 1) {
  386. buf->data[buf->pos++] = b64_table[hibits];
  387. buf->data[buf->pos++] = '=';
  388. buf->data[buf->pos++] = '=';
  389. break;
  390. }
  391. buf->data[buf->pos++] = b64_table[hibits | (in[1] >> 4)];
  392. hibits = (in[1] << 2) & 0x3c;
  393. if (len == 2) {
  394. buf->data[buf->pos++] = b64_table[hibits];
  395. buf->data[buf->pos++] = '=';
  396. break;
  397. }
  398. buf->data[buf->pos++] = b64_table[hibits | (in[2] >> 6)];
  399. buf->data[buf->pos++] = b64_table[in[2] & 0x3f];
  400. in += 3;
  401. len -= 3;
  402. }
  403. #ifdef BUFTEST
  404. if (buf->pos != orig_pos + needed - 1) {
  405. printf("Used %d instead of calculated %d for %d bytes at line len %d\n",
  406. buf->pos - orig_pos, needed, orig_len, line_len);
  407. buf->error = -EIO;
  408. }
  409. #endif
  410. buf->data[buf->pos] = 0;
  411. }