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