mfplat: Read queue subscriber within the critical section.
[wine/zf.git] / dlls / crypt32 / str.c
blob0ab4c7c6e85ce9aad9253fd82ade24147860dc38
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>
20 #define NONAMELESSUNION
22 #include "windef.h"
23 #include "winbase.h"
24 #include "winnls.h"
25 #include "winuser.h"
26 #include "wincrypt.h"
27 #include "wine/debug.h"
28 #include "crypt32_private.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(crypt);
32 DWORD WINAPI CertRDNValueToStrA(DWORD dwValueType, PCERT_RDN_VALUE_BLOB pValue,
33 LPSTR psz, DWORD csz)
35 DWORD ret = 0, len;
37 TRACE("(%d, %p, %p, %d)\n", dwValueType, pValue, psz, csz);
39 switch (dwValueType)
41 case CERT_RDN_ANY_TYPE:
42 break;
43 case CERT_RDN_NUMERIC_STRING:
44 case CERT_RDN_PRINTABLE_STRING:
45 case CERT_RDN_TELETEX_STRING:
46 case CERT_RDN_VIDEOTEX_STRING:
47 case CERT_RDN_IA5_STRING:
48 case CERT_RDN_GRAPHIC_STRING:
49 case CERT_RDN_VISIBLE_STRING:
50 case CERT_RDN_GENERAL_STRING:
51 len = pValue->cbData;
52 if (!psz || !csz)
53 ret = len;
54 else
56 DWORD chars = min(len, csz - 1);
58 if (chars)
60 memcpy(psz, pValue->pbData, chars);
61 ret += chars;
62 csz -= chars;
65 break;
66 case CERT_RDN_BMP_STRING:
67 case CERT_RDN_UTF8_STRING:
68 len = WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)pValue->pbData,
69 pValue->cbData / sizeof(WCHAR), NULL, 0, NULL, NULL);
70 if (!psz || !csz)
71 ret = len;
72 else
74 DWORD chars = min(pValue->cbData / sizeof(WCHAR), csz - 1);
76 if (chars)
78 ret = WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)pValue->pbData,
79 chars, psz, csz - 1, NULL, NULL);
80 csz -= ret;
83 break;
84 default:
85 FIXME("string type %d unimplemented\n", dwValueType);
87 if (psz && csz)
89 *(psz + ret) = '\0';
90 csz--;
91 ret++;
93 else
94 ret++;
95 TRACE("returning %d (%s)\n", ret, debugstr_a(psz));
96 return ret;
99 DWORD WINAPI CertRDNValueToStrW(DWORD dwValueType, PCERT_RDN_VALUE_BLOB pValue,
100 LPWSTR psz, DWORD csz)
102 DWORD ret = 0, len, i, strLen;
104 TRACE("(%d, %p, %p, %d)\n", dwValueType, pValue, psz, csz);
106 switch (dwValueType)
108 case CERT_RDN_ANY_TYPE:
109 break;
110 case CERT_RDN_NUMERIC_STRING:
111 case CERT_RDN_PRINTABLE_STRING:
112 case CERT_RDN_TELETEX_STRING:
113 case CERT_RDN_VIDEOTEX_STRING:
114 case CERT_RDN_IA5_STRING:
115 case CERT_RDN_GRAPHIC_STRING:
116 case CERT_RDN_VISIBLE_STRING:
117 case CERT_RDN_GENERAL_STRING:
118 len = pValue->cbData;
119 if (!psz || !csz)
120 ret = len;
121 else
123 WCHAR *ptr = psz;
125 for (i = 0; i < pValue->cbData && ptr - psz < csz; ptr++, i++)
126 *ptr = pValue->pbData[i];
127 ret = ptr - psz;
129 break;
130 case CERT_RDN_BMP_STRING:
131 case CERT_RDN_UTF8_STRING:
132 strLen = len = pValue->cbData / sizeof(WCHAR);
133 if (!psz || !csz)
134 ret = len;
135 else
137 WCHAR *ptr = psz;
139 for (i = 0; i < strLen && ptr - psz < csz; ptr++, i++)
140 *ptr = ((LPCWSTR)pValue->pbData)[i];
141 ret = ptr - psz;
143 break;
144 default:
145 FIXME("string type %d unimplemented\n", dwValueType);
147 if (psz && csz)
149 *(psz + ret) = '\0';
150 csz--;
151 ret++;
153 else
154 ret++;
155 TRACE("returning %d (%s)\n", ret, debugstr_w(psz));
156 return ret;
159 static inline BOOL is_quotable_char(char c)
161 switch(c)
163 case '+':
164 case ',':
165 case '"':
166 case '=':
167 case '<':
168 case '>':
169 case ';':
170 case '#':
171 case '\n':
172 return TRUE;
173 default:
174 return FALSE;
178 static DWORD quote_rdn_value_to_str_a(DWORD dwValueType,
179 PCERT_RDN_VALUE_BLOB pValue, LPSTR psz, DWORD csz)
181 DWORD ret = 0, len, i;
182 BOOL needsQuotes = FALSE;
184 TRACE("(%d, %p, %p, %d)\n", dwValueType, pValue, psz, csz);
186 switch (dwValueType)
188 case CERT_RDN_ANY_TYPE:
189 break;
190 case CERT_RDN_NUMERIC_STRING:
191 case CERT_RDN_PRINTABLE_STRING:
192 case CERT_RDN_TELETEX_STRING:
193 case CERT_RDN_VIDEOTEX_STRING:
194 case CERT_RDN_IA5_STRING:
195 case CERT_RDN_GRAPHIC_STRING:
196 case CERT_RDN_VISIBLE_STRING:
197 case CERT_RDN_GENERAL_STRING:
198 len = pValue->cbData;
199 if (pValue->cbData && isspace(pValue->pbData[0]))
200 needsQuotes = TRUE;
201 if (pValue->cbData && isspace(pValue->pbData[pValue->cbData - 1]))
202 needsQuotes = TRUE;
203 for (i = 0; i < pValue->cbData; i++)
205 if (is_quotable_char(pValue->pbData[i]))
206 needsQuotes = TRUE;
207 if (pValue->pbData[i] == '"')
208 len += 1;
210 if (needsQuotes)
211 len += 2;
212 if (!psz || !csz)
213 ret = len;
214 else
216 char *ptr = psz;
218 if (needsQuotes)
219 *ptr++ = '"';
220 for (i = 0; i < pValue->cbData && ptr - psz < csz; ptr++, i++)
222 *ptr = pValue->pbData[i];
223 if (pValue->pbData[i] == '"' && ptr - psz < csz - 1)
224 *(++ptr) = '"';
226 if (needsQuotes && ptr - psz < csz)
227 *ptr++ = '"';
228 ret = ptr - psz;
230 break;
231 case CERT_RDN_BMP_STRING:
232 case CERT_RDN_UTF8_STRING:
233 len = WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)pValue->pbData,
234 pValue->cbData / sizeof(WCHAR), NULL, 0, NULL, NULL);
235 if (pValue->cbData && iswspace(((LPCWSTR)pValue->pbData)[0]))
236 needsQuotes = TRUE;
237 if (pValue->cbData &&
238 iswspace(((LPCWSTR)pValue->pbData)[pValue->cbData / sizeof(WCHAR)-1]))
239 needsQuotes = TRUE;
240 for (i = 0; i < pValue->cbData / sizeof(WCHAR); i++)
242 if (is_quotable_char(((LPCWSTR)pValue->pbData)[i]))
243 needsQuotes = TRUE;
244 if (((LPCWSTR)pValue->pbData)[i] == '"')
245 len += 1;
247 if (needsQuotes)
248 len += 2;
249 if (!psz || !csz)
250 ret = len;
251 else
253 char *dst = psz;
255 if (needsQuotes)
256 *dst++ = '"';
257 for (i = 0; i < pValue->cbData / sizeof(WCHAR) &&
258 dst - psz < csz; dst++, i++)
260 LPCWSTR src = (LPCWSTR)pValue->pbData + i;
262 WideCharToMultiByte(CP_ACP, 0, src, 1, dst,
263 csz - (dst - psz) - 1, NULL, NULL);
264 if (*src == '"' && dst - psz < csz - 1)
265 *(++dst) = '"';
267 if (needsQuotes && dst - psz < csz)
268 *dst++ = '"';
269 ret = dst - psz;
271 break;
272 default:
273 FIXME("string type %d unimplemented\n", dwValueType);
275 if (psz && csz)
277 *(psz + ret) = '\0';
278 csz--;
279 ret++;
281 else
282 ret++;
283 TRACE("returning %d (%s)\n", ret, debugstr_a(psz));
284 return ret;
287 static DWORD quote_rdn_value_to_str_w(DWORD dwValueType,
288 PCERT_RDN_VALUE_BLOB pValue, LPWSTR psz, DWORD csz)
290 DWORD ret = 0, len, i, strLen;
291 BOOL needsQuotes = FALSE;
293 TRACE("(%d, %p, %p, %d)\n", dwValueType, pValue, psz, csz);
295 switch (dwValueType)
297 case CERT_RDN_ANY_TYPE:
298 break;
299 case CERT_RDN_NUMERIC_STRING:
300 case CERT_RDN_PRINTABLE_STRING:
301 case CERT_RDN_TELETEX_STRING:
302 case CERT_RDN_VIDEOTEX_STRING:
303 case CERT_RDN_IA5_STRING:
304 case CERT_RDN_GRAPHIC_STRING:
305 case CERT_RDN_VISIBLE_STRING:
306 case CERT_RDN_GENERAL_STRING:
307 len = pValue->cbData;
308 if (pValue->cbData && isspace(pValue->pbData[0]))
309 needsQuotes = TRUE;
310 if (pValue->cbData && isspace(pValue->pbData[pValue->cbData - 1]))
311 needsQuotes = TRUE;
312 for (i = 0; i < pValue->cbData; i++)
314 if (is_quotable_char(pValue->pbData[i]))
315 needsQuotes = TRUE;
316 if (pValue->pbData[i] == '"')
317 len += 1;
319 if (needsQuotes)
320 len += 2;
321 if (!psz || !csz)
322 ret = len;
323 else
325 WCHAR *ptr = psz;
327 if (needsQuotes)
328 *ptr++ = '"';
329 for (i = 0; i < pValue->cbData && ptr - psz < csz; ptr++, i++)
331 *ptr = pValue->pbData[i];
332 if (pValue->pbData[i] == '"' && ptr - psz < csz - 1)
333 *(++ptr) = '"';
335 if (needsQuotes && ptr - psz < csz)
336 *ptr++ = '"';
337 ret = ptr - psz;
339 break;
340 case CERT_RDN_BMP_STRING:
341 case CERT_RDN_UTF8_STRING:
342 strLen = len = pValue->cbData / sizeof(WCHAR);
343 if (pValue->cbData && isspace(pValue->pbData[0]))
344 needsQuotes = TRUE;
345 if (pValue->cbData && isspace(pValue->pbData[strLen - 1]))
346 needsQuotes = TRUE;
347 for (i = 0; i < strLen; i++)
349 if (is_quotable_char(((LPCWSTR)pValue->pbData)[i]))
350 needsQuotes = TRUE;
351 if (((LPCWSTR)pValue->pbData)[i] == '"')
352 len += 1;
354 if (needsQuotes)
355 len += 2;
356 if (!psz || !csz)
357 ret = len;
358 else
360 WCHAR *ptr = psz;
362 if (needsQuotes)
363 *ptr++ = '"';
364 for (i = 0; i < strLen && ptr - psz < csz; ptr++, i++)
366 *ptr = ((LPCWSTR)pValue->pbData)[i];
367 if (((LPCWSTR)pValue->pbData)[i] == '"' && ptr - psz < csz - 1)
368 *(++ptr) = '"';
370 if (needsQuotes && ptr - psz < csz)
371 *ptr++ = '"';
372 ret = ptr - psz;
374 break;
375 default:
376 FIXME("string type %d unimplemented\n", dwValueType);
378 if (psz && csz)
380 *(psz + ret) = '\0';
381 csz--;
382 ret++;
384 else
385 ret++;
386 TRACE("returning %d (%s)\n", ret, debugstr_w(psz));
387 return ret;
390 /* Adds the prefix prefix to the string pointed to by psz, followed by the
391 * character '='. Copies no more than csz characters. Returns the number of
392 * characters copied. If psz is NULL, returns the number of characters that
393 * would be copied.
395 static DWORD CRYPT_AddPrefixA(LPCSTR prefix, LPSTR psz, DWORD csz)
397 DWORD chars;
399 TRACE("(%s, %p, %d)\n", debugstr_a(prefix), psz, csz);
401 if (psz)
403 chars = min(strlen(prefix), csz);
404 memcpy(psz, prefix, chars);
405 *(psz + chars) = '=';
406 chars++;
408 else
409 chars = lstrlenA(prefix) + 1;
410 return chars;
413 DWORD WINAPI CertNameToStrA(DWORD dwCertEncodingType, PCERT_NAME_BLOB pName,
414 DWORD dwStrType, LPSTR psz, DWORD csz)
416 static const DWORD unsupportedFlags = CERT_NAME_STR_NO_QUOTING_FLAG |
417 CERT_NAME_STR_ENABLE_T61_UNICODE_FLAG;
418 static const char commaSep[] = ", ";
419 static const char semiSep[] = "; ";
420 static const char crlfSep[] = "\r\n";
421 static const char plusSep[] = " + ";
422 static const char spaceSep[] = " ";
423 DWORD ret = 0, bytes = 0;
424 BOOL bRet;
425 CERT_NAME_INFO *info;
427 TRACE("(%d, %p, %08x, %p, %d)\n", dwCertEncodingType, pName, dwStrType,
428 psz, csz);
429 if (dwStrType & unsupportedFlags)
430 FIXME("unsupported flags: %08x\n", dwStrType & unsupportedFlags);
432 bRet = CryptDecodeObjectEx(dwCertEncodingType, X509_NAME, pName->pbData,
433 pName->cbData, CRYPT_DECODE_ALLOC_FLAG, NULL, &info, &bytes);
434 if (bRet)
436 DWORD i, j, sepLen, rdnSepLen;
437 LPCSTR sep, rdnSep;
438 BOOL reverse = dwStrType & CERT_NAME_STR_REVERSE_FLAG;
439 const CERT_RDN *rdn = info->rgRDN;
441 if(reverse && info->cRDN > 1) rdn += (info->cRDN - 1);
443 if (dwStrType & CERT_NAME_STR_SEMICOLON_FLAG)
444 sep = semiSep;
445 else if (dwStrType & CERT_NAME_STR_CRLF_FLAG)
446 sep = crlfSep;
447 else
448 sep = commaSep;
449 sepLen = strlen(sep);
450 if (dwStrType & CERT_NAME_STR_NO_PLUS_FLAG)
451 rdnSep = spaceSep;
452 else
453 rdnSep = plusSep;
454 rdnSepLen = strlen(rdnSep);
455 for (i = 0; (!psz || ret < csz) && i < info->cRDN; i++)
457 for (j = 0; (!psz || ret < csz) && j < rdn->cRDNAttr; j++)
459 DWORD chars;
460 char prefixBuf[13]; /* big enough for SERIALNUMBER */
461 LPCSTR prefix = NULL;
463 if ((dwStrType & 0x000000ff) == CERT_OID_NAME_STR)
464 prefix = rdn->rgRDNAttr[j].pszObjId;
465 else if ((dwStrType & 0x000000ff) == CERT_X500_NAME_STR)
467 PCCRYPT_OID_INFO oidInfo = CryptFindOIDInfo(
468 CRYPT_OID_INFO_OID_KEY,
469 rdn->rgRDNAttr[j].pszObjId,
470 CRYPT_RDN_ATTR_OID_GROUP_ID);
472 if (oidInfo)
474 WideCharToMultiByte(CP_ACP, 0, oidInfo->pwszName, -1,
475 prefixBuf, sizeof(prefixBuf), NULL, NULL);
476 prefix = prefixBuf;
478 else
479 prefix = rdn->rgRDNAttr[j].pszObjId;
481 if (prefix)
483 /* - 1 is needed to account for the NULL terminator. */
484 chars = CRYPT_AddPrefixA(prefix,
485 psz ? psz + ret : NULL, psz ? csz - ret - 1 : 0);
486 ret += chars;
488 chars = quote_rdn_value_to_str_a(
489 rdn->rgRDNAttr[j].dwValueType,
490 &rdn->rgRDNAttr[j].Value, psz ? psz + ret : NULL,
491 psz ? csz - ret : 0);
492 if (chars)
493 ret += chars - 1;
494 if (j < rdn->cRDNAttr - 1)
496 if (psz && ret < csz - rdnSepLen - 1)
497 memcpy(psz + ret, rdnSep, rdnSepLen);
498 ret += rdnSepLen;
501 if (i < info->cRDN - 1)
503 if (psz && ret < csz - sepLen - 1)
504 memcpy(psz + ret, sep, sepLen);
505 ret += sepLen;
507 if(reverse) rdn--;
508 else rdn++;
510 LocalFree(info);
512 if (psz && csz)
514 *(psz + ret) = '\0';
515 ret++;
517 else
518 ret++;
519 TRACE("Returning %s\n", debugstr_a(psz));
520 return ret;
523 /* Adds the prefix prefix to the wide-character string pointed to by psz,
524 * followed by the character '='. Copies no more than csz characters. Returns
525 * the number of characters copied. If psz is NULL, returns the number of
526 * characters that would be copied.
527 * Assumes the characters in prefix are ASCII (not multibyte characters.)
529 static DWORD CRYPT_AddPrefixAToW(LPCSTR prefix, LPWSTR psz, DWORD csz)
531 DWORD chars;
533 TRACE("(%s, %p, %d)\n", debugstr_a(prefix), psz, csz);
535 if (psz)
537 DWORD i;
539 chars = min(strlen(prefix), csz);
540 for (i = 0; i < chars; i++)
541 *(psz + i) = prefix[i];
542 *(psz + chars) = '=';
543 chars++;
545 else
546 chars = lstrlenA(prefix) + 1;
547 return chars;
550 /* Adds the prefix prefix to the string pointed to by psz, followed by the
551 * character '='. Copies no more than csz characters. Returns the number of
552 * characters copied. If psz is NULL, returns the number of characters that
553 * would be copied.
555 static DWORD CRYPT_AddPrefixW(LPCWSTR prefix, LPWSTR psz, DWORD csz)
557 DWORD chars;
559 TRACE("(%s, %p, %d)\n", debugstr_w(prefix), psz, csz);
561 if (psz)
563 chars = min(lstrlenW(prefix), csz);
564 memcpy(psz, prefix, chars * sizeof(WCHAR));
565 *(psz + chars) = '=';
566 chars++;
568 else
569 chars = lstrlenW(prefix) + 1;
570 return chars;
573 static const WCHAR indent[] = L" ";
575 DWORD cert_name_to_str_with_indent(DWORD dwCertEncodingType, DWORD indentLevel,
576 const CERT_NAME_BLOB *pName, DWORD dwStrType, LPWSTR psz, DWORD csz)
578 static const DWORD unsupportedFlags = CERT_NAME_STR_NO_QUOTING_FLAG |
579 CERT_NAME_STR_ENABLE_T61_UNICODE_FLAG;
580 DWORD ret = 0, bytes = 0;
581 BOOL bRet;
582 CERT_NAME_INFO *info;
584 if (dwStrType & unsupportedFlags)
585 FIXME("unsupported flags: %08x\n", dwStrType & unsupportedFlags);
587 bRet = CryptDecodeObjectEx(dwCertEncodingType, X509_NAME, pName->pbData,
588 pName->cbData, CRYPT_DECODE_ALLOC_FLAG, NULL, &info, &bytes);
589 if (bRet)
591 DWORD i, j, sepLen, rdnSepLen;
592 LPCWSTR sep, rdnSep;
593 BOOL reverse = dwStrType & CERT_NAME_STR_REVERSE_FLAG;
594 const CERT_RDN *rdn = info->rgRDN;
596 if(reverse && info->cRDN > 1) rdn += (info->cRDN - 1);
598 if (dwStrType & CERT_NAME_STR_SEMICOLON_FLAG)
599 sep = L"; ";
600 else if (dwStrType & CERT_NAME_STR_CRLF_FLAG)
601 sep = L"\r\n";
602 else
603 sep = L", ";
604 sepLen = lstrlenW(sep);
605 if (dwStrType & CERT_NAME_STR_NO_PLUS_FLAG)
606 rdnSep = L" ";
607 else
608 rdnSep = L" + ";
609 rdnSepLen = lstrlenW(rdnSep);
610 for (i = 0; (!psz || ret < csz) && i < info->cRDN; i++)
612 for (j = 0; (!psz || ret < csz) && j < rdn->cRDNAttr; j++)
614 DWORD chars;
615 LPCSTR prefixA = NULL;
616 LPCWSTR prefixW = NULL;
618 if ((dwStrType & 0x000000ff) == CERT_OID_NAME_STR)
619 prefixA = rdn->rgRDNAttr[j].pszObjId;
620 else if ((dwStrType & 0x000000ff) == CERT_X500_NAME_STR)
622 PCCRYPT_OID_INFO oidInfo = CryptFindOIDInfo(
623 CRYPT_OID_INFO_OID_KEY,
624 rdn->rgRDNAttr[j].pszObjId,
625 CRYPT_RDN_ATTR_OID_GROUP_ID);
627 if (oidInfo)
628 prefixW = oidInfo->pwszName;
629 else
630 prefixA = rdn->rgRDNAttr[j].pszObjId;
632 if (dwStrType & CERT_NAME_STR_CRLF_FLAG)
634 DWORD k;
636 for (k = 0; k < indentLevel; k++)
638 if (psz)
640 chars = min(lstrlenW(indent), csz - ret - 1);
641 memcpy(psz + ret, indent, chars * sizeof(WCHAR));
643 else
644 chars = lstrlenW(indent);
645 ret += chars;
648 if (prefixW)
650 /* - 1 is needed to account for the NULL terminator. */
651 chars = CRYPT_AddPrefixW(prefixW,
652 psz ? psz + ret : NULL, psz ? csz - ret - 1 : 0);
653 ret += chars;
655 else if (prefixA)
657 /* - 1 is needed to account for the NULL terminator. */
658 chars = CRYPT_AddPrefixAToW(prefixA,
659 psz ? psz + ret : NULL, psz ? csz - ret - 1 : 0);
660 ret += chars;
662 chars = quote_rdn_value_to_str_w(
663 rdn->rgRDNAttr[j].dwValueType,
664 &rdn->rgRDNAttr[j].Value, psz ? psz + ret : NULL,
665 psz ? csz - ret : 0);
666 if (chars)
667 ret += chars - 1;
668 if (j < rdn->cRDNAttr - 1)
670 if (psz && ret < csz - rdnSepLen - 1)
671 memcpy(psz + ret, rdnSep, rdnSepLen * sizeof(WCHAR));
672 ret += rdnSepLen;
675 if (i < info->cRDN - 1)
677 if (psz && ret < csz - sepLen - 1)
678 memcpy(psz + ret, sep, sepLen * sizeof(WCHAR));
679 ret += sepLen;
681 if(reverse) rdn--;
682 else rdn++;
684 LocalFree(info);
686 if (psz && csz)
688 *(psz + ret) = '\0';
689 ret++;
691 else
692 ret++;
693 return ret;
696 DWORD WINAPI CertNameToStrW(DWORD dwCertEncodingType, PCERT_NAME_BLOB pName,
697 DWORD dwStrType, LPWSTR psz, DWORD csz)
699 BOOL ret;
701 TRACE("(%d, %p, %08x, %p, %d)\n", dwCertEncodingType, pName, dwStrType,
702 psz, csz);
704 ret = cert_name_to_str_with_indent(dwCertEncodingType, 0, pName, dwStrType,
705 psz, csz);
706 TRACE("Returning %s\n", debugstr_w(psz));
707 return ret;
710 BOOL WINAPI CertStrToNameA(DWORD dwCertEncodingType, LPCSTR pszX500,
711 DWORD dwStrType, void *pvReserved, BYTE *pbEncoded, DWORD *pcbEncoded,
712 LPCSTR *ppszError)
714 BOOL ret;
715 int len;
717 TRACE("(%08x, %s, %08x, %p, %p, %p, %p)\n", dwCertEncodingType,
718 debugstr_a(pszX500), dwStrType, pvReserved, pbEncoded, pcbEncoded,
719 ppszError);
721 len = MultiByteToWideChar(CP_ACP, 0, pszX500, -1, NULL, 0);
722 if (len)
724 LPWSTR x500, errorStr;
726 if ((x500 = CryptMemAlloc(len * sizeof(WCHAR))))
728 MultiByteToWideChar(CP_ACP, 0, pszX500, -1, x500, len);
729 ret = CertStrToNameW(dwCertEncodingType, x500, dwStrType,
730 pvReserved, pbEncoded, pcbEncoded,
731 ppszError ? (LPCWSTR *)&errorStr : NULL);
732 if (ppszError)
734 if (!ret)
736 LONG i;
738 *ppszError = pszX500;
739 for (i = 0; i < errorStr - x500; i++)
740 *ppszError = CharNextA(*ppszError);
742 else
743 *ppszError = NULL;
745 CryptMemFree(x500);
747 else
749 SetLastError(ERROR_OUTOFMEMORY);
750 ret = FALSE;
753 else
755 SetLastError(CRYPT_E_INVALID_X500_STRING);
756 if (ppszError)
757 *ppszError = pszX500;
758 ret = FALSE;
760 return ret;
763 struct KeynameKeeper
765 WCHAR buf[10]; /* big enough for L"GivenName" */
766 LPWSTR keyName; /* usually = buf, but may be allocated */
767 DWORD keyLen; /* full available buffer size in WCHARs */
770 static void CRYPT_InitializeKeynameKeeper(struct KeynameKeeper *keeper)
772 keeper->keyName = keeper->buf;
773 keeper->keyLen = ARRAY_SIZE(keeper->buf);
776 static void CRYPT_FreeKeynameKeeper(struct KeynameKeeper *keeper)
778 if (keeper->keyName != keeper->buf)
779 CryptMemFree(keeper->keyName);
782 struct X500TokenW
784 LPCWSTR start;
785 LPCWSTR end;
788 static void CRYPT_KeynameKeeperFromTokenW(struct KeynameKeeper *keeper,
789 const struct X500TokenW *key)
791 DWORD len = key->end - key->start;
793 if (len >= keeper->keyLen)
795 CRYPT_FreeKeynameKeeper( keeper );
796 keeper->keyLen = len + 1;
797 keeper->keyName = CryptMemAlloc(keeper->keyLen * sizeof(WCHAR));
799 memcpy(keeper->keyName, key->start, len * sizeof(WCHAR));
800 keeper->keyName[len] = '\0';
801 TRACE("Keyname is %s\n", debugstr_w(keeper->keyName));
804 static BOOL CRYPT_GetNextKeyW(LPCWSTR str, struct X500TokenW *token,
805 LPCWSTR *ppszError)
807 BOOL ret = TRUE;
809 while (*str && iswspace(*str))
810 str++;
811 if (*str)
813 token->start = str;
814 while (*str && *str != '=' && !iswspace(*str))
815 str++;
816 if (*str && (*str == '=' || iswspace(*str)))
817 token->end = str;
818 else
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;
827 else
828 token->start = NULL;
829 return ret;
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)
836 BOOL ret = TRUE;
838 TRACE("(%s, %s, %p, %p)\n", debugstr_w(str), debugstr_w(separators), token,
839 ppszError);
841 *separator_used = 0;
842 while (*str && iswspace(*str))
843 str++;
844 if (*str)
846 token->start = str;
847 if (!(dwFlags & CERT_NAME_STR_NO_QUOTING_FLAG) && *str == '"')
849 token->end = NULL;
850 str++;
851 while (!token->end && ret)
853 while (*str && *str != '"')
854 str++;
855 if (*str == '"')
857 if (*(str + 1) != '"')
858 token->end = str + 1;
859 else
860 str += 2;
862 else
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;
872 else
874 WCHAR map[256] = { 0 };
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;
884 else
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;
892 return ret;
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.
900 static BOOL CRYPT_EncodeValueWithType(DWORD dwCertEncodingType,
901 const struct X500TokenW *value, PCERT_NAME_BLOB output, DWORD type,
902 LPCWSTR *ppszError)
904 CERT_NAME_VALUE nameValue = { type, { 0, NULL } };
905 BOOL ret = TRUE;
907 if (value->end > value->start)
909 LONG i;
910 LPWSTR ptr;
912 nameValue.Value.pbData = CryptMemAlloc((value->end - value->start + 1) *
913 sizeof(WCHAR));
914 if (!nameValue.Value.pbData)
916 SetLastError(ERROR_OUTOFMEMORY);
917 return FALSE;
919 ptr = (LPWSTR)nameValue.Value.pbData;
920 for (i = 0; i < value->end - value->start; i++)
922 *ptr++ = value->start[i];
923 if (value->start[i] == '"')
924 i++;
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.
930 *ptr = 0;
931 nameValue.Value.cbData = (LPBYTE)ptr - nameValue.Value.pbData;
933 ret = CryptEncodeObjectEx(dwCertEncodingType, X509_UNICODE_NAME_VALUE,
934 &nameValue, CRYPT_ENCODE_ALLOC_FLAG, NULL, &output->pbData,
935 &output->cbData);
936 if (!ret && ppszError)
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;
948 CryptMemFree(nameValue.Value.pbData);
949 return ret;
952 static BOOL CRYPT_EncodeValue(DWORD dwCertEncodingType,
953 const struct X500TokenW *value, PCERT_NAME_BLOB output, const DWORD *types,
954 LPCWSTR *ppszError)
956 DWORD i;
957 BOOL ret;
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;
966 static BOOL CRYPT_ValueToRDN(DWORD dwCertEncodingType, PCERT_NAME_INFO info,
967 PCCRYPT_OID_INFO keyOID, struct X500TokenW *value, DWORD dwStrType, LPCWSTR *ppszError)
969 BOOL ret = FALSE;
971 TRACE("OID %s, value %s\n", debugstr_a(keyOID->pszOID),
972 debugstr_wn(value->start, value->end - value->start));
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)
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)
986 static const DWORD defaultTypes[] = { CERT_RDN_PRINTABLE_STRING,
987 CERT_RDN_BMP_STRING, 0 };
988 const DWORD *types;
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;
1000 /* Remove surrounding quotes */
1001 if (value->start[0] == '"' && !(dwStrType & CERT_NAME_STR_NO_QUOTING_FLAG))
1003 value->start++;
1004 value->end--;
1006 ret = CRYPT_EncodeValue(dwCertEncodingType, value,
1007 &info->rgRDN[info->cRDN].rgRDNAttr[0].Value, types, ppszError);
1009 else
1010 SetLastError(ERROR_OUTOFMEMORY);
1011 info->cRDN++;
1013 else
1014 SetLastError(ERROR_OUTOFMEMORY);
1015 return ret;
1018 BOOL WINAPI CertStrToNameW(DWORD dwCertEncodingType, LPCWSTR pszX500,
1019 DWORD dwStrType, void *pvReserved, BYTE *pbEncoded, DWORD *pcbEncoded,
1020 LPCWSTR *ppszError)
1022 CERT_NAME_INFO info = { 0, NULL };
1023 LPCWSTR str;
1024 struct KeynameKeeper keeper;
1025 DWORD i;
1026 BOOL ret = TRUE;
1028 TRACE("(%08x, %s, %08x, %p, %p, %p, %p)\n", dwCertEncodingType,
1029 debugstr_w(pszX500), dwStrType, pvReserved, pbEncoded, pcbEncoded,
1030 ppszError);
1032 CRYPT_InitializeKeynameKeeper(&keeper);
1033 str = pszX500;
1034 while (str && *str && ret)
1036 struct X500TokenW token;
1038 ret = CRYPT_GetNextKeyW(str, &token, ppszError);
1039 if (ret && token.start)
1041 PCCRYPT_OID_INFO keyOID;
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)
1048 if (ppszError)
1049 *ppszError = token.start;
1050 SetLastError(CRYPT_E_INVALID_X500_STRING);
1051 ret = FALSE;
1053 else
1055 str = token.end;
1056 while (iswspace(*str))
1057 str++;
1058 if (*str != '=')
1060 if (ppszError)
1061 *ppszError = str;
1062 SetLastError(CRYPT_E_INVALID_X500_STRING);
1063 ret = FALSE;
1065 else
1067 LPCWSTR sep;
1068 WCHAR sep_used;
1070 str++;
1071 if (dwStrType & CERT_NAME_STR_COMMA_FLAG)
1072 sep = L",";
1073 else if (dwStrType & CERT_NAME_STR_SEMICOLON_FLAG)
1074 sep = L";";
1075 else if (dwStrType & CERT_NAME_STR_CRLF_FLAG)
1076 sep = L"\r\n";
1077 else if (dwStrType & CERT_NAME_STR_NO_PLUS_FLAG)
1078 sep = L",;\r\n";
1079 else
1080 sep = L"+,;\r\n";
1081 ret = CRYPT_GetNextValueW(str, dwStrType, sep, &sep_used, &token,
1082 ppszError);
1083 if (ret)
1085 str = token.end;
1086 /* if token.end points to the separator, skip it */
1087 if (str && sep_used && *str == sep_used) str++;
1089 ret = CRYPT_ValueToRDN(dwCertEncodingType, &info,
1090 keyOID, &token, dwStrType, ppszError);
1096 CRYPT_FreeKeynameKeeper(&keeper);
1097 if (ret)
1099 if (ppszError)
1100 *ppszError = NULL;
1101 ret = CryptEncodeObjectEx(dwCertEncodingType, X509_NAME, &info,
1102 0, NULL, pbEncoded, pcbEncoded);
1104 for (i = 0; i < info.cRDN; i++)
1106 DWORD j;
1108 for (j = 0; j < info.rgRDN[i].cRDNAttr; j++)
1109 LocalFree(info.rgRDN[i].rgRDNAttr[j].Value.pbData);
1110 CryptMemFree(info.rgRDN[i].rgRDNAttr);
1112 CryptMemFree(info.rgRDN);
1113 return ret;
1116 DWORD WINAPI CertGetNameStringA(PCCERT_CONTEXT pCertContext, DWORD dwType,
1117 DWORD dwFlags, void *pvTypePara, LPSTR pszNameString, DWORD cchNameString)
1119 DWORD ret;
1121 TRACE("(%p, %d, %08x, %p, %p, %d)\n", pCertContext, dwType, dwFlags,
1122 pvTypePara, pszNameString, cchNameString);
1124 if (pszNameString)
1126 LPWSTR wideName;
1127 DWORD nameLen;
1129 nameLen = CertGetNameStringW(pCertContext, dwType, dwFlags, pvTypePara,
1130 NULL, 0);
1131 wideName = CryptMemAlloc(nameLen * sizeof(WCHAR));
1132 if (wideName)
1134 CertGetNameStringW(pCertContext, dwType, dwFlags, pvTypePara,
1135 wideName, nameLen);
1136 nameLen = WideCharToMultiByte(CP_ACP, 0, wideName, nameLen,
1137 pszNameString, cchNameString, NULL, NULL);
1138 if (nameLen <= cchNameString)
1139 ret = nameLen;
1140 else
1142 pszNameString[cchNameString - 1] = '\0';
1143 ret = cchNameString;
1145 CryptMemFree(wideName);
1147 else
1149 *pszNameString = '\0';
1150 ret = 1;
1153 else
1154 ret = CertGetNameStringW(pCertContext, dwType, dwFlags, pvTypePara,
1155 NULL, 0);
1156 return ret;
1159 /* Searches cert's extensions for the alternate name extension with OID
1160 * altNameOID, and if found, searches it for the alternate name type entryType.
1161 * If found, returns a pointer to the entry, otherwise returns NULL.
1162 * Regardless of whether an entry of the desired type is found, if the
1163 * alternate name extension is present, sets *info to the decoded alternate
1164 * name extension, which you must free using LocalFree.
1165 * The return value is a pointer within *info, so don't free *info before
1166 * you're done with the return value.
1168 static PCERT_ALT_NAME_ENTRY cert_find_alt_name_entry(PCCERT_CONTEXT cert,
1169 LPCSTR altNameOID, DWORD entryType, PCERT_ALT_NAME_INFO *info)
1171 PCERT_ALT_NAME_ENTRY entry = NULL;
1172 PCERT_EXTENSION ext = CertFindExtension(altNameOID,
1173 cert->pCertInfo->cExtension, cert->pCertInfo->rgExtension);
1175 if (ext)
1177 DWORD bytes = 0;
1179 if (CryptDecodeObjectEx(cert->dwCertEncodingType, X509_ALTERNATE_NAME,
1180 ext->Value.pbData, ext->Value.cbData, CRYPT_DECODE_ALLOC_FLAG, NULL,
1181 info, &bytes))
1183 DWORD i;
1185 for (i = 0; !entry && i < (*info)->cAltEntry; i++)
1186 if ((*info)->rgAltEntry[i].dwAltNameChoice == entryType)
1187 entry = &(*info)->rgAltEntry[i];
1190 else
1191 *info = NULL;
1192 return entry;
1195 static DWORD cert_get_name_from_rdn_attr(DWORD encodingType,
1196 const CERT_NAME_BLOB *name, LPCSTR oid, LPWSTR pszNameString, DWORD cchNameString)
1198 CERT_NAME_INFO *nameInfo;
1199 DWORD bytes = 0, ret = 0;
1201 if (CryptDecodeObjectEx(encodingType, X509_NAME, name->pbData,
1202 name->cbData, CRYPT_DECODE_ALLOC_FLAG, NULL, &nameInfo, &bytes))
1204 PCERT_RDN_ATTR nameAttr;
1206 if (!oid)
1207 oid = szOID_RSA_emailAddr;
1208 nameAttr = CertFindRDNAttr(oid, nameInfo);
1209 if (nameAttr)
1210 ret = CertRDNValueToStrW(nameAttr->dwValueType, &nameAttr->Value,
1211 pszNameString, cchNameString);
1212 LocalFree(nameInfo);
1214 return ret;
1217 DWORD WINAPI CertGetNameStringW(PCCERT_CONTEXT pCertContext, DWORD dwType,
1218 DWORD dwFlags, void *pvTypePara, LPWSTR pszNameString, DWORD cchNameString)
1220 DWORD ret = 0;
1221 PCERT_NAME_BLOB name;
1222 LPCSTR altNameOID;
1224 TRACE("(%p, %d, %08x, %p, %p, %d)\n", pCertContext, dwType,
1225 dwFlags, pvTypePara, pszNameString, cchNameString);
1227 if (!pCertContext)
1228 goto done;
1230 if (dwFlags & CERT_NAME_ISSUER_FLAG)
1232 name = &pCertContext->pCertInfo->Issuer;
1233 altNameOID = szOID_ISSUER_ALT_NAME;
1235 else
1237 name = &pCertContext->pCertInfo->Subject;
1238 altNameOID = szOID_SUBJECT_ALT_NAME;
1241 switch (dwType)
1243 case CERT_NAME_EMAIL_TYPE:
1245 CERT_ALT_NAME_INFO *info;
1246 PCERT_ALT_NAME_ENTRY entry = cert_find_alt_name_entry(pCertContext,
1247 altNameOID, CERT_ALT_NAME_RFC822_NAME, &info);
1249 if (entry)
1251 if (!pszNameString)
1252 ret = lstrlenW(entry->u.pwszRfc822Name) + 1;
1253 else if (cchNameString)
1255 ret = min(lstrlenW(entry->u.pwszRfc822Name), cchNameString - 1);
1256 memcpy(pszNameString, entry->u.pwszRfc822Name,
1257 ret * sizeof(WCHAR));
1258 pszNameString[ret++] = 0;
1261 if (info)
1262 LocalFree(info);
1263 if (!ret)
1264 ret = cert_get_name_from_rdn_attr(pCertContext->dwCertEncodingType,
1265 name, szOID_RSA_emailAddr, pszNameString, cchNameString);
1266 break;
1268 case CERT_NAME_RDN_TYPE:
1270 DWORD type = pvTypePara ? *(DWORD *)pvTypePara : 0;
1272 if (name->cbData)
1273 ret = CertNameToStrW(pCertContext->dwCertEncodingType, name,
1274 type, pszNameString, cchNameString);
1275 else
1277 CERT_ALT_NAME_INFO *info;
1278 PCERT_ALT_NAME_ENTRY entry = cert_find_alt_name_entry(pCertContext,
1279 altNameOID, CERT_ALT_NAME_DIRECTORY_NAME, &info);
1281 if (entry)
1282 ret = CertNameToStrW(pCertContext->dwCertEncodingType,
1283 &entry->u.DirectoryName, type, pszNameString, cchNameString);
1284 if (info)
1285 LocalFree(info);
1287 break;
1289 case CERT_NAME_ATTR_TYPE:
1290 ret = cert_get_name_from_rdn_attr(pCertContext->dwCertEncodingType,
1291 name, pvTypePara, pszNameString, cchNameString);
1292 if (!ret)
1294 CERT_ALT_NAME_INFO *altInfo;
1295 PCERT_ALT_NAME_ENTRY entry = cert_find_alt_name_entry(pCertContext,
1296 altNameOID, CERT_ALT_NAME_DIRECTORY_NAME, &altInfo);
1298 if (entry)
1299 ret = cert_name_to_str_with_indent(X509_ASN_ENCODING, 0,
1300 &entry->u.DirectoryName, 0, pszNameString, cchNameString);
1301 if (altInfo)
1302 LocalFree(altInfo);
1304 break;
1305 case CERT_NAME_SIMPLE_DISPLAY_TYPE:
1307 static const LPCSTR simpleAttributeOIDs[] = { szOID_COMMON_NAME,
1308 szOID_ORGANIZATIONAL_UNIT_NAME, szOID_ORGANIZATION_NAME,
1309 szOID_RSA_emailAddr };
1310 CERT_NAME_INFO *nameInfo = NULL;
1311 DWORD bytes = 0, i;
1313 if (CryptDecodeObjectEx(pCertContext->dwCertEncodingType, X509_NAME,
1314 name->pbData, name->cbData, CRYPT_DECODE_ALLOC_FLAG, NULL, &nameInfo,
1315 &bytes))
1317 PCERT_RDN_ATTR nameAttr = NULL;
1319 for (i = 0; !nameAttr && i < ARRAY_SIZE(simpleAttributeOIDs); i++)
1320 nameAttr = CertFindRDNAttr(simpleAttributeOIDs[i], nameInfo);
1321 if (nameAttr)
1322 ret = CertRDNValueToStrW(nameAttr->dwValueType,
1323 &nameAttr->Value, pszNameString, cchNameString);
1324 LocalFree(nameInfo);
1326 if (!ret)
1328 CERT_ALT_NAME_INFO *altInfo;
1329 PCERT_ALT_NAME_ENTRY entry = cert_find_alt_name_entry(pCertContext,
1330 altNameOID, CERT_ALT_NAME_RFC822_NAME, &altInfo);
1332 if (altInfo)
1334 if (!entry && altInfo->cAltEntry)
1335 entry = &altInfo->rgAltEntry[0];
1336 if (entry)
1338 if (!pszNameString)
1339 ret = lstrlenW(entry->u.pwszRfc822Name) + 1;
1340 else if (cchNameString)
1342 ret = min(lstrlenW(entry->u.pwszRfc822Name),
1343 cchNameString - 1);
1344 memcpy(pszNameString, entry->u.pwszRfc822Name,
1345 ret * sizeof(WCHAR));
1346 pszNameString[ret++] = 0;
1349 LocalFree(altInfo);
1352 break;
1354 case CERT_NAME_FRIENDLY_DISPLAY_TYPE:
1356 DWORD cch = cchNameString;
1358 if (CertGetCertificateContextProperty(pCertContext,
1359 CERT_FRIENDLY_NAME_PROP_ID, pszNameString, &cch))
1360 ret = cch;
1361 else
1362 ret = CertGetNameStringW(pCertContext,
1363 CERT_NAME_SIMPLE_DISPLAY_TYPE, dwFlags, pvTypePara, pszNameString,
1364 cchNameString);
1365 break;
1367 case CERT_NAME_DNS_TYPE:
1369 CERT_ALT_NAME_INFO *info;
1370 PCERT_ALT_NAME_ENTRY entry = cert_find_alt_name_entry(pCertContext,
1371 altNameOID, CERT_ALT_NAME_DNS_NAME, &info);
1373 if (entry)
1375 if (!pszNameString)
1376 ret = lstrlenW(entry->u.pwszDNSName) + 1;
1377 else if (cchNameString)
1379 ret = min(lstrlenW(entry->u.pwszDNSName), cchNameString - 1);
1380 memcpy(pszNameString, entry->u.pwszDNSName, ret * sizeof(WCHAR));
1381 pszNameString[ret++] = 0;
1384 if (info)
1385 LocalFree(info);
1386 if (!ret)
1387 ret = cert_get_name_from_rdn_attr(pCertContext->dwCertEncodingType,
1388 name, szOID_COMMON_NAME, pszNameString, cchNameString);
1389 break;
1391 case CERT_NAME_URL_TYPE:
1393 CERT_ALT_NAME_INFO *info;
1394 PCERT_ALT_NAME_ENTRY entry = cert_find_alt_name_entry(pCertContext,
1395 altNameOID, CERT_ALT_NAME_URL, &info);
1397 if (entry)
1399 if (!pszNameString)
1400 ret = lstrlenW(entry->u.pwszURL) + 1;
1401 else if (cchNameString)
1403 ret = min(lstrlenW(entry->u.pwszURL), cchNameString - 1);
1404 memcpy(pszNameString, entry->u.pwszURL, ret * sizeof(WCHAR));
1405 pszNameString[ret++] = 0;
1408 if (info)
1409 LocalFree(info);
1410 break;
1412 default:
1413 FIXME("unimplemented for type %d\n", dwType);
1414 ret = 0;
1416 done:
1417 if (!ret)
1419 if (!pszNameString)
1420 ret = 1;
1421 else if (cchNameString)
1423 pszNameString[0] = 0;
1424 ret = 1;
1427 return ret;