Net.Pkcs11Interop.HighLevelAPI81.Session.UnwrapKey C# (CSharp) Method

UnwrapKey() public method

Unwraps (i.e. decrypts) a wrapped key, creating a new private key or secret key object
public UnwrapKey ( Mechanism mechanism, ObjectHandle unwrappingKeyHandle, byte wrappedKey, List attributes ) : ObjectHandle
mechanism Mechanism Unwrapping mechanism
unwrappingKeyHandle ObjectHandle Handle of unwrapping key
wrappedKey byte Wrapped key
attributes List Attributes for unwrapped key
return ObjectHandle
        public ObjectHandle UnwrapKey(Mechanism mechanism, ObjectHandle unwrappingKeyHandle, byte[] wrappedKey, List<ObjectAttribute> attributes)
        {
            if (this._disposed)
                throw new ObjectDisposedException(this.GetType().FullName);

            if (mechanism == null)
                throw new ArgumentNullException("mechanism");
            
            if (unwrappingKeyHandle == null)
                throw new ArgumentNullException("unwrappingKeyHandle");
            
            if (wrappedKey == null)
                throw new ArgumentNullException("wrappedKey");

            CK_MECHANISM ckMechanism = mechanism.CkMechanism;

            CK_ATTRIBUTE[] template = null;
            ulong templateLen = 0;
            if (attributes != null)
            {
                template = new CK_ATTRIBUTE[attributes.Count];
                for (int i = 0; i < attributes.Count; i++)
                    template[i] = attributes[i].CkAttribute;
                templateLen = Convert.ToUInt64(attributes.Count);
            }

            ulong unwrappedKey = CK.CK_INVALID_HANDLE;
            CKR rv = _p11.C_UnwrapKey(_sessionId, ref ckMechanism, unwrappingKeyHandle.ObjectId, wrappedKey, Convert.ToUInt64(wrappedKey.Length), template, templateLen, ref unwrappedKey);
            if (rv != CKR.CKR_OK)
                throw new Pkcs11Exception("C_UnwrapKey", rv);

            return new ObjectHandle(unwrappedKey);
        }