public static byte[] GetCspPrivateKeySecurity(SafeCspHandle cspHandle) { byte[] buffer = null; var bufferSize = 0; // ReSharper disable once ExpressionIsAlwaysNull if (!Native.CryptGetProvParam(cspHandle, WindowsX509Native.CspProperties.SecurityDescriptor, buffer, ref bufferSize, WindowsX509Native.SecurityDesciptorParts.DACL_SECURITY_INFORMATION)) { // ReSharper disable once InconsistentNaming const int ERROR_MORE_DATA = 0x000000ea; var errorCode = Marshal.GetLastWin32Error(); if (errorCode != ERROR_MORE_DATA) { throw new CryptographicException(errorCode); } } buffer = new byte[bufferSize]; if (!Native.CryptGetProvParam(cspHandle, WindowsX509Native.CspProperties.SecurityDescriptor, buffer, ref bufferSize, WindowsX509Native.SecurityDesciptorParts.DACL_SECURITY_INFORMATION)) { throw new CryptographicException(Marshal.GetLastWin32Error()); } return(buffer); }
public SafeCspHandle Duplicate() { bool success = false; RuntimeHelpers.PrepareConstrainedRegions(); try { this.DangerousAddRef(ref success); IntPtr handle = this.DangerousGetHandle(); int hr = 0; SafeCspHandle safeCspHandle = new SafeCspHandle(); RuntimeHelpers.PrepareConstrainedRegions(); try { } finally { if (!SafeCspHandle.CryptContextAddRef(this, IntPtr.Zero, 0)) { hr = Marshal.GetLastWin32Error(); } else { safeCspHandle.SetHandle(handle); } } if (hr != 0) { safeCspHandle.Dispose(); throw new CryptographicException(hr); } return(safeCspHandle); } finally { if (success) { this.DangerousRelease(); } } }
internal static extern bool CryptAcquireCertificatePrivateKey(SafeCertContextHandle pCert, AcquireCertificateKeyOptions dwFlags, IntPtr pvReserved, // void * [Out] out SafeCspHandle phCryptProvOrNCryptKey, [Out] out int dwKeySpec, [Out, MarshalAs(UnmanagedType.Bool)] out bool pfCallerFreeProvOrNCryptKey);
public static extern bool CryptSetProvParam(SafeCspHandle hProv, CspProperties dwParam, [In] byte[] pbData, SecurityDesciptorParts dwFlags);
public static extern bool CryptGetProvParam(SafeCspHandle hProv, CspProperties dwParam, [Out, MarshalAs(UnmanagedType.LPArray)] byte[] pbData, ref int pdwDataLen, SecurityDesciptorParts dwFlags);
protected override bool ReleaseHandle() { return(SafeCspHandle.CryptReleaseContext(this.handle, 0)); }
private static extern bool CryptContextAddRef(SafeCspHandle hProv, IntPtr pdwReserved, int dwFlags);