Esempio n. 1
0
        public IFakeCommandAttribute BuildSaveFakeCommandByRouting <T>(PermissionMode permission, T target,
                                                                       IRoutingAttribute routing,
                                                                       IObjectAttribute objectAttribute,
                                                                       PropertyInfo[] properties)
            where T : IAlbianObject
        {
            if (null == routing)
            {
                throw new ArgumentNullException("routing");
            }
            if (null == properties || 0 == properties.Length)
            {
                throw new ArgumentNullException("properties");
            }
            if (null == objectAttribute)
            {
                throw new ArgumentNullException("objectAttribute");
            }
            if (0 == (permission & routing.Permission))
            {
                if (null != Logger)
                {
                    Logger.WarnFormat("The routing permission {0} is no enough.", permission);
                }
                return(null);
            }

            return(target.IsNew
                       ?
                   BuildCreateFakeCommandByRouting(permission, target, routing, objectAttribute, properties)
                       :
                   BuildModifyFakeCommandByRouting(permission, target, routing, objectAttribute, properties));
        }
Esempio n. 2
0
        private void EditAttributeValue()
        {
            ListView activeListView = GetActiveListView();

            ListViewItem selectedItem = WinFormsUtils.GetSingleSelectedItem(activeListView);

            if (selectedItem == null)
            {
                return;
            }

            IObjectAttribute objectAttribute = (IObjectAttribute)selectedItem.Tag;

            string attributeName = selectedItem.Text;

            byte[] attributeValue = objectAttribute.GetValueAsByteArray() ?? new byte[0];

            using (HexEditor hexEditor = new HexEditor(attributeName, attributeValue))
            {
                if (hexEditor.ShowDialog() == DialogResult.OK)
                {
                    IObjectAttribute updatedObjectAttribute = Pkcs11Admin.Instance.Factories.ObjectAttributeFactory.Create(objectAttribute.Type, hexEditor.Bytes);
                    selectedItem.Tag = updatedObjectAttribute;
                    ReloadListView(activeListView);
                }
            }
        }
Esempio n. 3
0
        private static List <Tuple <IObjectAttribute, ClassAttribute> > GetDefaultAttributes(ClassAttributesDefinition classAttributes, ulong?objectType, bool createObject)
        {
            if (classAttributes == null)
            {
                throw new ArgumentNullException("classAttributes");
            }

            List <Tuple <IObjectAttribute, ClassAttribute> > objectAttributes = new List <Tuple <IObjectAttribute, ClassAttribute> >();

            foreach (ClassAttribute classAttribute in classAttributes.CommonAttributes)
            {
                IObjectAttribute objectAttribute = (createObject) ? GetDefaultAttribute(classAttribute.Value, classAttribute.CreateDefaultValue) : GetDefaultAttribute(classAttribute.Value, classAttribute.GenerateDefaultValue);
                objectAttributes.Add(new Tuple <IObjectAttribute, ClassAttribute>(objectAttribute, classAttribute));
            }

            if ((objectType != null) && (classAttributes.TypeSpecificAttributes.ContainsKey(objectType.Value)))
            {
                foreach (ClassAttribute classAttribute in classAttributes.TypeSpecificAttributes[objectType.Value])
                {
                    IObjectAttribute objectAttribute = (createObject) ? GetDefaultAttribute(classAttribute.Value, classAttribute.CreateDefaultValue) : GetDefaultAttribute(classAttribute.Value, classAttribute.GenerateDefaultValue);
                    objectAttributes.Add(new Tuple <IObjectAttribute, ClassAttribute>(objectAttribute, classAttribute));
                }
            }

            return(objectAttributes);
        }
Esempio n. 4
0
        private void EditAttributeValue()
        {
            ListViewItem selectedItem = WinFormsUtils.GetSingleSelectedItem(ListViewAttributes);

            if (selectedItem == null)
            {
                return;
            }

            IObjectAttribute objectAttribute = (IObjectAttribute)selectedItem.Tag;

            string attributeName = selectedItem.Text;

            byte[] attributeValue = (objectAttribute.CannotBeRead) ? new byte[0] : objectAttribute.GetValueAsByteArray();

            using (HexEditor hexEditor = new HexEditor(attributeName, attributeValue))
            {
                if (hexEditor.ShowDialog() == DialogResult.OK)
                {
                    AttributeModified = true;
                    IObjectAttribute updatedObjectAttribute = Pkcs11Admin.Instance.Factories.ObjectAttributeFactory.Create(objectAttribute.Type, hexEditor.Bytes);
                    _pkcs11Slot.SaveObjectAttributes(_pkcs11ObjectInfo, new List <IObjectAttribute>()
                    {
                        updatedObjectAttribute
                    });
                    selectedItem.Tag = updatedObjectAttribute;
                    ReloadListView();
                }
            }
        }
Esempio n. 5
0
        public void _01_DisposeAttributeTest()
        {
            // Unmanaged memory for attribute value stored in low level CK_ATTRIBUTE struct
            // is allocated by constructor of ObjectAttribute class.
            IObjectAttribute attr1 = Settings.Factories.ObjectAttributeFactory.CreateObjectAttribute(CKA.CKA_CLASS, CKO.CKO_DATA);

            // Do something interesting with attribute

            // This unmanaged memory is freed by Dispose() method.
            attr1.Dispose();


            // ObjectAttribute class can be used in using statement which defines a scope
            // at the end of which an object will be disposed (and unmanaged memory freed).
            using (IObjectAttribute attr2 = Settings.Factories.ObjectAttributeFactory.CreateObjectAttribute(CKA.CKA_CLASS, CKO.CKO_DATA))
            {
                // Do something interesting with attribute
            }


            #pragma warning disable 0219

            // Explicit calling of Dispose() method can also be ommitted.
            IObjectAttribute attr3 = Settings.Factories.ObjectAttributeFactory.CreateObjectAttribute(CKA.CKA_CLASS, CKO.CKO_DATA);

            // Do something interesting with attribute

            // Dispose() method will be called (and unmanaged memory freed) by GC eventually
            // but we cannot be sure when will this occur.

            #pragma warning restore 0219
        }
Esempio n. 6
0
 public void _02_EmptyAttributeTest()
 {
     // Create attribute without the value
     using (IObjectAttribute attr = Settings.Factories.ObjectAttributeFactory.CreateObjectAttribute(CKA.CKA_CLASS))
     {
         Assert.IsTrue(attr.Type == ConvertUtils.UInt64FromCKA(CKA.CKA_CLASS));
         Assert.IsTrue(attr.GetValueAsByteArray() == null);
     }
 }
Esempio n. 7
0
        public void _03_ULongAttributeTest()
        {
            ulong value = ConvertUtils.UInt64FromCKO(CKO.CKO_DATA);

            // Create attribute with ulong value
            using (IObjectAttribute attr = Settings.Factories.ObjectAttributeFactory.CreateObjectAttribute(CKA.CKA_CLASS, value))
            {
                Assert.IsTrue(attr.Type == ConvertUtils.UInt64FromCKA(CKA.CKA_CLASS));
                Assert.IsTrue(attr.GetValueAsUlong() == value);
            }
        }
Esempio n. 8
0
        public void _04_BoolAttributeTest()
        {
            bool value = true;

            // Create attribute with bool value
            using (IObjectAttribute attr = Settings.Factories.ObjectAttributeFactory.CreateObjectAttribute(CKA.CKA_TOKEN, value))
            {
                Assert.IsTrue(attr.Type == ConvertUtils.UInt64FromCKA(CKA.CKA_TOKEN));
                Assert.IsTrue(attr.GetValueAsBool() == value);
            }
        }
Esempio n. 9
0
        public void _07_DateTimeAttributeTest()
        {
            DateTime value = new DateTime(2012, 1, 30, 0, 0, 0, DateTimeKind.Utc);

            // Create attribute with DateTime value
            using (IObjectAttribute attr = Settings.Factories.ObjectAttributeFactory.CreateObjectAttribute(CKA.CKA_START_DATE, value))
            {
                Assert.IsTrue(attr.Type == ConvertUtils.UInt64FromCKA(CKA.CKA_START_DATE));
                Assert.IsTrue(attr.GetValueAsDateTime() == value);
            }
        }
Esempio n. 10
0
        public IDictionary <string, IStorageContext> GenerateStorageContexts <T>(T target,
                                                                                 BuildFakeCommandByRoutingsHandler <T>
                                                                                 buildFakeCommandByRoutingsHandler,
                                                                                 BuildFakeCommandByRoutingHandler <T>
                                                                                 buildFakeCommandByRoutingHandler)
            where T : IAlbianObject
        {
            if (null == target)
            {
                throw new ArgumentNullException("target");
            }

            Type   type        = target.GetType();
            string fullName    = AssemblyManager.GetFullTypeName(target);
            object oProperties = PropertyCache.Get(fullName);

            PropertyInfo[] properties;
            if (null == oProperties)
            {
                if (null != Logger)
                {
                    Logger.Warn("Get the object property info from cache is null.Reflection now and add to cache.");
                }
                properties = type.GetProperties();
                PropertyCache.InsertOrUpdate(fullName, properties);
            }
            properties = (PropertyInfo[])oProperties;
            object oAttribute = ObjectCache.Get(fullName);

            if (null == oAttribute)
            {
                if (null != Logger)
                {
                    Logger.ErrorFormat("The {0} object attribute is null in the object cache.", fullName);
                }
                throw new Exception("The object attribute is null");
            }
            IObjectAttribute objectAttribute = (IObjectAttribute)oAttribute;
            IDictionary <string, IStorageContext> storageContexts = buildFakeCommandByRoutingsHandler(target, properties,
                                                                                                      objectAttribute,
                                                                                                      buildFakeCommandByRoutingHandler);

            if (0 == storageContexts.Count) //no the storage context
            {
                if (null != Logger)
                {
                    Logger.Warn("There is no storage contexts of the object.");
                }
                return(null);
            }
            return(storageContexts);
        }
Esempio n. 11
0
 private void AssociateYapClass(Transaction a_trans, object a_object)
 {
     if (a_object == null)
     {
     }
     else
     {
         //It seems that we need not result the following field
         //i_object = null;
         //i_comparator = Null.INSTANCE;
         //i_classMetadata = null;
         // FIXME: Setting the YapClass to null will prevent index use
         // If the field is typed we can guess the right one with the
         // following line. However this does break some SODA test cases.
         // Revisit!
         //            if(i_field != null){
         //                i_classMetadata = i_field.getYapClass();
         //            }
         _classMetadata = a_trans.Container().ProduceClassMetadata(a_trans.Reflector().ForObject
                                                                       (a_object));
         if (_classMetadata != null)
         {
             i_object = _classMetadata.GetComparableObject(a_object);
             if (a_object != i_object)
             {
                 i_attributeProvider = _classMetadata.Config().QueryAttributeProvider();
                 _classMetadata      = a_trans.Container().ProduceClassMetadata(a_trans.Reflector().ForObject
                                                                                    (i_object));
             }
             if (_classMetadata != null)
             {
                 _classMetadata.CollectConstraints(a_trans, this, i_object, new _IVisitor4_84(this
                                                                                              ));
             }
             else
             {
                 AssociateYapClass(a_trans, null);
             }
         }
         else
         {
             AssociateYapClass(a_trans, null);
         }
     }
 }
Esempio n. 12
0
		private void AssociateYapClass(Transaction a_trans, object a_object)
		{
			if (a_object == null)
			{
			}
			else
			{
				//It seems that we need not result the following field
				//i_object = null;
				//i_comparator = Null.INSTANCE;
				//i_classMetadata = null;
				// FIXME: Setting the YapClass to null will prevent index use
				// If the field is typed we can guess the right one with the
				// following line. However this does break some SODA test cases.
				// Revisit!
				//            if(i_field != null){
				//                i_classMetadata = i_field.getYapClass();
				//            }
				_classMetadata = a_trans.Container().ProduceClassMetadata(a_trans.Reflector().ForObject
					(a_object));
				if (_classMetadata != null)
				{
					i_object = _classMetadata.GetComparableObject(a_object);
					if (a_object != i_object)
					{
						i_attributeProvider = _classMetadata.Config().QueryAttributeProvider();
						_classMetadata = a_trans.Container().ProduceClassMetadata(a_trans.Reflector().ForObject
							(i_object));
					}
					if (_classMetadata != null)
					{
						_classMetadata.CollectConstraints(a_trans, this, i_object, new _IVisitor4_84(this
							));
					}
					else
					{
						AssociateYapClass(a_trans, null);
					}
				}
				else
				{
					AssociateYapClass(a_trans, null);
				}
			}
		}
Esempio n. 13
0
        private void ButtonAsn1_Click(object sender, EventArgs e)
        {
            ListViewItem selectedItem = WinFormsUtils.GetSingleSelectedItem(ListViewAttributes);

            if (selectedItem == null)
            {
                return;
            }

            IObjectAttribute objectAttribute = (IObjectAttribute)selectedItem.Tag;

            string attributeName = selectedItem.Text;

            byte[] attributeValue = (objectAttribute.CannotBeRead) ? new byte[0] : objectAttribute.GetValueAsByteArray();

            using (Asn1Viewer asn1Viewer = new Asn1Viewer(attributeName, attributeValue))
                asn1Viewer.ShowDialog();
        }
Esempio n. 14
0
        private static ICacheAttribute GetCacheAttribute <T>()
            where T : class, IAlbianObject
        {
            string fullName   = AssemblyManager.GetFullTypeName(typeof(T));
            object oAttribute = ObjectCache.Get(fullName);

            if (null == oAttribute)
            {
                if (null != Logger)
                {
                    Logger.ErrorFormat("The {0} object attribute is null in the object cache.", fullName);
                }
                return(null);
            }
            IObjectAttribute objectAttribute = (IObjectAttribute)oAttribute;

            return(objectAttribute.Cache);
        }
Esempio n. 15
0
        private void ButtonRefresh_Click(object sender, EventArgs e)
        {
            List <ulong> attributes = new List <ulong>();

            for (int i = 0; i < ListViewAttributes.Items.Count; i++)
            {
                IObjectAttribute objectAttribute = (IObjectAttribute)ListViewAttributes.Items[i].Tag;
                attributes.Add(objectAttribute.Type);
            }

            List <IObjectAttribute> objectAttributes = _pkcs11Slot.LoadObjectAttributes(_pkcs11ObjectInfo, attributes);

            for (int i = 0; i < ListViewAttributes.Items.Count; i++)
            {
                ListViewAttributes.Items[i].Tag = objectAttributes[i];
            }

            ReloadListView();
        }
Esempio n. 16
0
        public void _05_StringAttributeTest()
        {
            string value = "Hello world";

            // Create attribute with string value
            using (IObjectAttribute attr = Settings.Factories.ObjectAttributeFactory.CreateObjectAttribute(CKA.CKA_LABEL, value))
            {
                Assert.IsTrue(attr.Type == ConvertUtils.UInt64FromCKA(CKA.CKA_LABEL));
                Assert.IsTrue(attr.GetValueAsString() == value);
            }

            value = null;

            // Create attribute with null string value
            using (IObjectAttribute attr = Settings.Factories.ObjectAttributeFactory.CreateObjectAttribute(CKA.CKA_LABEL, value))
            {
                Assert.IsTrue(attr.Type == ConvertUtils.UInt64FromCKA(CKA.CKA_LABEL));
                Assert.IsTrue(attr.GetValueAsString() == value);
            }
        }
Esempio n. 17
0
        private void ViewAsn1AttributeValue()
        {
            ListView activeListView = GetActiveListView();

            ListViewItem selectedItem = WinFormsUtils.GetSingleSelectedItem(activeListView);

            if (selectedItem == null)
            {
                return;
            }

            IObjectAttribute objectAttribute = (IObjectAttribute)selectedItem.Tag;

            string attributeName = selectedItem.Text;

            byte[] attributeValue = (objectAttribute.CannotBeRead) ? new byte[0] : objectAttribute.GetValueAsByteArray();

            using (Asn1Viewer asn1Viewer = new Asn1Viewer(attributeName, attributeValue))
                asn1Viewer.ShowDialog();
        }
Esempio n. 18
0
        public void _06_ByteArrayAttributeTest()
        {
            byte[] value = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09 };

            // Create attribute with byte array value
            using (IObjectAttribute attr = Settings.Factories.ObjectAttributeFactory.CreateObjectAttribute(CKA.CKA_ID, value))
            {
                Assert.IsTrue(attr.Type == ConvertUtils.UInt64FromCKA(CKA.CKA_ID));
                Assert.IsTrue(ConvertUtils.BytesToBase64String(attr.GetValueAsByteArray()) == ConvertUtils.BytesToBase64String(value));
            }

            value = null;

            // Create attribute with null byte array value
            using (IObjectAttribute attr = Settings.Factories.ObjectAttributeFactory.CreateObjectAttribute(CKA.CKA_ID, value))
            {
                Assert.IsTrue(attr.Type == ConvertUtils.UInt64FromCKA(CKA.CKA_ID));
                Assert.IsTrue(attr.GetValueAsByteArray() == value);
            }
        }
Esempio n. 19
0
        private void ReloadListView()
        {
            foreach (ListViewItem listViewItem in ListViewAttributes.Items)
            {
                // Clear currently viewed values
                listViewItem.Text = null;
                listViewItem.SubItems.Clear();

                // Parse tag
                IObjectAttribute objectAttribute = (IObjectAttribute)listViewItem.Tag;

                // Determine viewable values
                string attributeName  = null;
                string attributeValue = null;
                StringUtils.GetAttributeNameAndValue(objectAttribute, out attributeName, out attributeValue);

                // Set new values
                listViewItem.Text = attributeName;
                listViewItem.SubItems.Add(attributeValue);
            }
        }
Esempio n. 20
0
        protected PropertyInfo[] AfterExecute <T>(IDataReader dr, out Hashtable reader,
                                                  out IDictionary <string, IMemberAttribute> members)
        {
            Type   type        = typeof(T);
            string fullName    = AssemblyManager.GetFullTypeName(type);
            object oProperties = PropertyCache.Get(fullName);

            PropertyInfo[] properties;
            if (null == oProperties)
            {
                if (null != Logger)
                {
                    Logger.Warn("Get the object property info from cache is null.Reflection now and add to cache.");
                }
                properties = type.GetProperties();
                PropertyCache.InsertOrUpdate(fullName, properties);
            }
            properties = (PropertyInfo[])oProperties;
            object oAttribute = ObjectCache.Get(fullName);

            if (null == oAttribute)
            {
                if (null != Logger)
                {
                    Logger.ErrorFormat("The {0} object attribute is null in the object cache.", fullName);
                }
                throw new Exception("The object attribute is null");
            }
            IObjectAttribute objectAttribute = (IObjectAttribute)oAttribute;

            members = objectAttribute.MemberAttributes;

            reader = new Hashtable();
            for (int i = 0; i < dr.FieldCount; i++)
            {
                reader[dr.GetName(i)] = i;
            }
            return(properties);
        }
Esempio n. 21
0
        public List <Tuple <IObjectAttribute, ClassAttribute> > ImportDataObject(string fileName, byte[] fileContent)
        {
            IObjectAttributeFactory objectAttributeFactory = Pkcs11Admin.Instance.Factories.ObjectAttributeFactory;

            List <Tuple <IObjectAttribute, ClassAttribute> > objectAttributes = StringUtils.GetCreateDefaultAttributes(Pkcs11Admin.Instance.Config.DataObjectAttributes, null);

            bool ckaLabelFound = false;
            bool ckaValueFound = false;

            for (int i = 0; i < objectAttributes.Count; i++)
            {
                IObjectAttribute objectAttribute = objectAttributes[i].Item1;
                ClassAttribute   classAttribute  = objectAttributes[i].Item2;

                if (objectAttribute.Type == (ulong)CKA.CKA_LABEL)
                {
                    objectAttributes[i] = new Tuple <IObjectAttribute, ClassAttribute>(objectAttributeFactory.Create(CKA.CKA_LABEL, fileName), classAttribute);
                    ckaLabelFound       = true;
                }
                else if (objectAttribute.Type == (ulong)CKA.CKA_VALUE)
                {
                    objectAttributes[i] = new Tuple <IObjectAttribute, ClassAttribute>(objectAttributeFactory.Create(CKA.CKA_VALUE, fileContent), classAttribute);
                    ckaValueFound       = true;
                }
            }

            if (!ckaLabelFound)
            {
                throw new Exception("TODO - ClassAttributeNotFoundException");
            }

            if (!ckaValueFound)
            {
                throw new Exception("TODO - ClassAttributeNotFoundException");
            }

            return(objectAttributes);
        }
Esempio n. 22
0
        public void _10_MechanismArrayAttributeTest()
        {
            List <CKM> originalValue = new List <CKM>();

            originalValue.Add(CKM.CKM_RSA_PKCS);
            originalValue.Add(CKM.CKM_AES_CBC);

            // Create attribute with mechanism array value
            using (IObjectAttribute attr = Settings.Factories.ObjectAttributeFactory.CreateObjectAttribute(CKA.CKA_ALLOWED_MECHANISMS, originalValue))
            {
                Assert.IsTrue(attr.Type == ConvertUtils.UInt64FromCKA(CKA.CKA_ALLOWED_MECHANISMS));

                List <CKM> recoveredValue = attr.GetValueAsCkmList();
                for (int i = 0; i < recoveredValue.Count; i++)
                {
                    Assert.IsTrue(originalValue[i] == recoveredValue[i]);
                }
            }

            originalValue = null;

            // Create attribute with null mechanism array value
            using (IObjectAttribute attr = Settings.Factories.ObjectAttributeFactory.CreateObjectAttribute(CKA.CKA_ALLOWED_MECHANISMS, originalValue))
            {
                Assert.IsTrue(attr.Type == ConvertUtils.UInt64FromCKA(CKA.CKA_ALLOWED_MECHANISMS));
                Assert.IsTrue(attr.GetValueAsCkmList() == originalValue);
            }

            originalValue = new List <CKM>();

            // Create attribute with empty mechanism array value
            using (IObjectAttribute attr = Settings.Factories.ObjectAttributeFactory.CreateObjectAttribute(CKA.CKA_ALLOWED_MECHANISMS, originalValue))
            {
                Assert.IsTrue(attr.Type == ConvertUtils.UInt64FromCKA(CKA.CKA_ALLOWED_MECHANISMS));
                Assert.IsTrue(attr.GetValueAsCkmList() == null);
            }
        }
Esempio n. 23
0
        public void _09_ULongArrayAttributeTest()
        {
            List <ulong> originalValue = new List <ulong>();

            originalValue.Add(333333);
            originalValue.Add(666666);

            // Create attribute with ulong array value
            using (IObjectAttribute attr = Settings.Factories.ObjectAttributeFactory.CreateObjectAttribute(CKA.CKA_ALLOWED_MECHANISMS, originalValue))
            {
                Assert.IsTrue(attr.Type == ConvertUtils.UInt64FromCKA(CKA.CKA_ALLOWED_MECHANISMS));

                List <ulong> recoveredValue = attr.GetValueAsULongList();
                for (int i = 0; i < recoveredValue.Count; i++)
                {
                    Assert.IsTrue(originalValue[i] == recoveredValue[i]);
                }
            }

            originalValue = null;

            // Create attribute with null ulong array value
            using (IObjectAttribute attr = Settings.Factories.ObjectAttributeFactory.CreateObjectAttribute(CKA.CKA_ALLOWED_MECHANISMS, originalValue))
            {
                Assert.IsTrue(attr.Type == ConvertUtils.UInt64FromCKA(CKA.CKA_ALLOWED_MECHANISMS));
                Assert.IsTrue(attr.GetValueAsULongList() == originalValue);
            }

            originalValue = new List <ulong>();

            // Create attribute with empty ulong array value
            using (IObjectAttribute attr = Settings.Factories.ObjectAttributeFactory.CreateObjectAttribute(CKA.CKA_ALLOWED_MECHANISMS, originalValue))
            {
                Assert.IsTrue(attr.Type == ConvertUtils.UInt64FromCKA(CKA.CKA_ALLOWED_MECHANISMS));
                Assert.IsTrue(attr.GetValueAsULongList() == null);
            }
        }
 internal bool Contains(IObjectAttribute ioa)
 {
     return _ObjectAttributePropertiesListened.ContainsKey(ioa);
 }
Esempio n. 25
0
        public IFakeCommandAttribute GenerateQuery <T>(string rountingName, int top, IFilterCondition[] where,
                                                       IOrderByCondition[] orderby)
            where T : class, IAlbianObject, new()
        {
            Type   type        = typeof(T);
            string fullName    = AssemblyManager.GetFullTypeName(type);
            object oProperties = PropertyCache.Get(fullName);

            PropertyInfo[] properties;
            if (null == oProperties)
            {
                if (null != Logger)
                {
                    Logger.Error("Get the object property info from cache is null.Reflection now and add to cache.");
                }
                throw new PersistenceException("object property is null in the cache.");
            }
            properties = (PropertyInfo[])oProperties;
            object oAttribute = ObjectCache.Get(fullName);

            if (null == oAttribute)
            {
                if (null != Logger)
                {
                    Logger.ErrorFormat("The {0} object attribute is null in the object cache.", fullName);
                }
                throw new Exception("The object attribute is null");
            }
            StringBuilder     sbSelect        = new StringBuilder();
            StringBuilder     sbCols          = new StringBuilder();
            StringBuilder     sbWhere         = new StringBuilder();
            StringBuilder     sbOrderBy       = new StringBuilder();
            IObjectAttribute  objectAttribute = (IObjectAttribute)oAttribute;
            IRoutingAttribute routing;

            if (!objectAttribute.RoutingAttributes.TryGetValue(rountingName, out routing))
            {
                if (null != Logger)
                {
                    Logger.WarnFormat("There is not routing of the {} object.Albian use the default routing tempate.",
                                      rountingName);
                }
                routing = objectAttribute.RountingTemplate;
            }

            if (0 == (PermissionMode.R & routing.Permission))
            {
                if (null != Logger)
                {
                    Logger.WarnFormat("The routing permission {0} is no enough.", routing.Permission);
                }
                return(null);
            }

            IStorageAttribute storageAttr = (IStorageAttribute)StorageCache.Get(routing.StorageName);

            if (null == storageAttr)
            {
                if (null != Logger)
                {
                    Logger.WarnFormat(
                        "No {0} rounting mapping storage attribute in the sotrage cache.Use default storage.",
                        routing.Name);
                }
                storageAttr = (IStorageAttribute)StorageCache.Get(StorageParser.DefaultStorageName);
            }

            if (!storageAttr.IsHealth)
            {
                if (null != Logger)
                {
                    Logger.WarnFormat("Routing:{0},Storage:{1} is not health.", routing.Name, storageAttr.Name);
                }
                return(null);
            }

            IDictionary <string, IMemberAttribute> members = objectAttribute.MemberAttributes;
            T target = AlbianObjectFactory.CreateInstance <T>();

            foreach (PropertyInfo property in properties)
            {
                IMemberAttribute member = members[property.Name];
                if (!member.IsSave)
                {
                    continue;
                }
                sbCols.AppendFormat("{0},", member.FieldName);

                if (null != where)
                {
                    foreach (IFilterCondition condition in where) //have better algorithm??
                    {
                        if (condition.PropertyName == property.Name)
                        {
                            property.SetValue(target, condition.Value, null); //Construct the splite object
                            break;
                        }
                    }
                }
                if (null != orderby)
                {
                    foreach (IOrderByCondition order in orderby)
                    {
                        if (order.PropertyName == property.Name)
                        {
                            sbOrderBy.AppendFormat("{0} {1},", member.FieldName,
                                                   System.Enum.GetName(typeof(SortStyle), order.SortStyle));
                            break;
                        }
                    }
                }
            }
            if (0 != sbOrderBy.Length)
            {
                sbOrderBy.Remove(sbOrderBy.Length - 1, 1);
            }
            if (0 != sbCols.Length)
            {
                sbCols.Remove(sbCols.Length - 1, 1);
            }
            IList <DbParameter> paras = new List <DbParameter>();

            if (null != where && 0 != where.Length)
            {
                foreach (IFilterCondition condition in where)
                {
                    IMemberAttribute member = members[condition.PropertyName];
                    if (!member.IsSave)
                    {
                        continue;
                    }
                    sbWhere.AppendFormat(" {0} {1} {2} {3} ", Utils.GetRelationalOperators(condition.Relational),
                                         member.FieldName, Utils.GetLogicalOperation(condition.Logical),
                                         DatabaseFactory.GetParameterName(storageAttr.DatabaseStyle, member.FieldName));
                    paras.Add(DatabaseFactory.GetDbParameter(storageAttr.DatabaseStyle, member.FieldName, member.DBType,
                                                             condition.Value, member.Length));
                }
            }
            string tableFullName = Utils.GetTableFullName(routing, target);

            switch (storageAttr.DatabaseStyle)
            {
            case DatabaseStyle.MySql:
            {
                sbSelect.AppendFormat("SELECT {0} FROM {1} WHERE 1=1 {2} {3} {4}",
                                      sbCols, tableFullName, sbWhere,
                                      0 == sbOrderBy.Length ? string.Empty : string.Format("ORDER BY {0}", sbOrderBy),
                                      0 == top ? string.Empty : string.Format("LIMIT {0}", top)
                                      );
                break;
            }

            case DatabaseStyle.Oracle:
            {
                if (0 == top)
                {
                    sbSelect.AppendFormat("SELECT {0} FROM {1} WHERE 1=1 {2} {3}",
                                          sbCols, tableFullName, sbWhere,
                                          0 == sbOrderBy.Length ? string.Empty : string.Format("ORDER BY {0}", sbOrderBy));
                }
                else
                {
                    sbSelect.AppendFormat("SELECT A.* FROM (SELECT {0} FROM {1} WHERE 1=1 {2} {3})A WHERE ROWNUM <= {4}",
                                          sbCols, tableFullName, sbWhere,
                                          0 == sbOrderBy.Length ? string.Empty : string.Format("ORDER BY {0}", sbOrderBy),
                                          top);
                }

                break;
            }

            case DatabaseStyle.SqlServer:
            default:
            {
                sbSelect.AppendFormat("SELECT {0} {1} FROM {2} WHERE 1=1 {3} {4}",
                                      0 == top ? string.Empty : string.Format("TOP {0}", top),
                                      sbCols, tableFullName, sbWhere,
                                      0 == sbOrderBy.Length ? string.Empty : string.Format("ORDER BY {0}", sbOrderBy));
                break;
            }
            }

            IFakeCommandAttribute fakeCommand = new FakeCommandAttribute
            {
                CommandText = sbSelect.ToString(),
                Paras       = ((List <DbParameter>)paras).ToArray(),
                StorageName = storageAttr.Name,
            };

            return(fakeCommand);
        }
Esempio n. 26
0
        public IFakeCommandAttribute BuildCreateFakeCommandByRouting <T>(PermissionMode permission, T target,
                                                                         IRoutingAttribute routing,
                                                                         IObjectAttribute objectAttribute,
                                                                         PropertyInfo[] properties)
            where T : IAlbianObject
        {
            if (null == routing)
            {
                throw new ArgumentNullException("routing");
            }
            if (null == properties || 0 == properties.Length)
            {
                throw new ArgumentNullException("properties");
            }
            if (null == objectAttribute)
            {
                throw new ArgumentNullException("objectAttribute");
            }
            if (0 == (permission & routing.Permission))
            {
                if (null != Logger)
                {
                    Logger.WarnFormat("The routing permission {0} is no enough.", permission);
                }
                return(null);
            }



            //create the connection string
            IStorageAttribute storageAttr = (IStorageAttribute)StorageCache.Get(routing.StorageName);

            if (null == storageAttr)
            {
                if (null != Logger)
                {
                    Logger.WarnFormat(
                        "No {0} rounting mapping storage attribute in the sotrage cache.Use default storage.",
                        routing.Name);
                }
                storageAttr = (IStorageAttribute)StorageCache.Get(StorageParser.DefaultStorageName);
            }

            if (!storageAttr.IsHealth)
            {
                if (null != Logger)
                {
                    Logger.WarnFormat("Routing:{0},Storage:{1} is not health.", routing.Name, storageAttr.Name);
                }
                return(null);
            }

            var sbInsert = new StringBuilder();
            var sbCols   = new StringBuilder();
            var sbValues = new StringBuilder();

            IList <DbParameter> paras = new List <DbParameter>();

            //create the hash table name
            string tableFullName = Utils.GetTableFullName(routing, target);

            //build the command text
            IDictionary <string, IMemberAttribute> members = objectAttribute.MemberAttributes;

            foreach (PropertyInfo property in properties)
            {
                object value = property.GetValue(target, null);
                if (null == value)
                {
                    continue;
                }
                IMemberAttribute member = members[property.Name];
                if (!member.IsSave)
                {
                    continue;
                }
                sbCols.AppendFormat("{0},", member.FieldName);
                string paraName = DatabaseFactory.GetParameterName(storageAttr.DatabaseStyle, member.FieldName);
                sbValues.AppendFormat("{0},", paraName);
                paras.Add(DatabaseFactory.GetDbParameter(storageAttr.DatabaseStyle, paraName, member.DBType, value,
                                                         member.Length));
            }
            int colsLen = sbCols.Length;

            if (0 < colsLen)
            {
                sbCols.Remove(colsLen - 1, 1);
            }
            int valLen = sbValues.Length;

            if (0 < valLen)
            {
                sbValues.Remove(valLen - 1, 1);
            }
            sbInsert.AppendFormat("INSERT INTO {0} ({1}) VALUES({2}) ", tableFullName, sbCols, sbValues);
            IFakeCommandAttribute fakeCmd = new FakeCommandAttribute
            {
                CommandText = sbInsert.ToString(),
                Paras       = ((List <DbParameter>)paras).ToArray(),
                StorageName = routing.StorageName
            };

            return(fakeCmd);
        }
Esempio n. 27
0
        public void _08_AttributeArrayAttributeTest()
        {
            IObjectAttribute nestedAttribute1 = Settings.Factories.ObjectAttributeFactory.CreateObjectAttribute(CKA.CKA_TOKEN, true);
            IObjectAttribute nestedAttribute2 = Settings.Factories.ObjectAttributeFactory.CreateObjectAttribute(CKA.CKA_PRIVATE, true);

            List <IObjectAttribute> originalValue = new List <IObjectAttribute>();

            originalValue.Add(nestedAttribute1);
            originalValue.Add(nestedAttribute2);

            // Create attribute with attribute array value
            using (IObjectAttribute attr = Settings.Factories.ObjectAttributeFactory.CreateObjectAttribute(CKA.CKA_WRAP_TEMPLATE, originalValue))
            {
                Assert.IsTrue(attr.Type == ConvertUtils.UInt64FromCKA(CKA.CKA_WRAP_TEMPLATE));

                List <IObjectAttribute> recoveredValue = attr.GetValueAsObjectAttributeList();
                Assert.IsTrue(recoveredValue.Count == 2);
                Assert.IsTrue(recoveredValue[0].Type == ConvertUtils.UInt64FromCKA(CKA.CKA_TOKEN));
                Assert.IsTrue(recoveredValue[0].GetValueAsBool() == true);
                Assert.IsTrue(recoveredValue[1].Type == ConvertUtils.UInt64FromCKA(CKA.CKA_PRIVATE));
                Assert.IsTrue(recoveredValue[1].GetValueAsBool() == true);
            }

            if (Platform.NativeULongSize == 4)
            {
                if (Platform.StructPackingSize == 0)
                {
                    // There is the same pointer to unmanaged memory in both nestedAttribute1 and recoveredValue[0] instances
                    // therefore private low level attribute structure needs to be modified to prevent double free.
                    // This special handling is needed only in this synthetic test and should be avoided in real world application.
                    HLA40.ObjectAttribute objectAttribute40a = (HLA40.ObjectAttribute)nestedAttribute1;
                    LLA40.CK_ATTRIBUTE    ckAttribute40a     = (LLA40.CK_ATTRIBUTE) typeof(HLA40.ObjectAttribute).GetField("_ckAttribute", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(objectAttribute40a);
                    ckAttribute40a.value    = IntPtr.Zero;
                    ckAttribute40a.valueLen = 0;
                    typeof(HLA40.ObjectAttribute).GetField("_ckAttribute", BindingFlags.NonPublic | BindingFlags.Instance).SetValue(objectAttribute40a, ckAttribute40a);

                    // There is the same pointer to unmanaged memory in both nestedAttribute2 and recoveredValue[1] instances
                    // therefore private low level attribute structure needs to be modified to prevent double free.
                    // This special handling is needed only in this synthetic test and should be avoided in real world application.
                    HLA40.ObjectAttribute objectAttribute40b = (HLA40.ObjectAttribute)nestedAttribute2;
                    LLA40.CK_ATTRIBUTE    ckAttribute40b     = (LLA40.CK_ATTRIBUTE) typeof(HLA40.ObjectAttribute).GetField("_ckAttribute", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(objectAttribute40b);
                    ckAttribute40b.value    = IntPtr.Zero;
                    ckAttribute40b.valueLen = 0;
                    typeof(HLA40.ObjectAttribute).GetField("_ckAttribute", BindingFlags.NonPublic | BindingFlags.Instance).SetValue(objectAttribute40b, ckAttribute40b);
                }
                else
                {
                    // There is the same pointer to unmanaged memory in both nestedAttribute1 and recoveredValue[0] instances
                    // therefore private low level attribute structure needs to be modified to prevent double free.
                    // This special handling is needed only in this synthetic test and should be avoided in real world application.
                    HLA41.ObjectAttribute objectAttribute41a = (HLA41.ObjectAttribute)nestedAttribute1;
                    LLA41.CK_ATTRIBUTE    ckAttribute41a     = (LLA41.CK_ATTRIBUTE) typeof(HLA41.ObjectAttribute).GetField("_ckAttribute", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(objectAttribute41a);
                    ckAttribute41a.value    = IntPtr.Zero;
                    ckAttribute41a.valueLen = 0;
                    typeof(HLA41.ObjectAttribute).GetField("_ckAttribute", BindingFlags.NonPublic | BindingFlags.Instance).SetValue(objectAttribute41a, ckAttribute41a);

                    // There is the same pointer to unmanaged memory in both nestedAttribute2 and recoveredValue[1] instances
                    // therefore private low level attribute structure needs to be modified to prevent double free.
                    // This special handling is needed only in this synthetic test and should be avoided in real world application.
                    HLA41.ObjectAttribute objectAttribute41b = (HLA41.ObjectAttribute)nestedAttribute2;
                    LLA41.CK_ATTRIBUTE    ckAttribute41b     = (LLA41.CK_ATTRIBUTE) typeof(HLA41.ObjectAttribute).GetField("_ckAttribute", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(objectAttribute41b);
                    ckAttribute41b.value    = IntPtr.Zero;
                    ckAttribute41b.valueLen = 0;
                    typeof(HLA41.ObjectAttribute).GetField("_ckAttribute", BindingFlags.NonPublic | BindingFlags.Instance).SetValue(objectAttribute41b, ckAttribute41b);
                }
            }
            else
            {
                if (Platform.StructPackingSize == 0)
                {
                    // There is the same pointer to unmanaged memory in both nestedAttribute1 and recoveredValue[0] instances
                    // therefore private low level attribute structure needs to be modified to prevent double free.
                    // This special handling is needed only in this synthetic test and should be avoided in real world application.
                    HLA80.ObjectAttribute objectAttribute80a = (HLA80.ObjectAttribute)nestedAttribute1;
                    LLA80.CK_ATTRIBUTE    ckAttribute80a     = (LLA80.CK_ATTRIBUTE) typeof(HLA80.ObjectAttribute).GetField("_ckAttribute", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(objectAttribute80a);
                    ckAttribute80a.value    = IntPtr.Zero;
                    ckAttribute80a.valueLen = 0;
                    typeof(HLA80.ObjectAttribute).GetField("_ckAttribute", BindingFlags.NonPublic | BindingFlags.Instance).SetValue(objectAttribute80a, ckAttribute80a);

                    // There is the same pointer to unmanaged memory in both nestedAttribute2 and recoveredValue[1] instances
                    // therefore private low level attribute structure needs to be modified to prevent double free.
                    // This special handling is needed only in this synthetic test and should be avoided in real world application.
                    HLA80.ObjectAttribute objectAttribute80b = (HLA80.ObjectAttribute)nestedAttribute2;
                    LLA80.CK_ATTRIBUTE    ckAttribute80b     = (LLA80.CK_ATTRIBUTE) typeof(HLA80.ObjectAttribute).GetField("_ckAttribute", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(objectAttribute80b);
                    ckAttribute80b.value    = IntPtr.Zero;
                    ckAttribute80b.valueLen = 0;
                    typeof(HLA80.ObjectAttribute).GetField("_ckAttribute", BindingFlags.NonPublic | BindingFlags.Instance).SetValue(objectAttribute80b, ckAttribute80b);
                }
                else
                {
                    // There is the same pointer to unmanaged memory in both nestedAttribute1 and recoveredValue[0] instances
                    // therefore private low level attribute structure needs to be modified to prevent double free.
                    // This special handling is needed only in this synthetic test and should be avoided in real world application.
                    HLA81.ObjectAttribute objectAttribute81a = (HLA81.ObjectAttribute)nestedAttribute1;
                    LLA81.CK_ATTRIBUTE    ckAttribute81a     = (LLA81.CK_ATTRIBUTE) typeof(HLA81.ObjectAttribute).GetField("_ckAttribute", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(objectAttribute81a);
                    ckAttribute81a.value    = IntPtr.Zero;
                    ckAttribute81a.valueLen = 0;
                    typeof(HLA81.ObjectAttribute).GetField("_ckAttribute", BindingFlags.NonPublic | BindingFlags.Instance).SetValue(objectAttribute81a, ckAttribute81a);

                    // There is the same pointer to unmanaged memory in both nestedAttribute2 and recoveredValue[1] instances
                    // therefore private low level attribute structure needs to be modified to prevent double free.
                    // This special handling is needed only in this synthetic test and should be avoided in real world application.
                    HLA81.ObjectAttribute objectAttribute81b = (HLA81.ObjectAttribute)nestedAttribute2;
                    LLA81.CK_ATTRIBUTE    ckAttribute81b     = (LLA81.CK_ATTRIBUTE) typeof(HLA81.ObjectAttribute).GetField("_ckAttribute", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(objectAttribute81b);
                    ckAttribute81b.value    = IntPtr.Zero;
                    ckAttribute81b.valueLen = 0;
                    typeof(HLA81.ObjectAttribute).GetField("_ckAttribute", BindingFlags.NonPublic | BindingFlags.Instance).SetValue(objectAttribute81b, ckAttribute81b);
                }
            }

            originalValue = null;

            // Create attribute with null attribute array value
            using (IObjectAttribute attr = Settings.Factories.ObjectAttributeFactory.CreateObjectAttribute(CKA.CKA_WRAP_TEMPLATE, originalValue))
            {
                Assert.IsTrue(attr.Type == ConvertUtils.UInt64FromCKA(CKA.CKA_WRAP_TEMPLATE));
                Assert.IsTrue(attr.GetValueAsObjectAttributeList() == originalValue);
            }

            originalValue = new List <IObjectAttribute>();

            // Create attribute with empty attribute array value
            using (IObjectAttribute attr = Settings.Factories.ObjectAttributeFactory.CreateObjectAttribute(CKA.CKA_WRAP_TEMPLATE, originalValue))
            {
                Assert.IsTrue(attr.Type == ConvertUtils.UInt64FromCKA(CKA.CKA_WRAP_TEMPLATE));
                Assert.IsTrue(attr.GetValueAsObjectAttributeList() == null);
            }
        }
 internal ListenedElement(IObjectAttribute LO, PropertyInfo pi)
 {
     ListenedObject = LO;
     _Properties.Add(pi.Name,pi);
 }
Esempio n. 29
0
        private static IObjectAttribute GetDefaultAttribute(ulong type, string defaultValue)
        {
            IObjectAttribute objectAttribute = null;

            IObjectAttributeFactory objectAttributeFactory = Pkcs11Admin.Instance.Factories.ObjectAttributeFactory;

            if (defaultValue == null)
            {
                objectAttribute = objectAttributeFactory.Create(type);
            }
            else
            {
                if (defaultValue.StartsWith("ULONG:"))
                {
                    string ulongString = defaultValue.Substring("ULONG:".Length);
                    ulong  ulongValue  = Convert.ToUInt64(ulongString);
                    objectAttribute = objectAttributeFactory.Create(type, ulongValue);
                }
                else if (defaultValue.StartsWith("BOOL:"))
                {
                    string boolString = defaultValue.Substring("BOOL:".Length);

                    bool boolValue = false;
                    if (0 == string.Compare(boolString, "TRUE", true))
                    {
                        boolValue = true;
                    }
                    else if (0 == string.Compare(boolString, "FALSE", true))
                    {
                        boolValue = false;
                    }
                    else
                    {
                        throw new Exception("Unable to parse default value of class attribute");
                    }

                    objectAttribute = objectAttributeFactory.Create(type, boolValue);
                }
                else if (defaultValue.StartsWith("STRING:"))
                {
                    string strValue = defaultValue.Substring("STRING:".Length);
                    objectAttribute = objectAttributeFactory.Create(type, strValue);
                }
                else if (defaultValue.StartsWith("BYTES:"))
                {
                    string hexString = defaultValue.Substring("BYTES:".Length);
                    byte[] bytes     = ConvertUtils.HexStringToBytes(hexString);
                    objectAttribute = objectAttributeFactory.Create(type, bytes);
                }
                else if (defaultValue.StartsWith("DATE:"))
                {
                    // TODO
                    throw new NotImplementedException();
                }
                else
                {
                    throw new Exception("Unable to parse default value of class attribute");
                }
            }

            return(objectAttribute);
        }
Esempio n. 30
0
        public List <Tuple <IObjectAttribute, ClassAttribute> > ImportCertificate(string fileName, byte[] fileContent)
        {
            IObjectAttributeFactory objectAttributeFactory = Pkcs11Admin.Instance.Factories.ObjectAttributeFactory;

            X509CertificateParser x509CertificateParser = new X509CertificateParser();
            X509Certificate       x509Certificate       = x509CertificateParser.ReadCertificate(fileContent);

            List <Tuple <IObjectAttribute, ClassAttribute> > objectAttributes = StringUtils.GetCreateDefaultAttributes(Pkcs11Admin.Instance.Config.CertificateAttributes, (ulong)CKC.CKC_X_509);

            for (int i = 0; i < objectAttributes.Count; i++)
            {
                IObjectAttribute objectAttribute = objectAttributes[i].Item1;
                ClassAttribute   classAttribute  = objectAttributes[i].Item2;

                if (objectAttribute.Type == (ulong)CKA.CKA_LABEL)
                {
                    string label = fileName;
                    Dictionary <string, List <string> > subject = Utils.ParseX509Name(x509Certificate.SubjectDN);
                    if (subject.ContainsKey(X509ObjectIdentifiers.CommonName.Id) && (subject[X509ObjectIdentifiers.CommonName.Id].Count > 0))
                    {
                        label = subject[X509ObjectIdentifiers.CommonName.Id][0];
                    }

                    objectAttributes[i] = new Tuple <IObjectAttribute, ClassAttribute>(objectAttributeFactory.Create(CKA.CKA_LABEL, label), classAttribute);
                }
                else if (objectAttribute.Type == (ulong)CKA.CKA_START_DATE)
                {
                    objectAttributes[i] = new Tuple <IObjectAttribute, ClassAttribute>(objectAttributeFactory.Create(CKA.CKA_START_DATE, x509Certificate.NotBefore), classAttribute);
                }
                else if (objectAttribute.Type == (ulong)CKA.CKA_END_DATE)
                {
                    objectAttributes[i] = new Tuple <IObjectAttribute, ClassAttribute>(objectAttributeFactory.Create(CKA.CKA_END_DATE, x509Certificate.NotAfter), classAttribute);
                }
                else if (objectAttribute.Type == (ulong)CKA.CKA_SUBJECT)
                {
                    objectAttributes[i] = new Tuple <IObjectAttribute, ClassAttribute>(objectAttributeFactory.Create(CKA.CKA_SUBJECT, x509Certificate.SubjectDN.GetDerEncoded()), classAttribute);
                }
                else if (objectAttribute.Type == (ulong)CKA.CKA_ID)
                {
                    byte[] thumbPrint = null;
                    using (SHA1Managed sha1Managed = new SHA1Managed())
                        thumbPrint = sha1Managed.ComputeHash(x509Certificate.GetEncoded());

                    objectAttributes[i] = new Tuple <IObjectAttribute, ClassAttribute>(objectAttributeFactory.Create(CKA.CKA_ID, thumbPrint), classAttribute);
                }
                else if (objectAttribute.Type == (ulong)CKA.CKA_ISSUER)
                {
                    objectAttributes[i] = new Tuple <IObjectAttribute, ClassAttribute>(objectAttributeFactory.Create(CKA.CKA_ISSUER, x509Certificate.IssuerDN.GetDerEncoded()), classAttribute);
                }
                else if (objectAttribute.Type == (ulong)CKA.CKA_SERIAL_NUMBER)
                {
                    objectAttributes[i] = new Tuple <IObjectAttribute, ClassAttribute>(objectAttributeFactory.Create(CKA.CKA_SERIAL_NUMBER, new DerInteger(x509Certificate.SerialNumber).GetDerEncoded()), classAttribute);
                }
                else if (objectAttribute.Type == (ulong)CKA.CKA_VALUE)
                {
                    objectAttributes[i] = new Tuple <IObjectAttribute, ClassAttribute>(objectAttributeFactory.Create(CKA.CKA_VALUE, x509Certificate.GetEncoded()), classAttribute);
                }
            }

            return(objectAttributes);
        }
Esempio n. 31
0
        public static void GetAttributeNameAndValue(IObjectAttribute objectAttribute, out string name, out string value)
        {
            if (objectAttribute == null)
            {
                throw new ArgumentNullException("objectAttribute");
            }

            string tmpName  = null;
            string tmpValue = null;

            // Find attribute definition in configuration
            AttributeDefinition pkcs11Attribute = null;

            if (Pkcs11Admin.Instance.Config.AttributeDefinitions.ContainsKey(objectAttribute.Type))
            {
                pkcs11Attribute = Pkcs11Admin.Instance.Config.AttributeDefinitions[objectAttribute.Type];
            }

            // Determine attribute name
            if (pkcs11Attribute == null)
            {
                tmpName = string.Format("Unknown ({0})", objectAttribute.Type.ToString());
            }
            else
            {
                tmpName = pkcs11Attribute.Name;
            }

            // Determine attribute value
            if (pkcs11Attribute == null)
            {
                if (objectAttribute.CannotBeRead)
                {
                    tmpValue = "<unextractable>";
                }
                else
                {
                    tmpValue = BytesToHexString(objectAttribute.GetValueAsByteArray());
                }
            }
            else
            {
                if (objectAttribute.CannotBeRead)
                {
                    tmpValue = "<unextractable>";
                }
                else
                {
                    // TODO - More robust conversions
                    switch (pkcs11Attribute.Type)
                    {
                    case AttributeType.Bool:

                        tmpValue = objectAttribute.GetValueAsBool().ToString();

                        break;

                    case AttributeType.ByteArray:

                        tmpValue = BytesToHexString(objectAttribute.GetValueAsByteArray());

                        break;

                    case AttributeType.DateTime:

                        DateTime?dateTime = objectAttribute.GetValueAsDateTime();
                        tmpValue = (dateTime == null) ? null : dateTime.Value.ToShortDateString();

                        break;

                    case AttributeType.String:

                        tmpValue = objectAttribute.GetValueAsString();

                        break;

                    case AttributeType.ULong:

                        tmpValue = GetAttributeEnumValue(pkcs11Attribute, objectAttribute.GetValueAsUlong(), false);

                        break;

                    case AttributeType.AttributeArray:
                    case AttributeType.MechanismArray:
                    case AttributeType.ULongArray:

                        tmpValue = "<unsupported>";     // TODO

                        break;

                    default:

                        tmpValue = "<unknown>";

                        break;
                    }

                    if (string.IsNullOrEmpty(tmpValue))
                    {
                        tmpValue = "<empty>";
                    }
                }
            }

            // Set output parameters
            name  = tmpName;
            value = tmpValue;
        }
Esempio n. 32
0
 public virtual void Compare(IObjectAttribute comparator)
 {
     _config.Put(QueryAttributeProviderKey, comparator);
 }
 private void Register(IObjectAttribute io, PropertyInfo myProperty)
 {
     var res = _ObjectAttributePropertiesListened.FindOrCreate(io, o => new ListenedElement(o, myProperty));
     if (res.CollectionStatus == CollectionStatus.Find)
     {
         res.Item.Add(myProperty);
     }
 }
            //private ListenedElementCollection<Tor, TDes> Result
            //{
            //    get
            //    {
            //        return _Result;
            //    }
            //}

            public void Visit(IObjectAttribute io, PropertyInfo myProperty, bool Isparameter)
            {
                _Result.Register(io, myProperty);
            }
Esempio n. 35
0
		public virtual void Compare(IObjectAttribute comparator)
		{
			_config.Put(QueryAttributeProviderKey, comparator);
		}
Esempio n. 36
0
        public IDictionary <string, IStorageContext> GenerateFakeCommandByRoutings <T>(T target, PropertyInfo[] properties,
                                                                                       IObjectAttribute objectAttribute,
                                                                                       BuildFakeCommandByRoutingHandler <T>
                                                                                       buildFakeCommandByRoutingHandler)
            where T : IAlbianObject
        {
            if (null == properties || 0 == properties.Length)
            {
                throw new ArgumentNullException("properties");
            }
            if (null == objectAttribute)
            {
                throw new ArgumentNullException("objectAttribute");
            }
            if (null == objectAttribute.RoutingAttributes || null == objectAttribute.RoutingAttributes.Values ||
                0 == objectAttribute.RoutingAttributes.Values.Count)
            {
                if (null != Logger)
                {
                    Logger.Error("The routing attributes or routings is null");
                }
                throw new Exception("The routing attributes or routing is null");
            }

            IDictionary <string, IStorageContext> storageContexts = new Dictionary <string, IStorageContext>();

            foreach (var routing in objectAttribute.RoutingAttributes.Values)
            {
                IFakeCommandAttribute fakeCommandAttrribute = buildFakeCommandByRoutingHandler(PermissionMode.W, target,
                                                                                               routing, objectAttribute,
                                                                                               properties);
                if (null == fakeCommandAttrribute) //the PermissionMode is not enough
                {
                    if (null != Logger)
                    {
                        Logger.WarnFormat("The permission is not enough in the {0} routing.", routing.Name);
                    }
                    continue;
                }
                if (storageContexts.ContainsKey(fakeCommandAttrribute.StorageName))
                {
                    storageContexts[fakeCommandAttrribute.StorageName].FakeCommand.Add(fakeCommandAttrribute);
                }
                else
                {
                    IStorageContext storageContext = new StorageContext
                    {
                        FakeCommand = new List <IFakeCommandAttribute>(),
                        StorageName = fakeCommandAttrribute.StorageName,
                    };
                    storageContext.FakeCommand.Add(fakeCommandAttrribute);
                    storageContexts.Add(fakeCommandAttrribute.StorageName, storageContext);
                }
            }
            return(storageContexts);
        }