Example #1
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);
            }
        }
Example #2
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;
        }