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
);
63 FIXME("string type %d unimplemented\n", dwValueType
);
73 TRACE("returning %d (%s)\n", ret
, debugstr_a(psz
));
77 DWORD WINAPI
CertRDNValueToStrW(DWORD dwValueType
, PCERT_RDN_VALUE_BLOB pValue
,
78 LPWSTR psz
, DWORD csz
)
82 TRACE("(%d, %p, %p, %d)\n", dwValueType
, pValue
, psz
, csz
);
86 case CERT_RDN_ANY_TYPE
:
88 case CERT_RDN_NUMERIC_STRING
:
89 case CERT_RDN_PRINTABLE_STRING
:
90 case CERT_RDN_TELETEX_STRING
:
91 case CERT_RDN_VIDEOTEX_STRING
:
92 case CERT_RDN_IA5_STRING
:
93 case CERT_RDN_GRAPHIC_STRING
:
94 case CERT_RDN_VISIBLE_STRING
:
95 case CERT_RDN_GENERAL_STRING
:
100 DWORD chars
= min(pValue
->cbData
, csz
- 1);
106 for (i
= 0; i
< chars
; i
++)
107 psz
[i
] = pValue
->pbData
[i
];
114 FIXME("string type %d unimplemented\n", dwValueType
);
124 TRACE("returning %d (%s)\n", ret
, debugstr_w(psz
));
128 /* Adds the prefix prefix to the string pointed to by psz, followed by the
129 * character '='. Copies no more than csz characters. Returns the number of
130 * characters copied. If psz is NULL, returns the number of characters that
133 static DWORD
CRYPT_AddPrefixA(LPCSTR prefix
, LPSTR psz
, DWORD csz
)
137 TRACE("(%s, %p, %d)\n", debugstr_a(prefix
), psz
, csz
);
141 chars
= min(lstrlenA(prefix
), csz
);
142 memcpy(psz
, prefix
, chars
);
144 *(psz
+ chars
) = '=';
149 chars
= lstrlenA(prefix
) + 1;
153 DWORD WINAPI
CertNameToStrA(DWORD dwCertEncodingType
, PCERT_NAME_BLOB pName
,
154 DWORD dwStrType
, LPSTR psz
, DWORD csz
)
156 static const DWORD unsupportedFlags
= CERT_NAME_STR_NO_QUOTING_FLAG
|
157 CERT_NAME_STR_REVERSE_FLAG
| CERT_NAME_STR_ENABLE_T61_UNICODE_FLAG
;
158 static const char commaSep
[] = ", ";
159 static const char semiSep
[] = "; ";
160 static const char crlfSep
[] = "\r\n";
161 static const char plusSep
[] = " + ";
162 static const char spaceSep
[] = " ";
163 DWORD ret
= 0, bytes
= 0;
165 CERT_NAME_INFO
*info
;
167 TRACE("(%d, %p, %08x, %p, %d)\n", dwCertEncodingType
, pName
, dwStrType
,
169 if (dwStrType
& unsupportedFlags
)
170 FIXME("unsupported flags: %08x\n", dwStrType
& unsupportedFlags
);
172 bRet
= CryptDecodeObjectEx(dwCertEncodingType
, X509_NAME
, pName
->pbData
,
173 pName
->cbData
, CRYPT_DECODE_ALLOC_FLAG
, NULL
, &info
, &bytes
);
176 DWORD i
, j
, sepLen
, rdnSepLen
;
179 if (dwStrType
& CERT_NAME_STR_SEMICOLON_FLAG
)
181 else if (dwStrType
& CERT_NAME_STR_CRLF_FLAG
)
185 sepLen
= strlen(sep
);
186 if (dwStrType
& CERT_NAME_STR_NO_PLUS_FLAG
)
190 rdnSepLen
= strlen(rdnSep
);
191 for (i
= 0; (!psz
|| ret
< csz
) && i
< info
->cRDN
; i
++)
193 for (j
= 0; (!psz
|| ret
< csz
) && j
< info
->rgRDN
[i
].cRDNAttr
; j
++)
196 char prefixBuf
[10]; /* big enough for GivenName */
197 LPCSTR prefix
= NULL
;
199 if ((dwStrType
& 0x000000ff) == CERT_OID_NAME_STR
)
200 prefix
= info
->rgRDN
[i
].rgRDNAttr
[j
].pszObjId
;
201 else if ((dwStrType
& 0x000000ff) == CERT_X500_NAME_STR
)
203 PCCRYPT_OID_INFO oidInfo
= CryptFindOIDInfo(
204 CRYPT_OID_INFO_OID_KEY
,
205 info
->rgRDN
[i
].rgRDNAttr
[j
].pszObjId
,
206 CRYPT_RDN_ATTR_OID_GROUP_ID
);
210 WideCharToMultiByte(CP_ACP
, 0, oidInfo
->pwszName
, -1,
211 prefixBuf
, sizeof(prefixBuf
), NULL
, NULL
);
215 prefix
= info
->rgRDN
[i
].rgRDNAttr
[j
].pszObjId
;
219 /* - 1 is needed to account for the NULL terminator. */
220 chars
= CRYPT_AddPrefixA(prefix
,
221 psz
? psz
+ ret
: NULL
, psz
? csz
- ret
- 1 : 0);
225 /* FIXME: handle quoting */
226 chars
= CertRDNValueToStrA(
227 info
->rgRDN
[i
].rgRDNAttr
[j
].dwValueType
,
228 &info
->rgRDN
[i
].rgRDNAttr
[j
].Value
, psz
? psz
+ ret
: NULL
,
229 psz
? csz
- ret
: 0);
232 if (j
< info
->rgRDN
[i
].cRDNAttr
- 1)
234 if (psz
&& ret
< csz
- rdnSepLen
- 1)
235 memcpy(psz
+ ret
, rdnSep
, rdnSepLen
);
239 if (i
< info
->cRDN
- 1)
241 if (psz
&& ret
< csz
- sepLen
- 1)
242 memcpy(psz
+ ret
, sep
, sepLen
);
256 TRACE("Returning %s\n", debugstr_a(psz
));
260 /* Adds the prefix prefix to the wide-character string pointed to by psz,
261 * followed by the character '='. Copies no more than csz characters. Returns
262 * the number of characters copied. If psz is NULL, returns the number of
263 * characters that would be copied.
264 * Assumes the characters in prefix are ASCII (not multibyte characters.)
266 static DWORD
CRYPT_AddPrefixAToW(LPCSTR prefix
, LPWSTR psz
, DWORD csz
)
270 TRACE("(%s, %p, %d)\n", debugstr_a(prefix
), psz
, csz
);
276 chars
= min(lstrlenA(prefix
), csz
);
277 for (i
= 0; i
< chars
; i
++)
278 *(psz
+ i
) = prefix
[i
];
280 *(psz
+ chars
) = '=';
285 chars
= lstrlenA(prefix
) + 1;
289 /* Adds the prefix prefix to the string pointed to by psz, followed by the
290 * character '='. Copies no more than csz characters. Returns the number of
291 * characters copied. If psz is NULL, returns the number of characters that
294 static DWORD
CRYPT_AddPrefixW(LPCWSTR prefix
, LPWSTR psz
, DWORD csz
)
298 TRACE("(%s, %p, %d)\n", debugstr_w(prefix
), psz
, csz
);
302 chars
= min(lstrlenW(prefix
), csz
);
303 memcpy(psz
, prefix
, chars
* sizeof(WCHAR
));
305 *(psz
+ chars
) = '=';
310 chars
= lstrlenW(prefix
) + 1;
314 DWORD WINAPI
CertNameToStrW(DWORD dwCertEncodingType
, PCERT_NAME_BLOB pName
,
315 DWORD dwStrType
, LPWSTR psz
, DWORD csz
)
317 static const DWORD unsupportedFlags
= CERT_NAME_STR_NO_QUOTING_FLAG
|
318 CERT_NAME_STR_REVERSE_FLAG
| CERT_NAME_STR_ENABLE_T61_UNICODE_FLAG
;
319 static const WCHAR commaSep
[] = { ',',' ',0 };
320 static const WCHAR semiSep
[] = { ';',' ',0 };
321 static const WCHAR crlfSep
[] = { '\r','\n',0 };
322 static const WCHAR plusSep
[] = { ' ','+',' ',0 };
323 static const WCHAR spaceSep
[] = { ' ',0 };
324 DWORD ret
= 0, bytes
= 0;
326 CERT_NAME_INFO
*info
;
328 TRACE("(%d, %p, %08x, %p, %d)\n", dwCertEncodingType
, pName
, dwStrType
,
330 if (dwStrType
& unsupportedFlags
)
331 FIXME("unsupported flags: %08x\n", dwStrType
& unsupportedFlags
);
333 bRet
= CryptDecodeObjectEx(dwCertEncodingType
, X509_NAME
, pName
->pbData
,
334 pName
->cbData
, CRYPT_DECODE_ALLOC_FLAG
, NULL
, &info
, &bytes
);
337 DWORD i
, j
, sepLen
, rdnSepLen
;
340 if (dwStrType
& CERT_NAME_STR_SEMICOLON_FLAG
)
342 else if (dwStrType
& CERT_NAME_STR_CRLF_FLAG
)
346 sepLen
= lstrlenW(sep
);
347 if (dwStrType
& CERT_NAME_STR_NO_PLUS_FLAG
)
351 rdnSepLen
= lstrlenW(rdnSep
);
352 for (i
= 0; (!psz
|| ret
< csz
) && i
< info
->cRDN
; i
++)
354 for (j
= 0; (!psz
|| ret
< csz
) && j
< info
->rgRDN
[i
].cRDNAttr
; j
++)
357 LPCSTR prefixA
= NULL
;
358 LPCWSTR prefixW
= NULL
;
360 if ((dwStrType
& 0x000000ff) == CERT_OID_NAME_STR
)
361 prefixA
= info
->rgRDN
[i
].rgRDNAttr
[j
].pszObjId
;
362 else if ((dwStrType
& 0x000000ff) == CERT_X500_NAME_STR
)
364 PCCRYPT_OID_INFO oidInfo
= CryptFindOIDInfo(
365 CRYPT_OID_INFO_OID_KEY
,
366 info
->rgRDN
[i
].rgRDNAttr
[j
].pszObjId
,
367 CRYPT_RDN_ATTR_OID_GROUP_ID
);
370 prefixW
= oidInfo
->pwszName
;
372 prefixA
= info
->rgRDN
[i
].rgRDNAttr
[j
].pszObjId
;
376 /* - 1 is needed to account for the NULL terminator. */
377 chars
= CRYPT_AddPrefixW(prefixW
,
378 psz
? psz
+ ret
: NULL
, psz
? csz
- ret
- 1 : 0);
384 /* - 1 is needed to account for the NULL terminator. */
385 chars
= CRYPT_AddPrefixAToW(prefixA
,
386 psz
? psz
+ ret
: NULL
, psz
? csz
- ret
- 1 : 0);
390 /* FIXME: handle quoting */
391 chars
= CertRDNValueToStrW(
392 info
->rgRDN
[i
].rgRDNAttr
[j
].dwValueType
,
393 &info
->rgRDN
[i
].rgRDNAttr
[j
].Value
, psz
? psz
+ ret
: NULL
,
394 psz
? csz
- ret
: 0);
397 if (j
< info
->rgRDN
[i
].cRDNAttr
- 1)
399 if (psz
&& ret
< csz
- rdnSepLen
- 1)
400 memcpy(psz
+ ret
, rdnSep
, rdnSepLen
* sizeof(WCHAR
));
404 if (i
< info
->cRDN
- 1)
406 if (psz
&& ret
< csz
- sepLen
- 1)
407 memcpy(psz
+ ret
, sep
, sepLen
* sizeof(WCHAR
));
421 TRACE("Returning %s\n", debugstr_w(psz
));
425 BOOL WINAPI
CertStrToNameA(DWORD dwCertEncodingType
, LPCSTR pszX500
,
426 DWORD dwStrType
, void *pvReserved
, BYTE
*pbEncoded
, DWORD
*pcbEncoded
,
429 LPWSTR x500
, errorStr
;
433 TRACE("(%08x, %s, %08x, %p, %p, %p, %p)\n", dwCertEncodingType
,
434 debugstr_a(pszX500
), dwStrType
, pvReserved
, pbEncoded
, pcbEncoded
,
437 len
= MultiByteToWideChar(CP_ACP
, 0, pszX500
, -1, NULL
, 0);
438 x500
= CryptMemAlloc(len
* sizeof(WCHAR
));
441 MultiByteToWideChar(CP_ACP
, 0, pszX500
, -1, x500
, len
);
442 ret
= CertStrToNameW(dwCertEncodingType
, x500
, dwStrType
, pvReserved
,
443 pbEncoded
, pcbEncoded
, ppszError
? (LPCWSTR
*)&errorStr
: NULL
);
448 *ppszError
= pszX500
;
449 for (i
= 0; i
< errorStr
- x500
; i
++)
450 *ppszError
= CharNextA(*ppszError
);
461 WCHAR buf
[10]; /* big enough for L"GivenName" */
462 LPWSTR keyName
; /* usually = buf, but may be allocated */
466 static void CRYPT_InitializeKeynameKeeper(struct KeynameKeeper
*keeper
)
468 keeper
->keyName
= keeper
->buf
;
469 keeper
->keyLen
= sizeof(keeper
->buf
) / sizeof(keeper
->buf
[0]);
472 static void CRYPT_FreeKeynameKeeper(struct KeynameKeeper
*keeper
)
474 if (keeper
->keyName
!= keeper
->buf
)
475 CryptMemFree(keeper
->keyName
);
484 static void CRYPT_KeynameKeeperFromTokenW(struct KeynameKeeper
*keeper
,
485 const struct X500TokenW
*key
)
487 DWORD len
= key
->end
- key
->start
;
489 if (len
> keeper
->keyLen
)
491 if (keeper
->keyName
== keeper
->buf
)
492 keeper
->keyName
= CryptMemAlloc(len
* sizeof(WCHAR
));
494 keeper
->keyName
= CryptMemRealloc(keeper
->keyName
,
495 len
* sizeof(WCHAR
));
496 keeper
->keyLen
= len
;
498 memcpy(keeper
->keyName
, key
->start
, (key
->end
- key
->start
) *
500 keeper
->keyName
[len
] = '\0';
501 TRACE("Keyname is %s\n", debugstr_w(keeper
->keyName
));
504 static DWORD
CRYPT_GetNextKeyW(LPCWSTR str
, struct X500TokenW
*token
,
507 DWORD ret
= ERROR_SUCCESS
;
509 while (*str
&& isspaceW(*str
))
514 while (*str
&& *str
!= '=' && !isspaceW(*str
))
516 if (*str
&& (*str
== '=' || isspaceW(*str
)))
520 TRACE("missing equals char at %s\n", debugstr_w(token
->start
));
522 *ppszError
= token
->start
;
523 ret
= CRYPT_E_INVALID_X500_STRING
;
531 /* Assumes separators are characters in the 0-255 range */
532 static DWORD
CRYPT_GetNextValueW(LPCWSTR str
, DWORD dwFlags
, LPCWSTR separators
,
533 struct X500TokenW
*token
, LPCWSTR
*ppszError
)
535 DWORD ret
= ERROR_SUCCESS
;
537 TRACE("(%s, %s, %p, %p)\n", debugstr_w(str
), debugstr_w(separators
), token
,
540 while (*str
&& isspaceW(*str
))
545 if (!(dwFlags
& CERT_NAME_STR_NO_QUOTING_FLAG
) && *str
== '"')
549 while (!token
->end
&& !ret
)
551 while (*str
&& *str
!= '"')
555 if (*(str
+ 1) != '"')
556 token
->end
= str
+ 1;
562 TRACE("unterminated quote at %s\n", debugstr_w(str
));
565 ret
= CRYPT_E_INVALID_X500_STRING
;
571 WCHAR map
[256] = { 0 };
574 map
[*separators
++] = 1;
575 while (*str
&& (*str
>= 0xff || !map
[*(const unsigned short *)str
]))
582 TRACE("missing value at %s\n", debugstr_w(str
));
585 ret
= CRYPT_E_INVALID_X500_STRING
;
590 /* Encodes the string represented by value as the string type type into the
591 * CERT_NAME_BLOB output. If there is an error and ppszError is not NULL,
592 * *ppszError is set to the first failing character. If there is no error,
593 * output's pbData must be freed with LocalFree.
595 static BOOL
CRYPT_EncodeValueWithType(DWORD dwCertEncodingType
,
596 const struct X500TokenW
*value
, PCERT_NAME_BLOB output
, DWORD type
,
599 CERT_NAME_VALUE nameValue
= { type
, { 0, NULL
} };
602 nameValue
.Value
.pbData
= CryptMemAlloc((value
->end
- value
->start
) *
604 if (nameValue
.Value
.pbData
)
607 LPWSTR ptr
= (LPWSTR
)nameValue
.Value
.pbData
;
609 for (i
= 0; i
< value
->end
- value
->start
; i
++)
611 *ptr
++ = value
->start
[i
];
612 if (value
->start
[i
] == '"')
615 nameValue
.Value
.cbData
= (LPBYTE
)ptr
- nameValue
.Value
.pbData
;
616 ret
= CryptEncodeObjectEx(dwCertEncodingType
, X509_UNICODE_NAME_VALUE
,
617 &nameValue
, CRYPT_ENCODE_ALLOC_FLAG
, NULL
, &output
->pbData
,
619 if (!ret
&& ppszError
)
621 if (type
== CERT_RDN_NUMERIC_STRING
&&
622 GetLastError() == CRYPT_E_INVALID_NUMERIC_STRING
)
623 *ppszError
= value
->start
+ output
->cbData
;
624 else if (type
== CERT_RDN_PRINTABLE_STRING
&&
625 GetLastError() == CRYPT_E_INVALID_PRINTABLE_STRING
)
626 *ppszError
= value
->start
+ output
->cbData
;
627 else if (type
== CERT_RDN_IA5_STRING
&&
628 GetLastError() == CRYPT_E_INVALID_IA5_STRING
)
629 *ppszError
= value
->start
+ output
->cbData
;
631 CryptMemFree(nameValue
.Value
.pbData
);
636 static BOOL
CRYPT_EncodeValue(DWORD dwCertEncodingType
,
637 const struct X500TokenW
*value
, PCERT_NAME_BLOB output
, const DWORD
*types
,
644 for (i
= 0; !ret
&& types
[i
]; i
++)
645 ret
= CRYPT_EncodeValueWithType(dwCertEncodingType
, value
, output
,
646 types
[i
], ppszError
);
650 static BOOL
CRYPT_ValueToRDN(DWORD dwCertEncodingType
, PCERT_NAME_INFO info
,
651 PCCRYPT_OID_INFO keyOID
, struct X500TokenW
*value
, LPCWSTR
*ppszError
)
655 TRACE("OID %s, value %s\n", debugstr_a(keyOID
->pszOID
),
656 debugstr_wn(value
->start
, value
->end
- value
->start
));
659 info
->rgRDN
= CryptMemAlloc(sizeof(CERT_RDN
));
661 info
->rgRDN
= CryptMemRealloc(info
->rgRDN
,
662 (info
->cRDN
+ 1) * sizeof(CERT_RDN
));
665 /* FIXME: support multiple RDN attrs */
666 info
->rgRDN
[info
->cRDN
].rgRDNAttr
=
667 CryptMemAlloc(sizeof(CERT_RDN_ATTR
));
668 if (info
->rgRDN
[info
->cRDN
].rgRDNAttr
)
670 static const DWORD defaultTypes
[] = { CERT_RDN_PRINTABLE_STRING
,
671 CERT_RDN_BMP_STRING
, 0 };
674 info
->rgRDN
[info
->cRDN
].cRDNAttr
= 1;
675 info
->rgRDN
[info
->cRDN
].rgRDNAttr
[0].pszObjId
=
676 (LPSTR
)keyOID
->pszOID
;
677 info
->rgRDN
[info
->cRDN
].rgRDNAttr
[0].dwValueType
=
678 CERT_RDN_ENCODED_BLOB
;
679 if (keyOID
->ExtraInfo
.cbData
)
680 types
= (const DWORD
*)keyOID
->ExtraInfo
.pbData
;
682 types
= defaultTypes
;
684 /* Remove surrounding quotes */
685 if (value
->start
[0] == '"')
690 ret
= CRYPT_EncodeValue(dwCertEncodingType
, value
,
691 &info
->rgRDN
[info
->cRDN
].rgRDNAttr
[0].Value
, types
, ppszError
);
699 BOOL WINAPI
CertStrToNameW(DWORD dwCertEncodingType
, LPCWSTR pszX500
,
700 DWORD dwStrType
, void *pvReserved
, BYTE
*pbEncoded
, DWORD
*pcbEncoded
,
703 CERT_NAME_INFO info
= { 0, NULL
};
705 struct KeynameKeeper keeper
;
706 DWORD i
, error
= ERROR_SUCCESS
;
709 TRACE("(%08x, %s, %08x, %p, %p, %p, %p)\n", dwCertEncodingType
,
710 debugstr_w(pszX500
), dwStrType
, pvReserved
, pbEncoded
, pcbEncoded
,
713 CRYPT_InitializeKeynameKeeper(&keeper
);
715 while (str
&& *str
&& !error
&& ret
)
717 struct X500TokenW token
;
719 error
= CRYPT_GetNextKeyW(str
, &token
, ppszError
);
720 if (!error
&& token
.start
)
722 PCCRYPT_OID_INFO keyOID
;
724 CRYPT_KeynameKeeperFromTokenW(&keeper
, &token
);
725 keyOID
= CryptFindOIDInfo(CRYPT_OID_INFO_NAME_KEY
, keeper
.keyName
,
726 CRYPT_RDN_ATTR_OID_GROUP_ID
);
730 *ppszError
= token
.start
;
731 error
= CRYPT_E_INVALID_X500_STRING
;
736 while (isspace(*str
))
742 error
= CRYPT_E_INVALID_X500_STRING
;
746 static const WCHAR commaSep
[] = { ',',0 };
747 static const WCHAR semiSep
[] = { ';',0 };
748 static const WCHAR crlfSep
[] = { '\r','\n',0 };
749 static const WCHAR allSeps
[] = { ',',';','\r','\n',0 };
753 if (dwStrType
& CERT_NAME_STR_COMMA_FLAG
)
755 else if (dwStrType
& CERT_NAME_STR_SEMICOLON_FLAG
)
757 else if (dwStrType
& CERT_NAME_STR_CRLF_FLAG
)
761 error
= CRYPT_GetNextValueW(str
, dwStrType
, sep
, &token
,
766 ret
= CRYPT_ValueToRDN(dwCertEncodingType
, &info
,
767 keyOID
, &token
, ppszError
);
773 CRYPT_FreeKeynameKeeper(&keeper
);
776 ret
= CryptEncodeObjectEx(dwCertEncodingType
, X509_NAME
, &info
,
777 0, NULL
, pbEncoded
, pcbEncoded
);
778 for (i
= 0; i
< info
.cRDN
; i
++)
782 for (j
= 0; j
< info
.rgRDN
[i
].cRDNAttr
; j
++)
783 LocalFree(info
.rgRDN
[i
].rgRDNAttr
[j
].Value
.pbData
);
784 CryptMemFree(info
.rgRDN
[i
].rgRDNAttr
);
786 CryptMemFree(info
.rgRDN
);
796 DWORD WINAPI
CertGetNameStringA(PCCERT_CONTEXT pCertContext
, DWORD dwType
,
797 DWORD dwFlags
, void *pvTypePara
, LPSTR pszNameString
, DWORD cchNameString
)
801 TRACE("(%p, %d, %08x, %p, %p, %d)\n", pCertContext
, dwType
, dwFlags
,
802 pvTypePara
, pszNameString
, cchNameString
);
809 nameLen
= CertGetNameStringW(pCertContext
, dwType
, dwFlags
, pvTypePara
,
811 wideName
= CryptMemAlloc(nameLen
* sizeof(WCHAR
));
814 CertGetNameStringW(pCertContext
, dwType
, dwFlags
, pvTypePara
,
816 nameLen
= WideCharToMultiByte(CP_ACP
, 0, wideName
, nameLen
,
817 pszNameString
, cchNameString
, NULL
, NULL
);
818 if (nameLen
<= cchNameString
)
822 pszNameString
[cchNameString
- 1] = '\0';
825 CryptMemFree(wideName
);
829 *pszNameString
= '\0';
834 ret
= CertGetNameStringW(pCertContext
, dwType
, dwFlags
, pvTypePara
,
839 DWORD WINAPI
CertGetNameStringW(PCCERT_CONTEXT pCertContext
, DWORD dwType
,
840 DWORD dwFlags
, void *pvTypePara
, LPWSTR pszNameString
, DWORD cchNameString
)
843 PCERT_NAME_BLOB name
;
846 TRACE("(%p, %d, %08x, %p, %p, %d)\n", pCertContext
, dwType
,
847 dwFlags
, pvTypePara
, pszNameString
, cchNameString
);
849 if (dwFlags
& CERT_NAME_ISSUER_FLAG
)
851 name
= &pCertContext
->pCertInfo
->Issuer
;
852 altNameOID
= szOID_ISSUER_ALT_NAME
;
856 name
= &pCertContext
->pCertInfo
->Subject
;
857 altNameOID
= szOID_SUBJECT_ALT_NAME
;
862 case CERT_NAME_SIMPLE_DISPLAY_TYPE
:
864 static const LPCSTR simpleAttributeOIDs
[] = { szOID_COMMON_NAME
,
865 szOID_ORGANIZATIONAL_UNIT_NAME
, szOID_ORGANIZATION_NAME
,
866 szOID_RSA_emailAddr
};
867 CERT_NAME_INFO
*info
= NULL
;
868 PCERT_RDN_ATTR nameAttr
= NULL
;
871 if (CryptDecodeObjectEx(pCertContext
->dwCertEncodingType
, X509_NAME
,
872 name
->pbData
, name
->cbData
, CRYPT_DECODE_ALLOC_FLAG
, NULL
, &info
,
875 for (i
= 0; !nameAttr
&& i
< sizeof(simpleAttributeOIDs
) /
876 sizeof(simpleAttributeOIDs
[0]); i
++)
877 nameAttr
= CertFindRDNAttr(simpleAttributeOIDs
[i
], info
);
881 PCERT_EXTENSION ext
= CertFindExtension(altNameOID
,
882 pCertContext
->pCertInfo
->cExtension
,
883 pCertContext
->pCertInfo
->rgExtension
);
887 for (i
= 0; !nameAttr
&& i
< sizeof(simpleAttributeOIDs
) /
888 sizeof(simpleAttributeOIDs
[0]); i
++)
889 nameAttr
= CertFindRDNAttr(simpleAttributeOIDs
[i
], info
);
892 /* FIXME: gotta then look for a rfc822Name choice in ext.
893 * Failing that, look for the first attribute.
895 FIXME("CERT_NAME_SIMPLE_DISPLAY_TYPE: stub\n");
900 ret
= CertRDNValueToStrW(nameAttr
->dwValueType
, &nameAttr
->Value
,
901 pszNameString
, cchNameString
);
908 case CERT_NAME_FRIENDLY_DISPLAY_TYPE
:
910 DWORD cch
= cchNameString
;
912 if (CertGetCertificateContextProperty(pCertContext
,
913 CERT_FRIENDLY_NAME_PROP_ID
, pszNameString
, &cch
))
916 ret
= CertGetNameStringW(pCertContext
,
917 CERT_NAME_SIMPLE_DISPLAY_TYPE
, dwFlags
, pvTypePara
, pszNameString
,
922 FIXME("unimplemented for type %d\n", dwType
);