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", },
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", },
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 if (!pCryptBinaryToStringA
) return;
137 ret
= pCryptBinaryToStringA(NULL
, 0, 0, NULL
, NULL
);
138 ok(!ret
&& GetLastError() == ERROR_INVALID_PARAMETER
,
139 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
140 ret
= pCryptBinaryToStringA(NULL
, 0, 0, NULL
, &strLen
);
141 ok(!ret
&& GetLastError() == ERROR_INVALID_PARAMETER
,
142 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
143 for (i
= 0; i
< sizeof(tests
) / sizeof(tests
[0]); i
++)
149 ret
= pCryptBinaryToStringA(tests
[i
].toEncode
, tests
[i
].toEncodeLen
,
150 CRYPT_STRING_BINARY
, NULL
, &strLen
);
151 ok(ret
, "CryptBinaryToStringA failed: %d\n", GetLastError());
152 str
= HeapAlloc(GetProcessHeap(), 0, strLen
);
155 DWORD strLen2
= strLen
;
157 ret
= pCryptBinaryToStringA(tests
[i
].toEncode
, tests
[i
].toEncodeLen
,
158 CRYPT_STRING_BINARY
, str
, &strLen2
);
159 ok(strLen
== strLen2
, "Expected length %d, got %d\n", strLen
,
161 ok(!memcmp(str
, tests
[i
].toEncode
, tests
[i
].toEncodeLen
),
162 "Unexpected value\n");
163 HeapFree(GetProcessHeap(), 0, str
);
165 encodeAndCompareBase64_A(tests
[i
].toEncode
, tests
[i
].toEncodeLen
,
166 CRYPT_STRING_BASE64
, tests
[i
].base64
, NULL
, NULL
);
167 encodeAndCompareBase64_A(tests
[i
].toEncode
, tests
[i
].toEncodeLen
,
168 CRYPT_STRING_BASE64HEADER
, tests
[i
].base64
, CERT_HEADER
,
170 encodeAndCompareBase64_A(tests
[i
].toEncode
, tests
[i
].toEncodeLen
,
171 CRYPT_STRING_BASE64REQUESTHEADER
, tests
[i
].base64
,
172 CERT_REQUEST_HEADER
, CERT_REQUEST_TRAILER
);
173 encodeAndCompareBase64_A(tests
[i
].toEncode
, tests
[i
].toEncodeLen
,
174 CRYPT_STRING_BASE64X509CRLHEADER
, tests
[i
].base64
, X509_HEADER
,
177 for (i
= 0; i
< sizeof(testsNoCR
) / sizeof(testsNoCR
[0]); i
++)
183 ret
= pCryptBinaryToStringA(testsNoCR
[i
].toEncode
,
184 testsNoCR
[i
].toEncodeLen
, CRYPT_STRING_BINARY
| CRYPT_STRING_NOCR
,
186 ok(ret
, "CryptBinaryToStringA failed: %d\n", GetLastError());
187 str
= HeapAlloc(GetProcessHeap(), 0, strLen
);
190 DWORD strLen2
= strLen
;
192 ret
= pCryptBinaryToStringA(testsNoCR
[i
].toEncode
,
193 testsNoCR
[i
].toEncodeLen
, CRYPT_STRING_BINARY
| CRYPT_STRING_NOCR
,
195 ok(strLen
== strLen2
, "Expected length %d, got %d\n", strLen
,
197 ok(!memcmp(str
, testsNoCR
[i
].toEncode
, testsNoCR
[i
].toEncodeLen
),
198 "Unexpected value\n");
199 HeapFree(GetProcessHeap(), 0, str
);
201 encodeAndCompareBase64_A(testsNoCR
[i
].toEncode
,
202 testsNoCR
[i
].toEncodeLen
, CRYPT_STRING_BASE64
| CRYPT_STRING_NOCR
,
203 testsNoCR
[i
].base64
, NULL
, NULL
);
204 encodeAndCompareBase64_A(testsNoCR
[i
].toEncode
,
205 testsNoCR
[i
].toEncodeLen
,
206 CRYPT_STRING_BASE64HEADER
| CRYPT_STRING_NOCR
, testsNoCR
[i
].base64
,
207 CERT_HEADER_NOCR
, CERT_TRAILER_NOCR
);
208 encodeAndCompareBase64_A(testsNoCR
[i
].toEncode
,
209 testsNoCR
[i
].toEncodeLen
,
210 CRYPT_STRING_BASE64REQUESTHEADER
| CRYPT_STRING_NOCR
,
211 testsNoCR
[i
].base64
, CERT_REQUEST_HEADER_NOCR
,
212 CERT_REQUEST_TRAILER_NOCR
);
213 encodeAndCompareBase64_A(testsNoCR
[i
].toEncode
,
214 testsNoCR
[i
].toEncodeLen
,
215 CRYPT_STRING_BASE64X509CRLHEADER
| CRYPT_STRING_NOCR
,
216 testsNoCR
[i
].base64
, X509_HEADER_NOCR
, X509_TRAILER_NOCR
);
220 static void decodeAndCompareBase64_A(LPCSTR toDecode
, LPCSTR header
,
221 LPCSTR trailer
, DWORD useFormat
, DWORD expectedFormat
, const BYTE
*expected
,
224 static const char garbage
[] = "garbage\r\n";
226 DWORD len
= strlen(toDecode
) + strlen(garbage
) + 1;
229 len
+= strlen(header
);
231 len
+= strlen(trailer
);
232 str
= HeapAlloc(GetProcessHeap(), 0, len
);
243 strcat(str
, toDecode
);
245 strcat(str
, trailer
);
246 ret
= pCryptStringToBinaryA(str
, 0, useFormat
, NULL
, &bufLen
, NULL
,
248 ok(ret
, "CryptStringToBinaryA failed: %d\n", GetLastError());
249 buf
= HeapAlloc(GetProcessHeap(), 0, bufLen
);
252 DWORD skipped
, usedFormat
;
254 /* check as normal, make sure last two parameters are optional */
255 ret
= pCryptStringToBinaryA(str
, 0, useFormat
, buf
, &bufLen
, NULL
,
257 ok(ret
, "CryptStringToBinaryA failed: %d\n", GetLastError());
258 ok(bufLen
== expectedLen
,
259 "Expected length %d, got %d\n", expectedLen
, bufLen
);
260 ok(!memcmp(buf
, expected
, bufLen
), "Unexpected value\n");
261 /* check last two params */
262 ret
= pCryptStringToBinaryA(str
, 0, useFormat
, buf
, &bufLen
,
263 &skipped
, &usedFormat
);
264 ok(ret
, "CryptStringToBinaryA failed: %d\n", GetLastError());
265 ok(skipped
== 0, "Expected skipped 0, got %d\n", skipped
);
266 ok(usedFormat
== expectedFormat
, "Expected format %d, got %d\n",
267 expectedFormat
, usedFormat
);
268 HeapFree(GetProcessHeap(), 0, buf
);
271 /* Check again, but with garbage up front */
272 strcpy(str
, garbage
);
275 strcat(str
, toDecode
);
277 strcat(str
, trailer
);
278 ret
= pCryptStringToBinaryA(str
, 0, useFormat
, NULL
, &bufLen
, NULL
,
280 /* expect failure with no header, and success with one */
282 ok(ret
, "CryptStringToBinaryA failed: %d\n", GetLastError());
284 ok(!ret
&& GetLastError() == ERROR_INVALID_DATA
,
285 "Expected ERROR_INVALID_DATA, got %d\n", GetLastError());
288 buf
= HeapAlloc(GetProcessHeap(), 0, bufLen
);
291 DWORD skipped
, usedFormat
;
293 ret
= pCryptStringToBinaryA(str
, 0, useFormat
, buf
, &bufLen
,
294 &skipped
, &usedFormat
);
295 ok(skipped
== strlen(garbage
),
296 "Expected %d characters skipped, got %d\n", lstrlenA(garbage
),
298 HeapFree(GetProcessHeap(), 0, buf
);
301 HeapFree(GetProcessHeap(), 0, str
);
311 struct BadString badStrings
[] = {
312 { "A\r\nA\r\n=\r\n=\r\n", CRYPT_STRING_BASE64
},
313 { "AA\r\n=\r\n=\r\n", CRYPT_STRING_BASE64
},
314 { "AA=\r\n=\r\n", CRYPT_STRING_BASE64
},
315 { "-----BEGIN X509 CRL-----\r\nAA==\r\n", CRYPT_STRING_BASE64X509CRLHEADER
},
318 static void testStringToBinaryA(void)
323 if (!pCryptStringToBinaryA
) return;
325 ret
= pCryptStringToBinaryA(NULL
, 0, 0, NULL
, NULL
, NULL
, NULL
);
326 ok(!ret
&& GetLastError() == ERROR_INVALID_PARAMETER
,
327 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
328 ret
= pCryptStringToBinaryA(NULL
, 0, 0, NULL
, &bufLen
, NULL
, NULL
);
329 ok(!ret
&& GetLastError() == ERROR_INVALID_PARAMETER
,
330 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
332 ret
= pCryptStringToBinaryA(tests
[0].base64
, 0, 0, NULL
, &bufLen
, NULL
,
334 ok(!ret
&& GetLastError() == ERROR_INVALID_DATA
,
335 "Expected ERROR_INVALID_DATA, got %d\n", GetLastError());
336 /* Decoding doesn't expect the NOCR flag to be specified */
337 ret
= pCryptStringToBinaryA(tests
[0].base64
, 1,
338 CRYPT_STRING_BASE64
| CRYPT_STRING_NOCR
, NULL
, &bufLen
, NULL
, NULL
);
339 ok(!ret
&& GetLastError() == ERROR_INVALID_DATA
,
340 "Expected ERROR_INVALID_DATA, got %d\n", GetLastError());
342 for (i
= 0; i
< sizeof(badStrings
) / sizeof(badStrings
[0]); i
++)
345 ret
= pCryptStringToBinaryA(badStrings
[i
].str
, 0, badStrings
[i
].format
,
346 NULL
, &bufLen
, NULL
, NULL
);
347 ok(!ret
&& GetLastError() == ERROR_INVALID_DATA
,
348 "Expected ERROR_INVALID_DATA, got %d\n", GetLastError());
351 for (i
= 0; i
< sizeof(tests
) / sizeof(tests
[0]); i
++)
354 /* Bogus length--oddly enough, that succeeds, even though it's not
357 ret
= pCryptStringToBinaryA(tests
[i
].base64
, 1, CRYPT_STRING_BASE64
,
358 NULL
, &bufLen
, NULL
, NULL
);
359 todo_wine
ok(ret
, "CryptStringToBinaryA failed: %d\n", GetLastError());
360 /* Check with the precise format */
361 decodeAndCompareBase64_A(tests
[i
].base64
, NULL
, NULL
,
362 CRYPT_STRING_BASE64
, CRYPT_STRING_BASE64
, tests
[i
].toEncode
,
363 tests
[i
].toEncodeLen
);
364 decodeAndCompareBase64_A(tests
[i
].base64
, CERT_HEADER
, CERT_TRAILER
,
365 CRYPT_STRING_BASE64HEADER
, CRYPT_STRING_BASE64HEADER
,
366 tests
[i
].toEncode
, tests
[i
].toEncodeLen
);
367 decodeAndCompareBase64_A(tests
[i
].base64
, CERT_REQUEST_HEADER
,
368 CERT_REQUEST_TRAILER
, CRYPT_STRING_BASE64REQUESTHEADER
,
369 CRYPT_STRING_BASE64REQUESTHEADER
, tests
[i
].toEncode
,
370 tests
[i
].toEncodeLen
);
371 decodeAndCompareBase64_A(tests
[i
].base64
, X509_HEADER
, X509_TRAILER
,
372 CRYPT_STRING_BASE64X509CRLHEADER
, CRYPT_STRING_BASE64X509CRLHEADER
,
373 tests
[i
].toEncode
, tests
[i
].toEncodeLen
);
374 /* And check with the "any" formats */
375 decodeAndCompareBase64_A(tests
[i
].base64
, NULL
, NULL
,
376 CRYPT_STRING_BASE64_ANY
, CRYPT_STRING_BASE64
, tests
[i
].toEncode
,
377 tests
[i
].toEncodeLen
);
378 /* Don't check with no header and the string_any format, that'll
381 decodeAndCompareBase64_A(tests
[i
].base64
, CERT_HEADER
, CERT_TRAILER
,
382 CRYPT_STRING_BASE64_ANY
, CRYPT_STRING_BASE64HEADER
, tests
[i
].toEncode
,
383 tests
[i
].toEncodeLen
);
384 decodeAndCompareBase64_A(tests
[i
].base64
, CERT_HEADER
, CERT_TRAILER
,
385 CRYPT_STRING_ANY
, CRYPT_STRING_BASE64HEADER
, tests
[i
].toEncode
,
386 tests
[i
].toEncodeLen
);
387 /* oddly, these seem to decode using the wrong format
388 decodeAndCompareBase64_A(tests[i].base64, CERT_REQUEST_HEADER,
389 CERT_REQUEST_TRAILER, CRYPT_STRING_BASE64_ANY,
390 CRYPT_STRING_BASE64REQUESTHEADER, tests[i].toEncode,
391 tests[i].toEncodeLen);
392 decodeAndCompareBase64_A(tests[i].base64, CERT_REQUEST_HEADER,
393 CERT_REQUEST_TRAILER, CRYPT_STRING_ANY,
394 CRYPT_STRING_BASE64REQUESTHEADER, tests[i].toEncode,
395 tests[i].toEncodeLen);
396 decodeAndCompareBase64_A(tests[i].base64, X509_HEADER, X509_TRAILER,
397 CRYPT_STRING_BASE64_ANY, CRYPT_STRING_BASE64X509CRLHEADER,
398 tests[i].toEncode, tests[i].toEncodeLen);
399 decodeAndCompareBase64_A(tests[i].base64, X509_HEADER, X509_TRAILER,
400 CRYPT_STRING_ANY, CRYPT_STRING_BASE64X509CRLHEADER, tests[i].toEncode,
401 tests[i].toEncodeLen);
404 /* And again, with no CR--decoding handles this automatically */
405 for (i
= 0; i
< sizeof(testsNoCR
) / sizeof(testsNoCR
[0]); i
++)
408 /* Bogus length--oddly enough, that succeeds, even though it's not
411 ret
= pCryptStringToBinaryA(testsNoCR
[i
].base64
, 1, CRYPT_STRING_BASE64
,
412 NULL
, &bufLen
, NULL
, NULL
);
413 todo_wine
ok(ret
, "CryptStringToBinaryA failed: %d\n", GetLastError());
414 /* Check with the precise format */
415 decodeAndCompareBase64_A(testsNoCR
[i
].base64
, NULL
, NULL
,
416 CRYPT_STRING_BASE64
, CRYPT_STRING_BASE64
, testsNoCR
[i
].toEncode
,
417 testsNoCR
[i
].toEncodeLen
);
418 decodeAndCompareBase64_A(testsNoCR
[i
].base64
, CERT_HEADER
, CERT_TRAILER
,
419 CRYPT_STRING_BASE64HEADER
, CRYPT_STRING_BASE64HEADER
,
420 testsNoCR
[i
].toEncode
, testsNoCR
[i
].toEncodeLen
);
421 decodeAndCompareBase64_A(testsNoCR
[i
].base64
, CERT_REQUEST_HEADER
,
422 CERT_REQUEST_TRAILER
, CRYPT_STRING_BASE64REQUESTHEADER
,
423 CRYPT_STRING_BASE64REQUESTHEADER
, testsNoCR
[i
].toEncode
,
424 testsNoCR
[i
].toEncodeLen
);
425 decodeAndCompareBase64_A(testsNoCR
[i
].base64
, X509_HEADER
, X509_TRAILER
,
426 CRYPT_STRING_BASE64X509CRLHEADER
, CRYPT_STRING_BASE64X509CRLHEADER
,
427 testsNoCR
[i
].toEncode
, testsNoCR
[i
].toEncodeLen
);
428 /* And check with the "any" formats */
429 decodeAndCompareBase64_A(testsNoCR
[i
].base64
, NULL
, NULL
,
430 CRYPT_STRING_BASE64_ANY
, CRYPT_STRING_BASE64
, testsNoCR
[i
].toEncode
,
431 testsNoCR
[i
].toEncodeLen
);
432 /* Don't check with no header and the string_any format, that'll
435 decodeAndCompareBase64_A(testsNoCR
[i
].base64
, CERT_HEADER
, CERT_TRAILER
,
436 CRYPT_STRING_BASE64_ANY
, CRYPT_STRING_BASE64HEADER
,
437 testsNoCR
[i
].toEncode
, testsNoCR
[i
].toEncodeLen
);
438 decodeAndCompareBase64_A(testsNoCR
[i
].base64
, CERT_HEADER
, CERT_TRAILER
,
439 CRYPT_STRING_ANY
, CRYPT_STRING_BASE64HEADER
, testsNoCR
[i
].toEncode
,
440 testsNoCR
[i
].toEncodeLen
);
446 HMODULE lib
= LoadLibraryA("crypt32");
450 pCryptBinaryToStringA
= (CryptBinaryToStringAFunc
)GetProcAddress(lib
,
451 "CryptBinaryToStringA");
452 pCryptStringToBinaryA
= (CryptStringToBinaryAFunc
)GetProcAddress(lib
,
453 "CryptStringToBinaryA");
455 testBinaryToStringA();
456 testStringToBinaryA();