locked
[UWP] CryptoAquireContext Not Working in x86 "Input PIN Dialog" RRS feed

  • Question

  • Hello All,

    I have a UWP application that is designed to be strictly an enterprise application (meaning it is not intended to be sold on the Windows Store).  This UWP app invokes the advapi32's CryptoAquireContext function against a Smart Card.  The issue I'm having is that when calling this function in an x86 architecture, no "Enter your PIN" dialog is shown and a 87 - The Parameter is incorrect Win32 error is thrown.  We can get by with x64, but it would be helpful to understand why this call fails in x86.

    Below is a UWP UnitTest that shows this issue.  Any help would be appreciated.

    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using System;
    using System.Runtime.InteropServices;
    
    namespace CIS.UWP.UT {
    
    	[TestClass]
    	public class SmartCardTests {
    
    		public enum ProviderType : uint {
    			PROV_RSA_FULL = 1,
    			PROV_RSA_SIG = 2,
    			PROV_DSS = 3,
    			PROV_FORTEZZA = 4,
    			PROV_MS_EXCHANGE = 5,
    			PROV_SSL = 6,
    			PROV_RSA_SCHANNEL = 12,
    			PROV_DSS_DH = 13,
    			PROV_EC_ECDSA_SIG = 14,
    			PROV_EC_ECNRA_SIG = 15,
    			PROV_EC_ECDSA_FULL = 16,
    			PROV_EC_ECNRA_FULL = 17,
    			PROV_DH_SCHANNEL = 18,
    			PROV_SPYRUS_LYNKS = 20,
    			PROV_RNG = 21,
    			PROV_INTEL_SEC = 22,
    			PROV_REPLACE_OWF = 23,
    			PROV_RSA_AES = 24,
    		}
    
    		[Flags]
    		public enum ContextFlags : uint {
    			CRYPT_VERIFYCONTEXT = 0xF0000000,
    			CRYPT_NEWKEYSET = 0x00000008,
    			CRYPT_DELETEKEYSET = 0x00000010,
    			CRYPT_MACHINE_KEYSET = 0x00000020,
    			CRYPT_SILENT = 0x00000040,
    			CRYPT_DEFAULT_CONTAINER_OPTIONAL = 0x00000080,
    		}
    
    		internal const string MS_SCARD_PROV = "Microsoft Base Smart Card Crypto Provider";
    
    		[DllImport("advapi32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
    		public static extern bool CryptAcquireContext(ref IntPtr hProv, string pszContainer, string pszProvider, ProviderType dwProvType, ContextFlags dwFlags);
    
    		[TestMethod]
    		public void CreateNewKeySetOnSmartCard() {
    
    			var hProv = IntPtr.Zero;
    			var containerName = string.Empty;
    			string providerName = MS_SCARD_PROV;
    
    			bool bStatus = CryptAcquireContext(
    				ref hProv,
    				"",
    				providerName,
    				ProviderType.PROV_RSA_FULL,
    				ContextFlags.CRYPT_NEWKEYSET);
    
    			if (!bStatus) {
    				var error = new System.ComponentModel.Win32Exception(System.Runtime.InteropServices.Marshal.GetLastWin32Error());
    				Assert.Fail(error.Message);
    			}
    		}
    	}
    }


    - Rashad Rivera [omegusprime.com]


    • Edited by Rashad Rivera Wednesday, August 30, 2017 12:23 AM Added win32 error code
    Friday, August 25, 2017 2:44 AM

All replies

  • So am I to take the community silence to mean "this is a new bug?"

    - Rashad Rivera [omegusprime.com]

    Monday, August 28, 2017 4:26 AM
  • Hi Rashad Rivera

    Could you please tell me the error message it shows? Please refer the “Return value” part of CryptAcquireContext function. There is a form about error message and their description. You could check your error message there.

    Besides, CryptAcquireContext function is desktop apps only. Please refer the “Requirements” part of the document. I also searched it in Win32 and COM APIs for UWP apps but couldn’t find this API. I think maybe it is not supported in UWP.

    Best regards,

    Roy 


    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.

    Monday, August 28, 2017 5:45 AM
  • Roy,

    I know this is "a desktop app" function and it can be called from UWP via P/Invoke as the Unit Test I provided shows when executing in x64 architecture.  And before you ask, "Yes I have a valid use case for this as UWP does not support direct calls against smart-cards other than APDU calls; which are poorly documented.  

    Also, the error being returned in x86 is Win32 Error 87 - The Parameter is incorrect.  

    I think at this point I'm going to raise this up as a bug to Microsoft and let them explain why it does not work. 


    - Rashad Rivera [omegusprime.com]

    Wednesday, August 30, 2017 12:21 AM
  • Hi Rashad Rivera 

    Frist, to call C++ APIs in UWP app, please refer How to: Use Existing C++ Code in a Universal Windows Platform App.

    I have a workaround for you here. You could create a desktop app and the use desktop bridge to convert the desktop app to a UWP app. In the app, you could still use desktop APIs. But if there are some APIs that UWP won’t supported, you will not able to upload the package to the store and you could only Sideload it.

    Best regards,

    Roy


    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, August 30, 2017 9:17 AM