< Summary

Line coverage
0%
Covered lines: 0
Uncovered lines: 23
Coverable lines: 23
Total lines: 97
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 2
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor()100%110%
ReleaseHandle()0%220%
CertHasProperty(...)100%110%

File(s)

https://raw.githubusercontent.com/dotnet/runtime/811a7eabb75c42db53440e8ba3f60c07511cfd1f/src/libraries/Common/src/Microsoft/Win32/SafeHandles/SafeCertContextHandle.cs

#LineLine coverage
 1// Licensed to the .NET Foundation under one or more agreements.
 2// The .NET Foundation licenses this file to you under the MIT license.
 3
 4#pragma warning disable CA1852 // some projects have types deriving from this
 5
 6using System;
 7using System.Diagnostics;
 8using static Interop.Crypt32;
 9
 10namespace Microsoft.Win32.SafeHandles
 11{
 12    /// <summary>
 13    /// SafeHandle for the CERT_CONTEXT structure defined by crypt32.
 14    /// </summary>
 15    internal class SafeCertContextHandle : SafeCrypt32Handle<SafeCertContextHandle>
 16    {
 17        private SafeCertContextHandle? _parent;
 18
 019        public SafeCertContextHandle() { }
 20
 21        public SafeCertContextHandle(SafeCertContextHandle parent)
 22        {
 23            ArgumentNullException.ThrowIfNull(parent);
 24
 25            Debug.Assert(!parent.IsInvalid);
 26            Debug.Assert(!parent.IsClosed);
 27
 28            bool ignored = false;
 29            parent.DangerousAddRef(ref ignored);
 30            _parent = parent;
 31
 32            SetHandle(_parent.handle);
 33        }
 34
 35        protected override bool ReleaseHandle()
 036        {
 037            if (_parent != null)
 038            {
 039                _parent.DangerousRelease();
 040                _parent = null;
 041            }
 42            else
 043            {
 044                CertFreeCertificateContext(handle);
 045            }
 46
 047            SetHandle(IntPtr.Zero);
 048            return true;
 049        }
 50
 51        public unsafe CERT_CONTEXT* DangerousCertContext
 52        {
 53            get { return (CERT_CONTEXT*)handle; }
 54        }
 55
 56        // Extract the raw CERT_CONTEXT* pointer and reset the SafeHandle to the invalid state so it no longer auto-dest
 57        public unsafe CERT_CONTEXT* Disconnect()
 58        {
 59            CERT_CONTEXT* pCertContext = (CERT_CONTEXT*)handle;
 60            SetHandle(IntPtr.Zero);
 61            return pCertContext;
 62        }
 63
 64        public bool HasPersistedPrivateKey
 65        {
 66            get { return CertHasProperty(CertContextPropId.CERT_KEY_PROV_INFO_PROP_ID); }
 67        }
 68
 69        public bool HasEphemeralPrivateKey
 70        {
 071            get { return CertHasProperty(CertContextPropId.CERT_KEY_CONTEXT_PROP_ID); }
 72        }
 73
 74        public bool ContainsPrivateKey
 75        {
 76            get { return HasPersistedPrivateKey || HasEphemeralPrivateKey; }
 77        }
 78
 79        public SafeCertContextHandle Duplicate()
 80        {
 81            return CertDuplicateCertificateContext(handle);
 82        }
 83
 84        private bool CertHasProperty(CertContextPropId propertyId)
 085        {
 086            int cb = 0;
 087            bool hasProperty = CertGetCertificateContextProperty(
 088                this,
 089                propertyId,
 090                null,
 091                ref cb);
 92
 093            return hasProperty;
 094        }
 95    }
 96}
 97