1 /* 2 * Base64 encoding/decoding (RFC1341) 3 * Copyright (c) 2005-2011, Jouni Malinen <j@w1.fi> 4 * 5 * This software may be distributed under the terms of the BSD license. 6 * See README for more details. 7 */ 8 9 #include "includes.h" 10 11 #include "os.h" 12 #include "base64.h" 13 14 static const unsigned char base64_table[65] = 15 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; 16 static const unsigned char base64_url_table[65] = 17 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"; 18 19 20 static unsigned char * base64_gen_encode(const unsigned char *src, size_t len, 21 size_t *out_len, 22 const unsigned char *table, 23 int add_pad) 24 { 25 unsigned char *out, *pos; 26 const unsigned char *end, *in; 27 size_t olen; 28 int line_len; 29 30 olen = len * 4 / 3 + 4; /* 3-byte blocks to 4-byte */ 31 if (add_pad) 32 olen += olen / 72; /* line feeds */ 33 olen++; /* nul termination */ 34 if (olen < len) 35 return NULL; /* integer overflow */ 36 out = os_malloc(olen); 37 if (out == NULL) 38 return NULL; 39 40 end = src + len; 41 in = src; 42 pos = out; 43 line_len = 0; 44 while (end - in >= 3) { 45 *pos++ = table[(in[0] >> 2) & 0x3f]; 46 *pos++ = table[(((in[0] & 0x03) << 4) | (in[1] >> 4)) & 0x3f]; 47 *pos++ = table[(((in[1] & 0x0f) << 2) | (in[2] >> 6)) & 0x3f]; 48 *pos++ = table[in[2] & 0x3f]; 49 in += 3; 50 line_len += 4; 51 if (add_pad && line_len >= 72) { 52 *pos++ = '\n'; 53 line_len = 0; 54 } 55 } 56 57 if (end - in) { 58 *pos++ = table[(in[0] >> 2) & 0x3f]; 59 if (end - in == 1) { 60 *pos++ = table[((in[0] & 0x03) << 4) & 0x3f]; 61 if (add_pad) 62 *pos++ = '='; 63 } else { 64 *pos++ = table[(((in[0] & 0x03) << 4) | 65 (in[1] >> 4)) & 0x3f]; 66 *pos++ = table[((in[1] & 0x0f) << 2) & 0x3f]; 67 } 68 if (add_pad) 69 *pos++ = '='; 70 line_len += 4; 71 } 72 73 if (add_pad && line_len) 74 *pos++ = '\n'; 75 76 *pos = '\0'; 77 if (out_len) 78 *out_len = pos - out; 79 return out; 80 } 81 82 83 static unsigned char * base64_gen_decode(const unsigned char *src, size_t len, 84 size_t *out_len, 85 const unsigned char *table) 86 { 87 unsigned char dtable[256], *out, *pos, block[4], tmp; 88 size_t i, count, olen; 89 int pad = 0; 90 size_t extra_pad; 91 92 os_memset(dtable, 0x80, 256); 93 for (i = 0; i < sizeof(base64_table) - 1; i++) 94 dtable[table[i]] = (unsigned char) i; 95 dtable['='] = 0; 96 97 count = 0; 98 for (i = 0; i < len; i++) { 99 if (dtable[src[i]] != 0x80) 100 count++; 101 } 102 103 if (count == 0) 104 return NULL; 105 extra_pad = (4 - count % 4) % 4; 106 107 olen = (count + extra_pad) / 4 * 3; 108 pos = out = os_malloc(olen); 109 if (out == NULL) 110 return NULL; 111 112 count = 0; 113 for (i = 0; i < len + extra_pad; i++) { 114 unsigned char val; 115 116 if (i >= len) 117 val = '='; 118 else 119 val = src[i]; 120 tmp = dtable[val]; 121 if (tmp == 0x80) 122 continue; 123 124 if (val == '=') 125 pad++; 126 block[count] = tmp; 127 count++; 128 if (count == 4) { 129 *pos++ = (block[0] << 2) | (block[1] >> 4); 130 *pos++ = (block[1] << 4) | (block[2] >> 2); 131 *pos++ = (block[2] << 6) | block[3]; 132 count = 0; 133 if (pad) { 134 if (pad == 1) 135 pos--; 136 else if (pad == 2) 137 pos -= 2; 138 else { 139 /* Invalid padding */ 140 os_free(out); 141 return NULL; 142 } 143 break; 144 } 145 } 146 } 147 148 *out_len = pos - out; 149 return out; 150 } 151 152 153 /** 154 * base64_encode - Base64 encode 155 * @src: Data to be encoded 156 * @len: Length of the data to be encoded 157 * @out_len: Pointer to output length variable, or %NULL if not used 158 * Returns: Allocated buffer of out_len bytes of encoded data, 159 * or %NULL on failure 160 * 161 * Caller is responsible for freeing the returned buffer. Returned buffer is 162 * nul terminated to make it easier to use as a C string. The nul terminator is 163 * not included in out_len. 164 */ 165 unsigned char * base64_encode(const unsigned char *src, size_t len, 166 size_t *out_len) 167 { 168 return base64_gen_encode(src, len, out_len, base64_table, 1); 169 } 170 171 172 unsigned char * base64_url_encode(const unsigned char *src, size_t len, 173 size_t *out_len, int add_pad) 174 { 175 return base64_gen_encode(src, len, out_len, base64_url_table, add_pad); 176 } 177 178 179 /** 180 * base64_decode - Base64 decode 181 * @src: Data to be decoded 182 * @len: Length of the data to be decoded 183 * @out_len: Pointer to output length variable 184 * Returns: Allocated buffer of out_len bytes of decoded data, 185 * or %NULL on failure 186 * 187 * Caller is responsible for freeing the returned buffer. 188 */ 189 unsigned char * base64_decode(const unsigned char *src, size_t len, 190 size_t *out_len) 191 { 192 return base64_gen_decode(src, len, out_len, base64_table); 193 } 194 195 196 unsigned char * base64_url_decode(const unsigned char *src, size_t len, 197 size_t *out_len) 198 { 199 return base64_gen_decode(src, len, out_len, base64_url_table); 200 } 201