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 static BOOL (WINAPI
*pCryptBinaryToStringA
)(const BYTE
*pbBinary
,
44 DWORD cbBinary
, DWORD dwFlags
, LPSTR pszString
, DWORD
*pcchString
);
45 static BOOL (WINAPI
*pCryptStringToBinaryA
)(LPCSTR pszString
,
46 DWORD cchString
, DWORD dwFlags
, BYTE
*pbBinary
, DWORD
*pcbBinary
,
47 DWORD
*pdwSkip
, DWORD
*pdwFlags
);
56 static const BYTE toEncode1
[] = { 0 };
57 static const BYTE toEncode2
[] = { 1,2 };
58 static const BYTE toEncode3
[] = { 1,2,3 };
59 static const BYTE toEncode4
[] =
60 "abcdefghijlkmnopqrstuvwxyz01234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890"
61 "abcdefghijlkmnopqrstuvwxyz01234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890"
62 "abcdefghijlkmnopqrstuvwxyz01234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890";
64 static const struct BinTests tests
[] = {
65 { toEncode1
, sizeof(toEncode1
), "AA==\r\n", },
66 { toEncode2
, sizeof(toEncode2
), "AQI=\r\n", },
67 /* { toEncode3, sizeof(toEncode3), "AQID\r\n", }, This test fails on Vista. */
68 { toEncode4
, sizeof(toEncode4
),
69 "YWJjZGVmZ2hpamxrbW5vcHFyc3R1dnd4eXowMTIzNDU2Nzg5MEFCQ0RFRkdISUpL\r\n"
70 "TE1OT1BRUlNUVVZXWFlaMDEyMzQ1Njc4OTBhYmNkZWZnaGlqbGttbm9wcXJzdHV2\r\n"
71 "d3h5ejAxMjM0NTY3ODkwQUJDREVGR0hJSktMTU5PUFFSU1RVVldYWVowMTIzNDU2\r\n"
72 "Nzg5MGFiY2RlZmdoaWpsa21ub3BxcnN0dXZ3eHl6MDEyMzQ1Njc4OTBBQkNERUZH\r\n"
73 "SElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0NTY3ODkwAA==\r\n" },
76 static const struct BinTests testsNoCR
[] = {
77 { toEncode1
, sizeof(toEncode1
), "AA==\n", },
78 { toEncode2
, sizeof(toEncode2
), "AQI=\n", },
79 /* { toEncode3, sizeof(toEncode3), "AQID\n", }, This test fails on Vista. */
80 { toEncode4
, sizeof(toEncode4
),
81 "YWJjZGVmZ2hpamxrbW5vcHFyc3R1dnd4eXowMTIzNDU2Nzg5MEFCQ0RFRkdISUpL\n"
82 "TE1OT1BRUlNUVVZXWFlaMDEyMzQ1Njc4OTBhYmNkZWZnaGlqbGttbm9wcXJzdHV2\n"
83 "d3h5ejAxMjM0NTY3ODkwQUJDREVGR0hJSktMTU5PUFFSU1RVVldYWVowMTIzNDU2\n"
84 "Nzg5MGFiY2RlZmdoaWpsa21ub3BxcnN0dXZ3eHl6MDEyMzQ1Njc4OTBBQkNERUZH\n"
85 "SElKS0xNTk9QUVJTVFVWV1hZWjAxMjM0NTY3ODkwAA==\n" },
88 static void encodeAndCompareBase64_A(const BYTE
*toEncode
, DWORD toEncodeLen
,
89 DWORD format
, const char *expected
, const char *header
, const char *trailer
)
95 ret
= pCryptBinaryToStringA(toEncode
, toEncodeLen
, format
, NULL
, &strLen
);
96 ok(ret
, "CryptBinaryToStringA failed: %d\n", GetLastError());
97 str
= HeapAlloc(GetProcessHeap(), 0, strLen
);
100 DWORD strLen2
= strLen
;
103 ret
= pCryptBinaryToStringA(toEncode
, toEncodeLen
, format
, str
,
105 ok(ret
, "CryptBinaryToStringA failed: %d\n", GetLastError());
106 ok(strLen2
== strLen
- 1, "Expected length %d, got %d\n",
110 ok(!strncmp(header
, ptr
, strlen(header
)),
111 "Expected header %s, got %s\n", header
, ptr
);
112 ptr
+= strlen(header
);
114 ok(!strncmp(expected
, ptr
, strlen(expected
)),
115 "Expected %s, got %s\n", expected
, ptr
);
116 ptr
+= strlen(expected
);
119 ok(!strncmp(trailer
, ptr
, strlen(trailer
)),
120 "Expected trailer %s, got %s\n", trailer
, ptr
);
121 ptr
+= strlen(trailer
);
123 HeapFree(GetProcessHeap(), 0, str
);
127 static void testBinaryToStringA(void)
132 ret
= pCryptBinaryToStringA(NULL
, 0, 0, NULL
, NULL
);
133 ok(!ret
&& GetLastError() == ERROR_INVALID_PARAMETER
,
134 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
135 ret
= pCryptBinaryToStringA(NULL
, 0, 0, NULL
, &strLen
);
136 ok(!ret
&& GetLastError() == ERROR_INVALID_PARAMETER
,
137 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
138 for (i
= 0; i
< sizeof(tests
) / sizeof(tests
[0]); i
++)
144 ret
= pCryptBinaryToStringA(tests
[i
].toEncode
, tests
[i
].toEncodeLen
,
145 CRYPT_STRING_BINARY
, NULL
, &strLen
);
146 ok(ret
, "CryptBinaryToStringA failed: %d\n", GetLastError());
147 str
= HeapAlloc(GetProcessHeap(), 0, strLen
);
150 DWORD strLen2
= strLen
;
152 ret
= pCryptBinaryToStringA(tests
[i
].toEncode
, tests
[i
].toEncodeLen
,
153 CRYPT_STRING_BINARY
, str
, &strLen2
);
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(strLen
== strLen2
, "Expected length %d, got %d\n", strLen
,
192 ok(!memcmp(str
, testsNoCR
[i
].toEncode
, testsNoCR
[i
].toEncodeLen
),
193 "Unexpected value\n");
194 HeapFree(GetProcessHeap(), 0, str
);
196 encodeAndCompareBase64_A(testsNoCR
[i
].toEncode
,
197 testsNoCR
[i
].toEncodeLen
, CRYPT_STRING_BASE64
| CRYPT_STRING_NOCR
,
198 testsNoCR
[i
].base64
, NULL
, NULL
);
199 encodeAndCompareBase64_A(testsNoCR
[i
].toEncode
,
200 testsNoCR
[i
].toEncodeLen
,
201 CRYPT_STRING_BASE64HEADER
| CRYPT_STRING_NOCR
, testsNoCR
[i
].base64
,
202 CERT_HEADER_NOCR
, CERT_TRAILER_NOCR
);
203 encodeAndCompareBase64_A(testsNoCR
[i
].toEncode
,
204 testsNoCR
[i
].toEncodeLen
,
205 CRYPT_STRING_BASE64REQUESTHEADER
| CRYPT_STRING_NOCR
,
206 testsNoCR
[i
].base64
, CERT_REQUEST_HEADER_NOCR
,
207 CERT_REQUEST_TRAILER_NOCR
);
208 encodeAndCompareBase64_A(testsNoCR
[i
].toEncode
,
209 testsNoCR
[i
].toEncodeLen
,
210 CRYPT_STRING_BASE64X509CRLHEADER
| CRYPT_STRING_NOCR
,
211 testsNoCR
[i
].base64
, X509_HEADER_NOCR
, X509_TRAILER_NOCR
);
215 static void decodeAndCompareBase64_A(LPCSTR toDecode
, LPCSTR header
,
216 LPCSTR trailer
, DWORD useFormat
, DWORD expectedFormat
, const BYTE
*expected
,
219 static const char garbage
[] = "garbage\r\n";
221 DWORD len
= strlen(toDecode
) + strlen(garbage
) + 1;
224 len
+= strlen(header
);
226 len
+= strlen(trailer
);
227 str
= HeapAlloc(GetProcessHeap(), 0, len
);
238 strcat(str
, toDecode
);
240 strcat(str
, trailer
);
241 ret
= pCryptStringToBinaryA(str
, 0, useFormat
, NULL
, &bufLen
, NULL
,
243 ok(ret
, "CryptStringToBinaryA failed: %d\n", GetLastError());
244 buf
= HeapAlloc(GetProcessHeap(), 0, bufLen
);
247 DWORD skipped
, usedFormat
;
249 /* check as normal, make sure last two parameters are optional */
250 ret
= pCryptStringToBinaryA(str
, 0, useFormat
, buf
, &bufLen
, NULL
,
252 ok(ret
, "CryptStringToBinaryA failed: %d\n", GetLastError());
253 ok(bufLen
== expectedLen
,
254 "Expected length %d, got %d\n", expectedLen
, bufLen
);
255 ok(!memcmp(buf
, expected
, bufLen
), "Unexpected value\n");
256 /* check last two params */
257 ret
= pCryptStringToBinaryA(str
, 0, useFormat
, buf
, &bufLen
,
258 &skipped
, &usedFormat
);
259 ok(ret
, "CryptStringToBinaryA failed: %d\n", GetLastError());
260 ok(skipped
== 0, "Expected skipped 0, got %d\n", skipped
);
261 ok(usedFormat
== expectedFormat
, "Expected format %d, got %d\n",
262 expectedFormat
, usedFormat
);
263 HeapFree(GetProcessHeap(), 0, buf
);
266 /* Check again, but with garbage up front */
267 strcpy(str
, garbage
);
270 strcat(str
, toDecode
);
272 strcat(str
, trailer
);
273 ret
= pCryptStringToBinaryA(str
, 0, useFormat
, NULL
, &bufLen
, NULL
,
275 /* expect failure with no header, and success with one */
277 ok(ret
, "CryptStringToBinaryA failed: %d\n", GetLastError());
279 ok(!ret
&& GetLastError() == ERROR_INVALID_DATA
,
280 "Expected !ret and last error ERROR_INVALID_DATA, got ret=%d, error=%d\n", ret
, GetLastError());
283 buf
= HeapAlloc(GetProcessHeap(), 0, bufLen
);
286 DWORD skipped
, usedFormat
;
288 ret
= pCryptStringToBinaryA(str
, 0, useFormat
, buf
, &bufLen
,
289 &skipped
, &usedFormat
);
290 ok(skipped
== strlen(garbage
),
291 "Expected %d characters of \"%s\" skipped when trying format %08x, got %d (used format is %08x)\n",
292 lstrlenA(garbage
), str
, useFormat
, skipped
, usedFormat
);
293 HeapFree(GetProcessHeap(), 0, buf
);
296 HeapFree(GetProcessHeap(), 0, str
);
306 static const struct BadString badStrings
[] = {
307 { "A\r\nA\r\n=\r\n=\r\n", CRYPT_STRING_BASE64
},
308 { "AA\r\n=\r\n=\r\n", CRYPT_STRING_BASE64
},
309 { "AA=\r\n=\r\n", CRYPT_STRING_BASE64
},
310 { "-----BEGIN X509 CRL-----\r\nAA==\r\n", CRYPT_STRING_BASE64X509CRLHEADER
},
313 static void testStringToBinaryA(void)
318 ret
= pCryptStringToBinaryA(NULL
, 0, 0, NULL
, NULL
, NULL
, NULL
);
319 ok(!ret
&& GetLastError() == ERROR_INVALID_PARAMETER
,
320 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
321 ret
= pCryptStringToBinaryA(NULL
, 0, 0, NULL
, &bufLen
, NULL
, NULL
);
322 ok(!ret
&& GetLastError() == ERROR_INVALID_PARAMETER
,
323 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
325 ret
= pCryptStringToBinaryA(tests
[0].base64
, 0, 0, NULL
, &bufLen
, NULL
,
327 ok(!ret
&& GetLastError() == ERROR_INVALID_DATA
,
328 "Expected ERROR_INVALID_DATA, got %d\n", GetLastError());
329 /* Decoding doesn't expect the NOCR flag to be specified */
330 ret
= pCryptStringToBinaryA(tests
[0].base64
, 1,
331 CRYPT_STRING_BASE64
| CRYPT_STRING_NOCR
, NULL
, &bufLen
, NULL
, NULL
);
332 ok(!ret
&& GetLastError() == ERROR_INVALID_DATA
,
333 "Expected ERROR_INVALID_DATA, got %d\n", GetLastError());
335 for (i
= 0; i
< sizeof(badStrings
) / sizeof(badStrings
[0]); i
++)
338 ret
= pCryptStringToBinaryA(badStrings
[i
].str
, 0, badStrings
[i
].format
,
339 NULL
, &bufLen
, NULL
, NULL
);
340 ok(!ret
&& GetLastError() == ERROR_INVALID_DATA
,
341 "Expected ERROR_INVALID_DATA, got %d\n", GetLastError());
344 for (i
= 0; i
< sizeof(tests
) / sizeof(tests
[0]); i
++)
347 /* Bogus length--oddly enough, that succeeds, even though it's not
350 ret
= pCryptStringToBinaryA(tests
[i
].base64
, 1, CRYPT_STRING_BASE64
,
351 NULL
, &bufLen
, NULL
, NULL
);
352 todo_wine
ok(ret
, "CryptStringToBinaryA failed: %d\n", GetLastError());
353 /* Check with the precise format */
354 decodeAndCompareBase64_A(tests
[i
].base64
, NULL
, NULL
,
355 CRYPT_STRING_BASE64
, CRYPT_STRING_BASE64
, tests
[i
].toEncode
,
356 tests
[i
].toEncodeLen
);
357 decodeAndCompareBase64_A(tests
[i
].base64
, CERT_HEADER
, CERT_TRAILER
,
358 CRYPT_STRING_BASE64HEADER
, CRYPT_STRING_BASE64HEADER
,
359 tests
[i
].toEncode
, tests
[i
].toEncodeLen
);
360 decodeAndCompareBase64_A(tests
[i
].base64
, CERT_REQUEST_HEADER
,
361 CERT_REQUEST_TRAILER
, CRYPT_STRING_BASE64REQUESTHEADER
,
362 CRYPT_STRING_BASE64REQUESTHEADER
, tests
[i
].toEncode
,
363 tests
[i
].toEncodeLen
);
364 decodeAndCompareBase64_A(tests
[i
].base64
, X509_HEADER
, X509_TRAILER
,
365 CRYPT_STRING_BASE64X509CRLHEADER
, CRYPT_STRING_BASE64X509CRLHEADER
,
366 tests
[i
].toEncode
, tests
[i
].toEncodeLen
);
367 /* And check with the "any" formats */
368 decodeAndCompareBase64_A(tests
[i
].base64
, NULL
, NULL
,
369 CRYPT_STRING_BASE64_ANY
, CRYPT_STRING_BASE64
, tests
[i
].toEncode
,
370 tests
[i
].toEncodeLen
);
371 /* Don't check with no header and the string_any format, that'll
374 decodeAndCompareBase64_A(tests
[i
].base64
, CERT_HEADER
, CERT_TRAILER
,
375 CRYPT_STRING_BASE64_ANY
, CRYPT_STRING_BASE64HEADER
, tests
[i
].toEncode
,
376 tests
[i
].toEncodeLen
);
377 decodeAndCompareBase64_A(tests
[i
].base64
, CERT_HEADER
, CERT_TRAILER
,
378 CRYPT_STRING_ANY
, CRYPT_STRING_BASE64HEADER
, tests
[i
].toEncode
,
379 tests
[i
].toEncodeLen
);
380 /* oddly, these seem to decode using the wrong format
381 decodeAndCompareBase64_A(tests[i].base64, CERT_REQUEST_HEADER,
382 CERT_REQUEST_TRAILER, CRYPT_STRING_BASE64_ANY,
383 CRYPT_STRING_BASE64REQUESTHEADER, tests[i].toEncode,
384 tests[i].toEncodeLen);
385 decodeAndCompareBase64_A(tests[i].base64, CERT_REQUEST_HEADER,
386 CERT_REQUEST_TRAILER, CRYPT_STRING_ANY,
387 CRYPT_STRING_BASE64REQUESTHEADER, tests[i].toEncode,
388 tests[i].toEncodeLen);
389 decodeAndCompareBase64_A(tests[i].base64, X509_HEADER, X509_TRAILER,
390 CRYPT_STRING_BASE64_ANY, CRYPT_STRING_BASE64X509CRLHEADER,
391 tests[i].toEncode, tests[i].toEncodeLen);
392 decodeAndCompareBase64_A(tests[i].base64, X509_HEADER, X509_TRAILER,
393 CRYPT_STRING_ANY, CRYPT_STRING_BASE64X509CRLHEADER, tests[i].toEncode,
394 tests[i].toEncodeLen);
397 /* And again, with no CR--decoding handles this automatically */
398 for (i
= 0; i
< sizeof(testsNoCR
) / sizeof(testsNoCR
[0]); i
++)
401 /* Bogus length--oddly enough, that succeeds, even though it's not
404 ret
= pCryptStringToBinaryA(testsNoCR
[i
].base64
, 1, CRYPT_STRING_BASE64
,
405 NULL
, &bufLen
, NULL
, NULL
);
406 todo_wine
ok(ret
, "CryptStringToBinaryA failed: %d\n", GetLastError());
407 /* Check with the precise format */
408 decodeAndCompareBase64_A(testsNoCR
[i
].base64
, NULL
, NULL
,
409 CRYPT_STRING_BASE64
, CRYPT_STRING_BASE64
, testsNoCR
[i
].toEncode
,
410 testsNoCR
[i
].toEncodeLen
);
411 decodeAndCompareBase64_A(testsNoCR
[i
].base64
, CERT_HEADER
, CERT_TRAILER
,
412 CRYPT_STRING_BASE64HEADER
, CRYPT_STRING_BASE64HEADER
,
413 testsNoCR
[i
].toEncode
, testsNoCR
[i
].toEncodeLen
);
414 decodeAndCompareBase64_A(testsNoCR
[i
].base64
, CERT_REQUEST_HEADER
,
415 CERT_REQUEST_TRAILER
, CRYPT_STRING_BASE64REQUESTHEADER
,
416 CRYPT_STRING_BASE64REQUESTHEADER
, testsNoCR
[i
].toEncode
,
417 testsNoCR
[i
].toEncodeLen
);
418 decodeAndCompareBase64_A(testsNoCR
[i
].base64
, X509_HEADER
, X509_TRAILER
,
419 CRYPT_STRING_BASE64X509CRLHEADER
, CRYPT_STRING_BASE64X509CRLHEADER
,
420 testsNoCR
[i
].toEncode
, testsNoCR
[i
].toEncodeLen
);
421 /* And check with the "any" formats */
422 decodeAndCompareBase64_A(testsNoCR
[i
].base64
, NULL
, NULL
,
423 CRYPT_STRING_BASE64_ANY
, CRYPT_STRING_BASE64
, testsNoCR
[i
].toEncode
,
424 testsNoCR
[i
].toEncodeLen
);
425 /* Don't check with no header and the string_any format, that'll
428 decodeAndCompareBase64_A(testsNoCR
[i
].base64
, CERT_HEADER
, CERT_TRAILER
,
429 CRYPT_STRING_BASE64_ANY
, CRYPT_STRING_BASE64HEADER
,
430 testsNoCR
[i
].toEncode
, testsNoCR
[i
].toEncodeLen
);
431 decodeAndCompareBase64_A(testsNoCR
[i
].base64
, CERT_HEADER
, CERT_TRAILER
,
432 CRYPT_STRING_ANY
, CRYPT_STRING_BASE64HEADER
, testsNoCR
[i
].toEncode
,
433 testsNoCR
[i
].toEncodeLen
);
439 HMODULE lib
= GetModuleHandleA("crypt32");
441 pCryptBinaryToStringA
= (void *)GetProcAddress(lib
, "CryptBinaryToStringA");
442 pCryptStringToBinaryA
= (void *)GetProcAddress(lib
, "CryptStringToBinaryA");
444 if (pCryptBinaryToStringA
)
445 testBinaryToStringA();
447 win_skip("CryptBinaryToStringA is not available\n");
449 if (pCryptStringToBinaryA
)
450 testStringToBinaryA();
452 win_skip("CryptStringToBinaryA is not available\n");