str.c 43.7 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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 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 145 146 147 148 149 150 151 152 153 154 155 156 157 158
DWORD WINAPI CertRDNValueToStrA(DWORD dwValueType, PCERT_RDN_VALUE_BLOB pValue,
 LPSTR psz, DWORD csz)
{
    DWORD ret = 0, len;

    TRACE("(%d, %p, %p, %d)\n", dwValueType, pValue, psz, csz);

    switch (dwValueType)
    {
    case CERT_RDN_ANY_TYPE:
        break;
    case CERT_RDN_NUMERIC_STRING:
    case CERT_RDN_PRINTABLE_STRING:
    case CERT_RDN_TELETEX_STRING:
    case CERT_RDN_VIDEOTEX_STRING:
    case CERT_RDN_IA5_STRING:
    case CERT_RDN_GRAPHIC_STRING:
    case CERT_RDN_VISIBLE_STRING:
    case CERT_RDN_GENERAL_STRING:
        len = pValue->cbData;
        if (!psz || !csz)
            ret = len;
        else
        {
            DWORD chars = min(len, csz - 1);

            if (chars)
            {
                memcpy(psz, pValue->pbData, chars);
                ret += chars;
                csz -= chars;
            }
        }
        break;
    case CERT_RDN_BMP_STRING:
    case CERT_RDN_UTF8_STRING:
        len = WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)pValue->pbData,
         pValue->cbData / sizeof(WCHAR), NULL, 0, NULL, NULL);
        if (!psz || !csz)
            ret = len;
        else
        {
            DWORD chars = min(pValue->cbData / sizeof(WCHAR), csz - 1);

            if (chars)
            {
                ret = WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)pValue->pbData,
                 chars, psz, csz - 1, NULL, NULL);
                csz -= ret;
            }
        }
        break;
    default:
        FIXME("string type %d unimplemented\n", dwValueType);
    }
    if (psz && csz)
    {
        *(psz + ret) = '\0';
        csz--;
        ret++;
    }
    else
        ret++;
    TRACE("returning %d (%s)\n", ret, debugstr_a(psz));
    return ret;
}

DWORD WINAPI CertRDNValueToStrW(DWORD dwValueType, PCERT_RDN_VALUE_BLOB pValue,
 LPWSTR psz, DWORD csz)
{
    DWORD ret = 0, len, i, strLen;

    TRACE("(%d, %p, %p, %d)\n", dwValueType, pValue, psz, csz);

    switch (dwValueType)
    {
    case CERT_RDN_ANY_TYPE:
        break;
    case CERT_RDN_NUMERIC_STRING:
    case CERT_RDN_PRINTABLE_STRING:
    case CERT_RDN_TELETEX_STRING:
    case CERT_RDN_VIDEOTEX_STRING:
    case CERT_RDN_IA5_STRING:
    case CERT_RDN_GRAPHIC_STRING:
    case CERT_RDN_VISIBLE_STRING:
    case CERT_RDN_GENERAL_STRING:
        len = pValue->cbData;
        if (!psz || !csz)
            ret = len;
        else
        {
            WCHAR *ptr = psz;

            for (i = 0; i < pValue->cbData && ptr - psz < csz; ptr++, i++)
                *ptr = pValue->pbData[i];
            ret = ptr - psz;
        }
        break;
    case CERT_RDN_BMP_STRING:
    case CERT_RDN_UTF8_STRING:
        strLen = len = pValue->cbData / sizeof(WCHAR);
        if (!psz || !csz)
            ret = len;
        else
        {
            WCHAR *ptr = psz;

            for (i = 0; i < strLen && ptr - psz < csz; ptr++, i++)
                *ptr = ((LPCWSTR)pValue->pbData)[i];
            ret = ptr - psz;
        }
        break;
    default:
        FIXME("string type %d unimplemented\n", dwValueType);
    }
    if (psz && csz)
    {
        *(psz + ret) = '\0';
        csz--;
        ret++;
    }
    else
        ret++;
    TRACE("returning %d (%s)\n", ret, debugstr_w(psz));
    return ret;
}

159 160 161 162 163 164 165 166 167 168 169 170
static inline BOOL is_quotable_char(char c)
{
    switch(c)
    {
    case '+':
    case ',':
    case '"':
    case '=':
    case '<':
    case '>':
    case ';':
    case '#':
171
    case '\n':
172 173 174 175 176 177
        return TRUE;
    default:
        return FALSE;
    }
}

178 179
static DWORD quote_rdn_value_to_str_a(DWORD dwValueType,
 PCERT_RDN_VALUE_BLOB pValue, LPSTR psz, DWORD csz)
180
{
181 182
    DWORD ret = 0, len, i;
    BOOL needsQuotes = FALSE;
183

184
    TRACE("(%d, %p, %p, %d)\n", dwValueType, pValue, psz, csz);
185 186 187 188 189

    switch (dwValueType)
    {
    case CERT_RDN_ANY_TYPE:
        break;
190
    case CERT_RDN_NUMERIC_STRING:
191
    case CERT_RDN_PRINTABLE_STRING:
192 193
    case CERT_RDN_TELETEX_STRING:
    case CERT_RDN_VIDEOTEX_STRING:
194
    case CERT_RDN_IA5_STRING:
195 196 197
    case CERT_RDN_GRAPHIC_STRING:
    case CERT_RDN_VISIBLE_STRING:
    case CERT_RDN_GENERAL_STRING:
198 199 200 201 202 203
        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++)
204
        {
205 206 207 208
            if (is_quotable_char(pValue->pbData[i]))
                needsQuotes = TRUE;
            if (pValue->pbData[i] == '"')
                len += 1;
209
        }
210 211
        if (needsQuotes)
            len += 2;
212
        if (!psz || !csz)
213
            ret = len;
214 215
        else
        {
216
            char *ptr = psz;
217

218 219 220
            if (needsQuotes)
                *ptr++ = '"';
            for (i = 0; i < pValue->cbData && ptr - psz < csz; ptr++, i++)
221
            {
222 223 224
                *ptr = pValue->pbData[i];
                if (pValue->pbData[i] == '"' && ptr - psz < csz - 1)
                    *(++ptr) = '"';
225
            }
226 227 228
            if (needsQuotes && ptr - psz < csz)
                *ptr++ = '"';
            ret = ptr - psz;
229 230
        }
        break;
231
    case CERT_RDN_BMP_STRING:
232
    case CERT_RDN_UTF8_STRING:
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
        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;
272
    default:
273
        FIXME("string type %d unimplemented\n", dwValueType);
274 275 276 277 278 279 280 281 282
    }
    if (psz && csz)
    {
        *(psz + ret) = '\0';
        csz--;
        ret++;
    }
    else
        ret++;
283
    TRACE("returning %d (%s)\n", ret, debugstr_a(psz));
284 285 286
    return ret;
}

287 288
static DWORD quote_rdn_value_to_str_w(DWORD dwValueType,
 PCERT_RDN_VALUE_BLOB pValue, LPWSTR psz, DWORD csz)
289
{
290
    DWORD ret = 0, len, i, strLen;
291
    BOOL needsQuotes = FALSE;
292

293
    TRACE("(%d, %p, %p, %d)\n", dwValueType, pValue, psz, csz);
294 295 296 297 298

    switch (dwValueType)
    {
    case CERT_RDN_ANY_TYPE:
        break;
299
    case CERT_RDN_NUMERIC_STRING:
300
    case CERT_RDN_PRINTABLE_STRING:
301 302
    case CERT_RDN_TELETEX_STRING:
    case CERT_RDN_VIDEOTEX_STRING:
303
    case CERT_RDN_IA5_STRING:
304 305 306
    case CERT_RDN_GRAPHIC_STRING:
    case CERT_RDN_VISIBLE_STRING:
    case CERT_RDN_GENERAL_STRING:
307 308 309 310 311 312
        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++)
313
        {
314 315 316 317
            if (is_quotable_char(pValue->pbData[i]))
                needsQuotes = TRUE;
            if (pValue->pbData[i] == '"')
                len += 1;
318
        }
319 320
        if (needsQuotes)
            len += 2;
321
        if (!psz || !csz)
322
            ret = len;
323 324
        else
        {
325
            WCHAR *ptr = psz;
326

327 328 329
            if (needsQuotes)
                *ptr++ = '"';
            for (i = 0; i < pValue->cbData && ptr - psz < csz; ptr++, i++)
330
            {
331 332
                *ptr = pValue->pbData[i];
                if (pValue->pbData[i] == '"' && ptr - psz < csz - 1)
333 334 335 336 337 338 339 340
                    *(++ptr) = '"';
            }
            if (needsQuotes && ptr - psz < csz)
                *ptr++ = '"';
            ret = ptr - psz;
        }
        break;
    case CERT_RDN_BMP_STRING:
341
    case CERT_RDN_UTF8_STRING:
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
        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)
368
                    *(++ptr) = '"';
369
            }
370 371 372
            if (needsQuotes && ptr - psz < csz)
                *ptr++ = '"';
            ret = ptr - psz;
373 374 375
        }
        break;
    default:
376
        FIXME("string type %d unimplemented\n", dwValueType);
377 378 379 380 381 382 383 384 385
    }
    if (psz && csz)
    {
        *(psz + ret) = '\0';
        csz--;
        ret++;
    }
    else
        ret++;
386
    TRACE("returning %d (%s)\n", ret, debugstr_w(psz));
387
    return ret;
388 389
}

390 391 392 393 394 395 396
/* 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)
{
397
    DWORD chars;
398

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

401
    if (psz)
402
    {
403
        chars = min(strlen(prefix), csz);
404 405
        memcpy(psz, prefix, chars);
        *(psz + chars) = '=';
406 407
        chars++;
    }
408 409
    else
        chars = lstrlenA(prefix) + 1;
410 411 412
    return chars;
}

413 414 415 416
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 |
417
     CERT_NAME_STR_ENABLE_T61_UNICODE_FLAG;
418 419 420 421 422 423 424 425 426
    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;

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);
431 432 433 434 435 436 437

    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;
438 439 440 441
        BOOL reverse = dwStrType & CERT_NAME_STR_REVERSE_FLAG;
        const CERT_RDN *rdn = info->rgRDN;

        if(reverse && info->cRDN > 1) rdn += (info->cRDN - 1);
442 443 444 445 446 447 448 449 450 451 452 453 454

        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);
455
        for (i = 0; (!psz || ret < csz) && i < info->cRDN; i++)
456
        {
457
            for (j = 0; (!psz || ret < csz) && j < rdn->cRDNAttr; j++)
458 459
            {
                DWORD chars;
460 461
                char prefixBuf[10]; /* big enough for GivenName */
                LPCSTR prefix = NULL;
462 463

                if ((dwStrType & 0x000000ff) == CERT_OID_NAME_STR)
464
                    prefix = rdn->rgRDNAttr[j].pszObjId;
465 466 467 468
                else if ((dwStrType & 0x000000ff) == CERT_X500_NAME_STR)
                {
                    PCCRYPT_OID_INFO oidInfo = CryptFindOIDInfo(
                     CRYPT_OID_INFO_OID_KEY,
469
                     rdn->rgRDNAttr[j].pszObjId,
470 471 472 473 474 475 476 477 478
                     CRYPT_RDN_ATTR_OID_GROUP_ID);

                    if (oidInfo)
                    {
                        WideCharToMultiByte(CP_ACP, 0, oidInfo->pwszName, -1,
                         prefixBuf, sizeof(prefixBuf), NULL, NULL);
                        prefix = prefixBuf;
                    }
                    else
479
                        prefix = rdn->rgRDNAttr[j].pszObjId;
480 481
                }
                if (prefix)
482 483
                {
                    /* - 1 is needed to account for the NULL terminator. */
484 485
                    chars = CRYPT_AddPrefixA(prefix,
                     psz ? psz + ret : NULL, psz ? csz - ret - 1 : 0);
486 487
                    ret += chars;
                }
488
                chars = quote_rdn_value_to_str_a(
489 490
                 rdn->rgRDNAttr[j].dwValueType,
                 &rdn->rgRDNAttr[j].Value, psz ? psz + ret : NULL,
491
                 psz ? csz - ret : 0);
492 493
                if (chars)
                    ret += chars - 1;
494
                if (j < rdn->cRDNAttr - 1)
495 496 497 498 499 500 501 502 503 504 505 506
                {
                    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;
            }
507 508
            if(reverse) rdn--;
            else rdn++;
509 510 511 512 513 514 515 516 517 518
        }
        LocalFree(info);
    }
    if (psz && csz)
    {
        *(psz + ret) = '\0';
        ret++;
    }
    else
        ret++;
519
    TRACE("Returning %s\n", debugstr_a(psz));
520 521 522
    return ret;
}

523 524 525 526 527 528 529 530
/* 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)
{
531
    DWORD chars;
532

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

535
    if (psz)
536 537 538
    {
        DWORD i;

539
        chars = min(strlen(prefix), csz);
540 541
        for (i = 0; i < chars; i++)
            *(psz + i) = prefix[i];
542
        *(psz + chars) = '=';
543 544
        chars++;
    }
545 546
    else
        chars = lstrlenA(prefix) + 1;
547 548 549 550 551 552 553 554 555 556
    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)
{
557
    DWORD chars;
558

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

561
    if (psz)
562
    {
563
        chars = min(strlenW(prefix), csz);
564 565
        memcpy(psz, prefix, chars * sizeof(WCHAR));
        *(psz + chars) = '=';
566 567
        chars++;
    }
568 569
    else
        chars = lstrlenW(prefix) + 1;
570 571 572
    return chars;
}

573 574 575
static const WCHAR indent[] = { ' ',' ',' ',' ',' ',0 };

DWORD cert_name_to_str_with_indent(DWORD dwCertEncodingType, DWORD indentLevel,
576
 const CERT_NAME_BLOB *pName, DWORD dwStrType, LPWSTR psz, DWORD csz)
577
{
578
    static const DWORD unsupportedFlags = CERT_NAME_STR_NO_QUOTING_FLAG |
579
     CERT_NAME_STR_ENABLE_T61_UNICODE_FLAG;
580 581 582 583 584 585 586 587 588 589
    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)
590
        FIXME("unsupported flags: %08x\n", dwStrType & unsupportedFlags);
591 592 593 594 595 596 597

    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;
598 599 600 601
        BOOL reverse = dwStrType & CERT_NAME_STR_REVERSE_FLAG;
        const CERT_RDN *rdn = info->rgRDN;

        if(reverse && info->cRDN > 1) rdn += (info->cRDN - 1);
602 603 604 605 606 607 608 609 610 611 612 613 614

        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);
615
        for (i = 0; (!psz || ret < csz) && i < info->cRDN; i++)
616
        {
617
            for (j = 0; (!psz || ret < csz) && j < rdn->cRDNAttr; j++)
618 619
            {
                DWORD chars;
620 621
                LPCSTR prefixA = NULL;
                LPCWSTR prefixW = NULL;
622 623

                if ((dwStrType & 0x000000ff) == CERT_OID_NAME_STR)
624
                    prefixA = rdn->rgRDNAttr[j].pszObjId;
625 626 627 628
                else if ((dwStrType & 0x000000ff) == CERT_X500_NAME_STR)
                {
                    PCCRYPT_OID_INFO oidInfo = CryptFindOIDInfo(
                     CRYPT_OID_INFO_OID_KEY,
629
                     rdn->rgRDNAttr[j].pszObjId,
630 631 632 633 634
                     CRYPT_RDN_ATTR_OID_GROUP_ID);

                    if (oidInfo)
                        prefixW = oidInfo->pwszName;
                    else
635
                        prefixA = rdn->rgRDNAttr[j].pszObjId;
636
                }
637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652
                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;
                    }
                }
653 654 655
                if (prefixW)
                {
                    /* - 1 is needed to account for the NULL terminator. */
656 657
                    chars = CRYPT_AddPrefixW(prefixW,
                     psz ? psz + ret : NULL, psz ? csz - ret - 1 : 0);
658 659 660
                    ret += chars;
                }
                else if (prefixA)
661 662
                {
                    /* - 1 is needed to account for the NULL terminator. */
663 664
                    chars = CRYPT_AddPrefixAToW(prefixA,
                     psz ? psz + ret : NULL, psz ? csz - ret - 1 : 0);
665 666
                    ret += chars;
                }
667
                chars = quote_rdn_value_to_str_w(
668 669
                 rdn->rgRDNAttr[j].dwValueType,
                 &rdn->rgRDNAttr[j].Value, psz ? psz + ret : NULL,
670
                 psz ? csz - ret : 0);
671 672
                if (chars)
                    ret += chars - 1;
673
                if (j < rdn->cRDNAttr - 1)
674 675 676 677 678 679 680 681 682 683 684 685
                {
                    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;
            }
686 687
            if(reverse) rdn--;
            else rdn++;
688 689 690 691 692 693 694 695 696 697
        }
        LocalFree(info);
    }
    if (psz && csz)
    {
        *(psz + ret) = '\0';
        ret++;
    }
    else
        ret++;
698 699 700 701 702 703 704 705 706 707 708 709 710
    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);
711
    TRACE("Returning %s\n", debugstr_w(psz));
712
    return ret;
713
}
714

715 716 717 718 719 720 721
BOOL WINAPI CertStrToNameA(DWORD dwCertEncodingType, LPCSTR pszX500,
 DWORD dwStrType, void *pvReserved, BYTE *pbEncoded, DWORD *pcbEncoded,
 LPCSTR *ppszError)
{
    BOOL ret;
    int len;

722
    TRACE("(%08x, %s, %08x, %p, %p, %p, %p)\n", dwCertEncodingType,
723 724 725 726
     debugstr_a(pszX500), dwStrType, pvReserved, pbEncoded, pcbEncoded,
     ppszError);

    len = MultiByteToWideChar(CP_ACP, 0, pszX500, -1, NULL, 0);
727
    if (len)
728
    {
729 730 731
        LPWSTR x500, errorStr;

        if ((x500 = CryptMemAlloc(len * sizeof(WCHAR))))
732
        {
733 734 735 736 737 738
            MultiByteToWideChar(CP_ACP, 0, pszX500, -1, x500, len);
            ret = CertStrToNameW(dwCertEncodingType, x500, dwStrType,
             pvReserved, pbEncoded, pcbEncoded,
             ppszError ? (LPCWSTR *)&errorStr : NULL);
            if (ppszError)
            {
739 740
                if (!ret)
                {
741
                    LONG i;
742

743 744 745 746 747 748
                    *ppszError = pszX500;
                    for (i = 0; i < errorStr - x500; i++)
                        *ppszError = CharNextA(*ppszError);
                }
                else
                    *ppszError = NULL;
749 750 751 752 753 754 755
            }
            CryptMemFree(x500);
        }
        else
        {
            SetLastError(ERROR_OUTOFMEMORY);
            ret = FALSE;
756 757 758
        }
    }
    else
759 760 761 762
    {
        SetLastError(CRYPT_E_INVALID_X500_STRING);
        if (ppszError)
            *ppszError = pszX500;
763
        ret = FALSE;
764
    }
765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793
    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,
794
 const struct X500TokenW *key)
795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812
{
    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));
}

813
static BOOL CRYPT_GetNextKeyW(LPCWSTR str, struct X500TokenW *token,
814 815
 LPCWSTR *ppszError)
{
816
    BOOL ret = TRUE;
817 818 819 820 821 822 823 824 825 826 827 828 829 830 831

    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;
832 833
            SetLastError(CRYPT_E_INVALID_X500_STRING);
            ret = FALSE;
834 835 836 837 838 839 840 841
        }
    }
    else
        token->start = NULL;
    return ret;
}

/* Assumes separators are characters in the 0-255 range */
842
static BOOL CRYPT_GetNextValueW(LPCWSTR str, DWORD dwFlags, LPCWSTR separators,
843 844
 struct X500TokenW *token, LPCWSTR *ppszError)
{
845
    BOOL ret = TRUE;
846 847 848 849 850 851 852 853 854 855 856 857 858

    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++;
859
            while (!token->end && ret)
860 861 862 863 864 865 866 867 868 869 870 871 872 873 874
            {
                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;
875 876
                    SetLastError(CRYPT_E_INVALID_X500_STRING);
                    ret = FALSE;
877 878 879 880 881 882 883 884 885
                }
            }
        }
        else
        {
            WCHAR map[256] = { 0 };

            while (*separators)
                map[*separators++] = 1;
886
            while (*str && (*str >= 0xff || !map[*str]))
887 888 889 890 891 892 893 894 895
                str++;
            token->end = str;
        }
    }
    else
    {
        TRACE("missing value at %s\n", debugstr_w(str));
        if (ppszError)
            *ppszError = str;
896 897
        SetLastError(CRYPT_E_INVALID_X500_STRING);
        ret = FALSE;
898 899 900 901 902 903 904 905 906 907
    }
    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,
908
 const struct X500TokenW *value, PCERT_NAME_BLOB output, DWORD type,
909 910 911
 LPCWSTR *ppszError)
{
    CERT_NAME_VALUE nameValue = { type, { 0, NULL } };
912
    BOOL ret = TRUE;
913

914
    if (value->end > value->start)
915
    {
916 917 918
        LONG i;
        LPWSTR ptr;

919
        nameValue.Value.pbData = CryptMemAlloc((value->end - value->start + 1) *
920 921 922 923
         sizeof(WCHAR));
        if (!nameValue.Value.pbData)
        {
            SetLastError(ERROR_OUTOFMEMORY);
924
            return FALSE;
925
        }
926 927
        ptr = (LPWSTR)nameValue.Value.pbData;
        for (i = 0; i < value->end - value->start; i++)
928
        {
929 930 931
            *ptr++ = value->start[i];
            if (value->start[i] == '"')
                i++;
932
        }
933 934 935 936 937
        /* The string is NULL terminated because of a quirk in encoding
         * unicode names values:  if the length is given as 0, the value is
         * assumed to be a NULL-terminated string.
         */
        *ptr = 0;
938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953
        nameValue.Value.cbData = (LPBYTE)ptr - nameValue.Value.pbData;
    }
    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;
954
    }
955
    CryptMemFree(nameValue.Value.pbData);
956 957 958 959
    return ret;
}

static BOOL CRYPT_EncodeValue(DWORD dwCertEncodingType,
960
 const struct X500TokenW *value, PCERT_NAME_BLOB output, const DWORD *types,
961 962 963 964 965 966 967 968 969 970 971 972 973
 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,
974
 PCCRYPT_OID_INFO keyOID, struct X500TokenW *value, DWORD dwStrType, LPCWSTR *ppszError)
975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007
{
    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 */
1008
            if (value->start[0] == '"' && !(dwStrType & CERT_NAME_STR_NO_QUOTING_FLAG))
1009 1010 1011 1012 1013 1014 1015
            {
                value->start++;
                value->end--;
            }
            ret = CRYPT_EncodeValue(dwCertEncodingType, value,
             &info->rgRDN[info->cRDN].rgRDNAttr[0].Value, types, ppszError);
        }
1016 1017
        else
            SetLastError(ERROR_OUTOFMEMORY);
1018
        info->cRDN++;
1019 1020 1021
    }
    else
        SetLastError(ERROR_OUTOFMEMORY);
1022 1023 1024 1025 1026 1027 1028 1029 1030 1031
    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;
1032
    DWORD i;
1033 1034
    BOOL ret = TRUE;

1035
    TRACE("(%08x, %s, %08x, %p, %p, %p, %p)\n", dwCertEncodingType,
1036 1037 1038 1039 1040
     debugstr_w(pszX500), dwStrType, pvReserved, pbEncoded, pcbEncoded,
     ppszError);

    CRYPT_InitializeKeynameKeeper(&keeper);
    str = pszX500;
1041
    while (str && *str && ret)
1042 1043 1044
    {
        struct X500TokenW token;

1045 1046
        ret = CRYPT_GetNextKeyW(str, &token, ppszError);
        if (ret && token.start)
1047 1048 1049 1050 1051 1052 1053 1054 1055 1056
        {
            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;
1057 1058
                SetLastError(CRYPT_E_INVALID_X500_STRING);
                ret = FALSE;
1059 1060 1061 1062 1063 1064 1065 1066 1067 1068
            }
            else
            {
                str = token.end;
                while (isspace(*str))
                    str++;
                if (*str != '=')
                {
                    if (ppszError)
                        *ppszError = str;
1069 1070
                    SetLastError(CRYPT_E_INVALID_X500_STRING);
                    ret = FALSE;
1071 1072 1073 1074 1075 1076
                }
                else
                {
                    static const WCHAR commaSep[] = { ',',0 };
                    static const WCHAR semiSep[] = { ';',0 };
                    static const WCHAR crlfSep[] = { '\r','\n',0 };
1077 1078
                    static const WCHAR allSepsWithoutPlus[] = { ',',';','\r','\n',0 };
                    static const WCHAR allSeps[] = { '+',',',';','\r','\n',0 };
1079 1080 1081 1082 1083 1084 1085 1086 1087
                    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;
1088 1089
                    else if (dwStrType & CERT_NAME_STR_NO_PLUS_FLAG)
                        sep = allSepsWithoutPlus;
1090 1091
                    else
                        sep = allSeps;
1092
                    ret = CRYPT_GetNextValueW(str, dwStrType, sep, &token,
1093
                     ppszError);
1094
                    if (ret)
1095 1096 1097
                    {
                        str = token.end;
                        ret = CRYPT_ValueToRDN(dwCertEncodingType, &info,
1098
                         keyOID, &token, dwStrType, ppszError);
1099 1100 1101 1102 1103 1104
                    }
                }
            }
        }
    }
    CRYPT_FreeKeynameKeeper(&keeper);
1105
    if (ret)
1106
    {
1107 1108
        if (ppszError)
            *ppszError = NULL;
1109 1110 1111
        ret = CryptEncodeObjectEx(dwCertEncodingType, X509_NAME, &info,
         0, NULL, pbEncoded, pcbEncoded);
    }
1112 1113 1114 1115 1116 1117 1118 1119 1120
    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);
1121 1122 1123
    return ret;
}

1124 1125 1126 1127 1128
DWORD WINAPI CertGetNameStringA(PCCERT_CONTEXT pCertContext, DWORD dwType,
 DWORD dwFlags, void *pvTypePara, LPSTR pszNameString, DWORD cchNameString)
{
    DWORD ret;

1129
    TRACE("(%p, %d, %08x, %p, %p, %d)\n", pCertContext, dwType, dwFlags,
1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166
     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;
}

1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202
/* 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;
}

1203
static DWORD cert_get_name_from_rdn_attr(DWORD encodingType,
1204
 const CERT_NAME_BLOB *name, LPCSTR oid, LPWSTR pszNameString, DWORD cchNameString)
1205 1206 1207 1208 1209 1210 1211
{
    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))
    {
1212
        PCERT_RDN_ATTR nameAttr;
1213

1214 1215 1216
        if (!oid)
            oid = szOID_RSA_emailAddr;
        nameAttr = CertFindRDNAttr(oid, nameInfo);
1217 1218 1219 1220 1221 1222 1223 1224
        if (nameAttr)
            ret = CertRDNValueToStrW(nameAttr->dwValueType, &nameAttr->Value,
             pszNameString, cchNameString);
        LocalFree(nameInfo);
    }
    return ret;
}

1225 1226 1227
DWORD WINAPI CertGetNameStringW(PCCERT_CONTEXT pCertContext, DWORD dwType,
 DWORD dwFlags, void *pvTypePara, LPWSTR pszNameString, DWORD cchNameString)
{
1228
    DWORD ret = 0;
1229 1230 1231
    PCERT_NAME_BLOB name;
    LPCSTR altNameOID;

1232
    TRACE("(%p, %d, %08x, %p, %p, %d)\n", pCertContext, dwType,
1233 1234
     dwFlags, pvTypePara, pszNameString, cchNameString);

1235 1236 1237
    if (!pCertContext)
        goto done;

1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250
    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)
    {
1251 1252 1253 1254 1255 1256 1257 1258 1259
    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)
1260
                ret = strlenW(entry->u.pwszRfc822Name) + 1;
1261
            else if (cchNameString)
1262
            {
1263 1264
                ret = min(strlenW(entry->u.pwszRfc822Name), cchNameString - 1);
                memcpy(pszNameString, entry->u.pwszRfc822Name,
1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275
                 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;
    }
1276
    case CERT_NAME_RDN_TYPE:
1277 1278 1279
    {
        DWORD type = pvTypePara ? *(DWORD *)pvTypePara : 0;

1280 1281
        if (name->cbData)
            ret = CertNameToStrW(pCertContext->dwCertEncodingType, name,
1282
             type, pszNameString, cchNameString);
1283 1284 1285 1286 1287 1288 1289 1290
        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,
1291
                 &entry->u.DirectoryName, type, pszNameString, cchNameString);
1292 1293 1294 1295
            if (info)
                LocalFree(info);
        }
        break;
1296
    }
1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307
    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,
1308
                 &entry->u.DirectoryName, 0, pszNameString, cchNameString);
1309 1310 1311 1312
            if (altInfo)
                LocalFree(altInfo);
        }
        break;
1313 1314 1315 1316 1317
    case CERT_NAME_SIMPLE_DISPLAY_TYPE:
    {
        static const LPCSTR simpleAttributeOIDs[] = { szOID_COMMON_NAME,
         szOID_ORGANIZATIONAL_UNIT_NAME, szOID_ORGANIZATION_NAME,
         szOID_RSA_emailAddr };
1318
        CERT_NAME_INFO *nameInfo = NULL;
1319 1320 1321
        DWORD bytes = 0, i;

        if (CryptDecodeObjectEx(pCertContext->dwCertEncodingType, X509_NAME,
1322
         name->pbData, name->cbData, CRYPT_DECODE_ALLOC_FLAG, NULL, &nameInfo,
1323 1324
         &bytes))
        {
1325 1326
            PCERT_RDN_ATTR nameAttr = NULL;

1327 1328
            for (i = 0; !nameAttr && i < sizeof(simpleAttributeOIDs) /
             sizeof(simpleAttributeOIDs[0]); i++)
1329
                nameAttr = CertFindRDNAttr(simpleAttributeOIDs[i], nameInfo);
1330 1331 1332 1333
            if (nameAttr)
                ret = CertRDNValueToStrW(nameAttr->dwValueType,
                 &nameAttr->Value, pszNameString, cchNameString);
            LocalFree(nameInfo);
1334
        }
1335
        if (!ret)
1336
        {
1337 1338 1339
            CERT_ALT_NAME_INFO *altInfo;
            PCERT_ALT_NAME_ENTRY entry = cert_find_alt_name_entry(pCertContext,
             altNameOID, CERT_ALT_NAME_RFC822_NAME, &altInfo);
1340

1341
            if (altInfo)
1342
            {
1343 1344 1345
                if (!entry && altInfo->cAltEntry)
                    entry = &altInfo->rgAltEntry[0];
                if (entry)
1346
                {
1347
                    if (!pszNameString)
1348
                        ret = strlenW(entry->u.pwszRfc822Name) + 1;
1349
                    else if (cchNameString)
1350
                    {
1351
                        ret = min(strlenW(entry->u.pwszRfc822Name),
1352
                         cchNameString - 1);
1353
                        memcpy(pszNameString, entry->u.pwszRfc822Name,
1354 1355
                         ret * sizeof(WCHAR));
                        pszNameString[ret++] = 0;
1356
                    }
1357
                }
1358
                LocalFree(altInfo);
1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375
            }
        }
        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;
    }
1376 1377 1378 1379 1380 1381 1382 1383 1384
    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)
1385
                ret = strlenW(entry->u.pwszDNSName) + 1;
1386
            else if (cchNameString)
1387
            {
1388 1389
                ret = min(strlenW(entry->u.pwszDNSName), cchNameString - 1);
                memcpy(pszNameString, entry->u.pwszDNSName, ret * sizeof(WCHAR));
1390 1391 1392 1393 1394 1395 1396 1397 1398 1399
                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;
    }
1400 1401 1402 1403 1404 1405 1406 1407 1408
    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)
1409
                ret = strlenW(entry->u.pwszURL) + 1;
1410
            else if (cchNameString)
1411
            {
1412 1413
                ret = min(strlenW(entry->u.pwszURL), cchNameString - 1);
                memcpy(pszNameString, entry->u.pwszURL, ret * sizeof(WCHAR));
1414 1415 1416 1417 1418 1419 1420
                pszNameString[ret++] = 0;
            }
        }
        if (info)
            LocalFree(info);
        break;
    }
1421
    default:
1422
        FIXME("unimplemented for type %d\n", dwType);
1423 1424
        ret = 0;
    }
1425
done:
1426 1427 1428 1429 1430 1431 1432 1433 1434 1435
    if (!ret)
    {
        if (!pszNameString)
            ret = 1;
        else if (cchNameString)
        {
            pszNameString[0] = 0;
            ret = 1;
        }
    }
1436 1437
    return ret;
}