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 static inline BOOL is_quotable_char(char c)
51 DWORD WINAPI CertRDNValueToStrA(DWORD dwValueType, PCERT_RDN_VALUE_BLOB pValue,
54 DWORD ret = 0, len, i;
55 BOOL needsQuotes = FALSE;
57 TRACE("(%d, %p, %p, %d)\n", dwValueType, pValue, psz, csz);
61 case CERT_RDN_ANY_TYPE:
63 case CERT_RDN_NUMERIC_STRING:
64 case CERT_RDN_PRINTABLE_STRING:
65 case CERT_RDN_TELETEX_STRING:
66 case CERT_RDN_VIDEOTEX_STRING:
67 case CERT_RDN_IA5_STRING:
68 case CERT_RDN_GRAPHIC_STRING:
69 case CERT_RDN_VISIBLE_STRING:
70 case CERT_RDN_GENERAL_STRING:
72 if (pValue->cbData && isspace(pValue->pbData[0]))
74 if (pValue->cbData && isspace(pValue->pbData[pValue->cbData - 1]))
76 for (i = 0; i < pValue->cbData; i++)
78 if (is_quotable_char(pValue->pbData[i]))
80 if (pValue->pbData[i] == '"')
93 for (i = 0; i < pValue->cbData && ptr - psz < csz; ptr++, i++)
95 *ptr = pValue->pbData[i];
96 if (pValue->pbData[i] == '"' && ptr - psz < csz - 1)
99 if (needsQuotes && ptr - psz < csz)
104 case CERT_RDN_BMP_STRING:
105 case CERT_RDN_UTF8_STRING:
106 len = WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)pValue->pbData,
107 pValue->cbData / sizeof(WCHAR), NULL, 0, NULL, NULL);
108 if (pValue->cbData && isspaceW(((LPCWSTR)pValue->pbData)[0]))
110 if (pValue->cbData &&
111 isspaceW(((LPCWSTR)pValue->pbData)[pValue->cbData / sizeof(WCHAR)-1]))
113 for (i = 0; i < pValue->cbData / sizeof(WCHAR); i++)
115 if (is_quotable_char(((LPCWSTR)pValue->pbData)[i]))
117 if (((LPCWSTR)pValue->pbData)[i] == '"')
130 for (i = 0; i < pValue->cbData / sizeof(WCHAR) &&
131 dst - psz < csz; dst++, i++)
133 LPCWSTR src = (LPCWSTR)pValue->pbData + i;
135 WideCharToMultiByte(CP_ACP, 0, src, 1, dst,
136 csz - (dst - psz) - 1, NULL, NULL);
137 if (*src == '"' && dst - psz < csz - 1)
140 if (needsQuotes && dst - psz < csz)
146 FIXME("string type %d unimplemented\n", dwValueType);
156 TRACE("returning %d (%s)\n", ret, debugstr_a(psz));
160 DWORD WINAPI CertRDNValueToStrW(DWORD dwValueType, PCERT_RDN_VALUE_BLOB pValue,
161 LPWSTR psz, DWORD csz)
163 DWORD ret = 0, len, i, strLen;
164 BOOL needsQuotes = FALSE;
166 TRACE("(%d, %p, %p, %d)\n", dwValueType, pValue, psz, csz);
170 case CERT_RDN_ANY_TYPE:
172 case CERT_RDN_NUMERIC_STRING:
173 case CERT_RDN_PRINTABLE_STRING:
174 case CERT_RDN_TELETEX_STRING:
175 case CERT_RDN_VIDEOTEX_STRING:
176 case CERT_RDN_IA5_STRING:
177 case CERT_RDN_GRAPHIC_STRING:
178 case CERT_RDN_VISIBLE_STRING:
179 case CERT_RDN_GENERAL_STRING:
180 len = pValue->cbData;
181 if (pValue->cbData && isspace(pValue->pbData[0]))
183 if (pValue->cbData && isspace(pValue->pbData[pValue->cbData - 1]))
185 for (i = 0; i < pValue->cbData; i++)
187 if (is_quotable_char(pValue->pbData[i]))
189 if (pValue->pbData[i] == '"')
202 for (i = 0; i < pValue->cbData && ptr - psz < csz; ptr++, i++)
204 *ptr = pValue->pbData[i];
205 if (pValue->pbData[i] == '"' && ptr - psz < csz - 1)
208 if (needsQuotes && ptr - psz < csz)
213 case CERT_RDN_BMP_STRING:
214 case CERT_RDN_UTF8_STRING:
215 strLen = len = pValue->cbData / sizeof(WCHAR);
216 if (pValue->cbData && isspace(pValue->pbData[0]))
218 if (pValue->cbData && isspace(pValue->pbData[strLen - 1]))
220 for (i = 0; i < strLen; i++)
222 if (is_quotable_char(((LPCWSTR)pValue->pbData)[i]))
224 if (((LPCWSTR)pValue->pbData)[i] == '"')
237 for (i = 0; i < strLen && ptr - psz < csz; ptr++, i++)
239 *ptr = ((LPCWSTR)pValue->pbData)[i];
240 if (((LPCWSTR)pValue->pbData)[i] == '"' && ptr - psz < csz - 1)
243 if (needsQuotes && ptr - psz < csz)
249 FIXME("string type %d unimplemented\n", dwValueType);
259 TRACE("returning %d (%s)\n", ret, debugstr_w(psz));
263 /* Adds the prefix prefix to the string pointed to by psz, followed by the
264 * character '='. Copies no more than csz characters. Returns the number of
265 * characters copied. If psz is NULL, returns the number of characters that
268 static DWORD CRYPT_AddPrefixA(LPCSTR prefix, LPSTR psz, DWORD csz)
272 TRACE("(%s, %p, %d)\n", debugstr_a(prefix), psz, csz);
276 chars = min(strlen(prefix), csz);
277 memcpy(psz, prefix, chars);
278 *(psz + chars) = '=';
282 chars = lstrlenA(prefix) + 1;
286 DWORD WINAPI CertNameToStrA(DWORD dwCertEncodingType, PCERT_NAME_BLOB pName,
287 DWORD dwStrType, LPSTR psz, DWORD csz)
289 static const DWORD unsupportedFlags = CERT_NAME_STR_NO_QUOTING_FLAG |
290 CERT_NAME_STR_ENABLE_T61_UNICODE_FLAG;
291 static const char commaSep[] = ", ";
292 static const char semiSep[] = "; ";
293 static const char crlfSep[] = "\r\n";
294 static const char plusSep[] = " + ";
295 static const char spaceSep[] = " ";
296 DWORD ret = 0, bytes = 0;
298 CERT_NAME_INFO *info;
300 TRACE("(%d, %p, %08x, %p, %d)\n", dwCertEncodingType, pName, dwStrType,
302 if (dwStrType & unsupportedFlags)
303 FIXME("unsupported flags: %08x\n", dwStrType & unsupportedFlags);
305 bRet = CryptDecodeObjectEx(dwCertEncodingType, X509_NAME, pName->pbData,
306 pName->cbData, CRYPT_DECODE_ALLOC_FLAG, NULL, &info, &bytes);
309 DWORD i, j, sepLen, rdnSepLen;
311 BOOL reverse = dwStrType & CERT_NAME_STR_REVERSE_FLAG;
312 const CERT_RDN *rdn = info->rgRDN;
314 if(reverse && info->cRDN > 1) rdn += (info->cRDN - 1);
316 if (dwStrType & CERT_NAME_STR_SEMICOLON_FLAG)
318 else if (dwStrType & CERT_NAME_STR_CRLF_FLAG)
322 sepLen = strlen(sep);
323 if (dwStrType & CERT_NAME_STR_NO_PLUS_FLAG)
327 rdnSepLen = strlen(rdnSep);
328 for (i = 0; (!psz || ret < csz) && i < info->cRDN; i++)
330 for (j = 0; (!psz || ret < csz) && j < rdn->cRDNAttr; j++)
333 char prefixBuf[10]; /* big enough for GivenName */
334 LPCSTR prefix = NULL;
336 if ((dwStrType & 0x000000ff) == CERT_OID_NAME_STR)
337 prefix = rdn->rgRDNAttr[j].pszObjId;
338 else if ((dwStrType & 0x000000ff) == CERT_X500_NAME_STR)
340 PCCRYPT_OID_INFO oidInfo = CryptFindOIDInfo(
341 CRYPT_OID_INFO_OID_KEY,
342 rdn->rgRDNAttr[j].pszObjId,
343 CRYPT_RDN_ATTR_OID_GROUP_ID);
347 WideCharToMultiByte(CP_ACP, 0, oidInfo->pwszName, -1,
348 prefixBuf, sizeof(prefixBuf), NULL, NULL);
352 prefix = rdn->rgRDNAttr[j].pszObjId;
356 /* - 1 is needed to account for the NULL terminator. */
357 chars = CRYPT_AddPrefixA(prefix,
358 psz ? psz + ret : NULL, psz ? csz - ret - 1 : 0);
361 chars = CertRDNValueToStrA(
362 rdn->rgRDNAttr[j].dwValueType,
363 &rdn->rgRDNAttr[j].Value, psz ? psz + ret : NULL,
364 psz ? csz - ret : 0);
367 if (j < rdn->cRDNAttr - 1)
369 if (psz && ret < csz - rdnSepLen - 1)
370 memcpy(psz + ret, rdnSep, rdnSepLen);
374 if (i < info->cRDN - 1)
376 if (psz && ret < csz - sepLen - 1)
377 memcpy(psz + ret, sep, sepLen);
392 TRACE("Returning %s\n", debugstr_a(psz));
396 /* Adds the prefix prefix to the wide-character string pointed to by psz,
397 * followed by the character '='. Copies no more than csz characters. Returns
398 * the number of characters copied. If psz is NULL, returns the number of
399 * characters that would be copied.
400 * Assumes the characters in prefix are ASCII (not multibyte characters.)
402 static DWORD CRYPT_AddPrefixAToW(LPCSTR prefix, LPWSTR psz, DWORD csz)
406 TRACE("(%s, %p, %d)\n", debugstr_a(prefix), psz, csz);
412 chars = min(strlen(prefix), csz);
413 for (i = 0; i < chars; i++)
414 *(psz + i) = prefix[i];
415 *(psz + chars) = '=';
419 chars = lstrlenA(prefix) + 1;
423 /* Adds the prefix prefix to the string pointed to by psz, followed by the
424 * character '='. Copies no more than csz characters. Returns the number of
425 * characters copied. If psz is NULL, returns the number of characters that
428 static DWORD CRYPT_AddPrefixW(LPCWSTR prefix, LPWSTR psz, DWORD csz)
432 TRACE("(%s, %p, %d)\n", debugstr_w(prefix), psz, csz);
436 chars = min(strlenW(prefix), csz);
437 memcpy(psz, prefix, chars * sizeof(WCHAR));
438 *(psz + chars) = '=';
442 chars = lstrlenW(prefix) + 1;
446 static const WCHAR indent[] = { ' ',' ',' ',' ',' ',0 };
448 DWORD cert_name_to_str_with_indent(DWORD dwCertEncodingType, DWORD indentLevel,
449 const CERT_NAME_BLOB *pName, DWORD dwStrType, LPWSTR psz, DWORD csz)
451 static const DWORD unsupportedFlags = CERT_NAME_STR_NO_QUOTING_FLAG |
452 CERT_NAME_STR_ENABLE_T61_UNICODE_FLAG;
453 static const WCHAR commaSep[] = { ',',' ',0 };
454 static const WCHAR semiSep[] = { ';',' ',0 };
455 static const WCHAR crlfSep[] = { '\r','\n',0 };
456 static const WCHAR plusSep[] = { ' ','+',' ',0 };
457 static const WCHAR spaceSep[] = { ' ',0 };
458 DWORD ret = 0, bytes = 0;
460 CERT_NAME_INFO *info;
462 if (dwStrType & unsupportedFlags)
463 FIXME("unsupported flags: %08x\n", dwStrType & unsupportedFlags);
465 bRet = CryptDecodeObjectEx(dwCertEncodingType, X509_NAME, pName->pbData,
466 pName->cbData, CRYPT_DECODE_ALLOC_FLAG, NULL, &info, &bytes);
469 DWORD i, j, sepLen, rdnSepLen;
471 BOOL reverse = dwStrType & CERT_NAME_STR_REVERSE_FLAG;
472 const CERT_RDN *rdn = info->rgRDN;
474 if(reverse && info->cRDN > 1) rdn += (info->cRDN - 1);
476 if (dwStrType & CERT_NAME_STR_SEMICOLON_FLAG)
478 else if (dwStrType & CERT_NAME_STR_CRLF_FLAG)
482 sepLen = lstrlenW(sep);
483 if (dwStrType & CERT_NAME_STR_NO_PLUS_FLAG)
487 rdnSepLen = lstrlenW(rdnSep);
488 for (i = 0; (!psz || ret < csz) && i < info->cRDN; i++)
490 for (j = 0; (!psz || ret < csz) && j < rdn->cRDNAttr; j++)
493 LPCSTR prefixA = NULL;
494 LPCWSTR prefixW = NULL;
496 if ((dwStrType & 0x000000ff) == CERT_OID_NAME_STR)
497 prefixA = rdn->rgRDNAttr[j].pszObjId;
498 else if ((dwStrType & 0x000000ff) == CERT_X500_NAME_STR)
500 PCCRYPT_OID_INFO oidInfo = CryptFindOIDInfo(
501 CRYPT_OID_INFO_OID_KEY,
502 rdn->rgRDNAttr[j].pszObjId,
503 CRYPT_RDN_ATTR_OID_GROUP_ID);
506 prefixW = oidInfo->pwszName;
508 prefixA = rdn->rgRDNAttr[j].pszObjId;
510 if (dwStrType & CERT_NAME_STR_CRLF_FLAG)
514 for (k = 0; k < indentLevel; k++)
518 chars = min(strlenW(indent), csz - ret - 1);
519 memcpy(psz + ret, indent, chars * sizeof(WCHAR));
522 chars = strlenW(indent);
528 /* - 1 is needed to account for the NULL terminator. */
529 chars = CRYPT_AddPrefixW(prefixW,
530 psz ? psz + ret : NULL, psz ? csz - ret - 1 : 0);
535 /* - 1 is needed to account for the NULL terminator. */
536 chars = CRYPT_AddPrefixAToW(prefixA,
537 psz ? psz + ret : NULL, psz ? csz - ret - 1 : 0);
540 chars = CertRDNValueToStrW(
541 rdn->rgRDNAttr[j].dwValueType,
542 &rdn->rgRDNAttr[j].Value, psz ? psz + ret : NULL,
543 psz ? csz - ret : 0);
546 if (j < rdn->cRDNAttr - 1)
548 if (psz && ret < csz - rdnSepLen - 1)
549 memcpy(psz + ret, rdnSep, rdnSepLen * sizeof(WCHAR));
553 if (i < info->cRDN - 1)
555 if (psz && ret < csz - sepLen - 1)
556 memcpy(psz + ret, sep, sepLen * sizeof(WCHAR));
574 DWORD WINAPI CertNameToStrW(DWORD dwCertEncodingType, PCERT_NAME_BLOB pName,
575 DWORD dwStrType, LPWSTR psz, DWORD csz)
579 TRACE("(%d, %p, %08x, %p, %d)\n", dwCertEncodingType, pName, dwStrType,
582 ret = cert_name_to_str_with_indent(dwCertEncodingType, 0, pName, dwStrType,
584 TRACE("Returning %s\n", debugstr_w(psz));
588 BOOL WINAPI CertStrToNameA(DWORD dwCertEncodingType, LPCSTR pszX500,
589 DWORD dwStrType, void *pvReserved, BYTE *pbEncoded, DWORD *pcbEncoded,
595 TRACE("(%08x, %s, %08x, %p, %p, %p, %p)\n", dwCertEncodingType,
596 debugstr_a(pszX500), dwStrType, pvReserved, pbEncoded, pcbEncoded,
599 len = MultiByteToWideChar(CP_ACP, 0, pszX500, -1, NULL, 0);
602 LPWSTR x500, errorStr;
604 if ((x500 = CryptMemAlloc(len * sizeof(WCHAR))))
606 MultiByteToWideChar(CP_ACP, 0, pszX500, -1, x500, len);
607 ret = CertStrToNameW(dwCertEncodingType, x500, dwStrType,
608 pvReserved, pbEncoded, pcbEncoded,
609 ppszError ? (LPCWSTR *)&errorStr : NULL);
616 *ppszError = pszX500;
617 for (i = 0; i < errorStr - x500; i++)
618 *ppszError = CharNextA(*ppszError);
627 SetLastError(ERROR_OUTOFMEMORY);
633 SetLastError(CRYPT_E_INVALID_X500_STRING);
635 *ppszError = pszX500;
643 WCHAR buf[10]; /* big enough for L"GivenName" */
644 LPWSTR keyName; /* usually = buf, but may be allocated */
648 static void CRYPT_InitializeKeynameKeeper(struct KeynameKeeper *keeper)
650 keeper->keyName = keeper->buf;
651 keeper->keyLen = sizeof(keeper->buf) / sizeof(keeper->buf[0]);
654 static void CRYPT_FreeKeynameKeeper(struct KeynameKeeper *keeper)
656 if (keeper->keyName != keeper->buf)
657 CryptMemFree(keeper->keyName);
666 static void CRYPT_KeynameKeeperFromTokenW(struct KeynameKeeper *keeper,
667 const struct X500TokenW *key)
669 DWORD len = key->end - key->start;
671 if (len > keeper->keyLen)
673 if (keeper->keyName == keeper->buf)
674 keeper->keyName = CryptMemAlloc(len * sizeof(WCHAR));
676 keeper->keyName = CryptMemRealloc(keeper->keyName,
677 len * sizeof(WCHAR));
678 keeper->keyLen = len;
680 memcpy(keeper->keyName, key->start, (key->end - key->start) *
682 keeper->keyName[len] = '\0';
683 TRACE("Keyname is %s\n", debugstr_w(keeper->keyName));
686 static BOOL CRYPT_GetNextKeyW(LPCWSTR str, struct X500TokenW *token,
691 while (*str && isspaceW(*str))
696 while (*str && *str != '=' && !isspaceW(*str))
698 if (*str && (*str == '=' || isspaceW(*str)))
702 TRACE("missing equals char at %s\n", debugstr_w(token->start));
704 *ppszError = token->start;
705 SetLastError(CRYPT_E_INVALID_X500_STRING);
714 /* Assumes separators are characters in the 0-255 range */
715 static BOOL CRYPT_GetNextValueW(LPCWSTR str, DWORD dwFlags, LPCWSTR separators,
716 struct X500TokenW *token, LPCWSTR *ppszError)
720 TRACE("(%s, %s, %p, %p)\n", debugstr_w(str), debugstr_w(separators), token,
723 while (*str && isspaceW(*str))
728 if (!(dwFlags & CERT_NAME_STR_NO_QUOTING_FLAG) && *str == '"')
732 while (!token->end && ret)
734 while (*str && *str != '"')
738 if (*(str + 1) != '"')
739 token->end = str + 1;
745 TRACE("unterminated quote at %s\n", debugstr_w(str));
748 SetLastError(CRYPT_E_INVALID_X500_STRING);
755 WCHAR map[256] = { 0 };
758 map[*separators++] = 1;
759 while (*str && (*str >= 0xff || !map[*str]))
766 TRACE("missing value at %s\n", debugstr_w(str));
769 SetLastError(CRYPT_E_INVALID_X500_STRING);
775 /* Encodes the string represented by value as the string type type into the
776 * CERT_NAME_BLOB output. If there is an error and ppszError is not NULL,
777 * *ppszError is set to the first failing character. If there is no error,
778 * output's pbData must be freed with LocalFree.
780 static BOOL CRYPT_EncodeValueWithType(DWORD dwCertEncodingType,
781 const struct X500TokenW *value, PCERT_NAME_BLOB output, DWORD type,
784 CERT_NAME_VALUE nameValue = { type, { 0, NULL } };
787 if (value->end > value->start)
789 nameValue.Value.pbData = CryptMemAlloc((value->end - value->start) *
791 if (!nameValue.Value.pbData)
793 SetLastError(ERROR_OUTOFMEMORY);
799 if (value->end > value->start)
802 LPWSTR ptr = (LPWSTR)nameValue.Value.pbData;
804 for (i = 0; i < value->end - value->start; i++)
806 *ptr++ = value->start[i];
807 if (value->start[i] == '"')
810 nameValue.Value.cbData = (LPBYTE)ptr - nameValue.Value.pbData;
812 ret = CryptEncodeObjectEx(dwCertEncodingType, X509_UNICODE_NAME_VALUE,
813 &nameValue, CRYPT_ENCODE_ALLOC_FLAG, NULL, &output->pbData,
815 if (!ret && ppszError)
817 if (type == CERT_RDN_NUMERIC_STRING &&
818 GetLastError() == CRYPT_E_INVALID_NUMERIC_STRING)
819 *ppszError = value->start + output->cbData;
820 else if (type == CERT_RDN_PRINTABLE_STRING &&
821 GetLastError() == CRYPT_E_INVALID_PRINTABLE_STRING)
822 *ppszError = value->start + output->cbData;
823 else if (type == CERT_RDN_IA5_STRING &&
824 GetLastError() == CRYPT_E_INVALID_IA5_STRING)
825 *ppszError = value->start + output->cbData;
827 CryptMemFree(nameValue.Value.pbData);
832 static BOOL CRYPT_EncodeValue(DWORD dwCertEncodingType,
833 const struct X500TokenW *value, PCERT_NAME_BLOB output, const DWORD *types,
840 for (i = 0; !ret && types[i]; i++)
841 ret = CRYPT_EncodeValueWithType(dwCertEncodingType, value, output,
842 types[i], ppszError);
846 static BOOL CRYPT_ValueToRDN(DWORD dwCertEncodingType, PCERT_NAME_INFO info,
847 PCCRYPT_OID_INFO keyOID, struct X500TokenW *value, LPCWSTR *ppszError)
851 TRACE("OID %s, value %s\n", debugstr_a(keyOID->pszOID),
852 debugstr_wn(value->start, value->end - value->start));
855 info->rgRDN = CryptMemAlloc(sizeof(CERT_RDN));
857 info->rgRDN = CryptMemRealloc(info->rgRDN,
858 (info->cRDN + 1) * sizeof(CERT_RDN));
861 /* FIXME: support multiple RDN attrs */
862 info->rgRDN[info->cRDN].rgRDNAttr =
863 CryptMemAlloc(sizeof(CERT_RDN_ATTR));
864 if (info->rgRDN[info->cRDN].rgRDNAttr)
866 static const DWORD defaultTypes[] = { CERT_RDN_PRINTABLE_STRING,
867 CERT_RDN_BMP_STRING, 0 };
870 info->rgRDN[info->cRDN].cRDNAttr = 1;
871 info->rgRDN[info->cRDN].rgRDNAttr[0].pszObjId =
872 (LPSTR)keyOID->pszOID;
873 info->rgRDN[info->cRDN].rgRDNAttr[0].dwValueType =
874 CERT_RDN_ENCODED_BLOB;
875 if (keyOID->ExtraInfo.cbData)
876 types = (const DWORD *)keyOID->ExtraInfo.pbData;
878 types = defaultTypes;
880 /* Remove surrounding quotes */
881 if (value->start[0] == '"')
886 ret = CRYPT_EncodeValue(dwCertEncodingType, value,
887 &info->rgRDN[info->cRDN].rgRDNAttr[0].Value, types, ppszError);
890 SetLastError(ERROR_OUTOFMEMORY);
894 SetLastError(ERROR_OUTOFMEMORY);
898 BOOL WINAPI CertStrToNameW(DWORD dwCertEncodingType, LPCWSTR pszX500,
899 DWORD dwStrType, void *pvReserved, BYTE *pbEncoded, DWORD *pcbEncoded,
902 CERT_NAME_INFO info = { 0, NULL };
904 struct KeynameKeeper keeper;
908 TRACE("(%08x, %s, %08x, %p, %p, %p, %p)\n", dwCertEncodingType,
909 debugstr_w(pszX500), dwStrType, pvReserved, pbEncoded, pcbEncoded,
912 CRYPT_InitializeKeynameKeeper(&keeper);
914 while (str && *str && ret)
916 struct X500TokenW token;
918 ret = CRYPT_GetNextKeyW(str, &token, ppszError);
919 if (ret && token.start)
921 PCCRYPT_OID_INFO keyOID;
923 CRYPT_KeynameKeeperFromTokenW(&keeper, &token);
924 keyOID = CryptFindOIDInfo(CRYPT_OID_INFO_NAME_KEY, keeper.keyName,
925 CRYPT_RDN_ATTR_OID_GROUP_ID);
929 *ppszError = token.start;
930 SetLastError(CRYPT_E_INVALID_X500_STRING);
936 while (isspace(*str))
942 SetLastError(CRYPT_E_INVALID_X500_STRING);
947 static const WCHAR commaSep[] = { ',',0 };
948 static const WCHAR semiSep[] = { ';',0 };
949 static const WCHAR crlfSep[] = { '\r','\n',0 };
950 static const WCHAR allSeps[] = { ',',';','\r','\n',0 };
954 if (dwStrType & CERT_NAME_STR_COMMA_FLAG)
956 else if (dwStrType & CERT_NAME_STR_SEMICOLON_FLAG)
958 else if (dwStrType & CERT_NAME_STR_CRLF_FLAG)
962 ret = CRYPT_GetNextValueW(str, dwStrType, sep, &token,
967 ret = CRYPT_ValueToRDN(dwCertEncodingType, &info,
968 keyOID, &token, ppszError);
974 CRYPT_FreeKeynameKeeper(&keeper);
979 ret = CryptEncodeObjectEx(dwCertEncodingType, X509_NAME, &info,
980 0, NULL, pbEncoded, pcbEncoded);
982 for (i = 0; i < info.cRDN; i++)
986 for (j = 0; j < info.rgRDN[i].cRDNAttr; j++)
987 LocalFree(info.rgRDN[i].rgRDNAttr[j].Value.pbData);
988 CryptMemFree(info.rgRDN[i].rgRDNAttr);
990 CryptMemFree(info.rgRDN);
994 DWORD WINAPI CertGetNameStringA(PCCERT_CONTEXT pCertContext, DWORD dwType,
995 DWORD dwFlags, void *pvTypePara, LPSTR pszNameString, DWORD cchNameString)
999 TRACE("(%p, %d, %08x, %p, %p, %d)\n", pCertContext, dwType, dwFlags,
1000 pvTypePara, pszNameString, cchNameString);
1007 nameLen = CertGetNameStringW(pCertContext, dwType, dwFlags, pvTypePara,
1009 wideName = CryptMemAlloc(nameLen * sizeof(WCHAR));
1012 CertGetNameStringW(pCertContext, dwType, dwFlags, pvTypePara,
1014 nameLen = WideCharToMultiByte(CP_ACP, 0, wideName, nameLen,
1015 pszNameString, cchNameString, NULL, NULL);
1016 if (nameLen <= cchNameString)
1020 pszNameString[cchNameString - 1] = '\0';
1021 ret = cchNameString;
1023 CryptMemFree(wideName);
1027 *pszNameString = '\0';
1032 ret = CertGetNameStringW(pCertContext, dwType, dwFlags, pvTypePara,
1037 /* Searches cert's extensions for the alternate name extension with OID
1038 * altNameOID, and if found, searches it for the alternate name type entryType.
1039 * If found, returns a pointer to the entry, otherwise returns NULL.
1040 * Regardless of whether an entry of the desired type is found, if the
1041 * alternate name extension is present, sets *info to the decoded alternate
1042 * name extension, which you must free using LocalFree.
1043 * The return value is a pointer within *info, so don't free *info before
1044 * you're done with the return value.
1046 static PCERT_ALT_NAME_ENTRY cert_find_alt_name_entry(PCCERT_CONTEXT cert,
1047 LPCSTR altNameOID, DWORD entryType, PCERT_ALT_NAME_INFO *info)
1049 PCERT_ALT_NAME_ENTRY entry = NULL;
1050 PCERT_EXTENSION ext = CertFindExtension(altNameOID,
1051 cert->pCertInfo->cExtension, cert->pCertInfo->rgExtension);
1057 if (CryptDecodeObjectEx(cert->dwCertEncodingType, X509_ALTERNATE_NAME,
1058 ext->Value.pbData, ext->Value.cbData, CRYPT_DECODE_ALLOC_FLAG, NULL,
1063 for (i = 0; !entry && i < (*info)->cAltEntry; i++)
1064 if ((*info)->rgAltEntry[i].dwAltNameChoice == entryType)
1065 entry = &(*info)->rgAltEntry[i];
1073 static DWORD cert_get_name_from_rdn_attr(DWORD encodingType,
1074 const CERT_NAME_BLOB *name, LPCSTR oid, LPWSTR pszNameString, DWORD cchNameString)
1076 CERT_NAME_INFO *nameInfo;
1077 DWORD bytes = 0, ret = 0;
1079 if (CryptDecodeObjectEx(encodingType, X509_NAME, name->pbData,
1080 name->cbData, CRYPT_DECODE_ALLOC_FLAG, NULL, &nameInfo, &bytes))
1082 PCERT_RDN_ATTR nameAttr = CertFindRDNAttr(oid, nameInfo);
1085 ret = CertRDNValueToStrW(nameAttr->dwValueType, &nameAttr->Value,
1086 pszNameString, cchNameString);
1087 LocalFree(nameInfo);
1092 DWORD WINAPI CertGetNameStringW(PCCERT_CONTEXT pCertContext, DWORD dwType,
1093 DWORD dwFlags, void *pvTypePara, LPWSTR pszNameString, DWORD cchNameString)
1096 PCERT_NAME_BLOB name;
1099 TRACE("(%p, %d, %08x, %p, %p, %d)\n", pCertContext, dwType,
1100 dwFlags, pvTypePara, pszNameString, cchNameString);
1102 if (dwFlags & CERT_NAME_ISSUER_FLAG)
1104 name = &pCertContext->pCertInfo->Issuer;
1105 altNameOID = szOID_ISSUER_ALT_NAME;
1109 name = &pCertContext->pCertInfo->Subject;
1110 altNameOID = szOID_SUBJECT_ALT_NAME;
1115 case CERT_NAME_EMAIL_TYPE:
1117 CERT_ALT_NAME_INFO *info;
1118 PCERT_ALT_NAME_ENTRY entry = cert_find_alt_name_entry(pCertContext,
1119 altNameOID, CERT_ALT_NAME_RFC822_NAME, &info);
1124 ret = strlenW(entry->u.pwszRfc822Name) + 1;
1125 else if (cchNameString)
1127 ret = min(strlenW(entry->u.pwszRfc822Name), cchNameString - 1);
1128 memcpy(pszNameString, entry->u.pwszRfc822Name,
1129 ret * sizeof(WCHAR));
1130 pszNameString[ret++] = 0;
1136 ret = cert_get_name_from_rdn_attr(pCertContext->dwCertEncodingType,
1137 name, szOID_RSA_emailAddr, pszNameString, cchNameString);
1140 case CERT_NAME_RDN_TYPE:
1142 ret = CertNameToStrW(pCertContext->dwCertEncodingType, name,
1143 *(DWORD *)pvTypePara, pszNameString, cchNameString);
1146 CERT_ALT_NAME_INFO *info;
1147 PCERT_ALT_NAME_ENTRY entry = cert_find_alt_name_entry(pCertContext,
1148 altNameOID, CERT_ALT_NAME_DIRECTORY_NAME, &info);
1151 ret = CertNameToStrW(pCertContext->dwCertEncodingType,
1152 &entry->u.DirectoryName, *(DWORD *)pvTypePara, pszNameString,
1158 case CERT_NAME_ATTR_TYPE:
1159 ret = cert_get_name_from_rdn_attr(pCertContext->dwCertEncodingType,
1160 name, pvTypePara, pszNameString, cchNameString);
1163 CERT_ALT_NAME_INFO *altInfo;
1164 PCERT_ALT_NAME_ENTRY entry = cert_find_alt_name_entry(pCertContext,
1165 altNameOID, CERT_ALT_NAME_DIRECTORY_NAME, &altInfo);
1168 ret = cert_name_to_str_with_indent(X509_ASN_ENCODING, 0,
1169 &entry->u.DirectoryName, 0, pszNameString, cchNameString);
1174 case CERT_NAME_SIMPLE_DISPLAY_TYPE:
1176 static const LPCSTR simpleAttributeOIDs[] = { szOID_COMMON_NAME,
1177 szOID_ORGANIZATIONAL_UNIT_NAME, szOID_ORGANIZATION_NAME,
1178 szOID_RSA_emailAddr };
1179 CERT_NAME_INFO *nameInfo = NULL;
1182 if (CryptDecodeObjectEx(pCertContext->dwCertEncodingType, X509_NAME,
1183 name->pbData, name->cbData, CRYPT_DECODE_ALLOC_FLAG, NULL, &nameInfo,
1186 PCERT_RDN_ATTR nameAttr = NULL;
1188 for (i = 0; !nameAttr && i < sizeof(simpleAttributeOIDs) /
1189 sizeof(simpleAttributeOIDs[0]); i++)
1190 nameAttr = CertFindRDNAttr(simpleAttributeOIDs[i], nameInfo);
1192 ret = CertRDNValueToStrW(nameAttr->dwValueType,
1193 &nameAttr->Value, pszNameString, cchNameString);
1194 LocalFree(nameInfo);
1198 CERT_ALT_NAME_INFO *altInfo;
1199 PCERT_ALT_NAME_ENTRY entry = cert_find_alt_name_entry(pCertContext,
1200 altNameOID, CERT_ALT_NAME_RFC822_NAME, &altInfo);
1204 if (!entry && altInfo->cAltEntry)
1205 entry = &altInfo->rgAltEntry[0];
1209 ret = strlenW(entry->u.pwszRfc822Name) + 1;
1210 else if (cchNameString)
1212 ret = min(strlenW(entry->u.pwszRfc822Name),
1214 memcpy(pszNameString, entry->u.pwszRfc822Name,
1215 ret * sizeof(WCHAR));
1216 pszNameString[ret++] = 0;
1224 case CERT_NAME_FRIENDLY_DISPLAY_TYPE:
1226 DWORD cch = cchNameString;
1228 if (CertGetCertificateContextProperty(pCertContext,
1229 CERT_FRIENDLY_NAME_PROP_ID, pszNameString, &cch))
1232 ret = CertGetNameStringW(pCertContext,
1233 CERT_NAME_SIMPLE_DISPLAY_TYPE, dwFlags, pvTypePara, pszNameString,
1237 case CERT_NAME_DNS_TYPE:
1239 CERT_ALT_NAME_INFO *info;
1240 PCERT_ALT_NAME_ENTRY entry = cert_find_alt_name_entry(pCertContext,
1241 altNameOID, CERT_ALT_NAME_DNS_NAME, &info);
1246 ret = strlenW(entry->u.pwszDNSName) + 1;
1247 else if (cchNameString)
1249 ret = min(strlenW(entry->u.pwszDNSName), cchNameString - 1);
1250 memcpy(pszNameString, entry->u.pwszDNSName, ret * sizeof(WCHAR));
1251 pszNameString[ret++] = 0;
1257 ret = cert_get_name_from_rdn_attr(pCertContext->dwCertEncodingType,
1258 name, szOID_COMMON_NAME, pszNameString, cchNameString);
1261 case CERT_NAME_URL_TYPE:
1263 CERT_ALT_NAME_INFO *info;
1264 PCERT_ALT_NAME_ENTRY entry = cert_find_alt_name_entry(pCertContext,
1265 altNameOID, CERT_ALT_NAME_URL, &info);
1270 ret = strlenW(entry->u.pwszURL) + 1;
1271 else if (cchNameString)
1273 ret = min(strlenW(entry->u.pwszURL), cchNameString - 1);
1274 memcpy(pszNameString, entry->u.pwszURL, ret * sizeof(WCHAR));
1275 pszNameString[ret++] = 0;
1283 FIXME("unimplemented for type %d\n", dwType);
1290 else if (cchNameString)
1292 pszNameString[0] = 0;