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 CERT_TRAILER "-----END CERTIFICATE-----\r\n"
32 #define CERT_REQUEST_HEADER "-----BEGIN NEW CERTIFICATE REQUEST-----\r\n"
33 #define CERT_REQUEST_TRAILER "-----END NEW CERTIFICATE REQUEST-----\r\n"
34 #define X509_HEADER "-----BEGIN X509 CRL-----\r\n"
35 #define X509_TRAILER "-----END X509 CRL-----\r\n"
36 #define CERT_HEADER_NOCR "-----BEGIN CERTIFICATE-----\n"
37 #define CERT_TRAILER_NOCR "-----END CERTIFICATE-----\n"
38 #define CERT_REQUEST_HEADER_NOCR "-----BEGIN NEW CERTIFICATE REQUEST-----\n"
39 #define CERT_REQUEST_TRAILER_NOCR "-----END NEW CERTIFICATE REQUEST-----\n"
40 #define X509_HEADER_NOCR "-----BEGIN X509 CRL-----\n"
41 #define X509_TRAILER_NOCR "-----END X509 CRL-----\n"
43 typedef BOOL (WINAPI *CryptBinaryToStringAFunc)(const BYTE *pbBinary,
44 DWORD cbBinary, DWORD dwFlags, LPSTR pszString, DWORD *pcchString);
45 typedef BOOL (WINAPI *CryptStringToBinaryAFunc)(LPCSTR pszString,
46 DWORD cchString, DWORD dwFlags, BYTE *pbBinary, DWORD *pcbBinary,
47 DWORD *pdwSkip, DWORD *pdwFlags);
49 CryptBinaryToStringAFunc pCryptBinaryToStringA;
50 CryptStringToBinaryAFunc pCryptStringToBinaryA;
59 static const BYTE toEncode1[] = { 0 };
60 static const BYTE toEncode2[] = { 1,2 };
61 static const BYTE toEncode3[] = { 1,2,3 };
62 static const BYTE toEncode4[] =
63 "abcdefghijlkmnopqrstuvwxyz01234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890"
64 "abcdefghijlkmnopqrstuvwxyz01234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890"
65 "abcdefghijlkmnopqrstuvwxyz01234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890";
67 struct BinTests tests[] = {
68 { toEncode1, sizeof(toEncode1), "AA==\r\n", },
69 { toEncode2, sizeof(toEncode2), "AQI=\r\n", },
70 /* { toEncode3, sizeof(toEncode3), "AQID\r\n", }, This test fails on Vista. */
71 { toEncode4, sizeof(toEncode4),
72 "YWJjZGVmZ2hpamxrbW5vcHFyc3R1dnd4eXowMTIzNDU2Nzg5MEFCQ0RFRkdISUpL\r\n"
73 "TE1OT1BRUlNUVVZXWFlaMDEyMzQ1Njc4OTBhYmNkZWZnaGlqbGttbm9wcXJzdHV2\r\n"
74 "d3h5ejAxMjM0NTY3ODkwQUJDREVGR0hJSktMTU5PUFFSU1RVVldYWVowMTIzNDU2\r\n"
75 "Nzg5MGFiY2RlZmdoaWpsa21ub3BxcnN0dXZ3eHl6MDEyMzQ1Njc4OTBBQkNERUZH\r\n"
76 "SElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0NTY3ODkwAA==\r\n" },
79 struct BinTests testsNoCR[] = {
80 { toEncode1, sizeof(toEncode1), "AA==\n", },
81 { toEncode2, sizeof(toEncode2), "AQI=\n", },
82 /* { toEncode3, sizeof(toEncode3), "AQID\n", }, This test fails on Vista. */
83 { toEncode4, sizeof(toEncode4),
84 "YWJjZGVmZ2hpamxrbW5vcHFyc3R1dnd4eXowMTIzNDU2Nzg5MEFCQ0RFRkdISUpL\n"
85 "TE1OT1BRUlNUVVZXWFlaMDEyMzQ1Njc4OTBhYmNkZWZnaGlqbGttbm9wcXJzdHV2\n"
86 "d3h5ejAxMjM0NTY3ODkwQUJDREVGR0hJSktMTU5PUFFSU1RVVldYWVowMTIzNDU2\n"
87 "Nzg5MGFiY2RlZmdoaWpsa21ub3BxcnN0dXZ3eHl6MDEyMzQ1Njc4OTBBQkNERUZH\n"
88 "SElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0NTY3ODkwAA==\n" },
91 static void encodeAndCompareBase64_A(const BYTE *toEncode, DWORD toEncodeLen,
92 DWORD format, const char *expected, const char *header, const char *trailer)
98 ret = pCryptBinaryToStringA(toEncode, toEncodeLen, format, NULL, &strLen);
99 ok(ret, "CryptBinaryToStringA failed: %d\n", GetLastError());
100 str = HeapAlloc(GetProcessHeap(), 0, strLen);
103 DWORD strLen2 = strLen;
106 ret = pCryptBinaryToStringA(toEncode, toEncodeLen, format, str,
108 ok(ret, "CryptBinaryToStringA failed: %d\n", GetLastError());
109 ok(strLen2 == strLen - 1, "Expected length %d, got %d\n",
113 ok(!strncmp(header, ptr, strlen(header)),
114 "Expected header %s, got %s\n", header, ptr);
115 ptr += strlen(header);
117 ok(!strncmp(expected, ptr, strlen(expected)),
118 "Expected %s, got %s\n", expected, ptr);
119 ptr += strlen(expected);
122 ok(!strncmp(trailer, ptr, strlen(trailer)),
123 "Expected trailer %s, got %s\n", trailer, ptr);
124 ptr += strlen(trailer);
126 HeapFree(GetProcessHeap(), 0, str);
130 static void testBinaryToStringA(void)
135 ret = pCryptBinaryToStringA(NULL, 0, 0, NULL, NULL);
136 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
137 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
138 ret = pCryptBinaryToStringA(NULL, 0, 0, NULL, &strLen);
139 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
140 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
141 for (i = 0; i < sizeof(tests) / sizeof(tests[0]); i++)
147 ret = pCryptBinaryToStringA(tests[i].toEncode, tests[i].toEncodeLen,
148 CRYPT_STRING_BINARY, NULL, &strLen);
149 ok(ret, "CryptBinaryToStringA failed: %d\n", GetLastError());
150 str = HeapAlloc(GetProcessHeap(), 0, strLen);
153 DWORD strLen2 = strLen;
155 ret = pCryptBinaryToStringA(tests[i].toEncode, tests[i].toEncodeLen,
156 CRYPT_STRING_BINARY, str, &strLen2);
157 ok(strLen == strLen2, "Expected length %d, got %d\n", strLen,
159 ok(!memcmp(str, tests[i].toEncode, tests[i].toEncodeLen),
160 "Unexpected value\n");
161 HeapFree(GetProcessHeap(), 0, str);
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,
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,
175 for (i = 0; i < sizeof(testsNoCR) / sizeof(testsNoCR[0]); i++)
181 ret = pCryptBinaryToStringA(testsNoCR[i].toEncode,
182 testsNoCR[i].toEncodeLen, CRYPT_STRING_BINARY | CRYPT_STRING_NOCR,
184 ok(ret, "CryptBinaryToStringA failed: %d\n", GetLastError());
185 str = HeapAlloc(GetProcessHeap(), 0, strLen);
188 DWORD strLen2 = strLen;
190 ret = pCryptBinaryToStringA(testsNoCR[i].toEncode,
191 testsNoCR[i].toEncodeLen, CRYPT_STRING_BINARY | CRYPT_STRING_NOCR,
193 ok(strLen == strLen2, "Expected length %d, got %d\n", strLen,
195 ok(!memcmp(str, testsNoCR[i].toEncode, testsNoCR[i].toEncodeLen),
196 "Unexpected value\n");
197 HeapFree(GetProcessHeap(), 0, str);
199 encodeAndCompareBase64_A(testsNoCR[i].toEncode,
200 testsNoCR[i].toEncodeLen, CRYPT_STRING_BASE64 | CRYPT_STRING_NOCR,
201 testsNoCR[i].base64, NULL, NULL);
202 encodeAndCompareBase64_A(testsNoCR[i].toEncode,
203 testsNoCR[i].toEncodeLen,
204 CRYPT_STRING_BASE64HEADER | CRYPT_STRING_NOCR, testsNoCR[i].base64,
205 CERT_HEADER_NOCR, CERT_TRAILER_NOCR);
206 encodeAndCompareBase64_A(testsNoCR[i].toEncode,
207 testsNoCR[i].toEncodeLen,
208 CRYPT_STRING_BASE64REQUESTHEADER | CRYPT_STRING_NOCR,
209 testsNoCR[i].base64, CERT_REQUEST_HEADER_NOCR,
210 CERT_REQUEST_TRAILER_NOCR);
211 encodeAndCompareBase64_A(testsNoCR[i].toEncode,
212 testsNoCR[i].toEncodeLen,
213 CRYPT_STRING_BASE64X509CRLHEADER | CRYPT_STRING_NOCR,
214 testsNoCR[i].base64, X509_HEADER_NOCR, X509_TRAILER_NOCR);
218 static void decodeAndCompareBase64_A(LPCSTR toDecode, LPCSTR header,
219 LPCSTR trailer, DWORD useFormat, DWORD expectedFormat, const BYTE *expected,
222 static const char garbage[] = "garbage\r\n";
224 DWORD len = strlen(toDecode) + strlen(garbage) + 1;
227 len += strlen(header);
229 len += strlen(trailer);
230 str = HeapAlloc(GetProcessHeap(), 0, len);
241 strcat(str, toDecode);
243 strcat(str, trailer);
244 ret = pCryptStringToBinaryA(str, 0, useFormat, NULL, &bufLen, NULL,
246 ok(ret, "CryptStringToBinaryA failed: %d\n", GetLastError());
247 buf = HeapAlloc(GetProcessHeap(), 0, bufLen);
250 DWORD skipped, usedFormat;
252 /* check as normal, make sure last two parameters are optional */
253 ret = pCryptStringToBinaryA(str, 0, useFormat, buf, &bufLen, NULL,
255 ok(ret, "CryptStringToBinaryA failed: %d\n", GetLastError());
256 ok(bufLen == expectedLen,
257 "Expected length %d, got %d\n", expectedLen, bufLen);
258 ok(!memcmp(buf, expected, bufLen), "Unexpected value\n");
259 /* check last two params */
260 ret = pCryptStringToBinaryA(str, 0, useFormat, buf, &bufLen,
261 &skipped, &usedFormat);
262 ok(ret, "CryptStringToBinaryA failed: %d\n", GetLastError());
263 ok(skipped == 0, "Expected skipped 0, got %d\n", skipped);
264 ok(usedFormat == expectedFormat, "Expected format %d, got %d\n",
265 expectedFormat, usedFormat);
266 HeapFree(GetProcessHeap(), 0, buf);
269 /* Check again, but with garbage up front */
270 strcpy(str, garbage);
273 strcat(str, toDecode);
275 strcat(str, trailer);
276 ret = pCryptStringToBinaryA(str, 0, useFormat, NULL, &bufLen, NULL,
278 /* expect failure with no header, and success with one */
280 ok(ret, "CryptStringToBinaryA failed: %d\n", GetLastError());
282 ok(!ret && GetLastError() == ERROR_INVALID_DATA,
283 "Expected !ret and last error ERROR_INVALID_DATA, got ret=%d, error=%d\n", ret, GetLastError());
286 buf = HeapAlloc(GetProcessHeap(), 0, bufLen);
289 DWORD skipped, usedFormat;
291 ret = pCryptStringToBinaryA(str, 0, useFormat, buf, &bufLen,
292 &skipped, &usedFormat);
293 ok(skipped == strlen(garbage),
294 "Expected %d characters of \"%s\" skipped when trying format %08x, got %d (used format is %08x)\n",
295 lstrlenA(garbage), str, useFormat, skipped, usedFormat);
296 HeapFree(GetProcessHeap(), 0, buf);
299 HeapFree(GetProcessHeap(), 0, str);
309 struct BadString badStrings[] = {
310 { "A\r\nA\r\n=\r\n=\r\n", CRYPT_STRING_BASE64 },
311 { "AA\r\n=\r\n=\r\n", CRYPT_STRING_BASE64 },
312 { "AA=\r\n=\r\n", CRYPT_STRING_BASE64 },
313 { "-----BEGIN X509 CRL-----\r\nAA==\r\n", CRYPT_STRING_BASE64X509CRLHEADER },
316 static void testStringToBinaryA(void)
321 ret = pCryptStringToBinaryA(NULL, 0, 0, NULL, NULL, NULL, NULL);
322 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
323 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
324 ret = pCryptStringToBinaryA(NULL, 0, 0, NULL, &bufLen, NULL, NULL);
325 ok(!ret && GetLastError() == ERROR_INVALID_PARAMETER,
326 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
328 ret = pCryptStringToBinaryA(tests[0].base64, 0, 0, NULL, &bufLen, NULL,
330 ok(!ret && GetLastError() == ERROR_INVALID_DATA,
331 "Expected ERROR_INVALID_DATA, got %d\n", GetLastError());
332 /* Decoding doesn't expect the NOCR flag to be specified */
333 ret = pCryptStringToBinaryA(tests[0].base64, 1,
334 CRYPT_STRING_BASE64 | CRYPT_STRING_NOCR, NULL, &bufLen, NULL, NULL);
335 ok(!ret && GetLastError() == ERROR_INVALID_DATA,
336 "Expected ERROR_INVALID_DATA, got %d\n", GetLastError());
338 for (i = 0; i < sizeof(badStrings) / sizeof(badStrings[0]); i++)
341 ret = pCryptStringToBinaryA(badStrings[i].str, 0, badStrings[i].format,
342 NULL, &bufLen, NULL, NULL);
343 ok(!ret && GetLastError() == ERROR_INVALID_DATA,
344 "Expected ERROR_INVALID_DATA, got %d\n", GetLastError());
347 for (i = 0; i < sizeof(tests) / sizeof(tests[0]); i++)
350 /* Bogus length--oddly enough, that succeeds, even though it's not
353 ret = pCryptStringToBinaryA(tests[i].base64, 1, CRYPT_STRING_BASE64,
354 NULL, &bufLen, NULL, NULL);
355 todo_wine ok(ret, "CryptStringToBinaryA failed: %d\n", GetLastError());
356 /* Check with the precise format */
357 decodeAndCompareBase64_A(tests[i].base64, NULL, NULL,
358 CRYPT_STRING_BASE64, CRYPT_STRING_BASE64, tests[i].toEncode,
359 tests[i].toEncodeLen);
360 decodeAndCompareBase64_A(tests[i].base64, CERT_HEADER, CERT_TRAILER,
361 CRYPT_STRING_BASE64HEADER, CRYPT_STRING_BASE64HEADER,
362 tests[i].toEncode, tests[i].toEncodeLen);
363 decodeAndCompareBase64_A(tests[i].base64, CERT_REQUEST_HEADER,
364 CERT_REQUEST_TRAILER, CRYPT_STRING_BASE64REQUESTHEADER,
365 CRYPT_STRING_BASE64REQUESTHEADER, tests[i].toEncode,
366 tests[i].toEncodeLen);
367 decodeAndCompareBase64_A(tests[i].base64, X509_HEADER, X509_TRAILER,
368 CRYPT_STRING_BASE64X509CRLHEADER, CRYPT_STRING_BASE64X509CRLHEADER,
369 tests[i].toEncode, tests[i].toEncodeLen);
370 /* And check with the "any" formats */
371 decodeAndCompareBase64_A(tests[i].base64, NULL, NULL,
372 CRYPT_STRING_BASE64_ANY, CRYPT_STRING_BASE64, tests[i].toEncode,
373 tests[i].toEncodeLen);
374 /* Don't check with no header and the string_any format, that'll
377 decodeAndCompareBase64_A(tests[i].base64, CERT_HEADER, CERT_TRAILER,
378 CRYPT_STRING_BASE64_ANY, CRYPT_STRING_BASE64HEADER, tests[i].toEncode,
379 tests[i].toEncodeLen);
380 decodeAndCompareBase64_A(tests[i].base64, CERT_HEADER, CERT_TRAILER,
381 CRYPT_STRING_ANY, CRYPT_STRING_BASE64HEADER, tests[i].toEncode,
382 tests[i].toEncodeLen);
383 /* oddly, these seem to decode using the wrong format
384 decodeAndCompareBase64_A(tests[i].base64, CERT_REQUEST_HEADER,
385 CERT_REQUEST_TRAILER, CRYPT_STRING_BASE64_ANY,
386 CRYPT_STRING_BASE64REQUESTHEADER, tests[i].toEncode,
387 tests[i].toEncodeLen);
388 decodeAndCompareBase64_A(tests[i].base64, CERT_REQUEST_HEADER,
389 CERT_REQUEST_TRAILER, CRYPT_STRING_ANY,
390 CRYPT_STRING_BASE64REQUESTHEADER, tests[i].toEncode,
391 tests[i].toEncodeLen);
392 decodeAndCompareBase64_A(tests[i].base64, X509_HEADER, X509_TRAILER,
393 CRYPT_STRING_BASE64_ANY, CRYPT_STRING_BASE64X509CRLHEADER,
394 tests[i].toEncode, tests[i].toEncodeLen);
395 decodeAndCompareBase64_A(tests[i].base64, X509_HEADER, X509_TRAILER,
396 CRYPT_STRING_ANY, CRYPT_STRING_BASE64X509CRLHEADER, tests[i].toEncode,
397 tests[i].toEncodeLen);
400 /* And again, with no CR--decoding handles this automatically */
401 for (i = 0; i < sizeof(testsNoCR) / sizeof(testsNoCR[0]); i++)
404 /* Bogus length--oddly enough, that succeeds, even though it's not
407 ret = pCryptStringToBinaryA(testsNoCR[i].base64, 1, CRYPT_STRING_BASE64,
408 NULL, &bufLen, NULL, NULL);
409 todo_wine ok(ret, "CryptStringToBinaryA failed: %d\n", GetLastError());
410 /* Check with the precise format */
411 decodeAndCompareBase64_A(testsNoCR[i].base64, NULL, NULL,
412 CRYPT_STRING_BASE64, CRYPT_STRING_BASE64, testsNoCR[i].toEncode,
413 testsNoCR[i].toEncodeLen);
414 decodeAndCompareBase64_A(testsNoCR[i].base64, CERT_HEADER, CERT_TRAILER,
415 CRYPT_STRING_BASE64HEADER, CRYPT_STRING_BASE64HEADER,
416 testsNoCR[i].toEncode, testsNoCR[i].toEncodeLen);
417 decodeAndCompareBase64_A(testsNoCR[i].base64, CERT_REQUEST_HEADER,
418 CERT_REQUEST_TRAILER, CRYPT_STRING_BASE64REQUESTHEADER,
419 CRYPT_STRING_BASE64REQUESTHEADER, testsNoCR[i].toEncode,
420 testsNoCR[i].toEncodeLen);
421 decodeAndCompareBase64_A(testsNoCR[i].base64, X509_HEADER, X509_TRAILER,
422 CRYPT_STRING_BASE64X509CRLHEADER, CRYPT_STRING_BASE64X509CRLHEADER,
423 testsNoCR[i].toEncode, testsNoCR[i].toEncodeLen);
424 /* And check with the "any" formats */
425 decodeAndCompareBase64_A(testsNoCR[i].base64, NULL, NULL,
426 CRYPT_STRING_BASE64_ANY, CRYPT_STRING_BASE64, testsNoCR[i].toEncode,
427 testsNoCR[i].toEncodeLen);
428 /* Don't check with no header and the string_any format, that'll
431 decodeAndCompareBase64_A(testsNoCR[i].base64, CERT_HEADER, CERT_TRAILER,
432 CRYPT_STRING_BASE64_ANY, CRYPT_STRING_BASE64HEADER,
433 testsNoCR[i].toEncode, testsNoCR[i].toEncodeLen);
434 decodeAndCompareBase64_A(testsNoCR[i].base64, CERT_HEADER, CERT_TRAILER,
435 CRYPT_STRING_ANY, CRYPT_STRING_BASE64HEADER, testsNoCR[i].toEncode,
436 testsNoCR[i].toEncodeLen);
442 HMODULE lib = GetModuleHandleA("crypt32");
444 pCryptBinaryToStringA = (CryptBinaryToStringAFunc)GetProcAddress(lib,
445 "CryptBinaryToStringA");
446 pCryptStringToBinaryA = (CryptStringToBinaryAFunc)GetProcAddress(lib,
447 "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");