Code:
/ WCF / WCF / 3.5.30729.1 / untmp / Orcas / SP / ndp / cdf / src / WCF / infocard / Service / managed / Microsoft / InfoCards / SafeRsaProviderHandle.cs / 1 / SafeRsaProviderHandle.cs
//------------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
namespace Microsoft.InfoCards
{
using System;
using System.ComponentModel;
using System.Globalization;
using System.Runtime.InteropServices;
using System.Runtime.ConstrainedExecution;
using System.Security;
using Microsoft.InfoCards.Diagnostics;
using IDT=Microsoft.InfoCards.Diagnostics.InfoCardTrace;
//
// Summary:
// Provides a wrapper over a handle retrieved by CryptAcquireContext
//
internal class SafeRsaProviderHandle : SafeHandle
{
[DllImport( "advapi32.dll",
EntryPoint ="CryptAcquireContextW",
CharSet = CharSet.Unicode,
ExactSpelling = true,
SetLastError = true ) ]
[SuppressUnmanagedCodeSecurity]
private static extern bool CryptAcquireContext(
[Out] out SafeRsaProviderHandle hProv,
[In] string pszContainer,
[In] string pszProvider,
[In] uint dwProvType,
[In] uint dwFlags );
[DllImport( "advapi32.dll",
EntryPoint = "CryptReleaseContext",
CharSet = CharSet.Unicode,
ExactSpelling = true,
SetLastError = true ) ]
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
[SuppressUnmanagedCodeSecurity]
private static extern bool CryptReleaseContext(
[In] IntPtr hProv,
[In] uint dwFlags );
public static SafeRsaProviderHandle Construct()
{
//
// The native provider we use to do encryption and decryption and associated constants to
// set up the Microsoft Enhanced RSA and AES provider
//
const uint CRYPT_VERIFYCONTEXT = 0xF0000000;
const byte PROV_RSA_AES = 24;
//
// We use a null provider and CRYPT_VERIFYCONTEXT as a flag
// to force the use of ephemeral keys. See http://support.microsoft.com/default.aspx?scid=kb%3Ben-us%3B238187
// for details.
//
SafeRsaProviderHandle h;
if( !SafeRsaProviderHandle.CryptAcquireContext( out h,
null,
null,
PROV_RSA_AES,
CRYPT_VERIFYCONTEXT ) )
{
Win32Exception e = new Win32Exception( Marshal.GetLastWin32Error() );
IDT.CloseInvalidOutSafeHandle(h);
IDT.TraceAndLogException( e );
throw IDT.ThrowHelperError( new CommunicationException( e.Message ) );
}
return h;
}
private SafeRsaProviderHandle() : base( IntPtr.Zero, true )
{
}
public override bool IsInvalid
{
get
{
return ( IntPtr.Zero == base.handle );
}
}
protected override bool ReleaseHandle()
{
#pragma warning suppress 56523
return CryptReleaseContext( base.handle, 0 );
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- Descriptor.cs
- MultipartContentParser.cs
- GetRecipientListRequest.cs
- Context.cs
- HijriCalendar.cs
- SingleBodyParameterMessageFormatter.cs
- PresentationSource.cs
- SocketStream.cs
- DbConnectionStringBuilder.cs
- LocalizableResourceBuilder.cs
- ApplicationSecurityManager.cs
- TypeNameConverter.cs
- WindowsListViewGroupHelper.cs
- AppearanceEditorPart.cs
- FixedSOMElement.cs
- HttpListenerException.cs
- GlobalItem.cs
- SecurityDocument.cs
- UnmanagedMemoryStream.cs
- ProfileManager.cs
- CompositeCollection.cs
- CodeAttributeArgumentCollection.cs
- GACIdentityPermission.cs
- ConfigurationManager.cs
- ResourceAssociationSetEnd.cs
- ZipIOFileItemStream.cs
- QilExpression.cs
- GraphicsContainer.cs
- PanelDesigner.cs
- DBCommand.cs
- AssemblyCache.cs
- ItemDragEvent.cs
- datacache.cs
- FunctionQuery.cs
- SoapFault.cs
- TableLayoutCellPaintEventArgs.cs
- UpdateProgress.cs
- GACIdentityPermission.cs
- Aes.cs
- __ConsoleStream.cs
- TextSchema.cs
- FormatConvertedBitmap.cs
- ClaimComparer.cs
- ChangeNode.cs
- CodeTypeReference.cs
- assemblycache.cs
- IHttpResponseInternal.cs
- WorkflowDesignerMessageFilter.cs
- DragDeltaEventArgs.cs
- ThreadStartException.cs
- AsyncResult.cs
- PersistenceTypeAttribute.cs
- Clipboard.cs
- TriggerActionCollection.cs
- HtmlHead.cs
- EventLogTraceListener.cs
- NavigationFailedEventArgs.cs
- CollectionBase.cs
- PrimaryKeyTypeConverter.cs
- _SecureChannel.cs
- KeyGesture.cs
- DetailsViewAutoFormat.cs
- FieldTemplateFactory.cs
- ExpressionPrefixAttribute.cs
- OleServicesContext.cs
- NumericUpDownAcceleration.cs
- AnyAllSearchOperator.cs
- ToolStripDropDownClosedEventArgs.cs
- DataGridViewRowPostPaintEventArgs.cs
- RegexCapture.cs
- EntityConnection.cs
- SmiTypedGetterSetter.cs
- ClrPerspective.cs
- RealizationDrawingContextWalker.cs
- DataSet.cs
- UnitySerializationHolder.cs
- XmlDeclaration.cs
- DataGridViewTextBoxColumn.cs
- TextEditorCopyPaste.cs
- WsdlInspector.cs
- DataObject.cs
- TextPatternIdentifiers.cs
- ToolboxItemAttribute.cs
- SparseMemoryStream.cs
- ICspAsymmetricAlgorithm.cs
- BindStream.cs
- WizardSideBarListControlItemEventArgs.cs
- CqlGenerator.cs
- SelectionEditingBehavior.cs
- PageTheme.cs
- CommonXSendMessage.cs
- SelectionItemProviderWrapper.cs
- SqlTypeSystemProvider.cs
- ColorBlend.cs
- ContentFileHelper.cs
- FacetValues.cs
- CodeStatementCollection.cs
- QuaternionConverter.cs
- PatternMatcher.cs
- MoveSizeWinEventHandler.cs