From ede2e24a6af7677217486cc2f213dfebac15314a Mon Sep 17 00:00:00 2001 From: Juan Lang Date: Thu, 2 Feb 2006 13:19:30 +0100 Subject: [PATCH] crypt32: Implement CertRDNValueToStrW and CertNameToStrW, with tests. --- dlls/crypt32/str.c | 139 +++++++++++++++++++++++++- dlls/crypt32/tests/str.c | 211 +++++++++++++++++++++++++++++++++++++-- 2 files changed, 335 insertions(+), 15 deletions(-) diff --git a/dlls/crypt32/str.c b/dlls/crypt32/str.c index d6aa977a40c..a35ce21d17c 100644 --- a/dlls/crypt32/str.c +++ b/dlls/crypt32/str.c @@ -67,8 +67,45 @@ DWORD WINAPI CertRDNValueToStrA(DWORD dwValueType, PCERT_RDN_VALUE_BLOB pValue, DWORD WINAPI CertRDNValueToStrW(DWORD dwValueType, PCERT_RDN_VALUE_BLOB pValue, LPWSTR psz, DWORD csz) { - FIXME("(%ld, %p, %p, %ld): stub\n", dwValueType, pValue, psz, csz); - return 0; + DWORD ret = 0; + + TRACE("(%ld, %p, %p, %ld)\n", dwValueType, pValue, psz, csz); + + switch (dwValueType) + { + case CERT_RDN_ANY_TYPE: + break; + case CERT_RDN_PRINTABLE_STRING: + case CERT_RDN_IA5_STRING: + if (!psz || !csz) + ret = pValue->cbData; + else + { + DWORD chars = min(pValue->cbData, csz - 1); + + if (chars) + { + DWORD i; + + for (i = 0; i < chars; i++) + psz[i] = pValue->pbData[i]; + ret += chars; + csz -= chars; + } + } + break; + default: + FIXME("string type %ld unimplemented\n", dwValueType); + } + if (psz && csz) + { + *(psz + ret) = '\0'; + csz--; + ret++; + } + else + ret++; + return ret; } @@ -171,6 +208,100 @@ DWORD WINAPI CertNameToStrA(DWORD dwCertEncodingType, PCERT_NAME_BLOB pName, DWORD WINAPI CertNameToStrW(DWORD dwCertEncodingType, PCERT_NAME_BLOB pName, DWORD dwStrType, LPWSTR psz, DWORD csz) { - FIXME("(%ld, %p, %p, %ld): stub\n", dwCertEncodingType, pName, psz, csz); - return 0; + static const DWORD unsupportedFlags = CERT_NAME_STR_NO_QUOTING_FLAG | + CERT_NAME_STR_REVERSE_FLAG | CERT_NAME_STR_ENABLE_T61_UNICODE_FLAG; + static const WCHAR commaSep[] = { ',',' ',0 }; + static const WCHAR semiSep[] = { ';',' ',0 }; + static const WCHAR crlfSep[] = { '\r','\n',0 }; + static const WCHAR plusSep[] = { ' ','+',' ',0 }; + static const WCHAR spaceSep[] = { ' ',0 }; + DWORD ret = 0, bytes = 0; + BOOL bRet; + CERT_NAME_INFO *info; + + TRACE("(%ld, %p, %p, %ld)\n", dwCertEncodingType, pName, psz, csz); + if (dwStrType & unsupportedFlags) + FIXME("unsupported flags: %08lx\n", dwStrType & unsupportedFlags); + + bRet = CryptDecodeObjectEx(dwCertEncodingType, X509_NAME, pName->pbData, + pName->cbData, CRYPT_DECODE_ALLOC_FLAG, NULL, &info, &bytes); + if (bRet) + { + DWORD i, j, sepLen, rdnSepLen; + LPCWSTR sep, rdnSep; + + if (dwStrType & CERT_NAME_STR_SEMICOLON_FLAG) + sep = semiSep; + else if (dwStrType & CERT_NAME_STR_CRLF_FLAG) + sep = crlfSep; + else + sep = commaSep; + sepLen = lstrlenW(sep); + if (dwStrType & CERT_NAME_STR_NO_PLUS_FLAG) + rdnSep = spaceSep; + else + rdnSep = plusSep; + rdnSepLen = lstrlenW(rdnSep); + for (i = 0; ret < csz && i < info->cRDN; i++) + { + for (j = 0; ret < csz && j < info->rgRDN[i].cRDNAttr; j++) + { + DWORD chars; + + if ((dwStrType & 0x000000ff) == CERT_OID_NAME_STR) + { + /* - 1 is needed to account for the NULL terminator. */ + chars = min( + lstrlenA(info->rgRDN[i].rgRDNAttr[j].pszObjId), + csz - ret - 1); + if (psz && chars) + { + DWORD k; + + for (k = 0; k < chars; k++) + *(psz + ret + k) = + info->rgRDN[i].rgRDNAttr[j].pszObjId[k]; + } + ret += chars; + csz -= chars; + if (csz > 1) + { + if (psz) + *(psz + ret) = '='; + ret++; + csz--; + } + } + /* FIXME: handle quoting */ + chars = CertRDNValueToStrW( + info->rgRDN[i].rgRDNAttr[j].dwValueType, + &info->rgRDN[i].rgRDNAttr[j].Value, psz ? psz + ret : NULL, + csz - ret - 1); + if (chars) + ret += chars - 1; + if (j < info->rgRDN[i].cRDNAttr - 1) + { + if (psz && ret < csz - rdnSepLen - 1) + memcpy(psz + ret, rdnSep, rdnSepLen * sizeof(WCHAR)); + ret += rdnSepLen; + } + } + if (i < info->cRDN - 1) + { + if (psz && ret < csz - sepLen - 1) + memcpy(psz + ret, sep, sepLen * sizeof(WCHAR)); + ret += sepLen; + } + } + LocalFree(info); + } + if (psz && csz) + { + *(psz + ret) = '\0'; + csz--; + ret++; + } + else + ret++; + return ret; } diff --git a/dlls/crypt32/tests/str.c b/dlls/crypt32/tests/str.c index 7d73123d488..bd6587d0d73 100644 --- a/dlls/crypt32/tests/str.c +++ b/dlls/crypt32/tests/str.c @@ -25,6 +25,7 @@ #include #include "wine/test.h" +#include "wine/debug.h" typedef struct _CertRDNAttrEncoding { LPCSTR pszObjId; @@ -33,6 +34,13 @@ typedef struct _CertRDNAttrEncoding { LPCSTR str; } CertRDNAttrEncoding, *PCertRDNAttrEncoding; +typedef struct _CertRDNAttrEncodingW { + LPCSTR pszObjId; + DWORD dwValueType; + CERT_RDN_VALUE_BLOB Value; + LPCWSTR str; +} CertRDNAttrEncodingW, *PCertRDNAttrEncodingW; + static const BYTE bin1[] = { 0x55, 0x53 }; static const BYTE bin2[] = { 0x4d, 0x69, 0x6e, 0x6e, 0x65, 0x73, 0x6f, 0x74, 0x61 }; @@ -106,19 +114,72 @@ 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"; +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 }; typedef BOOL (WINAPI *CryptDecodeObjectFunc)(DWORD, LPCSTR, const BYTE *, DWORD, DWORD, void *, DWORD *); typedef DWORD (WINAPI *CertNameToStrAFunc)(DWORD,LPVOID,DWORD,LPSTR,DWORD); +typedef DWORD (WINAPI *CertNameToStrWFunc)(DWORD,LPVOID,DWORD,LPWSTR,DWORD); typedef DWORD (WINAPI *CertRDNValueToStrAFunc)(DWORD, PCERT_RDN_VALUE_BLOB, LPSTR, DWORD); +typedef DWORD (WINAPI *CertRDNValueToStrWFunc)(DWORD, PCERT_RDN_VALUE_BLOB, + LPWSTR, DWORD); HMODULE dll; static CertNameToStrAFunc pCertNameToStrA; +static CertNameToStrWFunc pCertNameToStrW; static CryptDecodeObjectFunc pCryptDecodeObject; static CertRDNValueToStrAFunc pCertRDNValueToStrA; +static CertRDNValueToStrWFunc pCertRDNValueToStrW; -static void test_CertRDNValueToStr(void) +static void test_CertRDNValueToStrA(void) { CertRDNAttrEncoding attrs[] = { { "2.5.4.6", CERT_RDN_PRINTABLE_STRING, @@ -164,7 +225,65 @@ static void test_CertRDNValueToStr(void) } } -static void test_NameToStrConversion(PCERT_NAME_BLOB pName, DWORD dwStrType, +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 }; + CertRDNAttrEncodingW attrs[] = { + { "2.5.4.6", CERT_RDN_PRINTABLE_STRING, + { sizeof(bin1), (PBYTE)bin1 }, usW }, + { "2.5.4.8", CERT_RDN_PRINTABLE_STRING, + { sizeof(bin2), (PBYTE)bin2 }, minnesotaW }, + { "2.5.4.7", CERT_RDN_PRINTABLE_STRING, + { sizeof(bin3), (PBYTE)bin3 }, minneapolisW }, + { "2.5.4.10", CERT_RDN_PRINTABLE_STRING, + { sizeof(bin4), (PBYTE)bin4 }, codeweaversW }, + { "2.5.4.11", CERT_RDN_PRINTABLE_STRING, + { sizeof(bin5), (PBYTE)bin5 }, wineDevW }, + { "2.5.4.3", CERT_RDN_PRINTABLE_STRING, + { sizeof(bin6), (PBYTE)bin6 }, localhostW }, + { "1.2.840.113549.1.9.1", CERT_RDN_IA5_STRING, + { sizeof(bin7), (PBYTE)bin7 }, aricW }, + }; + DWORD i, ret; + WCHAR buffer[2000]; + CERT_RDN_VALUE_BLOB blob = { 0, NULL }; + + if (!pCertRDNValueToStrW) return; + + /* 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])); + ok(ret == lstrlenW(attrs[i].str) + 1, "Expected length %d, got %ld\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)); + } +} + +static void test_NameToStrConversionA(PCERT_NAME_BLOB pName, DWORD dwStrType, LPCSTR expected) { char buffer[2000] = { 0 }; @@ -177,7 +296,7 @@ static void test_NameToStrConversion(PCERT_NAME_BLOB pName, DWORD dwStrType, ok(!strcmp(buffer, expected), "Expected %s, got %s\n", expected, buffer); } -static void test_CertNameToStr(void) +static void test_CertNameToStrA(void) { PCCERT_CONTEXT context; @@ -207,20 +326,20 @@ static void test_CertNameToStr(void) "Expected positive return and ERROR_SUCCESS, got %ld - %08lx\n", ret, GetLastError()); - test_NameToStrConversion(&context->pCertInfo->Issuer, + test_NameToStrConversionA(&context->pCertInfo->Issuer, CERT_SIMPLE_NAME_STR, issuerStr); - test_NameToStrConversion(&context->pCertInfo->Issuer, + test_NameToStrConversionA(&context->pCertInfo->Issuer, CERT_SIMPLE_NAME_STR | CERT_NAME_STR_SEMICOLON_FLAG, issuerStrSemicolon); - test_NameToStrConversion(&context->pCertInfo->Issuer, + test_NameToStrConversionA(&context->pCertInfo->Issuer, CERT_SIMPLE_NAME_STR | CERT_NAME_STR_CRLF_FLAG, issuerStrCRLF); - test_NameToStrConversion(&context->pCertInfo->Subject, + test_NameToStrConversionA(&context->pCertInfo->Subject, CERT_OID_NAME_STR, subjectStr); - test_NameToStrConversion(&context->pCertInfo->Subject, + test_NameToStrConversionA(&context->pCertInfo->Subject, CERT_OID_NAME_STR | CERT_NAME_STR_SEMICOLON_FLAG, subjectStrSemicolon); - test_NameToStrConversion(&context->pCertInfo->Subject, + test_NameToStrConversionA(&context->pCertInfo->Subject, CERT_OID_NAME_STR | CERT_NAME_STR_CRLF_FLAG, subjectStrCRLF); @@ -228,18 +347,88 @@ static void test_CertNameToStr(void) } } +static void test_NameToStrConversionW(PCERT_NAME_BLOB pName, DWORD dwStrType, + LPCWSTR expected) +{ + WCHAR buffer[2000] = { 0 }; + DWORD i; + + i = pCertNameToStrW(X509_ASN_ENCODING,pName, dwStrType, buffer, + sizeof(buffer) / sizeof(buffer[0])); + ok(i == lstrlenW(expected) + 1, "Expected %d chars, got %ld\n", + lstrlenW(expected) + 1, i); + ok(!lstrcmpW(buffer, expected), "Expected %s, got %s\n", + wine_dbgstr_w(expected), wine_dbgstr_w(buffer)); +} + +static void test_CertNameToStrW(void) +{ + PCCERT_CONTEXT context; + + if (!pCertNameToStrW) return; + + context = CertCreateCertificateContext(X509_ASN_ENCODING, cert, + sizeof(cert)); + ok(context != NULL, "CertCreateCertificateContext failed: %08lx\n", + 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, + "Expected retval 1 and ERROR_FILE_NOT_FOUND, got %ld - %08lx\n", + ret, GetLastError()); + SetLastError(0xdeadbeef); + ret = pCertNameToStrW(X509_ASN_ENCODING, &context->pCertInfo->Issuer, + 0, NULL, 0); + ok(ret && GetLastError() == ERROR_SUCCESS, + "Expected positive return and ERROR_SUCCESS, got %ld - %08lx\n", + ret, GetLastError()); + + test_NameToStrConversionW(&context->pCertInfo->Issuer, + CERT_SIMPLE_NAME_STR, issuerStrW); + test_NameToStrConversionW(&context->pCertInfo->Issuer, + CERT_SIMPLE_NAME_STR | CERT_NAME_STR_SEMICOLON_FLAG, + issuerStrSemicolonW); + test_NameToStrConversionW(&context->pCertInfo->Issuer, + CERT_SIMPLE_NAME_STR | CERT_NAME_STR_CRLF_FLAG, + issuerStrCRLFW); + test_NameToStrConversionW(&context->pCertInfo->Subject, + CERT_OID_NAME_STR, subjectStrW); + test_NameToStrConversionW(&context->pCertInfo->Subject, + CERT_OID_NAME_STR | CERT_NAME_STR_SEMICOLON_FLAG, + subjectStrSemicolonW); + test_NameToStrConversionW(&context->pCertInfo->Subject, + CERT_OID_NAME_STR | CERT_NAME_STR_CRLF_FLAG, + subjectStrCRLFW); + + CertFreeCertificateContext(context); + } +} + START_TEST(str) { dll = LoadLibrary("Crypt32.dll"); pCertNameToStrA = (CertNameToStrAFunc)GetProcAddress(dll,"CertNameToStrA"); + pCertNameToStrW = (CertNameToStrWFunc)GetProcAddress(dll,"CertNameToStrW"); pCertRDNValueToStrA = (CertRDNValueToStrAFunc)GetProcAddress(dll, "CertRDNValueToStrA"); + pCertRDNValueToStrW = (CertRDNValueToStrWFunc)GetProcAddress(dll, + "CertRDNValueToStrW"); pCryptDecodeObject = (CryptDecodeObjectFunc)GetProcAddress(dll, "CryptDecodeObject"); - test_CertRDNValueToStr(); - test_CertNameToStr(); + test_CertRDNValueToStrA(); + test_CertRDNValueToStrW(); + test_CertNameToStrA(); + test_CertNameToStrW(); FreeLibrary(dll); }