/// <summary> /// Creates mechanism of given type with structure as parameter /// </summary> /// <param name="mechanism">Mechanism type</param> /// <param name="parameterStructure">Structure with mechanism parameters</param> /// <returns>Mechanism of given type with structure as parameter</returns> public static CK_MECHANISM CreateMechanism(CKM mechanism, object parameterStructure) { if (parameterStructure == null) throw new ArgumentNullException("parameterStructure"); return CreateMechanism(Convert.ToUInt64((uint)mechanism), parameterStructure); }
public void _01_BasicMechanismListAndInfoTest() { if (Platform.UnmanagedLongSize != 4 || Platform.StructPackingSize != 0) Assert.Inconclusive("Test cannot be executed on this platform"); CKR rv = CKR.CKR_OK; using (Pkcs11 pkcs11 = new Pkcs11(Settings.Pkcs11LibraryPath)) { rv = pkcs11.C_Initialize(Settings.InitArgs40); if ((rv != CKR.CKR_OK) && (rv != CKR.CKR_CRYPTOKI_ALREADY_INITIALIZED)) Assert.Fail(rv.ToString()); // Find first slot with token present uint slotId = Helpers.GetUsableSlot(pkcs11); // Get number of supported mechanisms in first call uint mechanismCount = 0; rv = pkcs11.C_GetMechanismList(slotId, null, ref mechanismCount); if (rv != CKR.CKR_OK) Assert.Fail(rv.ToString()); Assert.IsTrue(mechanismCount > 0); // Allocate array for supported mechanisms CKM[] mechanismList = new CKM[mechanismCount]; // Get supported mechanisms in second call rv = pkcs11.C_GetMechanismList(slotId, mechanismList, ref mechanismCount); if (rv != CKR.CKR_OK) Assert.Fail(rv.ToString()); // Analyze first supported mechanism CK_MECHANISM_INFO mechanismInfo = new CK_MECHANISM_INFO(); rv = pkcs11.C_GetMechanismInfo(slotId, mechanismList[0], ref mechanismInfo); if (rv != CKR.CKR_OK) Assert.Fail(rv.ToString()); // Do something interesting with mechanism info rv = pkcs11.C_Finalize(IntPtr.Zero); if (rv != CKR.CKR_OK) Assert.Fail(rv.ToString()); } }
/// <summary> /// Creates attribute of given type with mechanism array value /// </summary> /// <param name="type">Attribute type</param> /// <param name="value">Attribute value</param> /// <returns>Attribute of given type with mechanism array value</returns> public static CK_ATTRIBUTE CreateAttribute(uint type, CKM[] value) { uint[] uintArray = null; if (value != null) { uintArray = new uint[value.Length]; for (int i = 0; i < value.Length; i++) uintArray[i] = (uint)value[i]; } return CreateAttribute(type, uintArray); }
/// <summary> /// Obtains a list of mechanism types supported by a token /// </summary> /// <param name="slotId">The ID of the token's slot</param> /// <param name="mechanismList"> /// If set to null then the number of mechanisms is returned in "count" parameter, without actually returning a list of mechanisms. /// If not set to null then "count" parameter must contain the lenght of mechanismList array and mechanism list is returned in "mechanismList" parameter. /// </param> /// <param name="count">Location that receives the number of mechanisms</param> /// <returns>CKR_BUFFER_TOO_SMALL, CKR_CRYPTOKI_NOT_INITIALIZED, CKR_DEVICE_ERROR, CKR_DEVICE_MEMORY, CKR_DEVICE_REMOVED, CKR_FUNCTION_FAILED, CKR_GENERAL_ERROR, CKR_HOST_MEMORY, CKR_OK, CKR_SLOT_ID_INVALID, CKR_TOKEN_NOT_PRESENT, CKR_TOKEN_NOT_RECOGNIZED, CKR_ARGUMENTS_BAD</returns> public CKR C_GetMechanismList(uint slotId, CKM[] mechanismList, ref uint count) { if (this._disposed) throw new ObjectDisposedException(this.GetType().FullName); uint[] uintList = null; if (mechanismList != null) uintList = new uint[mechanismList.Length]; uint rv = _delegates.C_GetMechanismList(slotId, uintList, ref count); if (mechanismList != null) { for (int i = 0; i < mechanismList.Length; i++) mechanismList[i] = (CKM)uintList[i]; } return (CKR)rv; }
/// <summary> /// Creates attribute of given type with mechanism array value /// </summary> /// <param name="type">Attribute type</param> /// <param name="value">Attribute value</param> /// <returns>Attribute of given type with mechanism array value</returns> public static CK_ATTRIBUTE CreateAttribute(ulong type, CKM[] value) { ulong[] ulongArray = null; if (value != null) { ulongArray = new ulong[value.Length]; for (int i = 0; i < value.Length; i++) ulongArray[i] = Convert.ToUInt64((uint)value[i]); } return CreateAttribute(type, ulongArray); }
/// <summary> /// Creates mechanism of given type with byte array parameter /// </summary> /// <param name="type">Mechanism type</param> /// <param name="parameter">Mechanism parameter</param> public Mechanism(CKM type, byte[] parameter) { if (Platform.UnmanagedLongSize == 4) { if (Platform.StructPackingSize == 0) _mechanism40 = new HighLevelAPI40.Mechanism(Convert.ToUInt32(type), parameter); else _mechanism41 = new HighLevelAPI41.Mechanism(Convert.ToUInt32(type), parameter); } else { if (Platform.StructPackingSize == 0) _mechanism80 = new HighLevelAPI80.Mechanism(type, parameter); else _mechanism81 = new HighLevelAPI81.Mechanism(type, parameter); } }
/// <summary> /// Creates mechanism of given type with no parameter /// </summary> /// <param name="mechanism">Mechanism type</param> /// <returns>Mechanism of given type with no parameter</returns> public static CK_MECHANISM CreateMechanism(CKM mechanism) { return CreateMechanism((uint)mechanism); }
/// <summary> /// Creates mechanism of given type with byte array parameter /// </summary> /// <param name="mechanism">Mechanism type</param> /// <param name="parameter">Mechanism parameter</param> /// <returns>Mechanism of given type with byte array parameter</returns> public static CK_MECHANISM CreateMechanism(CKM mechanism, byte[] parameter) { return CreateMechanism(Convert.ToUInt64((uint)mechanism), parameter); }
/// <summary> /// Converts CKM to NativeULong /// </summary> /// <param name="value">CKM that should be converted</param> /// <returns>NativeULong with value from CKM</returns> public static NativeULong ConvertFromCKM(CKM value) { return(Convert.ToUInt64(value)); }
/// <summary> /// Creates mechanism of given type with no parameter /// </summary> /// <param name="mechanism">Mechanism type</param> /// <returns>Mechanism of given type with no parameter</returns> public static CK_MECHANISM CreateMechanism(CKM mechanism) { return(CreateMechanism((uint)mechanism)); }
/// <summary> /// Creates mechanism of given type with byte array parameter /// </summary> /// <param name="mechanism">Mechanism type</param> /// <param name="parameter">Mechanism parameter</param> /// <returns>Mechanism of given type with byte array parameter</returns> public static CK_MECHANISM CreateMechanism(CKM mechanism, byte[] parameter) { return(CreateMechanism((uint)mechanism, parameter)); }
/// <summary> /// Creates mechanism of given type with no parameter /// </summary> /// <param name="mechanism">Mechanism type</param> /// <returns>Mechanism of given type with no parameter</returns> public static CK_MECHANISM CreateMechanism(CKM mechanism) { return(CreateMechanism(NativeLongUtils.ConvertFromCKM(mechanism))); }
/// <summary> /// Creates mechanism of given type with byte array parameter /// </summary> /// <param name="mechanism">Mechanism type</param> /// <param name="parameter">Mechanism parameter</param> /// <returns>Mechanism of given type with byte array parameter</returns> public static CK_MECHANISM CreateMechanism(CKM mechanism, byte[] parameter) { return(CreateMechanism(NativeLongUtils.ConvertFromCKM(mechanism), parameter)); }
/// <summary> /// Creates mechanism of given type with byte array parameter /// </summary> /// <param name="mechanism">Mechanism type</param> /// <param name="parameter">Mechanism parameter</param> /// <returns>Mechanism of given type with byte array parameter</returns> public static CK_MECHANISM CreateMechanism(CKM mechanism, byte[] parameter) { return(CreateMechanism(Convert.ToUInt64((uint)mechanism), parameter)); }
/// <summary> /// Creates mechanism of given type with no parameter /// </summary> /// <param name="mechanism">Mechanism type</param> /// <returns>Mechanism of given type with no parameter</returns> public static CK_MECHANISM CreateMechanism(CKM mechanism) { return(CreateMechanism(Convert.ToUInt64((uint)mechanism))); }
public MechanismTypeAttribute(CKA type, CKM mechanismType) : base((uint)type) { MechanismType = mechanismType; }
/// <summary> /// Creates mechanism of given type with byte array parameter /// </summary> /// <param name="type">Mechanism type</param> /// <param name="parameter">Mechanism parameter</param> public Mechanism(CKM type, byte[] parameter) { _ckMechanism = CkmUtils.CreateMechanism(type, parameter); }
/// <summary> /// Obtains information about a particular mechanism possibly supported by a token /// </summary> /// <param name="mechanism">Mechanism</param> /// <returns>Information about mechanism</returns> public MechanismInfo GetMechanismInfo(CKM mechanism) { CK_MECHANISM_INFO mechanismInfo = new CK_MECHANISM_INFO(); CKR rv = _p11.C_GetMechanismInfo(_slotId, mechanism, ref mechanismInfo); if (rv != CKR.CKR_OK) throw new Pkcs11Exception("C_GetMechanismInfo", rv); return new MechanismInfo(mechanism, mechanismInfo); }
/// <summary> /// Creates mechanism of given type with no parameter /// </summary> /// <param name="type">Mechanism type</param> public Mechanism(CKM type) { _ckMechanism = CkmUtils.CreateMechanism(type); }
/// <summary> /// Obtains a list of mechanism types supported by a token /// </summary> /// <param name="slotId">The ID of the token's slot</param> /// <param name="mechanismList"> /// If set to null then the number of mechanisms is returned in "count" parameter, without actually returning a list of mechanisms. /// If not set to null then "count" parameter must contain the lenght of mechanismList array and mechanism list is returned in "mechanismList" parameter. /// </param> /// <param name="count">Location that receives the number of mechanisms</param> /// <returns>CKR_BUFFER_TOO_SMALL, CKR_CRYPTOKI_NOT_INITIALIZED, CKR_DEVICE_ERROR, CKR_DEVICE_MEMORY, CKR_DEVICE_REMOVED, CKR_FUNCTION_FAILED, CKR_GENERAL_ERROR, CKR_HOST_MEMORY, CKR_OK, CKR_SLOT_ID_INVALID, CKR_TOKEN_NOT_PRESENT, CKR_TOKEN_NOT_RECOGNIZED, CKR_ARGUMENTS_BAD</returns> public CKR C_GetMechanismList(ulong slotId, CKM[] mechanismList, ref ulong count) { if (this._disposed) throw new ObjectDisposedException(this.GetType().FullName); ulong[] ulongList = null; if (mechanismList != null) ulongList = new ulong[mechanismList.Length]; ulong rv = _delegates.C_GetMechanismList(slotId, ulongList, ref count); if (mechanismList != null) { for (int i = 0; i < mechanismList.Length; i++) mechanismList[i] = (CKM)Convert.ToUInt32(ulongList[i]); } return (CKR)Convert.ToUInt32(rv); }
/// <summary> /// Creates mechanism of given type with object parameter /// </summary> /// <param name="type">Mechanism type</param> /// <param name="parameter">Mechanism parameter</param> /// <returns>Mechanism and its parameter</returns> public IMechanism Create(CKM type, IMechanismParams parameter) { return(new Mechanism(type, parameter)); }
/// <summary> /// Obtain a parameter class here. /// </summary> /// <param name="ckm"></param> /// <returns></returns> public static Parameters GetParameters(CKM ckm) { Parameters lParameters = new Parameters(); return(lParameters); }
/// <summary> /// Obtains information about a particular mechanism possibly supported by a token /// </summary> /// <param name="mechanism">Mechanism</param> /// <returns>Information about mechanism</returns> public MechanismInfo GetMechanismInfo(CKM mechanism) { if (Platform.UnmanagedLongSize == 4) return (Platform.StructPackingSize == 0) ? new MechanismInfo(_slot40.GetMechanismInfo(mechanism)) : new MechanismInfo(_slot41.GetMechanismInfo(mechanism)); else return (Platform.StructPackingSize == 0) ? new MechanismInfo(_slot80.GetMechanismInfo(mechanism)) : new MechanismInfo(_slot81.GetMechanismInfo(mechanism)); }
/// <summary> /// Creates mechanism of given type with byte array parameter /// </summary> /// <param name="type">Mechanism type</param> /// <param name="parameter">Mechanism parameter</param> /// <returns>Mechanism and its parameter</returns> public IMechanism Create(CKM type, byte[] parameter) { return(_factory.Create(type, parameter)); }
/// <summary> /// Obtains information about a particular mechanism possibly supported by a token /// </summary> /// <param name="slotId">The ID of the token's slot</param> /// <param name="type">The type of mechanism</param> /// <param name="info">Structure that receives the mechanism information</param> /// <returns>CKR_CRYPTOKI_NOT_INITIALIZED, CKR_DEVICE_ERROR, CKR_DEVICE_MEMORY, CKR_DEVICE_REMOVED, CKR_FUNCTION_FAILED, CKR_GENERAL_ERROR, CKR_HOST_MEMORY, CKR_MECHANISM_INVALID, CKR_OK, CKR_SLOT_ID_INVALID, CKR_TOKEN_NOT_PRESENT, CKR_TOKEN_NOT_RECOGNIZED, CKR_ARGUMENTS_BAD</returns> public CKR C_GetMechanismInfo(ulong slotId, CKM type, ref CK_MECHANISM_INFO info) { if (this._disposed) throw new ObjectDisposedException(this.GetType().FullName); ulong rv = _delegates.C_GetMechanismInfo(slotId, Convert.ToUInt64(type), ref info); return (CKR)Convert.ToUInt32(rv); }
/// <summary> /// Creates mechanism of given type with object parameter /// </summary> /// <param name="type">Mechanism type</param> /// <param name="parameter">Mechanism parameter</param> /// <returns>Mechanism and its parameter</returns> public IMechanism Create(CKM type, IMechanismParams parameter) { return(_factory.Create(type, parameter)); }
/// <summary> /// Creates mechanism of given type with object parameter /// </summary> /// <param name="type">Mechanism type</param> /// <param name="parameter">Mechanism parameter</param> public Mechanism(CKM type, IMechanismParams parameter) { if (parameter == null) throw new ArgumentNullException("parameter"); if (Platform.UnmanagedLongSize == 4) { if (Platform.StructPackingSize == 0) _mechanism40 = new HighLevelAPI40.Mechanism(Convert.ToUInt32(type), parameter); else _mechanism41 = new HighLevelAPI41.Mechanism(Convert.ToUInt32(type), parameter); } else { if (Platform.StructPackingSize == 0) _mechanism80 = new HighLevelAPI80.Mechanism(type, parameter); else _mechanism81 = new HighLevelAPI81.Mechanism(type, parameter); } }
/// <summary> /// Creates mechanism of given type with no parameter /// </summary> /// <param name="type">Mechanism type</param> /// <returns>Mechanism with no parameter</returns> public IMechanism Create(CKM type) { return(_factory.Create(type)); }
/// <summary> /// Creates mechanism of given type with byte array parameter /// </summary> /// <param name="mechanism">Mechanism type</param> /// <param name="parameter">Mechanism parameter</param> /// <returns>Mechanism of given type with byte array parameter</returns> public static CK_MECHANISM CreateMechanism(CKM mechanism, byte[] parameter) { return CreateMechanism((uint)mechanism, parameter); }
/// <summary> /// Obtains information about a particular mechanism possibly supported by a token /// </summary> /// <param name="slotId">The ID of the token's slot</param> /// <param name="type">The type of mechanism</param> /// <param name="info">Structure that receives the mechanism information</param> /// <returns>CKR_CRYPTOKI_NOT_INITIALIZED, CKR_DEVICE_ERROR, CKR_DEVICE_MEMORY, CKR_DEVICE_REMOVED, CKR_FUNCTION_FAILED, CKR_GENERAL_ERROR, CKR_HOST_MEMORY, CKR_MECHANISM_INVALID, CKR_OK, CKR_SLOT_ID_INVALID, CKR_TOKEN_NOT_PRESENT, CKR_TOKEN_NOT_RECOGNIZED, CKR_ARGUMENTS_BAD</returns> public CKR C_GetMechanismInfo(uint slotId, CKM type, ref CK_MECHANISM_INFO info) { if (this._disposed) throw new ObjectDisposedException(this.GetType().FullName); uint rv = _delegates.C_GetMechanismInfo(slotId, (uint)type, ref info); return (CKR)rv; }
/// <summary> /// Reads value of attribute and returns it as mechanism array /// </summary> /// <param name="attribute">Attribute whose value should be read</param> /// <param name="value">Location that receives attribute value</param> public static void ConvertValue(ref CK_ATTRIBUTE attribute, out CKM[] value) { ulong[] ulongArray = null; ConvertValue(ref attribute, out ulongArray); CKM[] ckmArray = null; if (ulongArray != null) { ckmArray = new CKM[ulongArray.Length]; for (int i = 0; i < ulongArray.Length; i++) ckmArray[i] = (CKM)Convert.ToUInt32(ulongArray[i]); } value = ckmArray; }
/// <summary> /// Creates mechanism of given type with no parameter /// </summary> /// <param name="mechanism">Mechanism type</param> /// <returns>Mechanism of given type with no parameter</returns> public static CK_MECHANISM CreateMechanism(CKM mechanism) { return(CreateMechanism(ConvertUtils.UInt64FromCKM(mechanism))); }
/// <summary> /// Reads value of attribute and returns it as mechanism array /// </summary> /// <param name="attribute">Attribute whose value should be read</param> /// <param name="value">Location that receives attribute value</param> public static void ConvertValue(ref CK_ATTRIBUTE attribute, out CKM[] value) { uint[] uintArray = null; ConvertValue(ref attribute, out uintArray); CKM[] ckmArray = null; if (uintArray != null) { ckmArray = new CKM[uintArray.Length]; for (int i = 0; i < uintArray.Length; i++) ckmArray[i] = (CKM)uintArray[i]; } value = ckmArray; }
/// <summary> /// Creates mechanism of given type with byte array parameter /// </summary> /// <param name="mechanism">Mechanism type</param> /// <param name="parameter">Mechanism parameter</param> /// <returns>Mechanism of given type with byte array parameter</returns> public static CK_MECHANISM CreateMechanism(CKM mechanism, byte[] parameter) { return(CreateMechanism(ConvertUtils.UInt64FromCKM(mechanism), parameter)); }
/// <summary> /// Creates mechanism of given type with object parameter /// </summary> /// <param name="type">Mechanism type</param> /// <param name="parameter">Mechanism parameter</param> public Mechanism(CKM type, IMechanismParams parameter) { if (parameter == null) throw new ArgumentNullException("parameter"); // Keep reference to parameter so GC will not free it while mechanism exists _mechanismParams = parameter; object lowLevelParams = _mechanismParams.ToMarshalableStructure(); _ckMechanism = CkmUtils.CreateMechanism(type, lowLevelParams); }
public void _09_MechanismArrayAttributeTest() { if (Platform.UnmanagedLongSize != 4 || Platform.StructPackingSize != 1) Assert.Inconclusive("Test cannot be executed on this platform"); CKM[] originalValue = new CKM[2]; originalValue[0] = CKM.CKM_RSA_PKCS; originalValue[1] = CKM.CKM_AES_CBC; // Create attribute with mechanism array value CK_ATTRIBUTE attr = CkaUtils.CreateAttribute(CKA.CKA_ALLOWED_MECHANISMS, originalValue); Assert.IsTrue(attr.type == (uint)CKA.CKA_ALLOWED_MECHANISMS); Assert.IsTrue(attr.value != IntPtr.Zero); Assert.IsTrue(attr.valueLen == (UnmanagedMemory.SizeOf(typeof(uint)) * originalValue.Length)); CKM[] recoveredValue = null; // Read the value of attribute CkaUtils.ConvertValue(ref attr, out recoveredValue); Assert.IsTrue(originalValue.Length == recoveredValue.Length); for (int i = 0; i < recoveredValue.Length; i++) { Assert.IsTrue(originalValue[i] == recoveredValue[i]); } // Free attribute value UnmanagedMemory.Free(ref attr.value); attr.valueLen = 0; Assert.IsTrue(attr.type == (uint)CKA.CKA_ALLOWED_MECHANISMS); Assert.IsTrue(attr.value == IntPtr.Zero); Assert.IsTrue(attr.valueLen == 0); // Create attribute with null mechanism array value attr = CkaUtils.CreateAttribute(CKA.CKA_ALLOWED_MECHANISMS, (CKM[])null); Assert.IsTrue(attr.type == (uint)CKA.CKA_ALLOWED_MECHANISMS); Assert.IsTrue(attr.value == IntPtr.Zero); Assert.IsTrue(attr.valueLen == 0); // Create attribute with empty mechanism array value attr = CkaUtils.CreateAttribute(CKA.CKA_ALLOWED_MECHANISMS, new CKM[0]); Assert.IsTrue(attr.type == (uint)CKA.CKA_ALLOWED_MECHANISMS); Assert.IsTrue(attr.value == IntPtr.Zero); Assert.IsTrue(attr.valueLen == 0); }
/// <summary> /// Creates mechanism of given type with no parameter /// </summary> /// <param name="mechanism">Mechanism type</param> /// <returns>Mechanism of given type with no parameter</returns> public static CK_MECHANISM CreateMechanism(CKM mechanism) { return CreateMechanism(Convert.ToUInt64((uint)mechanism)); }
/// <summary> /// Converts CKM to UInt32 /// </summary> /// <param name="value">CKM that should be converted</param> /// <returns>UInt32 with value from CKM</returns> public static UInt32 UInt32FromCKM(CKM value) { return(Convert.ToUInt32(value)); }
/// <summary> /// Converts CKM to UInt64 /// </summary> /// <param name="value">CKM that should be converted</param> /// <returns>UInt64 with value from CKM</returns> public static UInt64 UInt64FromCKM(CKM value) { return(Convert.ToUInt64(value)); }
/// <summary> /// Creates mechanism of given type with no parameter /// </summary> /// <param name="type">Mechanism type</param> /// <returns>Mechanism with no parameter</returns> public IMechanism CreateMechanism(CKM type) { return(new Mechanism(type)); }
/// <summary> /// Obtains a list of mechanism types supported by a token /// </summary> /// <returns>List of mechanism types supported by a token</returns> public List<CKM> GetMechanismList() { ulong mechanismCount = 0; CKR rv = _p11.C_GetMechanismList(_slotId, null, ref mechanismCount); if (rv != CKR.CKR_OK) throw new Pkcs11Exception("C_GetMechanismList", rv); if (mechanismCount < 1) return new List<CKM>(); CKM[] mechanismList = new CKM[mechanismCount]; rv = _p11.C_GetMechanismList(_slotId, mechanismList, ref mechanismCount); if (rv != CKR.CKR_OK) throw new Pkcs11Exception("C_GetMechanismList", rv); if (mechanismList.Length != Convert.ToInt32(mechanismCount)) Array.Resize(ref mechanismList, Convert.ToInt32(mechanismCount)); return new List<CKM>(mechanismList); }
/// <summary> /// Creates mechanism of given type with byte array parameter /// </summary> /// <param name="type">Mechanism type</param> /// <param name="parameter">Mechanism parameter</param> /// <returns>Mechanism and its parameter</returns> public IMechanism CreateMechanism(CKM type, byte[] parameter) { return(new Mechanism(type, parameter)); }
/// <summary> /// Converts low level CK_MECHANISM_INFO structure to high level MechanismInfo class /// </summary> /// <param name="mechanism">Mechanism</param> /// <param name="ck_mechanism_info">Low level CK_MECHANISM_INFO structure</param> internal MechanismInfo(CKM mechanism, CK_MECHANISM_INFO ck_mechanism_info) { _mechanism = mechanism; _minKeySize = ck_mechanism_info.MinKeySize; _maxKeySize = ck_mechanism_info.MaxKeySize; _mechanismFlags = new MechanismFlags(ck_mechanism_info.Flags); }
/// <summary> /// Creates mechanism of given type with no parameter /// </summary> /// <param name="type">Mechanism type</param> public Mechanism(CKM type) { _ckMechanism = LowLevelAPI.CkmUtils.CreateMechanism(type); }