| 1 | /*
|
|---|
| 2 | * Copyright 2006 Juan Lang for CodeWeavers
|
|---|
| 3 | *
|
|---|
| 4 | * This library is free software; you can redistribute it and/or
|
|---|
| 5 | * modify it under the terms of the GNU Lesser General Public
|
|---|
| 6 | * License as published by the Free Software Foundation; either
|
|---|
| 7 | * version 2.1 of the License, or (at your option) any later version.
|
|---|
| 8 | *
|
|---|
| 9 | * This library is distributed in the hope that it will be useful,
|
|---|
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|---|
| 12 | * Lesser General Public License for more details.
|
|---|
| 13 | *
|
|---|
| 14 | * You should have received a copy of the GNU Lesser General Public
|
|---|
| 15 | * License along with this library; if not, write to the Free Software
|
|---|
| 16 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
|---|
| 17 | */
|
|---|
| 18 | #include <stdarg.h>
|
|---|
| 19 | #include "windef.h"
|
|---|
| 20 | #include "winbase.h"
|
|---|
| 21 | #include "winerror.h"
|
|---|
| 22 | #include "winnls.h"
|
|---|
| 23 | #include "winuser.h"
|
|---|
| 24 | #include "wincrypt.h"
|
|---|
| 25 | #include "wine/debug.h"
|
|---|
| 26 | #include "wine/unicode.h"
|
|---|
| 27 |
|
|---|
| 28 | WINE_DEFAULT_DEBUG_CHANNEL(crypt);
|
|---|
| 29 |
|
|---|
| 30 | DWORD WINAPI CertRDNValueToStrA(DWORD dwValueType, PCERT_RDN_VALUE_BLOB pValue,
|
|---|
| 31 | LPSTR psz, DWORD csz)
|
|---|
| 32 | {
|
|---|
| 33 | DWORD ret = 0;
|
|---|
| 34 |
|
|---|
| 35 | TRACE("(%d, %p, %p, %d)\n", dwValueType, pValue, psz, csz);
|
|---|
| 36 |
|
|---|
| 37 | switch (dwValueType)
|
|---|
| 38 | {
|
|---|
| 39 | case CERT_RDN_ANY_TYPE:
|
|---|
| 40 | break;
|
|---|
| 41 | case CERT_RDN_NUMERIC_STRING:
|
|---|
| 42 | case CERT_RDN_PRINTABLE_STRING:
|
|---|
| 43 | case CERT_RDN_TELETEX_STRING:
|
|---|
| 44 | case CERT_RDN_VIDEOTEX_STRING:
|
|---|
| 45 | case CERT_RDN_IA5_STRING:
|
|---|
| 46 | case CERT_RDN_GRAPHIC_STRING:
|
|---|
| 47 | case CERT_RDN_VISIBLE_STRING:
|
|---|
| 48 | case CERT_RDN_GENERAL_STRING:
|
|---|
| 49 | if (!psz || !csz)
|
|---|
| 50 | ret = pValue->cbData;
|
|---|
| 51 | else
|
|---|
| 52 | {
|
|---|
| 53 | DWORD chars = min(pValue->cbData, csz - 1);
|
|---|
| 54 |
|
|---|
| 55 | if (chars)
|
|---|
| 56 | {
|
|---|
| 57 | memcpy(psz, pValue->pbData, chars);
|
|---|
| 58 | ret += chars;
|
|---|
| 59 | csz -= chars;
|
|---|
| 60 | }
|
|---|
| 61 | }
|
|---|
| 62 | break;
|
|---|
| 63 | case CERT_RDN_UTF8_STRING:
|
|---|
| 64 | if (!psz || !csz)
|
|---|
| 65 | ret = WideCharToMultiByte(CP_UTF8, 0, (LPWSTR)pValue->pbData,
|
|---|
| 66 | pValue->cbData / sizeof(WCHAR) + 1, NULL, 0, NULL, NULL);
|
|---|
| 67 | else
|
|---|
| 68 | {
|
|---|
| 69 | ret = WideCharToMultiByte(CP_UTF8, 0, (LPWSTR)pValue->pbData,
|
|---|
| 70 | pValue->cbData / sizeof(WCHAR) + 1, psz, csz - 1, NULL, NULL);
|
|---|
| 71 | csz -= ret;
|
|---|
| 72 | }
|
|---|
| 73 | break;
|
|---|
| 74 | default:
|
|---|
| 75 | FIXME("string type %d unimplemented\n", dwValueType);
|
|---|
| 76 | }
|
|---|
| 77 | if (psz && csz)
|
|---|
| 78 | {
|
|---|
| 79 | *(psz + ret) = '\0';
|
|---|
| 80 | csz--;
|
|---|
| 81 | ret++;
|
|---|
| 82 | }
|
|---|
| 83 | else
|
|---|
| 84 | ret++;
|
|---|
| 85 | TRACE("returning %d (%s)\n", ret, debugstr_a(psz));
|
|---|
| 86 | return ret;
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|
| 89 | DWORD WINAPI CertRDNValueToStrW(DWORD dwValueType, PCERT_RDN_VALUE_BLOB pValue,
|
|---|
| 90 | LPWSTR psz, DWORD csz)
|
|---|
| 91 | {
|
|---|
| 92 | DWORD ret = 0;
|
|---|
| 93 |
|
|---|
| 94 | TRACE("(%d, %p, %p, %d)\n", dwValueType, pValue, psz, csz);
|
|---|
| 95 |
|
|---|
| 96 | switch (dwValueType)
|
|---|
| 97 | {
|
|---|
| 98 | case CERT_RDN_ANY_TYPE:
|
|---|
| 99 | break;
|
|---|
| 100 | case CERT_RDN_NUMERIC_STRING:
|
|---|
| 101 | case CERT_RDN_PRINTABLE_STRING:
|
|---|
| 102 | case CERT_RDN_TELETEX_STRING:
|
|---|
| 103 | case CERT_RDN_VIDEOTEX_STRING:
|
|---|
| 104 | case CERT_RDN_IA5_STRING:
|
|---|
| 105 | case CERT_RDN_GRAPHIC_STRING:
|
|---|
| 106 | case CERT_RDN_VISIBLE_STRING:
|
|---|
| 107 | case CERT_RDN_GENERAL_STRING:
|
|---|
| 108 | if (!psz || !csz)
|
|---|
| 109 | ret = pValue->cbData;
|
|---|
| 110 | else
|
|---|
| 111 | {
|
|---|
| 112 | DWORD chars = min(pValue->cbData, csz - 1);
|
|---|
| 113 |
|
|---|
| 114 | if (chars)
|
|---|
| 115 | {
|
|---|
| 116 | DWORD i;
|
|---|
| 117 |
|
|---|
| 118 | for (i = 0; i < chars; i++)
|
|---|
| 119 | psz[i] = pValue->pbData[i];
|
|---|
| 120 | ret += chars;
|
|---|
| 121 | csz -= chars;
|
|---|
| 122 | }
|
|---|
| 123 | }
|
|---|
| 124 | break;
|
|---|
| 125 | case CERT_RDN_UTF8_STRING:
|
|---|
| 126 | if (!psz || !csz)
|
|---|
| 127 | ret = pValue->cbData / sizeof(WCHAR);
|
|---|
| 128 | else
|
|---|
| 129 | {
|
|---|
| 130 | DWORD chars = min(pValue->cbData / sizeof(WCHAR), csz - 1);
|
|---|
| 131 |
|
|---|
| 132 | if (chars)
|
|---|
| 133 | {
|
|---|
| 134 | DWORD i;
|
|---|
| 135 |
|
|---|
| 136 | for (i = 0; i < chars; i++)
|
|---|
| 137 | psz[i] = *((LPWSTR)pValue->pbData + i);
|
|---|
| 138 | ret += chars;
|
|---|
| 139 | csz -= chars;
|
|---|
| 140 | }
|
|---|
| 141 | }
|
|---|
| 142 | break;
|
|---|
| 143 | default:
|
|---|
| 144 | FIXME("string type %d unimplemented\n", dwValueType);
|
|---|
| 145 | }
|
|---|
| 146 | if (psz && csz)
|
|---|
| 147 | {
|
|---|
| 148 | *(psz + ret) = '\0';
|
|---|
| 149 | csz--;
|
|---|
| 150 | ret++;
|
|---|
| 151 | }
|
|---|
| 152 | else
|
|---|
| 153 | ret++;
|
|---|
| 154 | TRACE("returning %d (%s)\n", ret, debugstr_w(psz));
|
|---|
| 155 | return ret;
|
|---|
| 156 | }
|
|---|
| 157 |
|
|---|
| 158 | /* Adds the prefix prefix to the string pointed to by psz, followed by the
|
|---|
| 159 | * character '='. Copies no more than csz characters. Returns the number of
|
|---|
| 160 | * characters copied. If psz is NULL, returns the number of characters that
|
|---|
| 161 | * would be copied.
|
|---|
| 162 | */
|
|---|
| 163 | static DWORD CRYPT_AddPrefixA(LPCSTR prefix, LPSTR psz, DWORD csz)
|
|---|
| 164 | {
|
|---|
| 165 | DWORD chars;
|
|---|
| 166 |
|
|---|
| 167 | TRACE("(%s, %p, %d)\n", debugstr_a(prefix), psz, csz);
|
|---|
| 168 |
|
|---|
| 169 | if (psz)
|
|---|
| 170 | {
|
|---|
| 171 | chars = min(strlen(prefix), csz);
|
|---|
| 172 | memcpy(psz, prefix, chars);
|
|---|
| 173 | *(psz + chars) = '=';
|
|---|
| 174 | chars++;
|
|---|
| 175 | }
|
|---|
| 176 | else
|
|---|
| 177 | chars = lstrlenA(prefix) + 1;
|
|---|
| 178 | return chars;
|
|---|
| 179 | }
|
|---|
| 180 |
|
|---|
| 181 | DWORD WINAPI CertNameToStrA(DWORD dwCertEncodingType, PCERT_NAME_BLOB pName,
|
|---|
| 182 | DWORD dwStrType, LPSTR psz, DWORD csz)
|
|---|
| 183 | {
|
|---|
| 184 | static const DWORD unsupportedFlags = CERT_NAME_STR_NO_QUOTING_FLAG |
|
|---|
| 185 | CERT_NAME_STR_ENABLE_T61_UNICODE_FLAG;
|
|---|
| 186 | static const char commaSep[] = ", ";
|
|---|
| 187 | static const char semiSep[] = "; ";
|
|---|
| 188 | static const char crlfSep[] = "\r\n";
|
|---|
| 189 | static const char plusSep[] = " + ";
|
|---|
| 190 | static const char spaceSep[] = " ";
|
|---|
| 191 | DWORD ret = 0, bytes = 0;
|
|---|
| 192 | BOOL bRet;
|
|---|
| 193 | CERT_NAME_INFO *info;
|
|---|
| 194 |
|
|---|
| 195 | TRACE("(%d, %p, %08x, %p, %d)\n", dwCertEncodingType, pName, dwStrType,
|
|---|
| 196 | psz, csz);
|
|---|
| 197 | if (dwStrType & unsupportedFlags)
|
|---|
| 198 | FIXME("unsupported flags: %08x\n", dwStrType & unsupportedFlags);
|
|---|
| 199 |
|
|---|
| 200 | bRet = CryptDecodeObjectEx(dwCertEncodingType, X509_NAME, pName->pbData,
|
|---|
| 201 | pName->cbData, CRYPT_DECODE_ALLOC_FLAG, NULL, &info, &bytes);
|
|---|
| 202 | if (bRet)
|
|---|
| 203 | {
|
|---|
| 204 | DWORD i, j, sepLen, rdnSepLen;
|
|---|
| 205 | LPCSTR sep, rdnSep;
|
|---|
| 206 | BOOL reverse = dwStrType & CERT_NAME_STR_REVERSE_FLAG;
|
|---|
| 207 | const CERT_RDN *rdn = info->rgRDN;
|
|---|
| 208 |
|
|---|
| 209 | if(reverse && info->cRDN > 1) rdn += (info->cRDN - 1);
|
|---|
| 210 |
|
|---|
| 211 | if (dwStrType & CERT_NAME_STR_SEMICOLON_FLAG)
|
|---|
| 212 | sep = semiSep;
|
|---|
| 213 | else if (dwStrType & CERT_NAME_STR_CRLF_FLAG)
|
|---|
| 214 | sep = crlfSep;
|
|---|
| 215 | else
|
|---|
| 216 | sep = commaSep;
|
|---|
| 217 | sepLen = strlen(sep);
|
|---|
| 218 | if (dwStrType & CERT_NAME_STR_NO_PLUS_FLAG)
|
|---|
| 219 | rdnSep = spaceSep;
|
|---|
| 220 | else
|
|---|
| 221 | rdnSep = plusSep;
|
|---|
| 222 | rdnSepLen = strlen(rdnSep);
|
|---|
| 223 | for (i = 0; (!psz || ret < csz) && i < info->cRDN; i++)
|
|---|
| 224 | {
|
|---|
| 225 | for (j = 0; (!psz || ret < csz) && j < rdn->cRDNAttr; j++)
|
|---|
| 226 | {
|
|---|
| 227 | DWORD chars;
|
|---|
| 228 | char prefixBuf[10]; /* big enough for GivenName */
|
|---|
| 229 | LPCSTR prefix = NULL;
|
|---|
| 230 |
|
|---|
| 231 | if ((dwStrType & 0x000000ff) == CERT_OID_NAME_STR)
|
|---|
| 232 | prefix = rdn->rgRDNAttr[j].pszObjId;
|
|---|
| 233 | else if ((dwStrType & 0x000000ff) == CERT_X500_NAME_STR)
|
|---|
| 234 | {
|
|---|
| 235 | PCCRYPT_OID_INFO oidInfo = CryptFindOIDInfo(
|
|---|
| 236 | CRYPT_OID_INFO_OID_KEY,
|
|---|
| 237 | rdn->rgRDNAttr[j].pszObjId,
|
|---|
| 238 | CRYPT_RDN_ATTR_OID_GROUP_ID);
|
|---|
| 239 |
|
|---|
| 240 | if (oidInfo)
|
|---|
| 241 | {
|
|---|
| 242 | WideCharToMultiByte(CP_ACP, 0, oidInfo->pwszName, -1,
|
|---|
| 243 | prefixBuf, sizeof(prefixBuf), NULL, NULL);
|
|---|
| 244 | prefix = prefixBuf;
|
|---|
| 245 | }
|
|---|
| 246 | else
|
|---|
| 247 | prefix = rdn->rgRDNAttr[j].pszObjId;
|
|---|
| 248 | }
|
|---|
| 249 | if (prefix)
|
|---|
| 250 | {
|
|---|
| 251 | /* - 1 is needed to account for the NULL terminator. */
|
|---|
| 252 | chars = CRYPT_AddPrefixA(prefix,
|
|---|
| 253 | psz ? psz + ret : NULL, psz ? csz - ret - 1 : 0);
|
|---|
| 254 | ret += chars;
|
|---|
| 255 | }
|
|---|
| 256 | /* FIXME: handle quoting */
|
|---|
| 257 | chars = CertRDNValueToStrA(
|
|---|
| 258 | rdn->rgRDNAttr[j].dwValueType,
|
|---|
| 259 | &rdn->rgRDNAttr[j].Value, psz ? psz + ret : NULL,
|
|---|
| 260 | psz ? csz - ret : 0);
|
|---|
| 261 | if (chars)
|
|---|
| 262 | ret += chars - 1;
|
|---|
| 263 | if (j < rdn->cRDNAttr - 1)
|
|---|
| 264 | {
|
|---|
| 265 | if (psz && ret < csz - rdnSepLen - 1)
|
|---|
| 266 | memcpy(psz + ret, rdnSep, rdnSepLen);
|
|---|
| 267 | ret += rdnSepLen;
|
|---|
| 268 | }
|
|---|
| 269 | }
|
|---|
| 270 | if (i < info->cRDN - 1)
|
|---|
| 271 | {
|
|---|
| 272 | if (psz && ret < csz - sepLen - 1)
|
|---|
| 273 | memcpy(psz + ret, sep, sepLen);
|
|---|
| 274 | ret += sepLen;
|
|---|
| 275 | }
|
|---|
| 276 | if(reverse) rdn--;
|
|---|
| 277 | else rdn++;
|
|---|
| 278 | }
|
|---|
| 279 | LocalFree(info);
|
|---|
| 280 | }
|
|---|
| 281 | if (psz && csz)
|
|---|
| 282 | {
|
|---|
| 283 | *(psz + ret) = '\0';
|
|---|
| 284 | ret++;
|
|---|
| 285 | }
|
|---|
| 286 | else
|
|---|
| 287 | ret++;
|
|---|
| 288 | TRACE("Returning %s\n", debugstr_a(psz));
|
|---|
| 289 | return ret;
|
|---|
| 290 | }
|
|---|
| 291 |
|
|---|
| 292 | /* Adds the prefix prefix to the wide-character string pointed to by psz,
|
|---|
| 293 | * followed by the character '='. Copies no more than csz characters. Returns
|
|---|
| 294 | * the number of characters copied. If psz is NULL, returns the number of
|
|---|
| 295 | * characters that would be copied.
|
|---|
| 296 | * Assumes the characters in prefix are ASCII (not multibyte characters.)
|
|---|
| 297 | */
|
|---|
| 298 | static DWORD CRYPT_AddPrefixAToW(LPCSTR prefix, LPWSTR psz, DWORD csz)
|
|---|
| 299 | {
|
|---|
| 300 | DWORD chars;
|
|---|
| 301 |
|
|---|
| 302 | TRACE("(%s, %p, %d)\n", debugstr_a(prefix), psz, csz);
|
|---|
| 303 |
|
|---|
| 304 | if (psz)
|
|---|
| 305 | {
|
|---|
| 306 | DWORD i;
|
|---|
| 307 |
|
|---|
| 308 | chars = min(strlen(prefix), csz);
|
|---|
| 309 | for (i = 0; i < chars; i++)
|
|---|
| 310 | *(psz + i) = prefix[i];
|
|---|
| 311 | *(psz + chars) = '=';
|
|---|
| 312 | chars++;
|
|---|
| 313 | }
|
|---|
| 314 | else
|
|---|
| 315 | chars = lstrlenA(prefix) + 1;
|
|---|
| 316 | return chars;
|
|---|
| 317 | }
|
|---|
| 318 |
|
|---|
| 319 | /* Adds the prefix prefix to the string pointed to by psz, followed by the
|
|---|
| 320 | * character '='. Copies no more than csz characters. Returns the number of
|
|---|
| 321 | * characters copied. If psz is NULL, returns the number of characters that
|
|---|
| 322 | * would be copied.
|
|---|
| 323 | */
|
|---|
| 324 | static DWORD CRYPT_AddPrefixW(LPCWSTR prefix, LPWSTR psz, DWORD csz)
|
|---|
| 325 | {
|
|---|
| 326 | DWORD chars;
|
|---|
| 327 |
|
|---|
| 328 | TRACE("(%s, %p, %d)\n", debugstr_w(prefix), psz, csz);
|
|---|
| 329 |
|
|---|
| 330 | if (psz)
|
|---|
| 331 | {
|
|---|
| 332 | chars = min(strlenW(prefix), csz);
|
|---|
| 333 | memcpy(psz, prefix, chars * sizeof(WCHAR));
|
|---|
| 334 | *(psz + chars) = '=';
|
|---|
| 335 | chars++;
|
|---|
| 336 | }
|
|---|
| 337 | else
|
|---|
| 338 | chars = lstrlenW(prefix) + 1;
|
|---|
| 339 | return chars;
|
|---|
| 340 | }
|
|---|
| 341 |
|
|---|
| 342 | static const WCHAR indent[] = { ' ',' ',' ',' ',' ',0 };
|
|---|
| 343 |
|
|---|
| 344 | DWORD cert_name_to_str_with_indent(DWORD dwCertEncodingType, DWORD indentLevel,
|
|---|
| 345 | PCERT_NAME_BLOB pName, DWORD dwStrType, LPWSTR psz, DWORD csz)
|
|---|
| 346 | {
|
|---|
| 347 | static const DWORD unsupportedFlags = CERT_NAME_STR_NO_QUOTING_FLAG |
|
|---|
| 348 | CERT_NAME_STR_ENABLE_T61_UNICODE_FLAG;
|
|---|
| 349 | static const WCHAR commaSep[] = { ',',' ',0 };
|
|---|
| 350 | static const WCHAR semiSep[] = { ';',' ',0 };
|
|---|
| 351 | static const WCHAR crlfSep[] = { '\r','\n',0 };
|
|---|
| 352 | static const WCHAR plusSep[] = { ' ','+',' ',0 };
|
|---|
| 353 | static const WCHAR spaceSep[] = { ' ',0 };
|
|---|
| 354 | DWORD ret = 0, bytes = 0;
|
|---|
| 355 | BOOL bRet;
|
|---|
| 356 | CERT_NAME_INFO *info;
|
|---|
| 357 |
|
|---|
| 358 | if (dwStrType & unsupportedFlags)
|
|---|
| 359 | FIXME("unsupported flags: %08x\n", dwStrType & unsupportedFlags);
|
|---|
| 360 |
|
|---|
| 361 | bRet = CryptDecodeObjectEx(dwCertEncodingType, X509_NAME, pName->pbData,
|
|---|
| 362 | pName->cbData, CRYPT_DECODE_ALLOC_FLAG, NULL, &info, &bytes);
|
|---|
| 363 | if (bRet)
|
|---|
| 364 | {
|
|---|
| 365 | DWORD i, j, sepLen, rdnSepLen;
|
|---|
| 366 | LPCWSTR sep, rdnSep;
|
|---|
| 367 | BOOL reverse = dwStrType & CERT_NAME_STR_REVERSE_FLAG;
|
|---|
| 368 | const CERT_RDN *rdn = info->rgRDN;
|
|---|
| 369 |
|
|---|
| 370 | if(reverse && info->cRDN > 1) rdn += (info->cRDN - 1);
|
|---|
| 371 |
|
|---|
| 372 | if (dwStrType & CERT_NAME_STR_SEMICOLON_FLAG)
|
|---|
| 373 | sep = semiSep;
|
|---|
| 374 | else if (dwStrType & CERT_NAME_STR_CRLF_FLAG)
|
|---|
| 375 | sep = crlfSep;
|
|---|
| 376 | else
|
|---|
| 377 | sep = commaSep;
|
|---|
| 378 | sepLen = lstrlenW(sep);
|
|---|
| 379 | if (dwStrType & CERT_NAME_STR_NO_PLUS_FLAG)
|
|---|
| 380 | rdnSep = spaceSep;
|
|---|
| 381 | else
|
|---|
| 382 | rdnSep = plusSep;
|
|---|
| 383 | rdnSepLen = lstrlenW(rdnSep);
|
|---|
| 384 | for (i = 0; (!psz || ret < csz) && i < info->cRDN; i++)
|
|---|
| 385 | {
|
|---|
| 386 | for (j = 0; (!psz || ret < csz) && j < rdn->cRDNAttr; j++)
|
|---|
| 387 | {
|
|---|
| 388 | DWORD chars;
|
|---|
| 389 | LPCSTR prefixA = NULL;
|
|---|
| 390 | LPCWSTR prefixW = NULL;
|
|---|
| 391 |
|
|---|
| 392 | if ((dwStrType & 0x000000ff) == CERT_OID_NAME_STR)
|
|---|
| 393 | prefixA = rdn->rgRDNAttr[j].pszObjId;
|
|---|
| 394 | else if ((dwStrType & 0x000000ff) == CERT_X500_NAME_STR)
|
|---|
| 395 | {
|
|---|
| 396 | PCCRYPT_OID_INFO oidInfo = CryptFindOIDInfo(
|
|---|
| 397 | CRYPT_OID_INFO_OID_KEY,
|
|---|
| 398 | rdn->rgRDNAttr[j].pszObjId,
|
|---|
| 399 | CRYPT_RDN_ATTR_OID_GROUP_ID);
|
|---|
| 400 |
|
|---|
| 401 | if (oidInfo)
|
|---|
| 402 | prefixW = oidInfo->pwszName;
|
|---|
| 403 | else
|
|---|
| 404 | prefixA = rdn->rgRDNAttr[j].pszObjId;
|
|---|
| 405 | }
|
|---|
| 406 | if (dwStrType & CERT_NAME_STR_CRLF_FLAG)
|
|---|
| 407 | {
|
|---|
| 408 | DWORD k;
|
|---|
| 409 |
|
|---|
| 410 | for (k = 0; k < indentLevel; k++)
|
|---|
| 411 | {
|
|---|
| 412 | if (psz)
|
|---|
| 413 | {
|
|---|
| 414 | chars = min(strlenW(indent), csz - ret - 1);
|
|---|
| 415 | memcpy(psz + ret, indent, chars * sizeof(WCHAR));
|
|---|
| 416 | }
|
|---|
| 417 | else
|
|---|
| 418 | chars = strlenW(indent);
|
|---|
| 419 | ret += chars;
|
|---|
| 420 | }
|
|---|
| 421 | }
|
|---|
| 422 | if (prefixW)
|
|---|
| 423 | {
|
|---|
| 424 | /* - 1 is needed to account for the NULL terminator. */
|
|---|
| 425 | chars = CRYPT_AddPrefixW(prefixW,
|
|---|
| 426 | psz ? psz + ret : NULL, psz ? csz - ret - 1 : 0);
|
|---|
| 427 | ret += chars;
|
|---|
| 428 | }
|
|---|
| 429 | else if (prefixA)
|
|---|
| 430 | {
|
|---|
| 431 | /* - 1 is needed to account for the NULL terminator. */
|
|---|
| 432 | chars = CRYPT_AddPrefixAToW(prefixA,
|
|---|
| 433 | psz ? psz + ret : NULL, psz ? csz - ret - 1 : 0);
|
|---|
| 434 | ret += chars;
|
|---|
| 435 | }
|
|---|
| 436 | /* FIXME: handle quoting */
|
|---|
| 437 | chars = CertRDNValueToStrW(
|
|---|
| 438 | rdn->rgRDNAttr[j].dwValueType,
|
|---|
| 439 | &rdn->rgRDNAttr[j].Value, psz ? psz + ret : NULL,
|
|---|
| 440 | psz ? csz - ret : 0);
|
|---|
| 441 | if (chars)
|
|---|
| 442 | ret += chars - 1;
|
|---|
| 443 | if (j < rdn->cRDNAttr - 1)
|
|---|
| 444 | {
|
|---|
| 445 | if (psz && ret < csz - rdnSepLen - 1)
|
|---|
| 446 | memcpy(psz + ret, rdnSep, rdnSepLen * sizeof(WCHAR));
|
|---|
| 447 | ret += rdnSepLen;
|
|---|
| 448 | }
|
|---|
| 449 | }
|
|---|
| 450 | if (i < info->cRDN - 1)
|
|---|
| 451 | {
|
|---|
| 452 | if (psz && ret < csz - sepLen - 1)
|
|---|
| 453 | memcpy(psz + ret, sep, sepLen * sizeof(WCHAR));
|
|---|
| 454 | ret += sepLen;
|
|---|
| 455 | }
|
|---|
| 456 | if(reverse) rdn--;
|
|---|
| 457 | else rdn++;
|
|---|
| 458 | }
|
|---|
| 459 | LocalFree(info);
|
|---|
| 460 | }
|
|---|
| 461 | if (psz && csz)
|
|---|
| 462 | {
|
|---|
| 463 | *(psz + ret) = '\0';
|
|---|
| 464 | ret++;
|
|---|
| 465 | }
|
|---|
| 466 | else
|
|---|
| 467 | ret++;
|
|---|
| 468 | return ret;
|
|---|
| 469 | }
|
|---|
| 470 |
|
|---|
| 471 | DWORD WINAPI CertNameToStrW(DWORD dwCertEncodingType, PCERT_NAME_BLOB pName,
|
|---|
| 472 | DWORD dwStrType, LPWSTR psz, DWORD csz)
|
|---|
| 473 | {
|
|---|
| 474 | BOOL ret;
|
|---|
| 475 |
|
|---|
| 476 | TRACE("(%d, %p, %08x, %p, %d)\n", dwCertEncodingType, pName, dwStrType,
|
|---|
| 477 | psz, csz);
|
|---|
| 478 |
|
|---|
| 479 | ret = cert_name_to_str_with_indent(dwCertEncodingType, 0, pName, dwStrType,
|
|---|
| 480 | psz, csz);
|
|---|
| 481 | TRACE("Returning %s\n", debugstr_w(psz));
|
|---|
| 482 | return ret;
|
|---|
| 483 | }
|
|---|
| 484 |
|
|---|
| 485 | BOOL WINAPI CertStrToNameA(DWORD dwCertEncodingType, LPCSTR pszX500,
|
|---|
| 486 | DWORD dwStrType, void *pvReserved, BYTE *pbEncoded, DWORD *pcbEncoded,
|
|---|
| 487 | LPCSTR *ppszError)
|
|---|
| 488 | {
|
|---|
| 489 | BOOL ret;
|
|---|
| 490 | int len;
|
|---|
| 491 |
|
|---|
| 492 | TRACE("(%08x, %s, %08x, %p, %p, %p, %p)\n", dwCertEncodingType,
|
|---|
| 493 | debugstr_a(pszX500), dwStrType, pvReserved, pbEncoded, pcbEncoded,
|
|---|
| 494 | ppszError);
|
|---|
| 495 |
|
|---|
| 496 | len = MultiByteToWideChar(CP_ACP, 0, pszX500, -1, NULL, 0);
|
|---|
| 497 | if (len)
|
|---|
| 498 | {
|
|---|
| 499 | LPWSTR x500, errorStr;
|
|---|
| 500 |
|
|---|
| 501 | if ((x500 = CryptMemAlloc(len * sizeof(WCHAR))))
|
|---|
| 502 | {
|
|---|
| 503 | MultiByteToWideChar(CP_ACP, 0, pszX500, -1, x500, len);
|
|---|
| 504 | ret = CertStrToNameW(dwCertEncodingType, x500, dwStrType,
|
|---|
| 505 | pvReserved, pbEncoded, pcbEncoded,
|
|---|
| 506 | ppszError ? (LPCWSTR *)&errorStr : NULL);
|
|---|
| 507 | if (ppszError)
|
|---|
| 508 | {
|
|---|
| 509 | if (!ret)
|
|---|
| 510 | {
|
|---|
| 511 | LONG i;
|
|---|
| 512 |
|
|---|
| 513 | *ppszError = pszX500;
|
|---|
| 514 | for (i = 0; i < errorStr - x500; i++)
|
|---|
| 515 | *ppszError = CharNextA(*ppszError);
|
|---|
| 516 | }
|
|---|
| 517 | else
|
|---|
| 518 | *ppszError = NULL;
|
|---|
| 519 | }
|
|---|
| 520 | CryptMemFree(x500);
|
|---|
| 521 | }
|
|---|
| 522 | else
|
|---|
| 523 | {
|
|---|
| 524 | SetLastError(ERROR_OUTOFMEMORY);
|
|---|
| 525 | ret = FALSE;
|
|---|
| 526 | }
|
|---|
| 527 | }
|
|---|
| 528 | else
|
|---|
| 529 | {
|
|---|
| 530 | SetLastError(CRYPT_E_INVALID_X500_STRING);
|
|---|
| 531 | if (ppszError)
|
|---|
| 532 | *ppszError = pszX500;
|
|---|
| 533 | ret = FALSE;
|
|---|
| 534 | }
|
|---|
| 535 | return ret;
|
|---|
| 536 | }
|
|---|
| 537 |
|
|---|
| 538 | struct KeynameKeeper
|
|---|
| 539 | {
|
|---|
| 540 | WCHAR buf[10]; /* big enough for L"GivenName" */
|
|---|
| 541 | LPWSTR keyName; /* usually = buf, but may be allocated */
|
|---|
| 542 | DWORD keyLen;
|
|---|
| 543 | };
|
|---|
| 544 |
|
|---|
| 545 | static void CRYPT_InitializeKeynameKeeper(struct KeynameKeeper *keeper)
|
|---|
| 546 | {
|
|---|
| 547 | keeper->keyName = keeper->buf;
|
|---|
| 548 | keeper->keyLen = sizeof(keeper->buf) / sizeof(keeper->buf[0]);
|
|---|
| 549 | }
|
|---|
| 550 |
|
|---|
| 551 | static void CRYPT_FreeKeynameKeeper(struct KeynameKeeper *keeper)
|
|---|
| 552 | {
|
|---|
| 553 | if (keeper->keyName != keeper->buf)
|
|---|
| 554 | CryptMemFree(keeper->keyName);
|
|---|
| 555 | }
|
|---|
| 556 |
|
|---|
| 557 | struct X500TokenW
|
|---|
| 558 | {
|
|---|
| 559 | LPCWSTR start;
|
|---|
| 560 | LPCWSTR end;
|
|---|
| 561 | };
|
|---|
| 562 |
|
|---|
| 563 | static void CRYPT_KeynameKeeperFromTokenW(struct KeynameKeeper *keeper,
|
|---|
| 564 | const struct X500TokenW *key)
|
|---|
| 565 | {
|
|---|
| 566 | DWORD len = key->end - key->start;
|
|---|
| 567 |
|
|---|
| 568 | if (len > keeper->keyLen)
|
|---|
| 569 | {
|
|---|
| 570 | if (keeper->keyName == keeper->buf)
|
|---|
| 571 | keeper->keyName = CryptMemAlloc(len * sizeof(WCHAR));
|
|---|
| 572 | else
|
|---|
| 573 | keeper->keyName = CryptMemRealloc(keeper->keyName,
|
|---|
| 574 | len * sizeof(WCHAR));
|
|---|
| 575 | keeper->keyLen = len;
|
|---|
| 576 | }
|
|---|
| 577 | memcpy(keeper->keyName, key->start, (key->end - key->start) *
|
|---|
| 578 | sizeof(WCHAR));
|
|---|
| 579 | keeper->keyName[len] = '\0';
|
|---|
| 580 | TRACE("Keyname is %s\n", debugstr_w(keeper->keyName));
|
|---|
| 581 | }
|
|---|
| 582 |
|
|---|
| 583 | static BOOL CRYPT_GetNextKeyW(LPCWSTR str, struct X500TokenW *token,
|
|---|
| 584 | LPCWSTR *ppszError)
|
|---|
| 585 | {
|
|---|
| 586 | BOOL ret = TRUE;
|
|---|
| 587 |
|
|---|
| 588 | while (*str && isspaceW(*str))
|
|---|
| 589 | str++;
|
|---|
| 590 | if (*str)
|
|---|
| 591 | {
|
|---|
| 592 | token->start = str;
|
|---|
| 593 | while (*str && *str != '=' && !isspaceW(*str))
|
|---|
| 594 | str++;
|
|---|
| 595 | if (*str && (*str == '=' || isspaceW(*str)))
|
|---|
| 596 | token->end = str;
|
|---|
| 597 | else
|
|---|
| 598 | {
|
|---|
| 599 | TRACE("missing equals char at %s\n", debugstr_w(token->start));
|
|---|
| 600 | if (ppszError)
|
|---|
| 601 | *ppszError = token->start;
|
|---|
| 602 | SetLastError(CRYPT_E_INVALID_X500_STRING);
|
|---|
| 603 | ret = FALSE;
|
|---|
| 604 | }
|
|---|
| 605 | }
|
|---|
| 606 | else
|
|---|
| 607 | token->start = NULL;
|
|---|
| 608 | return ret;
|
|---|
| 609 | }
|
|---|
| 610 |
|
|---|
| 611 | /* Assumes separators are characters in the 0-255 range */
|
|---|
| 612 | static BOOL CRYPT_GetNextValueW(LPCWSTR str, DWORD dwFlags, LPCWSTR separators,
|
|---|
| 613 | struct X500TokenW *token, LPCWSTR *ppszError)
|
|---|
| 614 | {
|
|---|
| 615 | BOOL ret = TRUE;
|
|---|
| 616 |
|
|---|
| 617 | TRACE("(%s, %s, %p, %p)\n", debugstr_w(str), debugstr_w(separators), token,
|
|---|
| 618 | ppszError);
|
|---|
| 619 |
|
|---|
| 620 | while (*str && isspaceW(*str))
|
|---|
| 621 | str++;
|
|---|
| 622 | if (*str)
|
|---|
| 623 | {
|
|---|
| 624 | token->start = str;
|
|---|
| 625 | if (!(dwFlags & CERT_NAME_STR_NO_QUOTING_FLAG) && *str == '"')
|
|---|
| 626 | {
|
|---|
| 627 | token->end = NULL;
|
|---|
| 628 | str++;
|
|---|
| 629 | while (!token->end && ret)
|
|---|
| 630 | {
|
|---|
| 631 | while (*str && *str != '"')
|
|---|
| 632 | str++;
|
|---|
| 633 | if (*str == '"')
|
|---|
| 634 | {
|
|---|
| 635 | if (*(str + 1) != '"')
|
|---|
| 636 | token->end = str + 1;
|
|---|
| 637 | else
|
|---|
| 638 | str += 2;
|
|---|
| 639 | }
|
|---|
| 640 | else
|
|---|
| 641 | {
|
|---|
| 642 | TRACE("unterminated quote at %s\n", debugstr_w(str));
|
|---|
| 643 | if (ppszError)
|
|---|
| 644 | *ppszError = str;
|
|---|
| 645 | SetLastError(CRYPT_E_INVALID_X500_STRING);
|
|---|
| 646 | ret = FALSE;
|
|---|
| 647 | }
|
|---|
| 648 | }
|
|---|
| 649 | }
|
|---|
| 650 | else
|
|---|
| 651 | {
|
|---|
| 652 | WCHAR map[256] = { 0 };
|
|---|
| 653 |
|
|---|
| 654 | while (*separators)
|
|---|
| 655 | map[*separators++] = 1;
|
|---|
| 656 | while (*str && (*str >= 0xff || !map[*str]))
|
|---|
| 657 | str++;
|
|---|
| 658 | token->end = str;
|
|---|
| 659 | }
|
|---|
| 660 | }
|
|---|
| 661 | else
|
|---|
| 662 | {
|
|---|
| 663 | TRACE("missing value at %s\n", debugstr_w(str));
|
|---|
| 664 | if (ppszError)
|
|---|
| 665 | *ppszError = str;
|
|---|
| 666 | SetLastError(CRYPT_E_INVALID_X500_STRING);
|
|---|
| 667 | ret = FALSE;
|
|---|
| 668 | }
|
|---|
| 669 | return ret;
|
|---|
| 670 | }
|
|---|
| 671 |
|
|---|
| 672 | /* Encodes the string represented by value as the string type type into the
|
|---|
| 673 | * CERT_NAME_BLOB output. If there is an error and ppszError is not NULL,
|
|---|
| 674 | * *ppszError is set to the first failing character. If there is no error,
|
|---|
| 675 | * output's pbData must be freed with LocalFree.
|
|---|
| 676 | */
|
|---|
| 677 | static BOOL CRYPT_EncodeValueWithType(DWORD dwCertEncodingType,
|
|---|
| 678 | const struct X500TokenW *value, PCERT_NAME_BLOB output, DWORD type,
|
|---|
| 679 | LPCWSTR *ppszError)
|
|---|
| 680 | {
|
|---|
| 681 | CERT_NAME_VALUE nameValue = { type, { 0, NULL } };
|
|---|
| 682 | BOOL ret = TRUE;
|
|---|
| 683 |
|
|---|
| 684 | if (value->end > value->start)
|
|---|
| 685 | {
|
|---|
| 686 | nameValue.Value.pbData = CryptMemAlloc((value->end - value->start) *
|
|---|
| 687 | sizeof(WCHAR));
|
|---|
| 688 | if (!nameValue.Value.pbData)
|
|---|
| 689 | {
|
|---|
| 690 | SetLastError(ERROR_OUTOFMEMORY);
|
|---|
| 691 | ret = FALSE;
|
|---|
| 692 | }
|
|---|
| 693 | }
|
|---|
| 694 | if (ret)
|
|---|
| 695 | {
|
|---|
| 696 | if (value->end > value->start)
|
|---|
| 697 | {
|
|---|
| 698 | LONG i;
|
|---|
| 699 | LPWSTR ptr = (LPWSTR)nameValue.Value.pbData;
|
|---|
| 700 |
|
|---|
| 701 | for (i = 0; i < value->end - value->start; i++)
|
|---|
| 702 | {
|
|---|
| 703 | *ptr++ = value->start[i];
|
|---|
| 704 | if (value->start[i] == '"')
|
|---|
| 705 | i++;
|
|---|
| 706 | }
|
|---|
| 707 | nameValue.Value.cbData = (LPBYTE)ptr - nameValue.Value.pbData;
|
|---|
| 708 | }
|
|---|
| 709 | ret = CryptEncodeObjectEx(dwCertEncodingType, X509_UNICODE_NAME_VALUE,
|
|---|
| 710 | &nameValue, CRYPT_ENCODE_ALLOC_FLAG, NULL, &output->pbData,
|
|---|
| 711 | &output->cbData);
|
|---|
| 712 | if (!ret && ppszError)
|
|---|
| 713 | {
|
|---|
| 714 | if (type == CERT_RDN_NUMERIC_STRING &&
|
|---|
| 715 | GetLastError() == CRYPT_E_INVALID_NUMERIC_STRING)
|
|---|
| 716 | *ppszError = value->start + output->cbData;
|
|---|
| 717 | else if (type == CERT_RDN_PRINTABLE_STRING &&
|
|---|
| 718 | GetLastError() == CRYPT_E_INVALID_PRINTABLE_STRING)
|
|---|
| 719 | *ppszError = value->start + output->cbData;
|
|---|
| 720 | else if (type == CERT_RDN_IA5_STRING &&
|
|---|
| 721 | GetLastError() == CRYPT_E_INVALID_IA5_STRING)
|
|---|
| 722 | *ppszError = value->start + output->cbData;
|
|---|
| 723 | }
|
|---|
| 724 | CryptMemFree(nameValue.Value.pbData);
|
|---|
| 725 | }
|
|---|
| 726 | return ret;
|
|---|
| 727 | }
|
|---|
| 728 |
|
|---|
| 729 | static BOOL CRYPT_EncodeValue(DWORD dwCertEncodingType,
|
|---|
| 730 | const struct X500TokenW *value, PCERT_NAME_BLOB output, const DWORD *types,
|
|---|
| 731 | LPCWSTR *ppszError)
|
|---|
| 732 | {
|
|---|
| 733 | DWORD i;
|
|---|
| 734 | BOOL ret;
|
|---|
| 735 |
|
|---|
| 736 | ret = FALSE;
|
|---|
| 737 | for (i = 0; !ret && types[i]; i++)
|
|---|
| 738 | ret = CRYPT_EncodeValueWithType(dwCertEncodingType, value, output,
|
|---|
| 739 | types[i], ppszError);
|
|---|
| 740 | return ret;
|
|---|
| 741 | }
|
|---|
| 742 |
|
|---|
| 743 | static BOOL CRYPT_ValueToRDN(DWORD dwCertEncodingType, PCERT_NAME_INFO info,
|
|---|
| 744 | PCCRYPT_OID_INFO keyOID, struct X500TokenW *value, LPCWSTR *ppszError)
|
|---|
| 745 | {
|
|---|
| 746 | BOOL ret = FALSE;
|
|---|
| 747 |
|
|---|
| 748 | TRACE("OID %s, value %s\n", debugstr_a(keyOID->pszOID),
|
|---|
| 749 | debugstr_wn(value->start, value->end - value->start));
|
|---|
| 750 |
|
|---|
| 751 | if (!info->rgRDN)
|
|---|
| 752 | info->rgRDN = CryptMemAlloc(sizeof(CERT_RDN));
|
|---|
| 753 | else
|
|---|
| 754 | info->rgRDN = CryptMemRealloc(info->rgRDN,
|
|---|
| 755 | (info->cRDN + 1) * sizeof(CERT_RDN));
|
|---|
| 756 | if (info->rgRDN)
|
|---|
| 757 | {
|
|---|
| 758 | /* FIXME: support multiple RDN attrs */
|
|---|
| 759 | info->rgRDN[info->cRDN].rgRDNAttr =
|
|---|
| 760 | CryptMemAlloc(sizeof(CERT_RDN_ATTR));
|
|---|
| 761 | if (info->rgRDN[info->cRDN].rgRDNAttr)
|
|---|
| 762 | {
|
|---|
| 763 | static const DWORD defaultTypes[] = { CERT_RDN_PRINTABLE_STRING,
|
|---|
| 764 | CERT_RDN_BMP_STRING, 0 };
|
|---|
| 765 | const DWORD *types;
|
|---|
| 766 |
|
|---|
| 767 | info->rgRDN[info->cRDN].cRDNAttr = 1;
|
|---|
| 768 | info->rgRDN[info->cRDN].rgRDNAttr[0].pszObjId =
|
|---|
| 769 | (LPSTR)keyOID->pszOID;
|
|---|
| 770 | info->rgRDN[info->cRDN].rgRDNAttr[0].dwValueType =
|
|---|
| 771 | CERT_RDN_ENCODED_BLOB;
|
|---|
| 772 | if (keyOID->ExtraInfo.cbData)
|
|---|
| 773 | types = (const DWORD *)keyOID->ExtraInfo.pbData;
|
|---|
| 774 | else
|
|---|
| 775 | types = defaultTypes;
|
|---|
| 776 |
|
|---|
| 777 | /* Remove surrounding quotes */
|
|---|
| 778 | if (value->start[0] == '"')
|
|---|
| 779 | {
|
|---|
| 780 | value->start++;
|
|---|
| 781 | value->end--;
|
|---|
| 782 | }
|
|---|
| 783 | ret = CRYPT_EncodeValue(dwCertEncodingType, value,
|
|---|
| 784 | &info->rgRDN[info->cRDN].rgRDNAttr[0].Value, types, ppszError);
|
|---|
| 785 | }
|
|---|
| 786 | else
|
|---|
| 787 | SetLastError(ERROR_OUTOFMEMORY);
|
|---|
| 788 | info->cRDN++;
|
|---|
| 789 | }
|
|---|
| 790 | else
|
|---|
| 791 | SetLastError(ERROR_OUTOFMEMORY);
|
|---|
| 792 | return ret;
|
|---|
| 793 | }
|
|---|
| 794 |
|
|---|
| 795 | BOOL WINAPI CertStrToNameW(DWORD dwCertEncodingType, LPCWSTR pszX500,
|
|---|
| 796 | DWORD dwStrType, void *pvReserved, BYTE *pbEncoded, DWORD *pcbEncoded,
|
|---|
| 797 | LPCWSTR *ppszError)
|
|---|
| 798 | {
|
|---|
| 799 | CERT_NAME_INFO info = { 0, NULL };
|
|---|
| 800 | LPCWSTR str;
|
|---|
| 801 | struct KeynameKeeper keeper;
|
|---|
| 802 | DWORD i;
|
|---|
| 803 | BOOL ret = TRUE;
|
|---|
| 804 |
|
|---|
| 805 | TRACE("(%08x, %s, %08x, %p, %p, %p, %p)\n", dwCertEncodingType,
|
|---|
| 806 | debugstr_w(pszX500), dwStrType, pvReserved, pbEncoded, pcbEncoded,
|
|---|
| 807 | ppszError);
|
|---|
| 808 |
|
|---|
| 809 | CRYPT_InitializeKeynameKeeper(&keeper);
|
|---|
| 810 | str = pszX500;
|
|---|
| 811 | while (str && *str && ret)
|
|---|
| 812 | {
|
|---|
| 813 | struct X500TokenW token;
|
|---|
| 814 |
|
|---|
| 815 | ret = CRYPT_GetNextKeyW(str, &token, ppszError);
|
|---|
| 816 | if (ret && token.start)
|
|---|
| 817 | {
|
|---|
| 818 | PCCRYPT_OID_INFO keyOID;
|
|---|
| 819 |
|
|---|
| 820 | CRYPT_KeynameKeeperFromTokenW(&keeper, &token);
|
|---|
| 821 | keyOID = CryptFindOIDInfo(CRYPT_OID_INFO_NAME_KEY, keeper.keyName,
|
|---|
| 822 | CRYPT_RDN_ATTR_OID_GROUP_ID);
|
|---|
| 823 | if (!keyOID)
|
|---|
| 824 | {
|
|---|
| 825 | if (ppszError)
|
|---|
| 826 | *ppszError = token.start;
|
|---|
| 827 | SetLastError(CRYPT_E_INVALID_X500_STRING);
|
|---|
| 828 | ret = FALSE;
|
|---|
| 829 | }
|
|---|
| 830 | else
|
|---|
| 831 | {
|
|---|
| 832 | str = token.end;
|
|---|
| 833 | while (isspace(*str))
|
|---|
| 834 | str++;
|
|---|
| 835 | if (*str != '=')
|
|---|
| 836 | {
|
|---|
| 837 | if (ppszError)
|
|---|
| 838 | *ppszError = str;
|
|---|
| 839 | SetLastError(CRYPT_E_INVALID_X500_STRING);
|
|---|
| 840 | ret = FALSE;
|
|---|
| 841 | }
|
|---|
| 842 | else
|
|---|
| 843 | {
|
|---|
| 844 | static const WCHAR commaSep[] = { ',',0 };
|
|---|
| 845 | static const WCHAR semiSep[] = { ';',0 };
|
|---|
| 846 | static const WCHAR crlfSep[] = { '\r','\n',0 };
|
|---|
| 847 | static const WCHAR allSeps[] = { ',',';','\r','\n',0 };
|
|---|
| 848 | LPCWSTR sep;
|
|---|
| 849 |
|
|---|
| 850 | str++;
|
|---|
| 851 | if (dwStrType & CERT_NAME_STR_COMMA_FLAG)
|
|---|
| 852 | sep = commaSep;
|
|---|
| 853 | else if (dwStrType & CERT_NAME_STR_SEMICOLON_FLAG)
|
|---|
| 854 | sep = semiSep;
|
|---|
| 855 | else if (dwStrType & CERT_NAME_STR_CRLF_FLAG)
|
|---|
| 856 | sep = crlfSep;
|
|---|
| 857 | else
|
|---|
| 858 | sep = allSeps;
|
|---|
| 859 | ret = CRYPT_GetNextValueW(str, dwStrType, sep, &token,
|
|---|
| 860 | ppszError);
|
|---|
| 861 | if (ret)
|
|---|
| 862 | {
|
|---|
| 863 | str = token.end;
|
|---|
| 864 | ret = CRYPT_ValueToRDN(dwCertEncodingType, &info,
|
|---|
| 865 | keyOID, &token, ppszError);
|
|---|
| 866 | }
|
|---|
| 867 | }
|
|---|
| 868 | }
|
|---|
| 869 | }
|
|---|
| 870 | }
|
|---|
| 871 | CRYPT_FreeKeynameKeeper(&keeper);
|
|---|
| 872 | if (ret)
|
|---|
| 873 | {
|
|---|
| 874 | if (ppszError)
|
|---|
| 875 | *ppszError = NULL;
|
|---|
| 876 | ret = CryptEncodeObjectEx(dwCertEncodingType, X509_NAME, &info,
|
|---|
| 877 | 0, NULL, pbEncoded, pcbEncoded);
|
|---|
| 878 | }
|
|---|
| 879 | for (i = 0; i < info.cRDN; i++)
|
|---|
| 880 | {
|
|---|
| 881 | DWORD j;
|
|---|
| 882 |
|
|---|
| 883 | for (j = 0; j < info.rgRDN[i].cRDNAttr; j++)
|
|---|
| 884 | LocalFree(info.rgRDN[i].rgRDNAttr[j].Value.pbData);
|
|---|
| 885 | CryptMemFree(info.rgRDN[i].rgRDNAttr);
|
|---|
| 886 | }
|
|---|
| 887 | CryptMemFree(info.rgRDN);
|
|---|
| 888 | return ret;
|
|---|
| 889 | }
|
|---|
| 890 |
|
|---|
| 891 | DWORD WINAPI CertGetNameStringA(PCCERT_CONTEXT pCertContext, DWORD dwType,
|
|---|
| 892 | DWORD dwFlags, void *pvTypePara, LPSTR pszNameString, DWORD cchNameString)
|
|---|
| 893 | {
|
|---|
| 894 | DWORD ret;
|
|---|
| 895 |
|
|---|
| 896 | TRACE("(%p, %d, %08x, %p, %p, %d)\n", pCertContext, dwType, dwFlags,
|
|---|
| 897 | pvTypePara, pszNameString, cchNameString);
|
|---|
| 898 |
|
|---|
| 899 | if (pszNameString)
|
|---|
| 900 | {
|
|---|
| 901 | LPWSTR wideName;
|
|---|
| 902 | DWORD nameLen;
|
|---|
| 903 |
|
|---|
| 904 | nameLen = CertGetNameStringW(pCertContext, dwType, dwFlags, pvTypePara,
|
|---|
| 905 | NULL, 0);
|
|---|
| 906 | wideName = CryptMemAlloc(nameLen * sizeof(WCHAR));
|
|---|
| 907 | if (wideName)
|
|---|
| 908 | {
|
|---|
| 909 | CertGetNameStringW(pCertContext, dwType, dwFlags, pvTypePara,
|
|---|
| 910 | wideName, nameLen);
|
|---|
| 911 | nameLen = WideCharToMultiByte(CP_ACP, 0, wideName, nameLen,
|
|---|
| 912 | pszNameString, cchNameString, NULL, NULL);
|
|---|
| 913 | if (nameLen <= cchNameString)
|
|---|
| 914 | ret = nameLen;
|
|---|
| 915 | else
|
|---|
| 916 | {
|
|---|
| 917 | pszNameString[cchNameString - 1] = '\0';
|
|---|
| 918 | ret = cchNameString;
|
|---|
| 919 | }
|
|---|
| 920 | CryptMemFree(wideName);
|
|---|
| 921 | }
|
|---|
| 922 | else
|
|---|
| 923 | {
|
|---|
| 924 | *pszNameString = '\0';
|
|---|
| 925 | ret = 1;
|
|---|
| 926 | }
|
|---|
| 927 | }
|
|---|
| 928 | else
|
|---|
| 929 | ret = CertGetNameStringW(pCertContext, dwType, dwFlags, pvTypePara,
|
|---|
| 930 | NULL, 0);
|
|---|
| 931 | return ret;
|
|---|
| 932 | }
|
|---|
| 933 |
|
|---|
| 934 | DWORD WINAPI CertGetNameStringW(PCCERT_CONTEXT pCertContext, DWORD dwType,
|
|---|
| 935 | DWORD dwFlags, void *pvTypePara, LPWSTR pszNameString, DWORD cchNameString)
|
|---|
| 936 | {
|
|---|
| 937 | DWORD ret;
|
|---|
| 938 | PCERT_NAME_BLOB name;
|
|---|
| 939 | LPCSTR altNameOID;
|
|---|
| 940 |
|
|---|
| 941 | TRACE("(%p, %d, %08x, %p, %p, %d)\n", pCertContext, dwType,
|
|---|
| 942 | dwFlags, pvTypePara, pszNameString, cchNameString);
|
|---|
| 943 |
|
|---|
| 944 | if (dwFlags & CERT_NAME_ISSUER_FLAG)
|
|---|
| 945 | {
|
|---|
| 946 | name = &pCertContext->pCertInfo->Issuer;
|
|---|
| 947 | altNameOID = szOID_ISSUER_ALT_NAME;
|
|---|
| 948 | }
|
|---|
| 949 | else
|
|---|
| 950 | {
|
|---|
| 951 | name = &pCertContext->pCertInfo->Subject;
|
|---|
| 952 | altNameOID = szOID_SUBJECT_ALT_NAME;
|
|---|
| 953 | }
|
|---|
| 954 |
|
|---|
| 955 | switch (dwType)
|
|---|
| 956 | {
|
|---|
| 957 | case CERT_NAME_SIMPLE_DISPLAY_TYPE:
|
|---|
| 958 | {
|
|---|
| 959 | static const LPCSTR simpleAttributeOIDs[] = { szOID_COMMON_NAME,
|
|---|
| 960 | szOID_ORGANIZATIONAL_UNIT_NAME, szOID_ORGANIZATION_NAME,
|
|---|
| 961 | szOID_RSA_emailAddr };
|
|---|
| 962 | CERT_NAME_INFO *info = NULL;
|
|---|
| 963 | PCERT_RDN_ATTR nameAttr = NULL;
|
|---|
| 964 | DWORD bytes = 0, i;
|
|---|
| 965 |
|
|---|
| 966 | if (CryptDecodeObjectEx(pCertContext->dwCertEncodingType, X509_NAME,
|
|---|
| 967 | name->pbData, name->cbData, CRYPT_DECODE_ALLOC_FLAG, NULL, &info,
|
|---|
| 968 | &bytes))
|
|---|
| 969 | {
|
|---|
| 970 | for (i = 0; !nameAttr && i < sizeof(simpleAttributeOIDs) /
|
|---|
| 971 | sizeof(simpleAttributeOIDs[0]); i++)
|
|---|
| 972 | nameAttr = CertFindRDNAttr(simpleAttributeOIDs[i], info);
|
|---|
| 973 | }
|
|---|
| 974 | if (!nameAttr)
|
|---|
| 975 | {
|
|---|
| 976 | PCERT_EXTENSION ext = CertFindExtension(altNameOID,
|
|---|
| 977 | pCertContext->pCertInfo->cExtension,
|
|---|
| 978 | pCertContext->pCertInfo->rgExtension);
|
|---|
| 979 |
|
|---|
| 980 | if (ext)
|
|---|
| 981 | {
|
|---|
| 982 | for (i = 0; !nameAttr && i < sizeof(simpleAttributeOIDs) /
|
|---|
| 983 | sizeof(simpleAttributeOIDs[0]); i++)
|
|---|
| 984 | nameAttr = CertFindRDNAttr(simpleAttributeOIDs[i], info);
|
|---|
| 985 | if (!nameAttr)
|
|---|
| 986 | {
|
|---|
| 987 | /* FIXME: gotta then look for a rfc822Name choice in ext.
|
|---|
| 988 | * Failing that, look for the first attribute.
|
|---|
| 989 | */
|
|---|
| 990 | FIXME("CERT_NAME_SIMPLE_DISPLAY_TYPE: stub\n");
|
|---|
| 991 | }
|
|---|
| 992 | }
|
|---|
| 993 | }
|
|---|
| 994 | if (nameAttr)
|
|---|
| 995 | ret = CertRDNValueToStrW(nameAttr->dwValueType, &nameAttr->Value,
|
|---|
| 996 | pszNameString, cchNameString);
|
|---|
| 997 | else
|
|---|
| 998 | ret = 0;
|
|---|
| 999 | if (info)
|
|---|
| 1000 | LocalFree(info);
|
|---|
| 1001 | break;
|
|---|
| 1002 | }
|
|---|
| 1003 | case CERT_NAME_FRIENDLY_DISPLAY_TYPE:
|
|---|
| 1004 | {
|
|---|
| 1005 | DWORD cch = cchNameString;
|
|---|
| 1006 |
|
|---|
| 1007 | if (CertGetCertificateContextProperty(pCertContext,
|
|---|
| 1008 | CERT_FRIENDLY_NAME_PROP_ID, pszNameString, &cch))
|
|---|
| 1009 | ret = cch;
|
|---|
| 1010 | else
|
|---|
| 1011 | ret = CertGetNameStringW(pCertContext,
|
|---|
| 1012 | CERT_NAME_SIMPLE_DISPLAY_TYPE, dwFlags, pvTypePara, pszNameString,
|
|---|
| 1013 | cchNameString);
|
|---|
| 1014 | break;
|
|---|
| 1015 | }
|
|---|
| 1016 | default:
|
|---|
| 1017 | FIXME("unimplemented for type %d\n", dwType);
|
|---|
| 1018 | ret = 0;
|
|---|
| 1019 | }
|
|---|
| 1020 | return ret;
|
|---|
| 1021 | }
|
|---|