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