2006-02-01 13:50:18 +01:00
|
|
|
/*
|
|
|
|
* Copyright 2006 Juan Lang for CodeWeavers
|
|
|
|
*
|
|
|
|
* 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
|
2006-05-18 14:49:52 +02:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
2006-02-01 13:50:18 +01:00
|
|
|
*/
|
|
|
|
#include <stdarg.h>
|
2009-10-17 20:27:58 +02:00
|
|
|
|
|
|
|
#define NONAMELESSUNION
|
|
|
|
|
2006-02-01 13:50:18 +01:00
|
|
|
#include "windef.h"
|
|
|
|
#include "winbase.h"
|
2006-02-16 12:08:19 +01:00
|
|
|
#include "winnls.h"
|
2006-07-26 19:57:32 +02:00
|
|
|
#include "winuser.h"
|
2006-02-01 13:50:18 +01:00
|
|
|
#include "wincrypt.h"
|
|
|
|
#include "wine/debug.h"
|
2006-07-26 19:57:32 +02:00
|
|
|
#include "wine/unicode.h"
|
2006-02-01 13:50:18 +01:00
|
|
|
|
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(crypt);
|
|
|
|
|
2010-10-19 02:23:05 +02:00
|
|
|
static inline BOOL is_quotable_char(char c)
|
|
|
|
{
|
|
|
|
switch(c)
|
|
|
|
{
|
|
|
|
case '+':
|
|
|
|
case ',':
|
|
|
|
case '"':
|
|
|
|
case '=':
|
|
|
|
case '<':
|
|
|
|
case '>':
|
|
|
|
case ';':
|
|
|
|
case '#':
|
2010-10-20 00:07:59 +02:00
|
|
|
case '\n':
|
2010-10-19 02:23:05 +02:00
|
|
|
return TRUE;
|
|
|
|
default:
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-02-01 13:50:18 +01:00
|
|
|
DWORD WINAPI CertRDNValueToStrA(DWORD dwValueType, PCERT_RDN_VALUE_BLOB pValue,
|
|
|
|
LPSTR psz, DWORD csz)
|
|
|
|
{
|
2010-10-19 02:23:05 +02:00
|
|
|
DWORD ret = 0, len, i;
|
|
|
|
BOOL needsQuotes = FALSE;
|
2006-02-01 13:50:18 +01:00
|
|
|
|
2006-10-04 06:58:09 +02:00
|
|
|
TRACE("(%d, %p, %p, %d)\n", dwValueType, pValue, psz, csz);
|
2006-02-01 13:50:18 +01:00
|
|
|
|
|
|
|
switch (dwValueType)
|
|
|
|
{
|
|
|
|
case CERT_RDN_ANY_TYPE:
|
|
|
|
break;
|
2006-08-02 02:15:14 +02:00
|
|
|
case CERT_RDN_NUMERIC_STRING:
|
2006-02-01 13:50:18 +01:00
|
|
|
case CERT_RDN_PRINTABLE_STRING:
|
2006-08-02 02:15:14 +02:00
|
|
|
case CERT_RDN_TELETEX_STRING:
|
|
|
|
case CERT_RDN_VIDEOTEX_STRING:
|
2006-02-01 13:50:18 +01:00
|
|
|
case CERT_RDN_IA5_STRING:
|
2006-08-02 02:15:14 +02:00
|
|
|
case CERT_RDN_GRAPHIC_STRING:
|
|
|
|
case CERT_RDN_VISIBLE_STRING:
|
|
|
|
case CERT_RDN_GENERAL_STRING:
|
2010-10-19 02:23:05 +02:00
|
|
|
len = pValue->cbData;
|
|
|
|
if (pValue->cbData && isspace(pValue->pbData[0]))
|
|
|
|
needsQuotes = TRUE;
|
|
|
|
if (pValue->cbData && isspace(pValue->pbData[pValue->cbData - 1]))
|
|
|
|
needsQuotes = TRUE;
|
|
|
|
for (i = 0; i < pValue->cbData; i++)
|
2010-10-20 04:23:50 +02:00
|
|
|
{
|
2010-10-19 02:23:05 +02:00
|
|
|
if (is_quotable_char(pValue->pbData[i]))
|
|
|
|
needsQuotes = TRUE;
|
|
|
|
if (pValue->pbData[i] == '"')
|
|
|
|
len += 1;
|
2010-10-20 04:23:50 +02:00
|
|
|
}
|
2010-10-19 02:23:05 +02:00
|
|
|
if (needsQuotes)
|
|
|
|
len += 2;
|
2006-02-01 13:50:18 +01:00
|
|
|
if (!psz || !csz)
|
2010-10-19 02:23:05 +02:00
|
|
|
ret = len;
|
2006-02-01 13:50:18 +01:00
|
|
|
else
|
|
|
|
{
|
2010-10-19 02:23:05 +02:00
|
|
|
char *ptr = psz;
|
2006-02-01 13:50:18 +01:00
|
|
|
|
2010-10-19 02:23:05 +02:00
|
|
|
if (needsQuotes)
|
|
|
|
*ptr++ = '"';
|
|
|
|
for (i = 0; i < pValue->cbData && ptr - psz < csz; ptr++, i++)
|
2006-02-01 13:50:18 +01:00
|
|
|
{
|
2010-10-19 02:23:05 +02:00
|
|
|
*ptr = pValue->pbData[i];
|
|
|
|
if (pValue->pbData[i] == '"' && ptr - psz < csz - 1)
|
|
|
|
*(++ptr) = '"';
|
2006-02-01 13:50:18 +01:00
|
|
|
}
|
2010-10-19 02:23:05 +02:00
|
|
|
if (needsQuotes && ptr - psz < csz)
|
|
|
|
*ptr++ = '"';
|
|
|
|
ret = ptr - psz;
|
2006-02-01 13:50:18 +01:00
|
|
|
}
|
|
|
|
break;
|
2010-10-19 02:21:43 +02:00
|
|
|
case CERT_RDN_BMP_STRING:
|
2010-10-20 02:32:01 +02:00
|
|
|
case CERT_RDN_UTF8_STRING:
|
2010-10-19 02:21:43 +02:00
|
|
|
len = WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)pValue->pbData,
|
|
|
|
pValue->cbData / sizeof(WCHAR), NULL, 0, NULL, NULL);
|
|
|
|
if (pValue->cbData && isspaceW(((LPCWSTR)pValue->pbData)[0]))
|
|
|
|
needsQuotes = TRUE;
|
|
|
|
if (pValue->cbData &&
|
|
|
|
isspaceW(((LPCWSTR)pValue->pbData)[pValue->cbData / sizeof(WCHAR)-1]))
|
|
|
|
needsQuotes = TRUE;
|
|
|
|
for (i = 0; i < pValue->cbData / sizeof(WCHAR); i++)
|
|
|
|
{
|
|
|
|
if (is_quotable_char(((LPCWSTR)pValue->pbData)[i]))
|
|
|
|
needsQuotes = TRUE;
|
|
|
|
if (((LPCWSTR)pValue->pbData)[i] == '"')
|
|
|
|
len += 1;
|
|
|
|
}
|
|
|
|
if (needsQuotes)
|
|
|
|
len += 2;
|
|
|
|
if (!psz || !csz)
|
|
|
|
ret = len;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
char *dst = psz;
|
|
|
|
|
|
|
|
if (needsQuotes)
|
|
|
|
*dst++ = '"';
|
|
|
|
for (i = 0; i < pValue->cbData / sizeof(WCHAR) &&
|
|
|
|
dst - psz < csz; dst++, i++)
|
|
|
|
{
|
|
|
|
LPCWSTR src = (LPCWSTR)pValue->pbData + i;
|
|
|
|
|
|
|
|
WideCharToMultiByte(CP_ACP, 0, src, 1, dst,
|
|
|
|
csz - (dst - psz) - 1, NULL, NULL);
|
|
|
|
if (*src == '"' && dst - psz < csz - 1)
|
|
|
|
*(++dst) = '"';
|
|
|
|
}
|
|
|
|
if (needsQuotes && dst - psz < csz)
|
|
|
|
*dst++ = '"';
|
|
|
|
ret = dst - psz;
|
|
|
|
}
|
|
|
|
break;
|
2006-02-01 13:50:18 +01:00
|
|
|
default:
|
2006-10-04 06:58:09 +02:00
|
|
|
FIXME("string type %d unimplemented\n", dwValueType);
|
2006-02-01 13:50:18 +01:00
|
|
|
}
|
|
|
|
if (psz && csz)
|
|
|
|
{
|
|
|
|
*(psz + ret) = '\0';
|
|
|
|
csz--;
|
|
|
|
ret++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ret++;
|
2006-10-04 06:58:09 +02:00
|
|
|
TRACE("returning %d (%s)\n", ret, debugstr_a(psz));
|
2006-02-01 13:50:18 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
DWORD WINAPI CertRDNValueToStrW(DWORD dwValueType, PCERT_RDN_VALUE_BLOB pValue,
|
|
|
|
LPWSTR psz, DWORD csz)
|
|
|
|
{
|
2010-10-20 04:23:50 +02:00
|
|
|
DWORD ret = 0, len, i, strLen;
|
2010-10-19 02:23:05 +02:00
|
|
|
BOOL needsQuotes = FALSE;
|
2006-02-02 13:19:30 +01:00
|
|
|
|
2006-10-04 06:58:09 +02:00
|
|
|
TRACE("(%d, %p, %p, %d)\n", dwValueType, pValue, psz, csz);
|
2006-02-02 13:19:30 +01:00
|
|
|
|
|
|
|
switch (dwValueType)
|
|
|
|
{
|
|
|
|
case CERT_RDN_ANY_TYPE:
|
|
|
|
break;
|
2006-08-02 02:15:14 +02:00
|
|
|
case CERT_RDN_NUMERIC_STRING:
|
2006-02-02 13:19:30 +01:00
|
|
|
case CERT_RDN_PRINTABLE_STRING:
|
2006-08-02 02:15:14 +02:00
|
|
|
case CERT_RDN_TELETEX_STRING:
|
|
|
|
case CERT_RDN_VIDEOTEX_STRING:
|
2006-02-02 13:19:30 +01:00
|
|
|
case CERT_RDN_IA5_STRING:
|
2006-08-02 02:15:14 +02:00
|
|
|
case CERT_RDN_GRAPHIC_STRING:
|
|
|
|
case CERT_RDN_VISIBLE_STRING:
|
|
|
|
case CERT_RDN_GENERAL_STRING:
|
2010-10-19 02:23:05 +02:00
|
|
|
len = pValue->cbData;
|
|
|
|
if (pValue->cbData && isspace(pValue->pbData[0]))
|
|
|
|
needsQuotes = TRUE;
|
|
|
|
if (pValue->cbData && isspace(pValue->pbData[pValue->cbData - 1]))
|
|
|
|
needsQuotes = TRUE;
|
|
|
|
for (i = 0; i < pValue->cbData; i++)
|
2010-10-20 04:23:50 +02:00
|
|
|
{
|
2010-10-19 02:23:05 +02:00
|
|
|
if (is_quotable_char(pValue->pbData[i]))
|
|
|
|
needsQuotes = TRUE;
|
|
|
|
if (pValue->pbData[i] == '"')
|
|
|
|
len += 1;
|
2010-10-20 04:23:50 +02:00
|
|
|
}
|
2010-10-19 02:23:05 +02:00
|
|
|
if (needsQuotes)
|
|
|
|
len += 2;
|
2006-02-02 13:19:30 +01:00
|
|
|
if (!psz || !csz)
|
2010-10-19 02:23:05 +02:00
|
|
|
ret = len;
|
2006-02-02 13:19:30 +01:00
|
|
|
else
|
|
|
|
{
|
2010-10-19 02:23:05 +02:00
|
|
|
WCHAR *ptr = psz;
|
2006-02-02 13:19:30 +01:00
|
|
|
|
2010-10-19 02:23:05 +02:00
|
|
|
if (needsQuotes)
|
|
|
|
*ptr++ = '"';
|
|
|
|
for (i = 0; i < pValue->cbData && ptr - psz < csz; ptr++, i++)
|
2006-02-02 13:19:30 +01:00
|
|
|
{
|
2010-10-19 02:23:05 +02:00
|
|
|
*ptr = pValue->pbData[i];
|
|
|
|
if (pValue->pbData[i] == '"' && ptr - psz < csz - 1)
|
2010-10-20 04:23:50 +02:00
|
|
|
*(++ptr) = '"';
|
|
|
|
}
|
|
|
|
if (needsQuotes && ptr - psz < csz)
|
|
|
|
*ptr++ = '"';
|
|
|
|
ret = ptr - psz;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case CERT_RDN_BMP_STRING:
|
2010-10-20 02:32:01 +02:00
|
|
|
case CERT_RDN_UTF8_STRING:
|
2010-10-20 04:23:50 +02:00
|
|
|
strLen = len = pValue->cbData / sizeof(WCHAR);
|
|
|
|
if (pValue->cbData && isspace(pValue->pbData[0]))
|
|
|
|
needsQuotes = TRUE;
|
|
|
|
if (pValue->cbData && isspace(pValue->pbData[strLen - 1]))
|
|
|
|
needsQuotes = TRUE;
|
|
|
|
for (i = 0; i < strLen; i++)
|
|
|
|
{
|
|
|
|
if (is_quotable_char(((LPCWSTR)pValue->pbData)[i]))
|
|
|
|
needsQuotes = TRUE;
|
|
|
|
if (((LPCWSTR)pValue->pbData)[i] == '"')
|
|
|
|
len += 1;
|
|
|
|
}
|
|
|
|
if (needsQuotes)
|
|
|
|
len += 2;
|
|
|
|
if (!psz || !csz)
|
|
|
|
ret = len;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
WCHAR *ptr = psz;
|
|
|
|
|
|
|
|
if (needsQuotes)
|
|
|
|
*ptr++ = '"';
|
|
|
|
for (i = 0; i < strLen && ptr - psz < csz; ptr++, i++)
|
|
|
|
{
|
|
|
|
*ptr = ((LPCWSTR)pValue->pbData)[i];
|
|
|
|
if (((LPCWSTR)pValue->pbData)[i] == '"' && ptr - psz < csz - 1)
|
2010-10-19 02:23:05 +02:00
|
|
|
*(++ptr) = '"';
|
2006-02-02 13:19:30 +01:00
|
|
|
}
|
2010-10-19 02:23:05 +02:00
|
|
|
if (needsQuotes && ptr - psz < csz)
|
|
|
|
*ptr++ = '"';
|
|
|
|
ret = ptr - psz;
|
2006-02-02 13:19:30 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2006-10-04 06:58:09 +02:00
|
|
|
FIXME("string type %d unimplemented\n", dwValueType);
|
2006-02-02 13:19:30 +01:00
|
|
|
}
|
|
|
|
if (psz && csz)
|
|
|
|
{
|
|
|
|
*(psz + ret) = '\0';
|
|
|
|
csz--;
|
|
|
|
ret++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ret++;
|
2006-10-04 06:58:09 +02:00
|
|
|
TRACE("returning %d (%s)\n", ret, debugstr_w(psz));
|
2006-02-02 13:19:30 +01:00
|
|
|
return ret;
|
2006-02-01 13:50:18 +01:00
|
|
|
}
|
|
|
|
|
2006-07-17 20:50:20 +02:00
|
|
|
/* Adds the prefix prefix to the string pointed to by psz, followed by the
|
|
|
|
* character '='. Copies no more than csz characters. Returns the number of
|
|
|
|
* characters copied. If psz is NULL, returns the number of characters that
|
|
|
|
* would be copied.
|
|
|
|
*/
|
|
|
|
static DWORD CRYPT_AddPrefixA(LPCSTR prefix, LPSTR psz, DWORD csz)
|
|
|
|
{
|
2006-07-19 21:30:43 +02:00
|
|
|
DWORD chars;
|
2006-07-17 20:50:20 +02:00
|
|
|
|
2006-10-04 06:58:09 +02:00
|
|
|
TRACE("(%s, %p, %d)\n", debugstr_a(prefix), psz, csz);
|
2006-07-17 20:50:20 +02:00
|
|
|
|
2006-07-19 21:30:43 +02:00
|
|
|
if (psz)
|
2006-07-17 20:50:20 +02:00
|
|
|
{
|
2008-09-23 23:38:38 +02:00
|
|
|
chars = min(strlen(prefix), csz);
|
2006-07-19 21:30:43 +02:00
|
|
|
memcpy(psz, prefix, chars);
|
|
|
|
*(psz + chars) = '=';
|
2006-07-17 20:50:20 +02:00
|
|
|
chars++;
|
|
|
|
}
|
2006-07-19 21:30:43 +02:00
|
|
|
else
|
|
|
|
chars = lstrlenA(prefix) + 1;
|
2006-07-17 20:50:20 +02:00
|
|
|
return chars;
|
|
|
|
}
|
|
|
|
|
2006-02-01 13:50:18 +01:00
|
|
|
DWORD WINAPI CertNameToStrA(DWORD dwCertEncodingType, PCERT_NAME_BLOB pName,
|
|
|
|
DWORD dwStrType, LPSTR psz, DWORD csz)
|
|
|
|
{
|
|
|
|
static const DWORD unsupportedFlags = CERT_NAME_STR_NO_QUOTING_FLAG |
|
2008-08-26 15:21:15 +02:00
|
|
|
CERT_NAME_STR_ENABLE_T61_UNICODE_FLAG;
|
2006-02-01 13:50:18 +01:00
|
|
|
static const char commaSep[] = ", ";
|
|
|
|
static const char semiSep[] = "; ";
|
|
|
|
static const char crlfSep[] = "\r\n";
|
|
|
|
static const char plusSep[] = " + ";
|
|
|
|
static const char spaceSep[] = " ";
|
|
|
|
DWORD ret = 0, bytes = 0;
|
|
|
|
BOOL bRet;
|
|
|
|
CERT_NAME_INFO *info;
|
|
|
|
|
2006-10-04 06:58:09 +02:00
|
|
|
TRACE("(%d, %p, %08x, %p, %d)\n", dwCertEncodingType, pName, dwStrType,
|
2006-02-15 11:51:16 +01:00
|
|
|
psz, csz);
|
2006-02-01 13:50:18 +01:00
|
|
|
if (dwStrType & unsupportedFlags)
|
2006-10-04 06:58:09 +02:00
|
|
|
FIXME("unsupported flags: %08x\n", dwStrType & unsupportedFlags);
|
2006-02-01 13:50:18 +01:00
|
|
|
|
|
|
|
bRet = CryptDecodeObjectEx(dwCertEncodingType, X509_NAME, pName->pbData,
|
|
|
|
pName->cbData, CRYPT_DECODE_ALLOC_FLAG, NULL, &info, &bytes);
|
|
|
|
if (bRet)
|
|
|
|
{
|
|
|
|
DWORD i, j, sepLen, rdnSepLen;
|
|
|
|
LPCSTR sep, rdnSep;
|
2008-08-26 15:21:15 +02:00
|
|
|
BOOL reverse = dwStrType & CERT_NAME_STR_REVERSE_FLAG;
|
|
|
|
const CERT_RDN *rdn = info->rgRDN;
|
|
|
|
|
|
|
|
if(reverse && info->cRDN > 1) rdn += (info->cRDN - 1);
|
2006-02-01 13:50:18 +01:00
|
|
|
|
|
|
|
if (dwStrType & CERT_NAME_STR_SEMICOLON_FLAG)
|
|
|
|
sep = semiSep;
|
|
|
|
else if (dwStrType & CERT_NAME_STR_CRLF_FLAG)
|
|
|
|
sep = crlfSep;
|
|
|
|
else
|
|
|
|
sep = commaSep;
|
|
|
|
sepLen = strlen(sep);
|
|
|
|
if (dwStrType & CERT_NAME_STR_NO_PLUS_FLAG)
|
|
|
|
rdnSep = spaceSep;
|
|
|
|
else
|
|
|
|
rdnSep = plusSep;
|
|
|
|
rdnSepLen = strlen(rdnSep);
|
2006-07-19 21:30:43 +02:00
|
|
|
for (i = 0; (!psz || ret < csz) && i < info->cRDN; i++)
|
2006-02-01 13:50:18 +01:00
|
|
|
{
|
2008-08-26 15:21:15 +02:00
|
|
|
for (j = 0; (!psz || ret < csz) && j < rdn->cRDNAttr; j++)
|
2006-02-01 13:50:18 +01:00
|
|
|
{
|
|
|
|
DWORD chars;
|
2006-07-17 20:50:20 +02:00
|
|
|
char prefixBuf[10]; /* big enough for GivenName */
|
|
|
|
LPCSTR prefix = NULL;
|
2006-02-01 13:50:18 +01:00
|
|
|
|
|
|
|
if ((dwStrType & 0x000000ff) == CERT_OID_NAME_STR)
|
2008-08-26 15:21:15 +02:00
|
|
|
prefix = rdn->rgRDNAttr[j].pszObjId;
|
2006-07-17 20:50:20 +02:00
|
|
|
else if ((dwStrType & 0x000000ff) == CERT_X500_NAME_STR)
|
|
|
|
{
|
|
|
|
PCCRYPT_OID_INFO oidInfo = CryptFindOIDInfo(
|
|
|
|
CRYPT_OID_INFO_OID_KEY,
|
2008-08-26 15:21:15 +02:00
|
|
|
rdn->rgRDNAttr[j].pszObjId,
|
2006-07-17 20:50:20 +02:00
|
|
|
CRYPT_RDN_ATTR_OID_GROUP_ID);
|
|
|
|
|
|
|
|
if (oidInfo)
|
|
|
|
{
|
|
|
|
WideCharToMultiByte(CP_ACP, 0, oidInfo->pwszName, -1,
|
|
|
|
prefixBuf, sizeof(prefixBuf), NULL, NULL);
|
|
|
|
prefix = prefixBuf;
|
|
|
|
}
|
|
|
|
else
|
2008-08-26 15:21:15 +02:00
|
|
|
prefix = rdn->rgRDNAttr[j].pszObjId;
|
2006-07-17 20:50:20 +02:00
|
|
|
}
|
|
|
|
if (prefix)
|
2006-02-01 13:50:18 +01:00
|
|
|
{
|
|
|
|
/* - 1 is needed to account for the NULL terminator. */
|
2006-07-19 21:30:43 +02:00
|
|
|
chars = CRYPT_AddPrefixA(prefix,
|
|
|
|
psz ? psz + ret : NULL, psz ? csz - ret - 1 : 0);
|
2006-02-01 13:50:18 +01:00
|
|
|
ret += chars;
|
|
|
|
}
|
|
|
|
chars = CertRDNValueToStrA(
|
2008-08-26 15:21:15 +02:00
|
|
|
rdn->rgRDNAttr[j].dwValueType,
|
|
|
|
&rdn->rgRDNAttr[j].Value, psz ? psz + ret : NULL,
|
2006-07-19 21:30:43 +02:00
|
|
|
psz ? csz - ret : 0);
|
2006-02-01 13:50:18 +01:00
|
|
|
if (chars)
|
|
|
|
ret += chars - 1;
|
2008-08-26 15:21:15 +02:00
|
|
|
if (j < rdn->cRDNAttr - 1)
|
2006-02-01 13:50:18 +01:00
|
|
|
{
|
|
|
|
if (psz && ret < csz - rdnSepLen - 1)
|
|
|
|
memcpy(psz + ret, rdnSep, rdnSepLen);
|
|
|
|
ret += rdnSepLen;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (i < info->cRDN - 1)
|
|
|
|
{
|
|
|
|
if (psz && ret < csz - sepLen - 1)
|
|
|
|
memcpy(psz + ret, sep, sepLen);
|
|
|
|
ret += sepLen;
|
|
|
|
}
|
2008-08-26 15:21:15 +02:00
|
|
|
if(reverse) rdn--;
|
|
|
|
else rdn++;
|
2006-02-01 13:50:18 +01:00
|
|
|
}
|
|
|
|
LocalFree(info);
|
|
|
|
}
|
|
|
|
if (psz && csz)
|
|
|
|
{
|
|
|
|
*(psz + ret) = '\0';
|
|
|
|
ret++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ret++;
|
2006-07-19 21:30:43 +02:00
|
|
|
TRACE("Returning %s\n", debugstr_a(psz));
|
2006-02-01 13:50:18 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2006-07-17 20:50:20 +02:00
|
|
|
/* Adds the prefix prefix to the wide-character string pointed to by psz,
|
|
|
|
* followed by the character '='. Copies no more than csz characters. Returns
|
|
|
|
* the number of characters copied. If psz is NULL, returns the number of
|
|
|
|
* characters that would be copied.
|
|
|
|
* Assumes the characters in prefix are ASCII (not multibyte characters.)
|
|
|
|
*/
|
|
|
|
static DWORD CRYPT_AddPrefixAToW(LPCSTR prefix, LPWSTR psz, DWORD csz)
|
|
|
|
{
|
2006-07-19 21:30:43 +02:00
|
|
|
DWORD chars;
|
2006-07-17 20:50:20 +02:00
|
|
|
|
2006-10-04 06:58:09 +02:00
|
|
|
TRACE("(%s, %p, %d)\n", debugstr_a(prefix), psz, csz);
|
2006-07-17 20:50:20 +02:00
|
|
|
|
2006-07-19 21:30:43 +02:00
|
|
|
if (psz)
|
2006-07-17 20:50:20 +02:00
|
|
|
{
|
|
|
|
DWORD i;
|
|
|
|
|
2008-09-23 23:38:38 +02:00
|
|
|
chars = min(strlen(prefix), csz);
|
2006-07-17 20:50:20 +02:00
|
|
|
for (i = 0; i < chars; i++)
|
|
|
|
*(psz + i) = prefix[i];
|
2006-07-19 21:30:43 +02:00
|
|
|
*(psz + chars) = '=';
|
2006-07-17 20:50:20 +02:00
|
|
|
chars++;
|
|
|
|
}
|
2006-07-19 21:30:43 +02:00
|
|
|
else
|
|
|
|
chars = lstrlenA(prefix) + 1;
|
2006-07-17 20:50:20 +02:00
|
|
|
return chars;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Adds the prefix prefix to the string pointed to by psz, followed by the
|
|
|
|
* character '='. Copies no more than csz characters. Returns the number of
|
|
|
|
* characters copied. If psz is NULL, returns the number of characters that
|
|
|
|
* would be copied.
|
|
|
|
*/
|
|
|
|
static DWORD CRYPT_AddPrefixW(LPCWSTR prefix, LPWSTR psz, DWORD csz)
|
|
|
|
{
|
2006-07-19 21:30:43 +02:00
|
|
|
DWORD chars;
|
2006-07-17 20:50:20 +02:00
|
|
|
|
2006-10-04 06:58:09 +02:00
|
|
|
TRACE("(%s, %p, %d)\n", debugstr_w(prefix), psz, csz);
|
2006-07-17 20:50:20 +02:00
|
|
|
|
2006-07-19 21:30:43 +02:00
|
|
|
if (psz)
|
2006-07-17 20:50:20 +02:00
|
|
|
{
|
2008-09-23 23:38:38 +02:00
|
|
|
chars = min(strlenW(prefix), csz);
|
2006-07-19 21:30:43 +02:00
|
|
|
memcpy(psz, prefix, chars * sizeof(WCHAR));
|
|
|
|
*(psz + chars) = '=';
|
2006-07-17 20:50:20 +02:00
|
|
|
chars++;
|
|
|
|
}
|
2006-07-19 21:30:43 +02:00
|
|
|
else
|
|
|
|
chars = lstrlenW(prefix) + 1;
|
2006-07-17 20:50:20 +02:00
|
|
|
return chars;
|
|
|
|
}
|
|
|
|
|
2008-11-14 18:59:00 +01:00
|
|
|
static const WCHAR indent[] = { ' ',' ',' ',' ',' ',0 };
|
|
|
|
|
|
|
|
DWORD cert_name_to_str_with_indent(DWORD dwCertEncodingType, DWORD indentLevel,
|
2009-06-16 22:14:05 +02:00
|
|
|
const CERT_NAME_BLOB *pName, DWORD dwStrType, LPWSTR psz, DWORD csz)
|
2006-02-01 13:50:18 +01:00
|
|
|
{
|
2006-02-02 13:19:30 +01:00
|
|
|
static const DWORD unsupportedFlags = CERT_NAME_STR_NO_QUOTING_FLAG |
|
2008-08-26 15:21:15 +02:00
|
|
|
CERT_NAME_STR_ENABLE_T61_UNICODE_FLAG;
|
2006-02-02 13:19:30 +01:00
|
|
|
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;
|
|
|
|
|
|
|
|
if (dwStrType & unsupportedFlags)
|
2006-10-04 06:58:09 +02:00
|
|
|
FIXME("unsupported flags: %08x\n", dwStrType & unsupportedFlags);
|
2006-02-02 13:19:30 +01:00
|
|
|
|
|
|
|
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;
|
2008-08-26 15:21:15 +02:00
|
|
|
BOOL reverse = dwStrType & CERT_NAME_STR_REVERSE_FLAG;
|
|
|
|
const CERT_RDN *rdn = info->rgRDN;
|
|
|
|
|
|
|
|
if(reverse && info->cRDN > 1) rdn += (info->cRDN - 1);
|
2006-02-02 13:19:30 +01:00
|
|
|
|
|
|
|
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);
|
2006-07-19 21:30:43 +02:00
|
|
|
for (i = 0; (!psz || ret < csz) && i < info->cRDN; i++)
|
2006-02-02 13:19:30 +01:00
|
|
|
{
|
2008-08-26 15:21:15 +02:00
|
|
|
for (j = 0; (!psz || ret < csz) && j < rdn->cRDNAttr; j++)
|
2006-02-02 13:19:30 +01:00
|
|
|
{
|
|
|
|
DWORD chars;
|
2006-07-17 20:50:20 +02:00
|
|
|
LPCSTR prefixA = NULL;
|
|
|
|
LPCWSTR prefixW = NULL;
|
2006-02-02 13:19:30 +01:00
|
|
|
|
|
|
|
if ((dwStrType & 0x000000ff) == CERT_OID_NAME_STR)
|
2008-08-26 15:21:15 +02:00
|
|
|
prefixA = rdn->rgRDNAttr[j].pszObjId;
|
2006-07-17 20:50:20 +02:00
|
|
|
else if ((dwStrType & 0x000000ff) == CERT_X500_NAME_STR)
|
|
|
|
{
|
|
|
|
PCCRYPT_OID_INFO oidInfo = CryptFindOIDInfo(
|
|
|
|
CRYPT_OID_INFO_OID_KEY,
|
2008-08-26 15:21:15 +02:00
|
|
|
rdn->rgRDNAttr[j].pszObjId,
|
2006-07-17 20:50:20 +02:00
|
|
|
CRYPT_RDN_ATTR_OID_GROUP_ID);
|
|
|
|
|
|
|
|
if (oidInfo)
|
|
|
|
prefixW = oidInfo->pwszName;
|
|
|
|
else
|
2008-08-26 15:21:15 +02:00
|
|
|
prefixA = rdn->rgRDNAttr[j].pszObjId;
|
2006-07-17 20:50:20 +02:00
|
|
|
}
|
2008-11-14 18:59:00 +01:00
|
|
|
if (dwStrType & CERT_NAME_STR_CRLF_FLAG)
|
|
|
|
{
|
|
|
|
DWORD k;
|
|
|
|
|
|
|
|
for (k = 0; k < indentLevel; k++)
|
|
|
|
{
|
|
|
|
if (psz)
|
|
|
|
{
|
|
|
|
chars = min(strlenW(indent), csz - ret - 1);
|
|
|
|
memcpy(psz + ret, indent, chars * sizeof(WCHAR));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
chars = strlenW(indent);
|
|
|
|
ret += chars;
|
|
|
|
}
|
|
|
|
}
|
2006-07-17 20:50:20 +02:00
|
|
|
if (prefixW)
|
|
|
|
{
|
|
|
|
/* - 1 is needed to account for the NULL terminator. */
|
2006-07-19 21:30:43 +02:00
|
|
|
chars = CRYPT_AddPrefixW(prefixW,
|
|
|
|
psz ? psz + ret : NULL, psz ? csz - ret - 1 : 0);
|
2006-07-17 20:50:20 +02:00
|
|
|
ret += chars;
|
|
|
|
}
|
|
|
|
else if (prefixA)
|
2006-02-02 13:19:30 +01:00
|
|
|
{
|
|
|
|
/* - 1 is needed to account for the NULL terminator. */
|
2006-07-19 21:30:43 +02:00
|
|
|
chars = CRYPT_AddPrefixAToW(prefixA,
|
|
|
|
psz ? psz + ret : NULL, psz ? csz - ret - 1 : 0);
|
2006-02-02 13:19:30 +01:00
|
|
|
ret += chars;
|
|
|
|
}
|
|
|
|
chars = CertRDNValueToStrW(
|
2008-08-26 15:21:15 +02:00
|
|
|
rdn->rgRDNAttr[j].dwValueType,
|
|
|
|
&rdn->rgRDNAttr[j].Value, psz ? psz + ret : NULL,
|
2006-07-19 21:30:43 +02:00
|
|
|
psz ? csz - ret : 0);
|
2006-02-02 13:19:30 +01:00
|
|
|
if (chars)
|
|
|
|
ret += chars - 1;
|
2008-08-26 15:21:15 +02:00
|
|
|
if (j < rdn->cRDNAttr - 1)
|
2006-02-02 13:19:30 +01:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
2008-08-26 15:21:15 +02:00
|
|
|
if(reverse) rdn--;
|
|
|
|
else rdn++;
|
2006-02-02 13:19:30 +01:00
|
|
|
}
|
|
|
|
LocalFree(info);
|
|
|
|
}
|
|
|
|
if (psz && csz)
|
|
|
|
{
|
|
|
|
*(psz + ret) = '\0';
|
|
|
|
ret++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ret++;
|
2008-11-14 18:59:00 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
DWORD WINAPI CertNameToStrW(DWORD dwCertEncodingType, PCERT_NAME_BLOB pName,
|
|
|
|
DWORD dwStrType, LPWSTR psz, DWORD csz)
|
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
|
|
|
|
TRACE("(%d, %p, %08x, %p, %d)\n", dwCertEncodingType, pName, dwStrType,
|
|
|
|
psz, csz);
|
|
|
|
|
|
|
|
ret = cert_name_to_str_with_indent(dwCertEncodingType, 0, pName, dwStrType,
|
|
|
|
psz, csz);
|
2006-07-19 21:30:43 +02:00
|
|
|
TRACE("Returning %s\n", debugstr_w(psz));
|
2006-02-02 13:19:30 +01:00
|
|
|
return ret;
|
2006-02-01 13:50:18 +01:00
|
|
|
}
|
2006-02-16 12:08:19 +01:00
|
|
|
|
2006-07-26 19:57:32 +02:00
|
|
|
BOOL WINAPI CertStrToNameA(DWORD dwCertEncodingType, LPCSTR pszX500,
|
|
|
|
DWORD dwStrType, void *pvReserved, BYTE *pbEncoded, DWORD *pcbEncoded,
|
|
|
|
LPCSTR *ppszError)
|
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
int len;
|
|
|
|
|
2006-10-04 06:58:09 +02:00
|
|
|
TRACE("(%08x, %s, %08x, %p, %p, %p, %p)\n", dwCertEncodingType,
|
2006-07-26 19:57:32 +02:00
|
|
|
debugstr_a(pszX500), dwStrType, pvReserved, pbEncoded, pcbEncoded,
|
|
|
|
ppszError);
|
|
|
|
|
|
|
|
len = MultiByteToWideChar(CP_ACP, 0, pszX500, -1, NULL, 0);
|
2007-10-20 23:22:04 +02:00
|
|
|
if (len)
|
2006-07-26 19:57:32 +02:00
|
|
|
{
|
2007-10-20 23:22:04 +02:00
|
|
|
LPWSTR x500, errorStr;
|
|
|
|
|
|
|
|
if ((x500 = CryptMemAlloc(len * sizeof(WCHAR))))
|
2006-07-26 19:57:32 +02:00
|
|
|
{
|
2007-10-20 23:22:04 +02:00
|
|
|
MultiByteToWideChar(CP_ACP, 0, pszX500, -1, x500, len);
|
|
|
|
ret = CertStrToNameW(dwCertEncodingType, x500, dwStrType,
|
|
|
|
pvReserved, pbEncoded, pcbEncoded,
|
|
|
|
ppszError ? (LPCWSTR *)&errorStr : NULL);
|
|
|
|
if (ppszError)
|
|
|
|
{
|
2007-10-20 23:26:00 +02:00
|
|
|
if (!ret)
|
|
|
|
{
|
2008-09-23 23:38:38 +02:00
|
|
|
LONG i;
|
2006-07-26 19:57:32 +02:00
|
|
|
|
2007-10-20 23:26:00 +02:00
|
|
|
*ppszError = pszX500;
|
|
|
|
for (i = 0; i < errorStr - x500; i++)
|
|
|
|
*ppszError = CharNextA(*ppszError);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
*ppszError = NULL;
|
2007-10-20 23:22:04 +02:00
|
|
|
}
|
|
|
|
CryptMemFree(x500);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
SetLastError(ERROR_OUTOFMEMORY);
|
|
|
|
ret = FALSE;
|
2006-07-26 19:57:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2007-10-20 23:22:04 +02:00
|
|
|
{
|
|
|
|
SetLastError(CRYPT_E_INVALID_X500_STRING);
|
|
|
|
if (ppszError)
|
|
|
|
*ppszError = pszX500;
|
2006-07-26 19:57:32 +02:00
|
|
|
ret = FALSE;
|
2007-10-20 23:22:04 +02:00
|
|
|
}
|
2006-07-26 19:57:32 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct KeynameKeeper
|
|
|
|
{
|
|
|
|
WCHAR buf[10]; /* big enough for L"GivenName" */
|
|
|
|
LPWSTR keyName; /* usually = buf, but may be allocated */
|
|
|
|
DWORD keyLen;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void CRYPT_InitializeKeynameKeeper(struct KeynameKeeper *keeper)
|
|
|
|
{
|
|
|
|
keeper->keyName = keeper->buf;
|
|
|
|
keeper->keyLen = sizeof(keeper->buf) / sizeof(keeper->buf[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void CRYPT_FreeKeynameKeeper(struct KeynameKeeper *keeper)
|
|
|
|
{
|
|
|
|
if (keeper->keyName != keeper->buf)
|
|
|
|
CryptMemFree(keeper->keyName);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct X500TokenW
|
|
|
|
{
|
|
|
|
LPCWSTR start;
|
|
|
|
LPCWSTR end;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void CRYPT_KeynameKeeperFromTokenW(struct KeynameKeeper *keeper,
|
2007-04-14 19:12:40 +02:00
|
|
|
const struct X500TokenW *key)
|
2006-07-26 19:57:32 +02:00
|
|
|
{
|
|
|
|
DWORD len = key->end - key->start;
|
|
|
|
|
|
|
|
if (len > keeper->keyLen)
|
|
|
|
{
|
|
|
|
if (keeper->keyName == keeper->buf)
|
|
|
|
keeper->keyName = CryptMemAlloc(len * sizeof(WCHAR));
|
|
|
|
else
|
|
|
|
keeper->keyName = CryptMemRealloc(keeper->keyName,
|
|
|
|
len * sizeof(WCHAR));
|
|
|
|
keeper->keyLen = len;
|
|
|
|
}
|
|
|
|
memcpy(keeper->keyName, key->start, (key->end - key->start) *
|
|
|
|
sizeof(WCHAR));
|
|
|
|
keeper->keyName[len] = '\0';
|
|
|
|
TRACE("Keyname is %s\n", debugstr_w(keeper->keyName));
|
|
|
|
}
|
|
|
|
|
2007-11-02 04:20:33 +01:00
|
|
|
static BOOL CRYPT_GetNextKeyW(LPCWSTR str, struct X500TokenW *token,
|
2006-07-26 19:57:32 +02:00
|
|
|
LPCWSTR *ppszError)
|
|
|
|
{
|
2007-11-02 04:20:33 +01:00
|
|
|
BOOL ret = TRUE;
|
2006-07-26 19:57:32 +02:00
|
|
|
|
|
|
|
while (*str && isspaceW(*str))
|
|
|
|
str++;
|
|
|
|
if (*str)
|
|
|
|
{
|
|
|
|
token->start = str;
|
|
|
|
while (*str && *str != '=' && !isspaceW(*str))
|
|
|
|
str++;
|
|
|
|
if (*str && (*str == '=' || isspaceW(*str)))
|
|
|
|
token->end = str;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TRACE("missing equals char at %s\n", debugstr_w(token->start));
|
|
|
|
if (ppszError)
|
|
|
|
*ppszError = token->start;
|
2007-11-02 04:20:33 +01:00
|
|
|
SetLastError(CRYPT_E_INVALID_X500_STRING);
|
|
|
|
ret = FALSE;
|
2006-07-26 19:57:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
token->start = NULL;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Assumes separators are characters in the 0-255 range */
|
2007-11-02 04:20:33 +01:00
|
|
|
static BOOL CRYPT_GetNextValueW(LPCWSTR str, DWORD dwFlags, LPCWSTR separators,
|
2006-07-26 19:57:32 +02:00
|
|
|
struct X500TokenW *token, LPCWSTR *ppszError)
|
|
|
|
{
|
2007-11-02 04:20:33 +01:00
|
|
|
BOOL ret = TRUE;
|
2006-07-26 19:57:32 +02:00
|
|
|
|
|
|
|
TRACE("(%s, %s, %p, %p)\n", debugstr_w(str), debugstr_w(separators), token,
|
|
|
|
ppszError);
|
|
|
|
|
|
|
|
while (*str && isspaceW(*str))
|
|
|
|
str++;
|
|
|
|
if (*str)
|
|
|
|
{
|
|
|
|
token->start = str;
|
|
|
|
if (!(dwFlags & CERT_NAME_STR_NO_QUOTING_FLAG) && *str == '"')
|
|
|
|
{
|
|
|
|
token->end = NULL;
|
|
|
|
str++;
|
2007-11-02 04:20:33 +01:00
|
|
|
while (!token->end && ret)
|
2006-07-26 19:57:32 +02:00
|
|
|
{
|
|
|
|
while (*str && *str != '"')
|
|
|
|
str++;
|
|
|
|
if (*str == '"')
|
|
|
|
{
|
|
|
|
if (*(str + 1) != '"')
|
|
|
|
token->end = str + 1;
|
|
|
|
else
|
|
|
|
str += 2;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TRACE("unterminated quote at %s\n", debugstr_w(str));
|
|
|
|
if (ppszError)
|
|
|
|
*ppszError = str;
|
2007-11-02 04:20:33 +01:00
|
|
|
SetLastError(CRYPT_E_INVALID_X500_STRING);
|
|
|
|
ret = FALSE;
|
2006-07-26 19:57:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
WCHAR map[256] = { 0 };
|
|
|
|
|
|
|
|
while (*separators)
|
|
|
|
map[*separators++] = 1;
|
2007-12-30 16:29:38 +01:00
|
|
|
while (*str && (*str >= 0xff || !map[*str]))
|
2006-07-26 19:57:32 +02:00
|
|
|
str++;
|
|
|
|
token->end = str;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TRACE("missing value at %s\n", debugstr_w(str));
|
|
|
|
if (ppszError)
|
|
|
|
*ppszError = str;
|
2007-11-02 04:20:33 +01:00
|
|
|
SetLastError(CRYPT_E_INVALID_X500_STRING);
|
|
|
|
ret = FALSE;
|
2006-07-26 19:57:32 +02:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Encodes the string represented by value as the string type type into the
|
|
|
|
* CERT_NAME_BLOB output. If there is an error and ppszError is not NULL,
|
|
|
|
* *ppszError is set to the first failing character. If there is no error,
|
|
|
|
* output's pbData must be freed with LocalFree.
|
|
|
|
*/
|
|
|
|
static BOOL CRYPT_EncodeValueWithType(DWORD dwCertEncodingType,
|
2007-04-14 19:12:40 +02:00
|
|
|
const struct X500TokenW *value, PCERT_NAME_BLOB output, DWORD type,
|
2006-07-26 19:57:32 +02:00
|
|
|
LPCWSTR *ppszError)
|
|
|
|
{
|
|
|
|
CERT_NAME_VALUE nameValue = { type, { 0, NULL } };
|
2007-10-20 23:25:15 +02:00
|
|
|
BOOL ret = TRUE;
|
2006-07-26 19:57:32 +02:00
|
|
|
|
2007-10-20 23:25:15 +02:00
|
|
|
if (value->end > value->start)
|
2006-07-26 19:57:32 +02:00
|
|
|
{
|
2007-10-20 23:25:15 +02:00
|
|
|
nameValue.Value.pbData = CryptMemAlloc((value->end - value->start) *
|
|
|
|
sizeof(WCHAR));
|
|
|
|
if (!nameValue.Value.pbData)
|
|
|
|
{
|
|
|
|
SetLastError(ERROR_OUTOFMEMORY);
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
if (value->end > value->start)
|
2006-07-26 19:57:32 +02:00
|
|
|
{
|
2008-09-23 23:38:38 +02:00
|
|
|
LONG i;
|
2007-10-20 23:25:15 +02:00
|
|
|
LPWSTR ptr = (LPWSTR)nameValue.Value.pbData;
|
|
|
|
|
|
|
|
for (i = 0; i < value->end - value->start; i++)
|
|
|
|
{
|
|
|
|
*ptr++ = value->start[i];
|
|
|
|
if (value->start[i] == '"')
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
nameValue.Value.cbData = (LPBYTE)ptr - nameValue.Value.pbData;
|
2006-07-26 19:57:32 +02:00
|
|
|
}
|
|
|
|
ret = CryptEncodeObjectEx(dwCertEncodingType, X509_UNICODE_NAME_VALUE,
|
|
|
|
&nameValue, CRYPT_ENCODE_ALLOC_FLAG, NULL, &output->pbData,
|
|
|
|
&output->cbData);
|
|
|
|
if (!ret && ppszError)
|
|
|
|
{
|
|
|
|
if (type == CERT_RDN_NUMERIC_STRING &&
|
|
|
|
GetLastError() == CRYPT_E_INVALID_NUMERIC_STRING)
|
|
|
|
*ppszError = value->start + output->cbData;
|
|
|
|
else if (type == CERT_RDN_PRINTABLE_STRING &&
|
|
|
|
GetLastError() == CRYPT_E_INVALID_PRINTABLE_STRING)
|
|
|
|
*ppszError = value->start + output->cbData;
|
|
|
|
else if (type == CERT_RDN_IA5_STRING &&
|
|
|
|
GetLastError() == CRYPT_E_INVALID_IA5_STRING)
|
|
|
|
*ppszError = value->start + output->cbData;
|
|
|
|
}
|
|
|
|
CryptMemFree(nameValue.Value.pbData);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL CRYPT_EncodeValue(DWORD dwCertEncodingType,
|
2007-04-14 19:12:40 +02:00
|
|
|
const struct X500TokenW *value, PCERT_NAME_BLOB output, const DWORD *types,
|
2006-07-26 19:57:32 +02:00
|
|
|
LPCWSTR *ppszError)
|
|
|
|
{
|
|
|
|
DWORD i;
|
|
|
|
BOOL ret;
|
|
|
|
|
|
|
|
ret = FALSE;
|
|
|
|
for (i = 0; !ret && types[i]; i++)
|
|
|
|
ret = CRYPT_EncodeValueWithType(dwCertEncodingType, value, output,
|
|
|
|
types[i], ppszError);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BOOL CRYPT_ValueToRDN(DWORD dwCertEncodingType, PCERT_NAME_INFO info,
|
|
|
|
PCCRYPT_OID_INFO keyOID, struct X500TokenW *value, LPCWSTR *ppszError)
|
|
|
|
{
|
|
|
|
BOOL ret = FALSE;
|
|
|
|
|
|
|
|
TRACE("OID %s, value %s\n", debugstr_a(keyOID->pszOID),
|
|
|
|
debugstr_wn(value->start, value->end - value->start));
|
|
|
|
|
|
|
|
if (!info->rgRDN)
|
|
|
|
info->rgRDN = CryptMemAlloc(sizeof(CERT_RDN));
|
|
|
|
else
|
|
|
|
info->rgRDN = CryptMemRealloc(info->rgRDN,
|
|
|
|
(info->cRDN + 1) * sizeof(CERT_RDN));
|
|
|
|
if (info->rgRDN)
|
|
|
|
{
|
|
|
|
/* FIXME: support multiple RDN attrs */
|
|
|
|
info->rgRDN[info->cRDN].rgRDNAttr =
|
|
|
|
CryptMemAlloc(sizeof(CERT_RDN_ATTR));
|
|
|
|
if (info->rgRDN[info->cRDN].rgRDNAttr)
|
|
|
|
{
|
|
|
|
static const DWORD defaultTypes[] = { CERT_RDN_PRINTABLE_STRING,
|
|
|
|
CERT_RDN_BMP_STRING, 0 };
|
|
|
|
const DWORD *types;
|
|
|
|
|
|
|
|
info->rgRDN[info->cRDN].cRDNAttr = 1;
|
|
|
|
info->rgRDN[info->cRDN].rgRDNAttr[0].pszObjId =
|
|
|
|
(LPSTR)keyOID->pszOID;
|
|
|
|
info->rgRDN[info->cRDN].rgRDNAttr[0].dwValueType =
|
|
|
|
CERT_RDN_ENCODED_BLOB;
|
|
|
|
if (keyOID->ExtraInfo.cbData)
|
|
|
|
types = (const DWORD *)keyOID->ExtraInfo.pbData;
|
|
|
|
else
|
|
|
|
types = defaultTypes;
|
|
|
|
|
|
|
|
/* Remove surrounding quotes */
|
|
|
|
if (value->start[0] == '"')
|
|
|
|
{
|
|
|
|
value->start++;
|
|
|
|
value->end--;
|
|
|
|
}
|
|
|
|
ret = CRYPT_EncodeValue(dwCertEncodingType, value,
|
|
|
|
&info->rgRDN[info->cRDN].rgRDNAttr[0].Value, types, ppszError);
|
|
|
|
}
|
2007-11-02 04:04:36 +01:00
|
|
|
else
|
|
|
|
SetLastError(ERROR_OUTOFMEMORY);
|
2006-07-26 19:57:32 +02:00
|
|
|
info->cRDN++;
|
2007-11-02 04:04:36 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
SetLastError(ERROR_OUTOFMEMORY);
|
2006-07-26 19:57:32 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL WINAPI CertStrToNameW(DWORD dwCertEncodingType, LPCWSTR pszX500,
|
|
|
|
DWORD dwStrType, void *pvReserved, BYTE *pbEncoded, DWORD *pcbEncoded,
|
|
|
|
LPCWSTR *ppszError)
|
|
|
|
{
|
|
|
|
CERT_NAME_INFO info = { 0, NULL };
|
|
|
|
LPCWSTR str;
|
|
|
|
struct KeynameKeeper keeper;
|
2007-11-02 04:20:33 +01:00
|
|
|
DWORD i;
|
2006-07-26 19:57:32 +02:00
|
|
|
BOOL ret = TRUE;
|
|
|
|
|
2006-10-04 06:58:09 +02:00
|
|
|
TRACE("(%08x, %s, %08x, %p, %p, %p, %p)\n", dwCertEncodingType,
|
2006-07-26 19:57:32 +02:00
|
|
|
debugstr_w(pszX500), dwStrType, pvReserved, pbEncoded, pcbEncoded,
|
|
|
|
ppszError);
|
|
|
|
|
|
|
|
CRYPT_InitializeKeynameKeeper(&keeper);
|
|
|
|
str = pszX500;
|
2007-11-02 04:20:33 +01:00
|
|
|
while (str && *str && ret)
|
2006-07-26 19:57:32 +02:00
|
|
|
{
|
|
|
|
struct X500TokenW token;
|
|
|
|
|
2007-11-02 04:20:33 +01:00
|
|
|
ret = CRYPT_GetNextKeyW(str, &token, ppszError);
|
|
|
|
if (ret && token.start)
|
2006-07-26 19:57:32 +02:00
|
|
|
{
|
|
|
|
PCCRYPT_OID_INFO keyOID;
|
|
|
|
|
|
|
|
CRYPT_KeynameKeeperFromTokenW(&keeper, &token);
|
|
|
|
keyOID = CryptFindOIDInfo(CRYPT_OID_INFO_NAME_KEY, keeper.keyName,
|
|
|
|
CRYPT_RDN_ATTR_OID_GROUP_ID);
|
|
|
|
if (!keyOID)
|
|
|
|
{
|
|
|
|
if (ppszError)
|
|
|
|
*ppszError = token.start;
|
2007-11-02 04:20:33 +01:00
|
|
|
SetLastError(CRYPT_E_INVALID_X500_STRING);
|
|
|
|
ret = FALSE;
|
2006-07-26 19:57:32 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
str = token.end;
|
|
|
|
while (isspace(*str))
|
|
|
|
str++;
|
|
|
|
if (*str != '=')
|
|
|
|
{
|
|
|
|
if (ppszError)
|
|
|
|
*ppszError = str;
|
2007-11-02 04:20:33 +01:00
|
|
|
SetLastError(CRYPT_E_INVALID_X500_STRING);
|
|
|
|
ret = FALSE;
|
2006-07-26 19:57:32 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
static const WCHAR commaSep[] = { ',',0 };
|
|
|
|
static const WCHAR semiSep[] = { ';',0 };
|
|
|
|
static const WCHAR crlfSep[] = { '\r','\n',0 };
|
|
|
|
static const WCHAR allSeps[] = { ',',';','\r','\n',0 };
|
|
|
|
LPCWSTR sep;
|
|
|
|
|
|
|
|
str++;
|
|
|
|
if (dwStrType & CERT_NAME_STR_COMMA_FLAG)
|
|
|
|
sep = commaSep;
|
|
|
|
else if (dwStrType & CERT_NAME_STR_SEMICOLON_FLAG)
|
|
|
|
sep = semiSep;
|
|
|
|
else if (dwStrType & CERT_NAME_STR_CRLF_FLAG)
|
|
|
|
sep = crlfSep;
|
|
|
|
else
|
|
|
|
sep = allSeps;
|
2007-11-02 04:20:33 +01:00
|
|
|
ret = CRYPT_GetNextValueW(str, dwStrType, sep, &token,
|
2006-07-26 19:57:32 +02:00
|
|
|
ppszError);
|
2007-11-02 04:20:33 +01:00
|
|
|
if (ret)
|
2006-07-26 19:57:32 +02:00
|
|
|
{
|
|
|
|
str = token.end;
|
|
|
|
ret = CRYPT_ValueToRDN(dwCertEncodingType, &info,
|
|
|
|
keyOID, &token, ppszError);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CRYPT_FreeKeynameKeeper(&keeper);
|
2007-11-02 04:20:33 +01:00
|
|
|
if (ret)
|
2006-07-26 19:57:32 +02:00
|
|
|
{
|
2007-10-20 23:26:00 +02:00
|
|
|
if (ppszError)
|
|
|
|
*ppszError = NULL;
|
2006-07-26 19:57:32 +02:00
|
|
|
ret = CryptEncodeObjectEx(dwCertEncodingType, X509_NAME, &info,
|
|
|
|
0, NULL, pbEncoded, pcbEncoded);
|
|
|
|
}
|
2007-11-02 04:05:31 +01:00
|
|
|
for (i = 0; i < info.cRDN; i++)
|
|
|
|
{
|
|
|
|
DWORD j;
|
|
|
|
|
|
|
|
for (j = 0; j < info.rgRDN[i].cRDNAttr; j++)
|
|
|
|
LocalFree(info.rgRDN[i].rgRDNAttr[j].Value.pbData);
|
|
|
|
CryptMemFree(info.rgRDN[i].rgRDNAttr);
|
|
|
|
}
|
|
|
|
CryptMemFree(info.rgRDN);
|
2006-07-26 19:57:32 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2006-02-16 12:08:19 +01:00
|
|
|
DWORD WINAPI CertGetNameStringA(PCCERT_CONTEXT pCertContext, DWORD dwType,
|
|
|
|
DWORD dwFlags, void *pvTypePara, LPSTR pszNameString, DWORD cchNameString)
|
|
|
|
{
|
|
|
|
DWORD ret;
|
|
|
|
|
2006-10-04 06:58:09 +02:00
|
|
|
TRACE("(%p, %d, %08x, %p, %p, %d)\n", pCertContext, dwType, dwFlags,
|
2006-02-16 12:08:19 +01:00
|
|
|
pvTypePara, pszNameString, cchNameString);
|
|
|
|
|
|
|
|
if (pszNameString)
|
|
|
|
{
|
|
|
|
LPWSTR wideName;
|
|
|
|
DWORD nameLen;
|
|
|
|
|
|
|
|
nameLen = CertGetNameStringW(pCertContext, dwType, dwFlags, pvTypePara,
|
|
|
|
NULL, 0);
|
|
|
|
wideName = CryptMemAlloc(nameLen * sizeof(WCHAR));
|
|
|
|
if (wideName)
|
|
|
|
{
|
|
|
|
CertGetNameStringW(pCertContext, dwType, dwFlags, pvTypePara,
|
|
|
|
wideName, nameLen);
|
|
|
|
nameLen = WideCharToMultiByte(CP_ACP, 0, wideName, nameLen,
|
|
|
|
pszNameString, cchNameString, NULL, NULL);
|
|
|
|
if (nameLen <= cchNameString)
|
|
|
|
ret = nameLen;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pszNameString[cchNameString - 1] = '\0';
|
|
|
|
ret = cchNameString;
|
|
|
|
}
|
|
|
|
CryptMemFree(wideName);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*pszNameString = '\0';
|
|
|
|
ret = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ret = CertGetNameStringW(pCertContext, dwType, dwFlags, pvTypePara,
|
|
|
|
NULL, 0);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-09-09 06:55:47 +02:00
|
|
|
/* Searches cert's extensions for the alternate name extension with OID
|
|
|
|
* altNameOID, and if found, searches it for the alternate name type entryType.
|
|
|
|
* If found, returns a pointer to the entry, otherwise returns NULL.
|
|
|
|
* Regardless of whether an entry of the desired type is found, if the
|
|
|
|
* alternate name extension is present, sets *info to the decoded alternate
|
|
|
|
* name extension, which you must free using LocalFree.
|
|
|
|
* The return value is a pointer within *info, so don't free *info before
|
|
|
|
* you're done with the return value.
|
|
|
|
*/
|
|
|
|
static PCERT_ALT_NAME_ENTRY cert_find_alt_name_entry(PCCERT_CONTEXT cert,
|
|
|
|
LPCSTR altNameOID, DWORD entryType, PCERT_ALT_NAME_INFO *info)
|
|
|
|
{
|
|
|
|
PCERT_ALT_NAME_ENTRY entry = NULL;
|
|
|
|
PCERT_EXTENSION ext = CertFindExtension(altNameOID,
|
|
|
|
cert->pCertInfo->cExtension, cert->pCertInfo->rgExtension);
|
|
|
|
|
|
|
|
if (ext)
|
|
|
|
{
|
|
|
|
DWORD bytes = 0;
|
|
|
|
|
|
|
|
if (CryptDecodeObjectEx(cert->dwCertEncodingType, X509_ALTERNATE_NAME,
|
|
|
|
ext->Value.pbData, ext->Value.cbData, CRYPT_DECODE_ALLOC_FLAG, NULL,
|
|
|
|
info, &bytes))
|
|
|
|
{
|
|
|
|
DWORD i;
|
|
|
|
|
|
|
|
for (i = 0; !entry && i < (*info)->cAltEntry; i++)
|
|
|
|
if ((*info)->rgAltEntry[i].dwAltNameChoice == entryType)
|
|
|
|
entry = &(*info)->rgAltEntry[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
*info = NULL;
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
|
2009-09-09 07:10:53 +02:00
|
|
|
static DWORD cert_get_name_from_rdn_attr(DWORD encodingType,
|
2010-08-19 23:35:15 +02:00
|
|
|
const CERT_NAME_BLOB *name, LPCSTR oid, LPWSTR pszNameString, DWORD cchNameString)
|
2009-09-09 07:10:53 +02:00
|
|
|
{
|
|
|
|
CERT_NAME_INFO *nameInfo;
|
|
|
|
DWORD bytes = 0, ret = 0;
|
|
|
|
|
|
|
|
if (CryptDecodeObjectEx(encodingType, X509_NAME, name->pbData,
|
|
|
|
name->cbData, CRYPT_DECODE_ALLOC_FLAG, NULL, &nameInfo, &bytes))
|
|
|
|
{
|
|
|
|
PCERT_RDN_ATTR nameAttr = CertFindRDNAttr(oid, nameInfo);
|
|
|
|
|
|
|
|
if (nameAttr)
|
|
|
|
ret = CertRDNValueToStrW(nameAttr->dwValueType, &nameAttr->Value,
|
|
|
|
pszNameString, cchNameString);
|
|
|
|
LocalFree(nameInfo);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2006-02-16 12:08:19 +01:00
|
|
|
DWORD WINAPI CertGetNameStringW(PCCERT_CONTEXT pCertContext, DWORD dwType,
|
|
|
|
DWORD dwFlags, void *pvTypePara, LPWSTR pszNameString, DWORD cchNameString)
|
|
|
|
{
|
2009-09-09 06:06:28 +02:00
|
|
|
DWORD ret = 0;
|
2006-02-16 12:08:19 +01:00
|
|
|
PCERT_NAME_BLOB name;
|
|
|
|
LPCSTR altNameOID;
|
|
|
|
|
2006-10-04 06:58:09 +02:00
|
|
|
TRACE("(%p, %d, %08x, %p, %p, %d)\n", pCertContext, dwType,
|
2006-02-16 12:08:19 +01:00
|
|
|
dwFlags, pvTypePara, pszNameString, cchNameString);
|
|
|
|
|
|
|
|
if (dwFlags & CERT_NAME_ISSUER_FLAG)
|
|
|
|
{
|
|
|
|
name = &pCertContext->pCertInfo->Issuer;
|
|
|
|
altNameOID = szOID_ISSUER_ALT_NAME;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
name = &pCertContext->pCertInfo->Subject;
|
|
|
|
altNameOID = szOID_SUBJECT_ALT_NAME;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (dwType)
|
|
|
|
{
|
2009-09-09 07:11:27 +02:00
|
|
|
case CERT_NAME_EMAIL_TYPE:
|
|
|
|
{
|
|
|
|
CERT_ALT_NAME_INFO *info;
|
|
|
|
PCERT_ALT_NAME_ENTRY entry = cert_find_alt_name_entry(pCertContext,
|
|
|
|
altNameOID, CERT_ALT_NAME_RFC822_NAME, &info);
|
|
|
|
|
|
|
|
if (entry)
|
|
|
|
{
|
|
|
|
if (!pszNameString)
|
2009-10-17 20:27:58 +02:00
|
|
|
ret = strlenW(entry->u.pwszRfc822Name) + 1;
|
2009-09-09 17:40:44 +02:00
|
|
|
else if (cchNameString)
|
2009-09-09 07:11:27 +02:00
|
|
|
{
|
2009-10-17 20:27:58 +02:00
|
|
|
ret = min(strlenW(entry->u.pwszRfc822Name), cchNameString - 1);
|
|
|
|
memcpy(pszNameString, entry->u.pwszRfc822Name,
|
2009-09-09 07:11:27 +02:00
|
|
|
ret * sizeof(WCHAR));
|
|
|
|
pszNameString[ret++] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (info)
|
|
|
|
LocalFree(info);
|
|
|
|
if (!ret)
|
|
|
|
ret = cert_get_name_from_rdn_attr(pCertContext->dwCertEncodingType,
|
|
|
|
name, szOID_RSA_emailAddr, pszNameString, cchNameString);
|
|
|
|
break;
|
|
|
|
}
|
2009-09-09 07:11:12 +02:00
|
|
|
case CERT_NAME_RDN_TYPE:
|
|
|
|
if (name->cbData)
|
|
|
|
ret = CertNameToStrW(pCertContext->dwCertEncodingType, name,
|
|
|
|
*(DWORD *)pvTypePara, pszNameString, cchNameString);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CERT_ALT_NAME_INFO *info;
|
|
|
|
PCERT_ALT_NAME_ENTRY entry = cert_find_alt_name_entry(pCertContext,
|
|
|
|
altNameOID, CERT_ALT_NAME_DIRECTORY_NAME, &info);
|
|
|
|
|
|
|
|
if (entry)
|
|
|
|
ret = CertNameToStrW(pCertContext->dwCertEncodingType,
|
2009-10-17 20:27:58 +02:00
|
|
|
&entry->u.DirectoryName, *(DWORD *)pvTypePara, pszNameString,
|
2009-09-09 07:11:12 +02:00
|
|
|
cchNameString);
|
|
|
|
if (info)
|
|
|
|
LocalFree(info);
|
|
|
|
}
|
|
|
|
break;
|
2009-09-09 07:10:53 +02:00
|
|
|
case CERT_NAME_ATTR_TYPE:
|
|
|
|
ret = cert_get_name_from_rdn_attr(pCertContext->dwCertEncodingType,
|
|
|
|
name, pvTypePara, pszNameString, cchNameString);
|
|
|
|
if (!ret)
|
|
|
|
{
|
|
|
|
CERT_ALT_NAME_INFO *altInfo;
|
|
|
|
PCERT_ALT_NAME_ENTRY entry = cert_find_alt_name_entry(pCertContext,
|
|
|
|
altNameOID, CERT_ALT_NAME_DIRECTORY_NAME, &altInfo);
|
|
|
|
|
|
|
|
if (entry)
|
|
|
|
ret = cert_name_to_str_with_indent(X509_ASN_ENCODING, 0,
|
2009-10-17 20:27:58 +02:00
|
|
|
&entry->u.DirectoryName, 0, pszNameString, cchNameString);
|
2009-09-09 07:10:53 +02:00
|
|
|
if (altInfo)
|
|
|
|
LocalFree(altInfo);
|
|
|
|
}
|
|
|
|
break;
|
2006-02-16 12:08:19 +01:00
|
|
|
case CERT_NAME_SIMPLE_DISPLAY_TYPE:
|
|
|
|
{
|
|
|
|
static const LPCSTR simpleAttributeOIDs[] = { szOID_COMMON_NAME,
|
|
|
|
szOID_ORGANIZATIONAL_UNIT_NAME, szOID_ORGANIZATION_NAME,
|
|
|
|
szOID_RSA_emailAddr };
|
2009-09-09 06:06:28 +02:00
|
|
|
CERT_NAME_INFO *nameInfo = NULL;
|
2006-02-16 12:08:19 +01:00
|
|
|
DWORD bytes = 0, i;
|
|
|
|
|
|
|
|
if (CryptDecodeObjectEx(pCertContext->dwCertEncodingType, X509_NAME,
|
2009-09-09 06:06:28 +02:00
|
|
|
name->pbData, name->cbData, CRYPT_DECODE_ALLOC_FLAG, NULL, &nameInfo,
|
2006-02-16 12:08:19 +01:00
|
|
|
&bytes))
|
|
|
|
{
|
2009-09-09 06:56:11 +02:00
|
|
|
PCERT_RDN_ATTR nameAttr = NULL;
|
|
|
|
|
2006-02-16 12:08:19 +01:00
|
|
|
for (i = 0; !nameAttr && i < sizeof(simpleAttributeOIDs) /
|
|
|
|
sizeof(simpleAttributeOIDs[0]); i++)
|
2009-09-09 06:06:28 +02:00
|
|
|
nameAttr = CertFindRDNAttr(simpleAttributeOIDs[i], nameInfo);
|
2009-09-09 06:56:11 +02:00
|
|
|
if (nameAttr)
|
|
|
|
ret = CertRDNValueToStrW(nameAttr->dwValueType,
|
|
|
|
&nameAttr->Value, pszNameString, cchNameString);
|
|
|
|
LocalFree(nameInfo);
|
2006-02-16 12:08:19 +01:00
|
|
|
}
|
2009-09-09 06:56:11 +02:00
|
|
|
if (!ret)
|
2006-02-16 12:08:19 +01:00
|
|
|
{
|
2009-09-09 06:55:47 +02:00
|
|
|
CERT_ALT_NAME_INFO *altInfo;
|
|
|
|
PCERT_ALT_NAME_ENTRY entry = cert_find_alt_name_entry(pCertContext,
|
|
|
|
altNameOID, CERT_ALT_NAME_RFC822_NAME, &altInfo);
|
2006-02-16 12:08:19 +01:00
|
|
|
|
2009-09-09 06:55:47 +02:00
|
|
|
if (altInfo)
|
2006-02-16 12:08:19 +01:00
|
|
|
{
|
2009-09-09 06:55:47 +02:00
|
|
|
if (!entry && altInfo->cAltEntry)
|
|
|
|
entry = &altInfo->rgAltEntry[0];
|
|
|
|
if (entry)
|
2006-02-16 12:08:19 +01:00
|
|
|
{
|
2009-09-09 06:55:47 +02:00
|
|
|
if (!pszNameString)
|
2009-10-17 20:27:58 +02:00
|
|
|
ret = strlenW(entry->u.pwszRfc822Name) + 1;
|
2009-09-09 17:40:44 +02:00
|
|
|
else if (cchNameString)
|
2009-09-09 06:06:28 +02:00
|
|
|
{
|
2009-10-17 20:27:58 +02:00
|
|
|
ret = min(strlenW(entry->u.pwszRfc822Name),
|
2009-09-09 06:55:47 +02:00
|
|
|
cchNameString - 1);
|
2009-10-17 20:27:58 +02:00
|
|
|
memcpy(pszNameString, entry->u.pwszRfc822Name,
|
2009-09-09 06:55:47 +02:00
|
|
|
ret * sizeof(WCHAR));
|
|
|
|
pszNameString[ret++] = 0;
|
2009-09-09 06:06:28 +02:00
|
|
|
}
|
2006-02-16 12:08:19 +01:00
|
|
|
}
|
2009-09-09 06:55:47 +02:00
|
|
|
LocalFree(altInfo);
|
2006-02-16 12:08:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case CERT_NAME_FRIENDLY_DISPLAY_TYPE:
|
|
|
|
{
|
|
|
|
DWORD cch = cchNameString;
|
|
|
|
|
|
|
|
if (CertGetCertificateContextProperty(pCertContext,
|
|
|
|
CERT_FRIENDLY_NAME_PROP_ID, pszNameString, &cch))
|
|
|
|
ret = cch;
|
|
|
|
else
|
|
|
|
ret = CertGetNameStringW(pCertContext,
|
|
|
|
CERT_NAME_SIMPLE_DISPLAY_TYPE, dwFlags, pvTypePara, pszNameString,
|
|
|
|
cchNameString);
|
|
|
|
break;
|
|
|
|
}
|
2009-09-09 07:11:40 +02:00
|
|
|
case CERT_NAME_DNS_TYPE:
|
|
|
|
{
|
|
|
|
CERT_ALT_NAME_INFO *info;
|
|
|
|
PCERT_ALT_NAME_ENTRY entry = cert_find_alt_name_entry(pCertContext,
|
|
|
|
altNameOID, CERT_ALT_NAME_DNS_NAME, &info);
|
|
|
|
|
|
|
|
if (entry)
|
|
|
|
{
|
|
|
|
if (!pszNameString)
|
2009-10-17 20:27:58 +02:00
|
|
|
ret = strlenW(entry->u.pwszDNSName) + 1;
|
2009-09-09 17:40:44 +02:00
|
|
|
else if (cchNameString)
|
2009-09-09 07:11:40 +02:00
|
|
|
{
|
2009-10-17 20:27:58 +02:00
|
|
|
ret = min(strlenW(entry->u.pwszDNSName), cchNameString - 1);
|
|
|
|
memcpy(pszNameString, entry->u.pwszDNSName, ret * sizeof(WCHAR));
|
2009-09-09 07:11:40 +02:00
|
|
|
pszNameString[ret++] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (info)
|
|
|
|
LocalFree(info);
|
|
|
|
if (!ret)
|
|
|
|
ret = cert_get_name_from_rdn_attr(pCertContext->dwCertEncodingType,
|
|
|
|
name, szOID_COMMON_NAME, pszNameString, cchNameString);
|
|
|
|
break;
|
|
|
|
}
|
2009-09-09 07:11:52 +02:00
|
|
|
case CERT_NAME_URL_TYPE:
|
|
|
|
{
|
|
|
|
CERT_ALT_NAME_INFO *info;
|
|
|
|
PCERT_ALT_NAME_ENTRY entry = cert_find_alt_name_entry(pCertContext,
|
|
|
|
altNameOID, CERT_ALT_NAME_URL, &info);
|
|
|
|
|
|
|
|
if (entry)
|
|
|
|
{
|
|
|
|
if (!pszNameString)
|
2009-10-17 20:27:58 +02:00
|
|
|
ret = strlenW(entry->u.pwszURL) + 1;
|
2009-09-09 17:40:44 +02:00
|
|
|
else if (cchNameString)
|
2009-09-09 07:11:52 +02:00
|
|
|
{
|
2009-10-17 20:27:58 +02:00
|
|
|
ret = min(strlenW(entry->u.pwszURL), cchNameString - 1);
|
|
|
|
memcpy(pszNameString, entry->u.pwszURL, ret * sizeof(WCHAR));
|
2009-09-09 07:11:52 +02:00
|
|
|
pszNameString[ret++] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (info)
|
|
|
|
LocalFree(info);
|
|
|
|
break;
|
|
|
|
}
|
2006-02-16 12:08:19 +01:00
|
|
|
default:
|
2006-10-04 06:58:09 +02:00
|
|
|
FIXME("unimplemented for type %d\n", dwType);
|
2006-02-16 12:08:19 +01:00
|
|
|
ret = 0;
|
|
|
|
}
|
2009-09-09 06:55:26 +02:00
|
|
|
if (!ret)
|
|
|
|
{
|
|
|
|
if (!pszNameString)
|
|
|
|
ret = 1;
|
|
|
|
else if (cchNameString)
|
|
|
|
{
|
|
|
|
pszNameString[0] = 0;
|
|
|
|
ret = 1;
|
|
|
|
}
|
|
|
|
}
|
2006-02-16 12:08:19 +01:00
|
|
|
return ret;
|
|
|
|
}
|