Exemplo n.º 1
0
        /// <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 <ObjectAttribute> objectAttributes)
        {
            if (pkcs11Uri == null)
            {
                throw new ArgumentNullException("pkcs11Uri");
            }

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

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

            NativeULong ckaLabelType  = NativeLongUtils.ConvertFromCKA(CKA.CKA_LABEL);
            string      ckaLabelValue = null;
            bool        ckaLabelFound = false;

            NativeULong ckaIdType = NativeLongUtils.ConvertFromCKA(CKA.CKA_ID);

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

            foreach (ObjectAttribute objectAttribute in objectAttributes)
            {
                if (objectAttribute == null)
                {
                    continue;
                }

                if (objectAttribute.Type == ckaClassType)
                {
                    ckaClassValue = NativeLongUtils.ConvertToCKO(objectAttribute.GetValueAsNativeUlong());
                    ckaClassFound = true;
                }
                else if (objectAttribute.Type == ckaLabelType)
                {
                    ckaLabelValue = objectAttribute.GetValueAsString();
                    ckaLabelFound = true;
                }
                else if (objectAttribute.Type == ckaIdType)
                {
                    ckaIdValue = objectAttribute.GetValueAsByteArray();
                    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));
        }