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
25 #include "wine/debug.h"
26 #include "crypt32_private.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(crypt
);
30 DWORD WINAPI
CertRDNValueToStrA(DWORD type
, PCERT_RDN_VALUE_BLOB value_blob
,
31 LPSTR value
, DWORD value_len
)
33 DWORD len
, len_mb
, ret
;
36 TRACE("(%ld, %p, %p, %ld)\n", type
, value_blob
, value
, value_len
);
38 len
= CertRDNValueToStrW(type
, value_blob
, NULL
, 0);
40 if (!(valueW
= CryptMemAlloc(len
* sizeof(*valueW
))))
43 if (value
&& value_len
) *value
= 0;
47 len
= CertRDNValueToStrW(type
, value_blob
, valueW
, len
);
48 len_mb
= WideCharToMultiByte(CP_ACP
, 0, valueW
, len
, NULL
, 0, NULL
, NULL
);
49 if (!value
|| !value_len
)
55 ret
= WideCharToMultiByte(CP_ACP
, 0, valueW
, len
, value
, value_len
, NULL
, NULL
);
65 static DWORD
rdn_value_to_strW(DWORD dwValueType
, PCERT_RDN_VALUE_BLOB pValue
,
66 LPWSTR psz
, DWORD csz
, BOOL partial_copy
)
68 DWORD ret
= 0, len
, i
;
70 TRACE("(%ld, %p, %p, %ld)\n", dwValueType
, pValue
, psz
, csz
);
74 case CERT_RDN_ANY_TYPE
:
76 case CERT_RDN_NUMERIC_STRING
:
77 case CERT_RDN_PRINTABLE_STRING
:
78 case CERT_RDN_TELETEX_STRING
:
79 case CERT_RDN_VIDEOTEX_STRING
:
80 case CERT_RDN_IA5_STRING
:
81 case CERT_RDN_GRAPHIC_STRING
:
82 case CERT_RDN_VISIBLE_STRING
:
83 case CERT_RDN_GENERAL_STRING
:
85 if (!psz
|| !csz
) ret
= len
;
86 else if (len
< csz
|| partial_copy
)
88 len
= min(len
, csz
- 1);
89 for (i
= 0; i
< len
; ++i
)
90 psz
[i
] = pValue
->pbData
[i
];
94 case CERT_RDN_BMP_STRING
:
95 case CERT_RDN_UTF8_STRING
:
96 len
= pValue
->cbData
/ sizeof(WCHAR
);
99 else if (len
< csz
|| partial_copy
)
103 len
= min(len
, csz
- 1);
104 for (i
= 0; i
< len
; ++i
)
105 ptr
[i
] = ((LPCWSTR
)pValue
->pbData
)[i
];
110 FIXME("string type %ld unimplemented\n", dwValueType
);
112 if (psz
&& csz
) psz
[ret
] = 0;
113 TRACE("returning %ld (%s)\n", ret
+ 1, debugstr_w(psz
));
117 DWORD WINAPI
CertRDNValueToStrW(DWORD dwValueType
, PCERT_RDN_VALUE_BLOB pValue
,
118 LPWSTR psz
, DWORD csz
)
120 return rdn_value_to_strW(dwValueType
, pValue
, psz
, csz
, FALSE
);
123 static inline BOOL
is_quotable_char(WCHAR c
)
142 static inline BOOL
is_spaceW(WCHAR c
)
144 return c
<= 0x7f && isspace((char)c
);
147 static DWORD
quote_rdn_value_to_str_w(DWORD dwValueType
,
148 PCERT_RDN_VALUE_BLOB pValue
, LPWSTR psz
, DWORD csz
)
150 DWORD ret
= 0, len
, i
, strLen
;
151 BOOL needsQuotes
= FALSE
;
153 TRACE("(%ld, %p, %p, %ld)\n", dwValueType
, pValue
, psz
, csz
);
157 case CERT_RDN_ANY_TYPE
:
159 case CERT_RDN_NUMERIC_STRING
:
160 case CERT_RDN_PRINTABLE_STRING
:
161 case CERT_RDN_TELETEX_STRING
:
162 case CERT_RDN_VIDEOTEX_STRING
:
163 case CERT_RDN_IA5_STRING
:
164 case CERT_RDN_GRAPHIC_STRING
:
165 case CERT_RDN_VISIBLE_STRING
:
166 case CERT_RDN_GENERAL_STRING
:
167 len
= pValue
->cbData
;
168 if (pValue
->cbData
&& isspace(pValue
->pbData
[0]))
170 if (pValue
->cbData
&& isspace(pValue
->pbData
[pValue
->cbData
- 1]))
172 for (i
= 0; i
< pValue
->cbData
; i
++)
174 if (is_quotable_char(pValue
->pbData
[i
]))
176 if (pValue
->pbData
[i
] == '"')
189 for (i
= 0; i
< pValue
->cbData
&& ptr
- psz
< csz
; ptr
++, i
++)
191 *ptr
= pValue
->pbData
[i
];
192 if (pValue
->pbData
[i
] == '"' && ptr
- psz
< csz
- 1)
195 if (needsQuotes
&& ptr
- psz
< csz
)
200 case CERT_RDN_BMP_STRING
:
201 case CERT_RDN_UTF8_STRING
:
202 strLen
= len
= pValue
->cbData
/ sizeof(WCHAR
);
203 if (strLen
&& is_spaceW(((LPCWSTR
)pValue
->pbData
)[0]))
205 if (strLen
&& _spaceW(((LPCWSTR
)pValue
->pbData
)[strLen
- 1]))
207 for (i
= 0; i
< strLen
; i
++)
209 if (is_quotable_char(((LPCWSTR
)pValue
->pbData
)[i
]))
211 if (((LPCWSTR
)pValue
->pbData
)[i
] == '"')
224 for (i
= 0; i
< strLen
&& ptr
- psz
< csz
; ptr
++, i
++)
226 *ptr
= ((LPCWSTR
)pValue
->pbData
)[i
];
227 if (((LPCWSTR
)pValue
->pbData
)[i
] == '"' && ptr
- psz
< csz
- 1)
230 if (needsQuotes
&& ptr
- psz
< csz
)
236 FIXME("string type %ld unimplemented\n", dwValueType
);
238 TRACE("returning %ld (%s)\n", ret
, debugstr_w(psz
));
242 DWORD WINAPI
CertNameToStrA(DWORD encoding_type
, PCERT_NAME_BLOB name_blob
, DWORD str_type
, LPSTR str
, DWORD str_len
)
244 DWORD len
, len_mb
, ret
;
247 TRACE("(%ld, %p, %08lx, %p, %ld)\n", encoding_type
, name_blob
, str_type
, str
, str_len
);
249 len
= CertNameToStrW(encoding_type
, name_blob
, str_type
, NULL
, 0);
251 if (!(strW
= CryptMemAlloc(len
* sizeof(*strW
))))
254 if (str
&& str_len
) *str
= 0;
258 len
= CertNameToStrW(encoding_type
, name_blob
, str_type
, strW
, len
);
259 len_mb
= WideCharToMultiByte(CP_ACP
, 0, strW
, len
, NULL
, 0, NULL
, NULL
);
260 if (!str
|| !str_len
)
266 ret
= WideCharToMultiByte(CP_ACP
, 0, strW
, len
, str
, str_len
, NULL
, NULL
);
276 /* Adds the prefix prefix to the wide-character string pointed to by psz,
277 * followed by the character '='. Copies no more than csz characters. Returns
278 * the number of characters copied. If psz is NULL, returns the number of
279 * characters that would be copied.
280 * Assumes the characters in prefix are ASCII (not multibyte characters.)
282 static DWORD
CRYPT_AddPrefixAToW(LPCSTR prefix
, LPWSTR psz
, DWORD csz
)
286 TRACE("(%s, %p, %ld)\n", debugstr_a(prefix
), psz
, csz
);
292 chars
= min(strlen(prefix
), csz
);
293 for (i
= 0; i
< chars
; i
++)
294 *(psz
+ i
) = prefix
[i
];
295 *(psz
+ chars
) = '=';
299 chars
= lstrlenA(prefix
) + 1;
303 /* Adds the prefix prefix to the string pointed to by psz, followed by the
304 * character '='. Copies no more than csz characters. Returns the number of
305 * characters copied. If psz is NULL, returns the number of characters that
308 static DWORD
CRYPT_AddPrefixW(LPCWSTR prefix
, LPWSTR psz
, DWORD csz
)
312 TRACE("(%s, %p, %ld)\n", debugstr_w(prefix
), psz
, csz
);
316 chars
= min(lstrlenW(prefix
), csz
);
317 memcpy(psz
, prefix
, chars
* sizeof(WCHAR
));
318 *(psz
+ chars
) = '=';
322 chars
= lstrlenW(prefix
) + 1;
326 static const WCHAR indent
[] = L
" ";
328 DWORD
cert_name_to_str_with_indent(DWORD dwCertEncodingType
, DWORD indentLevel
,
329 const CERT_NAME_BLOB
*pName
, DWORD dwStrType
, LPWSTR psz
, DWORD csz
)
331 static const DWORD unsupportedFlags
= CERT_NAME_STR_NO_QUOTING_FLAG
|
332 CERT_NAME_STR_ENABLE_T61_UNICODE_FLAG
;
333 DWORD ret
= 0, bytes
= 0;
335 CERT_NAME_INFO
*info
;
338 if (dwStrType
& unsupportedFlags
)
339 FIXME("unsupported flags: %08lx\n", dwStrType
& unsupportedFlags
);
341 bRet
= CryptDecodeObjectEx(dwCertEncodingType
, X509_NAME
, pName
->pbData
,
342 pName
->cbData
, CRYPT_DECODE_ALLOC_FLAG
, NULL
, &info
, &bytes
);
345 DWORD i
, j
, sepLen
, rdnSepLen
;
347 BOOL reverse
= dwStrType
& CERT_NAME_STR_REVERSE_FLAG
;
348 const CERT_RDN
*rdn
= info
->rgRDN
;
350 if(reverse
&& info
->cRDN
> 1) rdn
+= (info
->cRDN
- 1);
352 if (dwStrType
& CERT_NAME_STR_SEMICOLON_FLAG
)
354 else if (dwStrType
& CERT_NAME_STR_CRLF_FLAG
)
358 sepLen
= lstrlenW(sep
);
359 if (dwStrType
& CERT_NAME_STR_NO_PLUS_FLAG
)
363 rdnSepLen
= lstrlenW(rdnSep
);
364 if (!csz
) psz
= NULL
;
365 for (i
= 0; i
< info
->cRDN
; i
++)
367 if (psz
&& ret
+ 1 == csz
) break;
368 for (j
= 0; j
< rdn
->cRDNAttr
; j
++)
370 LPCSTR prefixA
= NULL
;
371 LPCWSTR prefixW
= NULL
;
373 if (psz
&& ret
+ 1 == csz
) break;
375 if ((dwStrType
& 0x000000ff) == CERT_OID_NAME_STR
)
376 prefixA
= rdn
->rgRDNAttr
[j
].pszObjId
;
377 else if ((dwStrType
& 0x000000ff) == CERT_X500_NAME_STR
)
379 PCCRYPT_OID_INFO oidInfo
= CryptFindOIDInfo(
380 CRYPT_OID_INFO_OID_KEY
,
381 rdn
->rgRDNAttr
[j
].pszObjId
,
382 CRYPT_RDN_ATTR_OID_GROUP_ID
);
385 prefixW
= oidInfo
->pwszName
;
387 prefixA
= rdn
->rgRDNAttr
[j
].pszObjId
;
389 if (dwStrType
& CERT_NAME_STR_CRLF_FLAG
)
393 for (k
= 0; k
< indentLevel
; k
++)
397 chars
= min(lstrlenW(indent
), csz
- ret
- 1);
398 memcpy(psz
+ ret
, indent
, chars
* sizeof(WCHAR
));
401 chars
= lstrlenW(indent
);
404 if (psz
&& ret
+ 1 == csz
) break;
408 /* - 1 is needed to account for the NULL terminator. */
409 chars
= CRYPT_AddPrefixW(prefixW
,
410 psz
? psz
+ ret
: NULL
, psz
? csz
- ret
- 1 : 0);
415 /* - 1 is needed to account for the NULL terminator. */
416 chars
= CRYPT_AddPrefixAToW(prefixA
,
417 psz
? psz
+ ret
: NULL
, psz
? csz
- ret
- 1 : 0);
420 if (psz
&& ret
+ 1 == csz
) break;
422 chars
= quote_rdn_value_to_str_w(rdn
->rgRDNAttr
[j
].dwValueType
, &rdn
->rgRDNAttr
[j
].Value
,
423 psz
? psz
+ ret
: NULL
, psz
? csz
- ret
- 1 : 0);
425 if (j
< rdn
->cRDNAttr
- 1)
429 chars
= min(rdnSepLen
, csz
- ret
- 1);
430 memcpy(psz
+ ret
, rdnSep
, chars
* sizeof(WCHAR
));
433 else ret
+= rdnSepLen
;
436 if (psz
&& ret
+ 1 == csz
) break;
437 if (i
< info
->cRDN
- 1)
441 chars
= min(sepLen
, csz
- ret
- 1);
442 memcpy(psz
+ ret
, sep
, chars
* sizeof(WCHAR
));
452 if (psz
&& csz
) psz
[ret
] = 0;
456 DWORD WINAPI
CertNameToStrW(DWORD dwCertEncodingType
, PCERT_NAME_BLOB pName
,
457 DWORD dwStrType
, LPWSTR psz
, DWORD csz
)
461 TRACE("(%ld, %p, %08lx, %p, %ld)\n", dwCertEncodingType
, pName
, dwStrType
,
464 ret
= cert_name_to_str_with_indent(dwCertEncodingType
, 0, pName
, dwStrType
,
466 TRACE("Returning %s\n", debugstr_w(psz
));
470 BOOL WINAPI
CertStrToNameA(DWORD dwCertEncodingType
, LPCSTR pszX500
,
471 DWORD dwStrType
, void *pvReserved
, BYTE
*pbEncoded
, DWORD
*pcbEncoded
,
477 TRACE("(%08lx, %s, %08lx, %p, %p, %p, %p)\n", dwCertEncodingType
,
478 debugstr_a(pszX500
), dwStrType
, pvReserved
, pbEncoded
, pcbEncoded
,
481 len
= MultiByteToWideChar(CP_ACP
, 0, pszX500
, -1, NULL
, 0);
484 LPWSTR x500
, errorStr
;
486 if ((x500
= CryptMemAlloc(len
* sizeof(WCHAR
))))
488 MultiByteToWideChar(CP_ACP
, 0, pszX500
, -1, x500
, len
);
489 ret
= CertStrToNameW(dwCertEncodingType
, x500
, dwStrType
,
490 pvReserved
, pbEncoded
, pcbEncoded
,
491 ppszError
? (LPCWSTR
*)&errorStr
: NULL
);
498 *ppszError
= pszX500
;
499 for (i
= 0; i
< errorStr
- x500
; i
++)
500 *ppszError
= CharNextA(*ppszError
);
509 SetLastError(ERROR_OUTOFMEMORY
);
515 SetLastError(CRYPT_E_INVALID_X500_STRING
);
517 *ppszError
= pszX500
;
525 WCHAR buf
[10]; /* big enough for L"GivenName" */
526 LPWSTR keyName
; /* usually = buf, but may be allocated */
527 DWORD keyLen
; /* full available buffer size in WCHARs */
530 static void CRYPT_InitializeKeynameKeeper(struct KeynameKeeper
*keeper
)
532 keeper
->keyName
= keeper
->buf
;
533 keeper
->keyLen
= ARRAY_SIZE(keeper
->buf
);
536 static void CRYPT_FreeKeynameKeeper(struct KeynameKeeper
*keeper
)
538 if (keeper
->keyName
!= keeper
->buf
)
539 CryptMemFree(keeper
->keyName
);
548 static void CRYPT_KeynameKeeperFromTokenW(struct KeynameKeeper
*keeper
,
549 const struct X500TokenW
*key
)
551 DWORD len
= key
->end
- key
->start
;
553 if (len
>= keeper
->keyLen
)
555 CRYPT_FreeKeynameKeeper( keeper
);
556 keeper
->keyLen
= len
+ 1;
557 keeper
->keyName
= CryptMemAlloc(keeper
->keyLen
* sizeof(WCHAR
));
559 memcpy(keeper
->keyName
, key
->start
, len
* sizeof(WCHAR
));
560 keeper
->keyName
[len
] = '\0';
561 TRACE("Keyname is %s\n", debugstr_w(keeper
->keyName
));
564 static BOOL
CRYPT_GetNextKeyW(LPCWSTR str
, struct X500TokenW
*token
,
569 while (*str
&& iswspace(*str
))
574 while (*str
&& *str
!= '=' && !iswspace(*str
))
576 if (*str
&& (*str
== '=' || iswspace(*str
)))
580 TRACE("missing equals char at %s\n", debugstr_w(token
->start
));
582 *ppszError
= token
->start
;
583 SetLastError(CRYPT_E_INVALID_X500_STRING
);
592 /* Assumes separators are characters in the 0-255 range */
593 static BOOL
CRYPT_GetNextValueW(LPCWSTR str
, DWORD dwFlags
, LPCWSTR separators
,
594 WCHAR
*separator_used
, struct X500TokenW
*token
, LPCWSTR
*ppszError
)
598 TRACE("(%s, %s, %p, %p)\n", debugstr_w(str
), debugstr_w(separators
), token
,
602 while (*str
&& iswspace(*str
))
607 if (!(dwFlags
& CERT_NAME_STR_NO_QUOTING_FLAG
) && *str
== '"')
611 while (!token
->end
&& ret
)
613 while (*str
&& *str
!= '"')
617 if (*(str
+ 1) != '"')
618 token
->end
= str
+ 1;
624 TRACE("unterminated quote at %s\n", debugstr_w(str
));
627 SetLastError(CRYPT_E_INVALID_X500_STRING
);
634 WCHAR map
[256] = { 0 };
637 map
[*separators
++] = 1;
638 while (*str
&& (*str
>= 0xff || !map
[*str
]))
641 if (map
[*str
]) *separator_used
= *str
;
646 TRACE("missing value at %s\n", debugstr_w(str
));
649 SetLastError(CRYPT_E_INVALID_X500_STRING
);
655 /* Encodes the string represented by value as the string type type into the
656 * CERT_NAME_BLOB output. If there is an error and ppszError is not NULL,
657 * *ppszError is set to the first failing character. If there is no error,
658 * output's pbData must be freed with LocalFree.
660 static BOOL
CRYPT_EncodeValueWithType(DWORD dwCertEncodingType
,
661 const struct X500TokenW
*value
, PCERT_NAME_BLOB output
, DWORD type
,
664 CERT_NAME_VALUE nameValue
= { type
, { 0, NULL
} };
667 if (value
->end
> value
->start
)
672 nameValue
.Value
.pbData
= CryptMemAlloc((value
->end
- value
->start
+ 1) *
674 if (!nameValue
.Value
.pbData
)
676 SetLastError(ERROR_OUTOFMEMORY
);
679 ptr
= (LPWSTR
)nameValue
.Value
.pbData
;
680 for (i
= 0; i
< value
->end
- value
->start
; i
++)
682 *ptr
++ = value
->start
[i
];
683 if (value
->start
[i
] == '"')
686 /* The string is NULL terminated because of a quirk in encoding
687 * unicode names values: if the length is given as 0, the value is
688 * assumed to be a NULL-terminated string.
691 nameValue
.Value
.cbData
= (LPBYTE
)ptr
- nameValue
.Value
.pbData
;
693 ret
= CryptEncodeObjectEx(dwCertEncodingType
, X509_UNICODE_NAME_VALUE
,
694 &nameValue
, CRYPT_ENCODE_ALLOC_FLAG
, NULL
, &output
->pbData
,
696 if (!ret
&& ppszError
)
698 if (type
== CERT_RDN_NUMERIC_STRING
&&
699 GetLastError() == CRYPT_E_INVALID_NUMERIC_STRING
)
700 *ppszError
= value
->start
+ output
->cbData
;
701 else if (type
== CERT_RDN_PRINTABLE_STRING
&&
702 GetLastError() == CRYPT_E_INVALID_PRINTABLE_STRING
)
703 *ppszError
= value
->start
+ output
->cbData
;
704 else if (type
== CERT_RDN_IA5_STRING
&&
705 GetLastError() == CRYPT_E_INVALID_IA5_STRING
)
706 *ppszError
= value
->start
+ output
->cbData
;
708 CryptMemFree(nameValue
.Value
.pbData
);
712 static BOOL
CRYPT_EncodeValue(DWORD dwCertEncodingType
,
713 const struct X500TokenW
*value
, PCERT_NAME_BLOB output
, const DWORD
*types
,
720 for (i
= 0; !ret
&& types
[i
]; i
++)
721 ret
= CRYPT_EncodeValueWithType(dwCertEncodingType
, value
, output
,
722 types
[i
], ppszError
);
726 static BOOL
CRYPT_ValueToRDN(DWORD dwCertEncodingType
, PCERT_NAME_INFO info
,
727 PCCRYPT_OID_INFO keyOID
, struct X500TokenW
*value
, DWORD dwStrType
, LPCWSTR
*ppszError
)
731 TRACE("OID %s, value %s\n", debugstr_a(keyOID
->pszOID
),
732 debugstr_wn(value
->start
, value
->end
- value
->start
));
735 info
->rgRDN
= CryptMemAlloc(sizeof(CERT_RDN
));
737 info
->rgRDN
= CryptMemRealloc(info
->rgRDN
,
738 (info
->cRDN
+ 1) * sizeof(CERT_RDN
));
741 /* FIXME: support multiple RDN attrs */
742 info
->rgRDN
[info
->cRDN
].rgRDNAttr
=
743 CryptMemAlloc(sizeof(CERT_RDN_ATTR
));
744 if (info
->rgRDN
[info
->cRDN
].rgRDNAttr
)
746 static const DWORD defaultTypes
[] = { CERT_RDN_PRINTABLE_STRING
,
747 CERT_RDN_BMP_STRING
, 0 };
750 info
->rgRDN
[info
->cRDN
].cRDNAttr
= 1;
751 info
->rgRDN
[info
->cRDN
].rgRDNAttr
[0].pszObjId
=
752 (LPSTR
)keyOID
->pszOID
;
753 info
->rgRDN
[info
->cRDN
].rgRDNAttr
[0].dwValueType
=
754 CERT_RDN_ENCODED_BLOB
;
755 if (keyOID
->ExtraInfo
.cbData
)
756 types
= (const DWORD
*)keyOID
->ExtraInfo
.pbData
;
758 types
= defaultTypes
;
760 /* Remove surrounding quotes */
761 if (value
->start
[0] == '"' && !(dwStrType
& CERT_NAME_STR_NO_QUOTING_FLAG
))
766 ret
= CRYPT_EncodeValue(dwCertEncodingType
, value
,
767 &info
->rgRDN
[info
->cRDN
].rgRDNAttr
[0].Value
, types
, ppszError
);
770 SetLastError(ERROR_OUTOFMEMORY
);
774 SetLastError(ERROR_OUTOFMEMORY
);
778 BOOL WINAPI
CertStrToNameW(DWORD dwCertEncodingType
, LPCWSTR pszX500
,
779 DWORD dwStrType
, void *pvReserved
, BYTE
*pbEncoded
, DWORD
*pcbEncoded
,
782 CERT_NAME_INFO info
= { 0, NULL
};
784 struct KeynameKeeper keeper
;
788 TRACE("(%08lx, %s, %08lx, %p, %p, %p, %p)\n", dwCertEncodingType
,
789 debugstr_w(pszX500
), dwStrType
, pvReserved
, pbEncoded
, pcbEncoded
,
792 CRYPT_InitializeKeynameKeeper(&keeper
);
794 while (str
&& *str
&& ret
)
796 struct X500TokenW token
;
798 ret
= CRYPT_GetNextKeyW(str
, &token
, ppszError
);
799 if (ret
&& token
.start
)
801 PCCRYPT_OID_INFO keyOID
;
803 CRYPT_KeynameKeeperFromTokenW(&keeper
, &token
);
804 keyOID
= CryptFindOIDInfo(CRYPT_OID_INFO_NAME_KEY
, keeper
.keyName
,
805 CRYPT_RDN_ATTR_OID_GROUP_ID
);
809 *ppszError
= token
.start
;
810 SetLastError(CRYPT_E_INVALID_X500_STRING
);
816 while (iswspace(*str
))
822 SetLastError(CRYPT_E_INVALID_X500_STRING
);
831 if (dwStrType
& CERT_NAME_STR_COMMA_FLAG
)
833 else if (dwStrType
& CERT_NAME_STR_SEMICOLON_FLAG
)
835 else if (dwStrType
& CERT_NAME_STR_CRLF_FLAG
)
837 else if (dwStrType
& CERT_NAME_STR_NO_PLUS_FLAG
)
841 ret
= CRYPT_GetNextValueW(str
, dwStrType
, sep
, &sep_used
, &token
,
846 /* if token.end points to the separator, skip it */
847 if (str
&& sep_used
&& *str
== sep_used
) str
++;
849 ret
= CRYPT_ValueToRDN(dwCertEncodingType
, &info
,
850 keyOID
, &token
, dwStrType
, ppszError
);
856 CRYPT_FreeKeynameKeeper(&keeper
);
861 ret
= CryptEncodeObjectEx(dwCertEncodingType
, X509_NAME
, &info
,
862 0, NULL
, pbEncoded
, pcbEncoded
);
864 for (i
= 0; i
< info
.cRDN
; i
++)
868 for (j
= 0; j
< info
.rgRDN
[i
].cRDNAttr
; j
++)
869 LocalFree(info
.rgRDN
[i
].rgRDNAttr
[j
].Value
.pbData
);
870 CryptMemFree(info
.rgRDN
[i
].rgRDNAttr
);
872 CryptMemFree(info
.rgRDN
);
876 DWORD WINAPI
CertGetNameStringA(PCCERT_CONTEXT cert
, DWORD type
,
877 DWORD flags
, void *type_para
, LPSTR name
, DWORD name_len
)
879 DWORD len
, len_mb
, ret
;
882 TRACE("(%p, %ld, %08lx, %p, %p, %ld)\n", cert
, type
, flags
, type_para
, name
, name_len
);
884 len
= CertGetNameStringW(cert
, type
, flags
, type_para
, NULL
, 0);
886 if (!(nameW
= CryptMemAlloc(len
* sizeof(*nameW
))))
889 if (name
&& name_len
) *name
= 0;
893 len
= CertGetNameStringW(cert
, type
, flags
, type_para
, nameW
, len
);
894 len_mb
= WideCharToMultiByte(CP_ACP
, 0, nameW
, len
, NULL
, 0, NULL
, NULL
);
895 if (!name
|| !name_len
)
901 ret
= WideCharToMultiByte(CP_ACP
, 0, nameW
, len
, name
, name_len
, NULL
, NULL
);
911 static BOOL
cert_get_alt_name_info(PCCERT_CONTEXT cert
, BOOL alt_name_issuer
, PCERT_ALT_NAME_INFO
*info
)
913 static const char *oids
[][2] =
915 { szOID_SUBJECT_ALT_NAME2
, szOID_SUBJECT_ALT_NAME
},
916 { szOID_ISSUER_ALT_NAME2
, szOID_ISSUER_ALT_NAME
},
921 ext
= CertFindExtension(oids
[!!alt_name_issuer
][0], cert
->pCertInfo
->cExtension
, cert
->pCertInfo
->rgExtension
);
923 ext
= CertFindExtension(oids
[!!alt_name_issuer
][1], cert
->pCertInfo
->cExtension
, cert
->pCertInfo
->rgExtension
);
924 if (!ext
) return FALSE
;
926 return CryptDecodeObjectEx(cert
->dwCertEncodingType
, X509_ALTERNATE_NAME
, ext
->Value
.pbData
, ext
->Value
.cbData
,
927 CRYPT_DECODE_ALLOC_FLAG
, NULL
, info
, &bytes
);
930 static PCERT_ALT_NAME_ENTRY
cert_find_next_alt_name_entry(PCERT_ALT_NAME_INFO info
, DWORD entry_type
,
935 for (i
= *index
; i
< info
->cAltEntry
; ++i
)
936 if (info
->rgAltEntry
[i
].dwAltNameChoice
== entry_type
)
939 return &info
->rgAltEntry
[i
];
944 /* Searches cert's extensions for the alternate name extension with OID
945 * altNameOID, and if found, searches it for the alternate name type entryType.
946 * If found, returns a pointer to the entry, otherwise returns NULL.
947 * Regardless of whether an entry of the desired type is found, if the
948 * alternate name extension is present, sets *info to the decoded alternate
949 * name extension, which you must free using LocalFree.
950 * The return value is a pointer within *info, so don't free *info before
951 * you're done with the return value.
953 static PCERT_ALT_NAME_ENTRY
cert_find_alt_name_entry(PCCERT_CONTEXT cert
, BOOL alt_name_issuer
,
954 DWORD entry_type
, PCERT_ALT_NAME_INFO
*info
)
956 unsigned int index
= 0;
958 if (!cert_get_alt_name_info(cert
, alt_name_issuer
, info
)) return NULL
;
959 return cert_find_next_alt_name_entry(*info
, entry_type
, &index
);
962 static DWORD
cert_get_name_from_rdn_attr(DWORD encodingType
,
963 const CERT_NAME_BLOB
*name
, LPCSTR oid
, LPWSTR pszNameString
, DWORD cchNameString
)
965 CERT_NAME_INFO
*nameInfo
;
966 DWORD bytes
= 0, ret
= 0;
968 if (CryptDecodeObjectEx(encodingType
, X509_NAME
, name
->pbData
,
969 name
->cbData
, CRYPT_DECODE_ALLOC_FLAG
, NULL
, &nameInfo
, &bytes
))
971 PCERT_RDN_ATTR nameAttr
= NULL
;
974 nameAttr
= CertFindRDNAttr(oid
, nameInfo
);
977 static const LPCSTR attributeOIDs
[] =
979 szOID_RSA_emailAddr
, szOID_COMMON_NAME
,
980 szOID_ORGANIZATIONAL_UNIT_NAME
, szOID_ORGANIZATION_NAME
984 for (i
= 0; !nameAttr
&& i
< ARRAY_SIZE(attributeOIDs
); i
++)
985 nameAttr
= CertFindRDNAttr(attributeOIDs
[i
], nameInfo
);
988 ret
= rdn_value_to_strW(nameAttr
->dwValueType
, &nameAttr
->Value
,
989 pszNameString
, cchNameString
, TRUE
);
995 static DWORD
copy_output_str(WCHAR
*dst
, const WCHAR
*src
, DWORD dst_size
)
997 DWORD len
= wcslen(src
);
999 if (!dst
|| !dst_size
) return len
+ 1;
1000 len
= min(len
, dst_size
- 1);
1001 memcpy(dst
, src
, len
* sizeof(*dst
));
1006 DWORD WINAPI
CertGetNameStringW(PCCERT_CONTEXT cert
, DWORD type
, DWORD flags
, void *type_para
,
1007 LPWSTR name_string
, DWORD name_len
)
1009 static const DWORD supported_flags
= CERT_NAME_ISSUER_FLAG
| CERT_NAME_SEARCH_ALL_NAMES_FLAG
;
1010 BOOL alt_name_issuer
, search_all_names
;
1011 CERT_ALT_NAME_INFO
*info
= NULL
;
1012 PCERT_ALT_NAME_ENTRY entry
;
1013 PCERT_NAME_BLOB name
;
1016 TRACE("(%p, %ld, %08lx, %p, %p, %ld)\n", cert
, type
, flags
, type_para
, name_string
, name_len
);
1021 if (flags
& ~supported_flags
)
1022 FIXME("Unsupported flags %#lx.\n", flags
);
1024 search_all_names
= flags
& CERT_NAME_SEARCH_ALL_NAMES_FLAG
;
1025 if (search_all_names
&& type
!= CERT_NAME_DNS_TYPE
)
1027 WARN("CERT_NAME_SEARCH_ALL_NAMES_FLAG used with type %lu.\n", type
);
1031 alt_name_issuer
= flags
& CERT_NAME_ISSUER_FLAG
;
1032 name
= alt_name_issuer
? &cert
->pCertInfo
->Issuer
: &cert
->pCertInfo
->Subject
;
1036 case CERT_NAME_EMAIL_TYPE
:
1038 entry
= cert_find_alt_name_entry(cert
, alt_name_issuer
, CERT_ALT_NAME_RFC822_NAME
, &info
);
1042 ret
= copy_output_str(name_string
, entry
->pwszRfc822Name
, name_len
);
1045 ret
= cert_get_name_from_rdn_attr(cert
->dwCertEncodingType
, name
, szOID_RSA_emailAddr
,
1046 name_string
, name_len
);
1049 case CERT_NAME_RDN_TYPE
:
1051 DWORD param
= type_para
? *(DWORD
*)type_para
: 0;
1055 ret
= CertNameToStrW(cert
->dwCertEncodingType
, name
, param
, name_string
, name_len
);
1059 entry
= cert_find_alt_name_entry(cert
, alt_name_issuer
, CERT_ALT_NAME_DIRECTORY_NAME
, &info
);
1062 ret
= CertNameToStrW(cert
->dwCertEncodingType
, &entry
->DirectoryName
,
1063 param
, name_string
, name_len
);
1067 case CERT_NAME_ATTR_TYPE
:
1068 ret
= cert_get_name_from_rdn_attr(cert
->dwCertEncodingType
, name
, type_para
,
1069 name_string
, name_len
);
1072 entry
= cert_find_alt_name_entry(cert
, alt_name_issuer
, CERT_ALT_NAME_DIRECTORY_NAME
, &info
);
1075 ret
= cert_name_to_str_with_indent(X509_ASN_ENCODING
, 0, &entry
->DirectoryName
,
1076 0, name_string
, name_len
);
1078 case CERT_NAME_SIMPLE_DISPLAY_TYPE
:
1080 static const LPCSTR simpleAttributeOIDs
[] =
1082 szOID_COMMON_NAME
, szOID_ORGANIZATIONAL_UNIT_NAME
, szOID_ORGANIZATION_NAME
, szOID_RSA_emailAddr
1084 CERT_NAME_INFO
*nameInfo
= NULL
;
1087 if (CryptDecodeObjectEx(cert
->dwCertEncodingType
, X509_NAME
, name
->pbData
, name
->cbData
,
1088 CRYPT_DECODE_ALLOC_FLAG
, NULL
, &nameInfo
, &bytes
))
1090 PCERT_RDN_ATTR nameAttr
= NULL
;
1092 for (i
= 0; !nameAttr
&& i
< ARRAY_SIZE(simpleAttributeOIDs
); i
++)
1093 nameAttr
= CertFindRDNAttr(simpleAttributeOIDs
[i
], nameInfo
);
1095 ret
= rdn_value_to_strW(nameAttr
->dwValueType
, &nameAttr
->Value
, name_string
, name_len
, TRUE
);
1096 LocalFree(nameInfo
);
1099 entry
= cert_find_alt_name_entry(cert
, alt_name_issuer
, CERT_ALT_NAME_RFC822_NAME
, &info
);
1101 if (!entry
&& info
->cAltEntry
)
1102 entry
= &info
->rgAltEntry
[0];
1103 if (entry
) ret
= copy_output_str(name_string
, entry
->pwszRfc822Name
, name_len
);
1106 case CERT_NAME_FRIENDLY_DISPLAY_TYPE
:
1108 DWORD len
= name_len
;
1110 if (CertGetCertificateContextProperty(cert
, CERT_FRIENDLY_NAME_PROP_ID
, name_string
, &len
))
1113 ret
= CertGetNameStringW(cert
, CERT_NAME_SIMPLE_DISPLAY_TYPE
, flags
,
1114 type_para
, name_string
, name_len
);
1117 case CERT_NAME_DNS_TYPE
:
1119 unsigned int index
= 0, len
;
1121 if (cert_get_alt_name_info(cert
, alt_name_issuer
, &info
)
1122 && (entry
= cert_find_next_alt_name_entry(info
, CERT_ALT_NAME_DNS_NAME
, &index
)))
1124 if (search_all_names
)
1128 if (name_string
&& name_len
== 1) break;
1129 ret
+= len
= copy_output_str(name_string
, entry
->pwszDNSName
, name_len
? name_len
- 1 : 0);
1130 if (name_string
&& name_len
)
1136 while ((entry
= cert_find_next_alt_name_entry(info
, CERT_ALT_NAME_DNS_NAME
, &index
)));
1138 else ret
= copy_output_str(name_string
, entry
->pwszDNSName
, name_len
);
1142 if (!search_all_names
|| name_len
!= 1)
1144 len
= search_all_names
&& name_len
? name_len
- 1 : name_len
;
1145 ret
= cert_get_name_from_rdn_attr(cert
->dwCertEncodingType
, name
, szOID_COMMON_NAME
,
1147 if (name_string
) name_string
+= ret
;
1151 if (search_all_names
)
1153 if (name_string
&& name_len
) *name_string
= 0;
1158 case CERT_NAME_URL_TYPE
:
1160 if ((entry
= cert_find_alt_name_entry(cert
, alt_name_issuer
, CERT_ALT_NAME_URL
, &info
)))
1161 ret
= copy_output_str(name_string
, entry
->pwszURL
, name_len
);
1165 FIXME("unimplemented for type %lu.\n", type
);
1176 if (name_string
&& name_len
) name_string
[0] = 0;