2 * base64 encoder/decoder
4 * Copyright 2005 by Kai Blin
5 * Copyright 2006 Juan Lang
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
27 #include "wine/debug.h"
28 #include "wine/unicode.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(crypt
);
32 #define CERT_HEADER "-----BEGIN CERTIFICATE-----"
33 #define CERT_HEADER_START "-----BEGIN "
34 #define CERT_DELIMITER "-----"
35 #define CERT_TRAILER "-----END CERTIFICATE-----"
36 #define CERT_TRAILER_START "-----END "
37 #define CERT_REQUEST_HEADER "-----BEGIN NEW CERTIFICATE REQUEST-----"
38 #define CERT_REQUEST_TRAILER "-----END NEW CERTIFICATE REQUEST-----"
39 #define X509_HEADER "-----BEGIN X509 CRL-----"
40 #define X509_TRAILER "-----END X509 CRL-----"
42 static const WCHAR CERT_HEADER_W
[] = {
43 '-','-','-','-','-','B','E','G','I','N',' ','C','E','R','T','I','F','I','C',
44 'A','T','E','-','-','-','-','-',0 };
45 static const WCHAR CERT_HEADER_START_W
[] = {
46 '-','-','-','-','-','B','E','G','I','N',' ',0 };
47 static const WCHAR CERT_DELIMITER_W
[] = {
48 '-','-','-','-','-',0 };
49 static const WCHAR CERT_TRAILER_W
[] = {
50 '-','-','-','-','-','E','N','D',' ','C','E','R','T','I','F','I','C','A','T',
51 'E','-','-','-','-','-',0 };
52 static const WCHAR CERT_TRAILER_START_W
[] = {
53 '-','-','-','-','-','E','N','D',' ',0 };
54 static const WCHAR CERT_REQUEST_HEADER_W
[] = {
55 '-','-','-','-','-','B','E','G','I','N',' ','N','E','W',' ','C','E','R','T',
56 'I','F','I','C','A','T','E','R','E','Q','U','E','S','T','-','-','-','-','-',0 };
57 static const WCHAR CERT_REQUEST_TRAILER_W
[] = {
58 '-','-','-','-','-','E','N','D',' ','N','E','W',' ','C','E','R','T','I','F',
59 'I','C','A','T','E','R','E','Q','U','E','S','T','-','-','-','-','-',0 };
60 static const WCHAR X509_HEADER_W
[] = {
61 '-','-','-','-','-','B','E','G','I','N',' ','X','5','0','9',' ','C','R','L',
62 '-','-','-','-','-',0 };
63 static const WCHAR X509_TRAILER_W
[] = {
64 '-','-','-','-','-','E','N','D',' ','X','5','0','9',' ','C','R','L','-','-',
67 static const char b64
[] =
68 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
70 typedef BOOL (*BinaryToStringAFunc
)(const BYTE
*pbBinary
,
71 DWORD cbBinary
, DWORD dwFlags
, LPSTR pszString
, DWORD
*pcchString
);
72 typedef BOOL (*BinaryToStringWFunc
)(const BYTE
*pbBinary
,
73 DWORD cbBinary
, DWORD dwFlags
, LPWSTR pszString
, DWORD
*pcchString
);
75 static BOOL
EncodeBinaryToBinaryA(const BYTE
*pbBinary
,
76 DWORD cbBinary
, DWORD dwFlags
, LPSTR pszString
, DWORD
*pcchString
)
80 if (*pcchString
< cbBinary
)
83 *pcchString
= cbBinary
;
86 SetLastError(ERROR_INSUFFICIENT_BUFFER
);
87 *pcchString
= cbBinary
;
94 memcpy(pszString
, pbBinary
, cbBinary
);
95 *pcchString
= cbBinary
;
100 static LONG
encodeBase64A(const BYTE
*in_buf
, int in_len
, LPCSTR sep
,
101 char* out_buf
, DWORD
*out_len
)
104 const BYTE
*d
= in_buf
;
105 int bytes
= (in_len
*8 + 5)/6, pad_bytes
= (bytes
% 4) ? 4 - (bytes
% 4) : 0;
109 TRACE("bytes is %d, pad bytes is %d\n", bytes
, pad_bytes
);
110 needed
= bytes
+ pad_bytes
+ 1;
111 needed
+= (needed
/ 64 + 1) * strlen(sep
);
113 if (needed
> *out_len
)
116 return ERROR_INSUFFICIENT_BUFFER
;
121 /* Three bytes of input give 4 chars of output */
128 if (i
&& i
% 64 == 0)
133 /* first char is the first 6 bits of the first byte*/
134 *ptr
++ = b64
[ ( d
[0] >> 2) & 0x3f ];
135 /* second char is the last 2 bits of the first byte and the first 4
136 * bits of the second byte */
137 *ptr
++ = b64
[ ((d
[0] << 4) & 0x30) | (d
[1] >> 4 & 0x0f)];
138 /* third char is the last 4 bits of the second byte and the first 2
139 * bits of the third byte */
140 *ptr
++ = b64
[ ((d
[1] << 2) & 0x3c) | (d
[2] >> 6 & 0x03)];
141 /* fourth char is the remaining 6 bits of the third byte */
142 *ptr
++ = b64
[ d
[2] & 0x3f];
151 /* first char is the first 6 bits of the first byte*/
152 *ptr
++ = b64
[ ( d
[0] >> 2) & 0x3f ];
153 /* second char is the last 2 bits of the first byte and the first 4
154 * bits of the second byte */
155 *ptr
++ = b64
[ ((d
[0] << 4) & 0x30) | (d
[1] >> 4 & 0x0f)];
156 /* third char is the last 4 bits of the second byte padded with
158 *ptr
++ = b64
[ ((d
[1] << 2) & 0x3c) ];
159 /* fourth char is a = to indicate one byte of padding */
163 /* first char is the first 6 bits of the first byte*/
164 *ptr
++ = b64
[ ( d
[0] >> 2) & 0x3f ];
165 /* second char is the last 2 bits of the first byte padded with
167 *ptr
++ = b64
[ ((d
[0] << 4) & 0x30)];
168 /* third char is = to indicate padding */
170 /* fourth char is = to indicate padding */
176 return ERROR_SUCCESS
;
179 static BOOL
BinaryToBase64A(const BYTE
*pbBinary
,
180 DWORD cbBinary
, DWORD dwFlags
, LPSTR pszString
, DWORD
*pcchString
)
182 static const char crlf
[] = "\r\n", lf
[] = "\n";
184 LPCSTR header
= NULL
, trailer
= NULL
, sep
;
187 if (dwFlags
& CRYPT_STRING_NOCR
)
189 else if (dwFlags
& CRYPT_STRING_NOCRLF
)
193 switch (dwFlags
& 0x0fffffff)
195 case CRYPT_STRING_BASE64
:
196 /* no header or footer */
198 case CRYPT_STRING_BASE64HEADER
:
199 header
= CERT_HEADER
;
200 trailer
= CERT_TRAILER
;
202 case CRYPT_STRING_BASE64REQUESTHEADER
:
203 header
= CERT_REQUEST_HEADER
;
204 trailer
= CERT_REQUEST_TRAILER
;
206 case CRYPT_STRING_BASE64X509CRLHEADER
:
207 header
= X509_HEADER
;
208 trailer
= X509_TRAILER
;
213 encodeBase64A(pbBinary
, cbBinary
, sep
, NULL
, &charsNeeded
);
215 charsNeeded
+= strlen(header
) + strlen(sep
);
217 charsNeeded
+= strlen(trailer
) + strlen(sep
);
218 if (charsNeeded
<= *pcchString
)
220 LPSTR ptr
= pszString
;
221 DWORD size
= charsNeeded
;
230 encodeBase64A(pbBinary
, cbBinary
, sep
, ptr
, &size
);
234 strcpy(ptr
, trailer
);
238 *pcchString
= charsNeeded
- 1;
242 *pcchString
= charsNeeded
;
243 SetLastError(ERROR_INSUFFICIENT_BUFFER
);
247 *pcchString
= charsNeeded
;
251 BOOL WINAPI
CryptBinaryToStringA(const BYTE
*pbBinary
,
252 DWORD cbBinary
, DWORD dwFlags
, LPSTR pszString
, DWORD
*pcchString
)
254 BinaryToStringAFunc encoder
= NULL
;
256 TRACE("(%p, %d, %08x, %p, %p)\n", pbBinary
, cbBinary
, dwFlags
, pszString
,
261 SetLastError(ERROR_INVALID_PARAMETER
);
266 SetLastError(ERROR_INVALID_PARAMETER
);
270 switch (dwFlags
& 0x0fffffff)
272 case CRYPT_STRING_BINARY
:
273 encoder
= EncodeBinaryToBinaryA
;
275 case CRYPT_STRING_BASE64
:
276 case CRYPT_STRING_BASE64HEADER
:
277 case CRYPT_STRING_BASE64REQUESTHEADER
:
278 case CRYPT_STRING_BASE64X509CRLHEADER
:
279 encoder
= BinaryToBase64A
;
281 case CRYPT_STRING_HEX
:
282 case CRYPT_STRING_HEXASCII
:
283 case CRYPT_STRING_HEXADDR
:
284 case CRYPT_STRING_HEXASCIIADDR
:
285 FIXME("Unimplemented type %d\n", dwFlags
& 0x0fffffff);
288 SetLastError(ERROR_INVALID_PARAMETER
);
291 return encoder(pbBinary
, cbBinary
, dwFlags
, pszString
, pcchString
);
294 static LONG
encodeBase64W(const BYTE
*in_buf
, int in_len
, LPCWSTR sep
,
295 WCHAR
* out_buf
, DWORD
*out_len
)
298 const BYTE
*d
= in_buf
;
299 int bytes
= (in_len
*8 + 5)/6, pad_bytes
= (bytes
% 4) ? 4 - (bytes
% 4) : 0;
303 TRACE("bytes is %d, pad bytes is %d\n", bytes
, pad_bytes
);
304 needed
= bytes
+ pad_bytes
+ 1;
305 needed
+= (needed
/ 64 + 1) * strlenW(sep
);
307 if (needed
> *out_len
)
310 return ERROR_INSUFFICIENT_BUFFER
;
315 /* Three bytes of input give 4 chars of output */
322 if (i
&& i
% 64 == 0)
327 /* first char is the first 6 bits of the first byte*/
328 *ptr
++ = b64
[ ( d
[0] >> 2) & 0x3f ];
329 /* second char is the last 2 bits of the first byte and the first 4
330 * bits of the second byte */
331 *ptr
++ = b64
[ ((d
[0] << 4) & 0x30) | (d
[1] >> 4 & 0x0f)];
332 /* third char is the last 4 bits of the second byte and the first 2
333 * bits of the third byte */
334 *ptr
++ = b64
[ ((d
[1] << 2) & 0x3c) | (d
[2] >> 6 & 0x03)];
335 /* fourth char is the remaining 6 bits of the third byte */
336 *ptr
++ = b64
[ d
[2] & 0x3f];
345 /* first char is the first 6 bits of the first byte*/
346 *ptr
++ = b64
[ ( d
[0] >> 2) & 0x3f ];
347 /* second char is the last 2 bits of the first byte and the first 4
348 * bits of the second byte */
349 *ptr
++ = b64
[ ((d
[0] << 4) & 0x30) | (d
[1] >> 4 & 0x0f)];
350 /* third char is the last 4 bits of the second byte padded with
352 *ptr
++ = b64
[ ((d
[1] << 2) & 0x3c) ];
353 /* fourth char is a = to indicate one byte of padding */
357 /* first char is the first 6 bits of the first byte*/
358 *ptr
++ = b64
[ ( d
[0] >> 2) & 0x3f ];
359 /* second char is the last 2 bits of the first byte padded with
361 *ptr
++ = b64
[ ((d
[0] << 4) & 0x30)];
362 /* third char is = to indicate padding */
364 /* fourth char is = to indicate padding */
370 return ERROR_SUCCESS
;
373 static BOOL
BinaryToBase64W(const BYTE
*pbBinary
,
374 DWORD cbBinary
, DWORD dwFlags
, LPWSTR pszString
, DWORD
*pcchString
)
376 static const WCHAR crlf
[] = { '\r','\n',0 }, lf
[] = { '\n',0 }, empty
[] = {0};
378 LPCWSTR header
= NULL
, trailer
= NULL
, sep
;
381 if (dwFlags
& CRYPT_STRING_NOCR
)
383 else if (dwFlags
& CRYPT_STRING_NOCRLF
)
387 switch (dwFlags
& 0x0fffffff)
389 case CRYPT_STRING_BASE64
:
390 /* no header or footer */
392 case CRYPT_STRING_BASE64HEADER
:
393 header
= CERT_HEADER_W
;
394 trailer
= CERT_TRAILER_W
;
396 case CRYPT_STRING_BASE64REQUESTHEADER
:
397 header
= CERT_REQUEST_HEADER_W
;
398 trailer
= CERT_REQUEST_TRAILER_W
;
400 case CRYPT_STRING_BASE64X509CRLHEADER
:
401 header
= X509_HEADER_W
;
402 trailer
= X509_TRAILER_W
;
407 encodeBase64W(pbBinary
, cbBinary
, sep
, NULL
, &charsNeeded
);
409 charsNeeded
+= strlenW(header
) + strlenW(sep
);
411 charsNeeded
+= strlenW(trailer
) + strlenW(sep
);
412 if (charsNeeded
<= *pcchString
)
414 LPWSTR ptr
= pszString
;
415 DWORD size
= charsNeeded
;
419 strcpyW(ptr
, header
);
424 encodeBase64W(pbBinary
, cbBinary
, sep
, ptr
, &size
);
428 strcpyW(ptr
, trailer
);
432 *pcchString
= charsNeeded
- 1;
436 *pcchString
= charsNeeded
;
437 SetLastError(ERROR_INSUFFICIENT_BUFFER
);
441 *pcchString
= charsNeeded
;
445 BOOL WINAPI
CryptBinaryToStringW(const BYTE
*pbBinary
,
446 DWORD cbBinary
, DWORD dwFlags
, LPWSTR pszString
, DWORD
*pcchString
)
448 BinaryToStringWFunc encoder
= NULL
;
450 TRACE("(%p, %d, %08x, %p, %p)\n", pbBinary
, cbBinary
, dwFlags
, pszString
,
455 SetLastError(ERROR_INVALID_PARAMETER
);
460 SetLastError(ERROR_INVALID_PARAMETER
);
464 switch (dwFlags
& 0x0fffffff)
466 case CRYPT_STRING_BASE64
:
467 case CRYPT_STRING_BASE64HEADER
:
468 case CRYPT_STRING_BASE64REQUESTHEADER
:
469 case CRYPT_STRING_BASE64X509CRLHEADER
:
470 encoder
= BinaryToBase64W
;
472 case CRYPT_STRING_BINARY
:
473 case CRYPT_STRING_HEX
:
474 case CRYPT_STRING_HEXASCII
:
475 case CRYPT_STRING_HEXADDR
:
476 case CRYPT_STRING_HEXASCIIADDR
:
477 FIXME("Unimplemented type %d\n", dwFlags
& 0x0fffffff);
480 SetLastError(ERROR_INVALID_PARAMETER
);
483 return encoder(pbBinary
, cbBinary
, dwFlags
, pszString
, pcchString
);
486 #define BASE64_DECODE_PADDING 0x100
487 #define BASE64_DECODE_WHITESPACE 0x200
488 #define BASE64_DECODE_INVALID 0x300
490 static inline int decodeBase64Byte(int c
)
492 int ret
= BASE64_DECODE_INVALID
;
494 if (c
>= 'A' && c
<= 'Z')
496 else if (c
>= 'a' && c
<= 'z')
498 else if (c
>= '0' && c
<= '9')
505 ret
= BASE64_DECODE_PADDING
;
506 else if (c
== ' ' || c
== '\t' || c
== '\r' || c
== '\n')
507 ret
= BASE64_DECODE_WHITESPACE
;
511 /* Unlike CryptStringToBinaryA, cchString is guaranteed to be the length of the
514 typedef LONG (*StringToBinaryAFunc
)(LPCSTR pszString
, DWORD cchString
,
515 BYTE
*pbBinary
, DWORD
*pcbBinary
, DWORD
*pdwSkip
, DWORD
*pdwFlags
);
517 static LONG
Base64ToBinary(const void* pszString
, BOOL wide
, DWORD cchString
,
518 BYTE
*pbBinary
, DWORD
*pcbBinary
, DWORD
*pdwSkip
, DWORD
*pdwFlags
)
520 DWORD cbIn
, cbValid
, cbOut
, hasPadding
;
522 for (cbIn
= cbValid
= cbOut
= hasPadding
= 0; cbIn
< cchString
; ++cbIn
)
524 int c
= wide
? (int)((WCHAR
*)pszString
)[cbIn
] : (int)((char*)pszString
)[cbIn
];
525 int d
= decodeBase64Byte(c
);
526 if (d
== BASE64_DECODE_INVALID
)
528 if (d
== BASE64_DECODE_WHITESPACE
)
531 /* When padding starts, data is not acceptable */
532 if (hasPadding
&& d
!= BASE64_DECODE_PADDING
)
535 /* Padding after a full block (like "VVVV=") is ok and stops decoding */
536 if (d
== BASE64_DECODE_PADDING
&& (cbValid
& 3) == 0)
541 if (d
== BASE64_DECODE_PADDING
)
544 /* When padding reaches a full block, stop decoding */
545 if ((cbValid
& 3) == 0)
550 /* cbOut is incremented in the 4-char block as follows: "1-23" */
551 if ((cbValid
& 3) != 2)
554 /* Fail if the block has bad padding; omitting padding is fine */
555 if ((cbValid
& 3) != 0 && hasPadding
)
557 /* Check available buffer size */
558 if (pbBinary
&& *pcbBinary
&& cbOut
> *pcbBinary
)
560 /* Convert the data; this step depends on the validity checks above! */
561 if (pbBinary
) for (cbIn
= cbValid
= cbOut
= 0; cbIn
< cchString
; ++cbIn
)
563 int c
= wide
? (int)((WCHAR
*)pszString
)[cbIn
] : (int)((char*)pszString
)[cbIn
];
564 int d
= decodeBase64Byte(c
);
565 if (d
== BASE64_DECODE_WHITESPACE
)
567 if (d
== BASE64_DECODE_PADDING
)
569 block
[cbValid
& 3] = d
;
571 switch (cbValid
& 3) {
573 pbBinary
[cbOut
++] = (block
[0] << 2);
576 pbBinary
[cbOut
-1] = (block
[0] << 2) | (block
[1] >> 4);
579 pbBinary
[cbOut
++] = (block
[1] << 4) | (block
[2] >> 2);
582 pbBinary
[cbOut
++] = (block
[2] << 6) | (block
[3] >> 0);
590 *pdwFlags
= CRYPT_STRING_BASE64
;
591 return ERROR_SUCCESS
;
593 return ERROR_INSUFFICIENT_BUFFER
;
596 return ERROR_INVALID_DATA
;
599 static LONG
Base64ToBinaryA(LPCSTR pszString
, DWORD cchString
,
600 BYTE
*pbBinary
, DWORD
*pcbBinary
, DWORD
*pdwSkip
, DWORD
*pdwFlags
)
602 return Base64ToBinary(pszString
, FALSE
, cchString
, pbBinary
, pcbBinary
, pdwSkip
, pdwFlags
);
605 static LONG
Base64WithHeaderAndTrailerToBinaryA(LPCSTR pszString
,
606 DWORD cchString
, BYTE
*pbBinary
,
607 DWORD
*pcbBinary
, DWORD
*pdwSkip
)
610 LPCSTR header
= CERT_HEADER_START
;
611 LPCSTR trailer
= CERT_TRAILER_START
;
615 LPCSTR trailerBegins
;
618 if ((strlen(header
) + strlen(trailer
)) > cchString
)
620 return ERROR_INVALID_DATA
;
623 if (!(headerBegins
= strstr(pszString
, header
)))
625 TRACE("Can't find %s in %s.\n", header
, pszString
);
626 return ERROR_INVALID_DATA
;
629 dataBegins
= headerBegins
+ strlen(header
);
630 if (!(dataBegins
= strstr(dataBegins
, CERT_DELIMITER
)))
632 return ERROR_INVALID_DATA
;
634 dataBegins
+= strlen(CERT_DELIMITER
);
635 if (*dataBegins
== '\r') dataBegins
++;
636 if (*dataBegins
== '\n') dataBegins
++;
638 if (!(trailerBegins
= strstr(dataBegins
, trailer
)))
640 return ERROR_INVALID_DATA
;
642 if (*(trailerBegins
-1) == '\n') trailerBegins
--;
643 if (*(trailerBegins
-1) == '\r') trailerBegins
--;
646 *pdwSkip
= headerBegins
- pszString
;
648 dataLength
= trailerBegins
- dataBegins
;
650 ret
= Base64ToBinaryA(dataBegins
, dataLength
, pbBinary
, pcbBinary
, NULL
,
656 static LONG
Base64HeaderToBinaryA(LPCSTR pszString
, DWORD cchString
,
657 BYTE
*pbBinary
, DWORD
*pcbBinary
, DWORD
*pdwSkip
, DWORD
*pdwFlags
)
659 LONG ret
= Base64WithHeaderAndTrailerToBinaryA(pszString
, cchString
,
660 pbBinary
, pcbBinary
, pdwSkip
);
662 if (!ret
&& pdwFlags
)
663 *pdwFlags
= CRYPT_STRING_BASE64HEADER
;
667 static LONG
Base64RequestHeaderToBinaryA(LPCSTR pszString
, DWORD cchString
,
668 BYTE
*pbBinary
, DWORD
*pcbBinary
, DWORD
*pdwSkip
, DWORD
*pdwFlags
)
670 LONG ret
= Base64WithHeaderAndTrailerToBinaryA(pszString
, cchString
,
671 pbBinary
, pcbBinary
, pdwSkip
);
673 if (!ret
&& pdwFlags
)
674 *pdwFlags
= CRYPT_STRING_BASE64REQUESTHEADER
;
678 static LONG
Base64X509HeaderToBinaryA(LPCSTR pszString
, DWORD cchString
,
679 BYTE
*pbBinary
, DWORD
*pcbBinary
, DWORD
*pdwSkip
, DWORD
*pdwFlags
)
681 LONG ret
= Base64WithHeaderAndTrailerToBinaryA(pszString
, cchString
,
682 pbBinary
, pcbBinary
, pdwSkip
);
684 if (!ret
&& pdwFlags
)
685 *pdwFlags
= CRYPT_STRING_BASE64X509CRLHEADER
;
689 static LONG
Base64AnyToBinaryA(LPCSTR pszString
, DWORD cchString
,
690 BYTE
*pbBinary
, DWORD
*pcbBinary
, DWORD
*pdwSkip
, DWORD
*pdwFlags
)
694 ret
= Base64HeaderToBinaryA(pszString
, cchString
, pbBinary
, pcbBinary
,
696 if (ret
== ERROR_INVALID_DATA
)
697 ret
= Base64ToBinaryA(pszString
, cchString
, pbBinary
, pcbBinary
,
702 static LONG
DecodeBinaryToBinaryA(LPCSTR pszString
, DWORD cchString
,
703 BYTE
*pbBinary
, DWORD
*pcbBinary
, DWORD
*pdwSkip
, DWORD
*pdwFlags
)
705 LONG ret
= ERROR_SUCCESS
;
707 if (*pcbBinary
< cchString
)
710 *pcbBinary
= cchString
;
713 ret
= ERROR_INSUFFICIENT_BUFFER
;
714 *pcbBinary
= cchString
;
720 memcpy(pbBinary
, pszString
, cchString
);
721 *pcbBinary
= cchString
;
726 static LONG
DecodeAnyA(LPCSTR pszString
, DWORD cchString
,
727 BYTE
*pbBinary
, DWORD
*pcbBinary
, DWORD
*pdwSkip
, DWORD
*pdwFlags
)
731 ret
= Base64HeaderToBinaryA(pszString
, cchString
, pbBinary
, pcbBinary
,
733 if (ret
== ERROR_INVALID_DATA
)
734 ret
= Base64ToBinaryA(pszString
, cchString
, pbBinary
, pcbBinary
,
736 if (ret
== ERROR_INVALID_DATA
)
737 ret
= DecodeBinaryToBinaryA(pszString
, cchString
, pbBinary
, pcbBinary
,
742 BOOL WINAPI
CryptStringToBinaryA(LPCSTR pszString
,
743 DWORD cchString
, DWORD dwFlags
, BYTE
*pbBinary
, DWORD
*pcbBinary
,
744 DWORD
*pdwSkip
, DWORD
*pdwFlags
)
746 StringToBinaryAFunc decoder
;
749 TRACE("(%s, %d, %08x, %p, %p, %p, %p)\n", debugstr_a(pszString
),
750 cchString
, dwFlags
, pbBinary
, pcbBinary
, pdwSkip
, pdwFlags
);
754 SetLastError(ERROR_INVALID_PARAMETER
);
757 /* Only the bottom byte contains valid types */
758 if (dwFlags
& 0xfffffff0)
760 SetLastError(ERROR_INVALID_DATA
);
765 case CRYPT_STRING_BASE64_ANY
:
766 decoder
= Base64AnyToBinaryA
;
768 case CRYPT_STRING_BASE64
:
769 decoder
= Base64ToBinaryA
;
771 case CRYPT_STRING_BASE64HEADER
:
772 decoder
= Base64HeaderToBinaryA
;
774 case CRYPT_STRING_BASE64REQUESTHEADER
:
775 decoder
= Base64RequestHeaderToBinaryA
;
777 case CRYPT_STRING_BASE64X509CRLHEADER
:
778 decoder
= Base64X509HeaderToBinaryA
;
780 case CRYPT_STRING_BINARY
:
781 decoder
= DecodeBinaryToBinaryA
;
783 case CRYPT_STRING_ANY
:
784 decoder
= DecodeAnyA
;
786 case CRYPT_STRING_HEX
:
787 case CRYPT_STRING_HEXASCII
:
788 case CRYPT_STRING_HEXADDR
:
789 case CRYPT_STRING_HEXASCIIADDR
:
790 FIXME("Unimplemented type %d\n", dwFlags
& 0x7fffffff);
793 SetLastError(ERROR_INVALID_PARAMETER
);
797 cchString
= strlen(pszString
);
798 ret
= decoder(pszString
, cchString
, pbBinary
, pcbBinary
, pdwSkip
, pdwFlags
);
801 return ret
== ERROR_SUCCESS
;
804 /* Unlike CryptStringToBinaryW, cchString is guaranteed to be the length of the
807 typedef LONG (*StringToBinaryWFunc
)(LPCWSTR pszString
, DWORD cchString
,
808 BYTE
*pbBinary
, DWORD
*pcbBinary
, DWORD
*pdwSkip
, DWORD
*pdwFlags
);
810 static LONG
Base64ToBinaryW(LPCWSTR pszString
, DWORD cchString
,
811 BYTE
*pbBinary
, DWORD
*pcbBinary
, DWORD
*pdwSkip
, DWORD
*pdwFlags
)
813 return Base64ToBinary(pszString
, TRUE
, cchString
, pbBinary
, pcbBinary
, pdwSkip
, pdwFlags
);
816 static LONG
Base64WithHeaderAndTrailerToBinaryW(LPCWSTR pszString
,
817 DWORD cchString
, BYTE
*pbBinary
,
818 DWORD
*pcbBinary
, DWORD
*pdwSkip
)
821 LPCWSTR header
= CERT_HEADER_START_W
;
822 LPCWSTR trailer
= CERT_TRAILER_START_W
;
824 LPCWSTR headerBegins
;
826 LPCWSTR trailerBegins
;
829 if ((strlenW(header
) + strlenW(trailer
)) > cchString
)
831 return ERROR_INVALID_DATA
;
834 if (!(headerBegins
= strstrW(pszString
, header
)))
836 TRACE("Can't find %s in %s.\n", debugstr_w(header
), debugstr_w(pszString
));
837 return ERROR_INVALID_DATA
;
840 dataBegins
= headerBegins
+ strlenW(header
);
841 if (!(dataBegins
= strstrW(dataBegins
, CERT_DELIMITER_W
)))
843 return ERROR_INVALID_DATA
;
845 dataBegins
+= strlenW(CERT_DELIMITER_W
);
846 if (*dataBegins
== '\r') dataBegins
++;
847 if (*dataBegins
== '\n') dataBegins
++;
849 if (!(trailerBegins
= strstrW(dataBegins
, trailer
)))
851 return ERROR_INVALID_DATA
;
853 if (*(trailerBegins
-1) == '\n') trailerBegins
--;
854 if (*(trailerBegins
-1) == '\r') trailerBegins
--;
857 *pdwSkip
= headerBegins
- pszString
;
859 dataLength
= trailerBegins
- dataBegins
;
861 ret
= Base64ToBinaryW(dataBegins
, dataLength
, pbBinary
, pcbBinary
, NULL
,
867 static LONG
Base64HeaderToBinaryW(LPCWSTR pszString
, DWORD cchString
,
868 BYTE
*pbBinary
, DWORD
*pcbBinary
, DWORD
*pdwSkip
, DWORD
*pdwFlags
)
870 LONG ret
= Base64WithHeaderAndTrailerToBinaryW(pszString
, cchString
,
871 pbBinary
, pcbBinary
, pdwSkip
);
873 if (!ret
&& pdwFlags
)
874 *pdwFlags
= CRYPT_STRING_BASE64HEADER
;
878 static LONG
Base64RequestHeaderToBinaryW(LPCWSTR pszString
, DWORD cchString
,
879 BYTE
*pbBinary
, DWORD
*pcbBinary
, DWORD
*pdwSkip
, DWORD
*pdwFlags
)
881 LONG ret
= Base64WithHeaderAndTrailerToBinaryW(pszString
, cchString
,
882 pbBinary
, pcbBinary
, pdwSkip
);
884 if (!ret
&& pdwFlags
)
885 *pdwFlags
= CRYPT_STRING_BASE64REQUESTHEADER
;
889 static LONG
Base64X509HeaderToBinaryW(LPCWSTR pszString
, DWORD cchString
,
890 BYTE
*pbBinary
, DWORD
*pcbBinary
, DWORD
*pdwSkip
, DWORD
*pdwFlags
)
892 LONG ret
= Base64WithHeaderAndTrailerToBinaryW(pszString
, cchString
,
893 pbBinary
, pcbBinary
, pdwSkip
);
895 if (!ret
&& pdwFlags
)
896 *pdwFlags
= CRYPT_STRING_BASE64X509CRLHEADER
;
900 static LONG
Base64AnyToBinaryW(LPCWSTR pszString
, DWORD cchString
,
901 BYTE
*pbBinary
, DWORD
*pcbBinary
, DWORD
*pdwSkip
, DWORD
*pdwFlags
)
905 ret
= Base64HeaderToBinaryW(pszString
, cchString
, pbBinary
, pcbBinary
,
907 if (ret
== ERROR_INVALID_DATA
)
908 ret
= Base64ToBinaryW(pszString
, cchString
, pbBinary
, pcbBinary
,
913 static LONG
DecodeBinaryToBinaryW(LPCWSTR pszString
, DWORD cchString
,
914 BYTE
*pbBinary
, DWORD
*pcbBinary
, DWORD
*pdwSkip
, DWORD
*pdwFlags
)
916 LONG ret
= ERROR_SUCCESS
;
918 if (*pcbBinary
< cchString
)
921 *pcbBinary
= cchString
;
924 ret
= ERROR_INSUFFICIENT_BUFFER
;
925 *pcbBinary
= cchString
;
931 memcpy(pbBinary
, pszString
, cchString
* sizeof(WCHAR
));
932 *pcbBinary
= cchString
* sizeof(WCHAR
);
937 static LONG
DecodeAnyW(LPCWSTR pszString
, DWORD cchString
,
938 BYTE
*pbBinary
, DWORD
*pcbBinary
, DWORD
*pdwSkip
, DWORD
*pdwFlags
)
942 ret
= Base64HeaderToBinaryW(pszString
, cchString
, pbBinary
, pcbBinary
,
944 if (ret
== ERROR_INVALID_DATA
)
945 ret
= Base64ToBinaryW(pszString
, cchString
, pbBinary
, pcbBinary
,
947 if (ret
== ERROR_INVALID_DATA
)
948 ret
= DecodeBinaryToBinaryW(pszString
, cchString
, pbBinary
, pcbBinary
,
953 BOOL WINAPI
CryptStringToBinaryW(LPCWSTR pszString
,
954 DWORD cchString
, DWORD dwFlags
, BYTE
*pbBinary
, DWORD
*pcbBinary
,
955 DWORD
*pdwSkip
, DWORD
*pdwFlags
)
957 StringToBinaryWFunc decoder
;
960 TRACE("(%s, %d, %08x, %p, %p, %p, %p)\n", debugstr_w(pszString
),
961 cchString
, dwFlags
, pbBinary
, pcbBinary
, pdwSkip
, pdwFlags
);
965 SetLastError(ERROR_INVALID_PARAMETER
);
968 /* Only the bottom byte contains valid types */
969 if (dwFlags
& 0xfffffff0)
971 SetLastError(ERROR_INVALID_DATA
);
976 case CRYPT_STRING_BASE64_ANY
:
977 decoder
= Base64AnyToBinaryW
;
979 case CRYPT_STRING_BASE64
:
980 decoder
= Base64ToBinaryW
;
982 case CRYPT_STRING_BASE64HEADER
:
983 decoder
= Base64HeaderToBinaryW
;
985 case CRYPT_STRING_BASE64REQUESTHEADER
:
986 decoder
= Base64RequestHeaderToBinaryW
;
988 case CRYPT_STRING_BASE64X509CRLHEADER
:
989 decoder
= Base64X509HeaderToBinaryW
;
991 case CRYPT_STRING_BINARY
:
992 decoder
= DecodeBinaryToBinaryW
;
994 case CRYPT_STRING_ANY
:
995 decoder
= DecodeAnyW
;
997 case CRYPT_STRING_HEX
:
998 case CRYPT_STRING_HEXASCII
:
999 case CRYPT_STRING_HEXADDR
:
1000 case CRYPT_STRING_HEXASCIIADDR
:
1001 FIXME("Unimplemented type %d\n", dwFlags
& 0x7fffffff);
1004 SetLastError(ERROR_INVALID_PARAMETER
);
1008 cchString
= strlenW(pszString
);
1009 ret
= decoder(pszString
, cchString
, pbBinary
, pcbBinary
, pdwSkip
, pdwFlags
);
1012 return ret
== ERROR_SUCCESS
;