blob: a17d2d36dc2f0c5f3346ac161f72aab4aa726da0 [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
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -080015static const char base64_table[65] =
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070016 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -080017static const char base64_url_table[65] =
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070018 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070019
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070020
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -080021static char * base64_gen_encode(const unsigned char *src, size_t len,
22 size_t *out_len, const char *table, int add_pad)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070023{
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -080024 char *out, *pos;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070025 const unsigned char *end, *in;
26 size_t olen;
27 int line_len;
28
Hai Shalom021b0b52019-04-10 11:17:58 -070029 if (len >= SIZE_MAX / 4)
30 return NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070031 olen = len * 4 / 3 + 4; /* 3-byte blocks to 4-byte */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070032 if (add_pad)
33 olen += olen / 72; /* line feeds */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070034 olen++; /* nul termination */
35 if (olen < len)
36 return NULL; /* integer overflow */
37 out = os_malloc(olen);
38 if (out == NULL)
39 return NULL;
40
41 end = src + len;
42 in = src;
43 pos = out;
44 line_len = 0;
45 while (end - in >= 3) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070046 *pos++ = table[(in[0] >> 2) & 0x3f];
47 *pos++ = table[(((in[0] & 0x03) << 4) | (in[1] >> 4)) & 0x3f];
48 *pos++ = table[(((in[1] & 0x0f) << 2) | (in[2] >> 6)) & 0x3f];
49 *pos++ = table[in[2] & 0x3f];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070050 in += 3;
51 line_len += 4;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070052 if (add_pad && line_len >= 72) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070053 *pos++ = '\n';
54 line_len = 0;
55 }
56 }
57
58 if (end - in) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070059 *pos++ = table[(in[0] >> 2) & 0x3f];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070060 if (end - in == 1) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070061 *pos++ = table[((in[0] & 0x03) << 4) & 0x3f];
62 if (add_pad)
63 *pos++ = '=';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070064 } else {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070065 *pos++ = table[(((in[0] & 0x03) << 4) |
66 (in[1] >> 4)) & 0x3f];
67 *pos++ = table[((in[1] & 0x0f) << 2) & 0x3f];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070068 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070069 if (add_pad)
70 *pos++ = '=';
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070071 line_len += 4;
72 }
73
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070074 if (add_pad && line_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070075 *pos++ = '\n';
76
77 *pos = '\0';
78 if (out_len)
79 *out_len = pos - out;
80 return out;
81}
82
83
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -080084static unsigned char * base64_gen_decode(const char *src, size_t len,
85 size_t *out_len, const 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++)
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -080094 dtable[(unsigned char) 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++) {
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -080099 if (dtable[(unsigned char) src[i]] != 0x80)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700100 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 */
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800165char * base64_encode(const void *src, size_t len, size_t *out_len)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700166{
167 return base64_gen_encode(src, len, out_len, base64_table, 1);
168}
169
170
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800171char * base64_url_encode(const void *src, size_t len, size_t *out_len)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700172{
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800173 return base64_gen_encode(src, len, out_len, base64_url_table, 0);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700174}
175
176
177/**
178 * base64_decode - Base64 decode
179 * @src: Data to be decoded
180 * @len: Length of the data to be decoded
181 * @out_len: Pointer to output length variable
182 * Returns: Allocated buffer of out_len bytes of decoded data,
183 * or %NULL on failure
184 *
185 * Caller is responsible for freeing the returned buffer.
186 */
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800187unsigned char * base64_decode(const char *src, size_t len, size_t *out_len)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700188{
189 return base64_gen_decode(src, len, out_len, base64_table);
190}
191
192
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800193unsigned char * base64_url_decode(const char *src, size_t len, size_t *out_len)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700194{
195 return base64_gen_decode(src, len, out_len, base64_url_table);
196}