Net.Pkcs11Interop.LowLevelAPI40.CkaUtils.ConvertValue C# (CSharp) Method

ConvertValue() private static method

Copies attribute value from unmanaged memory to managed byte array
private static ConvertValue ( CK_ATTRIBUTE &attribute ) : byte[]
attribute CK_ATTRIBUTE Attribute whose value should be read
return byte[]
        private static byte[] ConvertValue(ref CK_ATTRIBUTE attribute)
        {
            byte[] value = null;

            if (attribute.value != IntPtr.Zero)
                value = UnmanagedMemory.Read(attribute.value, Convert.ToInt32(attribute.valueLen));

            return value;
        }

Same methods

CkaUtils::ConvertValue ( CK_ATTRIBUTE &attribute, CKM &value ) : void
CkaUtils::ConvertValue ( CK_ATTRIBUTE &attribute, CK_ATTRIBUTE &value ) : void
CkaUtils::ConvertValue ( CK_ATTRIBUTE &attribute, System.DateTime &value ) : void
CkaUtils::ConvertValue ( CK_ATTRIBUTE &attribute, bool &value ) : void
CkaUtils::ConvertValue ( CK_ATTRIBUTE &attribute, byte &value ) : void
CkaUtils::ConvertValue ( CK_ATTRIBUTE &attribute, string &value ) : void
CkaUtils::ConvertValue ( CK_ATTRIBUTE &attribute, uint &value ) : void

Usage Example

        /// <summary>
        /// Checks whether object attributes match PKCS#11 URI
        /// </summary>
        /// <param name="pkcs11Uri">PKCS#11 URI</param>
        /// <param name="objectAttributes">Object attributes</param>
        /// <returns>True if object attributes match PKCS#11 URI</returns>
        public static bool Matches(Pkcs11Uri pkcs11Uri, List <CK_ATTRIBUTE> objectAttributes)
        {
            if (pkcs11Uri == null)
            {
                throw new ArgumentNullException("pkcs11Uri");
            }

            if (objectAttributes == null)
            {
                throw new ArgumentNullException("objectAttributes");
            }

            NativeULong ckaClassType  = ConvertUtils.UInt32FromCKA(CKA.CKA_CLASS);
            CKO?        ckaClassValue = null;
            bool        ckaClassFound = false;

            NativeULong ckaLabelType  = ConvertUtils.UInt32FromCKA(CKA.CKA_LABEL);
            string      ckaLabelValue = null;
            bool        ckaLabelFound = false;

            NativeULong ckaIdType = ConvertUtils.UInt32FromCKA(CKA.CKA_ID);

            byte[] ckaIdValue = null;
            bool   ckaIdFound = false;

            foreach (CK_ATTRIBUTE objectAttribute in objectAttributes)
            {
                CK_ATTRIBUTE attribute = objectAttribute;

                if (attribute.type == ckaClassType)
                {
                    NativeULong nativeUlongValue = 0;
                    CkaUtils.ConvertValue(ref attribute, out nativeUlongValue);
                    ckaClassValue = ConvertUtils.UInt32ToCKO(nativeUlongValue);
                    ckaClassFound = true;
                }
                else if (attribute.type == ckaLabelType)
                {
                    CkaUtils.ConvertValue(ref attribute, out ckaLabelValue);
                    ckaLabelFound = true;
                }
                else if (objectAttribute.type == ckaIdType)
                {
                    CkaUtils.ConvertValue(ref attribute, out ckaIdValue);
                    ckaIdFound = true;
                }

                if (ckaClassFound && ckaLabelFound && ckaIdFound)
                {
                    break;
                }
            }

            if ((!ckaClassFound) && (pkcs11Uri.Type != null))
            {
                throw new Pkcs11UriException("CKA_CLASS attribute is not present in the list of object attributes");
            }

            if ((!ckaLabelFound) && (pkcs11Uri.Object != null))
            {
                throw new Pkcs11UriException("CKA_LABEL attribute is not present in the list of object attributes");
            }

            if ((!ckaIdFound) && (pkcs11Uri.Id != null))
            {
                throw new Pkcs11UriException("CKA_ID attribute is not present in the list of object attributes");
            }

            return(Pkcs11UriSharedUtils.Matches(pkcs11Uri, ckaClassValue, ckaLabelValue, ckaIdValue));
        }