[CRYPT32]
[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;
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 if (keeper->keyName == keeper->buf)
792 keeper->keyName = CryptMemAlloc(len * sizeof(WCHAR));
793 else
794 keeper->keyName = CryptMemRealloc(keeper->keyName,
795 len * sizeof(WCHAR));
796 keeper->keyLen = len;
797 }
798 memcpy(keeper->keyName, key->start, (key->end - key->start) *
799 sizeof(WCHAR));
800 keeper->keyName[len] = '\0';
801 TRACE("Keyname is %s\n", debugstr_w(keeper->keyName));
802 }
803
804 static BOOL CRYPT_GetNextKeyW(LPCWSTR str, struct X500TokenW *token,
805 LPCWSTR *ppszError)
806 {
807 BOOL ret = TRUE;
808
809 while (*str && isspaceW(*str))
810 str++;
811 if (*str)
812 {
813 token->start = str;
814 while (*str && *str != '=' && !isspaceW(*str))
815 str++;
816 if (*str && (*str == '=' || isspaceW(*str)))
817 token->end = str;
818 else
819 {
820 TRACE("missing equals char at %s\n", debugstr_w(token->start));
821 if (ppszError)
822 *ppszError = token->start;
823 SetLastError(CRYPT_E_INVALID_X500_STRING);
824 ret = FALSE;
825 }
826 }
827 else
828 token->start = NULL;
829 return ret;
830 }
831
832 /* Assumes separators are characters in the 0-255 range */
833 static BOOL CRYPT_GetNextValueW(LPCWSTR str, DWORD dwFlags, LPCWSTR separators,
834 WCHAR *separator_used, struct X500TokenW *token, LPCWSTR *ppszError)
835 {
836 BOOL ret = TRUE;
837
838 TRACE("(%s, %s, %p, %p)\n", debugstr_w(str), debugstr_w(separators), token,
839 ppszError);
840
841 *separator_used = 0;
842 while (*str && isspaceW(*str))
843 str++;
844 if (*str)
845 {
846 token->start = str;
847 if (!(dwFlags & CERT_NAME_STR_NO_QUOTING_FLAG) && *str == '"')
848 {
849 token->end = NULL;
850 str++;
851 while (!token->end && ret)
852 {
853 while (*str && *str != '"')
854 str++;
855 if (*str == '"')
856 {
857 if (*(str + 1) != '"')
858 token->end = str + 1;
859 else
860 str += 2;
861 }
862 else
863 {
864 TRACE("unterminated quote at %s\n", debugstr_w(str));
865 if (ppszError)
866 *ppszError = str;
867 SetLastError(CRYPT_E_INVALID_X500_STRING);
868 ret = FALSE;
869 }
870 }
871 }
872 else
873 {
874 WCHAR map[256] = { 0 };
875
876 while (*separators)
877 map[*separators++] = 1;
878 while (*str && (*str >= 0xff || !map[*str]))
879 str++;
880 token->end = str;
881 if (map[*str]) *separator_used = *str;
882 }
883 }
884 else
885 {
886 TRACE("missing value at %s\n", debugstr_w(str));
887 if (ppszError)
888 *ppszError = str;
889 SetLastError(CRYPT_E_INVALID_X500_STRING);
890 ret = FALSE;
891 }
892 return ret;
893 }
894
895 /* Encodes the string represented by value as the string type type into the
896 * CERT_NAME_BLOB output. If there is an error and ppszError is not NULL,
897 * *ppszError is set to the first failing character. If there is no error,
898 * output's pbData must be freed with LocalFree.
899 */
900 static BOOL CRYPT_EncodeValueWithType(DWORD dwCertEncodingType,
901 const struct X500TokenW *value, PCERT_NAME_BLOB output, DWORD type,
902 LPCWSTR *ppszError)
903 {
904 CERT_NAME_VALUE nameValue = { type, { 0, NULL } };
905 BOOL ret = TRUE;
906
907 if (value->end > value->start)
908 {
909 LONG i;
910 LPWSTR ptr;
911
912 nameValue.Value.pbData = CryptMemAlloc((value->end - value->start + 1) *
913 sizeof(WCHAR));
914 if (!nameValue.Value.pbData)
915 {
916 SetLastError(ERROR_OUTOFMEMORY);
917 return FALSE;
918 }
919 ptr = (LPWSTR)nameValue.Value.pbData;
920 for (i = 0; i < value->end - value->start; i++)
921 {
922 *ptr++ = value->start[i];
923 if (value->start[i] == '"')
924 i++;
925 }
926 /* The string is NULL terminated because of a quirk in encoding
927 * unicode names values: if the length is given as 0, the value is
928 * assumed to be a NULL-terminated string.
929 */
930 *ptr = 0;
931 nameValue.Value.cbData = (LPBYTE)ptr - nameValue.Value.pbData;
932 }
933 ret = CryptEncodeObjectEx(dwCertEncodingType, X509_UNICODE_NAME_VALUE,
934 &nameValue, CRYPT_ENCODE_ALLOC_FLAG, NULL, &output->pbData,
935 &output->cbData);
936 if (!ret && ppszError)
937 {
938 if (type == CERT_RDN_NUMERIC_STRING &&
939 GetLastError() == CRYPT_E_INVALID_NUMERIC_STRING)
940 *ppszError = value->start + output->cbData;
941 else if (type == CERT_RDN_PRINTABLE_STRING &&
942 GetLastError() == CRYPT_E_INVALID_PRINTABLE_STRING)
943 *ppszError = value->start + output->cbData;
944 else if (type == CERT_RDN_IA5_STRING &&
945 GetLastError() == CRYPT_E_INVALID_IA5_STRING)
946 *ppszError = value->start + output->cbData;
947 }
948 CryptMemFree(nameValue.Value.pbData);
949 return ret;
950 }
951
952 static BOOL CRYPT_EncodeValue(DWORD dwCertEncodingType,
953 const struct X500TokenW *value, PCERT_NAME_BLOB output, const DWORD *types,
954 LPCWSTR *ppszError)
955 {
956 DWORD i;
957 BOOL ret;
958
959 ret = FALSE;
960 for (i = 0; !ret && types[i]; i++)
961 ret = CRYPT_EncodeValueWithType(dwCertEncodingType, value, output,
962 types[i], ppszError);
963 return ret;
964 }
965
966 static BOOL CRYPT_ValueToRDN(DWORD dwCertEncodingType, PCERT_NAME_INFO info,
967 PCCRYPT_OID_INFO keyOID, struct X500TokenW *value, DWORD dwStrType, LPCWSTR *ppszError)
968 {
969 BOOL ret = FALSE;
970
971 TRACE("OID %s, value %s\n", debugstr_a(keyOID->pszOID),
972 debugstr_wn(value->start, value->end - value->start));
973
974 if (!info->rgRDN)
975 info->rgRDN = CryptMemAlloc(sizeof(CERT_RDN));
976 else
977 info->rgRDN = CryptMemRealloc(info->rgRDN,
978 (info->cRDN + 1) * sizeof(CERT_RDN));
979 if (info->rgRDN)
980 {
981 /* FIXME: support multiple RDN attrs */
982 info->rgRDN[info->cRDN].rgRDNAttr =
983 CryptMemAlloc(sizeof(CERT_RDN_ATTR));
984 if (info->rgRDN[info->cRDN].rgRDNAttr)
985 {
986 static const DWORD defaultTypes[] = { CERT_RDN_PRINTABLE_STRING,
987 CERT_RDN_BMP_STRING, 0 };
988 const DWORD *types;
989
990 info->rgRDN[info->cRDN].cRDNAttr = 1;
991 info->rgRDN[info->cRDN].rgRDNAttr[0].pszObjId =
992 (LPSTR)keyOID->pszOID;
993 info->rgRDN[info->cRDN].rgRDNAttr[0].dwValueType =
994 CERT_RDN_ENCODED_BLOB;
995 if (keyOID->ExtraInfo.cbData)
996 types = (const DWORD *)keyOID->ExtraInfo.pbData;
997 else
998 types = defaultTypes;
999
1000 /* Remove surrounding quotes */
1001 if (value->start[0] == '"' && !(dwStrType & CERT_NAME_STR_NO_QUOTING_FLAG))
1002 {
1003 value->start++;
1004 value->end--;
1005 }
1006 ret = CRYPT_EncodeValue(dwCertEncodingType, value,
1007 &info->rgRDN[info->cRDN].rgRDNAttr[0].Value, types, ppszError);
1008 }
1009 else
1010 SetLastError(ERROR_OUTOFMEMORY);
1011 info->cRDN++;
1012 }
1013 else
1014 SetLastError(ERROR_OUTOFMEMORY);
1015 return ret;
1016 }
1017
1018 BOOL WINAPI CertStrToNameW(DWORD dwCertEncodingType, LPCWSTR pszX500,
1019 DWORD dwStrType, void *pvReserved, BYTE *pbEncoded, DWORD *pcbEncoded,
1020 LPCWSTR *ppszError)
1021 {
1022 CERT_NAME_INFO info = { 0, NULL };
1023 LPCWSTR str;
1024 struct KeynameKeeper keeper;
1025 DWORD i;
1026 BOOL ret = TRUE;
1027
1028 TRACE("(%08x, %s, %08x, %p, %p, %p, %p)\n", dwCertEncodingType,
1029 debugstr_w(pszX500), dwStrType, pvReserved, pbEncoded, pcbEncoded,
1030 ppszError);
1031
1032 CRYPT_InitializeKeynameKeeper(&keeper);
1033 str = pszX500;
1034 while (str && *str && ret)
1035 {
1036 struct X500TokenW token;
1037
1038 ret = CRYPT_GetNextKeyW(str, &token, ppszError);
1039 if (ret && token.start)
1040 {
1041 PCCRYPT_OID_INFO keyOID;
1042
1043 CRYPT_KeynameKeeperFromTokenW(&keeper, &token);
1044 keyOID = CryptFindOIDInfo(CRYPT_OID_INFO_NAME_KEY, keeper.keyName,
1045 CRYPT_RDN_ATTR_OID_GROUP_ID);
1046 if (!keyOID)
1047 {
1048 if (ppszError)
1049 *ppszError = token.start;
1050 SetLastError(CRYPT_E_INVALID_X500_STRING);
1051 ret = FALSE;
1052 }
1053 else
1054 {
1055 str = token.end;
1056 while (isspace(*str))
1057 str++;
1058 if (*str != '=')
1059 {
1060 if (ppszError)
1061 *ppszError = str;
1062 SetLastError(CRYPT_E_INVALID_X500_STRING);
1063 ret = FALSE;
1064 }
1065 else
1066 {
1067 static const WCHAR commaSep[] = { ',',0 };
1068 static const WCHAR semiSep[] = { ';',0 };
1069 static const WCHAR crlfSep[] = { '\r','\n',0 };
1070 static const WCHAR allSepsWithoutPlus[] = { ',',';','\r','\n',0 };
1071 static const WCHAR allSeps[] = { '+',',',';','\r','\n',0 };
1072 LPCWSTR sep;
1073 WCHAR sep_used;
1074
1075 str++;
1076 if (dwStrType & CERT_NAME_STR_COMMA_FLAG)
1077 sep = commaSep;
1078 else if (dwStrType & CERT_NAME_STR_SEMICOLON_FLAG)
1079 sep = semiSep;
1080 else if (dwStrType & CERT_NAME_STR_CRLF_FLAG)
1081 sep = crlfSep;
1082 else if (dwStrType & CERT_NAME_STR_NO_PLUS_FLAG)
1083 sep = allSepsWithoutPlus;
1084 else
1085 sep = allSeps;
1086 ret = CRYPT_GetNextValueW(str, dwStrType, sep, &sep_used, &token,
1087 ppszError);
1088 if (ret)
1089 {
1090 str = token.end;
1091 /* if token.end points to the separator, skip it */
1092 if (str && sep_used && *str == sep_used) str++;
1093
1094 ret = CRYPT_ValueToRDN(dwCertEncodingType, &info,
1095 keyOID, &token, dwStrType, ppszError);
1096 }
1097 }
1098 }
1099 }
1100 }
1101 CRYPT_FreeKeynameKeeper(&keeper);
1102 if (ret)
1103 {
1104 if (ppszError)
1105 *ppszError = NULL;
1106 ret = CryptEncodeObjectEx(dwCertEncodingType, X509_NAME, &info,
1107 0, NULL, pbEncoded, pcbEncoded);
1108 }
1109 for (i = 0; i < info.cRDN; i++)
1110 {
1111 DWORD j;
1112
1113 for (j = 0; j < info.rgRDN[i].cRDNAttr; j++)
1114 LocalFree(info.rgRDN[i].rgRDNAttr[j].Value.pbData);
1115 CryptMemFree(info.rgRDN[i].rgRDNAttr);
1116 }
1117 CryptMemFree(info.rgRDN);
1118 return ret;
1119 }
1120
1121 DWORD WINAPI CertGetNameStringA(PCCERT_CONTEXT pCertContext, DWORD dwType,
1122 DWORD dwFlags, void *pvTypePara, LPSTR pszNameString, DWORD cchNameString)
1123 {
1124 DWORD ret;
1125
1126 TRACE("(%p, %d, %08x, %p, %p, %d)\n", pCertContext, dwType, dwFlags,
1127 pvTypePara, pszNameString, cchNameString);
1128
1129 if (pszNameString)
1130 {
1131 LPWSTR wideName;
1132 DWORD nameLen;
1133
1134 nameLen = CertGetNameStringW(pCertContext, dwType, dwFlags, pvTypePara,
1135 NULL, 0);
1136 wideName = CryptMemAlloc(nameLen * sizeof(WCHAR));
1137 if (wideName)
1138 {
1139 CertGetNameStringW(pCertContext, dwType, dwFlags, pvTypePara,
1140 wideName, nameLen);
1141 nameLen = WideCharToMultiByte(CP_ACP, 0, wideName, nameLen,
1142 pszNameString, cchNameString, NULL, NULL);
1143 if (nameLen <= cchNameString)
1144 ret = nameLen;
1145 else
1146 {
1147 pszNameString[cchNameString - 1] = '\0';
1148 ret = cchNameString;
1149 }
1150 CryptMemFree(wideName);
1151 }
1152 else
1153 {
1154 *pszNameString = '\0';
1155 ret = 1;
1156 }
1157 }
1158 else
1159 ret = CertGetNameStringW(pCertContext, dwType, dwFlags, pvTypePara,
1160 NULL, 0);
1161 return ret;
1162 }
1163
1164 /* Searches cert's extensions for the alternate name extension with OID
1165 * altNameOID, and if found, searches it for the alternate name type entryType.
1166 * If found, returns a pointer to the entry, otherwise returns NULL.
1167 * Regardless of whether an entry of the desired type is found, if the
1168 * alternate name extension is present, sets *info to the decoded alternate
1169 * name extension, which you must free using LocalFree.
1170 * The return value is a pointer within *info, so don't free *info before
1171 * you're done with the return value.
1172 */
1173 static PCERT_ALT_NAME_ENTRY cert_find_alt_name_entry(PCCERT_CONTEXT cert,
1174 LPCSTR altNameOID, DWORD entryType, PCERT_ALT_NAME_INFO *info)
1175 {
1176 PCERT_ALT_NAME_ENTRY entry = NULL;
1177 PCERT_EXTENSION ext = CertFindExtension(altNameOID,
1178 cert->pCertInfo->cExtension, cert->pCertInfo->rgExtension);
1179
1180 if (ext)
1181 {
1182 DWORD bytes = 0;
1183
1184 if (CryptDecodeObjectEx(cert->dwCertEncodingType, X509_ALTERNATE_NAME,
1185 ext->Value.pbData, ext->Value.cbData, CRYPT_DECODE_ALLOC_FLAG, NULL,
1186 info, &bytes))
1187 {
1188 DWORD i;
1189
1190 for (i = 0; !entry && i < (*info)->cAltEntry; i++)
1191 if ((*info)->rgAltEntry[i].dwAltNameChoice == entryType)
1192 entry = &(*info)->rgAltEntry[i];
1193 }
1194 }
1195 else
1196 *info = NULL;
1197 return entry;
1198 }
1199
1200 static DWORD cert_get_name_from_rdn_attr(DWORD encodingType,
1201 const CERT_NAME_BLOB *name, LPCSTR oid, LPWSTR pszNameString, DWORD cchNameString)
1202 {
1203 CERT_NAME_INFO *nameInfo;
1204 DWORD bytes = 0, ret = 0;
1205
1206 if (CryptDecodeObjectEx(encodingType, X509_NAME, name->pbData,
1207 name->cbData, CRYPT_DECODE_ALLOC_FLAG, NULL, &nameInfo, &bytes))
1208 {
1209 PCERT_RDN_ATTR nameAttr;
1210
1211 if (!oid)
1212 oid = szOID_RSA_emailAddr;
1213 nameAttr = CertFindRDNAttr(oid, nameInfo);
1214 if (nameAttr)
1215 ret = CertRDNValueToStrW(nameAttr->dwValueType, &nameAttr->Value,
1216 pszNameString, cchNameString);
1217 LocalFree(nameInfo);
1218 }
1219 return ret;
1220 }
1221
1222 DWORD WINAPI CertGetNameStringW(PCCERT_CONTEXT pCertContext, DWORD dwType,
1223 DWORD dwFlags, void *pvTypePara, LPWSTR pszNameString, DWORD cchNameString)
1224 {
1225 DWORD ret = 0;
1226 PCERT_NAME_BLOB name;
1227 LPCSTR altNameOID;
1228
1229 TRACE("(%p, %d, %08x, %p, %p, %d)\n", pCertContext, dwType,
1230 dwFlags, pvTypePara, pszNameString, cchNameString);
1231
1232 if (!pCertContext)
1233 goto done;
1234
1235 if (dwFlags & CERT_NAME_ISSUER_FLAG)
1236 {
1237 name = &pCertContext->pCertInfo->Issuer;
1238 altNameOID = szOID_ISSUER_ALT_NAME;
1239 }
1240 else
1241 {
1242 name = &pCertContext->pCertInfo->Subject;
1243 altNameOID = szOID_SUBJECT_ALT_NAME;
1244 }
1245
1246 switch (dwType)
1247 {
1248 case CERT_NAME_EMAIL_TYPE:
1249 {
1250 CERT_ALT_NAME_INFO *info;
1251 PCERT_ALT_NAME_ENTRY entry = cert_find_alt_name_entry(pCertContext,
1252 altNameOID, CERT_ALT_NAME_RFC822_NAME, &info);
1253
1254 if (entry)
1255 {
1256 if (!pszNameString)
1257 ret = strlenW(entry->u.pwszRfc822Name) + 1;
1258 else if (cchNameString)
1259 {
1260 ret = min(strlenW(entry->u.pwszRfc822Name), cchNameString - 1);
1261 memcpy(pszNameString, entry->u.pwszRfc822Name,
1262 ret * sizeof(WCHAR));
1263 pszNameString[ret++] = 0;
1264 }
1265 }
1266 if (info)
1267 LocalFree(info);
1268 if (!ret)
1269 ret = cert_get_name_from_rdn_attr(pCertContext->dwCertEncodingType,
1270 name, szOID_RSA_emailAddr, pszNameString, cchNameString);
1271 break;
1272 }
1273 case CERT_NAME_RDN_TYPE:
1274 {
1275 DWORD type = pvTypePara ? *(DWORD *)pvTypePara : 0;
1276
1277 if (name->cbData)
1278 ret = CertNameToStrW(pCertContext->dwCertEncodingType, name,
1279 type, pszNameString, cchNameString);
1280 else
1281 {
1282 CERT_ALT_NAME_INFO *info;
1283 PCERT_ALT_NAME_ENTRY entry = cert_find_alt_name_entry(pCertContext,
1284 altNameOID, CERT_ALT_NAME_DIRECTORY_NAME, &info);
1285
1286 if (entry)
1287 ret = CertNameToStrW(pCertContext->dwCertEncodingType,
1288 &entry->u.DirectoryName, type, pszNameString, cchNameString);
1289 if (info)
1290 LocalFree(info);
1291 }
1292 break;
1293 }
1294 case CERT_NAME_ATTR_TYPE:
1295 ret = cert_get_name_from_rdn_attr(pCertContext->dwCertEncodingType,
1296 name, pvTypePara, pszNameString, cchNameString);
1297 if (!ret)
1298 {
1299 CERT_ALT_NAME_INFO *altInfo;
1300 PCERT_ALT_NAME_ENTRY entry = cert_find_alt_name_entry(pCertContext,
1301 altNameOID, CERT_ALT_NAME_DIRECTORY_NAME, &altInfo);
1302
1303 if (entry)
1304 ret = cert_name_to_str_with_indent(X509_ASN_ENCODING, 0,
1305 &entry->u.DirectoryName, 0, pszNameString, cchNameString);
1306 if (altInfo)
1307 LocalFree(altInfo);
1308 }
1309 break;
1310 case CERT_NAME_SIMPLE_DISPLAY_TYPE:
1311 {
1312 static const LPCSTR simpleAttributeOIDs[] = { szOID_COMMON_NAME,
1313 szOID_ORGANIZATIONAL_UNIT_NAME, szOID_ORGANIZATION_NAME,
1314 szOID_RSA_emailAddr };
1315 CERT_NAME_INFO *nameInfo = NULL;
1316 DWORD bytes = 0, i;
1317
1318 if (CryptDecodeObjectEx(pCertContext->dwCertEncodingType, X509_NAME,
1319 name->pbData, name->cbData, CRYPT_DECODE_ALLOC_FLAG, NULL, &nameInfo,
1320 &bytes))
1321 {
1322 PCERT_RDN_ATTR nameAttr = NULL;
1323
1324 for (i = 0; !nameAttr && i < sizeof(simpleAttributeOIDs) /
1325 sizeof(simpleAttributeOIDs[0]); i++)
1326 nameAttr = CertFindRDNAttr(simpleAttributeOIDs[i], nameInfo);
1327 if (nameAttr)
1328 ret = CertRDNValueToStrW(nameAttr->dwValueType,
1329 &nameAttr->Value, pszNameString, cchNameString);
1330 LocalFree(nameInfo);
1331 }
1332 if (!ret)
1333 {
1334 CERT_ALT_NAME_INFO *altInfo;
1335 PCERT_ALT_NAME_ENTRY entry = cert_find_alt_name_entry(pCertContext,
1336 altNameOID, CERT_ALT_NAME_RFC822_NAME, &altInfo);
1337
1338 if (altInfo)
1339 {
1340 if (!entry && altInfo->cAltEntry)
1341 entry = &altInfo->rgAltEntry[0];
1342 if (entry)
1343 {
1344 if (!pszNameString)
1345 ret = strlenW(entry->u.pwszRfc822Name) + 1;
1346 else if (cchNameString)
1347 {
1348 ret = min(strlenW(entry->u.pwszRfc822Name),
1349 cchNameString - 1);
1350 memcpy(pszNameString, entry->u.pwszRfc822Name,
1351 ret * sizeof(WCHAR));
1352 pszNameString[ret++] = 0;
1353 }
1354 }
1355 LocalFree(altInfo);
1356 }
1357 }
1358 break;
1359 }
1360 case CERT_NAME_FRIENDLY_DISPLAY_TYPE:
1361 {
1362 DWORD cch = cchNameString;
1363
1364 if (CertGetCertificateContextProperty(pCertContext,
1365 CERT_FRIENDLY_NAME_PROP_ID, pszNameString, &cch))
1366 ret = cch;
1367 else
1368 ret = CertGetNameStringW(pCertContext,
1369 CERT_NAME_SIMPLE_DISPLAY_TYPE, dwFlags, pvTypePara, pszNameString,
1370 cchNameString);
1371 break;
1372 }
1373 case CERT_NAME_DNS_TYPE:
1374 {
1375 CERT_ALT_NAME_INFO *info;
1376 PCERT_ALT_NAME_ENTRY entry = cert_find_alt_name_entry(pCertContext,
1377 altNameOID, CERT_ALT_NAME_DNS_NAME, &info);
1378
1379 if (entry)
1380 {
1381 if (!pszNameString)
1382 ret = strlenW(entry->u.pwszDNSName) + 1;
1383 else if (cchNameString)
1384 {
1385 ret = min(strlenW(entry->u.pwszDNSName), cchNameString - 1);
1386 memcpy(pszNameString, entry->u.pwszDNSName, ret * sizeof(WCHAR));
1387 pszNameString[ret++] = 0;
1388 }
1389 }
1390 if (info)
1391 LocalFree(info);
1392 if (!ret)
1393 ret = cert_get_name_from_rdn_attr(pCertContext->dwCertEncodingType,
1394 name, szOID_COMMON_NAME, pszNameString, cchNameString);
1395 break;
1396 }
1397 case CERT_NAME_URL_TYPE:
1398 {
1399 CERT_ALT_NAME_INFO *info;
1400 PCERT_ALT_NAME_ENTRY entry = cert_find_alt_name_entry(pCertContext,
1401 altNameOID, CERT_ALT_NAME_URL, &info);
1402
1403 if (entry)
1404 {
1405 if (!pszNameString)
1406 ret = strlenW(entry->u.pwszURL) + 1;
1407 else if (cchNameString)
1408 {
1409 ret = min(strlenW(entry->u.pwszURL), cchNameString - 1);
1410 memcpy(pszNameString, entry->u.pwszURL, ret * sizeof(WCHAR));
1411 pszNameString[ret++] = 0;
1412 }
1413 }
1414 if (info)
1415 LocalFree(info);
1416 break;
1417 }
1418 default:
1419 FIXME("unimplemented for type %d\n", dwType);
1420 ret = 0;
1421 }
1422 done:
1423 if (!ret)
1424 {
1425 if (!pszNameString)
1426 ret = 1;
1427 else if (cchNameString)
1428 {
1429 pszNameString[0] = 0;
1430 ret = 1;
1431 }
1432 }
1433 return ret;
1434 }