blob: 0d121c1989cac9349949b2cd3611f49a1ef2820f [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
Hai Shalom899fcc72020-10-19 14:38:18 -070012#include "utils/common.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070013#include "os.h"
14#include "base64.h"
15
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -080016static const char base64_table[65] =
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070017 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -080018static const char base64_url_table[65] =
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070019 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070020
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070021
Hai Shalom899fcc72020-10-19 14:38:18 -070022#define BASE64_PAD BIT(0)
23#define BASE64_LF BIT(1)
24
25
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -080026static char * base64_gen_encode(const unsigned char *src, size_t len,
27 size_t *out_len, const char *table, int add_pad)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070028{
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -080029 char *out, *pos;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070030 const unsigned char *end, *in;
31 size_t olen;
32 int line_len;
33
Hai Shalom021b0b52019-04-10 11:17:58 -070034 if (len >= SIZE_MAX / 4)
35 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070036 olen = len * 4 / 3 + 4; /* 3-byte blocks to 4-byte */
Hai Shalom899fcc72020-10-19 14:38:18 -070037 if (add_pad & BASE64_LF)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070038 olen += olen / 72; /* line feeds */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070039 olen++; /* nul termination */
40 if (olen < len)
41 return NULL; /* integer overflow */
42 out = os_malloc(olen);
43 if (out == NULL)
44 return NULL;
45
46 end = src + len;
47 in = src;
48 pos = out;
49 line_len = 0;
50 while (end - in >= 3) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070051 *pos++ = table[(in[0] >> 2) & 0x3f];
52 *pos++ = table[(((in[0] & 0x03) << 4) | (in[1] >> 4)) & 0x3f];
53 *pos++ = table[(((in[1] & 0x0f) << 2) | (in[2] >> 6)) & 0x3f];
54 *pos++ = table[in[2] & 0x3f];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070055 in += 3;
56 line_len += 4;
Hai Shalom899fcc72020-10-19 14:38:18 -070057 if ((add_pad & BASE64_LF) && line_len >= 72) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070058 *pos++ = '\n';
59 line_len = 0;
60 }
61 }
62
63 if (end - in) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070064 *pos++ = table[(in[0] >> 2) & 0x3f];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070065 if (end - in == 1) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070066 *pos++ = table[((in[0] & 0x03) << 4) & 0x3f];
Hai Shalom899fcc72020-10-19 14:38:18 -070067 if (add_pad & BASE64_PAD)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070068 *pos++ = '=';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070069 } else {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070070 *pos++ = table[(((in[0] & 0x03) << 4) |
71 (in[1] >> 4)) & 0x3f];
72 *pos++ = table[((in[1] & 0x0f) << 2) & 0x3f];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070073 }
Hai Shalom899fcc72020-10-19 14:38:18 -070074 if (add_pad & BASE64_PAD)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070075 *pos++ = '=';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070076 line_len += 4;
77 }
78
Hai Shalom899fcc72020-10-19 14:38:18 -070079 if ((add_pad & BASE64_LF) && line_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070080 *pos++ = '\n';
81
82 *pos = '\0';
83 if (out_len)
84 *out_len = pos - out;
85 return out;
86}
87
88
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -080089static unsigned char * base64_gen_decode(const char *src, size_t len,
90 size_t *out_len, const char *table)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070091{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080092 unsigned char dtable[256], *out, *pos, block[4], tmp;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070093 size_t i, count, olen;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080094 int pad = 0;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070095 size_t extra_pad;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070096
97 os_memset(dtable, 0x80, 256);
98 for (i = 0; i < sizeof(base64_table) - 1; i++)
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -080099 dtable[(unsigned char) table[i]] = (unsigned char) i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700100 dtable['='] = 0;
101
102 count = 0;
103 for (i = 0; i < len; i++) {
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800104 if (dtable[(unsigned char) src[i]] != 0x80)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700105 count++;
106 }
107
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700108 if (count == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700109 return NULL;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700110 extra_pad = (4 - count % 4) % 4;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700111
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700112 olen = (count + extra_pad) / 4 * 3;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700113 pos = out = os_malloc(olen);
114 if (out == NULL)
115 return NULL;
116
117 count = 0;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700118 for (i = 0; i < len + extra_pad; i++) {
119 unsigned char val;
120
121 if (i >= len)
122 val = '=';
123 else
124 val = src[i];
125 tmp = dtable[val];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700126 if (tmp == 0x80)
127 continue;
128
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700129 if (val == '=')
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800130 pad++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700131 block[count] = tmp;
132 count++;
133 if (count == 4) {
134 *pos++ = (block[0] << 2) | (block[1] >> 4);
135 *pos++ = (block[1] << 4) | (block[2] >> 2);
136 *pos++ = (block[2] << 6) | block[3];
137 count = 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800138 if (pad) {
139 if (pad == 1)
140 pos--;
141 else if (pad == 2)
142 pos -= 2;
143 else {
144 /* Invalid padding */
145 os_free(out);
146 return NULL;
147 }
148 break;
149 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700150 }
151 }
152
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700153 *out_len = pos - out;
154 return out;
155}
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700156
157
158/**
159 * base64_encode - Base64 encode
160 * @src: Data to be encoded
161 * @len: Length of the data to be encoded
162 * @out_len: Pointer to output length variable, or %NULL if not used
163 * Returns: Allocated buffer of out_len bytes of encoded data,
164 * or %NULL on failure
165 *
166 * Caller is responsible for freeing the returned buffer. Returned buffer is
167 * nul terminated to make it easier to use as a C string. The nul terminator is
168 * not included in out_len.
169 */
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800170char * base64_encode(const void *src, size_t len, size_t *out_len)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700171{
Hai Shalom899fcc72020-10-19 14:38:18 -0700172 return base64_gen_encode(src, len, out_len, base64_table,
173 BASE64_PAD | BASE64_LF);
174}
175
176
177char * base64_encode_no_lf(const void *src, size_t len, size_t *out_len)
178{
179 return base64_gen_encode(src, len, out_len, base64_table, BASE64_PAD);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700180}
181
182
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800183char * base64_url_encode(const void *src, size_t len, size_t *out_len)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700184{
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800185 return base64_gen_encode(src, len, out_len, base64_url_table, 0);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700186}
187
188
189/**
190 * base64_decode - Base64 decode
191 * @src: Data to be decoded
192 * @len: Length of the data to be decoded
193 * @out_len: Pointer to output length variable
194 * Returns: Allocated buffer of out_len bytes of decoded data,
195 * or %NULL on failure
196 *
197 * Caller is responsible for freeing the returned buffer.
198 */
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800199unsigned char * base64_decode(const char *src, size_t len, size_t *out_len)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700200{
201 return base64_gen_decode(src, len, out_len, base64_table);
202}
203
204
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800205unsigned char * base64_url_decode(const char *src, size_t len, size_t *out_len)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700206{
207 return base64_gen_decode(src, len, out_len, base64_url_table);
208}