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
20 #define NONAMELESSUNION
27 #include "wine/debug.h"
28 #include "wine/unicode.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(crypt);
32 DWORD WINAPI CertRDNValueToStrA(DWORD dwValueType, PCERT_RDN_VALUE_BLOB pValue,
37 TRACE("(%d, %p, %p, %d)\n", dwValueType, pValue, psz, csz);
41 case CERT_RDN_ANY_TYPE:
43 case CERT_RDN_NUMERIC_STRING:
44 case CERT_RDN_PRINTABLE_STRING:
45 case CERT_RDN_TELETEX_STRING:
46 case CERT_RDN_VIDEOTEX_STRING:
47 case CERT_RDN_IA5_STRING:
48 case CERT_RDN_GRAPHIC_STRING:
49 case CERT_RDN_VISIBLE_STRING:
50 case CERT_RDN_GENERAL_STRING:
55 DWORD chars = min(pValue->cbData, csz - 1);
59 memcpy(psz, pValue->pbData, chars);
65 case CERT_RDN_UTF8_STRING:
67 ret = WideCharToMultiByte(CP_UTF8, 0, (LPWSTR)pValue->pbData,
68 pValue->cbData / sizeof(WCHAR) + 1, NULL, 0, NULL, NULL);
71 ret = WideCharToMultiByte(CP_UTF8, 0, (LPWSTR)pValue->pbData,
72 pValue->cbData / sizeof(WCHAR) + 1, psz, csz - 1, NULL, NULL);
77 FIXME("string type %d unimplemented\n", dwValueType);
87 TRACE("returning %d (%s)\n", ret, debugstr_a(psz));
91 DWORD WINAPI CertRDNValueToStrW(DWORD dwValueType, PCERT_RDN_VALUE_BLOB pValue,
92 LPWSTR psz, DWORD csz)
96 TRACE("(%d, %p, %p, %d)\n", dwValueType, pValue, psz, csz);
100 case CERT_RDN_ANY_TYPE:
102 case CERT_RDN_NUMERIC_STRING:
103 case CERT_RDN_PRINTABLE_STRING:
104 case CERT_RDN_TELETEX_STRING:
105 case CERT_RDN_VIDEOTEX_STRING:
106 case CERT_RDN_IA5_STRING:
107 case CERT_RDN_GRAPHIC_STRING:
108 case CERT_RDN_VISIBLE_STRING:
109 case CERT_RDN_GENERAL_STRING:
111 ret = pValue->cbData;
114 DWORD chars = min(pValue->cbData, csz - 1);
120 for (i = 0; i < chars; i++)
121 psz[i] = pValue->pbData[i];
127 case CERT_RDN_UTF8_STRING:
129 ret = pValue->cbData / sizeof(WCHAR);
132 DWORD chars = min(pValue->cbData / sizeof(WCHAR), csz - 1);
138 for (i = 0; i < chars; i++)
139 psz[i] = *((LPWSTR)pValue->pbData + i);
146 FIXME("string type %d unimplemented\n", dwValueType);
156 TRACE("returning %d (%s)\n", ret, debugstr_w(psz));
160 /* Adds the prefix prefix to the string pointed to by psz, followed by the
161 * character '='. Copies no more than csz characters. Returns the number of
162 * characters copied. If psz is NULL, returns the number of characters that
165 static DWORD CRYPT_AddPrefixA(LPCSTR prefix, LPSTR psz, DWORD csz)
169 TRACE("(%s, %p, %d)\n", debugstr_a(prefix), psz, csz);
173 chars = min(strlen(prefix), csz);
174 memcpy(psz, prefix, chars);
175 *(psz + chars) = '=';
179 chars = lstrlenA(prefix) + 1;
183 DWORD WINAPI CertNameToStrA(DWORD dwCertEncodingType, PCERT_NAME_BLOB pName,
184 DWORD dwStrType, LPSTR psz, DWORD csz)
186 static const DWORD unsupportedFlags = CERT_NAME_STR_NO_QUOTING_FLAG |
187 CERT_NAME_STR_ENABLE_T61_UNICODE_FLAG;
188 static const char commaSep[] = ", ";
189 static const char semiSep[] = "; ";
190 static const char crlfSep[] = "\r\n";
191 static const char plusSep[] = " + ";
192 static const char spaceSep[] = " ";
193 DWORD ret = 0, bytes = 0;
195 CERT_NAME_INFO *info;
197 TRACE("(%d, %p, %08x, %p, %d)\n", dwCertEncodingType, pName, dwStrType,
199 if (dwStrType & unsupportedFlags)
200 FIXME("unsupported flags: %08x\n", dwStrType & unsupportedFlags);
202 bRet = CryptDecodeObjectEx(dwCertEncodingType, X509_NAME, pName->pbData,
203 pName->cbData, CRYPT_DECODE_ALLOC_FLAG, NULL, &info, &bytes);
206 DWORD i, j, sepLen, rdnSepLen;
208 BOOL reverse = dwStrType & CERT_NAME_STR_REVERSE_FLAG;
209 const CERT_RDN *rdn = info->rgRDN;
211 if(reverse && info->cRDN > 1) rdn += (info->cRDN - 1);
213 if (dwStrType & CERT_NAME_STR_SEMICOLON_FLAG)
215 else if (dwStrType & CERT_NAME_STR_CRLF_FLAG)
219 sepLen = strlen(sep);
220 if (dwStrType & CERT_NAME_STR_NO_PLUS_FLAG)
224 rdnSepLen = strlen(rdnSep);
225 for (i = 0; (!psz || ret < csz) && i < info->cRDN; i++)
227 for (j = 0; (!psz || ret < csz) && j < rdn->cRDNAttr; j++)
230 char prefixBuf[10]; /* big enough for GivenName */
231 LPCSTR prefix = NULL;
233 if ((dwStrType & 0x000000ff) == CERT_OID_NAME_STR)
234 prefix = rdn->rgRDNAttr[j].pszObjId;
235 else if ((dwStrType & 0x000000ff) == CERT_X500_NAME_STR)
237 PCCRYPT_OID_INFO oidInfo = CryptFindOIDInfo(
238 CRYPT_OID_INFO_OID_KEY,
239 rdn->rgRDNAttr[j].pszObjId,
240 CRYPT_RDN_ATTR_OID_GROUP_ID);
244 WideCharToMultiByte(CP_ACP, 0, oidInfo->pwszName, -1,
245 prefixBuf, sizeof(prefixBuf), NULL, NULL);
249 prefix = rdn->rgRDNAttr[j].pszObjId;
253 /* - 1 is needed to account for the NULL terminator. */
254 chars = CRYPT_AddPrefixA(prefix,
255 psz ? psz + ret : NULL, psz ? csz - ret - 1 : 0);
258 /* FIXME: handle quoting */
259 chars = CertRDNValueToStrA(
260 rdn->rgRDNAttr[j].dwValueType,
261 &rdn->rgRDNAttr[j].Value, psz ? psz + ret : NULL,
262 psz ? csz - ret : 0);
265 if (j < rdn->cRDNAttr - 1)
267 if (psz && ret < csz - rdnSepLen - 1)
268 memcpy(psz + ret, rdnSep, rdnSepLen);
272 if (i < info->cRDN - 1)
274 if (psz && ret < csz - sepLen - 1)
275 memcpy(psz + ret, sep, sepLen);
290 TRACE("Returning %s\n", debugstr_a(psz));
294 /* Adds the prefix prefix to the wide-character string pointed to by psz,
295 * followed by the character '='. Copies no more than csz characters. Returns
296 * the number of characters copied. If psz is NULL, returns the number of
297 * characters that would be copied.
298 * Assumes the characters in prefix are ASCII (not multibyte characters.)
300 static DWORD CRYPT_AddPrefixAToW(LPCSTR prefix, LPWSTR psz, DWORD csz)
304 TRACE("(%s, %p, %d)\n", debugstr_a(prefix), psz, csz);
310 chars = min(strlen(prefix), csz);
311 for (i = 0; i < chars; i++)
312 *(psz + i) = prefix[i];
313 *(psz + chars) = '=';
317 chars = lstrlenA(prefix) + 1;
321 /* Adds the prefix prefix to the string pointed to by psz, followed by the
322 * character '='. Copies no more than csz characters. Returns the number of
323 * characters copied. If psz is NULL, returns the number of characters that
326 static DWORD CRYPT_AddPrefixW(LPCWSTR prefix, LPWSTR psz, DWORD csz)
330 TRACE("(%s, %p, %d)\n", debugstr_w(prefix), psz, csz);
334 chars = min(strlenW(prefix), csz);
335 memcpy(psz, prefix, chars * sizeof(WCHAR));
336 *(psz + chars) = '=';
340 chars = lstrlenW(prefix) + 1;
344 static const WCHAR indent[] = { ' ',' ',' ',' ',' ',0 };
346 DWORD cert_name_to_str_with_indent(DWORD dwCertEncodingType, DWORD indentLevel,
347 const CERT_NAME_BLOB *pName, DWORD dwStrType, LPWSTR psz, DWORD csz)
349 static const DWORD unsupportedFlags = CERT_NAME_STR_NO_QUOTING_FLAG |
350 CERT_NAME_STR_ENABLE_T61_UNICODE_FLAG;
351 static const WCHAR commaSep[] = { ',',' ',0 };
352 static const WCHAR semiSep[] = { ';',' ',0 };
353 static const WCHAR crlfSep[] = { '\r','\n',0 };
354 static const WCHAR plusSep[] = { ' ','+',' ',0 };
355 static const WCHAR spaceSep[] = { ' ',0 };
356 DWORD ret = 0, bytes = 0;
358 CERT_NAME_INFO *info;
360 if (dwStrType & unsupportedFlags)
361 FIXME("unsupported flags: %08x\n", dwStrType & unsupportedFlags);
363 bRet = CryptDecodeObjectEx(dwCertEncodingType, X509_NAME, pName->pbData,
364 pName->cbData, CRYPT_DECODE_ALLOC_FLAG, NULL, &info, &bytes);
367 DWORD i, j, sepLen, rdnSepLen;
369 BOOL reverse = dwStrType & CERT_NAME_STR_REVERSE_FLAG;
370 const CERT_RDN *rdn = info->rgRDN;
372 if(reverse && info->cRDN > 1) rdn += (info->cRDN - 1);
374 if (dwStrType & CERT_NAME_STR_SEMICOLON_FLAG)
376 else if (dwStrType & CERT_NAME_STR_CRLF_FLAG)
380 sepLen = lstrlenW(sep);
381 if (dwStrType & CERT_NAME_STR_NO_PLUS_FLAG)
385 rdnSepLen = lstrlenW(rdnSep);
386 for (i = 0; (!psz || ret < csz) && i < info->cRDN; i++)
388 for (j = 0; (!psz || ret < csz) && j < rdn->cRDNAttr; j++)
391 LPCSTR prefixA = NULL;
392 LPCWSTR prefixW = NULL;
394 if ((dwStrType & 0x000000ff) == CERT_OID_NAME_STR)
395 prefixA = rdn->rgRDNAttr[j].pszObjId;
396 else if ((dwStrType & 0x000000ff) == CERT_X500_NAME_STR)
398 PCCRYPT_OID_INFO oidInfo = CryptFindOIDInfo(
399 CRYPT_OID_INFO_OID_KEY,
400 rdn->rgRDNAttr[j].pszObjId,
401 CRYPT_RDN_ATTR_OID_GROUP_ID);
404 prefixW = oidInfo->pwszName;
406 prefixA = rdn->rgRDNAttr[j].pszObjId;
408 if (dwStrType & CERT_NAME_STR_CRLF_FLAG)
412 for (k = 0; k < indentLevel; k++)
416 chars = min(strlenW(indent), csz - ret - 1);
417 memcpy(psz + ret, indent, chars * sizeof(WCHAR));
420 chars = strlenW(indent);
426 /* - 1 is needed to account for the NULL terminator. */
427 chars = CRYPT_AddPrefixW(prefixW,
428 psz ? psz + ret : NULL, psz ? csz - ret - 1 : 0);
433 /* - 1 is needed to account for the NULL terminator. */
434 chars = CRYPT_AddPrefixAToW(prefixA,
435 psz ? psz + ret : NULL, psz ? csz - ret - 1 : 0);
438 /* FIXME: handle quoting */
439 chars = CertRDNValueToStrW(
440 rdn->rgRDNAttr[j].dwValueType,
441 &rdn->rgRDNAttr[j].Value, psz ? psz + ret : NULL,
442 psz ? csz - ret : 0);
445 if (j < rdn->cRDNAttr - 1)
447 if (psz && ret < csz - rdnSepLen - 1)
448 memcpy(psz + ret, rdnSep, rdnSepLen * sizeof(WCHAR));
452 if (i < info->cRDN - 1)
454 if (psz && ret < csz - sepLen - 1)
455 memcpy(psz + ret, sep, sepLen * sizeof(WCHAR));
473 DWORD WINAPI CertNameToStrW(DWORD dwCertEncodingType, PCERT_NAME_BLOB pName,
474 DWORD dwStrType, LPWSTR psz, DWORD csz)
478 TRACE("(%d, %p, %08x, %p, %d)\n", dwCertEncodingType, pName, dwStrType,
481 ret = cert_name_to_str_with_indent(dwCertEncodingType, 0, pName, dwStrType,
483 TRACE("Returning %s\n", debugstr_w(psz));
487 BOOL WINAPI CertStrToNameA(DWORD dwCertEncodingType, LPCSTR pszX500,
488 DWORD dwStrType, void *pvReserved, BYTE *pbEncoded, DWORD *pcbEncoded,
494 TRACE("(%08x, %s, %08x, %p, %p, %p, %p)\n", dwCertEncodingType,
495 debugstr_a(pszX500), dwStrType, pvReserved, pbEncoded, pcbEncoded,
498 len = MultiByteToWideChar(CP_ACP, 0, pszX500, -1, NULL, 0);
501 LPWSTR x500, errorStr;
503 if ((x500 = CryptMemAlloc(len * sizeof(WCHAR))))
505 MultiByteToWideChar(CP_ACP, 0, pszX500, -1, x500, len);
506 ret = CertStrToNameW(dwCertEncodingType, x500, dwStrType,
507 pvReserved, pbEncoded, pcbEncoded,
508 ppszError ? (LPCWSTR *)&errorStr : NULL);
515 *ppszError = pszX500;
516 for (i = 0; i < errorStr - x500; i++)
517 *ppszError = CharNextA(*ppszError);
526 SetLastError(ERROR_OUTOFMEMORY);
532 SetLastError(CRYPT_E_INVALID_X500_STRING);
534 *ppszError = pszX500;
542 WCHAR buf[10]; /* big enough for L"GivenName" */
543 LPWSTR keyName; /* usually = buf, but may be allocated */
547 static void CRYPT_InitializeKeynameKeeper(struct KeynameKeeper *keeper)
549 keeper->keyName = keeper->buf;
550 keeper->keyLen = sizeof(keeper->buf) / sizeof(keeper->buf[0]);
553 static void CRYPT_FreeKeynameKeeper(struct KeynameKeeper *keeper)
555 if (keeper->keyName != keeper->buf)
556 CryptMemFree(keeper->keyName);
565 static void CRYPT_KeynameKeeperFromTokenW(struct KeynameKeeper *keeper,
566 const struct X500TokenW *key)
568 DWORD len = key->end - key->start;
570 if (len > keeper->keyLen)
572 if (keeper->keyName == keeper->buf)
573 keeper->keyName = CryptMemAlloc(len * sizeof(WCHAR));
575 keeper->keyName = CryptMemRealloc(keeper->keyName,
576 len * sizeof(WCHAR));
577 keeper->keyLen = len;
579 memcpy(keeper->keyName, key->start, (key->end - key->start) *
581 keeper->keyName[len] = '\0';
582 TRACE("Keyname is %s\n", debugstr_w(keeper->keyName));
585 static BOOL CRYPT_GetNextKeyW(LPCWSTR str, struct X500TokenW *token,
590 while (*str && isspaceW(*str))
595 while (*str && *str != '=' && !isspaceW(*str))
597 if (*str && (*str == '=' || isspaceW(*str)))
601 TRACE("missing equals char at %s\n", debugstr_w(token->start));
603 *ppszError = token->start;
604 SetLastError(CRYPT_E_INVALID_X500_STRING);
613 /* Assumes separators are characters in the 0-255 range */
614 static BOOL CRYPT_GetNextValueW(LPCWSTR str, DWORD dwFlags, LPCWSTR separators,
615 struct X500TokenW *token, LPCWSTR *ppszError)
619 TRACE("(%s, %s, %p, %p)\n", debugstr_w(str), debugstr_w(separators), token,
622 while (*str && isspaceW(*str))
627 if (!(dwFlags & CERT_NAME_STR_NO_QUOTING_FLAG) && *str == '"')
631 while (!token->end && ret)
633 while (*str && *str != '"')
637 if (*(str + 1) != '"')
638 token->end = str + 1;
644 TRACE("unterminated quote at %s\n", debugstr_w(str));
647 SetLastError(CRYPT_E_INVALID_X500_STRING);
654 WCHAR map[256] = { 0 };
657 map[*separators++] = 1;
658 while (*str && (*str >= 0xff || !map[*str]))
665 TRACE("missing value at %s\n", debugstr_w(str));
668 SetLastError(CRYPT_E_INVALID_X500_STRING);
674 /* Encodes the string represented by value as the string type type into the
675 * CERT_NAME_BLOB output. If there is an error and ppszError is not NULL,
676 * *ppszError is set to the first failing character. If there is no error,
677 * output's pbData must be freed with LocalFree.
679 static BOOL CRYPT_EncodeValueWithType(DWORD dwCertEncodingType,
680 const struct X500TokenW *value, PCERT_NAME_BLOB output, DWORD type,
683 CERT_NAME_VALUE nameValue = { type, { 0, NULL } };
686 if (value->end > value->start)
688 nameValue.Value.pbData = CryptMemAlloc((value->end - value->start) *
690 if (!nameValue.Value.pbData)
692 SetLastError(ERROR_OUTOFMEMORY);
698 if (value->end > value->start)
701 LPWSTR ptr = (LPWSTR)nameValue.Value.pbData;
703 for (i = 0; i < value->end - value->start; i++)
705 *ptr++ = value->start[i];
706 if (value->start[i] == '"')
709 nameValue.Value.cbData = (LPBYTE)ptr - nameValue.Value.pbData;
711 ret = CryptEncodeObjectEx(dwCertEncodingType, X509_UNICODE_NAME_VALUE,
712 &nameValue, CRYPT_ENCODE_ALLOC_FLAG, NULL, &output->pbData,
714 if (!ret && ppszError)
716 if (type == CERT_RDN_NUMERIC_STRING &&
717 GetLastError() == CRYPT_E_INVALID_NUMERIC_STRING)
718 *ppszError = value->start + output->cbData;
719 else if (type == CERT_RDN_PRINTABLE_STRING &&
720 GetLastError() == CRYPT_E_INVALID_PRINTABLE_STRING)
721 *ppszError = value->start + output->cbData;
722 else if (type == CERT_RDN_IA5_STRING &&
723 GetLastError() == CRYPT_E_INVALID_IA5_STRING)
724 *ppszError = value->start + output->cbData;
726 CryptMemFree(nameValue.Value.pbData);
731 static BOOL CRYPT_EncodeValue(DWORD dwCertEncodingType,
732 const struct X500TokenW *value, PCERT_NAME_BLOB output, const DWORD *types,
739 for (i = 0; !ret && types[i]; i++)
740 ret = CRYPT_EncodeValueWithType(dwCertEncodingType, value, output,
741 types[i], ppszError);
745 static BOOL CRYPT_ValueToRDN(DWORD dwCertEncodingType, PCERT_NAME_INFO info,
746 PCCRYPT_OID_INFO keyOID, struct X500TokenW *value, LPCWSTR *ppszError)
750 TRACE("OID %s, value %s\n", debugstr_a(keyOID->pszOID),
751 debugstr_wn(value->start, value->end - value->start));
754 info->rgRDN = CryptMemAlloc(sizeof(CERT_RDN));
756 info->rgRDN = CryptMemRealloc(info->rgRDN,
757 (info->cRDN + 1) * sizeof(CERT_RDN));
760 /* FIXME: support multiple RDN attrs */
761 info->rgRDN[info->cRDN].rgRDNAttr =
762 CryptMemAlloc(sizeof(CERT_RDN_ATTR));
763 if (info->rgRDN[info->cRDN].rgRDNAttr)
765 static const DWORD defaultTypes[] = { CERT_RDN_PRINTABLE_STRING,
766 CERT_RDN_BMP_STRING, 0 };
769 info->rgRDN[info->cRDN].cRDNAttr = 1;
770 info->rgRDN[info->cRDN].rgRDNAttr[0].pszObjId =
771 (LPSTR)keyOID->pszOID;
772 info->rgRDN[info->cRDN].rgRDNAttr[0].dwValueType =
773 CERT_RDN_ENCODED_BLOB;
774 if (keyOID->ExtraInfo.cbData)
775 types = (const DWORD *)keyOID->ExtraInfo.pbData;
777 types = defaultTypes;
779 /* Remove surrounding quotes */
780 if (value->start[0] == '"')
785 ret = CRYPT_EncodeValue(dwCertEncodingType, value,
786 &info->rgRDN[info->cRDN].rgRDNAttr[0].Value, types, ppszError);
789 SetLastError(ERROR_OUTOFMEMORY);
793 SetLastError(ERROR_OUTOFMEMORY);
797 BOOL WINAPI CertStrToNameW(DWORD dwCertEncodingType, LPCWSTR pszX500,
798 DWORD dwStrType, void *pvReserved, BYTE *pbEncoded, DWORD *pcbEncoded,
801 CERT_NAME_INFO info = { 0, NULL };
803 struct KeynameKeeper keeper;
807 TRACE("(%08x, %s, %08x, %p, %p, %p, %p)\n", dwCertEncodingType,
808 debugstr_w(pszX500), dwStrType, pvReserved, pbEncoded, pcbEncoded,
811 CRYPT_InitializeKeynameKeeper(&keeper);
813 while (str && *str && ret)
815 struct X500TokenW token;
817 ret = CRYPT_GetNextKeyW(str, &token, ppszError);
818 if (ret && token.start)
820 PCCRYPT_OID_INFO keyOID;
822 CRYPT_KeynameKeeperFromTokenW(&keeper, &token);
823 keyOID = CryptFindOIDInfo(CRYPT_OID_INFO_NAME_KEY, keeper.keyName,
824 CRYPT_RDN_ATTR_OID_GROUP_ID);
828 *ppszError = token.start;
829 SetLastError(CRYPT_E_INVALID_X500_STRING);
835 while (isspace(*str))
841 SetLastError(CRYPT_E_INVALID_X500_STRING);
846 static const WCHAR commaSep[] = { ',',0 };
847 static const WCHAR semiSep[] = { ';',0 };
848 static const WCHAR crlfSep[] = { '\r','\n',0 };
849 static const WCHAR allSeps[] = { ',',';','\r','\n',0 };
853 if (dwStrType & CERT_NAME_STR_COMMA_FLAG)
855 else if (dwStrType & CERT_NAME_STR_SEMICOLON_FLAG)
857 else if (dwStrType & CERT_NAME_STR_CRLF_FLAG)
861 ret = CRYPT_GetNextValueW(str, dwStrType, sep, &token,
866 ret = CRYPT_ValueToRDN(dwCertEncodingType, &info,
867 keyOID, &token, ppszError);
873 CRYPT_FreeKeynameKeeper(&keeper);
878 ret = CryptEncodeObjectEx(dwCertEncodingType, X509_NAME, &info,
879 0, NULL, pbEncoded, pcbEncoded);
881 for (i = 0; i < info.cRDN; i++)
885 for (j = 0; j < info.rgRDN[i].cRDNAttr; j++)
886 LocalFree(info.rgRDN[i].rgRDNAttr[j].Value.pbData);
887 CryptMemFree(info.rgRDN[i].rgRDNAttr);
889 CryptMemFree(info.rgRDN);
893 DWORD WINAPI CertGetNameStringA(PCCERT_CONTEXT pCertContext, DWORD dwType,
894 DWORD dwFlags, void *pvTypePara, LPSTR pszNameString, DWORD cchNameString)
898 TRACE("(%p, %d, %08x, %p, %p, %d)\n", pCertContext, dwType, dwFlags,
899 pvTypePara, pszNameString, cchNameString);
906 nameLen = CertGetNameStringW(pCertContext, dwType, dwFlags, pvTypePara,
908 wideName = CryptMemAlloc(nameLen * sizeof(WCHAR));
911 CertGetNameStringW(pCertContext, dwType, dwFlags, pvTypePara,
913 nameLen = WideCharToMultiByte(CP_ACP, 0, wideName, nameLen,
914 pszNameString, cchNameString, NULL, NULL);
915 if (nameLen <= cchNameString)
919 pszNameString[cchNameString - 1] = '\0';
922 CryptMemFree(wideName);
926 *pszNameString = '\0';
931 ret = CertGetNameStringW(pCertContext, dwType, dwFlags, pvTypePara,
936 /* Searches cert's extensions for the alternate name extension with OID
937 * altNameOID, and if found, searches it for the alternate name type entryType.
938 * If found, returns a pointer to the entry, otherwise returns NULL.
939 * Regardless of whether an entry of the desired type is found, if the
940 * alternate name extension is present, sets *info to the decoded alternate
941 * name extension, which you must free using LocalFree.
942 * The return value is a pointer within *info, so don't free *info before
943 * you're done with the return value.
945 static PCERT_ALT_NAME_ENTRY cert_find_alt_name_entry(PCCERT_CONTEXT cert,
946 LPCSTR altNameOID, DWORD entryType, PCERT_ALT_NAME_INFO *info)
948 PCERT_ALT_NAME_ENTRY entry = NULL;
949 PCERT_EXTENSION ext = CertFindExtension(altNameOID,
950 cert->pCertInfo->cExtension, cert->pCertInfo->rgExtension);
956 if (CryptDecodeObjectEx(cert->dwCertEncodingType, X509_ALTERNATE_NAME,
957 ext->Value.pbData, ext->Value.cbData, CRYPT_DECODE_ALLOC_FLAG, NULL,
962 for (i = 0; !entry && i < (*info)->cAltEntry; i++)
963 if ((*info)->rgAltEntry[i].dwAltNameChoice == entryType)
964 entry = &(*info)->rgAltEntry[i];
972 static DWORD cert_get_name_from_rdn_attr(DWORD encodingType,
973 PCERT_NAME_BLOB name, LPCSTR oid, LPWSTR pszNameString, DWORD cchNameString)
975 CERT_NAME_INFO *nameInfo;
976 DWORD bytes = 0, ret = 0;
978 if (CryptDecodeObjectEx(encodingType, X509_NAME, name->pbData,
979 name->cbData, CRYPT_DECODE_ALLOC_FLAG, NULL, &nameInfo, &bytes))
981 PCERT_RDN_ATTR nameAttr = CertFindRDNAttr(oid, nameInfo);
984 ret = CertRDNValueToStrW(nameAttr->dwValueType, &nameAttr->Value,
985 pszNameString, cchNameString);
991 DWORD WINAPI CertGetNameStringW(PCCERT_CONTEXT pCertContext, DWORD dwType,
992 DWORD dwFlags, void *pvTypePara, LPWSTR pszNameString, DWORD cchNameString)
995 PCERT_NAME_BLOB name;
998 TRACE("(%p, %d, %08x, %p, %p, %d)\n", pCertContext, dwType,
999 dwFlags, pvTypePara, pszNameString, cchNameString);
1001 if (dwFlags & CERT_NAME_ISSUER_FLAG)
1003 name = &pCertContext->pCertInfo->Issuer;
1004 altNameOID = szOID_ISSUER_ALT_NAME;
1008 name = &pCertContext->pCertInfo->Subject;
1009 altNameOID = szOID_SUBJECT_ALT_NAME;
1014 case CERT_NAME_EMAIL_TYPE:
1016 CERT_ALT_NAME_INFO *info;
1017 PCERT_ALT_NAME_ENTRY entry = cert_find_alt_name_entry(pCertContext,
1018 altNameOID, CERT_ALT_NAME_RFC822_NAME, &info);
1023 ret = strlenW(entry->u.pwszRfc822Name) + 1;
1024 else if (cchNameString)
1026 ret = min(strlenW(entry->u.pwszRfc822Name), cchNameString - 1);
1027 memcpy(pszNameString, entry->u.pwszRfc822Name,
1028 ret * sizeof(WCHAR));
1029 pszNameString[ret++] = 0;
1035 ret = cert_get_name_from_rdn_attr(pCertContext->dwCertEncodingType,
1036 name, szOID_RSA_emailAddr, pszNameString, cchNameString);
1039 case CERT_NAME_RDN_TYPE:
1041 ret = CertNameToStrW(pCertContext->dwCertEncodingType, name,
1042 *(DWORD *)pvTypePara, pszNameString, cchNameString);
1045 CERT_ALT_NAME_INFO *info;
1046 PCERT_ALT_NAME_ENTRY entry = cert_find_alt_name_entry(pCertContext,
1047 altNameOID, CERT_ALT_NAME_DIRECTORY_NAME, &info);
1050 ret = CertNameToStrW(pCertContext->dwCertEncodingType,
1051 &entry->u.DirectoryName, *(DWORD *)pvTypePara, pszNameString,
1057 case CERT_NAME_ATTR_TYPE:
1058 ret = cert_get_name_from_rdn_attr(pCertContext->dwCertEncodingType,
1059 name, pvTypePara, pszNameString, cchNameString);
1062 CERT_ALT_NAME_INFO *altInfo;
1063 PCERT_ALT_NAME_ENTRY entry = cert_find_alt_name_entry(pCertContext,
1064 altNameOID, CERT_ALT_NAME_DIRECTORY_NAME, &altInfo);
1067 ret = cert_name_to_str_with_indent(X509_ASN_ENCODING, 0,
1068 &entry->u.DirectoryName, 0, pszNameString, cchNameString);
1073 case CERT_NAME_SIMPLE_DISPLAY_TYPE:
1075 static const LPCSTR simpleAttributeOIDs[] = { szOID_COMMON_NAME,
1076 szOID_ORGANIZATIONAL_UNIT_NAME, szOID_ORGANIZATION_NAME,
1077 szOID_RSA_emailAddr };
1078 CERT_NAME_INFO *nameInfo = NULL;
1081 if (CryptDecodeObjectEx(pCertContext->dwCertEncodingType, X509_NAME,
1082 name->pbData, name->cbData, CRYPT_DECODE_ALLOC_FLAG, NULL, &nameInfo,
1085 PCERT_RDN_ATTR nameAttr = NULL;
1087 for (i = 0; !nameAttr && i < sizeof(simpleAttributeOIDs) /
1088 sizeof(simpleAttributeOIDs[0]); i++)
1089 nameAttr = CertFindRDNAttr(simpleAttributeOIDs[i], nameInfo);
1091 ret = CertRDNValueToStrW(nameAttr->dwValueType,
1092 &nameAttr->Value, pszNameString, cchNameString);
1093 LocalFree(nameInfo);
1097 CERT_ALT_NAME_INFO *altInfo;
1098 PCERT_ALT_NAME_ENTRY entry = cert_find_alt_name_entry(pCertContext,
1099 altNameOID, CERT_ALT_NAME_RFC822_NAME, &altInfo);
1103 if (!entry && altInfo->cAltEntry)
1104 entry = &altInfo->rgAltEntry[0];
1108 ret = strlenW(entry->u.pwszRfc822Name) + 1;
1109 else if (cchNameString)
1111 ret = min(strlenW(entry->u.pwszRfc822Name),
1113 memcpy(pszNameString, entry->u.pwszRfc822Name,
1114 ret * sizeof(WCHAR));
1115 pszNameString[ret++] = 0;
1123 case CERT_NAME_FRIENDLY_DISPLAY_TYPE:
1125 DWORD cch = cchNameString;
1127 if (CertGetCertificateContextProperty(pCertContext,
1128 CERT_FRIENDLY_NAME_PROP_ID, pszNameString, &cch))
1131 ret = CertGetNameStringW(pCertContext,
1132 CERT_NAME_SIMPLE_DISPLAY_TYPE, dwFlags, pvTypePara, pszNameString,
1136 case CERT_NAME_DNS_TYPE:
1138 CERT_ALT_NAME_INFO *info;
1139 PCERT_ALT_NAME_ENTRY entry = cert_find_alt_name_entry(pCertContext,
1140 altNameOID, CERT_ALT_NAME_DNS_NAME, &info);
1145 ret = strlenW(entry->u.pwszDNSName) + 1;
1146 else if (cchNameString)
1148 ret = min(strlenW(entry->u.pwszDNSName), cchNameString - 1);
1149 memcpy(pszNameString, entry->u.pwszDNSName, ret * sizeof(WCHAR));
1150 pszNameString[ret++] = 0;
1156 ret = cert_get_name_from_rdn_attr(pCertContext->dwCertEncodingType,
1157 name, szOID_COMMON_NAME, pszNameString, cchNameString);
1160 case CERT_NAME_URL_TYPE:
1162 CERT_ALT_NAME_INFO *info;
1163 PCERT_ALT_NAME_ENTRY entry = cert_find_alt_name_entry(pCertContext,
1164 altNameOID, CERT_ALT_NAME_URL, &info);
1169 ret = strlenW(entry->u.pwszURL) + 1;
1170 else if (cchNameString)
1172 ret = min(strlenW(entry->u.pwszURL), cchNameString - 1);
1173 memcpy(pszNameString, entry->u.pwszURL, ret * sizeof(WCHAR));
1174 pszNameString[ret++] = 0;
1182 FIXME("unimplemented for type %d\n", dwType);
1189 else if (cchNameString)
1191 pszNameString[0] = 0;