internal static X509Certificate2Collection GetStore(SafeFreeCertContext certContext) { X509Certificate2Collection result = new X509Certificate2Collection(); if (certContext.IsInvalid) { return(result); } _CERT_CONTEXT context = (_CERT_CONTEXT)Marshal.PtrToStructure(certContext.DangerousGetHandle(), typeof(_CERT_CONTEXT)); if (context.hCertStore != IntPtr.Zero) { X509Store store = null; try { store = new X509Store(context.hCertStore); result = store.Certificates; } finally { if (store != null) { store.Close(); } } } return(result); }
internal X509Certificate2 GetRemoteCertificate(out X509Certificate2Collection remoteCertificateStore) { remoteCertificateStore = null; if (this.m_SecurityContext == null) { return(null); } X509Certificate2 certificate = null; SafeFreeCertContext certContext = null; try { certContext = SSPIWrapper.QueryContextAttributes(GlobalSSPI.SSPISecureChannel, this.m_SecurityContext, ContextAttribute.RemoteCertificate) as SafeFreeCertContext; if ((certContext != null) && !certContext.IsInvalid) { certificate = new X509Certificate2(certContext.DangerousGetHandle()); } } finally { if (certContext != null) { remoteCertificateStore = UnmanagedCertificateContext.GetStore(certContext); certContext.Close(); } } if (Logging.On) { Logging.PrintInfo(Logging.Web, SR.GetString("net_log_remote_certificate", new object[] { (certificate == null) ? "null" : certificate.ToString(true) })); } return(certificate); }
//This method extracts a remote certificate and chain upon request. private void ExtractRemoteCertificate() { SafeFreeCertContext remoteContext = null; this.remoteCertificate = null; this.remoteCertificateChain = null; try { remoteContext = ExtractCertificateHandle(ContextAttribute.RemoteCertificate); if (remoteContext != null && !remoteContext.IsInvalid) { this.remoteCertificateChain = UnmanagedCertificateContext.GetStore(remoteContext); this.remoteCertificate = new X509Certificate2(remoteContext.DangerousGetHandle()); } } finally { if (remoteContext != null) { remoteContext.Close(); } } }
private SafeFreeCertContext ExtractCertificateHandle(ContextAttribute contextAttribute) { SafeFreeCertContext result = SspiWrapper.QueryContextAttributes(this.securityContext, contextAttribute) as SafeFreeCertContext; return(result); }
internal static X509Certificate2Collection GetRemoteCertificatesFromStoreContext(SafeFreeCertContext certContext) { X509Certificate2Collection result = new X509Certificate2Collection(); if (certContext.IsInvalid) { return result; } Interop.Crypt32.CERT_CONTEXT context = Marshal.PtrToStructure<Interop.Crypt32.CERT_CONTEXT>(certContext.DangerousGetHandle()); if (context.hCertStore != IntPtr.Zero) { X509Store store = null; try { store = X509StoreExtensions.CreateFromNativeHandle(context.hCertStore); result = store.Certificates; } finally { if (store != null) { store.Dispose(); } } } return result; }
static unsafe int QueryContextAttributes(SafeDeleteContext phContext, ContextAttribute attribute, byte[] buffer, Type handleType, out SafeHandle refHandle) { refHandle = null; if (handleType != null) { if (handleType == typeof(SafeFreeContextBuffer)) { refHandle = SafeFreeContextBuffer.CreateEmptyHandle(); } else if (handleType == typeof(SafeFreeCertContext)) { refHandle = new SafeFreeCertContext(); } else { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("handleType", SR.GetString(SR.ValueMustBeOf2Types, typeof(SafeFreeContextBuffer).ToString(), typeof(SafeFreeCertContext).ToString()))); } } fixed (byte* bufferPtr = buffer) { return SafeFreeContextBuffer.QueryContextAttributes(phContext, attribute, bufferPtr, refHandle); } }