po: Update simplified Chinese translation.
[wine/multimedia.git] / dlls / crypt32 / tests / str.c
blob14435975422a4d623022168dcc81e5c3a13821ed
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 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 typedef DWORD (WINAPI *CertGetNameStringAFunc)(PCCERT_CONTEXT cert, DWORD type,
203 DWORD flags, void *typePara, LPSTR str, DWORD cch);
205 static HMODULE dll;
206 static CertNameToStrAFunc pCertNameToStrA;
207 static CertNameToStrWFunc pCertNameToStrW;
208 static CryptDecodeObjectFunc pCryptDecodeObject;
209 static CertRDNValueToStrAFunc pCertRDNValueToStrA;
210 static CertRDNValueToStrWFunc pCertRDNValueToStrW;
211 static CertStrToNameAFunc pCertStrToNameA;
212 static CertStrToNameWFunc pCertStrToNameW;
213 static CertGetNameStringAFunc pCertGetNameStringA;
215 static void test_CertRDNValueToStrA(void)
217 CertRDNAttrEncoding attrs[] = {
218 { "2.5.4.6", CERT_RDN_PRINTABLE_STRING,
219 { sizeof(bin1), bin1 }, "US", FALSE },
220 { "2.5.4.8", CERT_RDN_PRINTABLE_STRING,
221 { sizeof(bin2), bin2 }, "Minnesota", FALSE },
222 { "2.5.4.7", CERT_RDN_PRINTABLE_STRING,
223 { sizeof(bin3), bin3 }, "Minneapolis", FALSE },
224 { "2.5.4.10", CERT_RDN_PRINTABLE_STRING,
225 { sizeof(bin4), bin4 }, "CodeWeavers", FALSE },
226 { "2.5.4.11", CERT_RDN_PRINTABLE_STRING,
227 { sizeof(bin5), bin5 }, "Wine Development", FALSE },
228 { "2.5.4.3", CERT_RDN_PRINTABLE_STRING,
229 { sizeof(bin6), bin6 }, "localhost", FALSE },
230 { "1.2.840.113549.1.9.1", CERT_RDN_IA5_STRING,
231 { sizeof(bin7), bin7 }, "aric@codeweavers.com", FALSE },
232 { "0", CERT_RDN_PRINTABLE_STRING,
233 { sizeof(bin9), bin9 }, "abc\"def", FALSE },
234 { "0", CERT_RDN_PRINTABLE_STRING,
235 { sizeof(bin10), bin10 }, "abc'def", FALSE },
236 { "0", CERT_RDN_PRINTABLE_STRING,
237 { sizeof(bin11), bin11 }, "abc, def", FALSE },
238 { "0", CERT_RDN_PRINTABLE_STRING,
239 { sizeof(bin12), bin12 }, " abc ", FALSE },
240 { "0", CERT_RDN_PRINTABLE_STRING,
241 { sizeof(bin13), bin13 }, "\"def\"", FALSE },
242 { "0", CERT_RDN_PRINTABLE_STRING,
243 { sizeof(bin14), bin14 }, "1;3", FALSE },
245 DWORD i, ret;
246 char buffer[2000];
247 CERT_RDN_VALUE_BLOB blob = { 0, NULL };
248 static const char ePKI[] = "ePKI Root Certification Authority";
250 if (!pCertRDNValueToStrA) return;
252 /* This crashes
253 ret = pCertRDNValueToStrA(0, NULL, NULL, 0);
255 /* With empty input, it generates the empty string */
256 SetLastError(0xdeadbeef);
257 ret = pCertRDNValueToStrA(0, &blob, NULL, 0);
258 ok(ret == 1 && GetLastError() == 0xdeadbeef, "Expected empty string\n");
259 ret = pCertRDNValueToStrA(0, &blob, buffer, sizeof(buffer));
260 ok(ret == 1 && GetLastError() == 0xdeadbeef, "Expected empty string\n");
261 ok(!buffer[0], "Expected empty string\n");
263 for (i = 0; i < sizeof(attrs) / sizeof(attrs[0]); i++)
265 ret = pCertRDNValueToStrA(attrs[i].dwValueType, &attrs[i].Value,
266 buffer, sizeof(buffer));
267 if (attrs[i].todo)
269 todo_wine
270 ok(ret == strlen(attrs[i].str) + 1, "Expected length %d, got %d\n",
271 lstrlenA(attrs[i].str) + 1, ret);
272 todo_wine
273 ok(!strcmp(buffer, attrs[i].str), "Expected %s, got %s\n",
274 attrs[i].str, buffer);
276 else
278 ok(ret == strlen(attrs[i].str) + 1, "Expected length %d, got %d\n",
279 lstrlenA(attrs[i].str) + 1, ret);
280 ok(!strcmp(buffer, attrs[i].str), "Expected %s, got %s\n",
281 attrs[i].str, buffer);
284 blob.pbData = bin8;
285 blob.cbData = sizeof(bin8);
286 ret = pCertRDNValueToStrA(CERT_RDN_UTF8_STRING, &blob, buffer,
287 sizeof(buffer));
288 ok(ret == strlen(ePKI) + 1 || broken(ret != strlen(ePKI) + 1),
289 "Expected length %d, got %d\n", lstrlenA(ePKI), ret);
290 if (ret == strlen(ePKI) + 1)
291 ok(!strcmp(buffer, ePKI), "Expected %s, got %s\n", ePKI, buffer);
294 static void test_CertRDNValueToStrW(void)
296 static const WCHAR usW[] = { 'U','S',0 };
297 static const WCHAR minnesotaW[] = { 'M','i','n','n','e','s','o','t','a',0 };
298 static const WCHAR minneapolisW[] = { 'M','i','n','n','e','a','p','o','l',
299 'i','s',0 };
300 static const WCHAR codeweaversW[] = { 'C','o','d','e','W','e','a','v','e',
301 'r','s',0 };
302 static const WCHAR wineDevW[] = { 'W','i','n','e',' ','D','e','v','e','l',
303 'o','p','m','e','n','t',0 };
304 static const WCHAR localhostW[] = { 'l','o','c','a','l','h','o','s','t',0 };
305 static const WCHAR aricW[] = { 'a','r','i','c','@','c','o','d','e','w','e',
306 'a','v','e','r','s','.','c','o','m',0 };
307 static const WCHAR ePKIW[] = { 'e','P','K','I',' ','R','o','o','t',' ',
308 'C','e','r','t','i','f','i','c','a','t','i','o','n',' ','A','u','t','h',
309 'o','r','i','t','y',0 };
310 static const WCHAR embeddedDoubleQuoteW[] = { 'a','b','c','"','d','e','f',
311 0 };
312 static const WCHAR embeddedSingleQuoteW[] = { 'a','b','c','\'','d','e','f',
313 0 };
314 static const WCHAR embeddedCommaW[] = { 'a','b','c',',',' ','d','e','f',0 };
315 static const WCHAR trailingAndEndingSpaceW[] = { ' ','a','b','c',' ',0 };
316 static const WCHAR enclosingQuotesW[] = { '"','d','e','f','"',0 };
317 static const WCHAR embeddedSemiW[] = { '1',';','3',0 };
318 CertRDNAttrEncodingW attrs[] = {
319 { "2.5.4.6", CERT_RDN_PRINTABLE_STRING,
320 { sizeof(bin1), bin1 }, usW, FALSE },
321 { "2.5.4.8", CERT_RDN_PRINTABLE_STRING,
322 { sizeof(bin2), bin2 }, minnesotaW, FALSE },
323 { "2.5.4.7", CERT_RDN_PRINTABLE_STRING,
324 { sizeof(bin3), bin3 }, minneapolisW, FALSE },
325 { "2.5.4.10", CERT_RDN_PRINTABLE_STRING,
326 { sizeof(bin4), bin4 }, codeweaversW, FALSE },
327 { "2.5.4.11", CERT_RDN_PRINTABLE_STRING,
328 { sizeof(bin5), bin5 }, wineDevW, FALSE },
329 { "2.5.4.3", CERT_RDN_PRINTABLE_STRING,
330 { sizeof(bin6), bin6 }, localhostW, FALSE },
331 { "1.2.840.113549.1.9.1", CERT_RDN_IA5_STRING,
332 { sizeof(bin7), bin7 }, aricW, FALSE },
333 { "0", CERT_RDN_PRINTABLE_STRING,
334 { sizeof(bin9), bin9 }, embeddedDoubleQuoteW, FALSE },
335 { "0", CERT_RDN_PRINTABLE_STRING,
336 { sizeof(bin10), bin10 }, embeddedSingleQuoteW, FALSE },
337 { "0", CERT_RDN_PRINTABLE_STRING,
338 { sizeof(bin11), bin11 }, embeddedCommaW, FALSE },
339 { "0", CERT_RDN_PRINTABLE_STRING,
340 { sizeof(bin12), bin12 }, trailingAndEndingSpaceW, FALSE },
341 { "0", CERT_RDN_PRINTABLE_STRING,
342 { sizeof(bin13), bin13 }, enclosingQuotesW, FALSE },
343 { "0", CERT_RDN_PRINTABLE_STRING,
344 { sizeof(bin14), bin14 }, embeddedSemiW, FALSE },
346 DWORD i, ret;
347 WCHAR buffer[2000];
348 CERT_RDN_VALUE_BLOB blob = { 0, NULL };
350 if (!pCertRDNValueToStrW)
352 win_skip("CertRDNValueToStrW is not available\n");
353 return;
356 /* This crashes
357 ret = pCertRDNValueToStrW(0, NULL, NULL, 0);
359 /* With empty input, it generates the empty string */
360 SetLastError(0xdeadbeef);
361 ret = pCertRDNValueToStrW(0, &blob, NULL, 0);
362 ok(ret == 1 && GetLastError() == 0xdeadbeef, "Expected empty string\n");
363 ret = pCertRDNValueToStrW(0, &blob, buffer,
364 sizeof(buffer) / sizeof(buffer[0]));
365 ok(ret == 1 && GetLastError() == 0xdeadbeef, "Expected empty string\n");
366 ok(!buffer[0], "Expected empty string\n");
368 for (i = 0; i < sizeof(attrs) / sizeof(attrs[0]); i++)
370 ret = pCertRDNValueToStrW(attrs[i].dwValueType, &attrs[i].Value,
371 buffer, sizeof(buffer) / sizeof(buffer[0]));
372 if (attrs[i].todo)
374 todo_wine
375 ok(ret == lstrlenW(attrs[i].str) + 1,
376 "Expected length %d, got %d\n", lstrlenW(attrs[i].str) + 1, ret);
377 todo_wine
378 ok(!lstrcmpW(buffer, attrs[i].str), "Expected %s, got %s\n",
379 wine_dbgstr_w(attrs[i].str), wine_dbgstr_w(buffer));
381 else
383 ok(ret == lstrlenW(attrs[i].str) + 1,
384 "Expected length %d, got %d\n", lstrlenW(attrs[i].str) + 1, ret);
385 ok(!lstrcmpW(buffer, attrs[i].str), "Expected %s, got %s\n",
386 wine_dbgstr_w(attrs[i].str), wine_dbgstr_w(buffer));
389 blob.pbData = bin8;
390 blob.cbData = sizeof(bin8);
391 ret = pCertRDNValueToStrW(CERT_RDN_UTF8_STRING, &blob, buffer,
392 sizeof(buffer));
393 ok(ret == lstrlenW(ePKIW) + 1 || broken(ret != lstrlenW(ePKIW) + 1),
394 "Expected length %d, got %d\n", lstrlenW(ePKIW), ret);
395 if (ret == lstrlenW(ePKIW) + 1)
396 ok(!lstrcmpW(buffer, ePKIW), "Expected %s, got %s\n",
397 wine_dbgstr_w(ePKIW), wine_dbgstr_w(buffer));
400 static void test_NameToStrConversionA(PCERT_NAME_BLOB pName, DWORD dwStrType,
401 LPCSTR expected, BOOL todo)
403 char buffer[2000] = { 0 };
404 DWORD i;
406 i = pCertNameToStrA(X509_ASN_ENCODING, pName, dwStrType, NULL, 0);
407 if (todo)
408 todo_wine
409 ok(i == strlen(expected) + 1, "Expected %d chars, got %d\n",
410 lstrlenA(expected) + 1, i);
411 else
412 ok(i == strlen(expected) + 1, "Expected %d chars, got %d\n",
413 lstrlenA(expected) + 1, i);
414 i = pCertNameToStrA(X509_ASN_ENCODING,pName, dwStrType, buffer,
415 sizeof(buffer));
416 if (todo)
417 todo_wine
418 ok(i == strlen(expected) + 1, "Expected %d chars, got %d\n",
419 lstrlenA(expected) + 1, i);
420 else
421 ok(i == strlen(expected) + 1, "Expected %d chars, got %d\n",
422 lstrlenA(expected) + 1, i);
423 if (todo)
424 todo_wine
425 ok(!strcmp(buffer, expected), "Expected %s, got %s\n", expected,
426 buffer);
427 else
428 ok(!strcmp(buffer, expected), "Expected %s, got %s\n", expected,
429 buffer);
432 static BYTE encodedSimpleCN[] = {
433 0x30,0x0c,0x31,0x0a,0x30,0x08,0x06,0x03,0x55,0x04,0x03,0x13,0x01,0x31 };
434 static BYTE encodedSingleQuotedCN[] = { 0x30,0x0e,0x31,0x0c,0x30,0x0a,
435 0x06,0x03,0x55,0x04,0x03,0x13,0x03,0x27,0x31,0x27 };
436 static BYTE encodedSpacedCN[] = { 0x30,0x0e,0x31,0x0c,0x30,0x0a,0x06,0x03,
437 0x55,0x04,0x03,0x13,0x03,0x20,0x31,0x20 };
438 static BYTE encodedQuotedCN[] = { 0x30,0x11,0x31,0x0f,0x30,0x0d,0x06,0x03,
439 0x55, 0x04,0x03,0x1e,0x06,0x00,0x22,0x00,0x31,0x00,0x22, };
440 static BYTE encodedMultipleAttrCN[] = { 0x30,0x0e,0x31,0x0c,0x30,0x0a,
441 0x06,0x03,0x55,0x04,0x03,0x13,0x03,0x31,0x2b,0x32 };
442 static BYTE encodedCommaCN[] = {
443 0x30,0x0e,0x31,0x0c,0x30,0x0a,0x06,0x03,0x55,0x04,0x03,0x13,0x03,0x61,0x2c,
444 0x62 };
445 static BYTE encodedEqualCN[] = {
446 0x30,0x0e,0x31,0x0c,0x30,0x0a,0x06,0x03,0x55,0x04,0x03,0x13,0x03,0x61,0x3d,
447 0x62 };
448 static BYTE encodedLessThanCN[] = {
449 0x30,0x0d,0x31,0x0b,0x30,0x09,0x06,0x03,0x55,0x04,0x03,0x1e,0x02,0x00,0x3c
451 static BYTE encodedGreaterThanCN[] = {
452 0x30,0x0d,0x31,0x0b,0x30,0x09,0x06,0x03,0x55,0x04,0x03,0x1e,0x02,0x00,0x3e
454 static BYTE encodedHashCN[] = {
455 0x30,0x0d,0x31,0x0b,0x30,0x09,0x06,0x03,0x55,0x04,0x03,0x1e,0x02,0x00,0x23
457 static BYTE encodedSemiCN[] = {
458 0x30,0x0d,0x31,0x0b,0x30,0x09,0x06,0x03,0x55,0x04,0x03,0x1e,0x02,0x00,0x3b
460 static BYTE encodedNewlineCN[] = {
461 0x30,0x11,0x31,0x0f,0x30,0x0d,0x06,0x03,0x55,0x04,0x03,0x1e,0x06,0x00,0x61,
462 0x00,0x0a,0x00,0x62 };
464 static void test_CertNameToStrA(void)
466 PCCERT_CONTEXT context;
467 CERT_NAME_BLOB blob;
469 if (!pCertNameToStrA)
471 win_skip("CertNameToStrA is not available\n");
472 return;
475 context = CertCreateCertificateContext(X509_ASN_ENCODING, cert,
476 sizeof(cert));
477 ok(context != NULL, "CertCreateCertificateContext failed: %08x\n",
478 GetLastError());
479 if (context)
481 DWORD ret;
483 /* This crashes
484 ret = pCertNameToStrA(0, NULL, 0, NULL, 0);
486 /* Test with a bogus encoding type */
487 SetLastError(0xdeadbeef);
488 ret = pCertNameToStrA(0, &context->pCertInfo->Issuer, 0, NULL, 0);
489 ok(ret == 1 && GetLastError() == ERROR_FILE_NOT_FOUND,
490 "Expected retval 1 and ERROR_FILE_NOT_FOUND, got %d - %08x\n",
491 ret, GetLastError());
492 SetLastError(0xdeadbeef);
493 ret = pCertNameToStrA(X509_ASN_ENCODING, &context->pCertInfo->Issuer,
494 0, NULL, 0);
495 ok(ret && GetLastError() == ERROR_SUCCESS,
496 "Expected positive return and ERROR_SUCCESS, got %d - %08x\n",
497 ret, GetLastError());
499 test_NameToStrConversionA(&context->pCertInfo->Issuer,
500 CERT_SIMPLE_NAME_STR, issuerStr, FALSE);
501 test_NameToStrConversionA(&context->pCertInfo->Issuer,
502 CERT_SIMPLE_NAME_STR | CERT_NAME_STR_SEMICOLON_FLAG,
503 issuerStrSemicolon, FALSE);
504 test_NameToStrConversionA(&context->pCertInfo->Issuer,
505 CERT_SIMPLE_NAME_STR | CERT_NAME_STR_CRLF_FLAG,
506 issuerStrCRLF, FALSE);
507 test_NameToStrConversionA(&context->pCertInfo->Subject,
508 CERT_OID_NAME_STR, subjectStr, FALSE);
509 test_NameToStrConversionA(&context->pCertInfo->Subject,
510 CERT_OID_NAME_STR | CERT_NAME_STR_SEMICOLON_FLAG,
511 subjectStrSemicolon, FALSE);
512 test_NameToStrConversionA(&context->pCertInfo->Subject,
513 CERT_OID_NAME_STR | CERT_NAME_STR_CRLF_FLAG,
514 subjectStrCRLF, FALSE);
515 test_NameToStrConversionA(&context->pCertInfo->Subject,
516 CERT_X500_NAME_STR, x500SubjectStr, FALSE);
517 test_NameToStrConversionA(&context->pCertInfo->Subject,
518 CERT_X500_NAME_STR | CERT_NAME_STR_SEMICOLON_FLAG | CERT_NAME_STR_REVERSE_FLAG,
519 x500SubjectStrSemicolonReverse, FALSE);
521 CertFreeCertificateContext(context);
523 blob.pbData = encodedSimpleCN;
524 blob.cbData = sizeof(encodedSimpleCN);
525 test_NameToStrConversionA(&blob, CERT_X500_NAME_STR, "CN=1", FALSE);
526 blob.pbData = encodedSingleQuotedCN;
527 blob.cbData = sizeof(encodedSingleQuotedCN);
528 test_NameToStrConversionA(&blob, CERT_X500_NAME_STR, "CN='1'", FALSE);
529 test_NameToStrConversionA(&blob, CERT_SIMPLE_NAME_STR, "'1'", FALSE);
530 blob.pbData = encodedSpacedCN;
531 blob.cbData = sizeof(encodedSpacedCN);
532 test_NameToStrConversionA(&blob, CERT_X500_NAME_STR, "CN=\" 1 \"", FALSE);
533 test_NameToStrConversionA(&blob, CERT_SIMPLE_NAME_STR, "\" 1 \"", FALSE);
534 blob.pbData = encodedQuotedCN;
535 blob.cbData = sizeof(encodedQuotedCN);
536 test_NameToStrConversionA(&blob, CERT_X500_NAME_STR, "CN=\"\"\"1\"\"\"",
537 FALSE);
538 test_NameToStrConversionA(&blob, CERT_SIMPLE_NAME_STR, "\"\"\"1\"\"\"",
539 FALSE);
540 blob.pbData = encodedMultipleAttrCN;
541 blob.cbData = sizeof(encodedMultipleAttrCN);
542 test_NameToStrConversionA(&blob, CERT_X500_NAME_STR, "CN=\"1+2\"", FALSE);
543 test_NameToStrConversionA(&blob, CERT_SIMPLE_NAME_STR, "\"1+2\"", FALSE);
544 blob.pbData = encodedCommaCN;
545 blob.cbData = sizeof(encodedCommaCN);
546 test_NameToStrConversionA(&blob, CERT_X500_NAME_STR, "CN=\"a,b\"", FALSE);
547 test_NameToStrConversionA(&blob, CERT_SIMPLE_NAME_STR, "\"a,b\"", FALSE);
548 blob.pbData = encodedEqualCN;
549 blob.cbData = sizeof(encodedEqualCN);
550 test_NameToStrConversionA(&blob, CERT_X500_NAME_STR, "CN=\"a=b\"", FALSE);
551 test_NameToStrConversionA(&blob, CERT_SIMPLE_NAME_STR, "\"a=b\"", FALSE);
552 blob.pbData = encodedLessThanCN;
553 blob.cbData = sizeof(encodedLessThanCN);
554 test_NameToStrConversionA(&blob, CERT_X500_NAME_STR, "CN=\"<\"", FALSE);
555 test_NameToStrConversionA(&blob, CERT_SIMPLE_NAME_STR, "\"<\"", FALSE);
556 blob.pbData = encodedGreaterThanCN;
557 blob.cbData = sizeof(encodedGreaterThanCN);
558 test_NameToStrConversionA(&blob, CERT_X500_NAME_STR, "CN=\">\"", FALSE);
559 test_NameToStrConversionA(&blob, CERT_SIMPLE_NAME_STR, "\">\"", FALSE);
560 blob.pbData = encodedHashCN;
561 blob.cbData = sizeof(encodedHashCN);
562 test_NameToStrConversionA(&blob, CERT_X500_NAME_STR, "CN=\"#\"", FALSE);
563 test_NameToStrConversionA(&blob, CERT_SIMPLE_NAME_STR, "\"#\"", FALSE);
564 blob.pbData = encodedSemiCN;
565 blob.cbData = sizeof(encodedSemiCN);
566 test_NameToStrConversionA(&blob, CERT_X500_NAME_STR, "CN=\";\"", FALSE);
567 test_NameToStrConversionA(&blob, CERT_SIMPLE_NAME_STR, "\";\"", FALSE);
568 blob.pbData = encodedNewlineCN;
569 blob.cbData = sizeof(encodedNewlineCN);
570 test_NameToStrConversionA(&blob, CERT_X500_NAME_STR, "CN=\"a\nb\"", FALSE);
571 test_NameToStrConversionA(&blob, CERT_SIMPLE_NAME_STR, "\"a\nb\"", FALSE);
574 static void test_NameToStrConversionW(PCERT_NAME_BLOB pName, DWORD dwStrType,
575 LPCWSTR expected, BOOL todo)
577 WCHAR buffer[2000] = { 0 };
578 DWORD i;
580 i = pCertNameToStrW(X509_ASN_ENCODING,pName, dwStrType, NULL, 0);
581 if (todo)
582 todo_wine ok(i == lstrlenW(expected) + 1, "Expected %d chars, got %d\n",
583 lstrlenW(expected) + 1, i);
584 else
585 ok(i == lstrlenW(expected) + 1, "Expected %d chars, got %d\n",
586 lstrlenW(expected) + 1, i);
587 i = pCertNameToStrW(X509_ASN_ENCODING,pName, dwStrType, buffer,
588 sizeof(buffer) / sizeof(buffer[0]));
589 if (todo)
590 todo_wine ok(i == lstrlenW(expected) + 1, "Expected %d chars, got %d\n",
591 lstrlenW(expected) + 1, i);
592 else
593 ok(i == lstrlenW(expected) + 1, "Expected %d chars, got %d\n",
594 lstrlenW(expected) + 1, i);
595 if (todo)
596 todo_wine ok(!lstrcmpW(buffer, expected), "Expected %s, got %s\n",
597 wine_dbgstr_w(expected), wine_dbgstr_w(buffer));
598 else
599 ok(!lstrcmpW(buffer, expected), "Expected %s, got %s\n",
600 wine_dbgstr_w(expected), wine_dbgstr_w(buffer));
603 static const WCHAR simpleCN_W[] = { 'C','N','=','1',0 };
604 static const WCHAR singledQuotedCN_W[] = { 'C','N','=','\'','1','\'',0 };
605 static const WCHAR simpleSingleQuotedCN_W[] = { '\'','1','\'',0 };
606 static const WCHAR spacedCN_W[] = { 'C','N','=','"',' ','1',' ','"',0 };
607 static const WCHAR simpleSpacedCN_W[] = { '"',' ','1',' ','"',0 };
608 static const WCHAR quotedCN_W[] = { 'C','N','=','"','"','"','1','"','"','"',0 };
609 static const WCHAR simpleQuotedCN_W[] = { '"','"','"','1','"','"','"',0 };
610 static const WCHAR multipleAttrCN_W[] = { 'C','N','=','"','1','+','2','"',0 };
611 static const WCHAR simpleMultipleAttrCN_W[] = { '"','1','+','2','"',0 };
612 static const WCHAR commaCN_W[] = { 'C','N','=','"','a',',','b','"',0 };
613 static const WCHAR simpleCommaCN_W[] = { '"','a',',','b','"',0 };
614 static const WCHAR equalCN_W[] = { 'C','N','=','"','a','=','b','"',0 };
615 static const WCHAR simpleEqualCN_W[] = { '"','a','=','b','"',0 };
616 static const WCHAR lessThanCN_W[] = { 'C','N','=','"','<','"',0 };
617 static const WCHAR simpleLessThanCN_W[] = { '"','<','"',0 };
618 static const WCHAR greaterThanCN_W[] = { 'C','N','=','"','>','"',0 };
619 static const WCHAR simpleGreaterThanCN_W[] = { '"','>','"',0 };
620 static const WCHAR hashCN_W[] = { 'C','N','=','"','#','"',0 };
621 static const WCHAR simpleHashCN_W[] = { '"','#','"',0 };
622 static const WCHAR semiCN_W[] = { 'C','N','=','"',';','"',0 };
623 static const WCHAR simpleSemiCN_W[] = { '"',';','"',0 };
624 static const WCHAR newlineCN_W[] = { 'C','N','=','"','a','\n','b','"',0 };
625 static const WCHAR simpleNewlineCN_W[] = { '"','a','\n','b','"',0 };
627 static void test_CertNameToStrW(void)
629 PCCERT_CONTEXT context;
630 CERT_NAME_BLOB blob;
632 if (!pCertNameToStrW)
634 win_skip("CertNameToStrW is not available\n");
635 return;
638 context = CertCreateCertificateContext(X509_ASN_ENCODING, cert,
639 sizeof(cert));
640 ok(context != NULL, "CertCreateCertificateContext failed: %08x\n",
641 GetLastError());
642 if (context)
644 DWORD ret;
646 /* This crashes
647 ret = pCertNameToStrW(0, NULL, 0, NULL, 0);
649 /* Test with a bogus encoding type */
650 SetLastError(0xdeadbeef);
651 ret = pCertNameToStrW(0, &context->pCertInfo->Issuer, 0, NULL, 0);
652 ok(ret == 1 && GetLastError() == ERROR_FILE_NOT_FOUND,
653 "Expected retval 1 and ERROR_FILE_NOT_FOUND, got %d - %08x\n",
654 ret, GetLastError());
655 SetLastError(0xdeadbeef);
656 ret = pCertNameToStrW(X509_ASN_ENCODING, &context->pCertInfo->Issuer,
657 0, NULL, 0);
658 ok(ret && GetLastError() == ERROR_SUCCESS,
659 "Expected positive return and ERROR_SUCCESS, got %d - %08x\n",
660 ret, GetLastError());
662 test_NameToStrConversionW(&context->pCertInfo->Issuer,
663 CERT_SIMPLE_NAME_STR, issuerStrW, FALSE);
664 test_NameToStrConversionW(&context->pCertInfo->Issuer,
665 CERT_SIMPLE_NAME_STR | CERT_NAME_STR_SEMICOLON_FLAG,
666 issuerStrSemicolonW, FALSE);
667 test_NameToStrConversionW(&context->pCertInfo->Issuer,
668 CERT_SIMPLE_NAME_STR | CERT_NAME_STR_CRLF_FLAG,
669 issuerStrCRLFW, FALSE);
670 test_NameToStrConversionW(&context->pCertInfo->Subject,
671 CERT_OID_NAME_STR, subjectStrW, FALSE);
672 test_NameToStrConversionW(&context->pCertInfo->Subject,
673 CERT_OID_NAME_STR | CERT_NAME_STR_SEMICOLON_FLAG,
674 subjectStrSemicolonW, FALSE);
675 test_NameToStrConversionW(&context->pCertInfo->Subject,
676 CERT_OID_NAME_STR | CERT_NAME_STR_CRLF_FLAG,
677 subjectStrCRLFW, FALSE);
678 test_NameToStrConversionW(&context->pCertInfo->Subject,
679 CERT_X500_NAME_STR | CERT_NAME_STR_SEMICOLON_FLAG | CERT_NAME_STR_REVERSE_FLAG,
680 x500SubjectStrSemicolonReverseW, FALSE);
682 CertFreeCertificateContext(context);
684 blob.pbData = encodedSimpleCN;
685 blob.cbData = sizeof(encodedSimpleCN);
686 test_NameToStrConversionW(&blob, CERT_X500_NAME_STR, simpleCN_W, FALSE);
687 blob.pbData = encodedSingleQuotedCN;
688 blob.cbData = sizeof(encodedSingleQuotedCN);
689 test_NameToStrConversionW(&blob, CERT_X500_NAME_STR, singledQuotedCN_W,
690 FALSE);
691 test_NameToStrConversionW(&blob, CERT_SIMPLE_NAME_STR,
692 simpleSingleQuotedCN_W, FALSE);
693 blob.pbData = encodedSpacedCN;
694 blob.cbData = sizeof(encodedSpacedCN);
695 test_NameToStrConversionW(&blob, CERT_X500_NAME_STR, spacedCN_W, FALSE);
696 test_NameToStrConversionW(&blob, CERT_SIMPLE_NAME_STR, simpleSpacedCN_W,
697 FALSE);
698 blob.pbData = encodedQuotedCN;
699 blob.cbData = sizeof(encodedQuotedCN);
700 test_NameToStrConversionW(&blob, CERT_X500_NAME_STR, quotedCN_W,
701 FALSE);
702 test_NameToStrConversionW(&blob, CERT_SIMPLE_NAME_STR, simpleQuotedCN_W,
703 FALSE);
704 blob.pbData = encodedMultipleAttrCN;
705 blob.cbData = sizeof(encodedMultipleAttrCN);
706 test_NameToStrConversionW(&blob, CERT_X500_NAME_STR, multipleAttrCN_W,
707 FALSE);
708 test_NameToStrConversionW(&blob, CERT_SIMPLE_NAME_STR,
709 simpleMultipleAttrCN_W, FALSE);
710 blob.pbData = encodedCommaCN;
711 blob.cbData = sizeof(encodedCommaCN);
712 test_NameToStrConversionW(&blob, CERT_X500_NAME_STR, commaCN_W, FALSE);
713 test_NameToStrConversionW(&blob, CERT_SIMPLE_NAME_STR, simpleCommaCN_W,
714 FALSE);
715 blob.pbData = encodedEqualCN;
716 blob.cbData = sizeof(encodedEqualCN);
717 test_NameToStrConversionW(&blob, CERT_X500_NAME_STR, equalCN_W, FALSE);
718 test_NameToStrConversionW(&blob, CERT_SIMPLE_NAME_STR, simpleEqualCN_W,
719 FALSE);
720 blob.pbData = encodedLessThanCN;
721 blob.cbData = sizeof(encodedLessThanCN);
722 test_NameToStrConversionW(&blob, CERT_X500_NAME_STR, lessThanCN_W, FALSE);
723 test_NameToStrConversionW(&blob, CERT_SIMPLE_NAME_STR, simpleLessThanCN_W,
724 FALSE);
725 blob.pbData = encodedGreaterThanCN;
726 blob.cbData = sizeof(encodedGreaterThanCN);
727 test_NameToStrConversionW(&blob, CERT_X500_NAME_STR, greaterThanCN_W,
728 FALSE);
729 test_NameToStrConversionW(&blob, CERT_SIMPLE_NAME_STR,
730 simpleGreaterThanCN_W, FALSE);
731 blob.pbData = encodedHashCN;
732 blob.cbData = sizeof(encodedHashCN);
733 test_NameToStrConversionW(&blob, CERT_X500_NAME_STR, hashCN_W, FALSE);
734 test_NameToStrConversionW(&blob, CERT_SIMPLE_NAME_STR, simpleHashCN_W,
735 FALSE);
736 blob.pbData = encodedSemiCN;
737 blob.cbData = sizeof(encodedSemiCN);
738 test_NameToStrConversionW(&blob, CERT_X500_NAME_STR, semiCN_W, FALSE);
739 test_NameToStrConversionW(&blob, CERT_SIMPLE_NAME_STR, simpleSemiCN_W,
740 FALSE);
741 blob.pbData = encodedNewlineCN;
742 blob.cbData = sizeof(encodedNewlineCN);
743 test_NameToStrConversionW(&blob, CERT_X500_NAME_STR, newlineCN_W, FALSE);
744 test_NameToStrConversionW(&blob, CERT_SIMPLE_NAME_STR, simpleNewlineCN_W,
745 FALSE);
748 struct StrToNameA
750 LPCSTR x500;
751 DWORD encodedSize;
752 const BYTE *encoded;
755 static const struct StrToNameA namesA[] = {
756 { "CN=1", sizeof(encodedSimpleCN), encodedSimpleCN },
757 { "CN=\"1\"", sizeof(encodedSimpleCN), encodedSimpleCN },
758 { "CN = \"1\"", sizeof(encodedSimpleCN), encodedSimpleCN },
759 { "CN='1'", sizeof(encodedSingleQuotedCN), encodedSingleQuotedCN },
760 { "CN=\" 1 \"", sizeof(encodedSpacedCN), encodedSpacedCN },
761 { "CN=\"\"\"1\"\"\"", sizeof(encodedQuotedCN), encodedQuotedCN },
762 { "CN=\"1+2\"", sizeof(encodedMultipleAttrCN), encodedMultipleAttrCN },
763 { "CN=\"a,b\"", sizeof(encodedCommaCN), encodedCommaCN },
764 { "CN=\"a=b\"", sizeof(encodedEqualCN), encodedEqualCN },
765 { "CN=\"<\"", sizeof(encodedLessThanCN), encodedLessThanCN },
766 { "CN=\">\"", sizeof(encodedGreaterThanCN), encodedGreaterThanCN },
767 { "CN=\"#\"", sizeof(encodedHashCN), encodedHashCN },
768 { "CN=\";\"", sizeof(encodedSemiCN), encodedSemiCN },
771 static void test_CertStrToNameA(void)
773 BOOL ret;
774 DWORD size, i;
775 BYTE buf[100];
777 if (!pCertStrToNameA)
779 win_skip("CertStrToNameA is not available\n");
780 return;
783 /* Crash
784 ret = pCertStrToNameA(0, NULL, 0, NULL, NULL, NULL, NULL);
786 ret = pCertStrToNameA(0, NULL, 0, NULL, NULL, &size, NULL);
787 ok(!ret, "Expected failure\n");
788 ret = pCertStrToNameA(0, "bogus", 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, "foo=1", 0, NULL, NULL, &size, NULL);
792 ok(!ret && GetLastError() == CRYPT_E_INVALID_X500_STRING,
793 "Expected CRYPT_E_INVALID_X500_STRING, got %08x\n", GetLastError());
794 ret = pCertStrToNameA(0, "CN=1", 0, NULL, NULL, &size, NULL);
795 ok(!ret && GetLastError() == ERROR_FILE_NOT_FOUND,
796 "Expected ERROR_FILE_NOT_FOUND, got %08x\n", GetLastError());
797 ret = pCertStrToNameA(X509_ASN_ENCODING, "CN=1", 0, NULL, NULL, &size, NULL);
798 ok(ret, "CertStrToNameA failed: %08x\n", GetLastError());
799 size = sizeof(buf);
800 ret = pCertStrToNameA(X509_ASN_ENCODING, "CN=\"\"1\"\"", 0, NULL, buf, &size,
801 NULL);
802 ok(!ret && GetLastError() == CRYPT_E_INVALID_X500_STRING,
803 "Expected CRYPT_E_INVALID_X500_STRING, got %08x\n", GetLastError());
804 ret = pCertStrToNameA(X509_ASN_ENCODING, "CN=1+2", 0, NULL, buf,
805 &size, NULL);
806 ok(!ret && GetLastError() == CRYPT_E_INVALID_X500_STRING,
807 "Expected CRYPT_E_INVALID_X500_STRING, got %08x\n", GetLastError());
808 ret = pCertStrToNameA(X509_ASN_ENCODING, "CN=1+2", CERT_NAME_STR_NO_PLUS_FLAG, NULL, buf,
809 &size, NULL);
810 ok(ret && GetLastError() == ERROR_SUCCESS,
811 "Expected ERROR_SUCCESS, got %08x\n", GetLastError());
812 ret = pCertStrToNameA(X509_ASN_ENCODING, "CN=1,2", CERT_NAME_STR_NO_QUOTING_FLAG, NULL, buf,
813 &size, NULL);
814 ok(!ret && GetLastError() == CRYPT_E_INVALID_X500_STRING,
815 "Expected CRYPT_E_INVALID_X500_STRING, got %08x\n", GetLastError());
816 ret = pCertStrToNameA(X509_ASN_ENCODING, "CN=\"1,2;3,4\"", CERT_NAME_STR_NO_QUOTING_FLAG, NULL, buf,
817 &size, NULL);
818 ok(!ret && GetLastError() == CRYPT_E_INVALID_X500_STRING,
819 "Expected CRYPT_E_INVALID_X500_STRING, got %08x\n", GetLastError());
820 ret = pCertStrToNameA(X509_ASN_ENCODING, "CN=abc", 0, NULL, buf,
821 &size, NULL);
822 ok(ret && GetLastError() == ERROR_SUCCESS,
823 "Expected ERROR_SUCCESS, got %08x\n", GetLastError());
824 ret = pCertStrToNameA(X509_ASN_ENCODING, "CN=abc", CERT_NAME_STR_NO_QUOTING_FLAG, NULL, buf,
825 &size, NULL);
826 ok(ret && GetLastError() == ERROR_SUCCESS,
827 "Expected ERROR_SUCCESS, got %08x\n", GetLastError());
828 ret = pCertStrToNameA(X509_ASN_ENCODING, "CN=\"abc\"", 0, NULL, buf,
829 &size, NULL);
830 ok(ret && GetLastError() == ERROR_SUCCESS,
831 "Expected ERROR_SUCCESS, got %08x\n", GetLastError());
832 ret = pCertStrToNameA(X509_ASN_ENCODING, "CN=\"abc\"", CERT_NAME_STR_NO_QUOTING_FLAG, NULL, buf,
833 &size, NULL);
834 ok(!ret && GetLastError() == ERROR_MORE_DATA,
835 "Expected ERROR_MORE_DATA, got %08x\n", GetLastError());
836 for (i = 0; i < sizeof(namesA) / sizeof(namesA[0]); i++)
838 size = sizeof(buf);
839 ret = pCertStrToNameA(X509_ASN_ENCODING, namesA[i].x500, 0, NULL, buf,
840 &size, NULL);
841 ok(ret, "CertStrToNameA failed on string %s: %08x\n", namesA[i].x500,
842 GetLastError());
843 ok(size == namesA[i].encodedSize,
844 "Expected size %d, got %d\n", namesA[i].encodedSize, size);
845 if (ret)
846 ok(!memcmp(buf, namesA[i].encoded, namesA[i].encodedSize),
847 "Unexpected value for string %s\n", namesA[i].x500);
851 struct StrToNameW
853 LPCWSTR x500;
854 DWORD encodedSize;
855 const BYTE *encoded;
858 static const WCHAR badlyQuotedCN_W[] = { 'C','N','=','"','"','1','"','"',0 };
859 static const WCHAR simpleCN2_W[] = { 'C','N','=','"','1','"',0 };
860 static const WCHAR simpleCN3_W[] = { 'C','N',' ','=',' ','"','1','"',0 };
861 static const WCHAR japaneseCN_W[] = { 'C','N','=',0x226f,0x575b,0 };
862 static const BYTE encodedJapaneseCN[] = { 0x30,0x0f,0x31,0x0d,0x30,0x0b,0x06,
863 0x03,0x55,0x04,0x03,0x1e,0x04,0x22,0x6f,0x57,0x5b };
865 static const struct StrToNameW namesW[] = {
866 { simpleCN_W, sizeof(encodedSimpleCN), encodedSimpleCN },
867 { simpleCN2_W, sizeof(encodedSimpleCN), encodedSimpleCN },
868 { simpleCN3_W, sizeof(encodedSimpleCN), encodedSimpleCN },
869 { singledQuotedCN_W, sizeof(encodedSingleQuotedCN), encodedSingleQuotedCN },
870 { spacedCN_W, sizeof(encodedSpacedCN), encodedSpacedCN },
871 { quotedCN_W, sizeof(encodedQuotedCN), encodedQuotedCN },
872 { multipleAttrCN_W, sizeof(encodedMultipleAttrCN), encodedMultipleAttrCN },
873 { japaneseCN_W, sizeof(encodedJapaneseCN), encodedJapaneseCN },
874 { commaCN_W, sizeof(encodedCommaCN), encodedCommaCN },
875 { equalCN_W, sizeof(encodedEqualCN), encodedEqualCN },
876 { lessThanCN_W, sizeof(encodedLessThanCN), encodedLessThanCN },
877 { greaterThanCN_W, sizeof(encodedGreaterThanCN), encodedGreaterThanCN },
878 { hashCN_W, sizeof(encodedHashCN), encodedHashCN },
879 { semiCN_W, sizeof(encodedSemiCN), encodedSemiCN },
882 static void test_CertStrToNameW(void)
884 static const WCHAR bogusW[] = { 'b','o','g','u','s',0 };
885 static const WCHAR fooW[] = { 'f','o','o','=','1',0 };
886 BOOL ret;
887 DWORD size, i;
888 LPCWSTR errorPtr;
889 BYTE buf[100];
891 if (!pCertStrToNameW)
893 win_skip("CertStrToNameW is not available\n");
894 return;
897 /* Crash
898 ret = pCertStrToNameW(0, NULL, 0, NULL, NULL, NULL, NULL);
900 ret = pCertStrToNameW(0, NULL, 0, NULL, NULL, &size, NULL);
901 ok(!ret, "Expected failure\n");
902 ret = pCertStrToNameW(0, bogusW, 0, NULL, NULL, &size, NULL);
903 ok(!ret && GetLastError() == CRYPT_E_INVALID_X500_STRING,
904 "Expected CRYPT_E_INVALID_X500_STRING, got %08x\n", GetLastError());
905 ret = pCertStrToNameW(0, fooW, 0, NULL, NULL, &size, NULL);
906 ok(!ret && GetLastError() == CRYPT_E_INVALID_X500_STRING,
907 "Expected CRYPT_E_INVALID_X500_STRING, got %08x\n", GetLastError());
908 ret = pCertStrToNameW(0, simpleCN_W, 0, NULL, NULL, &size, NULL);
909 ok(!ret && GetLastError() == ERROR_FILE_NOT_FOUND,
910 "Expected ERROR_FILE_NOT_FOUND, got %08x\n", GetLastError());
911 ret = pCertStrToNameW(X509_ASN_ENCODING, simpleCN_W, 0, NULL, NULL, &size,
912 NULL);
913 ok(ret, "CertStrToNameW failed: %08x\n", GetLastError());
914 size = sizeof(buf);
915 ret = pCertStrToNameW(X509_ASN_ENCODING, badlyQuotedCN_W, 0, NULL, buf,
916 &size, NULL);
917 ok(!ret && GetLastError() == CRYPT_E_INVALID_X500_STRING,
918 "Expected CRYPT_E_INVALID_X500_STRING, got %08x\n", GetLastError());
919 ret = pCertStrToNameW(X509_ASN_ENCODING, badlyQuotedCN_W, 0, NULL, buf,
920 &size, &errorPtr);
921 ok(!ret && GetLastError() == CRYPT_E_INVALID_X500_STRING,
922 "Expected CRYPT_E_INVALID_X500_STRING, got %08x\n", GetLastError());
923 ok(errorPtr && *errorPtr == '1', "Expected first error character was 1\n");
924 for (i = 0; i < sizeof(namesW) / sizeof(namesW[0]); i++)
926 size = sizeof(buf);
927 ret = pCertStrToNameW(X509_ASN_ENCODING, namesW[i].x500, 0, NULL, buf,
928 &size, NULL);
929 ok(ret, "Index %d: CertStrToNameW failed: %08x\n", i, GetLastError());
930 ok(size == namesW[i].encodedSize,
931 "Index %d: expected size %d, got %d\n", i, namesW[i].encodedSize,
932 size);
933 if (ret)
934 ok(!memcmp(buf, namesW[i].encoded, size),
935 "Index %d: unexpected value\n", i);
939 static void test_CertGetNameStringA(void)
941 PCCERT_CONTEXT context;
943 if (!pCertGetNameStringA)
945 win_skip("CertGetNameStringA is not available\n");
946 return;
949 context = CertCreateCertificateContext(X509_ASN_ENCODING, cert,
950 sizeof(cert));
951 ok(context != NULL, "CertCreateCertificateContext failed: %08x\n",
952 GetLastError());
953 if (context)
955 static const char aric[] = "aric@codeweavers.com";
956 static const char localhost[] = "localhost";
957 DWORD len, type;
958 LPSTR str;
960 /* Bad string types/types missing from the cert */
961 len = pCertGetNameStringA(NULL, 0, 0, NULL, NULL, 0);
962 ok(len == 1, "expected 1, got %d\n", len);
963 len = pCertGetNameStringA(context, 0, 0, NULL, NULL, 0);
964 ok(len == 1, "expected 1, got %d\n", len);
965 len = pCertGetNameStringA(context, CERT_NAME_URL_TYPE, 0, NULL, NULL,
967 ok(len == 1, "expected 1, got %d\n", len);
969 len = pCertGetNameStringA(context, CERT_NAME_EMAIL_TYPE, 0, NULL, NULL,
971 ok(len == strlen(aric) + 1, "unexpected length %d\n", len);
972 str = HeapAlloc(GetProcessHeap(), 0, len);
973 if (str)
975 len = pCertGetNameStringA(context, CERT_NAME_EMAIL_TYPE, 0, NULL,
976 str, len);
977 ok(!strcmp(str, aric), "unexpected value %s\n", str);
978 HeapFree(GetProcessHeap(), 0, str);
981 len = pCertGetNameStringA(context, CERT_NAME_RDN_TYPE, 0, NULL, NULL,
983 ok(len == strlen(issuerStr) + 1, "unexpected length %d\n", len);
984 str = HeapAlloc(GetProcessHeap(), 0, len);
985 if (str)
987 len = pCertGetNameStringA(context, CERT_NAME_RDN_TYPE, 0, NULL,
988 str, len);
989 ok(!strcmp(str, issuerStr), "unexpected value %s\n", str);
990 HeapFree(GetProcessHeap(), 0, str);
992 type = 0;
993 len = pCertGetNameStringA(context, CERT_NAME_RDN_TYPE, 0, &type, NULL,
995 ok(len == strlen(issuerStr) + 1, "unexpected length %d\n", len);
996 str = HeapAlloc(GetProcessHeap(), 0, len);
997 if (str)
999 len = pCertGetNameStringA(context, CERT_NAME_RDN_TYPE, 0, &type,
1000 str, len);
1001 ok(!strcmp(str, issuerStr), "unexpected value %s\n", str);
1002 HeapFree(GetProcessHeap(), 0, str);
1004 type = CERT_OID_NAME_STR;
1005 len = pCertGetNameStringA(context, CERT_NAME_RDN_TYPE, 0, &type, NULL,
1007 ok(len == strlen(subjectStr) + 1, "unexpected length %d\n", len);
1008 str = HeapAlloc(GetProcessHeap(), 0, len);
1009 if (str)
1011 len = pCertGetNameStringA(context, CERT_NAME_RDN_TYPE, 0, &type,
1012 str, len);
1013 ok(!strcmp(str, subjectStr), "unexpected value %s\n", str);
1014 HeapFree(GetProcessHeap(), 0, str);
1017 len = pCertGetNameStringA(context, CERT_NAME_ATTR_TYPE, 0, NULL, NULL,
1019 ok(len == strlen(aric) + 1, "unexpected length %d\n", len);
1020 str = HeapAlloc(GetProcessHeap(), 0, len);
1021 if (str)
1023 len = pCertGetNameStringA(context, CERT_NAME_ATTR_TYPE, 0, NULL,
1024 str, len);
1025 ok(!strcmp(str, aric), "unexpected value %s\n", str);
1026 HeapFree(GetProcessHeap(), 0, str);
1028 len = pCertGetNameStringA(context, CERT_NAME_ATTR_TYPE, 0,
1029 (void *)szOID_RSA_emailAddr, NULL, 0);
1030 ok(len == strlen(aric) + 1, "unexpected length %d\n", len);
1031 str = HeapAlloc(GetProcessHeap(), 0, len);
1032 if (str)
1034 len = pCertGetNameStringA(context, CERT_NAME_ATTR_TYPE, 0,
1035 (void *)szOID_RSA_emailAddr, str, len);
1036 ok(!strcmp(str, aric), "unexpected value %s\n", str);
1037 HeapFree(GetProcessHeap(), 0, str);
1039 len = pCertGetNameStringA(context, CERT_NAME_ATTR_TYPE, 0,
1040 (void *)szOID_COMMON_NAME, NULL, 0);
1041 ok(len == strlen(localhost) + 1, "unexpected length %d\n", len);
1042 str = HeapAlloc(GetProcessHeap(), 0, len);
1043 if (str)
1045 len = pCertGetNameStringA(context, CERT_NAME_ATTR_TYPE, 0,
1046 (void *)szOID_COMMON_NAME, str, len);
1047 ok(!strcmp(str, localhost), "unexpected value %s\n", str);
1048 HeapFree(GetProcessHeap(), 0, str);
1051 len = pCertGetNameStringA(context, CERT_NAME_SIMPLE_DISPLAY_TYPE, 0,
1052 NULL, NULL, 0);
1053 ok(len == strlen(localhost) + 1, "unexpected length %d\n", len);
1054 str = HeapAlloc(GetProcessHeap(), 0, len);
1055 if (str)
1057 len = pCertGetNameStringA(context, CERT_NAME_SIMPLE_DISPLAY_TYPE,
1058 0, NULL, str, len);
1059 ok(!strcmp(str, localhost), "unexpected value %s\n", str);
1060 HeapFree(GetProcessHeap(), 0, str);
1063 len = pCertGetNameStringA(context, CERT_NAME_FRIENDLY_DISPLAY_TYPE, 0,
1064 NULL, NULL, 0);
1065 ok(len == strlen(localhost) + 1, "unexpected length %d\n", len);
1066 str = HeapAlloc(GetProcessHeap(), 0, len);
1067 if (str)
1069 len = pCertGetNameStringA(context, CERT_NAME_FRIENDLY_DISPLAY_TYPE,
1070 0, NULL, str, len);
1071 ok(!strcmp(str, localhost), "unexpected value %s\n", str);
1072 HeapFree(GetProcessHeap(), 0, str);
1075 len = pCertGetNameStringA(context, CERT_NAME_DNS_TYPE, 0, NULL, NULL,
1077 ok(len == strlen(localhost) + 1 || broken(len == 1) /* NT4 */,
1078 "unexpected length %d\n", len);
1079 if (len > 1)
1081 str = HeapAlloc(GetProcessHeap(), 0, len);
1082 if (str)
1084 len = pCertGetNameStringA(context, CERT_NAME_DNS_TYPE, 0, NULL,
1085 str, len);
1086 ok(!strcmp(str, localhost), "unexpected value %s\n", str);
1087 HeapFree(GetProcessHeap(), 0, str);
1091 CertFreeCertificateContext(context);
1095 START_TEST(str)
1097 dll = GetModuleHandleA("Crypt32.dll");
1099 pCertNameToStrA = (CertNameToStrAFunc)GetProcAddress(dll,"CertNameToStrA");
1100 pCertNameToStrW = (CertNameToStrWFunc)GetProcAddress(dll,"CertNameToStrW");
1101 pCertRDNValueToStrA = (CertRDNValueToStrAFunc)GetProcAddress(dll,
1102 "CertRDNValueToStrA");
1103 pCertRDNValueToStrW = (CertRDNValueToStrWFunc)GetProcAddress(dll,
1104 "CertRDNValueToStrW");
1105 pCryptDecodeObject = (CryptDecodeObjectFunc)GetProcAddress(dll,
1106 "CryptDecodeObject");
1107 pCertStrToNameA = (CertStrToNameAFunc)GetProcAddress(dll,"CertStrToNameA");
1108 pCertStrToNameW = (CertStrToNameWFunc)GetProcAddress(dll,"CertStrToNameW");
1109 pCertGetNameStringA = (CertGetNameStringAFunc)GetProcAddress(dll,
1110 "CertGetNameStringA");
1112 test_CertRDNValueToStrA();
1113 test_CertRDNValueToStrW();
1114 test_CertNameToStrA();
1115 test_CertNameToStrW();
1116 test_CertStrToNameA();
1117 test_CertStrToNameW();
1118 test_CertGetNameStringA();