예제 #1
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);
            }
        }
예제 #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;
        }