/// <summary> /// Read a parameter using VERIFY_CONTEXT to read from the device being targeted by _parameters /// </summary> private object ReadDeviceParameterVerifyContext(CapiHelper.ClrPropertyId keyParam) { CspParameters parameters = new CspParameters(_parameters); // We're asking questions of the device container, the only flag that makes sense is Machine vs User. parameters.Flags &= CspProviderFlags.UseMachineKeyStore; // In order to ask about the device, instead of a key, we need to ensure that no key is named. parameters.KeyContainerName = null; const uint OpenDeviceFlags = (uint)Interop.Advapi32.CryptAcquireContextFlags.CRYPT_VERIFYCONTEXT; SafeProvHandle safeProvHandle; int hr = CapiHelper.OpenCSP(parameters, OpenDeviceFlags, out safeProvHandle); using (safeProvHandle) { if (hr != CapiHelper.S_OK) { throw new CryptographicException(SR.Cryptography_CSP_NotFound); } object retVal = CapiHelper.GetProviderParameter(safeProvHandle, parameters.KeyNumber, keyParam); return(retVal); } }
/// <summary> /// Read a parameter from the current key using CRYPT_SILENT, to avoid any potential UI prompts. /// </summary> private object?ReadKeyParameterSilent(CapiHelper.ClrPropertyId keyParam, bool throwOnNotFound = true) { const uint SilentFlags = (uint)Interop.Advapi32.CryptAcquireContextFlags.CRYPT_SILENT; SafeProvHandle safeProvHandle; int hr = CapiHelper.OpenCSP(_parameters, SilentFlags, out safeProvHandle); using (safeProvHandle) { if (hr != CapiHelper.S_OK) { if (throwOnNotFound) { throw new CryptographicException(SR.Cryptography_CSP_NotFound); } return(null); } object retVal = CapiHelper.GetProviderParameter(safeProvHandle, _parameters.KeyNumber, keyParam); return(retVal); } }