LCOV - code coverage report
Current view: top level - core/security - base64.c (source / functions) Hit Total Coverage
Test: tmp.CaZ6RjdVn2 Lines: 105 112 93.8 %
Date: 2015-12-10 22:15:08 Functions: 6 6 100.0 %

          Line data    Source code
       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          50 : char *grpc_base64_encode(const void *vdata, size_t data_size, int url_safe,
      70             :                          int multiline) {
      71          50 :   const unsigned char *data = vdata;
      72          50 :   const char *base64_chars =
      73          50 :       url_safe ? base64_url_safe_chars : base64_url_unsafe_chars;
      74          50 :   size_t result_projected_size =
      75         100 :       4 * ((data_size + 3) / 3) +
      76          50 :       2 * (multiline ? (data_size / (3 * GRPC_BASE64_MULTILINE_NUM_BLOCKS))
      77             :                      : 0) +
      78             :       1;
      79          50 :   char *result = gpr_malloc(result_projected_size);
      80          50 :   char *current = result;
      81          50 :   size_t num_blocks = 0;
      82          50 :   size_t i = 0;
      83             : 
      84             :   /* Encode each block. */
      85        2284 :   while (data_size >= 3) {
      86        2184 :     *current++ = base64_chars[(data[i] >> 2) & 0x3F];
      87        4368 :     *current++ =
      88        2184 :         base64_chars[((data[i] & 0x03) << 4) | ((data[i + 1] >> 4) & 0x0F)];
      89        4368 :     *current++ =
      90        2184 :         base64_chars[((data[i + 1] & 0x0F) << 2) | ((data[i + 2] >> 6) & 0x03)];
      91        2184 :     *current++ = base64_chars[data[i + 2] & 0x3F];
      92             : 
      93        2184 :     data_size -= 3;
      94        2184 :     i += 3;
      95        2184 :     if (multiline && (++num_blocks == GRPC_BASE64_MULTILINE_NUM_BLOCKS)) {
      96          24 :       *current++ = '\r';
      97          24 :       *current++ = '\n';
      98          24 :       num_blocks = 0;
      99             :     }
     100             :   }
     101             : 
     102             :   /* Take care of the tail. */
     103          50 :   if (data_size == 2) {
     104          19 :     *current++ = base64_chars[(data[i] >> 2) & 0x3F];
     105          38 :     *current++ =
     106          19 :         base64_chars[((data[i] & 0x03) << 4) | ((data[i + 1] >> 4) & 0x0F)];
     107          19 :     *current++ = base64_chars[(data[i + 1] & 0x0F) << 2];
     108          19 :     *current++ = GRPC_BASE64_PAD_CHAR;
     109          31 :   } else if (data_size == 1) {
     110          20 :     *current++ = base64_chars[(data[i] >> 2) & 0x3F];
     111          20 :     *current++ = base64_chars[(data[i] & 0x03) << 4];
     112          20 :     *current++ = GRPC_BASE64_PAD_CHAR;
     113          20 :     *current++ = GRPC_BASE64_PAD_CHAR;
     114             :   }
     115             : 
     116          50 :   GPR_ASSERT(current >= result);
     117          50 :   GPR_ASSERT((gpr_uintptr)(current - result) < result_projected_size);
     118          50 :   result[current - result] = '\0';
     119          50 :   return result;
     120             : }
     121             : 
     122          44 : gpr_slice grpc_base64_decode(const char *b64, int url_safe) {
     123          44 :   return grpc_base64_decode_with_len(b64, strlen(b64), url_safe);
     124             : }
     125             : 
     126          18 : static void decode_one_char(const unsigned char *codes, unsigned char *result,
     127             :                             size_t *result_offset) {
     128          18 :   gpr_uint32 packed = ((gpr_uint32)codes[0] << 2) | ((gpr_uint32)codes[1] >> 4);
     129          18 :   result[(*result_offset)++] = (unsigned char)packed;
     130          18 : }
     131             : 
     132          22 : static void decode_two_chars(const unsigned char *codes, unsigned char *result,
     133             :                              size_t *result_offset) {
     134          66 :   gpr_uint32 packed = ((gpr_uint32)codes[0] << 10) |
     135          44 :                       ((gpr_uint32)codes[1] << 4) | ((gpr_uint32)codes[2] >> 2);
     136          22 :   result[(*result_offset)++] = (unsigned char)(packed >> 8);
     137          22 :   result[(*result_offset)++] = (unsigned char)(packed);
     138          22 : }
     139             : 
     140        2223 : static int decode_group(const unsigned char *codes, size_t num_codes,
     141             :                         unsigned char *result, size_t *result_offset) {
     142        2223 :   GPR_ASSERT(num_codes <= 4);
     143             : 
     144             :   /* Short end groups that may not have padding. */
     145        2223 :   if (num_codes == 1) {
     146           0 :     gpr_log(GPR_ERROR, "Invalid group. Must be at least 2 bytes.");
     147           0 :     return 0;
     148             :   }
     149        2223 :   if (num_codes == 2) {
     150           2 :     decode_one_char(codes, result, result_offset);
     151           2 :     return 1;
     152             :   }
     153        2221 :   if (num_codes == 3) {
     154           2 :     decode_two_chars(codes, result, result_offset);
     155           2 :     return 1;
     156             :   }
     157             : 
     158             :   /* Regular 4 byte groups with padding or not. */
     159        2219 :   GPR_ASSERT(num_codes == 4);
     160        2219 :   if (codes[0] == GRPC_BASE64_PAD_BYTE || codes[1] == GRPC_BASE64_PAD_BYTE) {
     161           0 :     gpr_log(GPR_ERROR, "Invalid padding detected.");
     162           0 :     return 0;
     163             :   }
     164        2219 :   if (codes[2] == GRPC_BASE64_PAD_BYTE) {
     165          16 :     if (codes[3] == GRPC_BASE64_PAD_BYTE) {
     166          16 :       decode_one_char(codes, result, result_offset);
     167             :     } else {
     168           0 :       gpr_log(GPR_ERROR, "Invalid padding detected.");
     169           0 :       return 0;
     170             :     }
     171        2203 :   } else if (codes[3] == GRPC_BASE64_PAD_BYTE) {
     172          20 :     decode_two_chars(codes, result, result_offset);
     173             :   } else {
     174             :     /* No padding. */
     175        6549 :     gpr_uint32 packed = ((gpr_uint32)codes[0] << 18) |
     176        4366 :                         ((gpr_uint32)codes[1] << 12) |
     177        4366 :                         ((gpr_uint32)codes[2] << 6) | codes[3];
     178        2183 :     result[(*result_offset)++] = (unsigned char)(packed >> 16);
     179        2183 :     result[(*result_offset)++] = (unsigned char)(packed >> 8);
     180        2183 :     result[(*result_offset)++] = (unsigned char)(packed);
     181             :   }
     182        2219 :   return 1;
     183             : }
     184             : 
     185          56 : gpr_slice grpc_base64_decode_with_len(const char *b64, size_t b64_len,
     186             :                                       int url_safe) {
     187          56 :   gpr_slice result = gpr_slice_malloc(b64_len);
     188          56 :   unsigned char *current = GPR_SLICE_START_PTR(result);
     189          56 :   size_t result_size = 0;
     190             :   unsigned char codes[4];
     191          56 :   size_t num_codes = 0;
     192             : 
     193        9052 :   while (b64_len--) {
     194        8942 :     unsigned char c = (unsigned char)(*b64++);
     195             :     signed char code;
     196        8942 :     if (c >= GPR_ARRAY_SIZE(base64_bytes)) continue;
     197        8942 :     if (url_safe) {
     198        6740 :       if (c == '+' || c == '/') {
     199           1 :         gpr_log(GPR_ERROR, "Invalid character for url safe base64 %c", c);
     200           1 :         goto fail;
     201             :       }
     202        6739 :       if (c == '-') {
     203          68 :         c = '+';
     204        6671 :       } else if (c == '_') {
     205          66 :         c = '/';
     206             :       }
     207             :     }
     208        8941 :     code = base64_bytes[c];
     209        8941 :     if (code == -1) {
     210          49 :       if (c != '\r' && c != '\n') {
     211           1 :         gpr_log(GPR_ERROR, "Invalid character %c", c);
     212           1 :         goto fail;
     213             :       }
     214             :     } else {
     215        8892 :       codes[num_codes++] = (unsigned char)code;
     216        8892 :       if (num_codes == 4) {
     217        2219 :         if (!decode_group(codes, num_codes, current, &result_size)) goto fail;
     218        2219 :         num_codes = 0;
     219             :       }
     220             :     }
     221             :   }
     222             : 
     223          58 :   if (num_codes != 0 &&
     224           4 :       !decode_group(codes, num_codes, current, &result_size)) {
     225           0 :     goto fail;
     226             :   }
     227          54 :   GPR_SLICE_SET_LENGTH(result, result_size);
     228          54 :   return result;
     229             : 
     230             : fail:
     231           2 :   gpr_slice_unref(result);
     232           2 :   return gpr_empty_slice();
     233             : }

Generated by: LCOV version 1.11