Code:
/ WCF / WCF / 3.5.30729.1 / untmp / Orcas / SP / ndp / cdf / src / WCF / IdentityModel / System / IdentityModel / SafeCryptHandles.cs / 1 / SafeCryptHandles.cs
//------------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
namespace System.IdentityModel
{
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;
using System.Runtime.ConstrainedExecution;
using System.Security.Cryptography;
using System.ServiceModel.Diagnostics;
using Microsoft.Win32.SafeHandles;
class SafeProvHandle : SafeHandleZeroOrMinusOneIsInvalid
{
SafeProvHandle() : base(true) { }
// 0 is an Invalid Handle
SafeProvHandle(IntPtr handle)
: base(true)
{
DiagnosticUtility.DebugAssert(handle == IntPtr.Zero, "SafeProvHandle constructor can only be called with IntPtr.Zero.");
SetHandle(handle);
}
internal static SafeProvHandle InvalidHandle
{
get { return new SafeProvHandle(IntPtr.Zero); }
}
protected override bool ReleaseHandle()
{
// PreSharp Bug: Call 'Marshal.GetLastWin32Error' or 'Marshal.GetHRForLastWin32Error' before any other interop call.
#pragma warning suppress 56523 // We are not interested in throwing an exception here if CloseHandle fails.
return NativeMethods.CryptReleaseContext(handle, 0);
}
}
class SafeKeyHandle : SafeHandleZeroOrMinusOneIsInvalid
{
SafeProvHandle provHandle = null;
SafeKeyHandle() : base(true) { }
// 0 is an Invalid Handle
SafeKeyHandle(IntPtr handle)
: base(true)
{
DiagnosticUtility.DebugAssert(handle == IntPtr.Zero, "SafeKeyHandle constructor can only be called with IntPtr.Zero.");
SetHandle(handle);
}
internal static SafeKeyHandle InvalidHandle
{
get { return new SafeKeyHandle(IntPtr.Zero); }
}
protected override bool ReleaseHandle()
{
// PreSharp Bug: Call 'Marshal.GetLastWin32Error' or 'Marshal.GetHRForLastWin32Error' before any other interop call.
#pragma warning suppress 56523 // We are not interested in throwing an exception here if CloseHandle fails.
bool ret = NativeMethods.CryptDestroyKey(handle);
if (this.provHandle != null)
{
this.provHandle.DangerousRelease();
this.provHandle = null;
}
return ret;
}
internal static unsafe SafeKeyHandle SafeCryptImportKey(SafeProvHandle provHandle, void* pbDataPtr, int cbData)
{
bool b = false;
int err = 0;
SafeKeyHandle keyHandle = null;
RuntimeHelpers.PrepareConstrainedRegions();
try
{
provHandle.DangerousAddRef(ref b);
}
catch (Exception e)
{
if (b)
{
provHandle.DangerousRelease();
b = false;
}
if (!(e is ObjectDisposedException))
throw;
}
finally
{
if (b)
{
b = NativeMethods.CryptImportKey(provHandle, pbDataPtr, (uint)cbData, IntPtr.Zero, 0, out keyHandle);
if (!b)
{
err = Marshal.GetLastWin32Error();
provHandle.DangerousRelease();
}
else
{
// Take ownership of AddRef. Will Release at Close.
keyHandle.provHandle = provHandle;
}
}
}
if (!b)
{
Utility.CloseInvalidOutSafeHandle(keyHandle);
string reason = (err != 0) ? new Win32Exception(err).Message : String.Empty;
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CryptographicException(SR.GetString(SR.AESCryptImportKeyFailed, reason)));
}
return keyHandle;
}
}
}
// 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
- FastEncoder.cs
- UserControlAutomationPeer.cs
- Point3DAnimation.cs
- UIElementIsland.cs
- SqlColumnizer.cs
- DateTimeValueSerializerContext.cs
- FixedDocumentSequencePaginator.cs
- PinnedBufferMemoryStream.cs
- HttpWrapper.cs
- PointCollection.cs
- OdbcConnectionPoolProviderInfo.cs
- TypeListConverter.cs
- TextSelectionProcessor.cs
- SiteMapHierarchicalDataSourceView.cs
- PostBackOptions.cs
- Vertex.cs
- DefaultCompensation.cs
- AnonymousIdentificationModule.cs
- DetailsViewPagerRow.cs
- TextPenaltyModule.cs
- FormViewRow.cs
- NumberFormatInfo.cs
- EnumerableValidator.cs
- Configuration.cs
- ClassHandlersStore.cs
- MarginCollapsingState.cs
- ZipIORawDataFileBlock.cs
- ProtectedConfigurationProviderCollection.cs
- MetadataArtifactLoaderXmlReaderWrapper.cs
- VirtualDirectoryMappingCollection.cs
- ColorMatrix.cs
- DrawingCollection.cs
- NetNamedPipeBindingElement.cs
- ConfigUtil.cs
- XmlReflectionMember.cs
- ToolStripPanel.cs
- StartUpEventArgs.cs
- GiveFeedbackEventArgs.cs
- ManagementInstaller.cs
- PositiveTimeSpanValidatorAttribute.cs
- listviewsubitemcollectioneditor.cs
- WebSysDefaultValueAttribute.cs
- PageRequestManager.cs
- DateTimeFormatInfo.cs
- Timer.cs
- FilteredDataSetHelper.cs
- ListView.cs
- CodeCatchClauseCollection.cs
- CounterCreationData.cs
- SignedPkcs7.cs
- CqlParser.cs
- WebHttpSecurityModeHelper.cs
- LinqExpressionNormalizer.cs
- ComPlusTraceRecord.cs
- ControlFilterExpression.cs
- AdapterUtil.cs
- BridgeDataReader.cs
- ConfigXmlReader.cs
- InlinedAggregationOperator.cs
- ProcessModelInfo.cs
- SelectionHighlightInfo.cs
- MailSettingsSection.cs
- SoundPlayerAction.cs
- RowCache.cs
- DataGridViewCellStyleBuilderDialog.cs
- IPAddressCollection.cs
- ScriptControl.cs
- WpfKnownTypeInvoker.cs
- NumericPagerField.cs
- SystemDropShadowChrome.cs
- HtmlElementEventArgs.cs
- BuildProvidersCompiler.cs
- ComponentConverter.cs
- InfoCardRSAPKCS1SignatureFormatter.cs
- AttributeUsageAttribute.cs
- WebConfigurationFileMap.cs
- SchemaImporter.cs
- FilterException.cs
- ConfigXmlElement.cs
- XpsTokenContext.cs
- Int64Storage.cs
- DTCTransactionManager.cs
- AnimationLayer.cs
- CriticalHandle.cs
- _HeaderInfo.cs
- EncoderBestFitFallback.cs
- IListConverters.cs
- DefaultClaimSet.cs
- LocatorManager.cs
- ProxyManager.cs
- PopOutPanel.cs
- XamlRtfConverter.cs
- SqlCachedBuffer.cs
- DataGridViewEditingControlShowingEventArgs.cs
- ConfigurationElementProperty.cs
- PageBuildProvider.cs
- ObjectSet.cs
- CompareValidator.cs
- FontStyles.cs
- TableItemStyle.cs