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
);
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(lstrlenA(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(lstrlenW(prefix
), csz
);
332 memcpy(psz
, prefix
, chars
* sizeof(WCHAR
));
333 *(psz
+ chars
) = '=';
337 chars
= lstrlenW(prefix
) + 1;
341 DWORD WINAPI
CertNameToStrW(DWORD dwCertEncodingType
, PCERT_NAME_BLOB pName
,
342 DWORD dwStrType
, LPWSTR psz
, DWORD csz
)
344 static const DWORD unsupportedFlags
= CERT_NAME_STR_NO_QUOTING_FLAG
|
345 CERT_NAME_STR_ENABLE_T61_UNICODE_FLAG
;
346 static const WCHAR commaSep
[] = { ',',' ',0 };
347 static const WCHAR semiSep
[] = { ';',' ',0 };
348 static const WCHAR crlfSep
[] = { '\r','\n',0 };
349 static const WCHAR plusSep
[] = { ' ','+',' ',0 };
350 static const WCHAR spaceSep
[] = { ' ',0 };
351 DWORD ret
= 0, bytes
= 0;
353 CERT_NAME_INFO
*info
;
355 TRACE("(%d, %p, %08x, %p, %d)\n", dwCertEncodingType
, pName
, dwStrType
,
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
;
407 /* - 1 is needed to account for the NULL terminator. */
408 chars
= CRYPT_AddPrefixW(prefixW
,
409 psz
? psz
+ ret
: NULL
, psz
? csz
- ret
- 1 : 0);
414 /* - 1 is needed to account for the NULL terminator. */
415 chars
= CRYPT_AddPrefixAToW(prefixA
,
416 psz
? psz
+ ret
: NULL
, psz
? csz
- ret
- 1 : 0);
419 /* FIXME: handle quoting */
420 chars
= CertRDNValueToStrW(
421 rdn
->rgRDNAttr
[j
].dwValueType
,
422 &rdn
->rgRDNAttr
[j
].Value
, psz
? psz
+ ret
: NULL
,
423 psz
? csz
- ret
: 0);
426 if (j
< rdn
->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
));
451 TRACE("Returning %s\n", debugstr_w(psz
));
455 BOOL WINAPI
CertStrToNameA(DWORD dwCertEncodingType
, LPCSTR pszX500
,
456 DWORD dwStrType
, void *pvReserved
, BYTE
*pbEncoded
, DWORD
*pcbEncoded
,
462 TRACE("(%08x, %s, %08x, %p, %p, %p, %p)\n", dwCertEncodingType
,
463 debugstr_a(pszX500
), dwStrType
, pvReserved
, pbEncoded
, pcbEncoded
,
466 len
= MultiByteToWideChar(CP_ACP
, 0, pszX500
, -1, NULL
, 0);
469 LPWSTR x500
, errorStr
;
471 if ((x500
= CryptMemAlloc(len
* sizeof(WCHAR
))))
473 MultiByteToWideChar(CP_ACP
, 0, pszX500
, -1, x500
, len
);
474 ret
= CertStrToNameW(dwCertEncodingType
, x500
, dwStrType
,
475 pvReserved
, pbEncoded
, pcbEncoded
,
476 ppszError
? (LPCWSTR
*)&errorStr
: NULL
);
483 *ppszError
= pszX500
;
484 for (i
= 0; i
< errorStr
- x500
; i
++)
485 *ppszError
= CharNextA(*ppszError
);
494 SetLastError(ERROR_OUTOFMEMORY
);
500 SetLastError(CRYPT_E_INVALID_X500_STRING
);
502 *ppszError
= pszX500
;
510 WCHAR buf
[10]; /* big enough for L"GivenName" */
511 LPWSTR keyName
; /* usually = buf, but may be allocated */
515 static void CRYPT_InitializeKeynameKeeper(struct KeynameKeeper
*keeper
)
517 keeper
->keyName
= keeper
->buf
;
518 keeper
->keyLen
= sizeof(keeper
->buf
) / sizeof(keeper
->buf
[0]);
521 static void CRYPT_FreeKeynameKeeper(struct KeynameKeeper
*keeper
)
523 if (keeper
->keyName
!= keeper
->buf
)
524 CryptMemFree(keeper
->keyName
);
533 static void CRYPT_KeynameKeeperFromTokenW(struct KeynameKeeper
*keeper
,
534 const struct X500TokenW
*key
)
536 DWORD len
= key
->end
- key
->start
;
538 if (len
> keeper
->keyLen
)
540 if (keeper
->keyName
== keeper
->buf
)
541 keeper
->keyName
= CryptMemAlloc(len
* sizeof(WCHAR
));
543 keeper
->keyName
= CryptMemRealloc(keeper
->keyName
,
544 len
* sizeof(WCHAR
));
545 keeper
->keyLen
= len
;
547 memcpy(keeper
->keyName
, key
->start
, (key
->end
- key
->start
) *
549 keeper
->keyName
[len
] = '\0';
550 TRACE("Keyname is %s\n", debugstr_w(keeper
->keyName
));
553 static BOOL
CRYPT_GetNextKeyW(LPCWSTR str
, struct X500TokenW
*token
,
558 while (*str
&& isspaceW(*str
))
563 while (*str
&& *str
!= '=' && !isspaceW(*str
))
565 if (*str
&& (*str
== '=' || isspaceW(*str
)))
569 TRACE("missing equals char at %s\n", debugstr_w(token
->start
));
571 *ppszError
= token
->start
;
572 SetLastError(CRYPT_E_INVALID_X500_STRING
);
581 /* Assumes separators are characters in the 0-255 range */
582 static BOOL
CRYPT_GetNextValueW(LPCWSTR str
, DWORD dwFlags
, LPCWSTR separators
,
583 struct X500TokenW
*token
, LPCWSTR
*ppszError
)
587 TRACE("(%s, %s, %p, %p)\n", debugstr_w(str
), debugstr_w(separators
), token
,
590 while (*str
&& isspaceW(*str
))
595 if (!(dwFlags
& CERT_NAME_STR_NO_QUOTING_FLAG
) && *str
== '"')
599 while (!token
->end
&& ret
)
601 while (*str
&& *str
!= '"')
605 if (*(str
+ 1) != '"')
606 token
->end
= str
+ 1;
612 TRACE("unterminated quote at %s\n", debugstr_w(str
));
615 SetLastError(CRYPT_E_INVALID_X500_STRING
);
622 WCHAR map
[256] = { 0 };
625 map
[*separators
++] = 1;
626 while (*str
&& (*str
>= 0xff || !map
[*str
]))
633 TRACE("missing value at %s\n", debugstr_w(str
));
636 SetLastError(CRYPT_E_INVALID_X500_STRING
);
642 /* Encodes the string represented by value as the string type type into the
643 * CERT_NAME_BLOB output. If there is an error and ppszError is not NULL,
644 * *ppszError is set to the first failing character. If there is no error,
645 * output's pbData must be freed with LocalFree.
647 static BOOL
CRYPT_EncodeValueWithType(DWORD dwCertEncodingType
,
648 const struct X500TokenW
*value
, PCERT_NAME_BLOB output
, DWORD type
,
651 CERT_NAME_VALUE nameValue
= { type
, { 0, NULL
} };
654 if (value
->end
> value
->start
)
656 nameValue
.Value
.pbData
= CryptMemAlloc((value
->end
- value
->start
) *
658 if (!nameValue
.Value
.pbData
)
660 SetLastError(ERROR_OUTOFMEMORY
);
666 if (value
->end
> value
->start
)
669 LPWSTR ptr
= (LPWSTR
)nameValue
.Value
.pbData
;
671 for (i
= 0; i
< value
->end
- value
->start
; i
++)
673 *ptr
++ = value
->start
[i
];
674 if (value
->start
[i
] == '"')
677 nameValue
.Value
.cbData
= (LPBYTE
)ptr
- nameValue
.Value
.pbData
;
679 ret
= CryptEncodeObjectEx(dwCertEncodingType
, X509_UNICODE_NAME_VALUE
,
680 &nameValue
, CRYPT_ENCODE_ALLOC_FLAG
, NULL
, &output
->pbData
,
682 if (!ret
&& ppszError
)
684 if (type
== CERT_RDN_NUMERIC_STRING
&&
685 GetLastError() == CRYPT_E_INVALID_NUMERIC_STRING
)
686 *ppszError
= value
->start
+ output
->cbData
;
687 else if (type
== CERT_RDN_PRINTABLE_STRING
&&
688 GetLastError() == CRYPT_E_INVALID_PRINTABLE_STRING
)
689 *ppszError
= value
->start
+ output
->cbData
;
690 else if (type
== CERT_RDN_IA5_STRING
&&
691 GetLastError() == CRYPT_E_INVALID_IA5_STRING
)
692 *ppszError
= value
->start
+ output
->cbData
;
694 CryptMemFree(nameValue
.Value
.pbData
);
699 static BOOL
CRYPT_EncodeValue(DWORD dwCertEncodingType
,
700 const struct X500TokenW
*value
, PCERT_NAME_BLOB output
, const DWORD
*types
,
707 for (i
= 0; !ret
&& types
[i
]; i
++)
708 ret
= CRYPT_EncodeValueWithType(dwCertEncodingType
, value
, output
,
709 types
[i
], ppszError
);
713 static BOOL
CRYPT_ValueToRDN(DWORD dwCertEncodingType
, PCERT_NAME_INFO info
,
714 PCCRYPT_OID_INFO keyOID
, struct X500TokenW
*value
, LPCWSTR
*ppszError
)
718 TRACE("OID %s, value %s\n", debugstr_a(keyOID
->pszOID
),
719 debugstr_wn(value
->start
, value
->end
- value
->start
));
722 info
->rgRDN
= CryptMemAlloc(sizeof(CERT_RDN
));
724 info
->rgRDN
= CryptMemRealloc(info
->rgRDN
,
725 (info
->cRDN
+ 1) * sizeof(CERT_RDN
));
728 /* FIXME: support multiple RDN attrs */
729 info
->rgRDN
[info
->cRDN
].rgRDNAttr
=
730 CryptMemAlloc(sizeof(CERT_RDN_ATTR
));
731 if (info
->rgRDN
[info
->cRDN
].rgRDNAttr
)
733 static const DWORD defaultTypes
[] = { CERT_RDN_PRINTABLE_STRING
,
734 CERT_RDN_BMP_STRING
, 0 };
737 info
->rgRDN
[info
->cRDN
].cRDNAttr
= 1;
738 info
->rgRDN
[info
->cRDN
].rgRDNAttr
[0].pszObjId
=
739 (LPSTR
)keyOID
->pszOID
;
740 info
->rgRDN
[info
->cRDN
].rgRDNAttr
[0].dwValueType
=
741 CERT_RDN_ENCODED_BLOB
;
742 if (keyOID
->ExtraInfo
.cbData
)
743 types
= (const DWORD
*)keyOID
->ExtraInfo
.pbData
;
745 types
= defaultTypes
;
747 /* Remove surrounding quotes */
748 if (value
->start
[0] == '"')
753 ret
= CRYPT_EncodeValue(dwCertEncodingType
, value
,
754 &info
->rgRDN
[info
->cRDN
].rgRDNAttr
[0].Value
, types
, ppszError
);
757 SetLastError(ERROR_OUTOFMEMORY
);
761 SetLastError(ERROR_OUTOFMEMORY
);
765 BOOL WINAPI
CertStrToNameW(DWORD dwCertEncodingType
, LPCWSTR pszX500
,
766 DWORD dwStrType
, void *pvReserved
, BYTE
*pbEncoded
, DWORD
*pcbEncoded
,
769 CERT_NAME_INFO info
= { 0, NULL
};
771 struct KeynameKeeper keeper
;
775 TRACE("(%08x, %s, %08x, %p, %p, %p, %p)\n", dwCertEncodingType
,
776 debugstr_w(pszX500
), dwStrType
, pvReserved
, pbEncoded
, pcbEncoded
,
779 CRYPT_InitializeKeynameKeeper(&keeper
);
781 while (str
&& *str
&& ret
)
783 struct X500TokenW token
;
785 ret
= CRYPT_GetNextKeyW(str
, &token
, ppszError
);
786 if (ret
&& token
.start
)
788 PCCRYPT_OID_INFO keyOID
;
790 CRYPT_KeynameKeeperFromTokenW(&keeper
, &token
);
791 keyOID
= CryptFindOIDInfo(CRYPT_OID_INFO_NAME_KEY
, keeper
.keyName
,
792 CRYPT_RDN_ATTR_OID_GROUP_ID
);
796 *ppszError
= token
.start
;
797 SetLastError(CRYPT_E_INVALID_X500_STRING
);
803 while (isspace(*str
))
809 SetLastError(CRYPT_E_INVALID_X500_STRING
);
814 static const WCHAR commaSep
[] = { ',',0 };
815 static const WCHAR semiSep
[] = { ';',0 };
816 static const WCHAR crlfSep
[] = { '\r','\n',0 };
817 static const WCHAR allSeps
[] = { ',',';','\r','\n',0 };
821 if (dwStrType
& CERT_NAME_STR_COMMA_FLAG
)
823 else if (dwStrType
& CERT_NAME_STR_SEMICOLON_FLAG
)
825 else if (dwStrType
& CERT_NAME_STR_CRLF_FLAG
)
829 ret
= CRYPT_GetNextValueW(str
, dwStrType
, sep
, &token
,
834 ret
= CRYPT_ValueToRDN(dwCertEncodingType
, &info
,
835 keyOID
, &token
, ppszError
);
841 CRYPT_FreeKeynameKeeper(&keeper
);
846 ret
= CryptEncodeObjectEx(dwCertEncodingType
, X509_NAME
, &info
,
847 0, NULL
, pbEncoded
, pcbEncoded
);
849 for (i
= 0; i
< info
.cRDN
; i
++)
853 for (j
= 0; j
< info
.rgRDN
[i
].cRDNAttr
; j
++)
854 LocalFree(info
.rgRDN
[i
].rgRDNAttr
[j
].Value
.pbData
);
855 CryptMemFree(info
.rgRDN
[i
].rgRDNAttr
);
857 CryptMemFree(info
.rgRDN
);
861 DWORD WINAPI
CertGetNameStringA(PCCERT_CONTEXT pCertContext
, DWORD dwType
,
862 DWORD dwFlags
, void *pvTypePara
, LPSTR pszNameString
, DWORD cchNameString
)
866 TRACE("(%p, %d, %08x, %p, %p, %d)\n", pCertContext
, dwType
, dwFlags
,
867 pvTypePara
, pszNameString
, cchNameString
);
874 nameLen
= CertGetNameStringW(pCertContext
, dwType
, dwFlags
, pvTypePara
,
876 wideName
= CryptMemAlloc(nameLen
* sizeof(WCHAR
));
879 CertGetNameStringW(pCertContext
, dwType
, dwFlags
, pvTypePara
,
881 nameLen
= WideCharToMultiByte(CP_ACP
, 0, wideName
, nameLen
,
882 pszNameString
, cchNameString
, NULL
, NULL
);
883 if (nameLen
<= cchNameString
)
887 pszNameString
[cchNameString
- 1] = '\0';
890 CryptMemFree(wideName
);
894 *pszNameString
= '\0';
899 ret
= CertGetNameStringW(pCertContext
, dwType
, dwFlags
, pvTypePara
,
904 DWORD WINAPI
CertGetNameStringW(PCCERT_CONTEXT pCertContext
, DWORD dwType
,
905 DWORD dwFlags
, void *pvTypePara
, LPWSTR pszNameString
, DWORD cchNameString
)
908 PCERT_NAME_BLOB name
;
911 TRACE("(%p, %d, %08x, %p, %p, %d)\n", pCertContext
, dwType
,
912 dwFlags
, pvTypePara
, pszNameString
, cchNameString
);
914 if (dwFlags
& CERT_NAME_ISSUER_FLAG
)
916 name
= &pCertContext
->pCertInfo
->Issuer
;
917 altNameOID
= szOID_ISSUER_ALT_NAME
;
921 name
= &pCertContext
->pCertInfo
->Subject
;
922 altNameOID
= szOID_SUBJECT_ALT_NAME
;
927 case CERT_NAME_SIMPLE_DISPLAY_TYPE
:
929 static const LPCSTR simpleAttributeOIDs
[] = { szOID_COMMON_NAME
,
930 szOID_ORGANIZATIONAL_UNIT_NAME
, szOID_ORGANIZATION_NAME
,
931 szOID_RSA_emailAddr
};
932 CERT_NAME_INFO
*info
= NULL
;
933 PCERT_RDN_ATTR nameAttr
= NULL
;
936 if (CryptDecodeObjectEx(pCertContext
->dwCertEncodingType
, X509_NAME
,
937 name
->pbData
, name
->cbData
, CRYPT_DECODE_ALLOC_FLAG
, NULL
, &info
,
940 for (i
= 0; !nameAttr
&& i
< sizeof(simpleAttributeOIDs
) /
941 sizeof(simpleAttributeOIDs
[0]); i
++)
942 nameAttr
= CertFindRDNAttr(simpleAttributeOIDs
[i
], info
);
946 PCERT_EXTENSION ext
= CertFindExtension(altNameOID
,
947 pCertContext
->pCertInfo
->cExtension
,
948 pCertContext
->pCertInfo
->rgExtension
);
952 for (i
= 0; !nameAttr
&& i
< sizeof(simpleAttributeOIDs
) /
953 sizeof(simpleAttributeOIDs
[0]); i
++)
954 nameAttr
= CertFindRDNAttr(simpleAttributeOIDs
[i
], info
);
957 /* FIXME: gotta then look for a rfc822Name choice in ext.
958 * Failing that, look for the first attribute.
960 FIXME("CERT_NAME_SIMPLE_DISPLAY_TYPE: stub\n");
965 ret
= CertRDNValueToStrW(nameAttr
->dwValueType
, &nameAttr
->Value
,
966 pszNameString
, cchNameString
);
973 case CERT_NAME_FRIENDLY_DISPLAY_TYPE
:
975 DWORD cch
= cchNameString
;
977 if (CertGetCertificateContextProperty(pCertContext
,
978 CERT_FRIENDLY_NAME_PROP_ID
, pszNameString
, &cch
))
981 ret
= CertGetNameStringW(pCertContext
,
982 CERT_NAME_SIMPLE_DISPLAY_TYPE
, dwFlags
, pvTypePara
, pszNameString
,
987 FIXME("unimplemented for type %d\n", dwType
);