2006-05-31 09:31:10 +02:00
|
|
|
/*
|
|
|
|
* base64 encoder/decoder
|
|
|
|
*
|
|
|
|
* Copyright 2005 by Kai Blin
|
|
|
|
* Copyright 2006 Juan Lang
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
2008-12-12 01:49:16 +01:00
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2006-05-31 09:31:10 +02:00
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include "windef.h"
|
|
|
|
#include "winbase.h"
|
|
|
|
#include "winerror.h"
|
|
|
|
#include "wincrypt.h"
|
|
|
|
#include "wine/debug.h"
|
|
|
|
|
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(crypt);
|
|
|
|
|
|
|
|
#define CERT_HEADER "-----BEGIN CERTIFICATE-----"
|
2017-02-14 15:59:33 +01:00
|
|
|
#define CERT_HEADER_START "-----BEGIN "
|
2011-04-23 17:18:55 +02:00
|
|
|
#define CERT_DELIMITER "-----"
|
2006-05-31 09:31:10 +02:00
|
|
|
#define CERT_TRAILER "-----END CERTIFICATE-----"
|
2017-02-14 15:59:33 +01:00
|
|
|
#define CERT_TRAILER_START "-----END "
|
2006-05-31 09:31:10 +02:00
|
|
|
#define CERT_REQUEST_HEADER "-----BEGIN NEW CERTIFICATE REQUEST-----"
|
|
|
|
#define CERT_REQUEST_TRAILER "-----END NEW CERTIFICATE REQUEST-----"
|
|
|
|
#define X509_HEADER "-----BEGIN X509 CRL-----"
|
|
|
|
#define X509_TRAILER "-----END X509 CRL-----"
|
|
|
|
|
2020-11-09 11:41:52 +01:00
|
|
|
static const WCHAR CERT_HEADER_W[] = L"-----BEGIN CERTIFICATE-----";
|
|
|
|
static const WCHAR CERT_HEADER_START_W[] = L"-----BEGIN ";
|
|
|
|
static const WCHAR CERT_DELIMITER_W[] = L"-----";
|
|
|
|
static const WCHAR CERT_TRAILER_W[] = L"-----END CERTIFICATE-----";
|
|
|
|
static const WCHAR CERT_TRAILER_START_W[] = L"-----END ";
|
|
|
|
static const WCHAR CERT_REQUEST_HEADER_W[] = L"-----BEGIN NEW CERTIFICATE REQUEST-----";
|
|
|
|
static const WCHAR CERT_REQUEST_TRAILER_W[] = L"-----END NEW CERTIFICATE REQUEST-----";
|
|
|
|
static const WCHAR X509_HEADER_W[] = L"-----BEGIN X509 CRL-----";
|
|
|
|
static const WCHAR X509_TRAILER_W[] = L"-----END X509 CRL-----";
|
2008-12-12 01:49:16 +01:00
|
|
|
|
2006-05-31 09:31:10 +02:00
|
|
|
static const char b64[] =
|
|
|
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
|
|
|
|
|
|
|
typedef BOOL (*BinaryToStringAFunc)(const BYTE *pbBinary,
|
|
|
|
DWORD cbBinary, DWORD dwFlags, LPSTR pszString, DWORD *pcchString);
|
2009-10-29 23:13:29 +01:00
|
|
|
typedef BOOL (*BinaryToStringWFunc)(const BYTE *pbBinary,
|
|
|
|
DWORD cbBinary, DWORD dwFlags, LPWSTR pszString, DWORD *pcchString);
|
2006-05-31 09:31:10 +02:00
|
|
|
|
|
|
|
static BOOL EncodeBinaryToBinaryA(const BYTE *pbBinary,
|
|
|
|
DWORD cbBinary, DWORD dwFlags, LPSTR pszString, DWORD *pcchString)
|
|
|
|
{
|
|
|
|
BOOL ret = TRUE;
|
|
|
|
|
2018-09-26 12:09:53 +02:00
|
|
|
if (pszString)
|
2006-05-31 09:31:10 +02:00
|
|
|
{
|
2018-09-26 12:09:53 +02:00
|
|
|
if (*pcchString < cbBinary)
|
2006-05-31 09:31:10 +02:00
|
|
|
{
|
|
|
|
SetLastError(ERROR_INSUFFICIENT_BUFFER);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
2018-09-26 12:09:53 +02:00
|
|
|
else if (cbBinary)
|
2006-05-31 09:31:10 +02:00
|
|
|
memcpy(pszString, pbBinary, cbBinary);
|
|
|
|
}
|
2018-09-26 12:09:53 +02:00
|
|
|
else
|
2018-09-27 16:27:56 +02:00
|
|
|
*pcchString = cbBinary;
|
2018-09-26 12:09:53 +02:00
|
|
|
|
2006-05-31 09:31:10 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-10-09 19:53:55 +02:00
|
|
|
static DWORD stradd(LPSTR ptr, LPCSTR end, LPCSTR s, DWORD slen)
|
|
|
|
{
|
|
|
|
if (ptr + slen > end)
|
|
|
|
slen = end - ptr;
|
|
|
|
memcpy(ptr, s, slen);
|
|
|
|
return slen;
|
|
|
|
}
|
|
|
|
|
|
|
|
static DWORD encodeBase64A(const BYTE *in_buf, int in_len, LPCSTR sep,
|
2006-05-31 09:31:10 +02:00
|
|
|
char* out_buf, DWORD *out_len)
|
|
|
|
{
|
|
|
|
int div, i;
|
|
|
|
const BYTE *d = in_buf;
|
|
|
|
int bytes = (in_len*8 + 5)/6, pad_bytes = (bytes % 4) ? 4 - (bytes % 4) : 0;
|
|
|
|
LPSTR ptr;
|
2019-10-09 19:53:55 +02:00
|
|
|
LPCSTR end;
|
|
|
|
char chunk[4];
|
2006-05-31 09:31:10 +02:00
|
|
|
|
2019-10-09 19:53:55 +02:00
|
|
|
if (!out_buf)
|
2006-05-31 09:31:10 +02:00
|
|
|
{
|
2019-10-09 19:53:55 +02:00
|
|
|
TRACE("bytes is %d, pad bytes is %d\n", bytes, pad_bytes);
|
|
|
|
*out_len = bytes + pad_bytes;
|
|
|
|
*out_len += (*out_len / 64 + (*out_len % 64 ? 1 : 0)) * strlen(sep) + 1;
|
|
|
|
return 0;
|
2006-05-31 09:31:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Three bytes of input give 4 chars of output */
|
|
|
|
div = in_len / 3;
|
|
|
|
|
|
|
|
ptr = out_buf;
|
2019-10-09 19:53:55 +02:00
|
|
|
end = ptr + *out_len;
|
2006-05-31 09:31:10 +02:00
|
|
|
i = 0;
|
2019-10-09 19:53:55 +02:00
|
|
|
while (div > 0 && ptr < end)
|
2006-05-31 09:31:10 +02:00
|
|
|
{
|
2010-03-24 19:51:23 +01:00
|
|
|
if (i && i % 64 == 0)
|
2019-10-09 19:53:55 +02:00
|
|
|
ptr += stradd(ptr, end, sep, strlen(sep));
|
2006-05-31 09:31:10 +02:00
|
|
|
/* first char is the first 6 bits of the first byte*/
|
2019-10-09 19:53:55 +02:00
|
|
|
chunk[0] = b64[ ( d[0] >> 2) & 0x3f ];
|
2006-05-31 09:31:10 +02:00
|
|
|
/* second char is the last 2 bits of the first byte and the first 4
|
|
|
|
* bits of the second byte */
|
2019-10-09 19:53:55 +02:00
|
|
|
chunk[1] = b64[ ((d[0] << 4) & 0x30) | (d[1] >> 4 & 0x0f)];
|
2006-05-31 09:31:10 +02:00
|
|
|
/* third char is the last 4 bits of the second byte and the first 2
|
|
|
|
* bits of the third byte */
|
2019-10-09 19:53:55 +02:00
|
|
|
chunk[2] = b64[ ((d[1] << 2) & 0x3c) | (d[2] >> 6 & 0x03)];
|
2006-05-31 09:31:10 +02:00
|
|
|
/* fourth char is the remaining 6 bits of the third byte */
|
2019-10-09 19:53:55 +02:00
|
|
|
chunk[3] = b64[ d[2] & 0x3f];
|
|
|
|
ptr += stradd(ptr, end, chunk, 4);
|
2006-05-31 09:31:10 +02:00
|
|
|
i += 4;
|
|
|
|
d += 3;
|
|
|
|
div--;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch(pad_bytes)
|
|
|
|
{
|
|
|
|
case 1:
|
|
|
|
/* first char is the first 6 bits of the first byte*/
|
2019-10-09 19:53:55 +02:00
|
|
|
chunk[0] = b64[ ( d[0] >> 2) & 0x3f ];
|
2006-05-31 09:31:10 +02:00
|
|
|
/* second char is the last 2 bits of the first byte and the first 4
|
|
|
|
* bits of the second byte */
|
2019-10-09 19:53:55 +02:00
|
|
|
chunk[1] = b64[ ((d[0] << 4) & 0x30) | (d[1] >> 4 & 0x0f)];
|
2006-05-31 09:31:10 +02:00
|
|
|
/* third char is the last 4 bits of the second byte padded with
|
|
|
|
* two zeroes */
|
2019-10-09 19:53:55 +02:00
|
|
|
chunk[2] = b64[ ((d[1] << 2) & 0x3c) ];
|
2006-05-31 09:31:10 +02:00
|
|
|
/* fourth char is a = to indicate one byte of padding */
|
2019-10-09 19:53:55 +02:00
|
|
|
chunk[3] = '=';
|
|
|
|
ptr += stradd(ptr, end, chunk, 4);
|
2006-05-31 09:31:10 +02:00
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
/* first char is the first 6 bits of the first byte*/
|
2019-10-09 19:53:55 +02:00
|
|
|
chunk[0] = b64[ ( d[0] >> 2) & 0x3f ];
|
2006-05-31 09:31:10 +02:00
|
|
|
/* second char is the last 2 bits of the first byte padded with
|
|
|
|
* four zeroes*/
|
2019-10-09 19:53:55 +02:00
|
|
|
chunk[1] = b64[ ((d[0] << 4) & 0x30)];
|
2006-05-31 09:31:10 +02:00
|
|
|
/* third char is = to indicate padding */
|
2019-10-09 19:53:55 +02:00
|
|
|
chunk[2] = '=';
|
2006-05-31 09:31:10 +02:00
|
|
|
/* fourth char is = to indicate padding */
|
2019-10-09 19:53:55 +02:00
|
|
|
chunk[3] = '=';
|
|
|
|
ptr += stradd(ptr, end, chunk, 4);
|
2006-05-31 09:31:10 +02:00
|
|
|
break;
|
|
|
|
}
|
2019-10-09 19:53:55 +02:00
|
|
|
ptr += stradd(ptr, end, sep, strlen(sep));
|
2006-05-31 09:31:10 +02:00
|
|
|
|
2019-10-09 19:53:55 +02:00
|
|
|
return ptr - out_buf;
|
2006-05-31 09:31:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL BinaryToBase64A(const BYTE *pbBinary,
|
|
|
|
DWORD cbBinary, DWORD dwFlags, LPSTR pszString, DWORD *pcchString)
|
|
|
|
{
|
|
|
|
static const char crlf[] = "\r\n", lf[] = "\n";
|
|
|
|
BOOL ret = TRUE;
|
2009-10-29 23:43:37 +01:00
|
|
|
LPCSTR header = NULL, trailer = NULL, sep;
|
2006-05-31 09:31:10 +02:00
|
|
|
DWORD charsNeeded;
|
|
|
|
|
|
|
|
if (dwFlags & CRYPT_STRING_NOCR)
|
|
|
|
sep = lf;
|
2009-10-29 23:43:37 +01:00
|
|
|
else if (dwFlags & CRYPT_STRING_NOCRLF)
|
2010-03-24 19:51:23 +01:00
|
|
|
sep = "";
|
2006-05-31 09:31:10 +02:00
|
|
|
else
|
|
|
|
sep = crlf;
|
2009-10-29 23:43:37 +01:00
|
|
|
switch (dwFlags & 0x0fffffff)
|
2006-05-31 09:31:10 +02:00
|
|
|
{
|
|
|
|
case CRYPT_STRING_BASE64:
|
|
|
|
/* no header or footer */
|
|
|
|
break;
|
|
|
|
case CRYPT_STRING_BASE64HEADER:
|
|
|
|
header = CERT_HEADER;
|
|
|
|
trailer = CERT_TRAILER;
|
|
|
|
break;
|
|
|
|
case CRYPT_STRING_BASE64REQUESTHEADER:
|
|
|
|
header = CERT_REQUEST_HEADER;
|
|
|
|
trailer = CERT_REQUEST_TRAILER;
|
|
|
|
break;
|
|
|
|
case CRYPT_STRING_BASE64X509CRLHEADER:
|
|
|
|
header = X509_HEADER;
|
|
|
|
trailer = X509_TRAILER;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
charsNeeded = 0;
|
|
|
|
encodeBase64A(pbBinary, cbBinary, sep, NULL, &charsNeeded);
|
|
|
|
if (header)
|
|
|
|
charsNeeded += strlen(header) + strlen(sep);
|
|
|
|
if (trailer)
|
|
|
|
charsNeeded += strlen(trailer) + strlen(sep);
|
|
|
|
|
2018-09-26 12:09:52 +02:00
|
|
|
if (pszString)
|
|
|
|
{
|
2019-10-09 19:53:55 +02:00
|
|
|
LPSTR ptr = pszString;
|
|
|
|
DWORD size = *pcchString;
|
|
|
|
LPSTR end = ptr + size;
|
|
|
|
|
|
|
|
if (header)
|
|
|
|
{
|
|
|
|
ptr += stradd(ptr, end, header, strlen(header));
|
|
|
|
ptr += stradd(ptr, end, sep, strlen(sep));
|
|
|
|
size = end - ptr;
|
|
|
|
}
|
|
|
|
ptr += encodeBase64A(pbBinary, cbBinary, sep, ptr, &size);
|
|
|
|
if (trailer)
|
2006-05-31 09:31:10 +02:00
|
|
|
{
|
2019-10-09 19:53:55 +02:00
|
|
|
ptr += stradd(ptr, end, trailer, strlen(trailer));
|
|
|
|
ptr += stradd(ptr, end, sep, strlen(sep));
|
|
|
|
}
|
2018-09-26 12:09:52 +02:00
|
|
|
|
2019-10-09 19:53:55 +02:00
|
|
|
if (ptr < end)
|
|
|
|
*ptr = '\0';
|
|
|
|
|
|
|
|
if (charsNeeded <= *pcchString)
|
|
|
|
{
|
2018-09-26 12:09:52 +02:00
|
|
|
*pcchString = charsNeeded - 1;
|
2006-05-31 09:31:10 +02:00
|
|
|
}
|
2018-09-26 12:09:52 +02:00
|
|
|
else
|
2006-05-31 09:31:10 +02:00
|
|
|
{
|
2018-09-26 12:09:52 +02:00
|
|
|
*pcchString = charsNeeded;
|
2019-10-09 19:53:54 +02:00
|
|
|
SetLastError(ERROR_MORE_DATA);
|
2018-09-26 12:09:52 +02:00
|
|
|
ret = FALSE;
|
2006-05-31 09:31:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
*pcchString = charsNeeded;
|
2018-09-26 12:09:52 +02:00
|
|
|
|
2006-05-31 09:31:10 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL WINAPI CryptBinaryToStringA(const BYTE *pbBinary,
|
|
|
|
DWORD cbBinary, DWORD dwFlags, LPSTR pszString, DWORD *pcchString)
|
|
|
|
{
|
|
|
|
BinaryToStringAFunc encoder = NULL;
|
|
|
|
|
2006-10-04 06:58:09 +02:00
|
|
|
TRACE("(%p, %d, %08x, %p, %p)\n", pbBinary, cbBinary, dwFlags, pszString,
|
2006-05-31 09:31:10 +02:00
|
|
|
pcchString);
|
|
|
|
|
|
|
|
if (!pbBinary)
|
|
|
|
{
|
|
|
|
SetLastError(ERROR_INVALID_PARAMETER);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
if (!pcchString)
|
|
|
|
{
|
|
|
|
SetLastError(ERROR_INVALID_PARAMETER);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2009-10-29 23:43:37 +01:00
|
|
|
switch (dwFlags & 0x0fffffff)
|
2006-05-31 09:31:10 +02:00
|
|
|
{
|
|
|
|
case CRYPT_STRING_BINARY:
|
|
|
|
encoder = EncodeBinaryToBinaryA;
|
|
|
|
break;
|
|
|
|
case CRYPT_STRING_BASE64:
|
|
|
|
case CRYPT_STRING_BASE64HEADER:
|
|
|
|
case CRYPT_STRING_BASE64REQUESTHEADER:
|
|
|
|
case CRYPT_STRING_BASE64X509CRLHEADER:
|
|
|
|
encoder = BinaryToBase64A;
|
|
|
|
break;
|
|
|
|
case CRYPT_STRING_HEX:
|
|
|
|
case CRYPT_STRING_HEXASCII:
|
|
|
|
case CRYPT_STRING_HEXADDR:
|
|
|
|
case CRYPT_STRING_HEXASCIIADDR:
|
2009-10-29 23:43:37 +01:00
|
|
|
FIXME("Unimplemented type %d\n", dwFlags & 0x0fffffff);
|
2006-05-31 09:31:10 +02:00
|
|
|
/* fall through */
|
|
|
|
default:
|
|
|
|
SetLastError(ERROR_INVALID_PARAMETER);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
return encoder(pbBinary, cbBinary, dwFlags, pszString, pcchString);
|
|
|
|
}
|
|
|
|
|
2018-09-27 16:27:56 +02:00
|
|
|
static BOOL EncodeBinaryToBinaryW(const BYTE *in_buf, DWORD in_len, DWORD flags, WCHAR *out_buf, DWORD *out_len)
|
|
|
|
{
|
|
|
|
BOOL ret = TRUE;
|
|
|
|
|
|
|
|
if (out_buf)
|
|
|
|
{
|
|
|
|
if (*out_len < in_len)
|
|
|
|
{
|
|
|
|
SetLastError(ERROR_INSUFFICIENT_BUFFER);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
else if (in_len)
|
|
|
|
memcpy(out_buf, in_buf, in_len);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
*out_len = in_len;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-10-29 23:13:29 +01:00
|
|
|
static LONG encodeBase64W(const BYTE *in_buf, int in_len, LPCWSTR sep,
|
|
|
|
WCHAR* out_buf, DWORD *out_len)
|
|
|
|
{
|
|
|
|
int div, i;
|
|
|
|
const BYTE *d = in_buf;
|
|
|
|
int bytes = (in_len*8 + 5)/6, pad_bytes = (bytes % 4) ? 4 - (bytes % 4) : 0;
|
|
|
|
DWORD needed;
|
|
|
|
LPWSTR ptr;
|
|
|
|
|
|
|
|
TRACE("bytes is %d, pad bytes is %d\n", bytes, pad_bytes);
|
2018-09-26 12:09:54 +02:00
|
|
|
needed = bytes + pad_bytes;
|
2020-11-09 11:41:21 +01:00
|
|
|
needed += (needed / 64 + (needed % 64 ? 1 : 0)) * lstrlenW(sep);
|
2018-09-26 12:09:54 +02:00
|
|
|
needed++;
|
2009-10-29 23:13:29 +01:00
|
|
|
|
|
|
|
if (needed > *out_len)
|
|
|
|
{
|
|
|
|
*out_len = needed;
|
|
|
|
return ERROR_INSUFFICIENT_BUFFER;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
*out_len = needed;
|
|
|
|
|
|
|
|
/* Three bytes of input give 4 chars of output */
|
|
|
|
div = in_len / 3;
|
|
|
|
|
|
|
|
ptr = out_buf;
|
|
|
|
i = 0;
|
|
|
|
while (div > 0)
|
|
|
|
{
|
2010-03-24 19:51:23 +01:00
|
|
|
if (i && i % 64 == 0)
|
2009-10-29 23:13:29 +01:00
|
|
|
{
|
2020-11-09 11:41:21 +01:00
|
|
|
lstrcpyW(ptr, sep);
|
|
|
|
ptr += lstrlenW(sep);
|
2009-10-29 23:13:29 +01:00
|
|
|
}
|
|
|
|
/* first char is the first 6 bits of the first byte*/
|
|
|
|
*ptr++ = b64[ ( d[0] >> 2) & 0x3f ];
|
|
|
|
/* second char is the last 2 bits of the first byte and the first 4
|
|
|
|
* bits of the second byte */
|
|
|
|
*ptr++ = b64[ ((d[0] << 4) & 0x30) | (d[1] >> 4 & 0x0f)];
|
|
|
|
/* third char is the last 4 bits of the second byte and the first 2
|
|
|
|
* bits of the third byte */
|
|
|
|
*ptr++ = b64[ ((d[1] << 2) & 0x3c) | (d[2] >> 6 & 0x03)];
|
|
|
|
/* fourth char is the remaining 6 bits of the third byte */
|
|
|
|
*ptr++ = b64[ d[2] & 0x3f];
|
|
|
|
i += 4;
|
|
|
|
d += 3;
|
|
|
|
div--;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch(pad_bytes)
|
|
|
|
{
|
|
|
|
case 1:
|
|
|
|
/* first char is the first 6 bits of the first byte*/
|
|
|
|
*ptr++ = b64[ ( d[0] >> 2) & 0x3f ];
|
|
|
|
/* second char is the last 2 bits of the first byte and the first 4
|
|
|
|
* bits of the second byte */
|
|
|
|
*ptr++ = b64[ ((d[0] << 4) & 0x30) | (d[1] >> 4 & 0x0f)];
|
|
|
|
/* third char is the last 4 bits of the second byte padded with
|
|
|
|
* two zeroes */
|
|
|
|
*ptr++ = b64[ ((d[1] << 2) & 0x3c) ];
|
|
|
|
/* fourth char is a = to indicate one byte of padding */
|
|
|
|
*ptr++ = '=';
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
/* first char is the first 6 bits of the first byte*/
|
|
|
|
*ptr++ = b64[ ( d[0] >> 2) & 0x3f ];
|
|
|
|
/* second char is the last 2 bits of the first byte padded with
|
|
|
|
* four zeroes*/
|
|
|
|
*ptr++ = b64[ ((d[0] << 4) & 0x30)];
|
|
|
|
/* third char is = to indicate padding */
|
|
|
|
*ptr++ = '=';
|
|
|
|
/* fourth char is = to indicate padding */
|
|
|
|
*ptr++ = '=';
|
|
|
|
break;
|
|
|
|
}
|
2020-11-09 11:41:21 +01:00
|
|
|
lstrcpyW(ptr, sep);
|
2009-10-29 23:13:29 +01:00
|
|
|
|
|
|
|
return ERROR_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL BinaryToBase64W(const BYTE *pbBinary,
|
|
|
|
DWORD cbBinary, DWORD dwFlags, LPWSTR pszString, DWORD *pcchString)
|
|
|
|
{
|
|
|
|
BOOL ret = TRUE;
|
2009-10-29 23:43:37 +01:00
|
|
|
LPCWSTR header = NULL, trailer = NULL, sep;
|
2009-10-29 23:13:29 +01:00
|
|
|
DWORD charsNeeded;
|
|
|
|
|
|
|
|
if (dwFlags & CRYPT_STRING_NOCR)
|
2020-11-09 11:41:52 +01:00
|
|
|
sep = L"\n";
|
2009-10-29 23:43:37 +01:00
|
|
|
else if (dwFlags & CRYPT_STRING_NOCRLF)
|
2020-11-09 11:41:52 +01:00
|
|
|
sep = L"";
|
2009-10-29 23:13:29 +01:00
|
|
|
else
|
2020-11-09 11:41:52 +01:00
|
|
|
sep = L"\r\n";
|
2009-10-29 23:43:37 +01:00
|
|
|
switch (dwFlags & 0x0fffffff)
|
2009-10-29 23:13:29 +01:00
|
|
|
{
|
|
|
|
case CRYPT_STRING_BASE64:
|
|
|
|
/* no header or footer */
|
|
|
|
break;
|
|
|
|
case CRYPT_STRING_BASE64HEADER:
|
|
|
|
header = CERT_HEADER_W;
|
|
|
|
trailer = CERT_TRAILER_W;
|
|
|
|
break;
|
|
|
|
case CRYPT_STRING_BASE64REQUESTHEADER:
|
|
|
|
header = CERT_REQUEST_HEADER_W;
|
|
|
|
trailer = CERT_REQUEST_TRAILER_W;
|
|
|
|
break;
|
|
|
|
case CRYPT_STRING_BASE64X509CRLHEADER:
|
|
|
|
header = X509_HEADER_W;
|
|
|
|
trailer = X509_TRAILER_W;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
charsNeeded = 0;
|
|
|
|
encodeBase64W(pbBinary, cbBinary, sep, NULL, &charsNeeded);
|
|
|
|
if (header)
|
2020-11-09 11:41:21 +01:00
|
|
|
charsNeeded += lstrlenW(header) + lstrlenW(sep);
|
2009-10-29 23:13:29 +01:00
|
|
|
if (trailer)
|
2020-11-09 11:41:21 +01:00
|
|
|
charsNeeded += lstrlenW(trailer) + lstrlenW(sep);
|
2009-10-29 23:13:29 +01:00
|
|
|
|
2018-09-26 12:09:52 +02:00
|
|
|
if (pszString)
|
|
|
|
{
|
|
|
|
if (charsNeeded <= *pcchString)
|
2009-10-29 23:13:29 +01:00
|
|
|
{
|
2018-09-26 12:09:52 +02:00
|
|
|
LPWSTR ptr = pszString;
|
|
|
|
DWORD size = charsNeeded;
|
|
|
|
|
|
|
|
if (header)
|
|
|
|
{
|
2020-11-09 11:41:21 +01:00
|
|
|
lstrcpyW(ptr, header);
|
|
|
|
ptr += lstrlenW(ptr);
|
|
|
|
lstrcpyW(ptr, sep);
|
|
|
|
ptr += lstrlenW(sep);
|
2018-09-26 12:09:52 +02:00
|
|
|
}
|
|
|
|
encodeBase64W(pbBinary, cbBinary, sep, ptr, &size);
|
|
|
|
ptr += size - 1;
|
|
|
|
if (trailer)
|
|
|
|
{
|
2020-11-09 11:41:21 +01:00
|
|
|
lstrcpyW(ptr, trailer);
|
|
|
|
ptr += lstrlenW(ptr);
|
|
|
|
lstrcpyW(ptr, sep);
|
2018-09-26 12:09:52 +02:00
|
|
|
}
|
|
|
|
*pcchString = charsNeeded - 1;
|
2009-10-29 23:13:29 +01:00
|
|
|
}
|
2018-09-26 12:09:52 +02:00
|
|
|
else
|
2009-10-29 23:13:29 +01:00
|
|
|
{
|
2018-09-26 12:09:52 +02:00
|
|
|
*pcchString = charsNeeded;
|
2019-10-09 19:53:54 +02:00
|
|
|
SetLastError(ERROR_MORE_DATA);
|
2018-09-26 12:09:52 +02:00
|
|
|
ret = FALSE;
|
2009-10-29 23:13:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
*pcchString = charsNeeded;
|
2018-09-26 12:09:52 +02:00
|
|
|
|
2009-10-29 23:13:29 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-12-03 07:57:21 +01:00
|
|
|
static BOOL BinaryToHexRawW(const BYTE *bin, DWORD nbin, DWORD flags, LPWSTR str, DWORD *nstr)
|
2020-01-24 16:05:08 +01:00
|
|
|
{
|
2020-11-09 11:41:52 +01:00
|
|
|
static const WCHAR hex[] = L"0123456789abcdef";
|
2020-01-24 16:05:08 +01:00
|
|
|
DWORD needed;
|
|
|
|
|
|
|
|
if (flags & CRYPT_STRING_NOCRLF)
|
|
|
|
needed = 0;
|
|
|
|
else if (flags & CRYPT_STRING_NOCR)
|
|
|
|
needed = 1;
|
|
|
|
else
|
|
|
|
needed = 2;
|
|
|
|
|
|
|
|
needed += nbin * 2 + 1;
|
2020-02-11 10:25:40 +01:00
|
|
|
|
|
|
|
if (!str)
|
|
|
|
{
|
|
|
|
*nstr = needed;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2020-01-24 16:05:08 +01:00
|
|
|
if (needed > *nstr)
|
|
|
|
{
|
|
|
|
SetLastError(ERROR_MORE_DATA);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (nbin--)
|
|
|
|
{
|
|
|
|
*str++ = hex[(*bin >> 4) & 0xf];
|
|
|
|
*str++ = hex[*bin & 0xf];
|
|
|
|
bin++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & CRYPT_STRING_NOCR)
|
|
|
|
*str++ = '\n';
|
|
|
|
else if (!(flags & CRYPT_STRING_NOCRLF))
|
|
|
|
{
|
|
|
|
*str++ = '\r';
|
|
|
|
*str++ = '\n';
|
|
|
|
}
|
|
|
|
|
|
|
|
*str = 0;
|
|
|
|
*nstr = needed - 1;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2020-12-03 07:57:21 +01:00
|
|
|
static BOOL binary_to_hexW(const BYTE *bin, DWORD nbin, DWORD flags, LPWSTR str, DWORD *nstr)
|
|
|
|
{
|
|
|
|
static const WCHAR hex[] = L"0123456789abcdef";
|
|
|
|
DWORD needed, i;
|
|
|
|
|
|
|
|
needed = nbin * 3; /* spaces + terminating \0 */
|
|
|
|
|
|
|
|
if (flags & CRYPT_STRING_NOCR)
|
|
|
|
{
|
|
|
|
needed += (nbin + 7) / 16; /* space every 16 characters */
|
|
|
|
needed += 1; /* terminating \n */
|
|
|
|
}
|
|
|
|
else if (!(flags & CRYPT_STRING_NOCRLF))
|
|
|
|
{
|
|
|
|
needed += (nbin + 7) / 16; /* space every 16 characters */
|
|
|
|
needed += nbin / 16 + 1; /* LF every 16 characters + terminating \r */
|
|
|
|
|
|
|
|
if (nbin % 16)
|
|
|
|
needed += 1; /* terminating \n */
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!str)
|
|
|
|
{
|
|
|
|
*nstr = needed;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (needed > *nstr)
|
|
|
|
{
|
|
|
|
SetLastError(ERROR_MORE_DATA);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < nbin; i++)
|
|
|
|
{
|
|
|
|
*str++ = hex[(bin[i] >> 4) & 0xf];
|
|
|
|
*str++ = hex[bin[i] & 0xf];
|
|
|
|
|
|
|
|
if (i >= nbin - 1) break;
|
|
|
|
|
|
|
|
if (i && !(flags & CRYPT_STRING_NOCRLF))
|
|
|
|
{
|
|
|
|
if (!((i + 1) % 16))
|
|
|
|
{
|
|
|
|
if (flags & CRYPT_STRING_NOCR)
|
|
|
|
*str++ = '\n';
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*str++ = '\r';
|
|
|
|
*str++ = '\n';
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if (!((i + 1) % 8))
|
|
|
|
*str++ = ' ';
|
|
|
|
}
|
|
|
|
|
|
|
|
*str++ = ' ';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & CRYPT_STRING_NOCR)
|
|
|
|
*str++ = '\n';
|
|
|
|
else if (!(flags & CRYPT_STRING_NOCRLF))
|
|
|
|
{
|
|
|
|
*str++ = '\r';
|
|
|
|
*str++ = '\n';
|
|
|
|
}
|
|
|
|
|
|
|
|
*str = 0;
|
|
|
|
*nstr = needed - 1;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2009-10-29 23:13:29 +01:00
|
|
|
BOOL WINAPI CryptBinaryToStringW(const BYTE *pbBinary,
|
|
|
|
DWORD cbBinary, DWORD dwFlags, LPWSTR pszString, DWORD *pcchString)
|
|
|
|
{
|
|
|
|
BinaryToStringWFunc encoder = NULL;
|
|
|
|
|
|
|
|
TRACE("(%p, %d, %08x, %p, %p)\n", pbBinary, cbBinary, dwFlags, pszString,
|
|
|
|
pcchString);
|
|
|
|
|
|
|
|
if (!pbBinary)
|
|
|
|
{
|
|
|
|
SetLastError(ERROR_INVALID_PARAMETER);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
if (!pcchString)
|
|
|
|
{
|
|
|
|
SetLastError(ERROR_INVALID_PARAMETER);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2009-10-29 23:43:37 +01:00
|
|
|
switch (dwFlags & 0x0fffffff)
|
2009-10-29 23:13:29 +01:00
|
|
|
{
|
2018-09-27 16:27:56 +02:00
|
|
|
case CRYPT_STRING_BINARY:
|
|
|
|
encoder = EncodeBinaryToBinaryW;
|
|
|
|
break;
|
2009-10-29 23:13:29 +01:00
|
|
|
case CRYPT_STRING_BASE64:
|
|
|
|
case CRYPT_STRING_BASE64HEADER:
|
|
|
|
case CRYPT_STRING_BASE64REQUESTHEADER:
|
|
|
|
case CRYPT_STRING_BASE64X509CRLHEADER:
|
|
|
|
encoder = BinaryToBase64W;
|
|
|
|
break;
|
2020-01-24 16:05:08 +01:00
|
|
|
case CRYPT_STRING_HEXRAW:
|
2020-12-03 07:57:21 +01:00
|
|
|
encoder = BinaryToHexRawW;
|
2020-01-24 16:05:08 +01:00
|
|
|
break;
|
2009-10-29 23:13:29 +01:00
|
|
|
case CRYPT_STRING_HEX:
|
2020-12-03 07:57:21 +01:00
|
|
|
encoder = binary_to_hexW;
|
|
|
|
break;
|
2009-10-29 23:13:29 +01:00
|
|
|
case CRYPT_STRING_HEXASCII:
|
|
|
|
case CRYPT_STRING_HEXADDR:
|
|
|
|
case CRYPT_STRING_HEXASCIIADDR:
|
2009-10-29 23:43:37 +01:00
|
|
|
FIXME("Unimplemented type %d\n", dwFlags & 0x0fffffff);
|
2009-10-29 23:13:29 +01:00
|
|
|
/* fall through */
|
|
|
|
default:
|
|
|
|
SetLastError(ERROR_INVALID_PARAMETER);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
return encoder(pbBinary, cbBinary, dwFlags, pszString, pcchString);
|
|
|
|
}
|
|
|
|
|
2017-02-14 15:59:34 +01:00
|
|
|
#define BASE64_DECODE_PADDING 0x100
|
|
|
|
#define BASE64_DECODE_WHITESPACE 0x200
|
|
|
|
#define BASE64_DECODE_INVALID 0x300
|
|
|
|
|
|
|
|
static inline int decodeBase64Byte(int c)
|
2006-05-31 09:31:10 +02:00
|
|
|
{
|
2017-02-14 15:59:34 +01:00
|
|
|
int ret = BASE64_DECODE_INVALID;
|
2006-05-31 09:31:10 +02:00
|
|
|
|
|
|
|
if (c >= 'A' && c <= 'Z')
|
|
|
|
ret = c - 'A';
|
|
|
|
else if (c >= 'a' && c <= 'z')
|
|
|
|
ret = c - 'a' + 26;
|
|
|
|
else if (c >= '0' && c <= '9')
|
|
|
|
ret = c - '0' + 52;
|
|
|
|
else if (c == '+')
|
|
|
|
ret = 62;
|
|
|
|
else if (c == '/')
|
|
|
|
ret = 63;
|
2017-02-14 15:59:34 +01:00
|
|
|
else if (c == '=')
|
|
|
|
ret = BASE64_DECODE_PADDING;
|
|
|
|
else if (c == ' ' || c == '\t' || c == '\r' || c == '\n')
|
|
|
|
ret = BASE64_DECODE_WHITESPACE;
|
2006-05-31 09:31:10 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-02-14 15:59:34 +01:00
|
|
|
/* Unlike CryptStringToBinaryA, cchString is guaranteed to be the length of the
|
|
|
|
* string to convert.
|
|
|
|
*/
|
|
|
|
typedef LONG (*StringToBinaryAFunc)(LPCSTR pszString, DWORD cchString,
|
|
|
|
BYTE *pbBinary, DWORD *pcbBinary, DWORD *pdwSkip, DWORD *pdwFlags);
|
2006-05-31 09:31:10 +02:00
|
|
|
|
2017-02-14 15:59:34 +01:00
|
|
|
static LONG Base64ToBinary(const void* pszString, BOOL wide, DWORD cchString,
|
|
|
|
BYTE *pbBinary, DWORD *pcbBinary, DWORD *pdwSkip, DWORD *pdwFlags)
|
|
|
|
{
|
|
|
|
DWORD cbIn, cbValid, cbOut, hasPadding;
|
|
|
|
BYTE block[4];
|
|
|
|
for (cbIn = cbValid = cbOut = hasPadding = 0; cbIn < cchString; ++cbIn)
|
|
|
|
{
|
|
|
|
int c = wide ? (int)((WCHAR*)pszString)[cbIn] : (int)((char*)pszString)[cbIn];
|
|
|
|
int d = decodeBase64Byte(c);
|
|
|
|
if (d == BASE64_DECODE_INVALID)
|
|
|
|
goto invalid;
|
|
|
|
if (d == BASE64_DECODE_WHITESPACE)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* When padding starts, data is not acceptable */
|
|
|
|
if (hasPadding && d != BASE64_DECODE_PADDING)
|
|
|
|
goto invalid;
|
|
|
|
|
|
|
|
/* Padding after a full block (like "VVVV=") is ok and stops decoding */
|
|
|
|
if (d == BASE64_DECODE_PADDING && (cbValid & 3) == 0)
|
|
|
|
break;
|
2006-05-31 09:31:10 +02:00
|
|
|
|
2017-02-14 15:59:34 +01:00
|
|
|
cbValid += 1;
|
2006-05-31 09:31:10 +02:00
|
|
|
|
2017-02-14 15:59:34 +01:00
|
|
|
if (d == BASE64_DECODE_PADDING)
|
2006-05-31 09:31:10 +02:00
|
|
|
{
|
2017-02-14 15:59:34 +01:00
|
|
|
hasPadding = 1;
|
|
|
|
/* When padding reaches a full block, stop decoding */
|
|
|
|
if ((cbValid & 3) == 0)
|
|
|
|
break;
|
|
|
|
continue;
|
2006-05-31 09:31:10 +02:00
|
|
|
}
|
|
|
|
|
2017-02-14 15:59:34 +01:00
|
|
|
/* cbOut is incremented in the 4-char block as follows: "1-23" */
|
|
|
|
if ((cbValid & 3) != 2)
|
|
|
|
cbOut += 1;
|
|
|
|
}
|
|
|
|
/* Fail if the block has bad padding; omitting padding is fine */
|
|
|
|
if ((cbValid & 3) != 0 && hasPadding)
|
|
|
|
goto invalid;
|
|
|
|
/* Check available buffer size */
|
|
|
|
if (pbBinary && *pcbBinary && cbOut > *pcbBinary)
|
|
|
|
goto overflow;
|
|
|
|
/* Convert the data; this step depends on the validity checks above! */
|
|
|
|
if (pbBinary) for (cbIn = cbValid = cbOut = 0; cbIn < cchString; ++cbIn)
|
|
|
|
{
|
|
|
|
int c = wide ? (int)((WCHAR*)pszString)[cbIn] : (int)((char*)pszString)[cbIn];
|
|
|
|
int d = decodeBase64Byte(c);
|
|
|
|
if (d == BASE64_DECODE_WHITESPACE)
|
|
|
|
continue;
|
|
|
|
if (d == BASE64_DECODE_PADDING)
|
|
|
|
break;
|
|
|
|
block[cbValid & 3] = d;
|
|
|
|
cbValid += 1;
|
|
|
|
switch (cbValid & 3) {
|
|
|
|
case 1:
|
|
|
|
pbBinary[cbOut++] = (block[0] << 2);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
pbBinary[cbOut-1] = (block[0] << 2) | (block[1] >> 4);
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
pbBinary[cbOut++] = (block[1] << 4) | (block[2] >> 2);
|
|
|
|
break;
|
|
|
|
case 0:
|
|
|
|
pbBinary[cbOut++] = (block[2] << 6) | (block[3] >> 0);
|
|
|
|
break;
|
2006-05-31 09:31:10 +02:00
|
|
|
}
|
2017-02-14 15:59:34 +01:00
|
|
|
}
|
|
|
|
*pcbBinary = cbOut;
|
|
|
|
if (pdwSkip)
|
|
|
|
*pdwSkip = 0;
|
|
|
|
if (pdwFlags)
|
|
|
|
*pdwFlags = CRYPT_STRING_BASE64;
|
2006-05-31 09:31:10 +02:00
|
|
|
return ERROR_SUCCESS;
|
2017-02-14 15:59:34 +01:00
|
|
|
overflow:
|
|
|
|
return ERROR_INSUFFICIENT_BUFFER;
|
|
|
|
invalid:
|
|
|
|
*pcbBinary = cbOut;
|
|
|
|
return ERROR_INVALID_DATA;
|
2006-05-31 09:31:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static LONG Base64ToBinaryA(LPCSTR pszString, DWORD cchString,
|
|
|
|
BYTE *pbBinary, DWORD *pcbBinary, DWORD *pdwSkip, DWORD *pdwFlags)
|
|
|
|
{
|
2017-02-14 15:59:34 +01:00
|
|
|
return Base64ToBinary(pszString, FALSE, cchString, pbBinary, pcbBinary, pdwSkip, pdwFlags);
|
2006-05-31 09:31:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static LONG Base64WithHeaderAndTrailerToBinaryA(LPCSTR pszString,
|
2017-02-14 15:59:35 +01:00
|
|
|
DWORD cchString, BYTE *pbBinary,
|
2017-02-17 15:46:51 +01:00
|
|
|
DWORD *pcbBinary, DWORD *pdwSkip)
|
2006-05-31 09:31:10 +02:00
|
|
|
{
|
|
|
|
LONG ret;
|
2017-02-14 15:59:35 +01:00
|
|
|
LPCSTR header = CERT_HEADER_START;
|
|
|
|
LPCSTR trailer = CERT_TRAILER_START;
|
2006-05-31 09:31:10 +02:00
|
|
|
|
2011-04-23 17:18:55 +02:00
|
|
|
LPCSTR headerBegins;
|
|
|
|
LPCSTR dataBegins;
|
|
|
|
LPCSTR trailerBegins;
|
|
|
|
size_t dataLength;
|
|
|
|
|
|
|
|
if ((strlen(header) + strlen(trailer)) > cchString)
|
2006-05-31 09:31:10 +02:00
|
|
|
{
|
2011-04-23 17:18:55 +02:00
|
|
|
return ERROR_INVALID_DATA;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(headerBegins = strstr(pszString, header)))
|
|
|
|
{
|
2018-06-26 09:59:38 +02:00
|
|
|
TRACE("Can't find %s in %s.\n", header, debugstr_an(pszString, cchString));
|
2011-04-23 17:18:55 +02:00
|
|
|
return ERROR_INVALID_DATA;
|
|
|
|
}
|
2006-05-31 09:31:10 +02:00
|
|
|
|
2011-04-23 17:18:55 +02:00
|
|
|
dataBegins = headerBegins + strlen(header);
|
2017-02-17 15:46:51 +01:00
|
|
|
if (!(dataBegins = strstr(dataBegins, CERT_DELIMITER)))
|
2011-04-23 17:18:55 +02:00
|
|
|
{
|
2017-02-17 15:46:51 +01:00
|
|
|
return ERROR_INVALID_DATA;
|
2011-04-23 17:18:55 +02:00
|
|
|
}
|
2017-02-17 15:46:51 +01:00
|
|
|
dataBegins += strlen(CERT_DELIMITER);
|
2011-04-23 17:18:55 +02:00
|
|
|
if (*dataBegins == '\r') dataBegins++;
|
|
|
|
if (*dataBegins == '\n') dataBegins++;
|
|
|
|
|
2017-02-17 15:46:51 +01:00
|
|
|
if (!(trailerBegins = strstr(dataBegins, trailer)))
|
2011-04-23 17:18:55 +02:00
|
|
|
{
|
2017-02-17 15:46:51 +01:00
|
|
|
return ERROR_INVALID_DATA;
|
2011-04-23 17:18:55 +02:00
|
|
|
}
|
2017-02-17 15:46:51 +01:00
|
|
|
if (*(trailerBegins-1) == '\n') trailerBegins--;
|
|
|
|
if (*(trailerBegins-1) == '\r') trailerBegins--;
|
2011-04-23 17:18:55 +02:00
|
|
|
|
|
|
|
if (pdwSkip)
|
|
|
|
*pdwSkip = headerBegins - pszString;
|
|
|
|
|
|
|
|
dataLength = trailerBegins - dataBegins;
|
|
|
|
|
|
|
|
ret = Base64ToBinaryA(dataBegins, dataLength, pbBinary, pcbBinary, NULL,
|
|
|
|
NULL);
|
|
|
|
|
2006-05-31 09:31:10 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static LONG Base64HeaderToBinaryA(LPCSTR pszString, DWORD cchString,
|
|
|
|
BYTE *pbBinary, DWORD *pcbBinary, DWORD *pdwSkip, DWORD *pdwFlags)
|
|
|
|
{
|
|
|
|
LONG ret = Base64WithHeaderAndTrailerToBinaryA(pszString, cchString,
|
2017-02-17 15:46:51 +01:00
|
|
|
pbBinary, pcbBinary, pdwSkip);
|
2006-05-31 09:31:10 +02:00
|
|
|
|
|
|
|
if (!ret && pdwFlags)
|
|
|
|
*pdwFlags = CRYPT_STRING_BASE64HEADER;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static LONG Base64RequestHeaderToBinaryA(LPCSTR pszString, DWORD cchString,
|
|
|
|
BYTE *pbBinary, DWORD *pcbBinary, DWORD *pdwSkip, DWORD *pdwFlags)
|
|
|
|
{
|
|
|
|
LONG ret = Base64WithHeaderAndTrailerToBinaryA(pszString, cchString,
|
2017-02-17 15:46:51 +01:00
|
|
|
pbBinary, pcbBinary, pdwSkip);
|
2006-05-31 09:31:10 +02:00
|
|
|
|
|
|
|
if (!ret && pdwFlags)
|
|
|
|
*pdwFlags = CRYPT_STRING_BASE64REQUESTHEADER;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static LONG Base64X509HeaderToBinaryA(LPCSTR pszString, DWORD cchString,
|
|
|
|
BYTE *pbBinary, DWORD *pcbBinary, DWORD *pdwSkip, DWORD *pdwFlags)
|
|
|
|
{
|
|
|
|
LONG ret = Base64WithHeaderAndTrailerToBinaryA(pszString, cchString,
|
2017-02-17 15:46:51 +01:00
|
|
|
pbBinary, pcbBinary, pdwSkip);
|
2006-05-31 09:31:10 +02:00
|
|
|
|
|
|
|
if (!ret && pdwFlags)
|
|
|
|
*pdwFlags = CRYPT_STRING_BASE64X509CRLHEADER;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static LONG Base64AnyToBinaryA(LPCSTR pszString, DWORD cchString,
|
|
|
|
BYTE *pbBinary, DWORD *pcbBinary, DWORD *pdwSkip, DWORD *pdwFlags)
|
|
|
|
{
|
|
|
|
LONG ret;
|
|
|
|
|
|
|
|
ret = Base64HeaderToBinaryA(pszString, cchString, pbBinary, pcbBinary,
|
|
|
|
pdwSkip, pdwFlags);
|
|
|
|
if (ret == ERROR_INVALID_DATA)
|
|
|
|
ret = Base64ToBinaryA(pszString, cchString, pbBinary, pcbBinary,
|
|
|
|
pdwSkip, pdwFlags);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static LONG DecodeBinaryToBinaryA(LPCSTR pszString, DWORD cchString,
|
|
|
|
BYTE *pbBinary, DWORD *pcbBinary, DWORD *pdwSkip, DWORD *pdwFlags)
|
|
|
|
{
|
|
|
|
LONG ret = ERROR_SUCCESS;
|
|
|
|
|
|
|
|
if (*pcbBinary < cchString)
|
|
|
|
{
|
|
|
|
if (!pbBinary)
|
|
|
|
*pcbBinary = cchString;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = ERROR_INSUFFICIENT_BUFFER;
|
|
|
|
*pcbBinary = cchString;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (cchString)
|
|
|
|
memcpy(pbBinary, pszString, cchString);
|
|
|
|
*pcbBinary = cchString;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static LONG DecodeAnyA(LPCSTR pszString, DWORD cchString,
|
|
|
|
BYTE *pbBinary, DWORD *pcbBinary, DWORD *pdwSkip, DWORD *pdwFlags)
|
|
|
|
{
|
|
|
|
LONG ret;
|
|
|
|
|
|
|
|
ret = Base64HeaderToBinaryA(pszString, cchString, pbBinary, pcbBinary,
|
|
|
|
pdwSkip, pdwFlags);
|
|
|
|
if (ret == ERROR_INVALID_DATA)
|
|
|
|
ret = Base64ToBinaryA(pszString, cchString, pbBinary, pcbBinary,
|
|
|
|
pdwSkip, pdwFlags);
|
|
|
|
if (ret == ERROR_INVALID_DATA)
|
|
|
|
ret = DecodeBinaryToBinaryA(pszString, cchString, pbBinary, pcbBinary,
|
|
|
|
pdwSkip, pdwFlags);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL WINAPI CryptStringToBinaryA(LPCSTR pszString,
|
|
|
|
DWORD cchString, DWORD dwFlags, BYTE *pbBinary, DWORD *pcbBinary,
|
|
|
|
DWORD *pdwSkip, DWORD *pdwFlags)
|
|
|
|
{
|
|
|
|
StringToBinaryAFunc decoder;
|
|
|
|
LONG ret;
|
|
|
|
|
2018-06-26 09:59:38 +02:00
|
|
|
TRACE("(%s, %d, %08x, %p, %p, %p, %p)\n", debugstr_an(pszString, cchString ? cchString : -1),
|
2006-05-31 09:31:10 +02:00
|
|
|
cchString, dwFlags, pbBinary, pcbBinary, pdwSkip, pdwFlags);
|
|
|
|
|
|
|
|
if (!pszString)
|
|
|
|
{
|
|
|
|
SetLastError(ERROR_INVALID_PARAMETER);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
/* Only the bottom byte contains valid types */
|
|
|
|
if (dwFlags & 0xfffffff0)
|
|
|
|
{
|
|
|
|
SetLastError(ERROR_INVALID_DATA);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
switch (dwFlags)
|
|
|
|
{
|
|
|
|
case CRYPT_STRING_BASE64_ANY:
|
|
|
|
decoder = Base64AnyToBinaryA;
|
|
|
|
break;
|
|
|
|
case CRYPT_STRING_BASE64:
|
|
|
|
decoder = Base64ToBinaryA;
|
|
|
|
break;
|
|
|
|
case CRYPT_STRING_BASE64HEADER:
|
|
|
|
decoder = Base64HeaderToBinaryA;
|
|
|
|
break;
|
|
|
|
case CRYPT_STRING_BASE64REQUESTHEADER:
|
|
|
|
decoder = Base64RequestHeaderToBinaryA;
|
|
|
|
break;
|
|
|
|
case CRYPT_STRING_BASE64X509CRLHEADER:
|
|
|
|
decoder = Base64X509HeaderToBinaryA;
|
|
|
|
break;
|
|
|
|
case CRYPT_STRING_BINARY:
|
|
|
|
decoder = DecodeBinaryToBinaryA;
|
|
|
|
break;
|
|
|
|
case CRYPT_STRING_ANY:
|
|
|
|
decoder = DecodeAnyA;
|
|
|
|
break;
|
|
|
|
case CRYPT_STRING_HEX:
|
|
|
|
case CRYPT_STRING_HEXASCII:
|
|
|
|
case CRYPT_STRING_HEXADDR:
|
|
|
|
case CRYPT_STRING_HEXASCIIADDR:
|
2006-10-04 06:58:09 +02:00
|
|
|
FIXME("Unimplemented type %d\n", dwFlags & 0x7fffffff);
|
2006-05-31 09:31:10 +02:00
|
|
|
/* fall through */
|
|
|
|
default:
|
|
|
|
SetLastError(ERROR_INVALID_PARAMETER);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
if (!cchString)
|
|
|
|
cchString = strlen(pszString);
|
|
|
|
ret = decoder(pszString, cchString, pbBinary, pcbBinary, pdwSkip, pdwFlags);
|
|
|
|
if (ret)
|
|
|
|
SetLastError(ret);
|
2012-08-06 11:19:22 +02:00
|
|
|
return ret == ERROR_SUCCESS;
|
2006-05-31 09:31:10 +02:00
|
|
|
}
|
2008-12-12 01:49:16 +01:00
|
|
|
|
|
|
|
/* Unlike CryptStringToBinaryW, cchString is guaranteed to be the length of the
|
|
|
|
* string to convert.
|
|
|
|
*/
|
|
|
|
typedef LONG (*StringToBinaryWFunc)(LPCWSTR pszString, DWORD cchString,
|
|
|
|
BYTE *pbBinary, DWORD *pcbBinary, DWORD *pdwSkip, DWORD *pdwFlags);
|
|
|
|
|
|
|
|
static LONG Base64ToBinaryW(LPCWSTR pszString, DWORD cchString,
|
|
|
|
BYTE *pbBinary, DWORD *pcbBinary, DWORD *pdwSkip, DWORD *pdwFlags)
|
|
|
|
{
|
2017-02-14 15:59:34 +01:00
|
|
|
return Base64ToBinary(pszString, TRUE, cchString, pbBinary, pcbBinary, pdwSkip, pdwFlags);
|
2008-12-12 01:49:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static LONG Base64WithHeaderAndTrailerToBinaryW(LPCWSTR pszString,
|
2017-02-14 15:59:35 +01:00
|
|
|
DWORD cchString, BYTE *pbBinary,
|
2017-02-17 15:46:51 +01:00
|
|
|
DWORD *pcbBinary, DWORD *pdwSkip)
|
2008-12-12 01:49:16 +01:00
|
|
|
{
|
|
|
|
LONG ret;
|
2017-02-14 15:59:35 +01:00
|
|
|
LPCWSTR header = CERT_HEADER_START_W;
|
|
|
|
LPCWSTR trailer = CERT_TRAILER_START_W;
|
2008-12-12 01:49:16 +01:00
|
|
|
|
2011-04-23 17:18:55 +02:00
|
|
|
LPCWSTR headerBegins;
|
|
|
|
LPCWSTR dataBegins;
|
|
|
|
LPCWSTR trailerBegins;
|
|
|
|
size_t dataLength;
|
|
|
|
|
2020-11-09 11:41:21 +01:00
|
|
|
if ((lstrlenW(header) + lstrlenW(trailer)) > cchString)
|
2008-12-12 01:49:16 +01:00
|
|
|
{
|
2011-04-23 17:18:55 +02:00
|
|
|
return ERROR_INVALID_DATA;
|
|
|
|
}
|
|
|
|
|
2020-11-09 11:41:21 +01:00
|
|
|
if (!(headerBegins = wcsstr(pszString, header)))
|
2011-04-23 17:18:55 +02:00
|
|
|
{
|
2018-06-26 09:59:38 +02:00
|
|
|
TRACE("Can't find %s in %s.\n", debugstr_w(header), debugstr_wn(pszString, cchString));
|
2011-04-23 17:18:55 +02:00
|
|
|
return ERROR_INVALID_DATA;
|
|
|
|
}
|
2008-12-12 01:49:16 +01:00
|
|
|
|
2020-11-09 11:41:21 +01:00
|
|
|
dataBegins = headerBegins + lstrlenW(header);
|
|
|
|
if (!(dataBegins = wcsstr(dataBegins, CERT_DELIMITER_W)))
|
2011-04-23 17:18:55 +02:00
|
|
|
{
|
2017-02-17 15:46:51 +01:00
|
|
|
return ERROR_INVALID_DATA;
|
2011-04-23 17:18:55 +02:00
|
|
|
}
|
2020-11-09 11:41:21 +01:00
|
|
|
dataBegins += lstrlenW(CERT_DELIMITER_W);
|
2011-04-23 17:18:55 +02:00
|
|
|
if (*dataBegins == '\r') dataBegins++;
|
|
|
|
if (*dataBegins == '\n') dataBegins++;
|
|
|
|
|
2020-11-09 11:41:21 +01:00
|
|
|
if (!(trailerBegins = wcsstr(dataBegins, trailer)))
|
2011-04-23 17:18:55 +02:00
|
|
|
{
|
2017-02-17 15:46:51 +01:00
|
|
|
return ERROR_INVALID_DATA;
|
2011-04-23 17:18:55 +02:00
|
|
|
}
|
2017-02-17 15:46:51 +01:00
|
|
|
if (*(trailerBegins-1) == '\n') trailerBegins--;
|
|
|
|
if (*(trailerBegins-1) == '\r') trailerBegins--;
|
2011-04-23 17:18:55 +02:00
|
|
|
|
|
|
|
if (pdwSkip)
|
|
|
|
*pdwSkip = headerBegins - pszString;
|
|
|
|
|
|
|
|
dataLength = trailerBegins - dataBegins;
|
|
|
|
|
|
|
|
ret = Base64ToBinaryW(dataBegins, dataLength, pbBinary, pcbBinary, NULL,
|
|
|
|
NULL);
|
|
|
|
|
2008-12-12 01:49:16 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static LONG Base64HeaderToBinaryW(LPCWSTR pszString, DWORD cchString,
|
|
|
|
BYTE *pbBinary, DWORD *pcbBinary, DWORD *pdwSkip, DWORD *pdwFlags)
|
|
|
|
{
|
|
|
|
LONG ret = Base64WithHeaderAndTrailerToBinaryW(pszString, cchString,
|
2017-02-17 15:46:51 +01:00
|
|
|
pbBinary, pcbBinary, pdwSkip);
|
2008-12-12 01:49:16 +01:00
|
|
|
|
|
|
|
if (!ret && pdwFlags)
|
|
|
|
*pdwFlags = CRYPT_STRING_BASE64HEADER;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static LONG Base64RequestHeaderToBinaryW(LPCWSTR pszString, DWORD cchString,
|
|
|
|
BYTE *pbBinary, DWORD *pcbBinary, DWORD *pdwSkip, DWORD *pdwFlags)
|
|
|
|
{
|
|
|
|
LONG ret = Base64WithHeaderAndTrailerToBinaryW(pszString, cchString,
|
2017-02-17 15:46:51 +01:00
|
|
|
pbBinary, pcbBinary, pdwSkip);
|
2008-12-12 01:49:16 +01:00
|
|
|
|
|
|
|
if (!ret && pdwFlags)
|
|
|
|
*pdwFlags = CRYPT_STRING_BASE64REQUESTHEADER;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static LONG Base64X509HeaderToBinaryW(LPCWSTR pszString, DWORD cchString,
|
|
|
|
BYTE *pbBinary, DWORD *pcbBinary, DWORD *pdwSkip, DWORD *pdwFlags)
|
|
|
|
{
|
|
|
|
LONG ret = Base64WithHeaderAndTrailerToBinaryW(pszString, cchString,
|
2017-02-17 15:46:51 +01:00
|
|
|
pbBinary, pcbBinary, pdwSkip);
|
2008-12-12 01:49:16 +01:00
|
|
|
|
|
|
|
if (!ret && pdwFlags)
|
|
|
|
*pdwFlags = CRYPT_STRING_BASE64X509CRLHEADER;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static LONG Base64AnyToBinaryW(LPCWSTR pszString, DWORD cchString,
|
|
|
|
BYTE *pbBinary, DWORD *pcbBinary, DWORD *pdwSkip, DWORD *pdwFlags)
|
|
|
|
{
|
|
|
|
LONG ret;
|
|
|
|
|
|
|
|
ret = Base64HeaderToBinaryW(pszString, cchString, pbBinary, pcbBinary,
|
|
|
|
pdwSkip, pdwFlags);
|
|
|
|
if (ret == ERROR_INVALID_DATA)
|
|
|
|
ret = Base64ToBinaryW(pszString, cchString, pbBinary, pcbBinary,
|
|
|
|
pdwSkip, pdwFlags);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static LONG DecodeBinaryToBinaryW(LPCWSTR pszString, DWORD cchString,
|
|
|
|
BYTE *pbBinary, DWORD *pcbBinary, DWORD *pdwSkip, DWORD *pdwFlags)
|
|
|
|
{
|
|
|
|
LONG ret = ERROR_SUCCESS;
|
|
|
|
|
|
|
|
if (*pcbBinary < cchString)
|
|
|
|
{
|
|
|
|
if (!pbBinary)
|
|
|
|
*pcbBinary = cchString;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = ERROR_INSUFFICIENT_BUFFER;
|
|
|
|
*pcbBinary = cchString;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (cchString)
|
|
|
|
memcpy(pbBinary, pszString, cchString * sizeof(WCHAR));
|
|
|
|
*pcbBinary = cchString * sizeof(WCHAR);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static LONG DecodeAnyW(LPCWSTR pszString, DWORD cchString,
|
|
|
|
BYTE *pbBinary, DWORD *pcbBinary, DWORD *pdwSkip, DWORD *pdwFlags)
|
|
|
|
{
|
|
|
|
LONG ret;
|
|
|
|
|
|
|
|
ret = Base64HeaderToBinaryW(pszString, cchString, pbBinary, pcbBinary,
|
|
|
|
pdwSkip, pdwFlags);
|
|
|
|
if (ret == ERROR_INVALID_DATA)
|
|
|
|
ret = Base64ToBinaryW(pszString, cchString, pbBinary, pcbBinary,
|
|
|
|
pdwSkip, pdwFlags);
|
|
|
|
if (ret == ERROR_INVALID_DATA)
|
|
|
|
ret = DecodeBinaryToBinaryW(pszString, cchString, pbBinary, pcbBinary,
|
|
|
|
pdwSkip, pdwFlags);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL WINAPI CryptStringToBinaryW(LPCWSTR pszString,
|
|
|
|
DWORD cchString, DWORD dwFlags, BYTE *pbBinary, DWORD *pcbBinary,
|
|
|
|
DWORD *pdwSkip, DWORD *pdwFlags)
|
|
|
|
{
|
|
|
|
StringToBinaryWFunc decoder;
|
|
|
|
LONG ret;
|
|
|
|
|
2018-06-26 09:59:38 +02:00
|
|
|
TRACE("(%s, %d, %08x, %p, %p, %p, %p)\n", debugstr_wn(pszString, cchString ? cchString : -1),
|
2008-12-12 01:49:16 +01:00
|
|
|
cchString, dwFlags, pbBinary, pcbBinary, pdwSkip, pdwFlags);
|
|
|
|
|
|
|
|
if (!pszString)
|
|
|
|
{
|
|
|
|
SetLastError(ERROR_INVALID_PARAMETER);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
/* Only the bottom byte contains valid types */
|
|
|
|
if (dwFlags & 0xfffffff0)
|
|
|
|
{
|
|
|
|
SetLastError(ERROR_INVALID_DATA);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
switch (dwFlags)
|
|
|
|
{
|
|
|
|
case CRYPT_STRING_BASE64_ANY:
|
|
|
|
decoder = Base64AnyToBinaryW;
|
|
|
|
break;
|
|
|
|
case CRYPT_STRING_BASE64:
|
|
|
|
decoder = Base64ToBinaryW;
|
|
|
|
break;
|
|
|
|
case CRYPT_STRING_BASE64HEADER:
|
|
|
|
decoder = Base64HeaderToBinaryW;
|
|
|
|
break;
|
|
|
|
case CRYPT_STRING_BASE64REQUESTHEADER:
|
|
|
|
decoder = Base64RequestHeaderToBinaryW;
|
|
|
|
break;
|
|
|
|
case CRYPT_STRING_BASE64X509CRLHEADER:
|
|
|
|
decoder = Base64X509HeaderToBinaryW;
|
|
|
|
break;
|
|
|
|
case CRYPT_STRING_BINARY:
|
|
|
|
decoder = DecodeBinaryToBinaryW;
|
|
|
|
break;
|
|
|
|
case CRYPT_STRING_ANY:
|
|
|
|
decoder = DecodeAnyW;
|
|
|
|
break;
|
|
|
|
case CRYPT_STRING_HEX:
|
|
|
|
case CRYPT_STRING_HEXASCII:
|
|
|
|
case CRYPT_STRING_HEXADDR:
|
|
|
|
case CRYPT_STRING_HEXASCIIADDR:
|
|
|
|
FIXME("Unimplemented type %d\n", dwFlags & 0x7fffffff);
|
|
|
|
/* fall through */
|
|
|
|
default:
|
|
|
|
SetLastError(ERROR_INVALID_PARAMETER);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
if (!cchString)
|
2020-11-09 11:41:21 +01:00
|
|
|
cchString = lstrlenW(pszString);
|
2008-12-12 01:49:16 +01:00
|
|
|
ret = decoder(pszString, cchString, pbBinary, pcbBinary, pdwSkip, pdwFlags);
|
|
|
|
if (ret)
|
|
|
|
SetLastError(ret);
|
2012-08-06 11:19:22 +02:00
|
|
|
return ret == ERROR_SUCCESS;
|
2008-12-12 01:49:16 +01:00
|
|
|
}
|