Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / clr / src / ManagedLibraries / Security / System / Security / Cryptography / X509 / X509UI.cs / 1305376 / 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 static class X509Certificate2UI {
[SecuritySafeCritical]
public static void DisplayCertificate (X509Certificate2 certificate) {
if (certificate == null)
throw new ArgumentNullException("certificate");
DisplayX509Certificate(X509Utils.GetCertContext(certificate), IntPtr.Zero);
}
[SecurityCritical]
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);
}
[SecurityCritical]
public static X509Certificate2Collection SelectFromCollection (X509Certificate2Collection certificates, string title, string message, X509SelectionFlag selectionFlag, IntPtr hwndParent) {
return SelectFromCollectionHelper(certificates, title, message, selectionFlag, hwndParent);
}
[SecurityCritical]
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());
}
[SecuritySafeCritical]
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();
using (SafeCertStoreHandle safeSourceStoreHandle = X509Utils.ExportToMemoryStore(certificates))
using (SafeCertStoreHandle safeTargetStoreHandle = SelectFromStore(safeSourceStoreHandle, title, message, selectionFlag, hwndParent))
{
return X509Utils.GetCertificates(safeTargetStoreHandle);
}
}
[SecurityCritical]
private static unsafe 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 static class X509Certificate2UI {
[SecuritySafeCritical]
public static void DisplayCertificate (X509Certificate2 certificate) {
if (certificate == null)
throw new ArgumentNullException("certificate");
DisplayX509Certificate(X509Utils.GetCertContext(certificate), IntPtr.Zero);
}
[SecurityCritical]
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);
}
[SecurityCritical]
public static X509Certificate2Collection SelectFromCollection (X509Certificate2Collection certificates, string title, string message, X509SelectionFlag selectionFlag, IntPtr hwndParent) {
return SelectFromCollectionHelper(certificates, title, message, selectionFlag, hwndParent);
}
[SecurityCritical]
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());
}
[SecuritySafeCritical]
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();
using (SafeCertStoreHandle safeSourceStoreHandle = X509Utils.ExportToMemoryStore(certificates))
using (SafeCertStoreHandle safeTargetStoreHandle = SelectFromStore(safeSourceStoreHandle, title, message, selectionFlag, hwndParent))
{
return X509Utils.GetCertificates(safeTargetStoreHandle);
}
}
[SecurityCritical]
private static unsafe 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
- XamlTreeBuilder.cs
- CorrelationValidator.cs
- SizeAnimationBase.cs
- XamlFilter.cs
- DBSqlParserTableCollection.cs
- StrongNameKeyPair.cs
- BitmapEffectDrawingContent.cs
- Quaternion.cs
- Utility.cs
- ToolStripOverflow.cs
- PageHandlerFactory.cs
- DataGridViewSelectedCellCollection.cs
- EntityKey.cs
- SqlColumnizer.cs
- HtmlTableCellCollection.cs
- ComplexTypeEmitter.cs
- XmlnsPrefixAttribute.cs
- Propagator.JoinPropagator.cs
- TreeNodeMouseHoverEvent.cs
- ResourceSetExpression.cs
- ResourcePart.cs
- TextSpan.cs
- IPHostEntry.cs
- XmlSerializableReader.cs
- TableAdapterManagerMethodGenerator.cs
- RectangleF.cs
- Parallel.cs
- AppDomainInstanceProvider.cs
- ReflectPropertyDescriptor.cs
- Serializer.cs
- Visual.cs
- PageScaling.cs
- SqlWebEventProvider.cs
- DragDeltaEventArgs.cs
- CompositeDispatchFormatter.cs
- TouchesOverProperty.cs
- WebUtil.cs
- ParameterCollectionEditorForm.cs
- PrincipalPermission.cs
- XmlILOptimizerVisitor.cs
- Site.cs
- BroadcastEventHelper.cs
- RemotingConfigParser.cs
- LogReserveAndAppendState.cs
- BindingWorker.cs
- HashMembershipCondition.cs
- CreateRefExpr.cs
- VisualBrush.cs
- Line.cs
- ReadWriteObjectLock.cs
- StateManagedCollection.cs
- PatternMatcher.cs
- InputLanguageEventArgs.cs
- ApplicationSettingsBase.cs
- FlowDocumentView.cs
- ColumnBinding.cs
- XmlDataSourceView.cs
- ToolStripPanelRow.cs
- DesignerTransaction.cs
- ServicesUtilities.cs
- DiagnosticStrings.cs
- VersionPair.cs
- SqlDataAdapter.cs
- Interlocked.cs
- VBCodeProvider.cs
- ProtectedConfiguration.cs
- ScrollEvent.cs
- CharStorage.cs
- EntityDataSourceState.cs
- UInt64Storage.cs
- AvTraceDetails.cs
- PropertySegmentSerializer.cs
- UrlAuthFailedErrorFormatter.cs
- MetabaseServerConfig.cs
- DataGridTableCollection.cs
- QuaternionRotation3D.cs
- DynamicILGenerator.cs
- ScrollChrome.cs
- NamespaceCollection.cs
- TextBreakpoint.cs
- SoapCodeExporter.cs
- RichTextBoxAutomationPeer.cs
- StorageMappingItemLoader.cs
- Literal.cs
- StringTraceRecord.cs
- smtppermission.cs
- ComboBox.cs
- Attributes.cs
- DynamicValueConverter.cs
- XmlBufferReader.cs
- XmlAutoDetectWriter.cs
- LinqTreeNodeEvaluator.cs
- PeerChannelListener.cs
- DBCSCodePageEncoding.cs
- EventDescriptor.cs
- FileChangesMonitor.cs
- TimeSpanValidator.cs
- ObjectFullSpanRewriter.cs
- XNodeValidator.cs
- Sequence.cs