coding.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. /* coding.c
  2. *
  3. * Copyright (C) 2006-2015 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL. (formerly known as CyaSSL)
  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-1301, 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. enum {
  30. BAD = 0xFF, /* invalid encoding */
  31. PAD = '=',
  32. PEM_LINE_SZ = 64
  33. };
  34. static
  35. const byte base64Decode[] = { 62, BAD, BAD, BAD, 63, /* + starts at 0x2B */
  36. 52, 53, 54, 55, 56, 57, 58, 59, 60, 61,
  37. BAD, BAD, BAD, BAD, BAD, BAD, BAD,
  38. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
  39. 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
  40. 20, 21, 22, 23, 24, 25,
  41. BAD, BAD, BAD, BAD, BAD, BAD,
  42. 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
  43. 36, 37, 38, 39, 40, 41, 42, 43, 44, 45,
  44. 46, 47, 48, 49, 50, 51
  45. };
  46. int Base64_Decode(const byte* in, word32 inLen, byte* out, word32* outLen)
  47. {
  48. word32 i = 0;
  49. word32 j = 0;
  50. word32 plainSz = inLen - ((inLen + (PEM_LINE_SZ - 1)) / PEM_LINE_SZ );
  51. const byte maxIdx = (byte)sizeof(base64Decode) + 0x2B - 1;
  52. plainSz = (plainSz * 3 + 3) / 4;
  53. if (plainSz > *outLen) return BAD_FUNC_ARG;
  54. while (inLen > 3) {
  55. byte b1, b2, b3;
  56. byte e1 = in[j++];
  57. byte e2 = in[j++];
  58. byte e3 = in[j++];
  59. byte e4 = in[j++];
  60. int pad3 = 0;
  61. int pad4 = 0;
  62. if (e1 == 0) /* end file 0's */
  63. break;
  64. if (e3 == PAD)
  65. pad3 = 1;
  66. if (e4 == PAD)
  67. pad4 = 1;
  68. if (e1 < 0x2B || e2 < 0x2B || e3 < 0x2B || e4 < 0x2B) {
  69. WOLFSSL_MSG("Bad Base64 Decode data, too small");
  70. return ASN_INPUT_E;
  71. }
  72. if (e1 > maxIdx || e2 > maxIdx || e3 > maxIdx || e4 > maxIdx) {
  73. WOLFSSL_MSG("Bad Base64 Decode data, too big");
  74. return ASN_INPUT_E;
  75. }
  76. e1 = base64Decode[e1 - 0x2B];
  77. e2 = base64Decode[e2 - 0x2B];
  78. e3 = (e3 == PAD) ? 0 : base64Decode[e3 - 0x2B];
  79. e4 = (e4 == PAD) ? 0 : base64Decode[e4 - 0x2B];
  80. b1 = (byte)((e1 << 2) | (e2 >> 4));
  81. b2 = (byte)(((e2 & 0xF) << 4) | (e3 >> 2));
  82. b3 = (byte)(((e3 & 0x3) << 6) | e4);
  83. out[i++] = b1;
  84. if (!pad3)
  85. out[i++] = b2;
  86. if (!pad4)
  87. out[i++] = b3;
  88. else
  89. break;
  90. inLen -= 4;
  91. if (inLen && (in[j] == ' ' || in[j] == '\r' || in[j] == '\n')) {
  92. byte endLine = in[j++];
  93. inLen--;
  94. while (inLen && endLine == ' ') { /* allow trailing whitespace */
  95. endLine = in[j++];
  96. inLen--;
  97. }
  98. if (endLine == '\r') {
  99. if (inLen) {
  100. endLine = in[j++];
  101. inLen--;
  102. }
  103. }
  104. if (endLine != '\n') {
  105. WOLFSSL_MSG("Bad end of line in Base64 Decode");
  106. return ASN_INPUT_E;
  107. }
  108. }
  109. }
  110. *outLen = i;
  111. return 0;
  112. }
  113. #if defined(WOLFSSL_BASE64_ENCODE)
  114. static
  115. const byte base64Encode[] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
  116. 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
  117. 'U', 'V', 'W', 'X', 'Y', 'Z',
  118. 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
  119. 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
  120. 'u', 'v', 'w', 'x', 'y', 'z',
  121. '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  122. '+', '/'
  123. };
  124. /* make sure *i (idx) won't exceed max, store and possibly escape to out,
  125. * raw means use e w/o decode, 0 on success */
  126. static int CEscape(int escaped, byte e, byte* out, word32* i, word32 max,
  127. int raw, int getSzOnly)
  128. {
  129. int doEscape = 0;
  130. word32 needed = 1;
  131. word32 idx = *i;
  132. byte basic;
  133. byte plus = 0;
  134. byte equals = 0;
  135. byte newline = 0;
  136. if (raw)
  137. basic = e;
  138. else
  139. basic = base64Encode[e];
  140. /* check whether to escape. Only escape for EncodeEsc */
  141. if (escaped == WC_ESC_NL_ENC) {
  142. switch ((char)basic) {
  143. case '+' :
  144. plus = 1;
  145. doEscape = 1;
  146. needed += 2;
  147. break;
  148. case '=' :
  149. equals = 1;
  150. doEscape = 1;
  151. needed += 2;
  152. break;
  153. case '\n' :
  154. newline = 1;
  155. doEscape = 1;
  156. needed += 2;
  157. break;
  158. default:
  159. /* do nothing */
  160. break;
  161. }
  162. }
  163. /* check size */
  164. if ( (idx+needed) > max && !getSzOnly) {
  165. WOLFSSL_MSG("Escape buffer max too small");
  166. return BUFFER_E;
  167. }
  168. /* store it */
  169. if (doEscape == 0) {
  170. if(getSzOnly)
  171. idx++;
  172. else
  173. out[idx++] = basic;
  174. }
  175. else {
  176. if(getSzOnly)
  177. idx+=3;
  178. else {
  179. out[idx++] = '%'; /* start escape */
  180. if (plus) {
  181. out[idx++] = '2';
  182. out[idx++] = 'B';
  183. }
  184. else if (equals) {
  185. out[idx++] = '3';
  186. out[idx++] = 'D';
  187. }
  188. else if (newline) {
  189. out[idx++] = '0';
  190. out[idx++] = 'A';
  191. }
  192. }
  193. }
  194. *i = idx;
  195. return 0;
  196. }
  197. /* internal worker, handles both escaped and normal line endings.
  198. If out buffer is NULL, will return sz needed in outLen */
  199. static int DoBase64_Encode(const byte* in, word32 inLen, byte* out,
  200. word32* outLen, int escaped)
  201. {
  202. int ret = 0;
  203. word32 i = 0,
  204. j = 0,
  205. n = 0; /* new line counter */
  206. int getSzOnly = (out == NULL);
  207. word32 outSz = (inLen + 3 - 1) / 3 * 4;
  208. word32 addSz = (outSz + PEM_LINE_SZ - 1) / PEM_LINE_SZ; /* new lines */
  209. if (escaped == WC_ESC_NL_ENC)
  210. addSz *= 3; /* instead of just \n, we're doing %0A triplet */
  211. else if (escaped == WC_NO_NL_ENC)
  212. addSz = 0; /* encode without \n */
  213. outSz += addSz;
  214. /* if escaped we can't predetermine size for one pass encoding, but
  215. * make sure we have enough if no escapes are in input
  216. * Also need to ensure outLen valid before dereference */
  217. if (!outLen || (outSz > *outLen && !getSzOnly)) return BAD_FUNC_ARG;
  218. while (inLen > 2) {
  219. byte b1 = in[j++];
  220. byte b2 = in[j++];
  221. byte b3 = in[j++];
  222. /* encoded idx */
  223. byte e1 = b1 >> 2;
  224. byte e2 = (byte)(((b1 & 0x3) << 4) | (b2 >> 4));
  225. byte e3 = (byte)(((b2 & 0xF) << 2) | (b3 >> 6));
  226. byte e4 = b3 & 0x3F;
  227. /* store */
  228. ret = CEscape(escaped, e1, out, &i, *outLen, 0, getSzOnly);
  229. if (ret != 0) break;
  230. ret = CEscape(escaped, e2, out, &i, *outLen, 0, getSzOnly);
  231. if (ret != 0) break;
  232. ret = CEscape(escaped, e3, out, &i, *outLen, 0, getSzOnly);
  233. if (ret != 0) break;
  234. ret = CEscape(escaped, e4, out, &i, *outLen, 0, getSzOnly);
  235. if (ret != 0) break;
  236. inLen -= 3;
  237. /* Insert newline after PEM_LINE_SZ, unless no \n requested */
  238. if (escaped != WC_NO_NL_ENC && (++n % (PEM_LINE_SZ/4)) == 0 && inLen){
  239. ret = CEscape(escaped, '\n', out, &i, *outLen, 1, getSzOnly);
  240. if (ret != 0) break;
  241. }
  242. }
  243. /* last integral */
  244. if (inLen && ret == 0) {
  245. int twoBytes = (inLen == 2);
  246. byte b1 = in[j++];
  247. byte b2 = (twoBytes) ? in[j++] : 0;
  248. byte e1 = b1 >> 2;
  249. byte e2 = (byte)(((b1 & 0x3) << 4) | (b2 >> 4));
  250. byte e3 = (byte)((b2 & 0xF) << 2);
  251. ret = CEscape(escaped, e1, out, &i, *outLen, 0, getSzOnly);
  252. if (ret == 0)
  253. ret = CEscape(escaped, e2, out, &i, *outLen, 0, getSzOnly);
  254. if (ret == 0) {
  255. /* third */
  256. if (twoBytes)
  257. ret = CEscape(escaped, e3, out, &i, *outLen, 0, getSzOnly);
  258. else
  259. ret = CEscape(escaped, '=', out, &i, *outLen, 1, getSzOnly);
  260. }
  261. /* fourth always pad */
  262. if (ret == 0)
  263. ret = CEscape(escaped, '=', out, &i, *outLen, 1, getSzOnly);
  264. }
  265. if (ret == 0 && escaped != WC_NO_NL_ENC)
  266. ret = CEscape(escaped, '\n', out, &i, *outLen, 1, getSzOnly);
  267. if (i != outSz && escaped != 1 && ret == 0)
  268. return ASN_INPUT_E;
  269. *outLen = i;
  270. if(ret == 0)
  271. return getSzOnly ? LENGTH_ONLY_E : 0;
  272. return ret;
  273. }
  274. /* Base64 Encode, PEM style, with \n line endings */
  275. int Base64_Encode(const byte* in, word32 inLen, byte* out, word32* outLen)
  276. {
  277. return DoBase64_Encode(in, inLen, out, outLen, WC_STD_ENC);
  278. }
  279. /* Base64 Encode, with %0A esacped line endings instead of \n */
  280. int Base64_EncodeEsc(const byte* in, word32 inLen, byte* out, word32* outLen)
  281. {
  282. return DoBase64_Encode(in, inLen, out, outLen, WC_ESC_NL_ENC);
  283. }
  284. int Base64_Encode_NoNl(const byte* in, word32 inLen, byte* out, word32* outLen)
  285. {
  286. return DoBase64_Encode(in, inLen, out, outLen, WC_NO_NL_ENC);
  287. }
  288. #endif /* defined(WOLFSSL_BASE64_ENCODE) */
  289. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER) || defined(HAVE_FIPS)
  290. static
  291. const byte hexDecode[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
  292. BAD, BAD, BAD, BAD, BAD, BAD, BAD,
  293. 10, 11, 12, 13, 14, 15, /* upper case A-F */
  294. BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD,
  295. BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD,
  296. BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD,
  297. BAD, BAD, /* G - ` */
  298. 10, 11, 12, 13, 14, 15 /* lower case a-f */
  299. }; /* A starts at 0x41 not 0x3A */
  300. int Base16_Decode(const byte* in, word32 inLen, byte* out, word32* outLen)
  301. {
  302. word32 inIdx = 0;
  303. word32 outIdx = 0;
  304. if (inLen == 1 && *outLen && in) {
  305. byte b = in[inIdx++] - 0x30; /* 0 starts at 0x30 */
  306. /* sanity check */
  307. if (b >= sizeof(hexDecode)/sizeof(hexDecode[0]))
  308. return ASN_INPUT_E;
  309. b = hexDecode[b];
  310. if (b == BAD)
  311. return ASN_INPUT_E;
  312. out[outIdx++] = b;
  313. *outLen = outIdx;
  314. return 0;
  315. }
  316. if (inLen % 2)
  317. return BAD_FUNC_ARG;
  318. if (*outLen < (inLen / 2))
  319. return BAD_FUNC_ARG;
  320. while (inLen) {
  321. byte b = in[inIdx++] - 0x30; /* 0 starts at 0x30 */
  322. byte b2 = in[inIdx++] - 0x30;
  323. /* sanity checks */
  324. if (b >= sizeof(hexDecode)/sizeof(hexDecode[0]))
  325. return ASN_INPUT_E;
  326. if (b2 >= sizeof(hexDecode)/sizeof(hexDecode[0]))
  327. return ASN_INPUT_E;
  328. b = hexDecode[b];
  329. b2 = hexDecode[b2];
  330. if (b == BAD || b2 == BAD)
  331. return ASN_INPUT_E;
  332. out[outIdx++] = (byte)((b << 4) | b2);
  333. inLen -= 2;
  334. }
  335. *outLen = outIdx;
  336. return 0;
  337. }
  338. int Base16_Encode(const byte* in, word32 inLen, byte* out, word32* outLen)
  339. {
  340. word32 outIdx = 0;
  341. word32 i;
  342. byte hb, lb;
  343. if (*outLen < (2 * inLen + 1))
  344. return BAD_FUNC_ARG;
  345. for (i = 0; i < inLen; i++) {
  346. hb = in[i] >> 4;
  347. lb = in[i] & 0x0f;
  348. /* ASCII value */
  349. hb += '0';
  350. if (hb > '9')
  351. hb += 7;
  352. /* ASCII value */
  353. lb += '0';
  354. if (lb>'9')
  355. lb += 7;
  356. out[outIdx++] = hb;
  357. out[outIdx++] = lb;
  358. }
  359. /* force 0 at this end */
  360. out[outIdx++] = 0;
  361. *outLen = outIdx;
  362. return 0;
  363. }
  364. #endif /* (OPENSSL_EXTRA) || (HAVE_WEBSERVER) || (HAVE_FIPS) */
  365. #endif /* NO_CODING */