CryptEncodeObjectEx segfault при обращении

128
26 апреля 2019, 08:20

CryptEncodeObjectEx разваливается при обращении, параметры не влияют, как выяснилось.Собираю gcc-tdm, через Code::Blocks, что опробовано:

  1. Удалил родной для gcc crypt32.a, так как подозреваю что он довольно старый, прицепил в опциях линкера напрямую файл windows/system32/crypt32.dll 1.1 Тоже самое проделал для advapi.a
  2. На всякий случай вставил флаг -Lwinsdk/lib/../x86
  3. Перепроверил декларацию этой функции, взяв ее с сайта микрософт.

Все шаги не изменили поведение, код примерно следующий:

CryptEncodeObjectEx(X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
        PKCS_RSA_PRIVATE_KEY, pPrivKeyBLOB, 0, NULL, NULL, &size);

То есть никаких серьезных действий, только получить размер..

При сборке пишет следующий warning:

Warning: resolving _CryptEncodeObjectEx@28 by linking to _CryptEncodeObjectEx
Use --enable-stdcall-fixup to disable these warnings
Use --disable-stdcall-fixup to disable these fixups

В Call stack примерно такая картина при segfault :

Какие есть мысли по этому поводу? Win10, последний билд.

P.S.

Переставил gcc 8.10 i686-w64-mingw32, результат несколько изменился но примерно тоже самое. Для независимого теста нашел на гитхабе проектик где активно используется CryptEncodeObjectEx и есть уже скомпилированный вариант. Собранный автором работает, собранный мной нет. Приложение проходит 22 цикла в функции CryptEncodeObjectEx и вылетает...

Вот значимые части прохождения функции CryptEncodeObjectEx дебагером:

Лог GDB:

Child process PID: 7720
At rsa.c:474
At rsa.c:477
В CryptStringToBinaryA () (C:\Windows\System32\crypt32.dll)
#4  0x00401f27 in RSA_write_key (ctx=0x21298, ofile=0x40536d "public.pem", pemType=1) at rsa.c:477
rsa.c:477:11635:beg:0x401f27
At rsa.c:477
#4  0x00401f27 in RSA_write_key (ctx=0x21298, ofile=0x40536d "public.pem", pemType=1) at rsa.c:477
rsa.c:477:11635:beg:0x401f27
В ntdll!KiUserExceptionDispatcher () (C:\Windows\SYSTEM32\ntdll.dll)
#5  0x00401f27 in RSA_write_key (ctx=0x21298, ofile=0x40536d "public.pem", pemType=1) at rsa.c:477
rsa.c:477:11635:beg:0x401f27
At rsa.c:477
#5  0x00401f27 in RSA_write_key (ctx=0x21298, ofile=0x40536d "public.pem", pemType=1) at rsa.c:477
rsa.c:477:11635:beg:0x401f27
В ntdll!RtlUnwind () (C:\Windows\SYSTEM32\ntdll.dll)
#4  0x00401f27 in RSA_write_key (ctx=0x21298, ofile=0x40536d "public.pem", pemType=1) at rsa.c:477
rsa.c:477:11635:beg:0x401f27
At rsa.c:477
#4  0x00401f27 in RSA_write_key (ctx=0x21298, ofile=0x40536d "public.pem", pemType=1) at rsa.c:477
rsa.c:477:11635:beg:0x401f27
В ntdll!RtlImageNtHeaderEx () (C:\Windows\SYSTEM32\ntdll.dll)
#6  0x00401f27 in RSA_write_key (ctx=0x21298, ofile=0x40536d "public.pem", pemType=1) at rsa.c:477
rsa.c:477:11635:beg:0x401f27
At rsa.c:477
#6  0x00401f27 in RSA_write_key (ctx=0x21298, ofile=0x40536d "public.pem", pemType=1) at rsa.c:477
rsa.c:477:11635:beg:0x401f27
В ntdll!RtlUnwind () (C:\Windows\SYSTEM32\ntdll.dll)
#6  0x00401f27 in RSA_write_key (ctx=0x21298, ofile=0x40536d "public.pem", pemType=1) at rsa.c:477
rsa.c:477:11635:beg:0x401f27
At rsa.c:477
#6  0x00401f27 in RSA_write_key (ctx=0x21298, ofile=0x40536d "public.pem", pemType=1) at rsa.c:477
rsa.c:477:11635:beg:0x401f27
В ntdll!RtlSleepConditionVariableSRW () (C:\Windows\SYSTEM32\ntdll.dll)
#6  0x00401f27 in RSA_write_key (ctx=0x21298, ofile=0x40536d "public.pem", pemType=1) at rsa.c:477
rsa.c:477:11635:beg:0x401f27
At rsa.c:477
#6  0x00401f27 in RSA_write_key (ctx=0x21298, ofile=0x40536d "public.pem", pemType=1) at rsa.c:477
rsa.c:477:11635:beg:0x401f27
В ntdll!RtlAcquireSRWLockExclusive () (C:\Windows\SYSTEM32\ntdll.dll)
#8  0x00401f27 in RSA_write_key (ctx=0x21298, ofile=0x40536d "public.pem", pemType=1) at rsa.c:477
rsa.c:477:11635:beg:0x401f27
At rsa.c:477
#8  0x00401f27 in RSA_write_key (ctx=0x21298, ofile=0x40536d "public.pem", pemType=1) at rsa.c:477
rsa.c:477:11635:beg:0x401f27
В ntdll!RtlSleepConditionVariableSRW () (C:\Windows\SYSTEM32\ntdll.dll)
#7  0x00401f27 in RSA_write_key (ctx=0x21298, ofile=0x40536d "public.pem", pemType=1) at rsa.c:477
rsa.c:477:11635:beg:0x401f27
At rsa.c:477
#7  0x00401f27 in RSA_write_key (ctx=0x21298, ofile=0x40536d "public.pem", pemType=1) at rsa.c:477
rsa.c:477:11635:beg:0x401f27
В ntdll!LdrGetProcedureAddressEx () (C:\Windows\SYSTEM32\ntdll.dll)
#7  0x00401f27 in RSA_write_key (ctx=0x21298, ofile=0x40536d "public.pem", pemType=1) at rsa.c:477
rsa.c:477:11635:beg:0x401f27
At rsa.c:477
#7  0x00401f27 in RSA_write_key (ctx=0x21298, ofile=0x40536d "public.pem", pemType=1) at rsa.c:477
rsa.c:477:11635:beg:0x401f27
В ntdll!RtlSleepConditionVariableSRW () (C:\Windows\SYSTEM32\ntdll.dll)
#7  0x00401f27 in RSA_write_key (ctx=0x21298, ofile=0x40536d "public.pem", pemType=1) at rsa.c:477
rsa.c:477:11635:beg:0x401f27
At rsa.c:477
#7  0x00401f27 in RSA_write_key (ctx=0x21298, ofile=0x40536d "public.pem", pemType=1) at rsa.c:477
rsa.c:477:11635:beg:0x401f27
В ntdll!LdrGetProcedureAddressEx () (C:\Windows\SYSTEM32\ntdll.dll)
#7  0x00401f27 in RSA_write_key (ctx=0x21298, ofile=0x40536d "public.pem", pemType=1) at rsa.c:477
rsa.c:477:11635:beg:0x401f27
At rsa.c:477
#7  0x00401f27 in RSA_write_key (ctx=0x21298, ofile=0x40536d "public.pem", pemType=1) at rsa.c:477
rsa.c:477:11635:beg:0x401f27
В ntdll!RtlSleepConditionVariableSRW () (C:\Windows\SYSTEM32\ntdll.dll)
#7  0x00401f27 in RSA_write_key (ctx=0x21298, ofile=0x40536d "public.pem", pemType=1) at rsa.c:477
rsa.c:477:11635:beg:0x401f27
At rsa.c:477
#7  0x00401f27 in RSA_write_key (ctx=0x21298, ofile=0x40536d "public.pem", pemType=1) at rsa.c:477
rsa.c:477:11635:beg:0x401f27
В ntdll!LdrGetProcedureAddressEx () (C:\Windows\SYSTEM32\ntdll.dll)
#7  0x00401f27 in RSA_write_key (ctx=0x21298, ofile=0x40536d "public.pem", pemType=1) at rsa.c:477
rsa.c:477:11635:beg:0x401f27
At rsa.c:477
#7  0x00401f27 in RSA_write_key (ctx=0x21298, ofile=0x40536d "public.pem", pemType=1) at rsa.c:477
rsa.c:477:11635:beg:0x401f27
В ntdll!RtlSleepConditionVariableSRW () (C:\Windows\SYSTEM32\ntdll.dll)
#7  0x00401f27 in RSA_write_key (ctx=0x21298, ofile=0x40536d "public.pem", pemType=1) at rsa.c:477
rsa.c:477:11635:beg:0x401f27
At rsa.c:477
#7  0x00401f27 in RSA_write_key (ctx=0x21298, ofile=0x40536d "public.pem", pemType=1) at rsa.c:477
rsa.c:477:11635:beg:0x401f27
В ntdll!LdrGetProcedureAddressEx () (C:\Windows\SYSTEM32\ntdll.dll)
#7  0x00401f27 in RSA_write_key (ctx=0x21298, ofile=0x40536d "public.pem", pemType=1) at rsa.c:477
rsa.c:477:11635:beg:0x401f27
At rsa.c:477
#7  0x00401f27 in RSA_write_key (ctx=0x21298, ofile=0x40536d "public.pem", pemType=1) at rsa.c:477
rsa.c:477:11635:beg:0x401f27
В ntdll!RtlSleepConditionVariableSRW () (C:\Windows\SYSTEM32\ntdll.dll)
#7  0x00401f27 in RSA_write_key (ctx=0x21298, ofile=0x40536d "public.pem", pemType=1) at rsa.c:477
rsa.c:477:11635:beg:0x401f27
At rsa.c:477
#7  0x00401f27 in RSA_write_key (ctx=0x21298, ofile=0x40536d "public.pem", pemType=1) at rsa.c:477
rsa.c:477:11635:beg:0x401f27
В ntdll!RtlRetrieveNtUserPfn () (C:\Windows\SYSTEM32\ntdll.dll)
#7  0x00401f27 in RSA_write_key (ctx=0x21298, ofile=0x40536d "public.pem", pemType=1) at rsa.c:477
rsa.c:477:11635:beg:0x401f27
At rsa.c:477
#7  0x00401f27 in RSA_write_key (ctx=0x21298, ofile=0x40536d "public.pem", pemType=1) at rsa.c:477
rsa.c:477:11635:beg:0x401f27
В ntdll!RtlSleepConditionVariableSRW () (C:\Windows\SYSTEM32\ntdll.dll)
#7  0x00401f27 in RSA_write_key (ctx=0x21298, ofile=0x40536d "public.pem", pemType=1) at rsa.c:477
rsa.c:477:11635:beg:0x401f27
At rsa.c:477
#7  0x00401f27 in RSA_write_key (ctx=0x21298, ofile=0x40536d "public.pem", pemType=1) at rsa.c:477
rsa.c:477:11635:beg:0x401f27
В ntdll!RtlUnwind () (C:\Windows\SYSTEM32\ntdll.dll)
#6  0x00401f27 in RSA_write_key (ctx=0x21298, ofile=0x40536d "public.pem", pemType=1) at rsa.c:477
rsa.c:477:11635:beg:0x401f27
At rsa.c:477
#6  0x00401f27 in RSA_write_key (ctx=0x21298, ofile=0x40536d "public.pem", pemType=1) at rsa.c:477
rsa.c:477:11635:beg:0x401f27
В ntdll!RtlImageNtHeaderEx () (C:\Windows\SYSTEM32\ntdll.dll)
#6  0x00401f27 in RSA_write_key (ctx=0x21298, ofile=0x40536d "public.pem", pemType=1) at rsa.c:477
rsa.c:477:11635:beg:0x401f27
At rsa.c:477
#6  0x00401f27 in RSA_write_key (ctx=0x21298, ofile=0x40536d "public.pem", pemType=1) at rsa.c:477
rsa.c:477:11635:beg:0x401f27
В ntdll!RtlUnwind () (C:\Windows\SYSTEM32\ntdll.dll)
#6  0x00401f27 in RSA_write_key (ctx=0x21298, ofile=0x40536d "public.pem", pemType=1) at rsa.c:477
rsa.c:477:11635:beg:0x401f27
At rsa.c:477
#6  0x00401f27 in RSA_write_key (ctx=0x21298, ofile=0x40536d "public.pem", pemType=1) at rsa.c:477
rsa.c:477:11635:beg:0x401f27
[Inferior 1 (process 7720) exited normally]
Отладчик завершился со статусом 0

nm при разных сборках (gcc версия) показывает одинаково:

00408224 I __imp__CryptDecodeObjectEx@32
00408228 I __imp__CryptEncodeObjectEx@28
0040822c I __imp__CryptExportPKCS8@28
00408230 I __imp__CryptExportPublicKeyInfo@20
0040820c I __imp__CryptImportKey@24
00408234 I __imp__CryptImportPublicKeyInfo@16
00408238 I __imp__CryptStringToBinaryA@28
00408218 I __imp__CryptVerifySignatureA@24
Answer 1

Спасибо @Zed за помощь, изучив в редакторе свой проект.cbp и сравнив его с вашим - нашел отличие, оно и поставило ситуацию на место, все заработало! Это был флаг компилятора -fexceptions, который в Code::Blocks никак не отражается, и вероятно устанавливается им по умолчанию :)

почему это происходит

Если исключение распространяется на функцию, которая была скомпилирована без поддержки исключений, программа завершается.

READ ALSO
Определение pure virtual function

Определение pure virtual function

Недавно обнаружил, что можно дать определение для pure virtual function вне класса:

132
How to sort Map by Key, but with two conditions in Java?

How to sort Map by Key, but with two conditions in Java?

У меня есть проблема в сортировке MapИмеется LinkedHashMap с ключами типа "September-2018", "October-2018", "November-2017", "November-2018"

121
Что в Java является константой, а что - нет?

Что в Java является константой, а что - нет?

До недавнего времени я был почти на 100% убеждён в том, что прекрасно понимаю сущность констант в языке программирования JavaТак было, пока я не наткнулся...

120