crypt32: Fix a couple leaks in test.
[wine] / dlls / crypt32 / tests / str.c
1 /*
2  * Unit test suite for crypt32.dll's Cert*ToStr and CertStrToName functions.
3  *
4  * Copyright 2006 Juan Lang, Aric Stewart for CodeWeavers
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 /*#define DUMP_STRINGS*/
30 #ifdef DUMP_STRINGS
31 #include "wine/debug.h"
32 #endif
33
34 typedef struct _CertRDNAttrEncoding {
35     LPCSTR pszObjId;
36     DWORD  dwValueType;
37     CERT_RDN_VALUE_BLOB Value;
38     LPCSTR str;
39 } CertRDNAttrEncoding, *PCertRDNAttrEncoding;
40
41 typedef struct _CertRDNAttrEncodingW {
42     LPCSTR pszObjId;
43     DWORD  dwValueType;
44     CERT_RDN_VALUE_BLOB Value;
45     LPCWSTR str;
46 } CertRDNAttrEncodingW, *PCertRDNAttrEncodingW;
47
48 static BYTE bin1[] = { 0x55, 0x53 };
49 static BYTE bin2[] = { 0x4d, 0x69, 0x6e, 0x6e, 0x65, 0x73, 0x6f, 0x74,
50  0x61 };
51 static BYTE bin3[] = { 0x4d, 0x69, 0x6e, 0x6e, 0x65, 0x61, 0x70, 0x6f,
52  0x6c, 0x69, 0x73 };
53 static BYTE bin4[] = { 0x43, 0x6f, 0x64, 0x65, 0x57, 0x65, 0x61, 0x76,
54  0x65, 0x72, 0x73 };
55 static BYTE bin5[] = { 0x57, 0x69, 0x6e, 0x65, 0x20, 0x44, 0x65, 0x76,
56  0x65, 0x6c, 0x6f, 0x70, 0x6d, 0x65, 0x6e, 0x74 };
57 static BYTE bin6[] = { 0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x68, 0x6f, 0x73,
58  0x74 };
59 static BYTE bin7[] = { 0x61, 0x72, 0x69, 0x63, 0x40, 0x63, 0x6f, 0x64,
60  0x65, 0x77, 0x65, 0x61, 0x76, 0x65, 0x72, 0x73, 0x2e, 0x63, 0x6f, 0x6d };
61
62 static const BYTE cert[] = 
63 {0x30,0x82,0x2,0xbb,0x30,0x82,0x2,0x24,0x2,0x9,0x0,0xe3,0x5a,0x10,0xf1,0xfc,
64  0x4b,0xf3,0xa2,0x30,0xd,0x6,0x9,0x2a,0x86,0x48,0x86,0xf7,0xd,0x1,0x1,0x4,0x5,
65  0x0,0x30,0x81,0xa1,0x31,0xb,0x30,0x9,0x6,0x3,0x55,0x4,0x6,0x13,0x2,0x55,0x53,
66  0x31,0x12,0x30,0x10,0x6,0x3,0x55,0x4,0x8,0x13,0x9,0x4d,0x69,0x6e,0x6e,0x65,
67  0x73,0x6f,0x74,0x61,0x31,0x14,0x30,0x12,0x6,0x3,0x55,0x4,0x7,0x13,0xb,0x4d,
68  0x69,0x6e,0x6e,0x65,0x61,0x70,0x6f,0x6c,0x69,0x73,0x31,0x14,0x30,0x12,0x6,0x3,
69  0x55,0x4,0xa,0x13,0xb,0x43,0x6f,0x64,0x65,0x57,0x65,0x61,0x76,0x65,0x72,0x73,
70  0x31,0x19,0x30,0x17,0x6,0x3,0x55,0x4,0xb,0x13,0x10,0x57,0x69,0x6e,0x65,0x20,
71  0x44,0x65,0x76,0x65,0x6c,0x6f,0x70,0x6d,0x65,0x6e,0x74,0x31,0x12,0x30,0x10,
72  0x6,0x3,0x55,0x4,0x3,0x13,0x9,0x6c,0x6f,0x63,0x61,0x6c,0x68,0x6f,0x73,0x74,
73  0x31,0x23,0x30,0x21,0x6,0x9,0x2a,0x86,0x48,0x86,0xf7,0xd,0x1,0x9,0x1,0x16,
74  0x14,0x61,0x72,0x69,0x63,0x40,0x63,0x6f,0x64,0x65,0x77,0x65,0x61,0x76,0x65,
75  0x72,0x73,0x2e,0x63,0x6f,0x6d,0x30,0x1e,0x17,0xd,0x30,0x36,0x30,0x31,0x32,
76  0x35,0x31,0x33,0x35,0x37,0x32,0x34,0x5a,0x17,0xd,0x30,0x36,0x30,0x32,0x32,
77  0x34,0x31,0x33,0x35,0x37,0x32,0x34,0x5a,0x30,0x81,0xa1,0x31,0xb,0x30,0x9,0x6,
78  0x3,0x55,0x4,0x6,0x13,0x2,0x55,0x53,0x31,0x12,0x30,0x10,0x6,0x3,0x55,0x4,0x8,
79  0x13,0x9,0x4d,0x69,0x6e,0x6e,0x65,0x73,0x6f,0x74,0x61,0x31,0x14,0x30,0x12,0x6,
80  0x3,0x55,0x4,0x7,0x13,0xb,0x4d,0x69,0x6e,0x6e,0x65,0x61,0x70,0x6f,0x6c,0x69,
81  0x73,0x31,0x14,0x30,0x12,0x6,0x3,0x55,0x4,0xa,0x13,0xb,0x43,0x6f,0x64,0x65,
82  0x57,0x65,0x61,0x76,0x65,0x72,0x73,0x31,0x19,0x30,0x17,0x6,0x3,0x55,0x4,0xb,
83  0x13,0x10,0x57,0x69,0x6e,0x65,0x20,0x44,0x65,0x76,0x65,0x6c,0x6f,0x70,0x6d,
84  0x65,0x6e,0x74,0x31,0x12,0x30,0x10,0x6,0x3,0x55,0x4,0x3,0x13,0x9,0x6c,0x6f,
85  0x63,0x61,0x6c,0x68,0x6f,0x73,0x74,0x31,0x23,0x30,0x21,0x6,0x9,0x2a,0x86,0x48,
86  0x86,0xf7,0xd,0x1,0x9,0x1,0x16,0x14,0x61,0x72,0x69,0x63,0x40,0x63,0x6f,0x64,
87  0x65,0x77,0x65,0x61,0x76,0x65,0x72,0x73,0x2e,0x63,0x6f,0x6d,0x30,0x81,0x9f,
88  0x30,0xd,0x6,0x9,0x2a,0x86,0x48,0x86,0xf7,0xd,0x1,0x1,0x1,0x5,0x0,0x3,0x81,
89  0x8d,0x0,0x30,0x81,0x89,0x2,0x81,0x81,0x0,0x9b,0xb5,0x8f,0xaf,0xfb,0x9a,0xaf,
90  0xdc,0xa2,0x4d,0xb1,0xc8,0x72,0x44,0xef,0x79,0x7f,0x28,0xb6,0xfe,0x50,0xdc,
91  0x8a,0xf7,0x11,0x2f,0x90,0x70,0xed,0xa4,0xa9,0xd,0xbf,0x82,0x3e,0x56,0xd8,
92  0x36,0xb6,0x9,0x52,0x83,0xab,0x65,0x95,0x0,0xe2,0xea,0x3c,0x4f,0x85,0xb8,0xc,
93  0x41,0x42,0x77,0x5c,0x9d,0x44,0xeb,0xcf,0x7d,0x60,0x64,0x7a,0x6c,0x4c,0xac,
94  0x4a,0x9a,0x23,0x25,0x15,0xd7,0x92,0xb4,0x10,0xe7,0x95,0xad,0x4b,0x93,0xda,
95  0x6a,0x76,0xe0,0xa5,0xd2,0x13,0x8,0x12,0x30,0x68,0xde,0xb9,0x5b,0x6e,0x2a,
96  0x97,0x43,0xaa,0x7b,0x22,0x33,0x34,0xb1,0xca,0x5d,0x19,0xd8,0x42,0x26,0x45,
97  0xc6,0xe9,0x1d,0xee,0x7,0xc2,0x27,0x95,0x87,0xd8,0x12,0xec,0x4b,0x16,0x9f,0x2,
98  0x3,0x1,0x0,0x1,0x30,0xd,0x6,0x9,0x2a,0x86,0x48,0x86,0xf7,0xd,0x1,0x1,0x4,0x5,
99  0x0,0x3,0x81,0x81,0x0,0x96,0xf9,0xf6,0x6a,0x3d,0xd9,0xca,0x6e,0xd5,0x76,0x73,
100  0xab,0x75,0xc1,0xcc,0x98,0x44,0xc3,0xa9,0x90,0x68,0x88,0x76,0xb9,0xeb,0xb6,
101  0xbe,0x60,0x62,0xb9,0x67,0x1e,0xcc,0xf4,0xe1,0xe7,0x6c,0xc8,0x67,0x3f,0x1d,
102  0xf3,0x68,0x86,0x30,0xee,0xaa,0x92,0x61,0x37,0xd7,0x82,0x90,0x28,0xaa,0x7a,
103  0x18,0x88,0x60,0x14,0x88,0x75,0xc0,0x4a,0x4e,0x7d,0x48,0xe7,0x3,0xa6,0xfd,
104  0xd7,0xce,0x3c,0xe5,0x9b,0xaf,0x2f,0xdc,0xbb,0x7c,0xbd,0x20,0x49,0xd9,0x68,
105  0x37,0xeb,0x5d,0xbb,0xe2,0x6d,0x66,0xe3,0x11,0xc1,0xa7,0x88,0x49,0xc6,0x6f,
106  0x65,0xd3,0xce,0xae,0x26,0x19,0x3,0x2e,0x4f,0x78,0xa5,0xa,0x97,0x7e,0x4f,0xc4,
107  0x91,0x8a,0xf8,0x5,0xef,0x5b,0x3b,0x49,0xbf,0x5f,0x2b};
108
109 static char issuerStr[] =
110  "US, Minnesota, Minneapolis, CodeWeavers, Wine Development, localhost, aric@codeweavers.com";
111 static char issuerStrSemicolon[] =
112  "US; Minnesota; Minneapolis; CodeWeavers; Wine Development; localhost; aric@codeweavers.com";
113 static char issuerStrCRLF[] =
114  "US\r\nMinnesota\r\nMinneapolis\r\nCodeWeavers\r\nWine Development\r\nlocalhost\r\naric@codeweavers.com";
115 static char subjectStr[] =
116  "2.5.4.6=US, 2.5.4.8=Minnesota, 2.5.4.7=Minneapolis, 2.5.4.10=CodeWeavers, 2.5.4.11=Wine Development, 2.5.4.3=localhost, 1.2.840.113549.1.9.1=aric@codeweavers.com";
117 static char subjectStrSemicolon[] =
118  "2.5.4.6=US; 2.5.4.8=Minnesota; 2.5.4.7=Minneapolis; 2.5.4.10=CodeWeavers; 2.5.4.11=Wine Development; 2.5.4.3=localhost; 1.2.840.113549.1.9.1=aric@codeweavers.com";
119 static char subjectStrCRLF[] =
120  "2.5.4.6=US\r\n2.5.4.8=Minnesota\r\n2.5.4.7=Minneapolis\r\n2.5.4.10=CodeWeavers\r\n2.5.4.11=Wine Development\r\n2.5.4.3=localhost\r\n1.2.840.113549.1.9.1=aric@codeweavers.com";
121 static char x500SubjectStr[] = "C=US, S=Minnesota, L=Minneapolis, O=CodeWeavers, OU=Wine Development, CN=localhost, E=aric@codeweavers.com";
122 static WCHAR issuerStrW[] = {
123  'U','S',',',' ','M','i','n','n','e','s','o','t','a',',',' ','M','i','n','n',
124  'e','a','p','o','l','i','s',',',' ','C','o','d','e','W','e','a','v','e','r',
125  's',',',' ','W','i','n','e',' ','D','e','v','e','l','o','p','m','e','n','t',
126  ',',' ','l','o','c','a','l','h','o','s','t',',',' ','a','r','i','c','@','c',
127  'o','d','e','w','e','a','v','e','r','s','.','c','o','m',0 };
128 static WCHAR issuerStrSemicolonW[] = {
129  'U','S',';',' ','M','i','n','n','e','s','o','t','a',';',' ','M','i','n','n',
130  'e','a','p','o','l','i','s',';',' ','C','o','d','e','W','e','a','v','e','r',
131  's',';',' ','W','i','n','e',' ','D','e','v','e','l','o','p','m','e','n','t',
132  ';',' ','l','o','c','a','l','h','o','s','t',';',' ','a','r','i','c','@','c',
133  'o','d','e','w','e','a','v','e','r','s','.','c','o','m',0 };
134 static WCHAR issuerStrCRLFW[] = {
135  'U','S','\r','\n','M','i','n','n','e','s','o','t','a','\r','\n','M','i','n',
136  'n','e','a','p','o','l','i','s','\r','\n','C','o','d','e','W','e','a','v','e',
137  'r','s','\r','\n','W','i','n','e',' ','D','e','v','e','l','o','p','m','e','n',
138  't','\r','\n','l','o','c','a','l','h','o','s','t','\r','\n','a','r','i','c',
139  '@','c','o','d','e','w','e','a','v','e','r','s','.','c','o','m',0 };
140 static WCHAR subjectStrW[] = {
141  '2','.','5','.','4','.','6','=','U','S',',',' ','2','.','5','.','4','.','8',
142  '=','M','i','n','n','e','s','o','t','a',',',' ','2','.','5','.','4','.','7',
143  '=','M','i','n','n','e','a','p','o','l','i','s',',',' ','2','.','5','.','4',
144  '.','1','0','=','C','o','d','e','W','e','a','v','e','r','s',',',' ','2','.',
145  '5','.','4','.','1','1','=','W','i','n','e',' ','D','e','v','e','l','o','p',
146  'm','e','n','t',',',' ','2','.','5','.','4','.','3','=','l','o','c','a','l',
147  'h','o','s','t',',',' ','1','.','2','.','8','4','0','.','1','1','3','5','4',
148  '9','.','1','.','9','.','1','=','a','r','i','c','@','c','o','d','e','w','e',
149  'a','v','e','r','s','.','c','o','m',0 };
150 static WCHAR subjectStrSemicolonW[] = {
151  '2','.','5','.','4','.','6','=','U','S',';',' ','2','.','5','.','4','.','8',
152  '=','M','i','n','n','e','s','o','t','a',';',' ','2','.','5','.','4','.','7',
153  '=','M','i','n','n','e','a','p','o','l','i','s',';',' ','2','.','5','.','4',
154  '.','1','0','=','C','o','d','e','W','e','a','v','e','r','s',';',' ','2','.',
155  '5','.','4','.','1','1','=','W','i','n','e',' ','D','e','v','e','l','o','p',
156  'm','e','n','t',';',' ','2','.','5','.','4','.','3','=','l','o','c','a','l',
157  'h','o','s','t',';',' ','1','.','2','.','8','4','0','.','1','1','3','5','4',
158  '9','.','1','.','9','.','1','=','a','r','i','c','@','c','o','d','e','w','e',
159  'a','v','e','r','s','.','c','o','m',0 };
160 static WCHAR subjectStrCRLFW[] = {
161  '2','.','5','.','4','.','6','=','U','S','\r','\n','2','.','5','.','4','.','8',
162  '=','M','i','n','n','e','s','o','t','a','\r','\n','2','.','5','.','4','.','7',
163  '=','M','i','n','n','e','a','p','o','l','i','s','\r','\n','2','.','5','.','4',
164  '.','1','0','=','C','o','d','e','W','e','a','v','e','r','s','\r','\n','2','.',
165  '5','.','4','.','1','1','=','W','i','n','e',' ','D','e','v','e','l','o','p',
166  'm','e','n','t','\r','\n','2','.','5','.','4','.','3','=','l','o','c','a','l',
167  'h','o','s','t','\r','\n','1','.','2','.','8','4','0','.','1','1','3','5','4',
168  '9','.','1','.','9','.','1','=','a','r','i','c','@','c','o','d','e','w','e',
169  'a','v','e','r','s','.','c','o','m',0 };
170
171 typedef BOOL (WINAPI *CryptDecodeObjectFunc)(DWORD, LPCSTR, const BYTE *,
172  DWORD, DWORD, void *, DWORD *);
173 typedef DWORD (WINAPI *CertNameToStrAFunc)(DWORD,LPVOID,DWORD,LPSTR,DWORD);
174 typedef DWORD (WINAPI *CertNameToStrWFunc)(DWORD,LPVOID,DWORD,LPWSTR,DWORD);
175 typedef DWORD (WINAPI *CertRDNValueToStrAFunc)(DWORD, PCERT_RDN_VALUE_BLOB,
176  LPSTR, DWORD);
177 typedef DWORD (WINAPI *CertRDNValueToStrWFunc)(DWORD, PCERT_RDN_VALUE_BLOB,
178  LPWSTR, DWORD);
179 typedef BOOL (WINAPI *CertStrToNameAFunc)(DWORD dwCertEncodingType,
180  LPCSTR pszX500, DWORD dwStrType, void *pvReserved, BYTE *pbEncoded,
181  DWORD *pcbEncoded, LPCSTR *ppszError);
182 typedef BOOL (WINAPI *CertStrToNameWFunc)(DWORD dwCertEncodingType,
183  LPCWSTR pszX500, DWORD dwStrType, void *pvReserved, BYTE *pbEncoded,
184  DWORD *pcbEncoded, LPCWSTR *ppszError);
185
186 HMODULE dll;
187 static CertNameToStrAFunc pCertNameToStrA;
188 static CertNameToStrWFunc pCertNameToStrW;
189 static CryptDecodeObjectFunc pCryptDecodeObject;
190 static CertRDNValueToStrAFunc pCertRDNValueToStrA;
191 static CertRDNValueToStrWFunc pCertRDNValueToStrW;
192 static CertStrToNameAFunc pCertStrToNameA;
193 static CertStrToNameWFunc pCertStrToNameW;
194
195 static void test_CertRDNValueToStrA(void)
196 {
197     CertRDNAttrEncoding attrs[] = {
198      { "2.5.4.6", CERT_RDN_PRINTABLE_STRING,
199        { sizeof(bin1), (PBYTE)bin1 }, "US" },
200      { "2.5.4.8", CERT_RDN_PRINTABLE_STRING,
201        { sizeof(bin2), (PBYTE)bin2 }, "Minnesota" },
202      { "2.5.4.7", CERT_RDN_PRINTABLE_STRING,
203        { sizeof(bin3), (PBYTE)bin3 }, "Minneapolis" },
204      { "2.5.4.10", CERT_RDN_PRINTABLE_STRING,
205        { sizeof(bin4), (PBYTE)bin4 }, "CodeWeavers" },
206      { "2.5.4.11", CERT_RDN_PRINTABLE_STRING,
207        { sizeof(bin5), (PBYTE)bin5 }, "Wine Development" },
208      { "2.5.4.3", CERT_RDN_PRINTABLE_STRING,
209        { sizeof(bin6), (PBYTE)bin6 }, "localhost" },
210      { "1.2.840.113549.1.9.1", CERT_RDN_IA5_STRING,
211        { sizeof(bin7), (PBYTE)bin7 }, "aric@codeweavers.com" },
212     };
213     DWORD i, ret;
214     char buffer[2000];
215     CERT_RDN_VALUE_BLOB blob = { 0, NULL };
216
217     if (!pCertRDNValueToStrA) return;
218
219     /* This crashes
220     ret = pCertRDNValueToStrA(0, NULL, NULL, 0);
221      */
222     /* With empty input, it generates the empty string */
223     SetLastError(0xdeadbeef);
224     ret = pCertRDNValueToStrA(0, &blob, NULL, 0);
225     ok(ret == 1 && GetLastError() == 0xdeadbeef, "Expected empty string\n");
226     ret = pCertRDNValueToStrA(0, &blob, buffer, sizeof(buffer));
227     ok(ret == 1 && GetLastError() == 0xdeadbeef, "Expected empty string\n");
228     ok(!buffer[0], "Expected empty string\n");
229
230     for (i = 0; i < sizeof(attrs) / sizeof(attrs[0]); i++)
231     {
232         ret = pCertRDNValueToStrA(attrs[i].dwValueType, &attrs[i].Value,
233          buffer, sizeof(buffer));
234         ok(ret == strlen(attrs[i].str) + 1, "Expected length %d, got %d\n",
235          lstrlenA(attrs[i].str) + 1, ret);
236         ok(!strcmp(buffer, attrs[i].str), "Expected %s, got %s\n", attrs[i].str,
237          buffer);
238     }
239 }
240
241 static void test_CertRDNValueToStrW(void)
242 {
243     static const WCHAR usW[] = { 'U','S',0 };
244     static const WCHAR minnesotaW[] = { 'M','i','n','n','e','s','o','t','a',0 };
245     static const WCHAR minneapolisW[] = { 'M','i','n','n','e','a','p','o','l',
246      'i','s',0 };
247     static const WCHAR codeweaversW[] = { 'C','o','d','e','W','e','a','v','e',
248      'r','s',0 };
249     static const WCHAR wineDevW[] = { 'W','i','n','e',' ','D','e','v','e','l',
250      'o','p','m','e','n','t',0 };
251     static const WCHAR localhostW[] = { 'l','o','c','a','l','h','o','s','t',0 };
252     static const WCHAR aricW[] = { 'a','r','i','c','@','c','o','d','e','w','e',
253      'a','v','e','r','s','.','c','o','m',0 };
254     CertRDNAttrEncodingW attrs[] = {
255      { "2.5.4.6", CERT_RDN_PRINTABLE_STRING,
256        { sizeof(bin1), (PBYTE)bin1 }, usW },
257      { "2.5.4.8", CERT_RDN_PRINTABLE_STRING,
258        { sizeof(bin2), (PBYTE)bin2 }, minnesotaW },
259      { "2.5.4.7", CERT_RDN_PRINTABLE_STRING,
260        { sizeof(bin3), (PBYTE)bin3 }, minneapolisW },
261      { "2.5.4.10", CERT_RDN_PRINTABLE_STRING,
262        { sizeof(bin4), (PBYTE)bin4 }, codeweaversW },
263      { "2.5.4.11", CERT_RDN_PRINTABLE_STRING,
264        { sizeof(bin5), (PBYTE)bin5 }, wineDevW },
265      { "2.5.4.3", CERT_RDN_PRINTABLE_STRING,
266        { sizeof(bin6), (PBYTE)bin6 }, localhostW },
267      { "1.2.840.113549.1.9.1", CERT_RDN_IA5_STRING,
268        { sizeof(bin7), (PBYTE)bin7 }, aricW },
269     };
270     DWORD i, ret;
271     WCHAR buffer[2000];
272     CERT_RDN_VALUE_BLOB blob = { 0, NULL };
273
274     if (!pCertRDNValueToStrW)
275     {
276         skip("CertRDNValueToStrW is not available\n");
277         return;
278     }
279
280     /* This crashes
281     ret = pCertRDNValueToStrW(0, NULL, NULL, 0);
282      */
283     /* With empty input, it generates the empty string */
284     SetLastError(0xdeadbeef);
285     ret = pCertRDNValueToStrW(0, &blob, NULL, 0);
286     ok(ret == 1 && GetLastError() == 0xdeadbeef, "Expected empty string\n");
287     ret = pCertRDNValueToStrW(0, &blob, buffer,
288      sizeof(buffer) / sizeof(buffer[0]));
289     ok(ret == 1 && GetLastError() == 0xdeadbeef, "Expected empty string\n");
290     ok(!buffer[0], "Expected empty string\n");
291
292     for (i = 0; i < sizeof(attrs) / sizeof(attrs[0]); i++)
293     {
294         ret = pCertRDNValueToStrW(attrs[i].dwValueType, &attrs[i].Value,
295          buffer, sizeof(buffer) / sizeof(buffer[0]));
296         ok(ret == lstrlenW(attrs[i].str) + 1, "Expected length %d, got %d\n",
297          lstrlenW(attrs[i].str) + 1, ret);
298         ok(!lstrcmpW(buffer, attrs[i].str), "Unexpected value\n");
299 #ifdef DUMP_STRINGS
300         trace("Expected %s, got %s\n",
301          wine_dbgstr_w(attrs[i].str), wine_dbgstr_w(buffer));
302 #endif
303     }
304 }
305
306 static void test_NameToStrConversionA(PCERT_NAME_BLOB pName, DWORD dwStrType,
307  LPCSTR expected)
308 {
309     char buffer[2000] = { 0 };
310     DWORD i;
311
312     i = pCertNameToStrA(X509_ASN_ENCODING, pName, dwStrType, NULL, 0);
313     ok(i == strlen(expected) + 1, "Expected %d chars, got %d\n",
314      lstrlenA(expected) + 1, i);
315     i = pCertNameToStrA(X509_ASN_ENCODING,pName, dwStrType, buffer,
316      sizeof(buffer));
317     ok(i == strlen(expected) + 1, "Expected %d chars, got %d\n",
318      lstrlenA(expected) + 1, i);
319     ok(!strcmp(buffer, expected), "Expected %s, got %s\n", expected, buffer);
320 }
321
322 static void test_CertNameToStrA(void)
323 {
324     PCCERT_CONTEXT context;
325
326     if (!pCertNameToStrA)
327     {
328         skip("CertNameToStrA is not available\n");
329         return;
330     }
331
332     context = CertCreateCertificateContext(X509_ASN_ENCODING, cert,
333      sizeof(cert));
334     ok(context != NULL, "CertCreateCertificateContext failed: %08x\n",
335      GetLastError());
336     if (context)
337     {
338         DWORD ret;
339
340         /* This crashes
341         ret = pCertNameToStrA(0, NULL, 0, NULL, 0);
342          */
343         /* Test with a bogus encoding type */
344         SetLastError(0xdeadbeef);
345         ret = pCertNameToStrA(0, &context->pCertInfo->Issuer, 0, NULL, 0);
346         ok(ret == 1 && GetLastError() == ERROR_FILE_NOT_FOUND,
347          "Expected retval 1 and ERROR_FILE_NOT_FOUND, got %d - %08x\n",
348          ret, GetLastError());
349         SetLastError(0xdeadbeef);
350         ret = pCertNameToStrA(X509_ASN_ENCODING, &context->pCertInfo->Issuer,
351          0, NULL, 0);
352         ok(ret && GetLastError() == ERROR_SUCCESS,
353          "Expected positive return and ERROR_SUCCESS, got %d - %08x\n",
354          ret, GetLastError());
355
356         test_NameToStrConversionA(&context->pCertInfo->Issuer,
357          CERT_SIMPLE_NAME_STR, issuerStr);
358         test_NameToStrConversionA(&context->pCertInfo->Issuer,
359          CERT_SIMPLE_NAME_STR | CERT_NAME_STR_SEMICOLON_FLAG,
360          issuerStrSemicolon);
361         test_NameToStrConversionA(&context->pCertInfo->Issuer,
362          CERT_SIMPLE_NAME_STR | CERT_NAME_STR_CRLF_FLAG,
363          issuerStrCRLF);
364         test_NameToStrConversionA(&context->pCertInfo->Subject,
365          CERT_OID_NAME_STR, subjectStr);
366         test_NameToStrConversionA(&context->pCertInfo->Subject,
367          CERT_OID_NAME_STR | CERT_NAME_STR_SEMICOLON_FLAG,
368          subjectStrSemicolon);
369         test_NameToStrConversionA(&context->pCertInfo->Subject,
370          CERT_OID_NAME_STR | CERT_NAME_STR_CRLF_FLAG,
371          subjectStrCRLF);
372         test_NameToStrConversionA(&context->pCertInfo->Subject,
373          CERT_X500_NAME_STR, x500SubjectStr);
374
375         CertFreeCertificateContext(context);
376     }
377 }
378
379 static void test_NameToStrConversionW(PCERT_NAME_BLOB pName, DWORD dwStrType,
380  LPCWSTR expected)
381 {
382     WCHAR buffer[2000] = { 0 };
383     DWORD i;
384
385     i = pCertNameToStrW(X509_ASN_ENCODING,pName, dwStrType, NULL, 0);
386     ok(i == lstrlenW(expected) + 1, "Expected %d chars, got %d\n",
387      lstrlenW(expected) + 1, i);
388     i = pCertNameToStrW(X509_ASN_ENCODING,pName, dwStrType, buffer,
389      sizeof(buffer) / sizeof(buffer[0]));
390     ok(i == lstrlenW(expected) + 1, "Expected %d chars, got %d\n",
391      lstrlenW(expected) + 1, i);
392     ok(!lstrcmpW(buffer, expected), "Unexpected value\n");
393 #ifdef DUMP_STRINGS
394     trace("Expected %s, got %s\n",
395      wine_dbgstr_w(expected), wine_dbgstr_w(buffer));
396 #endif
397 }
398
399 static void test_CertNameToStrW(void)
400 {
401     PCCERT_CONTEXT context;
402
403     if (!pCertNameToStrW)
404     {
405         skip("CertNameToStrW is not available\n");
406         return;
407     }
408
409     context = CertCreateCertificateContext(X509_ASN_ENCODING, cert,
410      sizeof(cert));
411     ok(context != NULL, "CertCreateCertificateContext failed: %08x\n",
412      GetLastError());
413     if (context)
414     {
415         DWORD ret;
416
417         /* This crashes
418         ret = pCertNameToStrW(0, NULL, 0, NULL, 0);
419          */
420         /* Test with a bogus encoding type */
421         SetLastError(0xdeadbeef);
422         ret = pCertNameToStrW(0, &context->pCertInfo->Issuer, 0, NULL, 0);
423         ok(ret == 1 && GetLastError() == ERROR_FILE_NOT_FOUND,
424          "Expected retval 1 and ERROR_FILE_NOT_FOUND, got %d - %08x\n",
425          ret, GetLastError());
426         SetLastError(0xdeadbeef);
427         ret = pCertNameToStrW(X509_ASN_ENCODING, &context->pCertInfo->Issuer,
428          0, NULL, 0);
429         ok(ret && GetLastError() == ERROR_SUCCESS,
430          "Expected positive return and ERROR_SUCCESS, got %d - %08x\n",
431          ret, GetLastError());
432
433         test_NameToStrConversionW(&context->pCertInfo->Issuer,
434          CERT_SIMPLE_NAME_STR, issuerStrW);
435         test_NameToStrConversionW(&context->pCertInfo->Issuer,
436          CERT_SIMPLE_NAME_STR | CERT_NAME_STR_SEMICOLON_FLAG,
437          issuerStrSemicolonW);
438         test_NameToStrConversionW(&context->pCertInfo->Issuer,
439          CERT_SIMPLE_NAME_STR | CERT_NAME_STR_CRLF_FLAG,
440          issuerStrCRLFW);
441         test_NameToStrConversionW(&context->pCertInfo->Subject,
442          CERT_OID_NAME_STR, subjectStrW);
443         test_NameToStrConversionW(&context->pCertInfo->Subject,
444          CERT_OID_NAME_STR | CERT_NAME_STR_SEMICOLON_FLAG,
445          subjectStrSemicolonW);
446         test_NameToStrConversionW(&context->pCertInfo->Subject,
447          CERT_OID_NAME_STR | CERT_NAME_STR_CRLF_FLAG,
448          subjectStrCRLFW);
449
450         CertFreeCertificateContext(context);
451     }
452 }
453
454 struct StrToNameA
455 {
456     LPCSTR x500;
457     DWORD encodedSize;
458     const BYTE *encoded;
459 };
460
461 const BYTE encodedSimpleCN[] = {
462 0x30,0x0c,0x31,0x0a,0x30,0x08,0x06,0x03,0x55,0x04,0x03,0x13,0x01,0x31 };
463 static const BYTE encodedSingleQuotedCN[] = { 0x30,0x0e,0x31,0x0c,0x30,0x0a,
464  0x06,0x03,0x55,0x04,0x03,0x13,0x03,0x27,0x31,0x27 };
465 static const BYTE encodedSpacedCN[] = { 0x30,0x0e,0x31,0x0c,0x30,0x0a,0x06,0x03,
466  0x55,0x04,0x03,0x13,0x03,0x20,0x31,0x20 };
467 static const BYTE encodedQuotedCN[] = { 0x30,0x11,0x31,0x0f,0x30,0x0d,0x06,0x03,
468  0x55, 0x04,0x03,0x1e,0x06,0x00,0x22,0x00,0x31,0x00,0x22, };
469 static const BYTE encodedMultipleAttrCN[] = { 0x30,0x0e,0x31,0x0c,0x30,0x0a,
470  0x06,0x03,0x55,0x04,0x03,0x13,0x03,0x31,0x2b,0x32 };
471
472 struct StrToNameA namesA[] = {
473  { "CN=1", sizeof(encodedSimpleCN), encodedSimpleCN },
474  { "CN=\"1\"", sizeof(encodedSimpleCN), encodedSimpleCN },
475  { "CN = \"1\"", sizeof(encodedSimpleCN), encodedSimpleCN },
476  { "CN='1'", sizeof(encodedSingleQuotedCN), encodedSingleQuotedCN },
477  { "CN=\" 1 \"", sizeof(encodedSpacedCN), encodedSpacedCN },
478  { "CN=\"\"\"1\"\"\"", sizeof(encodedQuotedCN), encodedQuotedCN },
479  { "CN=\"1+2\"", sizeof(encodedMultipleAttrCN), encodedMultipleAttrCN },
480 };
481
482 static void test_CertStrToNameA(void)
483 {
484     BOOL ret;
485     DWORD size, i;
486     BYTE buf[100];
487
488     if (!pCertStrToNameA)
489     {
490         skip("CertStrToNameA is not available\n");
491         return;
492     }
493
494     /* Crash
495     ret = pCertStrToNameA(0, NULL, 0, NULL, NULL, NULL, NULL);
496      */
497     ret = pCertStrToNameA(0, NULL, 0, NULL, NULL, &size, NULL);
498     ok(!ret, "Expected failure\n");
499     ret = pCertStrToNameA(0, "bogus", 0, NULL, NULL, &size, NULL);
500     ok(!ret && GetLastError() == CRYPT_E_INVALID_X500_STRING,
501      "Expected CRYPT_E_INVALID_X500_STRING, got %08x\n", GetLastError());
502     ret = pCertStrToNameA(0, "foo=1", 0, NULL, NULL, &size, NULL);
503     ok(!ret && GetLastError() == CRYPT_E_INVALID_X500_STRING,
504      "Expected CRYPT_E_INVALID_X500_STRING, got %08x\n", GetLastError());
505     ret = pCertStrToNameA(0, "CN=1", 0, NULL, NULL, &size, NULL);
506     ok(!ret && GetLastError() == ERROR_FILE_NOT_FOUND,
507      "Expected ERROR_FILE_NOT_FOUND, got %08x\n", GetLastError());
508     ret = pCertStrToNameA(X509_ASN_ENCODING, "CN=1", 0, NULL, NULL, &size, NULL);
509     ok(ret, "CertStrToNameA failed: %08x\n", GetLastError());
510     size = sizeof(buf);
511     ret = pCertStrToNameA(X509_ASN_ENCODING, "CN=\"\"1\"\"", 0, NULL, buf, &size,
512      NULL);
513     ok(!ret && GetLastError() == CRYPT_E_INVALID_X500_STRING,
514      "Expected CRYPT_E_INVALID_X500_STRING, got %08x\n", GetLastError());
515     ret = pCertStrToNameA(X509_ASN_ENCODING, "CN=1+2", 0, NULL, buf,
516      &size, NULL);
517     todo_wine ok(!ret && GetLastError() == CRYPT_E_INVALID_X500_STRING,
518      "Expected CRYPT_E_INVALID_X500_STRING, got %08x\n", GetLastError());
519     for (i = 0; i < sizeof(namesA) / sizeof(namesA[0]); i++)
520     {
521         size = sizeof(buf);
522         ret = pCertStrToNameA(X509_ASN_ENCODING, namesA[i].x500, 0, NULL, buf,
523          &size, NULL);
524         ok(ret, "CertStrToNameA failed on string %s: %08x\n", namesA[i].x500,
525          GetLastError());
526         ok(size == namesA[i].encodedSize,
527          "Expected size %d, got %d\n", namesA[i].encodedSize, size);
528         if (ret)
529             ok(!memcmp(buf, namesA[i].encoded, namesA[i].encodedSize),
530              "Unexpected value for string %s\n", namesA[i].x500);
531     }
532 }
533
534 struct StrToNameW
535 {
536     LPCWSTR x500;
537     DWORD encodedSize;
538     const BYTE *encoded;
539 };
540
541 static const WCHAR badlyQuotedCN_W[] = { 'C','N','=','"','"','1','"','"',0 };
542 static const WCHAR simpleCN_W[] = { 'C','N','=','1',0 };
543 static const WCHAR simpleCN2_W[] = { 'C','N','=','"','1','"',0 };
544 static const WCHAR simpleCN3_W[] = { 'C','N',' ','=',' ','"','1','"',0 };
545 static const WCHAR singledQuotedCN_W[] = { 'C','N','=','\'','1','\'',0 };
546 static const WCHAR spacedCN_W[] = { 'C','N','=','"',' ','1',' ','"',0 };
547 static const WCHAR quotedCN_W[] = { 'C','N','=','"','"','"','1','"','"','"',0 };
548 static const WCHAR multipleAttrCN_W[] = { 'C','N','=','"','1','+','2','"',0 };
549 static const WCHAR japaneseCN_W[] = { 'C','N','=',0x226f,0x575b,0 };
550 static const BYTE encodedJapaneseCN[] = { 0x30,0x0f,0x31,0x0d,0x30,0x0b,0x06,
551  0x03,0x55,0x04,0x03,0x1e,0x04,0x22,0x6f,0x57,0x5b };
552
553 struct StrToNameW namesW[] = {
554  { simpleCN_W, sizeof(encodedSimpleCN), encodedSimpleCN },
555  { simpleCN2_W, sizeof(encodedSimpleCN), encodedSimpleCN },
556  { simpleCN3_W, sizeof(encodedSimpleCN), encodedSimpleCN },
557  { singledQuotedCN_W, sizeof(encodedSingleQuotedCN), encodedSingleQuotedCN },
558  { spacedCN_W, sizeof(encodedSpacedCN), encodedSpacedCN },
559  { quotedCN_W, sizeof(encodedQuotedCN), encodedQuotedCN },
560  { multipleAttrCN_W, sizeof(encodedMultipleAttrCN), encodedMultipleAttrCN },
561  { japaneseCN_W, sizeof(encodedJapaneseCN), encodedJapaneseCN },
562 };
563
564 static void test_CertStrToNameW(void)
565 {
566     static const WCHAR bogusW[] = { 'b','o','g','u','s',0 };
567     static const WCHAR fooW[] = { 'f','o','o','=','1',0 };
568     BOOL ret;
569     DWORD size, i;
570     LPCWSTR errorPtr;
571     BYTE buf[100];
572
573     if (!pCertStrToNameW)
574     {
575         skip("CertStrToNameW is not available\n");
576         return;
577     }
578
579     /* Crash
580     ret = pCertStrToNameW(0, NULL, 0, NULL, NULL, NULL, NULL);
581      */
582     ret = pCertStrToNameW(0, NULL, 0, NULL, NULL, &size, NULL);
583     ok(!ret, "Expected failure\n");
584     ret = pCertStrToNameW(0, bogusW, 0, NULL, NULL, &size, NULL);
585     ok(!ret && GetLastError() == CRYPT_E_INVALID_X500_STRING,
586      "Expected CRYPT_E_INVALID_X500_STRING, got %08x\n", GetLastError());
587     ret = pCertStrToNameW(0, fooW, 0, NULL, NULL, &size, NULL);
588     ok(!ret && GetLastError() == CRYPT_E_INVALID_X500_STRING,
589      "Expected CRYPT_E_INVALID_X500_STRING, got %08x\n", GetLastError());
590     ret = pCertStrToNameW(0, simpleCN_W, 0, NULL, NULL, &size, NULL);
591     ok(!ret && GetLastError() == ERROR_FILE_NOT_FOUND,
592      "Expected ERROR_FILE_NOT_FOUND, got %08x\n", GetLastError());
593     ret = pCertStrToNameW(X509_ASN_ENCODING, simpleCN_W, 0, NULL, NULL, &size,
594      NULL);
595     ok(ret, "CertStrToNameW failed: %08x\n", GetLastError());
596     size = sizeof(buf);
597     ret = pCertStrToNameW(X509_ASN_ENCODING, badlyQuotedCN_W, 0, NULL, buf,
598      &size, NULL);
599     ok(!ret && GetLastError() == CRYPT_E_INVALID_X500_STRING,
600      "Expected CRYPT_E_INVALID_X500_STRING, got %08x\n", GetLastError());
601     ret = pCertStrToNameW(X509_ASN_ENCODING, badlyQuotedCN_W, 0, NULL, buf,
602      &size, &errorPtr);
603     ok(!ret && GetLastError() == CRYPT_E_INVALID_X500_STRING,
604      "Expected CRYPT_E_INVALID_X500_STRING, got %08x\n", GetLastError());
605     ok(errorPtr && *errorPtr == '1', "Expected first error character was 1\n");
606     for (i = 0; i < sizeof(namesW) / sizeof(namesW[0]); i++)
607     {
608         size = sizeof(buf);
609         ret = pCertStrToNameW(X509_ASN_ENCODING, namesW[i].x500, 0, NULL, buf,
610          &size, NULL);
611         ok(ret, "Index %d: CertStrToNameW failed: %08x\n", i, GetLastError());
612         ok(size == namesW[i].encodedSize,
613          "Index %d: expected size %d, got %d\n", i, namesW[i].encodedSize,
614          size);
615         if (ret)
616             ok(!memcmp(buf, namesW[i].encoded, size),
617              "Index %d: unexpected value\n", i);
618     }
619 }
620
621 START_TEST(str)
622 {
623     dll = GetModuleHandleA("Crypt32.dll");
624
625     pCertNameToStrA = (CertNameToStrAFunc)GetProcAddress(dll,"CertNameToStrA");
626     pCertNameToStrW = (CertNameToStrWFunc)GetProcAddress(dll,"CertNameToStrW");
627     pCertRDNValueToStrA = (CertRDNValueToStrAFunc)GetProcAddress(dll,
628      "CertRDNValueToStrA");
629     pCertRDNValueToStrW = (CertRDNValueToStrWFunc)GetProcAddress(dll,
630      "CertRDNValueToStrW");
631     pCryptDecodeObject = (CryptDecodeObjectFunc)GetProcAddress(dll,
632      "CryptDecodeObject");
633     pCertStrToNameA = (CertStrToNameAFunc)GetProcAddress(dll,"CertStrToNameA");
634     pCertStrToNameW = (CertStrToNameWFunc)GetProcAddress(dll,"CertStrToNameW");
635
636     test_CertRDNValueToStrA();
637     test_CertRDNValueToStrW();
638     test_CertNameToStrA();
639     test_CertNameToStrW();
640     test_CertStrToNameA();
641     test_CertStrToNameW();
642 }