[CRYPT32_WINETEST] Sync with Wine Staging 2.2. CORE-12823
[reactos.git] / rostests / winetests / crypt32 / base64.c
1 /*
2 * Unit test suite for crypt32.dll's CryptStringToBinary and CryptBinaryToString
3 * functions.
4 *
5 * Copyright 2006 Juan Lang
6 *
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.
11 *
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.
16 *
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
20 */
21 //#include <stdio.h>
22 #include <stdarg.h>
23 #include <windef.h>
24 #include <winbase.h>
25 //#include <winerror.h>
26 #include <wincrypt.h>
27
28 #include <wine/test.h>
29
30 #define CERT_HEADER "-----BEGIN CERTIFICATE-----\r\n"
31 #define ALT_CERT_HEADER "-----BEGIN This is some arbitrary text that goes on and on-----\r\n"
32 #define CERT_TRAILER "-----END CERTIFICATE-----\r\n"
33 #define ALT_CERT_TRAILER "-----END More arbitrary text------\r\n"
34 #define CERT_REQUEST_HEADER "-----BEGIN NEW CERTIFICATE REQUEST-----\r\n"
35 #define CERT_REQUEST_TRAILER "-----END NEW CERTIFICATE REQUEST-----\r\n"
36 #define X509_HEADER "-----BEGIN X509 CRL-----\r\n"
37 #define X509_TRAILER "-----END X509 CRL-----\r\n"
38 #define CERT_HEADER_NOCR "-----BEGIN CERTIFICATE-----\n"
39 #define CERT_TRAILER_NOCR "-----END CERTIFICATE-----\n"
40 #define CERT_REQUEST_HEADER_NOCR "-----BEGIN NEW CERTIFICATE REQUEST-----\n"
41 #define CERT_REQUEST_TRAILER_NOCR "-----END NEW CERTIFICATE REQUEST-----\n"
42 #define X509_HEADER_NOCR "-----BEGIN X509 CRL-----\n"
43 #define X509_TRAILER_NOCR "-----END X509 CRL-----\n"
44
45 static BOOL (WINAPI *pCryptBinaryToStringA)(const BYTE *pbBinary,
46 DWORD cbBinary, DWORD dwFlags, LPSTR pszString, DWORD *pcchString);
47 static BOOL (WINAPI *pCryptStringToBinaryA)(LPCSTR pszString,
48 DWORD cchString, DWORD dwFlags, BYTE *pbBinary, DWORD *pcbBinary,
49 DWORD *pdwSkip, DWORD *pdwFlags);
50 static BOOL (WINAPI *pCryptStringToBinaryW)(LPCWSTR pszString,
51 DWORD cchString, DWORD dwFlags, BYTE *pbBinary, DWORD *pcbBinary,
52 DWORD *pdwSkip, DWORD *pdwFlags);
53
54 struct BinTests
55 {
56 const BYTE *toEncode;
57 DWORD toEncodeLen;
58 const char *base64;
59 };
60
61 static const BYTE toEncode1[] = { 0 };
62 static const BYTE toEncode2[] = { 1,2 };
63 /* static const BYTE toEncode3[] = { 1,2,3 }; */
64 static const BYTE toEncode4[] =
65 "abcdefghijlkmnopqrstuvwxyz01234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890"
66 "abcdefghijlkmnopqrstuvwxyz01234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890"
67 "abcdefghijlkmnopqrstuvwxyz01234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890";
68
69 static const struct BinTests tests[] = {
70 { toEncode1, sizeof(toEncode1), "AA==\r\n", },
71 { toEncode2, sizeof(toEncode2), "AQI=\r\n", },
72 /* { toEncode3, sizeof(toEncode3), "AQID\r\n", }, This test fails on Vista. */
73 { toEncode4, sizeof(toEncode4),
74 "YWJjZGVmZ2hpamxrbW5vcHFyc3R1dnd4eXowMTIzNDU2Nzg5MEFCQ0RFRkdISUpL\r\n"
75 "TE1OT1BRUlNUVVZXWFlaMDEyMzQ1Njc4OTBhYmNkZWZnaGlqbGttbm9wcXJzdHV2\r\n"
76 "d3h5ejAxMjM0NTY3ODkwQUJDREVGR0hJSktMTU5PUFFSU1RVVldYWVowMTIzNDU2\r\n"
77 "Nzg5MGFiY2RlZmdoaWpsa21ub3BxcnN0dXZ3eHl6MDEyMzQ1Njc4OTBBQkNERUZH\r\n"
78 "SElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0NTY3ODkwAA==\r\n" },
79 };
80
81 static const struct BinTests testsNoCR[] = {
82 { toEncode1, sizeof(toEncode1), "AA==\n", },
83 { toEncode2, sizeof(toEncode2), "AQI=\n", },
84 /* { toEncode3, sizeof(toEncode3), "AQID\n", }, This test fails on Vista. */
85 { toEncode4, sizeof(toEncode4),
86 "YWJjZGVmZ2hpamxrbW5vcHFyc3R1dnd4eXowMTIzNDU2Nzg5MEFCQ0RFRkdISUpL\n"
87 "TE1OT1BRUlNUVVZXWFlaMDEyMzQ1Njc4OTBhYmNkZWZnaGlqbGttbm9wcXJzdHV2\n"
88 "d3h5ejAxMjM0NTY3ODkwQUJDREVGR0hJSktMTU5PUFFSU1RVVldYWVowMTIzNDU2\n"
89 "Nzg5MGFiY2RlZmdoaWpsa21ub3BxcnN0dXZ3eHl6MDEyMzQ1Njc4OTBBQkNERUZH\n"
90 "SElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0NTY3ODkwAA==\n" },
91 };
92
93 static void encodeAndCompareBase64_A(const BYTE *toEncode, DWORD toEncodeLen,
94 DWORD format, const char *expected, const char *header, const char *trailer)
95 {
96 DWORD strLen = 0;
97 LPSTR str = NULL;
98 BOOL ret;
99
100 ret = pCryptBinaryToStringA(toEncode, toEncodeLen, format, NULL, &strLen);
101 ok(ret, "CryptBinaryToStringA failed: %d\n", GetLastError());
102 str = HeapAlloc(GetProcessHeap(), 0, strLen);
103 if (str)
104 {
105 DWORD strLen2 = strLen;
106 LPCSTR ptr = str;
107
108 ret = pCryptBinaryToStringA(toEncode, toEncodeLen, format, str,
109 &strLen2);
110 ok(ret, "CryptBinaryToStringA failed: %d\n", GetLastError());
111 ok(strLen2 == strLen - 1, "Expected length %d, got %d\n",
112 strLen - 1, strLen);
113 if (header)
114 {
115 ok(!strncmp(header, ptr, strlen(header)),
116 "Expected header %s, got %s\n", header, ptr);
117 ptr += strlen(header);
118 }
119 ok(!strncmp(expected, ptr, strlen(expected)),
120 "Expected %s, got %s\n", expected, ptr);
121 ptr += strlen(expected);
122 if (trailer)
123 ok(!strncmp(trailer, ptr, strlen(trailer)),
124 "Expected trailer %s, got %s\n", trailer, ptr);
125 HeapFree(GetProcessHeap(), 0, str);
126 }
127 }
128
129 static void testBinaryToStringA(void)
130 {
131 BOOL ret;
132 DWORD strLen = 0, i;
133
134 ret = pCryptBinaryToStringA(NULL, 0, 0, NULL, NULL);
135 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
136 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
137 ret = pCryptBinaryToStringA(NULL, 0, 0, NULL, &strLen);
138 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
139 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
140 for (i = 0; i < sizeof(tests) / sizeof(tests[0]); i++)
141 {
142 DWORD strLen = 0;
143 LPSTR str = NULL;
144 BOOL ret;
145
146 ret = pCryptBinaryToStringA(tests[i].toEncode, tests[i].toEncodeLen,
147 CRYPT_STRING_BINARY, NULL, &strLen);
148 ok(ret, "CryptBinaryToStringA failed: %d\n", GetLastError());
149 str = HeapAlloc(GetProcessHeap(), 0, strLen);
150 if (str)
151 {
152 DWORD strLen2 = strLen;
153
154 ret = pCryptBinaryToStringA(tests[i].toEncode, tests[i].toEncodeLen,
155 CRYPT_STRING_BINARY, str, &strLen2);
156 ok(ret, "CryptBinaryToStringA failed: %d\n", GetLastError());
157 ok(strLen == strLen2, "Expected length %d, got %d\n", strLen,
158 strLen2);
159 ok(!memcmp(str, tests[i].toEncode, tests[i].toEncodeLen),
160 "Unexpected value\n");
161 HeapFree(GetProcessHeap(), 0, str);
162 }
163 encodeAndCompareBase64_A(tests[i].toEncode, tests[i].toEncodeLen,
164 CRYPT_STRING_BASE64, tests[i].base64, NULL, NULL);
165 encodeAndCompareBase64_A(tests[i].toEncode, tests[i].toEncodeLen,
166 CRYPT_STRING_BASE64HEADER, tests[i].base64, CERT_HEADER,
167 CERT_TRAILER);
168 encodeAndCompareBase64_A(tests[i].toEncode, tests[i].toEncodeLen,
169 CRYPT_STRING_BASE64REQUESTHEADER, tests[i].base64,
170 CERT_REQUEST_HEADER, CERT_REQUEST_TRAILER);
171 encodeAndCompareBase64_A(tests[i].toEncode, tests[i].toEncodeLen,
172 CRYPT_STRING_BASE64X509CRLHEADER, tests[i].base64, X509_HEADER,
173 X509_TRAILER);
174 }
175 for (i = 0; i < sizeof(testsNoCR) / sizeof(testsNoCR[0]); i++)
176 {
177 DWORD strLen = 0;
178 LPSTR str = NULL;
179 BOOL ret;
180
181 ret = pCryptBinaryToStringA(testsNoCR[i].toEncode,
182 testsNoCR[i].toEncodeLen, CRYPT_STRING_BINARY | CRYPT_STRING_NOCR,
183 NULL, &strLen);
184 ok(ret, "CryptBinaryToStringA failed: %d\n", GetLastError());
185 str = HeapAlloc(GetProcessHeap(), 0, strLen);
186 if (str)
187 {
188 DWORD strLen2 = strLen;
189
190 ret = pCryptBinaryToStringA(testsNoCR[i].toEncode,
191 testsNoCR[i].toEncodeLen, CRYPT_STRING_BINARY | CRYPT_STRING_NOCR,
192 str, &strLen2);
193 ok(ret, "CryptBinaryToStringA failed: %d\n", GetLastError());
194 ok(strLen == strLen2, "Expected length %d, got %d\n", strLen,
195 strLen2);
196 ok(!memcmp(str, testsNoCR[i].toEncode, testsNoCR[i].toEncodeLen),
197 "Unexpected value\n");
198 HeapFree(GetProcessHeap(), 0, str);
199 }
200 encodeAndCompareBase64_A(testsNoCR[i].toEncode,
201 testsNoCR[i].toEncodeLen, CRYPT_STRING_BASE64 | CRYPT_STRING_NOCR,
202 testsNoCR[i].base64, NULL, NULL);
203 encodeAndCompareBase64_A(testsNoCR[i].toEncode,
204 testsNoCR[i].toEncodeLen,
205 CRYPT_STRING_BASE64HEADER | CRYPT_STRING_NOCR, testsNoCR[i].base64,
206 CERT_HEADER_NOCR, CERT_TRAILER_NOCR);
207 encodeAndCompareBase64_A(testsNoCR[i].toEncode,
208 testsNoCR[i].toEncodeLen,
209 CRYPT_STRING_BASE64REQUESTHEADER | CRYPT_STRING_NOCR,
210 testsNoCR[i].base64, CERT_REQUEST_HEADER_NOCR,
211 CERT_REQUEST_TRAILER_NOCR);
212 encodeAndCompareBase64_A(testsNoCR[i].toEncode,
213 testsNoCR[i].toEncodeLen,
214 CRYPT_STRING_BASE64X509CRLHEADER | CRYPT_STRING_NOCR,
215 testsNoCR[i].base64, X509_HEADER_NOCR, X509_TRAILER_NOCR);
216 }
217 }
218
219 static void decodeAndCompareBase64_A(LPCSTR toDecode, LPCSTR header,
220 LPCSTR trailer, DWORD useFormat, DWORD expectedFormat, const BYTE *expected,
221 DWORD expectedLen)
222 {
223 static const char garbage[] = "garbage\r\n";
224 LPSTR str;
225 DWORD len = strlen(toDecode) + strlen(garbage) + 1;
226
227 if (header)
228 len += strlen(header);
229 if (trailer)
230 len += strlen(trailer);
231 str = HeapAlloc(GetProcessHeap(), 0, len);
232 if (str)
233 {
234 LPBYTE buf;
235 DWORD bufLen = 0;
236 BOOL ret;
237
238 if (header)
239 strcpy(str, header);
240 else
241 *str = 0;
242 strcat(str, toDecode);
243 if (trailer)
244 strcat(str, trailer);
245 ret = pCryptStringToBinaryA(str, 0, useFormat, NULL, &bufLen, NULL,
246 NULL);
247 ok(ret, "CryptStringToBinaryA failed: %d\n", GetLastError());
248 buf = HeapAlloc(GetProcessHeap(), 0, bufLen);
249 if (buf)
250 {
251 DWORD skipped, usedFormat;
252
253 /* check as normal, make sure last two parameters are optional */
254 ret = pCryptStringToBinaryA(str, 0, useFormat, buf, &bufLen, NULL,
255 NULL);
256 ok(ret, "CryptStringToBinaryA failed: %d\n", GetLastError());
257 ok(bufLen == expectedLen,
258 "Expected length %d, got %d\n", expectedLen, bufLen);
259 ok(!memcmp(buf, expected, bufLen), "Unexpected value\n");
260 /* check last two params */
261 ret = pCryptStringToBinaryA(str, 0, useFormat, buf, &bufLen,
262 &skipped, &usedFormat);
263 ok(ret, "CryptStringToBinaryA failed: %d\n", GetLastError());
264 ok(skipped == 0, "Expected skipped 0, got %d\n", skipped);
265 ok(usedFormat == expectedFormat, "Expected format %d, got %d\n",
266 expectedFormat, usedFormat);
267 HeapFree(GetProcessHeap(), 0, buf);
268 }
269
270 /* Check again, but with garbage up front */
271 strcpy(str, garbage);
272 if (header)
273 strcat(str, header);
274 strcat(str, toDecode);
275 if (trailer)
276 strcat(str, trailer);
277 ret = pCryptStringToBinaryA(str, 0, useFormat, NULL, &bufLen, NULL,
278 NULL);
279 /* expect failure with no header, and success with one */
280 if (header)
281 ok(ret, "CryptStringToBinaryA failed: %d\n", GetLastError());
282 else
283 ok(!ret && GetLastError() == ERROR_INVALID_DATA,
284 "Expected !ret and last error ERROR_INVALID_DATA, got ret=%d, error=%d\n", ret, GetLastError());
285 if (ret)
286 {
287 buf = HeapAlloc(GetProcessHeap(), 0, bufLen);
288 if (buf)
289 {
290 DWORD skipped, usedFormat;
291
292 ret = pCryptStringToBinaryA(str, 0, useFormat, buf, &bufLen,
293 &skipped, &usedFormat);
294 ok(ret, "CryptStringToBinaryA failed: %d\n", GetLastError());
295 ok(skipped == strlen(garbage),
296 "Expected %d characters of \"%s\" skipped when trying format %08x, got %d (used format is %08x)\n",
297 lstrlenA(garbage), str, useFormat, skipped, usedFormat);
298 HeapFree(GetProcessHeap(), 0, buf);
299 }
300 }
301 HeapFree(GetProcessHeap(), 0, str);
302 }
303 }
304
305 static void decodeBase64WithLenFmtW(LPCSTR strA, int len, DWORD fmt, BOOL retA,
306 const BYTE *bufA, DWORD bufLenA, DWORD fmtUsedA)
307 {
308 BYTE buf[8] = {0};
309 DWORD bufLen = sizeof(buf)-1, fmtUsed = 0xdeadbeef;
310 BOOL ret;
311 WCHAR strW[64];
312 int i;
313 for (i = 0; (strW[i] = strA[i]) != 0; ++i);
314 ret = pCryptStringToBinaryW(strW, len, fmt, buf, &bufLen, NULL, &fmtUsed);
315 ok(ret == retA && bufLen == bufLenA && memcmp(bufA, buf, bufLen) == 0
316 && fmtUsed == fmtUsedA, "base64 \"%s\" len %d: W and A differ\n", strA, len);
317 }
318
319 static void decodeBase64WithLenFmt(LPCSTR str, int len, DWORD fmt, LPCSTR expected, int le, BOOL isBroken)
320 {
321 BYTE buf[8] = {0};
322 DWORD bufLen = sizeof(buf)-1, fmtUsed = 0xdeadbeef;
323 BOOL ret;
324 SetLastError(0xdeadbeef);
325 ret = pCryptStringToBinaryA(str, len, fmt, buf, &bufLen, NULL, &fmtUsed);
326 buf[bufLen] = 0;
327 if (expected) {
328 BOOL correct = ret && strcmp(expected, (char*)buf) == 0;
329 ok(correct || (isBroken && broken(!ret)),
330 "base64 \"%s\" len %d: expected \"%s\", got \"%s\" (ret %d, le %d)\n",
331 str, len, expected, (char*)buf, ret, GetLastError());
332 if (correct)
333 ok(fmtUsed == fmt, "base64 \"%s\" len %d: expected fmt %d, used %d\n",
334 str, len, fmt, fmtUsed);
335 } else {
336 ok(!ret && GetLastError() == le,
337 "base64 \"%s\" len %d: expected failure, got \"%s\" (ret %d, le %d)\n",
338 str, len, (char*)buf, ret, GetLastError());
339 }
340 if (pCryptStringToBinaryW)
341 decodeBase64WithLenFmtW(str, len, fmt, ret, buf, bufLen, fmtUsed);
342 }
343
344 static void decodeBase64WithLenBroken(LPCSTR str, int len, LPCSTR expected, int le)
345 {
346 decodeBase64WithLenFmt(str, len, CRYPT_STRING_BASE64, expected, le, TRUE);
347 }
348
349 static void decodeBase64WithLen(LPCSTR str, int len, LPCSTR expected, int le)
350 {
351 decodeBase64WithLenFmt(str, len, CRYPT_STRING_BASE64, expected, le, FALSE);
352 }
353
354 static void decodeBase64WithFmt(LPCSTR str, DWORD fmt, LPCSTR expected, int le)
355 {
356 decodeBase64WithLenFmt(str, 0, fmt, expected, le, FALSE);
357 }
358
359 struct BadString
360 {
361 const char *str;
362 DWORD format;
363 };
364
365 static const struct BadString badStrings[] = {
366 { "-----BEGIN X509 CRL-----\r\nAA==\r\n", CRYPT_STRING_BASE64X509CRLHEADER },
367 };
368
369 static void testStringToBinaryA(void)
370 {
371 BOOL ret;
372 DWORD bufLen = 0, i;
373 BYTE buf[8];
374
375 ret = pCryptStringToBinaryA(NULL, 0, 0, NULL, NULL, NULL, NULL);
376 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
377 "Expected ERROR_INVALID_PARAMETER, got ret=%d le=%u\n", ret, GetLastError());
378 ret = pCryptStringToBinaryA(NULL, 0, 0, NULL, &bufLen, NULL, NULL);
379 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
380 "Expected ERROR_INVALID_PARAMETER, got ret=%d le=%u\n", ret, GetLastError());
381 /* Bogus format */
382 ret = pCryptStringToBinaryA(tests[0].base64, 0, 0, NULL, &bufLen, NULL,
383 NULL);
384 ok(!ret && GetLastError() == ERROR_INVALID_DATA,
385 "Expected ERROR_INVALID_DATA, got ret=%d le=%u\n", ret, GetLastError());
386 /* Decoding doesn't expect the NOCR flag to be specified */
387 ret = pCryptStringToBinaryA(tests[0].base64, 1,
388 CRYPT_STRING_BASE64 | CRYPT_STRING_NOCR, NULL, &bufLen, NULL, NULL);
389 ok(!ret && GetLastError() == ERROR_INVALID_DATA,
390 "Expected ERROR_INVALID_DATA, got ret=%d le=%u\n", ret, GetLastError());
391 /* Bad strings */
392 for (i = 0; i < sizeof(badStrings) / sizeof(badStrings[0]); i++)
393 {
394 bufLen = 0;
395 ret = pCryptStringToBinaryA(badStrings[i].str, 0, badStrings[i].format,
396 NULL, &bufLen, NULL, NULL);
397 ok(!ret && GetLastError() == ERROR_INVALID_DATA,
398 "%d: Expected ERROR_INVALID_DATA, got ret=%d le=%u\n", i, ret, GetLastError());
399 }
400 /* Weird base64 strings (invalid padding, extra white-space etc.) */
401 decodeBase64WithLen("V=", 0, 0, ERROR_INVALID_DATA);
402 decodeBase64WithLen("VV=", 0, 0, ERROR_INVALID_DATA);
403 decodeBase64WithLen("V==", 0, 0, ERROR_INVALID_DATA);
404 decodeBase64WithLen("V=", 2, 0, ERROR_INVALID_DATA);
405 decodeBase64WithLen("VV=", 3, 0, ERROR_INVALID_DATA);
406 decodeBase64WithLen("V==", 3, 0, ERROR_INVALID_DATA);
407 decodeBase64WithLenBroken("V", 0, "T", 0);
408 decodeBase64WithLenBroken("VV", 0, "U", 0);
409 decodeBase64WithLenBroken("VVV", 0, "UU", 0);
410 decodeBase64WithLen("V", 1, "T", 0);
411 decodeBase64WithLen("VV", 2, "U", 0);
412 decodeBase64WithLen("VVV", 3, "UU", 0);
413 decodeBase64WithLen("V===", 0, "T", 0);
414 decodeBase64WithLen("V========", 0, "T", 0);
415 decodeBase64WithLen("V===", 4, "T", 0);
416 decodeBase64WithLen("V\nVVV", 0, "UUU", 0);
417 decodeBase64WithLen("VV\nVV", 0, "UUU", 0);
418 decodeBase64WithLen("VVV\nV", 0, "UUU", 0);
419 decodeBase64WithLen("V\nVVV", 5, "UUU", 0);
420 decodeBase64WithLen("VV\nVV", 5, "UUU", 0);
421 decodeBase64WithLen("VVV\nV", 5, "UUU", 0);
422 decodeBase64WithLen("VV VV", 0, "UUU", 0);
423 decodeBase64WithLen("V===VVVV", 0, "T", 0);
424 decodeBase64WithLen("VV==VVVV", 0, "U", 0);
425 decodeBase64WithLen("VVV=VVVV", 0, "UU", 0);
426 decodeBase64WithLen("VVVV=VVVV", 0, "UUU", 0);
427 decodeBase64WithLen("V===VVVV", 8, "T", 0);
428 decodeBase64WithLen("VV==VVVV", 8, "U", 0);
429 decodeBase64WithLen("VVV=VVVV", 8, "UU", 0);
430 decodeBase64WithLen("VVVV=VVVV", 8, "UUU", 0);
431
432 decodeBase64WithFmt("-----BEGIN-----VVVV-----END-----", CRYPT_STRING_BASE64HEADER, 0, ERROR_INVALID_DATA);
433 decodeBase64WithFmt("-----BEGIN-----VVVV-----END -----", CRYPT_STRING_BASE64HEADER, 0, ERROR_INVALID_DATA);
434 decodeBase64WithFmt("-----BEGIN -----VVVV-----END-----", CRYPT_STRING_BASE64HEADER, 0, ERROR_INVALID_DATA);
435 decodeBase64WithFmt("-----BEGIN -----VVVV-----END -----", CRYPT_STRING_BASE64HEADER, "UUU", 0);
436
437 decodeBase64WithFmt("-----BEGIN -----V-----END -----", CRYPT_STRING_BASE64HEADER, "T", 0);
438 decodeBase64WithFmt("-----BEGIN foo-----V-----END -----", CRYPT_STRING_BASE64HEADER, "T", 0);
439 decodeBase64WithFmt("-----BEGIN foo-----V-----END foo-----", CRYPT_STRING_BASE64HEADER, "T", 0);
440 decodeBase64WithFmt("-----BEGIN -----V-----END foo-----", CRYPT_STRING_BASE64HEADER, "T", 0);
441 decodeBase64WithFmt("-----BEGIN -----V-----END -----", CRYPT_STRING_BASE64X509CRLHEADER, "T", 0);
442 decodeBase64WithFmt("-----BEGIN foo-----V-----END -----", CRYPT_STRING_BASE64X509CRLHEADER, "T", 0);
443 decodeBase64WithFmt("-----BEGIN foo-----V-----END foo-----", CRYPT_STRING_BASE64X509CRLHEADER, "T", 0);
444 decodeBase64WithFmt("-----BEGIN -----V-----END foo-----", CRYPT_STRING_BASE64X509CRLHEADER, "T", 0);
445 decodeBase64WithFmt("-----BEGIN -----V-----END -----", CRYPT_STRING_BASE64REQUESTHEADER, "T", 0);
446 decodeBase64WithFmt("-----BEGIN foo-----V-----END -----", CRYPT_STRING_BASE64REQUESTHEADER, "T", 0);
447 decodeBase64WithFmt("-----BEGIN foo-----V-----END foo-----", CRYPT_STRING_BASE64REQUESTHEADER, "T", 0);
448 decodeBase64WithFmt("-----BEGIN -----V-----END foo-----", CRYPT_STRING_BASE64REQUESTHEADER, "T", 0);
449
450 /* Too small buffer */
451 buf[0] = 0;
452 bufLen = 4;
453 ret = pCryptStringToBinaryA("VVVVVVVV", 8, CRYPT_STRING_BASE64, (BYTE*)buf, &bufLen, NULL, NULL);
454 ok(!ret && bufLen == 4 && buf[0] == 0,
455 "Expected ret 0, bufLen 4, buf[0] '\\0', got ret %d, bufLen %d, buf[0] '%c'\n",
456 ret, bufLen, buf[0]);
457
458 /* Good strings */
459 for (i = 0; i < sizeof(tests) / sizeof(tests[0]); i++)
460 {
461 bufLen = 0;
462 /* Bogus length--oddly enough, that succeeds, even though it's not
463 * properly padded.
464 */
465 ret = pCryptStringToBinaryA(tests[i].base64, 1, CRYPT_STRING_BASE64,
466 NULL, &bufLen, NULL, NULL);
467 ok(ret, "CryptStringToBinaryA failed: %d\n", GetLastError());
468 /* Check with the precise format */
469 decodeAndCompareBase64_A(tests[i].base64, NULL, NULL,
470 CRYPT_STRING_BASE64, CRYPT_STRING_BASE64, tests[i].toEncode,
471 tests[i].toEncodeLen);
472 decodeAndCompareBase64_A(tests[i].base64, CERT_HEADER, CERT_TRAILER,
473 CRYPT_STRING_BASE64HEADER, CRYPT_STRING_BASE64HEADER,
474 tests[i].toEncode, tests[i].toEncodeLen);
475 decodeAndCompareBase64_A(tests[i].base64, ALT_CERT_HEADER, ALT_CERT_TRAILER,
476 CRYPT_STRING_BASE64HEADER, CRYPT_STRING_BASE64HEADER,
477 tests[i].toEncode, tests[i].toEncodeLen);
478 decodeAndCompareBase64_A(tests[i].base64, CERT_REQUEST_HEADER,
479 CERT_REQUEST_TRAILER, CRYPT_STRING_BASE64REQUESTHEADER,
480 CRYPT_STRING_BASE64REQUESTHEADER, tests[i].toEncode,
481 tests[i].toEncodeLen);
482 decodeAndCompareBase64_A(tests[i].base64, X509_HEADER, X509_TRAILER,
483 CRYPT_STRING_BASE64X509CRLHEADER, CRYPT_STRING_BASE64X509CRLHEADER,
484 tests[i].toEncode, tests[i].toEncodeLen);
485 /* And check with the "any" formats */
486 decodeAndCompareBase64_A(tests[i].base64, NULL, NULL,
487 CRYPT_STRING_BASE64_ANY, CRYPT_STRING_BASE64, tests[i].toEncode,
488 tests[i].toEncodeLen);
489 /* Don't check with no header and the string_any format, that'll
490 * always succeed.
491 */
492 decodeAndCompareBase64_A(tests[i].base64, CERT_HEADER, CERT_TRAILER,
493 CRYPT_STRING_BASE64_ANY, CRYPT_STRING_BASE64HEADER, tests[i].toEncode,
494 tests[i].toEncodeLen);
495 decodeAndCompareBase64_A(tests[i].base64, CERT_HEADER, CERT_TRAILER,
496 CRYPT_STRING_ANY, CRYPT_STRING_BASE64HEADER, tests[i].toEncode,
497 tests[i].toEncodeLen);
498 /* oddly, these seem to decode using the wrong format
499 decodeAndCompareBase64_A(tests[i].base64, CERT_REQUEST_HEADER,
500 CERT_REQUEST_TRAILER, CRYPT_STRING_BASE64_ANY,
501 CRYPT_STRING_BASE64REQUESTHEADER, tests[i].toEncode,
502 tests[i].toEncodeLen);
503 decodeAndCompareBase64_A(tests[i].base64, CERT_REQUEST_HEADER,
504 CERT_REQUEST_TRAILER, CRYPT_STRING_ANY,
505 CRYPT_STRING_BASE64REQUESTHEADER, tests[i].toEncode,
506 tests[i].toEncodeLen);
507 decodeAndCompareBase64_A(tests[i].base64, X509_HEADER, X509_TRAILER,
508 CRYPT_STRING_BASE64_ANY, CRYPT_STRING_BASE64X509CRLHEADER,
509 tests[i].toEncode, tests[i].toEncodeLen);
510 decodeAndCompareBase64_A(tests[i].base64, X509_HEADER, X509_TRAILER,
511 CRYPT_STRING_ANY, CRYPT_STRING_BASE64X509CRLHEADER, tests[i].toEncode,
512 tests[i].toEncodeLen);
513 */
514 }
515 /* And again, with no CR--decoding handles this automatically */
516 for (i = 0; i < sizeof(testsNoCR) / sizeof(testsNoCR[0]); i++)
517 {
518 bufLen = 0;
519 /* Bogus length--oddly enough, that succeeds, even though it's not
520 * properly padded.
521 */
522 ret = pCryptStringToBinaryA(testsNoCR[i].base64, 1, CRYPT_STRING_BASE64,
523 NULL, &bufLen, NULL, NULL);
524 ok(ret, "CryptStringToBinaryA failed: %d\n", GetLastError());
525 /* Check with the precise format */
526 decodeAndCompareBase64_A(testsNoCR[i].base64, NULL, NULL,
527 CRYPT_STRING_BASE64, CRYPT_STRING_BASE64, testsNoCR[i].toEncode,
528 testsNoCR[i].toEncodeLen);
529 decodeAndCompareBase64_A(testsNoCR[i].base64, CERT_HEADER, CERT_TRAILER,
530 CRYPT_STRING_BASE64HEADER, CRYPT_STRING_BASE64HEADER,
531 testsNoCR[i].toEncode, testsNoCR[i].toEncodeLen);
532 decodeAndCompareBase64_A(testsNoCR[i].base64, CERT_REQUEST_HEADER,
533 CERT_REQUEST_TRAILER, CRYPT_STRING_BASE64REQUESTHEADER,
534 CRYPT_STRING_BASE64REQUESTHEADER, testsNoCR[i].toEncode,
535 testsNoCR[i].toEncodeLen);
536 decodeAndCompareBase64_A(testsNoCR[i].base64, X509_HEADER, X509_TRAILER,
537 CRYPT_STRING_BASE64X509CRLHEADER, CRYPT_STRING_BASE64X509CRLHEADER,
538 testsNoCR[i].toEncode, testsNoCR[i].toEncodeLen);
539 /* And check with the "any" formats */
540 decodeAndCompareBase64_A(testsNoCR[i].base64, NULL, NULL,
541 CRYPT_STRING_BASE64_ANY, CRYPT_STRING_BASE64, testsNoCR[i].toEncode,
542 testsNoCR[i].toEncodeLen);
543 /* Don't check with no header and the string_any format, that'll
544 * always succeed.
545 */
546 decodeAndCompareBase64_A(testsNoCR[i].base64, CERT_HEADER, CERT_TRAILER,
547 CRYPT_STRING_BASE64_ANY, CRYPT_STRING_BASE64HEADER,
548 testsNoCR[i].toEncode, testsNoCR[i].toEncodeLen);
549 decodeAndCompareBase64_A(testsNoCR[i].base64, CERT_HEADER, CERT_TRAILER,
550 CRYPT_STRING_ANY, CRYPT_STRING_BASE64HEADER, testsNoCR[i].toEncode,
551 testsNoCR[i].toEncodeLen);
552 }
553 }
554
555 START_TEST(base64)
556 {
557 HMODULE lib = GetModuleHandleA("crypt32");
558
559 pCryptBinaryToStringA = (void *)GetProcAddress(lib, "CryptBinaryToStringA");
560 pCryptStringToBinaryA = (void *)GetProcAddress(lib, "CryptStringToBinaryA");
561 pCryptStringToBinaryW = (void *)GetProcAddress(lib, "CryptStringToBinaryW");
562
563 if (pCryptBinaryToStringA)
564 testBinaryToStringA();
565 else
566 win_skip("CryptBinaryToStringA is not available\n");
567
568 if (pCryptStringToBinaryA)
569 testStringToBinaryA();
570 else
571 win_skip("CryptStringToBinaryA is not available\n");
572 }