private NCryptStorageProvider(string name) { var result = NCrypt.NCryptOpenStorageProvider(out _handle, name, 0u); if (result != SECURITY_STATUS.ERROR_SUCCESS) { throw new Win32Exception(unchecked ((int)result), "Failed to open the CNG storage provider."); } }
public static void WriteStringUni(NCryptHandleBase handle, string propertyName, string value) { var ptr = Marshal.StringToHGlobalUni(value); try { if (NCrypt.NCryptSetProperty(handle, propertyName, ptr, (uint)value.Length * 2, 0u) != SECURITY_STATUS.ERROR_SUCCESS) { throw new InvalidOperationException("Unable to set property."); } } finally { Marshal.FreeHGlobal(ptr); } }
public static void WriteInt32(NCryptHandleBase handle, string propertyName, int value) { var size = Marshal.SizeOf(typeof(int)); var ptr = Marshal.AllocHGlobal(size); Marshal.WriteInt32(ptr, value); try { if (NCrypt.NCryptSetProperty(handle, propertyName, ptr, (uint)size, 0u) != SECURITY_STATUS.ERROR_SUCCESS) { throw new InvalidOperationException("Unable to set property."); } } finally { Marshal.FreeHGlobal(ptr); } }
public static T Read <T>(NCryptHandleBase handle, string propertyName, Func <IntPtr, T> convert) { var buffer = IntPtr.Zero; uint size; if (NCrypt.NCryptGetProperty(handle, propertyName, buffer, 0, out size, 0u) != SECURITY_STATUS.ERROR_SUCCESS) { throw new InvalidOperationException("Unable to query property."); } buffer = Marshal.AllocHGlobal((int)size); try { if (NCrypt.NCryptGetProperty(handle, propertyName, buffer, size, out size, 0u) != SECURITY_STATUS.ERROR_SUCCESS) { throw new InvalidOperationException("Unable to query property."); } return(convert(buffer)); } finally { Marshal.FreeHGlobal(buffer); } }