Esempio n. 1
0
 /// <summary>
 /// Creates attribute of given type with CKO value
 /// </summary>
 /// <param name="type">Attribute type</param>
 /// <param name="value">Attribute value</param>
 public ObjectAttribute(CKA type, CKO value)
 {
     if (Platform.UnmanagedLongSize == 4)
     {
         if (Platform.StructPackingSize == 0)
         {
             _objectAttribute40 = new HighLevelAPI40.ObjectAttribute(type, value);
         }
         else
         {
             _objectAttribute41 = new HighLevelAPI41.ObjectAttribute(type, value);
         }
     }
     else
     {
         if (Platform.StructPackingSize == 0)
         {
             _objectAttribute80 = new HighLevelAPI80.ObjectAttribute(type, value);
         }
         else
         {
             _objectAttribute81 = new HighLevelAPI81.ObjectAttribute(type, value);
         }
     }
 }
Esempio n. 2
0
        private List <Pkcs11KeyInfo> ReadKeys(CKO objectClass, ClassAttributesDefinition keyAttributes)
        {
            List <Pkcs11KeyInfo> infos = new List <Pkcs11KeyInfo>();

            using (Session session = _slot.OpenSession(SessionType.ReadWrite))
            {
                List <ObjectAttribute> searchTemplate = new List <ObjectAttribute>();
                searchTemplate.Add(new ObjectAttribute(CKA.CKA_CLASS, objectClass));

                List <ObjectHandle> foundObjects = session.FindAllObjects(searchTemplate);
                foreach (ObjectHandle foundObject in foundObjects)
                {
                    // Read attributes required for sane object presentation
                    List <ulong> attributes = new List <ulong>();
                    attributes.Add((ulong)CKA.CKA_PRIVATE);
                    attributes.Add((ulong)CKA.CKA_KEY_TYPE);
                    attributes.Add((ulong)CKA.CKA_LABEL);
                    attributes.Add((ulong)CKA.CKA_ID);

                    List <ObjectAttribute> requiredAttributes = session.GetAttributeValue(foundObject, attributes);

                    // Read attributes configured for specific object class and type
                    attributes = new List <ulong>();
                    foreach (ClassAttribute classAttribute in keyAttributes.CommonAttributes)
                    {
                        attributes.Add(classAttribute.Value);
                    }
                    ulong keyType = requiredAttributes[1].GetValueAsUlong();
                    if (keyAttributes.TypeSpecificAttributes.ContainsKey(keyType))
                    {
                        foreach (ClassAttribute classAttribute in keyAttributes.TypeSpecificAttributes[keyType])
                        {
                            attributes.Add(classAttribute.Value);
                        }
                    }

                    List <ObjectAttribute> configuredAttributes = session.GetAttributeValue(foundObject, attributes);

                    // Read object storage size
                    ulong?storageSize = ReadObjectSize(session, foundObject);

                    // Construct info object
                    Pkcs11KeyInfo info = new Pkcs11KeyInfo(foundObject, configuredAttributes, storageSize)
                    {
                        CkaPrivate = requiredAttributes[0].GetValueAsBool(),
                        CkaClass   = (ulong)objectClass,
                        CkaKeyType = requiredAttributes[1].GetValueAsUlong(),
                        CkaLabel   = requiredAttributes[2].GetValueAsString(),
                        CkaId      = requiredAttributes[3].GetValueAsByteArray()
                    };

                    infos.Add(info);
                }
            }

            return(infos);
        }
Esempio n. 3
0
        private static ObjectHandle GetObjectHandle(string label, Session session, CKO keyType)
        {
            var objectAttributes = new List <ObjectAttribute>
            {
                new ObjectAttribute(CKA.CKA_CLASS, keyType),
                new ObjectAttribute(CKA.CKA_LABEL, label),
                new ObjectAttribute(CKA.CKA_TOKEN, true)
            };

            return(session.FindAllObjects(objectAttributes).First());
        }
        /// <summary>
        /// Finds handle of key object present on token
        /// </summary>
        /// <param name="session">PKCS#11 session for finding operation</param>
        /// <param name="keyClass">Value of CKA_CLASS attribute used in search template</param>
        /// <param name="ckaId">Value of CKA_ID attribute used in search template</param>
        /// <param name="ckaLabel">Value of CKA_LABEL attribute used in search template</param>
        /// <returns>Handle of key object present on token or null</returns>
        private IObjectHandle FindKey(ISession session, CKO keyClass, byte[] ckaId, string ckaLabel)
        {
            IObjectHandle keyHandle = null;

            var searchTemplate = new List <IObjectAttribute>()
            {
                session.Factories.ObjectAttributeFactory.Create(CKA.CKA_CLASS, keyClass),
                session.Factories.ObjectAttributeFactory.Create(CKA.CKA_TOKEN, true),
                session.Factories.ObjectAttributeFactory.Create(CKA.CKA_ID, ckaId),
            };

            foreach (IObjectHandle foundObjectHandle in session.FindAllObjects(searchTemplate))
            {
                keyHandle = foundObjectHandle;
                break;
            }

            return(keyHandle);
        }
Esempio n. 5
0
        /// <summary>
        /// Finds handle of key object present on token
        /// </summary>
        /// <param name="session">PKCS#11 session for finding operation</param>
        /// <param name="keyClass">Value of CKA_CLASS attribute used in search template</param>
        /// <param name="ckaId">Value of CKA_ID attribute used in search template</param>
        /// <param name="ckaLabel">Value of CKA_LABEL attribute used in search template</param>
        /// <returns>Handle of key object present on token or null</returns>
        private ObjectHandle FindKey(Session session, CKO keyClass, byte[] ckaId, string ckaLabel)
        {
            ObjectHandle keyHandle = null;

            var searchTemplate = new List <ObjectAttribute>()
            {
                new ObjectAttribute(CKA.CKA_CLASS, keyClass),
                new ObjectAttribute(CKA.CKA_TOKEN, true),
                new ObjectAttribute(CKA.CKA_ID, ckaId),
                new ObjectAttribute(CKA.CKA_LABEL, ckaLabel)
            };

            foreach (ObjectHandle foundObjectHandle in session.FindAllObjects(searchTemplate))
            {
                keyHandle = foundObjectHandle;
                break;
            }

            return(keyHandle);
        }
Esempio n. 6
0
 /// <summary>
 /// Creates attribute of given type with CKO value
 /// </summary>
 /// <param name="type">Attribute type</param>
 /// <param name="value">Attribute value</param>
 /// <returns>Attribute of given type with CKO value</returns>
 public static CK_ATTRIBUTE CreateAttribute(CKA type, CKO value)
 {
     return(CreateAttribute(ConvertUtils.UInt32FromCKA(type), ConvertUtils.UInt32FromCKO(value)));
 }
Esempio n. 7
0
 /// <summary>
 /// Converts CKO to NativeULong
 /// </summary>
 /// <param name="value">CKO that should be converted</param>
 /// <returns>NativeULong with value from CKO</returns>
 public static NativeULong ConvertFromCKO(CKO value)
 {
     return(Convert.ToUInt64(value));
 }
Esempio n. 8
0
 /// <summary>
 /// Creates attribute of given type with CKO value
 /// </summary>
 /// <param name="type">Attribute type</param>
 /// <param name="value">Attribute value</param>
 /// <returns>Attribute of given type with CKO value</returns>
 public static CK_ATTRIBUTE CreateAttribute(CKA type, CKO value)
 {
     return(CreateAttribute(NativeLongUtils.ConvertFromCKA(type), NativeLongUtils.ConvertFromCKO(value)));
 }
Esempio n. 9
0
 public static CK_ATTRIBUTE CreateClassAttribute(CKO objectClass)
 {
     return(createAttribute((uint)CKA.CLASS, BitConverter.GetBytes((uint)objectClass)));
 }
Esempio n. 10
0
 /// <summary>
 /// Creates attribute of given type with CKO value
 /// </summary>
 /// <param name="type">Attribute type</param>
 /// <param name="value">Attribute value</param>
 /// <returns>Attribute of given type with CKO value</returns>
 public static CK_ATTRIBUTE CreateAttribute(CKA type, CKO value)
 {
     return CreateAttribute((uint)type, (uint)value);
 }
Esempio n. 11
0
 /// <summary>
 /// Creates attribute of given type with CKO value
 /// </summary>
 /// <param name="type">Attribute type</param>
 /// <param name="value">Attribute value</param>
 /// <returns>Attribute of given type with CKO value</returns>
 public static CK_ATTRIBUTE CreateAttribute(CKA type, CKO value)
 {
     return(CreateAttribute(Convert.ToUInt64((uint)type), Convert.ToUInt64((uint)value)));
 }
Esempio n. 12
0
 /// <summary>
 /// Creates attribute of given type with CKO value
 /// </summary>
 /// <param name="type">Attribute type</param>
 /// <param name="value">Attribute value</param>
 public ObjectAttribute(CKA type, CKO value)
 {
     _ckAttribute = CkaUtils.CreateAttribute(type, value);
 }
Esempio n. 13
0
 /// <summary>
 /// Converts CKO to UInt32
 /// </summary>
 /// <param name="value">CKO that should be converted</param>
 /// <returns>UInt32 with value from CKO</returns>
 public static UInt32 UInt32FromCKO(CKO value)
 {
     return(Convert.ToUInt32(value));
 }
 /// <summary>
 /// Creates attribute of given type with CKO value
 /// </summary>
 /// <param name="type">Attribute type</param>
 /// <param name="value">Attribute value</param>
 public ObjectAttribute(CKA type, CKO value)
 {
     _ckAttribute = CkaUtils.CreateAttribute(type, value);
 }
 public ObjectClassAttribute(CKO objectType) : base((uint)CKA.CLASS)
 {
     ObjectType = objectType;
 }
Esempio n. 16
0
 /// <summary>
 /// Converts CKO to UInt64
 /// </summary>
 /// <param name="value">CKO that should be converted</param>
 /// <returns>UInt64 with value from CKO</returns>
 public static UInt64 UInt64FromCKO(CKO value)
 {
     return(Convert.ToUInt64(value));
 }
Esempio n. 17
0
 /// <summary>
 /// Creates attribute of given type with CKO value
 /// </summary>
 /// <param name="type">Attribute type</param>
 /// <param name="value">Attribute value</param>
 /// <returns>Attribute of given type with CKO value</returns>
 public static CK_ATTRIBUTE CreateAttribute(CKA type, CKO value)
 {
     return(CreateAttribute((uint)type, (uint)value));
 }
 /// <summary>
 /// Creates attribute of given type with CKO value
 /// </summary>
 /// <param name="type">Attribute type</param>
 /// <param name="value">Attribute value</param>
 /// <returns>Attribute of cryptoki object</returns>
 public IObjectAttribute Create(CKA type, CKO value)
 {
     return(_factory.Create(type, value));
 }
Esempio n. 19
0
        /// <summary>
        /// Checks whether type matches the value of "type" path attribute
        /// </summary>
        /// <param name="uriType">Value of "type" path attribute present (or not) in PKCS#11 URI</param>
        /// <param name="inputType">Type that should be compared with the value of "type" path attribute</param>
        /// <returns>True if type matches the value of "type" path attribute</returns>
        private static bool ObjectTypesMatch(CKO? uriType, CKO? inputType)
        {
            if (inputType == null)
            {
                if (uriType != null)
                    return false;
            }
            else
            {
                if (uriType != null)
                {
                    if (uriType.Value != inputType.Value)
                        return false;
                }
            }

            return true;
        }
 /// <summary>
 /// Creates attribute of given type with CKO value
 /// </summary>
 /// <param name="type">Attribute type</param>
 /// <param name="value">Attribute value</param>
 public ObjectAttribute(CKA type, CKO value)
 {
     if (Platform.UnmanagedLongSize == 4)
     {
         if (Platform.StructPackingSize == 0)
             _objectAttribute40 = new HighLevelAPI40.ObjectAttribute(type, value);
         else
             _objectAttribute41 = new HighLevelAPI41.ObjectAttribute(type, value);
     }
     else
     {
         if (Platform.StructPackingSize == 0)
             _objectAttribute80 = new HighLevelAPI80.ObjectAttribute(type, value);
         else
             _objectAttribute81 = new HighLevelAPI81.ObjectAttribute(type, value);
     }
 }
Esempio n. 21
0
        /// <summary>
        /// Checks whether object attributes match PKCS#11 URI
        /// </summary>
        /// <param name="pkcs11Uri">PKCS#11 URI</param>
        /// <param name="ckaClass">Value of CKA_CLASS object attribute</param>
        /// <param name="ckaLabel">Value of CKA_LABEL object attribute</param>
        /// <param name="ckaId">Value of CKA_ID object attribute</param>
        /// <returns>True if object attributes match PKCS#11 URI</returns>
        private static bool Matches(Pkcs11Uri pkcs11Uri, CKO? ckaClass, string ckaLabel, byte[] ckaId)
        {
            if (pkcs11Uri == null)
                throw new ArgumentNullException("pkcs11Uri");

            if (pkcs11Uri.UnknownPathAttributes != null)
                return false;

            if (!ObjectTypesMatch(pkcs11Uri.Type, ckaClass))
                return false;

            if (!SimpleStringsMatch(pkcs11Uri.Object, ckaLabel))
                return false;

            if (!ByteArraysMatch(pkcs11Uri.Id, ckaId))
                return false;

            return true;
        }
 /// <summary>
 /// Creates attribute of given type with CKO value
 /// </summary>
 /// <param name="type">Attribute type</param>
 /// <param name="value">Attribute value</param>
 /// <returns>Attribute of cryptoki object</returns>
 public IObjectAttribute CreateObjectAttribute(CKA type, CKO value)
 {
     return(new ObjectAttribute(type, value));
 }
Esempio n. 23
0
 /// <summary>
 /// Creates attribute of given type with CKO value
 /// </summary>
 /// <param name="type">Attribute type</param>
 /// <param name="value">Attribute value</param>
 /// <returns>Attribute of given type with CKO value</returns>
 public static CK_ATTRIBUTE CreateAttribute(CKA type, CKO value)
 {
     return CreateAttribute(Convert.ToUInt64((uint)type), Convert.ToUInt64((uint)value));
 }