crypt32: CryptStringToBinary accepts any header.
[wine.git] / dlls / crypt32 / base64.c
blob6c2e25293230764a97728efd16362a258389e5d9
1 /*
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
22 #include <stdarg.h>
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winerror.h"
26 #include "wincrypt.h"
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',' ',0 };
51 static const WCHAR CERT_TRAILER_START_W[] = {
52 '-','-','-','-','-','E','N','D',' ','C','E','R','T','I','F','I','C','A','T',
53 'E','-','-','-','-','-',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','-','-',
65 '-','-','-',0 };
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)
78 BOOL ret = TRUE;
80 if (*pcchString < cbBinary)
82 if (!pszString)
83 *pcchString = cbBinary;
84 else
86 SetLastError(ERROR_INSUFFICIENT_BUFFER);
87 *pcchString = cbBinary;
88 ret = FALSE;
91 else
93 if (cbBinary)
94 memcpy(pszString, pbBinary, cbBinary);
95 *pcchString = cbBinary;
97 return ret;
100 static LONG encodeBase64A(const BYTE *in_buf, int in_len, LPCSTR sep,
101 char* out_buf, DWORD *out_len)
103 int div, i;
104 const BYTE *d = in_buf;
105 int bytes = (in_len*8 + 5)/6, pad_bytes = (bytes % 4) ? 4 - (bytes % 4) : 0;
106 DWORD needed;
107 LPSTR ptr;
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)
115 *out_len = needed;
116 return ERROR_INSUFFICIENT_BUFFER;
118 else
119 *out_len = needed;
121 /* Three bytes of input give 4 chars of output */
122 div = in_len / 3;
124 ptr = out_buf;
125 i = 0;
126 while (div > 0)
128 if (i && i % 64 == 0)
130 strcpy(ptr, sep);
131 ptr += strlen(sep);
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];
143 i += 4;
144 d += 3;
145 div--;
148 switch(pad_bytes)
150 case 1:
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
157 * two zeroes */
158 *ptr++ = b64[ ((d[1] << 2) & 0x3c) ];
159 /* fourth char is a = to indicate one byte of padding */
160 *ptr++ = '=';
161 break;
162 case 2:
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
166 * four zeroes*/
167 *ptr++ = b64[ ((d[0] << 4) & 0x30)];
168 /* third char is = to indicate padding */
169 *ptr++ = '=';
170 /* fourth char is = to indicate padding */
171 *ptr++ = '=';
172 break;
174 strcpy(ptr, sep);
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";
183 BOOL ret = TRUE;
184 LPCSTR header = NULL, trailer = NULL, sep;
185 DWORD charsNeeded;
187 if (dwFlags & CRYPT_STRING_NOCR)
188 sep = lf;
189 else if (dwFlags & CRYPT_STRING_NOCRLF)
190 sep = "";
191 else
192 sep = crlf;
193 switch (dwFlags & 0x0fffffff)
195 case CRYPT_STRING_BASE64:
196 /* no header or footer */
197 break;
198 case CRYPT_STRING_BASE64HEADER:
199 header = CERT_HEADER;
200 trailer = CERT_TRAILER;
201 break;
202 case CRYPT_STRING_BASE64REQUESTHEADER:
203 header = CERT_REQUEST_HEADER;
204 trailer = CERT_REQUEST_TRAILER;
205 break;
206 case CRYPT_STRING_BASE64X509CRLHEADER:
207 header = X509_HEADER;
208 trailer = X509_TRAILER;
209 break;
212 charsNeeded = 0;
213 encodeBase64A(pbBinary, cbBinary, sep, NULL, &charsNeeded);
214 if (header)
215 charsNeeded += strlen(header) + strlen(sep);
216 if (trailer)
217 charsNeeded += strlen(trailer) + strlen(sep);
218 if (charsNeeded <= *pcchString)
220 LPSTR ptr = pszString;
221 DWORD size = charsNeeded;
223 if (header)
225 strcpy(ptr, header);
226 ptr += strlen(ptr);
227 strcpy(ptr, sep);
228 ptr += strlen(sep);
230 encodeBase64A(pbBinary, cbBinary, sep, ptr, &size);
231 ptr += size - 1;
232 if (trailer)
234 strcpy(ptr, trailer);
235 ptr += strlen(ptr);
236 strcpy(ptr, sep);
238 *pcchString = charsNeeded - 1;
240 else if (pszString)
242 *pcchString = charsNeeded;
243 SetLastError(ERROR_INSUFFICIENT_BUFFER);
244 ret = FALSE;
246 else
247 *pcchString = charsNeeded;
248 return ret;
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,
257 pcchString);
259 if (!pbBinary)
261 SetLastError(ERROR_INVALID_PARAMETER);
262 return FALSE;
264 if (!pcchString)
266 SetLastError(ERROR_INVALID_PARAMETER);
267 return FALSE;
270 switch (dwFlags & 0x0fffffff)
272 case CRYPT_STRING_BINARY:
273 encoder = EncodeBinaryToBinaryA;
274 break;
275 case CRYPT_STRING_BASE64:
276 case CRYPT_STRING_BASE64HEADER:
277 case CRYPT_STRING_BASE64REQUESTHEADER:
278 case CRYPT_STRING_BASE64X509CRLHEADER:
279 encoder = BinaryToBase64A;
280 break;
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);
286 /* fall through */
287 default:
288 SetLastError(ERROR_INVALID_PARAMETER);
289 return FALSE;
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)
297 int div, i;
298 const BYTE *d = in_buf;
299 int bytes = (in_len*8 + 5)/6, pad_bytes = (bytes % 4) ? 4 - (bytes % 4) : 0;
300 DWORD needed;
301 LPWSTR ptr;
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)
309 *out_len = needed;
310 return ERROR_INSUFFICIENT_BUFFER;
312 else
313 *out_len = needed;
315 /* Three bytes of input give 4 chars of output */
316 div = in_len / 3;
318 ptr = out_buf;
319 i = 0;
320 while (div > 0)
322 if (i && i % 64 == 0)
324 strcpyW(ptr, sep);
325 ptr += strlenW(sep);
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];
337 i += 4;
338 d += 3;
339 div--;
342 switch(pad_bytes)
344 case 1:
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
351 * two zeroes */
352 *ptr++ = b64[ ((d[1] << 2) & 0x3c) ];
353 /* fourth char is a = to indicate one byte of padding */
354 *ptr++ = '=';
355 break;
356 case 2:
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
360 * four zeroes*/
361 *ptr++ = b64[ ((d[0] << 4) & 0x30)];
362 /* third char is = to indicate padding */
363 *ptr++ = '=';
364 /* fourth char is = to indicate padding */
365 *ptr++ = '=';
366 break;
368 strcpyW(ptr, sep);
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};
377 BOOL ret = TRUE;
378 LPCWSTR header = NULL, trailer = NULL, sep;
379 DWORD charsNeeded;
381 if (dwFlags & CRYPT_STRING_NOCR)
382 sep = lf;
383 else if (dwFlags & CRYPT_STRING_NOCRLF)
384 sep = empty;
385 else
386 sep = crlf;
387 switch (dwFlags & 0x0fffffff)
389 case CRYPT_STRING_BASE64:
390 /* no header or footer */
391 break;
392 case CRYPT_STRING_BASE64HEADER:
393 header = CERT_HEADER_W;
394 trailer = CERT_TRAILER_W;
395 break;
396 case CRYPT_STRING_BASE64REQUESTHEADER:
397 header = CERT_REQUEST_HEADER_W;
398 trailer = CERT_REQUEST_TRAILER_W;
399 break;
400 case CRYPT_STRING_BASE64X509CRLHEADER:
401 header = X509_HEADER_W;
402 trailer = X509_TRAILER_W;
403 break;
406 charsNeeded = 0;
407 encodeBase64W(pbBinary, cbBinary, sep, NULL, &charsNeeded);
408 if (header)
409 charsNeeded += strlenW(header) + strlenW(sep);
410 if (trailer)
411 charsNeeded += strlenW(trailer) + strlenW(sep);
412 if (charsNeeded <= *pcchString)
414 LPWSTR ptr = pszString;
415 DWORD size = charsNeeded;
417 if (header)
419 strcpyW(ptr, header);
420 ptr += strlenW(ptr);
421 strcpyW(ptr, sep);
422 ptr += strlenW(sep);
424 encodeBase64W(pbBinary, cbBinary, sep, ptr, &size);
425 ptr += size - 1;
426 if (trailer)
428 strcpyW(ptr, trailer);
429 ptr += strlenW(ptr);
430 strcpyW(ptr, sep);
432 *pcchString = charsNeeded - 1;
434 else if (pszString)
436 *pcchString = charsNeeded;
437 SetLastError(ERROR_INSUFFICIENT_BUFFER);
438 ret = FALSE;
440 else
441 *pcchString = charsNeeded;
442 return ret;
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,
451 pcchString);
453 if (!pbBinary)
455 SetLastError(ERROR_INVALID_PARAMETER);
456 return FALSE;
458 if (!pcchString)
460 SetLastError(ERROR_INVALID_PARAMETER);
461 return FALSE;
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;
471 break;
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);
478 /* fall through */
479 default:
480 SetLastError(ERROR_INVALID_PARAMETER);
481 return FALSE;
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')
495 ret = c - 'A';
496 else if (c >= 'a' && c <= 'z')
497 ret = c - 'a' + 26;
498 else if (c >= '0' && c <= '9')
499 ret = c - '0' + 52;
500 else if (c == '+')
501 ret = 62;
502 else if (c == '/')
503 ret = 63;
504 else if (c == '=')
505 ret = BASE64_DECODE_PADDING;
506 else if (c == ' ' || c == '\t' || c == '\r' || c == '\n')
507 ret = BASE64_DECODE_WHITESPACE;
508 return ret;
511 /* Unlike CryptStringToBinaryA, cchString is guaranteed to be the length of the
512 * string to convert.
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;
521 BYTE block[4];
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)
527 goto invalid;
528 if (d == BASE64_DECODE_WHITESPACE)
529 continue;
531 /* When padding starts, data is not acceptable */
532 if (hasPadding && d != BASE64_DECODE_PADDING)
533 goto invalid;
535 /* Padding after a full block (like "VVVV=") is ok and stops decoding */
536 if (d == BASE64_DECODE_PADDING && (cbValid & 3) == 0)
537 break;
539 cbValid += 1;
541 if (d == BASE64_DECODE_PADDING)
543 hasPadding = 1;
544 /* When padding reaches a full block, stop decoding */
545 if ((cbValid & 3) == 0)
546 break;
547 continue;
550 /* cbOut is incremented in the 4-char block as follows: "1-23" */
551 if ((cbValid & 3) != 2)
552 cbOut += 1;
554 /* Fail if the block has bad padding; omitting padding is fine */
555 if ((cbValid & 3) != 0 && hasPadding)
556 goto invalid;
557 /* Check available buffer size */
558 if (pbBinary && *pcbBinary && cbOut > *pcbBinary)
559 goto overflow;
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)
566 continue;
567 if (d == BASE64_DECODE_PADDING)
568 break;
569 block[cbValid & 3] = d;
570 cbValid += 1;
571 switch (cbValid & 3) {
572 case 1:
573 pbBinary[cbOut++] = (block[0] << 2);
574 break;
575 case 2:
576 pbBinary[cbOut-1] = (block[0] << 2) | (block[1] >> 4);
577 break;
578 case 3:
579 pbBinary[cbOut++] = (block[1] << 4) | (block[2] >> 2);
580 break;
581 case 0:
582 pbBinary[cbOut++] = (block[2] << 6) | (block[3] >> 0);
583 break;
586 *pcbBinary = cbOut;
587 if (pdwSkip)
588 *pdwSkip = 0;
589 if (pdwFlags)
590 *pdwFlags = CRYPT_STRING_BASE64;
591 return ERROR_SUCCESS;
592 overflow:
593 return ERROR_INSUFFICIENT_BUFFER;
594 invalid:
595 *pcbBinary = cbOut;
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, BOOL exactHeaderAndTrailerMatch)
609 LONG ret;
610 LPCSTR header = CERT_HEADER_START;
611 LPCSTR trailer = CERT_TRAILER_START;
613 LPCSTR headerBegins;
614 LPCSTR dataBegins;
615 LPCSTR trailerBegins;
616 size_t dataLength;
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 (!exactHeaderAndTrailerMatch)
632 if ((dataBegins = strstr(dataBegins, CERT_DELIMITER)))
634 dataBegins += strlen(CERT_DELIMITER);
636 else
638 return ERROR_INVALID_DATA;
641 if (*dataBegins == '\r') dataBegins++;
642 if (*dataBegins == '\n') dataBegins++;
644 if (exactHeaderAndTrailerMatch)
646 trailerBegins = pszString + cchString - strlen(trailer);
647 if (pszString[cchString - 1] == '\n') trailerBegins--;
648 if (pszString[cchString - 2] == '\r') trailerBegins--;
650 if (*(trailerBegins-1) == '\n') trailerBegins--;
651 if (*(trailerBegins-1) == '\r') trailerBegins--;
653 if (!strncmp(trailerBegins, trailer, strlen(trailer)))
655 return ERROR_INVALID_DATA;
658 else
660 if (!(trailerBegins = strstr(dataBegins, trailer)))
662 return ERROR_INVALID_DATA;
664 if (*(trailerBegins-1) == '\n') trailerBegins--;
665 if (*(trailerBegins-1) == '\r') trailerBegins--;
668 if (pdwSkip)
669 *pdwSkip = headerBegins - pszString;
671 dataLength = trailerBegins - dataBegins;
673 ret = Base64ToBinaryA(dataBegins, dataLength, pbBinary, pcbBinary, NULL,
674 NULL);
676 return ret;
679 static LONG Base64HeaderToBinaryA(LPCSTR pszString, DWORD cchString,
680 BYTE *pbBinary, DWORD *pcbBinary, DWORD *pdwSkip, DWORD *pdwFlags)
682 LONG ret = Base64WithHeaderAndTrailerToBinaryA(pszString, cchString,
683 pbBinary, pcbBinary, pdwSkip, FALSE);
685 if (!ret && pdwFlags)
686 *pdwFlags = CRYPT_STRING_BASE64HEADER;
687 return ret;
690 static LONG Base64RequestHeaderToBinaryA(LPCSTR pszString, DWORD cchString,
691 BYTE *pbBinary, DWORD *pcbBinary, DWORD *pdwSkip, DWORD *pdwFlags)
693 LONG ret = Base64WithHeaderAndTrailerToBinaryA(pszString, cchString,
694 pbBinary, pcbBinary, pdwSkip, FALSE);
696 if (!ret && pdwFlags)
697 *pdwFlags = CRYPT_STRING_BASE64REQUESTHEADER;
698 return ret;
701 static LONG Base64X509HeaderToBinaryA(LPCSTR pszString, DWORD cchString,
702 BYTE *pbBinary, DWORD *pcbBinary, DWORD *pdwSkip, DWORD *pdwFlags)
704 LONG ret = Base64WithHeaderAndTrailerToBinaryA(pszString, cchString,
705 pbBinary, pcbBinary, pdwSkip, FALSE);
707 if (!ret && pdwFlags)
708 *pdwFlags = CRYPT_STRING_BASE64X509CRLHEADER;
709 return ret;
712 static LONG Base64AnyToBinaryA(LPCSTR pszString, DWORD cchString,
713 BYTE *pbBinary, DWORD *pcbBinary, DWORD *pdwSkip, DWORD *pdwFlags)
715 LONG ret;
717 ret = Base64HeaderToBinaryA(pszString, cchString, pbBinary, pcbBinary,
718 pdwSkip, pdwFlags);
719 if (ret == ERROR_INVALID_DATA)
720 ret = Base64ToBinaryA(pszString, cchString, pbBinary, pcbBinary,
721 pdwSkip, pdwFlags);
722 return ret;
725 static LONG DecodeBinaryToBinaryA(LPCSTR pszString, DWORD cchString,
726 BYTE *pbBinary, DWORD *pcbBinary, DWORD *pdwSkip, DWORD *pdwFlags)
728 LONG ret = ERROR_SUCCESS;
730 if (*pcbBinary < cchString)
732 if (!pbBinary)
733 *pcbBinary = cchString;
734 else
736 ret = ERROR_INSUFFICIENT_BUFFER;
737 *pcbBinary = cchString;
740 else
742 if (cchString)
743 memcpy(pbBinary, pszString, cchString);
744 *pcbBinary = cchString;
746 return ret;
749 static LONG DecodeAnyA(LPCSTR pszString, DWORD cchString,
750 BYTE *pbBinary, DWORD *pcbBinary, DWORD *pdwSkip, DWORD *pdwFlags)
752 LONG ret;
754 ret = Base64HeaderToBinaryA(pszString, cchString, pbBinary, pcbBinary,
755 pdwSkip, pdwFlags);
756 if (ret == ERROR_INVALID_DATA)
757 ret = Base64ToBinaryA(pszString, cchString, pbBinary, pcbBinary,
758 pdwSkip, pdwFlags);
759 if (ret == ERROR_INVALID_DATA)
760 ret = DecodeBinaryToBinaryA(pszString, cchString, pbBinary, pcbBinary,
761 pdwSkip, pdwFlags);
762 return ret;
765 BOOL WINAPI CryptStringToBinaryA(LPCSTR pszString,
766 DWORD cchString, DWORD dwFlags, BYTE *pbBinary, DWORD *pcbBinary,
767 DWORD *pdwSkip, DWORD *pdwFlags)
769 StringToBinaryAFunc decoder;
770 LONG ret;
772 TRACE("(%s, %d, %08x, %p, %p, %p, %p)\n", debugstr_a(pszString),
773 cchString, dwFlags, pbBinary, pcbBinary, pdwSkip, pdwFlags);
775 if (!pszString)
777 SetLastError(ERROR_INVALID_PARAMETER);
778 return FALSE;
780 /* Only the bottom byte contains valid types */
781 if (dwFlags & 0xfffffff0)
783 SetLastError(ERROR_INVALID_DATA);
784 return FALSE;
786 switch (dwFlags)
788 case CRYPT_STRING_BASE64_ANY:
789 decoder = Base64AnyToBinaryA;
790 break;
791 case CRYPT_STRING_BASE64:
792 decoder = Base64ToBinaryA;
793 break;
794 case CRYPT_STRING_BASE64HEADER:
795 decoder = Base64HeaderToBinaryA;
796 break;
797 case CRYPT_STRING_BASE64REQUESTHEADER:
798 decoder = Base64RequestHeaderToBinaryA;
799 break;
800 case CRYPT_STRING_BASE64X509CRLHEADER:
801 decoder = Base64X509HeaderToBinaryA;
802 break;
803 case CRYPT_STRING_BINARY:
804 decoder = DecodeBinaryToBinaryA;
805 break;
806 case CRYPT_STRING_ANY:
807 decoder = DecodeAnyA;
808 break;
809 case CRYPT_STRING_HEX:
810 case CRYPT_STRING_HEXASCII:
811 case CRYPT_STRING_HEXADDR:
812 case CRYPT_STRING_HEXASCIIADDR:
813 FIXME("Unimplemented type %d\n", dwFlags & 0x7fffffff);
814 /* fall through */
815 default:
816 SetLastError(ERROR_INVALID_PARAMETER);
817 return FALSE;
819 if (!cchString)
820 cchString = strlen(pszString);
821 ret = decoder(pszString, cchString, pbBinary, pcbBinary, pdwSkip, pdwFlags);
822 if (ret)
823 SetLastError(ret);
824 return ret == ERROR_SUCCESS;
827 /* Unlike CryptStringToBinaryW, cchString is guaranteed to be the length of the
828 * string to convert.
830 typedef LONG (*StringToBinaryWFunc)(LPCWSTR pszString, DWORD cchString,
831 BYTE *pbBinary, DWORD *pcbBinary, DWORD *pdwSkip, DWORD *pdwFlags);
833 static LONG Base64ToBinaryW(LPCWSTR pszString, DWORD cchString,
834 BYTE *pbBinary, DWORD *pcbBinary, DWORD *pdwSkip, DWORD *pdwFlags)
836 return Base64ToBinary(pszString, TRUE, cchString, pbBinary, pcbBinary, pdwSkip, pdwFlags);
839 static LONG Base64WithHeaderAndTrailerToBinaryW(LPCWSTR pszString,
840 DWORD cchString, BYTE *pbBinary,
841 DWORD *pcbBinary, DWORD *pdwSkip, BOOL exactHeaderAndTrailerMatch)
843 LONG ret;
844 LPCWSTR header = CERT_HEADER_START_W;
845 LPCWSTR trailer = CERT_TRAILER_START_W;
847 LPCWSTR headerBegins;
848 LPCWSTR dataBegins;
849 LPCWSTR trailerBegins;
850 size_t dataLength;
852 if ((strlenW(header) + strlenW(trailer)) > cchString)
854 return ERROR_INVALID_DATA;
857 if (!(headerBegins = strstrW(pszString, header)))
859 TRACE("Can't find %s in %s.\n", debugstr_w(header), debugstr_w(pszString));
860 return ERROR_INVALID_DATA;
863 dataBegins = headerBegins + strlenW(header);
864 if (!exactHeaderAndTrailerMatch)
866 if ((dataBegins = strstrW(dataBegins, CERT_DELIMITER_W)))
868 dataBegins += strlenW(CERT_DELIMITER_W);
870 else
872 return ERROR_INVALID_DATA;
875 if (*dataBegins == '\r') dataBegins++;
876 if (*dataBegins == '\n') dataBegins++;
878 if (exactHeaderAndTrailerMatch)
880 trailerBegins = pszString + cchString - strlenW(trailer);
881 if (pszString[cchString - 1] == '\n') trailerBegins--;
882 if (pszString[cchString - 2] == '\r') trailerBegins--;
884 if (*(trailerBegins-1) == '\n') trailerBegins--;
885 if (*(trailerBegins-1) == '\r') trailerBegins--;
887 if (!strncmpW(trailerBegins, trailer, strlenW(trailer)))
889 return ERROR_INVALID_DATA;
892 else
894 if (!(trailerBegins = strstrW(dataBegins, trailer)))
896 return ERROR_INVALID_DATA;
898 if (*(trailerBegins-1) == '\n') trailerBegins--;
899 if (*(trailerBegins-1) == '\r') trailerBegins--;
902 if (pdwSkip)
903 *pdwSkip = headerBegins - pszString;
905 dataLength = trailerBegins - dataBegins;
907 ret = Base64ToBinaryW(dataBegins, dataLength, pbBinary, pcbBinary, NULL,
908 NULL);
910 return ret;
913 static LONG Base64HeaderToBinaryW(LPCWSTR pszString, DWORD cchString,
914 BYTE *pbBinary, DWORD *pcbBinary, DWORD *pdwSkip, DWORD *pdwFlags)
916 LONG ret = Base64WithHeaderAndTrailerToBinaryW(pszString, cchString,
917 pbBinary, pcbBinary, pdwSkip, FALSE);
919 if (!ret && pdwFlags)
920 *pdwFlags = CRYPT_STRING_BASE64HEADER;
921 return ret;
924 static LONG Base64RequestHeaderToBinaryW(LPCWSTR pszString, DWORD cchString,
925 BYTE *pbBinary, DWORD *pcbBinary, DWORD *pdwSkip, DWORD *pdwFlags)
927 LONG ret = Base64WithHeaderAndTrailerToBinaryW(pszString, cchString,
928 pbBinary, pcbBinary, pdwSkip, FALSE);
930 if (!ret && pdwFlags)
931 *pdwFlags = CRYPT_STRING_BASE64REQUESTHEADER;
932 return ret;
935 static LONG Base64X509HeaderToBinaryW(LPCWSTR pszString, DWORD cchString,
936 BYTE *pbBinary, DWORD *pcbBinary, DWORD *pdwSkip, DWORD *pdwFlags)
938 LONG ret = Base64WithHeaderAndTrailerToBinaryW(pszString, cchString,
939 pbBinary, pcbBinary, pdwSkip, FALSE);
941 if (!ret && pdwFlags)
942 *pdwFlags = CRYPT_STRING_BASE64X509CRLHEADER;
943 return ret;
946 static LONG Base64AnyToBinaryW(LPCWSTR pszString, DWORD cchString,
947 BYTE *pbBinary, DWORD *pcbBinary, DWORD *pdwSkip, DWORD *pdwFlags)
949 LONG ret;
951 ret = Base64HeaderToBinaryW(pszString, cchString, pbBinary, pcbBinary,
952 pdwSkip, pdwFlags);
953 if (ret == ERROR_INVALID_DATA)
954 ret = Base64ToBinaryW(pszString, cchString, pbBinary, pcbBinary,
955 pdwSkip, pdwFlags);
956 return ret;
959 static LONG DecodeBinaryToBinaryW(LPCWSTR pszString, DWORD cchString,
960 BYTE *pbBinary, DWORD *pcbBinary, DWORD *pdwSkip, DWORD *pdwFlags)
962 LONG ret = ERROR_SUCCESS;
964 if (*pcbBinary < cchString)
966 if (!pbBinary)
967 *pcbBinary = cchString;
968 else
970 ret = ERROR_INSUFFICIENT_BUFFER;
971 *pcbBinary = cchString;
974 else
976 if (cchString)
977 memcpy(pbBinary, pszString, cchString * sizeof(WCHAR));
978 *pcbBinary = cchString * sizeof(WCHAR);
980 return ret;
983 static LONG DecodeAnyW(LPCWSTR pszString, DWORD cchString,
984 BYTE *pbBinary, DWORD *pcbBinary, DWORD *pdwSkip, DWORD *pdwFlags)
986 LONG ret;
988 ret = Base64HeaderToBinaryW(pszString, cchString, pbBinary, pcbBinary,
989 pdwSkip, pdwFlags);
990 if (ret == ERROR_INVALID_DATA)
991 ret = Base64ToBinaryW(pszString, cchString, pbBinary, pcbBinary,
992 pdwSkip, pdwFlags);
993 if (ret == ERROR_INVALID_DATA)
994 ret = DecodeBinaryToBinaryW(pszString, cchString, pbBinary, pcbBinary,
995 pdwSkip, pdwFlags);
996 return ret;
999 BOOL WINAPI CryptStringToBinaryW(LPCWSTR pszString,
1000 DWORD cchString, DWORD dwFlags, BYTE *pbBinary, DWORD *pcbBinary,
1001 DWORD *pdwSkip, DWORD *pdwFlags)
1003 StringToBinaryWFunc decoder;
1004 LONG ret;
1006 TRACE("(%s, %d, %08x, %p, %p, %p, %p)\n", debugstr_w(pszString),
1007 cchString, dwFlags, pbBinary, pcbBinary, pdwSkip, pdwFlags);
1009 if (!pszString)
1011 SetLastError(ERROR_INVALID_PARAMETER);
1012 return FALSE;
1014 /* Only the bottom byte contains valid types */
1015 if (dwFlags & 0xfffffff0)
1017 SetLastError(ERROR_INVALID_DATA);
1018 return FALSE;
1020 switch (dwFlags)
1022 case CRYPT_STRING_BASE64_ANY:
1023 decoder = Base64AnyToBinaryW;
1024 break;
1025 case CRYPT_STRING_BASE64:
1026 decoder = Base64ToBinaryW;
1027 break;
1028 case CRYPT_STRING_BASE64HEADER:
1029 decoder = Base64HeaderToBinaryW;
1030 break;
1031 case CRYPT_STRING_BASE64REQUESTHEADER:
1032 decoder = Base64RequestHeaderToBinaryW;
1033 break;
1034 case CRYPT_STRING_BASE64X509CRLHEADER:
1035 decoder = Base64X509HeaderToBinaryW;
1036 break;
1037 case CRYPT_STRING_BINARY:
1038 decoder = DecodeBinaryToBinaryW;
1039 break;
1040 case CRYPT_STRING_ANY:
1041 decoder = DecodeAnyW;
1042 break;
1043 case CRYPT_STRING_HEX:
1044 case CRYPT_STRING_HEXASCII:
1045 case CRYPT_STRING_HEXADDR:
1046 case CRYPT_STRING_HEXASCIIADDR:
1047 FIXME("Unimplemented type %d\n", dwFlags & 0x7fffffff);
1048 /* fall through */
1049 default:
1050 SetLastError(ERROR_INVALID_PARAMETER);
1051 return FALSE;
1053 if (!cchString)
1054 cchString = strlenW(pszString);
1055 ret = decoder(pszString, cchString, pbBinary, pcbBinary, pdwSkip, pdwFlags);
1056 if (ret)
1057 SetLastError(ret);
1058 return ret == ERROR_SUCCESS;