str.c 40 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Copyright 2006 Juan Lang for CodeWeavers
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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
 */
#include <stdarg.h>
19 20 21

#define NONAMELESSUNION

22 23
#include "windef.h"
#include "winbase.h"
24
#include "winnls.h"
25
#include "winuser.h"
26 27
#include "wincrypt.h"
#include "wine/debug.h"
28
#include "wine/unicode.h"
29 30 31

WINE_DEFAULT_DEBUG_CHANNEL(crypt);

32 33 34 35 36 37 38 39 40 41 42 43
static inline BOOL is_quotable_char(char c)
{
    switch(c)
    {
    case '+':
    case ',':
    case '"':
    case '=':
    case '<':
    case '>':
    case ';':
    case '#':
44
    case '\n':
45 46 47 48 49 50
        return TRUE;
    default:
        return FALSE;
    }
}

51 52 53
DWORD WINAPI CertRDNValueToStrA(DWORD dwValueType, PCERT_RDN_VALUE_BLOB pValue,
 LPSTR psz, DWORD csz)
{
54 55
    DWORD ret = 0, len, i;
    BOOL needsQuotes = FALSE;
56

57
    TRACE("(%d, %p, %p, %d)\n", dwValueType, pValue, psz, csz);
58 59 60 61 62

    switch (dwValueType)
    {
    case CERT_RDN_ANY_TYPE:
        break;
63
    case CERT_RDN_NUMERIC_STRING:
64
    case CERT_RDN_PRINTABLE_STRING:
65 66
    case CERT_RDN_TELETEX_STRING:
    case CERT_RDN_VIDEOTEX_STRING:
67
    case CERT_RDN_IA5_STRING:
68 69 70
    case CERT_RDN_GRAPHIC_STRING:
    case CERT_RDN_VISIBLE_STRING:
    case CERT_RDN_GENERAL_STRING:
71 72 73 74 75 76
        len = pValue->cbData;
        if (pValue->cbData && isspace(pValue->pbData[0]))
            needsQuotes = TRUE;
        if (pValue->cbData && isspace(pValue->pbData[pValue->cbData - 1]))
            needsQuotes = TRUE;
        for (i = 0; i < pValue->cbData; i++)
77
        {
78 79 80 81
            if (is_quotable_char(pValue->pbData[i]))
                needsQuotes = TRUE;
            if (pValue->pbData[i] == '"')
                len += 1;
82
        }
83 84
        if (needsQuotes)
            len += 2;
85
        if (!psz || !csz)
86
            ret = len;
87 88
        else
        {
89
            char *ptr = psz;
90

91 92 93
            if (needsQuotes)
                *ptr++ = '"';
            for (i = 0; i < pValue->cbData && ptr - psz < csz; ptr++, i++)
94
            {
95 96 97
                *ptr = pValue->pbData[i];
                if (pValue->pbData[i] == '"' && ptr - psz < csz - 1)
                    *(++ptr) = '"';
98
            }
99 100 101
            if (needsQuotes && ptr - psz < csz)
                *ptr++ = '"';
            ret = ptr - psz;
102 103
        }
        break;
104
    case CERT_RDN_BMP_STRING:
105
    case CERT_RDN_UTF8_STRING:
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
        len = WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)pValue->pbData,
         pValue->cbData / sizeof(WCHAR), NULL, 0, NULL, NULL);
        if (pValue->cbData && isspaceW(((LPCWSTR)pValue->pbData)[0]))
            needsQuotes = TRUE;
        if (pValue->cbData &&
         isspaceW(((LPCWSTR)pValue->pbData)[pValue->cbData / sizeof(WCHAR)-1]))
            needsQuotes = TRUE;
        for (i = 0; i < pValue->cbData / sizeof(WCHAR); i++)
        {
            if (is_quotable_char(((LPCWSTR)pValue->pbData)[i]))
                needsQuotes = TRUE;
            if (((LPCWSTR)pValue->pbData)[i] == '"')
                len += 1;
        }
        if (needsQuotes)
            len += 2;
        if (!psz || !csz)
            ret = len;
        else
        {
            char *dst = psz;

            if (needsQuotes)
                *dst++ = '"';
            for (i = 0; i < pValue->cbData / sizeof(WCHAR) &&
             dst - psz < csz; dst++, i++)
            {
                LPCWSTR src = (LPCWSTR)pValue->pbData + i;

                WideCharToMultiByte(CP_ACP, 0, src, 1, dst,
                 csz - (dst - psz) - 1, NULL, NULL);
                if (*src == '"' && dst - psz < csz - 1)
                    *(++dst) = '"';
            }
            if (needsQuotes && dst - psz < csz)
                *dst++ = '"';
            ret = dst - psz;
        }
        break;
145
    default:
146
        FIXME("string type %d unimplemented\n", dwValueType);
147 148 149 150 151 152 153 154 155
    }
    if (psz && csz)
    {
        *(psz + ret) = '\0';
        csz--;
        ret++;
    }
    else
        ret++;
156
    TRACE("returning %d (%s)\n", ret, debugstr_a(psz));
157 158 159 160 161 162
    return ret;
}

DWORD WINAPI CertRDNValueToStrW(DWORD dwValueType, PCERT_RDN_VALUE_BLOB pValue,
 LPWSTR psz, DWORD csz)
{
163
    DWORD ret = 0, len, i, strLen;
164
    BOOL needsQuotes = FALSE;
165

166
    TRACE("(%d, %p, %p, %d)\n", dwValueType, pValue, psz, csz);
167 168 169 170 171

    switch (dwValueType)
    {
    case CERT_RDN_ANY_TYPE:
        break;
172
    case CERT_RDN_NUMERIC_STRING:
173
    case CERT_RDN_PRINTABLE_STRING:
174 175
    case CERT_RDN_TELETEX_STRING:
    case CERT_RDN_VIDEOTEX_STRING:
176
    case CERT_RDN_IA5_STRING:
177 178 179
    case CERT_RDN_GRAPHIC_STRING:
    case CERT_RDN_VISIBLE_STRING:
    case CERT_RDN_GENERAL_STRING:
180 181 182 183 184 185
        len = pValue->cbData;
        if (pValue->cbData && isspace(pValue->pbData[0]))
            needsQuotes = TRUE;
        if (pValue->cbData && isspace(pValue->pbData[pValue->cbData - 1]))
            needsQuotes = TRUE;
        for (i = 0; i < pValue->cbData; i++)
186
        {
187 188 189 190
            if (is_quotable_char(pValue->pbData[i]))
                needsQuotes = TRUE;
            if (pValue->pbData[i] == '"')
                len += 1;
191
        }
192 193
        if (needsQuotes)
            len += 2;
194
        if (!psz || !csz)
195
            ret = len;
196 197
        else
        {
198
            WCHAR *ptr = psz;
199

200 201 202
            if (needsQuotes)
                *ptr++ = '"';
            for (i = 0; i < pValue->cbData && ptr - psz < csz; ptr++, i++)
203
            {
204 205
                *ptr = pValue->pbData[i];
                if (pValue->pbData[i] == '"' && ptr - psz < csz - 1)
206 207 208 209 210 211 212 213
                    *(++ptr) = '"';
            }
            if (needsQuotes && ptr - psz < csz)
                *ptr++ = '"';
            ret = ptr - psz;
        }
        break;
    case CERT_RDN_BMP_STRING:
214
    case CERT_RDN_UTF8_STRING:
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
        strLen = len = pValue->cbData / sizeof(WCHAR);
        if (pValue->cbData && isspace(pValue->pbData[0]))
            needsQuotes = TRUE;
        if (pValue->cbData && isspace(pValue->pbData[strLen - 1]))
            needsQuotes = TRUE;
        for (i = 0; i < strLen; i++)
        {
            if (is_quotable_char(((LPCWSTR)pValue->pbData)[i]))
                needsQuotes = TRUE;
            if (((LPCWSTR)pValue->pbData)[i] == '"')
                len += 1;
        }
        if (needsQuotes)
            len += 2;
        if (!psz || !csz)
            ret = len;
        else
        {
            WCHAR *ptr = psz;

            if (needsQuotes)
                *ptr++ = '"';
            for (i = 0; i < strLen && ptr - psz < csz; ptr++, i++)
            {
                *ptr = ((LPCWSTR)pValue->pbData)[i];
                if (((LPCWSTR)pValue->pbData)[i] == '"' && ptr - psz < csz - 1)
241
                    *(++ptr) = '"';
242
            }
243 244 245
            if (needsQuotes && ptr - psz < csz)
                *ptr++ = '"';
            ret = ptr - psz;
246 247 248
        }
        break;
    default:
249
        FIXME("string type %d unimplemented\n", dwValueType);
250 251 252 253 254 255 256 257 258
    }
    if (psz && csz)
    {
        *(psz + ret) = '\0';
        csz--;
        ret++;
    }
    else
        ret++;
259
    TRACE("returning %d (%s)\n", ret, debugstr_w(psz));
260
    return ret;
261 262
}

263 264 265 266 267 268 269
/* Adds the prefix prefix to the string pointed to by psz, followed by the
 * character '='.  Copies no more than csz characters.  Returns the number of
 * characters copied.  If psz is NULL, returns the number of characters that
 * would be copied.
 */
static DWORD CRYPT_AddPrefixA(LPCSTR prefix, LPSTR psz, DWORD csz)
{
270
    DWORD chars;
271

272
    TRACE("(%s, %p, %d)\n", debugstr_a(prefix), psz, csz);
273

274
    if (psz)
275
    {
276
        chars = min(strlen(prefix), csz);
277 278
        memcpy(psz, prefix, chars);
        *(psz + chars) = '=';
279 280
        chars++;
    }
281 282
    else
        chars = lstrlenA(prefix) + 1;
283 284 285
    return chars;
}

286 287 288 289
DWORD WINAPI CertNameToStrA(DWORD dwCertEncodingType, PCERT_NAME_BLOB pName,
 DWORD dwStrType, LPSTR psz, DWORD csz)
{
    static const DWORD unsupportedFlags = CERT_NAME_STR_NO_QUOTING_FLAG |
290
     CERT_NAME_STR_ENABLE_T61_UNICODE_FLAG;
291 292 293 294 295 296 297 298 299
    static const char commaSep[] = ", ";
    static const char semiSep[] = "; ";
    static const char crlfSep[] = "\r\n";
    static const char plusSep[] = " + ";
    static const char spaceSep[] = " ";
    DWORD ret = 0, bytes = 0;
    BOOL bRet;
    CERT_NAME_INFO *info;

300
    TRACE("(%d, %p, %08x, %p, %d)\n", dwCertEncodingType, pName, dwStrType,
301
     psz, csz);
302
    if (dwStrType & unsupportedFlags)
303
        FIXME("unsupported flags: %08x\n", dwStrType & unsupportedFlags);
304 305 306 307 308 309 310

    bRet = CryptDecodeObjectEx(dwCertEncodingType, X509_NAME, pName->pbData,
     pName->cbData, CRYPT_DECODE_ALLOC_FLAG, NULL, &info, &bytes);
    if (bRet)
    {
        DWORD i, j, sepLen, rdnSepLen;
        LPCSTR sep, rdnSep;
311 312 313 314
        BOOL reverse = dwStrType & CERT_NAME_STR_REVERSE_FLAG;
        const CERT_RDN *rdn = info->rgRDN;

        if(reverse && info->cRDN > 1) rdn += (info->cRDN - 1);
315 316 317 318 319 320 321 322 323 324 325 326 327

        if (dwStrType & CERT_NAME_STR_SEMICOLON_FLAG)
            sep = semiSep;
        else if (dwStrType & CERT_NAME_STR_CRLF_FLAG)
            sep = crlfSep;
        else
            sep = commaSep;
        sepLen = strlen(sep);
        if (dwStrType & CERT_NAME_STR_NO_PLUS_FLAG)
            rdnSep = spaceSep;
        else
            rdnSep = plusSep;
        rdnSepLen = strlen(rdnSep);
328
        for (i = 0; (!psz || ret < csz) && i < info->cRDN; i++)
329
        {
330
            for (j = 0; (!psz || ret < csz) && j < rdn->cRDNAttr; j++)
331 332
            {
                DWORD chars;
333 334
                char prefixBuf[10]; /* big enough for GivenName */
                LPCSTR prefix = NULL;
335 336

                if ((dwStrType & 0x000000ff) == CERT_OID_NAME_STR)
337
                    prefix = rdn->rgRDNAttr[j].pszObjId;
338 339 340 341
                else if ((dwStrType & 0x000000ff) == CERT_X500_NAME_STR)
                {
                    PCCRYPT_OID_INFO oidInfo = CryptFindOIDInfo(
                     CRYPT_OID_INFO_OID_KEY,
342
                     rdn->rgRDNAttr[j].pszObjId,
343 344 345 346 347 348 349 350 351
                     CRYPT_RDN_ATTR_OID_GROUP_ID);

                    if (oidInfo)
                    {
                        WideCharToMultiByte(CP_ACP, 0, oidInfo->pwszName, -1,
                         prefixBuf, sizeof(prefixBuf), NULL, NULL);
                        prefix = prefixBuf;
                    }
                    else
352
                        prefix = rdn->rgRDNAttr[j].pszObjId;
353 354
                }
                if (prefix)
355 356
                {
                    /* - 1 is needed to account for the NULL terminator. */
357 358
                    chars = CRYPT_AddPrefixA(prefix,
                     psz ? psz + ret : NULL, psz ? csz - ret - 1 : 0);
359 360 361
                    ret += chars;
                }
                chars = CertRDNValueToStrA(
362 363
                 rdn->rgRDNAttr[j].dwValueType,
                 &rdn->rgRDNAttr[j].Value, psz ? psz + ret : NULL,
364
                 psz ? csz - ret : 0);
365 366
                if (chars)
                    ret += chars - 1;
367
                if (j < rdn->cRDNAttr - 1)
368 369 370 371 372 373 374 375 376 377 378 379
                {
                    if (psz && ret < csz - rdnSepLen - 1)
                        memcpy(psz + ret, rdnSep, rdnSepLen);
                    ret += rdnSepLen;
                }
            }
            if (i < info->cRDN - 1)
            {
                if (psz && ret < csz - sepLen - 1)
                    memcpy(psz + ret, sep, sepLen);
                ret += sepLen;
            }
380 381
            if(reverse) rdn--;
            else rdn++;
382 383 384 385 386 387 388 389 390 391
        }
        LocalFree(info);
    }
    if (psz && csz)
    {
        *(psz + ret) = '\0';
        ret++;
    }
    else
        ret++;
392
    TRACE("Returning %s\n", debugstr_a(psz));
393 394 395
    return ret;
}

396 397 398 399 400 401 402 403
/* Adds the prefix prefix to the wide-character string pointed to by psz,
 * followed by the character '='.  Copies no more than csz characters.  Returns
 * the number of characters copied.  If psz is NULL, returns the number of
 * characters that would be copied.
 * Assumes the characters in prefix are ASCII (not multibyte characters.)
 */
static DWORD CRYPT_AddPrefixAToW(LPCSTR prefix, LPWSTR psz, DWORD csz)
{
404
    DWORD chars;
405

406
    TRACE("(%s, %p, %d)\n", debugstr_a(prefix), psz, csz);
407

408
    if (psz)
409 410 411
    {
        DWORD i;

412
        chars = min(strlen(prefix), csz);
413 414
        for (i = 0; i < chars; i++)
            *(psz + i) = prefix[i];
415
        *(psz + chars) = '=';
416 417
        chars++;
    }
418 419
    else
        chars = lstrlenA(prefix) + 1;
420 421 422 423 424 425 426 427 428 429
    return chars;
}

/* Adds the prefix prefix to the string pointed to by psz, followed by the
 * character '='.  Copies no more than csz characters.  Returns the number of
 * characters copied.  If psz is NULL, returns the number of characters that
 * would be copied.
 */
static DWORD CRYPT_AddPrefixW(LPCWSTR prefix, LPWSTR psz, DWORD csz)
{
430
    DWORD chars;
431

432
    TRACE("(%s, %p, %d)\n", debugstr_w(prefix), psz, csz);
433

434
    if (psz)
435
    {
436
        chars = min(strlenW(prefix), csz);
437 438
        memcpy(psz, prefix, chars * sizeof(WCHAR));
        *(psz + chars) = '=';
439 440
        chars++;
    }
441 442
    else
        chars = lstrlenW(prefix) + 1;
443 444 445
    return chars;
}

446 447 448
static const WCHAR indent[] = { ' ',' ',' ',' ',' ',0 };

DWORD cert_name_to_str_with_indent(DWORD dwCertEncodingType, DWORD indentLevel,
449
 const CERT_NAME_BLOB *pName, DWORD dwStrType, LPWSTR psz, DWORD csz)
450
{
451
    static const DWORD unsupportedFlags = CERT_NAME_STR_NO_QUOTING_FLAG |
452
     CERT_NAME_STR_ENABLE_T61_UNICODE_FLAG;
453 454 455 456 457 458 459 460 461 462
    static const WCHAR commaSep[] = { ',',' ',0 };
    static const WCHAR semiSep[] = { ';',' ',0 };
    static const WCHAR crlfSep[] = { '\r','\n',0 };
    static const WCHAR plusSep[] = { ' ','+',' ',0 };
    static const WCHAR spaceSep[] = { ' ',0 };
    DWORD ret = 0, bytes = 0;
    BOOL bRet;
    CERT_NAME_INFO *info;

    if (dwStrType & unsupportedFlags)
463
        FIXME("unsupported flags: %08x\n", dwStrType & unsupportedFlags);
464 465 466 467 468 469 470

    bRet = CryptDecodeObjectEx(dwCertEncodingType, X509_NAME, pName->pbData,
     pName->cbData, CRYPT_DECODE_ALLOC_FLAG, NULL, &info, &bytes);
    if (bRet)
    {
        DWORD i, j, sepLen, rdnSepLen;
        LPCWSTR sep, rdnSep;
471 472 473 474
        BOOL reverse = dwStrType & CERT_NAME_STR_REVERSE_FLAG;
        const CERT_RDN *rdn = info->rgRDN;

        if(reverse && info->cRDN > 1) rdn += (info->cRDN - 1);
475 476 477 478 479 480 481 482 483 484 485 486 487

        if (dwStrType & CERT_NAME_STR_SEMICOLON_FLAG)
            sep = semiSep;
        else if (dwStrType & CERT_NAME_STR_CRLF_FLAG)
            sep = crlfSep;
        else
            sep = commaSep;
        sepLen = lstrlenW(sep);
        if (dwStrType & CERT_NAME_STR_NO_PLUS_FLAG)
            rdnSep = spaceSep;
        else
            rdnSep = plusSep;
        rdnSepLen = lstrlenW(rdnSep);
488
        for (i = 0; (!psz || ret < csz) && i < info->cRDN; i++)
489
        {
490
            for (j = 0; (!psz || ret < csz) && j < rdn->cRDNAttr; j++)
491 492
            {
                DWORD chars;
493 494
                LPCSTR prefixA = NULL;
                LPCWSTR prefixW = NULL;
495 496

                if ((dwStrType & 0x000000ff) == CERT_OID_NAME_STR)
497
                    prefixA = rdn->rgRDNAttr[j].pszObjId;
498 499 500 501
                else if ((dwStrType & 0x000000ff) == CERT_X500_NAME_STR)
                {
                    PCCRYPT_OID_INFO oidInfo = CryptFindOIDInfo(
                     CRYPT_OID_INFO_OID_KEY,
502
                     rdn->rgRDNAttr[j].pszObjId,
503 504 505 506 507
                     CRYPT_RDN_ATTR_OID_GROUP_ID);

                    if (oidInfo)
                        prefixW = oidInfo->pwszName;
                    else
508
                        prefixA = rdn->rgRDNAttr[j].pszObjId;
509
                }
510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
                if (dwStrType & CERT_NAME_STR_CRLF_FLAG)
                {
                    DWORD k;

                    for (k = 0; k < indentLevel; k++)
                    {
                        if (psz)
                        {
                            chars = min(strlenW(indent), csz - ret - 1);
                            memcpy(psz + ret, indent, chars * sizeof(WCHAR));
                        }
                        else
                            chars = strlenW(indent);
                        ret += chars;
                    }
                }
526 527 528
                if (prefixW)
                {
                    /* - 1 is needed to account for the NULL terminator. */
529 530
                    chars = CRYPT_AddPrefixW(prefixW,
                     psz ? psz + ret : NULL, psz ? csz - ret - 1 : 0);
531 532 533
                    ret += chars;
                }
                else if (prefixA)
534 535
                {
                    /* - 1 is needed to account for the NULL terminator. */
536 537
                    chars = CRYPT_AddPrefixAToW(prefixA,
                     psz ? psz + ret : NULL, psz ? csz - ret - 1 : 0);
538 539 540
                    ret += chars;
                }
                chars = CertRDNValueToStrW(
541 542
                 rdn->rgRDNAttr[j].dwValueType,
                 &rdn->rgRDNAttr[j].Value, psz ? psz + ret : NULL,
543
                 psz ? csz - ret : 0);
544 545
                if (chars)
                    ret += chars - 1;
546
                if (j < rdn->cRDNAttr - 1)
547 548 549 550 551 552 553 554 555 556 557 558
                {
                    if (psz && ret < csz - rdnSepLen - 1)
                        memcpy(psz + ret, rdnSep, rdnSepLen * sizeof(WCHAR));
                    ret += rdnSepLen;
                }
            }
            if (i < info->cRDN - 1)
            {
                if (psz && ret < csz - sepLen - 1)
                    memcpy(psz + ret, sep, sepLen * sizeof(WCHAR));
                ret += sepLen;
            }
559 560
            if(reverse) rdn--;
            else rdn++;
561 562 563 564 565 566 567 568 569 570
        }
        LocalFree(info);
    }
    if (psz && csz)
    {
        *(psz + ret) = '\0';
        ret++;
    }
    else
        ret++;
571 572 573 574 575 576 577 578 579 580 581 582 583
    return ret;
}

DWORD WINAPI CertNameToStrW(DWORD dwCertEncodingType, PCERT_NAME_BLOB pName,
 DWORD dwStrType, LPWSTR psz, DWORD csz)
{
    BOOL ret;

    TRACE("(%d, %p, %08x, %p, %d)\n", dwCertEncodingType, pName, dwStrType,
     psz, csz);

    ret = cert_name_to_str_with_indent(dwCertEncodingType, 0, pName, dwStrType,
     psz, csz);
584
    TRACE("Returning %s\n", debugstr_w(psz));
585
    return ret;
586
}
587

588 589 590 591 592 593 594
BOOL WINAPI CertStrToNameA(DWORD dwCertEncodingType, LPCSTR pszX500,
 DWORD dwStrType, void *pvReserved, BYTE *pbEncoded, DWORD *pcbEncoded,
 LPCSTR *ppszError)
{
    BOOL ret;
    int len;

595
    TRACE("(%08x, %s, %08x, %p, %p, %p, %p)\n", dwCertEncodingType,
596 597 598 599
     debugstr_a(pszX500), dwStrType, pvReserved, pbEncoded, pcbEncoded,
     ppszError);

    len = MultiByteToWideChar(CP_ACP, 0, pszX500, -1, NULL, 0);
600
    if (len)
601
    {
602 603 604
        LPWSTR x500, errorStr;

        if ((x500 = CryptMemAlloc(len * sizeof(WCHAR))))
605
        {
606 607 608 609 610 611
            MultiByteToWideChar(CP_ACP, 0, pszX500, -1, x500, len);
            ret = CertStrToNameW(dwCertEncodingType, x500, dwStrType,
             pvReserved, pbEncoded, pcbEncoded,
             ppszError ? (LPCWSTR *)&errorStr : NULL);
            if (ppszError)
            {
612 613
                if (!ret)
                {
614
                    LONG i;
615

616 617 618 619 620 621
                    *ppszError = pszX500;
                    for (i = 0; i < errorStr - x500; i++)
                        *ppszError = CharNextA(*ppszError);
                }
                else
                    *ppszError = NULL;
622 623 624 625 626 627 628
            }
            CryptMemFree(x500);
        }
        else
        {
            SetLastError(ERROR_OUTOFMEMORY);
            ret = FALSE;
629 630 631
        }
    }
    else
632 633 634 635
    {
        SetLastError(CRYPT_E_INVALID_X500_STRING);
        if (ppszError)
            *ppszError = pszX500;
636
        ret = FALSE;
637
    }
638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666
    return ret;
}

struct KeynameKeeper
{
    WCHAR  buf[10]; /* big enough for L"GivenName" */
    LPWSTR keyName; /* usually = buf, but may be allocated */
    DWORD  keyLen;
};

static void CRYPT_InitializeKeynameKeeper(struct KeynameKeeper *keeper)
{
    keeper->keyName = keeper->buf;
    keeper->keyLen = sizeof(keeper->buf) / sizeof(keeper->buf[0]);
}

static void CRYPT_FreeKeynameKeeper(struct KeynameKeeper *keeper)
{
    if (keeper->keyName != keeper->buf)
        CryptMemFree(keeper->keyName);
}

struct X500TokenW
{
    LPCWSTR start;
    LPCWSTR end;
};

static void CRYPT_KeynameKeeperFromTokenW(struct KeynameKeeper *keeper,
667
 const struct X500TokenW *key)
668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685
{
    DWORD len = key->end - key->start;

    if (len > keeper->keyLen)
    {
        if (keeper->keyName == keeper->buf)
            keeper->keyName = CryptMemAlloc(len * sizeof(WCHAR));
        else
            keeper->keyName = CryptMemRealloc(keeper->keyName,
             len * sizeof(WCHAR));
        keeper->keyLen = len;
    }
    memcpy(keeper->keyName, key->start, (key->end - key->start) *
     sizeof(WCHAR));
    keeper->keyName[len] = '\0';
    TRACE("Keyname is %s\n", debugstr_w(keeper->keyName));
}

686
static BOOL CRYPT_GetNextKeyW(LPCWSTR str, struct X500TokenW *token,
687 688
 LPCWSTR *ppszError)
{
689
    BOOL ret = TRUE;
690 691 692 693 694 695 696 697 698 699 700 701 702 703 704

    while (*str && isspaceW(*str))
        str++;
    if (*str)
    {
        token->start = str;
        while (*str && *str != '=' && !isspaceW(*str))
            str++;
        if (*str && (*str == '=' || isspaceW(*str)))
            token->end = str;
        else
        {
            TRACE("missing equals char at %s\n", debugstr_w(token->start));
            if (ppszError)
                *ppszError = token->start;
705 706
            SetLastError(CRYPT_E_INVALID_X500_STRING);
            ret = FALSE;
707 708 709 710 711 712 713 714
        }
    }
    else
        token->start = NULL;
    return ret;
}

/* Assumes separators are characters in the 0-255 range */
715
static BOOL CRYPT_GetNextValueW(LPCWSTR str, DWORD dwFlags, LPCWSTR separators,
716 717
 struct X500TokenW *token, LPCWSTR *ppszError)
{
718
    BOOL ret = TRUE;
719 720 721 722 723 724 725 726 727 728 729 730 731

    TRACE("(%s, %s, %p, %p)\n", debugstr_w(str), debugstr_w(separators), token,
     ppszError);

    while (*str && isspaceW(*str))
        str++;
    if (*str)
    {
        token->start = str;
        if (!(dwFlags & CERT_NAME_STR_NO_QUOTING_FLAG) && *str == '"')
        {
            token->end = NULL;
            str++;
732
            while (!token->end && ret)
733 734 735 736 737 738 739 740 741 742 743 744 745 746 747
            {
                while (*str && *str != '"')
                    str++;
                if (*str == '"')
                {
                    if (*(str + 1) != '"')
                        token->end = str + 1;
                    else
                        str += 2;
                }
                else
                {
                    TRACE("unterminated quote at %s\n", debugstr_w(str));
                    if (ppszError)
                        *ppszError = str;
748 749
                    SetLastError(CRYPT_E_INVALID_X500_STRING);
                    ret = FALSE;
750 751 752 753 754 755 756 757 758
                }
            }
        }
        else
        {
            WCHAR map[256] = { 0 };

            while (*separators)
                map[*separators++] = 1;
759
            while (*str && (*str >= 0xff || !map[*str]))
760 761 762 763 764 765 766 767 768
                str++;
            token->end = str;
        }
    }
    else
    {
        TRACE("missing value at %s\n", debugstr_w(str));
        if (ppszError)
            *ppszError = str;
769 770
        SetLastError(CRYPT_E_INVALID_X500_STRING);
        ret = FALSE;
771 772 773 774 775 776 777 778 779 780
    }
    return ret;
}

/* Encodes the string represented by value as the string type type into the
 * CERT_NAME_BLOB output.  If there is an error and ppszError is not NULL,
 * *ppszError is set to the first failing character.  If there is no error,
 * output's pbData must be freed with LocalFree.
 */
static BOOL CRYPT_EncodeValueWithType(DWORD dwCertEncodingType,
781
 const struct X500TokenW *value, PCERT_NAME_BLOB output, DWORD type,
782 783 784
 LPCWSTR *ppszError)
{
    CERT_NAME_VALUE nameValue = { type, { 0, NULL } };
785
    BOOL ret = TRUE;
786

787
    if (value->end > value->start)
788
    {
789 790 791 792 793 794 795 796 797 798 799
        nameValue.Value.pbData = CryptMemAlloc((value->end - value->start) *
         sizeof(WCHAR));
        if (!nameValue.Value.pbData)
        {
            SetLastError(ERROR_OUTOFMEMORY);
            ret = FALSE;
        }
    }
    if (ret)
    {
        if (value->end > value->start)
800
        {
801
            LONG i;
802 803 804 805 806 807 808 809 810
            LPWSTR ptr = (LPWSTR)nameValue.Value.pbData;

            for (i = 0; i < value->end - value->start; i++)
            {
                *ptr++ = value->start[i];
                if (value->start[i] == '"')
                    i++;
            }
            nameValue.Value.cbData = (LPBYTE)ptr - nameValue.Value.pbData;
811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832
        }
        ret = CryptEncodeObjectEx(dwCertEncodingType, X509_UNICODE_NAME_VALUE,
         &nameValue, CRYPT_ENCODE_ALLOC_FLAG, NULL, &output->pbData,
         &output->cbData);
        if (!ret && ppszError)
        {
            if (type == CERT_RDN_NUMERIC_STRING &&
             GetLastError() == CRYPT_E_INVALID_NUMERIC_STRING)
                *ppszError = value->start + output->cbData;
            else if (type == CERT_RDN_PRINTABLE_STRING &&
             GetLastError() == CRYPT_E_INVALID_PRINTABLE_STRING)
                *ppszError = value->start + output->cbData;
            else if (type == CERT_RDN_IA5_STRING &&
             GetLastError() == CRYPT_E_INVALID_IA5_STRING)
                *ppszError = value->start + output->cbData;
        }
        CryptMemFree(nameValue.Value.pbData);
    }
    return ret;
}

static BOOL CRYPT_EncodeValue(DWORD dwCertEncodingType,
833
 const struct X500TokenW *value, PCERT_NAME_BLOB output, const DWORD *types,
834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888
 LPCWSTR *ppszError)
{
    DWORD i;
    BOOL ret;

    ret = FALSE;
    for (i = 0; !ret && types[i]; i++)
        ret = CRYPT_EncodeValueWithType(dwCertEncodingType, value, output,
         types[i], ppszError);
    return ret;
}

static BOOL CRYPT_ValueToRDN(DWORD dwCertEncodingType, PCERT_NAME_INFO info,
 PCCRYPT_OID_INFO keyOID, struct X500TokenW *value, LPCWSTR *ppszError)
{
    BOOL ret = FALSE;

    TRACE("OID %s, value %s\n", debugstr_a(keyOID->pszOID),
     debugstr_wn(value->start, value->end - value->start));

    if (!info->rgRDN)
        info->rgRDN = CryptMemAlloc(sizeof(CERT_RDN));
    else
        info->rgRDN = CryptMemRealloc(info->rgRDN,
         (info->cRDN + 1) * sizeof(CERT_RDN));
    if (info->rgRDN)
    {
        /* FIXME: support multiple RDN attrs */
        info->rgRDN[info->cRDN].rgRDNAttr =
         CryptMemAlloc(sizeof(CERT_RDN_ATTR));
        if (info->rgRDN[info->cRDN].rgRDNAttr)
        {
            static const DWORD defaultTypes[] = { CERT_RDN_PRINTABLE_STRING,
             CERT_RDN_BMP_STRING, 0 };
            const DWORD *types;

            info->rgRDN[info->cRDN].cRDNAttr = 1;
            info->rgRDN[info->cRDN].rgRDNAttr[0].pszObjId =
             (LPSTR)keyOID->pszOID;
            info->rgRDN[info->cRDN].rgRDNAttr[0].dwValueType =
             CERT_RDN_ENCODED_BLOB;
            if (keyOID->ExtraInfo.cbData)
                types = (const DWORD *)keyOID->ExtraInfo.pbData;
            else
                types = defaultTypes;

            /* Remove surrounding quotes */
            if (value->start[0] == '"')
            {
                value->start++;
                value->end--;
            }
            ret = CRYPT_EncodeValue(dwCertEncodingType, value,
             &info->rgRDN[info->cRDN].rgRDNAttr[0].Value, types, ppszError);
        }
889 890
        else
            SetLastError(ERROR_OUTOFMEMORY);
891
        info->cRDN++;
892 893 894
    }
    else
        SetLastError(ERROR_OUTOFMEMORY);
895 896 897 898 899 900 901 902 903 904
    return ret;
}

BOOL WINAPI CertStrToNameW(DWORD dwCertEncodingType, LPCWSTR pszX500,
 DWORD dwStrType, void *pvReserved, BYTE *pbEncoded, DWORD *pcbEncoded,
 LPCWSTR *ppszError)
{
    CERT_NAME_INFO info = { 0, NULL };
    LPCWSTR str;
    struct KeynameKeeper keeper;
905
    DWORD i;
906 907
    BOOL ret = TRUE;

908
    TRACE("(%08x, %s, %08x, %p, %p, %p, %p)\n", dwCertEncodingType,
909 910 911 912 913
     debugstr_w(pszX500), dwStrType, pvReserved, pbEncoded, pcbEncoded,
     ppszError);

    CRYPT_InitializeKeynameKeeper(&keeper);
    str = pszX500;
914
    while (str && *str && ret)
915 916 917
    {
        struct X500TokenW token;

918 919
        ret = CRYPT_GetNextKeyW(str, &token, ppszError);
        if (ret && token.start)
920 921 922 923 924 925 926 927 928 929
        {
            PCCRYPT_OID_INFO keyOID;

            CRYPT_KeynameKeeperFromTokenW(&keeper, &token);
            keyOID = CryptFindOIDInfo(CRYPT_OID_INFO_NAME_KEY, keeper.keyName,
             CRYPT_RDN_ATTR_OID_GROUP_ID);
            if (!keyOID)
            {
                if (ppszError)
                    *ppszError = token.start;
930 931
                SetLastError(CRYPT_E_INVALID_X500_STRING);
                ret = FALSE;
932 933 934 935 936 937 938 939 940 941
            }
            else
            {
                str = token.end;
                while (isspace(*str))
                    str++;
                if (*str != '=')
                {
                    if (ppszError)
                        *ppszError = str;
942 943
                    SetLastError(CRYPT_E_INVALID_X500_STRING);
                    ret = FALSE;
944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961
                }
                else
                {
                    static const WCHAR commaSep[] = { ',',0 };
                    static const WCHAR semiSep[] = { ';',0 };
                    static const WCHAR crlfSep[] = { '\r','\n',0 };
                    static const WCHAR allSeps[] = { ',',';','\r','\n',0 };
                    LPCWSTR sep;

                    str++;
                    if (dwStrType & CERT_NAME_STR_COMMA_FLAG)
                        sep = commaSep;
                    else if (dwStrType & CERT_NAME_STR_SEMICOLON_FLAG)
                        sep = semiSep;
                    else if (dwStrType & CERT_NAME_STR_CRLF_FLAG)
                        sep = crlfSep;
                    else
                        sep = allSeps;
962
                    ret = CRYPT_GetNextValueW(str, dwStrType, sep, &token,
963
                     ppszError);
964
                    if (ret)
965 966 967 968 969 970 971 972 973 974
                    {
                        str = token.end;
                        ret = CRYPT_ValueToRDN(dwCertEncodingType, &info,
                         keyOID, &token, ppszError);
                    }
                }
            }
        }
    }
    CRYPT_FreeKeynameKeeper(&keeper);
975
    if (ret)
976
    {
977 978
        if (ppszError)
            *ppszError = NULL;
979 980 981
        ret = CryptEncodeObjectEx(dwCertEncodingType, X509_NAME, &info,
         0, NULL, pbEncoded, pcbEncoded);
    }
982 983 984 985 986 987 988 989 990
    for (i = 0; i < info.cRDN; i++)
    {
        DWORD j;

        for (j = 0; j < info.rgRDN[i].cRDNAttr; j++)
            LocalFree(info.rgRDN[i].rgRDNAttr[j].Value.pbData);
        CryptMemFree(info.rgRDN[i].rgRDNAttr);
    }
    CryptMemFree(info.rgRDN);
991 992 993
    return ret;
}

994 995 996 997 998
DWORD WINAPI CertGetNameStringA(PCCERT_CONTEXT pCertContext, DWORD dwType,
 DWORD dwFlags, void *pvTypePara, LPSTR pszNameString, DWORD cchNameString)
{
    DWORD ret;

999
    TRACE("(%p, %d, %08x, %p, %p, %d)\n", pCertContext, dwType, dwFlags,
1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036
     pvTypePara, pszNameString, cchNameString);

    if (pszNameString)
    {
        LPWSTR wideName;
        DWORD nameLen;

        nameLen = CertGetNameStringW(pCertContext, dwType, dwFlags, pvTypePara,
         NULL, 0);
        wideName = CryptMemAlloc(nameLen * sizeof(WCHAR));
        if (wideName)
        {
            CertGetNameStringW(pCertContext, dwType, dwFlags, pvTypePara,
             wideName, nameLen);
            nameLen = WideCharToMultiByte(CP_ACP, 0, wideName, nameLen,
             pszNameString, cchNameString, NULL, NULL);
            if (nameLen <= cchNameString)
                ret = nameLen;
            else
            {
                pszNameString[cchNameString - 1] = '\0';
                ret = cchNameString;
            }
            CryptMemFree(wideName);
        }
        else
        {
            *pszNameString = '\0';
            ret = 1;
        }
    }
    else
        ret = CertGetNameStringW(pCertContext, dwType, dwFlags, pvTypePara,
         NULL, 0);
    return ret;
}

1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072
/* Searches cert's extensions for the alternate name extension with OID
 * altNameOID, and if found, searches it for the alternate name type entryType.
 * If found, returns a pointer to the entry, otherwise returns NULL.
 * Regardless of whether an entry of the desired type is found, if the
 * alternate name extension is present, sets *info to the decoded alternate
 * name extension, which you must free using LocalFree.
 * The return value is a pointer within *info, so don't free *info before
 * you're done with the return value.
 */
static PCERT_ALT_NAME_ENTRY cert_find_alt_name_entry(PCCERT_CONTEXT cert,
 LPCSTR altNameOID, DWORD entryType, PCERT_ALT_NAME_INFO *info)
{
    PCERT_ALT_NAME_ENTRY entry = NULL;
    PCERT_EXTENSION ext = CertFindExtension(altNameOID,
     cert->pCertInfo->cExtension, cert->pCertInfo->rgExtension);

    if (ext)
    {
        DWORD bytes = 0;

        if (CryptDecodeObjectEx(cert->dwCertEncodingType, X509_ALTERNATE_NAME,
         ext->Value.pbData, ext->Value.cbData, CRYPT_DECODE_ALLOC_FLAG, NULL,
         info, &bytes))
        {
            DWORD i;

            for (i = 0; !entry && i < (*info)->cAltEntry; i++)
                if ((*info)->rgAltEntry[i].dwAltNameChoice == entryType)
                    entry = &(*info)->rgAltEntry[i];
        }
    }
    else
        *info = NULL;
    return entry;
}

1073
static DWORD cert_get_name_from_rdn_attr(DWORD encodingType,
1074
 const CERT_NAME_BLOB *name, LPCSTR oid, LPWSTR pszNameString, DWORD cchNameString)
1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091
{
    CERT_NAME_INFO *nameInfo;
    DWORD bytes = 0, ret = 0;

    if (CryptDecodeObjectEx(encodingType, X509_NAME, name->pbData,
     name->cbData, CRYPT_DECODE_ALLOC_FLAG, NULL, &nameInfo, &bytes))
    {
        PCERT_RDN_ATTR nameAttr = CertFindRDNAttr(oid, nameInfo);

        if (nameAttr)
            ret = CertRDNValueToStrW(nameAttr->dwValueType, &nameAttr->Value,
             pszNameString, cchNameString);
        LocalFree(nameInfo);
    }
    return ret;
}

1092 1093 1094
DWORD WINAPI CertGetNameStringW(PCCERT_CONTEXT pCertContext, DWORD dwType,
 DWORD dwFlags, void *pvTypePara, LPWSTR pszNameString, DWORD cchNameString)
{
1095
    DWORD ret = 0;
1096 1097 1098
    PCERT_NAME_BLOB name;
    LPCSTR altNameOID;

1099
    TRACE("(%p, %d, %08x, %p, %p, %d)\n", pCertContext, dwType,
1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114
     dwFlags, pvTypePara, pszNameString, cchNameString);

    if (dwFlags & CERT_NAME_ISSUER_FLAG)
    {
        name = &pCertContext->pCertInfo->Issuer;
        altNameOID = szOID_ISSUER_ALT_NAME;
    }
    else
    {
        name = &pCertContext->pCertInfo->Subject;
        altNameOID = szOID_SUBJECT_ALT_NAME;
    }

    switch (dwType)
    {
1115 1116 1117 1118 1119 1120 1121 1122 1123
    case CERT_NAME_EMAIL_TYPE:
    {
        CERT_ALT_NAME_INFO *info;
        PCERT_ALT_NAME_ENTRY entry = cert_find_alt_name_entry(pCertContext,
         altNameOID, CERT_ALT_NAME_RFC822_NAME, &info);

        if (entry)
        {
            if (!pszNameString)
1124
                ret = strlenW(entry->u.pwszRfc822Name) + 1;
1125
            else if (cchNameString)
1126
            {
1127 1128
                ret = min(strlenW(entry->u.pwszRfc822Name), cchNameString - 1);
                memcpy(pszNameString, entry->u.pwszRfc822Name,
1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139
                 ret * sizeof(WCHAR));
                pszNameString[ret++] = 0;
            }
        }
        if (info)
            LocalFree(info);
        if (!ret)
            ret = cert_get_name_from_rdn_attr(pCertContext->dwCertEncodingType,
             name, szOID_RSA_emailAddr, pszNameString, cchNameString);
        break;
    }
1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151
    case CERT_NAME_RDN_TYPE:
        if (name->cbData)
            ret = CertNameToStrW(pCertContext->dwCertEncodingType, name,
             *(DWORD *)pvTypePara, pszNameString, cchNameString);
        else
        {
            CERT_ALT_NAME_INFO *info;
            PCERT_ALT_NAME_ENTRY entry = cert_find_alt_name_entry(pCertContext,
             altNameOID, CERT_ALT_NAME_DIRECTORY_NAME, &info);

            if (entry)
                ret = CertNameToStrW(pCertContext->dwCertEncodingType,
1152
                 &entry->u.DirectoryName, *(DWORD *)pvTypePara, pszNameString,
1153 1154 1155 1156 1157
                 cchNameString);
            if (info)
                LocalFree(info);
        }
        break;
1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168
    case CERT_NAME_ATTR_TYPE:
        ret = cert_get_name_from_rdn_attr(pCertContext->dwCertEncodingType,
         name, pvTypePara, pszNameString, cchNameString);
        if (!ret)
        {
            CERT_ALT_NAME_INFO *altInfo;
            PCERT_ALT_NAME_ENTRY entry = cert_find_alt_name_entry(pCertContext,
             altNameOID, CERT_ALT_NAME_DIRECTORY_NAME, &altInfo);

            if (entry)
                ret = cert_name_to_str_with_indent(X509_ASN_ENCODING, 0,
1169
                 &entry->u.DirectoryName, 0, pszNameString, cchNameString);
1170 1171 1172 1173
            if (altInfo)
                LocalFree(altInfo);
        }
        break;
1174 1175 1176 1177 1178
    case CERT_NAME_SIMPLE_DISPLAY_TYPE:
    {
        static const LPCSTR simpleAttributeOIDs[] = { szOID_COMMON_NAME,
         szOID_ORGANIZATIONAL_UNIT_NAME, szOID_ORGANIZATION_NAME,
         szOID_RSA_emailAddr };
1179
        CERT_NAME_INFO *nameInfo = NULL;
1180 1181 1182
        DWORD bytes = 0, i;

        if (CryptDecodeObjectEx(pCertContext->dwCertEncodingType, X509_NAME,
1183
         name->pbData, name->cbData, CRYPT_DECODE_ALLOC_FLAG, NULL, &nameInfo,
1184 1185
         &bytes))
        {
1186 1187
            PCERT_RDN_ATTR nameAttr = NULL;

1188 1189
            for (i = 0; !nameAttr && i < sizeof(simpleAttributeOIDs) /
             sizeof(simpleAttributeOIDs[0]); i++)
1190
                nameAttr = CertFindRDNAttr(simpleAttributeOIDs[i], nameInfo);
1191 1192 1193 1194
            if (nameAttr)
                ret = CertRDNValueToStrW(nameAttr->dwValueType,
                 &nameAttr->Value, pszNameString, cchNameString);
            LocalFree(nameInfo);
1195
        }
1196
        if (!ret)
1197
        {
1198 1199 1200
            CERT_ALT_NAME_INFO *altInfo;
            PCERT_ALT_NAME_ENTRY entry = cert_find_alt_name_entry(pCertContext,
             altNameOID, CERT_ALT_NAME_RFC822_NAME, &altInfo);
1201

1202
            if (altInfo)
1203
            {
1204 1205 1206
                if (!entry && altInfo->cAltEntry)
                    entry = &altInfo->rgAltEntry[0];
                if (entry)
1207
                {
1208
                    if (!pszNameString)
1209
                        ret = strlenW(entry->u.pwszRfc822Name) + 1;
1210
                    else if (cchNameString)
1211
                    {
1212
                        ret = min(strlenW(entry->u.pwszRfc822Name),
1213
                         cchNameString - 1);
1214
                        memcpy(pszNameString, entry->u.pwszRfc822Name,
1215 1216
                         ret * sizeof(WCHAR));
                        pszNameString[ret++] = 0;
1217
                    }
1218
                }
1219
                LocalFree(altInfo);
1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236
            }
        }
        break;
    }
    case CERT_NAME_FRIENDLY_DISPLAY_TYPE:
    {
        DWORD cch = cchNameString;

        if (CertGetCertificateContextProperty(pCertContext,
         CERT_FRIENDLY_NAME_PROP_ID, pszNameString, &cch))
            ret = cch;
        else
            ret = CertGetNameStringW(pCertContext,
             CERT_NAME_SIMPLE_DISPLAY_TYPE, dwFlags, pvTypePara, pszNameString,
             cchNameString);
        break;
    }
1237 1238 1239 1240 1241 1242 1243 1244 1245
    case CERT_NAME_DNS_TYPE:
    {
        CERT_ALT_NAME_INFO *info;
        PCERT_ALT_NAME_ENTRY entry = cert_find_alt_name_entry(pCertContext,
         altNameOID, CERT_ALT_NAME_DNS_NAME, &info);

        if (entry)
        {
            if (!pszNameString)
1246
                ret = strlenW(entry->u.pwszDNSName) + 1;
1247
            else if (cchNameString)
1248
            {
1249 1250
                ret = min(strlenW(entry->u.pwszDNSName), cchNameString - 1);
                memcpy(pszNameString, entry->u.pwszDNSName, ret * sizeof(WCHAR));
1251 1252 1253 1254 1255 1256 1257 1258 1259 1260
                pszNameString[ret++] = 0;
            }
        }
        if (info)
            LocalFree(info);
        if (!ret)
            ret = cert_get_name_from_rdn_attr(pCertContext->dwCertEncodingType,
             name, szOID_COMMON_NAME, pszNameString, cchNameString);
        break;
    }
1261 1262 1263 1264 1265 1266 1267 1268 1269
    case CERT_NAME_URL_TYPE:
    {
        CERT_ALT_NAME_INFO *info;
        PCERT_ALT_NAME_ENTRY entry = cert_find_alt_name_entry(pCertContext,
         altNameOID, CERT_ALT_NAME_URL, &info);

        if (entry)
        {
            if (!pszNameString)
1270
                ret = strlenW(entry->u.pwszURL) + 1;
1271
            else if (cchNameString)
1272
            {
1273 1274
                ret = min(strlenW(entry->u.pwszURL), cchNameString - 1);
                memcpy(pszNameString, entry->u.pwszURL, ret * sizeof(WCHAR));
1275 1276 1277 1278 1279 1280 1281
                pszNameString[ret++] = 0;
            }
        }
        if (info)
            LocalFree(info);
        break;
    }
1282
    default:
1283
        FIXME("unimplemented for type %d\n", dwType);
1284 1285
        ret = 0;
    }
1286 1287 1288 1289 1290 1291 1292 1293 1294 1295
    if (!ret)
    {
        if (!pszNameString)
            ret = 1;
        else if (cchNameString)
        {
            pszNameString[0] = 0;
            ret = 1;
        }
    }
1296 1297
    return ret;
}