crypt32/tests: Add a trailing '\n' to ok() calls.
[wine] / dlls / crypt32 / tests / object.c
1 /*
2  * Unit test suite for crypt32.dll's Crypt*Object functions
3  *
4  * Copyright 2008 Juan Lang
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20 #include <stdio.h>
21 #include <stdarg.h>
22 #include <windef.h>
23 #include <winbase.h>
24 #include <winerror.h>
25 #include <wincrypt.h>
26
27 #include "wine/test.h"
28
29 static BOOL (WINAPI * pCryptQueryObject)(DWORD, const void *, DWORD, DWORD,
30  DWORD, DWORD *, DWORD *, DWORD *, HCERTSTORE *, HCRYPTMSG *, const void **);
31
32 static BYTE bigCert[] = {
33 0x30,0x7a,0x02,0x01,0x01,0x30,0x02,0x06,0x00,0x30,0x15,0x31,
34 0x13,0x30,0x11,0x06,0x03,0x55,0x04,0x03,0x13,0x0a,0x4a,0x75,
35 0x61,0x6e,0x20,0x4c,0x61,0x6e,0x67,0x00,0x30,0x22,0x18,0x0f,
36 0x31,0x36,0x30,0x31,0x30,0x31,0x30,0x31,0x30,0x30,0x30,0x30,
37 0x30,0x30,0x5a,0x18,0x0f,0x31,0x36,0x30,0x31,0x30,0x31,0x30,
38 0x31,0x30,0x30,0x30,0x30,0x30,0x30,0x5a,0x30,0x15,0x31,0x13,
39 0x30,0x11,0x06,0x03,0x55,0x04,0x03,0x13,0x0a,0x4a,0x75,0x61,
40 0x6e,0x20,0x4c,0x61,0x6e,0x67,0x00,0x30,0x07,0x30,0x02,0x06,
41 0x00,0x03,0x01,0x00,0xa3,0x16,0x30,0x14,0x30,0x12,0x06,0x03,
42 0x55,0x1d,0x13,0x01,0x01,0xff,0x04,0x08,0x30,0x06,0x01,0x01,
43 0xff,0x02,0x01,0x01 };
44 static char bigCertBase64[] =
45 "MHoCAQEwAgYAMBUxEzARBgNVBAMTCkp1YW4gTGFuZwAwIhgPMTYwMTAxMDEwMDAw\n"
46 "MDBaGA8xNjAxMDEwMTAwMDAwMFowFTETMBEGA1UEAxMKSnVhbiBMYW5nADAHMAIG\n"
47 "AAMBAKMWMBQwEgYDVR0TAQH/BAgwBgEB/wIBAQ==\n";
48 static WCHAR bigCertBase64W[] = {
49 'M','H','o','C','A','Q','E','w','A','g','Y','A','M','B','U','x','E','z','A',
50 'R','B','g','N','V','B','A','M','T','C','k','p','1','Y','W','4','g','T','G',
51 'F','u','Z','w','A','w','I','h','g','P','M','T','Y','w','M','T','A','x','M',
52 'D','E','w',',','D','A','w','\n',
53 'M','D','B','a','G','A','8','x','N','j','A','x','M','D','E','w','M','T','A',
54 'w','M','D','A','w','M','F','o','w','F','T','E','T','M','B','E','G','A','1',
55 'U','E','A','x','M','K','S','n','V','h','b','i','B','M','Y','W','5','n','A',
56 'D','A','H','M','A','I','G','\n',
57 'A','A','M','B','A','K','M','W','M','B','Q','w','E','g','Y','D','V','R','0',
58 'T','A','Q','H','/','B','A','g','w','B','g','E','B','/','w','I','B','A','Q',
59 '=','=','\n',0 };
60 static BYTE signedWithCertWithValidPubKeyContent[] = {
61 0x30,0x82,0x01,0x89,0x06,0x09,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,0x07,0x02,
62 0xa0,0x82,0x01,0x7a,0x30,0x82,0x01,0x76,0x02,0x01,0x01,0x31,0x0e,0x30,0x0c,
63 0x06,0x08,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x02,0x05,0x05,0x00,0x30,0x13,0x06,
64 0x09,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x01,0x07,0x01,0xa0,0x06,0x04,0x04,0x01,
65 0x02,0x03,0x04,0xa0,0x81,0xd2,0x30,0x81,0xcf,0x02,0x01,0x01,0x30,0x02,0x06,
66 0x00,0x30,0x15,0x31,0x13,0x30,0x11,0x06,0x03,0x55,0x04,0x03,0x13,0x0a,0x4a,
67 0x75,0x61,0x6e,0x20,0x4c,0x61,0x6e,0x67,0x00,0x30,0x22,0x18,0x0f,0x31,0x36,
68 0x30,0x31,0x30,0x31,0x30,0x31,0x30,0x30,0x30,0x30,0x30,0x30,0x5a,0x18,0x0f,
69 0x31,0x36,0x30,0x31,0x30,0x31,0x30,0x31,0x30,0x30,0x30,0x30,0x30,0x30,0x5a,
70 0x30,0x15,0x31,0x13,0x30,0x11,0x06,0x03,0x55,0x04,0x03,0x13,0x0a,0x4a,0x75,
71 0x61,0x6e,0x20,0x4c,0x61,0x6e,0x67,0x00,0x30,0x5c,0x30,0x0d,0x06,0x09,0x2a,
72 0x86,0x48,0x86,0xf7,0x0d,0x01,0x01,0x01,0x05,0x00,0x03,0x4b,0x00,0x30,0x48,
73 0x02,0x41,0x00,0xe2,0x54,0x3a,0xa7,0x83,0xb1,0x27,0x14,0x3e,0x59,0xbb,0xb4,
74 0x53,0xe6,0x1f,0xe7,0x5d,0xf1,0x21,0x68,0xad,0x85,0x53,0xdb,0x6b,0x1e,0xeb,
75 0x65,0x97,0x03,0x86,0x60,0xde,0xf3,0x6c,0x38,0x75,0xe0,0x4c,0x61,0xbb,0xbc,
76 0x62,0x17,0xa9,0xcd,0x79,0x3f,0x21,0x4e,0x96,0xcb,0x0e,0xdc,0x61,0x94,0x30,
77 0x18,0x10,0x6b,0xd0,0x1c,0x10,0x79,0x02,0x03,0x01,0x00,0x01,0xa3,0x16,0x30,
78 0x14,0x30,0x12,0x06,0x03,0x55,0x1d,0x13,0x01,0x01,0xff,0x04,0x08,0x30,0x06,
79 0x01,0x01,0xff,0x02,0x01,0x01,0x31,0x77,0x30,0x75,0x02,0x01,0x01,0x30,0x1a,
80 0x30,0x15,0x31,0x13,0x30,0x11,0x06,0x03,0x55,0x04,0x03,0x13,0x0a,0x4a,0x75,
81 0x61,0x6e,0x20,0x4c,0x61,0x6e,0x67,0x00,0x02,0x01,0x01,0x30,0x0c,0x06,0x08,
82 0x2a,0x86,0x48,0x86,0xf7,0x0d,0x02,0x05,0x05,0x00,0x30,0x04,0x06,0x00,0x05,
83 0x00,0x04,0x40,0x81,0xa6,0x70,0xb3,0xef,0x59,0xd1,0x66,0xd1,0x9b,0xc0,0x9a,
84 0xb6,0x9a,0x5e,0x6d,0x6f,0x6d,0x0d,0x59,0xa9,0xaa,0x6e,0xe9,0x2c,0xa0,0x1e,
85 0xee,0xc2,0x60,0xbc,0x59,0xbe,0x3f,0x63,0x06,0x8d,0xc9,0x11,0x1d,0x23,0x64,
86 0x92,0xef,0x2e,0xfc,0x57,0x29,0xa4,0xaf,0xe0,0xee,0x93,0x19,0x39,0x51,0xe4,
87 0x44,0xb8,0x0b,0x28,0xf4,0xa8,0x0d };
88 static char signedWithCertWithValidPubKeyContentBase64[] =
89 "MIIBiQYJKoZIhvcNAQcCoIIBejCCAXYCAQExDjAMBggqhkiG9w0CBQUAMBMGCSqG"
90 "SIb3DQEHAaAGBAQBAgMEoIHSMIHPAgEBMAIGADAVMRMwEQYDVQQDEwpKdWFuIExh"
91 "bmcAMCIYDzE2MDEwMTAxMDAwMDAwWhgPMTYwMTAxMDEwMDAwMDBaMBUxEzARBgNV"
92 "BAMTCkp1YW4gTGFuZwAwXDANBgkqhkiG9w0BAQEFAANLADBIAkEA4lQ6p4OxJxQ+"
93 "Wbu0U+Yf513xIWithVPbax7rZZcDhmDe82w4deBMYbu8YhepzXk/IU6Wyw7cYZQw"
94 "GBBr0BwQeQIDAQABoxYwFDASBgNVHRMBAf8ECDAGAQH/AgEBMXcwdQIBATAaMBUx"
95 "EzARBgNVBAMTCkp1YW4gTGFuZwACAQEwDAYIKoZIhvcNAgUFADAEBgAFAARAgaZw"
96 "s+9Z0WbRm8CatppebW9tDVmpqm7pLKAe7sJgvFm+P2MGjckRHSNkku8u/FcppK/g"
97 "7pMZOVHkRLgLKPSoDQ==";
98 static WCHAR signedWithCertWithValidPubKeyContentBase64W[] = {
99 'M','I','I','B','i','Q','Y','J','K','o','Z','I','h','v','c','N','A','Q','c','C',
100 'o','I','I','B','e','j','C','C','A','X','Y','C','A','Q','E','x','D','j','A','M',
101 'B','g','g','q','h','k','i','G','9','w','0','C','B','Q','U','A','M','B','M','G',
102 'C','S','q','G','S','I','b','3','D','Q','E','H','A','a','A','G','B','A','Q','B',
103 'A','g','M','E','o','I','H','S','M','I','H','P','A','g','E','B','M','A','I','G',
104 'A','D','A','V','M','R','M','w','E','Q','Y','D','V','Q','Q','D','E','w','p','K',
105 'd','W','F','u','I','E','x','h','b','m','c','A','M','C','I','Y','D','z','E','2',
106 'M','D','E','w','M','T','A','x','M','D','A','w','M','D','A','w','W','h','g','P',
107 'M','T','Y','w','M','T','A','x','M','D','E','w','M','D','A','w','M','D','B','a',
108 'M','B','U','x','E','z','A','R','B','g','N','V','B','A','M','T','C','k','p','1',
109 'Y','W','4','g','T','G','F','u','Z','w','A','w','X','D','A','N','B','g','k','q',
110 'h','k','i','G','9','w','0','B','A','Q','E','F','A','A','N','L','A','D','B','I',
111 'A','k','E','A','4','l','Q','6','p','4','O','x','J','x','Q','+','W','b','u','0',
112 'U','+','Y','f','5','1','3','x','I','W','i','t','h','V','P','b','a','x','7','r',
113 'Z','Z','c','D','h','m','D','e','8','2','w','4','d','e','B','M','Y','b','u','8',
114 'Y','h','e','p','z','X','k','/','I','U','6','W','y','w','7','c','Y','Z','Q','w',
115 'G','B','B','r','0','B','w','Q','e','Q','I','D','A','Q','A','B','o','x','Y','w',
116 'F','D','A','S','B','g','N','V','H','R','M','B','A','f','8','E','C','D','A','G',
117 'A','Q','H','/','A','g','E','B','M','X','c','w','d','Q','I','B','A','T','A','a',
118 'M','B','U','x','E','z','A','R','B','g','N','V','B','A','M','T','C','k','p','1',
119 'Y','W','4','g','T','G','F','u','Z','w','A','C','A','Q','E','w','D','A','Y','I',
120 'K','o','Z','I','h','v','c','N','A','g','U','F','A','D','A','E','B','g','A','F',
121 'A','A','R','A','g','a','Z','w','s','+','9','Z','0','W','b','R','m','8','C','a',
122 't','p','p','e','b','W','9','t','D','V','m','p','q','m','7','p','L','K','A','e',
123 '7','s','J','g','v','F','m','+','P','2','M','G','j','c','k','R','H','S','N','k',
124 'k','u','8','u','/','F','c','p','p','K','/','g','7','p','M','Z','O','V','H','k',
125 'R','L','g','L','K','P','S','o','D','Q','=','=',0 };
126
127 static void test_query_object(void)
128 {
129     BOOL ret;
130     CRYPT_DATA_BLOB blob;
131
132     /* Test the usual invalid arguments */
133     SetLastError(0xdeadbeef);
134     ret = pCryptQueryObject(0, NULL, 0, 0, 0, NULL, NULL, NULL, NULL, NULL,
135      NULL);
136     ok(!ret && GetLastError() == E_INVALIDARG,
137      "expected E_INVALIDARG, got %08x\n", GetLastError());
138     SetLastError(0xdeadbeef);
139     ret = pCryptQueryObject(CERT_QUERY_OBJECT_BLOB, NULL, 0, 0, 0, NULL, NULL,
140      NULL, NULL, NULL, NULL);
141     ok(!ret && GetLastError() == E_INVALIDARG,
142      "expected E_INVALIDARG, got %08x\n", GetLastError());
143     /* Test with a simple cert */
144     blob.pbData = bigCert;
145     blob.cbData = sizeof(bigCert);
146     SetLastError(0xdeadbeef);
147     ret = pCryptQueryObject(CERT_QUERY_OBJECT_BLOB, &blob,
148      CERT_QUERY_CONTENT_FLAG_ALL, CERT_QUERY_FORMAT_FLAG_ALL, 0, NULL, NULL,
149      NULL, NULL, NULL, NULL);
150     ok(ret, "CryptQueryObject failed: %08x\n", GetLastError());
151     /* The same cert, base64-encoded */
152     blob.pbData = (BYTE *)bigCertBase64;
153     blob.cbData = sizeof(bigCertBase64);
154     SetLastError(0xdeadbeef);
155     ret = pCryptQueryObject(CERT_QUERY_OBJECT_BLOB, &blob,
156      CERT_QUERY_CONTENT_FLAG_ALL, CERT_QUERY_FORMAT_FLAG_ALL, 0, NULL, NULL,
157      NULL, NULL, NULL, NULL);
158     ok(ret, "CryptQueryObject failed: %08x\n", GetLastError());
159     /* The same base64-encoded cert, restricting the format types */
160     SetLastError(0xdeadbeef);
161     ret = pCryptQueryObject(CERT_QUERY_OBJECT_BLOB, &blob,
162      CERT_QUERY_CONTENT_FLAG_ALL, CERT_QUERY_FORMAT_FLAG_BINARY, 0, NULL, NULL,
163      NULL, NULL, NULL, NULL);
164     ok(!ret && GetLastError() == CRYPT_E_NO_MATCH,
165      "expected CRYPT_E_NO_MATCH, got %08x\n", GetLastError());
166     SetLastError(0xdeadbeef);
167     ret = pCryptQueryObject(CERT_QUERY_OBJECT_BLOB, &blob,
168      CERT_QUERY_CONTENT_FLAG_ALL, CERT_QUERY_FORMAT_FLAG_BASE64_ENCODED, 0,
169      NULL, NULL, NULL, NULL, NULL, NULL);
170     ok(ret, "CryptQueryObject failed: %08x\n", GetLastError());
171     /* The same cert, base64-encoded but as a wide character string */
172     blob.pbData = (BYTE *)bigCertBase64W;
173     blob.cbData = sizeof(bigCertBase64W);
174     SetLastError(0xdeadbeef);
175     ret = pCryptQueryObject(CERT_QUERY_OBJECT_BLOB, &blob,
176      CERT_QUERY_CONTENT_FLAG_ALL, CERT_QUERY_FORMAT_FLAG_ALL, 0, NULL, NULL,
177      NULL, NULL, NULL, NULL);
178     ok(!ret && GetLastError() == CRYPT_E_NO_MATCH,
179      "expected CRYPT_E_NO_MATCH, got %08x\n", GetLastError());
180     /* For brevity, not tested here, but tested on Windows:  same failure
181      * (CRYPT_E_NO_MATCH) when the wide character base64-encoded cert
182      * is written to a file and queried.
183      */
184     /* Test with a valid signed message */
185     blob.pbData = signedWithCertWithValidPubKeyContent;
186     blob.cbData = sizeof(signedWithCertWithValidPubKeyContent);
187     SetLastError(0xdeadbeef);
188     ret = pCryptQueryObject(CERT_QUERY_OBJECT_BLOB, &blob,
189      CERT_QUERY_CONTENT_FLAG_ALL, CERT_QUERY_FORMAT_FLAG_ALL, 0, NULL, NULL,
190      NULL, NULL, NULL, NULL);
191     ok(ret, "CryptQueryObject failed: %08x\n", GetLastError());
192     blob.pbData = (BYTE *)signedWithCertWithValidPubKeyContentBase64;
193     blob.cbData = sizeof(signedWithCertWithValidPubKeyContentBase64);
194     SetLastError(0xdeadbeef);
195     ret = pCryptQueryObject(CERT_QUERY_OBJECT_BLOB, &blob,
196      CERT_QUERY_CONTENT_FLAG_ALL, CERT_QUERY_FORMAT_FLAG_ALL, 0, NULL, NULL,
197      NULL, NULL, NULL, NULL);
198     ok(ret, "CryptQueryObject failed: %08x\n", GetLastError());
199     /* A valid signed message, encoded as a wide character base64 string, can
200      * be queried successfully.
201      */
202     blob.pbData = (BYTE *)signedWithCertWithValidPubKeyContentBase64W;
203     blob.cbData = sizeof(signedWithCertWithValidPubKeyContentBase64W);
204     SetLastError(0xdeadbeef);
205     ret = pCryptQueryObject(CERT_QUERY_OBJECT_BLOB, &blob,
206      CERT_QUERY_CONTENT_FLAG_ALL, CERT_QUERY_FORMAT_FLAG_ALL, 0, NULL, NULL,
207      NULL, NULL, NULL, NULL);
208     ok(ret, "CryptQueryObject failed: %08x\n", GetLastError());
209     SetLastError(0xdeadbeef);
210     ret = pCryptQueryObject(CERT_QUERY_OBJECT_BLOB, &blob,
211      CERT_QUERY_CONTENT_FLAG_ALL, CERT_QUERY_FORMAT_FLAG_BINARY, 0, NULL, NULL,
212      NULL, NULL, NULL, NULL);
213     ok(!ret && GetLastError() == CRYPT_E_NO_MATCH,
214      "expected CRYPT_E_NO_MATCH, got %08x\n", GetLastError());
215     SetLastError(0xdeadbeef);
216     ret = pCryptQueryObject(CERT_QUERY_OBJECT_BLOB, &blob,
217      CERT_QUERY_CONTENT_FLAG_ALL, CERT_QUERY_FORMAT_FLAG_BASE64_ENCODED, 0,
218      NULL, NULL, NULL, NULL, NULL, NULL);
219     ok(ret, "CryptQueryObject failed: %08x\n", GetLastError());
220 }
221
222 START_TEST(object)
223 {
224     HMODULE mod = GetModuleHandleA("crypt32.dll");
225
226     pCryptQueryObject = (void *)GetProcAddress(mod, "CryptQueryObject");
227
228     if (!pCryptQueryObject)
229     {
230         win_skip("CryptQueryObject is not available\n");
231         return;
232     }
233
234     test_query_object();
235 }