coding.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. /* coding.c
  2. *
  3. * Copyright (C) 2006-2023 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. */
  21. #ifdef HAVE_CONFIG_H
  22. #include <config.h>
  23. #endif
  24. #include <wolfssl/wolfcrypt/settings.h>
  25. #ifndef NO_CODING
  26. #include <wolfssl/wolfcrypt/coding.h>
  27. #include <wolfssl/wolfcrypt/error-crypt.h>
  28. #include <wolfssl/wolfcrypt/logging.h>
  29. #ifndef NO_ASN
  30. #include <wolfssl/wolfcrypt/asn.h> /* For PEM_LINE_SZ */
  31. #endif
  32. #ifdef NO_INLINE
  33. #include <wolfssl/wolfcrypt/misc.h>
  34. #else
  35. #define WOLFSSL_MISC_INCLUDED
  36. #include <wolfcrypt/src/misc.c>
  37. #endif
  38. enum {
  39. BAD = 0xFF, /* invalid encoding */
  40. PAD = '=',
  41. BASE64_MIN = 0x2B,
  42. BASE16_MIN = 0x30
  43. };
  44. #ifndef BASE64_LINE_SZ
  45. #ifdef NO_ASN
  46. #define BASE64_LINE_SZ 64
  47. #else
  48. #define BASE64_LINE_SZ PEM_LINE_SZ
  49. #endif
  50. #endif
  51. #ifdef WOLFSSL_BASE64_DECODE
  52. #ifdef BASE64_NO_TABLE
  53. static WC_INLINE byte Base64_Char2Val(byte c)
  54. {
  55. word16 v = 0x0000;
  56. v |= 0xff3E & ctMask16Eq(c, 0x2b);
  57. v |= 0xff3F & ctMask16Eq(c, 0x2f);
  58. v |= (c + 0xff04) & ctMask16GTE(c, 0x30) & ctMask16LTE(c, 0x39);
  59. v |= (0xff00 + c - 0x41) & ctMask16GTE(c, 0x41) & ctMask16LTE(c, 0x5a);
  60. v |= (0xff00 + c - 0x47) & ctMask16GTE(c, 0x61) & ctMask16LTE(c, 0x7a);
  61. v |= ~(v >> 8);
  62. return (byte)v;
  63. }
  64. #else
  65. static
  66. ALIGN64 const byte base64Decode[] = { /* + starts at 0x2B */
  67. /* 0x28: + , - . / */ 62, BAD, BAD, BAD, 63,
  68. /* 0x30: 0 1 2 3 4 5 6 7 */ 52, 53, 54, 55, 56, 57, 58, 59,
  69. /* 0x38: 8 9 : ; < = > ? */ 60, 61, BAD, BAD, BAD, BAD, BAD, BAD,
  70. /* 0x40: @ A B C D E F G */ BAD, 0, 1, 2, 3, 4, 5, 6,
  71. /* 0x48: H I J K L M N O */ 7, 8, 9, 10, 11, 12, 13, 14,
  72. /* 0x50: P Q R S T U V W */ 15, 16, 17, 18, 19, 20, 21, 22,
  73. /* 0x58: X Y Z [ \ ] ^ _ */ 23, 24, 25, BAD, BAD, BAD, BAD, BAD,
  74. /* 0x60: ` a b c d e f g */ BAD, 26, 27, 28, 29, 30, 31, 32,
  75. /* 0x68: h i j k l m n o */ 33, 34, 35, 36, 37, 38, 39, 40,
  76. /* 0x70: p q r s t u v w */ 41, 42, 43, 44, 45, 46, 47, 48,
  77. /* 0x78: x y z */ 49, 50, 51
  78. };
  79. #define BASE64DECODE_SZ (byte)(sizeof(base64Decode))
  80. static WC_INLINE byte Base64_Char2Val(byte c)
  81. {
  82. #ifndef WC_NO_CACHE_RESISTANT
  83. /* 80 characters in table.
  84. * 64 bytes in a cache line - first line has 64, second has 16
  85. */
  86. byte v;
  87. byte mask;
  88. c -= BASE64_MIN;
  89. mask = (byte)((((byte)(0x3f - c)) >> 7) - 1);
  90. /* Load a value from the first cache line and use when mask set. */
  91. v = (byte)(base64Decode[ c & 0x3f ] & mask);
  92. /* Load a value from the second cache line and use when mask not set. */
  93. v |= (byte)(base64Decode[(c & 0x0f) | 0x40] & (~mask));
  94. return v;
  95. #else
  96. return base64Decode[c - BASE64_MIN];
  97. #endif
  98. }
  99. #endif
  100. int Base64_SkipNewline(const byte* in, word32 *inLen,
  101. word32 *outJ)
  102. {
  103. word32 len = *inLen;
  104. word32 j = *outJ;
  105. byte curChar;
  106. if (len == 0) {
  107. return BUFFER_E;
  108. }
  109. curChar = in[j];
  110. while (len > 1 && curChar == ' ') {
  111. /* skip whitespace in the middle or end of line */
  112. curChar = in[++j];
  113. len--;
  114. }
  115. if (len && (curChar == '\r' || curChar == '\n')) {
  116. j++;
  117. len--;
  118. if (curChar == '\r') {
  119. if (len) {
  120. curChar = in[j++];
  121. len--;
  122. }
  123. }
  124. if (curChar != '\n') {
  125. WOLFSSL_MSG("Bad end of line in Base64 Decode");
  126. return ASN_INPUT_E;
  127. }
  128. if (len) {
  129. curChar = in[j];
  130. }
  131. }
  132. while (len && curChar == ' ') {
  133. if (--len > 0) {
  134. curChar = in[++j];
  135. }
  136. }
  137. if (!len) {
  138. return BUFFER_E;
  139. }
  140. *inLen = len;
  141. *outJ = j;
  142. return 0;
  143. }
  144. int Base64_Decode(const byte* in, word32 inLen, byte* out, word32* outLen)
  145. {
  146. word32 i = 0;
  147. word32 j = 0;
  148. word32 plainSz = inLen - ((inLen + (BASE64_LINE_SZ - 1)) / BASE64_LINE_SZ );
  149. int ret;
  150. #ifndef BASE64_NO_TABLE
  151. const byte maxIdx = BASE64DECODE_SZ + BASE64_MIN - 1;
  152. #endif
  153. plainSz = (plainSz * 3 + 3) / 4;
  154. if (plainSz > *outLen) return BAD_FUNC_ARG;
  155. while (inLen > 3) {
  156. int pad3 = 0;
  157. int pad4 = 0;
  158. byte b1, b2, b3;
  159. byte e1, e2, e3, e4;
  160. if ((ret = Base64_SkipNewline(in, &inLen, &j)) != 0) {
  161. if (ret == BUFFER_E) {
  162. /* Running out of buffer here is not an error */
  163. break;
  164. }
  165. return ret;
  166. }
  167. e1 = in[j++];
  168. if (e1 == '\0') {
  169. break;
  170. }
  171. inLen--;
  172. if ((ret = Base64_SkipNewline(in, &inLen, &j)) != 0) {
  173. return ret;
  174. }
  175. e2 = in[j++];
  176. inLen--;
  177. if ((ret = Base64_SkipNewline(in, &inLen, &j)) != 0) {
  178. return ret;
  179. }
  180. e3 = in[j++];
  181. inLen--;
  182. if ((ret = Base64_SkipNewline(in, &inLen, &j)) != 0) {
  183. return ret;
  184. }
  185. e4 = in[j++];
  186. inLen--;
  187. if (e3 == PAD)
  188. pad3 = 1;
  189. if (e4 == PAD)
  190. pad4 = 1;
  191. if (pad3 && !pad4)
  192. return ASN_INPUT_E;
  193. #ifndef BASE64_NO_TABLE
  194. if (e1 < BASE64_MIN || e2 < BASE64_MIN || e3 < BASE64_MIN ||
  195. e4 < BASE64_MIN) {
  196. WOLFSSL_MSG("Bad Base64 Decode data, too small");
  197. return ASN_INPUT_E;
  198. }
  199. if (e1 > maxIdx || e2 > maxIdx || e3 > maxIdx || e4 > maxIdx) {
  200. WOLFSSL_MSG("Bad Base64 Decode data, too big");
  201. return ASN_INPUT_E;
  202. }
  203. #endif
  204. if (i + 1 + !pad3 + !pad4 > *outLen) {
  205. WOLFSSL_MSG("Bad Base64 Decode out buffer, too small");
  206. return BAD_FUNC_ARG;
  207. }
  208. e1 = Base64_Char2Val(e1);
  209. e2 = Base64_Char2Val(e2);
  210. e3 = (byte)((e3 == PAD) ? 0 : Base64_Char2Val(e3));
  211. e4 = (byte)((e4 == PAD) ? 0 : Base64_Char2Val(e4));
  212. if (e1 == BAD || e2 == BAD || e3 == BAD || e4 == BAD) {
  213. WOLFSSL_MSG("Bad Base64 Decode bad character");
  214. return ASN_INPUT_E;
  215. }
  216. b1 = (byte)((e1 << 2) | (e2 >> 4));
  217. b2 = (byte)(((e2 & 0xF) << 4) | (e3 >> 2));
  218. b3 = (byte)(((e3 & 0x3) << 6) | e4);
  219. out[i++] = b1;
  220. if (!pad3)
  221. out[i++] = b2;
  222. if (!pad4)
  223. out[i++] = b3;
  224. else
  225. break;
  226. }
  227. /* If the output buffer has a room for an extra byte, add a null terminator */
  228. if (out && *outLen > i)
  229. out[i]= '\0';
  230. *outLen = i;
  231. return 0;
  232. }
  233. #endif /* WOLFSSL_BASE64_DECODE */
  234. #if defined(WOLFSSL_BASE64_ENCODE)
  235. static
  236. const byte base64Encode[] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
  237. 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
  238. 'U', 'V', 'W', 'X', 'Y', 'Z',
  239. 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
  240. 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
  241. 'u', 'v', 'w', 'x', 'y', 'z',
  242. '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  243. '+', '/'
  244. };
  245. /* make sure *i (idx) won't exceed max, store and possibly escape to out,
  246. * raw means use e w/o decode, 0 on success */
  247. static int CEscape(int escaped, byte e, byte* out, word32* i, word32 maxSz,
  248. int raw, int getSzOnly)
  249. {
  250. int doEscape = 0;
  251. word32 needed = 1;
  252. word32 idx = *i;
  253. byte basic;
  254. byte plus = 0;
  255. byte equals = 0;
  256. byte newline = 0;
  257. if (raw)
  258. basic = e;
  259. else
  260. basic = base64Encode[e];
  261. /* check whether to escape. Only escape for EncodeEsc */
  262. if (escaped == WC_ESC_NL_ENC) {
  263. switch ((char)basic) {
  264. case '+' :
  265. plus = 1;
  266. doEscape = 1;
  267. needed += 2;
  268. break;
  269. case '=' :
  270. equals = 1;
  271. doEscape = 1;
  272. needed += 2;
  273. break;
  274. case '\n' :
  275. newline = 1;
  276. doEscape = 1;
  277. needed += 2;
  278. break;
  279. default:
  280. /* do nothing */
  281. break;
  282. }
  283. }
  284. /* check size */
  285. if ( (idx+needed) > maxSz && !getSzOnly) {
  286. WOLFSSL_MSG("Escape buffer max too small");
  287. return BUFFER_E;
  288. }
  289. /* store it */
  290. if (doEscape == 0) {
  291. if(getSzOnly)
  292. idx++;
  293. else
  294. out[idx++] = basic;
  295. }
  296. else {
  297. if(getSzOnly)
  298. idx+=3;
  299. else {
  300. out[idx++] = '%'; /* start escape */
  301. if (plus) {
  302. out[idx++] = '2';
  303. out[idx++] = 'B';
  304. }
  305. else if (equals) {
  306. out[idx++] = '3';
  307. out[idx++] = 'D';
  308. }
  309. else if (newline) {
  310. out[idx++] = '0';
  311. out[idx++] = 'A';
  312. }
  313. }
  314. }
  315. *i = idx;
  316. return 0;
  317. }
  318. /* internal worker, handles both escaped and normal line endings.
  319. If out buffer is NULL, will return sz needed in outLen */
  320. static int DoBase64_Encode(const byte* in, word32 inLen, byte* out,
  321. word32* outLen, int escaped)
  322. {
  323. int ret = 0;
  324. word32 i = 0,
  325. j = 0,
  326. n = 0; /* new line counter */
  327. int getSzOnly = (out == NULL);
  328. word32 outSz = (inLen + 3 - 1) / 3 * 4;
  329. word32 addSz = (outSz + BASE64_LINE_SZ - 1) / BASE64_LINE_SZ; /* new lines */
  330. if (escaped == WC_ESC_NL_ENC)
  331. addSz *= 3; /* instead of just \n, we're doing %0A triplet */
  332. else if (escaped == WC_NO_NL_ENC)
  333. addSz = 0; /* encode without \n */
  334. outSz += addSz;
  335. /* if escaped we can't predetermine size for one pass encoding, but
  336. * make sure we have enough if no escapes are in input
  337. * Also need to ensure outLen valid before dereference */
  338. if (!outLen || (outSz > *outLen && !getSzOnly)) return BAD_FUNC_ARG;
  339. while (inLen > 2) {
  340. byte b1 = in[j++];
  341. byte b2 = in[j++];
  342. byte b3 = in[j++];
  343. /* encoded idx */
  344. byte e1 = b1 >> 2;
  345. byte e2 = (byte)(((b1 & 0x3) << 4) | (b2 >> 4));
  346. byte e3 = (byte)(((b2 & 0xF) << 2) | (b3 >> 6));
  347. byte e4 = b3 & 0x3F;
  348. /* store */
  349. ret = CEscape(escaped, e1, out, &i, *outLen, 0, getSzOnly);
  350. if (ret != 0) break;
  351. ret = CEscape(escaped, e2, out, &i, *outLen, 0, getSzOnly);
  352. if (ret != 0) break;
  353. ret = CEscape(escaped, e3, out, &i, *outLen, 0, getSzOnly);
  354. if (ret != 0) break;
  355. ret = CEscape(escaped, e4, out, &i, *outLen, 0, getSzOnly);
  356. if (ret != 0) break;
  357. inLen -= 3;
  358. /* Insert newline after BASE64_LINE_SZ, unless no \n requested */
  359. if (escaped != WC_NO_NL_ENC && (++n % (BASE64_LINE_SZ/4)) == 0 && inLen) {
  360. ret = CEscape(escaped, '\n', out, &i, *outLen, 1, getSzOnly);
  361. if (ret != 0) break;
  362. }
  363. }
  364. /* last integral */
  365. if (inLen && ret == 0) {
  366. int twoBytes = (inLen == 2);
  367. byte b1 = in[j++];
  368. byte b2 = (twoBytes) ? in[j++] : 0;
  369. byte e1 = b1 >> 2;
  370. byte e2 = (byte)(((b1 & 0x3) << 4) | (b2 >> 4));
  371. byte e3 = (byte)((b2 & 0xF) << 2);
  372. ret = CEscape(escaped, e1, out, &i, *outLen, 0, getSzOnly);
  373. if (ret == 0)
  374. ret = CEscape(escaped, e2, out, &i, *outLen, 0, getSzOnly);
  375. if (ret == 0) {
  376. /* third */
  377. if (twoBytes)
  378. ret = CEscape(escaped, e3, out, &i, *outLen, 0, getSzOnly);
  379. else
  380. ret = CEscape(escaped, '=', out, &i, *outLen, 1, getSzOnly);
  381. }
  382. /* fourth always pad */
  383. if (ret == 0)
  384. ret = CEscape(escaped, '=', out, &i, *outLen, 1, getSzOnly);
  385. }
  386. if (ret == 0 && escaped != WC_NO_NL_ENC)
  387. ret = CEscape(escaped, '\n', out, &i, *outLen, 1, getSzOnly);
  388. if (i != outSz && escaped != 1 && ret == 0)
  389. return ASN_INPUT_E;
  390. /* If the output buffer has a room for an extra byte, add a null terminator */
  391. if (out && *outLen > i)
  392. out[i]= '\0';
  393. *outLen = i;
  394. if (ret == 0)
  395. return getSzOnly ? LENGTH_ONLY_E : 0;
  396. return ret;
  397. }
  398. /* Base64 Encode, PEM style, with \n line endings */
  399. int Base64_Encode(const byte* in, word32 inLen, byte* out, word32* outLen)
  400. {
  401. return DoBase64_Encode(in, inLen, out, outLen, WC_STD_ENC);
  402. }
  403. /* Base64 Encode, with %0A escaped line endings instead of \n */
  404. int Base64_EncodeEsc(const byte* in, word32 inLen, byte* out, word32* outLen)
  405. {
  406. return DoBase64_Encode(in, inLen, out, outLen, WC_ESC_NL_ENC);
  407. }
  408. int Base64_Encode_NoNl(const byte* in, word32 inLen, byte* out, word32* outLen)
  409. {
  410. return DoBase64_Encode(in, inLen, out, outLen, WC_NO_NL_ENC);
  411. }
  412. #endif /* WOLFSSL_BASE64_ENCODE */
  413. #ifdef WOLFSSL_BASE16
  414. static
  415. const byte hexDecode[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
  416. BAD, BAD, BAD, BAD, BAD, BAD, BAD,
  417. 10, 11, 12, 13, 14, 15, /* upper case A-F */
  418. BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD,
  419. BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD,
  420. BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD,
  421. BAD, BAD, /* G - ` */
  422. 10, 11, 12, 13, 14, 15 /* lower case a-f */
  423. }; /* A starts at 0x41 not 0x3A */
  424. int Base16_Decode(const byte* in, word32 inLen, byte* out, word32* outLen)
  425. {
  426. word32 inIdx = 0;
  427. word32 outIdx = 0;
  428. if (in == NULL || out == NULL || outLen == NULL)
  429. return BAD_FUNC_ARG;
  430. if (inLen == 1 && *outLen && in) {
  431. byte b = in[inIdx++] - BASE16_MIN; /* 0 starts at 0x30 */
  432. /* sanity check */
  433. if (b >= sizeof(hexDecode)/sizeof(hexDecode[0]))
  434. return ASN_INPUT_E;
  435. b = hexDecode[b];
  436. if (b == BAD)
  437. return ASN_INPUT_E;
  438. out[outIdx++] = b;
  439. *outLen = outIdx;
  440. return 0;
  441. }
  442. if (inLen % 2)
  443. return BAD_FUNC_ARG;
  444. if (*outLen < (inLen / 2))
  445. return BAD_FUNC_ARG;
  446. while (inLen) {
  447. byte b = in[inIdx++] - BASE16_MIN; /* 0 starts at 0x30 */
  448. byte b2 = in[inIdx++] - BASE16_MIN;
  449. /* sanity checks */
  450. if (b >= sizeof(hexDecode)/sizeof(hexDecode[0]))
  451. return ASN_INPUT_E;
  452. if (b2 >= sizeof(hexDecode)/sizeof(hexDecode[0]))
  453. return ASN_INPUT_E;
  454. b = hexDecode[b];
  455. b2 = hexDecode[b2];
  456. if (b == BAD || b2 == BAD)
  457. return ASN_INPUT_E;
  458. out[outIdx++] = (byte)((b << 4) | b2);
  459. inLen -= 2;
  460. }
  461. *outLen = outIdx;
  462. return 0;
  463. }
  464. int Base16_Encode(const byte* in, word32 inLen, byte* out, word32* outLen)
  465. {
  466. word32 outIdx = 0;
  467. word32 i;
  468. if (in == NULL || out == NULL || outLen == NULL)
  469. return BAD_FUNC_ARG;
  470. if (*outLen < (2 * inLen))
  471. return BAD_FUNC_ARG;
  472. for (i = 0; i < inLen; i++) {
  473. byte hb = in[i] >> 4;
  474. byte lb = in[i] & 0x0f;
  475. /* ASCII value */
  476. hb += '0';
  477. if (hb > '9')
  478. hb += 7;
  479. /* ASCII value */
  480. lb += '0';
  481. if (lb>'9')
  482. lb += 7;
  483. out[outIdx++] = hb;
  484. out[outIdx++] = lb;
  485. }
  486. /* If the output buffer has a room for an extra byte, add a null terminator */
  487. if (*outLen > outIdx)
  488. out[outIdx++]= '\0';
  489. *outLen = outIdx;
  490. return 0;
  491. }
  492. #endif /* WOLFSSL_BASE16 */
  493. #endif /* !NO_CODING */