Статус: Активный участник
Группы: Участники
Зарегистрирован: 30.07.2012(UTC) Сообщений: 34
|
Добрый день Сделал функцию выводящую на экран\в файл имена сертификатов и их отпечатки. Код:if ( !( hCertStore = CertOpenStore(
CERT_STORE_PROV_SYSTEM,
0,
NULL,
CERT_SYSTEM_STORE_CURRENT_USER,
lpszStoreSubject)))
{
MyHandleError(TEXT("The MY store could not be opened."));
goto quit_list;
}else printf("Store opened\n\n");
if(outfile){
outfile_f = fopen(outfile, "w");
if(!outfile_f) {
return(0);
}
}
while(pCertContext= CertEnumCertificatesInStore(
hCertStore,
pCertContext)) // on the first call to the function,
// this parameter is NULL
// on all subsequent calls,
// this parameter is the last pointer
// returned by the function
{
cbDecoded = 256;
if(!(pbDecoded = (BYTE*)malloc(cbDecoded)))
{
MyHandleError("Decode buffer memory allocation failed.");
goto quit_list;
}
if(!(CertGetNameString(
pCertContext,
CERT_NAME_RDN_TYPE,
0,
NULL,
(LPTSTR)pbDecoded,
cbDecoded)))
{
MyHandleError("Can`t find cert name.");
goto quit_list;
}
else {
if(outfile)
fprintf (outfile_f, "CertName: \n%s\n\n",pbDecoded);
else
printf("%s\n\n",pbDecoded);
if(!(CertGetCertificateContextProperty(
pCertContext,
CERT_HASH_PROP_ID,
NULL,
&cbHash)))
{
MyHandleError("The first hash calculate pass failed.");
goto quit_list;
}
if(!(pbHash = (BYTE*)malloc(cbHash)))
{
MyHandleError("Hash buffer memory allocation failed.");
goto quit_list;
}
if(!(CertGetCertificateContextProperty(
pCertContext,
CERT_HASH_PROP_ID,
pbHash,
&cbHash)))
{
MyHandleError("The second hash calculate pass failed.");
goto quit_list;
}
else{
char hex[40];
int i;
strncpy(hex,bytesToHex(pbHash),40);
if(outfile){
fprintf (outfile_f, "FingerPrint: ");
for (i=0;i<40;i++){
fprintf (outfile_f, "%c",hex[i]);
if((i+1)%2==0)
fprintf (outfile_f, " ");
}
fprintf (outfile_f, "\n\n");
}else{
for (i=0;i<40;i++){
printf("%c",hex[i]);
if((i+1)%2==0)
printf(" ");
}
printf("\n\n");
}
}
}
}
Проблемы две. 1: Когда мы проходим по хранилищу ROOT хеш некоторых сертификатов не 40 символов а 10 или 20. Пример :Microsoft Root Certificate Authority cd d4 ee ae 60. По коду пробежал , и почему-то мне CertGetCertificateContextProperty возвращает 5 символов, а не 20. Вот функция конверта Hex to Byte: Код:char* bytesToHex(BYTE* raw) {
char *kDigits[] = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a",
"b", "c", "d", "e", "f" };
int length = strlen((char*)raw);
char *hex;
int value,highIndex,lowIndex,i;
hex = (char*)malloc(length*2);
for (i = 0; i < length; i++) {
value = (raw[i] + 256) % 256;
highIndex = value >> 4;
lowIndex = value & 0x0f;
if(i==0)
strcpy(hex,kDigits[highIndex]);
else
strcat(hex,kDigits[highIndex]);
strcat(hex,kDigits[lowIndex]);
}
return (char*)hex;
}
Что неправильно? 2: CertGetNameString возвращает что-то вроде: Microsoft Root Certificate Authority microsoft com А хотелось бы :CA=Microsoft Root Certificate Authority,C=microsoft и т.д. Можно это както получить через cryptoApi?
|