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
19 #include "crypt32_private.h"
21 WINE_DEFAULT_DEBUG_CHANNEL(crypt
);
23 DWORD WINAPI
CertRDNValueToStrA(DWORD dwValueType
, PCERT_RDN_VALUE_BLOB pValue
,
28 TRACE("(%d, %p, %p, %d)\n", dwValueType
, pValue
, psz
, csz
);
32 case CERT_RDN_ANY_TYPE
:
34 case CERT_RDN_NUMERIC_STRING
:
35 case CERT_RDN_PRINTABLE_STRING
:
36 case CERT_RDN_TELETEX_STRING
:
37 case CERT_RDN_VIDEOTEX_STRING
:
38 case CERT_RDN_IA5_STRING
:
39 case CERT_RDN_GRAPHIC_STRING
:
40 case CERT_RDN_VISIBLE_STRING
:
41 case CERT_RDN_GENERAL_STRING
:
47 DWORD chars
= min(len
, csz
- 1);
51 memcpy(psz
, pValue
->pbData
, chars
);
57 case CERT_RDN_BMP_STRING
:
58 case CERT_RDN_UTF8_STRING
:
59 len
= WideCharToMultiByte(CP_ACP
, 0, (LPCWSTR
)pValue
->pbData
,
60 pValue
->cbData
/ sizeof(WCHAR
), NULL
, 0, NULL
, NULL
);
65 DWORD chars
= min(pValue
->cbData
/ sizeof(WCHAR
), csz
- 1);
69 ret
= WideCharToMultiByte(CP_ACP
, 0, (LPCWSTR
)pValue
->pbData
,
70 chars
, psz
, csz
- 1, NULL
, NULL
);
76 FIXME("string type %d unimplemented\n", dwValueType
);
86 TRACE("returning %d (%s)\n", ret
, debugstr_a(psz
));
90 DWORD WINAPI
CertRDNValueToStrW(DWORD dwValueType
, PCERT_RDN_VALUE_BLOB pValue
,
91 LPWSTR psz
, DWORD csz
)
93 DWORD ret
= 0, len
, i
, strLen
;
95 TRACE("(%d, %p, %p, %d)\n", dwValueType
, pValue
, psz
, csz
);
99 case CERT_RDN_ANY_TYPE
:
101 case CERT_RDN_NUMERIC_STRING
:
102 case CERT_RDN_PRINTABLE_STRING
:
103 case CERT_RDN_TELETEX_STRING
:
104 case CERT_RDN_VIDEOTEX_STRING
:
105 case CERT_RDN_IA5_STRING
:
106 case CERT_RDN_GRAPHIC_STRING
:
107 case CERT_RDN_VISIBLE_STRING
:
108 case CERT_RDN_GENERAL_STRING
:
109 len
= pValue
->cbData
;
116 for (i
= 0; i
< pValue
->cbData
&& ptr
- psz
< csz
; ptr
++, i
++)
117 *ptr
= pValue
->pbData
[i
];
121 case CERT_RDN_BMP_STRING
:
122 case CERT_RDN_UTF8_STRING
:
123 strLen
= len
= pValue
->cbData
/ sizeof(WCHAR
);
130 for (i
= 0; i
< strLen
&& ptr
- psz
< csz
; ptr
++, i
++)
131 *ptr
= ((LPCWSTR
)pValue
->pbData
)[i
];
136 FIXME("string type %d unimplemented\n", dwValueType
);
146 TRACE("returning %d (%s)\n", ret
, debugstr_w(psz
));
150 static inline BOOL
is_quotable_char(char c
)
169 static DWORD
quote_rdn_value_to_str_a(DWORD dwValueType
,
170 PCERT_RDN_VALUE_BLOB pValue
, LPSTR psz
, DWORD csz
)
172 DWORD ret
= 0, len
, i
;
173 BOOL needsQuotes
= FALSE
;
175 TRACE("(%d, %p, %p, %d)\n", dwValueType
, pValue
, psz
, csz
);
179 case CERT_RDN_ANY_TYPE
:
181 case CERT_RDN_NUMERIC_STRING
:
182 case CERT_RDN_PRINTABLE_STRING
:
183 case CERT_RDN_TELETEX_STRING
:
184 case CERT_RDN_VIDEOTEX_STRING
:
185 case CERT_RDN_IA5_STRING
:
186 case CERT_RDN_GRAPHIC_STRING
:
187 case CERT_RDN_VISIBLE_STRING
:
188 case CERT_RDN_GENERAL_STRING
:
189 len
= pValue
->cbData
;
190 if (pValue
->cbData
&& isspace(pValue
->pbData
[0]))
192 if (pValue
->cbData
&& isspace(pValue
->pbData
[pValue
->cbData
- 1]))
194 for (i
= 0; i
< pValue
->cbData
; i
++)
196 if (is_quotable_char(pValue
->pbData
[i
]))
198 if (pValue
->pbData
[i
] == '"')
211 for (i
= 0; i
< pValue
->cbData
&& ptr
- psz
< csz
; ptr
++, i
++)
213 *ptr
= pValue
->pbData
[i
];
214 if (pValue
->pbData
[i
] == '"' && ptr
- psz
< csz
- 1)
217 if (needsQuotes
&& ptr
- psz
< csz
)
222 case CERT_RDN_BMP_STRING
:
223 case CERT_RDN_UTF8_STRING
:
224 len
= WideCharToMultiByte(CP_ACP
, 0, (LPCWSTR
)pValue
->pbData
,
225 pValue
->cbData
/ sizeof(WCHAR
), NULL
, 0, NULL
, NULL
);
226 if (pValue
->cbData
&& isspaceW(((LPCWSTR
)pValue
->pbData
)[0]))
228 if (pValue
->cbData
&&
229 isspaceW(((LPCWSTR
)pValue
->pbData
)[pValue
->cbData
/ sizeof(WCHAR
)-1]))
231 for (i
= 0; i
< pValue
->cbData
/ sizeof(WCHAR
); i
++)
233 if (is_quotable_char(((LPCWSTR
)pValue
->pbData
)[i
]))
235 if (((LPCWSTR
)pValue
->pbData
)[i
] == '"')
248 for (i
= 0; i
< pValue
->cbData
/ sizeof(WCHAR
) &&
249 dst
- psz
< csz
; dst
++, i
++)
251 LPCWSTR src
= (LPCWSTR
)pValue
->pbData
+ i
;
253 WideCharToMultiByte(CP_ACP
, 0, src
, 1, dst
,
254 csz
- (dst
- psz
) - 1, NULL
, NULL
);
255 if (*src
== '"' && dst
- psz
< csz
- 1)
258 if (needsQuotes
&& dst
- psz
< csz
)
264 FIXME("string type %d unimplemented\n", dwValueType
);
274 TRACE("returning %d (%s)\n", ret
, debugstr_a(psz
));
278 static DWORD
quote_rdn_value_to_str_w(DWORD dwValueType
,
279 PCERT_RDN_VALUE_BLOB pValue
, LPWSTR psz
, DWORD csz
)
281 DWORD ret
= 0, len
, i
, strLen
;
282 BOOL needsQuotes
= FALSE
;
284 TRACE("(%d, %p, %p, %d)\n", dwValueType
, pValue
, psz
, csz
);
288 case CERT_RDN_ANY_TYPE
:
290 case CERT_RDN_NUMERIC_STRING
:
291 case CERT_RDN_PRINTABLE_STRING
:
292 case CERT_RDN_TELETEX_STRING
:
293 case CERT_RDN_VIDEOTEX_STRING
:
294 case CERT_RDN_IA5_STRING
:
295 case CERT_RDN_GRAPHIC_STRING
:
296 case CERT_RDN_VISIBLE_STRING
:
297 case CERT_RDN_GENERAL_STRING
:
298 len
= pValue
->cbData
;
299 if (pValue
->cbData
&& isspace(pValue
->pbData
[0]))
301 if (pValue
->cbData
&& isspace(pValue
->pbData
[pValue
->cbData
- 1]))
303 for (i
= 0; i
< pValue
->cbData
; i
++)
305 if (is_quotable_char(pValue
->pbData
[i
]))
307 if (pValue
->pbData
[i
] == '"')
320 for (i
= 0; i
< pValue
->cbData
&& ptr
- psz
< csz
; ptr
++, i
++)
322 *ptr
= pValue
->pbData
[i
];
323 if (pValue
->pbData
[i
] == '"' && ptr
- psz
< csz
- 1)
326 if (needsQuotes
&& ptr
- psz
< csz
)
331 case CERT_RDN_BMP_STRING
:
332 case CERT_RDN_UTF8_STRING
:
333 strLen
= len
= pValue
->cbData
/ sizeof(WCHAR
);
334 if (pValue
->cbData
&& isspace(pValue
->pbData
[0]))
336 if (pValue
->cbData
&& isspace(pValue
->pbData
[strLen
- 1]))
338 for (i
= 0; i
< strLen
; i
++)
340 if (is_quotable_char(((LPCWSTR
)pValue
->pbData
)[i
]))
342 if (((LPCWSTR
)pValue
->pbData
)[i
] == '"')
355 for (i
= 0; i
< strLen
&& ptr
- psz
< csz
; ptr
++, i
++)
357 *ptr
= ((LPCWSTR
)pValue
->pbData
)[i
];
358 if (((LPCWSTR
)pValue
->pbData
)[i
] == '"' && ptr
- psz
< csz
- 1)
361 if (needsQuotes
&& ptr
- psz
< csz
)
367 FIXME("string type %d unimplemented\n", dwValueType
);
377 TRACE("returning %d (%s)\n", ret
, debugstr_w(psz
));
381 /* Adds the prefix prefix to the string pointed to by psz, followed by the
382 * character '='. Copies no more than csz characters. Returns the number of
383 * characters copied. If psz is NULL, returns the number of characters that
386 static DWORD
CRYPT_AddPrefixA(LPCSTR prefix
, LPSTR psz
, DWORD csz
)
390 TRACE("(%s, %p, %d)\n", debugstr_a(prefix
), psz
, csz
);
394 chars
= min(strlen(prefix
), csz
);
395 memcpy(psz
, prefix
, chars
);
396 *(psz
+ chars
) = '=';
400 chars
= lstrlenA(prefix
) + 1;
404 DWORD WINAPI
CertNameToStrA(DWORD dwCertEncodingType
, PCERT_NAME_BLOB pName
,
405 DWORD dwStrType
, LPSTR psz
, DWORD csz
)
407 static const DWORD unsupportedFlags
= CERT_NAME_STR_NO_QUOTING_FLAG
|
408 CERT_NAME_STR_ENABLE_T61_UNICODE_FLAG
;
409 static const char commaSep
[] = ", ";
410 static const char semiSep
[] = "; ";
411 static const char crlfSep
[] = "\r\n";
412 static const char plusSep
[] = " + ";
413 static const char spaceSep
[] = " ";
414 DWORD ret
= 0, bytes
= 0;
416 CERT_NAME_INFO
*info
;
418 TRACE("(%d, %p, %08x, %p, %d)\n", dwCertEncodingType
, pName
, dwStrType
,
420 if (dwStrType
& unsupportedFlags
)
421 FIXME("unsupported flags: %08x\n", dwStrType
& unsupportedFlags
);
423 bRet
= CryptDecodeObjectEx(dwCertEncodingType
, X509_NAME
, pName
->pbData
,
424 pName
->cbData
, CRYPT_DECODE_ALLOC_FLAG
, NULL
, &info
, &bytes
);
427 DWORD i
, j
, sepLen
, rdnSepLen
;
429 BOOL reverse
= dwStrType
& CERT_NAME_STR_REVERSE_FLAG
;
430 const CERT_RDN
*rdn
= info
->rgRDN
;
432 if(reverse
&& info
->cRDN
> 1) rdn
+= (info
->cRDN
- 1);
434 if (dwStrType
& CERT_NAME_STR_SEMICOLON_FLAG
)
436 else if (dwStrType
& CERT_NAME_STR_CRLF_FLAG
)
440 sepLen
= strlen(sep
);
441 if (dwStrType
& CERT_NAME_STR_NO_PLUS_FLAG
)
445 rdnSepLen
= strlen(rdnSep
);
446 for (i
= 0; (!psz
|| ret
< csz
) && i
< info
->cRDN
; i
++)
448 for (j
= 0; (!psz
|| ret
< csz
) && j
< rdn
->cRDNAttr
; j
++)
451 char prefixBuf
[13]; /* big enough for SERIALNUMBER */
452 LPCSTR prefix
= NULL
;
454 if ((dwStrType
& 0x000000ff) == CERT_OID_NAME_STR
)
455 prefix
= rdn
->rgRDNAttr
[j
].pszObjId
;
456 else if ((dwStrType
& 0x000000ff) == CERT_X500_NAME_STR
)
458 PCCRYPT_OID_INFO oidInfo
= CryptFindOIDInfo(
459 CRYPT_OID_INFO_OID_KEY
,
460 rdn
->rgRDNAttr
[j
].pszObjId
,
461 CRYPT_RDN_ATTR_OID_GROUP_ID
);
465 WideCharToMultiByte(CP_ACP
, 0, oidInfo
->pwszName
, -1,
466 prefixBuf
, sizeof(prefixBuf
), NULL
, NULL
);
470 prefix
= rdn
->rgRDNAttr
[j
].pszObjId
;
474 /* - 1 is needed to account for the NULL terminator. */
475 chars
= CRYPT_AddPrefixA(prefix
,
476 psz
? psz
+ ret
: NULL
, psz
? csz
- ret
- 1 : 0);
479 chars
= quote_rdn_value_to_str_a(
480 rdn
->rgRDNAttr
[j
].dwValueType
,
481 &rdn
->rgRDNAttr
[j
].Value
, psz
? psz
+ ret
: NULL
,
482 psz
? csz
- ret
: 0);
485 if (j
< rdn
->cRDNAttr
- 1)
487 if (psz
&& ret
< csz
- rdnSepLen
- 1)
488 memcpy(psz
+ ret
, rdnSep
, rdnSepLen
);
492 if (i
< info
->cRDN
- 1)
494 if (psz
&& ret
< csz
- sepLen
- 1)
495 memcpy(psz
+ ret
, sep
, sepLen
);
510 TRACE("Returning %s\n", debugstr_a(psz
));
514 /* Adds the prefix prefix to the wide-character string pointed to by psz,
515 * followed by the character '='. Copies no more than csz characters. Returns
516 * the number of characters copied. If psz is NULL, returns the number of
517 * characters that would be copied.
518 * Assumes the characters in prefix are ASCII (not multibyte characters.)
520 static DWORD
CRYPT_AddPrefixAToW(LPCSTR prefix
, LPWSTR psz
, DWORD csz
)
524 TRACE("(%s, %p, %d)\n", debugstr_a(prefix
), psz
, csz
);
530 chars
= min(strlen(prefix
), csz
);
531 for (i
= 0; i
< chars
; i
++)
532 *(psz
+ i
) = prefix
[i
];
533 *(psz
+ chars
) = '=';
537 chars
= lstrlenA(prefix
) + 1;
541 /* Adds the prefix prefix to the string pointed to by psz, followed by the
542 * character '='. Copies no more than csz characters. Returns the number of
543 * characters copied. If psz is NULL, returns the number of characters that
546 static DWORD
CRYPT_AddPrefixW(LPCWSTR prefix
, LPWSTR psz
, DWORD csz
)
550 TRACE("(%s, %p, %d)\n", debugstr_w(prefix
), psz
, csz
);
554 chars
= min(strlenW(prefix
), csz
);
555 memcpy(psz
, prefix
, chars
* sizeof(WCHAR
));
556 *(psz
+ chars
) = '=';
560 chars
= lstrlenW(prefix
) + 1;
564 static const WCHAR indent
[] = { ' ',' ',' ',' ',' ',0 };
566 DWORD
cert_name_to_str_with_indent(DWORD dwCertEncodingType
, DWORD indentLevel
,
567 const CERT_NAME_BLOB
*pName
, DWORD dwStrType
, LPWSTR psz
, DWORD csz
)
569 static const DWORD unsupportedFlags
= CERT_NAME_STR_NO_QUOTING_FLAG
|
570 CERT_NAME_STR_ENABLE_T61_UNICODE_FLAG
;
571 static const WCHAR commaSep
[] = { ',',' ',0 };
572 static const WCHAR semiSep
[] = { ';',' ',0 };
573 static const WCHAR crlfSep
[] = { '\r','\n',0 };
574 static const WCHAR plusSep
[] = { ' ','+',' ',0 };
575 static const WCHAR spaceSep
[] = { ' ',0 };
576 DWORD ret
= 0, bytes
= 0;
578 CERT_NAME_INFO
*info
;
580 if (dwStrType
& unsupportedFlags
)
581 FIXME("unsupported flags: %08x\n", dwStrType
& unsupportedFlags
);
583 bRet
= CryptDecodeObjectEx(dwCertEncodingType
, X509_NAME
, pName
->pbData
,
584 pName
->cbData
, CRYPT_DECODE_ALLOC_FLAG
, NULL
, &info
, &bytes
);
587 DWORD i
, j
, sepLen
, rdnSepLen
;
589 BOOL reverse
= dwStrType
& CERT_NAME_STR_REVERSE_FLAG
;
590 const CERT_RDN
*rdn
= info
->rgRDN
;
592 if(reverse
&& info
->cRDN
> 1) rdn
+= (info
->cRDN
- 1);
594 if (dwStrType
& CERT_NAME_STR_SEMICOLON_FLAG
)
596 else if (dwStrType
& CERT_NAME_STR_CRLF_FLAG
)
600 sepLen
= lstrlenW(sep
);
601 if (dwStrType
& CERT_NAME_STR_NO_PLUS_FLAG
)
605 rdnSepLen
= lstrlenW(rdnSep
);
606 for (i
= 0; (!psz
|| ret
< csz
) && i
< info
->cRDN
; i
++)
608 for (j
= 0; (!psz
|| ret
< csz
) && j
< rdn
->cRDNAttr
; j
++)
611 LPCSTR prefixA
= NULL
;
612 LPCWSTR prefixW
= NULL
;
614 if ((dwStrType
& 0x000000ff) == CERT_OID_NAME_STR
)
615 prefixA
= rdn
->rgRDNAttr
[j
].pszObjId
;
616 else if ((dwStrType
& 0x000000ff) == CERT_X500_NAME_STR
)
618 PCCRYPT_OID_INFO oidInfo
= CryptFindOIDInfo(
619 CRYPT_OID_INFO_OID_KEY
,
620 rdn
->rgRDNAttr
[j
].pszObjId
,
621 CRYPT_RDN_ATTR_OID_GROUP_ID
);
624 prefixW
= oidInfo
->pwszName
;
626 prefixA
= rdn
->rgRDNAttr
[j
].pszObjId
;
628 if (dwStrType
& CERT_NAME_STR_CRLF_FLAG
)
632 for (k
= 0; k
< indentLevel
; k
++)
636 chars
= min(strlenW(indent
), csz
- ret
- 1);
637 memcpy(psz
+ ret
, indent
, chars
* sizeof(WCHAR
));
640 chars
= strlenW(indent
);
646 /* - 1 is needed to account for the NULL terminator. */
647 chars
= CRYPT_AddPrefixW(prefixW
,
648 psz
? psz
+ ret
: NULL
, psz
? csz
- ret
- 1 : 0);
653 /* - 1 is needed to account for the NULL terminator. */
654 chars
= CRYPT_AddPrefixAToW(prefixA
,
655 psz
? psz
+ ret
: NULL
, psz
? csz
- ret
- 1 : 0);
658 chars
= quote_rdn_value_to_str_w(
659 rdn
->rgRDNAttr
[j
].dwValueType
,
660 &rdn
->rgRDNAttr
[j
].Value
, psz
? psz
+ ret
: NULL
,
661 psz
? csz
- ret
: 0);
664 if (j
< rdn
->cRDNAttr
- 1)
666 if (psz
&& ret
< csz
- rdnSepLen
- 1)
667 memcpy(psz
+ ret
, rdnSep
, rdnSepLen
* sizeof(WCHAR
));
671 if (i
< info
->cRDN
- 1)
673 if (psz
&& ret
< csz
- sepLen
- 1)
674 memcpy(psz
+ ret
, sep
, sepLen
* sizeof(WCHAR
));
692 DWORD WINAPI
CertNameToStrW(DWORD dwCertEncodingType
, PCERT_NAME_BLOB pName
,
693 DWORD dwStrType
, LPWSTR psz
, DWORD csz
)
697 TRACE("(%d, %p, %08x, %p, %d)\n", dwCertEncodingType
, pName
, dwStrType
,
700 ret
= cert_name_to_str_with_indent(dwCertEncodingType
, 0, pName
, dwStrType
,
702 TRACE("Returning %s\n", debugstr_w(psz
));
706 BOOL WINAPI
CertStrToNameA(DWORD dwCertEncodingType
, LPCSTR pszX500
,
707 DWORD dwStrType
, void *pvReserved
, BYTE
*pbEncoded
, DWORD
*pcbEncoded
,
713 TRACE("(%08x, %s, %08x, %p, %p, %p, %p)\n", dwCertEncodingType
,
714 debugstr_a(pszX500
), dwStrType
, pvReserved
, pbEncoded
, pcbEncoded
,
717 len
= MultiByteToWideChar(CP_ACP
, 0, pszX500
, -1, NULL
, 0);
720 LPWSTR x500
, errorStr
;
722 if ((x500
= CryptMemAlloc(len
* sizeof(WCHAR
))))
724 MultiByteToWideChar(CP_ACP
, 0, pszX500
, -1, x500
, len
);
725 ret
= CertStrToNameW(dwCertEncodingType
, x500
, dwStrType
,
726 pvReserved
, pbEncoded
, pcbEncoded
,
727 ppszError
? (LPCWSTR
*)&errorStr
: NULL
);
734 *ppszError
= pszX500
;
735 for (i
= 0; i
< errorStr
- x500
; i
++)
736 *ppszError
= CharNextA(*ppszError
);
745 SetLastError(ERROR_OUTOFMEMORY
);
751 SetLastError(CRYPT_E_INVALID_X500_STRING
);
753 *ppszError
= pszX500
;
761 WCHAR buf
[10]; /* big enough for L"GivenName" */
762 LPWSTR keyName
; /* usually = buf, but may be allocated */
766 static void CRYPT_InitializeKeynameKeeper(struct KeynameKeeper
*keeper
)
768 keeper
->keyName
= keeper
->buf
;
769 keeper
->keyLen
= sizeof(keeper
->buf
) / sizeof(keeper
->buf
[0]);
772 static void CRYPT_FreeKeynameKeeper(struct KeynameKeeper
*keeper
)
774 if (keeper
->keyName
!= keeper
->buf
)
775 CryptMemFree(keeper
->keyName
);
784 static void CRYPT_KeynameKeeperFromTokenW(struct KeynameKeeper
*keeper
,
785 const struct X500TokenW
*key
)
787 DWORD len
= key
->end
- key
->start
;
789 if (len
> keeper
->keyLen
)
791 if (keeper
->keyName
== keeper
->buf
)
792 keeper
->keyName
= CryptMemAlloc(len
* sizeof(WCHAR
));
794 keeper
->keyName
= CryptMemRealloc(keeper
->keyName
,
795 len
* sizeof(WCHAR
));
796 keeper
->keyLen
= len
;
798 memcpy(keeper
->keyName
, key
->start
, (key
->end
- key
->start
) *
800 keeper
->keyName
[len
] = '\0';
801 TRACE("Keyname is %s\n", debugstr_w(keeper
->keyName
));
804 static BOOL
CRYPT_GetNextKeyW(LPCWSTR str
, struct X500TokenW
*token
,
809 while (*str
&& isspaceW(*str
))
814 while (*str
&& *str
!= '=' && !isspaceW(*str
))
816 if (*str
&& (*str
== '=' || isspaceW(*str
)))
820 TRACE("missing equals char at %s\n", debugstr_w(token
->start
));
822 *ppszError
= token
->start
;
823 SetLastError(CRYPT_E_INVALID_X500_STRING
);
832 /* Assumes separators are characters in the 0-255 range */
833 static BOOL
CRYPT_GetNextValueW(LPCWSTR str
, DWORD dwFlags
, LPCWSTR separators
,
834 WCHAR
*separator_used
, struct X500TokenW
*token
, LPCWSTR
*ppszError
)
838 TRACE("(%s, %s, %p, %p)\n", debugstr_w(str
), debugstr_w(separators
), token
,
842 while (*str
&& isspaceW(*str
))
847 if (!(dwFlags
& CERT_NAME_STR_NO_QUOTING_FLAG
) && *str
== '"')
851 while (!token
->end
&& ret
)
853 while (*str
&& *str
!= '"')
857 if (*(str
+ 1) != '"')
858 token
->end
= str
+ 1;
864 TRACE("unterminated quote at %s\n", debugstr_w(str
));
867 SetLastError(CRYPT_E_INVALID_X500_STRING
);
874 WCHAR map
[256] = { 0 };
877 map
[*separators
++] = 1;
878 while (*str
&& (*str
>= 0xff || !map
[*str
]))
881 if (map
[*str
]) *separator_used
= *str
;
886 TRACE("missing value at %s\n", debugstr_w(str
));
889 SetLastError(CRYPT_E_INVALID_X500_STRING
);
895 /* Encodes the string represented by value as the string type type into the
896 * CERT_NAME_BLOB output. If there is an error and ppszError is not NULL,
897 * *ppszError is set to the first failing character. If there is no error,
898 * output's pbData must be freed with LocalFree.
900 static BOOL
CRYPT_EncodeValueWithType(DWORD dwCertEncodingType
,
901 const struct X500TokenW
*value
, PCERT_NAME_BLOB output
, DWORD type
,
904 CERT_NAME_VALUE nameValue
= { type
, { 0, NULL
} };
907 if (value
->end
> value
->start
)
912 nameValue
.Value
.pbData
= CryptMemAlloc((value
->end
- value
->start
+ 1) *
914 if (!nameValue
.Value
.pbData
)
916 SetLastError(ERROR_OUTOFMEMORY
);
919 ptr
= (LPWSTR
)nameValue
.Value
.pbData
;
920 for (i
= 0; i
< value
->end
- value
->start
; i
++)
922 *ptr
++ = value
->start
[i
];
923 if (value
->start
[i
] == '"')
926 /* The string is NULL terminated because of a quirk in encoding
927 * unicode names values: if the length is given as 0, the value is
928 * assumed to be a NULL-terminated string.
931 nameValue
.Value
.cbData
= (LPBYTE
)ptr
- nameValue
.Value
.pbData
;
933 ret
= CryptEncodeObjectEx(dwCertEncodingType
, X509_UNICODE_NAME_VALUE
,
934 &nameValue
, CRYPT_ENCODE_ALLOC_FLAG
, NULL
, &output
->pbData
,
936 if (!ret
&& ppszError
)
938 if (type
== CERT_RDN_NUMERIC_STRING
&&
939 GetLastError() == CRYPT_E_INVALID_NUMERIC_STRING
)
940 *ppszError
= value
->start
+ output
->cbData
;
941 else if (type
== CERT_RDN_PRINTABLE_STRING
&&
942 GetLastError() == CRYPT_E_INVALID_PRINTABLE_STRING
)
943 *ppszError
= value
->start
+ output
->cbData
;
944 else if (type
== CERT_RDN_IA5_STRING
&&
945 GetLastError() == CRYPT_E_INVALID_IA5_STRING
)
946 *ppszError
= value
->start
+ output
->cbData
;
948 CryptMemFree(nameValue
.Value
.pbData
);
952 static BOOL
CRYPT_EncodeValue(DWORD dwCertEncodingType
,
953 const struct X500TokenW
*value
, PCERT_NAME_BLOB output
, const DWORD
*types
,
960 for (i
= 0; !ret
&& types
[i
]; i
++)
961 ret
= CRYPT_EncodeValueWithType(dwCertEncodingType
, value
, output
,
962 types
[i
], ppszError
);
966 static BOOL
CRYPT_ValueToRDN(DWORD dwCertEncodingType
, PCERT_NAME_INFO info
,
967 PCCRYPT_OID_INFO keyOID
, struct X500TokenW
*value
, DWORD dwStrType
, LPCWSTR
*ppszError
)
971 TRACE("OID %s, value %s\n", debugstr_a(keyOID
->pszOID
),
972 debugstr_wn(value
->start
, value
->end
- value
->start
));
975 info
->rgRDN
= CryptMemAlloc(sizeof(CERT_RDN
));
977 info
->rgRDN
= CryptMemRealloc(info
->rgRDN
,
978 (info
->cRDN
+ 1) * sizeof(CERT_RDN
));
981 /* FIXME: support multiple RDN attrs */
982 info
->rgRDN
[info
->cRDN
].rgRDNAttr
=
983 CryptMemAlloc(sizeof(CERT_RDN_ATTR
));
984 if (info
->rgRDN
[info
->cRDN
].rgRDNAttr
)
986 static const DWORD defaultTypes
[] = { CERT_RDN_PRINTABLE_STRING
,
987 CERT_RDN_BMP_STRING
, 0 };
990 info
->rgRDN
[info
->cRDN
].cRDNAttr
= 1;
991 info
->rgRDN
[info
->cRDN
].rgRDNAttr
[0].pszObjId
=
992 (LPSTR
)keyOID
->pszOID
;
993 info
->rgRDN
[info
->cRDN
].rgRDNAttr
[0].dwValueType
=
994 CERT_RDN_ENCODED_BLOB
;
995 if (keyOID
->ExtraInfo
.cbData
)
996 types
= (const DWORD
*)keyOID
->ExtraInfo
.pbData
;
998 types
= defaultTypes
;
1000 /* Remove surrounding quotes */
1001 if (value
->start
[0] == '"' && !(dwStrType
& CERT_NAME_STR_NO_QUOTING_FLAG
))
1006 ret
= CRYPT_EncodeValue(dwCertEncodingType
, value
,
1007 &info
->rgRDN
[info
->cRDN
].rgRDNAttr
[0].Value
, types
, ppszError
);
1010 SetLastError(ERROR_OUTOFMEMORY
);
1014 SetLastError(ERROR_OUTOFMEMORY
);
1018 BOOL WINAPI
CertStrToNameW(DWORD dwCertEncodingType
, LPCWSTR pszX500
,
1019 DWORD dwStrType
, void *pvReserved
, BYTE
*pbEncoded
, DWORD
*pcbEncoded
,
1022 CERT_NAME_INFO info
= { 0, NULL
};
1024 struct KeynameKeeper keeper
;
1028 TRACE("(%08x, %s, %08x, %p, %p, %p, %p)\n", dwCertEncodingType
,
1029 debugstr_w(pszX500
), dwStrType
, pvReserved
, pbEncoded
, pcbEncoded
,
1032 CRYPT_InitializeKeynameKeeper(&keeper
);
1034 while (str
&& *str
&& ret
)
1036 struct X500TokenW token
;
1038 ret
= CRYPT_GetNextKeyW(str
, &token
, ppszError
);
1039 if (ret
&& token
.start
)
1041 PCCRYPT_OID_INFO keyOID
;
1043 CRYPT_KeynameKeeperFromTokenW(&keeper
, &token
);
1044 keyOID
= CryptFindOIDInfo(CRYPT_OID_INFO_NAME_KEY
, keeper
.keyName
,
1045 CRYPT_RDN_ATTR_OID_GROUP_ID
);
1049 *ppszError
= token
.start
;
1050 SetLastError(CRYPT_E_INVALID_X500_STRING
);
1056 while (isspace(*str
))
1062 SetLastError(CRYPT_E_INVALID_X500_STRING
);
1067 static const WCHAR commaSep
[] = { ',',0 };
1068 static const WCHAR semiSep
[] = { ';',0 };
1069 static const WCHAR crlfSep
[] = { '\r','\n',0 };
1070 static const WCHAR allSepsWithoutPlus
[] = { ',',';','\r','\n',0 };
1071 static const WCHAR allSeps
[] = { '+',',',';','\r','\n',0 };
1076 if (dwStrType
& CERT_NAME_STR_COMMA_FLAG
)
1078 else if (dwStrType
& CERT_NAME_STR_SEMICOLON_FLAG
)
1080 else if (dwStrType
& CERT_NAME_STR_CRLF_FLAG
)
1082 else if (dwStrType
& CERT_NAME_STR_NO_PLUS_FLAG
)
1083 sep
= allSepsWithoutPlus
;
1086 ret
= CRYPT_GetNextValueW(str
, dwStrType
, sep
, &sep_used
, &token
,
1091 /* if token.end points to the separator, skip it */
1092 if (str
&& sep_used
&& *str
== sep_used
) str
++;
1094 ret
= CRYPT_ValueToRDN(dwCertEncodingType
, &info
,
1095 keyOID
, &token
, dwStrType
, ppszError
);
1101 CRYPT_FreeKeynameKeeper(&keeper
);
1106 ret
= CryptEncodeObjectEx(dwCertEncodingType
, X509_NAME
, &info
,
1107 0, NULL
, pbEncoded
, pcbEncoded
);
1109 for (i
= 0; i
< info
.cRDN
; i
++)
1113 for (j
= 0; j
< info
.rgRDN
[i
].cRDNAttr
; j
++)
1114 LocalFree(info
.rgRDN
[i
].rgRDNAttr
[j
].Value
.pbData
);
1115 CryptMemFree(info
.rgRDN
[i
].rgRDNAttr
);
1117 CryptMemFree(info
.rgRDN
);
1121 DWORD WINAPI
CertGetNameStringA(PCCERT_CONTEXT pCertContext
, DWORD dwType
,
1122 DWORD dwFlags
, void *pvTypePara
, LPSTR pszNameString
, DWORD cchNameString
)
1126 TRACE("(%p, %d, %08x, %p, %p, %d)\n", pCertContext
, dwType
, dwFlags
,
1127 pvTypePara
, pszNameString
, cchNameString
);
1134 nameLen
= CertGetNameStringW(pCertContext
, dwType
, dwFlags
, pvTypePara
,
1136 wideName
= CryptMemAlloc(nameLen
* sizeof(WCHAR
));
1139 CertGetNameStringW(pCertContext
, dwType
, dwFlags
, pvTypePara
,
1141 nameLen
= WideCharToMultiByte(CP_ACP
, 0, wideName
, nameLen
,
1142 pszNameString
, cchNameString
, NULL
, NULL
);
1143 if (nameLen
<= cchNameString
)
1147 pszNameString
[cchNameString
- 1] = '\0';
1148 ret
= cchNameString
;
1150 CryptMemFree(wideName
);
1154 *pszNameString
= '\0';
1159 ret
= CertGetNameStringW(pCertContext
, dwType
, dwFlags
, pvTypePara
,
1164 /* Searches cert's extensions for the alternate name extension with OID
1165 * altNameOID, and if found, searches it for the alternate name type entryType.
1166 * If found, returns a pointer to the entry, otherwise returns NULL.
1167 * Regardless of whether an entry of the desired type is found, if the
1168 * alternate name extension is present, sets *info to the decoded alternate
1169 * name extension, which you must free using LocalFree.
1170 * The return value is a pointer within *info, so don't free *info before
1171 * you're done with the return value.
1173 static PCERT_ALT_NAME_ENTRY
cert_find_alt_name_entry(PCCERT_CONTEXT cert
,
1174 LPCSTR altNameOID
, DWORD entryType
, PCERT_ALT_NAME_INFO
*info
)
1176 PCERT_ALT_NAME_ENTRY entry
= NULL
;
1177 PCERT_EXTENSION ext
= CertFindExtension(altNameOID
,
1178 cert
->pCertInfo
->cExtension
, cert
->pCertInfo
->rgExtension
);
1184 if (CryptDecodeObjectEx(cert
->dwCertEncodingType
, X509_ALTERNATE_NAME
,
1185 ext
->Value
.pbData
, ext
->Value
.cbData
, CRYPT_DECODE_ALLOC_FLAG
, NULL
,
1190 for (i
= 0; !entry
&& i
< (*info
)->cAltEntry
; i
++)
1191 if ((*info
)->rgAltEntry
[i
].dwAltNameChoice
== entryType
)
1192 entry
= &(*info
)->rgAltEntry
[i
];
1200 static DWORD
cert_get_name_from_rdn_attr(DWORD encodingType
,
1201 const CERT_NAME_BLOB
*name
, LPCSTR oid
, LPWSTR pszNameString
, DWORD cchNameString
)
1203 CERT_NAME_INFO
*nameInfo
;
1204 DWORD bytes
= 0, ret
= 0;
1206 if (CryptDecodeObjectEx(encodingType
, X509_NAME
, name
->pbData
,
1207 name
->cbData
, CRYPT_DECODE_ALLOC_FLAG
, NULL
, &nameInfo
, &bytes
))
1209 PCERT_RDN_ATTR nameAttr
;
1212 oid
= szOID_RSA_emailAddr
;
1213 nameAttr
= CertFindRDNAttr(oid
, nameInfo
);
1215 ret
= CertRDNValueToStrW(nameAttr
->dwValueType
, &nameAttr
->Value
,
1216 pszNameString
, cchNameString
);
1217 LocalFree(nameInfo
);
1222 DWORD WINAPI
CertGetNameStringW(PCCERT_CONTEXT pCertContext
, DWORD dwType
,
1223 DWORD dwFlags
, void *pvTypePara
, LPWSTR pszNameString
, DWORD cchNameString
)
1226 PCERT_NAME_BLOB name
;
1229 TRACE("(%p, %d, %08x, %p, %p, %d)\n", pCertContext
, dwType
,
1230 dwFlags
, pvTypePara
, pszNameString
, cchNameString
);
1235 if (dwFlags
& CERT_NAME_ISSUER_FLAG
)
1237 name
= &pCertContext
->pCertInfo
->Issuer
;
1238 altNameOID
= szOID_ISSUER_ALT_NAME
;
1242 name
= &pCertContext
->pCertInfo
->Subject
;
1243 altNameOID
= szOID_SUBJECT_ALT_NAME
;
1248 case CERT_NAME_EMAIL_TYPE
:
1250 CERT_ALT_NAME_INFO
*info
;
1251 PCERT_ALT_NAME_ENTRY entry
= cert_find_alt_name_entry(pCertContext
,
1252 altNameOID
, CERT_ALT_NAME_RFC822_NAME
, &info
);
1257 ret
= strlenW(entry
->u
.pwszRfc822Name
) + 1;
1258 else if (cchNameString
)
1260 ret
= min(strlenW(entry
->u
.pwszRfc822Name
), cchNameString
- 1);
1261 memcpy(pszNameString
, entry
->u
.pwszRfc822Name
,
1262 ret
* sizeof(WCHAR
));
1263 pszNameString
[ret
++] = 0;
1269 ret
= cert_get_name_from_rdn_attr(pCertContext
->dwCertEncodingType
,
1270 name
, szOID_RSA_emailAddr
, pszNameString
, cchNameString
);
1273 case CERT_NAME_RDN_TYPE
:
1275 DWORD type
= pvTypePara
? *(DWORD
*)pvTypePara
: 0;
1278 ret
= CertNameToStrW(pCertContext
->dwCertEncodingType
, name
,
1279 type
, pszNameString
, cchNameString
);
1282 CERT_ALT_NAME_INFO
*info
;
1283 PCERT_ALT_NAME_ENTRY entry
= cert_find_alt_name_entry(pCertContext
,
1284 altNameOID
, CERT_ALT_NAME_DIRECTORY_NAME
, &info
);
1287 ret
= CertNameToStrW(pCertContext
->dwCertEncodingType
,
1288 &entry
->u
.DirectoryName
, type
, pszNameString
, cchNameString
);
1294 case CERT_NAME_ATTR_TYPE
:
1295 ret
= cert_get_name_from_rdn_attr(pCertContext
->dwCertEncodingType
,
1296 name
, pvTypePara
, pszNameString
, cchNameString
);
1299 CERT_ALT_NAME_INFO
*altInfo
;
1300 PCERT_ALT_NAME_ENTRY entry
= cert_find_alt_name_entry(pCertContext
,
1301 altNameOID
, CERT_ALT_NAME_DIRECTORY_NAME
, &altInfo
);
1304 ret
= cert_name_to_str_with_indent(X509_ASN_ENCODING
, 0,
1305 &entry
->u
.DirectoryName
, 0, pszNameString
, cchNameString
);
1310 case CERT_NAME_SIMPLE_DISPLAY_TYPE
:
1312 static const LPCSTR simpleAttributeOIDs
[] = { szOID_COMMON_NAME
,
1313 szOID_ORGANIZATIONAL_UNIT_NAME
, szOID_ORGANIZATION_NAME
,
1314 szOID_RSA_emailAddr
};
1315 CERT_NAME_INFO
*nameInfo
= NULL
;
1318 if (CryptDecodeObjectEx(pCertContext
->dwCertEncodingType
, X509_NAME
,
1319 name
->pbData
, name
->cbData
, CRYPT_DECODE_ALLOC_FLAG
, NULL
, &nameInfo
,
1322 PCERT_RDN_ATTR nameAttr
= NULL
;
1324 for (i
= 0; !nameAttr
&& i
< sizeof(simpleAttributeOIDs
) /
1325 sizeof(simpleAttributeOIDs
[0]); i
++)
1326 nameAttr
= CertFindRDNAttr(simpleAttributeOIDs
[i
], nameInfo
);
1328 ret
= CertRDNValueToStrW(nameAttr
->dwValueType
,
1329 &nameAttr
->Value
, pszNameString
, cchNameString
);
1330 LocalFree(nameInfo
);
1334 CERT_ALT_NAME_INFO
*altInfo
;
1335 PCERT_ALT_NAME_ENTRY entry
= cert_find_alt_name_entry(pCertContext
,
1336 altNameOID
, CERT_ALT_NAME_RFC822_NAME
, &altInfo
);
1340 if (!entry
&& altInfo
->cAltEntry
)
1341 entry
= &altInfo
->rgAltEntry
[0];
1345 ret
= strlenW(entry
->u
.pwszRfc822Name
) + 1;
1346 else if (cchNameString
)
1348 ret
= min(strlenW(entry
->u
.pwszRfc822Name
),
1350 memcpy(pszNameString
, entry
->u
.pwszRfc822Name
,
1351 ret
* sizeof(WCHAR
));
1352 pszNameString
[ret
++] = 0;
1360 case CERT_NAME_FRIENDLY_DISPLAY_TYPE
:
1362 DWORD cch
= cchNameString
;
1364 if (CertGetCertificateContextProperty(pCertContext
,
1365 CERT_FRIENDLY_NAME_PROP_ID
, pszNameString
, &cch
))
1368 ret
= CertGetNameStringW(pCertContext
,
1369 CERT_NAME_SIMPLE_DISPLAY_TYPE
, dwFlags
, pvTypePara
, pszNameString
,
1373 case CERT_NAME_DNS_TYPE
:
1375 CERT_ALT_NAME_INFO
*info
;
1376 PCERT_ALT_NAME_ENTRY entry
= cert_find_alt_name_entry(pCertContext
,
1377 altNameOID
, CERT_ALT_NAME_DNS_NAME
, &info
);
1382 ret
= strlenW(entry
->u
.pwszDNSName
) + 1;
1383 else if (cchNameString
)
1385 ret
= min(strlenW(entry
->u
.pwszDNSName
), cchNameString
- 1);
1386 memcpy(pszNameString
, entry
->u
.pwszDNSName
, ret
* sizeof(WCHAR
));
1387 pszNameString
[ret
++] = 0;
1393 ret
= cert_get_name_from_rdn_attr(pCertContext
->dwCertEncodingType
,
1394 name
, szOID_COMMON_NAME
, pszNameString
, cchNameString
);
1397 case CERT_NAME_URL_TYPE
:
1399 CERT_ALT_NAME_INFO
*info
;
1400 PCERT_ALT_NAME_ENTRY entry
= cert_find_alt_name_entry(pCertContext
,
1401 altNameOID
, CERT_ALT_NAME_URL
, &info
);
1406 ret
= strlenW(entry
->u
.pwszURL
) + 1;
1407 else if (cchNameString
)
1409 ret
= min(strlenW(entry
->u
.pwszURL
), cchNameString
- 1);
1410 memcpy(pszNameString
, entry
->u
.pwszURL
, ret
* sizeof(WCHAR
));
1411 pszNameString
[ret
++] = 0;
1419 FIXME("unimplemented for type %d\n", dwType
);
1427 else if (cchNameString
)
1429 pszNameString
[0] = 0;