[CRYPT32] Sync with Wine Staging 1.9.4. CORE-10912
[reactos.git] / reactos / dll / win32 / crypt32 / str.c
1 /*
2 * Copyright 2006 Juan Lang for CodeWeavers
3 *
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.
8 *
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.
13 *
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
17 */
18
19 #include "crypt32_private.h"
20
21 WINE_DEFAULT_DEBUG_CHANNEL(crypt);
22
23 DWORD WINAPI CertRDNValueToStrA(DWORD dwValueType, PCERT_RDN_VALUE_BLOB pValue,
24 LPSTR psz, DWORD csz)
25 {
26 DWORD ret = 0, len;
27
28 TRACE("(%d, %p, %p, %d)\n", dwValueType, pValue, psz, csz);
29
30 switch (dwValueType)
31 {
32 case CERT_RDN_ANY_TYPE:
33 break;
34 case CERT_RDN_NUMERIC_STRING:
35 case CERT_RDN_PRINTABLE_STRING:
36 case CERT_RDN_TELETEX_STRING:
37 case CERT_RDN_VIDEOTEX_STRING:
38 case CERT_RDN_IA5_STRING:
39 case CERT_RDN_GRAPHIC_STRING:
40 case CERT_RDN_VISIBLE_STRING:
41 case CERT_RDN_GENERAL_STRING:
42 len = pValue->cbData;
43 if (!psz || !csz)
44 ret = len;
45 else
46 {
47 DWORD chars = min(len, csz - 1);
48
49 if (chars)
50 {
51 memcpy(psz, pValue->pbData, chars);
52 ret += chars;
53 csz -= chars;
54 }
55 }
56 break;
57 case CERT_RDN_BMP_STRING:
58 case CERT_RDN_UTF8_STRING:
59 len = WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)pValue->pbData,
60 pValue->cbData / sizeof(WCHAR), NULL, 0, NULL, NULL);
61 if (!psz || !csz)
62 ret = len;
63 else
64 {
65 DWORD chars = min(pValue->cbData / sizeof(WCHAR), csz - 1);
66
67 if (chars)
68 {
69 ret = WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)pValue->pbData,
70 chars, psz, csz - 1, NULL, NULL);
71 csz -= ret;
72 }
73 }
74 break;
75 default:
76 FIXME("string type %d unimplemented\n", dwValueType);
77 }
78 if (psz && csz)
79 {
80 *(psz + ret) = '\0';
81 csz--;
82 ret++;
83 }
84 else
85 ret++;
86 TRACE("returning %d (%s)\n", ret, debugstr_a(psz));
87 return ret;
88 }
89
90 DWORD WINAPI CertRDNValueToStrW(DWORD dwValueType, PCERT_RDN_VALUE_BLOB pValue,
91 LPWSTR psz, DWORD csz)
92 {
93 DWORD ret = 0, len, i, strLen;
94
95 TRACE("(%d, %p, %p, %d)\n", dwValueType, pValue, psz, csz);
96
97 switch (dwValueType)
98 {
99 case CERT_RDN_ANY_TYPE:
100 break;
101 case CERT_RDN_NUMERIC_STRING:
102 case CERT_RDN_PRINTABLE_STRING:
103 case CERT_RDN_TELETEX_STRING:
104 case CERT_RDN_VIDEOTEX_STRING:
105 case CERT_RDN_IA5_STRING:
106 case CERT_RDN_GRAPHIC_STRING:
107 case CERT_RDN_VISIBLE_STRING:
108 case CERT_RDN_GENERAL_STRING:
109 len = pValue->cbData;
110 if (!psz || !csz)
111 ret = len;
112 else
113 {
114 WCHAR *ptr = psz;
115
116 for (i = 0; i < pValue->cbData && ptr - psz < csz; ptr++, i++)
117 *ptr = pValue->pbData[i];
118 ret = ptr - psz;
119 }
120 break;
121 case CERT_RDN_BMP_STRING:
122 case CERT_RDN_UTF8_STRING:
123 strLen = len = pValue->cbData / sizeof(WCHAR);
124 if (!psz || !csz)
125 ret = len;
126 else
127 {
128 WCHAR *ptr = psz;
129
130 for (i = 0; i < strLen && ptr - psz < csz; ptr++, i++)
131 *ptr = ((LPCWSTR)pValue->pbData)[i];
132 ret = ptr - psz;
133 }
134 break;
135 default:
136 FIXME("string type %d unimplemented\n", dwValueType);
137 }
138 if (psz && csz)
139 {
140 *(psz + ret) = '\0';
141 csz--;
142 ret++;
143 }
144 else
145 ret++;
146 TRACE("returning %d (%s)\n", ret, debugstr_w(psz));
147 return ret;
148 }
149
150 static inline BOOL is_quotable_char(char c)
151 {
152 switch(c)
153 {
154 case '+':
155 case ',':
156 case '"':
157 case '=':
158 case '<':
159 case '>':
160 case ';':
161 case '#':
162 case '\n':
163 return TRUE;
164 default:
165 return FALSE;
166 }
167 }
168
169 static DWORD quote_rdn_value_to_str_a(DWORD dwValueType,
170 PCERT_RDN_VALUE_BLOB pValue, LPSTR psz, DWORD csz)
171 {
172 DWORD ret = 0, len, i;
173 BOOL needsQuotes = FALSE;
174
175 TRACE("(%d, %p, %p, %d)\n", dwValueType, pValue, psz, csz);
176
177 switch (dwValueType)
178 {
179 case CERT_RDN_ANY_TYPE:
180 break;
181 case CERT_RDN_NUMERIC_STRING:
182 case CERT_RDN_PRINTABLE_STRING:
183 case CERT_RDN_TELETEX_STRING:
184 case CERT_RDN_VIDEOTEX_STRING:
185 case CERT_RDN_IA5_STRING:
186 case CERT_RDN_GRAPHIC_STRING:
187 case CERT_RDN_VISIBLE_STRING:
188 case CERT_RDN_GENERAL_STRING:
189 len = pValue->cbData;
190 if (pValue->cbData && isspace(pValue->pbData[0]))
191 needsQuotes = TRUE;
192 if (pValue->cbData && isspace(pValue->pbData[pValue->cbData - 1]))
193 needsQuotes = TRUE;
194 for (i = 0; i < pValue->cbData; i++)
195 {
196 if (is_quotable_char(pValue->pbData[i]))
197 needsQuotes = TRUE;
198 if (pValue->pbData[i] == '"')
199 len += 1;
200 }
201 if (needsQuotes)
202 len += 2;
203 if (!psz || !csz)
204 ret = len;
205 else
206 {
207 char *ptr = psz;
208
209 if (needsQuotes)
210 *ptr++ = '"';
211 for (i = 0; i < pValue->cbData && ptr - psz < csz; ptr++, i++)
212 {
213 *ptr = pValue->pbData[i];
214 if (pValue->pbData[i] == '"' && ptr - psz < csz - 1)
215 *(++ptr) = '"';
216 }
217 if (needsQuotes && ptr - psz < csz)
218 *ptr++ = '"';
219 ret = ptr - psz;
220 }
221 break;
222 case CERT_RDN_BMP_STRING:
223 case CERT_RDN_UTF8_STRING:
224 len = WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)pValue->pbData,
225 pValue->cbData / sizeof(WCHAR), NULL, 0, NULL, NULL);
226 if (pValue->cbData && isspaceW(((LPCWSTR)pValue->pbData)[0]))
227 needsQuotes = TRUE;
228 if (pValue->cbData &&
229 isspaceW(((LPCWSTR)pValue->pbData)[pValue->cbData / sizeof(WCHAR)-1]))
230 needsQuotes = TRUE;
231 for (i = 0; i < pValue->cbData / sizeof(WCHAR); i++)
232 {
233 if (is_quotable_char(((LPCWSTR)pValue->pbData)[i]))
234 needsQuotes = TRUE;
235 if (((LPCWSTR)pValue->pbData)[i] == '"')
236 len += 1;
237 }
238 if (needsQuotes)
239 len += 2;
240 if (!psz || !csz)
241 ret = len;
242 else
243 {
244 char *dst = psz;
245
246 if (needsQuotes)
247 *dst++ = '"';
248 for (i = 0; i < pValue->cbData / sizeof(WCHAR) &&
249 dst - psz < csz; dst++, i++)
250 {
251 LPCWSTR src = (LPCWSTR)pValue->pbData + i;
252
253 WideCharToMultiByte(CP_ACP, 0, src, 1, dst,
254 csz - (dst - psz) - 1, NULL, NULL);
255 if (*src == '"' && dst - psz < csz - 1)
256 *(++dst) = '"';
257 }
258 if (needsQuotes && dst - psz < csz)
259 *dst++ = '"';
260 ret = dst - psz;
261 }
262 break;
263 default:
264 FIXME("string type %d unimplemented\n", dwValueType);
265 }
266 if (psz && csz)
267 {
268 *(psz + ret) = '\0';
269 csz--;
270 ret++;
271 }
272 else
273 ret++;
274 TRACE("returning %d (%s)\n", ret, debugstr_a(psz));
275 return ret;
276 }
277
278 static DWORD quote_rdn_value_to_str_w(DWORD dwValueType,
279 PCERT_RDN_VALUE_BLOB pValue, LPWSTR psz, DWORD csz)
280 {
281 DWORD ret = 0, len, i, strLen;
282 BOOL needsQuotes = FALSE;
283
284 TRACE("(%d, %p, %p, %d)\n", dwValueType, pValue, psz, csz);
285
286 switch (dwValueType)
287 {
288 case CERT_RDN_ANY_TYPE:
289 break;
290 case CERT_RDN_NUMERIC_STRING:
291 case CERT_RDN_PRINTABLE_STRING:
292 case CERT_RDN_TELETEX_STRING:
293 case CERT_RDN_VIDEOTEX_STRING:
294 case CERT_RDN_IA5_STRING:
295 case CERT_RDN_GRAPHIC_STRING:
296 case CERT_RDN_VISIBLE_STRING:
297 case CERT_RDN_GENERAL_STRING:
298 len = pValue->cbData;
299 if (pValue->cbData && isspace(pValue->pbData[0]))
300 needsQuotes = TRUE;
301 if (pValue->cbData && isspace(pValue->pbData[pValue->cbData - 1]))
302 needsQuotes = TRUE;
303 for (i = 0; i < pValue->cbData; i++)
304 {
305 if (is_quotable_char(pValue->pbData[i]))
306 needsQuotes = TRUE;
307 if (pValue->pbData[i] == '"')
308 len += 1;
309 }
310 if (needsQuotes)
311 len += 2;
312 if (!psz || !csz)
313 ret = len;
314 else
315 {
316 WCHAR *ptr = psz;
317
318 if (needsQuotes)
319 *ptr++ = '"';
320 for (i = 0; i < pValue->cbData && ptr - psz < csz; ptr++, i++)
321 {
322 *ptr = pValue->pbData[i];
323 if (pValue->pbData[i] == '"' && ptr - psz < csz - 1)
324 *(++ptr) = '"';
325 }
326 if (needsQuotes && ptr - psz < csz)
327 *ptr++ = '"';
328 ret = ptr - psz;
329 }
330 break;
331 case CERT_RDN_BMP_STRING:
332 case CERT_RDN_UTF8_STRING:
333 strLen = len = pValue->cbData / sizeof(WCHAR);
334 if (pValue->cbData && isspace(pValue->pbData[0]))
335 needsQuotes = TRUE;
336 if (pValue->cbData && isspace(pValue->pbData[strLen - 1]))
337 needsQuotes = TRUE;
338 for (i = 0; i < strLen; i++)
339 {
340 if (is_quotable_char(((LPCWSTR)pValue->pbData)[i]))
341 needsQuotes = TRUE;
342 if (((LPCWSTR)pValue->pbData)[i] == '"')
343 len += 1;
344 }
345 if (needsQuotes)
346 len += 2;
347 if (!psz || !csz)
348 ret = len;
349 else
350 {
351 WCHAR *ptr = psz;
352
353 if (needsQuotes)
354 *ptr++ = '"';
355 for (i = 0; i < strLen && ptr - psz < csz; ptr++, i++)
356 {
357 *ptr = ((LPCWSTR)pValue->pbData)[i];
358 if (((LPCWSTR)pValue->pbData)[i] == '"' && ptr - psz < csz - 1)
359 *(++ptr) = '"';
360 }
361 if (needsQuotes && ptr - psz < csz)
362 *ptr++ = '"';
363 ret = ptr - psz;
364 }
365 break;
366 default:
367 FIXME("string type %d unimplemented\n", dwValueType);
368 }
369 if (psz && csz)
370 {
371 *(psz + ret) = '\0';
372 csz--;
373 ret++;
374 }
375 else
376 ret++;
377 TRACE("returning %d (%s)\n", ret, debugstr_w(psz));
378 return ret;
379 }
380
381 /* Adds the prefix prefix to the string pointed to by psz, followed by the
382 * character '='. Copies no more than csz characters. Returns the number of
383 * characters copied. If psz is NULL, returns the number of characters that
384 * would be copied.
385 */
386 static DWORD CRYPT_AddPrefixA(LPCSTR prefix, LPSTR psz, DWORD csz)
387 {
388 DWORD chars;
389
390 TRACE("(%s, %p, %d)\n", debugstr_a(prefix), psz, csz);
391
392 if (psz)
393 {
394 chars = min(strlen(prefix), csz);
395 memcpy(psz, prefix, chars);
396 *(psz + chars) = '=';
397 chars++;
398 }
399 else
400 chars = lstrlenA(prefix) + 1;
401 return chars;
402 }
403
404 DWORD WINAPI CertNameToStrA(DWORD dwCertEncodingType, PCERT_NAME_BLOB pName,
405 DWORD dwStrType, LPSTR psz, DWORD csz)
406 {
407 static const DWORD unsupportedFlags = CERT_NAME_STR_NO_QUOTING_FLAG |
408 CERT_NAME_STR_ENABLE_T61_UNICODE_FLAG;
409 static const char commaSep[] = ", ";
410 static const char semiSep[] = "; ";
411 static const char crlfSep[] = "\r\n";
412 static const char plusSep[] = " + ";
413 static const char spaceSep[] = " ";
414 DWORD ret = 0, bytes = 0;
415 BOOL bRet;
416 CERT_NAME_INFO *info;
417
418 TRACE("(%d, %p, %08x, %p, %d)\n", dwCertEncodingType, pName, dwStrType,
419 psz, csz);
420 if (dwStrType & unsupportedFlags)
421 FIXME("unsupported flags: %08x\n", dwStrType & unsupportedFlags);
422
423 bRet = CryptDecodeObjectEx(dwCertEncodingType, X509_NAME, pName->pbData,
424 pName->cbData, CRYPT_DECODE_ALLOC_FLAG, NULL, &info, &bytes);
425 if (bRet)
426 {
427 DWORD i, j, sepLen, rdnSepLen;
428 LPCSTR sep, rdnSep;
429 BOOL reverse = dwStrType & CERT_NAME_STR_REVERSE_FLAG;
430 const CERT_RDN *rdn = info->rgRDN;
431
432 if(reverse && info->cRDN > 1) rdn += (info->cRDN - 1);
433
434 if (dwStrType & CERT_NAME_STR_SEMICOLON_FLAG)
435 sep = semiSep;
436 else if (dwStrType & CERT_NAME_STR_CRLF_FLAG)
437 sep = crlfSep;
438 else
439 sep = commaSep;
440 sepLen = strlen(sep);
441 if (dwStrType & CERT_NAME_STR_NO_PLUS_FLAG)
442 rdnSep = spaceSep;
443 else
444 rdnSep = plusSep;
445 rdnSepLen = strlen(rdnSep);
446 for (i = 0; (!psz || ret < csz) && i < info->cRDN; i++)
447 {
448 for (j = 0; (!psz || ret < csz) && j < rdn->cRDNAttr; j++)
449 {
450 DWORD chars;
451 char prefixBuf[13]; /* big enough for SERIALNUMBER */
452 LPCSTR prefix = NULL;
453
454 if ((dwStrType & 0x000000ff) == CERT_OID_NAME_STR)
455 prefix = rdn->rgRDNAttr[j].pszObjId;
456 else if ((dwStrType & 0x000000ff) == CERT_X500_NAME_STR)
457 {
458 PCCRYPT_OID_INFO oidInfo = CryptFindOIDInfo(
459 CRYPT_OID_INFO_OID_KEY,
460 rdn->rgRDNAttr[j].pszObjId,
461 CRYPT_RDN_ATTR_OID_GROUP_ID);
462
463 if (oidInfo)
464 {
465 WideCharToMultiByte(CP_ACP, 0, oidInfo->pwszName, -1,
466 prefixBuf, sizeof(prefixBuf), NULL, NULL);
467 prefix = prefixBuf;
468 }
469 else
470 prefix = rdn->rgRDNAttr[j].pszObjId;
471 }
472 if (prefix)
473 {
474 /* - 1 is needed to account for the NULL terminator. */
475 chars = CRYPT_AddPrefixA(prefix,
476 psz ? psz + ret : NULL, psz ? csz - ret - 1 : 0);
477 ret += chars;
478 }
479 chars = quote_rdn_value_to_str_a(
480 rdn->rgRDNAttr[j].dwValueType,
481 &rdn->rgRDNAttr[j].Value, psz ? psz + ret : NULL,
482 psz ? csz - ret : 0);
483 if (chars)
484 ret += chars - 1;
485 if (j < rdn->cRDNAttr - 1)
486 {
487 if (psz && ret < csz - rdnSepLen - 1)
488 memcpy(psz + ret, rdnSep, rdnSepLen);
489 ret += rdnSepLen;
490 }
491 }
492 if (i < info->cRDN - 1)
493 {
494 if (psz && ret < csz - sepLen - 1)
495 memcpy(psz + ret, sep, sepLen);
496 ret += sepLen;
497 }
498 if(reverse) rdn--;
499 else rdn++;
500 }
501 LocalFree(info);
502 }
503 if (psz && csz)
504 {
505 *(psz + ret) = '\0';
506 ret++;
507 }
508 else
509 ret++;
510 TRACE("Returning %s\n", debugstr_a(psz));
511 return ret;
512 }
513
514 /* Adds the prefix prefix to the wide-character string pointed to by psz,
515 * followed by the character '='. Copies no more than csz characters. Returns
516 * the number of characters copied. If psz is NULL, returns the number of
517 * characters that would be copied.
518 * Assumes the characters in prefix are ASCII (not multibyte characters.)
519 */
520 static DWORD CRYPT_AddPrefixAToW(LPCSTR prefix, LPWSTR psz, DWORD csz)
521 {
522 DWORD chars;
523
524 TRACE("(%s, %p, %d)\n", debugstr_a(prefix), psz, csz);
525
526 if (psz)
527 {
528 DWORD i;
529
530 chars = min(strlen(prefix), csz);
531 for (i = 0; i < chars; i++)
532 *(psz + i) = prefix[i];
533 *(psz + chars) = '=';
534 chars++;
535 }
536 else
537 chars = lstrlenA(prefix) + 1;
538 return chars;
539 }
540
541 /* Adds the prefix prefix to the string pointed to by psz, followed by the
542 * character '='. Copies no more than csz characters. Returns the number of
543 * characters copied. If psz is NULL, returns the number of characters that
544 * would be copied.
545 */
546 static DWORD CRYPT_AddPrefixW(LPCWSTR prefix, LPWSTR psz, DWORD csz)
547 {
548 DWORD chars;
549
550 TRACE("(%s, %p, %d)\n", debugstr_w(prefix), psz, csz);
551
552 if (psz)
553 {
554 chars = min(strlenW(prefix), csz);
555 memcpy(psz, prefix, chars * sizeof(WCHAR));
556 *(psz + chars) = '=';
557 chars++;
558 }
559 else
560 chars = lstrlenW(prefix) + 1;
561 return chars;
562 }
563
564 static const WCHAR indent[] = { ' ',' ',' ',' ',' ',0 };
565
566 DWORD cert_name_to_str_with_indent(DWORD dwCertEncodingType, DWORD indentLevel,
567 const CERT_NAME_BLOB *pName, DWORD dwStrType, LPWSTR psz, DWORD csz)
568 {
569 static const DWORD unsupportedFlags = CERT_NAME_STR_NO_QUOTING_FLAG |
570 CERT_NAME_STR_ENABLE_T61_UNICODE_FLAG;
571 static const WCHAR commaSep[] = { ',',' ',0 };
572 static const WCHAR semiSep[] = { ';',' ',0 };
573 static const WCHAR crlfSep[] = { '\r','\n',0 };
574 static const WCHAR plusSep[] = { ' ','+',' ',0 };
575 static const WCHAR spaceSep[] = { ' ',0 };
576 DWORD ret = 0, bytes = 0;
577 BOOL bRet;
578 CERT_NAME_INFO *info;
579
580 if (dwStrType & unsupportedFlags)
581 FIXME("unsupported flags: %08x\n", dwStrType & unsupportedFlags);
582
583 bRet = CryptDecodeObjectEx(dwCertEncodingType, X509_NAME, pName->pbData,
584 pName->cbData, CRYPT_DECODE_ALLOC_FLAG, NULL, &info, &bytes);
585 if (bRet)
586 {
587 DWORD i, j, sepLen, rdnSepLen;
588 LPCWSTR sep, rdnSep;
589 BOOL reverse = dwStrType & CERT_NAME_STR_REVERSE_FLAG;
590 const CERT_RDN *rdn = info->rgRDN;
591
592 if(reverse && info->cRDN > 1) rdn += (info->cRDN - 1);
593
594 if (dwStrType & CERT_NAME_STR_SEMICOLON_FLAG)
595 sep = semiSep;
596 else if (dwStrType & CERT_NAME_STR_CRLF_FLAG)
597 sep = crlfSep;
598 else
599 sep = commaSep;
600 sepLen = lstrlenW(sep);
601 if (dwStrType & CERT_NAME_STR_NO_PLUS_FLAG)
602 rdnSep = spaceSep;
603 else
604 rdnSep = plusSep;
605 rdnSepLen = lstrlenW(rdnSep);
606 for (i = 0; (!psz || ret < csz) && i < info->cRDN; i++)
607 {
608 for (j = 0; (!psz || ret < csz) && j < rdn->cRDNAttr; j++)
609 {
610 DWORD chars;
611 LPCSTR prefixA = NULL;
612 LPCWSTR prefixW = NULL;
613
614 if ((dwStrType & 0x000000ff) == CERT_OID_NAME_STR)
615 prefixA = rdn->rgRDNAttr[j].pszObjId;
616 else if ((dwStrType & 0x000000ff) == CERT_X500_NAME_STR)
617 {
618 PCCRYPT_OID_INFO oidInfo = CryptFindOIDInfo(
619 CRYPT_OID_INFO_OID_KEY,
620 rdn->rgRDNAttr[j].pszObjId,
621 CRYPT_RDN_ATTR_OID_GROUP_ID);
622
623 if (oidInfo)
624 prefixW = oidInfo->pwszName;
625 else
626 prefixA = rdn->rgRDNAttr[j].pszObjId;
627 }
628 if (dwStrType & CERT_NAME_STR_CRLF_FLAG)
629 {
630 DWORD k;
631
632 for (k = 0; k < indentLevel; k++)
633 {
634 if (psz)
635 {
636 chars = min(strlenW(indent), csz - ret - 1);
637 memcpy(psz + ret, indent, chars * sizeof(WCHAR));
638 }
639 else
640 chars = strlenW(indent);
641 ret += chars;
642 }
643 }
644 if (prefixW)
645 {
646 /* - 1 is needed to account for the NULL terminator. */
647 chars = CRYPT_AddPrefixW(prefixW,
648 psz ? psz + ret : NULL, psz ? csz - ret - 1 : 0);
649 ret += chars;
650 }
651 else if (prefixA)
652 {
653 /* - 1 is needed to account for the NULL terminator. */
654 chars = CRYPT_AddPrefixAToW(prefixA,
655 psz ? psz + ret : NULL, psz ? csz - ret - 1 : 0);
656 ret += chars;
657 }
658 chars = quote_rdn_value_to_str_w(
659 rdn->rgRDNAttr[j].dwValueType,
660 &rdn->rgRDNAttr[j].Value, psz ? psz + ret : NULL,
661 psz ? csz - ret : 0);
662 if (chars)
663 ret += chars - 1;
664 if (j < rdn->cRDNAttr - 1)
665 {
666 if (psz && ret < csz - rdnSepLen - 1)
667 memcpy(psz + ret, rdnSep, rdnSepLen * sizeof(WCHAR));
668 ret += rdnSepLen;
669 }
670 }
671 if (i < info->cRDN - 1)
672 {
673 if (psz && ret < csz - sepLen - 1)
674 memcpy(psz + ret, sep, sepLen * sizeof(WCHAR));
675 ret += sepLen;
676 }
677 if(reverse) rdn--;
678 else rdn++;
679 }
680 LocalFree(info);
681 }
682 if (psz && csz)
683 {
684 *(psz + ret) = '\0';
685 ret++;
686 }
687 else
688 ret++;
689 return ret;
690 }
691
692 DWORD WINAPI CertNameToStrW(DWORD dwCertEncodingType, PCERT_NAME_BLOB pName,
693 DWORD dwStrType, LPWSTR psz, DWORD csz)
694 {
695 BOOL ret;
696
697 TRACE("(%d, %p, %08x, %p, %d)\n", dwCertEncodingType, pName, dwStrType,
698 psz, csz);
699
700 ret = cert_name_to_str_with_indent(dwCertEncodingType, 0, pName, dwStrType,
701 psz, csz);
702 TRACE("Returning %s\n", debugstr_w(psz));
703 return ret;
704 }
705
706 BOOL WINAPI CertStrToNameA(DWORD dwCertEncodingType, LPCSTR pszX500,
707 DWORD dwStrType, void *pvReserved, BYTE *pbEncoded, DWORD *pcbEncoded,
708 LPCSTR *ppszError)
709 {
710 BOOL ret;
711 int len;
712
713 TRACE("(%08x, %s, %08x, %p, %p, %p, %p)\n", dwCertEncodingType,
714 debugstr_a(pszX500), dwStrType, pvReserved, pbEncoded, pcbEncoded,
715 ppszError);
716
717 len = MultiByteToWideChar(CP_ACP, 0, pszX500, -1, NULL, 0);
718 if (len)
719 {
720 LPWSTR x500, errorStr;
721
722 if ((x500 = CryptMemAlloc(len * sizeof(WCHAR))))
723 {
724 MultiByteToWideChar(CP_ACP, 0, pszX500, -1, x500, len);
725 ret = CertStrToNameW(dwCertEncodingType, x500, dwStrType,
726 pvReserved, pbEncoded, pcbEncoded,
727 ppszError ? (LPCWSTR *)&errorStr : NULL);
728 if (ppszError)
729 {
730 if (!ret)
731 {
732 LONG i;
733
734 *ppszError = pszX500;
735 for (i = 0; i < errorStr - x500; i++)
736 *ppszError = CharNextA(*ppszError);
737 }
738 else
739 *ppszError = NULL;
740 }
741 CryptMemFree(x500);
742 }
743 else
744 {
745 SetLastError(ERROR_OUTOFMEMORY);
746 ret = FALSE;
747 }
748 }
749 else
750 {
751 SetLastError(CRYPT_E_INVALID_X500_STRING);
752 if (ppszError)
753 *ppszError = pszX500;
754 ret = FALSE;
755 }
756 return ret;
757 }
758
759 struct KeynameKeeper
760 {
761 WCHAR buf[10]; /* big enough for L"GivenName" */
762 LPWSTR keyName; /* usually = buf, but may be allocated */
763 DWORD keyLen; /* full available buffer size in WCHARs */
764 };
765
766 static void CRYPT_InitializeKeynameKeeper(struct KeynameKeeper *keeper)
767 {
768 keeper->keyName = keeper->buf;
769 keeper->keyLen = sizeof(keeper->buf) / sizeof(keeper->buf[0]);
770 }
771
772 static void CRYPT_FreeKeynameKeeper(struct KeynameKeeper *keeper)
773 {
774 if (keeper->keyName != keeper->buf)
775 CryptMemFree(keeper->keyName);
776 }
777
778 struct X500TokenW
779 {
780 LPCWSTR start;
781 LPCWSTR end;
782 };
783
784 static void CRYPT_KeynameKeeperFromTokenW(struct KeynameKeeper *keeper,
785 const struct X500TokenW *key)
786 {
787 DWORD len = key->end - key->start;
788
789 if (len >= keeper->keyLen)
790 {
791 CRYPT_FreeKeynameKeeper( keeper );
792 keeper->keyLen = len + 1;
793 keeper->keyName = CryptMemAlloc(keeper->keyLen * sizeof(WCHAR));
794 }
795 memcpy(keeper->keyName, key->start, len * sizeof(WCHAR));
796 keeper->keyName[len] = '\0';
797 TRACE("Keyname is %s\n", debugstr_w(keeper->keyName));
798 }
799
800 static BOOL CRYPT_GetNextKeyW(LPCWSTR str, struct X500TokenW *token,
801 LPCWSTR *ppszError)
802 {
803 BOOL ret = TRUE;
804
805 while (*str && isspaceW(*str))
806 str++;
807 if (*str)
808 {
809 token->start = str;
810 while (*str && *str != '=' && !isspaceW(*str))
811 str++;
812 if (*str && (*str == '=' || isspaceW(*str)))
813 token->end = str;
814 else
815 {
816 TRACE("missing equals char at %s\n", debugstr_w(token->start));
817 if (ppszError)
818 *ppszError = token->start;
819 SetLastError(CRYPT_E_INVALID_X500_STRING);
820 ret = FALSE;
821 }
822 }
823 else
824 token->start = NULL;
825 return ret;
826 }
827
828 /* Assumes separators are characters in the 0-255 range */
829 static BOOL CRYPT_GetNextValueW(LPCWSTR str, DWORD dwFlags, LPCWSTR separators,
830 WCHAR *separator_used, struct X500TokenW *token, LPCWSTR *ppszError)
831 {
832 BOOL ret = TRUE;
833
834 TRACE("(%s, %s, %p, %p)\n", debugstr_w(str), debugstr_w(separators), token,
835 ppszError);
836
837 *separator_used = 0;
838 while (*str && isspaceW(*str))
839 str++;
840 if (*str)
841 {
842 token->start = str;
843 if (!(dwFlags & CERT_NAME_STR_NO_QUOTING_FLAG) && *str == '"')
844 {
845 token->end = NULL;
846 str++;
847 while (!token->end && ret)
848 {
849 while (*str && *str != '"')
850 str++;
851 if (*str == '"')
852 {
853 if (*(str + 1) != '"')
854 token->end = str + 1;
855 else
856 str += 2;
857 }
858 else
859 {
860 TRACE("unterminated quote at %s\n", debugstr_w(str));
861 if (ppszError)
862 *ppszError = str;
863 SetLastError(CRYPT_E_INVALID_X500_STRING);
864 ret = FALSE;
865 }
866 }
867 }
868 else
869 {
870 WCHAR map[256] = { 0 };
871
872 while (*separators)
873 map[*separators++] = 1;
874 while (*str && (*str >= 0xff || !map[*str]))
875 str++;
876 token->end = str;
877 if (map[*str]) *separator_used = *str;
878 }
879 }
880 else
881 {
882 TRACE("missing value at %s\n", debugstr_w(str));
883 if (ppszError)
884 *ppszError = str;
885 SetLastError(CRYPT_E_INVALID_X500_STRING);
886 ret = FALSE;
887 }
888 return ret;
889 }
890
891 /* Encodes the string represented by value as the string type type into the
892 * CERT_NAME_BLOB output. If there is an error and ppszError is not NULL,
893 * *ppszError is set to the first failing character. If there is no error,
894 * output's pbData must be freed with LocalFree.
895 */
896 static BOOL CRYPT_EncodeValueWithType(DWORD dwCertEncodingType,
897 const struct X500TokenW *value, PCERT_NAME_BLOB output, DWORD type,
898 LPCWSTR *ppszError)
899 {
900 CERT_NAME_VALUE nameValue = { type, { 0, NULL } };
901 BOOL ret = TRUE;
902
903 if (value->end > value->start)
904 {
905 LONG i;
906 LPWSTR ptr;
907
908 nameValue.Value.pbData = CryptMemAlloc((value->end - value->start + 1) *
909 sizeof(WCHAR));
910 if (!nameValue.Value.pbData)
911 {
912 SetLastError(ERROR_OUTOFMEMORY);
913 return FALSE;
914 }
915 ptr = (LPWSTR)nameValue.Value.pbData;
916 for (i = 0; i < value->end - value->start; i++)
917 {
918 *ptr++ = value->start[i];
919 if (value->start[i] == '"')
920 i++;
921 }
922 /* The string is NULL terminated because of a quirk in encoding
923 * unicode names values: if the length is given as 0, the value is
924 * assumed to be a NULL-terminated string.
925 */
926 *ptr = 0;
927 nameValue.Value.cbData = (LPBYTE)ptr - nameValue.Value.pbData;
928 }
929 ret = CryptEncodeObjectEx(dwCertEncodingType, X509_UNICODE_NAME_VALUE,
930 &nameValue, CRYPT_ENCODE_ALLOC_FLAG, NULL, &output->pbData,
931 &output->cbData);
932 if (!ret && ppszError)
933 {
934 if (type == CERT_RDN_NUMERIC_STRING &&
935 GetLastError() == CRYPT_E_INVALID_NUMERIC_STRING)
936 *ppszError = value->start + output->cbData;
937 else if (type == CERT_RDN_PRINTABLE_STRING &&
938 GetLastError() == CRYPT_E_INVALID_PRINTABLE_STRING)
939 *ppszError = value->start + output->cbData;
940 else if (type == CERT_RDN_IA5_STRING &&
941 GetLastError() == CRYPT_E_INVALID_IA5_STRING)
942 *ppszError = value->start + output->cbData;
943 }
944 CryptMemFree(nameValue.Value.pbData);
945 return ret;
946 }
947
948 static BOOL CRYPT_EncodeValue(DWORD dwCertEncodingType,
949 const struct X500TokenW *value, PCERT_NAME_BLOB output, const DWORD *types,
950 LPCWSTR *ppszError)
951 {
952 DWORD i;
953 BOOL ret;
954
955 ret = FALSE;
956 for (i = 0; !ret && types[i]; i++)
957 ret = CRYPT_EncodeValueWithType(dwCertEncodingType, value, output,
958 types[i], ppszError);
959 return ret;
960 }
961
962 static BOOL CRYPT_ValueToRDN(DWORD dwCertEncodingType, PCERT_NAME_INFO info,
963 PCCRYPT_OID_INFO keyOID, struct X500TokenW *value, DWORD dwStrType, LPCWSTR *ppszError)
964 {
965 BOOL ret = FALSE;
966
967 TRACE("OID %s, value %s\n", debugstr_a(keyOID->pszOID),
968 debugstr_wn(value->start, value->end - value->start));
969
970 if (!info->rgRDN)
971 info->rgRDN = CryptMemAlloc(sizeof(CERT_RDN));
972 else
973 info->rgRDN = CryptMemRealloc(info->rgRDN,
974 (info->cRDN + 1) * sizeof(CERT_RDN));
975 if (info->rgRDN)
976 {
977 /* FIXME: support multiple RDN attrs */
978 info->rgRDN[info->cRDN].rgRDNAttr =
979 CryptMemAlloc(sizeof(CERT_RDN_ATTR));
980 if (info->rgRDN[info->cRDN].rgRDNAttr)
981 {
982 static const DWORD defaultTypes[] = { CERT_RDN_PRINTABLE_STRING,
983 CERT_RDN_BMP_STRING, 0 };
984 const DWORD *types;
985
986 info->rgRDN[info->cRDN].cRDNAttr = 1;
987 info->rgRDN[info->cRDN].rgRDNAttr[0].pszObjId =
988 (LPSTR)keyOID->pszOID;
989 info->rgRDN[info->cRDN].rgRDNAttr[0].dwValueType =
990 CERT_RDN_ENCODED_BLOB;
991 if (keyOID->ExtraInfo.cbData)
992 types = (const DWORD *)keyOID->ExtraInfo.pbData;
993 else
994 types = defaultTypes;
995
996 /* Remove surrounding quotes */
997 if (value->start[0] == '"' && !(dwStrType & CERT_NAME_STR_NO_QUOTING_FLAG))
998 {
999 value->start++;
1000 value->end--;
1001 }
1002 ret = CRYPT_EncodeValue(dwCertEncodingType, value,
1003 &info->rgRDN[info->cRDN].rgRDNAttr[0].Value, types, ppszError);
1004 }
1005 else
1006 SetLastError(ERROR_OUTOFMEMORY);
1007 info->cRDN++;
1008 }
1009 else
1010 SetLastError(ERROR_OUTOFMEMORY);
1011 return ret;
1012 }
1013
1014 BOOL WINAPI CertStrToNameW(DWORD dwCertEncodingType, LPCWSTR pszX500,
1015 DWORD dwStrType, void *pvReserved, BYTE *pbEncoded, DWORD *pcbEncoded,
1016 LPCWSTR *ppszError)
1017 {
1018 CERT_NAME_INFO info = { 0, NULL };
1019 LPCWSTR str;
1020 struct KeynameKeeper keeper;
1021 DWORD i;
1022 BOOL ret = TRUE;
1023
1024 TRACE("(%08x, %s, %08x, %p, %p, %p, %p)\n", dwCertEncodingType,
1025 debugstr_w(pszX500), dwStrType, pvReserved, pbEncoded, pcbEncoded,
1026 ppszError);
1027
1028 CRYPT_InitializeKeynameKeeper(&keeper);
1029 str = pszX500;
1030 while (str && *str && ret)
1031 {
1032 struct X500TokenW token;
1033
1034 ret = CRYPT_GetNextKeyW(str, &token, ppszError);
1035 if (ret && token.start)
1036 {
1037 PCCRYPT_OID_INFO keyOID;
1038
1039 CRYPT_KeynameKeeperFromTokenW(&keeper, &token);
1040 keyOID = CryptFindOIDInfo(CRYPT_OID_INFO_NAME_KEY, keeper.keyName,
1041 CRYPT_RDN_ATTR_OID_GROUP_ID);
1042 if (!keyOID)
1043 {
1044 if (ppszError)
1045 *ppszError = token.start;
1046 SetLastError(CRYPT_E_INVALID_X500_STRING);
1047 ret = FALSE;
1048 }
1049 else
1050 {
1051 str = token.end;
1052 while (isspace(*str))
1053 str++;
1054 if (*str != '=')
1055 {
1056 if (ppszError)
1057 *ppszError = str;
1058 SetLastError(CRYPT_E_INVALID_X500_STRING);
1059 ret = FALSE;
1060 }
1061 else
1062 {
1063 static const WCHAR commaSep[] = { ',',0 };
1064 static const WCHAR semiSep[] = { ';',0 };
1065 static const WCHAR crlfSep[] = { '\r','\n',0 };
1066 static const WCHAR allSepsWithoutPlus[] = { ',',';','\r','\n',0 };
1067 static const WCHAR allSeps[] = { '+',',',';','\r','\n',0 };
1068 LPCWSTR sep;
1069 WCHAR sep_used;
1070
1071 str++;
1072 if (dwStrType & CERT_NAME_STR_COMMA_FLAG)
1073 sep = commaSep;
1074 else if (dwStrType & CERT_NAME_STR_SEMICOLON_FLAG)
1075 sep = semiSep;
1076 else if (dwStrType & CERT_NAME_STR_CRLF_FLAG)
1077 sep = crlfSep;
1078 else if (dwStrType & CERT_NAME_STR_NO_PLUS_FLAG)
1079 sep = allSepsWithoutPlus;
1080 else
1081 sep = allSeps;
1082 ret = CRYPT_GetNextValueW(str, dwStrType, sep, &sep_used, &token,
1083 ppszError);
1084 if (ret)
1085 {
1086 str = token.end;
1087 /* if token.end points to the separator, skip it */
1088 if (str && sep_used && *str == sep_used) str++;
1089
1090 ret = CRYPT_ValueToRDN(dwCertEncodingType, &info,
1091 keyOID, &token, dwStrType, ppszError);
1092 }
1093 }
1094 }
1095 }
1096 }
1097 CRYPT_FreeKeynameKeeper(&keeper);
1098 if (ret)
1099 {
1100 if (ppszError)
1101 *ppszError = NULL;
1102 ret = CryptEncodeObjectEx(dwCertEncodingType, X509_NAME, &info,
1103 0, NULL, pbEncoded, pcbEncoded);
1104 }
1105 for (i = 0; i < info.cRDN; i++)
1106 {
1107 DWORD j;
1108
1109 for (j = 0; j < info.rgRDN[i].cRDNAttr; j++)
1110 LocalFree(info.rgRDN[i].rgRDNAttr[j].Value.pbData);
1111 CryptMemFree(info.rgRDN[i].rgRDNAttr);
1112 }
1113 CryptMemFree(info.rgRDN);
1114 return ret;
1115 }
1116
1117 DWORD WINAPI CertGetNameStringA(PCCERT_CONTEXT pCertContext, DWORD dwType,
1118 DWORD dwFlags, void *pvTypePara, LPSTR pszNameString, DWORD cchNameString)
1119 {
1120 DWORD ret;
1121
1122 TRACE("(%p, %d, %08x, %p, %p, %d)\n", pCertContext, dwType, dwFlags,
1123 pvTypePara, pszNameString, cchNameString);
1124
1125 if (pszNameString)
1126 {
1127 LPWSTR wideName;
1128 DWORD nameLen;
1129
1130 nameLen = CertGetNameStringW(pCertContext, dwType, dwFlags, pvTypePara,
1131 NULL, 0);
1132 wideName = CryptMemAlloc(nameLen * sizeof(WCHAR));
1133 if (wideName)
1134 {
1135 CertGetNameStringW(pCertContext, dwType, dwFlags, pvTypePara,
1136 wideName, nameLen);
1137 nameLen = WideCharToMultiByte(CP_ACP, 0, wideName, nameLen,
1138 pszNameString, cchNameString, NULL, NULL);
1139 if (nameLen <= cchNameString)
1140 ret = nameLen;
1141 else
1142 {
1143 pszNameString[cchNameString - 1] = '\0';
1144 ret = cchNameString;
1145 }
1146 CryptMemFree(wideName);
1147 }
1148 else
1149 {
1150 *pszNameString = '\0';
1151 ret = 1;
1152 }
1153 }
1154 else
1155 ret = CertGetNameStringW(pCertContext, dwType, dwFlags, pvTypePara,
1156 NULL, 0);
1157 return ret;
1158 }
1159
1160 /* Searches cert's extensions for the alternate name extension with OID
1161 * altNameOID, and if found, searches it for the alternate name type entryType.
1162 * If found, returns a pointer to the entry, otherwise returns NULL.
1163 * Regardless of whether an entry of the desired type is found, if the
1164 * alternate name extension is present, sets *info to the decoded alternate
1165 * name extension, which you must free using LocalFree.
1166 * The return value is a pointer within *info, so don't free *info before
1167 * you're done with the return value.
1168 */
1169 static PCERT_ALT_NAME_ENTRY cert_find_alt_name_entry(PCCERT_CONTEXT cert,
1170 LPCSTR altNameOID, DWORD entryType, PCERT_ALT_NAME_INFO *info)
1171 {
1172 PCERT_ALT_NAME_ENTRY entry = NULL;
1173 PCERT_EXTENSION ext = CertFindExtension(altNameOID,
1174 cert->pCertInfo->cExtension, cert->pCertInfo->rgExtension);
1175
1176 if (ext)
1177 {
1178 DWORD bytes = 0;
1179
1180 if (CryptDecodeObjectEx(cert->dwCertEncodingType, X509_ALTERNATE_NAME,
1181 ext->Value.pbData, ext->Value.cbData, CRYPT_DECODE_ALLOC_FLAG, NULL,
1182 info, &bytes))
1183 {
1184 DWORD i;
1185
1186 for (i = 0; !entry && i < (*info)->cAltEntry; i++)
1187 if ((*info)->rgAltEntry[i].dwAltNameChoice == entryType)
1188 entry = &(*info)->rgAltEntry[i];
1189 }
1190 }
1191 else
1192 *info = NULL;
1193 return entry;
1194 }
1195
1196 static DWORD cert_get_name_from_rdn_attr(DWORD encodingType,
1197 const CERT_NAME_BLOB *name, LPCSTR oid, LPWSTR pszNameString, DWORD cchNameString)
1198 {
1199 CERT_NAME_INFO *nameInfo;
1200 DWORD bytes = 0, ret = 0;
1201
1202 if (CryptDecodeObjectEx(encodingType, X509_NAME, name->pbData,
1203 name->cbData, CRYPT_DECODE_ALLOC_FLAG, NULL, &nameInfo, &bytes))
1204 {
1205 PCERT_RDN_ATTR nameAttr;
1206
1207 if (!oid)
1208 oid = szOID_RSA_emailAddr;
1209 nameAttr = CertFindRDNAttr(oid, nameInfo);
1210 if (nameAttr)
1211 ret = CertRDNValueToStrW(nameAttr->dwValueType, &nameAttr->Value,
1212 pszNameString, cchNameString);
1213 LocalFree(nameInfo);
1214 }
1215 return ret;
1216 }
1217
1218 DWORD WINAPI CertGetNameStringW(PCCERT_CONTEXT pCertContext, DWORD dwType,
1219 DWORD dwFlags, void *pvTypePara, LPWSTR pszNameString, DWORD cchNameString)
1220 {
1221 DWORD ret = 0;
1222 PCERT_NAME_BLOB name;
1223 LPCSTR altNameOID;
1224
1225 TRACE("(%p, %d, %08x, %p, %p, %d)\n", pCertContext, dwType,
1226 dwFlags, pvTypePara, pszNameString, cchNameString);
1227
1228 if (!pCertContext)
1229 goto done;
1230
1231 if (dwFlags & CERT_NAME_ISSUER_FLAG)
1232 {
1233 name = &pCertContext->pCertInfo->Issuer;
1234 altNameOID = szOID_ISSUER_ALT_NAME;
1235 }
1236 else
1237 {
1238 name = &pCertContext->pCertInfo->Subject;
1239 altNameOID = szOID_SUBJECT_ALT_NAME;
1240 }
1241
1242 switch (dwType)
1243 {
1244 case CERT_NAME_EMAIL_TYPE:
1245 {
1246 CERT_ALT_NAME_INFO *info;
1247 PCERT_ALT_NAME_ENTRY entry = cert_find_alt_name_entry(pCertContext,
1248 altNameOID, CERT_ALT_NAME_RFC822_NAME, &info);
1249
1250 if (entry)
1251 {
1252 if (!pszNameString)
1253 ret = strlenW(entry->u.pwszRfc822Name) + 1;
1254 else if (cchNameString)
1255 {
1256 ret = min(strlenW(entry->u.pwszRfc822Name), cchNameString - 1);
1257 memcpy(pszNameString, entry->u.pwszRfc822Name,
1258 ret * sizeof(WCHAR));
1259 pszNameString[ret++] = 0;
1260 }
1261 }
1262 if (info)
1263 LocalFree(info);
1264 if (!ret)
1265 ret = cert_get_name_from_rdn_attr(pCertContext->dwCertEncodingType,
1266 name, szOID_RSA_emailAddr, pszNameString, cchNameString);
1267 break;
1268 }
1269 case CERT_NAME_RDN_TYPE:
1270 {
1271 DWORD type = pvTypePara ? *(DWORD *)pvTypePara : 0;
1272
1273 if (name->cbData)
1274 ret = CertNameToStrW(pCertContext->dwCertEncodingType, name,
1275 type, pszNameString, cchNameString);
1276 else
1277 {
1278 CERT_ALT_NAME_INFO *info;
1279 PCERT_ALT_NAME_ENTRY entry = cert_find_alt_name_entry(pCertContext,
1280 altNameOID, CERT_ALT_NAME_DIRECTORY_NAME, &info);
1281
1282 if (entry)
1283 ret = CertNameToStrW(pCertContext->dwCertEncodingType,
1284 &entry->u.DirectoryName, type, pszNameString, cchNameString);
1285 if (info)
1286 LocalFree(info);
1287 }
1288 break;
1289 }
1290 case CERT_NAME_ATTR_TYPE:
1291 ret = cert_get_name_from_rdn_attr(pCertContext->dwCertEncodingType,
1292 name, pvTypePara, pszNameString, cchNameString);
1293 if (!ret)
1294 {
1295 CERT_ALT_NAME_INFO *altInfo;
1296 PCERT_ALT_NAME_ENTRY entry = cert_find_alt_name_entry(pCertContext,
1297 altNameOID, CERT_ALT_NAME_DIRECTORY_NAME, &altInfo);
1298
1299 if (entry)
1300 ret = cert_name_to_str_with_indent(X509_ASN_ENCODING, 0,
1301 &entry->u.DirectoryName, 0, pszNameString, cchNameString);
1302 if (altInfo)
1303 LocalFree(altInfo);
1304 }
1305 break;
1306 case CERT_NAME_SIMPLE_DISPLAY_TYPE:
1307 {
1308 static const LPCSTR simpleAttributeOIDs[] = { szOID_COMMON_NAME,
1309 szOID_ORGANIZATIONAL_UNIT_NAME, szOID_ORGANIZATION_NAME,
1310 szOID_RSA_emailAddr };
1311 CERT_NAME_INFO *nameInfo = NULL;
1312 DWORD bytes = 0, i;
1313
1314 if (CryptDecodeObjectEx(pCertContext->dwCertEncodingType, X509_NAME,
1315 name->pbData, name->cbData, CRYPT_DECODE_ALLOC_FLAG, NULL, &nameInfo,
1316 &bytes))
1317 {
1318 PCERT_RDN_ATTR nameAttr = NULL;
1319
1320 for (i = 0; !nameAttr && i < sizeof(simpleAttributeOIDs) /
1321 sizeof(simpleAttributeOIDs[0]); i++)
1322 nameAttr = CertFindRDNAttr(simpleAttributeOIDs[i], nameInfo);
1323 if (nameAttr)
1324 ret = CertRDNValueToStrW(nameAttr->dwValueType,
1325 &nameAttr->Value, pszNameString, cchNameString);
1326 LocalFree(nameInfo);
1327 }
1328 if (!ret)
1329 {
1330 CERT_ALT_NAME_INFO *altInfo;
1331 PCERT_ALT_NAME_ENTRY entry = cert_find_alt_name_entry(pCertContext,
1332 altNameOID, CERT_ALT_NAME_RFC822_NAME, &altInfo);
1333
1334 if (altInfo)
1335 {
1336 if (!entry && altInfo->cAltEntry)
1337 entry = &altInfo->rgAltEntry[0];
1338 if (entry)
1339 {
1340 if (!pszNameString)
1341 ret = strlenW(entry->u.pwszRfc822Name) + 1;
1342 else if (cchNameString)
1343 {
1344 ret = min(strlenW(entry->u.pwszRfc822Name),
1345 cchNameString - 1);
1346 memcpy(pszNameString, entry->u.pwszRfc822Name,
1347 ret * sizeof(WCHAR));
1348 pszNameString[ret++] = 0;
1349 }
1350 }
1351 LocalFree(altInfo);
1352 }
1353 }
1354 break;
1355 }
1356 case CERT_NAME_FRIENDLY_DISPLAY_TYPE:
1357 {
1358 DWORD cch = cchNameString;
1359
1360 if (CertGetCertificateContextProperty(pCertContext,
1361 CERT_FRIENDLY_NAME_PROP_ID, pszNameString, &cch))
1362 ret = cch;
1363 else
1364 ret = CertGetNameStringW(pCertContext,
1365 CERT_NAME_SIMPLE_DISPLAY_TYPE, dwFlags, pvTypePara, pszNameString,
1366 cchNameString);
1367 break;
1368 }
1369 case CERT_NAME_DNS_TYPE:
1370 {
1371 CERT_ALT_NAME_INFO *info;
1372 PCERT_ALT_NAME_ENTRY entry = cert_find_alt_name_entry(pCertContext,
1373 altNameOID, CERT_ALT_NAME_DNS_NAME, &info);
1374
1375 if (entry)
1376 {
1377 if (!pszNameString)
1378 ret = strlenW(entry->u.pwszDNSName) + 1;
1379 else if (cchNameString)
1380 {
1381 ret = min(strlenW(entry->u.pwszDNSName), cchNameString - 1);
1382 memcpy(pszNameString, entry->u.pwszDNSName, ret * sizeof(WCHAR));
1383 pszNameString[ret++] = 0;
1384 }
1385 }
1386 if (info)
1387 LocalFree(info);
1388 if (!ret)
1389 ret = cert_get_name_from_rdn_attr(pCertContext->dwCertEncodingType,
1390 name, szOID_COMMON_NAME, pszNameString, cchNameString);
1391 break;
1392 }
1393 case CERT_NAME_URL_TYPE:
1394 {
1395 CERT_ALT_NAME_INFO *info;
1396 PCERT_ALT_NAME_ENTRY entry = cert_find_alt_name_entry(pCertContext,
1397 altNameOID, CERT_ALT_NAME_URL, &info);
1398
1399 if (entry)
1400 {
1401 if (!pszNameString)
1402 ret = strlenW(entry->u.pwszURL) + 1;
1403 else if (cchNameString)
1404 {
1405 ret = min(strlenW(entry->u.pwszURL), cchNameString - 1);
1406 memcpy(pszNameString, entry->u.pwszURL, ret * sizeof(WCHAR));
1407 pszNameString[ret++] = 0;
1408 }
1409 }
1410 if (info)
1411 LocalFree(info);
1412 break;
1413 }
1414 default:
1415 FIXME("unimplemented for type %d\n", dwType);
1416 ret = 0;
1417 }
1418 done:
1419 if (!ret)
1420 {
1421 if (!pszNameString)
1422 ret = 1;
1423 else if (cchNameString)
1424 {
1425 pszNameString[0] = 0;
1426 ret = 1;
1427 }
1428 }
1429 return ret;
1430 }