mfplat: Read queue subscriber within the critical section.
[wine/zf.git] / dlls / crypt32 / tests / str.c
blobd382c0e6b09abd367235004884d534833636a96c
1 /*
2 * Unit test suite for crypt32.dll's Cert*ToStr and CertStrToName functions.
4 * Copyright 2006 Juan Lang, Aric Stewart for CodeWeavers
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.
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.
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
20 #include <stdio.h>
21 #include <stdarg.h>
22 #include <windef.h>
23 #include <winbase.h>
24 #include <winerror.h>
25 #include <wincrypt.h>
27 #include "wine/test.h"
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;
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;
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 };
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};
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 };
188 static HMODULE dll;
189 static DWORD (WINAPI *pCertNameToStrA)(DWORD,LPVOID,DWORD,LPSTR,DWORD);
190 static DWORD (WINAPI *pCertNameToStrW)(DWORD,LPVOID,DWORD,LPWSTR,DWORD);
191 static DWORD (WINAPI *pCertRDNValueToStrA)(DWORD, PCERT_RDN_VALUE_BLOB,
192 LPSTR, DWORD);
193 static DWORD (WINAPI *pCertRDNValueToStrW)(DWORD, PCERT_RDN_VALUE_BLOB,
194 LPWSTR, DWORD);
195 static BOOL (WINAPI *pCertStrToNameA)(DWORD dwCertEncodingType,
196 LPCSTR pszX500, DWORD dwStrType, void *pvReserved, BYTE *pbEncoded,
197 DWORD *pcbEncoded, LPCSTR *ppszError);
198 static BOOL (WINAPI *pCertStrToNameW)(DWORD dwCertEncodingType,
199 LPCWSTR pszX500, DWORD dwStrType, void *pvReserved, BYTE *pbEncoded,
200 DWORD *pcbEncoded, LPCWSTR *ppszError);
201 static DWORD (WINAPI *pCertGetNameStringA)(PCCERT_CONTEXT cert, DWORD type,
202 DWORD flags, void *typePara, LPSTR str, DWORD cch);
205 static void test_CertRDNValueToStrA(void)
207 CertRDNAttrEncoding attrs[] = {
208 { "2.5.4.6", CERT_RDN_PRINTABLE_STRING,
209 { sizeof(bin1), bin1 }, "US", FALSE },
210 { "2.5.4.8", CERT_RDN_PRINTABLE_STRING,
211 { sizeof(bin2), bin2 }, "Minnesota", FALSE },
212 { "2.5.4.7", CERT_RDN_PRINTABLE_STRING,
213 { sizeof(bin3), bin3 }, "Minneapolis", FALSE },
214 { "2.5.4.10", CERT_RDN_PRINTABLE_STRING,
215 { sizeof(bin4), bin4 }, "CodeWeavers", FALSE },
216 { "2.5.4.11", CERT_RDN_PRINTABLE_STRING,
217 { sizeof(bin5), bin5 }, "Wine Development", FALSE },
218 { "2.5.4.3", CERT_RDN_PRINTABLE_STRING,
219 { sizeof(bin6), bin6 }, "localhost", FALSE },
220 { "1.2.840.113549.1.9.1", CERT_RDN_IA5_STRING,
221 { sizeof(bin7), bin7 }, "aric@codeweavers.com", FALSE },
222 { "0", CERT_RDN_PRINTABLE_STRING,
223 { sizeof(bin9), bin9 }, "abc\"def", FALSE },
224 { "0", CERT_RDN_PRINTABLE_STRING,
225 { sizeof(bin10), bin10 }, "abc'def", FALSE },
226 { "0", CERT_RDN_PRINTABLE_STRING,
227 { sizeof(bin11), bin11 }, "abc, def", FALSE },
228 { "0", CERT_RDN_PRINTABLE_STRING,
229 { sizeof(bin12), bin12 }, " abc ", FALSE },
230 { "0", CERT_RDN_PRINTABLE_STRING,
231 { sizeof(bin13), bin13 }, "\"def\"", FALSE },
232 { "0", CERT_RDN_PRINTABLE_STRING,
233 { sizeof(bin14), bin14 }, "1;3", FALSE },
235 DWORD i, ret;
236 char buffer[2000];
237 CERT_RDN_VALUE_BLOB blob = { 0, NULL };
238 static const char ePKI[] = "ePKI Root Certification Authority";
240 if (!pCertRDNValueToStrA) return;
242 /* This crashes
243 ret = pCertRDNValueToStrA(0, NULL, NULL, 0);
245 /* With empty input, it generates the empty string */
246 SetLastError(0xdeadbeef);
247 ret = pCertRDNValueToStrA(0, &blob, NULL, 0);
248 ok(ret == 1 && GetLastError() == 0xdeadbeef, "Expected empty string\n");
249 ret = pCertRDNValueToStrA(0, &blob, buffer, sizeof(buffer));
250 ok(ret == 1 && GetLastError() == 0xdeadbeef, "Expected empty string\n");
251 ok(!buffer[0], "Expected empty string\n");
253 for (i = 0; i < ARRAY_SIZE(attrs); i++)
255 ret = pCertRDNValueToStrA(attrs[i].dwValueType, &attrs[i].Value,
256 buffer, sizeof(buffer));
257 todo_wine_if (attrs[i].todo)
259 ok(ret == strlen(attrs[i].str) + 1, "Expected length %d, got %d\n",
260 lstrlenA(attrs[i].str) + 1, ret);
261 ok(!strcmp(buffer, attrs[i].str), "Expected %s, got %s\n",
262 attrs[i].str, buffer);
265 blob.pbData = bin8;
266 blob.cbData = sizeof(bin8);
267 ret = pCertRDNValueToStrA(CERT_RDN_UTF8_STRING, &blob, buffer,
268 sizeof(buffer));
269 ok(ret == strlen(ePKI) + 1 || broken(ret != strlen(ePKI) + 1),
270 "Expected length %d, got %d\n", lstrlenA(ePKI), ret);
271 if (ret == strlen(ePKI) + 1)
272 ok(!strcmp(buffer, ePKI), "Expected %s, got %s\n", ePKI, buffer);
275 static void test_CertRDNValueToStrW(void)
277 static const WCHAR usW[] = { 'U','S',0 };
278 static const WCHAR minnesotaW[] = { 'M','i','n','n','e','s','o','t','a',0 };
279 static const WCHAR minneapolisW[] = { 'M','i','n','n','e','a','p','o','l',
280 'i','s',0 };
281 static const WCHAR codeweaversW[] = { 'C','o','d','e','W','e','a','v','e',
282 'r','s',0 };
283 static const WCHAR wineDevW[] = { 'W','i','n','e',' ','D','e','v','e','l',
284 'o','p','m','e','n','t',0 };
285 static const WCHAR localhostW[] = { 'l','o','c','a','l','h','o','s','t',0 };
286 static const WCHAR aricW[] = { 'a','r','i','c','@','c','o','d','e','w','e',
287 'a','v','e','r','s','.','c','o','m',0 };
288 static const WCHAR ePKIW[] = { 'e','P','K','I',' ','R','o','o','t',' ',
289 'C','e','r','t','i','f','i','c','a','t','i','o','n',' ','A','u','t','h',
290 'o','r','i','t','y',0 };
291 static const WCHAR embeddedDoubleQuoteW[] = { 'a','b','c','"','d','e','f',
292 0 };
293 static const WCHAR embeddedSingleQuoteW[] = { 'a','b','c','\'','d','e','f',
294 0 };
295 static const WCHAR embeddedCommaW[] = { 'a','b','c',',',' ','d','e','f',0 };
296 static const WCHAR trailingAndEndingSpaceW[] = { ' ','a','b','c',' ',0 };
297 static const WCHAR enclosingQuotesW[] = { '"','d','e','f','"',0 };
298 static const WCHAR embeddedSemiW[] = { '1',';','3',0 };
299 CertRDNAttrEncodingW attrs[] = {
300 { "2.5.4.6", CERT_RDN_PRINTABLE_STRING,
301 { sizeof(bin1), bin1 }, usW, FALSE },
302 { "2.5.4.8", CERT_RDN_PRINTABLE_STRING,
303 { sizeof(bin2), bin2 }, minnesotaW, FALSE },
304 { "2.5.4.7", CERT_RDN_PRINTABLE_STRING,
305 { sizeof(bin3), bin3 }, minneapolisW, FALSE },
306 { "2.5.4.10", CERT_RDN_PRINTABLE_STRING,
307 { sizeof(bin4), bin4 }, codeweaversW, FALSE },
308 { "2.5.4.11", CERT_RDN_PRINTABLE_STRING,
309 { sizeof(bin5), bin5 }, wineDevW, FALSE },
310 { "2.5.4.3", CERT_RDN_PRINTABLE_STRING,
311 { sizeof(bin6), bin6 }, localhostW, FALSE },
312 { "1.2.840.113549.1.9.1", CERT_RDN_IA5_STRING,
313 { sizeof(bin7), bin7 }, aricW, FALSE },
314 { "0", CERT_RDN_PRINTABLE_STRING,
315 { sizeof(bin9), bin9 }, embeddedDoubleQuoteW, FALSE },
316 { "0", CERT_RDN_PRINTABLE_STRING,
317 { sizeof(bin10), bin10 }, embeddedSingleQuoteW, FALSE },
318 { "0", CERT_RDN_PRINTABLE_STRING,
319 { sizeof(bin11), bin11 }, embeddedCommaW, FALSE },
320 { "0", CERT_RDN_PRINTABLE_STRING,
321 { sizeof(bin12), bin12 }, trailingAndEndingSpaceW, FALSE },
322 { "0", CERT_RDN_PRINTABLE_STRING,
323 { sizeof(bin13), bin13 }, enclosingQuotesW, FALSE },
324 { "0", CERT_RDN_PRINTABLE_STRING,
325 { sizeof(bin14), bin14 }, embeddedSemiW, FALSE },
327 DWORD i, ret;
328 WCHAR buffer[2000];
329 CERT_RDN_VALUE_BLOB blob = { 0, NULL };
331 if (!pCertRDNValueToStrW)
333 win_skip("CertRDNValueToStrW is not available\n");
334 return;
337 /* This crashes
338 ret = pCertRDNValueToStrW(0, NULL, NULL, 0);
340 /* With empty input, it generates the empty string */
341 SetLastError(0xdeadbeef);
342 ret = pCertRDNValueToStrW(0, &blob, NULL, 0);
343 ok(ret == 1 && GetLastError() == 0xdeadbeef, "Expected empty string\n");
344 ret = pCertRDNValueToStrW(0, &blob, buffer, ARRAY_SIZE(buffer));
345 ok(ret == 1 && GetLastError() == 0xdeadbeef, "Expected empty string\n");
346 ok(!buffer[0], "Expected empty string\n");
348 for (i = 0; i < ARRAY_SIZE(attrs); i++)
350 ret = pCertRDNValueToStrW(attrs[i].dwValueType, &attrs[i].Value, buffer, ARRAY_SIZE(buffer));
351 todo_wine_if (attrs[i].todo)
353 ok(ret == lstrlenW(attrs[i].str) + 1,
354 "Expected length %d, got %d\n", lstrlenW(attrs[i].str) + 1, ret);
355 ok(!lstrcmpW(buffer, attrs[i].str), "Expected %s, got %s\n",
356 wine_dbgstr_w(attrs[i].str), wine_dbgstr_w(buffer));
359 blob.pbData = bin8;
360 blob.cbData = sizeof(bin8);
361 ret = pCertRDNValueToStrW(CERT_RDN_UTF8_STRING, &blob, buffer,
362 sizeof(buffer));
363 ok(ret == lstrlenW(ePKIW) + 1 || broken(ret != lstrlenW(ePKIW) + 1),
364 "Expected length %d, got %d\n", lstrlenW(ePKIW), ret);
365 if (ret == lstrlenW(ePKIW) + 1)
366 ok(!lstrcmpW(buffer, ePKIW), "Expected %s, got %s\n",
367 wine_dbgstr_w(ePKIW), wine_dbgstr_w(buffer));
370 static void test_NameToStrConversionA(PCERT_NAME_BLOB pName, DWORD dwStrType,
371 LPCSTR expected, BOOL todo)
373 char buffer[2000] = { 0 };
374 DWORD i;
376 i = pCertNameToStrA(X509_ASN_ENCODING, pName, dwStrType, NULL, 0);
377 todo_wine_if (todo)
378 ok(i == strlen(expected) + 1, "Expected %d chars, got %d\n",
379 lstrlenA(expected) + 1, i);
380 i = pCertNameToStrA(X509_ASN_ENCODING,pName, dwStrType, buffer,
381 sizeof(buffer));
382 todo_wine_if (todo)
383 ok(i == strlen(expected) + 1, "Expected %d chars, got %d\n",
384 lstrlenA(expected) + 1, i);
385 todo_wine_if (todo)
386 ok(!strcmp(buffer, expected), "Expected %s, got %s\n", expected,
387 buffer);
390 static BYTE encodedSimpleCN[] = {
391 0x30,0x0c,0x31,0x0a,0x30,0x08,0x06,0x03,0x55,0x04,0x03,0x13,0x01,0x31 };
392 static BYTE encodedSingleQuotedCN[] = { 0x30,0x0e,0x31,0x0c,0x30,0x0a,
393 0x06,0x03,0x55,0x04,0x03,0x13,0x03,0x27,0x31,0x27 };
394 static BYTE encodedSpacedCN[] = { 0x30,0x0e,0x31,0x0c,0x30,0x0a,0x06,0x03,
395 0x55,0x04,0x03,0x13,0x03,0x20,0x31,0x20 };
396 static BYTE encodedQuotedCN[] = { 0x30,0x11,0x31,0x0f,0x30,0x0d,0x06,0x03,
397 0x55, 0x04,0x03,0x1e,0x06,0x00,0x22,0x00,0x31,0x00,0x22, };
398 static BYTE encodedMultipleAttrCN[] = { 0x30,0x0e,0x31,0x0c,0x30,0x0a,
399 0x06,0x03,0x55,0x04,0x03,0x13,0x03,0x31,0x2b,0x32 };
400 static BYTE encodedCommaCN[] = {
401 0x30,0x0e,0x31,0x0c,0x30,0x0a,0x06,0x03,0x55,0x04,0x03,0x13,0x03,0x61,0x2c,
402 0x62 };
403 static BYTE encodedEqualCN[] = {
404 0x30,0x0e,0x31,0x0c,0x30,0x0a,0x06,0x03,0x55,0x04,0x03,0x13,0x03,0x61,0x3d,
405 0x62 };
406 static BYTE encodedLessThanCN[] = {
407 0x30,0x0d,0x31,0x0b,0x30,0x09,0x06,0x03,0x55,0x04,0x03,0x1e,0x02,0x00,0x3c
409 static BYTE encodedGreaterThanCN[] = {
410 0x30,0x0d,0x31,0x0b,0x30,0x09,0x06,0x03,0x55,0x04,0x03,0x1e,0x02,0x00,0x3e
412 static BYTE encodedHashCN[] = {
413 0x30,0x0d,0x31,0x0b,0x30,0x09,0x06,0x03,0x55,0x04,0x03,0x1e,0x02,0x00,0x23
415 static BYTE encodedSemiCN[] = {
416 0x30,0x0d,0x31,0x0b,0x30,0x09,0x06,0x03,0x55,0x04,0x03,0x1e,0x02,0x00,0x3b
418 static BYTE encodedNewlineCN[] = {
419 0x30,0x11,0x31,0x0f,0x30,0x0d,0x06,0x03,0x55,0x04,0x03,0x1e,0x06,0x00,0x61,
420 0x00,0x0a,0x00,0x62 };
421 static BYTE encodedDummyCN[] = {
422 0x30,0x1F,0x31,0x0E,0x30,0x0C,0x06,0x03,0x55,0x04,0x03,0x13,0x05,0x64,0x75,
423 0x6D,0x6D,0x79,0x31,0x0D,0x30,0x0B,0x06,0x03,0x55,0x04,0x0C,0x13,0x04,0x74,
424 0x65,0x73,0x74 };
425 static BYTE encodedFields[] = {
426 0x30,0x2F,0x31,0x12,0x30,0x10,0x06,0x03,0x55,0x04,0x03,0x13,0x09,0x57,0x69,
427 0x6E,0x65,0x20,0x54,0x65,0x73,0x74,0x31,0x0C,0x30,0x0A,0x06,0x03,0x55,0x04,
428 0x0C,0x13,0x03,0x31,0x32,0x33,0x31,0x0B,0x30,0x09,0x06,0x03,0x55,0x04,0x06,
429 0x13,0x02,0x42,0x52 };
431 static void test_CertNameToStrA(void)
433 PCCERT_CONTEXT context;
434 CERT_NAME_BLOB blob;
436 if (!pCertNameToStrA)
438 win_skip("CertNameToStrA is not available\n");
439 return;
442 context = CertCreateCertificateContext(X509_ASN_ENCODING, cert,
443 sizeof(cert));
444 ok(context != NULL, "CertCreateCertificateContext failed: %08x\n",
445 GetLastError());
446 if (context)
448 DWORD ret;
450 /* This crashes
451 ret = pCertNameToStrA(0, NULL, 0, NULL, 0);
453 /* Test with a bogus encoding type */
454 SetLastError(0xdeadbeef);
455 ret = pCertNameToStrA(0, &context->pCertInfo->Issuer, 0, NULL, 0);
456 ok(ret == 1 && GetLastError() == ERROR_FILE_NOT_FOUND,
457 "Expected retval 1 and ERROR_FILE_NOT_FOUND, got %d - %08x\n",
458 ret, GetLastError());
459 SetLastError(0xdeadbeef);
460 ret = pCertNameToStrA(X509_ASN_ENCODING, &context->pCertInfo->Issuer,
461 0, NULL, 0);
462 ok(ret && GetLastError() == ERROR_SUCCESS,
463 "Expected positive return and ERROR_SUCCESS, got %d - %08x\n",
464 ret, GetLastError());
466 test_NameToStrConversionA(&context->pCertInfo->Issuer,
467 CERT_SIMPLE_NAME_STR, issuerStr, FALSE);
468 test_NameToStrConversionA(&context->pCertInfo->Issuer,
469 CERT_SIMPLE_NAME_STR | CERT_NAME_STR_SEMICOLON_FLAG,
470 issuerStrSemicolon, FALSE);
471 test_NameToStrConversionA(&context->pCertInfo->Issuer,
472 CERT_SIMPLE_NAME_STR | CERT_NAME_STR_CRLF_FLAG,
473 issuerStrCRLF, FALSE);
474 test_NameToStrConversionA(&context->pCertInfo->Subject,
475 CERT_OID_NAME_STR, subjectStr, FALSE);
476 test_NameToStrConversionA(&context->pCertInfo->Subject,
477 CERT_OID_NAME_STR | CERT_NAME_STR_SEMICOLON_FLAG,
478 subjectStrSemicolon, FALSE);
479 test_NameToStrConversionA(&context->pCertInfo->Subject,
480 CERT_OID_NAME_STR | CERT_NAME_STR_CRLF_FLAG,
481 subjectStrCRLF, FALSE);
482 test_NameToStrConversionA(&context->pCertInfo->Subject,
483 CERT_X500_NAME_STR, x500SubjectStr, FALSE);
484 test_NameToStrConversionA(&context->pCertInfo->Subject,
485 CERT_X500_NAME_STR | CERT_NAME_STR_SEMICOLON_FLAG | CERT_NAME_STR_REVERSE_FLAG,
486 x500SubjectStrSemicolonReverse, FALSE);
488 CertFreeCertificateContext(context);
490 blob.pbData = encodedSimpleCN;
491 blob.cbData = sizeof(encodedSimpleCN);
492 test_NameToStrConversionA(&blob, CERT_X500_NAME_STR, "CN=1", FALSE);
493 blob.pbData = encodedSingleQuotedCN;
494 blob.cbData = sizeof(encodedSingleQuotedCN);
495 test_NameToStrConversionA(&blob, CERT_X500_NAME_STR, "CN='1'", FALSE);
496 test_NameToStrConversionA(&blob, CERT_SIMPLE_NAME_STR, "'1'", FALSE);
497 blob.pbData = encodedSpacedCN;
498 blob.cbData = sizeof(encodedSpacedCN);
499 test_NameToStrConversionA(&blob, CERT_X500_NAME_STR, "CN=\" 1 \"", FALSE);
500 test_NameToStrConversionA(&blob, CERT_SIMPLE_NAME_STR, "\" 1 \"", FALSE);
501 blob.pbData = encodedQuotedCN;
502 blob.cbData = sizeof(encodedQuotedCN);
503 test_NameToStrConversionA(&blob, CERT_X500_NAME_STR, "CN=\"\"\"1\"\"\"",
504 FALSE);
505 test_NameToStrConversionA(&blob, CERT_SIMPLE_NAME_STR, "\"\"\"1\"\"\"",
506 FALSE);
507 blob.pbData = encodedMultipleAttrCN;
508 blob.cbData = sizeof(encodedMultipleAttrCN);
509 test_NameToStrConversionA(&blob, CERT_X500_NAME_STR, "CN=\"1+2\"", FALSE);
510 test_NameToStrConversionA(&blob, CERT_SIMPLE_NAME_STR, "\"1+2\"", FALSE);
511 blob.pbData = encodedCommaCN;
512 blob.cbData = sizeof(encodedCommaCN);
513 test_NameToStrConversionA(&blob, CERT_X500_NAME_STR, "CN=\"a,b\"", FALSE);
514 test_NameToStrConversionA(&blob, CERT_SIMPLE_NAME_STR, "\"a,b\"", FALSE);
515 blob.pbData = encodedEqualCN;
516 blob.cbData = sizeof(encodedEqualCN);
517 test_NameToStrConversionA(&blob, CERT_X500_NAME_STR, "CN=\"a=b\"", FALSE);
518 test_NameToStrConversionA(&blob, CERT_SIMPLE_NAME_STR, "\"a=b\"", FALSE);
519 blob.pbData = encodedLessThanCN;
520 blob.cbData = sizeof(encodedLessThanCN);
521 test_NameToStrConversionA(&blob, CERT_X500_NAME_STR, "CN=\"<\"", FALSE);
522 test_NameToStrConversionA(&blob, CERT_SIMPLE_NAME_STR, "\"<\"", FALSE);
523 blob.pbData = encodedGreaterThanCN;
524 blob.cbData = sizeof(encodedGreaterThanCN);
525 test_NameToStrConversionA(&blob, CERT_X500_NAME_STR, "CN=\">\"", FALSE);
526 test_NameToStrConversionA(&blob, CERT_SIMPLE_NAME_STR, "\">\"", FALSE);
527 blob.pbData = encodedHashCN;
528 blob.cbData = sizeof(encodedHashCN);
529 test_NameToStrConversionA(&blob, CERT_X500_NAME_STR, "CN=\"#\"", FALSE);
530 test_NameToStrConversionA(&blob, CERT_SIMPLE_NAME_STR, "\"#\"", FALSE);
531 blob.pbData = encodedSemiCN;
532 blob.cbData = sizeof(encodedSemiCN);
533 test_NameToStrConversionA(&blob, CERT_X500_NAME_STR, "CN=\";\"", FALSE);
534 test_NameToStrConversionA(&blob, CERT_SIMPLE_NAME_STR, "\";\"", FALSE);
535 blob.pbData = encodedNewlineCN;
536 blob.cbData = sizeof(encodedNewlineCN);
537 test_NameToStrConversionA(&blob, CERT_X500_NAME_STR, "CN=\"a\nb\"", FALSE);
538 test_NameToStrConversionA(&blob, CERT_SIMPLE_NAME_STR, "\"a\nb\"", FALSE);
541 static void test_NameToStrConversionW(PCERT_NAME_BLOB pName, DWORD dwStrType,
542 LPCWSTR expected, BOOL todo)
544 WCHAR buffer[2000] = { 0 };
545 DWORD i;
547 i = pCertNameToStrW(X509_ASN_ENCODING,pName, dwStrType, NULL, 0);
548 todo_wine_if (todo)
549 ok(i == lstrlenW(expected) + 1, "Expected %d chars, got %d\n",
550 lstrlenW(expected) + 1, i);
551 i = pCertNameToStrW(X509_ASN_ENCODING,pName, dwStrType, buffer, ARRAY_SIZE(buffer));
552 todo_wine_if (todo)
553 ok(i == lstrlenW(expected) + 1, "Expected %d chars, got %d\n",
554 lstrlenW(expected) + 1, i);
555 todo_wine_if (todo)
556 ok(!lstrcmpW(buffer, expected), "Expected %s, got %s\n",
557 wine_dbgstr_w(expected), wine_dbgstr_w(buffer));
560 static const WCHAR simpleCN_W[] = { 'C','N','=','1',0 };
561 static const WCHAR singledQuotedCN_W[] = { 'C','N','=','\'','1','\'',0 };
562 static const WCHAR simpleSingleQuotedCN_W[] = { '\'','1','\'',0 };
563 static const WCHAR spacedCN_W[] = { 'C','N','=','"',' ','1',' ','"',0 };
564 static const WCHAR simpleSpacedCN_W[] = { '"',' ','1',' ','"',0 };
565 static const WCHAR quotedCN_W[] = { 'C','N','=','"','"','"','1','"','"','"',0 };
566 static const WCHAR simpleQuotedCN_W[] = { '"','"','"','1','"','"','"',0 };
567 static const WCHAR multipleAttrCN_W[] = { 'C','N','=','"','1','+','2','"',0 };
568 static const WCHAR simpleMultipleAttrCN_W[] = { '"','1','+','2','"',0 };
569 static const WCHAR commaCN_W[] = { 'C','N','=','"','a',',','b','"',0 };
570 static const WCHAR simpleCommaCN_W[] = { '"','a',',','b','"',0 };
571 static const WCHAR equalCN_W[] = { 'C','N','=','"','a','=','b','"',0 };
572 static const WCHAR simpleEqualCN_W[] = { '"','a','=','b','"',0 };
573 static const WCHAR lessThanCN_W[] = { 'C','N','=','"','<','"',0 };
574 static const WCHAR simpleLessThanCN_W[] = { '"','<','"',0 };
575 static const WCHAR greaterThanCN_W[] = { 'C','N','=','"','>','"',0 };
576 static const WCHAR simpleGreaterThanCN_W[] = { '"','>','"',0 };
577 static const WCHAR hashCN_W[] = { 'C','N','=','"','#','"',0 };
578 static const WCHAR simpleHashCN_W[] = { '"','#','"',0 };
579 static const WCHAR semiCN_W[] = { 'C','N','=','"',';','"',0 };
580 static const WCHAR simpleSemiCN_W[] = { '"',';','"',0 };
581 static const WCHAR newlineCN_W[] = { 'C','N','=','"','a','\n','b','"',0 };
582 static const WCHAR simpleNewlineCN_W[] = { '"','a','\n','b','"',0 };
584 static void test_CertNameToStrW(void)
586 PCCERT_CONTEXT context;
587 CERT_NAME_BLOB blob;
589 if (!pCertNameToStrW)
591 win_skip("CertNameToStrW is not available\n");
592 return;
595 context = CertCreateCertificateContext(X509_ASN_ENCODING, cert,
596 sizeof(cert));
597 ok(context != NULL, "CertCreateCertificateContext failed: %08x\n",
598 GetLastError());
599 if (context)
601 DWORD ret;
603 /* This crashes
604 ret = pCertNameToStrW(0, NULL, 0, NULL, 0);
606 /* Test with a bogus encoding type */
607 SetLastError(0xdeadbeef);
608 ret = pCertNameToStrW(0, &context->pCertInfo->Issuer, 0, NULL, 0);
609 ok(ret == 1 && GetLastError() == ERROR_FILE_NOT_FOUND,
610 "Expected retval 1 and ERROR_FILE_NOT_FOUND, got %d - %08x\n",
611 ret, GetLastError());
612 SetLastError(0xdeadbeef);
613 ret = pCertNameToStrW(X509_ASN_ENCODING, &context->pCertInfo->Issuer,
614 0, NULL, 0);
615 ok(ret && GetLastError() == ERROR_SUCCESS,
616 "Expected positive return and ERROR_SUCCESS, got %d - %08x\n",
617 ret, GetLastError());
619 test_NameToStrConversionW(&context->pCertInfo->Issuer,
620 CERT_SIMPLE_NAME_STR, issuerStrW, FALSE);
621 test_NameToStrConversionW(&context->pCertInfo->Issuer,
622 CERT_SIMPLE_NAME_STR | CERT_NAME_STR_SEMICOLON_FLAG,
623 issuerStrSemicolonW, FALSE);
624 test_NameToStrConversionW(&context->pCertInfo->Issuer,
625 CERT_SIMPLE_NAME_STR | CERT_NAME_STR_CRLF_FLAG,
626 issuerStrCRLFW, FALSE);
627 test_NameToStrConversionW(&context->pCertInfo->Subject,
628 CERT_OID_NAME_STR, subjectStrW, FALSE);
629 test_NameToStrConversionW(&context->pCertInfo->Subject,
630 CERT_OID_NAME_STR | CERT_NAME_STR_SEMICOLON_FLAG,
631 subjectStrSemicolonW, FALSE);
632 test_NameToStrConversionW(&context->pCertInfo->Subject,
633 CERT_OID_NAME_STR | CERT_NAME_STR_CRLF_FLAG,
634 subjectStrCRLFW, FALSE);
635 test_NameToStrConversionW(&context->pCertInfo->Subject,
636 CERT_X500_NAME_STR | CERT_NAME_STR_SEMICOLON_FLAG | CERT_NAME_STR_REVERSE_FLAG,
637 x500SubjectStrSemicolonReverseW, FALSE);
639 CertFreeCertificateContext(context);
641 blob.pbData = encodedSimpleCN;
642 blob.cbData = sizeof(encodedSimpleCN);
643 test_NameToStrConversionW(&blob, CERT_X500_NAME_STR, simpleCN_W, FALSE);
644 blob.pbData = encodedSingleQuotedCN;
645 blob.cbData = sizeof(encodedSingleQuotedCN);
646 test_NameToStrConversionW(&blob, CERT_X500_NAME_STR, singledQuotedCN_W,
647 FALSE);
648 test_NameToStrConversionW(&blob, CERT_SIMPLE_NAME_STR,
649 simpleSingleQuotedCN_W, FALSE);
650 blob.pbData = encodedSpacedCN;
651 blob.cbData = sizeof(encodedSpacedCN);
652 test_NameToStrConversionW(&blob, CERT_X500_NAME_STR, spacedCN_W, FALSE);
653 test_NameToStrConversionW(&blob, CERT_SIMPLE_NAME_STR, simpleSpacedCN_W,
654 FALSE);
655 blob.pbData = encodedQuotedCN;
656 blob.cbData = sizeof(encodedQuotedCN);
657 test_NameToStrConversionW(&blob, CERT_X500_NAME_STR, quotedCN_W,
658 FALSE);
659 test_NameToStrConversionW(&blob, CERT_SIMPLE_NAME_STR, simpleQuotedCN_W,
660 FALSE);
661 blob.pbData = encodedMultipleAttrCN;
662 blob.cbData = sizeof(encodedMultipleAttrCN);
663 test_NameToStrConversionW(&blob, CERT_X500_NAME_STR, multipleAttrCN_W,
664 FALSE);
665 test_NameToStrConversionW(&blob, CERT_SIMPLE_NAME_STR,
666 simpleMultipleAttrCN_W, FALSE);
667 blob.pbData = encodedCommaCN;
668 blob.cbData = sizeof(encodedCommaCN);
669 test_NameToStrConversionW(&blob, CERT_X500_NAME_STR, commaCN_W, FALSE);
670 test_NameToStrConversionW(&blob, CERT_SIMPLE_NAME_STR, simpleCommaCN_W,
671 FALSE);
672 blob.pbData = encodedEqualCN;
673 blob.cbData = sizeof(encodedEqualCN);
674 test_NameToStrConversionW(&blob, CERT_X500_NAME_STR, equalCN_W, FALSE);
675 test_NameToStrConversionW(&blob, CERT_SIMPLE_NAME_STR, simpleEqualCN_W,
676 FALSE);
677 blob.pbData = encodedLessThanCN;
678 blob.cbData = sizeof(encodedLessThanCN);
679 test_NameToStrConversionW(&blob, CERT_X500_NAME_STR, lessThanCN_W, FALSE);
680 test_NameToStrConversionW(&blob, CERT_SIMPLE_NAME_STR, simpleLessThanCN_W,
681 FALSE);
682 blob.pbData = encodedGreaterThanCN;
683 blob.cbData = sizeof(encodedGreaterThanCN);
684 test_NameToStrConversionW(&blob, CERT_X500_NAME_STR, greaterThanCN_W,
685 FALSE);
686 test_NameToStrConversionW(&blob, CERT_SIMPLE_NAME_STR,
687 simpleGreaterThanCN_W, FALSE);
688 blob.pbData = encodedHashCN;
689 blob.cbData = sizeof(encodedHashCN);
690 test_NameToStrConversionW(&blob, CERT_X500_NAME_STR, hashCN_W, FALSE);
691 test_NameToStrConversionW(&blob, CERT_SIMPLE_NAME_STR, simpleHashCN_W,
692 FALSE);
693 blob.pbData = encodedSemiCN;
694 blob.cbData = sizeof(encodedSemiCN);
695 test_NameToStrConversionW(&blob, CERT_X500_NAME_STR, semiCN_W, FALSE);
696 test_NameToStrConversionW(&blob, CERT_SIMPLE_NAME_STR, simpleSemiCN_W,
697 FALSE);
698 blob.pbData = encodedNewlineCN;
699 blob.cbData = sizeof(encodedNewlineCN);
700 test_NameToStrConversionW(&blob, CERT_X500_NAME_STR, newlineCN_W, FALSE);
701 test_NameToStrConversionW(&blob, CERT_SIMPLE_NAME_STR, simpleNewlineCN_W,
702 FALSE);
705 struct StrToNameA
707 LPCSTR x500;
708 DWORD encodedSize;
709 const BYTE *encoded;
712 static const struct StrToNameA namesA[] = {
713 { "CN=1", sizeof(encodedSimpleCN), encodedSimpleCN },
714 { "CN=\"1\"", sizeof(encodedSimpleCN), encodedSimpleCN },
715 { "CN = \"1\"", sizeof(encodedSimpleCN), encodedSimpleCN },
716 { "CN='1'", sizeof(encodedSingleQuotedCN), encodedSingleQuotedCN },
717 { "CN=\" 1 \"", sizeof(encodedSpacedCN), encodedSpacedCN },
718 { "CN=\"\"\"1\"\"\"", sizeof(encodedQuotedCN), encodedQuotedCN },
719 { "CN=\"1+2\"", sizeof(encodedMultipleAttrCN), encodedMultipleAttrCN },
720 { "CN=\"a,b\"", sizeof(encodedCommaCN), encodedCommaCN },
721 { "CN=\"a=b\"", sizeof(encodedEqualCN), encodedEqualCN },
722 { "CN=\"<\"", sizeof(encodedLessThanCN), encodedLessThanCN },
723 { "CN=\">\"", sizeof(encodedGreaterThanCN), encodedGreaterThanCN },
724 { "CN=\"#\"", sizeof(encodedHashCN), encodedHashCN },
725 { "CN=\";\"", sizeof(encodedSemiCN), encodedSemiCN },
726 { "CN=dummy,T=test", sizeof(encodedDummyCN), encodedDummyCN },
727 { " CN = Wine Test,T = 123, C = BR", sizeof(encodedFields), encodedFields },
730 static void test_CertStrToNameA(void)
732 BOOL ret;
733 DWORD size, i;
734 BYTE buf[100];
736 if (!pCertStrToNameA)
738 win_skip("CertStrToNameA is not available\n");
739 return;
742 /* Crash
743 ret = pCertStrToNameA(0, NULL, 0, NULL, NULL, NULL, NULL);
745 ret = pCertStrToNameA(0, NULL, 0, NULL, NULL, &size, NULL);
746 ok(!ret, "Expected failure\n");
747 ret = pCertStrToNameA(0, "bogus", 0, NULL, NULL, &size, NULL);
748 ok(!ret && GetLastError() == CRYPT_E_INVALID_X500_STRING,
749 "Expected CRYPT_E_INVALID_X500_STRING, got %08x\n", GetLastError());
750 ret = pCertStrToNameA(0, "foo=1", 0, NULL, NULL, &size, NULL);
751 ok(!ret && GetLastError() == CRYPT_E_INVALID_X500_STRING,
752 "Expected CRYPT_E_INVALID_X500_STRING, got %08x\n", GetLastError());
753 ret = pCertStrToNameA(0, "CN=1", 0, NULL, NULL, &size, NULL);
754 ok(!ret && GetLastError() == ERROR_FILE_NOT_FOUND,
755 "Expected ERROR_FILE_NOT_FOUND, got %08x\n", GetLastError());
756 ret = pCertStrToNameA(X509_ASN_ENCODING, "CN=1", 0, NULL, NULL, &size, NULL);
757 ok(ret, "CertStrToNameA failed: %08x\n", GetLastError());
758 size = sizeof(buf);
759 ret = pCertStrToNameA(X509_ASN_ENCODING, "CN=\"\"1\"\"", 0, NULL, buf, &size,
760 NULL);
761 ok(!ret && GetLastError() == CRYPT_E_INVALID_X500_STRING,
762 "Expected CRYPT_E_INVALID_X500_STRING, got %08x\n", GetLastError());
763 ret = pCertStrToNameA(X509_ASN_ENCODING, "CN=1+2", 0, NULL, buf,
764 &size, NULL);
765 ok(!ret && GetLastError() == CRYPT_E_INVALID_X500_STRING,
766 "Expected CRYPT_E_INVALID_X500_STRING, got %08x\n", GetLastError());
767 ret = pCertStrToNameA(X509_ASN_ENCODING, "CN=1+2", CERT_NAME_STR_NO_PLUS_FLAG, NULL, buf,
768 &size, NULL);
769 ok(ret && GetLastError() == ERROR_SUCCESS,
770 "Expected ERROR_SUCCESS, got %08x\n", GetLastError());
771 ret = pCertStrToNameA(X509_ASN_ENCODING, "CN=1,2", CERT_NAME_STR_NO_QUOTING_FLAG, NULL, buf,
772 &size, NULL);
773 ok(!ret && GetLastError() == CRYPT_E_INVALID_X500_STRING,
774 "Expected CRYPT_E_INVALID_X500_STRING, got %08x\n", GetLastError());
775 ret = pCertStrToNameA(X509_ASN_ENCODING, "CN=\"1,2;3,4\"", CERT_NAME_STR_NO_QUOTING_FLAG, 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 = pCertStrToNameA(X509_ASN_ENCODING, "CN=abc", 0, NULL, buf,
780 &size, NULL);
781 ok(ret && GetLastError() == ERROR_SUCCESS,
782 "Expected ERROR_SUCCESS, got %08x\n", GetLastError());
783 ret = pCertStrToNameA(X509_ASN_ENCODING, "CN=abc", CERT_NAME_STR_NO_QUOTING_FLAG, NULL, buf,
784 &size, NULL);
785 ok(ret && GetLastError() == ERROR_SUCCESS,
786 "Expected ERROR_SUCCESS, got %08x\n", GetLastError());
787 ret = pCertStrToNameA(X509_ASN_ENCODING, "CN=\"abc\"", 0, NULL, buf,
788 &size, NULL);
789 ok(ret && GetLastError() == ERROR_SUCCESS,
790 "Expected ERROR_SUCCESS, got %08x\n", GetLastError());
791 ret = pCertStrToNameA(X509_ASN_ENCODING, "CN=\"abc\"", CERT_NAME_STR_NO_QUOTING_FLAG, NULL, buf,
792 &size, NULL);
793 ok(!ret && GetLastError() == ERROR_MORE_DATA,
794 "Expected ERROR_MORE_DATA, got %08x\n", GetLastError());
795 for (i = 0; i < ARRAY_SIZE(namesA); i++)
797 size = sizeof(buf);
798 ret = pCertStrToNameA(X509_ASN_ENCODING, namesA[i].x500, 0, NULL, buf,
799 &size, NULL);
800 ok(ret, "CertStrToNameA failed on string %s: %08x\n", namesA[i].x500,
801 GetLastError());
802 ok(size == namesA[i].encodedSize,
803 "Expected size %d, got %d\n", namesA[i].encodedSize, size);
804 if (ret)
805 ok(!memcmp(buf, namesA[i].encoded, namesA[i].encodedSize),
806 "Unexpected value for string %s\n", namesA[i].x500);
810 struct StrToNameW
812 LPCWSTR x500;
813 DWORD encodedSize;
814 const BYTE *encoded;
817 static const WCHAR badlyQuotedCN_W[] = { 'C','N','=','"','"','1','"','"',0 };
818 static const WCHAR simpleCN2_W[] = { 'C','N','=','"','1','"',0 };
819 static const WCHAR simpleCN3_W[] = { 'C','N',' ','=',' ','"','1','"',0 };
820 static const WCHAR japaneseCN_W[] = { 'C','N','=',0x226f,0x575b,0 };
821 static const WCHAR dummyCN_W[] = { 'C','N','=','d','u','m','m','y',',','T','=','t','e','s','t',0 };
822 static const WCHAR encodedFields_W[] = { ' ','C','N',' ','=',' ',' ',' ','W','i','n','e',' ','T',
823 'e','s','t',',','T',' ','=',' ','1','2','3',',',' ','C',
824 ' ','=',' ','B','R',0 };
825 static const BYTE encodedJapaneseCN[] = { 0x30,0x0f,0x31,0x0d,0x30,0x0b,0x06,
826 0x03,0x55,0x04,0x03,0x1e,0x04,0x22,0x6f,0x57,0x5b };
828 static const struct StrToNameW namesW[] = {
829 { simpleCN_W, sizeof(encodedSimpleCN), encodedSimpleCN },
830 { simpleCN2_W, sizeof(encodedSimpleCN), encodedSimpleCN },
831 { simpleCN3_W, sizeof(encodedSimpleCN), encodedSimpleCN },
832 { singledQuotedCN_W, sizeof(encodedSingleQuotedCN), encodedSingleQuotedCN },
833 { spacedCN_W, sizeof(encodedSpacedCN), encodedSpacedCN },
834 { quotedCN_W, sizeof(encodedQuotedCN), encodedQuotedCN },
835 { multipleAttrCN_W, sizeof(encodedMultipleAttrCN), encodedMultipleAttrCN },
836 { japaneseCN_W, sizeof(encodedJapaneseCN), encodedJapaneseCN },
837 { commaCN_W, sizeof(encodedCommaCN), encodedCommaCN },
838 { equalCN_W, sizeof(encodedEqualCN), encodedEqualCN },
839 { lessThanCN_W, sizeof(encodedLessThanCN), encodedLessThanCN },
840 { greaterThanCN_W, sizeof(encodedGreaterThanCN), encodedGreaterThanCN },
841 { hashCN_W, sizeof(encodedHashCN), encodedHashCN },
842 { semiCN_W, sizeof(encodedSemiCN), encodedSemiCN },
843 { dummyCN_W, sizeof(encodedDummyCN), encodedDummyCN },
844 { encodedFields_W, sizeof(encodedFields), encodedFields },
847 static void test_CertStrToNameW(void)
849 static const WCHAR bogusW[] = { 'b','o','g','u','s',0 };
850 static const WCHAR fooW[] = { 'f','o','o','=','1',0 };
851 BOOL ret;
852 DWORD size, i;
853 LPCWSTR errorPtr;
854 BYTE buf[100];
856 if (!pCertStrToNameW)
858 win_skip("CertStrToNameW is not available\n");
859 return;
862 /* Crash
863 ret = pCertStrToNameW(0, NULL, 0, NULL, NULL, NULL, NULL);
865 ret = pCertStrToNameW(0, NULL, 0, NULL, NULL, &size, NULL);
866 ok(!ret, "Expected failure\n");
867 ret = pCertStrToNameW(0, bogusW, 0, NULL, NULL, &size, NULL);
868 ok(!ret && GetLastError() == CRYPT_E_INVALID_X500_STRING,
869 "Expected CRYPT_E_INVALID_X500_STRING, got %08x\n", GetLastError());
870 ret = pCertStrToNameW(0, fooW, 0, NULL, NULL, &size, NULL);
871 ok(!ret && GetLastError() == CRYPT_E_INVALID_X500_STRING,
872 "Expected CRYPT_E_INVALID_X500_STRING, got %08x\n", GetLastError());
873 ret = pCertStrToNameW(0, simpleCN_W, 0, NULL, NULL, &size, NULL);
874 ok(!ret && GetLastError() == ERROR_FILE_NOT_FOUND,
875 "Expected ERROR_FILE_NOT_FOUND, got %08x\n", GetLastError());
876 ret = pCertStrToNameW(X509_ASN_ENCODING, simpleCN_W, 0, NULL, NULL, &size,
877 NULL);
878 ok(ret, "CertStrToNameW failed: %08x\n", GetLastError());
879 size = sizeof(buf);
880 ret = pCertStrToNameW(X509_ASN_ENCODING, badlyQuotedCN_W, 0, NULL, buf,
881 &size, NULL);
882 ok(!ret && GetLastError() == CRYPT_E_INVALID_X500_STRING,
883 "Expected CRYPT_E_INVALID_X500_STRING, got %08x\n", GetLastError());
884 ret = pCertStrToNameW(X509_ASN_ENCODING, badlyQuotedCN_W, 0, NULL, buf,
885 &size, &errorPtr);
886 ok(!ret && GetLastError() == CRYPT_E_INVALID_X500_STRING,
887 "Expected CRYPT_E_INVALID_X500_STRING, got %08x\n", GetLastError());
888 ok(errorPtr && *errorPtr == '1', "Expected first error character was 1\n");
889 for (i = 0; i < ARRAY_SIZE(namesW); i++)
891 size = sizeof(buf);
892 ret = pCertStrToNameW(X509_ASN_ENCODING, namesW[i].x500, 0, NULL, buf,
893 &size, NULL);
894 ok(ret, "Index %d: CertStrToNameW failed: %08x\n", i, GetLastError());
895 ok(size == namesW[i].encodedSize,
896 "Index %d: expected size %d, got %d\n", i, namesW[i].encodedSize,
897 size);
898 if (ret)
899 ok(!memcmp(buf, namesW[i].encoded, size),
900 "Index %d: unexpected value for string %s\n", i, wine_dbgstr_w(namesW[i].x500));
904 static void test_CertGetNameStringA(void)
906 PCCERT_CONTEXT context;
908 if (!pCertGetNameStringA)
910 win_skip("CertGetNameStringA is not available\n");
911 return;
914 context = CertCreateCertificateContext(X509_ASN_ENCODING, cert,
915 sizeof(cert));
916 ok(context != NULL, "CertCreateCertificateContext failed: %08x\n",
917 GetLastError());
918 if (context)
920 static const char aric[] = "aric@codeweavers.com";
921 static const char localhost[] = "localhost";
922 DWORD len, type;
923 LPSTR str;
925 /* Bad string types/types missing from the cert */
926 len = pCertGetNameStringA(NULL, 0, 0, NULL, NULL, 0);
927 ok(len == 1, "expected 1, got %d\n", len);
928 len = pCertGetNameStringA(context, 0, 0, NULL, NULL, 0);
929 ok(len == 1, "expected 1, got %d\n", len);
930 len = pCertGetNameStringA(context, CERT_NAME_URL_TYPE, 0, NULL, NULL,
932 ok(len == 1, "expected 1, got %d\n", len);
934 len = pCertGetNameStringA(context, CERT_NAME_EMAIL_TYPE, 0, NULL, NULL,
936 ok(len == strlen(aric) + 1, "unexpected length %d\n", len);
937 str = HeapAlloc(GetProcessHeap(), 0, len);
938 if (str)
940 len = pCertGetNameStringA(context, CERT_NAME_EMAIL_TYPE, 0, NULL,
941 str, len);
942 ok(!strcmp(str, aric), "unexpected value %s\n", str);
943 HeapFree(GetProcessHeap(), 0, str);
946 len = pCertGetNameStringA(context, CERT_NAME_RDN_TYPE, 0, NULL, NULL,
948 ok(len == strlen(issuerStr) + 1, "unexpected length %d\n", len);
949 str = HeapAlloc(GetProcessHeap(), 0, len);
950 if (str)
952 len = pCertGetNameStringA(context, CERT_NAME_RDN_TYPE, 0, NULL,
953 str, len);
954 ok(!strcmp(str, issuerStr), "unexpected value %s\n", str);
955 HeapFree(GetProcessHeap(), 0, str);
957 type = 0;
958 len = pCertGetNameStringA(context, CERT_NAME_RDN_TYPE, 0, &type, NULL,
960 ok(len == strlen(issuerStr) + 1, "unexpected length %d\n", len);
961 str = HeapAlloc(GetProcessHeap(), 0, len);
962 if (str)
964 len = pCertGetNameStringA(context, CERT_NAME_RDN_TYPE, 0, &type,
965 str, len);
966 ok(!strcmp(str, issuerStr), "unexpected value %s\n", str);
967 HeapFree(GetProcessHeap(), 0, str);
969 type = CERT_OID_NAME_STR;
970 len = pCertGetNameStringA(context, CERT_NAME_RDN_TYPE, 0, &type, NULL,
972 ok(len == strlen(subjectStr) + 1, "unexpected length %d\n", len);
973 str = HeapAlloc(GetProcessHeap(), 0, len);
974 if (str)
976 len = pCertGetNameStringA(context, CERT_NAME_RDN_TYPE, 0, &type,
977 str, len);
978 ok(!strcmp(str, subjectStr), "unexpected value %s\n", str);
979 HeapFree(GetProcessHeap(), 0, str);
982 len = pCertGetNameStringA(context, CERT_NAME_ATTR_TYPE, 0, NULL, NULL,
984 ok(len == strlen(aric) + 1, "unexpected length %d\n", len);
985 str = HeapAlloc(GetProcessHeap(), 0, len);
986 if (str)
988 len = pCertGetNameStringA(context, CERT_NAME_ATTR_TYPE, 0, NULL,
989 str, len);
990 ok(!strcmp(str, aric), "unexpected value %s\n", str);
991 HeapFree(GetProcessHeap(), 0, str);
993 len = pCertGetNameStringA(context, CERT_NAME_ATTR_TYPE, 0,
994 (void *)szOID_RSA_emailAddr, NULL, 0);
995 ok(len == strlen(aric) + 1, "unexpected length %d\n", len);
996 str = HeapAlloc(GetProcessHeap(), 0, len);
997 if (str)
999 len = pCertGetNameStringA(context, CERT_NAME_ATTR_TYPE, 0,
1000 (void *)szOID_RSA_emailAddr, str, len);
1001 ok(!strcmp(str, aric), "unexpected value %s\n", str);
1002 HeapFree(GetProcessHeap(), 0, str);
1004 len = pCertGetNameStringA(context, CERT_NAME_ATTR_TYPE, 0,
1005 (void *)szOID_COMMON_NAME, NULL, 0);
1006 ok(len == strlen(localhost) + 1, "unexpected length %d\n", len);
1007 str = HeapAlloc(GetProcessHeap(), 0, len);
1008 if (str)
1010 len = pCertGetNameStringA(context, CERT_NAME_ATTR_TYPE, 0,
1011 (void *)szOID_COMMON_NAME, str, len);
1012 ok(!strcmp(str, localhost), "unexpected value %s\n", str);
1013 HeapFree(GetProcessHeap(), 0, str);
1016 len = pCertGetNameStringA(context, CERT_NAME_SIMPLE_DISPLAY_TYPE, 0,
1017 NULL, NULL, 0);
1018 ok(len == strlen(localhost) + 1, "unexpected length %d\n", len);
1019 str = HeapAlloc(GetProcessHeap(), 0, len);
1020 if (str)
1022 len = pCertGetNameStringA(context, CERT_NAME_SIMPLE_DISPLAY_TYPE,
1023 0, NULL, str, len);
1024 ok(!strcmp(str, localhost), "unexpected value %s\n", str);
1025 HeapFree(GetProcessHeap(), 0, str);
1028 len = pCertGetNameStringA(context, CERT_NAME_FRIENDLY_DISPLAY_TYPE, 0,
1029 NULL, NULL, 0);
1030 ok(len == strlen(localhost) + 1, "unexpected length %d\n", len);
1031 str = HeapAlloc(GetProcessHeap(), 0, len);
1032 if (str)
1034 len = pCertGetNameStringA(context, CERT_NAME_FRIENDLY_DISPLAY_TYPE,
1035 0, NULL, str, len);
1036 ok(!strcmp(str, localhost), "unexpected value %s\n", str);
1037 HeapFree(GetProcessHeap(), 0, str);
1040 len = pCertGetNameStringA(context, CERT_NAME_DNS_TYPE, 0, NULL, NULL,
1042 ok(len == strlen(localhost) + 1 || broken(len == 1) /* NT4 */,
1043 "unexpected length %d\n", len);
1044 if (len > 1)
1046 str = HeapAlloc(GetProcessHeap(), 0, len);
1047 if (str)
1049 len = pCertGetNameStringA(context, CERT_NAME_DNS_TYPE, 0, NULL,
1050 str, len);
1051 ok(!strcmp(str, localhost), "unexpected value %s\n", str);
1052 HeapFree(GetProcessHeap(), 0, str);
1056 CertFreeCertificateContext(context);
1060 START_TEST(str)
1062 dll = GetModuleHandleA("Crypt32.dll");
1064 pCertNameToStrA = (void*)GetProcAddress(dll,"CertNameToStrA");
1065 pCertNameToStrW = (void*)GetProcAddress(dll,"CertNameToStrW");
1066 pCertRDNValueToStrA = (void*)GetProcAddress(dll, "CertRDNValueToStrA");
1067 pCertRDNValueToStrW = (void*)GetProcAddress(dll, "CertRDNValueToStrW");
1068 pCertStrToNameA = (void*)GetProcAddress(dll,"CertStrToNameA");
1069 pCertStrToNameW = (void*)GetProcAddress(dll,"CertStrToNameW");
1070 pCertGetNameStringA = (void*)GetProcAddress(dll, "CertGetNameStringA");
1072 test_CertRDNValueToStrA();
1073 test_CertRDNValueToStrW();
1074 test_CertNameToStrA();
1075 test_CertNameToStrW();
1076 test_CertStrToNameA();
1077 test_CertStrToNameW();
1078 test_CertGetNameStringA();