2 * Unit test suite for crypt32.dll's CryptStringToBinary and CryptBinaryToString
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
28 #include "wine/test.h"
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"
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);
58 static const BYTE toEncode1[] = { 0 };
59 static const BYTE toEncode2[] = { 1,2 };
60 static const BYTE toEncode3[] = { 1,2,3 };
61 static const BYTE toEncode4[] =
62 "abcdefghijlkmnopqrstuvwxyz01234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890"
63 "abcdefghijlkmnopqrstuvwxyz01234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890"
64 "abcdefghijlkmnopqrstuvwxyz01234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890";
66 static const struct BinTests tests[] = {
67 { toEncode1, sizeof(toEncode1), "AA==\r\n", },
68 { toEncode2, sizeof(toEncode2), "AQI=\r\n", },
69 /* { toEncode3, sizeof(toEncode3), "AQID\r\n", }, This test fails on Vista. */
70 { toEncode4, sizeof(toEncode4),
71 "YWJjZGVmZ2hpamxrbW5vcHFyc3R1dnd4eXowMTIzNDU2Nzg5MEFCQ0RFRkdISUpL\r\n"
72 "TE1OT1BRUlNUVVZXWFlaMDEyMzQ1Njc4OTBhYmNkZWZnaGlqbGttbm9wcXJzdHV2\r\n"
73 "d3h5ejAxMjM0NTY3ODkwQUJDREVGR0hJSktMTU5PUFFSU1RVVldYWVowMTIzNDU2\r\n"
74 "Nzg5MGFiY2RlZmdoaWpsa21ub3BxcnN0dXZ3eHl6MDEyMzQ1Njc4OTBBQkNERUZH\r\n"
75 "SElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0NTY3ODkwAA==\r\n" },
78 static const struct BinTests testsNoCR[] = {
79 { toEncode1, sizeof(toEncode1), "AA==\n", },
80 { toEncode2, sizeof(toEncode2), "AQI=\n", },
81 /* { toEncode3, sizeof(toEncode3), "AQID\n", }, This test fails on Vista. */
82 { toEncode4, sizeof(toEncode4),
83 "YWJjZGVmZ2hpamxrbW5vcHFyc3R1dnd4eXowMTIzNDU2Nzg5MEFCQ0RFRkdISUpL\n"
84 "TE1OT1BRUlNUVVZXWFlaMDEyMzQ1Njc4OTBhYmNkZWZnaGlqbGttbm9wcXJzdHV2\n"
85 "d3h5ejAxMjM0NTY3ODkwQUJDREVGR0hJSktMTU5PUFFSU1RVVldYWVowMTIzNDU2\n"
86 "Nzg5MGFiY2RlZmdoaWpsa21ub3BxcnN0dXZ3eHl6MDEyMzQ1Njc4OTBBQkNERUZH\n"
87 "SElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0NTY3ODkwAA==\n" },
90 static void encodeAndCompareBase64_A(const BYTE *toEncode, DWORD toEncodeLen,
91 DWORD format, const char *expected, const char *header, const char *trailer)
97 ret = pCryptBinaryToStringA(toEncode, toEncodeLen, format, NULL, &strLen);
98 ok(ret, "CryptBinaryToStringA failed: %d\n", GetLastError());
99 str = HeapAlloc(GetProcessHeap(), 0, strLen);
102 DWORD strLen2 = strLen;
105 ret = pCryptBinaryToStringA(toEncode, toEncodeLen, format, str,
107 ok(ret, "CryptBinaryToStringA failed: %d\n", GetLastError());
108 ok(strLen2 == strLen - 1, "Expected length %d, got %d\n",
112 ok(!strncmp(header, ptr, strlen(header)),
113 "Expected header %s, got %s\n", header, ptr);
114 ptr += strlen(header);
116 ok(!strncmp(expected, ptr, strlen(expected)),
117 "Expected %s, got %s\n", expected, ptr);
118 ptr += strlen(expected);
120 ok(!strncmp(trailer, ptr, strlen(trailer)),
121 "Expected trailer %s, got %s\n", trailer, ptr);
122 HeapFree(GetProcessHeap(), 0, str);
126 static void testBinaryToStringA(void)
131 ret = pCryptBinaryToStringA(NULL, 0, 0, NULL, NULL);
132 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
133 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
134 ret = pCryptBinaryToStringA(NULL, 0, 0, NULL, &strLen);
135 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
136 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
137 for (i = 0; i < sizeof(tests) / sizeof(tests[0]); i++)
143 ret = pCryptBinaryToStringA(tests[i].toEncode, tests[i].toEncodeLen,
144 CRYPT_STRING_BINARY, NULL, &strLen);
145 ok(ret, "CryptBinaryToStringA failed: %d\n", GetLastError());
146 str = HeapAlloc(GetProcessHeap(), 0, strLen);
149 DWORD strLen2 = strLen;
151 ret = pCryptBinaryToStringA(tests[i].toEncode, tests[i].toEncodeLen,
152 CRYPT_STRING_BINARY, str, &strLen2);
153 ok(ret, "CryptBinaryToStringA failed: %d\n", GetLastError());
154 ok(strLen == strLen2, "Expected length %d, got %d\n", strLen,
156 ok(!memcmp(str, tests[i].toEncode, tests[i].toEncodeLen),
157 "Unexpected value\n");
158 HeapFree(GetProcessHeap(), 0, str);
160 encodeAndCompareBase64_A(tests[i].toEncode, tests[i].toEncodeLen,
161 CRYPT_STRING_BASE64, tests[i].base64, NULL, NULL);
162 encodeAndCompareBase64_A(tests[i].toEncode, tests[i].toEncodeLen,
163 CRYPT_STRING_BASE64HEADER, tests[i].base64, CERT_HEADER,
165 encodeAndCompareBase64_A(tests[i].toEncode, tests[i].toEncodeLen,
166 CRYPT_STRING_BASE64REQUESTHEADER, tests[i].base64,
167 CERT_REQUEST_HEADER, CERT_REQUEST_TRAILER);
168 encodeAndCompareBase64_A(tests[i].toEncode, tests[i].toEncodeLen,
169 CRYPT_STRING_BASE64X509CRLHEADER, tests[i].base64, X509_HEADER,
172 for (i = 0; i < sizeof(testsNoCR) / sizeof(testsNoCR[0]); i++)
178 ret = pCryptBinaryToStringA(testsNoCR[i].toEncode,
179 testsNoCR[i].toEncodeLen, CRYPT_STRING_BINARY | CRYPT_STRING_NOCR,
181 ok(ret, "CryptBinaryToStringA failed: %d\n", GetLastError());
182 str = HeapAlloc(GetProcessHeap(), 0, strLen);
185 DWORD strLen2 = strLen;
187 ret = pCryptBinaryToStringA(testsNoCR[i].toEncode,
188 testsNoCR[i].toEncodeLen, CRYPT_STRING_BINARY | CRYPT_STRING_NOCR,
190 ok(ret, "CryptBinaryToStringA failed: %d\n", GetLastError());
191 ok(strLen == strLen2, "Expected length %d, got %d\n", strLen,
193 ok(!memcmp(str, testsNoCR[i].toEncode, testsNoCR[i].toEncodeLen),
194 "Unexpected value\n");
195 HeapFree(GetProcessHeap(), 0, str);
197 encodeAndCompareBase64_A(testsNoCR[i].toEncode,
198 testsNoCR[i].toEncodeLen, CRYPT_STRING_BASE64 | CRYPT_STRING_NOCR,
199 testsNoCR[i].base64, NULL, NULL);
200 encodeAndCompareBase64_A(testsNoCR[i].toEncode,
201 testsNoCR[i].toEncodeLen,
202 CRYPT_STRING_BASE64HEADER | CRYPT_STRING_NOCR, testsNoCR[i].base64,
203 CERT_HEADER_NOCR, CERT_TRAILER_NOCR);
204 encodeAndCompareBase64_A(testsNoCR[i].toEncode,
205 testsNoCR[i].toEncodeLen,
206 CRYPT_STRING_BASE64REQUESTHEADER | CRYPT_STRING_NOCR,
207 testsNoCR[i].base64, CERT_REQUEST_HEADER_NOCR,
208 CERT_REQUEST_TRAILER_NOCR);
209 encodeAndCompareBase64_A(testsNoCR[i].toEncode,
210 testsNoCR[i].toEncodeLen,
211 CRYPT_STRING_BASE64X509CRLHEADER | CRYPT_STRING_NOCR,
212 testsNoCR[i].base64, X509_HEADER_NOCR, X509_TRAILER_NOCR);
216 static void decodeAndCompareBase64_A(LPCSTR toDecode, LPCSTR header,
217 LPCSTR trailer, DWORD useFormat, DWORD expectedFormat, const BYTE *expected,
220 static const char garbage[] = "garbage\r\n";
222 DWORD len = strlen(toDecode) + strlen(garbage) + 1;
225 len += strlen(header);
227 len += strlen(trailer);
228 str = HeapAlloc(GetProcessHeap(), 0, len);
239 strcat(str, toDecode);
241 strcat(str, trailer);
242 ret = pCryptStringToBinaryA(str, 0, useFormat, NULL, &bufLen, NULL,
244 ok(ret, "CryptStringToBinaryA failed: %d\n", GetLastError());
245 buf = HeapAlloc(GetProcessHeap(), 0, bufLen);
248 DWORD skipped, usedFormat;
250 /* check as normal, make sure last two parameters are optional */
251 ret = pCryptStringToBinaryA(str, 0, useFormat, buf, &bufLen, NULL,
253 ok(ret, "CryptStringToBinaryA failed: %d\n", GetLastError());
254 ok(bufLen == expectedLen,
255 "Expected length %d, got %d\n", expectedLen, bufLen);
256 ok(!memcmp(buf, expected, bufLen), "Unexpected value\n");
257 /* check last two params */
258 ret = pCryptStringToBinaryA(str, 0, useFormat, buf, &bufLen,
259 &skipped, &usedFormat);
260 ok(ret, "CryptStringToBinaryA failed: %d\n", GetLastError());
261 ok(skipped == 0, "Expected skipped 0, got %d\n", skipped);
262 ok(usedFormat == expectedFormat, "Expected format %d, got %d\n",
263 expectedFormat, usedFormat);
264 HeapFree(GetProcessHeap(), 0, buf);
267 /* Check again, but with garbage up front */
268 strcpy(str, garbage);
271 strcat(str, toDecode);
273 strcat(str, trailer);
274 ret = pCryptStringToBinaryA(str, 0, useFormat, NULL, &bufLen, NULL,
276 /* expect failure with no header, and success with one */
278 ok(ret, "CryptStringToBinaryA failed: %d\n", GetLastError());
280 ok(!ret && GetLastError() == ERROR_INVALID_DATA,
281 "Expected !ret and last error ERROR_INVALID_DATA, got ret=%d, error=%d\n", ret, GetLastError());
284 buf = HeapAlloc(GetProcessHeap(), 0, bufLen);
287 DWORD skipped, usedFormat;
289 ret = pCryptStringToBinaryA(str, 0, useFormat, buf, &bufLen,
290 &skipped, &usedFormat);
291 ok(ret, "CryptStringToBinaryA failed: %d\n", GetLastError());
292 ok(skipped == strlen(garbage),
293 "Expected %d characters of \"%s\" skipped when trying format %08x, got %d (used format is %08x)\n",
294 lstrlenA(garbage), str, useFormat, skipped, usedFormat);
295 HeapFree(GetProcessHeap(), 0, buf);
298 HeapFree(GetProcessHeap(), 0, str);
308 static const struct BadString badStrings[] = {
309 { "A\r\nA\r\n=\r\n=\r\n", CRYPT_STRING_BASE64 },
310 { "AA\r\n=\r\n=\r\n", CRYPT_STRING_BASE64 },
311 { "AA=\r\n=\r\n", CRYPT_STRING_BASE64 },
312 { "-----BEGIN X509 CRL-----\r\nAA==\r\n", CRYPT_STRING_BASE64X509CRLHEADER },
315 static void testStringToBinaryA(void)
320 ret = pCryptStringToBinaryA(NULL, 0, 0, NULL, NULL, NULL, NULL);
321 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
322 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
323 ret = pCryptStringToBinaryA(NULL, 0, 0, NULL, &bufLen, NULL, NULL);
324 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
325 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
327 ret = pCryptStringToBinaryA(tests[0].base64, 0, 0, NULL, &bufLen, NULL,
329 ok(!ret && GetLastError() == ERROR_INVALID_DATA,
330 "Expected ERROR_INVALID_DATA, got %d\n", GetLastError());
331 /* Decoding doesn't expect the NOCR flag to be specified */
332 ret = pCryptStringToBinaryA(tests[0].base64, 1,
333 CRYPT_STRING_BASE64 | CRYPT_STRING_NOCR, NULL, &bufLen, NULL, NULL);
334 ok(!ret && GetLastError() == ERROR_INVALID_DATA,
335 "Expected ERROR_INVALID_DATA, got %d\n", GetLastError());
337 for (i = 0; i < sizeof(badStrings) / sizeof(badStrings[0]); i++)
340 ret = pCryptStringToBinaryA(badStrings[i].str, 0, badStrings[i].format,
341 NULL, &bufLen, NULL, NULL);
342 ok(!ret && GetLastError() == ERROR_INVALID_DATA,
343 "Expected ERROR_INVALID_DATA, got %d\n", GetLastError());
346 for (i = 0; i < sizeof(tests) / sizeof(tests[0]); i++)
349 /* Bogus length--oddly enough, that succeeds, even though it's not
352 ret = pCryptStringToBinaryA(tests[i].base64, 1, CRYPT_STRING_BASE64,
353 NULL, &bufLen, NULL, NULL);
354 todo_wine ok(ret, "CryptStringToBinaryA failed: %d\n", GetLastError());
355 /* Check with the precise format */
356 decodeAndCompareBase64_A(tests[i].base64, NULL, NULL,
357 CRYPT_STRING_BASE64, CRYPT_STRING_BASE64, tests[i].toEncode,
358 tests[i].toEncodeLen);
359 decodeAndCompareBase64_A(tests[i].base64, CERT_HEADER, CERT_TRAILER,
360 CRYPT_STRING_BASE64HEADER, CRYPT_STRING_BASE64HEADER,
361 tests[i].toEncode, tests[i].toEncodeLen);
362 decodeAndCompareBase64_A(tests[i].base64, ALT_CERT_HEADER, ALT_CERT_TRAILER,
363 CRYPT_STRING_BASE64HEADER, CRYPT_STRING_BASE64HEADER,
364 tests[i].toEncode, tests[i].toEncodeLen);
365 decodeAndCompareBase64_A(tests[i].base64, CERT_REQUEST_HEADER,
366 CERT_REQUEST_TRAILER, CRYPT_STRING_BASE64REQUESTHEADER,
367 CRYPT_STRING_BASE64REQUESTHEADER, tests[i].toEncode,
368 tests[i].toEncodeLen);
369 decodeAndCompareBase64_A(tests[i].base64, X509_HEADER, X509_TRAILER,
370 CRYPT_STRING_BASE64X509CRLHEADER, CRYPT_STRING_BASE64X509CRLHEADER,
371 tests[i].toEncode, tests[i].toEncodeLen);
372 /* And check with the "any" formats */
373 decodeAndCompareBase64_A(tests[i].base64, NULL, NULL,
374 CRYPT_STRING_BASE64_ANY, CRYPT_STRING_BASE64, tests[i].toEncode,
375 tests[i].toEncodeLen);
376 /* Don't check with no header and the string_any format, that'll
379 decodeAndCompareBase64_A(tests[i].base64, CERT_HEADER, CERT_TRAILER,
380 CRYPT_STRING_BASE64_ANY, CRYPT_STRING_BASE64HEADER, tests[i].toEncode,
381 tests[i].toEncodeLen);
382 decodeAndCompareBase64_A(tests[i].base64, CERT_HEADER, CERT_TRAILER,
383 CRYPT_STRING_ANY, CRYPT_STRING_BASE64HEADER, tests[i].toEncode,
384 tests[i].toEncodeLen);
385 /* oddly, these seem to decode using the wrong format
386 decodeAndCompareBase64_A(tests[i].base64, CERT_REQUEST_HEADER,
387 CERT_REQUEST_TRAILER, CRYPT_STRING_BASE64_ANY,
388 CRYPT_STRING_BASE64REQUESTHEADER, tests[i].toEncode,
389 tests[i].toEncodeLen);
390 decodeAndCompareBase64_A(tests[i].base64, CERT_REQUEST_HEADER,
391 CERT_REQUEST_TRAILER, CRYPT_STRING_ANY,
392 CRYPT_STRING_BASE64REQUESTHEADER, tests[i].toEncode,
393 tests[i].toEncodeLen);
394 decodeAndCompareBase64_A(tests[i].base64, X509_HEADER, X509_TRAILER,
395 CRYPT_STRING_BASE64_ANY, CRYPT_STRING_BASE64X509CRLHEADER,
396 tests[i].toEncode, tests[i].toEncodeLen);
397 decodeAndCompareBase64_A(tests[i].base64, X509_HEADER, X509_TRAILER,
398 CRYPT_STRING_ANY, CRYPT_STRING_BASE64X509CRLHEADER, tests[i].toEncode,
399 tests[i].toEncodeLen);
402 /* And again, with no CR--decoding handles this automatically */
403 for (i = 0; i < sizeof(testsNoCR) / sizeof(testsNoCR[0]); i++)
406 /* Bogus length--oddly enough, that succeeds, even though it's not
409 ret = pCryptStringToBinaryA(testsNoCR[i].base64, 1, CRYPT_STRING_BASE64,
410 NULL, &bufLen, NULL, NULL);
411 todo_wine ok(ret, "CryptStringToBinaryA failed: %d\n", GetLastError());
412 /* Check with the precise format */
413 decodeAndCompareBase64_A(testsNoCR[i].base64, NULL, NULL,
414 CRYPT_STRING_BASE64, CRYPT_STRING_BASE64, testsNoCR[i].toEncode,
415 testsNoCR[i].toEncodeLen);
416 decodeAndCompareBase64_A(testsNoCR[i].base64, CERT_HEADER, CERT_TRAILER,
417 CRYPT_STRING_BASE64HEADER, CRYPT_STRING_BASE64HEADER,
418 testsNoCR[i].toEncode, testsNoCR[i].toEncodeLen);
419 decodeAndCompareBase64_A(testsNoCR[i].base64, CERT_REQUEST_HEADER,
420 CERT_REQUEST_TRAILER, CRYPT_STRING_BASE64REQUESTHEADER,
421 CRYPT_STRING_BASE64REQUESTHEADER, testsNoCR[i].toEncode,
422 testsNoCR[i].toEncodeLen);
423 decodeAndCompareBase64_A(testsNoCR[i].base64, X509_HEADER, X509_TRAILER,
424 CRYPT_STRING_BASE64X509CRLHEADER, CRYPT_STRING_BASE64X509CRLHEADER,
425 testsNoCR[i].toEncode, testsNoCR[i].toEncodeLen);
426 /* And check with the "any" formats */
427 decodeAndCompareBase64_A(testsNoCR[i].base64, NULL, NULL,
428 CRYPT_STRING_BASE64_ANY, CRYPT_STRING_BASE64, testsNoCR[i].toEncode,
429 testsNoCR[i].toEncodeLen);
430 /* Don't check with no header and the string_any format, that'll
433 decodeAndCompareBase64_A(testsNoCR[i].base64, CERT_HEADER, CERT_TRAILER,
434 CRYPT_STRING_BASE64_ANY, CRYPT_STRING_BASE64HEADER,
435 testsNoCR[i].toEncode, testsNoCR[i].toEncodeLen);
436 decodeAndCompareBase64_A(testsNoCR[i].base64, CERT_HEADER, CERT_TRAILER,
437 CRYPT_STRING_ANY, CRYPT_STRING_BASE64HEADER, testsNoCR[i].toEncode,
438 testsNoCR[i].toEncodeLen);
444 HMODULE lib = GetModuleHandleA("crypt32");
446 pCryptBinaryToStringA = (void *)GetProcAddress(lib, "CryptBinaryToStringA");
447 pCryptStringToBinaryA = (void *)GetProcAddress(lib, "CryptStringToBinaryA");
449 if (pCryptBinaryToStringA)
450 testBinaryToStringA();
452 win_skip("CryptBinaryToStringA is not available\n");
454 if (pCryptStringToBinaryA)
455 testStringToBinaryA();
457 win_skip("CryptStringToBinaryA is not available\n");