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"
30 WINE_DEFAULT_DEBUG_CHANNEL(crypt
);
32 DWORD WINAPI
CertRDNValueToStrA(DWORD dwValueType
, PCERT_RDN_VALUE_BLOB pValue
,
37 TRACE("(%d, %p, %p, %d)\n", dwValueType
, pValue
, psz
, csz
);
41 case CERT_RDN_ANY_TYPE
:
43 case CERT_RDN_NUMERIC_STRING
:
44 case CERT_RDN_PRINTABLE_STRING
:
45 case CERT_RDN_TELETEX_STRING
:
46 case CERT_RDN_VIDEOTEX_STRING
:
47 case CERT_RDN_IA5_STRING
:
48 case CERT_RDN_GRAPHIC_STRING
:
49 case CERT_RDN_VISIBLE_STRING
:
50 case CERT_RDN_GENERAL_STRING
:
56 DWORD chars
= min(len
, csz
- 1);
60 memcpy(psz
, pValue
->pbData
, chars
);
66 case CERT_RDN_BMP_STRING
:
67 case CERT_RDN_UTF8_STRING
:
68 len
= WideCharToMultiByte(CP_ACP
, 0, (LPCWSTR
)pValue
->pbData
,
69 pValue
->cbData
/ sizeof(WCHAR
), NULL
, 0, NULL
, NULL
);
74 DWORD chars
= min(pValue
->cbData
/ sizeof(WCHAR
), csz
- 1);
78 ret
= WideCharToMultiByte(CP_ACP
, 0, (LPCWSTR
)pValue
->pbData
,
79 chars
, psz
, csz
- 1, NULL
, NULL
);
85 FIXME("string type %d unimplemented\n", dwValueType
);
95 TRACE("returning %d (%s)\n", ret
, debugstr_a(psz
));
99 DWORD WINAPI
CertRDNValueToStrW(DWORD dwValueType
, PCERT_RDN_VALUE_BLOB pValue
,
100 LPWSTR psz
, DWORD csz
)
102 DWORD ret
= 0, len
, i
, strLen
;
104 TRACE("(%d, %p, %p, %d)\n", dwValueType
, pValue
, psz
, csz
);
108 case CERT_RDN_ANY_TYPE
:
110 case CERT_RDN_NUMERIC_STRING
:
111 case CERT_RDN_PRINTABLE_STRING
:
112 case CERT_RDN_TELETEX_STRING
:
113 case CERT_RDN_VIDEOTEX_STRING
:
114 case CERT_RDN_IA5_STRING
:
115 case CERT_RDN_GRAPHIC_STRING
:
116 case CERT_RDN_VISIBLE_STRING
:
117 case CERT_RDN_GENERAL_STRING
:
118 len
= pValue
->cbData
;
125 for (i
= 0; i
< pValue
->cbData
&& ptr
- psz
< csz
; ptr
++, i
++)
126 *ptr
= pValue
->pbData
[i
];
130 case CERT_RDN_BMP_STRING
:
131 case CERT_RDN_UTF8_STRING
:
132 strLen
= len
= pValue
->cbData
/ sizeof(WCHAR
);
139 for (i
= 0; i
< strLen
&& ptr
- psz
< csz
; ptr
++, i
++)
140 *ptr
= ((LPCWSTR
)pValue
->pbData
)[i
];
145 FIXME("string type %d unimplemented\n", dwValueType
);
155 TRACE("returning %d (%s)\n", ret
, debugstr_w(psz
));
159 static inline BOOL
is_quotable_char(char c
)
178 static DWORD
quote_rdn_value_to_str_a(DWORD dwValueType
,
179 PCERT_RDN_VALUE_BLOB pValue
, LPSTR psz
, DWORD csz
)
181 DWORD ret
= 0, len
, i
;
182 BOOL needsQuotes
= FALSE
;
184 TRACE("(%d, %p, %p, %d)\n", dwValueType
, pValue
, psz
, csz
);
188 case CERT_RDN_ANY_TYPE
:
190 case CERT_RDN_NUMERIC_STRING
:
191 case CERT_RDN_PRINTABLE_STRING
:
192 case CERT_RDN_TELETEX_STRING
:
193 case CERT_RDN_VIDEOTEX_STRING
:
194 case CERT_RDN_IA5_STRING
:
195 case CERT_RDN_GRAPHIC_STRING
:
196 case CERT_RDN_VISIBLE_STRING
:
197 case CERT_RDN_GENERAL_STRING
:
198 len
= pValue
->cbData
;
199 if (pValue
->cbData
&& isspace(pValue
->pbData
[0]))
201 if (pValue
->cbData
&& isspace(pValue
->pbData
[pValue
->cbData
- 1]))
203 for (i
= 0; i
< pValue
->cbData
; i
++)
205 if (is_quotable_char(pValue
->pbData
[i
]))
207 if (pValue
->pbData
[i
] == '"')
220 for (i
= 0; i
< pValue
->cbData
&& ptr
- psz
< csz
; ptr
++, i
++)
222 *ptr
= pValue
->pbData
[i
];
223 if (pValue
->pbData
[i
] == '"' && ptr
- psz
< csz
- 1)
226 if (needsQuotes
&& ptr
- psz
< csz
)
231 case CERT_RDN_BMP_STRING
:
232 case CERT_RDN_UTF8_STRING
:
233 len
= WideCharToMultiByte(CP_ACP
, 0, (LPCWSTR
)pValue
->pbData
,
234 pValue
->cbData
/ sizeof(WCHAR
), NULL
, 0, NULL
, NULL
);
235 if (pValue
->cbData
&& isspaceW(((LPCWSTR
)pValue
->pbData
)[0]))
237 if (pValue
->cbData
&&
238 isspaceW(((LPCWSTR
)pValue
->pbData
)[pValue
->cbData
/ sizeof(WCHAR
)-1]))
240 for (i
= 0; i
< pValue
->cbData
/ sizeof(WCHAR
); i
++)
242 if (is_quotable_char(((LPCWSTR
)pValue
->pbData
)[i
]))
244 if (((LPCWSTR
)pValue
->pbData
)[i
] == '"')
257 for (i
= 0; i
< pValue
->cbData
/ sizeof(WCHAR
) &&
258 dst
- psz
< csz
; dst
++, i
++)
260 LPCWSTR src
= (LPCWSTR
)pValue
->pbData
+ i
;
262 WideCharToMultiByte(CP_ACP
, 0, src
, 1, dst
,
263 csz
- (dst
- psz
) - 1, NULL
, NULL
);
264 if (*src
== '"' && dst
- psz
< csz
- 1)
267 if (needsQuotes
&& dst
- psz
< csz
)
273 FIXME("string type %d unimplemented\n", dwValueType
);
283 TRACE("returning %d (%s)\n", ret
, debugstr_a(psz
));
287 static DWORD
quote_rdn_value_to_str_w(DWORD dwValueType
,
288 PCERT_RDN_VALUE_BLOB pValue
, LPWSTR psz
, DWORD csz
)
290 DWORD ret
= 0, len
, i
, strLen
;
291 BOOL needsQuotes
= FALSE
;
293 TRACE("(%d, %p, %p, %d)\n", dwValueType
, pValue
, psz
, csz
);
297 case CERT_RDN_ANY_TYPE
:
299 case CERT_RDN_NUMERIC_STRING
:
300 case CERT_RDN_PRINTABLE_STRING
:
301 case CERT_RDN_TELETEX_STRING
:
302 case CERT_RDN_VIDEOTEX_STRING
:
303 case CERT_RDN_IA5_STRING
:
304 case CERT_RDN_GRAPHIC_STRING
:
305 case CERT_RDN_VISIBLE_STRING
:
306 case CERT_RDN_GENERAL_STRING
:
307 len
= pValue
->cbData
;
308 if (pValue
->cbData
&& isspace(pValue
->pbData
[0]))
310 if (pValue
->cbData
&& isspace(pValue
->pbData
[pValue
->cbData
- 1]))
312 for (i
= 0; i
< pValue
->cbData
; i
++)
314 if (is_quotable_char(pValue
->pbData
[i
]))
316 if (pValue
->pbData
[i
] == '"')
329 for (i
= 0; i
< pValue
->cbData
&& ptr
- psz
< csz
; ptr
++, i
++)
331 *ptr
= pValue
->pbData
[i
];
332 if (pValue
->pbData
[i
] == '"' && ptr
- psz
< csz
- 1)
335 if (needsQuotes
&& ptr
- psz
< csz
)
340 case CERT_RDN_BMP_STRING
:
341 case CERT_RDN_UTF8_STRING
:
342 strLen
= len
= pValue
->cbData
/ sizeof(WCHAR
);
343 if (pValue
->cbData
&& isspace(pValue
->pbData
[0]))
345 if (pValue
->cbData
&& isspace(pValue
->pbData
[strLen
- 1]))
347 for (i
= 0; i
< strLen
; i
++)
349 if (is_quotable_char(((LPCWSTR
)pValue
->pbData
)[i
]))
351 if (((LPCWSTR
)pValue
->pbData
)[i
] == '"')
364 for (i
= 0; i
< strLen
&& ptr
- psz
< csz
; ptr
++, i
++)
366 *ptr
= ((LPCWSTR
)pValue
->pbData
)[i
];
367 if (((LPCWSTR
)pValue
->pbData
)[i
] == '"' && ptr
- psz
< csz
- 1)
370 if (needsQuotes
&& ptr
- psz
< csz
)
376 FIXME("string type %d unimplemented\n", dwValueType
);
386 TRACE("returning %d (%s)\n", ret
, debugstr_w(psz
));
390 /* Adds the prefix prefix to the string pointed to by psz, followed by the
391 * character '='. Copies no more than csz characters. Returns the number of
392 * characters copied. If psz is NULL, returns the number of characters that
395 static DWORD
CRYPT_AddPrefixA(LPCSTR prefix
, LPSTR psz
, DWORD csz
)
399 TRACE("(%s, %p, %d)\n", debugstr_a(prefix
), psz
, csz
);
403 chars
= min(strlen(prefix
), csz
);
404 memcpy(psz
, prefix
, chars
);
405 *(psz
+ chars
) = '=';
409 chars
= lstrlenA(prefix
) + 1;
413 DWORD WINAPI
CertNameToStrA(DWORD dwCertEncodingType
, PCERT_NAME_BLOB pName
,
414 DWORD dwStrType
, LPSTR psz
, DWORD csz
)
416 static const DWORD unsupportedFlags
= CERT_NAME_STR_NO_QUOTING_FLAG
|
417 CERT_NAME_STR_ENABLE_T61_UNICODE_FLAG
;
418 static const char commaSep
[] = ", ";
419 static const char semiSep
[] = "; ";
420 static const char crlfSep
[] = "\r\n";
421 static const char plusSep
[] = " + ";
422 static const char spaceSep
[] = " ";
423 DWORD ret
= 0, bytes
= 0;
425 CERT_NAME_INFO
*info
;
427 TRACE("(%d, %p, %08x, %p, %d)\n", dwCertEncodingType
, pName
, dwStrType
,
429 if (dwStrType
& unsupportedFlags
)
430 FIXME("unsupported flags: %08x\n", dwStrType
& unsupportedFlags
);
432 bRet
= CryptDecodeObjectEx(dwCertEncodingType
, X509_NAME
, pName
->pbData
,
433 pName
->cbData
, CRYPT_DECODE_ALLOC_FLAG
, NULL
, &info
, &bytes
);
436 DWORD i
, j
, sepLen
, rdnSepLen
;
438 BOOL reverse
= dwStrType
& CERT_NAME_STR_REVERSE_FLAG
;
439 const CERT_RDN
*rdn
= info
->rgRDN
;
441 if(reverse
&& info
->cRDN
> 1) rdn
+= (info
->cRDN
- 1);
443 if (dwStrType
& CERT_NAME_STR_SEMICOLON_FLAG
)
445 else if (dwStrType
& CERT_NAME_STR_CRLF_FLAG
)
449 sepLen
= strlen(sep
);
450 if (dwStrType
& CERT_NAME_STR_NO_PLUS_FLAG
)
454 rdnSepLen
= strlen(rdnSep
);
455 for (i
= 0; (!psz
|| ret
< csz
) && i
< info
->cRDN
; i
++)
457 for (j
= 0; (!psz
|| ret
< csz
) && j
< rdn
->cRDNAttr
; j
++)
460 char prefixBuf
[13]; /* big enough for SERIALNUMBER */
461 LPCSTR prefix
= NULL
;
463 if ((dwStrType
& 0x000000ff) == CERT_OID_NAME_STR
)
464 prefix
= rdn
->rgRDNAttr
[j
].pszObjId
;
465 else if ((dwStrType
& 0x000000ff) == CERT_X500_NAME_STR
)
467 PCCRYPT_OID_INFO oidInfo
= CryptFindOIDInfo(
468 CRYPT_OID_INFO_OID_KEY
,
469 rdn
->rgRDNAttr
[j
].pszObjId
,
470 CRYPT_RDN_ATTR_OID_GROUP_ID
);
474 WideCharToMultiByte(CP_ACP
, 0, oidInfo
->pwszName
, -1,
475 prefixBuf
, sizeof(prefixBuf
), NULL
, NULL
);
479 prefix
= rdn
->rgRDNAttr
[j
].pszObjId
;
483 /* - 1 is needed to account for the NULL terminator. */
484 chars
= CRYPT_AddPrefixA(prefix
,
485 psz
? psz
+ ret
: NULL
, psz
? csz
- ret
- 1 : 0);
488 chars
= quote_rdn_value_to_str_a(
489 rdn
->rgRDNAttr
[j
].dwValueType
,
490 &rdn
->rgRDNAttr
[j
].Value
, psz
? psz
+ ret
: NULL
,
491 psz
? csz
- ret
: 0);
494 if (j
< rdn
->cRDNAttr
- 1)
496 if (psz
&& ret
< csz
- rdnSepLen
- 1)
497 memcpy(psz
+ ret
, rdnSep
, rdnSepLen
);
501 if (i
< info
->cRDN
- 1)
503 if (psz
&& ret
< csz
- sepLen
- 1)
504 memcpy(psz
+ ret
, sep
, sepLen
);
519 TRACE("Returning %s\n", debugstr_a(psz
));
523 /* Adds the prefix prefix to the wide-character string pointed to by psz,
524 * followed by the character '='. Copies no more than csz characters. Returns
525 * the number of characters copied. If psz is NULL, returns the number of
526 * characters that would be copied.
527 * Assumes the characters in prefix are ASCII (not multibyte characters.)
529 static DWORD
CRYPT_AddPrefixAToW(LPCSTR prefix
, LPWSTR psz
, DWORD csz
)
533 TRACE("(%s, %p, %d)\n", debugstr_a(prefix
), psz
, csz
);
539 chars
= min(strlen(prefix
), csz
);
540 for (i
= 0; i
< chars
; i
++)
541 *(psz
+ i
) = prefix
[i
];
542 *(psz
+ chars
) = '=';
546 chars
= lstrlenA(prefix
) + 1;
550 /* Adds the prefix prefix to the string pointed to by psz, followed by the
551 * character '='. Copies no more than csz characters. Returns the number of
552 * characters copied. If psz is NULL, returns the number of characters that
555 static DWORD
CRYPT_AddPrefixW(LPCWSTR prefix
, LPWSTR psz
, DWORD csz
)
559 TRACE("(%s, %p, %d)\n", debugstr_w(prefix
), psz
, csz
);
563 chars
= min(strlenW(prefix
), csz
);
564 memcpy(psz
, prefix
, chars
* sizeof(WCHAR
));
565 *(psz
+ chars
) = '=';
569 chars
= lstrlenW(prefix
) + 1;
573 static const WCHAR indent
[] = { ' ',' ',' ',' ',' ',0 };
575 DWORD
cert_name_to_str_with_indent(DWORD dwCertEncodingType
, DWORD indentLevel
,
576 const CERT_NAME_BLOB
*pName
, DWORD dwStrType
, LPWSTR psz
, DWORD csz
)
578 static const DWORD unsupportedFlags
= CERT_NAME_STR_NO_QUOTING_FLAG
|
579 CERT_NAME_STR_ENABLE_T61_UNICODE_FLAG
;
580 static const WCHAR commaSep
[] = { ',',' ',0 };
581 static const WCHAR semiSep
[] = { ';',' ',0 };
582 static const WCHAR crlfSep
[] = { '\r','\n',0 };
583 static const WCHAR plusSep
[] = { ' ','+',' ',0 };
584 static const WCHAR spaceSep
[] = { ' ',0 };
585 DWORD ret
= 0, bytes
= 0;
587 CERT_NAME_INFO
*info
;
589 if (dwStrType
& unsupportedFlags
)
590 FIXME("unsupported flags: %08x\n", dwStrType
& unsupportedFlags
);
592 bRet
= CryptDecodeObjectEx(dwCertEncodingType
, X509_NAME
, pName
->pbData
,
593 pName
->cbData
, CRYPT_DECODE_ALLOC_FLAG
, NULL
, &info
, &bytes
);
596 DWORD i
, j
, sepLen
, rdnSepLen
;
598 BOOL reverse
= dwStrType
& CERT_NAME_STR_REVERSE_FLAG
;
599 const CERT_RDN
*rdn
= info
->rgRDN
;
601 if(reverse
&& info
->cRDN
> 1) rdn
+= (info
->cRDN
- 1);
603 if (dwStrType
& CERT_NAME_STR_SEMICOLON_FLAG
)
605 else if (dwStrType
& CERT_NAME_STR_CRLF_FLAG
)
609 sepLen
= lstrlenW(sep
);
610 if (dwStrType
& CERT_NAME_STR_NO_PLUS_FLAG
)
614 rdnSepLen
= lstrlenW(rdnSep
);
615 for (i
= 0; (!psz
|| ret
< csz
) && i
< info
->cRDN
; i
++)
617 for (j
= 0; (!psz
|| ret
< csz
) && j
< rdn
->cRDNAttr
; j
++)
620 LPCSTR prefixA
= NULL
;
621 LPCWSTR prefixW
= NULL
;
623 if ((dwStrType
& 0x000000ff) == CERT_OID_NAME_STR
)
624 prefixA
= rdn
->rgRDNAttr
[j
].pszObjId
;
625 else if ((dwStrType
& 0x000000ff) == CERT_X500_NAME_STR
)
627 PCCRYPT_OID_INFO oidInfo
= CryptFindOIDInfo(
628 CRYPT_OID_INFO_OID_KEY
,
629 rdn
->rgRDNAttr
[j
].pszObjId
,
630 CRYPT_RDN_ATTR_OID_GROUP_ID
);
633 prefixW
= oidInfo
->pwszName
;
635 prefixA
= rdn
->rgRDNAttr
[j
].pszObjId
;
637 if (dwStrType
& CERT_NAME_STR_CRLF_FLAG
)
641 for (k
= 0; k
< indentLevel
; k
++)
645 chars
= min(strlenW(indent
), csz
- ret
- 1);
646 memcpy(psz
+ ret
, indent
, chars
* sizeof(WCHAR
));
649 chars
= strlenW(indent
);
655 /* - 1 is needed to account for the NULL terminator. */
656 chars
= CRYPT_AddPrefixW(prefixW
,
657 psz
? psz
+ ret
: NULL
, psz
? csz
- ret
- 1 : 0);
662 /* - 1 is needed to account for the NULL terminator. */
663 chars
= CRYPT_AddPrefixAToW(prefixA
,
664 psz
? psz
+ ret
: NULL
, psz
? csz
- ret
- 1 : 0);
667 chars
= quote_rdn_value_to_str_w(
668 rdn
->rgRDNAttr
[j
].dwValueType
,
669 &rdn
->rgRDNAttr
[j
].Value
, psz
? psz
+ ret
: NULL
,
670 psz
? csz
- ret
: 0);
673 if (j
< rdn
->cRDNAttr
- 1)
675 if (psz
&& ret
< csz
- rdnSepLen
- 1)
676 memcpy(psz
+ ret
, rdnSep
, rdnSepLen
* sizeof(WCHAR
));
680 if (i
< info
->cRDN
- 1)
682 if (psz
&& ret
< csz
- sepLen
- 1)
683 memcpy(psz
+ ret
, sep
, sepLen
* sizeof(WCHAR
));
701 DWORD WINAPI
CertNameToStrW(DWORD dwCertEncodingType
, PCERT_NAME_BLOB pName
,
702 DWORD dwStrType
, LPWSTR psz
, DWORD csz
)
706 TRACE("(%d, %p, %08x, %p, %d)\n", dwCertEncodingType
, pName
, dwStrType
,
709 ret
= cert_name_to_str_with_indent(dwCertEncodingType
, 0, pName
, dwStrType
,
711 TRACE("Returning %s\n", debugstr_w(psz
));
715 BOOL WINAPI
CertStrToNameA(DWORD dwCertEncodingType
, LPCSTR pszX500
,
716 DWORD dwStrType
, void *pvReserved
, BYTE
*pbEncoded
, DWORD
*pcbEncoded
,
722 TRACE("(%08x, %s, %08x, %p, %p, %p, %p)\n", dwCertEncodingType
,
723 debugstr_a(pszX500
), dwStrType
, pvReserved
, pbEncoded
, pcbEncoded
,
726 len
= MultiByteToWideChar(CP_ACP
, 0, pszX500
, -1, NULL
, 0);
729 LPWSTR x500
, errorStr
;
731 if ((x500
= CryptMemAlloc(len
* sizeof(WCHAR
))))
733 MultiByteToWideChar(CP_ACP
, 0, pszX500
, -1, x500
, len
);
734 ret
= CertStrToNameW(dwCertEncodingType
, x500
, dwStrType
,
735 pvReserved
, pbEncoded
, pcbEncoded
,
736 ppszError
? (LPCWSTR
*)&errorStr
: NULL
);
743 *ppszError
= pszX500
;
744 for (i
= 0; i
< errorStr
- x500
; i
++)
745 *ppszError
= CharNextA(*ppszError
);
754 SetLastError(ERROR_OUTOFMEMORY
);
760 SetLastError(CRYPT_E_INVALID_X500_STRING
);
762 *ppszError
= pszX500
;
770 WCHAR buf
[10]; /* big enough for L"GivenName" */
771 LPWSTR keyName
; /* usually = buf, but may be allocated */
775 static void CRYPT_InitializeKeynameKeeper(struct KeynameKeeper
*keeper
)
777 keeper
->keyName
= keeper
->buf
;
778 keeper
->keyLen
= sizeof(keeper
->buf
) / sizeof(keeper
->buf
[0]);
781 static void CRYPT_FreeKeynameKeeper(struct KeynameKeeper
*keeper
)
783 if (keeper
->keyName
!= keeper
->buf
)
784 CryptMemFree(keeper
->keyName
);
793 static void CRYPT_KeynameKeeperFromTokenW(struct KeynameKeeper
*keeper
,
794 const struct X500TokenW
*key
)
796 DWORD len
= key
->end
- key
->start
;
798 if (len
> keeper
->keyLen
)
800 if (keeper
->keyName
== keeper
->buf
)
801 keeper
->keyName
= CryptMemAlloc(len
* sizeof(WCHAR
));
803 keeper
->keyName
= CryptMemRealloc(keeper
->keyName
,
804 len
* sizeof(WCHAR
));
805 keeper
->keyLen
= len
;
807 memcpy(keeper
->keyName
, key
->start
, (key
->end
- key
->start
) *
809 keeper
->keyName
[len
] = '\0';
810 TRACE("Keyname is %s\n", debugstr_w(keeper
->keyName
));
813 static BOOL
CRYPT_GetNextKeyW(LPCWSTR str
, struct X500TokenW
*token
,
818 while (*str
&& isspaceW(*str
))
823 while (*str
&& *str
!= '=' && !isspaceW(*str
))
825 if (*str
&& (*str
== '=' || isspaceW(*str
)))
829 TRACE("missing equals char at %s\n", debugstr_w(token
->start
));
831 *ppszError
= token
->start
;
832 SetLastError(CRYPT_E_INVALID_X500_STRING
);
841 /* Assumes separators are characters in the 0-255 range */
842 static BOOL
CRYPT_GetNextValueW(LPCWSTR str
, DWORD dwFlags
, LPCWSTR separators
,
843 WCHAR
*separator_used
, struct X500TokenW
*token
, LPCWSTR
*ppszError
)
847 TRACE("(%s, %s, %p, %p)\n", debugstr_w(str
), debugstr_w(separators
), token
,
851 while (*str
&& isspaceW(*str
))
856 if (!(dwFlags
& CERT_NAME_STR_NO_QUOTING_FLAG
) && *str
== '"')
860 while (!token
->end
&& ret
)
862 while (*str
&& *str
!= '"')
866 if (*(str
+ 1) != '"')
867 token
->end
= str
+ 1;
873 TRACE("unterminated quote at %s\n", debugstr_w(str
));
876 SetLastError(CRYPT_E_INVALID_X500_STRING
);
883 WCHAR map
[256] = { 0 };
886 map
[*separators
++] = 1;
887 while (*str
&& (*str
>= 0xff || !map
[*str
]))
890 if (map
[*str
]) *separator_used
= *str
;
895 TRACE("missing value at %s\n", debugstr_w(str
));
898 SetLastError(CRYPT_E_INVALID_X500_STRING
);
904 /* Encodes the string represented by value as the string type type into the
905 * CERT_NAME_BLOB output. If there is an error and ppszError is not NULL,
906 * *ppszError is set to the first failing character. If there is no error,
907 * output's pbData must be freed with LocalFree.
909 static BOOL
CRYPT_EncodeValueWithType(DWORD dwCertEncodingType
,
910 const struct X500TokenW
*value
, PCERT_NAME_BLOB output
, DWORD type
,
913 CERT_NAME_VALUE nameValue
= { type
, { 0, NULL
} };
916 if (value
->end
> value
->start
)
921 nameValue
.Value
.pbData
= CryptMemAlloc((value
->end
- value
->start
+ 1) *
923 if (!nameValue
.Value
.pbData
)
925 SetLastError(ERROR_OUTOFMEMORY
);
928 ptr
= (LPWSTR
)nameValue
.Value
.pbData
;
929 for (i
= 0; i
< value
->end
- value
->start
; i
++)
931 *ptr
++ = value
->start
[i
];
932 if (value
->start
[i
] == '"')
935 /* The string is NULL terminated because of a quirk in encoding
936 * unicode names values: if the length is given as 0, the value is
937 * assumed to be a NULL-terminated string.
940 nameValue
.Value
.cbData
= (LPBYTE
)ptr
- nameValue
.Value
.pbData
;
942 ret
= CryptEncodeObjectEx(dwCertEncodingType
, X509_UNICODE_NAME_VALUE
,
943 &nameValue
, CRYPT_ENCODE_ALLOC_FLAG
, NULL
, &output
->pbData
,
945 if (!ret
&& ppszError
)
947 if (type
== CERT_RDN_NUMERIC_STRING
&&
948 GetLastError() == CRYPT_E_INVALID_NUMERIC_STRING
)
949 *ppszError
= value
->start
+ output
->cbData
;
950 else if (type
== CERT_RDN_PRINTABLE_STRING
&&
951 GetLastError() == CRYPT_E_INVALID_PRINTABLE_STRING
)
952 *ppszError
= value
->start
+ output
->cbData
;
953 else if (type
== CERT_RDN_IA5_STRING
&&
954 GetLastError() == CRYPT_E_INVALID_IA5_STRING
)
955 *ppszError
= value
->start
+ output
->cbData
;
957 CryptMemFree(nameValue
.Value
.pbData
);
961 static BOOL
CRYPT_EncodeValue(DWORD dwCertEncodingType
,
962 const struct X500TokenW
*value
, PCERT_NAME_BLOB output
, const DWORD
*types
,
969 for (i
= 0; !ret
&& types
[i
]; i
++)
970 ret
= CRYPT_EncodeValueWithType(dwCertEncodingType
, value
, output
,
971 types
[i
], ppszError
);
975 static BOOL
CRYPT_ValueToRDN(DWORD dwCertEncodingType
, PCERT_NAME_INFO info
,
976 PCCRYPT_OID_INFO keyOID
, struct X500TokenW
*value
, DWORD dwStrType
, LPCWSTR
*ppszError
)
980 TRACE("OID %s, value %s\n", debugstr_a(keyOID
->pszOID
),
981 debugstr_wn(value
->start
, value
->end
- value
->start
));
984 info
->rgRDN
= CryptMemAlloc(sizeof(CERT_RDN
));
986 info
->rgRDN
= CryptMemRealloc(info
->rgRDN
,
987 (info
->cRDN
+ 1) * sizeof(CERT_RDN
));
990 /* FIXME: support multiple RDN attrs */
991 info
->rgRDN
[info
->cRDN
].rgRDNAttr
=
992 CryptMemAlloc(sizeof(CERT_RDN_ATTR
));
993 if (info
->rgRDN
[info
->cRDN
].rgRDNAttr
)
995 static const DWORD defaultTypes
[] = { CERT_RDN_PRINTABLE_STRING
,
996 CERT_RDN_BMP_STRING
, 0 };
999 info
->rgRDN
[info
->cRDN
].cRDNAttr
= 1;
1000 info
->rgRDN
[info
->cRDN
].rgRDNAttr
[0].pszObjId
=
1001 (LPSTR
)keyOID
->pszOID
;
1002 info
->rgRDN
[info
->cRDN
].rgRDNAttr
[0].dwValueType
=
1003 CERT_RDN_ENCODED_BLOB
;
1004 if (keyOID
->ExtraInfo
.cbData
)
1005 types
= (const DWORD
*)keyOID
->ExtraInfo
.pbData
;
1007 types
= defaultTypes
;
1009 /* Remove surrounding quotes */
1010 if (value
->start
[0] == '"' && !(dwStrType
& CERT_NAME_STR_NO_QUOTING_FLAG
))
1015 ret
= CRYPT_EncodeValue(dwCertEncodingType
, value
,
1016 &info
->rgRDN
[info
->cRDN
].rgRDNAttr
[0].Value
, types
, ppszError
);
1019 SetLastError(ERROR_OUTOFMEMORY
);
1023 SetLastError(ERROR_OUTOFMEMORY
);
1027 BOOL WINAPI
CertStrToNameW(DWORD dwCertEncodingType
, LPCWSTR pszX500
,
1028 DWORD dwStrType
, void *pvReserved
, BYTE
*pbEncoded
, DWORD
*pcbEncoded
,
1031 CERT_NAME_INFO info
= { 0, NULL
};
1033 struct KeynameKeeper keeper
;
1037 TRACE("(%08x, %s, %08x, %p, %p, %p, %p)\n", dwCertEncodingType
,
1038 debugstr_w(pszX500
), dwStrType
, pvReserved
, pbEncoded
, pcbEncoded
,
1041 CRYPT_InitializeKeynameKeeper(&keeper
);
1043 while (str
&& *str
&& ret
)
1045 struct X500TokenW token
;
1047 ret
= CRYPT_GetNextKeyW(str
, &token
, ppszError
);
1048 if (ret
&& token
.start
)
1050 PCCRYPT_OID_INFO keyOID
;
1052 CRYPT_KeynameKeeperFromTokenW(&keeper
, &token
);
1053 keyOID
= CryptFindOIDInfo(CRYPT_OID_INFO_NAME_KEY
, keeper
.keyName
,
1054 CRYPT_RDN_ATTR_OID_GROUP_ID
);
1058 *ppszError
= token
.start
;
1059 SetLastError(CRYPT_E_INVALID_X500_STRING
);
1065 while (isspace(*str
))
1071 SetLastError(CRYPT_E_INVALID_X500_STRING
);
1076 static const WCHAR commaSep
[] = { ',',0 };
1077 static const WCHAR semiSep
[] = { ';',0 };
1078 static const WCHAR crlfSep
[] = { '\r','\n',0 };
1079 static const WCHAR allSepsWithoutPlus
[] = { ',',';','\r','\n',0 };
1080 static const WCHAR allSeps
[] = { '+',',',';','\r','\n',0 };
1085 if (dwStrType
& CERT_NAME_STR_COMMA_FLAG
)
1087 else if (dwStrType
& CERT_NAME_STR_SEMICOLON_FLAG
)
1089 else if (dwStrType
& CERT_NAME_STR_CRLF_FLAG
)
1091 else if (dwStrType
& CERT_NAME_STR_NO_PLUS_FLAG
)
1092 sep
= allSepsWithoutPlus
;
1095 ret
= CRYPT_GetNextValueW(str
, dwStrType
, sep
, &sep_used
, &token
,
1100 /* if token.end points to the separator, skip it */
1101 if (str
&& sep_used
&& *str
== sep_used
) str
++;
1103 ret
= CRYPT_ValueToRDN(dwCertEncodingType
, &info
,
1104 keyOID
, &token
, dwStrType
, ppszError
);
1110 CRYPT_FreeKeynameKeeper(&keeper
);
1115 ret
= CryptEncodeObjectEx(dwCertEncodingType
, X509_NAME
, &info
,
1116 0, NULL
, pbEncoded
, pcbEncoded
);
1118 for (i
= 0; i
< info
.cRDN
; i
++)
1122 for (j
= 0; j
< info
.rgRDN
[i
].cRDNAttr
; j
++)
1123 LocalFree(info
.rgRDN
[i
].rgRDNAttr
[j
].Value
.pbData
);
1124 CryptMemFree(info
.rgRDN
[i
].rgRDNAttr
);
1126 CryptMemFree(info
.rgRDN
);
1130 DWORD WINAPI
CertGetNameStringA(PCCERT_CONTEXT pCertContext
, DWORD dwType
,
1131 DWORD dwFlags
, void *pvTypePara
, LPSTR pszNameString
, DWORD cchNameString
)
1135 TRACE("(%p, %d, %08x, %p, %p, %d)\n", pCertContext
, dwType
, dwFlags
,
1136 pvTypePara
, pszNameString
, cchNameString
);
1143 nameLen
= CertGetNameStringW(pCertContext
, dwType
, dwFlags
, pvTypePara
,
1145 wideName
= CryptMemAlloc(nameLen
* sizeof(WCHAR
));
1148 CertGetNameStringW(pCertContext
, dwType
, dwFlags
, pvTypePara
,
1150 nameLen
= WideCharToMultiByte(CP_ACP
, 0, wideName
, nameLen
,
1151 pszNameString
, cchNameString
, NULL
, NULL
);
1152 if (nameLen
<= cchNameString
)
1156 pszNameString
[cchNameString
- 1] = '\0';
1157 ret
= cchNameString
;
1159 CryptMemFree(wideName
);
1163 *pszNameString
= '\0';
1168 ret
= CertGetNameStringW(pCertContext
, dwType
, dwFlags
, pvTypePara
,
1173 /* Searches cert's extensions for the alternate name extension with OID
1174 * altNameOID, and if found, searches it for the alternate name type entryType.
1175 * If found, returns a pointer to the entry, otherwise returns NULL.
1176 * Regardless of whether an entry of the desired type is found, if the
1177 * alternate name extension is present, sets *info to the decoded alternate
1178 * name extension, which you must free using LocalFree.
1179 * The return value is a pointer within *info, so don't free *info before
1180 * you're done with the return value.
1182 static PCERT_ALT_NAME_ENTRY
cert_find_alt_name_entry(PCCERT_CONTEXT cert
,
1183 LPCSTR altNameOID
, DWORD entryType
, PCERT_ALT_NAME_INFO
*info
)
1185 PCERT_ALT_NAME_ENTRY entry
= NULL
;
1186 PCERT_EXTENSION ext
= CertFindExtension(altNameOID
,
1187 cert
->pCertInfo
->cExtension
, cert
->pCertInfo
->rgExtension
);
1193 if (CryptDecodeObjectEx(cert
->dwCertEncodingType
, X509_ALTERNATE_NAME
,
1194 ext
->Value
.pbData
, ext
->Value
.cbData
, CRYPT_DECODE_ALLOC_FLAG
, NULL
,
1199 for (i
= 0; !entry
&& i
< (*info
)->cAltEntry
; i
++)
1200 if ((*info
)->rgAltEntry
[i
].dwAltNameChoice
== entryType
)
1201 entry
= &(*info
)->rgAltEntry
[i
];
1209 static DWORD
cert_get_name_from_rdn_attr(DWORD encodingType
,
1210 const CERT_NAME_BLOB
*name
, LPCSTR oid
, LPWSTR pszNameString
, DWORD cchNameString
)
1212 CERT_NAME_INFO
*nameInfo
;
1213 DWORD bytes
= 0, ret
= 0;
1215 if (CryptDecodeObjectEx(encodingType
, X509_NAME
, name
->pbData
,
1216 name
->cbData
, CRYPT_DECODE_ALLOC_FLAG
, NULL
, &nameInfo
, &bytes
))
1218 PCERT_RDN_ATTR nameAttr
;
1221 oid
= szOID_RSA_emailAddr
;
1222 nameAttr
= CertFindRDNAttr(oid
, nameInfo
);
1224 ret
= CertRDNValueToStrW(nameAttr
->dwValueType
, &nameAttr
->Value
,
1225 pszNameString
, cchNameString
);
1226 LocalFree(nameInfo
);
1231 DWORD WINAPI
CertGetNameStringW(PCCERT_CONTEXT pCertContext
, DWORD dwType
,
1232 DWORD dwFlags
, void *pvTypePara
, LPWSTR pszNameString
, DWORD cchNameString
)
1235 PCERT_NAME_BLOB name
;
1238 TRACE("(%p, %d, %08x, %p, %p, %d)\n", pCertContext
, dwType
,
1239 dwFlags
, pvTypePara
, pszNameString
, cchNameString
);
1244 if (dwFlags
& CERT_NAME_ISSUER_FLAG
)
1246 name
= &pCertContext
->pCertInfo
->Issuer
;
1247 altNameOID
= szOID_ISSUER_ALT_NAME
;
1251 name
= &pCertContext
->pCertInfo
->Subject
;
1252 altNameOID
= szOID_SUBJECT_ALT_NAME
;
1257 case CERT_NAME_EMAIL_TYPE
:
1259 CERT_ALT_NAME_INFO
*info
;
1260 PCERT_ALT_NAME_ENTRY entry
= cert_find_alt_name_entry(pCertContext
,
1261 altNameOID
, CERT_ALT_NAME_RFC822_NAME
, &info
);
1266 ret
= strlenW(entry
->u
.pwszRfc822Name
) + 1;
1267 else if (cchNameString
)
1269 ret
= min(strlenW(entry
->u
.pwszRfc822Name
), cchNameString
- 1);
1270 memcpy(pszNameString
, entry
->u
.pwszRfc822Name
,
1271 ret
* sizeof(WCHAR
));
1272 pszNameString
[ret
++] = 0;
1278 ret
= cert_get_name_from_rdn_attr(pCertContext
->dwCertEncodingType
,
1279 name
, szOID_RSA_emailAddr
, pszNameString
, cchNameString
);
1282 case CERT_NAME_RDN_TYPE
:
1284 DWORD type
= pvTypePara
? *(DWORD
*)pvTypePara
: 0;
1287 ret
= CertNameToStrW(pCertContext
->dwCertEncodingType
, name
,
1288 type
, pszNameString
, cchNameString
);
1291 CERT_ALT_NAME_INFO
*info
;
1292 PCERT_ALT_NAME_ENTRY entry
= cert_find_alt_name_entry(pCertContext
,
1293 altNameOID
, CERT_ALT_NAME_DIRECTORY_NAME
, &info
);
1296 ret
= CertNameToStrW(pCertContext
->dwCertEncodingType
,
1297 &entry
->u
.DirectoryName
, type
, pszNameString
, cchNameString
);
1303 case CERT_NAME_ATTR_TYPE
:
1304 ret
= cert_get_name_from_rdn_attr(pCertContext
->dwCertEncodingType
,
1305 name
, pvTypePara
, pszNameString
, cchNameString
);
1308 CERT_ALT_NAME_INFO
*altInfo
;
1309 PCERT_ALT_NAME_ENTRY entry
= cert_find_alt_name_entry(pCertContext
,
1310 altNameOID
, CERT_ALT_NAME_DIRECTORY_NAME
, &altInfo
);
1313 ret
= cert_name_to_str_with_indent(X509_ASN_ENCODING
, 0,
1314 &entry
->u
.DirectoryName
, 0, pszNameString
, cchNameString
);
1319 case CERT_NAME_SIMPLE_DISPLAY_TYPE
:
1321 static const LPCSTR simpleAttributeOIDs
[] = { szOID_COMMON_NAME
,
1322 szOID_ORGANIZATIONAL_UNIT_NAME
, szOID_ORGANIZATION_NAME
,
1323 szOID_RSA_emailAddr
};
1324 CERT_NAME_INFO
*nameInfo
= NULL
;
1327 if (CryptDecodeObjectEx(pCertContext
->dwCertEncodingType
, X509_NAME
,
1328 name
->pbData
, name
->cbData
, CRYPT_DECODE_ALLOC_FLAG
, NULL
, &nameInfo
,
1331 PCERT_RDN_ATTR nameAttr
= NULL
;
1333 for (i
= 0; !nameAttr
&& i
< sizeof(simpleAttributeOIDs
) /
1334 sizeof(simpleAttributeOIDs
[0]); i
++)
1335 nameAttr
= CertFindRDNAttr(simpleAttributeOIDs
[i
], nameInfo
);
1337 ret
= CertRDNValueToStrW(nameAttr
->dwValueType
,
1338 &nameAttr
->Value
, pszNameString
, cchNameString
);
1339 LocalFree(nameInfo
);
1343 CERT_ALT_NAME_INFO
*altInfo
;
1344 PCERT_ALT_NAME_ENTRY entry
= cert_find_alt_name_entry(pCertContext
,
1345 altNameOID
, CERT_ALT_NAME_RFC822_NAME
, &altInfo
);
1349 if (!entry
&& altInfo
->cAltEntry
)
1350 entry
= &altInfo
->rgAltEntry
[0];
1354 ret
= strlenW(entry
->u
.pwszRfc822Name
) + 1;
1355 else if (cchNameString
)
1357 ret
= min(strlenW(entry
->u
.pwszRfc822Name
),
1359 memcpy(pszNameString
, entry
->u
.pwszRfc822Name
,
1360 ret
* sizeof(WCHAR
));
1361 pszNameString
[ret
++] = 0;
1369 case CERT_NAME_FRIENDLY_DISPLAY_TYPE
:
1371 DWORD cch
= cchNameString
;
1373 if (CertGetCertificateContextProperty(pCertContext
,
1374 CERT_FRIENDLY_NAME_PROP_ID
, pszNameString
, &cch
))
1377 ret
= CertGetNameStringW(pCertContext
,
1378 CERT_NAME_SIMPLE_DISPLAY_TYPE
, dwFlags
, pvTypePara
, pszNameString
,
1382 case CERT_NAME_DNS_TYPE
:
1384 CERT_ALT_NAME_INFO
*info
;
1385 PCERT_ALT_NAME_ENTRY entry
= cert_find_alt_name_entry(pCertContext
,
1386 altNameOID
, CERT_ALT_NAME_DNS_NAME
, &info
);
1391 ret
= strlenW(entry
->u
.pwszDNSName
) + 1;
1392 else if (cchNameString
)
1394 ret
= min(strlenW(entry
->u
.pwszDNSName
), cchNameString
- 1);
1395 memcpy(pszNameString
, entry
->u
.pwszDNSName
, ret
* sizeof(WCHAR
));
1396 pszNameString
[ret
++] = 0;
1402 ret
= cert_get_name_from_rdn_attr(pCertContext
->dwCertEncodingType
,
1403 name
, szOID_COMMON_NAME
, pszNameString
, cchNameString
);
1406 case CERT_NAME_URL_TYPE
:
1408 CERT_ALT_NAME_INFO
*info
;
1409 PCERT_ALT_NAME_ENTRY entry
= cert_find_alt_name_entry(pCertContext
,
1410 altNameOID
, CERT_ALT_NAME_URL
, &info
);
1415 ret
= strlenW(entry
->u
.pwszURL
) + 1;
1416 else if (cchNameString
)
1418 ret
= min(strlenW(entry
->u
.pwszURL
), cchNameString
- 1);
1419 memcpy(pszNameString
, entry
->u
.pwszURL
, ret
* sizeof(WCHAR
));
1420 pszNameString
[ret
++] = 0;
1428 FIXME("unimplemented for type %d\n", dwType
);
1436 else if (cchNameString
)
1438 pszNameString
[0] = 0;