Example #1
0
        internal static SafeEvpPkeyHandle DuplicateHandle(SafeEvpPkeyHandle handle)
        {
            Debug.Assert(handle != null && !handle.IsInvalid);

            // Reliability: Allocate the SafeHandle before calling UpRefEvpPkey so
            // that we don't lose a tracked reference in low-memory situations.
            SafeEvpPkeyHandle safeHandle = new SafeEvpPkeyHandle();

            int newRefCount = Interop.NativeCrypto.UpRefEvpPkey(handle);

            // UpRefEvpPkey returns the number of references to this key, if it's less than 2
            // (the incoming handle, and this one) then someone has already Disposed() this key
            // into non-existence.
            if (newRefCount < 2)
            {
                Debug.Fail("Called UpRefEvpPkey on a key which was already marked for destruction");
                throw Interop.libcrypto.CreateOpenSslCryptographicException();
            }

            // Since we didn't actually create a new handle, copy the handle
            // to the new SafeHandle.
            safeHandle.SetHandle(handle.DangerousGetHandle());
            return safeHandle;
        }
Example #2
0
        internal static SafeEvpPkeyHandle DuplicateHandle(SafeEvpPkeyHandle handle)
        {
            Debug.Assert(handle != null && !handle.IsInvalid);

            // Reliability: Allocate the SafeHandle before calling UpRefEvpPkey so
            // that we don't lose a tracked reference in low-memory situations.
            SafeEvpPkeyHandle safeHandle = new SafeEvpPkeyHandle();

            int newRefCount = Interop.Crypto.UpRefEvpPkey(handle);

            // UpRefEvpPkey returns the number of references to this key, if it's less than 2
            // (the incoming handle, and this one) then someone has already Disposed() this key
            // into non-existence.
            if (newRefCount < 2)
            {
                Debug.Fail("Called UpRefEvpPkey on a key which was already marked for destruction");
                throw Interop.libcrypto.CreateOpenSslCryptographicException();
            }

            // Since we didn't actually create a new handle, copy the handle
            // to the new SafeHandle.
            safeHandle.SetHandle(handle.DangerousGetHandle());
            return(safeHandle);
        }