blob: 53a92f49ed83f639acf9f83f48f1de1f071d6953 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * Base64 encoding/decoding (RFC1341)
Hai Shalom021b0b52019-04-10 11:17:58 -07003 * Copyright (c) 2005-2019, Jouni Malinen <j@w1.fi>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007 */
8
9#include "includes.h"
Hai Shalom021b0b52019-04-10 11:17:58 -070010#include <stdint.h>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070011
12#include "os.h"
13#include "base64.h"
14
15static const unsigned char base64_table[65] =
16 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070017static const unsigned char base64_url_table[65] =
18 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070019
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070020
21static unsigned char * base64_gen_encode(const unsigned char *src, size_t len,
22 size_t *out_len,
23 const unsigned char *table,
24 int add_pad)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070025{
26 unsigned char *out, *pos;
27 const unsigned char *end, *in;
28 size_t olen;
29 int line_len;
30
Hai Shalom021b0b52019-04-10 11:17:58 -070031 if (len >= SIZE_MAX / 4)
32 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070033 olen = len * 4 / 3 + 4; /* 3-byte blocks to 4-byte */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070034 if (add_pad)
35 olen += olen / 72; /* line feeds */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070036 olen++; /* nul termination */
37 if (olen < len)
38 return NULL; /* integer overflow */
39 out = os_malloc(olen);
40 if (out == NULL)
41 return NULL;
42
43 end = src + len;
44 in = src;
45 pos = out;
46 line_len = 0;
47 while (end - in >= 3) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070048 *pos++ = table[(in[0] >> 2) & 0x3f];
49 *pos++ = table[(((in[0] & 0x03) << 4) | (in[1] >> 4)) & 0x3f];
50 *pos++ = table[(((in[1] & 0x0f) << 2) | (in[2] >> 6)) & 0x3f];
51 *pos++ = table[in[2] & 0x3f];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070052 in += 3;
53 line_len += 4;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070054 if (add_pad && line_len >= 72) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070055 *pos++ = '\n';
56 line_len = 0;
57 }
58 }
59
60 if (end - in) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070061 *pos++ = table[(in[0] >> 2) & 0x3f];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070062 if (end - in == 1) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070063 *pos++ = table[((in[0] & 0x03) << 4) & 0x3f];
64 if (add_pad)
65 *pos++ = '=';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070066 } else {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070067 *pos++ = table[(((in[0] & 0x03) << 4) |
68 (in[1] >> 4)) & 0x3f];
69 *pos++ = table[((in[1] & 0x0f) << 2) & 0x3f];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070070 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070071 if (add_pad)
72 *pos++ = '=';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070073 line_len += 4;
74 }
75
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070076 if (add_pad && line_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070077 *pos++ = '\n';
78
79 *pos = '\0';
80 if (out_len)
81 *out_len = pos - out;
82 return out;
83}
84
85
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070086static unsigned char * base64_gen_decode(const unsigned char *src, size_t len,
87 size_t *out_len,
88 const unsigned char *table)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070089{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080090 unsigned char dtable[256], *out, *pos, block[4], tmp;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070091 size_t i, count, olen;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080092 int pad = 0;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070093 size_t extra_pad;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070094
95 os_memset(dtable, 0x80, 256);
96 for (i = 0; i < sizeof(base64_table) - 1; i++)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070097 dtable[table[i]] = (unsigned char) i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070098 dtable['='] = 0;
99
100 count = 0;
101 for (i = 0; i < len; i++) {
102 if (dtable[src[i]] != 0x80)
103 count++;
104 }
105
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700106 if (count == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700107 return NULL;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700108 extra_pad = (4 - count % 4) % 4;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700109
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700110 olen = (count + extra_pad) / 4 * 3;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700111 pos = out = os_malloc(olen);
112 if (out == NULL)
113 return NULL;
114
115 count = 0;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700116 for (i = 0; i < len + extra_pad; i++) {
117 unsigned char val;
118
119 if (i >= len)
120 val = '=';
121 else
122 val = src[i];
123 tmp = dtable[val];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700124 if (tmp == 0x80)
125 continue;
126
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700127 if (val == '=')
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800128 pad++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700129 block[count] = tmp;
130 count++;
131 if (count == 4) {
132 *pos++ = (block[0] << 2) | (block[1] >> 4);
133 *pos++ = (block[1] << 4) | (block[2] >> 2);
134 *pos++ = (block[2] << 6) | block[3];
135 count = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800136 if (pad) {
137 if (pad == 1)
138 pos--;
139 else if (pad == 2)
140 pos -= 2;
141 else {
142 /* Invalid padding */
143 os_free(out);
144 return NULL;
145 }
146 break;
147 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700148 }
149 }
150
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700151 *out_len = pos - out;
152 return out;
153}
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700154
155
156/**
157 * base64_encode - Base64 encode
158 * @src: Data to be encoded
159 * @len: Length of the data to be encoded
160 * @out_len: Pointer to output length variable, or %NULL if not used
161 * Returns: Allocated buffer of out_len bytes of encoded data,
162 * or %NULL on failure
163 *
164 * Caller is responsible for freeing the returned buffer. Returned buffer is
165 * nul terminated to make it easier to use as a C string. The nul terminator is
166 * not included in out_len.
167 */
168unsigned char * base64_encode(const unsigned char *src, size_t len,
169 size_t *out_len)
170{
171 return base64_gen_encode(src, len, out_len, base64_table, 1);
172}
173
174
175unsigned char * base64_url_encode(const unsigned char *src, size_t len,
176 size_t *out_len, int add_pad)
177{
178 return base64_gen_encode(src, len, out_len, base64_url_table, add_pad);
179}
180
181
182/**
183 * base64_decode - Base64 decode
184 * @src: Data to be decoded
185 * @len: Length of the data to be decoded
186 * @out_len: Pointer to output length variable
187 * Returns: Allocated buffer of out_len bytes of decoded data,
188 * or %NULL on failure
189 *
190 * Caller is responsible for freeing the returned buffer.
191 */
192unsigned char * base64_decode(const unsigned char *src, size_t len,
193 size_t *out_len)
194{
195 return base64_gen_decode(src, len, out_len, base64_table);
196}
197
198
199unsigned char * base64_url_decode(const unsigned char *src, size_t len,
200 size_t *out_len)
201{
202 return base64_gen_decode(src, len, out_len, base64_url_table);
203}