Exemple #1
0
 public static extern uint NCryptSetProperty
 (
     [param: In] NCryptHandleBase hObject,
     [param: In, MarshalAs(UnmanagedType.LPWStr)] string pszProperty,
     [param: In] IntPtr pbInput,
     [param: In, MarshalAs(UnmanagedType.U4)] uint cbInput,
     [param: In, MarshalAs(UnmanagedType.U4)] uint dwFlags
 );
Exemple #2
0
        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);
            }
        }
Exemple #3
0
        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);
            }
        }
Exemple #4
0
        public static void WriteEnum <TEnum>(NCryptHandleBase handle, string propertyName, TEnum value)
        {
            if (!typeof(TEnum).IsEnum)
            {
                throw new ArgumentException("Value must be an enumeration.", nameof(value));
            }
            var type = Enum.GetUnderlyingType(typeof(TEnum));

            if (type == typeof(uint))
            {
                WriteUInt32(handle, propertyName, (uint)(object)value);
            }
            else if (type == typeof(int))
            {
                WriteInt32(handle, propertyName, (int)(object)value);
            }
            else
            {
                throw new NotSupportedException();
            }
        }
        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);
            }
        }
Exemple #6
0
 public static void WriteUInt32(NCryptHandleBase handle, string propertyName, uint value)
 {
     WriteInt32(handle, propertyName, unchecked ((int)value));
 }
 public static string ReadStringUni(NCryptHandleBase handle, string propertyName)
 {
     return(Read(handle, propertyName, Marshal.PtrToStringUni));
 }
 public static int ReadInt32(NCryptHandleBase handle, string propertyName)
 {
     return(Read(handle, propertyName, Marshal.ReadInt32));
 }