locked
Reading certficates in a smartcard with CNG RRS feed

  • Question

  • Hello,

    I am trying to access to certificates stored in a smartcard with CNG API.

    The following code crashes when I insert a smartcard when executing NCryptEnumKeys(). As soon as I removed the smartcard it is working. I am using an "Alcor Micro USB Smart Card Reader".

        NCRYPT_PROV_HANDLE hProv;
        NCRYPT_KEY_HANDLE hKey;
        SECURITY_STATUS ret;
        NCryptKeyName *pKeyName;
        PVOID pState;

        ret = NCryptOpenStorageProvider(&hProv, MS_SMART_CARD_KEY_STORAGE_PROVIDER, 0);
        if (ret != ERROR_SUCCESS)
            return FALSE;
        pKeyName = 0;
        pState = 0;
        while (NCryptEnumKeys(hProv, 0, &pKeyName, &pState, 0) == ERROR_SUCCESS)
        {
            wprintf(L"Found key : %s\n", pKeyName->pszName);
            NCryptFreeBuffer(pKeyName);
        }
        ret = NCryptFreeObject(hProv);

    Is there something wrong with this code ?

    Regards.

    Eric

    Monday, January 13, 2020 11:20 AM

All replies

  • There is no crash, if I use NCRYPT_SILENT_FLAG flag with NCryptEnumKeys

    NCryptEnumKeys(hProv, 0, &pKeyName, &pState, NCRYPT_SILENT_FLAG)

    In the call stack, I can find calls to Windows.UI.Xaml. Maybe something is missing.

    Eric

    Monday, January 13, 2020 2:55 PM
  • Hi,

    Thanks for posting here.

    I test the code above and it works for me. with "Gemalto IDBridge CT30 USB Smart Card Reader".

    Is that your complete, reproducible code? 

    According to the document:

    • If a service calls this function from its StartService function, a deadlock can occur, and the service may stop responding.

    Make sure to follow the document.

    Best Regards,

    Drake


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, January 14, 2020 6:15 AM
  • Hello,

    The code I provided is complete. It is run as a Win32 console application.

    If I insert no smartcard, it outputs :

    Found key : DigitalPersona
    Found key : te-TPMVirtualSmartCardLogon-04e25-43898

    If I insert a smartcard into my Alcor Micro USB Smartcard reader, it crashes. This smartcard reader is integrated with my laptop. If I plug another smartcard reader and I insert a smartcard, it does not crash but the certificate in the smartcard is not detected.

    Regards.

    Eric

    Tuesday, January 14, 2020 8:53 AM
  • Retry and still cannot repro it. It seems to be related to USB Card Reader, have you tried to update the USB Card Reader Driver?

    Best Regards,

    Drake


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, January 14, 2020 9:53 AM
  • It works when NCRYPT_SILENT_FLAG is set :
    - no issue with NCryptEnumKeys(hProv, 0, &pKeyName, &pState, NCRYPT_SILENT_FLAG)
    - issue with NCryptEnumKeys(hProv, 0, &pKeyName, &pState, 0)

    I have the same stack call with CryptoAPI code, when CryptSignHash is called :
    CryptSignHash(m_hCryptHash, AT_KEYEXCHANGE, NULL, 0, m_signature, (DWORD*)&signature_length)

    The stack call shows exception from Windows.UI.Xaml.Controls.dll :
    KernelBase.dll!_RaiseException@16()    Inconnu
    ucrtbase.dll!__CxxThrowException@8()    Inconnu
    Windows.UI.Xaml.Controls.dll!winrt::throw_hresult(struct winrt::hresult)    Inconnu
    Windows.UI.Xaml.Controls.dll!winrt::impl::consume_Windows_UI_Xaml_Controls_ISwipeControl<struct winrt::Windows::UI::Xaml::Controls::ISwipeControl>::BottomItems(void)    Inconnu
    Windows.UI.Xaml.Controls.dll!winrt::impl::factory_cache_entry<winrt::Windows::ApplicationModel::Core::CoreApplication,winrt::Windows::ApplicationModel::Core::ICoreApplication>::call<<lambda_dac4b3ce7a64b1a4047f35968bf63952> &>()    Inconnu
    Windows.UI.Xaml.Controls.dll!DispatcherHelper::DispatcherHelper()    Inconnu
    Windows.UI.Xaml.Controls.dll!CommandBarFlyout::CommandBarFlyout(void)    Inconnu
    Windows.UI.Xaml.Controls.dll!TextCommandBarFlyout::TextCommandBarFlyout(void)    Inconnu
    Windows.UI.Xaml.Controls.dll!winrt::make_self<class TextCommandBarFlyout>(void)    Inconnu
    Windows.UI.Xaml.Controls.dll!winrt::impl::produce<TextCommandBarFlyoutFactory,winrt::Windows::UI::Xaml::Controls::ITextCommandBarFlyoutFactory>::CreateInstance()    Inconnu
    Windows.UI.Xaml.Controls.dll!winrt::impl::consume_Windows_UI_Xaml_Controls_IAppBarButtonFactory<struct winrt::Windows::UI::Xaml::Controls::IAppBarButtonFactory>::CreateInstance(struct winrt::Windows::Foundation::IInspectable const &,struct winrt::Windows::Foundation::IInspectable &)    Inconnu
    Windows.UI.Xaml.Controls.dll!Private::XamlRuntimeType::ActivateInstance()    Inconnu
    Windows.UI.Xaml.dll!Private::XamlTypeInfoProvider::ActivateInstance(unsigned short typeId, IInspectable * * instance) Ligne 241    C++
    Windows.UI.Xaml.dll!Internal::XamlType::ActivateInstance(IInspectable * * instance) Ligne 274    C++
    Windows.UI.Xaml.dll!DirectUI::ActivationAPI::ActivateInstance(const CClassInfo * pType, IInspectable * pOuter, IInspectable * * ppInstance) Ligne 42    C++
    [Cadre en ligne] Windows.UI.Xaml.dll!DirectUI::ActivationAPI::ActivateInstance(const CClassInfo *) Ligne 29    C++
    Windows.UI.Xaml.dll!DirectUI::XamlParserCallbacks::XamlManagedRuntimeRPInvokes_CreateInstance(XamlTypeToken tokType, XamlQualifiedObject * pqoInstance) Ligne 49    C++
    [Cadre en ligne] Windows.UI.Xaml.dll!CFxCallbacks::XamlManagedRuntimeRPInvokes_CreateInstance(XamlTypeToken) Ligne 1278    C++
    Windows.UI.Xaml.dll!XamlManagedRuntime::CreateInstance(const XamlTypeToken & inXamlType, std::shared_ptr<XamlQualifiedObject> & returnQo) Ligne 40    C++
    [Cadre en ligne] Windows.UI.Xaml.dll!XamlType::CreateInstance(std::shared_ptr<XamlQualifiedObject> &) Ligne 40    C++
    [Cadre en ligne] Windows.UI.Xaml.dll!ObjectWriterRuntime::CreateTypeImpl(const XamlLineInfo &) Ligne 76    C++
    Windows.UI.Xaml.dll!ObjectWriterCommonRuntime::CreateType(const XamlLineInfo & lineInfo, const std::shared_ptr<XamlType> & spType, const std::shared_ptr<ObjectWriterCallbacksDelegate> & spCallback, const std::shared_ptr<XamlQualifiedObject> & spRootObjectInstance, std::shared_ptr<XamlQualifiedObject> & spInstance) Ligne 116    C++
    Windows.UI.Xaml.dll!BinaryFormatObjectWriter::CreateInstanceFromType(const ObjectWriterNode & node) Ligne 461    C++
    Windows.UI.Xaml.dll!BinaryFormatObjectWriter::WriteNode(const ObjectWriterNode & inNode) Ligne 135    C++
    [Cadre en ligne] Windows.UI.Xaml.dll!CustomWriterRuntimeObjectCreator::RunObjectWriter(StreamOffsetToken) Ligne 139    C++
    Windows.UI.Xaml.dll!CustomWriterRuntimeObjectCreator::CreateInstance(StreamOffsetToken token, std::shared_ptr<CDependencyObject> * pResult, xref_ptr<CThemeResource> * resultAsThemeResource) Ligne 58    C++
    Windows.UI.Xaml.dll!CResourceDictionary2::LoadValueIfExists(const xstring_ptr & key, bool isImplicitKey, bool & keyFound, std::shared_ptr<CDependencyObject> & value) Ligne 50    C++
    [Cadre en ligne] Windows.UI.Xaml.dll!CResourceDictionary::TryLoadDeferredResource(const xstring_ptr & isImplicitKey, bool) Ligne 1856    C++
    Windows.UI.Xaml.dll!CResourceDictionary::FindDeferredResource(const xstring_ptr_view & key, bool keyIsType, CDependencyObject * * result) Ligne 491    C++
    Windows.UI.Xaml.dll!CResourceDictionary::GetKeyNoRefImpl(const xstring_ptr_view & strKey, bool keyIsType, Resources::LookupScope scope, CDependencyObject * * keyNoRef, xref_ptr<CResourceDictionary> * dictionaryReadFrom) Ligne 404    C++
    Windows.UI.Xaml.dll!CResourceDictionary::GetKeyNoRef(const xstring_ptr_view & strKey, CDependencyObject * * keyNoRef) Ligne 543    C++
    Windows.UI.Xaml.dll!CCoreServices::LookupThemeResource(const xstring_ptr_view & strKey, CDependencyObject * * ppValue) Ligne 2028    C++
    Windows.UI.Xaml.dll!GetTextControlFlyoutResource(CCoreServices * core, const xstring_ptr_view & themeResourceName, CDependencyObject * * ppFlyout) Ligne 188    C++
    [Cadre en ligne] Windows.UI.Xaml.dll!CDependencyProperty::GetDefaultTextControlContextFlyout(CCoreServices *) Ligne 205    C++
    Windows.UI.Xaml.dll!CDependencyProperty::GetDefaultValue(CCoreServices * core, CDependencyObject * referenceObject, const CClassInfo * type, CValue * defaultValue) Ligne 1172    C++
    Windows.UI.Xaml.dll!CDependencyObject::Enter(CDependencyObject * pNamescopeOwner, EnterParams params) Ligne 984    C++
    Windows.UI.Xaml.dll!CNoParentShareableDependencyObject::Enter(CDependencyObject * pNamescopeOwner, EnterParams params) Ligne 45    C++
    Windows.UI.Xaml.dll!56713188()    C++

    Regards.

    Eric

     
    Tuesday, January 14, 2020 2:38 PM
  • Hi,

    In addition, could you provide the os and sdk version? I will internally submit the problem to the relevant engineers to see if it can be reproduced, and reply here if there is any update.

    Thanks,

    Drake


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, January 15, 2020 2:18 AM
  • Hello,

    I think the issue is specific to my computer. I could not reproduce it with another one. No crash when last parameter for NCryptEnumKeys is set to 0.

    The host with the issue is a Windows 10 build 1903 (version 18362.535)  and SDK used it 10.0.17763.0. Same issue  with SDK 10.0.16299.0.

    Regards.

    Eric

    Wednesday, January 15, 2020 3:14 PM
  • Is the issue related to the fact I am debugging step-by-step the application with Visual Studio when a popup is displayed for smartcard pin code ? If I run the application from console,  the windows security is displayed and I have no exception.

    Regards.

    Eric

    Wednesday, January 15, 2020 3:54 PM
  • What I've tested is debugging step by step the application with visual studio, in windows 1903, sdk 10.0.17763.0. 

    So that's not the problem.

    Best Regards,

    Drake


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, January 16, 2020 1:32 AM