str.c 49.7 KB
Newer Older
1 2 3
/*
 * Unit test suite for crypt32.dll's Cert*ToStr and CertStrToName functions.
 *
4
 * Copyright 2006 Juan Lang, Aric Stewart for CodeWeavers
5 6 7 8 9 10 11 12 13 14 15 16 17
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 20 21 22 23 24 25 26 27
 */
#include <stdio.h>
#include <stdarg.h>
#include <windef.h>
#include <winbase.h>
#include <winerror.h>
#include <wincrypt.h>

#include "wine/test.h"
Juan Lang's avatar
Juan Lang committed
28

29 30 31 32 33
typedef struct _CertRDNAttrEncoding {
    LPCSTR pszObjId;
    DWORD  dwValueType;
    CERT_RDN_VALUE_BLOB Value;
    LPCSTR str;
34
    BOOL todo;
35 36
} CertRDNAttrEncoding, *PCertRDNAttrEncoding;

37 38 39 40 41
typedef struct _CertRDNAttrEncodingW {
    LPCSTR pszObjId;
    DWORD  dwValueType;
    CERT_RDN_VALUE_BLOB Value;
    LPCWSTR str;
42
    BOOL todo;
43 44
} CertRDNAttrEncodingW, *PCertRDNAttrEncodingW;

45 46
static BYTE bin1[] = { 0x55, 0x53 };
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 59 60 61 62 63
static BYTE bin8[] = {
0x65,0x00,0x50,0x00,0x4b,0x00,0x49,0x00,0x20,0x00,0x52,0x00,0x6f,0x00,0x6f,
0x00,0x74,0x00,0x20,0x00,0x43,0x00,0x65,0x00,0x72,0x00,0x74,0x00,0x69,0x00,
0x66,0x00,0x69,0x00,0x63,0x00,0x61,0x00,0x74,0x00,0x69,0x00,0x6f,0x00,0x6e,
0x00,0x20,0x00,0x41,0x00,0x75,0x00,0x74,0x00,0x68,0x00,0x6f,0x00,0x72,0x00,
0x69,0x00,0x74,0x00,0x79,0x00 };
64 65 66 67 68 69
static BYTE bin9[] = { 0x61, 0x62, 0x63, 0x22, 0x64, 0x65, 0x66 };
static BYTE bin10[] = { 0x61, 0x62, 0x63, 0x27, 0x64, 0x65, 0x66 };
static BYTE bin11[] = { 0x61, 0x62, 0x63, 0x2c, 0x20, 0x64, 0x65, 0x66 };
static BYTE bin12[] = { 0x20, 0x61, 0x62, 0x63, 0x20 };
static BYTE bin13[] = { 0x22, 0x64, 0x65, 0x66, 0x22 };
static BYTE bin14[] = { 0x31, 0x3b, 0x33 };
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129

static const BYTE cert[] = 
{0x30,0x82,0x2,0xbb,0x30,0x82,0x2,0x24,0x2,0x9,0x0,0xe3,0x5a,0x10,0xf1,0xfc,
 0x4b,0xf3,0xa2,0x30,0xd,0x6,0x9,0x2a,0x86,0x48,0x86,0xf7,0xd,0x1,0x1,0x4,0x5,
 0x0,0x30,0x81,0xa1,0x31,0xb,0x30,0x9,0x6,0x3,0x55,0x4,0x6,0x13,0x2,0x55,0x53,
 0x31,0x12,0x30,0x10,0x6,0x3,0x55,0x4,0x8,0x13,0x9,0x4d,0x69,0x6e,0x6e,0x65,
 0x73,0x6f,0x74,0x61,0x31,0x14,0x30,0x12,0x6,0x3,0x55,0x4,0x7,0x13,0xb,0x4d,
 0x69,0x6e,0x6e,0x65,0x61,0x70,0x6f,0x6c,0x69,0x73,0x31,0x14,0x30,0x12,0x6,0x3,
 0x55,0x4,0xa,0x13,0xb,0x43,0x6f,0x64,0x65,0x57,0x65,0x61,0x76,0x65,0x72,0x73,
 0x31,0x19,0x30,0x17,0x6,0x3,0x55,0x4,0xb,0x13,0x10,0x57,0x69,0x6e,0x65,0x20,
 0x44,0x65,0x76,0x65,0x6c,0x6f,0x70,0x6d,0x65,0x6e,0x74,0x31,0x12,0x30,0x10,
 0x6,0x3,0x55,0x4,0x3,0x13,0x9,0x6c,0x6f,0x63,0x61,0x6c,0x68,0x6f,0x73,0x74,
 0x31,0x23,0x30,0x21,0x6,0x9,0x2a,0x86,0x48,0x86,0xf7,0xd,0x1,0x9,0x1,0x16,
 0x14,0x61,0x72,0x69,0x63,0x40,0x63,0x6f,0x64,0x65,0x77,0x65,0x61,0x76,0x65,
 0x72,0x73,0x2e,0x63,0x6f,0x6d,0x30,0x1e,0x17,0xd,0x30,0x36,0x30,0x31,0x32,
 0x35,0x31,0x33,0x35,0x37,0x32,0x34,0x5a,0x17,0xd,0x30,0x36,0x30,0x32,0x32,
 0x34,0x31,0x33,0x35,0x37,0x32,0x34,0x5a,0x30,0x81,0xa1,0x31,0xb,0x30,0x9,0x6,
 0x3,0x55,0x4,0x6,0x13,0x2,0x55,0x53,0x31,0x12,0x30,0x10,0x6,0x3,0x55,0x4,0x8,
 0x13,0x9,0x4d,0x69,0x6e,0x6e,0x65,0x73,0x6f,0x74,0x61,0x31,0x14,0x30,0x12,0x6,
 0x3,0x55,0x4,0x7,0x13,0xb,0x4d,0x69,0x6e,0x6e,0x65,0x61,0x70,0x6f,0x6c,0x69,
 0x73,0x31,0x14,0x30,0x12,0x6,0x3,0x55,0x4,0xa,0x13,0xb,0x43,0x6f,0x64,0x65,
 0x57,0x65,0x61,0x76,0x65,0x72,0x73,0x31,0x19,0x30,0x17,0x6,0x3,0x55,0x4,0xb,
 0x13,0x10,0x57,0x69,0x6e,0x65,0x20,0x44,0x65,0x76,0x65,0x6c,0x6f,0x70,0x6d,
 0x65,0x6e,0x74,0x31,0x12,0x30,0x10,0x6,0x3,0x55,0x4,0x3,0x13,0x9,0x6c,0x6f,
 0x63,0x61,0x6c,0x68,0x6f,0x73,0x74,0x31,0x23,0x30,0x21,0x6,0x9,0x2a,0x86,0x48,
 0x86,0xf7,0xd,0x1,0x9,0x1,0x16,0x14,0x61,0x72,0x69,0x63,0x40,0x63,0x6f,0x64,
 0x65,0x77,0x65,0x61,0x76,0x65,0x72,0x73,0x2e,0x63,0x6f,0x6d,0x30,0x81,0x9f,
 0x30,0xd,0x6,0x9,0x2a,0x86,0x48,0x86,0xf7,0xd,0x1,0x1,0x1,0x5,0x0,0x3,0x81,
 0x8d,0x0,0x30,0x81,0x89,0x2,0x81,0x81,0x0,0x9b,0xb5,0x8f,0xaf,0xfb,0x9a,0xaf,
 0xdc,0xa2,0x4d,0xb1,0xc8,0x72,0x44,0xef,0x79,0x7f,0x28,0xb6,0xfe,0x50,0xdc,
 0x8a,0xf7,0x11,0x2f,0x90,0x70,0xed,0xa4,0xa9,0xd,0xbf,0x82,0x3e,0x56,0xd8,
 0x36,0xb6,0x9,0x52,0x83,0xab,0x65,0x95,0x0,0xe2,0xea,0x3c,0x4f,0x85,0xb8,0xc,
 0x41,0x42,0x77,0x5c,0x9d,0x44,0xeb,0xcf,0x7d,0x60,0x64,0x7a,0x6c,0x4c,0xac,
 0x4a,0x9a,0x23,0x25,0x15,0xd7,0x92,0xb4,0x10,0xe7,0x95,0xad,0x4b,0x93,0xda,
 0x6a,0x76,0xe0,0xa5,0xd2,0x13,0x8,0x12,0x30,0x68,0xde,0xb9,0x5b,0x6e,0x2a,
 0x97,0x43,0xaa,0x7b,0x22,0x33,0x34,0xb1,0xca,0x5d,0x19,0xd8,0x42,0x26,0x45,
 0xc6,0xe9,0x1d,0xee,0x7,0xc2,0x27,0x95,0x87,0xd8,0x12,0xec,0x4b,0x16,0x9f,0x2,
 0x3,0x1,0x0,0x1,0x30,0xd,0x6,0x9,0x2a,0x86,0x48,0x86,0xf7,0xd,0x1,0x1,0x4,0x5,
 0x0,0x3,0x81,0x81,0x0,0x96,0xf9,0xf6,0x6a,0x3d,0xd9,0xca,0x6e,0xd5,0x76,0x73,
 0xab,0x75,0xc1,0xcc,0x98,0x44,0xc3,0xa9,0x90,0x68,0x88,0x76,0xb9,0xeb,0xb6,
 0xbe,0x60,0x62,0xb9,0x67,0x1e,0xcc,0xf4,0xe1,0xe7,0x6c,0xc8,0x67,0x3f,0x1d,
 0xf3,0x68,0x86,0x30,0xee,0xaa,0x92,0x61,0x37,0xd7,0x82,0x90,0x28,0xaa,0x7a,
 0x18,0x88,0x60,0x14,0x88,0x75,0xc0,0x4a,0x4e,0x7d,0x48,0xe7,0x3,0xa6,0xfd,
 0xd7,0xce,0x3c,0xe5,0x9b,0xaf,0x2f,0xdc,0xbb,0x7c,0xbd,0x20,0x49,0xd9,0x68,
 0x37,0xeb,0x5d,0xbb,0xe2,0x6d,0x66,0xe3,0x11,0xc1,0xa7,0x88,0x49,0xc6,0x6f,
 0x65,0xd3,0xce,0xae,0x26,0x19,0x3,0x2e,0x4f,0x78,0xa5,0xa,0x97,0x7e,0x4f,0xc4,
 0x91,0x8a,0xf8,0x5,0xef,0x5b,0x3b,0x49,0xbf,0x5f,0x2b};

static char issuerStr[] =
 "US, Minnesota, Minneapolis, CodeWeavers, Wine Development, localhost, aric@codeweavers.com";
static char issuerStrSemicolon[] =
 "US; Minnesota; Minneapolis; CodeWeavers; Wine Development; localhost; aric@codeweavers.com";
static char issuerStrCRLF[] =
 "US\r\nMinnesota\r\nMinneapolis\r\nCodeWeavers\r\nWine Development\r\nlocalhost\r\naric@codeweavers.com";
static char subjectStr[] =
 "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";
static char subjectStrSemicolon[] =
 "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";
static char subjectStrCRLF[] =
 "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 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
static WCHAR issuerStrW[] = {
 'U','S',',',' ','M','i','n','n','e','s','o','t','a',',',' ','M','i','n','n',
 'e','a','p','o','l','i','s',',',' ','C','o','d','e','W','e','a','v','e','r',
 's',',',' ','W','i','n','e',' ','D','e','v','e','l','o','p','m','e','n','t',
 ',',' ','l','o','c','a','l','h','o','s','t',',',' ','a','r','i','c','@','c',
 'o','d','e','w','e','a','v','e','r','s','.','c','o','m',0 };
static WCHAR issuerStrSemicolonW[] = {
 'U','S',';',' ','M','i','n','n','e','s','o','t','a',';',' ','M','i','n','n',
 'e','a','p','o','l','i','s',';',' ','C','o','d','e','W','e','a','v','e','r',
 's',';',' ','W','i','n','e',' ','D','e','v','e','l','o','p','m','e','n','t',
 ';',' ','l','o','c','a','l','h','o','s','t',';',' ','a','r','i','c','@','c',
 'o','d','e','w','e','a','v','e','r','s','.','c','o','m',0 };
static WCHAR issuerStrCRLFW[] = {
 'U','S','\r','\n','M','i','n','n','e','s','o','t','a','\r','\n','M','i','n',
 'n','e','a','p','o','l','i','s','\r','\n','C','o','d','e','W','e','a','v','e',
 'r','s','\r','\n','W','i','n','e',' ','D','e','v','e','l','o','p','m','e','n',
 't','\r','\n','l','o','c','a','l','h','o','s','t','\r','\n','a','r','i','c',
 '@','c','o','d','e','w','e','a','v','e','r','s','.','c','o','m',0 };
static WCHAR subjectStrW[] = {
 '2','.','5','.','4','.','6','=','U','S',',',' ','2','.','5','.','4','.','8',
 '=','M','i','n','n','e','s','o','t','a',',',' ','2','.','5','.','4','.','7',
 '=','M','i','n','n','e','a','p','o','l','i','s',',',' ','2','.','5','.','4',
 '.','1','0','=','C','o','d','e','W','e','a','v','e','r','s',',',' ','2','.',
 '5','.','4','.','1','1','=','W','i','n','e',' ','D','e','v','e','l','o','p',
 'm','e','n','t',',',' ','2','.','5','.','4','.','3','=','l','o','c','a','l',
 'h','o','s','t',',',' ','1','.','2','.','8','4','0','.','1','1','3','5','4',
 '9','.','1','.','9','.','1','=','a','r','i','c','@','c','o','d','e','w','e',
 'a','v','e','r','s','.','c','o','m',0 };
static WCHAR subjectStrSemicolonW[] = {
 '2','.','5','.','4','.','6','=','U','S',';',' ','2','.','5','.','4','.','8',
 '=','M','i','n','n','e','s','o','t','a',';',' ','2','.','5','.','4','.','7',
 '=','M','i','n','n','e','a','p','o','l','i','s',';',' ','2','.','5','.','4',
 '.','1','0','=','C','o','d','e','W','e','a','v','e','r','s',';',' ','2','.',
 '5','.','4','.','1','1','=','W','i','n','e',' ','D','e','v','e','l','o','p',
 'm','e','n','t',';',' ','2','.','5','.','4','.','3','=','l','o','c','a','l',
 'h','o','s','t',';',' ','1','.','2','.','8','4','0','.','1','1','3','5','4',
 '9','.','1','.','9','.','1','=','a','r','i','c','@','c','o','d','e','w','e',
 'a','v','e','r','s','.','c','o','m',0 };
static WCHAR subjectStrCRLFW[] = {
 '2','.','5','.','4','.','6','=','U','S','\r','\n','2','.','5','.','4','.','8',
 '=','M','i','n','n','e','s','o','t','a','\r','\n','2','.','5','.','4','.','7',
 '=','M','i','n','n','e','a','p','o','l','i','s','\r','\n','2','.','5','.','4',
 '.','1','0','=','C','o','d','e','W','e','a','v','e','r','s','\r','\n','2','.',
 '5','.','4','.','1','1','=','W','i','n','e',' ','D','e','v','e','l','o','p',
 'm','e','n','t','\r','\n','2','.','5','.','4','.','3','=','l','o','c','a','l',
 'h','o','s','t','\r','\n','1','.','2','.','8','4','0','.','1','1','3','5','4',
 '9','.','1','.','9','.','1','=','a','r','i','c','@','c','o','d','e','w','e',
 'a','v','e','r','s','.','c','o','m',0 };
180 181 182 183 184 185 186
static WCHAR x500SubjectStrSemicolonReverseW[] = {
 'E','=','a','r','i','c','@','c','o','d','e','w','e','a','v','e','r','s','.','c',
 'o','m',';',' ','C','N','=','l','o','c','a','l','h','o','s','t',';',' ','O','U',
 '=','W','i','n','e',' ','D','e','v','e','l','o','p','m','e','n','t',';',' ','O',
 '=','C','o','d','e','W','e','a','v','e','r','s',';',' ','L','=','M','i','n','n',
 'e','a','p','o','l','i','s',';',' ','S','=','M','i','n','n','e','s','o','t','a',
 ';',' ','C','=','U','S',0 };
187

188 189 190 191
static HMODULE dll;
static DWORD (WINAPI *pCertNameToStrA)(DWORD,LPVOID,DWORD,LPSTR,DWORD);
static DWORD (WINAPI *pCertNameToStrW)(DWORD,LPVOID,DWORD,LPWSTR,DWORD);
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 197
 LPCSTR pszX500, DWORD dwStrType, void *pvReserved, BYTE *pbEncoded,
 DWORD *pcbEncoded, LPCSTR *ppszError);
198
static BOOL (WINAPI *pCertStrToNameW)(DWORD dwCertEncodingType,
199 200
 LPCWSTR pszX500, DWORD dwStrType, void *pvReserved, BYTE *pbEncoded,
 DWORD *pcbEncoded, LPCWSTR *ppszError);
201
static DWORD (WINAPI *pCertGetNameStringA)(PCCERT_CONTEXT cert, DWORD type,
202
 DWORD flags, void *typePara, LPSTR str, DWORD cch);
203 204


205
static void test_CertRDNValueToStrA(void)
206 207 208
{
    CertRDNAttrEncoding attrs[] = {
     { "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 222
       { sizeof(bin7), bin7 }, "aric@codeweavers.com", FALSE },
     { "0", CERT_RDN_PRINTABLE_STRING,
223
       { sizeof(bin9), bin9 }, "abc\"def", FALSE },
224 225 226
     { "0", CERT_RDN_PRINTABLE_STRING,
       { sizeof(bin10), bin10 }, "abc'def", FALSE },
     { "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 },
234 235 236 237
    };
    DWORD i, ret;
    char buffer[2000];
    CERT_RDN_VALUE_BLOB blob = { 0, NULL };
238
    static const char ePKI[] = "ePKI Root Certification Authority";
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256

    if (!pCertRDNValueToStrA) return;

    /* This crashes
    ret = pCertRDNValueToStrA(0, NULL, NULL, 0);
     */
    /* With empty input, it generates the empty string */
    SetLastError(0xdeadbeef);
    ret = pCertRDNValueToStrA(0, &blob, NULL, 0);
    ok(ret == 1 && GetLastError() == 0xdeadbeef, "Expected empty string\n");
    ret = pCertRDNValueToStrA(0, &blob, buffer, sizeof(buffer));
    ok(ret == 1 && GetLastError() == 0xdeadbeef, "Expected empty string\n");
    ok(!buffer[0], "Expected empty string\n");

    for (i = 0; i < sizeof(attrs) / sizeof(attrs[0]); i++)
    {
        ret = pCertRDNValueToStrA(attrs[i].dwValueType, &attrs[i].Value,
         buffer, sizeof(buffer));
257
        todo_wine_if (attrs[i].todo)
258 259 260 261 262 263
        {
            ok(ret == strlen(attrs[i].str) + 1, "Expected length %d, got %d\n",
             lstrlenA(attrs[i].str) + 1, ret);
            ok(!strcmp(buffer, attrs[i].str), "Expected %s, got %s\n",
             attrs[i].str, buffer);
        }
264
    }
265 266 267 268 269 270 271 272
    blob.pbData = bin8;
    blob.cbData = sizeof(bin8);
    ret = pCertRDNValueToStrA(CERT_RDN_UTF8_STRING, &blob, buffer,
     sizeof(buffer));
    ok(ret == strlen(ePKI) + 1 || broken(ret != strlen(ePKI) + 1),
     "Expected length %d, got %d\n", lstrlenA(ePKI), ret);
    if (ret == strlen(ePKI) + 1)
        ok(!strcmp(buffer, ePKI), "Expected %s, got %s\n", ePKI, buffer);
273 274
}

275 276 277 278 279 280 281 282 283 284 285 286 287
static void test_CertRDNValueToStrW(void)
{
    static const WCHAR usW[] = { 'U','S',0 };
    static const WCHAR minnesotaW[] = { 'M','i','n','n','e','s','o','t','a',0 };
    static const WCHAR minneapolisW[] = { 'M','i','n','n','e','a','p','o','l',
     'i','s',0 };
    static const WCHAR codeweaversW[] = { 'C','o','d','e','W','e','a','v','e',
     'r','s',0 };
    static const WCHAR wineDevW[] = { 'W','i','n','e',' ','D','e','v','e','l',
     'o','p','m','e','n','t',0 };
    static const WCHAR localhostW[] = { 'l','o','c','a','l','h','o','s','t',0 };
    static const WCHAR aricW[] = { 'a','r','i','c','@','c','o','d','e','w','e',
     'a','v','e','r','s','.','c','o','m',0 };
288 289 290
    static const WCHAR ePKIW[] = { 'e','P','K','I',' ','R','o','o','t',' ',
     'C','e','r','t','i','f','i','c','a','t','i','o','n',' ','A','u','t','h',
     'o','r','i','t','y',0 };
291 292 293 294 295 296 297 298
    static const WCHAR embeddedDoubleQuoteW[] = { 'a','b','c','"','d','e','f',
     0 };
    static const WCHAR embeddedSingleQuoteW[] = { 'a','b','c','\'','d','e','f',
     0 };
    static const WCHAR embeddedCommaW[] = { 'a','b','c',',',' ','d','e','f',0 };
    static const WCHAR trailingAndEndingSpaceW[] = { ' ','a','b','c',' ',0 };
    static const WCHAR enclosingQuotesW[] = { '"','d','e','f','"',0 };
    static const WCHAR embeddedSemiW[] = { '1',';','3',0 };
299 300
    CertRDNAttrEncodingW attrs[] = {
     { "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 314
       { sizeof(bin7), bin7 }, aricW, FALSE },
     { "0", CERT_RDN_PRINTABLE_STRING,
315
       { sizeof(bin9), bin9 }, embeddedDoubleQuoteW, FALSE },
316 317 318
     { "0", CERT_RDN_PRINTABLE_STRING,
       { sizeof(bin10), bin10 }, embeddedSingleQuoteW, FALSE },
     { "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 },
326 327 328 329 330
    };
    DWORD i, ret;
    WCHAR buffer[2000];
    CERT_RDN_VALUE_BLOB blob = { 0, NULL };

331 332
    if (!pCertRDNValueToStrW)
    {
333
        win_skip("CertRDNValueToStrW is not available\n");
334 335
        return;
    }
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352

    /* This crashes
    ret = pCertRDNValueToStrW(0, NULL, NULL, 0);
     */
    /* With empty input, it generates the empty string */
    SetLastError(0xdeadbeef);
    ret = pCertRDNValueToStrW(0, &blob, NULL, 0);
    ok(ret == 1 && GetLastError() == 0xdeadbeef, "Expected empty string\n");
    ret = pCertRDNValueToStrW(0, &blob, buffer,
     sizeof(buffer) / sizeof(buffer[0]));
    ok(ret == 1 && GetLastError() == 0xdeadbeef, "Expected empty string\n");
    ok(!buffer[0], "Expected empty string\n");

    for (i = 0; i < sizeof(attrs) / sizeof(attrs[0]); i++)
    {
        ret = pCertRDNValueToStrW(attrs[i].dwValueType, &attrs[i].Value,
         buffer, sizeof(buffer) / sizeof(buffer[0]));
353
        todo_wine_if (attrs[i].todo)
354 355 356 357 358 359
        {
            ok(ret == lstrlenW(attrs[i].str) + 1,
             "Expected length %d, got %d\n", lstrlenW(attrs[i].str) + 1, ret);
            ok(!lstrcmpW(buffer, attrs[i].str), "Expected %s, got %s\n",
             wine_dbgstr_w(attrs[i].str), wine_dbgstr_w(buffer));
        }
360
    }
361 362 363 364 365 366 367 368 369
    blob.pbData = bin8;
    blob.cbData = sizeof(bin8);
    ret = pCertRDNValueToStrW(CERT_RDN_UTF8_STRING, &blob, buffer,
     sizeof(buffer));
    ok(ret == lstrlenW(ePKIW) + 1 || broken(ret != lstrlenW(ePKIW) + 1),
     "Expected length %d, got %d\n", lstrlenW(ePKIW), ret);
    if (ret == lstrlenW(ePKIW) + 1)
        ok(!lstrcmpW(buffer, ePKIW), "Expected %s, got %s\n",
         wine_dbgstr_w(ePKIW), wine_dbgstr_w(buffer));
370 371 372
}

static void test_NameToStrConversionA(PCERT_NAME_BLOB pName, DWORD dwStrType,
373
 LPCSTR expected, BOOL todo)
374 375 376 377
{
    char buffer[2000] = { 0 };
    DWORD i;

378
    i = pCertNameToStrA(X509_ASN_ENCODING, pName, dwStrType, NULL, 0);
379
    todo_wine_if (todo)
380 381
        ok(i == strlen(expected) + 1, "Expected %d chars, got %d\n",
         lstrlenA(expected) + 1, i);
382 383
    i = pCertNameToStrA(X509_ASN_ENCODING,pName, dwStrType, buffer,
     sizeof(buffer));
384
    todo_wine_if (todo)
385 386
        ok(i == strlen(expected) + 1, "Expected %d chars, got %d\n",
         lstrlenA(expected) + 1, i);
387
    todo_wine_if (todo)
388 389
        ok(!strcmp(buffer, expected), "Expected %s, got %s\n", expected,
         buffer);
390 391
}

392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
static BYTE encodedSimpleCN[] = {
0x30,0x0c,0x31,0x0a,0x30,0x08,0x06,0x03,0x55,0x04,0x03,0x13,0x01,0x31 };
static BYTE encodedSingleQuotedCN[] = { 0x30,0x0e,0x31,0x0c,0x30,0x0a,
 0x06,0x03,0x55,0x04,0x03,0x13,0x03,0x27,0x31,0x27 };
static BYTE encodedSpacedCN[] = { 0x30,0x0e,0x31,0x0c,0x30,0x0a,0x06,0x03,
 0x55,0x04,0x03,0x13,0x03,0x20,0x31,0x20 };
static BYTE encodedQuotedCN[] = { 0x30,0x11,0x31,0x0f,0x30,0x0d,0x06,0x03,
 0x55, 0x04,0x03,0x1e,0x06,0x00,0x22,0x00,0x31,0x00,0x22, };
static BYTE encodedMultipleAttrCN[] = { 0x30,0x0e,0x31,0x0c,0x30,0x0a,
 0x06,0x03,0x55,0x04,0x03,0x13,0x03,0x31,0x2b,0x32 };
static BYTE encodedCommaCN[] = {
0x30,0x0e,0x31,0x0c,0x30,0x0a,0x06,0x03,0x55,0x04,0x03,0x13,0x03,0x61,0x2c,
0x62 };
static BYTE encodedEqualCN[] = {
0x30,0x0e,0x31,0x0c,0x30,0x0a,0x06,0x03,0x55,0x04,0x03,0x13,0x03,0x61,0x3d,
0x62 };
static BYTE encodedLessThanCN[] = {
0x30,0x0d,0x31,0x0b,0x30,0x09,0x06,0x03,0x55,0x04,0x03,0x1e,0x02,0x00,0x3c
};
static BYTE encodedGreaterThanCN[] = {
0x30,0x0d,0x31,0x0b,0x30,0x09,0x06,0x03,0x55,0x04,0x03,0x1e,0x02,0x00,0x3e
};
static BYTE encodedHashCN[] = {
0x30,0x0d,0x31,0x0b,0x30,0x09,0x06,0x03,0x55,0x04,0x03,0x1e,0x02,0x00,0x23
};
static BYTE encodedSemiCN[] = {
0x30,0x0d,0x31,0x0b,0x30,0x09,0x06,0x03,0x55,0x04,0x03,0x1e,0x02,0x00,0x3b
};
420 421 422
static BYTE encodedNewlineCN[] = {
0x30,0x11,0x31,0x0f,0x30,0x0d,0x06,0x03,0x55,0x04,0x03,0x1e,0x06,0x00,0x61,
0x00,0x0a,0x00,0x62 };
423 424 425 426 427 428 429 430 431
static BYTE encodedDummyCN[] = {
0x30,0x1F,0x31,0x0E,0x30,0x0C,0x06,0x03,0x55,0x04,0x03,0x13,0x05,0x64,0x75,
0x6D,0x6D,0x79,0x31,0x0D,0x30,0x0B,0x06,0x03,0x55,0x04,0x0C,0x13,0x04,0x74,
0x65,0x73,0x74 };
static BYTE encodedFields[] = {
0x30,0x2F,0x31,0x12,0x30,0x10,0x06,0x03,0x55,0x04,0x03,0x13,0x09,0x57,0x69,
0x6E,0x65,0x20,0x54,0x65,0x73,0x74,0x31,0x0C,0x30,0x0A,0x06,0x03,0x55,0x04,
0x0C,0x13,0x03,0x31,0x32,0x33,0x31,0x0B,0x30,0x09,0x06,0x03,0x55,0x04,0x06,
0x13,0x02,0x42,0x52 };
432

433
static void test_CertNameToStrA(void)
434 435
{
    PCCERT_CONTEXT context;
436
    CERT_NAME_BLOB blob;
437

438 439
    if (!pCertNameToStrA)
    {
440
        win_skip("CertNameToStrA is not available\n");
441 442
        return;
    }
443 444 445

    context = CertCreateCertificateContext(X509_ASN_ENCODING, cert,
     sizeof(cert));
446
    ok(context != NULL, "CertCreateCertificateContext failed: %08x\n",
447 448 449 450 451 452 453 454 455 456 457 458
     GetLastError());
    if (context)
    {
        DWORD ret;

        /* This crashes
        ret = pCertNameToStrA(0, NULL, 0, NULL, 0);
         */
        /* Test with a bogus encoding type */
        SetLastError(0xdeadbeef);
        ret = pCertNameToStrA(0, &context->pCertInfo->Issuer, 0, NULL, 0);
        ok(ret == 1 && GetLastError() == ERROR_FILE_NOT_FOUND,
459
         "Expected retval 1 and ERROR_FILE_NOT_FOUND, got %d - %08x\n",
460 461 462 463 464
         ret, GetLastError());
        SetLastError(0xdeadbeef);
        ret = pCertNameToStrA(X509_ASN_ENCODING, &context->pCertInfo->Issuer,
         0, NULL, 0);
        ok(ret && GetLastError() == ERROR_SUCCESS,
465
         "Expected positive return and ERROR_SUCCESS, got %d - %08x\n",
466 467
         ret, GetLastError());

468
        test_NameToStrConversionA(&context->pCertInfo->Issuer,
469
         CERT_SIMPLE_NAME_STR, issuerStr, FALSE);
470
        test_NameToStrConversionA(&context->pCertInfo->Issuer,
471
         CERT_SIMPLE_NAME_STR | CERT_NAME_STR_SEMICOLON_FLAG,
472
         issuerStrSemicolon, FALSE);
473
        test_NameToStrConversionA(&context->pCertInfo->Issuer,
474
         CERT_SIMPLE_NAME_STR | CERT_NAME_STR_CRLF_FLAG,
475
         issuerStrCRLF, FALSE);
476
        test_NameToStrConversionA(&context->pCertInfo->Subject,
477
         CERT_OID_NAME_STR, subjectStr, FALSE);
478
        test_NameToStrConversionA(&context->pCertInfo->Subject,
479
         CERT_OID_NAME_STR | CERT_NAME_STR_SEMICOLON_FLAG,
480
         subjectStrSemicolon, FALSE);
481
        test_NameToStrConversionA(&context->pCertInfo->Subject,
482
         CERT_OID_NAME_STR | CERT_NAME_STR_CRLF_FLAG,
483
         subjectStrCRLF, FALSE);
484
        test_NameToStrConversionA(&context->pCertInfo->Subject,
485
         CERT_X500_NAME_STR, x500SubjectStr, FALSE);
486
        test_NameToStrConversionA(&context->pCertInfo->Subject,
487 488
         CERT_X500_NAME_STR | CERT_NAME_STR_SEMICOLON_FLAG | CERT_NAME_STR_REVERSE_FLAG,
         x500SubjectStrSemicolonReverse, FALSE);
489 490 491

        CertFreeCertificateContext(context);
    }
492 493 494 495 496 497
    blob.pbData = encodedSimpleCN;
    blob.cbData = sizeof(encodedSimpleCN);
    test_NameToStrConversionA(&blob, CERT_X500_NAME_STR, "CN=1", FALSE);
    blob.pbData = encodedSingleQuotedCN;
    blob.cbData = sizeof(encodedSingleQuotedCN);
    test_NameToStrConversionA(&blob, CERT_X500_NAME_STR, "CN='1'", FALSE);
498
    test_NameToStrConversionA(&blob, CERT_SIMPLE_NAME_STR, "'1'", FALSE);
499 500
    blob.pbData = encodedSpacedCN;
    blob.cbData = sizeof(encodedSpacedCN);
501
    test_NameToStrConversionA(&blob, CERT_X500_NAME_STR, "CN=\" 1 \"", FALSE);
502
    test_NameToStrConversionA(&blob, CERT_SIMPLE_NAME_STR, "\" 1 \"", FALSE);
503 504 505
    blob.pbData = encodedQuotedCN;
    blob.cbData = sizeof(encodedQuotedCN);
    test_NameToStrConversionA(&blob, CERT_X500_NAME_STR, "CN=\"\"\"1\"\"\"",
506
     FALSE);
507 508
    test_NameToStrConversionA(&blob, CERT_SIMPLE_NAME_STR, "\"\"\"1\"\"\"",
     FALSE);
509 510
    blob.pbData = encodedMultipleAttrCN;
    blob.cbData = sizeof(encodedMultipleAttrCN);
511
    test_NameToStrConversionA(&blob, CERT_X500_NAME_STR, "CN=\"1+2\"", FALSE);
512
    test_NameToStrConversionA(&blob, CERT_SIMPLE_NAME_STR, "\"1+2\"", FALSE);
513 514
    blob.pbData = encodedCommaCN;
    blob.cbData = sizeof(encodedCommaCN);
515
    test_NameToStrConversionA(&blob, CERT_X500_NAME_STR, "CN=\"a,b\"", FALSE);
516
    test_NameToStrConversionA(&blob, CERT_SIMPLE_NAME_STR, "\"a,b\"", FALSE);
517 518
    blob.pbData = encodedEqualCN;
    blob.cbData = sizeof(encodedEqualCN);
519
    test_NameToStrConversionA(&blob, CERT_X500_NAME_STR, "CN=\"a=b\"", FALSE);
520
    test_NameToStrConversionA(&blob, CERT_SIMPLE_NAME_STR, "\"a=b\"", FALSE);
521 522
    blob.pbData = encodedLessThanCN;
    blob.cbData = sizeof(encodedLessThanCN);
523
    test_NameToStrConversionA(&blob, CERT_X500_NAME_STR, "CN=\"<\"", FALSE);
524
    test_NameToStrConversionA(&blob, CERT_SIMPLE_NAME_STR, "\"<\"", FALSE);
525 526
    blob.pbData = encodedGreaterThanCN;
    blob.cbData = sizeof(encodedGreaterThanCN);
527
    test_NameToStrConversionA(&blob, CERT_X500_NAME_STR, "CN=\">\"", FALSE);
528
    test_NameToStrConversionA(&blob, CERT_SIMPLE_NAME_STR, "\">\"", FALSE);
529 530
    blob.pbData = encodedHashCN;
    blob.cbData = sizeof(encodedHashCN);
531
    test_NameToStrConversionA(&blob, CERT_X500_NAME_STR, "CN=\"#\"", FALSE);
532
    test_NameToStrConversionA(&blob, CERT_SIMPLE_NAME_STR, "\"#\"", FALSE);
533 534
    blob.pbData = encodedSemiCN;
    blob.cbData = sizeof(encodedSemiCN);
535
    test_NameToStrConversionA(&blob, CERT_X500_NAME_STR, "CN=\";\"", FALSE);
536
    test_NameToStrConversionA(&blob, CERT_SIMPLE_NAME_STR, "\";\"", FALSE);
537 538 539
    blob.pbData = encodedNewlineCN;
    blob.cbData = sizeof(encodedNewlineCN);
    test_NameToStrConversionA(&blob, CERT_X500_NAME_STR, "CN=\"a\nb\"", FALSE);
540
    test_NameToStrConversionA(&blob, CERT_SIMPLE_NAME_STR, "\"a\nb\"", FALSE);
541 542
}

543
static void test_NameToStrConversionW(PCERT_NAME_BLOB pName, DWORD dwStrType,
544
 LPCWSTR expected, BOOL todo)
545 546 547 548
{
    WCHAR buffer[2000] = { 0 };
    DWORD i;

549
    i = pCertNameToStrW(X509_ASN_ENCODING,pName, dwStrType, NULL, 0);
550
    todo_wine_if (todo)
551 552
        ok(i == lstrlenW(expected) + 1, "Expected %d chars, got %d\n",
         lstrlenW(expected) + 1, i);
553 554
    i = pCertNameToStrW(X509_ASN_ENCODING,pName, dwStrType, buffer,
     sizeof(buffer) / sizeof(buffer[0]));
555
    todo_wine_if (todo)
556 557
        ok(i == lstrlenW(expected) + 1, "Expected %d chars, got %d\n",
         lstrlenW(expected) + 1, i);
558
    todo_wine_if (todo)
559 560
        ok(!lstrcmpW(buffer, expected), "Expected %s, got %s\n",
         wine_dbgstr_w(expected), wine_dbgstr_w(buffer));
561 562
}

563 564
static const WCHAR simpleCN_W[] = { 'C','N','=','1',0 };
static const WCHAR singledQuotedCN_W[] = { 'C','N','=','\'','1','\'',0 };
565
static const WCHAR simpleSingleQuotedCN_W[] = { '\'','1','\'',0 };
566
static const WCHAR spacedCN_W[] = { 'C','N','=','"',' ','1',' ','"',0 };
567
static const WCHAR simpleSpacedCN_W[] = { '"',' ','1',' ','"',0 };
568
static const WCHAR quotedCN_W[] = { 'C','N','=','"','"','"','1','"','"','"',0 };
569
static const WCHAR simpleQuotedCN_W[] = { '"','"','"','1','"','"','"',0 };
570
static const WCHAR multipleAttrCN_W[] = { 'C','N','=','"','1','+','2','"',0 };
571
static const WCHAR simpleMultipleAttrCN_W[] = { '"','1','+','2','"',0 };
572
static const WCHAR commaCN_W[] = { 'C','N','=','"','a',',','b','"',0 };
573
static const WCHAR simpleCommaCN_W[] = { '"','a',',','b','"',0 };
574
static const WCHAR equalCN_W[] = { 'C','N','=','"','a','=','b','"',0 };
575
static const WCHAR simpleEqualCN_W[] = { '"','a','=','b','"',0 };
576
static const WCHAR lessThanCN_W[] = { 'C','N','=','"','<','"',0 };
577
static const WCHAR simpleLessThanCN_W[] = { '"','<','"',0 };
578
static const WCHAR greaterThanCN_W[] = { 'C','N','=','"','>','"',0 };
579
static const WCHAR simpleGreaterThanCN_W[] = { '"','>','"',0 };
580
static const WCHAR hashCN_W[] = { 'C','N','=','"','#','"',0 };
581
static const WCHAR simpleHashCN_W[] = { '"','#','"',0 };
582
static const WCHAR semiCN_W[] = { 'C','N','=','"',';','"',0 };
583
static const WCHAR simpleSemiCN_W[] = { '"',';','"',0 };
584
static const WCHAR newlineCN_W[] = { 'C','N','=','"','a','\n','b','"',0 };
585
static const WCHAR simpleNewlineCN_W[] = { '"','a','\n','b','"',0 };
586

587 588 589
static void test_CertNameToStrW(void)
{
    PCCERT_CONTEXT context;
590
    CERT_NAME_BLOB blob;
591

592 593
    if (!pCertNameToStrW)
    {
594
        win_skip("CertNameToStrW is not available\n");
595 596
        return;
    }
597 598 599

    context = CertCreateCertificateContext(X509_ASN_ENCODING, cert,
     sizeof(cert));
600
    ok(context != NULL, "CertCreateCertificateContext failed: %08x\n",
601 602 603 604 605 606 607 608 609 610 611 612
     GetLastError());
    if (context)
    {
        DWORD ret;

        /* This crashes
        ret = pCertNameToStrW(0, NULL, 0, NULL, 0);
         */
        /* Test with a bogus encoding type */
        SetLastError(0xdeadbeef);
        ret = pCertNameToStrW(0, &context->pCertInfo->Issuer, 0, NULL, 0);
        ok(ret == 1 && GetLastError() == ERROR_FILE_NOT_FOUND,
613
         "Expected retval 1 and ERROR_FILE_NOT_FOUND, got %d - %08x\n",
614 615 616 617 618
         ret, GetLastError());
        SetLastError(0xdeadbeef);
        ret = pCertNameToStrW(X509_ASN_ENCODING, &context->pCertInfo->Issuer,
         0, NULL, 0);
        ok(ret && GetLastError() == ERROR_SUCCESS,
619
         "Expected positive return and ERROR_SUCCESS, got %d - %08x\n",
620 621 622
         ret, GetLastError());

        test_NameToStrConversionW(&context->pCertInfo->Issuer,
623
         CERT_SIMPLE_NAME_STR, issuerStrW, FALSE);
624 625
        test_NameToStrConversionW(&context->pCertInfo->Issuer,
         CERT_SIMPLE_NAME_STR | CERT_NAME_STR_SEMICOLON_FLAG,
626
         issuerStrSemicolonW, FALSE);
627 628
        test_NameToStrConversionW(&context->pCertInfo->Issuer,
         CERT_SIMPLE_NAME_STR | CERT_NAME_STR_CRLF_FLAG,
629
         issuerStrCRLFW, FALSE);
630
        test_NameToStrConversionW(&context->pCertInfo->Subject,
631
         CERT_OID_NAME_STR, subjectStrW, FALSE);
632 633
        test_NameToStrConversionW(&context->pCertInfo->Subject,
         CERT_OID_NAME_STR | CERT_NAME_STR_SEMICOLON_FLAG,
634
         subjectStrSemicolonW, FALSE);
635 636
        test_NameToStrConversionW(&context->pCertInfo->Subject,
         CERT_OID_NAME_STR | CERT_NAME_STR_CRLF_FLAG,
637
         subjectStrCRLFW, FALSE);
638
        test_NameToStrConversionW(&context->pCertInfo->Subject,
639 640
         CERT_X500_NAME_STR | CERT_NAME_STR_SEMICOLON_FLAG | CERT_NAME_STR_REVERSE_FLAG,
         x500SubjectStrSemicolonReverseW, FALSE);
641 642 643

        CertFreeCertificateContext(context);
    }
644 645 646 647 648 649 650
    blob.pbData = encodedSimpleCN;
    blob.cbData = sizeof(encodedSimpleCN);
    test_NameToStrConversionW(&blob, CERT_X500_NAME_STR, simpleCN_W, FALSE);
    blob.pbData = encodedSingleQuotedCN;
    blob.cbData = sizeof(encodedSingleQuotedCN);
    test_NameToStrConversionW(&blob, CERT_X500_NAME_STR, singledQuotedCN_W,
     FALSE);
651 652
    test_NameToStrConversionW(&blob, CERT_SIMPLE_NAME_STR,
     simpleSingleQuotedCN_W, FALSE);
653 654 655
    blob.pbData = encodedSpacedCN;
    blob.cbData = sizeof(encodedSpacedCN);
    test_NameToStrConversionW(&blob, CERT_X500_NAME_STR, spacedCN_W, FALSE);
656 657
    test_NameToStrConversionW(&blob, CERT_SIMPLE_NAME_STR, simpleSpacedCN_W,
     FALSE);
658 659 660
    blob.pbData = encodedQuotedCN;
    blob.cbData = sizeof(encodedQuotedCN);
    test_NameToStrConversionW(&blob, CERT_X500_NAME_STR, quotedCN_W,
661
     FALSE);
662 663
    test_NameToStrConversionW(&blob, CERT_SIMPLE_NAME_STR, simpleQuotedCN_W,
     FALSE);
664 665 666 667
    blob.pbData = encodedMultipleAttrCN;
    blob.cbData = sizeof(encodedMultipleAttrCN);
    test_NameToStrConversionW(&blob, CERT_X500_NAME_STR, multipleAttrCN_W,
     FALSE);
668 669
    test_NameToStrConversionW(&blob, CERT_SIMPLE_NAME_STR,
     simpleMultipleAttrCN_W, FALSE);
670 671 672
    blob.pbData = encodedCommaCN;
    blob.cbData = sizeof(encodedCommaCN);
    test_NameToStrConversionW(&blob, CERT_X500_NAME_STR, commaCN_W, FALSE);
673 674
    test_NameToStrConversionW(&blob, CERT_SIMPLE_NAME_STR, simpleCommaCN_W,
     FALSE);
675 676 677
    blob.pbData = encodedEqualCN;
    blob.cbData = sizeof(encodedEqualCN);
    test_NameToStrConversionW(&blob, CERT_X500_NAME_STR, equalCN_W, FALSE);
678 679
    test_NameToStrConversionW(&blob, CERT_SIMPLE_NAME_STR, simpleEqualCN_W,
     FALSE);
680 681
    blob.pbData = encodedLessThanCN;
    blob.cbData = sizeof(encodedLessThanCN);
682
    test_NameToStrConversionW(&blob, CERT_X500_NAME_STR, lessThanCN_W, FALSE);
683 684
    test_NameToStrConversionW(&blob, CERT_SIMPLE_NAME_STR, simpleLessThanCN_W,
     FALSE);
685 686 687
    blob.pbData = encodedGreaterThanCN;
    blob.cbData = sizeof(encodedGreaterThanCN);
    test_NameToStrConversionW(&blob, CERT_X500_NAME_STR, greaterThanCN_W,
688
     FALSE);
689 690
    test_NameToStrConversionW(&blob, CERT_SIMPLE_NAME_STR,
     simpleGreaterThanCN_W, FALSE);
691 692
    blob.pbData = encodedHashCN;
    blob.cbData = sizeof(encodedHashCN);
693
    test_NameToStrConversionW(&blob, CERT_X500_NAME_STR, hashCN_W, FALSE);
694 695
    test_NameToStrConversionW(&blob, CERT_SIMPLE_NAME_STR, simpleHashCN_W,
     FALSE);
696 697
    blob.pbData = encodedSemiCN;
    blob.cbData = sizeof(encodedSemiCN);
698
    test_NameToStrConversionW(&blob, CERT_X500_NAME_STR, semiCN_W, FALSE);
699 700
    test_NameToStrConversionW(&blob, CERT_SIMPLE_NAME_STR, simpleSemiCN_W,
     FALSE);
701 702 703
    blob.pbData = encodedNewlineCN;
    blob.cbData = sizeof(encodedNewlineCN);
    test_NameToStrConversionW(&blob, CERT_X500_NAME_STR, newlineCN_W, FALSE);
704 705
    test_NameToStrConversionW(&blob, CERT_SIMPLE_NAME_STR, simpleNewlineCN_W,
     FALSE);
706 707
}

708 709 710 711 712 713 714
struct StrToNameA
{
    LPCSTR x500;
    DWORD encodedSize;
    const BYTE *encoded;
};

715
static const struct StrToNameA namesA[] = {
716 717 718 719 720 721 722
 { "CN=1", sizeof(encodedSimpleCN), encodedSimpleCN },
 { "CN=\"1\"", sizeof(encodedSimpleCN), encodedSimpleCN },
 { "CN = \"1\"", sizeof(encodedSimpleCN), encodedSimpleCN },
 { "CN='1'", sizeof(encodedSingleQuotedCN), encodedSingleQuotedCN },
 { "CN=\" 1 \"", sizeof(encodedSpacedCN), encodedSpacedCN },
 { "CN=\"\"\"1\"\"\"", sizeof(encodedQuotedCN), encodedQuotedCN },
 { "CN=\"1+2\"", sizeof(encodedMultipleAttrCN), encodedMultipleAttrCN },
723 724 725 726 727 728
 { "CN=\"a,b\"", sizeof(encodedCommaCN), encodedCommaCN },
 { "CN=\"a=b\"", sizeof(encodedEqualCN), encodedEqualCN },
 { "CN=\"<\"", sizeof(encodedLessThanCN), encodedLessThanCN },
 { "CN=\">\"", sizeof(encodedGreaterThanCN), encodedGreaterThanCN },
 { "CN=\"#\"", sizeof(encodedHashCN), encodedHashCN },
 { "CN=\";\"", sizeof(encodedSemiCN), encodedSemiCN },
729 730
 { "CN=dummy,T=test", sizeof(encodedDummyCN), encodedDummyCN },
 { " CN =   Wine Test,T = 123, C = BR", sizeof(encodedFields), encodedFields },
731 732 733 734 735 736 737 738
};

static void test_CertStrToNameA(void)
{
    BOOL ret;
    DWORD size, i;
    BYTE buf[100];

739 740
    if (!pCertStrToNameA)
    {
741
        win_skip("CertStrToNameA is not available\n");
742 743
        return;
    }
744 745 746 747 748 749 750 751

    /* Crash
    ret = pCertStrToNameA(0, NULL, 0, NULL, NULL, NULL, NULL);
     */
    ret = pCertStrToNameA(0, NULL, 0, NULL, NULL, &size, NULL);
    ok(!ret, "Expected failure\n");
    ret = pCertStrToNameA(0, "bogus", 0, NULL, NULL, &size, NULL);
    ok(!ret && GetLastError() == CRYPT_E_INVALID_X500_STRING,
752
     "Expected CRYPT_E_INVALID_X500_STRING, got %08x\n", GetLastError());
753 754
    ret = pCertStrToNameA(0, "foo=1", 0, NULL, NULL, &size, NULL);
    ok(!ret && GetLastError() == CRYPT_E_INVALID_X500_STRING,
755
     "Expected CRYPT_E_INVALID_X500_STRING, got %08x\n", GetLastError());
756 757
    ret = pCertStrToNameA(0, "CN=1", 0, NULL, NULL, &size, NULL);
    ok(!ret && GetLastError() == ERROR_FILE_NOT_FOUND,
758
     "Expected ERROR_FILE_NOT_FOUND, got %08x\n", GetLastError());
759
    ret = pCertStrToNameA(X509_ASN_ENCODING, "CN=1", 0, NULL, NULL, &size, NULL);
760
    ok(ret, "CertStrToNameA failed: %08x\n", GetLastError());
761 762 763 764
    size = sizeof(buf);
    ret = pCertStrToNameA(X509_ASN_ENCODING, "CN=\"\"1\"\"", 0, NULL, buf, &size,
     NULL);
    ok(!ret && GetLastError() == CRYPT_E_INVALID_X500_STRING,
765
     "Expected CRYPT_E_INVALID_X500_STRING, got %08x\n", GetLastError());
766 767
    ret = pCertStrToNameA(X509_ASN_ENCODING, "CN=1+2", 0, NULL, buf,
     &size, NULL);
768
    ok(!ret && GetLastError() == CRYPT_E_INVALID_X500_STRING,
769
     "Expected CRYPT_E_INVALID_X500_STRING, got %08x\n", GetLastError());
770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795
    ret = pCertStrToNameA(X509_ASN_ENCODING, "CN=1+2", CERT_NAME_STR_NO_PLUS_FLAG, NULL, buf,
                          &size, NULL);
    ok(ret && GetLastError() == ERROR_SUCCESS,
                 "Expected ERROR_SUCCESS, got %08x\n", GetLastError());
    ret = pCertStrToNameA(X509_ASN_ENCODING, "CN=1,2", CERT_NAME_STR_NO_QUOTING_FLAG, NULL, buf,
                          &size, NULL);
    ok(!ret && GetLastError() == CRYPT_E_INVALID_X500_STRING,
                 "Expected CRYPT_E_INVALID_X500_STRING, got %08x\n", GetLastError());
    ret = pCertStrToNameA(X509_ASN_ENCODING, "CN=\"1,2;3,4\"", CERT_NAME_STR_NO_QUOTING_FLAG, NULL, buf,
                          &size, NULL);
    ok(!ret && GetLastError() == CRYPT_E_INVALID_X500_STRING,
                 "Expected CRYPT_E_INVALID_X500_STRING, got %08x\n", GetLastError());
    ret = pCertStrToNameA(X509_ASN_ENCODING, "CN=abc", 0, NULL, buf,
                          &size, NULL);
    ok(ret && GetLastError() == ERROR_SUCCESS,
                 "Expected ERROR_SUCCESS, got %08x\n", GetLastError());
    ret = pCertStrToNameA(X509_ASN_ENCODING, "CN=abc", CERT_NAME_STR_NO_QUOTING_FLAG, NULL, buf,
                          &size, NULL);
    ok(ret && GetLastError() == ERROR_SUCCESS,
                 "Expected ERROR_SUCCESS, got %08x\n", GetLastError());
    ret = pCertStrToNameA(X509_ASN_ENCODING, "CN=\"abc\"", 0, NULL, buf,
                          &size, NULL);
    ok(ret && GetLastError() == ERROR_SUCCESS,
                 "Expected ERROR_SUCCESS, got %08x\n", GetLastError());
    ret = pCertStrToNameA(X509_ASN_ENCODING, "CN=\"abc\"", CERT_NAME_STR_NO_QUOTING_FLAG, NULL, buf,
                          &size, NULL);
796
    ok(!ret && GetLastError() == ERROR_MORE_DATA,
797
                 "Expected ERROR_MORE_DATA, got %08x\n", GetLastError());
798 799 800 801 802
    for (i = 0; i < sizeof(namesA) / sizeof(namesA[0]); i++)
    {
        size = sizeof(buf);
        ret = pCertStrToNameA(X509_ASN_ENCODING, namesA[i].x500, 0, NULL, buf,
         &size, NULL);
803
        ok(ret, "CertStrToNameA failed on string %s: %08x\n", namesA[i].x500,
804 805
         GetLastError());
        ok(size == namesA[i].encodedSize,
806
         "Expected size %d, got %d\n", namesA[i].encodedSize, size);
807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823
        if (ret)
            ok(!memcmp(buf, namesA[i].encoded, namesA[i].encodedSize),
             "Unexpected value for string %s\n", namesA[i].x500);
    }
}

struct StrToNameW
{
    LPCWSTR x500;
    DWORD encodedSize;
    const BYTE *encoded;
};

static const WCHAR badlyQuotedCN_W[] = { 'C','N','=','"','"','1','"','"',0 };
static const WCHAR simpleCN2_W[] = { 'C','N','=','"','1','"',0 };
static const WCHAR simpleCN3_W[] = { 'C','N',' ','=',' ','"','1','"',0 };
static const WCHAR japaneseCN_W[] = { 'C','N','=',0x226f,0x575b,0 };
824 825 826 827
static const WCHAR dummyCN_W[] = { 'C','N','=','d','u','m','m','y',',','T','=','t','e','s','t',0 };
static const WCHAR encodedFields_W[] = { ' ','C','N',' ','=',' ',' ',' ','W','i','n','e',' ','T',
                                         'e','s','t',',','T',' ','=',' ','1','2','3',',',' ','C',
                                         ' ','=',' ','B','R',0 };
828 829 830
static const BYTE encodedJapaneseCN[] = { 0x30,0x0f,0x31,0x0d,0x30,0x0b,0x06,
 0x03,0x55,0x04,0x03,0x1e,0x04,0x22,0x6f,0x57,0x5b };

831
static const struct StrToNameW namesW[] = {
832 833 834 835 836 837 838 839
 { simpleCN_W, sizeof(encodedSimpleCN), encodedSimpleCN },
 { simpleCN2_W, sizeof(encodedSimpleCN), encodedSimpleCN },
 { simpleCN3_W, sizeof(encodedSimpleCN), encodedSimpleCN },
 { singledQuotedCN_W, sizeof(encodedSingleQuotedCN), encodedSingleQuotedCN },
 { spacedCN_W, sizeof(encodedSpacedCN), encodedSpacedCN },
 { quotedCN_W, sizeof(encodedQuotedCN), encodedQuotedCN },
 { multipleAttrCN_W, sizeof(encodedMultipleAttrCN), encodedMultipleAttrCN },
 { japaneseCN_W, sizeof(encodedJapaneseCN), encodedJapaneseCN },
840 841 842 843 844 845
 { commaCN_W, sizeof(encodedCommaCN), encodedCommaCN },
 { equalCN_W, sizeof(encodedEqualCN), encodedEqualCN },
 { lessThanCN_W, sizeof(encodedLessThanCN), encodedLessThanCN },
 { greaterThanCN_W, sizeof(encodedGreaterThanCN), encodedGreaterThanCN },
 { hashCN_W, sizeof(encodedHashCN), encodedHashCN },
 { semiCN_W, sizeof(encodedSemiCN), encodedSemiCN },
846 847
 { dummyCN_W, sizeof(encodedDummyCN), encodedDummyCN },
 { encodedFields_W, sizeof(encodedFields), encodedFields },
848 849 850 851 852 853 854 855 856 857 858
};

static void test_CertStrToNameW(void)
{
    static const WCHAR bogusW[] = { 'b','o','g','u','s',0 };
    static const WCHAR fooW[] = { 'f','o','o','=','1',0 };
    BOOL ret;
    DWORD size, i;
    LPCWSTR errorPtr;
    BYTE buf[100];

859 860
    if (!pCertStrToNameW)
    {
861
        win_skip("CertStrToNameW is not available\n");
862 863
        return;
    }
864 865 866 867 868 869 870 871

    /* Crash
    ret = pCertStrToNameW(0, NULL, 0, NULL, NULL, NULL, NULL);
     */
    ret = pCertStrToNameW(0, NULL, 0, NULL, NULL, &size, NULL);
    ok(!ret, "Expected failure\n");
    ret = pCertStrToNameW(0, bogusW, 0, NULL, NULL, &size, NULL);
    ok(!ret && GetLastError() == CRYPT_E_INVALID_X500_STRING,
872
     "Expected CRYPT_E_INVALID_X500_STRING, got %08x\n", GetLastError());
873 874
    ret = pCertStrToNameW(0, fooW, 0, NULL, NULL, &size, NULL);
    ok(!ret && GetLastError() == CRYPT_E_INVALID_X500_STRING,
875
     "Expected CRYPT_E_INVALID_X500_STRING, got %08x\n", GetLastError());
876 877
    ret = pCertStrToNameW(0, simpleCN_W, 0, NULL, NULL, &size, NULL);
    ok(!ret && GetLastError() == ERROR_FILE_NOT_FOUND,
878
     "Expected ERROR_FILE_NOT_FOUND, got %08x\n", GetLastError());
879 880
    ret = pCertStrToNameW(X509_ASN_ENCODING, simpleCN_W, 0, NULL, NULL, &size,
     NULL);
881
    ok(ret, "CertStrToNameW failed: %08x\n", GetLastError());
882 883 884 885
    size = sizeof(buf);
    ret = pCertStrToNameW(X509_ASN_ENCODING, badlyQuotedCN_W, 0, NULL, buf,
     &size, NULL);
    ok(!ret && GetLastError() == CRYPT_E_INVALID_X500_STRING,
886
     "Expected CRYPT_E_INVALID_X500_STRING, got %08x\n", GetLastError());
887 888 889
    ret = pCertStrToNameW(X509_ASN_ENCODING, badlyQuotedCN_W, 0, NULL, buf,
     &size, &errorPtr);
    ok(!ret && GetLastError() == CRYPT_E_INVALID_X500_STRING,
890
     "Expected CRYPT_E_INVALID_X500_STRING, got %08x\n", GetLastError());
891 892 893 894 895 896
    ok(errorPtr && *errorPtr == '1', "Expected first error character was 1\n");
    for (i = 0; i < sizeof(namesW) / sizeof(namesW[0]); i++)
    {
        size = sizeof(buf);
        ret = pCertStrToNameW(X509_ASN_ENCODING, namesW[i].x500, 0, NULL, buf,
         &size, NULL);
897
        ok(ret, "Index %d: CertStrToNameW failed: %08x\n", i, GetLastError());
898
        ok(size == namesW[i].encodedSize,
899
         "Index %d: expected size %d, got %d\n", i, namesW[i].encodedSize,
900 901 902
         size);
        if (ret)
            ok(!memcmp(buf, namesW[i].encoded, size),
903
             "Index %d: unexpected value for string %s\n", i, wine_dbgstr_w(namesW[i].x500));
904 905 906
    }
}

907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062
static void test_CertGetNameStringA(void)
{
    PCCERT_CONTEXT context;

    if (!pCertGetNameStringA)
    {
        win_skip("CertGetNameStringA is not available\n");
        return;
    }

    context = CertCreateCertificateContext(X509_ASN_ENCODING, cert,
     sizeof(cert));
    ok(context != NULL, "CertCreateCertificateContext failed: %08x\n",
     GetLastError());
    if (context)
    {
        static const char aric[] = "aric@codeweavers.com";
        static const char localhost[] = "localhost";
        DWORD len, type;
        LPSTR str;

        /* Bad string types/types missing from the cert */
        len = pCertGetNameStringA(NULL, 0, 0, NULL, NULL, 0);
        ok(len == 1, "expected 1, got %d\n", len);
        len = pCertGetNameStringA(context, 0, 0, NULL, NULL, 0);
        ok(len == 1, "expected 1, got %d\n", len);
        len = pCertGetNameStringA(context, CERT_NAME_URL_TYPE, 0, NULL, NULL,
         0);
        ok(len == 1, "expected 1, got %d\n", len);

        len = pCertGetNameStringA(context, CERT_NAME_EMAIL_TYPE, 0, NULL, NULL,
         0);
        ok(len == strlen(aric) + 1, "unexpected length %d\n", len);
        str = HeapAlloc(GetProcessHeap(), 0, len);
        if (str)
        {
            len = pCertGetNameStringA(context, CERT_NAME_EMAIL_TYPE, 0, NULL,
             str, len);
            ok(!strcmp(str, aric), "unexpected value %s\n", str);
            HeapFree(GetProcessHeap(), 0, str);
        }

        len = pCertGetNameStringA(context, CERT_NAME_RDN_TYPE, 0, NULL, NULL,
         0);
        ok(len == strlen(issuerStr) + 1, "unexpected length %d\n", len);
        str = HeapAlloc(GetProcessHeap(), 0, len);
        if (str)
        {
            len = pCertGetNameStringA(context, CERT_NAME_RDN_TYPE, 0, NULL,
             str, len);
            ok(!strcmp(str, issuerStr), "unexpected value %s\n", str);
            HeapFree(GetProcessHeap(), 0, str);
        }
        type = 0;
        len = pCertGetNameStringA(context, CERT_NAME_RDN_TYPE, 0, &type, NULL,
         0);
        ok(len == strlen(issuerStr) + 1, "unexpected length %d\n", len);
        str = HeapAlloc(GetProcessHeap(), 0, len);
        if (str)
        {
            len = pCertGetNameStringA(context, CERT_NAME_RDN_TYPE, 0, &type,
             str, len);
            ok(!strcmp(str, issuerStr), "unexpected value %s\n", str);
            HeapFree(GetProcessHeap(), 0, str);
        }
        type = CERT_OID_NAME_STR;
        len = pCertGetNameStringA(context, CERT_NAME_RDN_TYPE, 0, &type, NULL,
         0);
        ok(len == strlen(subjectStr) + 1, "unexpected length %d\n", len);
        str = HeapAlloc(GetProcessHeap(), 0, len);
        if (str)
        {
            len = pCertGetNameStringA(context, CERT_NAME_RDN_TYPE, 0, &type,
             str, len);
            ok(!strcmp(str, subjectStr), "unexpected value %s\n", str);
            HeapFree(GetProcessHeap(), 0, str);
        }

        len = pCertGetNameStringA(context, CERT_NAME_ATTR_TYPE, 0, NULL, NULL,
         0);
        ok(len == strlen(aric) + 1, "unexpected length %d\n", len);
        str = HeapAlloc(GetProcessHeap(), 0, len);
        if (str)
        {
            len = pCertGetNameStringA(context, CERT_NAME_ATTR_TYPE, 0, NULL,
             str, len);
            ok(!strcmp(str, aric), "unexpected value %s\n", str);
            HeapFree(GetProcessHeap(), 0, str);
        }
        len = pCertGetNameStringA(context, CERT_NAME_ATTR_TYPE, 0,
         (void *)szOID_RSA_emailAddr, NULL, 0);
        ok(len == strlen(aric) + 1, "unexpected length %d\n", len);
        str = HeapAlloc(GetProcessHeap(), 0, len);
        if (str)
        {
            len = pCertGetNameStringA(context, CERT_NAME_ATTR_TYPE, 0,
             (void *)szOID_RSA_emailAddr, str, len);
            ok(!strcmp(str, aric), "unexpected value %s\n", str);
            HeapFree(GetProcessHeap(), 0, str);
        }
        len = pCertGetNameStringA(context, CERT_NAME_ATTR_TYPE, 0,
         (void *)szOID_COMMON_NAME, NULL, 0);
        ok(len == strlen(localhost) + 1, "unexpected length %d\n", len);
        str = HeapAlloc(GetProcessHeap(), 0, len);
        if (str)
        {
            len = pCertGetNameStringA(context, CERT_NAME_ATTR_TYPE, 0,
             (void *)szOID_COMMON_NAME, str, len);
            ok(!strcmp(str, localhost), "unexpected value %s\n", str);
            HeapFree(GetProcessHeap(), 0, str);
        }

        len = pCertGetNameStringA(context, CERT_NAME_SIMPLE_DISPLAY_TYPE, 0,
         NULL, NULL, 0);
        ok(len == strlen(localhost) + 1, "unexpected length %d\n", len);
        str = HeapAlloc(GetProcessHeap(), 0, len);
        if (str)
        {
            len = pCertGetNameStringA(context, CERT_NAME_SIMPLE_DISPLAY_TYPE,
             0, NULL, str, len);
            ok(!strcmp(str, localhost), "unexpected value %s\n", str);
            HeapFree(GetProcessHeap(), 0, str);
        }

        len = pCertGetNameStringA(context, CERT_NAME_FRIENDLY_DISPLAY_TYPE, 0,
         NULL, NULL, 0);
        ok(len == strlen(localhost) + 1, "unexpected length %d\n", len);
        str = HeapAlloc(GetProcessHeap(), 0, len);
        if (str)
        {
            len = pCertGetNameStringA(context, CERT_NAME_FRIENDLY_DISPLAY_TYPE,
             0, NULL, str, len);
            ok(!strcmp(str, localhost), "unexpected value %s\n", str);
            HeapFree(GetProcessHeap(), 0, str);
        }

        len = pCertGetNameStringA(context, CERT_NAME_DNS_TYPE, 0, NULL, NULL,
         0);
        ok(len == strlen(localhost) + 1 || broken(len == 1) /* NT4 */,
         "unexpected length %d\n", len);
        if (len > 1)
        {
            str = HeapAlloc(GetProcessHeap(), 0, len);
            if (str)
            {
                len = pCertGetNameStringA(context, CERT_NAME_DNS_TYPE, 0, NULL,
                 str, len);
                ok(!strcmp(str, localhost), "unexpected value %s\n", str);
                HeapFree(GetProcessHeap(), 0, str);
            }
        }

        CertFreeCertificateContext(context);
    }
}

1063 1064
START_TEST(str)
{
1065
    dll = GetModuleHandleA("Crypt32.dll");
1066

1067 1068 1069 1070 1071 1072 1073
    pCertNameToStrA = (void*)GetProcAddress(dll,"CertNameToStrA");
    pCertNameToStrW = (void*)GetProcAddress(dll,"CertNameToStrW");
    pCertRDNValueToStrA = (void*)GetProcAddress(dll, "CertRDNValueToStrA");
    pCertRDNValueToStrW = (void*)GetProcAddress(dll, "CertRDNValueToStrW");
    pCertStrToNameA = (void*)GetProcAddress(dll,"CertStrToNameA");
    pCertStrToNameW = (void*)GetProcAddress(dll,"CertStrToNameW");
    pCertGetNameStringA = (void*)GetProcAddress(dll, "CertGetNameStringA");
1074

1075 1076 1077 1078
    test_CertRDNValueToStrA();
    test_CertRDNValueToStrW();
    test_CertNameToStrA();
    test_CertNameToStrW();
1079 1080
    test_CertStrToNameA();
    test_CertStrToNameW();
1081
    test_CertGetNameStringA();
1082
}