~ubuntu-branches/ubuntu/wily/grpc/wily

« back to all changes in this revision

Viewing changes to src/core/security/base64.c

  • Committer: Package Import Robot
  • Author(s): Andrew Pollock
  • Date: 2015-05-07 13:28:11 UTC
  • Revision ID: package-import@ubuntu.com-20150507132811-ybm4hfq73tnvvd2e
Tags: upstream-0.10.0
ImportĀ upstreamĀ versionĀ 0.10.0

Show diffs side-by-side

added added

removed removed

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