blob: c40b528e2f56650876d66e285b7be3acb5e6f606 [file] [log] [blame]
jboeufbefd2652014-12-12 15:39:47 -08001/*
2 *
murgatroid993466c4b2016-01-12 10:26:04 -08003 * Copyright 2015-2016, Google Inc.
jboeufbefd2652014-12-12 15:39:47 -08004 * 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
Craig Tiller732a8752016-02-22 15:59:19 -080034#include "src/core/security/b64.h"
jboeufbefd2652014-12-12 15:39:47 -080035
Ian Coolidge069384d2016-01-11 13:41:02 -080036#include <stdint.h>
jboeufbefd2652014-12-12 15:39:47 -080037#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
Ian Coolidge069384d2016-01-11 13:41:02 -080045static const int8_t base64_bytes[] = {
jboeufbefd2652014-12-12 15:39:47 -080046 -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
58static const char base64_url_unsafe_chars[] =
59 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
60static 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
70char *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
Nicolas "Pixel" Noble213ed912015-01-30 02:11:35 +0100117 GPR_ASSERT(current >= result);
Craig Tiller7536af02015-12-22 13:49:30 -0800118 GPR_ASSERT((uintptr_t)(current - result) < result_projected_size);
jboeufbefd2652014-12-12 15:39:47 -0800119 result[current - result] = '\0';
120 return result;
121}
122
123gpr_slice grpc_base64_decode(const char *b64, int url_safe) {
Julien Boeuf3e55b9f2015-06-25 17:54:19 +0200124 return grpc_base64_decode_with_len(b64, strlen(b64), url_safe);
125}
126
127static void decode_one_char(const unsigned char *codes, unsigned char *result,
128 size_t *result_offset) {
Craig Tiller7536af02015-12-22 13:49:30 -0800129 uint32_t packed = ((uint32_t)codes[0] << 2) | ((uint32_t)codes[1] >> 4);
Julien Boeuf3e55b9f2015-06-25 17:54:19 +0200130 result[(*result_offset)++] = (unsigned char)packed;
131}
132
133static void decode_two_chars(const unsigned char *codes, unsigned char *result,
134 size_t *result_offset) {
Craig Tiller7536af02015-12-22 13:49:30 -0800135 uint32_t packed = ((uint32_t)codes[0] << 10) | ((uint32_t)codes[1] << 4) |
136 ((uint32_t)codes[2] >> 2);
Julien Boeuf3e55b9f2015-06-25 17:54:19 +0200137 result[(*result_offset)++] = (unsigned char)(packed >> 8);
138 result[(*result_offset)++] = (unsigned char)(packed);
139}
140
141static 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);
Julien Boeuf73d8b572015-06-30 11:43:18 +0200168 } else {
Julien Boeuf3e55b9f2015-06-25 17:54:19 +0200169 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. */
Craig Tiller7536af02015-12-22 13:49:30 -0800176 uint32_t packed = ((uint32_t)codes[0] << 18) | ((uint32_t)codes[1] << 12) |
177 ((uint32_t)codes[2] << 6) | codes[3];
Julien Boeuf3e55b9f2015-06-25 17:54:19 +0200178 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
185gpr_slice grpc_base64_decode_with_len(const char *b64, size_t b64_len,
186 int url_safe) {
jboeufbefd2652014-12-12 15:39:47 -0800187 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--) {
murgatroid99e582d092015-06-19 12:28:27 -0700194 unsigned char c = (unsigned char)(*b64++);
jboeufbefd2652014-12-12 15:39:47 -0800195 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 {
murgatroid99e582d092015-06-19 12:28:27 -0700215 codes[num_codes++] = (unsigned char)code;
jboeufbefd2652014-12-12 15:39:47 -0800216 if (num_codes == 4) {
Julien Boeuf3e55b9f2015-06-25 17:54:19 +0200217 if (!decode_group(codes, num_codes, current, &result_size)) goto fail;
jboeufbefd2652014-12-12 15:39:47 -0800218 num_codes = 0;
219 }
220 }
221 }
222
Julien Boeuf3e55b9f2015-06-25 17:54:19 +0200223 if (num_codes != 0 &&
224 !decode_group(codes, num_codes, current, &result_size)) {
225 goto fail;
jboeufbefd2652014-12-12 15:39:47 -0800226 }
murgatroid994fca87a2015-06-19 16:15:13 -0700227 GPR_SLICE_SET_LENGTH(result, result_size);
jboeufbefd2652014-12-12 15:39:47 -0800228 return result;
229
230fail:
231 gpr_slice_unref(result);
232 return gpr_empty_slice();
Craig Tiller190d3602015-02-18 09:23:38 -0800233}