coding.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /* coding.c
  2. *
  3. * Copyright (C) 2006-2011 Sawtooth Consulting Ltd.
  4. *
  5. * This file is part of CyaSSL.
  6. *
  7. * CyaSSL 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. * CyaSSL 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
  20. */
  21. #include "coding.h"
  22. enum {
  23. BAD = 0xFF, /* invalid encoding */
  24. PAD = '=',
  25. PEM_LINE_SZ = 64
  26. };
  27. static
  28. const byte base64Decode[] = { 62, BAD, BAD, BAD, 63, /* + starts at 0x2B */
  29. 52, 53, 54, 55, 56, 57, 58, 59, 60, 61,
  30. BAD, BAD, BAD, BAD, BAD, BAD, BAD,
  31. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
  32. 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
  33. 20, 21, 22, 23, 24, 25,
  34. BAD, BAD, BAD, BAD, BAD, BAD,
  35. 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
  36. 36, 37, 38, 39, 40, 41, 42, 43, 44, 45,
  37. 46, 47, 48, 49, 50, 51
  38. };
  39. int Base64Decode(const byte* in, word32 inLen, byte* out, word32* outLen)
  40. {
  41. word32 i = 0;
  42. word32 j = 0;
  43. word32 plainSz = inLen - ((inLen + (PEM_LINE_SZ - 1)) / PEM_LINE_SZ );
  44. plainSz = (plainSz * 3 + 3) / 4;
  45. if (plainSz > *outLen) return -1;
  46. while (inLen > 3) {
  47. byte b1, b2, b3;
  48. byte e1 = in[j++];
  49. byte e2 = in[j++];
  50. byte e3 = in[j++];
  51. byte e4 = in[j++];
  52. int pad3 = 0;
  53. int pad4 = 0;
  54. if (e1 == 0) /* end file 0's */
  55. break;
  56. if (e3 == PAD)
  57. pad3 = 1;
  58. if (e4 == PAD)
  59. pad4 = 1;
  60. e1 = base64Decode[e1 - 0x2B];
  61. e2 = base64Decode[e2 - 0x2B];
  62. e3 = (e3 == PAD) ? 0 : base64Decode[e3 - 0x2B];
  63. e4 = (e4 == PAD) ? 0 : base64Decode[e4 - 0x2B];
  64. b1 = (e1 << 2) | (e2 >> 4);
  65. b2 = ((e2 & 0xF) << 4) | (e3 >> 2);
  66. b3 = ((e3 & 0x3) << 6) | e4;
  67. out[i++] = b1;
  68. if (!pad3)
  69. out[i++] = b2;
  70. if (!pad4)
  71. out[i++] = b3;
  72. else
  73. break;
  74. inLen -= 4;
  75. if (in[j] == ' ' || in[j] == '\r' || in[j] == '\n') {
  76. byte endLine = in[j++];
  77. inLen--;
  78. while (endLine == ' ') { /* allow trailing whitespace */
  79. endLine = in[j++];
  80. inLen--;
  81. }
  82. if (endLine == '\r') {
  83. endLine = in[j++];
  84. inLen--;
  85. }
  86. if (endLine != '\n')
  87. return -1;
  88. }
  89. }
  90. *outLen = i;
  91. return 0;
  92. }
  93. #if defined(OPENSSL_EXTRA) || defined (SESSION_CERTS) || defined(CYASSL_KEY_GEN) || defined(CYASSL_CERT_GEN)
  94. static
  95. const byte base64Encode[] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
  96. 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
  97. 'U', 'V', 'W', 'X', 'Y', 'Z',
  98. 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
  99. 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
  100. 'u', 'v', 'w', 'x', 'y', 'z',
  101. '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  102. '+', '/'
  103. };
  104. /* porting assistance from yaSSL by Raphael HUCK */
  105. int Base64Encode(const byte* in, word32 inLen, byte* out, word32* outLen)
  106. {
  107. word32 i = 0,
  108. j = 0,
  109. n = 0; /* new line counter */
  110. word32 outSz = (inLen + 3 - 1) / 3 * 4;
  111. outSz += (outSz + PEM_LINE_SZ - 1) / PEM_LINE_SZ; /* new lines */
  112. if (outSz > *outLen) return -1;
  113. while (inLen > 2) {
  114. byte b1 = in[j++];
  115. byte b2 = in[j++];
  116. byte b3 = in[j++];
  117. /* encoded idx */
  118. byte e1 = b1 >> 2;
  119. byte e2 = ((b1 & 0x3) << 4) | (b2 >> 4);
  120. byte e3 = ((b2 & 0xF) << 2) | (b3 >> 6);
  121. byte e4 = b3 & 0x3F;
  122. /* store */
  123. out[i++] = base64Encode[e1];
  124. out[i++] = base64Encode[e2];
  125. out[i++] = base64Encode[e3];
  126. out[i++] = base64Encode[e4];
  127. inLen -= 3;
  128. if ((++n % (PEM_LINE_SZ / 4)) == 0 && inLen)
  129. out[i++] = '\n';
  130. }
  131. /* last integral */
  132. if (inLen) {
  133. int twoBytes = (inLen == 2);
  134. byte b1 = in[j++];
  135. byte b2 = (twoBytes) ? in[j++] : 0;
  136. byte e1 = b1 >> 2;
  137. byte e2 = ((b1 & 0x3) << 4) | (b2 >> 4);
  138. byte e3 = (b2 & 0xF) << 2;
  139. out[i++] = base64Encode[e1];
  140. out[i++] = base64Encode[e2];
  141. out[i++] = (twoBytes) ? base64Encode[e3] : PAD;
  142. out[i++] = PAD;
  143. }
  144. out[i++] = '\n';
  145. if (i != outSz)
  146. return -1;
  147. *outLen = outSz;
  148. return 0;
  149. }
  150. static
  151. const byte hexDecode[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
  152. BAD, BAD, BAD, BAD, BAD, BAD, BAD,
  153. 10, 11, 12, 13, 14, 15
  154. }; /* A starts at 0x41 not 0x3A */
  155. int Base16Decode(const byte* in, word32 inLen, byte* out, word32* outLen)
  156. {
  157. word32 inIdx = 0;
  158. word32 outIdx = 0;
  159. if (inLen % 2)
  160. return -1;
  161. if (*outLen < (inLen / 2))
  162. return -1;
  163. while (inLen) {
  164. byte b = in[inIdx++] - 0x30; /* 0 starts at 0x30 */
  165. byte b2 = in[inIdx++] - 0x30;
  166. /* sanity checks */
  167. if (b >= sizeof(hexDecode)/sizeof(hexDecode[0]))
  168. return -1;
  169. if (b2 >= sizeof(hexDecode)/sizeof(hexDecode[0]))
  170. return -1;
  171. b = hexDecode[b];
  172. b2 = hexDecode[b2];
  173. if (b == BAD || b2 == BAD)
  174. return -1;
  175. out[outIdx++] = (b << 4) | b2;
  176. inLen -= 2;
  177. }
  178. *outLen = outIdx;
  179. return 0;
  180. }
  181. #endif /* OPENSSL_EXTRA */