Code:
/ Net / Net / 3.5.50727.3053 / DEVDIV / depot / DevDiv / releases / whidbey / netfxsp / ndp / clr / src / ManagedLibraries / Security / System / Security / Cryptography / X509 / X509UI.cs / 5 / X509UI.cs
// ==++== // // Copyright (c) Microsoft Corporation. All rights reserved. // // ==--== // // X509UI.cs // namespace System.Security.Cryptography.X509Certificates { using System; using System.Globalization; using System.Runtime.InteropServices; using System.Security.Cryptography; using System.Security.Permissions; public enum X509SelectionFlag { SingleSelection = 0x00, MultiSelection = 0x01 } [System.Security.Permissions.HostProtection(MayLeakOnAbort = true)] public sealed class X509Certificate2UI { private X509Certificate2UI () {} public static void DisplayCertificate (X509Certificate2 certificate) { if (certificate == null) throw new ArgumentNullException("certificate"); DisplayX509Certificate(X509Utils.GetCertContext(certificate), IntPtr.Zero); } [SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags=SecurityPermissionFlag.UnmanagedCode)] [SecurityPermissionAttribute(SecurityAction.InheritanceDemand, Flags=SecurityPermissionFlag.UnmanagedCode)] public static void DisplayCertificate (X509Certificate2 certificate, IntPtr hwndParent) { if (certificate == null) throw new ArgumentNullException("certificate"); DisplayX509Certificate(X509Utils.GetCertContext(certificate), hwndParent); } public static X509Certificate2Collection SelectFromCollection (X509Certificate2Collection certificates, string title, string message, X509SelectionFlag selectionFlag) { return SelectFromCollectionHelper(certificates, title, message, selectionFlag, IntPtr.Zero); } [SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags=SecurityPermissionFlag.UnmanagedCode)] [SecurityPermissionAttribute(SecurityAction.InheritanceDemand, Flags=SecurityPermissionFlag.UnmanagedCode)] public static X509Certificate2Collection SelectFromCollection (X509Certificate2Collection certificates, string title, string message, X509SelectionFlag selectionFlag, IntPtr hwndParent) { return SelectFromCollectionHelper(certificates, title, message, selectionFlag, hwndParent); } private static void DisplayX509Certificate (SafeCertContextHandle safeCertContext, IntPtr hwndParent) { if (safeCertContext.IsInvalid) throw new CryptographicException(SecurityResources.GetResourceString("Cryptography_InvalidHandle"), "safeCertContext"); int dwErrorCode = CAPI.ERROR_SUCCESS; // Initialize view structure. CAPI.CRYPTUI_VIEWCERTIFICATE_STRUCTW ViewInfo = new CAPI.CRYPTUI_VIEWCERTIFICATE_STRUCTW(); ViewInfo.dwSize = (uint) Marshal.SizeOf(ViewInfo); ViewInfo.hwndParent = hwndParent; ViewInfo.dwFlags = 0; ViewInfo.szTitle = null; ViewInfo.pCertContext = safeCertContext.DangerousGetHandle(); ViewInfo.rgszPurposes = IntPtr.Zero; ViewInfo.cPurposes = 0; ViewInfo.pCryptProviderData = IntPtr.Zero; ViewInfo.fpCryptProviderDataTrustedUsage = false; ViewInfo.idxSigner = 0; ViewInfo.idxCert = 0; ViewInfo.fCounterSigner = false; ViewInfo.idxCounterSigner = 0; ViewInfo.cStores = 0; ViewInfo.rghStores = IntPtr.Zero; ViewInfo.cPropSheetPages = 0; ViewInfo.rgPropSheetPages = IntPtr.Zero; ViewInfo.nStartPage = 0; // View the certificate if (!CAPI.CryptUIDlgViewCertificateW(ViewInfo, IntPtr.Zero)) dwErrorCode = Marshal.GetLastWin32Error(); // CryptUIDlgViewCertificateW returns ERROR_CANCELLED if the user closes // the window through the x button or by pressing CANCEL, so ignore this error code if (dwErrorCode != CAPI.ERROR_SUCCESS && dwErrorCode != CAPI.ERROR_CANCELLED) throw new CryptographicException(Marshal.GetLastWin32Error()); } private static X509Certificate2Collection SelectFromCollectionHelper (X509Certificate2Collection certificates, string title, string message, X509SelectionFlag selectionFlag, IntPtr hwndParent) { if (certificates == null) throw new ArgumentNullException("certificates"); if (selectionFlag < X509SelectionFlag.SingleSelection || selectionFlag > X509SelectionFlag.MultiSelection) throw new ArgumentException(String.Format(CultureInfo.CurrentCulture, SecurityResources.GetResourceString("Arg_EnumIllegalVal"), "selectionFlag")); // // We need to Assert all StorePermission flags since this is a memory store and we want // semi-trusted code to be able to select certificates from a memory store. // StorePermission sp = new StorePermission(StorePermissionFlags.AllFlags); sp.Assert(); SafeCertStoreHandle safeSourceStoreHandle = X509Utils.ExportToMemoryStore(certificates); SafeCertStoreHandle safeTargetStoreHandle = SafeCertStoreHandle.InvalidHandle; safeTargetStoreHandle = SelectFromStore(safeSourceStoreHandle, title, message, selectionFlag, hwndParent); X509Certificate2Collection collection = X509Utils.GetCertificates(safeTargetStoreHandle); safeTargetStoreHandle.Dispose(); safeSourceStoreHandle.Dispose(); return collection; } private unsafe static SafeCertStoreHandle SelectFromStore (SafeCertStoreHandle safeSourceStoreHandle, string title, string message, X509SelectionFlag selectionFlags, IntPtr hwndParent) { int dwErrorCode = CAPI.ERROR_SUCCESS; // First, create a memory store SafeCertStoreHandle safeCertStoreHandle = CAPI.CertOpenStore((IntPtr) CAPI.CERT_STORE_PROV_MEMORY, CAPI.X509_ASN_ENCODING | CAPI.PKCS_7_ASN_ENCODING, IntPtr.Zero, 0, null); if (safeCertStoreHandle == null || safeCertStoreHandle.IsInvalid) throw new CryptographicException(Marshal.GetLastWin32Error()); CAPI.CRYPTUI_SELECTCERTIFICATE_STRUCTW csc = new CAPI.CRYPTUI_SELECTCERTIFICATE_STRUCTW(); // Older versions of CRYPTUI do not check the size correctly, // so always force it to the oldest version of the structure. csc.dwSize = (uint) Marshal.OffsetOf(typeof(CAPI.CRYPTUI_SELECTCERTIFICATE_STRUCTW), "hSelectedCertStore"); csc.hwndParent = hwndParent; csc.dwFlags = (uint) selectionFlags; csc.szTitle = title; csc.dwDontUseColumn = 0; csc.szDisplayString = message; csc.pFilterCallback = IntPtr.Zero; csc.pDisplayCallback = IntPtr.Zero; csc.pvCallbackData = IntPtr.Zero; csc.cDisplayStores = 1; IntPtr hSourceCertStore = safeSourceStoreHandle.DangerousGetHandle(); csc.rghDisplayStores = new IntPtr(&hSourceCertStore); csc.cStores = 0; csc.rghStores = IntPtr.Zero; csc.cPropSheetPages = 0; csc.rgPropSheetPages = IntPtr.Zero; csc.hSelectedCertStore = safeCertStoreHandle.DangerousGetHandle(); SafeCertContextHandle safeCertContextHandle = CAPI.CryptUIDlgSelectCertificateW(csc); if (safeCertContextHandle != null && !safeCertContextHandle.IsInvalid) { // Single select, so add it to our hCertStore SafeCertContextHandle ppStoreContext = SafeCertContextHandle.InvalidHandle; if (!CAPI.CertAddCertificateContextToStore(safeCertStoreHandle, safeCertContextHandle, CAPI.CERT_STORE_ADD_NEWER_INHERIT_PROPERTIES, ppStoreContext)) dwErrorCode = Marshal.GetLastWin32Error(); } if (dwErrorCode != CAPI.ERROR_SUCCESS) throw new CryptographicException(Marshal.GetLastWin32Error()); return safeCertStoreHandle; } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // ==++== // // Copyright (c) Microsoft Corporation. All rights reserved. // // ==--== // // X509UI.cs // namespace System.Security.Cryptography.X509Certificates { using System; using System.Globalization; using System.Runtime.InteropServices; using System.Security.Cryptography; using System.Security.Permissions; public enum X509SelectionFlag { SingleSelection = 0x00, MultiSelection = 0x01 } [System.Security.Permissions.HostProtection(MayLeakOnAbort = true)] public sealed class X509Certificate2UI { private X509Certificate2UI () {} public static void DisplayCertificate (X509Certificate2 certificate) { if (certificate == null) throw new ArgumentNullException("certificate"); DisplayX509Certificate(X509Utils.GetCertContext(certificate), IntPtr.Zero); } [SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags=SecurityPermissionFlag.UnmanagedCode)] [SecurityPermissionAttribute(SecurityAction.InheritanceDemand, Flags=SecurityPermissionFlag.UnmanagedCode)] public static void DisplayCertificate (X509Certificate2 certificate, IntPtr hwndParent) { if (certificate == null) throw new ArgumentNullException("certificate"); DisplayX509Certificate(X509Utils.GetCertContext(certificate), hwndParent); } public static X509Certificate2Collection SelectFromCollection (X509Certificate2Collection certificates, string title, string message, X509SelectionFlag selectionFlag) { return SelectFromCollectionHelper(certificates, title, message, selectionFlag, IntPtr.Zero); } [SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags=SecurityPermissionFlag.UnmanagedCode)] [SecurityPermissionAttribute(SecurityAction.InheritanceDemand, Flags=SecurityPermissionFlag.UnmanagedCode)] public static X509Certificate2Collection SelectFromCollection (X509Certificate2Collection certificates, string title, string message, X509SelectionFlag selectionFlag, IntPtr hwndParent) { return SelectFromCollectionHelper(certificates, title, message, selectionFlag, hwndParent); } private static void DisplayX509Certificate (SafeCertContextHandle safeCertContext, IntPtr hwndParent) { if (safeCertContext.IsInvalid) throw new CryptographicException(SecurityResources.GetResourceString("Cryptography_InvalidHandle"), "safeCertContext"); int dwErrorCode = CAPI.ERROR_SUCCESS; // Initialize view structure. CAPI.CRYPTUI_VIEWCERTIFICATE_STRUCTW ViewInfo = new CAPI.CRYPTUI_VIEWCERTIFICATE_STRUCTW(); ViewInfo.dwSize = (uint) Marshal.SizeOf(ViewInfo); ViewInfo.hwndParent = hwndParent; ViewInfo.dwFlags = 0; ViewInfo.szTitle = null; ViewInfo.pCertContext = safeCertContext.DangerousGetHandle(); ViewInfo.rgszPurposes = IntPtr.Zero; ViewInfo.cPurposes = 0; ViewInfo.pCryptProviderData = IntPtr.Zero; ViewInfo.fpCryptProviderDataTrustedUsage = false; ViewInfo.idxSigner = 0; ViewInfo.idxCert = 0; ViewInfo.fCounterSigner = false; ViewInfo.idxCounterSigner = 0; ViewInfo.cStores = 0; ViewInfo.rghStores = IntPtr.Zero; ViewInfo.cPropSheetPages = 0; ViewInfo.rgPropSheetPages = IntPtr.Zero; ViewInfo.nStartPage = 0; // View the certificate if (!CAPI.CryptUIDlgViewCertificateW(ViewInfo, IntPtr.Zero)) dwErrorCode = Marshal.GetLastWin32Error(); // CryptUIDlgViewCertificateW returns ERROR_CANCELLED if the user closes // the window through the x button or by pressing CANCEL, so ignore this error code if (dwErrorCode != CAPI.ERROR_SUCCESS && dwErrorCode != CAPI.ERROR_CANCELLED) throw new CryptographicException(Marshal.GetLastWin32Error()); } private static X509Certificate2Collection SelectFromCollectionHelper (X509Certificate2Collection certificates, string title, string message, X509SelectionFlag selectionFlag, IntPtr hwndParent) { if (certificates == null) throw new ArgumentNullException("certificates"); if (selectionFlag < X509SelectionFlag.SingleSelection || selectionFlag > X509SelectionFlag.MultiSelection) throw new ArgumentException(String.Format(CultureInfo.CurrentCulture, SecurityResources.GetResourceString("Arg_EnumIllegalVal"), "selectionFlag")); // // We need to Assert all StorePermission flags since this is a memory store and we want // semi-trusted code to be able to select certificates from a memory store. // StorePermission sp = new StorePermission(StorePermissionFlags.AllFlags); sp.Assert(); SafeCertStoreHandle safeSourceStoreHandle = X509Utils.ExportToMemoryStore(certificates); SafeCertStoreHandle safeTargetStoreHandle = SafeCertStoreHandle.InvalidHandle; safeTargetStoreHandle = SelectFromStore(safeSourceStoreHandle, title, message, selectionFlag, hwndParent); X509Certificate2Collection collection = X509Utils.GetCertificates(safeTargetStoreHandle); safeTargetStoreHandle.Dispose(); safeSourceStoreHandle.Dispose(); return collection; } private unsafe static SafeCertStoreHandle SelectFromStore (SafeCertStoreHandle safeSourceStoreHandle, string title, string message, X509SelectionFlag selectionFlags, IntPtr hwndParent) { int dwErrorCode = CAPI.ERROR_SUCCESS; // First, create a memory store SafeCertStoreHandle safeCertStoreHandle = CAPI.CertOpenStore((IntPtr) CAPI.CERT_STORE_PROV_MEMORY, CAPI.X509_ASN_ENCODING | CAPI.PKCS_7_ASN_ENCODING, IntPtr.Zero, 0, null); if (safeCertStoreHandle == null || safeCertStoreHandle.IsInvalid) throw new CryptographicException(Marshal.GetLastWin32Error()); CAPI.CRYPTUI_SELECTCERTIFICATE_STRUCTW csc = new CAPI.CRYPTUI_SELECTCERTIFICATE_STRUCTW(); // Older versions of CRYPTUI do not check the size correctly, // so always force it to the oldest version of the structure. csc.dwSize = (uint) Marshal.OffsetOf(typeof(CAPI.CRYPTUI_SELECTCERTIFICATE_STRUCTW), "hSelectedCertStore"); csc.hwndParent = hwndParent; csc.dwFlags = (uint) selectionFlags; csc.szTitle = title; csc.dwDontUseColumn = 0; csc.szDisplayString = message; csc.pFilterCallback = IntPtr.Zero; csc.pDisplayCallback = IntPtr.Zero; csc.pvCallbackData = IntPtr.Zero; csc.cDisplayStores = 1; IntPtr hSourceCertStore = safeSourceStoreHandle.DangerousGetHandle(); csc.rghDisplayStores = new IntPtr(&hSourceCertStore); csc.cStores = 0; csc.rghStores = IntPtr.Zero; csc.cPropSheetPages = 0; csc.rgPropSheetPages = IntPtr.Zero; csc.hSelectedCertStore = safeCertStoreHandle.DangerousGetHandle(); SafeCertContextHandle safeCertContextHandle = CAPI.CryptUIDlgSelectCertificateW(csc); if (safeCertContextHandle != null && !safeCertContextHandle.IsInvalid) { // Single select, so add it to our hCertStore SafeCertContextHandle ppStoreContext = SafeCertContextHandle.InvalidHandle; if (!CAPI.CertAddCertificateContextToStore(safeCertStoreHandle, safeCertContextHandle, CAPI.CERT_STORE_ADD_NEWER_INHERIT_PROPERTIES, ppStoreContext)) dwErrorCode = Marshal.GetLastWin32Error(); } if (dwErrorCode != CAPI.ERROR_SUCCESS) throw new CryptographicException(Marshal.GetLastWin32Error()); return safeCertStoreHandle; } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- BasicExpressionVisitor.cs
- StrokeCollection.cs
- ColumnWidthChangingEvent.cs
- SafeFileMappingHandle.cs
- DrawingBrush.cs
- StrokeNodeData.cs
- RIPEMD160Managed.cs
- BrowserTree.cs
- InternalResources.cs
- PlaceHolder.cs
- RuleSetDialog.Designer.cs
- SQLString.cs
- ZipIOExtraFieldPaddingElement.cs
- ToolboxItem.cs
- ClientSettingsProvider.cs
- CssStyleCollection.cs
- ContextStack.cs
- WindowsFormsSynchronizationContext.cs
- FixedPageAutomationPeer.cs
- BinaryFormatterWriter.cs
- BuildProvidersCompiler.cs
- LZCodec.cs
- Verify.cs
- ZoneLinkButton.cs
- DrawingServices.cs
- IisTraceListener.cs
- _ScatterGatherBuffers.cs
- RecipientInfo.cs
- XmlTextWriter.cs
- UndoEngine.cs
- RenderDataDrawingContext.cs
- versioninfo.cs
- CodeSubDirectory.cs
- IERequestCache.cs
- OptimizedTemplateContentHelper.cs
- PathFigure.cs
- TypeBuilderInstantiation.cs
- MenuItem.cs
- AsyncResult.cs
- CompatibleIComparer.cs
- CompoundFileStorageReference.cs
- ToolStripMenuItemDesigner.cs
- WebPermission.cs
- DbParameterCollectionHelper.cs
- InfocardExtendedInformationCollection.cs
- CompilerInfo.cs
- XmlDocumentType.cs
- ResourceManagerWrapper.cs
- ParameterCollection.cs
- InputDevice.cs
- BaseCodePageEncoding.cs
- Menu.cs
- User.cs
- DesignerDataTableBase.cs
- SqlProfileProvider.cs
- listitem.cs
- ToggleButtonAutomationPeer.cs
- ReaderWriterLockWrapper.cs
- SymmetricKey.cs
- WmiEventSink.cs
- HeaderedContentControl.cs
- ImageField.cs
- SmiContextFactory.cs
- TransportSecurityBindingElement.cs
- AppDomainProtocolHandler.cs
- ProviderSettingsCollection.cs
- DBConnection.cs
- RenderContext.cs
- XMLSchema.cs
- CommentGlyph.cs
- WorkflowTraceTransfer.cs
- DefaultBindingPropertyAttribute.cs
- EmptyCollection.cs
- Configuration.cs
- GregorianCalendarHelper.cs
- StringWriter.cs
- DescendentsWalker.cs
- ConfigurationPropertyCollection.cs
- HierarchicalDataBoundControlAdapter.cs
- IgnoreFlushAndCloseStream.cs
- ExtensionDataObject.cs
- DictationGrammar.cs
- XmlQueryTypeFactory.cs
- SafeHandles.cs
- Dictionary.cs
- VerificationAttribute.cs
- IntSecurity.cs
- RtfNavigator.cs
- XamlStream.cs
- WebCategoryAttribute.cs
- DataGridState.cs
- FileLogRecord.cs
- IndependentAnimationStorage.cs
- FilterableAttribute.cs
- LayoutSettings.cs
- AlgoModule.cs
- TemplatedWizardStep.cs
- RecordsAffectedEventArgs.cs
- EntitySetDataBindingList.cs
- SoapUnknownHeader.cs