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(lstrlenA(prefix
), csz
);
171 memcpy(psz
, prefix
, chars
);
173 *(psz
+ chars
) = '=';
178 chars
= lstrlenA(prefix
) + 1;
182 DWORD WINAPI
CertNameToStrA(DWORD dwCertEncodingType
, PCERT_NAME_BLOB pName
,
183 DWORD dwStrType
, LPSTR psz
, DWORD csz
)
185 static const DWORD unsupportedFlags
= CERT_NAME_STR_NO_QUOTING_FLAG
|
186 CERT_NAME_STR_REVERSE_FLAG
| CERT_NAME_STR_ENABLE_T61_UNICODE_FLAG
;
187 static const char commaSep
[] = ", ";
188 static const char semiSep
[] = "; ";
189 static const char crlfSep
[] = "\r\n";
190 static const char plusSep
[] = " + ";
191 static const char spaceSep
[] = " ";
192 DWORD ret
= 0, bytes
= 0;
194 CERT_NAME_INFO
*info
;
196 TRACE("(%d, %p, %08x, %p, %d)\n", dwCertEncodingType
, pName
, dwStrType
,
198 if (dwStrType
& unsupportedFlags
)
199 FIXME("unsupported flags: %08x\n", dwStrType
& unsupportedFlags
);
201 bRet
= CryptDecodeObjectEx(dwCertEncodingType
, X509_NAME
, pName
->pbData
,
202 pName
->cbData
, CRYPT_DECODE_ALLOC_FLAG
, NULL
, &info
, &bytes
);
205 DWORD i
, j
, sepLen
, rdnSepLen
;
208 if (dwStrType
& CERT_NAME_STR_SEMICOLON_FLAG
)
210 else if (dwStrType
& CERT_NAME_STR_CRLF_FLAG
)
214 sepLen
= strlen(sep
);
215 if (dwStrType
& CERT_NAME_STR_NO_PLUS_FLAG
)
219 rdnSepLen
= strlen(rdnSep
);
220 for (i
= 0; (!psz
|| ret
< csz
) && i
< info
->cRDN
; i
++)
222 for (j
= 0; (!psz
|| ret
< csz
) && j
< info
->rgRDN
[i
].cRDNAttr
; j
++)
225 char prefixBuf
[10]; /* big enough for GivenName */
226 LPCSTR prefix
= NULL
;
228 if ((dwStrType
& 0x000000ff) == CERT_OID_NAME_STR
)
229 prefix
= info
->rgRDN
[i
].rgRDNAttr
[j
].pszObjId
;
230 else if ((dwStrType
& 0x000000ff) == CERT_X500_NAME_STR
)
232 PCCRYPT_OID_INFO oidInfo
= CryptFindOIDInfo(
233 CRYPT_OID_INFO_OID_KEY
,
234 info
->rgRDN
[i
].rgRDNAttr
[j
].pszObjId
,
235 CRYPT_RDN_ATTR_OID_GROUP_ID
);
239 WideCharToMultiByte(CP_ACP
, 0, oidInfo
->pwszName
, -1,
240 prefixBuf
, sizeof(prefixBuf
), NULL
, NULL
);
244 prefix
= info
->rgRDN
[i
].rgRDNAttr
[j
].pszObjId
;
248 /* - 1 is needed to account for the NULL terminator. */
249 chars
= CRYPT_AddPrefixA(prefix
,
250 psz
? psz
+ ret
: NULL
, psz
? csz
- ret
- 1 : 0);
254 /* FIXME: handle quoting */
255 chars
= CertRDNValueToStrA(
256 info
->rgRDN
[i
].rgRDNAttr
[j
].dwValueType
,
257 &info
->rgRDN
[i
].rgRDNAttr
[j
].Value
, psz
? psz
+ ret
: NULL
,
258 psz
? csz
- ret
: 0);
261 if (j
< info
->rgRDN
[i
].cRDNAttr
- 1)
263 if (psz
&& ret
< csz
- rdnSepLen
- 1)
264 memcpy(psz
+ ret
, rdnSep
, rdnSepLen
);
268 if (i
< info
->cRDN
- 1)
270 if (psz
&& ret
< csz
- sepLen
- 1)
271 memcpy(psz
+ ret
, sep
, sepLen
);
285 TRACE("Returning %s\n", debugstr_a(psz
));
289 /* Adds the prefix prefix to the wide-character string pointed to by psz,
290 * followed by the character '='. Copies no more than csz characters. Returns
291 * the number of characters copied. If psz is NULL, returns the number of
292 * characters that would be copied.
293 * Assumes the characters in prefix are ASCII (not multibyte characters.)
295 static DWORD
CRYPT_AddPrefixAToW(LPCSTR prefix
, LPWSTR psz
, DWORD csz
)
299 TRACE("(%s, %p, %d)\n", debugstr_a(prefix
), psz
, csz
);
305 chars
= min(lstrlenA(prefix
), csz
);
306 for (i
= 0; i
< chars
; i
++)
307 *(psz
+ i
) = prefix
[i
];
309 *(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(lstrlenW(prefix
), csz
);
332 memcpy(psz
, prefix
, chars
* sizeof(WCHAR
));
334 *(psz
+ chars
) = '=';
339 chars
= lstrlenW(prefix
) + 1;
343 DWORD WINAPI
CertNameToStrW(DWORD dwCertEncodingType
, PCERT_NAME_BLOB pName
,
344 DWORD dwStrType
, LPWSTR psz
, DWORD csz
)
346 static const DWORD unsupportedFlags
= CERT_NAME_STR_NO_QUOTING_FLAG
|
347 CERT_NAME_STR_REVERSE_FLAG
| 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 TRACE("(%d, %p, %08x, %p, %d)\n", dwCertEncodingType
, pName
, dwStrType
,
359 if (dwStrType
& unsupportedFlags
)
360 FIXME("unsupported flags: %08x\n", dwStrType
& unsupportedFlags
);
362 bRet
= CryptDecodeObjectEx(dwCertEncodingType
, X509_NAME
, pName
->pbData
,
363 pName
->cbData
, CRYPT_DECODE_ALLOC_FLAG
, NULL
, &info
, &bytes
);
366 DWORD i
, j
, sepLen
, rdnSepLen
;
369 if (dwStrType
& CERT_NAME_STR_SEMICOLON_FLAG
)
371 else if (dwStrType
& CERT_NAME_STR_CRLF_FLAG
)
375 sepLen
= lstrlenW(sep
);
376 if (dwStrType
& CERT_NAME_STR_NO_PLUS_FLAG
)
380 rdnSepLen
= lstrlenW(rdnSep
);
381 for (i
= 0; (!psz
|| ret
< csz
) && i
< info
->cRDN
; i
++)
383 for (j
= 0; (!psz
|| ret
< csz
) && j
< info
->rgRDN
[i
].cRDNAttr
; j
++)
386 LPCSTR prefixA
= NULL
;
387 LPCWSTR prefixW
= NULL
;
389 if ((dwStrType
& 0x000000ff) == CERT_OID_NAME_STR
)
390 prefixA
= info
->rgRDN
[i
].rgRDNAttr
[j
].pszObjId
;
391 else if ((dwStrType
& 0x000000ff) == CERT_X500_NAME_STR
)
393 PCCRYPT_OID_INFO oidInfo
= CryptFindOIDInfo(
394 CRYPT_OID_INFO_OID_KEY
,
395 info
->rgRDN
[i
].rgRDNAttr
[j
].pszObjId
,
396 CRYPT_RDN_ATTR_OID_GROUP_ID
);
399 prefixW
= oidInfo
->pwszName
;
401 prefixA
= info
->rgRDN
[i
].rgRDNAttr
[j
].pszObjId
;
405 /* - 1 is needed to account for the NULL terminator. */
406 chars
= CRYPT_AddPrefixW(prefixW
,
407 psz
? psz
+ ret
: NULL
, psz
? csz
- ret
- 1 : 0);
413 /* - 1 is needed to account for the NULL terminator. */
414 chars
= CRYPT_AddPrefixAToW(prefixA
,
415 psz
? psz
+ ret
: NULL
, psz
? csz
- ret
- 1 : 0);
419 /* FIXME: handle quoting */
420 chars
= CertRDNValueToStrW(
421 info
->rgRDN
[i
].rgRDNAttr
[j
].dwValueType
,
422 &info
->rgRDN
[i
].rgRDNAttr
[j
].Value
, psz
? psz
+ ret
: NULL
,
423 psz
? csz
- ret
: 0);
426 if (j
< info
->rgRDN
[i
].cRDNAttr
- 1)
428 if (psz
&& ret
< csz
- rdnSepLen
- 1)
429 memcpy(psz
+ ret
, rdnSep
, rdnSepLen
* sizeof(WCHAR
));
433 if (i
< info
->cRDN
- 1)
435 if (psz
&& ret
< csz
- sepLen
- 1)
436 memcpy(psz
+ ret
, sep
, sepLen
* sizeof(WCHAR
));
450 TRACE("Returning %s\n", debugstr_w(psz
));
454 BOOL WINAPI
CertStrToNameA(DWORD dwCertEncodingType
, LPCSTR pszX500
,
455 DWORD dwStrType
, void *pvReserved
, BYTE
*pbEncoded
, DWORD
*pcbEncoded
,
461 TRACE("(%08x, %s, %08x, %p, %p, %p, %p)\n", dwCertEncodingType
,
462 debugstr_a(pszX500
), dwStrType
, pvReserved
, pbEncoded
, pcbEncoded
,
465 len
= MultiByteToWideChar(CP_ACP
, 0, pszX500
, -1, NULL
, 0);
468 LPWSTR x500
, errorStr
;
470 if ((x500
= CryptMemAlloc(len
* sizeof(WCHAR
))))
472 MultiByteToWideChar(CP_ACP
, 0, pszX500
, -1, x500
, len
);
473 ret
= CertStrToNameW(dwCertEncodingType
, x500
, dwStrType
,
474 pvReserved
, pbEncoded
, pcbEncoded
,
475 ppszError
? (LPCWSTR
*)&errorStr
: NULL
);
482 *ppszError
= pszX500
;
483 for (i
= 0; i
< errorStr
- x500
; i
++)
484 *ppszError
= CharNextA(*ppszError
);
493 SetLastError(ERROR_OUTOFMEMORY
);
499 SetLastError(CRYPT_E_INVALID_X500_STRING
);
501 *ppszError
= pszX500
;
509 WCHAR buf
[10]; /* big enough for L"GivenName" */
510 LPWSTR keyName
; /* usually = buf, but may be allocated */
514 static void CRYPT_InitializeKeynameKeeper(struct KeynameKeeper
*keeper
)
516 keeper
->keyName
= keeper
->buf
;
517 keeper
->keyLen
= sizeof(keeper
->buf
) / sizeof(keeper
->buf
[0]);
520 static void CRYPT_FreeKeynameKeeper(struct KeynameKeeper
*keeper
)
522 if (keeper
->keyName
!= keeper
->buf
)
523 CryptMemFree(keeper
->keyName
);
532 static void CRYPT_KeynameKeeperFromTokenW(struct KeynameKeeper
*keeper
,
533 const struct X500TokenW
*key
)
535 DWORD len
= key
->end
- key
->start
;
537 if (len
> keeper
->keyLen
)
539 if (keeper
->keyName
== keeper
->buf
)
540 keeper
->keyName
= CryptMemAlloc(len
* sizeof(WCHAR
));
542 keeper
->keyName
= CryptMemRealloc(keeper
->keyName
,
543 len
* sizeof(WCHAR
));
544 keeper
->keyLen
= len
;
546 memcpy(keeper
->keyName
, key
->start
, (key
->end
- key
->start
) *
548 keeper
->keyName
[len
] = '\0';
549 TRACE("Keyname is %s\n", debugstr_w(keeper
->keyName
));
552 static BOOL
CRYPT_GetNextKeyW(LPCWSTR str
, struct X500TokenW
*token
,
557 while (*str
&& isspaceW(*str
))
562 while (*str
&& *str
!= '=' && !isspaceW(*str
))
564 if (*str
&& (*str
== '=' || isspaceW(*str
)))
568 TRACE("missing equals char at %s\n", debugstr_w(token
->start
));
570 *ppszError
= token
->start
;
571 SetLastError(CRYPT_E_INVALID_X500_STRING
);
580 /* Assumes separators are characters in the 0-255 range */
581 static BOOL
CRYPT_GetNextValueW(LPCWSTR str
, DWORD dwFlags
, LPCWSTR separators
,
582 struct X500TokenW
*token
, LPCWSTR
*ppszError
)
586 TRACE("(%s, %s, %p, %p)\n", debugstr_w(str
), debugstr_w(separators
), token
,
589 while (*str
&& isspaceW(*str
))
594 if (!(dwFlags
& CERT_NAME_STR_NO_QUOTING_FLAG
) && *str
== '"')
598 while (!token
->end
&& ret
)
600 while (*str
&& *str
!= '"')
604 if (*(str
+ 1) != '"')
605 token
->end
= str
+ 1;
611 TRACE("unterminated quote at %s\n", debugstr_w(str
));
614 SetLastError(CRYPT_E_INVALID_X500_STRING
);
621 WCHAR map
[256] = { 0 };
624 map
[*separators
++] = 1;
625 while (*str
&& (*str
>= 0xff || !map
[*str
]))
632 TRACE("missing value at %s\n", debugstr_w(str
));
635 SetLastError(CRYPT_E_INVALID_X500_STRING
);
641 /* Encodes the string represented by value as the string type type into the
642 * CERT_NAME_BLOB output. If there is an error and ppszError is not NULL,
643 * *ppszError is set to the first failing character. If there is no error,
644 * output's pbData must be freed with LocalFree.
646 static BOOL
CRYPT_EncodeValueWithType(DWORD dwCertEncodingType
,
647 const struct X500TokenW
*value
, PCERT_NAME_BLOB output
, DWORD type
,
650 CERT_NAME_VALUE nameValue
= { type
, { 0, NULL
} };
653 if (value
->end
> value
->start
)
655 nameValue
.Value
.pbData
= CryptMemAlloc((value
->end
- value
->start
) *
657 if (!nameValue
.Value
.pbData
)
659 SetLastError(ERROR_OUTOFMEMORY
);
665 if (value
->end
> value
->start
)
668 LPWSTR ptr
= (LPWSTR
)nameValue
.Value
.pbData
;
670 for (i
= 0; i
< value
->end
- value
->start
; i
++)
672 *ptr
++ = value
->start
[i
];
673 if (value
->start
[i
] == '"')
676 nameValue
.Value
.cbData
= (LPBYTE
)ptr
- nameValue
.Value
.pbData
;
678 ret
= CryptEncodeObjectEx(dwCertEncodingType
, X509_UNICODE_NAME_VALUE
,
679 &nameValue
, CRYPT_ENCODE_ALLOC_FLAG
, NULL
, &output
->pbData
,
681 if (!ret
&& ppszError
)
683 if (type
== CERT_RDN_NUMERIC_STRING
&&
684 GetLastError() == CRYPT_E_INVALID_NUMERIC_STRING
)
685 *ppszError
= value
->start
+ output
->cbData
;
686 else if (type
== CERT_RDN_PRINTABLE_STRING
&&
687 GetLastError() == CRYPT_E_INVALID_PRINTABLE_STRING
)
688 *ppszError
= value
->start
+ output
->cbData
;
689 else if (type
== CERT_RDN_IA5_STRING
&&
690 GetLastError() == CRYPT_E_INVALID_IA5_STRING
)
691 *ppszError
= value
->start
+ output
->cbData
;
693 CryptMemFree(nameValue
.Value
.pbData
);
698 static BOOL
CRYPT_EncodeValue(DWORD dwCertEncodingType
,
699 const struct X500TokenW
*value
, PCERT_NAME_BLOB output
, const DWORD
*types
,
706 for (i
= 0; !ret
&& types
[i
]; i
++)
707 ret
= CRYPT_EncodeValueWithType(dwCertEncodingType
, value
, output
,
708 types
[i
], ppszError
);
712 static BOOL
CRYPT_ValueToRDN(DWORD dwCertEncodingType
, PCERT_NAME_INFO info
,
713 PCCRYPT_OID_INFO keyOID
, struct X500TokenW
*value
, LPCWSTR
*ppszError
)
717 TRACE("OID %s, value %s\n", debugstr_a(keyOID
->pszOID
),
718 debugstr_wn(value
->start
, value
->end
- value
->start
));
721 info
->rgRDN
= CryptMemAlloc(sizeof(CERT_RDN
));
723 info
->rgRDN
= CryptMemRealloc(info
->rgRDN
,
724 (info
->cRDN
+ 1) * sizeof(CERT_RDN
));
727 /* FIXME: support multiple RDN attrs */
728 info
->rgRDN
[info
->cRDN
].rgRDNAttr
=
729 CryptMemAlloc(sizeof(CERT_RDN_ATTR
));
730 if (info
->rgRDN
[info
->cRDN
].rgRDNAttr
)
732 static const DWORD defaultTypes
[] = { CERT_RDN_PRINTABLE_STRING
,
733 CERT_RDN_BMP_STRING
, 0 };
736 info
->rgRDN
[info
->cRDN
].cRDNAttr
= 1;
737 info
->rgRDN
[info
->cRDN
].rgRDNAttr
[0].pszObjId
=
738 (LPSTR
)keyOID
->pszOID
;
739 info
->rgRDN
[info
->cRDN
].rgRDNAttr
[0].dwValueType
=
740 CERT_RDN_ENCODED_BLOB
;
741 if (keyOID
->ExtraInfo
.cbData
)
742 types
= (const DWORD
*)keyOID
->ExtraInfo
.pbData
;
744 types
= defaultTypes
;
746 /* Remove surrounding quotes */
747 if (value
->start
[0] == '"')
752 ret
= CRYPT_EncodeValue(dwCertEncodingType
, value
,
753 &info
->rgRDN
[info
->cRDN
].rgRDNAttr
[0].Value
, types
, ppszError
);
756 SetLastError(ERROR_OUTOFMEMORY
);
760 SetLastError(ERROR_OUTOFMEMORY
);
764 BOOL WINAPI
CertStrToNameW(DWORD dwCertEncodingType
, LPCWSTR pszX500
,
765 DWORD dwStrType
, void *pvReserved
, BYTE
*pbEncoded
, DWORD
*pcbEncoded
,
768 CERT_NAME_INFO info
= { 0, NULL
};
770 struct KeynameKeeper keeper
;
774 TRACE("(%08x, %s, %08x, %p, %p, %p, %p)\n", dwCertEncodingType
,
775 debugstr_w(pszX500
), dwStrType
, pvReserved
, pbEncoded
, pcbEncoded
,
778 CRYPT_InitializeKeynameKeeper(&keeper
);
780 while (str
&& *str
&& ret
)
782 struct X500TokenW token
;
784 ret
= CRYPT_GetNextKeyW(str
, &token
, ppszError
);
785 if (ret
&& token
.start
)
787 PCCRYPT_OID_INFO keyOID
;
789 CRYPT_KeynameKeeperFromTokenW(&keeper
, &token
);
790 keyOID
= CryptFindOIDInfo(CRYPT_OID_INFO_NAME_KEY
, keeper
.keyName
,
791 CRYPT_RDN_ATTR_OID_GROUP_ID
);
795 *ppszError
= token
.start
;
796 SetLastError(CRYPT_E_INVALID_X500_STRING
);
802 while (isspace(*str
))
808 SetLastError(CRYPT_E_INVALID_X500_STRING
);
813 static const WCHAR commaSep
[] = { ',',0 };
814 static const WCHAR semiSep
[] = { ';',0 };
815 static const WCHAR crlfSep
[] = { '\r','\n',0 };
816 static const WCHAR allSeps
[] = { ',',';','\r','\n',0 };
820 if (dwStrType
& CERT_NAME_STR_COMMA_FLAG
)
822 else if (dwStrType
& CERT_NAME_STR_SEMICOLON_FLAG
)
824 else if (dwStrType
& CERT_NAME_STR_CRLF_FLAG
)
828 ret
= CRYPT_GetNextValueW(str
, dwStrType
, sep
, &token
,
833 ret
= CRYPT_ValueToRDN(dwCertEncodingType
, &info
,
834 keyOID
, &token
, ppszError
);
840 CRYPT_FreeKeynameKeeper(&keeper
);
845 ret
= CryptEncodeObjectEx(dwCertEncodingType
, X509_NAME
, &info
,
846 0, NULL
, pbEncoded
, pcbEncoded
);
848 for (i
= 0; i
< info
.cRDN
; i
++)
852 for (j
= 0; j
< info
.rgRDN
[i
].cRDNAttr
; j
++)
853 LocalFree(info
.rgRDN
[i
].rgRDNAttr
[j
].Value
.pbData
);
854 CryptMemFree(info
.rgRDN
[i
].rgRDNAttr
);
856 CryptMemFree(info
.rgRDN
);
860 DWORD WINAPI
CertGetNameStringA(PCCERT_CONTEXT pCertContext
, DWORD dwType
,
861 DWORD dwFlags
, void *pvTypePara
, LPSTR pszNameString
, DWORD cchNameString
)
865 TRACE("(%p, %d, %08x, %p, %p, %d)\n", pCertContext
, dwType
, dwFlags
,
866 pvTypePara
, pszNameString
, cchNameString
);
873 nameLen
= CertGetNameStringW(pCertContext
, dwType
, dwFlags
, pvTypePara
,
875 wideName
= CryptMemAlloc(nameLen
* sizeof(WCHAR
));
878 CertGetNameStringW(pCertContext
, dwType
, dwFlags
, pvTypePara
,
880 nameLen
= WideCharToMultiByte(CP_ACP
, 0, wideName
, nameLen
,
881 pszNameString
, cchNameString
, NULL
, NULL
);
882 if (nameLen
<= cchNameString
)
886 pszNameString
[cchNameString
- 1] = '\0';
889 CryptMemFree(wideName
);
893 *pszNameString
= '\0';
898 ret
= CertGetNameStringW(pCertContext
, dwType
, dwFlags
, pvTypePara
,
903 DWORD WINAPI
CertGetNameStringW(PCCERT_CONTEXT pCertContext
, DWORD dwType
,
904 DWORD dwFlags
, void *pvTypePara
, LPWSTR pszNameString
, DWORD cchNameString
)
907 PCERT_NAME_BLOB name
;
910 TRACE("(%p, %d, %08x, %p, %p, %d)\n", pCertContext
, dwType
,
911 dwFlags
, pvTypePara
, pszNameString
, cchNameString
);
913 if (dwFlags
& CERT_NAME_ISSUER_FLAG
)
915 name
= &pCertContext
->pCertInfo
->Issuer
;
916 altNameOID
= szOID_ISSUER_ALT_NAME
;
920 name
= &pCertContext
->pCertInfo
->Subject
;
921 altNameOID
= szOID_SUBJECT_ALT_NAME
;
926 case CERT_NAME_SIMPLE_DISPLAY_TYPE
:
928 static const LPCSTR simpleAttributeOIDs
[] = { szOID_COMMON_NAME
,
929 szOID_ORGANIZATIONAL_UNIT_NAME
, szOID_ORGANIZATION_NAME
,
930 szOID_RSA_emailAddr
};
931 CERT_NAME_INFO
*info
= NULL
;
932 PCERT_RDN_ATTR nameAttr
= NULL
;
935 if (CryptDecodeObjectEx(pCertContext
->dwCertEncodingType
, X509_NAME
,
936 name
->pbData
, name
->cbData
, CRYPT_DECODE_ALLOC_FLAG
, NULL
, &info
,
939 for (i
= 0; !nameAttr
&& i
< sizeof(simpleAttributeOIDs
) /
940 sizeof(simpleAttributeOIDs
[0]); i
++)
941 nameAttr
= CertFindRDNAttr(simpleAttributeOIDs
[i
], info
);
945 PCERT_EXTENSION ext
= CertFindExtension(altNameOID
,
946 pCertContext
->pCertInfo
->cExtension
,
947 pCertContext
->pCertInfo
->rgExtension
);
951 for (i
= 0; !nameAttr
&& i
< sizeof(simpleAttributeOIDs
) /
952 sizeof(simpleAttributeOIDs
[0]); i
++)
953 nameAttr
= CertFindRDNAttr(simpleAttributeOIDs
[i
], info
);
956 /* FIXME: gotta then look for a rfc822Name choice in ext.
957 * Failing that, look for the first attribute.
959 FIXME("CERT_NAME_SIMPLE_DISPLAY_TYPE: stub\n");
964 ret
= CertRDNValueToStrW(nameAttr
->dwValueType
, &nameAttr
->Value
,
965 pszNameString
, cchNameString
);
972 case CERT_NAME_FRIENDLY_DISPLAY_TYPE
:
974 DWORD cch
= cchNameString
;
976 if (CertGetCertificateContextProperty(pCertContext
,
977 CERT_FRIENDLY_NAME_PROP_ID
, pszNameString
, &cch
))
980 ret
= CertGetNameStringW(pCertContext
,
981 CERT_NAME_SIMPLE_DISPLAY_TYPE
, dwFlags
, pvTypePara
, pszNameString
,
986 FIXME("unimplemented for type %d\n", dwType
);