2013-02-01 03:07:04 +04:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <winpr/crt.h>
|
|
|
|
|
|
|
|
#include <freerdp/crypto/crypto.h>
|
|
|
|
|
|
|
|
static const char base64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
|
|
|
|
2021-04-13 12:01:43 +03:00
|
|
|
char* crypto_base64_encode(const BYTE* data, size_t length)
|
2013-02-01 03:07:04 +04:00
|
|
|
{
|
|
|
|
int c;
|
2014-05-10 00:36:50 +04:00
|
|
|
const BYTE* q;
|
2013-02-01 03:07:04 +04:00
|
|
|
char* p;
|
2014-05-10 00:36:50 +04:00
|
|
|
char* ret;
|
2013-02-01 03:07:04 +04:00
|
|
|
int i = 0;
|
2014-05-12 00:42:01 +04:00
|
|
|
int blocks;
|
2013-02-01 03:07:04 +04:00
|
|
|
|
|
|
|
q = data;
|
2019-11-06 17:24:51 +03:00
|
|
|
p = ret = (char*)malloc((length + 3) * 4 / 3 + 1);
|
2014-05-10 00:36:50 +04:00
|
|
|
if (!p)
|
|
|
|
return NULL;
|
2013-02-01 03:07:04 +04:00
|
|
|
|
2014-05-12 00:42:01 +04:00
|
|
|
/* 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)
|
2013-02-01 03:07:04 +04:00
|
|
|
{
|
2014-05-12 00:42:01 +04:00
|
|
|
c = (q[0] << 16) + (q[1] << 8) + q[2];
|
2013-02-01 03:07:04 +04:00
|
|
|
|
|
|
|
*p++ = base64[(c & 0x00FC0000) >> 18];
|
|
|
|
*p++ = base64[(c & 0x0003F000) >> 12];
|
2014-05-12 00:42:01 +04:00
|
|
|
*p++ = base64[(c & 0x00000FC0) >> 6];
|
|
|
|
*p++ = base64[c & 0x0000003F];
|
|
|
|
}
|
2013-02-01 03:07:04 +04:00
|
|
|
|
2014-05-12 00:42:01 +04:00
|
|
|
/* then remainder */
|
|
|
|
switch (length % 3)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
case 0:
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
c = (q[0] << 16);
|
|
|
|
*p++ = base64[(c & 0x00FC0000) >> 18];
|
|
|
|
*p++ = base64[(c & 0x0003F000) >> 12];
|
|
|
|
*p++ = '=';
|
|
|
|
*p++ = '=';
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
c = (q[0] << 16) + (q[1] << 8);
|
|
|
|
*p++ = base64[(c & 0x00FC0000) >> 18];
|
|
|
|
*p++ = base64[(c & 0x0003F000) >> 12];
|
|
|
|
*p++ = base64[(c & 0x00000FC0) >> 6];
|
|
|
|
*p++ = '=';
|
|
|
|
break;
|
2013-02-01 03:07:04 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
*p = 0;
|
|
|
|
|
2014-05-10 00:36:50 +04:00
|
|
|
return ret;
|
2013-02-01 03:07:04 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int base64_decode_char(char c)
|
|
|
|
{
|
|
|
|
if (c >= 'A' && c <= 'Z')
|
|
|
|
return c - 'A';
|
|
|
|
|
|
|
|
if (c >= 'a' && c <= 'z')
|
|
|
|
return c - 'a' + 26;
|
|
|
|
|
|
|
|
if (c >= '0' && c <= '9')
|
|
|
|
return c - '0' + 52;
|
|
|
|
|
|
|
|
if (c == '+')
|
|
|
|
return 62;
|
|
|
|
|
|
|
|
if (c == '/')
|
|
|
|
return 63;
|
|
|
|
|
|
|
|
if (c == '=')
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2021-04-13 12:01:43 +03:00
|
|
|
static void* base64_decode(const char* s, size_t length, size_t* data_len)
|
2013-02-01 03:07:04 +04:00
|
|
|
{
|
|
|
|
int n[4];
|
|
|
|
BYTE* q;
|
|
|
|
BYTE* data;
|
2021-04-13 12:01:43 +03:00
|
|
|
size_t nBlocks, i, outputLen;
|
2013-02-01 03:07:04 +04:00
|
|
|
|
|
|
|
if (length % 4)
|
|
|
|
return NULL;
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
q = data = (BYTE*)malloc(length / 4 * 3 + 1);
|
2015-06-16 16:42:07 +03:00
|
|
|
if (!q)
|
|
|
|
return NULL;
|
2013-02-01 03:07:04 +04:00
|
|
|
|
2014-05-12 00:42:01 +04:00
|
|
|
/* first treat complete blocks */
|
|
|
|
nBlocks = (length / 4);
|
|
|
|
outputLen = 0;
|
|
|
|
|
2021-04-13 12:01:43 +03:00
|
|
|
if (nBlocks < 1)
|
|
|
|
{
|
|
|
|
free(data);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
for (i = 0; i < nBlocks - 1; i++, q += 3)
|
2013-02-01 03:07:04 +04:00
|
|
|
{
|
2014-05-12 00:42:01 +04:00
|
|
|
n[0] = base64_decode_char(*s++);
|
|
|
|
n[1] = base64_decode_char(*s++);
|
|
|
|
n[2] = base64_decode_char(*s++);
|
|
|
|
n[3] = base64_decode_char(*s++);
|
|
|
|
|
|
|
|
if ((n[0] == -1) || (n[1] == -1) || (n[2] == -1) || (n[3] == -1))
|
|
|
|
goto out_free;
|
2013-02-01 03:07:04 +04:00
|
|
|
|
|
|
|
q[0] = (n[0] << 2) + (n[1] >> 4);
|
2014-05-12 00:42:01 +04:00
|
|
|
q[1] = ((n[1] & 15) << 4) + (n[2] >> 2);
|
|
|
|
q[2] = ((n[2] & 3) << 6) + n[3];
|
|
|
|
outputLen += 3;
|
|
|
|
}
|
2013-02-01 03:07:04 +04:00
|
|
|
|
2014-05-12 00:42:01 +04:00
|
|
|
/* treat last block */
|
|
|
|
n[0] = base64_decode_char(*s++);
|
|
|
|
n[1] = base64_decode_char(*s++);
|
|
|
|
if ((n[0] == -1) || (n[1] == -1))
|
|
|
|
goto out_free;
|
2013-02-01 03:07:04 +04:00
|
|
|
|
2014-05-12 00:42:01 +04:00
|
|
|
n[2] = base64_decode_char(*s++);
|
|
|
|
n[3] = base64_decode_char(*s++);
|
|
|
|
|
|
|
|
q[0] = (n[0] << 2) + (n[1] >> 4);
|
|
|
|
if (n[2] == -1)
|
|
|
|
{
|
|
|
|
/* XX== */
|
|
|
|
outputLen += 1;
|
2013-02-01 03:07:04 +04:00
|
|
|
if (n[3] != -1)
|
2014-05-12 00:42:01 +04:00
|
|
|
goto out_free;
|
2013-02-01 03:07:04 +04:00
|
|
|
|
2014-05-12 00:42:01 +04:00
|
|
|
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];
|
2013-02-01 03:07:04 +04:00
|
|
|
}
|
|
|
|
|
2021-07-29 11:07:04 +03:00
|
|
|
if (data_len)
|
|
|
|
*data_len = outputLen;
|
2015-08-27 10:27:23 +03:00
|
|
|
data[outputLen] = '\0';
|
2013-02-01 03:07:04 +04:00
|
|
|
|
|
|
|
return data;
|
2014-05-12 00:42:01 +04:00
|
|
|
out_free:
|
|
|
|
free(data);
|
|
|
|
return NULL;
|
2013-02-01 03:07:04 +04:00
|
|
|
}
|
|
|
|
|
2021-04-13 12:01:43 +03:00
|
|
|
void crypto_base64_decode(const char* enc_data, size_t length, BYTE** dec_data, size_t* res_length)
|
2013-02-01 03:07:04 +04:00
|
|
|
{
|
|
|
|
*dec_data = base64_decode(enc_data, length, res_length);
|
|
|
|
}
|