| | | 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 | | |
| | | 6 | | using System; |
| | | 7 | | using System.Diagnostics; |
| | | 8 | | using static Interop.Crypt32; |
| | | 9 | | |
| | | 10 | | namespace 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 | | |
| | 0 | 19 | | 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() |
| | 0 | 36 | | { |
| | 0 | 37 | | if (_parent != null) |
| | 0 | 38 | | { |
| | 0 | 39 | | _parent.DangerousRelease(); |
| | 0 | 40 | | _parent = null; |
| | 0 | 41 | | } |
| | | 42 | | else |
| | 0 | 43 | | { |
| | 0 | 44 | | CertFreeCertificateContext(handle); |
| | 0 | 45 | | } |
| | | 46 | | |
| | 0 | 47 | | SetHandle(IntPtr.Zero); |
| | 0 | 48 | | return true; |
| | 0 | 49 | | } |
| | | 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 | | { |
| | 0 | 71 | | 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) |
| | 0 | 85 | | { |
| | 0 | 86 | | int cb = 0; |
| | 0 | 87 | | bool hasProperty = CertGetCertificateContextProperty( |
| | 0 | 88 | | this, |
| | 0 | 89 | | propertyId, |
| | 0 | 90 | | null, |
| | 0 | 91 | | ref cb); |
| | | 92 | | |
| | 0 | 93 | | return hasProperty; |
| | 0 | 94 | | } |
| | | 95 | | } |
| | | 96 | | } |
| | | 97 | | |