OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * |
| 3 * Copyright 2015-2016, 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 |
| 34 #include "src/core/security/b64.h" |
| 35 |
| 36 #include <stdint.h> |
| 37 #include <string.h> |
| 38 |
| 39 #include <grpc/support/alloc.h> |
| 40 #include <grpc/support/log.h> |
| 41 #include <grpc/support/useful.h> |
| 42 |
| 43 /* --- Constants. --- */ |
| 44 |
| 45 static const int8_t base64_bytes[] = { |
| 46 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 47 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 48 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 49 -1, -1, -1, -1, -1, -1, -1, 0x3E, -1, -1, -1, 0x3F, |
| 50 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, -1, -1, |
| 51 -1, 0x7F, -1, -1, -1, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, |
| 52 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, |
| 53 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, -1, -1, -1, -1, -1, |
| 54 -1, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23, 0x24, |
| 55 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, 0x30, |
| 56 0x31, 0x32, 0x33, -1, -1, -1, -1, -1}; |
| 57 |
| 58 static const char base64_url_unsafe_chars[] = |
| 59 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
| 60 static const char base64_url_safe_chars[] = |
| 61 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"; |
| 62 |
| 63 #define GRPC_BASE64_PAD_CHAR '=' |
| 64 #define GRPC_BASE64_PAD_BYTE 0x7F |
| 65 #define GRPC_BASE64_MULTILINE_LINE_LEN 76 |
| 66 #define GRPC_BASE64_MULTILINE_NUM_BLOCKS (GRPC_BASE64_MULTILINE_LINE_LEN / 4) |
| 67 |
| 68 /* --- base64 functions. --- */ |
| 69 |
| 70 char *grpc_base64_encode(const void *vdata, size_t data_size, int url_safe, |
| 71 int multiline) { |
| 72 const unsigned char *data = vdata; |
| 73 const char *base64_chars = |
| 74 url_safe ? base64_url_safe_chars : base64_url_unsafe_chars; |
| 75 size_t result_projected_size = |
| 76 4 * ((data_size + 3) / 3) + |
| 77 2 * (multiline ? (data_size / (3 * GRPC_BASE64_MULTILINE_NUM_BLOCKS)) |
| 78 : 0) + |
| 79 1; |
| 80 char *result = gpr_malloc(result_projected_size); |
| 81 char *current = result; |
| 82 size_t num_blocks = 0; |
| 83 size_t i = 0; |
| 84 |
| 85 /* Encode each block. */ |
| 86 while (data_size >= 3) { |
| 87 *current++ = base64_chars[(data[i] >> 2) & 0x3F]; |
| 88 *current++ = |
| 89 base64_chars[((data[i] & 0x03) << 4) | ((data[i + 1] >> 4) & 0x0F)]; |
| 90 *current++ = |
| 91 base64_chars[((data[i + 1] & 0x0F) << 2) | ((data[i + 2] >> 6) & 0x03)]; |
| 92 *current++ = base64_chars[data[i + 2] & 0x3F]; |
| 93 |
| 94 data_size -= 3; |
| 95 i += 3; |
| 96 if (multiline && (++num_blocks == GRPC_BASE64_MULTILINE_NUM_BLOCKS)) { |
| 97 *current++ = '\r'; |
| 98 *current++ = '\n'; |
| 99 num_blocks = 0; |
| 100 } |
| 101 } |
| 102 |
| 103 /* Take care of the tail. */ |
| 104 if (data_size == 2) { |
| 105 *current++ = base64_chars[(data[i] >> 2) & 0x3F]; |
| 106 *current++ = |
| 107 base64_chars[((data[i] & 0x03) << 4) | ((data[i + 1] >> 4) & 0x0F)]; |
| 108 *current++ = base64_chars[(data[i + 1] & 0x0F) << 2]; |
| 109 *current++ = GRPC_BASE64_PAD_CHAR; |
| 110 } else if (data_size == 1) { |
| 111 *current++ = base64_chars[(data[i] >> 2) & 0x3F]; |
| 112 *current++ = base64_chars[(data[i] & 0x03) << 4]; |
| 113 *current++ = GRPC_BASE64_PAD_CHAR; |
| 114 *current++ = GRPC_BASE64_PAD_CHAR; |
| 115 } |
| 116 |
| 117 GPR_ASSERT(current >= result); |
| 118 GPR_ASSERT((uintptr_t)(current - result) < result_projected_size); |
| 119 result[current - result] = '\0'; |
| 120 return result; |
| 121 } |
| 122 |
| 123 gpr_slice grpc_base64_decode(const char *b64, int url_safe) { |
| 124 return grpc_base64_decode_with_len(b64, strlen(b64), url_safe); |
| 125 } |
| 126 |
| 127 static void decode_one_char(const unsigned char *codes, unsigned char *result, |
| 128 size_t *result_offset) { |
| 129 uint32_t packed = ((uint32_t)codes[0] << 2) | ((uint32_t)codes[1] >> 4); |
| 130 result[(*result_offset)++] = (unsigned char)packed; |
| 131 } |
| 132 |
| 133 static void decode_two_chars(const unsigned char *codes, unsigned char *result, |
| 134 size_t *result_offset) { |
| 135 uint32_t packed = ((uint32_t)codes[0] << 10) | ((uint32_t)codes[1] << 4) | |
| 136 ((uint32_t)codes[2] >> 2); |
| 137 result[(*result_offset)++] = (unsigned char)(packed >> 8); |
| 138 result[(*result_offset)++] = (unsigned char)(packed); |
| 139 } |
| 140 |
| 141 static int decode_group(const unsigned char *codes, size_t num_codes, |
| 142 unsigned char *result, size_t *result_offset) { |
| 143 GPR_ASSERT(num_codes <= 4); |
| 144 |
| 145 /* Short end groups that may not have padding. */ |
| 146 if (num_codes == 1) { |
| 147 gpr_log(GPR_ERROR, "Invalid group. Must be at least 2 bytes."); |
| 148 return 0; |
| 149 } |
| 150 if (num_codes == 2) { |
| 151 decode_one_char(codes, result, result_offset); |
| 152 return 1; |
| 153 } |
| 154 if (num_codes == 3) { |
| 155 decode_two_chars(codes, result, result_offset); |
| 156 return 1; |
| 157 } |
| 158 |
| 159 /* Regular 4 byte groups with padding or not. */ |
| 160 GPR_ASSERT(num_codes == 4); |
| 161 if (codes[0] == GRPC_BASE64_PAD_BYTE || codes[1] == GRPC_BASE64_PAD_BYTE) { |
| 162 gpr_log(GPR_ERROR, "Invalid padding detected."); |
| 163 return 0; |
| 164 } |
| 165 if (codes[2] == GRPC_BASE64_PAD_BYTE) { |
| 166 if (codes[3] == GRPC_BASE64_PAD_BYTE) { |
| 167 decode_one_char(codes, result, result_offset); |
| 168 } else { |
| 169 gpr_log(GPR_ERROR, "Invalid padding detected."); |
| 170 return 0; |
| 171 } |
| 172 } else if (codes[3] == GRPC_BASE64_PAD_BYTE) { |
| 173 decode_two_chars(codes, result, result_offset); |
| 174 } else { |
| 175 /* No padding. */ |
| 176 uint32_t packed = ((uint32_t)codes[0] << 18) | ((uint32_t)codes[1] << 12) | |
| 177 ((uint32_t)codes[2] << 6) | codes[3]; |
| 178 result[(*result_offset)++] = (unsigned char)(packed >> 16); |
| 179 result[(*result_offset)++] = (unsigned char)(packed >> 8); |
| 180 result[(*result_offset)++] = (unsigned char)(packed); |
| 181 } |
| 182 return 1; |
| 183 } |
| 184 |
| 185 gpr_slice grpc_base64_decode_with_len(const char *b64, size_t b64_len, |
| 186 int url_safe) { |
| 187 gpr_slice result = gpr_slice_malloc(b64_len); |
| 188 unsigned char *current = GPR_SLICE_START_PTR(result); |
| 189 size_t result_size = 0; |
| 190 unsigned char codes[4]; |
| 191 size_t num_codes = 0; |
| 192 |
| 193 while (b64_len--) { |
| 194 unsigned char c = (unsigned char)(*b64++); |
| 195 signed char code; |
| 196 if (c >= GPR_ARRAY_SIZE(base64_bytes)) continue; |
| 197 if (url_safe) { |
| 198 if (c == '+' || c == '/') { |
| 199 gpr_log(GPR_ERROR, "Invalid character for url safe base64 %c", c); |
| 200 goto fail; |
| 201 } |
| 202 if (c == '-') { |
| 203 c = '+'; |
| 204 } else if (c == '_') { |
| 205 c = '/'; |
| 206 } |
| 207 } |
| 208 code = base64_bytes[c]; |
| 209 if (code == -1) { |
| 210 if (c != '\r' && c != '\n') { |
| 211 gpr_log(GPR_ERROR, "Invalid character %c", c); |
| 212 goto fail; |
| 213 } |
| 214 } else { |
| 215 codes[num_codes++] = (unsigned char)code; |
| 216 if (num_codes == 4) { |
| 217 if (!decode_group(codes, num_codes, current, &result_size)) goto fail; |
| 218 num_codes = 0; |
| 219 } |
| 220 } |
| 221 } |
| 222 |
| 223 if (num_codes != 0 && |
| 224 !decode_group(codes, num_codes, current, &result_size)) { |
| 225 goto fail; |
| 226 } |
| 227 GPR_SLICE_SET_LENGTH(result, result_size); |
| 228 return result; |
| 229 |
| 230 fail: |
| 231 gpr_slice_unref(result); |
| 232 return gpr_empty_slice(); |
| 233 } |
OLD | NEW |