2 * Copyright 2006 Juan Lang for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #define NONAMELESSUNION
27 #include "wine/debug.h"
28 #include "wine/unicode.h"
29 #include "crypt32_private.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(crypt
);
33 DWORD WINAPI
CertRDNValueToStrA(DWORD dwValueType
, PCERT_RDN_VALUE_BLOB pValue
,
38 TRACE("(%d, %p, %p, %d)\n", dwValueType
, pValue
, psz
, csz
);
42 case CERT_RDN_ANY_TYPE
:
44 case CERT_RDN_NUMERIC_STRING
:
45 case CERT_RDN_PRINTABLE_STRING
:
46 case CERT_RDN_TELETEX_STRING
:
47 case CERT_RDN_VIDEOTEX_STRING
:
48 case CERT_RDN_IA5_STRING
:
49 case CERT_RDN_GRAPHIC_STRING
:
50 case CERT_RDN_VISIBLE_STRING
:
51 case CERT_RDN_GENERAL_STRING
:
57 DWORD chars
= min(len
, csz
- 1);
61 memcpy(psz
, pValue
->pbData
, chars
);
67 case CERT_RDN_BMP_STRING
:
68 case CERT_RDN_UTF8_STRING
:
69 len
= WideCharToMultiByte(CP_ACP
, 0, (LPCWSTR
)pValue
->pbData
,
70 pValue
->cbData
/ sizeof(WCHAR
), NULL
, 0, NULL
, NULL
);
75 DWORD chars
= min(pValue
->cbData
/ sizeof(WCHAR
), csz
- 1);
79 ret
= WideCharToMultiByte(CP_ACP
, 0, (LPCWSTR
)pValue
->pbData
,
80 chars
, psz
, csz
- 1, NULL
, NULL
);
86 FIXME("string type %d unimplemented\n", dwValueType
);
96 TRACE("returning %d (%s)\n", ret
, debugstr_a(psz
));
100 DWORD WINAPI
CertRDNValueToStrW(DWORD dwValueType
, PCERT_RDN_VALUE_BLOB pValue
,
101 LPWSTR psz
, DWORD csz
)
103 DWORD ret
= 0, len
, i
, strLen
;
105 TRACE("(%d, %p, %p, %d)\n", dwValueType
, pValue
, psz
, csz
);
109 case CERT_RDN_ANY_TYPE
:
111 case CERT_RDN_NUMERIC_STRING
:
112 case CERT_RDN_PRINTABLE_STRING
:
113 case CERT_RDN_TELETEX_STRING
:
114 case CERT_RDN_VIDEOTEX_STRING
:
115 case CERT_RDN_IA5_STRING
:
116 case CERT_RDN_GRAPHIC_STRING
:
117 case CERT_RDN_VISIBLE_STRING
:
118 case CERT_RDN_GENERAL_STRING
:
119 len
= pValue
->cbData
;
126 for (i
= 0; i
< pValue
->cbData
&& ptr
- psz
< csz
; ptr
++, i
++)
127 *ptr
= pValue
->pbData
[i
];
131 case CERT_RDN_BMP_STRING
:
132 case CERT_RDN_UTF8_STRING
:
133 strLen
= len
= pValue
->cbData
/ sizeof(WCHAR
);
140 for (i
= 0; i
< strLen
&& ptr
- psz
< csz
; ptr
++, i
++)
141 *ptr
= ((LPCWSTR
)pValue
->pbData
)[i
];
146 FIXME("string type %d unimplemented\n", dwValueType
);
156 TRACE("returning %d (%s)\n", ret
, debugstr_w(psz
));
160 static inline BOOL
is_quotable_char(char c
)
179 static DWORD
quote_rdn_value_to_str_a(DWORD dwValueType
,
180 PCERT_RDN_VALUE_BLOB pValue
, LPSTR psz
, DWORD csz
)
182 DWORD ret
= 0, len
, i
;
183 BOOL needsQuotes
= FALSE
;
185 TRACE("(%d, %p, %p, %d)\n", dwValueType
, pValue
, psz
, csz
);
189 case CERT_RDN_ANY_TYPE
:
191 case CERT_RDN_NUMERIC_STRING
:
192 case CERT_RDN_PRINTABLE_STRING
:
193 case CERT_RDN_TELETEX_STRING
:
194 case CERT_RDN_VIDEOTEX_STRING
:
195 case CERT_RDN_IA5_STRING
:
196 case CERT_RDN_GRAPHIC_STRING
:
197 case CERT_RDN_VISIBLE_STRING
:
198 case CERT_RDN_GENERAL_STRING
:
199 len
= pValue
->cbData
;
200 if (pValue
->cbData
&& isspace(pValue
->pbData
[0]))
202 if (pValue
->cbData
&& isspace(pValue
->pbData
[pValue
->cbData
- 1]))
204 for (i
= 0; i
< pValue
->cbData
; i
++)
206 if (is_quotable_char(pValue
->pbData
[i
]))
208 if (pValue
->pbData
[i
] == '"')
221 for (i
= 0; i
< pValue
->cbData
&& ptr
- psz
< csz
; ptr
++, i
++)
223 *ptr
= pValue
->pbData
[i
];
224 if (pValue
->pbData
[i
] == '"' && ptr
- psz
< csz
- 1)
227 if (needsQuotes
&& ptr
- psz
< csz
)
232 case CERT_RDN_BMP_STRING
:
233 case CERT_RDN_UTF8_STRING
:
234 len
= WideCharToMultiByte(CP_ACP
, 0, (LPCWSTR
)pValue
->pbData
,
235 pValue
->cbData
/ sizeof(WCHAR
), NULL
, 0, NULL
, NULL
);
236 if (pValue
->cbData
&& isspaceW(((LPCWSTR
)pValue
->pbData
)[0]))
238 if (pValue
->cbData
&&
239 isspaceW(((LPCWSTR
)pValue
->pbData
)[pValue
->cbData
/ sizeof(WCHAR
)-1]))
241 for (i
= 0; i
< pValue
->cbData
/ sizeof(WCHAR
); i
++)
243 if (is_quotable_char(((LPCWSTR
)pValue
->pbData
)[i
]))
245 if (((LPCWSTR
)pValue
->pbData
)[i
] == '"')
258 for (i
= 0; i
< pValue
->cbData
/ sizeof(WCHAR
) &&
259 dst
- psz
< csz
; dst
++, i
++)
261 LPCWSTR src
= (LPCWSTR
)pValue
->pbData
+ i
;
263 WideCharToMultiByte(CP_ACP
, 0, src
, 1, dst
,
264 csz
- (dst
- psz
) - 1, NULL
, NULL
);
265 if (*src
== '"' && dst
- psz
< csz
- 1)
268 if (needsQuotes
&& dst
- psz
< csz
)
274 FIXME("string type %d unimplemented\n", dwValueType
);
284 TRACE("returning %d (%s)\n", ret
, debugstr_a(psz
));
288 static DWORD
quote_rdn_value_to_str_w(DWORD dwValueType
,
289 PCERT_RDN_VALUE_BLOB pValue
, LPWSTR psz
, DWORD csz
)
291 DWORD ret
= 0, len
, i
, strLen
;
292 BOOL needsQuotes
= FALSE
;
294 TRACE("(%d, %p, %p, %d)\n", dwValueType
, pValue
, psz
, csz
);
298 case CERT_RDN_ANY_TYPE
:
300 case CERT_RDN_NUMERIC_STRING
:
301 case CERT_RDN_PRINTABLE_STRING
:
302 case CERT_RDN_TELETEX_STRING
:
303 case CERT_RDN_VIDEOTEX_STRING
:
304 case CERT_RDN_IA5_STRING
:
305 case CERT_RDN_GRAPHIC_STRING
:
306 case CERT_RDN_VISIBLE_STRING
:
307 case CERT_RDN_GENERAL_STRING
:
308 len
= pValue
->cbData
;
309 if (pValue
->cbData
&& isspace(pValue
->pbData
[0]))
311 if (pValue
->cbData
&& isspace(pValue
->pbData
[pValue
->cbData
- 1]))
313 for (i
= 0; i
< pValue
->cbData
; i
++)
315 if (is_quotable_char(pValue
->pbData
[i
]))
317 if (pValue
->pbData
[i
] == '"')
330 for (i
= 0; i
< pValue
->cbData
&& ptr
- psz
< csz
; ptr
++, i
++)
332 *ptr
= pValue
->pbData
[i
];
333 if (pValue
->pbData
[i
] == '"' && ptr
- psz
< csz
- 1)
336 if (needsQuotes
&& ptr
- psz
< csz
)
341 case CERT_RDN_BMP_STRING
:
342 case CERT_RDN_UTF8_STRING
:
343 strLen
= len
= pValue
->cbData
/ sizeof(WCHAR
);
344 if (pValue
->cbData
&& isspace(pValue
->pbData
[0]))
346 if (pValue
->cbData
&& isspace(pValue
->pbData
[strLen
- 1]))
348 for (i
= 0; i
< strLen
; i
++)
350 if (is_quotable_char(((LPCWSTR
)pValue
->pbData
)[i
]))
352 if (((LPCWSTR
)pValue
->pbData
)[i
] == '"')
365 for (i
= 0; i
< strLen
&& ptr
- psz
< csz
; ptr
++, i
++)
367 *ptr
= ((LPCWSTR
)pValue
->pbData
)[i
];
368 if (((LPCWSTR
)pValue
->pbData
)[i
] == '"' && ptr
- psz
< csz
- 1)
371 if (needsQuotes
&& ptr
- psz
< csz
)
377 FIXME("string type %d unimplemented\n", dwValueType
);
387 TRACE("returning %d (%s)\n", ret
, debugstr_w(psz
));
391 /* Adds the prefix prefix to the string pointed to by psz, followed by the
392 * character '='. Copies no more than csz characters. Returns the number of
393 * characters copied. If psz is NULL, returns the number of characters that
396 static DWORD
CRYPT_AddPrefixA(LPCSTR prefix
, LPSTR psz
, DWORD csz
)
400 TRACE("(%s, %p, %d)\n", debugstr_a(prefix
), psz
, csz
);
404 chars
= min(strlen(prefix
), csz
);
405 memcpy(psz
, prefix
, chars
);
406 *(psz
+ chars
) = '=';
410 chars
= lstrlenA(prefix
) + 1;
414 DWORD WINAPI
CertNameToStrA(DWORD dwCertEncodingType
, PCERT_NAME_BLOB pName
,
415 DWORD dwStrType
, LPSTR psz
, DWORD csz
)
417 static const DWORD unsupportedFlags
= CERT_NAME_STR_NO_QUOTING_FLAG
|
418 CERT_NAME_STR_ENABLE_T61_UNICODE_FLAG
;
419 static const char commaSep
[] = ", ";
420 static const char semiSep
[] = "; ";
421 static const char crlfSep
[] = "\r\n";
422 static const char plusSep
[] = " + ";
423 static const char spaceSep
[] = " ";
424 DWORD ret
= 0, bytes
= 0;
426 CERT_NAME_INFO
*info
;
428 TRACE("(%d, %p, %08x, %p, %d)\n", dwCertEncodingType
, pName
, dwStrType
,
430 if (dwStrType
& unsupportedFlags
)
431 FIXME("unsupported flags: %08x\n", dwStrType
& unsupportedFlags
);
433 bRet
= CryptDecodeObjectEx(dwCertEncodingType
, X509_NAME
, pName
->pbData
,
434 pName
->cbData
, CRYPT_DECODE_ALLOC_FLAG
, NULL
, &info
, &bytes
);
437 DWORD i
, j
, sepLen
, rdnSepLen
;
439 BOOL reverse
= dwStrType
& CERT_NAME_STR_REVERSE_FLAG
;
440 const CERT_RDN
*rdn
= info
->rgRDN
;
442 if(reverse
&& info
->cRDN
> 1) rdn
+= (info
->cRDN
- 1);
444 if (dwStrType
& CERT_NAME_STR_SEMICOLON_FLAG
)
446 else if (dwStrType
& CERT_NAME_STR_CRLF_FLAG
)
450 sepLen
= strlen(sep
);
451 if (dwStrType
& CERT_NAME_STR_NO_PLUS_FLAG
)
455 rdnSepLen
= strlen(rdnSep
);
456 for (i
= 0; (!psz
|| ret
< csz
) && i
< info
->cRDN
; i
++)
458 for (j
= 0; (!psz
|| ret
< csz
) && j
< rdn
->cRDNAttr
; j
++)
461 char prefixBuf
[13]; /* big enough for SERIALNUMBER */
462 LPCSTR prefix
= NULL
;
464 if ((dwStrType
& 0x000000ff) == CERT_OID_NAME_STR
)
465 prefix
= rdn
->rgRDNAttr
[j
].pszObjId
;
466 else if ((dwStrType
& 0x000000ff) == CERT_X500_NAME_STR
)
468 PCCRYPT_OID_INFO oidInfo
= CryptFindOIDInfo(
469 CRYPT_OID_INFO_OID_KEY
,
470 rdn
->rgRDNAttr
[j
].pszObjId
,
471 CRYPT_RDN_ATTR_OID_GROUP_ID
);
475 WideCharToMultiByte(CP_ACP
, 0, oidInfo
->pwszName
, -1,
476 prefixBuf
, sizeof(prefixBuf
), NULL
, NULL
);
480 prefix
= rdn
->rgRDNAttr
[j
].pszObjId
;
484 /* - 1 is needed to account for the NULL terminator. */
485 chars
= CRYPT_AddPrefixA(prefix
,
486 psz
? psz
+ ret
: NULL
, psz
? csz
- ret
- 1 : 0);
489 chars
= quote_rdn_value_to_str_a(
490 rdn
->rgRDNAttr
[j
].dwValueType
,
491 &rdn
->rgRDNAttr
[j
].Value
, psz
? psz
+ ret
: NULL
,
492 psz
? csz
- ret
: 0);
495 if (j
< rdn
->cRDNAttr
- 1)
497 if (psz
&& ret
< csz
- rdnSepLen
- 1)
498 memcpy(psz
+ ret
, rdnSep
, rdnSepLen
);
502 if (i
< info
->cRDN
- 1)
504 if (psz
&& ret
< csz
- sepLen
- 1)
505 memcpy(psz
+ ret
, sep
, sepLen
);
520 TRACE("Returning %s\n", debugstr_a(psz
));
524 /* Adds the prefix prefix to the wide-character string pointed to by psz,
525 * followed by the character '='. Copies no more than csz characters. Returns
526 * the number of characters copied. If psz is NULL, returns the number of
527 * characters that would be copied.
528 * Assumes the characters in prefix are ASCII (not multibyte characters.)
530 static DWORD
CRYPT_AddPrefixAToW(LPCSTR prefix
, LPWSTR psz
, DWORD csz
)
534 TRACE("(%s, %p, %d)\n", debugstr_a(prefix
), psz
, csz
);
540 chars
= min(strlen(prefix
), csz
);
541 for (i
= 0; i
< chars
; i
++)
542 *(psz
+ i
) = prefix
[i
];
543 *(psz
+ chars
) = '=';
547 chars
= lstrlenA(prefix
) + 1;
551 /* Adds the prefix prefix to the string pointed to by psz, followed by the
552 * character '='. Copies no more than csz characters. Returns the number of
553 * characters copied. If psz is NULL, returns the number of characters that
556 static DWORD
CRYPT_AddPrefixW(LPCWSTR prefix
, LPWSTR psz
, DWORD csz
)
560 TRACE("(%s, %p, %d)\n", debugstr_w(prefix
), psz
, csz
);
564 chars
= min(strlenW(prefix
), csz
);
565 memcpy(psz
, prefix
, chars
* sizeof(WCHAR
));
566 *(psz
+ chars
) = '=';
570 chars
= lstrlenW(prefix
) + 1;
574 static const WCHAR indent
[] = { ' ',' ',' ',' ',' ',0 };
576 DWORD
cert_name_to_str_with_indent(DWORD dwCertEncodingType
, DWORD indentLevel
,
577 const CERT_NAME_BLOB
*pName
, DWORD dwStrType
, LPWSTR psz
, DWORD csz
)
579 static const DWORD unsupportedFlags
= CERT_NAME_STR_NO_QUOTING_FLAG
|
580 CERT_NAME_STR_ENABLE_T61_UNICODE_FLAG
;
581 static const WCHAR commaSep
[] = { ',',' ',0 };
582 static const WCHAR semiSep
[] = { ';',' ',0 };
583 static const WCHAR crlfSep
[] = { '\r','\n',0 };
584 static const WCHAR plusSep
[] = { ' ','+',' ',0 };
585 static const WCHAR spaceSep
[] = { ' ',0 };
586 DWORD ret
= 0, bytes
= 0;
588 CERT_NAME_INFO
*info
;
590 if (dwStrType
& unsupportedFlags
)
591 FIXME("unsupported flags: %08x\n", dwStrType
& unsupportedFlags
);
593 bRet
= CryptDecodeObjectEx(dwCertEncodingType
, X509_NAME
, pName
->pbData
,
594 pName
->cbData
, CRYPT_DECODE_ALLOC_FLAG
, NULL
, &info
, &bytes
);
597 DWORD i
, j
, sepLen
, rdnSepLen
;
599 BOOL reverse
= dwStrType
& CERT_NAME_STR_REVERSE_FLAG
;
600 const CERT_RDN
*rdn
= info
->rgRDN
;
602 if(reverse
&& info
->cRDN
> 1) rdn
+= (info
->cRDN
- 1);
604 if (dwStrType
& CERT_NAME_STR_SEMICOLON_FLAG
)
606 else if (dwStrType
& CERT_NAME_STR_CRLF_FLAG
)
610 sepLen
= lstrlenW(sep
);
611 if (dwStrType
& CERT_NAME_STR_NO_PLUS_FLAG
)
615 rdnSepLen
= lstrlenW(rdnSep
);
616 for (i
= 0; (!psz
|| ret
< csz
) && i
< info
->cRDN
; i
++)
618 for (j
= 0; (!psz
|| ret
< csz
) && j
< rdn
->cRDNAttr
; j
++)
621 LPCSTR prefixA
= NULL
;
622 LPCWSTR prefixW
= NULL
;
624 if ((dwStrType
& 0x000000ff) == CERT_OID_NAME_STR
)
625 prefixA
= rdn
->rgRDNAttr
[j
].pszObjId
;
626 else if ((dwStrType
& 0x000000ff) == CERT_X500_NAME_STR
)
628 PCCRYPT_OID_INFO oidInfo
= CryptFindOIDInfo(
629 CRYPT_OID_INFO_OID_KEY
,
630 rdn
->rgRDNAttr
[j
].pszObjId
,
631 CRYPT_RDN_ATTR_OID_GROUP_ID
);
634 prefixW
= oidInfo
->pwszName
;
636 prefixA
= rdn
->rgRDNAttr
[j
].pszObjId
;
638 if (dwStrType
& CERT_NAME_STR_CRLF_FLAG
)
642 for (k
= 0; k
< indentLevel
; k
++)
646 chars
= min(strlenW(indent
), csz
- ret
- 1);
647 memcpy(psz
+ ret
, indent
, chars
* sizeof(WCHAR
));
650 chars
= strlenW(indent
);
656 /* - 1 is needed to account for the NULL terminator. */
657 chars
= CRYPT_AddPrefixW(prefixW
,
658 psz
? psz
+ ret
: NULL
, psz
? csz
- ret
- 1 : 0);
663 /* - 1 is needed to account for the NULL terminator. */
664 chars
= CRYPT_AddPrefixAToW(prefixA
,
665 psz
? psz
+ ret
: NULL
, psz
? csz
- ret
- 1 : 0);
668 chars
= quote_rdn_value_to_str_w(
669 rdn
->rgRDNAttr
[j
].dwValueType
,
670 &rdn
->rgRDNAttr
[j
].Value
, psz
? psz
+ ret
: NULL
,
671 psz
? csz
- ret
: 0);
674 if (j
< rdn
->cRDNAttr
- 1)
676 if (psz
&& ret
< csz
- rdnSepLen
- 1)
677 memcpy(psz
+ ret
, rdnSep
, rdnSepLen
* sizeof(WCHAR
));
681 if (i
< info
->cRDN
- 1)
683 if (psz
&& ret
< csz
- sepLen
- 1)
684 memcpy(psz
+ ret
, sep
, sepLen
* sizeof(WCHAR
));
702 DWORD WINAPI
CertNameToStrW(DWORD dwCertEncodingType
, PCERT_NAME_BLOB pName
,
703 DWORD dwStrType
, LPWSTR psz
, DWORD csz
)
707 TRACE("(%d, %p, %08x, %p, %d)\n", dwCertEncodingType
, pName
, dwStrType
,
710 ret
= cert_name_to_str_with_indent(dwCertEncodingType
, 0, pName
, dwStrType
,
712 TRACE("Returning %s\n", debugstr_w(psz
));
716 BOOL WINAPI
CertStrToNameA(DWORD dwCertEncodingType
, LPCSTR pszX500
,
717 DWORD dwStrType
, void *pvReserved
, BYTE
*pbEncoded
, DWORD
*pcbEncoded
,
723 TRACE("(%08x, %s, %08x, %p, %p, %p, %p)\n", dwCertEncodingType
,
724 debugstr_a(pszX500
), dwStrType
, pvReserved
, pbEncoded
, pcbEncoded
,
727 len
= MultiByteToWideChar(CP_ACP
, 0, pszX500
, -1, NULL
, 0);
730 LPWSTR x500
, errorStr
;
732 if ((x500
= CryptMemAlloc(len
* sizeof(WCHAR
))))
734 MultiByteToWideChar(CP_ACP
, 0, pszX500
, -1, x500
, len
);
735 ret
= CertStrToNameW(dwCertEncodingType
, x500
, dwStrType
,
736 pvReserved
, pbEncoded
, pcbEncoded
,
737 ppszError
? (LPCWSTR
*)&errorStr
: NULL
);
744 *ppszError
= pszX500
;
745 for (i
= 0; i
< errorStr
- x500
; i
++)
746 *ppszError
= CharNextA(*ppszError
);
755 SetLastError(ERROR_OUTOFMEMORY
);
761 SetLastError(CRYPT_E_INVALID_X500_STRING
);
763 *ppszError
= pszX500
;
771 WCHAR buf
[10]; /* big enough for L"GivenName" */
772 LPWSTR keyName
; /* usually = buf, but may be allocated */
773 DWORD keyLen
; /* full available buffer size in WCHARs */
776 static void CRYPT_InitializeKeynameKeeper(struct KeynameKeeper
*keeper
)
778 keeper
->keyName
= keeper
->buf
;
779 keeper
->keyLen
= ARRAY_SIZE(keeper
->buf
);
782 static void CRYPT_FreeKeynameKeeper(struct KeynameKeeper
*keeper
)
784 if (keeper
->keyName
!= keeper
->buf
)
785 CryptMemFree(keeper
->keyName
);
794 static void CRYPT_KeynameKeeperFromTokenW(struct KeynameKeeper
*keeper
,
795 const struct X500TokenW
*key
)
797 DWORD len
= key
->end
- key
->start
;
799 if (len
>= keeper
->keyLen
)
801 CRYPT_FreeKeynameKeeper( keeper
);
802 keeper
->keyLen
= len
+ 1;
803 keeper
->keyName
= CryptMemAlloc(keeper
->keyLen
* sizeof(WCHAR
));
805 memcpy(keeper
->keyName
, key
->start
, len
* sizeof(WCHAR
));
806 keeper
->keyName
[len
] = '\0';
807 TRACE("Keyname is %s\n", debugstr_w(keeper
->keyName
));
810 static BOOL
CRYPT_GetNextKeyW(LPCWSTR str
, struct X500TokenW
*token
,
815 while (*str
&& isspaceW(*str
))
820 while (*str
&& *str
!= '=' && !isspaceW(*str
))
822 if (*str
&& (*str
== '=' || isspaceW(*str
)))
826 TRACE("missing equals char at %s\n", debugstr_w(token
->start
));
828 *ppszError
= token
->start
;
829 SetLastError(CRYPT_E_INVALID_X500_STRING
);
838 /* Assumes separators are characters in the 0-255 range */
839 static BOOL
CRYPT_GetNextValueW(LPCWSTR str
, DWORD dwFlags
, LPCWSTR separators
,
840 WCHAR
*separator_used
, struct X500TokenW
*token
, LPCWSTR
*ppszError
)
844 TRACE("(%s, %s, %p, %p)\n", debugstr_w(str
), debugstr_w(separators
), token
,
848 while (*str
&& isspaceW(*str
))
853 if (!(dwFlags
& CERT_NAME_STR_NO_QUOTING_FLAG
) && *str
== '"')
857 while (!token
->end
&& ret
)
859 while (*str
&& *str
!= '"')
863 if (*(str
+ 1) != '"')
864 token
->end
= str
+ 1;
870 TRACE("unterminated quote at %s\n", debugstr_w(str
));
873 SetLastError(CRYPT_E_INVALID_X500_STRING
);
880 WCHAR map
[256] = { 0 };
883 map
[*separators
++] = 1;
884 while (*str
&& (*str
>= 0xff || !map
[*str
]))
887 if (map
[*str
]) *separator_used
= *str
;
892 TRACE("missing value at %s\n", debugstr_w(str
));
895 SetLastError(CRYPT_E_INVALID_X500_STRING
);
901 /* Encodes the string represented by value as the string type type into the
902 * CERT_NAME_BLOB output. If there is an error and ppszError is not NULL,
903 * *ppszError is set to the first failing character. If there is no error,
904 * output's pbData must be freed with LocalFree.
906 static BOOL
CRYPT_EncodeValueWithType(DWORD dwCertEncodingType
,
907 const struct X500TokenW
*value
, PCERT_NAME_BLOB output
, DWORD type
,
910 CERT_NAME_VALUE nameValue
= { type
, { 0, NULL
} };
913 if (value
->end
> value
->start
)
918 nameValue
.Value
.pbData
= CryptMemAlloc((value
->end
- value
->start
+ 1) *
920 if (!nameValue
.Value
.pbData
)
922 SetLastError(ERROR_OUTOFMEMORY
);
925 ptr
= (LPWSTR
)nameValue
.Value
.pbData
;
926 for (i
= 0; i
< value
->end
- value
->start
; i
++)
928 *ptr
++ = value
->start
[i
];
929 if (value
->start
[i
] == '"')
932 /* The string is NULL terminated because of a quirk in encoding
933 * unicode names values: if the length is given as 0, the value is
934 * assumed to be a NULL-terminated string.
937 nameValue
.Value
.cbData
= (LPBYTE
)ptr
- nameValue
.Value
.pbData
;
939 ret
= CryptEncodeObjectEx(dwCertEncodingType
, X509_UNICODE_NAME_VALUE
,
940 &nameValue
, CRYPT_ENCODE_ALLOC_FLAG
, NULL
, &output
->pbData
,
942 if (!ret
&& ppszError
)
944 if (type
== CERT_RDN_NUMERIC_STRING
&&
945 GetLastError() == CRYPT_E_INVALID_NUMERIC_STRING
)
946 *ppszError
= value
->start
+ output
->cbData
;
947 else if (type
== CERT_RDN_PRINTABLE_STRING
&&
948 GetLastError() == CRYPT_E_INVALID_PRINTABLE_STRING
)
949 *ppszError
= value
->start
+ output
->cbData
;
950 else if (type
== CERT_RDN_IA5_STRING
&&
951 GetLastError() == CRYPT_E_INVALID_IA5_STRING
)
952 *ppszError
= value
->start
+ output
->cbData
;
954 CryptMemFree(nameValue
.Value
.pbData
);
958 static BOOL
CRYPT_EncodeValue(DWORD dwCertEncodingType
,
959 const struct X500TokenW
*value
, PCERT_NAME_BLOB output
, const DWORD
*types
,
966 for (i
= 0; !ret
&& types
[i
]; i
++)
967 ret
= CRYPT_EncodeValueWithType(dwCertEncodingType
, value
, output
,
968 types
[i
], ppszError
);
972 static BOOL
CRYPT_ValueToRDN(DWORD dwCertEncodingType
, PCERT_NAME_INFO info
,
973 PCCRYPT_OID_INFO keyOID
, struct X500TokenW
*value
, DWORD dwStrType
, LPCWSTR
*ppszError
)
977 TRACE("OID %s, value %s\n", debugstr_a(keyOID
->pszOID
),
978 debugstr_wn(value
->start
, value
->end
- value
->start
));
981 info
->rgRDN
= CryptMemAlloc(sizeof(CERT_RDN
));
983 info
->rgRDN
= CryptMemRealloc(info
->rgRDN
,
984 (info
->cRDN
+ 1) * sizeof(CERT_RDN
));
987 /* FIXME: support multiple RDN attrs */
988 info
->rgRDN
[info
->cRDN
].rgRDNAttr
=
989 CryptMemAlloc(sizeof(CERT_RDN_ATTR
));
990 if (info
->rgRDN
[info
->cRDN
].rgRDNAttr
)
992 static const DWORD defaultTypes
[] = { CERT_RDN_PRINTABLE_STRING
,
993 CERT_RDN_BMP_STRING
, 0 };
996 info
->rgRDN
[info
->cRDN
].cRDNAttr
= 1;
997 info
->rgRDN
[info
->cRDN
].rgRDNAttr
[0].pszObjId
=
998 (LPSTR
)keyOID
->pszOID
;
999 info
->rgRDN
[info
->cRDN
].rgRDNAttr
[0].dwValueType
=
1000 CERT_RDN_ENCODED_BLOB
;
1001 if (keyOID
->ExtraInfo
.cbData
)
1002 types
= (const DWORD
*)keyOID
->ExtraInfo
.pbData
;
1004 types
= defaultTypes
;
1006 /* Remove surrounding quotes */
1007 if (value
->start
[0] == '"' && !(dwStrType
& CERT_NAME_STR_NO_QUOTING_FLAG
))
1012 ret
= CRYPT_EncodeValue(dwCertEncodingType
, value
,
1013 &info
->rgRDN
[info
->cRDN
].rgRDNAttr
[0].Value
, types
, ppszError
);
1016 SetLastError(ERROR_OUTOFMEMORY
);
1020 SetLastError(ERROR_OUTOFMEMORY
);
1024 BOOL WINAPI
CertStrToNameW(DWORD dwCertEncodingType
, LPCWSTR pszX500
,
1025 DWORD dwStrType
, void *pvReserved
, BYTE
*pbEncoded
, DWORD
*pcbEncoded
,
1028 CERT_NAME_INFO info
= { 0, NULL
};
1030 struct KeynameKeeper keeper
;
1034 TRACE("(%08x, %s, %08x, %p, %p, %p, %p)\n", dwCertEncodingType
,
1035 debugstr_w(pszX500
), dwStrType
, pvReserved
, pbEncoded
, pcbEncoded
,
1038 CRYPT_InitializeKeynameKeeper(&keeper
);
1040 while (str
&& *str
&& ret
)
1042 struct X500TokenW token
;
1044 ret
= CRYPT_GetNextKeyW(str
, &token
, ppszError
);
1045 if (ret
&& token
.start
)
1047 PCCRYPT_OID_INFO keyOID
;
1049 CRYPT_KeynameKeeperFromTokenW(&keeper
, &token
);
1050 keyOID
= CryptFindOIDInfo(CRYPT_OID_INFO_NAME_KEY
, keeper
.keyName
,
1051 CRYPT_RDN_ATTR_OID_GROUP_ID
);
1055 *ppszError
= token
.start
;
1056 SetLastError(CRYPT_E_INVALID_X500_STRING
);
1062 while (isspaceW(*str
))
1068 SetLastError(CRYPT_E_INVALID_X500_STRING
);
1073 static const WCHAR commaSep
[] = { ',',0 };
1074 static const WCHAR semiSep
[] = { ';',0 };
1075 static const WCHAR crlfSep
[] = { '\r','\n',0 };
1076 static const WCHAR allSepsWithoutPlus
[] = { ',',';','\r','\n',0 };
1077 static const WCHAR allSeps
[] = { '+',',',';','\r','\n',0 };
1082 if (dwStrType
& CERT_NAME_STR_COMMA_FLAG
)
1084 else if (dwStrType
& CERT_NAME_STR_SEMICOLON_FLAG
)
1086 else if (dwStrType
& CERT_NAME_STR_CRLF_FLAG
)
1088 else if (dwStrType
& CERT_NAME_STR_NO_PLUS_FLAG
)
1089 sep
= allSepsWithoutPlus
;
1092 ret
= CRYPT_GetNextValueW(str
, dwStrType
, sep
, &sep_used
, &token
,
1097 /* if token.end points to the separator, skip it */
1098 if (str
&& sep_used
&& *str
== sep_used
) str
++;
1100 ret
= CRYPT_ValueToRDN(dwCertEncodingType
, &info
,
1101 keyOID
, &token
, dwStrType
, ppszError
);
1107 CRYPT_FreeKeynameKeeper(&keeper
);
1112 ret
= CryptEncodeObjectEx(dwCertEncodingType
, X509_NAME
, &info
,
1113 0, NULL
, pbEncoded
, pcbEncoded
);
1115 for (i
= 0; i
< info
.cRDN
; i
++)
1119 for (j
= 0; j
< info
.rgRDN
[i
].cRDNAttr
; j
++)
1120 LocalFree(info
.rgRDN
[i
].rgRDNAttr
[j
].Value
.pbData
);
1121 CryptMemFree(info
.rgRDN
[i
].rgRDNAttr
);
1123 CryptMemFree(info
.rgRDN
);
1127 DWORD WINAPI
CertGetNameStringA(PCCERT_CONTEXT pCertContext
, DWORD dwType
,
1128 DWORD dwFlags
, void *pvTypePara
, LPSTR pszNameString
, DWORD cchNameString
)
1132 TRACE("(%p, %d, %08x, %p, %p, %d)\n", pCertContext
, dwType
, dwFlags
,
1133 pvTypePara
, pszNameString
, cchNameString
);
1140 nameLen
= CertGetNameStringW(pCertContext
, dwType
, dwFlags
, pvTypePara
,
1142 wideName
= CryptMemAlloc(nameLen
* sizeof(WCHAR
));
1145 CertGetNameStringW(pCertContext
, dwType
, dwFlags
, pvTypePara
,
1147 nameLen
= WideCharToMultiByte(CP_ACP
, 0, wideName
, nameLen
,
1148 pszNameString
, cchNameString
, NULL
, NULL
);
1149 if (nameLen
<= cchNameString
)
1153 pszNameString
[cchNameString
- 1] = '\0';
1154 ret
= cchNameString
;
1156 CryptMemFree(wideName
);
1160 *pszNameString
= '\0';
1165 ret
= CertGetNameStringW(pCertContext
, dwType
, dwFlags
, pvTypePara
,
1170 /* Searches cert's extensions for the alternate name extension with OID
1171 * altNameOID, and if found, searches it for the alternate name type entryType.
1172 * If found, returns a pointer to the entry, otherwise returns NULL.
1173 * Regardless of whether an entry of the desired type is found, if the
1174 * alternate name extension is present, sets *info to the decoded alternate
1175 * name extension, which you must free using LocalFree.
1176 * The return value is a pointer within *info, so don't free *info before
1177 * you're done with the return value.
1179 static PCERT_ALT_NAME_ENTRY
cert_find_alt_name_entry(PCCERT_CONTEXT cert
,
1180 LPCSTR altNameOID
, DWORD entryType
, PCERT_ALT_NAME_INFO
*info
)
1182 PCERT_ALT_NAME_ENTRY entry
= NULL
;
1183 PCERT_EXTENSION ext
= CertFindExtension(altNameOID
,
1184 cert
->pCertInfo
->cExtension
, cert
->pCertInfo
->rgExtension
);
1190 if (CryptDecodeObjectEx(cert
->dwCertEncodingType
, X509_ALTERNATE_NAME
,
1191 ext
->Value
.pbData
, ext
->Value
.cbData
, CRYPT_DECODE_ALLOC_FLAG
, NULL
,
1196 for (i
= 0; !entry
&& i
< (*info
)->cAltEntry
; i
++)
1197 if ((*info
)->rgAltEntry
[i
].dwAltNameChoice
== entryType
)
1198 entry
= &(*info
)->rgAltEntry
[i
];
1206 static DWORD
cert_get_name_from_rdn_attr(DWORD encodingType
,
1207 const CERT_NAME_BLOB
*name
, LPCSTR oid
, LPWSTR pszNameString
, DWORD cchNameString
)
1209 CERT_NAME_INFO
*nameInfo
;
1210 DWORD bytes
= 0, ret
= 0;
1212 if (CryptDecodeObjectEx(encodingType
, X509_NAME
, name
->pbData
,
1213 name
->cbData
, CRYPT_DECODE_ALLOC_FLAG
, NULL
, &nameInfo
, &bytes
))
1215 PCERT_RDN_ATTR nameAttr
;
1218 oid
= szOID_RSA_emailAddr
;
1219 nameAttr
= CertFindRDNAttr(oid
, nameInfo
);
1221 ret
= CertRDNValueToStrW(nameAttr
->dwValueType
, &nameAttr
->Value
,
1222 pszNameString
, cchNameString
);
1223 LocalFree(nameInfo
);
1228 DWORD WINAPI
CertGetNameStringW(PCCERT_CONTEXT pCertContext
, DWORD dwType
,
1229 DWORD dwFlags
, void *pvTypePara
, LPWSTR pszNameString
, DWORD cchNameString
)
1232 PCERT_NAME_BLOB name
;
1235 TRACE("(%p, %d, %08x, %p, %p, %d)\n", pCertContext
, dwType
,
1236 dwFlags
, pvTypePara
, pszNameString
, cchNameString
);
1241 if (dwFlags
& CERT_NAME_ISSUER_FLAG
)
1243 name
= &pCertContext
->pCertInfo
->Issuer
;
1244 altNameOID
= szOID_ISSUER_ALT_NAME
;
1248 name
= &pCertContext
->pCertInfo
->Subject
;
1249 altNameOID
= szOID_SUBJECT_ALT_NAME
;
1254 case CERT_NAME_EMAIL_TYPE
:
1256 CERT_ALT_NAME_INFO
*info
;
1257 PCERT_ALT_NAME_ENTRY entry
= cert_find_alt_name_entry(pCertContext
,
1258 altNameOID
, CERT_ALT_NAME_RFC822_NAME
, &info
);
1263 ret
= strlenW(entry
->u
.pwszRfc822Name
) + 1;
1264 else if (cchNameString
)
1266 ret
= min(strlenW(entry
->u
.pwszRfc822Name
), cchNameString
- 1);
1267 memcpy(pszNameString
, entry
->u
.pwszRfc822Name
,
1268 ret
* sizeof(WCHAR
));
1269 pszNameString
[ret
++] = 0;
1275 ret
= cert_get_name_from_rdn_attr(pCertContext
->dwCertEncodingType
,
1276 name
, szOID_RSA_emailAddr
, pszNameString
, cchNameString
);
1279 case CERT_NAME_RDN_TYPE
:
1281 DWORD type
= pvTypePara
? *(DWORD
*)pvTypePara
: 0;
1284 ret
= CertNameToStrW(pCertContext
->dwCertEncodingType
, name
,
1285 type
, pszNameString
, cchNameString
);
1288 CERT_ALT_NAME_INFO
*info
;
1289 PCERT_ALT_NAME_ENTRY entry
= cert_find_alt_name_entry(pCertContext
,
1290 altNameOID
, CERT_ALT_NAME_DIRECTORY_NAME
, &info
);
1293 ret
= CertNameToStrW(pCertContext
->dwCertEncodingType
,
1294 &entry
->u
.DirectoryName
, type
, pszNameString
, cchNameString
);
1300 case CERT_NAME_ATTR_TYPE
:
1301 ret
= cert_get_name_from_rdn_attr(pCertContext
->dwCertEncodingType
,
1302 name
, pvTypePara
, pszNameString
, cchNameString
);
1305 CERT_ALT_NAME_INFO
*altInfo
;
1306 PCERT_ALT_NAME_ENTRY entry
= cert_find_alt_name_entry(pCertContext
,
1307 altNameOID
, CERT_ALT_NAME_DIRECTORY_NAME
, &altInfo
);
1310 ret
= cert_name_to_str_with_indent(X509_ASN_ENCODING
, 0,
1311 &entry
->u
.DirectoryName
, 0, pszNameString
, cchNameString
);
1316 case CERT_NAME_SIMPLE_DISPLAY_TYPE
:
1318 static const LPCSTR simpleAttributeOIDs
[] = { szOID_COMMON_NAME
,
1319 szOID_ORGANIZATIONAL_UNIT_NAME
, szOID_ORGANIZATION_NAME
,
1320 szOID_RSA_emailAddr
};
1321 CERT_NAME_INFO
*nameInfo
= NULL
;
1324 if (CryptDecodeObjectEx(pCertContext
->dwCertEncodingType
, X509_NAME
,
1325 name
->pbData
, name
->cbData
, CRYPT_DECODE_ALLOC_FLAG
, NULL
, &nameInfo
,
1328 PCERT_RDN_ATTR nameAttr
= NULL
;
1330 for (i
= 0; !nameAttr
&& i
< ARRAY_SIZE(simpleAttributeOIDs
); i
++)
1331 nameAttr
= CertFindRDNAttr(simpleAttributeOIDs
[i
], nameInfo
);
1333 ret
= CertRDNValueToStrW(nameAttr
->dwValueType
,
1334 &nameAttr
->Value
, pszNameString
, cchNameString
);
1335 LocalFree(nameInfo
);
1339 CERT_ALT_NAME_INFO
*altInfo
;
1340 PCERT_ALT_NAME_ENTRY entry
= cert_find_alt_name_entry(pCertContext
,
1341 altNameOID
, CERT_ALT_NAME_RFC822_NAME
, &altInfo
);
1345 if (!entry
&& altInfo
->cAltEntry
)
1346 entry
= &altInfo
->rgAltEntry
[0];
1350 ret
= strlenW(entry
->u
.pwszRfc822Name
) + 1;
1351 else if (cchNameString
)
1353 ret
= min(strlenW(entry
->u
.pwszRfc822Name
),
1355 memcpy(pszNameString
, entry
->u
.pwszRfc822Name
,
1356 ret
* sizeof(WCHAR
));
1357 pszNameString
[ret
++] = 0;
1365 case CERT_NAME_FRIENDLY_DISPLAY_TYPE
:
1367 DWORD cch
= cchNameString
;
1369 if (CertGetCertificateContextProperty(pCertContext
,
1370 CERT_FRIENDLY_NAME_PROP_ID
, pszNameString
, &cch
))
1373 ret
= CertGetNameStringW(pCertContext
,
1374 CERT_NAME_SIMPLE_DISPLAY_TYPE
, dwFlags
, pvTypePara
, pszNameString
,
1378 case CERT_NAME_DNS_TYPE
:
1380 CERT_ALT_NAME_INFO
*info
;
1381 PCERT_ALT_NAME_ENTRY entry
= cert_find_alt_name_entry(pCertContext
,
1382 altNameOID
, CERT_ALT_NAME_DNS_NAME
, &info
);
1387 ret
= strlenW(entry
->u
.pwszDNSName
) + 1;
1388 else if (cchNameString
)
1390 ret
= min(strlenW(entry
->u
.pwszDNSName
), cchNameString
- 1);
1391 memcpy(pszNameString
, entry
->u
.pwszDNSName
, ret
* sizeof(WCHAR
));
1392 pszNameString
[ret
++] = 0;
1398 ret
= cert_get_name_from_rdn_attr(pCertContext
->dwCertEncodingType
,
1399 name
, szOID_COMMON_NAME
, pszNameString
, cchNameString
);
1402 case CERT_NAME_URL_TYPE
:
1404 CERT_ALT_NAME_INFO
*info
;
1405 PCERT_ALT_NAME_ENTRY entry
= cert_find_alt_name_entry(pCertContext
,
1406 altNameOID
, CERT_ALT_NAME_URL
, &info
);
1411 ret
= strlenW(entry
->u
.pwszURL
) + 1;
1412 else if (cchNameString
)
1414 ret
= min(strlenW(entry
->u
.pwszURL
), cchNameString
- 1);
1415 memcpy(pszNameString
, entry
->u
.pwszURL
, ret
* sizeof(WCHAR
));
1416 pszNameString
[ret
++] = 0;
1424 FIXME("unimplemented for type %d\n", dwType
);
1432 else if (cchNameString
)
1434 pszNameString
[0] = 0;