FreeRDP/libfreerdp/crypto/base64.c
fifthdegree 304ce6d702 Test base64url en/decoding
Add tests for base64url and fix a bug discovered while doing that
2023-03-10 16:38:07 +01:00

220 lines
4.9 KiB
C

/**
* FreeRDP: A Remote Desktop Protocol Implementation
* Base64 Encoding & Decoding
*
* Copyright 2011-2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <freerdp/config.h>
#include <winpr/crt.h>
#include <freerdp/crypto/crypto.h>
static const char base64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
static const char base64url[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
static char* base64_encode(const char* alphabet, const BYTE* data, size_t length, BOOL pad)
{
int c;
const BYTE* q;
char* p;
char* ret;
int i = 0;
int blocks;
q = data;
p = ret = (char*)malloc((length + 3) * 4 / 3 + 1);
if (!p)
return NULL;
/* b1, b2, b3 are input bytes
*
* 0 1 2
* 012345678901234567890123
* | b1 | b2 | b3 |
*
* [ c1 ] [ c3 ]
* [ c2 ] [ c4 ]
*
* c1, c2, c3, c4 are output chars in base64
*/
/* first treat complete blocks */
blocks = length - (length % 3);
for (i = 0; i < blocks; i += 3, q += 3)
{
c = (q[0] << 16) + (q[1] << 8) + q[2];
*p++ = alphabet[(c & 0x00FC0000) >> 18];
*p++ = alphabet[(c & 0x0003F000) >> 12];
*p++ = alphabet[(c & 0x00000FC0) >> 6];
*p++ = alphabet[c & 0x0000003F];
}
/* then remainder */
switch (length % 3)
{
case 0:
break;
case 1:
c = (q[0] << 16);
*p++ = alphabet[(c & 0x00FC0000) >> 18];
*p++ = alphabet[(c & 0x0003F000) >> 12];
if (pad)
{
*p++ = '=';
*p++ = '=';
}
break;
case 2:
c = (q[0] << 16) + (q[1] << 8);
*p++ = alphabet[(c & 0x00FC0000) >> 18];
*p++ = alphabet[(c & 0x0003F000) >> 12];
*p++ = alphabet[(c & 0x00000FC0) >> 6];
if (pad)
*p++ = '=';
break;
}
*p = 0;
return ret;
}
static int base64_decode_char(const char* alphabet, char c)
{
char* p = NULL;
if (c == '\0')
return -1;
if ((p = strchr(alphabet, c)))
return p - alphabet;
return -1;
}
static void* base64_decode(const char* alphabet, const char* s, size_t length, size_t* data_len,
BOOL pad)
{
int n[4];
BYTE* q;
BYTE* data;
size_t nBlocks, i, outputLen;
int remainder = length % 4;
if ((pad && remainder > 0) || (remainder == 1))
return NULL;
if (!pad && remainder)
length += 4 - remainder;
q = data = (BYTE*)malloc(length / 4 * 3 + 1);
if (!q)
return NULL;
/* first treat complete blocks */
nBlocks = (length / 4);
outputLen = 0;
if (nBlocks < 1)
{
free(data);
return NULL;
}
for (i = 0; i < nBlocks - 1; i++, q += 3)
{
n[0] = base64_decode_char(alphabet, *s++);
n[1] = base64_decode_char(alphabet, *s++);
n[2] = base64_decode_char(alphabet, *s++);
n[3] = base64_decode_char(alphabet, *s++);
if ((n[0] == -1) || (n[1] == -1) || (n[2] == -1) || (n[3] == -1))
goto out_free;
q[0] = (n[0] << 2) + (n[1] >> 4);
q[1] = ((n[1] & 15) << 4) + (n[2] >> 2);
q[2] = ((n[2] & 3) << 6) + n[3];
outputLen += 3;
}
/* treat last block */
n[0] = base64_decode_char(alphabet, *s++);
n[1] = base64_decode_char(alphabet, *s++);
if ((n[0] == -1) || (n[1] == -1))
goto out_free;
n[2] = remainder == 2 ? -1 : base64_decode_char(alphabet, *s++);
n[3] = remainder >= 2 ? -1 : base64_decode_char(alphabet, *s++);
q[0] = (n[0] << 2) + (n[1] >> 4);
if (n[2] == -1)
{
/* XX== */
outputLen += 1;
if (n[3] != -1)
goto out_free;
q[1] = ((n[1] & 15) << 4);
}
else if (n[3] == -1)
{
/* yyy= */
outputLen += 2;
q[1] = ((n[1] & 15) << 4) + (n[2] >> 2);
q[2] = ((n[2] & 3) << 6);
}
else
{
/* XXXX */
outputLen += 3;
q[0] = (n[0] << 2) + (n[1] >> 4);
q[1] = ((n[1] & 15) << 4) + (n[2] >> 2);
q[2] = ((n[2] & 3) << 6) + n[3];
}
if (data_len)
*data_len = outputLen;
data[outputLen] = '\0';
return data;
out_free:
free(data);
return NULL;
}
char* crypto_base64_encode(const BYTE* data, size_t length)
{
return base64_encode(base64, data, length, TRUE);
}
void crypto_base64_decode(const char* enc_data, size_t length, BYTE** dec_data, size_t* res_length)
{
*dec_data = base64_decode(base64, enc_data, length, res_length, TRUE);
}
char* crypto_base64url_encode(const BYTE* data, size_t length)
{
return base64_encode(base64url, data, length, FALSE);
}
void crypto_base64url_decode(const char* enc_data, size_t length, BYTE** dec_data,
size_t* res_length)
{
*dec_data = base64_decode(base64url, enc_data, length, res_length, FALSE);
}