internal static void ImportCspBlobHelper(CspAlgorithmType keyType, byte[] keyBlob, bool publicOnly, ref CspParameters parameters, bool randomKeyContainer, ref SafeProvHandle safeProvHandle, ref SafeKeyHandle safeKeyHandle) { if (safeKeyHandle != null && !safeKeyHandle.IsClosed) { safeKeyHandle.Dispose(); } safeKeyHandle = SafeKeyHandle.InvalidHandle; if (publicOnly) { parameters.KeyNumber = Utils._ImportCspBlob(keyBlob, keyType == CspAlgorithmType.Dss ? Utils.StaticDssProvHandle : Utils.StaticProvHandle, CspProviderFlags.NoFlags, ref safeKeyHandle); } else { if (!CompatibilitySwitches.IsAppEarlierThanWindowsPhone8) { KeyContainerPermission containerPermission = new KeyContainerPermission(KeyContainerPermissionFlags.NoFlags); KeyContainerPermissionAccessEntry accessEntry = new KeyContainerPermissionAccessEntry(parameters, KeyContainerPermissionFlags.Import); containerPermission.AccessEntries.Add(accessEntry); containerPermission.Demand(); } if (safeProvHandle == null) { safeProvHandle = Utils.CreateProvHandle(parameters, randomKeyContainer); } parameters.KeyNumber = Utils._ImportCspBlob(keyBlob, safeProvHandle, parameters.Flags, ref safeKeyHandle); } }
[System.Security.SecuritySafeCritical] // auto-generated public override void ImportParameters(DSAParameters parameters) { DSACspObject dsaCspObject = DSAStructToObject(parameters); // Free the current key handle if (_safeKeyHandle != null && !_safeKeyHandle.IsClosed) { _safeKeyHandle.Dispose(); } _safeKeyHandle = SafeKeyHandle.InvalidHandle; if (IsPublic(parameters)) { // Use our CRYPT_VERIFYCONTEXT handle, CRYPT_EXPORTABLE is not applicable to public only keys, so pass false Utils._ImportKey(Utils.StaticDssProvHandle, Constants.CALG_DSS_SIGN, (CspProviderFlags)0, dsaCspObject, ref _safeKeyHandle); } else { KeyContainerPermission kp = new KeyContainerPermission(KeyContainerPermissionFlags.NoFlags); KeyContainerPermissionAccessEntry entry = new KeyContainerPermissionAccessEntry(_parameters, KeyContainerPermissionFlags.Import); kp.AccessEntries.Add(entry); kp.Demand(); if (_safeProvHandle == null) { _safeProvHandle = Utils.CreateProvHandle(_parameters, _randomKeyContainer); } // Now, import the key into the CSP; _ImportKey will check for failures. Utils._ImportKey(_safeProvHandle, Constants.CALG_DSS_SIGN, _parameters.Flags, dsaCspObject, ref _safeKeyHandle); } }
public override void ImportParameters(DSAParameters parameters) { DSACspObject cspObject = DSAStructToObject(parameters); if ((this._safeKeyHandle != null) && !this._safeKeyHandle.IsClosed) { this._safeKeyHandle.Dispose(); } this._safeKeyHandle = SafeKeyHandle.InvalidHandle; if (IsPublic(parameters)) { Utils._ImportKey(Utils.StaticDssProvHandle, 0x2200, CspProviderFlags.NoFlags, cspObject, ref this._safeKeyHandle); } else { KeyContainerPermission permission = new KeyContainerPermission(KeyContainerPermissionFlags.NoFlags); KeyContainerPermissionAccessEntry accessEntry = new KeyContainerPermissionAccessEntry(this._parameters, KeyContainerPermissionFlags.Import); permission.AccessEntries.Add(accessEntry); permission.Demand(); if (this._safeProvHandle == null) { this._safeProvHandle = Utils.CreateProvHandle(this._parameters, this._randomKeyContainer); } Utils._ImportKey(this._safeProvHandle, 0x2200, this._parameters.Flags, cspObject, ref this._safeKeyHandle); } }
public override void ImportParameters(RSAParameters parameters) { if (this._safeKeyHandle != null && !this._safeKeyHandle.IsClosed) { this._safeKeyHandle.Dispose(); this._safeKeyHandle = (SafeKeyHandle)null; } RSACspObject @object = RSACryptoServiceProvider.RSAStructToObject(parameters); this._safeKeyHandle = SafeKeyHandle.InvalidHandle; if (RSACryptoServiceProvider.IsPublic(parameters)) { Utils._ImportKey(Utils.StaticProvHandle, 41984, CspProviderFlags.NoFlags, (object)@object, ref this._safeKeyHandle); } else { if (!CompatibilitySwitches.IsAppEarlierThanWindowsPhone8) { KeyContainerPermission containerPermission = new KeyContainerPermission(KeyContainerPermissionFlags.NoFlags); KeyContainerPermissionAccessEntry accessEntry = new KeyContainerPermissionAccessEntry(this._parameters, KeyContainerPermissionFlags.Import); containerPermission.AccessEntries.Add(accessEntry); containerPermission.Demand(); } if (this._safeProvHandle == null) { this._safeProvHandle = Utils.CreateProvHandle(this._parameters, this._randomKeyContainer); } Utils._ImportKey(this._safeProvHandle, 41984, this._parameters.Flags, (object)@object, ref this._safeKeyHandle); } }
internal unsafe static void GetKeyPairHelper(CspAlgorithmType keyType, CspParameters parameters, bool randomKeyContainer, int dwKeySize, ref SafeProvHandle safeProvHandle, ref SafeKeyHandle safeKeyHandle) { SafeProvHandle safeProvHandle2 = Utils.CreateProvHandle(parameters, randomKeyContainer); if (parameters.CryptoKeySecurity != null) { KeyContainerPermission keyContainerPermission = new KeyContainerPermission(KeyContainerPermissionFlags.NoFlags); KeyContainerPermissionAccessEntry accessEntry = new KeyContainerPermissionAccessEntry(parameters, KeyContainerPermissionFlags.ChangeAcl); keyContainerPermission.AccessEntries.Add(accessEntry); keyContainerPermission.Demand(); Utils.SetKeySetSecurityInfo(safeProvHandle2, parameters.CryptoKeySecurity, parameters.CryptoKeySecurity.ChangedAccessControlSections); } if (parameters.ParentWindowHandle != IntPtr.Zero) { IntPtr parentWindowHandle = parameters.ParentWindowHandle; IntPtr pbData = parentWindowHandle; if (!AppContextSwitches.DoNotAddrOfCspParentWindowHandle) { pbData = new IntPtr((void *)(&parentWindowHandle)); } Utils.SetProviderParameter(safeProvHandle2, parameters.KeyNumber, 10U, pbData); } else if (parameters.KeyPassword != null) { IntPtr intPtr = Marshal.SecureStringToCoTaskMemAnsi(parameters.KeyPassword); try { Utils.SetProviderParameter(safeProvHandle2, parameters.KeyNumber, 11U, intPtr); } finally { if (intPtr != IntPtr.Zero) { Marshal.ZeroFreeCoTaskMemAnsi(intPtr); } } } safeProvHandle = safeProvHandle2; SafeKeyHandle invalidHandle = SafeKeyHandle.InvalidHandle; int num = Utils._GetUserKey(safeProvHandle, parameters.KeyNumber, ref invalidHandle); if (num != 0) { if ((parameters.Flags & CspProviderFlags.UseExistingKey) != CspProviderFlags.NoFlags || num != -2146893811) { throw new CryptographicException(num); } Utils._GenerateKey(safeProvHandle, parameters.KeyNumber, parameters.Flags, dwKeySize, ref invalidHandle); } byte[] array = Utils._GetKeyParameter(invalidHandle, 9U); int num2 = (int)array[0] | (int)array[1] << 8 | (int)array[2] << 16 | (int)array[3] << 24; if ((keyType == CspAlgorithmType.Rsa && num2 != 41984 && num2 != 9216) || (keyType == CspAlgorithmType.Dss && num2 != 8704)) { invalidHandle.Dispose(); throw new CryptographicException(Environment.GetResourceString("Cryptography_CSP_WrongKeySpec")); } safeKeyHandle = invalidHandle; }
internal static void GetKeyPairHelper(CspAlgorithmType keyType, CspParameters parameters, bool randomKeyContainer, int dwKeySize, ref SafeProvHandle safeProvHandle, ref SafeKeyHandle safeKeyHandle) { SafeProvHandle provHandle = Utils.CreateProvHandle(parameters, randomKeyContainer); if (parameters.CryptoKeySecurity != null) { KeyContainerPermission containerPermission = new KeyContainerPermission(KeyContainerPermissionFlags.NoFlags); KeyContainerPermissionAccessEntry accessEntry = new KeyContainerPermissionAccessEntry(parameters, KeyContainerPermissionFlags.ChangeAcl); containerPermission.AccessEntries.Add(accessEntry); containerPermission.Demand(); Utils.SetKeySetSecurityInfo(provHandle, parameters.CryptoKeySecurity, parameters.CryptoKeySecurity.ChangedAccessControlSections); } if (parameters.ParentWindowHandle != IntPtr.Zero) { Utils.SetProviderParameter(provHandle, parameters.KeyNumber, 10U, parameters.ParentWindowHandle); } else if (parameters.KeyPassword != null) { IntPtr coTaskMemAnsi = Marshal.SecureStringToCoTaskMemAnsi(parameters.KeyPassword); try { Utils.SetProviderParameter(provHandle, parameters.KeyNumber, 11U, coTaskMemAnsi); } finally { if (coTaskMemAnsi != IntPtr.Zero) { Marshal.ZeroFreeCoTaskMemAnsi(coTaskMemAnsi); } } } safeProvHandle = provHandle; SafeKeyHandle invalidHandle = SafeKeyHandle.InvalidHandle; int userKey = Utils._GetUserKey(safeProvHandle, parameters.KeyNumber, ref invalidHandle); if (userKey != 0) { if ((parameters.Flags & CspProviderFlags.UseExistingKey) != CspProviderFlags.NoFlags || userKey != -2146893811) { throw new CryptographicException(userKey); } Utils._GenerateKey(safeProvHandle, parameters.KeyNumber, parameters.Flags, dwKeySize, ref invalidHandle); } byte[] keyParameter = Utils._GetKeyParameter(invalidHandle, 9U); int num = (int)keyParameter[0] | (int)keyParameter[1] << 8 | (int)keyParameter[2] << 16 | (int)keyParameter[3] << 24; if (keyType == CspAlgorithmType.Rsa && num != 41984 && num != 9216 || keyType == CspAlgorithmType.Dss && num != 8704) { invalidHandle.Dispose(); throw new CryptographicException(Environment.GetResourceString("Cryptography_CSP_WrongKeySpec")); } safeKeyHandle = invalidHandle; }