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
19 #include "crypt32_private.h"
21 WINE_DEFAULT_DEBUG_CHANNEL(crypt
);
23 DWORD WINAPI
CertRDNValueToStrA(DWORD dwValueType
, PCERT_RDN_VALUE_BLOB pValue
,
28 TRACE("(%d, %p, %p, %d)\n", dwValueType
, pValue
, psz
, csz
);
32 case CERT_RDN_ANY_TYPE
:
34 case CERT_RDN_NUMERIC_STRING
:
35 case CERT_RDN_PRINTABLE_STRING
:
36 case CERT_RDN_TELETEX_STRING
:
37 case CERT_RDN_VIDEOTEX_STRING
:
38 case CERT_RDN_IA5_STRING
:
39 case CERT_RDN_GRAPHIC_STRING
:
40 case CERT_RDN_VISIBLE_STRING
:
41 case CERT_RDN_GENERAL_STRING
:
47 DWORD chars
= min(len
, csz
- 1);
51 memcpy(psz
, pValue
->pbData
, chars
);
57 case CERT_RDN_BMP_STRING
:
58 case CERT_RDN_UTF8_STRING
:
59 len
= WideCharToMultiByte(CP_ACP
, 0, (LPCWSTR
)pValue
->pbData
,
60 pValue
->cbData
/ sizeof(WCHAR
), NULL
, 0, NULL
, NULL
);
65 DWORD chars
= min(pValue
->cbData
/ sizeof(WCHAR
), csz
- 1);
69 ret
= WideCharToMultiByte(CP_ACP
, 0, (LPCWSTR
)pValue
->pbData
,
70 chars
, psz
, csz
- 1, NULL
, NULL
);
76 FIXME("string type %d unimplemented\n", dwValueType
);
86 TRACE("returning %d (%s)\n", ret
, debugstr_a(psz
));
90 DWORD WINAPI
CertRDNValueToStrW(DWORD dwValueType
, PCERT_RDN_VALUE_BLOB pValue
,
91 LPWSTR psz
, DWORD csz
)
93 DWORD ret
= 0, len
, i
, strLen
;
95 TRACE("(%d, %p, %p, %d)\n", dwValueType
, pValue
, psz
, csz
);
99 case CERT_RDN_ANY_TYPE
:
101 case CERT_RDN_NUMERIC_STRING
:
102 case CERT_RDN_PRINTABLE_STRING
:
103 case CERT_RDN_TELETEX_STRING
:
104 case CERT_RDN_VIDEOTEX_STRING
:
105 case CERT_RDN_IA5_STRING
:
106 case CERT_RDN_GRAPHIC_STRING
:
107 case CERT_RDN_VISIBLE_STRING
:
108 case CERT_RDN_GENERAL_STRING
:
109 len
= pValue
->cbData
;
116 for (i
= 0; i
< pValue
->cbData
&& ptr
- psz
< csz
; ptr
++, i
++)
117 *ptr
= pValue
->pbData
[i
];
121 case CERT_RDN_BMP_STRING
:
122 case CERT_RDN_UTF8_STRING
:
123 strLen
= len
= pValue
->cbData
/ sizeof(WCHAR
);
130 for (i
= 0; i
< strLen
&& ptr
- psz
< csz
; ptr
++, i
++)
131 *ptr
= ((LPCWSTR
)pValue
->pbData
)[i
];
136 FIXME("string type %d unimplemented\n", dwValueType
);
146 TRACE("returning %d (%s)\n", ret
, debugstr_w(psz
));
150 static inline BOOL
is_quotable_char(char c
)
169 static DWORD
quote_rdn_value_to_str_a(DWORD dwValueType
,
170 PCERT_RDN_VALUE_BLOB pValue
, LPSTR psz
, DWORD csz
)
172 DWORD ret
= 0, len
, i
;
173 BOOL needsQuotes
= FALSE
;
175 TRACE("(%d, %p, %p, %d)\n", dwValueType
, pValue
, psz
, csz
);
179 case CERT_RDN_ANY_TYPE
:
181 case CERT_RDN_NUMERIC_STRING
:
182 case CERT_RDN_PRINTABLE_STRING
:
183 case CERT_RDN_TELETEX_STRING
:
184 case CERT_RDN_VIDEOTEX_STRING
:
185 case CERT_RDN_IA5_STRING
:
186 case CERT_RDN_GRAPHIC_STRING
:
187 case CERT_RDN_VISIBLE_STRING
:
188 case CERT_RDN_GENERAL_STRING
:
189 len
= pValue
->cbData
;
190 if (pValue
->cbData
&& isspace(pValue
->pbData
[0]))
192 if (pValue
->cbData
&& isspace(pValue
->pbData
[pValue
->cbData
- 1]))
194 for (i
= 0; i
< pValue
->cbData
; i
++)
196 if (is_quotable_char(pValue
->pbData
[i
]))
198 if (pValue
->pbData
[i
] == '"')
211 for (i
= 0; i
< pValue
->cbData
&& ptr
- psz
< csz
; ptr
++, i
++)
213 *ptr
= pValue
->pbData
[i
];
214 if (pValue
->pbData
[i
] == '"' && ptr
- psz
< csz
- 1)
217 if (needsQuotes
&& ptr
- psz
< csz
)
222 case CERT_RDN_BMP_STRING
:
223 case CERT_RDN_UTF8_STRING
:
224 len
= WideCharToMultiByte(CP_ACP
, 0, (LPCWSTR
)pValue
->pbData
,
225 pValue
->cbData
/ sizeof(WCHAR
), NULL
, 0, NULL
, NULL
);
226 if (pValue
->cbData
&& isspaceW(((LPCWSTR
)pValue
->pbData
)[0]))
228 if (pValue
->cbData
&&
229 isspaceW(((LPCWSTR
)pValue
->pbData
)[pValue
->cbData
/ sizeof(WCHAR
)-1]))
231 for (i
= 0; i
< pValue
->cbData
/ sizeof(WCHAR
); i
++)
233 if (is_quotable_char(((LPCWSTR
)pValue
->pbData
)[i
]))
235 if (((LPCWSTR
)pValue
->pbData
)[i
] == '"')
248 for (i
= 0; i
< pValue
->cbData
/ sizeof(WCHAR
) &&
249 dst
- psz
< csz
; dst
++, i
++)
251 LPCWSTR src
= (LPCWSTR
)pValue
->pbData
+ i
;
253 WideCharToMultiByte(CP_ACP
, 0, src
, 1, dst
,
254 csz
- (dst
- psz
) - 1, NULL
, NULL
);
255 if (*src
== '"' && dst
- psz
< csz
- 1)
258 if (needsQuotes
&& dst
- psz
< csz
)
264 FIXME("string type %d unimplemented\n", dwValueType
);
274 TRACE("returning %d (%s)\n", ret
, debugstr_a(psz
));
278 static DWORD
quote_rdn_value_to_str_w(DWORD dwValueType
,
279 PCERT_RDN_VALUE_BLOB pValue
, LPWSTR psz
, DWORD csz
)
281 DWORD ret
= 0, len
, i
, strLen
;
282 BOOL needsQuotes
= FALSE
;
284 TRACE("(%d, %p, %p, %d)\n", dwValueType
, pValue
, psz
, csz
);
288 case CERT_RDN_ANY_TYPE
:
290 case CERT_RDN_NUMERIC_STRING
:
291 case CERT_RDN_PRINTABLE_STRING
:
292 case CERT_RDN_TELETEX_STRING
:
293 case CERT_RDN_VIDEOTEX_STRING
:
294 case CERT_RDN_IA5_STRING
:
295 case CERT_RDN_GRAPHIC_STRING
:
296 case CERT_RDN_VISIBLE_STRING
:
297 case CERT_RDN_GENERAL_STRING
:
298 len
= pValue
->cbData
;
299 if (pValue
->cbData
&& isspace(pValue
->pbData
[0]))
301 if (pValue
->cbData
&& isspace(pValue
->pbData
[pValue
->cbData
- 1]))
303 for (i
= 0; i
< pValue
->cbData
; i
++)
305 if (is_quotable_char(pValue
->pbData
[i
]))
307 if (pValue
->pbData
[i
] == '"')
320 for (i
= 0; i
< pValue
->cbData
&& ptr
- psz
< csz
; ptr
++, i
++)
322 *ptr
= pValue
->pbData
[i
];
323 if (pValue
->pbData
[i
] == '"' && ptr
- psz
< csz
- 1)
326 if (needsQuotes
&& ptr
- psz
< csz
)
331 case CERT_RDN_BMP_STRING
:
332 case CERT_RDN_UTF8_STRING
:
333 strLen
= len
= pValue
->cbData
/ sizeof(WCHAR
);
334 if (pValue
->cbData
&& isspace(pValue
->pbData
[0]))
336 if (pValue
->cbData
&& isspace(pValue
->pbData
[strLen
- 1]))
338 for (i
= 0; i
< strLen
; i
++)
340 if (is_quotable_char(((LPCWSTR
)pValue
->pbData
)[i
]))
342 if (((LPCWSTR
)pValue
->pbData
)[i
] == '"')
355 for (i
= 0; i
< strLen
&& ptr
- psz
< csz
; ptr
++, i
++)
357 *ptr
= ((LPCWSTR
)pValue
->pbData
)[i
];
358 if (((LPCWSTR
)pValue
->pbData
)[i
] == '"' && ptr
- psz
< csz
- 1)
361 if (needsQuotes
&& ptr
- psz
< csz
)
367 FIXME("string type %d unimplemented\n", dwValueType
);
377 TRACE("returning %d (%s)\n", ret
, debugstr_w(psz
));
381 /* Adds the prefix prefix to the string pointed to by psz, followed by the
382 * character '='. Copies no more than csz characters. Returns the number of
383 * characters copied. If psz is NULL, returns the number of characters that
386 static DWORD
CRYPT_AddPrefixA(LPCSTR prefix
, LPSTR psz
, DWORD csz
)
390 TRACE("(%s, %p, %d)\n", debugstr_a(prefix
), psz
, csz
);
394 chars
= min(strlen(prefix
), csz
);
395 memcpy(psz
, prefix
, chars
);
396 *(psz
+ chars
) = '=';
400 chars
= lstrlenA(prefix
) + 1;
404 DWORD WINAPI
CertNameToStrA(DWORD dwCertEncodingType
, PCERT_NAME_BLOB pName
,
405 DWORD dwStrType
, LPSTR psz
, DWORD csz
)
407 static const DWORD unsupportedFlags
= CERT_NAME_STR_NO_QUOTING_FLAG
|
408 CERT_NAME_STR_ENABLE_T61_UNICODE_FLAG
;
409 static const char commaSep
[] = ", ";
410 static const char semiSep
[] = "; ";
411 static const char crlfSep
[] = "\r\n";
412 static const char plusSep
[] = " + ";
413 static const char spaceSep
[] = " ";
414 DWORD ret
= 0, bytes
= 0;
416 CERT_NAME_INFO
*info
;
418 TRACE("(%d, %p, %08x, %p, %d)\n", dwCertEncodingType
, pName
, dwStrType
,
420 if (dwStrType
& unsupportedFlags
)
421 FIXME("unsupported flags: %08x\n", dwStrType
& unsupportedFlags
);
423 bRet
= CryptDecodeObjectEx(dwCertEncodingType
, X509_NAME
, pName
->pbData
,
424 pName
->cbData
, CRYPT_DECODE_ALLOC_FLAG
, NULL
, &info
, &bytes
);
427 DWORD i
, j
, sepLen
, rdnSepLen
;
429 BOOL reverse
= dwStrType
& CERT_NAME_STR_REVERSE_FLAG
;
430 const CERT_RDN
*rdn
= info
->rgRDN
;
432 if(reverse
&& info
->cRDN
> 1) rdn
+= (info
->cRDN
- 1);
434 if (dwStrType
& CERT_NAME_STR_SEMICOLON_FLAG
)
436 else if (dwStrType
& CERT_NAME_STR_CRLF_FLAG
)
440 sepLen
= strlen(sep
);
441 if (dwStrType
& CERT_NAME_STR_NO_PLUS_FLAG
)
445 rdnSepLen
= strlen(rdnSep
);
446 for (i
= 0; (!psz
|| ret
< csz
) && i
< info
->cRDN
; i
++)
448 for (j
= 0; (!psz
|| ret
< csz
) && j
< rdn
->cRDNAttr
; j
++)
451 char prefixBuf
[13]; /* big enough for SERIALNUMBER */
452 LPCSTR prefix
= NULL
;
454 if ((dwStrType
& 0x000000ff) == CERT_OID_NAME_STR
)
455 prefix
= rdn
->rgRDNAttr
[j
].pszObjId
;
456 else if ((dwStrType
& 0x000000ff) == CERT_X500_NAME_STR
)
458 PCCRYPT_OID_INFO oidInfo
= CryptFindOIDInfo(
459 CRYPT_OID_INFO_OID_KEY
,
460 rdn
->rgRDNAttr
[j
].pszObjId
,
461 CRYPT_RDN_ATTR_OID_GROUP_ID
);
465 WideCharToMultiByte(CP_ACP
, 0, oidInfo
->pwszName
, -1,
466 prefixBuf
, sizeof(prefixBuf
), NULL
, NULL
);
470 prefix
= rdn
->rgRDNAttr
[j
].pszObjId
;
474 /* - 1 is needed to account for the NULL terminator. */
475 chars
= CRYPT_AddPrefixA(prefix
,
476 psz
? psz
+ ret
: NULL
, psz
? csz
- ret
- 1 : 0);
479 chars
= quote_rdn_value_to_str_a(
480 rdn
->rgRDNAttr
[j
].dwValueType
,
481 &rdn
->rgRDNAttr
[j
].Value
, psz
? psz
+ ret
: NULL
,
482 psz
? csz
- ret
: 0);
485 if (j
< rdn
->cRDNAttr
- 1)
487 if (psz
&& ret
< csz
- rdnSepLen
- 1)
488 memcpy(psz
+ ret
, rdnSep
, rdnSepLen
);
492 if (i
< info
->cRDN
- 1)
494 if (psz
&& ret
< csz
- sepLen
- 1)
495 memcpy(psz
+ ret
, sep
, sepLen
);
510 TRACE("Returning %s\n", debugstr_a(psz
));
514 /* Adds the prefix prefix to the wide-character string pointed to by psz,
515 * followed by the character '='. Copies no more than csz characters. Returns
516 * the number of characters copied. If psz is NULL, returns the number of
517 * characters that would be copied.
518 * Assumes the characters in prefix are ASCII (not multibyte characters.)
520 static DWORD
CRYPT_AddPrefixAToW(LPCSTR prefix
, LPWSTR psz
, DWORD csz
)
524 TRACE("(%s, %p, %d)\n", debugstr_a(prefix
), psz
, csz
);
530 chars
= min(strlen(prefix
), csz
);
531 for (i
= 0; i
< chars
; i
++)
532 *(psz
+ i
) = prefix
[i
];
533 *(psz
+ chars
) = '=';
537 chars
= lstrlenA(prefix
) + 1;
541 /* Adds the prefix prefix to the string pointed to by psz, followed by the
542 * character '='. Copies no more than csz characters. Returns the number of
543 * characters copied. If psz is NULL, returns the number of characters that
546 static DWORD
CRYPT_AddPrefixW(LPCWSTR prefix
, LPWSTR psz
, DWORD csz
)
550 TRACE("(%s, %p, %d)\n", debugstr_w(prefix
), psz
, csz
);
554 chars
= min(strlenW(prefix
), csz
);
555 memcpy(psz
, prefix
, chars
* sizeof(WCHAR
));
556 *(psz
+ chars
) = '=';
560 chars
= lstrlenW(prefix
) + 1;
564 static const WCHAR indent
[] = { ' ',' ',' ',' ',' ',0 };
566 DWORD
cert_name_to_str_with_indent(DWORD dwCertEncodingType
, DWORD indentLevel
,
567 const CERT_NAME_BLOB
*pName
, DWORD dwStrType
, LPWSTR psz
, DWORD csz
)
569 static const DWORD unsupportedFlags
= CERT_NAME_STR_NO_QUOTING_FLAG
|
570 CERT_NAME_STR_ENABLE_T61_UNICODE_FLAG
;
571 static const WCHAR commaSep
[] = { ',',' ',0 };
572 static const WCHAR semiSep
[] = { ';',' ',0 };
573 static const WCHAR crlfSep
[] = { '\r','\n',0 };
574 static const WCHAR plusSep
[] = { ' ','+',' ',0 };
575 static const WCHAR spaceSep
[] = { ' ',0 };
576 DWORD ret
= 0, bytes
= 0;
578 CERT_NAME_INFO
*info
;
580 if (dwStrType
& unsupportedFlags
)
581 FIXME("unsupported flags: %08x\n", dwStrType
& unsupportedFlags
);
583 bRet
= CryptDecodeObjectEx(dwCertEncodingType
, X509_NAME
, pName
->pbData
,
584 pName
->cbData
, CRYPT_DECODE_ALLOC_FLAG
, NULL
, &info
, &bytes
);
587 DWORD i
, j
, sepLen
, rdnSepLen
;
589 BOOL reverse
= dwStrType
& CERT_NAME_STR_REVERSE_FLAG
;
590 const CERT_RDN
*rdn
= info
->rgRDN
;
592 if(reverse
&& info
->cRDN
> 1) rdn
+= (info
->cRDN
- 1);
594 if (dwStrType
& CERT_NAME_STR_SEMICOLON_FLAG
)
596 else if (dwStrType
& CERT_NAME_STR_CRLF_FLAG
)
600 sepLen
= lstrlenW(sep
);
601 if (dwStrType
& CERT_NAME_STR_NO_PLUS_FLAG
)
605 rdnSepLen
= lstrlenW(rdnSep
);
606 for (i
= 0; (!psz
|| ret
< csz
) && i
< info
->cRDN
; i
++)
608 for (j
= 0; (!psz
|| ret
< csz
) && j
< rdn
->cRDNAttr
; j
++)
611 LPCSTR prefixA
= NULL
;
612 LPCWSTR prefixW
= NULL
;
614 if ((dwStrType
& 0x000000ff) == CERT_OID_NAME_STR
)
615 prefixA
= rdn
->rgRDNAttr
[j
].pszObjId
;
616 else if ((dwStrType
& 0x000000ff) == CERT_X500_NAME_STR
)
618 PCCRYPT_OID_INFO oidInfo
= CryptFindOIDInfo(
619 CRYPT_OID_INFO_OID_KEY
,
620 rdn
->rgRDNAttr
[j
].pszObjId
,
621 CRYPT_RDN_ATTR_OID_GROUP_ID
);
624 prefixW
= oidInfo
->pwszName
;
626 prefixA
= rdn
->rgRDNAttr
[j
].pszObjId
;
628 if (dwStrType
& CERT_NAME_STR_CRLF_FLAG
)
632 for (k
= 0; k
< indentLevel
; k
++)
636 chars
= min(strlenW(indent
), csz
- ret
- 1);
637 memcpy(psz
+ ret
, indent
, chars
* sizeof(WCHAR
));
640 chars
= strlenW(indent
);
646 /* - 1 is needed to account for the NULL terminator. */
647 chars
= CRYPT_AddPrefixW(prefixW
,
648 psz
? psz
+ ret
: NULL
, psz
? csz
- ret
- 1 : 0);
653 /* - 1 is needed to account for the NULL terminator. */
654 chars
= CRYPT_AddPrefixAToW(prefixA
,
655 psz
? psz
+ ret
: NULL
, psz
? csz
- ret
- 1 : 0);
658 chars
= quote_rdn_value_to_str_w(
659 rdn
->rgRDNAttr
[j
].dwValueType
,
660 &rdn
->rgRDNAttr
[j
].Value
, psz
? psz
+ ret
: NULL
,
661 psz
? csz
- ret
: 0);
664 if (j
< rdn
->cRDNAttr
- 1)
666 if (psz
&& ret
< csz
- rdnSepLen
- 1)
667 memcpy(psz
+ ret
, rdnSep
, rdnSepLen
* sizeof(WCHAR
));
671 if (i
< info
->cRDN
- 1)
673 if (psz
&& ret
< csz
- sepLen
- 1)
674 memcpy(psz
+ ret
, sep
, sepLen
* sizeof(WCHAR
));
692 DWORD WINAPI
CertNameToStrW(DWORD dwCertEncodingType
, PCERT_NAME_BLOB pName
,
693 DWORD dwStrType
, LPWSTR psz
, DWORD csz
)
697 TRACE("(%d, %p, %08x, %p, %d)\n", dwCertEncodingType
, pName
, dwStrType
,
700 ret
= cert_name_to_str_with_indent(dwCertEncodingType
, 0, pName
, dwStrType
,
702 TRACE("Returning %s\n", debugstr_w(psz
));
706 BOOL WINAPI
CertStrToNameA(DWORD dwCertEncodingType
, LPCSTR pszX500
,
707 DWORD dwStrType
, void *pvReserved
, BYTE
*pbEncoded
, DWORD
*pcbEncoded
,
713 TRACE("(%08x, %s, %08x, %p, %p, %p, %p)\n", dwCertEncodingType
,
714 debugstr_a(pszX500
), dwStrType
, pvReserved
, pbEncoded
, pcbEncoded
,
717 len
= MultiByteToWideChar(CP_ACP
, 0, pszX500
, -1, NULL
, 0);
720 LPWSTR x500
, errorStr
;
722 if ((x500
= CryptMemAlloc(len
* sizeof(WCHAR
))))
724 MultiByteToWideChar(CP_ACP
, 0, pszX500
, -1, x500
, len
);
725 ret
= CertStrToNameW(dwCertEncodingType
, x500
, dwStrType
,
726 pvReserved
, pbEncoded
, pcbEncoded
,
727 ppszError
? (LPCWSTR
*)&errorStr
: NULL
);
734 *ppszError
= pszX500
;
735 for (i
= 0; i
< errorStr
- x500
; i
++)
736 *ppszError
= CharNextA(*ppszError
);
745 SetLastError(ERROR_OUTOFMEMORY
);
751 SetLastError(CRYPT_E_INVALID_X500_STRING
);
753 *ppszError
= pszX500
;
761 WCHAR buf
[10]; /* big enough for L"GivenName" */
762 LPWSTR keyName
; /* usually = buf, but may be allocated */
763 DWORD keyLen
; /* full available buffer size in WCHARs */
766 static void CRYPT_InitializeKeynameKeeper(struct KeynameKeeper
*keeper
)
768 keeper
->keyName
= keeper
->buf
;
769 keeper
->keyLen
= sizeof(keeper
->buf
) / sizeof(keeper
->buf
[0]);
772 static void CRYPT_FreeKeynameKeeper(struct KeynameKeeper
*keeper
)
774 if (keeper
->keyName
!= keeper
->buf
)
775 CryptMemFree(keeper
->keyName
);
784 static void CRYPT_KeynameKeeperFromTokenW(struct KeynameKeeper
*keeper
,
785 const struct X500TokenW
*key
)
787 DWORD len
= key
->end
- key
->start
;
789 if (len
>= keeper
->keyLen
)
791 CRYPT_FreeKeynameKeeper( keeper
);
792 keeper
->keyLen
= len
+ 1;
793 keeper
->keyName
= CryptMemAlloc(keeper
->keyLen
* sizeof(WCHAR
));
795 memcpy(keeper
->keyName
, key
->start
, len
* sizeof(WCHAR
));
796 keeper
->keyName
[len
] = '\0';
797 TRACE("Keyname is %s\n", debugstr_w(keeper
->keyName
));
800 static BOOL
CRYPT_GetNextKeyW(LPCWSTR str
, struct X500TokenW
*token
,
805 while (*str
&& isspaceW(*str
))
810 while (*str
&& *str
!= '=' && !isspaceW(*str
))
812 if (*str
&& (*str
== '=' || isspaceW(*str
)))
816 TRACE("missing equals char at %s\n", debugstr_w(token
->start
));
818 *ppszError
= token
->start
;
819 SetLastError(CRYPT_E_INVALID_X500_STRING
);
828 /* Assumes separators are characters in the 0-255 range */
829 static BOOL
CRYPT_GetNextValueW(LPCWSTR str
, DWORD dwFlags
, LPCWSTR separators
,
830 WCHAR
*separator_used
, struct X500TokenW
*token
, LPCWSTR
*ppszError
)
834 TRACE("(%s, %s, %p, %p)\n", debugstr_w(str
), debugstr_w(separators
), token
,
838 while (*str
&& isspaceW(*str
))
843 if (!(dwFlags
& CERT_NAME_STR_NO_QUOTING_FLAG
) && *str
== '"')
847 while (!token
->end
&& ret
)
849 while (*str
&& *str
!= '"')
853 if (*(str
+ 1) != '"')
854 token
->end
= str
+ 1;
860 TRACE("unterminated quote at %s\n", debugstr_w(str
));
863 SetLastError(CRYPT_E_INVALID_X500_STRING
);
870 WCHAR map
[256] = { 0 };
873 map
[*separators
++] = 1;
874 while (*str
&& (*str
>= 0xff || !map
[*str
]))
877 if (map
[*str
]) *separator_used
= *str
;
882 TRACE("missing value at %s\n", debugstr_w(str
));
885 SetLastError(CRYPT_E_INVALID_X500_STRING
);
891 /* Encodes the string represented by value as the string type type into the
892 * CERT_NAME_BLOB output. If there is an error and ppszError is not NULL,
893 * *ppszError is set to the first failing character. If there is no error,
894 * output's pbData must be freed with LocalFree.
896 static BOOL
CRYPT_EncodeValueWithType(DWORD dwCertEncodingType
,
897 const struct X500TokenW
*value
, PCERT_NAME_BLOB output
, DWORD type
,
900 CERT_NAME_VALUE nameValue
= { type
, { 0, NULL
} };
903 if (value
->end
> value
->start
)
908 nameValue
.Value
.pbData
= CryptMemAlloc((value
->end
- value
->start
+ 1) *
910 if (!nameValue
.Value
.pbData
)
912 SetLastError(ERROR_OUTOFMEMORY
);
915 ptr
= (LPWSTR
)nameValue
.Value
.pbData
;
916 for (i
= 0; i
< value
->end
- value
->start
; i
++)
918 *ptr
++ = value
->start
[i
];
919 if (value
->start
[i
] == '"')
922 /* The string is NULL terminated because of a quirk in encoding
923 * unicode names values: if the length is given as 0, the value is
924 * assumed to be a NULL-terminated string.
927 nameValue
.Value
.cbData
= (LPBYTE
)ptr
- nameValue
.Value
.pbData
;
929 ret
= CryptEncodeObjectEx(dwCertEncodingType
, X509_UNICODE_NAME_VALUE
,
930 &nameValue
, CRYPT_ENCODE_ALLOC_FLAG
, NULL
, &output
->pbData
,
932 if (!ret
&& ppszError
)
934 if (type
== CERT_RDN_NUMERIC_STRING
&&
935 GetLastError() == CRYPT_E_INVALID_NUMERIC_STRING
)
936 *ppszError
= value
->start
+ output
->cbData
;
937 else if (type
== CERT_RDN_PRINTABLE_STRING
&&
938 GetLastError() == CRYPT_E_INVALID_PRINTABLE_STRING
)
939 *ppszError
= value
->start
+ output
->cbData
;
940 else if (type
== CERT_RDN_IA5_STRING
&&
941 GetLastError() == CRYPT_E_INVALID_IA5_STRING
)
942 *ppszError
= value
->start
+ output
->cbData
;
944 CryptMemFree(nameValue
.Value
.pbData
);
948 static BOOL
CRYPT_EncodeValue(DWORD dwCertEncodingType
,
949 const struct X500TokenW
*value
, PCERT_NAME_BLOB output
, const DWORD
*types
,
956 for (i
= 0; !ret
&& types
[i
]; i
++)
957 ret
= CRYPT_EncodeValueWithType(dwCertEncodingType
, value
, output
,
958 types
[i
], ppszError
);
962 static BOOL
CRYPT_ValueToRDN(DWORD dwCertEncodingType
, PCERT_NAME_INFO info
,
963 PCCRYPT_OID_INFO keyOID
, struct X500TokenW
*value
, DWORD dwStrType
, LPCWSTR
*ppszError
)
967 TRACE("OID %s, value %s\n", debugstr_a(keyOID
->pszOID
),
968 debugstr_wn(value
->start
, value
->end
- value
->start
));
971 info
->rgRDN
= CryptMemAlloc(sizeof(CERT_RDN
));
973 info
->rgRDN
= CryptMemRealloc(info
->rgRDN
,
974 (info
->cRDN
+ 1) * sizeof(CERT_RDN
));
977 /* FIXME: support multiple RDN attrs */
978 info
->rgRDN
[info
->cRDN
].rgRDNAttr
=
979 CryptMemAlloc(sizeof(CERT_RDN_ATTR
));
980 if (info
->rgRDN
[info
->cRDN
].rgRDNAttr
)
982 static const DWORD defaultTypes
[] = { CERT_RDN_PRINTABLE_STRING
,
983 CERT_RDN_BMP_STRING
, 0 };
986 info
->rgRDN
[info
->cRDN
].cRDNAttr
= 1;
987 info
->rgRDN
[info
->cRDN
].rgRDNAttr
[0].pszObjId
=
988 (LPSTR
)keyOID
->pszOID
;
989 info
->rgRDN
[info
->cRDN
].rgRDNAttr
[0].dwValueType
=
990 CERT_RDN_ENCODED_BLOB
;
991 if (keyOID
->ExtraInfo
.cbData
)
992 types
= (const DWORD
*)keyOID
->ExtraInfo
.pbData
;
994 types
= defaultTypes
;
996 /* Remove surrounding quotes */
997 if (value
->start
[0] == '"' && !(dwStrType
& CERT_NAME_STR_NO_QUOTING_FLAG
))
1002 ret
= CRYPT_EncodeValue(dwCertEncodingType
, value
,
1003 &info
->rgRDN
[info
->cRDN
].rgRDNAttr
[0].Value
, types
, ppszError
);
1006 SetLastError(ERROR_OUTOFMEMORY
);
1010 SetLastError(ERROR_OUTOFMEMORY
);
1014 BOOL WINAPI
CertStrToNameW(DWORD dwCertEncodingType
, LPCWSTR pszX500
,
1015 DWORD dwStrType
, void *pvReserved
, BYTE
*pbEncoded
, DWORD
*pcbEncoded
,
1018 CERT_NAME_INFO info
= { 0, NULL
};
1020 struct KeynameKeeper keeper
;
1024 TRACE("(%08x, %s, %08x, %p, %p, %p, %p)\n", dwCertEncodingType
,
1025 debugstr_w(pszX500
), dwStrType
, pvReserved
, pbEncoded
, pcbEncoded
,
1028 CRYPT_InitializeKeynameKeeper(&keeper
);
1030 while (str
&& *str
&& ret
)
1032 struct X500TokenW token
;
1034 ret
= CRYPT_GetNextKeyW(str
, &token
, ppszError
);
1035 if (ret
&& token
.start
)
1037 PCCRYPT_OID_INFO keyOID
;
1039 CRYPT_KeynameKeeperFromTokenW(&keeper
, &token
);
1040 keyOID
= CryptFindOIDInfo(CRYPT_OID_INFO_NAME_KEY
, keeper
.keyName
,
1041 CRYPT_RDN_ATTR_OID_GROUP_ID
);
1045 *ppszError
= token
.start
;
1046 SetLastError(CRYPT_E_INVALID_X500_STRING
);
1052 while (isspace(*str
))
1058 SetLastError(CRYPT_E_INVALID_X500_STRING
);
1063 static const WCHAR commaSep
[] = { ',',0 };
1064 static const WCHAR semiSep
[] = { ';',0 };
1065 static const WCHAR crlfSep
[] = { '\r','\n',0 };
1066 static const WCHAR allSepsWithoutPlus
[] = { ',',';','\r','\n',0 };
1067 static const WCHAR allSeps
[] = { '+',',',';','\r','\n',0 };
1072 if (dwStrType
& CERT_NAME_STR_COMMA_FLAG
)
1074 else if (dwStrType
& CERT_NAME_STR_SEMICOLON_FLAG
)
1076 else if (dwStrType
& CERT_NAME_STR_CRLF_FLAG
)
1078 else if (dwStrType
& CERT_NAME_STR_NO_PLUS_FLAG
)
1079 sep
= allSepsWithoutPlus
;
1082 ret
= CRYPT_GetNextValueW(str
, dwStrType
, sep
, &sep_used
, &token
,
1087 /* if token.end points to the separator, skip it */
1088 if (str
&& sep_used
&& *str
== sep_used
) str
++;
1090 ret
= CRYPT_ValueToRDN(dwCertEncodingType
, &info
,
1091 keyOID
, &token
, dwStrType
, ppszError
);
1097 CRYPT_FreeKeynameKeeper(&keeper
);
1102 ret
= CryptEncodeObjectEx(dwCertEncodingType
, X509_NAME
, &info
,
1103 0, NULL
, pbEncoded
, pcbEncoded
);
1105 for (i
= 0; i
< info
.cRDN
; i
++)
1109 for (j
= 0; j
< info
.rgRDN
[i
].cRDNAttr
; j
++)
1110 LocalFree(info
.rgRDN
[i
].rgRDNAttr
[j
].Value
.pbData
);
1111 CryptMemFree(info
.rgRDN
[i
].rgRDNAttr
);
1113 CryptMemFree(info
.rgRDN
);
1117 DWORD WINAPI
CertGetNameStringA(PCCERT_CONTEXT pCertContext
, DWORD dwType
,
1118 DWORD dwFlags
, void *pvTypePara
, LPSTR pszNameString
, DWORD cchNameString
)
1122 TRACE("(%p, %d, %08x, %p, %p, %d)\n", pCertContext
, dwType
, dwFlags
,
1123 pvTypePara
, pszNameString
, cchNameString
);
1130 nameLen
= CertGetNameStringW(pCertContext
, dwType
, dwFlags
, pvTypePara
,
1132 wideName
= CryptMemAlloc(nameLen
* sizeof(WCHAR
));
1135 CertGetNameStringW(pCertContext
, dwType
, dwFlags
, pvTypePara
,
1137 nameLen
= WideCharToMultiByte(CP_ACP
, 0, wideName
, nameLen
,
1138 pszNameString
, cchNameString
, NULL
, NULL
);
1139 if (nameLen
<= cchNameString
)
1143 pszNameString
[cchNameString
- 1] = '\0';
1144 ret
= cchNameString
;
1146 CryptMemFree(wideName
);
1150 *pszNameString
= '\0';
1155 ret
= CertGetNameStringW(pCertContext
, dwType
, dwFlags
, pvTypePara
,
1160 /* Searches cert's extensions for the alternate name extension with OID
1161 * altNameOID, and if found, searches it for the alternate name type entryType.
1162 * If found, returns a pointer to the entry, otherwise returns NULL.
1163 * Regardless of whether an entry of the desired type is found, if the
1164 * alternate name extension is present, sets *info to the decoded alternate
1165 * name extension, which you must free using LocalFree.
1166 * The return value is a pointer within *info, so don't free *info before
1167 * you're done with the return value.
1169 static PCERT_ALT_NAME_ENTRY
cert_find_alt_name_entry(PCCERT_CONTEXT cert
,
1170 LPCSTR altNameOID
, DWORD entryType
, PCERT_ALT_NAME_INFO
*info
)
1172 PCERT_ALT_NAME_ENTRY entry
= NULL
;
1173 PCERT_EXTENSION ext
= CertFindExtension(altNameOID
,
1174 cert
->pCertInfo
->cExtension
, cert
->pCertInfo
->rgExtension
);
1180 if (CryptDecodeObjectEx(cert
->dwCertEncodingType
, X509_ALTERNATE_NAME
,
1181 ext
->Value
.pbData
, ext
->Value
.cbData
, CRYPT_DECODE_ALLOC_FLAG
, NULL
,
1186 for (i
= 0; !entry
&& i
< (*info
)->cAltEntry
; i
++)
1187 if ((*info
)->rgAltEntry
[i
].dwAltNameChoice
== entryType
)
1188 entry
= &(*info
)->rgAltEntry
[i
];
1196 static DWORD
cert_get_name_from_rdn_attr(DWORD encodingType
,
1197 const CERT_NAME_BLOB
*name
, LPCSTR oid
, LPWSTR pszNameString
, DWORD cchNameString
)
1199 CERT_NAME_INFO
*nameInfo
;
1200 DWORD bytes
= 0, ret
= 0;
1202 if (CryptDecodeObjectEx(encodingType
, X509_NAME
, name
->pbData
,
1203 name
->cbData
, CRYPT_DECODE_ALLOC_FLAG
, NULL
, &nameInfo
, &bytes
))
1205 PCERT_RDN_ATTR nameAttr
;
1208 oid
= szOID_RSA_emailAddr
;
1209 nameAttr
= CertFindRDNAttr(oid
, nameInfo
);
1211 ret
= CertRDNValueToStrW(nameAttr
->dwValueType
, &nameAttr
->Value
,
1212 pszNameString
, cchNameString
);
1213 LocalFree(nameInfo
);
1218 DWORD WINAPI
CertGetNameStringW(PCCERT_CONTEXT pCertContext
, DWORD dwType
,
1219 DWORD dwFlags
, void *pvTypePara
, LPWSTR pszNameString
, DWORD cchNameString
)
1222 PCERT_NAME_BLOB name
;
1225 TRACE("(%p, %d, %08x, %p, %p, %d)\n", pCertContext
, dwType
,
1226 dwFlags
, pvTypePara
, pszNameString
, cchNameString
);
1231 if (dwFlags
& CERT_NAME_ISSUER_FLAG
)
1233 name
= &pCertContext
->pCertInfo
->Issuer
;
1234 altNameOID
= szOID_ISSUER_ALT_NAME
;
1238 name
= &pCertContext
->pCertInfo
->Subject
;
1239 altNameOID
= szOID_SUBJECT_ALT_NAME
;
1244 case CERT_NAME_EMAIL_TYPE
:
1246 CERT_ALT_NAME_INFO
*info
;
1247 PCERT_ALT_NAME_ENTRY entry
= cert_find_alt_name_entry(pCertContext
,
1248 altNameOID
, CERT_ALT_NAME_RFC822_NAME
, &info
);
1253 ret
= strlenW(entry
->u
.pwszRfc822Name
) + 1;
1254 else if (cchNameString
)
1256 ret
= min(strlenW(entry
->u
.pwszRfc822Name
), cchNameString
- 1);
1257 memcpy(pszNameString
, entry
->u
.pwszRfc822Name
,
1258 ret
* sizeof(WCHAR
));
1259 pszNameString
[ret
++] = 0;
1265 ret
= cert_get_name_from_rdn_attr(pCertContext
->dwCertEncodingType
,
1266 name
, szOID_RSA_emailAddr
, pszNameString
, cchNameString
);
1269 case CERT_NAME_RDN_TYPE
:
1271 DWORD type
= pvTypePara
? *(DWORD
*)pvTypePara
: 0;
1274 ret
= CertNameToStrW(pCertContext
->dwCertEncodingType
, name
,
1275 type
, pszNameString
, cchNameString
);
1278 CERT_ALT_NAME_INFO
*info
;
1279 PCERT_ALT_NAME_ENTRY entry
= cert_find_alt_name_entry(pCertContext
,
1280 altNameOID
, CERT_ALT_NAME_DIRECTORY_NAME
, &info
);
1283 ret
= CertNameToStrW(pCertContext
->dwCertEncodingType
,
1284 &entry
->u
.DirectoryName
, type
, pszNameString
, cchNameString
);
1290 case CERT_NAME_ATTR_TYPE
:
1291 ret
= cert_get_name_from_rdn_attr(pCertContext
->dwCertEncodingType
,
1292 name
, pvTypePara
, pszNameString
, cchNameString
);
1295 CERT_ALT_NAME_INFO
*altInfo
;
1296 PCERT_ALT_NAME_ENTRY entry
= cert_find_alt_name_entry(pCertContext
,
1297 altNameOID
, CERT_ALT_NAME_DIRECTORY_NAME
, &altInfo
);
1300 ret
= cert_name_to_str_with_indent(X509_ASN_ENCODING
, 0,
1301 &entry
->u
.DirectoryName
, 0, pszNameString
, cchNameString
);
1306 case CERT_NAME_SIMPLE_DISPLAY_TYPE
:
1308 static const LPCSTR simpleAttributeOIDs
[] = { szOID_COMMON_NAME
,
1309 szOID_ORGANIZATIONAL_UNIT_NAME
, szOID_ORGANIZATION_NAME
,
1310 szOID_RSA_emailAddr
};
1311 CERT_NAME_INFO
*nameInfo
= NULL
;
1314 if (CryptDecodeObjectEx(pCertContext
->dwCertEncodingType
, X509_NAME
,
1315 name
->pbData
, name
->cbData
, CRYPT_DECODE_ALLOC_FLAG
, NULL
, &nameInfo
,
1318 PCERT_RDN_ATTR nameAttr
= NULL
;
1320 for (i
= 0; !nameAttr
&& i
< sizeof(simpleAttributeOIDs
) /
1321 sizeof(simpleAttributeOIDs
[0]); i
++)
1322 nameAttr
= CertFindRDNAttr(simpleAttributeOIDs
[i
], nameInfo
);
1324 ret
= CertRDNValueToStrW(nameAttr
->dwValueType
,
1325 &nameAttr
->Value
, pszNameString
, cchNameString
);
1326 LocalFree(nameInfo
);
1330 CERT_ALT_NAME_INFO
*altInfo
;
1331 PCERT_ALT_NAME_ENTRY entry
= cert_find_alt_name_entry(pCertContext
,
1332 altNameOID
, CERT_ALT_NAME_RFC822_NAME
, &altInfo
);
1336 if (!entry
&& altInfo
->cAltEntry
)
1337 entry
= &altInfo
->rgAltEntry
[0];
1341 ret
= strlenW(entry
->u
.pwszRfc822Name
) + 1;
1342 else if (cchNameString
)
1344 ret
= min(strlenW(entry
->u
.pwszRfc822Name
),
1346 memcpy(pszNameString
, entry
->u
.pwszRfc822Name
,
1347 ret
* sizeof(WCHAR
));
1348 pszNameString
[ret
++] = 0;
1356 case CERT_NAME_FRIENDLY_DISPLAY_TYPE
:
1358 DWORD cch
= cchNameString
;
1360 if (CertGetCertificateContextProperty(pCertContext
,
1361 CERT_FRIENDLY_NAME_PROP_ID
, pszNameString
, &cch
))
1364 ret
= CertGetNameStringW(pCertContext
,
1365 CERT_NAME_SIMPLE_DISPLAY_TYPE
, dwFlags
, pvTypePara
, pszNameString
,
1369 case CERT_NAME_DNS_TYPE
:
1371 CERT_ALT_NAME_INFO
*info
;
1372 PCERT_ALT_NAME_ENTRY entry
= cert_find_alt_name_entry(pCertContext
,
1373 altNameOID
, CERT_ALT_NAME_DNS_NAME
, &info
);
1378 ret
= strlenW(entry
->u
.pwszDNSName
) + 1;
1379 else if (cchNameString
)
1381 ret
= min(strlenW(entry
->u
.pwszDNSName
), cchNameString
- 1);
1382 memcpy(pszNameString
, entry
->u
.pwszDNSName
, ret
* sizeof(WCHAR
));
1383 pszNameString
[ret
++] = 0;
1389 ret
= cert_get_name_from_rdn_attr(pCertContext
->dwCertEncodingType
,
1390 name
, szOID_COMMON_NAME
, pszNameString
, cchNameString
);
1393 case CERT_NAME_URL_TYPE
:
1395 CERT_ALT_NAME_INFO
*info
;
1396 PCERT_ALT_NAME_ENTRY entry
= cert_find_alt_name_entry(pCertContext
,
1397 altNameOID
, CERT_ALT_NAME_URL
, &info
);
1402 ret
= strlenW(entry
->u
.pwszURL
) + 1;
1403 else if (cchNameString
)
1405 ret
= min(strlenW(entry
->u
.pwszURL
), cchNameString
- 1);
1406 memcpy(pszNameString
, entry
->u
.pwszURL
, ret
* sizeof(WCHAR
));
1407 pszNameString
[ret
++] = 0;
1415 FIXME("unimplemented for type %d\n", dwType
);
1423 else if (cchNameString
)
1425 pszNameString
[0] = 0;