blob: 8eb4ba127d480372c259612315016bdffd47cffd [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * Base64 encoding/decoding (RFC1341)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003 * Copyright (c) 2005-2011, 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"
10
11#include "os.h"
12#include "base64.h"
13
14static const unsigned char base64_table[65] =
15 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070016static const unsigned char base64_url_table[65] =
17 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070018
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070019
20static 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)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070024{
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 */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070031 if (add_pad)
32 olen += olen / 72; /* line feeds */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070033 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) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070045 *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];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070049 in += 3;
50 line_len += 4;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070051 if (add_pad && line_len >= 72) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070052 *pos++ = '\n';
53 line_len = 0;
54 }
55 }
56
57 if (end - in) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070058 *pos++ = table[(in[0] >> 2) & 0x3f];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070059 if (end - in == 1) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070060 *pos++ = table[((in[0] & 0x03) << 4) & 0x3f];
61 if (add_pad)
62 *pos++ = '=';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070063 } else {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070064 *pos++ = table[(((in[0] & 0x03) << 4) |
65 (in[1] >> 4)) & 0x3f];
66 *pos++ = table[((in[1] & 0x0f) << 2) & 0x3f];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070067 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070068 if (add_pad)
69 *pos++ = '=';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070070 line_len += 4;
71 }
72
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070073 if (add_pad && line_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070074 *pos++ = '\n';
75
76 *pos = '\0';
77 if (out_len)
78 *out_len = pos - out;
79 return out;
80}
81
82
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070083static unsigned char * base64_gen_decode(const unsigned char *src, size_t len,
84 size_t *out_len,
85 const unsigned char *table)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070086{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080087 unsigned char dtable[256], *out, *pos, block[4], tmp;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070088 size_t i, count, olen;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080089 int pad = 0;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070090 size_t extra_pad;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070091
92 os_memset(dtable, 0x80, 256);
93 for (i = 0; i < sizeof(base64_table) - 1; i++)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070094 dtable[table[i]] = (unsigned char) i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070095 dtable['='] = 0;
96
97 count = 0;
98 for (i = 0; i < len; i++) {
99 if (dtable[src[i]] != 0x80)
100 count++;
101 }
102
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700103 if (count == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700104 return NULL;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700105 extra_pad = (4 - count % 4) % 4;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700106
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700107 olen = (count + extra_pad) / 4 * 3;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700108 pos = out = os_malloc(olen);
109 if (out == NULL)
110 return NULL;
111
112 count = 0;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700113 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];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700121 if (tmp == 0x80)
122 continue;
123
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700124 if (val == '=')
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800125 pad++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700126 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;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800133 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 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700145 }
146 }
147
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700148 *out_len = pos - out;
149 return out;
150}
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700151
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 */
165unsigned 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
172unsigned 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 */
189unsigned 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
196unsigned 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}