2 * Copyright 2006 Juan Lang for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #include "wine/debug.h"
25 #include "wine/unicode.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(crypt);
29 DWORD WINAPI CertRDNValueToStrA(DWORD dwValueType, PCERT_RDN_VALUE_BLOB pValue,
34 TRACE("(%d, %p, %p, %d)\n", dwValueType, pValue, psz, csz);
38 case CERT_RDN_ANY_TYPE:
40 case CERT_RDN_NUMERIC_STRING:
41 case CERT_RDN_PRINTABLE_STRING:
42 case CERT_RDN_TELETEX_STRING:
43 case CERT_RDN_VIDEOTEX_STRING:
44 case CERT_RDN_IA5_STRING:
45 case CERT_RDN_GRAPHIC_STRING:
46 case CERT_RDN_VISIBLE_STRING:
47 case CERT_RDN_GENERAL_STRING:
52 DWORD chars = min(pValue->cbData, csz - 1);
56 memcpy(psz, pValue->pbData, chars);
62 case CERT_RDN_UTF8_STRING:
64 ret = WideCharToMultiByte(CP_UTF8, 0, (LPWSTR)pValue->pbData,
65 pValue->cbData / sizeof(WCHAR) + 1, NULL, 0, NULL, NULL);
68 ret = WideCharToMultiByte(CP_UTF8, 0, (LPWSTR)pValue->pbData,
69 pValue->cbData / sizeof(WCHAR) + 1, psz, csz - 1, NULL, NULL);
74 FIXME("string type %d unimplemented\n", dwValueType);
84 TRACE("returning %d (%s)\n", ret, debugstr_a(psz));
88 DWORD WINAPI CertRDNValueToStrW(DWORD dwValueType, PCERT_RDN_VALUE_BLOB pValue,
89 LPWSTR psz, DWORD csz)
93 TRACE("(%d, %p, %p, %d)\n", dwValueType, pValue, psz, csz);
97 case CERT_RDN_ANY_TYPE:
99 case CERT_RDN_NUMERIC_STRING:
100 case CERT_RDN_PRINTABLE_STRING:
101 case CERT_RDN_TELETEX_STRING:
102 case CERT_RDN_VIDEOTEX_STRING:
103 case CERT_RDN_IA5_STRING:
104 case CERT_RDN_GRAPHIC_STRING:
105 case CERT_RDN_VISIBLE_STRING:
106 case CERT_RDN_GENERAL_STRING:
108 ret = pValue->cbData;
111 DWORD chars = min(pValue->cbData, csz - 1);
117 for (i = 0; i < chars; i++)
118 psz[i] = pValue->pbData[i];
124 case CERT_RDN_UTF8_STRING:
126 ret = pValue->cbData / sizeof(WCHAR);
129 DWORD chars = min(pValue->cbData / sizeof(WCHAR), csz - 1);
135 for (i = 0; i < chars; i++)
136 psz[i] = *((LPWSTR)pValue->pbData + i);
143 FIXME("string type %d unimplemented\n", dwValueType);
153 TRACE("returning %d (%s)\n", ret, debugstr_w(psz));
157 /* Adds the prefix prefix to the string pointed to by psz, followed by the
158 * character '='. Copies no more than csz characters. Returns the number of
159 * characters copied. If psz is NULL, returns the number of characters that
162 static DWORD CRYPT_AddPrefixA(LPCSTR prefix, LPSTR psz, DWORD csz)
166 TRACE("(%s, %p, %d)\n", debugstr_a(prefix), psz, csz);
170 chars = min(strlen(prefix), csz);
171 memcpy(psz, prefix, chars);
172 *(psz + chars) = '=';
176 chars = lstrlenA(prefix) + 1;
180 DWORD WINAPI CertNameToStrA(DWORD dwCertEncodingType, PCERT_NAME_BLOB pName,
181 DWORD dwStrType, LPSTR psz, DWORD csz)
183 static const DWORD unsupportedFlags = CERT_NAME_STR_NO_QUOTING_FLAG |
184 CERT_NAME_STR_ENABLE_T61_UNICODE_FLAG;
185 static const char commaSep[] = ", ";
186 static const char semiSep[] = "; ";
187 static const char crlfSep[] = "\r\n";
188 static const char plusSep[] = " + ";
189 static const char spaceSep[] = " ";
190 DWORD ret = 0, bytes = 0;
192 CERT_NAME_INFO *info;
194 TRACE("(%d, %p, %08x, %p, %d)\n", dwCertEncodingType, pName, dwStrType,
196 if (dwStrType & unsupportedFlags)
197 FIXME("unsupported flags: %08x\n", dwStrType & unsupportedFlags);
199 bRet = CryptDecodeObjectEx(dwCertEncodingType, X509_NAME, pName->pbData,
200 pName->cbData, CRYPT_DECODE_ALLOC_FLAG, NULL, &info, &bytes);
203 DWORD i, j, sepLen, rdnSepLen;
205 BOOL reverse = dwStrType & CERT_NAME_STR_REVERSE_FLAG;
206 const CERT_RDN *rdn = info->rgRDN;
208 if(reverse && info->cRDN > 1) rdn += (info->cRDN - 1);
210 if (dwStrType & CERT_NAME_STR_SEMICOLON_FLAG)
212 else if (dwStrType & CERT_NAME_STR_CRLF_FLAG)
216 sepLen = strlen(sep);
217 if (dwStrType & CERT_NAME_STR_NO_PLUS_FLAG)
221 rdnSepLen = strlen(rdnSep);
222 for (i = 0; (!psz || ret < csz) && i < info->cRDN; i++)
224 for (j = 0; (!psz || ret < csz) && j < rdn->cRDNAttr; j++)
227 char prefixBuf[10]; /* big enough for GivenName */
228 LPCSTR prefix = NULL;
230 if ((dwStrType & 0x000000ff) == CERT_OID_NAME_STR)
231 prefix = rdn->rgRDNAttr[j].pszObjId;
232 else if ((dwStrType & 0x000000ff) == CERT_X500_NAME_STR)
234 PCCRYPT_OID_INFO oidInfo = CryptFindOIDInfo(
235 CRYPT_OID_INFO_OID_KEY,
236 rdn->rgRDNAttr[j].pszObjId,
237 CRYPT_RDN_ATTR_OID_GROUP_ID);
241 WideCharToMultiByte(CP_ACP, 0, oidInfo->pwszName, -1,
242 prefixBuf, sizeof(prefixBuf), NULL, NULL);
246 prefix = rdn->rgRDNAttr[j].pszObjId;
250 /* - 1 is needed to account for the NULL terminator. */
251 chars = CRYPT_AddPrefixA(prefix,
252 psz ? psz + ret : NULL, psz ? csz - ret - 1 : 0);
255 /* FIXME: handle quoting */
256 chars = CertRDNValueToStrA(
257 rdn->rgRDNAttr[j].dwValueType,
258 &rdn->rgRDNAttr[j].Value, psz ? psz + ret : NULL,
259 psz ? csz - ret : 0);
262 if (j < rdn->cRDNAttr - 1)
264 if (psz && ret < csz - rdnSepLen - 1)
265 memcpy(psz + ret, rdnSep, rdnSepLen);
269 if (i < info->cRDN - 1)
271 if (psz && ret < csz - sepLen - 1)
272 memcpy(psz + ret, sep, sepLen);
287 TRACE("Returning %s\n", debugstr_a(psz));
291 /* Adds the prefix prefix to the wide-character string pointed to by psz,
292 * followed by the character '='. Copies no more than csz characters. Returns
293 * the number of characters copied. If psz is NULL, returns the number of
294 * characters that would be copied.
295 * Assumes the characters in prefix are ASCII (not multibyte characters.)
297 static DWORD CRYPT_AddPrefixAToW(LPCSTR prefix, LPWSTR psz, DWORD csz)
301 TRACE("(%s, %p, %d)\n", debugstr_a(prefix), psz, csz);
307 chars = min(strlen(prefix), csz);
308 for (i = 0; i < chars; i++)
309 *(psz + i) = prefix[i];
310 *(psz + chars) = '=';
314 chars = lstrlenA(prefix) + 1;
318 /* Adds the prefix prefix to the string pointed to by psz, followed by the
319 * character '='. Copies no more than csz characters. Returns the number of
320 * characters copied. If psz is NULL, returns the number of characters that
323 static DWORD CRYPT_AddPrefixW(LPCWSTR prefix, LPWSTR psz, DWORD csz)
327 TRACE("(%s, %p, %d)\n", debugstr_w(prefix), psz, csz);
331 chars = min(strlenW(prefix), csz);
332 memcpy(psz, prefix, chars * sizeof(WCHAR));
333 *(psz + chars) = '=';
337 chars = lstrlenW(prefix) + 1;
341 static const WCHAR indent[] = { ' ',' ',' ',' ',' ',0 };
343 DWORD cert_name_to_str_with_indent(DWORD dwCertEncodingType, DWORD indentLevel,
344 const CERT_NAME_BLOB *pName, DWORD dwStrType, LPWSTR psz, DWORD csz)
346 static const DWORD unsupportedFlags = CERT_NAME_STR_NO_QUOTING_FLAG |
347 CERT_NAME_STR_ENABLE_T61_UNICODE_FLAG;
348 static const WCHAR commaSep[] = { ',',' ',0 };
349 static const WCHAR semiSep[] = { ';',' ',0 };
350 static const WCHAR crlfSep[] = { '\r','\n',0 };
351 static const WCHAR plusSep[] = { ' ','+',' ',0 };
352 static const WCHAR spaceSep[] = { ' ',0 };
353 DWORD ret = 0, bytes = 0;
355 CERT_NAME_INFO *info;
357 if (dwStrType & unsupportedFlags)
358 FIXME("unsupported flags: %08x\n", dwStrType & unsupportedFlags);
360 bRet = CryptDecodeObjectEx(dwCertEncodingType, X509_NAME, pName->pbData,
361 pName->cbData, CRYPT_DECODE_ALLOC_FLAG, NULL, &info, &bytes);
364 DWORD i, j, sepLen, rdnSepLen;
366 BOOL reverse = dwStrType & CERT_NAME_STR_REVERSE_FLAG;
367 const CERT_RDN *rdn = info->rgRDN;
369 if(reverse && info->cRDN > 1) rdn += (info->cRDN - 1);
371 if (dwStrType & CERT_NAME_STR_SEMICOLON_FLAG)
373 else if (dwStrType & CERT_NAME_STR_CRLF_FLAG)
377 sepLen = lstrlenW(sep);
378 if (dwStrType & CERT_NAME_STR_NO_PLUS_FLAG)
382 rdnSepLen = lstrlenW(rdnSep);
383 for (i = 0; (!psz || ret < csz) && i < info->cRDN; i++)
385 for (j = 0; (!psz || ret < csz) && j < rdn->cRDNAttr; j++)
388 LPCSTR prefixA = NULL;
389 LPCWSTR prefixW = NULL;
391 if ((dwStrType & 0x000000ff) == CERT_OID_NAME_STR)
392 prefixA = rdn->rgRDNAttr[j].pszObjId;
393 else if ((dwStrType & 0x000000ff) == CERT_X500_NAME_STR)
395 PCCRYPT_OID_INFO oidInfo = CryptFindOIDInfo(
396 CRYPT_OID_INFO_OID_KEY,
397 rdn->rgRDNAttr[j].pszObjId,
398 CRYPT_RDN_ATTR_OID_GROUP_ID);
401 prefixW = oidInfo->pwszName;
403 prefixA = rdn->rgRDNAttr[j].pszObjId;
405 if (dwStrType & CERT_NAME_STR_CRLF_FLAG)
409 for (k = 0; k < indentLevel; k++)
413 chars = min(strlenW(indent), csz - ret - 1);
414 memcpy(psz + ret, indent, chars * sizeof(WCHAR));
417 chars = strlenW(indent);
423 /* - 1 is needed to account for the NULL terminator. */
424 chars = CRYPT_AddPrefixW(prefixW,
425 psz ? psz + ret : NULL, psz ? csz - ret - 1 : 0);
430 /* - 1 is needed to account for the NULL terminator. */
431 chars = CRYPT_AddPrefixAToW(prefixA,
432 psz ? psz + ret : NULL, psz ? csz - ret - 1 : 0);
435 /* FIXME: handle quoting */
436 chars = CertRDNValueToStrW(
437 rdn->rgRDNAttr[j].dwValueType,
438 &rdn->rgRDNAttr[j].Value, psz ? psz + ret : NULL,
439 psz ? csz - ret : 0);
442 if (j < rdn->cRDNAttr - 1)
444 if (psz && ret < csz - rdnSepLen - 1)
445 memcpy(psz + ret, rdnSep, rdnSepLen * sizeof(WCHAR));
449 if (i < info->cRDN - 1)
451 if (psz && ret < csz - sepLen - 1)
452 memcpy(psz + ret, sep, sepLen * sizeof(WCHAR));
470 DWORD WINAPI CertNameToStrW(DWORD dwCertEncodingType, PCERT_NAME_BLOB pName,
471 DWORD dwStrType, LPWSTR psz, DWORD csz)
475 TRACE("(%d, %p, %08x, %p, %d)\n", dwCertEncodingType, pName, dwStrType,
478 ret = cert_name_to_str_with_indent(dwCertEncodingType, 0, pName, dwStrType,
480 TRACE("Returning %s\n", debugstr_w(psz));
484 BOOL WINAPI CertStrToNameA(DWORD dwCertEncodingType, LPCSTR pszX500,
485 DWORD dwStrType, void *pvReserved, BYTE *pbEncoded, DWORD *pcbEncoded,
491 TRACE("(%08x, %s, %08x, %p, %p, %p, %p)\n", dwCertEncodingType,
492 debugstr_a(pszX500), dwStrType, pvReserved, pbEncoded, pcbEncoded,
495 len = MultiByteToWideChar(CP_ACP, 0, pszX500, -1, NULL, 0);
498 LPWSTR x500, errorStr;
500 if ((x500 = CryptMemAlloc(len * sizeof(WCHAR))))
502 MultiByteToWideChar(CP_ACP, 0, pszX500, -1, x500, len);
503 ret = CertStrToNameW(dwCertEncodingType, x500, dwStrType,
504 pvReserved, pbEncoded, pcbEncoded,
505 ppszError ? (LPCWSTR *)&errorStr : NULL);
512 *ppszError = pszX500;
513 for (i = 0; i < errorStr - x500; i++)
514 *ppszError = CharNextA(*ppszError);
523 SetLastError(ERROR_OUTOFMEMORY);
529 SetLastError(CRYPT_E_INVALID_X500_STRING);
531 *ppszError = pszX500;
539 WCHAR buf[10]; /* big enough for L"GivenName" */
540 LPWSTR keyName; /* usually = buf, but may be allocated */
544 static void CRYPT_InitializeKeynameKeeper(struct KeynameKeeper *keeper)
546 keeper->keyName = keeper->buf;
547 keeper->keyLen = sizeof(keeper->buf) / sizeof(keeper->buf[0]);
550 static void CRYPT_FreeKeynameKeeper(struct KeynameKeeper *keeper)
552 if (keeper->keyName != keeper->buf)
553 CryptMemFree(keeper->keyName);
562 static void CRYPT_KeynameKeeperFromTokenW(struct KeynameKeeper *keeper,
563 const struct X500TokenW *key)
565 DWORD len = key->end - key->start;
567 if (len > keeper->keyLen)
569 if (keeper->keyName == keeper->buf)
570 keeper->keyName = CryptMemAlloc(len * sizeof(WCHAR));
572 keeper->keyName = CryptMemRealloc(keeper->keyName,
573 len * sizeof(WCHAR));
574 keeper->keyLen = len;
576 memcpy(keeper->keyName, key->start, (key->end - key->start) *
578 keeper->keyName[len] = '\0';
579 TRACE("Keyname is %s\n", debugstr_w(keeper->keyName));
582 static BOOL CRYPT_GetNextKeyW(LPCWSTR str, struct X500TokenW *token,
587 while (*str && isspaceW(*str))
592 while (*str && *str != '=' && !isspaceW(*str))
594 if (*str && (*str == '=' || isspaceW(*str)))
598 TRACE("missing equals char at %s\n", debugstr_w(token->start));
600 *ppszError = token->start;
601 SetLastError(CRYPT_E_INVALID_X500_STRING);
610 /* Assumes separators are characters in the 0-255 range */
611 static BOOL CRYPT_GetNextValueW(LPCWSTR str, DWORD dwFlags, LPCWSTR separators,
612 struct X500TokenW *token, LPCWSTR *ppszError)
616 TRACE("(%s, %s, %p, %p)\n", debugstr_w(str), debugstr_w(separators), token,
619 while (*str && isspaceW(*str))
624 if (!(dwFlags & CERT_NAME_STR_NO_QUOTING_FLAG) && *str == '"')
628 while (!token->end && ret)
630 while (*str && *str != '"')
634 if (*(str + 1) != '"')
635 token->end = str + 1;
641 TRACE("unterminated quote at %s\n", debugstr_w(str));
644 SetLastError(CRYPT_E_INVALID_X500_STRING);
651 WCHAR map[256] = { 0 };
654 map[*separators++] = 1;
655 while (*str && (*str >= 0xff || !map[*str]))
662 TRACE("missing value at %s\n", debugstr_w(str));
665 SetLastError(CRYPT_E_INVALID_X500_STRING);
671 /* Encodes the string represented by value as the string type type into the
672 * CERT_NAME_BLOB output. If there is an error and ppszError is not NULL,
673 * *ppszError is set to the first failing character. If there is no error,
674 * output's pbData must be freed with LocalFree.
676 static BOOL CRYPT_EncodeValueWithType(DWORD dwCertEncodingType,
677 const struct X500TokenW *value, PCERT_NAME_BLOB output, DWORD type,
680 CERT_NAME_VALUE nameValue = { type, { 0, NULL } };
683 if (value->end > value->start)
685 nameValue.Value.pbData = CryptMemAlloc((value->end - value->start) *
687 if (!nameValue.Value.pbData)
689 SetLastError(ERROR_OUTOFMEMORY);
695 if (value->end > value->start)
698 LPWSTR ptr = (LPWSTR)nameValue.Value.pbData;
700 for (i = 0; i < value->end - value->start; i++)
702 *ptr++ = value->start[i];
703 if (value->start[i] == '"')
706 nameValue.Value.cbData = (LPBYTE)ptr - nameValue.Value.pbData;
708 ret = CryptEncodeObjectEx(dwCertEncodingType, X509_UNICODE_NAME_VALUE,
709 &nameValue, CRYPT_ENCODE_ALLOC_FLAG, NULL, &output->pbData,
711 if (!ret && ppszError)
713 if (type == CERT_RDN_NUMERIC_STRING &&
714 GetLastError() == CRYPT_E_INVALID_NUMERIC_STRING)
715 *ppszError = value->start + output->cbData;
716 else if (type == CERT_RDN_PRINTABLE_STRING &&
717 GetLastError() == CRYPT_E_INVALID_PRINTABLE_STRING)
718 *ppszError = value->start + output->cbData;
719 else if (type == CERT_RDN_IA5_STRING &&
720 GetLastError() == CRYPT_E_INVALID_IA5_STRING)
721 *ppszError = value->start + output->cbData;
723 CryptMemFree(nameValue.Value.pbData);
728 static BOOL CRYPT_EncodeValue(DWORD dwCertEncodingType,
729 const struct X500TokenW *value, PCERT_NAME_BLOB output, const DWORD *types,
736 for (i = 0; !ret && types[i]; i++)
737 ret = CRYPT_EncodeValueWithType(dwCertEncodingType, value, output,
738 types[i], ppszError);
742 static BOOL CRYPT_ValueToRDN(DWORD dwCertEncodingType, PCERT_NAME_INFO info,
743 PCCRYPT_OID_INFO keyOID, struct X500TokenW *value, LPCWSTR *ppszError)
747 TRACE("OID %s, value %s\n", debugstr_a(keyOID->pszOID),
748 debugstr_wn(value->start, value->end - value->start));
751 info->rgRDN = CryptMemAlloc(sizeof(CERT_RDN));
753 info->rgRDN = CryptMemRealloc(info->rgRDN,
754 (info->cRDN + 1) * sizeof(CERT_RDN));
757 /* FIXME: support multiple RDN attrs */
758 info->rgRDN[info->cRDN].rgRDNAttr =
759 CryptMemAlloc(sizeof(CERT_RDN_ATTR));
760 if (info->rgRDN[info->cRDN].rgRDNAttr)
762 static const DWORD defaultTypes[] = { CERT_RDN_PRINTABLE_STRING,
763 CERT_RDN_BMP_STRING, 0 };
766 info->rgRDN[info->cRDN].cRDNAttr = 1;
767 info->rgRDN[info->cRDN].rgRDNAttr[0].pszObjId =
768 (LPSTR)keyOID->pszOID;
769 info->rgRDN[info->cRDN].rgRDNAttr[0].dwValueType =
770 CERT_RDN_ENCODED_BLOB;
771 if (keyOID->ExtraInfo.cbData)
772 types = (const DWORD *)keyOID->ExtraInfo.pbData;
774 types = defaultTypes;
776 /* Remove surrounding quotes */
777 if (value->start[0] == '"')
782 ret = CRYPT_EncodeValue(dwCertEncodingType, value,
783 &info->rgRDN[info->cRDN].rgRDNAttr[0].Value, types, ppszError);
786 SetLastError(ERROR_OUTOFMEMORY);
790 SetLastError(ERROR_OUTOFMEMORY);
794 BOOL WINAPI CertStrToNameW(DWORD dwCertEncodingType, LPCWSTR pszX500,
795 DWORD dwStrType, void *pvReserved, BYTE *pbEncoded, DWORD *pcbEncoded,
798 CERT_NAME_INFO info = { 0, NULL };
800 struct KeynameKeeper keeper;
804 TRACE("(%08x, %s, %08x, %p, %p, %p, %p)\n", dwCertEncodingType,
805 debugstr_w(pszX500), dwStrType, pvReserved, pbEncoded, pcbEncoded,
808 CRYPT_InitializeKeynameKeeper(&keeper);
810 while (str && *str && ret)
812 struct X500TokenW token;
814 ret = CRYPT_GetNextKeyW(str, &token, ppszError);
815 if (ret && token.start)
817 PCCRYPT_OID_INFO keyOID;
819 CRYPT_KeynameKeeperFromTokenW(&keeper, &token);
820 keyOID = CryptFindOIDInfo(CRYPT_OID_INFO_NAME_KEY, keeper.keyName,
821 CRYPT_RDN_ATTR_OID_GROUP_ID);
825 *ppszError = token.start;
826 SetLastError(CRYPT_E_INVALID_X500_STRING);
832 while (isspace(*str))
838 SetLastError(CRYPT_E_INVALID_X500_STRING);
843 static const WCHAR commaSep[] = { ',',0 };
844 static const WCHAR semiSep[] = { ';',0 };
845 static const WCHAR crlfSep[] = { '\r','\n',0 };
846 static const WCHAR allSeps[] = { ',',';','\r','\n',0 };
850 if (dwStrType & CERT_NAME_STR_COMMA_FLAG)
852 else if (dwStrType & CERT_NAME_STR_SEMICOLON_FLAG)
854 else if (dwStrType & CERT_NAME_STR_CRLF_FLAG)
858 ret = CRYPT_GetNextValueW(str, dwStrType, sep, &token,
863 ret = CRYPT_ValueToRDN(dwCertEncodingType, &info,
864 keyOID, &token, ppszError);
870 CRYPT_FreeKeynameKeeper(&keeper);
875 ret = CryptEncodeObjectEx(dwCertEncodingType, X509_NAME, &info,
876 0, NULL, pbEncoded, pcbEncoded);
878 for (i = 0; i < info.cRDN; i++)
882 for (j = 0; j < info.rgRDN[i].cRDNAttr; j++)
883 LocalFree(info.rgRDN[i].rgRDNAttr[j].Value.pbData);
884 CryptMemFree(info.rgRDN[i].rgRDNAttr);
886 CryptMemFree(info.rgRDN);
890 DWORD WINAPI CertGetNameStringA(PCCERT_CONTEXT pCertContext, DWORD dwType,
891 DWORD dwFlags, void *pvTypePara, LPSTR pszNameString, DWORD cchNameString)
895 TRACE("(%p, %d, %08x, %p, %p, %d)\n", pCertContext, dwType, dwFlags,
896 pvTypePara, pszNameString, cchNameString);
903 nameLen = CertGetNameStringW(pCertContext, dwType, dwFlags, pvTypePara,
905 wideName = CryptMemAlloc(nameLen * sizeof(WCHAR));
908 CertGetNameStringW(pCertContext, dwType, dwFlags, pvTypePara,
910 nameLen = WideCharToMultiByte(CP_ACP, 0, wideName, nameLen,
911 pszNameString, cchNameString, NULL, NULL);
912 if (nameLen <= cchNameString)
916 pszNameString[cchNameString - 1] = '\0';
919 CryptMemFree(wideName);
923 *pszNameString = '\0';
928 ret = CertGetNameStringW(pCertContext, dwType, dwFlags, pvTypePara,
933 /* Searches cert's extensions for the alternate name extension with OID
934 * altNameOID, and if found, searches it for the alternate name type entryType.
935 * If found, returns a pointer to the entry, otherwise returns NULL.
936 * Regardless of whether an entry of the desired type is found, if the
937 * alternate name extension is present, sets *info to the decoded alternate
938 * name extension, which you must free using LocalFree.
939 * The return value is a pointer within *info, so don't free *info before
940 * you're done with the return value.
942 static PCERT_ALT_NAME_ENTRY cert_find_alt_name_entry(PCCERT_CONTEXT cert,
943 LPCSTR altNameOID, DWORD entryType, PCERT_ALT_NAME_INFO *info)
945 PCERT_ALT_NAME_ENTRY entry = NULL;
946 PCERT_EXTENSION ext = CertFindExtension(altNameOID,
947 cert->pCertInfo->cExtension, cert->pCertInfo->rgExtension);
953 if (CryptDecodeObjectEx(cert->dwCertEncodingType, X509_ALTERNATE_NAME,
954 ext->Value.pbData, ext->Value.cbData, CRYPT_DECODE_ALLOC_FLAG, NULL,
959 for (i = 0; !entry && i < (*info)->cAltEntry; i++)
960 if ((*info)->rgAltEntry[i].dwAltNameChoice == entryType)
961 entry = &(*info)->rgAltEntry[i];
969 static DWORD cert_get_name_from_rdn_attr(DWORD encodingType,
970 PCERT_NAME_BLOB name, LPCSTR oid, LPWSTR pszNameString, DWORD cchNameString)
972 CERT_NAME_INFO *nameInfo;
973 DWORD bytes = 0, ret = 0;
975 if (CryptDecodeObjectEx(encodingType, X509_NAME, name->pbData,
976 name->cbData, CRYPT_DECODE_ALLOC_FLAG, NULL, &nameInfo, &bytes))
978 PCERT_RDN_ATTR nameAttr = CertFindRDNAttr(oid, nameInfo);
981 ret = CertRDNValueToStrW(nameAttr->dwValueType, &nameAttr->Value,
982 pszNameString, cchNameString);
988 DWORD WINAPI CertGetNameStringW(PCCERT_CONTEXT pCertContext, DWORD dwType,
989 DWORD dwFlags, void *pvTypePara, LPWSTR pszNameString, DWORD cchNameString)
992 PCERT_NAME_BLOB name;
995 TRACE("(%p, %d, %08x, %p, %p, %d)\n", pCertContext, dwType,
996 dwFlags, pvTypePara, pszNameString, cchNameString);
998 if (dwFlags & CERT_NAME_ISSUER_FLAG)
1000 name = &pCertContext->pCertInfo->Issuer;
1001 altNameOID = szOID_ISSUER_ALT_NAME;
1005 name = &pCertContext->pCertInfo->Subject;
1006 altNameOID = szOID_SUBJECT_ALT_NAME;
1011 case CERT_NAME_EMAIL_TYPE:
1013 CERT_ALT_NAME_INFO *info;
1014 PCERT_ALT_NAME_ENTRY entry = cert_find_alt_name_entry(pCertContext,
1015 altNameOID, CERT_ALT_NAME_RFC822_NAME, &info);
1020 ret = strlenW(entry->pwszRfc822Name) + 1;
1021 else if (cchNameString)
1023 ret = min(strlenW(entry->pwszRfc822Name), cchNameString - 1);
1024 memcpy(pszNameString, entry->pwszRfc822Name,
1025 ret * sizeof(WCHAR));
1026 pszNameString[ret++] = 0;
1032 ret = cert_get_name_from_rdn_attr(pCertContext->dwCertEncodingType,
1033 name, szOID_RSA_emailAddr, pszNameString, cchNameString);
1036 case CERT_NAME_RDN_TYPE:
1038 ret = CertNameToStrW(pCertContext->dwCertEncodingType, name,
1039 *(DWORD *)pvTypePara, pszNameString, cchNameString);
1042 CERT_ALT_NAME_INFO *info;
1043 PCERT_ALT_NAME_ENTRY entry = cert_find_alt_name_entry(pCertContext,
1044 altNameOID, CERT_ALT_NAME_DIRECTORY_NAME, &info);
1047 ret = CertNameToStrW(pCertContext->dwCertEncodingType,
1048 &entry->DirectoryName, *(DWORD *)pvTypePara, pszNameString,
1054 case CERT_NAME_ATTR_TYPE:
1055 ret = cert_get_name_from_rdn_attr(pCertContext->dwCertEncodingType,
1056 name, pvTypePara, pszNameString, cchNameString);
1059 CERT_ALT_NAME_INFO *altInfo;
1060 PCERT_ALT_NAME_ENTRY entry = cert_find_alt_name_entry(pCertContext,
1061 altNameOID, CERT_ALT_NAME_DIRECTORY_NAME, &altInfo);
1064 ret = cert_name_to_str_with_indent(X509_ASN_ENCODING, 0,
1065 &entry->DirectoryName, 0, pszNameString, cchNameString);
1070 case CERT_NAME_SIMPLE_DISPLAY_TYPE:
1072 static const LPCSTR simpleAttributeOIDs[] = { szOID_COMMON_NAME,
1073 szOID_ORGANIZATIONAL_UNIT_NAME, szOID_ORGANIZATION_NAME,
1074 szOID_RSA_emailAddr };
1075 CERT_NAME_INFO *nameInfo = NULL;
1078 if (CryptDecodeObjectEx(pCertContext->dwCertEncodingType, X509_NAME,
1079 name->pbData, name->cbData, CRYPT_DECODE_ALLOC_FLAG, NULL, &nameInfo,
1082 PCERT_RDN_ATTR nameAttr = NULL;
1084 for (i = 0; !nameAttr && i < sizeof(simpleAttributeOIDs) /
1085 sizeof(simpleAttributeOIDs[0]); i++)
1086 nameAttr = CertFindRDNAttr(simpleAttributeOIDs[i], nameInfo);
1088 ret = CertRDNValueToStrW(nameAttr->dwValueType,
1089 &nameAttr->Value, pszNameString, cchNameString);
1090 LocalFree(nameInfo);
1094 CERT_ALT_NAME_INFO *altInfo;
1095 PCERT_ALT_NAME_ENTRY entry = cert_find_alt_name_entry(pCertContext,
1096 altNameOID, CERT_ALT_NAME_RFC822_NAME, &altInfo);
1100 if (!entry && altInfo->cAltEntry)
1101 entry = &altInfo->rgAltEntry[0];
1105 ret = strlenW(entry->pwszRfc822Name) + 1;
1106 else if (cchNameString)
1108 ret = min(strlenW(entry->pwszRfc822Name),
1110 memcpy(pszNameString, entry->pwszRfc822Name,
1111 ret * sizeof(WCHAR));
1112 pszNameString[ret++] = 0;
1120 case CERT_NAME_FRIENDLY_DISPLAY_TYPE:
1122 DWORD cch = cchNameString;
1124 if (CertGetCertificateContextProperty(pCertContext,
1125 CERT_FRIENDLY_NAME_PROP_ID, pszNameString, &cch))
1128 ret = CertGetNameStringW(pCertContext,
1129 CERT_NAME_SIMPLE_DISPLAY_TYPE, dwFlags, pvTypePara, pszNameString,
1133 case CERT_NAME_DNS_TYPE:
1135 CERT_ALT_NAME_INFO *info;
1136 PCERT_ALT_NAME_ENTRY entry = cert_find_alt_name_entry(pCertContext,
1137 altNameOID, CERT_ALT_NAME_DNS_NAME, &info);
1142 ret = strlenW(entry->pwszDNSName) + 1;
1143 else if (cchNameString)
1145 ret = min(strlenW(entry->pwszDNSName), cchNameString - 1);
1146 memcpy(pszNameString, entry->pwszDNSName, ret * sizeof(WCHAR));
1147 pszNameString[ret++] = 0;
1153 ret = cert_get_name_from_rdn_attr(pCertContext->dwCertEncodingType,
1154 name, szOID_COMMON_NAME, pszNameString, cchNameString);
1157 case CERT_NAME_URL_TYPE:
1159 CERT_ALT_NAME_INFO *info;
1160 PCERT_ALT_NAME_ENTRY entry = cert_find_alt_name_entry(pCertContext,
1161 altNameOID, CERT_ALT_NAME_URL, &info);
1166 ret = strlenW(entry->pwszURL) + 1;
1167 else if (cchNameString)
1169 ret = min(strlenW(entry->pwszURL), cchNameString - 1);
1170 memcpy(pszNameString, entry->pwszURL, ret * sizeof(WCHAR));
1171 pszNameString[ret++] = 0;
1179 FIXME("unimplemented for type %d\n", dwType);
1186 else if (cchNameString)
1188 pszNameString[0] = 0;