base64.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. *
  3. * Copyright 2015, Google Inc.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are
  8. * met:
  9. *
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the following disclaimer
  14. * in the documentation and/or other materials provided with the
  15. * distribution.
  16. * * Neither the name of Google Inc. nor the names of its
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. */
  33. #include "src/core/security/base64.h"
  34. #include <string.h>
  35. #include <grpc/support/alloc.h>
  36. #include <grpc/support/log.h>
  37. #include <grpc/support/useful.h>
  38. /* --- Constants. --- */
  39. static const char base64_bytes[] = {
  40. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  41. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  42. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  43. -1, -1, -1, -1, -1, -1, -1, 0x3E, -1, -1, -1, 0x3F,
  44. 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, -1, -1,
  45. -1, 0x7F, -1, -1, -1, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
  46. 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12,
  47. 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, -1, -1, -1, -1, -1,
  48. -1, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23, 0x24,
  49. 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, 0x30,
  50. 0x31, 0x32, 0x33, -1, -1, -1, -1, -1};
  51. static const char base64_url_unsafe_chars[] =
  52. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  53. static const char base64_url_safe_chars[] =
  54. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
  55. #define GRPC_BASE64_PAD_CHAR '='
  56. #define GRPC_BASE64_PAD_BYTE 0x7F
  57. #define GRPC_BASE64_MULTILINE_LINE_LEN 76
  58. #define GRPC_BASE64_MULTILINE_NUM_BLOCKS (GRPC_BASE64_MULTILINE_LINE_LEN / 4)
  59. /* --- base64 functions. --- */
  60. char *grpc_base64_encode(const void *vdata, size_t data_size, int url_safe,
  61. int multiline) {
  62. const unsigned char *data = vdata;
  63. const char *base64_chars =
  64. url_safe ? base64_url_safe_chars : base64_url_unsafe_chars;
  65. size_t result_projected_size =
  66. 4 * ((data_size + 3) / 3) +
  67. 2 * (multiline ? (data_size / (3 * GRPC_BASE64_MULTILINE_NUM_BLOCKS))
  68. : 0) +
  69. 1;
  70. char *result = gpr_malloc(result_projected_size);
  71. char *current = result;
  72. size_t num_blocks = 0;
  73. size_t i = 0;
  74. /* Encode each block. */
  75. while (data_size >= 3) {
  76. *current++ = base64_chars[(data[i] >> 2) & 0x3F];
  77. *current++ =
  78. base64_chars[((data[i] & 0x03) << 4) | ((data[i + 1] >> 4) & 0x0F)];
  79. *current++ =
  80. base64_chars[((data[i + 1] & 0x0F) << 2) | ((data[i + 2] >> 6) & 0x03)];
  81. *current++ = base64_chars[data[i + 2] & 0x3F];
  82. data_size -= 3;
  83. i += 3;
  84. if (multiline && (++num_blocks == GRPC_BASE64_MULTILINE_NUM_BLOCKS)) {
  85. *current++ = '\r';
  86. *current++ = '\n';
  87. num_blocks = 0;
  88. }
  89. }
  90. /* Take care of the tail. */
  91. if (data_size == 2) {
  92. *current++ = base64_chars[(data[i] >> 2) & 0x3F];
  93. *current++ =
  94. base64_chars[((data[i] & 0x03) << 4) | ((data[i + 1] >> 4) & 0x0F)];
  95. *current++ = base64_chars[(data[i + 1] & 0x0F) << 2];
  96. *current++ = GRPC_BASE64_PAD_CHAR;
  97. } else if (data_size == 1) {
  98. *current++ = base64_chars[(data[i] >> 2) & 0x3F];
  99. *current++ = base64_chars[(data[i] & 0x03) << 4];
  100. *current++ = GRPC_BASE64_PAD_CHAR;
  101. *current++ = GRPC_BASE64_PAD_CHAR;
  102. }
  103. GPR_ASSERT(current >= result);
  104. GPR_ASSERT((gpr_uintptr)(current - result) < result_projected_size);
  105. result[current - result] = '\0';
  106. return result;
  107. }
  108. gpr_slice grpc_base64_decode(const char *b64, int url_safe) {
  109. size_t b64_len = strlen(b64);
  110. gpr_slice result = gpr_slice_malloc(b64_len);
  111. unsigned char *current = GPR_SLICE_START_PTR(result);
  112. size_t result_size = 0;
  113. unsigned char codes[4];
  114. size_t num_codes = 0;
  115. while (b64_len--) {
  116. unsigned char c = *b64++;
  117. signed char code;
  118. if (c >= GPR_ARRAY_SIZE(base64_bytes)) continue;
  119. if (url_safe) {
  120. if (c == '+' || c == '/') {
  121. gpr_log(GPR_ERROR, "Invalid character for url safe base64 %c", c);
  122. goto fail;
  123. }
  124. if (c == '-') {
  125. c = '+';
  126. } else if (c == '_') {
  127. c = '/';
  128. }
  129. }
  130. code = base64_bytes[c];
  131. if (code == -1) {
  132. if (c != '\r' && c != '\n') {
  133. gpr_log(GPR_ERROR, "Invalid character %c", c);
  134. goto fail;
  135. }
  136. } else {
  137. codes[num_codes++] = code;
  138. if (num_codes == 4) {
  139. if (codes[0] == GRPC_BASE64_PAD_BYTE ||
  140. codes[1] == GRPC_BASE64_PAD_BYTE) {
  141. gpr_log(GPR_ERROR, "Invalid padding detected.");
  142. goto fail;
  143. }
  144. if (codes[2] == GRPC_BASE64_PAD_BYTE) {
  145. if (codes[3] == GRPC_BASE64_PAD_BYTE) {
  146. /* Double padding. */
  147. gpr_uint32 packed = (codes[0] << 2) | (codes[1] >> 4);
  148. current[result_size++] = (unsigned char)packed;
  149. } else {
  150. gpr_log(GPR_ERROR, "Invalid padding detected.");
  151. goto fail;
  152. }
  153. } else if (codes[3] == GRPC_BASE64_PAD_BYTE) {
  154. /* Single padding. */
  155. gpr_uint32 packed =
  156. (codes[0] << 10) | (codes[1] << 4) | (codes[2] >> 2);
  157. current[result_size++] = (unsigned char)(packed >> 8);
  158. current[result_size++] = (unsigned char)(packed);
  159. } else {
  160. /* No padding. */
  161. gpr_uint32 packed =
  162. (codes[0] << 18) | (codes[1] << 12) | (codes[2] << 6) | codes[3];
  163. current[result_size++] = (unsigned char)(packed >> 16);
  164. current[result_size++] = (unsigned char)(packed >> 8);
  165. current[result_size++] = (unsigned char)(packed);
  166. }
  167. num_codes = 0;
  168. }
  169. }
  170. }
  171. if (num_codes != 0) {
  172. gpr_log(GPR_ERROR, "Invalid base64.");
  173. gpr_slice_unref(result);
  174. return gpr_empty_slice();
  175. }
  176. GPR_SLICE_SET_LENGTH(result, result_size);
  177. return result;
  178. fail:
  179. gpr_slice_unref(result);
  180. return gpr_empty_slice();
  181. }