crypt32: Use set encoding function for encoding PKCS signed info.
[wine/multimedia.git] / dlls / crypt32 / str.c
blobef2d2e3815f6dd5899dfb513e02176fe0a0268bb
1 /*
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
18 #include <stdarg.h>
19 #include "windef.h"
20 #include "winbase.h"
21 #include "winnls.h"
22 #include "winuser.h"
23 #include "wincrypt.h"
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,
30 LPSTR psz, DWORD csz)
32 DWORD ret = 0;
34 TRACE("(%d, %p, %p, %d)\n", dwValueType, pValue, psz, csz);
36 switch (dwValueType)
38 case CERT_RDN_ANY_TYPE:
39 break;
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:
48 if (!psz || !csz)
49 ret = pValue->cbData;
50 else
52 DWORD chars = min(pValue->cbData, csz - 1);
54 if (chars)
56 memcpy(psz, pValue->pbData, chars);
57 ret += chars;
58 csz -= chars;
61 break;
62 default:
63 FIXME("string type %d unimplemented\n", dwValueType);
65 if (psz && csz)
67 *(psz + ret) = '\0';
68 csz--;
69 ret++;
71 else
72 ret++;
73 TRACE("returning %d (%s)\n", ret, debugstr_a(psz));
74 return ret;
77 DWORD WINAPI CertRDNValueToStrW(DWORD dwValueType, PCERT_RDN_VALUE_BLOB pValue,
78 LPWSTR psz, DWORD csz)
80 DWORD ret = 0;
82 TRACE("(%d, %p, %p, %d)\n", dwValueType, pValue, psz, csz);
84 switch (dwValueType)
86 case CERT_RDN_ANY_TYPE:
87 break;
88 case CERT_RDN_NUMERIC_STRING:
89 case CERT_RDN_PRINTABLE_STRING:
90 case CERT_RDN_TELETEX_STRING:
91 case CERT_RDN_VIDEOTEX_STRING:
92 case CERT_RDN_IA5_STRING:
93 case CERT_RDN_GRAPHIC_STRING:
94 case CERT_RDN_VISIBLE_STRING:
95 case CERT_RDN_GENERAL_STRING:
96 if (!psz || !csz)
97 ret = pValue->cbData;
98 else
100 DWORD chars = min(pValue->cbData, csz - 1);
102 if (chars)
104 DWORD i;
106 for (i = 0; i < chars; i++)
107 psz[i] = pValue->pbData[i];
108 ret += chars;
109 csz -= chars;
112 break;
113 default:
114 FIXME("string type %d unimplemented\n", dwValueType);
116 if (psz && csz)
118 *(psz + ret) = '\0';
119 csz--;
120 ret++;
122 else
123 ret++;
124 TRACE("returning %d (%s)\n", ret, debugstr_w(psz));
125 return ret;
128 /* Adds the prefix prefix to the string pointed to by psz, followed by the
129 * character '='. Copies no more than csz characters. Returns the number of
130 * characters copied. If psz is NULL, returns the number of characters that
131 * would be copied.
133 static DWORD CRYPT_AddPrefixA(LPCSTR prefix, LPSTR psz, DWORD csz)
135 DWORD chars;
137 TRACE("(%s, %p, %d)\n", debugstr_a(prefix), psz, csz);
139 if (psz)
141 chars = min(lstrlenA(prefix), csz);
142 memcpy(psz, prefix, chars);
143 csz -= chars;
144 *(psz + chars) = '=';
145 chars++;
146 csz--;
148 else
149 chars = lstrlenA(prefix) + 1;
150 return chars;
153 DWORD WINAPI CertNameToStrA(DWORD dwCertEncodingType, PCERT_NAME_BLOB pName,
154 DWORD dwStrType, LPSTR psz, DWORD csz)
156 static const DWORD unsupportedFlags = CERT_NAME_STR_NO_QUOTING_FLAG |
157 CERT_NAME_STR_REVERSE_FLAG | CERT_NAME_STR_ENABLE_T61_UNICODE_FLAG;
158 static const char commaSep[] = ", ";
159 static const char semiSep[] = "; ";
160 static const char crlfSep[] = "\r\n";
161 static const char plusSep[] = " + ";
162 static const char spaceSep[] = " ";
163 DWORD ret = 0, bytes = 0;
164 BOOL bRet;
165 CERT_NAME_INFO *info;
167 TRACE("(%d, %p, %08x, %p, %d)\n", dwCertEncodingType, pName, dwStrType,
168 psz, csz);
169 if (dwStrType & unsupportedFlags)
170 FIXME("unsupported flags: %08x\n", dwStrType & unsupportedFlags);
172 bRet = CryptDecodeObjectEx(dwCertEncodingType, X509_NAME, pName->pbData,
173 pName->cbData, CRYPT_DECODE_ALLOC_FLAG, NULL, &info, &bytes);
174 if (bRet)
176 DWORD i, j, sepLen, rdnSepLen;
177 LPCSTR sep, rdnSep;
179 if (dwStrType & CERT_NAME_STR_SEMICOLON_FLAG)
180 sep = semiSep;
181 else if (dwStrType & CERT_NAME_STR_CRLF_FLAG)
182 sep = crlfSep;
183 else
184 sep = commaSep;
185 sepLen = strlen(sep);
186 if (dwStrType & CERT_NAME_STR_NO_PLUS_FLAG)
187 rdnSep = spaceSep;
188 else
189 rdnSep = plusSep;
190 rdnSepLen = strlen(rdnSep);
191 for (i = 0; (!psz || ret < csz) && i < info->cRDN; i++)
193 for (j = 0; (!psz || ret < csz) && j < info->rgRDN[i].cRDNAttr; j++)
195 DWORD chars;
196 char prefixBuf[10]; /* big enough for GivenName */
197 LPCSTR prefix = NULL;
199 if ((dwStrType & 0x000000ff) == CERT_OID_NAME_STR)
200 prefix = info->rgRDN[i].rgRDNAttr[j].pszObjId;
201 else if ((dwStrType & 0x000000ff) == CERT_X500_NAME_STR)
203 PCCRYPT_OID_INFO oidInfo = CryptFindOIDInfo(
204 CRYPT_OID_INFO_OID_KEY,
205 info->rgRDN[i].rgRDNAttr[j].pszObjId,
206 CRYPT_RDN_ATTR_OID_GROUP_ID);
208 if (oidInfo)
210 WideCharToMultiByte(CP_ACP, 0, oidInfo->pwszName, -1,
211 prefixBuf, sizeof(prefixBuf), NULL, NULL);
212 prefix = prefixBuf;
214 else
215 prefix = info->rgRDN[i].rgRDNAttr[j].pszObjId;
217 if (prefix)
219 /* - 1 is needed to account for the NULL terminator. */
220 chars = CRYPT_AddPrefixA(prefix,
221 psz ? psz + ret : NULL, psz ? csz - ret - 1 : 0);
222 ret += chars;
223 csz -= chars;
225 /* FIXME: handle quoting */
226 chars = CertRDNValueToStrA(
227 info->rgRDN[i].rgRDNAttr[j].dwValueType,
228 &info->rgRDN[i].rgRDNAttr[j].Value, psz ? psz + ret : NULL,
229 psz ? csz - ret : 0);
230 if (chars)
231 ret += chars - 1;
232 if (j < info->rgRDN[i].cRDNAttr - 1)
234 if (psz && ret < csz - rdnSepLen - 1)
235 memcpy(psz + ret, rdnSep, rdnSepLen);
236 ret += rdnSepLen;
239 if (i < info->cRDN - 1)
241 if (psz && ret < csz - sepLen - 1)
242 memcpy(psz + ret, sep, sepLen);
243 ret += sepLen;
246 LocalFree(info);
248 if (psz && csz)
250 *(psz + ret) = '\0';
251 csz--;
252 ret++;
254 else
255 ret++;
256 TRACE("Returning %s\n", debugstr_a(psz));
257 return ret;
260 /* Adds the prefix prefix to the wide-character string pointed to by psz,
261 * followed by the character '='. Copies no more than csz characters. Returns
262 * the number of characters copied. If psz is NULL, returns the number of
263 * characters that would be copied.
264 * Assumes the characters in prefix are ASCII (not multibyte characters.)
266 static DWORD CRYPT_AddPrefixAToW(LPCSTR prefix, LPWSTR psz, DWORD csz)
268 DWORD chars;
270 TRACE("(%s, %p, %d)\n", debugstr_a(prefix), psz, csz);
272 if (psz)
274 DWORD i;
276 chars = min(lstrlenA(prefix), csz);
277 for (i = 0; i < chars; i++)
278 *(psz + i) = prefix[i];
279 csz -= chars;
280 *(psz + chars) = '=';
281 chars++;
282 csz--;
284 else
285 chars = lstrlenA(prefix) + 1;
286 return chars;
289 /* Adds the prefix prefix to the string pointed to by psz, followed by the
290 * character '='. Copies no more than csz characters. Returns the number of
291 * characters copied. If psz is NULL, returns the number of characters that
292 * would be copied.
294 static DWORD CRYPT_AddPrefixW(LPCWSTR prefix, LPWSTR psz, DWORD csz)
296 DWORD chars;
298 TRACE("(%s, %p, %d)\n", debugstr_w(prefix), psz, csz);
300 if (psz)
302 chars = min(lstrlenW(prefix), csz);
303 memcpy(psz, prefix, chars * sizeof(WCHAR));
304 csz -= chars;
305 *(psz + chars) = '=';
306 chars++;
307 csz--;
309 else
310 chars = lstrlenW(prefix) + 1;
311 return chars;
314 DWORD WINAPI CertNameToStrW(DWORD dwCertEncodingType, PCERT_NAME_BLOB pName,
315 DWORD dwStrType, LPWSTR psz, DWORD csz)
317 static const DWORD unsupportedFlags = CERT_NAME_STR_NO_QUOTING_FLAG |
318 CERT_NAME_STR_REVERSE_FLAG | CERT_NAME_STR_ENABLE_T61_UNICODE_FLAG;
319 static const WCHAR commaSep[] = { ',',' ',0 };
320 static const WCHAR semiSep[] = { ';',' ',0 };
321 static const WCHAR crlfSep[] = { '\r','\n',0 };
322 static const WCHAR plusSep[] = { ' ','+',' ',0 };
323 static const WCHAR spaceSep[] = { ' ',0 };
324 DWORD ret = 0, bytes = 0;
325 BOOL bRet;
326 CERT_NAME_INFO *info;
328 TRACE("(%d, %p, %08x, %p, %d)\n", dwCertEncodingType, pName, dwStrType,
329 psz, csz);
330 if (dwStrType & unsupportedFlags)
331 FIXME("unsupported flags: %08x\n", dwStrType & unsupportedFlags);
333 bRet = CryptDecodeObjectEx(dwCertEncodingType, X509_NAME, pName->pbData,
334 pName->cbData, CRYPT_DECODE_ALLOC_FLAG, NULL, &info, &bytes);
335 if (bRet)
337 DWORD i, j, sepLen, rdnSepLen;
338 LPCWSTR sep, rdnSep;
340 if (dwStrType & CERT_NAME_STR_SEMICOLON_FLAG)
341 sep = semiSep;
342 else if (dwStrType & CERT_NAME_STR_CRLF_FLAG)
343 sep = crlfSep;
344 else
345 sep = commaSep;
346 sepLen = lstrlenW(sep);
347 if (dwStrType & CERT_NAME_STR_NO_PLUS_FLAG)
348 rdnSep = spaceSep;
349 else
350 rdnSep = plusSep;
351 rdnSepLen = lstrlenW(rdnSep);
352 for (i = 0; (!psz || ret < csz) && i < info->cRDN; i++)
354 for (j = 0; (!psz || ret < csz) && j < info->rgRDN[i].cRDNAttr; j++)
356 DWORD chars;
357 LPCSTR prefixA = NULL;
358 LPCWSTR prefixW = NULL;
360 if ((dwStrType & 0x000000ff) == CERT_OID_NAME_STR)
361 prefixA = info->rgRDN[i].rgRDNAttr[j].pszObjId;
362 else if ((dwStrType & 0x000000ff) == CERT_X500_NAME_STR)
364 PCCRYPT_OID_INFO oidInfo = CryptFindOIDInfo(
365 CRYPT_OID_INFO_OID_KEY,
366 info->rgRDN[i].rgRDNAttr[j].pszObjId,
367 CRYPT_RDN_ATTR_OID_GROUP_ID);
369 if (oidInfo)
370 prefixW = oidInfo->pwszName;
371 else
372 prefixA = info->rgRDN[i].rgRDNAttr[j].pszObjId;
374 if (prefixW)
376 /* - 1 is needed to account for the NULL terminator. */
377 chars = CRYPT_AddPrefixW(prefixW,
378 psz ? psz + ret : NULL, psz ? csz - ret - 1 : 0);
379 ret += chars;
380 csz -= chars;
382 else if (prefixA)
384 /* - 1 is needed to account for the NULL terminator. */
385 chars = CRYPT_AddPrefixAToW(prefixA,
386 psz ? psz + ret : NULL, psz ? csz - ret - 1 : 0);
387 ret += chars;
388 csz -= chars;
390 /* FIXME: handle quoting */
391 chars = CertRDNValueToStrW(
392 info->rgRDN[i].rgRDNAttr[j].dwValueType,
393 &info->rgRDN[i].rgRDNAttr[j].Value, psz ? psz + ret : NULL,
394 psz ? csz - ret : 0);
395 if (chars)
396 ret += chars - 1;
397 if (j < info->rgRDN[i].cRDNAttr - 1)
399 if (psz && ret < csz - rdnSepLen - 1)
400 memcpy(psz + ret, rdnSep, rdnSepLen * sizeof(WCHAR));
401 ret += rdnSepLen;
404 if (i < info->cRDN - 1)
406 if (psz && ret < csz - sepLen - 1)
407 memcpy(psz + ret, sep, sepLen * sizeof(WCHAR));
408 ret += sepLen;
411 LocalFree(info);
413 if (psz && csz)
415 *(psz + ret) = '\0';
416 csz--;
417 ret++;
419 else
420 ret++;
421 TRACE("Returning %s\n", debugstr_w(psz));
422 return ret;
425 BOOL WINAPI CertStrToNameA(DWORD dwCertEncodingType, LPCSTR pszX500,
426 DWORD dwStrType, void *pvReserved, BYTE *pbEncoded, DWORD *pcbEncoded,
427 LPCSTR *ppszError)
429 LPWSTR x500, errorStr;
430 BOOL ret;
431 int len;
433 TRACE("(%08x, %s, %08x, %p, %p, %p, %p)\n", dwCertEncodingType,
434 debugstr_a(pszX500), dwStrType, pvReserved, pbEncoded, pcbEncoded,
435 ppszError);
437 len = MultiByteToWideChar(CP_ACP, 0, pszX500, -1, NULL, 0);
438 x500 = CryptMemAlloc(len * sizeof(WCHAR));
439 if (x500)
441 MultiByteToWideChar(CP_ACP, 0, pszX500, -1, x500, len);
442 ret = CertStrToNameW(dwCertEncodingType, x500, dwStrType, pvReserved,
443 pbEncoded, pcbEncoded, ppszError ? (LPCWSTR *)&errorStr : NULL);
444 if (ppszError)
446 DWORD i;
448 *ppszError = pszX500;
449 for (i = 0; i < errorStr - x500; i++)
450 *ppszError = CharNextA(*ppszError);
452 CryptMemFree(x500);
454 else
455 ret = FALSE;
456 return ret;
459 struct KeynameKeeper
461 WCHAR buf[10]; /* big enough for L"GivenName" */
462 LPWSTR keyName; /* usually = buf, but may be allocated */
463 DWORD keyLen;
466 static void CRYPT_InitializeKeynameKeeper(struct KeynameKeeper *keeper)
468 keeper->keyName = keeper->buf;
469 keeper->keyLen = sizeof(keeper->buf) / sizeof(keeper->buf[0]);
472 static void CRYPT_FreeKeynameKeeper(struct KeynameKeeper *keeper)
474 if (keeper->keyName != keeper->buf)
475 CryptMemFree(keeper->keyName);
478 struct X500TokenW
480 LPCWSTR start;
481 LPCWSTR end;
484 static void CRYPT_KeynameKeeperFromTokenW(struct KeynameKeeper *keeper,
485 const struct X500TokenW *key)
487 DWORD len = key->end - key->start;
489 if (len > keeper->keyLen)
491 if (keeper->keyName == keeper->buf)
492 keeper->keyName = CryptMemAlloc(len * sizeof(WCHAR));
493 else
494 keeper->keyName = CryptMemRealloc(keeper->keyName,
495 len * sizeof(WCHAR));
496 keeper->keyLen = len;
498 memcpy(keeper->keyName, key->start, (key->end - key->start) *
499 sizeof(WCHAR));
500 keeper->keyName[len] = '\0';
501 TRACE("Keyname is %s\n", debugstr_w(keeper->keyName));
504 static DWORD CRYPT_GetNextKeyW(LPCWSTR str, struct X500TokenW *token,
505 LPCWSTR *ppszError)
507 DWORD ret = ERROR_SUCCESS;
509 while (*str && isspaceW(*str))
510 str++;
511 if (*str)
513 token->start = str;
514 while (*str && *str != '=' && !isspaceW(*str))
515 str++;
516 if (*str && (*str == '=' || isspaceW(*str)))
517 token->end = str;
518 else
520 TRACE("missing equals char at %s\n", debugstr_w(token->start));
521 if (ppszError)
522 *ppszError = token->start;
523 ret = CRYPT_E_INVALID_X500_STRING;
526 else
527 token->start = NULL;
528 return ret;
531 /* Assumes separators are characters in the 0-255 range */
532 static DWORD CRYPT_GetNextValueW(LPCWSTR str, DWORD dwFlags, LPCWSTR separators,
533 struct X500TokenW *token, LPCWSTR *ppszError)
535 DWORD ret = ERROR_SUCCESS;
537 TRACE("(%s, %s, %p, %p)\n", debugstr_w(str), debugstr_w(separators), token,
538 ppszError);
540 while (*str && isspaceW(*str))
541 str++;
542 if (*str)
544 token->start = str;
545 if (!(dwFlags & CERT_NAME_STR_NO_QUOTING_FLAG) && *str == '"')
547 token->end = NULL;
548 str++;
549 while (!token->end && !ret)
551 while (*str && *str != '"')
552 str++;
553 if (*str == '"')
555 if (*(str + 1) != '"')
556 token->end = str + 1;
557 else
558 str += 2;
560 else
562 TRACE("unterminated quote at %s\n", debugstr_w(str));
563 if (ppszError)
564 *ppszError = str;
565 ret = CRYPT_E_INVALID_X500_STRING;
569 else
571 WCHAR map[256] = { 0 };
573 while (*separators)
574 map[*separators++] = 1;
575 while (*str && (*str >= 0xff || !map[*(const unsigned short *)str]))
576 str++;
577 token->end = str;
580 else
582 TRACE("missing value at %s\n", debugstr_w(str));
583 if (ppszError)
584 *ppszError = str;
585 ret = CRYPT_E_INVALID_X500_STRING;
587 return ret;
590 /* Encodes the string represented by value as the string type type into the
591 * CERT_NAME_BLOB output. If there is an error and ppszError is not NULL,
592 * *ppszError is set to the first failing character. If there is no error,
593 * output's pbData must be freed with LocalFree.
595 static BOOL CRYPT_EncodeValueWithType(DWORD dwCertEncodingType,
596 const struct X500TokenW *value, PCERT_NAME_BLOB output, DWORD type,
597 LPCWSTR *ppszError)
599 CERT_NAME_VALUE nameValue = { type, { 0, NULL } };
600 BOOL ret = FALSE;
602 nameValue.Value.pbData = CryptMemAlloc((value->end - value->start) *
603 sizeof(WCHAR));
604 if (nameValue.Value.pbData)
606 DWORD i;
607 LPWSTR ptr = (LPWSTR)nameValue.Value.pbData;
609 for (i = 0; i < value->end - value->start; i++)
611 *ptr++ = value->start[i];
612 if (value->start[i] == '"')
613 i++;
615 nameValue.Value.cbData = (LPBYTE)ptr - nameValue.Value.pbData;
616 ret = CryptEncodeObjectEx(dwCertEncodingType, X509_UNICODE_NAME_VALUE,
617 &nameValue, CRYPT_ENCODE_ALLOC_FLAG, NULL, &output->pbData,
618 &output->cbData);
619 if (!ret && ppszError)
621 if (type == CERT_RDN_NUMERIC_STRING &&
622 GetLastError() == CRYPT_E_INVALID_NUMERIC_STRING)
623 *ppszError = value->start + output->cbData;
624 else if (type == CERT_RDN_PRINTABLE_STRING &&
625 GetLastError() == CRYPT_E_INVALID_PRINTABLE_STRING)
626 *ppszError = value->start + output->cbData;
627 else if (type == CERT_RDN_IA5_STRING &&
628 GetLastError() == CRYPT_E_INVALID_IA5_STRING)
629 *ppszError = value->start + output->cbData;
631 CryptMemFree(nameValue.Value.pbData);
633 return ret;
636 static BOOL CRYPT_EncodeValue(DWORD dwCertEncodingType,
637 const struct X500TokenW *value, PCERT_NAME_BLOB output, const DWORD *types,
638 LPCWSTR *ppszError)
640 DWORD i;
641 BOOL ret;
643 ret = FALSE;
644 for (i = 0; !ret && types[i]; i++)
645 ret = CRYPT_EncodeValueWithType(dwCertEncodingType, value, output,
646 types[i], ppszError);
647 return ret;
650 static BOOL CRYPT_ValueToRDN(DWORD dwCertEncodingType, PCERT_NAME_INFO info,
651 PCCRYPT_OID_INFO keyOID, struct X500TokenW *value, LPCWSTR *ppszError)
653 BOOL ret = FALSE;
655 TRACE("OID %s, value %s\n", debugstr_a(keyOID->pszOID),
656 debugstr_wn(value->start, value->end - value->start));
658 if (!info->rgRDN)
659 info->rgRDN = CryptMemAlloc(sizeof(CERT_RDN));
660 else
661 info->rgRDN = CryptMemRealloc(info->rgRDN,
662 (info->cRDN + 1) * sizeof(CERT_RDN));
663 if (info->rgRDN)
665 /* FIXME: support multiple RDN attrs */
666 info->rgRDN[info->cRDN].rgRDNAttr =
667 CryptMemAlloc(sizeof(CERT_RDN_ATTR));
668 if (info->rgRDN[info->cRDN].rgRDNAttr)
670 static const DWORD defaultTypes[] = { CERT_RDN_PRINTABLE_STRING,
671 CERT_RDN_BMP_STRING, 0 };
672 const DWORD *types;
674 info->rgRDN[info->cRDN].cRDNAttr = 1;
675 info->rgRDN[info->cRDN].rgRDNAttr[0].pszObjId =
676 (LPSTR)keyOID->pszOID;
677 info->rgRDN[info->cRDN].rgRDNAttr[0].dwValueType =
678 CERT_RDN_ENCODED_BLOB;
679 if (keyOID->ExtraInfo.cbData)
680 types = (const DWORD *)keyOID->ExtraInfo.pbData;
681 else
682 types = defaultTypes;
684 /* Remove surrounding quotes */
685 if (value->start[0] == '"')
687 value->start++;
688 value->end--;
690 ret = CRYPT_EncodeValue(dwCertEncodingType, value,
691 &info->rgRDN[info->cRDN].rgRDNAttr[0].Value, types, ppszError);
694 if (ret)
695 info->cRDN++;
696 return ret;
699 BOOL WINAPI CertStrToNameW(DWORD dwCertEncodingType, LPCWSTR pszX500,
700 DWORD dwStrType, void *pvReserved, BYTE *pbEncoded, DWORD *pcbEncoded,
701 LPCWSTR *ppszError)
703 CERT_NAME_INFO info = { 0, NULL };
704 LPCWSTR str;
705 struct KeynameKeeper keeper;
706 DWORD i, error = ERROR_SUCCESS;
707 BOOL ret = TRUE;
709 TRACE("(%08x, %s, %08x, %p, %p, %p, %p)\n", dwCertEncodingType,
710 debugstr_w(pszX500), dwStrType, pvReserved, pbEncoded, pcbEncoded,
711 ppszError);
713 CRYPT_InitializeKeynameKeeper(&keeper);
714 str = pszX500;
715 while (str && *str && !error && ret)
717 struct X500TokenW token;
719 error = CRYPT_GetNextKeyW(str, &token, ppszError);
720 if (!error && token.start)
722 PCCRYPT_OID_INFO keyOID;
724 CRYPT_KeynameKeeperFromTokenW(&keeper, &token);
725 keyOID = CryptFindOIDInfo(CRYPT_OID_INFO_NAME_KEY, keeper.keyName,
726 CRYPT_RDN_ATTR_OID_GROUP_ID);
727 if (!keyOID)
729 if (ppszError)
730 *ppszError = token.start;
731 error = CRYPT_E_INVALID_X500_STRING;
733 else
735 str = token.end;
736 while (isspace(*str))
737 str++;
738 if (*str != '=')
740 if (ppszError)
741 *ppszError = str;
742 error = CRYPT_E_INVALID_X500_STRING;
744 else
746 static const WCHAR commaSep[] = { ',',0 };
747 static const WCHAR semiSep[] = { ';',0 };
748 static const WCHAR crlfSep[] = { '\r','\n',0 };
749 static const WCHAR allSeps[] = { ',',';','\r','\n',0 };
750 LPCWSTR sep;
752 str++;
753 if (dwStrType & CERT_NAME_STR_COMMA_FLAG)
754 sep = commaSep;
755 else if (dwStrType & CERT_NAME_STR_SEMICOLON_FLAG)
756 sep = semiSep;
757 else if (dwStrType & CERT_NAME_STR_CRLF_FLAG)
758 sep = crlfSep;
759 else
760 sep = allSeps;
761 error = CRYPT_GetNextValueW(str, dwStrType, sep, &token,
762 ppszError);
763 if (!error)
765 str = token.end;
766 ret = CRYPT_ValueToRDN(dwCertEncodingType, &info,
767 keyOID, &token, ppszError);
773 CRYPT_FreeKeynameKeeper(&keeper);
774 if (!error)
776 ret = CryptEncodeObjectEx(dwCertEncodingType, X509_NAME, &info,
777 0, NULL, pbEncoded, pcbEncoded);
778 for (i = 0; i < info.cRDN; i++)
780 DWORD j;
782 for (j = 0; j < info.rgRDN[i].cRDNAttr; j++)
783 LocalFree(info.rgRDN[i].rgRDNAttr[j].Value.pbData);
784 CryptMemFree(info.rgRDN[i].rgRDNAttr);
786 CryptMemFree(info.rgRDN);
788 else
790 SetLastError(error);
791 ret = FALSE;
793 return ret;
796 DWORD WINAPI CertGetNameStringA(PCCERT_CONTEXT pCertContext, DWORD dwType,
797 DWORD dwFlags, void *pvTypePara, LPSTR pszNameString, DWORD cchNameString)
799 DWORD ret;
801 TRACE("(%p, %d, %08x, %p, %p, %d)\n", pCertContext, dwType, dwFlags,
802 pvTypePara, pszNameString, cchNameString);
804 if (pszNameString)
806 LPWSTR wideName;
807 DWORD nameLen;
809 nameLen = CertGetNameStringW(pCertContext, dwType, dwFlags, pvTypePara,
810 NULL, 0);
811 wideName = CryptMemAlloc(nameLen * sizeof(WCHAR));
812 if (wideName)
814 CertGetNameStringW(pCertContext, dwType, dwFlags, pvTypePara,
815 wideName, nameLen);
816 nameLen = WideCharToMultiByte(CP_ACP, 0, wideName, nameLen,
817 pszNameString, cchNameString, NULL, NULL);
818 if (nameLen <= cchNameString)
819 ret = nameLen;
820 else
822 pszNameString[cchNameString - 1] = '\0';
823 ret = cchNameString;
825 CryptMemFree(wideName);
827 else
829 *pszNameString = '\0';
830 ret = 1;
833 else
834 ret = CertGetNameStringW(pCertContext, dwType, dwFlags, pvTypePara,
835 NULL, 0);
836 return ret;
839 DWORD WINAPI CertGetNameStringW(PCCERT_CONTEXT pCertContext, DWORD dwType,
840 DWORD dwFlags, void *pvTypePara, LPWSTR pszNameString, DWORD cchNameString)
842 DWORD ret;
843 PCERT_NAME_BLOB name;
844 LPCSTR altNameOID;
846 TRACE("(%p, %d, %08x, %p, %p, %d)\n", pCertContext, dwType,
847 dwFlags, pvTypePara, pszNameString, cchNameString);
849 if (dwFlags & CERT_NAME_ISSUER_FLAG)
851 name = &pCertContext->pCertInfo->Issuer;
852 altNameOID = szOID_ISSUER_ALT_NAME;
854 else
856 name = &pCertContext->pCertInfo->Subject;
857 altNameOID = szOID_SUBJECT_ALT_NAME;
860 switch (dwType)
862 case CERT_NAME_SIMPLE_DISPLAY_TYPE:
864 static const LPCSTR simpleAttributeOIDs[] = { szOID_COMMON_NAME,
865 szOID_ORGANIZATIONAL_UNIT_NAME, szOID_ORGANIZATION_NAME,
866 szOID_RSA_emailAddr };
867 CERT_NAME_INFO *info = NULL;
868 PCERT_RDN_ATTR nameAttr = NULL;
869 DWORD bytes = 0, i;
871 if (CryptDecodeObjectEx(pCertContext->dwCertEncodingType, X509_NAME,
872 name->pbData, name->cbData, CRYPT_DECODE_ALLOC_FLAG, NULL, &info,
873 &bytes))
875 for (i = 0; !nameAttr && i < sizeof(simpleAttributeOIDs) /
876 sizeof(simpleAttributeOIDs[0]); i++)
877 nameAttr = CertFindRDNAttr(simpleAttributeOIDs[i], info);
879 if (!nameAttr)
881 PCERT_EXTENSION ext = CertFindExtension(altNameOID,
882 pCertContext->pCertInfo->cExtension,
883 pCertContext->pCertInfo->rgExtension);
885 if (ext)
887 for (i = 0; !nameAttr && i < sizeof(simpleAttributeOIDs) /
888 sizeof(simpleAttributeOIDs[0]); i++)
889 nameAttr = CertFindRDNAttr(simpleAttributeOIDs[i], info);
890 if (!nameAttr)
892 /* FIXME: gotta then look for a rfc822Name choice in ext.
893 * Failing that, look for the first attribute.
895 FIXME("CERT_NAME_SIMPLE_DISPLAY_TYPE: stub\n");
899 if (nameAttr)
900 ret = CertRDNValueToStrW(nameAttr->dwValueType, &nameAttr->Value,
901 pszNameString, cchNameString);
902 else
903 ret = 0;
904 if (info)
905 LocalFree(info);
906 break;
908 case CERT_NAME_FRIENDLY_DISPLAY_TYPE:
910 DWORD cch = cchNameString;
912 if (CertGetCertificateContextProperty(pCertContext,
913 CERT_FRIENDLY_NAME_PROP_ID, pszNameString, &cch))
914 ret = cch;
915 else
916 ret = CertGetNameStringW(pCertContext,
917 CERT_NAME_SIMPLE_DISPLAY_TYPE, dwFlags, pvTypePara, pszNameString,
918 cchNameString);
919 break;
921 default:
922 FIXME("unimplemented for type %d\n", dwType);
923 ret = 0;
925 return ret;