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
24 #include "wine/debug.h"
25 #include "wine/unicode.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(crypt
);
29 DWORD WINAPI
CertRDNValueToStrA(DWORD dwValueType
, PCERT_RDN_VALUE_BLOB pValue
,
34 TRACE("(%d, %p, %p, %d)\n", dwValueType
, pValue
, psz
, csz
);
38 case CERT_RDN_ANY_TYPE
:
40 case CERT_RDN_NUMERIC_STRING
:
41 case CERT_RDN_PRINTABLE_STRING
:
42 case CERT_RDN_TELETEX_STRING
:
43 case CERT_RDN_VIDEOTEX_STRING
:
44 case CERT_RDN_IA5_STRING
:
45 case CERT_RDN_GRAPHIC_STRING
:
46 case CERT_RDN_VISIBLE_STRING
:
47 case CERT_RDN_GENERAL_STRING
:
52 DWORD chars
= min(pValue
->cbData
, csz
- 1);
56 memcpy(psz
, pValue
->pbData
, chars
);
62 case CERT_RDN_UTF8_STRING
:
64 ret
= WideCharToMultiByte(CP_UTF8
, 0, (LPWSTR
)pValue
->pbData
,
65 pValue
->cbData
/ sizeof(WCHAR
) + 1, NULL
, 0, NULL
, NULL
);
68 ret
= WideCharToMultiByte(CP_UTF8
, 0, (LPWSTR
)pValue
->pbData
,
69 pValue
->cbData
/ sizeof(WCHAR
) + 1, psz
, csz
- 1, NULL
, NULL
);
74 FIXME("string type %d unimplemented\n", dwValueType
);
84 TRACE("returning %d (%s)\n", ret
, debugstr_a(psz
));
88 DWORD WINAPI
CertRDNValueToStrW(DWORD dwValueType
, PCERT_RDN_VALUE_BLOB pValue
,
89 LPWSTR psz
, DWORD csz
)
93 TRACE("(%d, %p, %p, %d)\n", dwValueType
, pValue
, psz
, csz
);
97 case CERT_RDN_ANY_TYPE
:
99 case CERT_RDN_NUMERIC_STRING
:
100 case CERT_RDN_PRINTABLE_STRING
:
101 case CERT_RDN_TELETEX_STRING
:
102 case CERT_RDN_VIDEOTEX_STRING
:
103 case CERT_RDN_IA5_STRING
:
104 case CERT_RDN_GRAPHIC_STRING
:
105 case CERT_RDN_VISIBLE_STRING
:
106 case CERT_RDN_GENERAL_STRING
:
108 ret
= pValue
->cbData
;
111 DWORD chars
= min(pValue
->cbData
, csz
- 1);
117 for (i
= 0; i
< chars
; i
++)
118 psz
[i
] = pValue
->pbData
[i
];
124 case CERT_RDN_UTF8_STRING
:
126 ret
= pValue
->cbData
/ sizeof(WCHAR
);
129 DWORD chars
= min(pValue
->cbData
/ sizeof(WCHAR
), csz
- 1);
135 for (i
= 0; i
< chars
; i
++)
136 psz
[i
] = *((LPWSTR
)pValue
->pbData
+ i
);
143 FIXME("string type %d unimplemented\n", dwValueType
);
153 TRACE("returning %d (%s)\n", ret
, debugstr_w(psz
));
157 /* Adds the prefix prefix to the string pointed to by psz, followed by the
158 * character '='. Copies no more than csz characters. Returns the number of
159 * characters copied. If psz is NULL, returns the number of characters that
162 static DWORD
CRYPT_AddPrefixA(LPCSTR prefix
, LPSTR psz
, DWORD csz
)
166 TRACE("(%s, %p, %d)\n", debugstr_a(prefix
), psz
, csz
);
170 chars
= min(strlen(prefix
), csz
);
171 memcpy(psz
, prefix
, chars
);
172 *(psz
+ chars
) = '=';
176 chars
= lstrlenA(prefix
) + 1;
180 DWORD WINAPI
CertNameToStrA(DWORD dwCertEncodingType
, PCERT_NAME_BLOB pName
,
181 DWORD dwStrType
, LPSTR psz
, DWORD csz
)
183 static const DWORD unsupportedFlags
= CERT_NAME_STR_NO_QUOTING_FLAG
|
184 CERT_NAME_STR_ENABLE_T61_UNICODE_FLAG
;
185 static const char commaSep
[] = ", ";
186 static const char semiSep
[] = "; ";
187 static const char crlfSep
[] = "\r\n";
188 static const char plusSep
[] = " + ";
189 static const char spaceSep
[] = " ";
190 DWORD ret
= 0, bytes
= 0;
192 CERT_NAME_INFO
*info
;
194 TRACE("(%d, %p, %08x, %p, %d)\n", dwCertEncodingType
, pName
, dwStrType
,
196 if (dwStrType
& unsupportedFlags
)
197 FIXME("unsupported flags: %08x\n", dwStrType
& unsupportedFlags
);
199 bRet
= CryptDecodeObjectEx(dwCertEncodingType
, X509_NAME
, pName
->pbData
,
200 pName
->cbData
, CRYPT_DECODE_ALLOC_FLAG
, NULL
, &info
, &bytes
);
203 DWORD i
, j
, sepLen
, rdnSepLen
;
205 BOOL reverse
= dwStrType
& CERT_NAME_STR_REVERSE_FLAG
;
206 const CERT_RDN
*rdn
= info
->rgRDN
;
208 if(reverse
&& info
->cRDN
> 1) rdn
+= (info
->cRDN
- 1);
210 if (dwStrType
& CERT_NAME_STR_SEMICOLON_FLAG
)
212 else if (dwStrType
& CERT_NAME_STR_CRLF_FLAG
)
216 sepLen
= strlen(sep
);
217 if (dwStrType
& CERT_NAME_STR_NO_PLUS_FLAG
)
221 rdnSepLen
= strlen(rdnSep
);
222 for (i
= 0; (!psz
|| ret
< csz
) && i
< info
->cRDN
; i
++)
224 for (j
= 0; (!psz
|| ret
< csz
) && j
< rdn
->cRDNAttr
; j
++)
227 char prefixBuf
[10]; /* big enough for GivenName */
228 LPCSTR prefix
= NULL
;
230 if ((dwStrType
& 0x000000ff) == CERT_OID_NAME_STR
)
231 prefix
= rdn
->rgRDNAttr
[j
].pszObjId
;
232 else if ((dwStrType
& 0x000000ff) == CERT_X500_NAME_STR
)
234 PCCRYPT_OID_INFO oidInfo
= CryptFindOIDInfo(
235 CRYPT_OID_INFO_OID_KEY
,
236 rdn
->rgRDNAttr
[j
].pszObjId
,
237 CRYPT_RDN_ATTR_OID_GROUP_ID
);
241 WideCharToMultiByte(CP_ACP
, 0, oidInfo
->pwszName
, -1,
242 prefixBuf
, sizeof(prefixBuf
), NULL
, NULL
);
246 prefix
= rdn
->rgRDNAttr
[j
].pszObjId
;
250 /* - 1 is needed to account for the NULL terminator. */
251 chars
= CRYPT_AddPrefixA(prefix
,
252 psz
? psz
+ ret
: NULL
, psz
? csz
- ret
- 1 : 0);
255 /* FIXME: handle quoting */
256 chars
= CertRDNValueToStrA(
257 rdn
->rgRDNAttr
[j
].dwValueType
,
258 &rdn
->rgRDNAttr
[j
].Value
, psz
? psz
+ ret
: NULL
,
259 psz
? csz
- ret
: 0);
262 if (j
< rdn
->cRDNAttr
- 1)
264 if (psz
&& ret
< csz
- rdnSepLen
- 1)
265 memcpy(psz
+ ret
, rdnSep
, rdnSepLen
);
269 if (i
< info
->cRDN
- 1)
271 if (psz
&& ret
< csz
- sepLen
- 1)
272 memcpy(psz
+ ret
, sep
, sepLen
);
287 TRACE("Returning %s\n", debugstr_a(psz
));
291 /* Adds the prefix prefix to the wide-character string pointed to by psz,
292 * followed by the character '='. Copies no more than csz characters. Returns
293 * the number of characters copied. If psz is NULL, returns the number of
294 * characters that would be copied.
295 * Assumes the characters in prefix are ASCII (not multibyte characters.)
297 static DWORD
CRYPT_AddPrefixAToW(LPCSTR prefix
, LPWSTR psz
, DWORD csz
)
301 TRACE("(%s, %p, %d)\n", debugstr_a(prefix
), psz
, csz
);
307 chars
= min(strlen(prefix
), csz
);
308 for (i
= 0; i
< chars
; i
++)
309 *(psz
+ i
) = prefix
[i
];
310 *(psz
+ chars
) = '=';
314 chars
= lstrlenA(prefix
) + 1;
318 /* Adds the prefix prefix to the string pointed to by psz, followed by the
319 * character '='. Copies no more than csz characters. Returns the number of
320 * characters copied. If psz is NULL, returns the number of characters that
323 static DWORD
CRYPT_AddPrefixW(LPCWSTR prefix
, LPWSTR psz
, DWORD csz
)
327 TRACE("(%s, %p, %d)\n", debugstr_w(prefix
), psz
, csz
);
331 chars
= min(strlenW(prefix
), csz
);
332 memcpy(psz
, prefix
, chars
* sizeof(WCHAR
));
333 *(psz
+ chars
) = '=';
337 chars
= lstrlenW(prefix
) + 1;
341 static const WCHAR indent
[] = { ' ',' ',' ',' ',' ',0 };
343 DWORD
cert_name_to_str_with_indent(DWORD dwCertEncodingType
, DWORD indentLevel
,
344 PCERT_NAME_BLOB pName
, DWORD dwStrType
, LPWSTR psz
, DWORD csz
)
346 static const DWORD unsupportedFlags
= CERT_NAME_STR_NO_QUOTING_FLAG
|
347 CERT_NAME_STR_ENABLE_T61_UNICODE_FLAG
;
348 static const WCHAR commaSep
[] = { ',',' ',0 };
349 static const WCHAR semiSep
[] = { ';',' ',0 };
350 static const WCHAR crlfSep
[] = { '\r','\n',0 };
351 static const WCHAR plusSep
[] = { ' ','+',' ',0 };
352 static const WCHAR spaceSep
[] = { ' ',0 };
353 DWORD ret
= 0, bytes
= 0;
355 CERT_NAME_INFO
*info
;
357 if (dwStrType
& unsupportedFlags
)
358 FIXME("unsupported flags: %08x\n", dwStrType
& unsupportedFlags
);
360 bRet
= CryptDecodeObjectEx(dwCertEncodingType
, X509_NAME
, pName
->pbData
,
361 pName
->cbData
, CRYPT_DECODE_ALLOC_FLAG
, NULL
, &info
, &bytes
);
364 DWORD i
, j
, sepLen
, rdnSepLen
;
366 BOOL reverse
= dwStrType
& CERT_NAME_STR_REVERSE_FLAG
;
367 const CERT_RDN
*rdn
= info
->rgRDN
;
369 if(reverse
&& info
->cRDN
> 1) rdn
+= (info
->cRDN
- 1);
371 if (dwStrType
& CERT_NAME_STR_SEMICOLON_FLAG
)
373 else if (dwStrType
& CERT_NAME_STR_CRLF_FLAG
)
377 sepLen
= lstrlenW(sep
);
378 if (dwStrType
& CERT_NAME_STR_NO_PLUS_FLAG
)
382 rdnSepLen
= lstrlenW(rdnSep
);
383 for (i
= 0; (!psz
|| ret
< csz
) && i
< info
->cRDN
; i
++)
385 for (j
= 0; (!psz
|| ret
< csz
) && j
< rdn
->cRDNAttr
; j
++)
388 LPCSTR prefixA
= NULL
;
389 LPCWSTR prefixW
= NULL
;
391 if ((dwStrType
& 0x000000ff) == CERT_OID_NAME_STR
)
392 prefixA
= rdn
->rgRDNAttr
[j
].pszObjId
;
393 else if ((dwStrType
& 0x000000ff) == CERT_X500_NAME_STR
)
395 PCCRYPT_OID_INFO oidInfo
= CryptFindOIDInfo(
396 CRYPT_OID_INFO_OID_KEY
,
397 rdn
->rgRDNAttr
[j
].pszObjId
,
398 CRYPT_RDN_ATTR_OID_GROUP_ID
);
401 prefixW
= oidInfo
->pwszName
;
403 prefixA
= rdn
->rgRDNAttr
[j
].pszObjId
;
405 if (dwStrType
& CERT_NAME_STR_CRLF_FLAG
)
409 for (k
= 0; k
< indentLevel
; k
++)
413 chars
= min(strlenW(indent
), csz
- ret
- 1);
414 memcpy(psz
+ ret
, indent
, chars
* sizeof(WCHAR
));
417 chars
= strlenW(indent
);
423 /* - 1 is needed to account for the NULL terminator. */
424 chars
= CRYPT_AddPrefixW(prefixW
,
425 psz
? psz
+ ret
: NULL
, psz
? csz
- ret
- 1 : 0);
430 /* - 1 is needed to account for the NULL terminator. */
431 chars
= CRYPT_AddPrefixAToW(prefixA
,
432 psz
? psz
+ ret
: NULL
, psz
? csz
- ret
- 1 : 0);
435 /* FIXME: handle quoting */
436 chars
= CertRDNValueToStrW(
437 rdn
->rgRDNAttr
[j
].dwValueType
,
438 &rdn
->rgRDNAttr
[j
].Value
, psz
? psz
+ ret
: NULL
,
439 psz
? csz
- ret
: 0);
442 if (j
< rdn
->cRDNAttr
- 1)
444 if (psz
&& ret
< csz
- rdnSepLen
- 1)
445 memcpy(psz
+ ret
, rdnSep
, rdnSepLen
* sizeof(WCHAR
));
449 if (i
< info
->cRDN
- 1)
451 if (psz
&& ret
< csz
- sepLen
- 1)
452 memcpy(psz
+ ret
, sep
, sepLen
* sizeof(WCHAR
));
470 DWORD WINAPI
CertNameToStrW(DWORD dwCertEncodingType
, PCERT_NAME_BLOB pName
,
471 DWORD dwStrType
, LPWSTR psz
, DWORD csz
)
475 TRACE("(%d, %p, %08x, %p, %d)\n", dwCertEncodingType
, pName
, dwStrType
,
478 ret
= cert_name_to_str_with_indent(dwCertEncodingType
, 0, pName
, dwStrType
,
480 TRACE("Returning %s\n", debugstr_w(psz
));
484 BOOL WINAPI
CertStrToNameA(DWORD dwCertEncodingType
, LPCSTR pszX500
,
485 DWORD dwStrType
, void *pvReserved
, BYTE
*pbEncoded
, DWORD
*pcbEncoded
,
491 TRACE("(%08x, %s, %08x, %p, %p, %p, %p)\n", dwCertEncodingType
,
492 debugstr_a(pszX500
), dwStrType
, pvReserved
, pbEncoded
, pcbEncoded
,
495 len
= MultiByteToWideChar(CP_ACP
, 0, pszX500
, -1, NULL
, 0);
498 LPWSTR x500
, errorStr
;
500 if ((x500
= CryptMemAlloc(len
* sizeof(WCHAR
))))
502 MultiByteToWideChar(CP_ACP
, 0, pszX500
, -1, x500
, len
);
503 ret
= CertStrToNameW(dwCertEncodingType
, x500
, dwStrType
,
504 pvReserved
, pbEncoded
, pcbEncoded
,
505 ppszError
? (LPCWSTR
*)&errorStr
: NULL
);
512 *ppszError
= pszX500
;
513 for (i
= 0; i
< errorStr
- x500
; i
++)
514 *ppszError
= CharNextA(*ppszError
);
523 SetLastError(ERROR_OUTOFMEMORY
);
529 SetLastError(CRYPT_E_INVALID_X500_STRING
);
531 *ppszError
= pszX500
;
539 WCHAR buf
[10]; /* big enough for L"GivenName" */
540 LPWSTR keyName
; /* usually = buf, but may be allocated */
544 static void CRYPT_InitializeKeynameKeeper(struct KeynameKeeper
*keeper
)
546 keeper
->keyName
= keeper
->buf
;
547 keeper
->keyLen
= sizeof(keeper
->buf
) / sizeof(keeper
->buf
[0]);
550 static void CRYPT_FreeKeynameKeeper(struct KeynameKeeper
*keeper
)
552 if (keeper
->keyName
!= keeper
->buf
)
553 CryptMemFree(keeper
->keyName
);
562 static void CRYPT_KeynameKeeperFromTokenW(struct KeynameKeeper
*keeper
,
563 const struct X500TokenW
*key
)
565 DWORD len
= key
->end
- key
->start
;
567 if (len
> keeper
->keyLen
)
569 if (keeper
->keyName
== keeper
->buf
)
570 keeper
->keyName
= CryptMemAlloc(len
* sizeof(WCHAR
));
572 keeper
->keyName
= CryptMemRealloc(keeper
->keyName
,
573 len
* sizeof(WCHAR
));
574 keeper
->keyLen
= len
;
576 memcpy(keeper
->keyName
, key
->start
, (key
->end
- key
->start
) *
578 keeper
->keyName
[len
] = '\0';
579 TRACE("Keyname is %s\n", debugstr_w(keeper
->keyName
));
582 static BOOL
CRYPT_GetNextKeyW(LPCWSTR str
, struct X500TokenW
*token
,
587 while (*str
&& isspaceW(*str
))
592 while (*str
&& *str
!= '=' && !isspaceW(*str
))
594 if (*str
&& (*str
== '=' || isspaceW(*str
)))
598 TRACE("missing equals char at %s\n", debugstr_w(token
->start
));
600 *ppszError
= token
->start
;
601 SetLastError(CRYPT_E_INVALID_X500_STRING
);
610 /* Assumes separators are characters in the 0-255 range */
611 static BOOL
CRYPT_GetNextValueW(LPCWSTR str
, DWORD dwFlags
, LPCWSTR separators
,
612 struct X500TokenW
*token
, LPCWSTR
*ppszError
)
616 TRACE("(%s, %s, %p, %p)\n", debugstr_w(str
), debugstr_w(separators
), token
,
619 while (*str
&& isspaceW(*str
))
624 if (!(dwFlags
& CERT_NAME_STR_NO_QUOTING_FLAG
) && *str
== '"')
628 while (!token
->end
&& ret
)
630 while (*str
&& *str
!= '"')
634 if (*(str
+ 1) != '"')
635 token
->end
= str
+ 1;
641 TRACE("unterminated quote at %s\n", debugstr_w(str
));
644 SetLastError(CRYPT_E_INVALID_X500_STRING
);
651 WCHAR map
[256] = { 0 };
654 map
[*separators
++] = 1;
655 while (*str
&& (*str
>= 0xff || !map
[*str
]))
662 TRACE("missing value at %s\n", debugstr_w(str
));
665 SetLastError(CRYPT_E_INVALID_X500_STRING
);
671 /* Encodes the string represented by value as the string type type into the
672 * CERT_NAME_BLOB output. If there is an error and ppszError is not NULL,
673 * *ppszError is set to the first failing character. If there is no error,
674 * output's pbData must be freed with LocalFree.
676 static BOOL
CRYPT_EncodeValueWithType(DWORD dwCertEncodingType
,
677 const struct X500TokenW
*value
, PCERT_NAME_BLOB output
, DWORD type
,
680 CERT_NAME_VALUE nameValue
= { type
, { 0, NULL
} };
683 if (value
->end
> value
->start
)
685 nameValue
.Value
.pbData
= CryptMemAlloc((value
->end
- value
->start
) *
687 if (!nameValue
.Value
.pbData
)
689 SetLastError(ERROR_OUTOFMEMORY
);
695 if (value
->end
> value
->start
)
698 LPWSTR ptr
= (LPWSTR
)nameValue
.Value
.pbData
;
700 for (i
= 0; i
< value
->end
- value
->start
; i
++)
702 *ptr
++ = value
->start
[i
];
703 if (value
->start
[i
] == '"')
706 nameValue
.Value
.cbData
= (LPBYTE
)ptr
- nameValue
.Value
.pbData
;
708 ret
= CryptEncodeObjectEx(dwCertEncodingType
, X509_UNICODE_NAME_VALUE
,
709 &nameValue
, CRYPT_ENCODE_ALLOC_FLAG
, NULL
, &output
->pbData
,
711 if (!ret
&& ppszError
)
713 if (type
== CERT_RDN_NUMERIC_STRING
&&
714 GetLastError() == CRYPT_E_INVALID_NUMERIC_STRING
)
715 *ppszError
= value
->start
+ output
->cbData
;
716 else if (type
== CERT_RDN_PRINTABLE_STRING
&&
717 GetLastError() == CRYPT_E_INVALID_PRINTABLE_STRING
)
718 *ppszError
= value
->start
+ output
->cbData
;
719 else if (type
== CERT_RDN_IA5_STRING
&&
720 GetLastError() == CRYPT_E_INVALID_IA5_STRING
)
721 *ppszError
= value
->start
+ output
->cbData
;
723 CryptMemFree(nameValue
.Value
.pbData
);
728 static BOOL
CRYPT_EncodeValue(DWORD dwCertEncodingType
,
729 const struct X500TokenW
*value
, PCERT_NAME_BLOB output
, const DWORD
*types
,
736 for (i
= 0; !ret
&& types
[i
]; i
++)
737 ret
= CRYPT_EncodeValueWithType(dwCertEncodingType
, value
, output
,
738 types
[i
], ppszError
);
742 static BOOL
CRYPT_ValueToRDN(DWORD dwCertEncodingType
, PCERT_NAME_INFO info
,
743 PCCRYPT_OID_INFO keyOID
, struct X500TokenW
*value
, LPCWSTR
*ppszError
)
747 TRACE("OID %s, value %s\n", debugstr_a(keyOID
->pszOID
),
748 debugstr_wn(value
->start
, value
->end
- value
->start
));
751 info
->rgRDN
= CryptMemAlloc(sizeof(CERT_RDN
));
753 info
->rgRDN
= CryptMemRealloc(info
->rgRDN
,
754 (info
->cRDN
+ 1) * sizeof(CERT_RDN
));
757 /* FIXME: support multiple RDN attrs */
758 info
->rgRDN
[info
->cRDN
].rgRDNAttr
=
759 CryptMemAlloc(sizeof(CERT_RDN_ATTR
));
760 if (info
->rgRDN
[info
->cRDN
].rgRDNAttr
)
762 static const DWORD defaultTypes
[] = { CERT_RDN_PRINTABLE_STRING
,
763 CERT_RDN_BMP_STRING
, 0 };
766 info
->rgRDN
[info
->cRDN
].cRDNAttr
= 1;
767 info
->rgRDN
[info
->cRDN
].rgRDNAttr
[0].pszObjId
=
768 (LPSTR
)keyOID
->pszOID
;
769 info
->rgRDN
[info
->cRDN
].rgRDNAttr
[0].dwValueType
=
770 CERT_RDN_ENCODED_BLOB
;
771 if (keyOID
->ExtraInfo
.cbData
)
772 types
= (const DWORD
*)keyOID
->ExtraInfo
.pbData
;
774 types
= defaultTypes
;
776 /* Remove surrounding quotes */
777 if (value
->start
[0] == '"')
782 ret
= CRYPT_EncodeValue(dwCertEncodingType
, value
,
783 &info
->rgRDN
[info
->cRDN
].rgRDNAttr
[0].Value
, types
, ppszError
);
786 SetLastError(ERROR_OUTOFMEMORY
);
790 SetLastError(ERROR_OUTOFMEMORY
);
794 BOOL WINAPI
CertStrToNameW(DWORD dwCertEncodingType
, LPCWSTR pszX500
,
795 DWORD dwStrType
, void *pvReserved
, BYTE
*pbEncoded
, DWORD
*pcbEncoded
,
798 CERT_NAME_INFO info
= { 0, NULL
};
800 struct KeynameKeeper keeper
;
804 TRACE("(%08x, %s, %08x, %p, %p, %p, %p)\n", dwCertEncodingType
,
805 debugstr_w(pszX500
), dwStrType
, pvReserved
, pbEncoded
, pcbEncoded
,
808 CRYPT_InitializeKeynameKeeper(&keeper
);
810 while (str
&& *str
&& ret
)
812 struct X500TokenW token
;
814 ret
= CRYPT_GetNextKeyW(str
, &token
, ppszError
);
815 if (ret
&& token
.start
)
817 PCCRYPT_OID_INFO keyOID
;
819 CRYPT_KeynameKeeperFromTokenW(&keeper
, &token
);
820 keyOID
= CryptFindOIDInfo(CRYPT_OID_INFO_NAME_KEY
, keeper
.keyName
,
821 CRYPT_RDN_ATTR_OID_GROUP_ID
);
825 *ppszError
= token
.start
;
826 SetLastError(CRYPT_E_INVALID_X500_STRING
);
832 while (isspace(*str
))
838 SetLastError(CRYPT_E_INVALID_X500_STRING
);
843 static const WCHAR commaSep
[] = { ',',0 };
844 static const WCHAR semiSep
[] = { ';',0 };
845 static const WCHAR crlfSep
[] = { '\r','\n',0 };
846 static const WCHAR allSeps
[] = { ',',';','\r','\n',0 };
850 if (dwStrType
& CERT_NAME_STR_COMMA_FLAG
)
852 else if (dwStrType
& CERT_NAME_STR_SEMICOLON_FLAG
)
854 else if (dwStrType
& CERT_NAME_STR_CRLF_FLAG
)
858 ret
= CRYPT_GetNextValueW(str
, dwStrType
, sep
, &token
,
863 ret
= CRYPT_ValueToRDN(dwCertEncodingType
, &info
,
864 keyOID
, &token
, ppszError
);
870 CRYPT_FreeKeynameKeeper(&keeper
);
875 ret
= CryptEncodeObjectEx(dwCertEncodingType
, X509_NAME
, &info
,
876 0, NULL
, pbEncoded
, pcbEncoded
);
878 for (i
= 0; i
< info
.cRDN
; i
++)
882 for (j
= 0; j
< info
.rgRDN
[i
].cRDNAttr
; j
++)
883 LocalFree(info
.rgRDN
[i
].rgRDNAttr
[j
].Value
.pbData
);
884 CryptMemFree(info
.rgRDN
[i
].rgRDNAttr
);
886 CryptMemFree(info
.rgRDN
);
890 DWORD WINAPI
CertGetNameStringA(PCCERT_CONTEXT pCertContext
, DWORD dwType
,
891 DWORD dwFlags
, void *pvTypePara
, LPSTR pszNameString
, DWORD cchNameString
)
895 TRACE("(%p, %d, %08x, %p, %p, %d)\n", pCertContext
, dwType
, dwFlags
,
896 pvTypePara
, pszNameString
, cchNameString
);
903 nameLen
= CertGetNameStringW(pCertContext
, dwType
, dwFlags
, pvTypePara
,
905 wideName
= CryptMemAlloc(nameLen
* sizeof(WCHAR
));
908 CertGetNameStringW(pCertContext
, dwType
, dwFlags
, pvTypePara
,
910 nameLen
= WideCharToMultiByte(CP_ACP
, 0, wideName
, nameLen
,
911 pszNameString
, cchNameString
, NULL
, NULL
);
912 if (nameLen
<= cchNameString
)
916 pszNameString
[cchNameString
- 1] = '\0';
919 CryptMemFree(wideName
);
923 *pszNameString
= '\0';
928 ret
= CertGetNameStringW(pCertContext
, dwType
, dwFlags
, pvTypePara
,
933 DWORD WINAPI
CertGetNameStringW(PCCERT_CONTEXT pCertContext
, DWORD dwType
,
934 DWORD dwFlags
, void *pvTypePara
, LPWSTR pszNameString
, DWORD cchNameString
)
937 PCERT_NAME_BLOB name
;
940 TRACE("(%p, %d, %08x, %p, %p, %d)\n", pCertContext
, dwType
,
941 dwFlags
, pvTypePara
, pszNameString
, cchNameString
);
943 if (dwFlags
& CERT_NAME_ISSUER_FLAG
)
945 name
= &pCertContext
->pCertInfo
->Issuer
;
946 altNameOID
= szOID_ISSUER_ALT_NAME
;
950 name
= &pCertContext
->pCertInfo
->Subject
;
951 altNameOID
= szOID_SUBJECT_ALT_NAME
;
956 case CERT_NAME_SIMPLE_DISPLAY_TYPE
:
958 static const LPCSTR simpleAttributeOIDs
[] = { szOID_COMMON_NAME
,
959 szOID_ORGANIZATIONAL_UNIT_NAME
, szOID_ORGANIZATION_NAME
,
960 szOID_RSA_emailAddr
};
961 CERT_NAME_INFO
*info
= NULL
;
962 PCERT_RDN_ATTR nameAttr
= NULL
;
965 if (CryptDecodeObjectEx(pCertContext
->dwCertEncodingType
, X509_NAME
,
966 name
->pbData
, name
->cbData
, CRYPT_DECODE_ALLOC_FLAG
, NULL
, &info
,
969 for (i
= 0; !nameAttr
&& i
< sizeof(simpleAttributeOIDs
) /
970 sizeof(simpleAttributeOIDs
[0]); i
++)
971 nameAttr
= CertFindRDNAttr(simpleAttributeOIDs
[i
], info
);
975 PCERT_EXTENSION ext
= CertFindExtension(altNameOID
,
976 pCertContext
->pCertInfo
->cExtension
,
977 pCertContext
->pCertInfo
->rgExtension
);
981 for (i
= 0; !nameAttr
&& i
< sizeof(simpleAttributeOIDs
) /
982 sizeof(simpleAttributeOIDs
[0]); i
++)
983 nameAttr
= CertFindRDNAttr(simpleAttributeOIDs
[i
], info
);
986 /* FIXME: gotta then look for a rfc822Name choice in ext.
987 * Failing that, look for the first attribute.
989 FIXME("CERT_NAME_SIMPLE_DISPLAY_TYPE: stub\n");
994 ret
= CertRDNValueToStrW(nameAttr
->dwValueType
, &nameAttr
->Value
,
995 pszNameString
, cchNameString
);
1002 case CERT_NAME_FRIENDLY_DISPLAY_TYPE
:
1004 DWORD cch
= cchNameString
;
1006 if (CertGetCertificateContextProperty(pCertContext
,
1007 CERT_FRIENDLY_NAME_PROP_ID
, pszNameString
, &cch
))
1010 ret
= CertGetNameStringW(pCertContext
,
1011 CERT_NAME_SIMPLE_DISPLAY_TYPE
, dwFlags
, pvTypePara
, pszNameString
,
1016 FIXME("unimplemented for type %d\n", dwType
);