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
- AssemblyResourceLoader.cs
- KeysConverter.cs
- XmlSchemaIdentityConstraint.cs
- InstanceLockTracking.cs
- CqlWriter.cs
- NameValueCollection.cs
- Byte.cs
- DataGridViewRowPostPaintEventArgs.cs
- CacheMode.cs
- TreeNodeEventArgs.cs
- RegexMatch.cs
- DataSourceCacheDurationConverter.cs
- EqualityComparer.cs
- ListViewItem.cs
- LabelDesigner.cs
- PassportAuthentication.cs
- FocusWithinProperty.cs
- EntityDataSourceSelectedEventArgs.cs
- RemoteDebugger.cs
- SkewTransform.cs
- HttpListener.cs
- Attributes.cs
- ProcessHost.cs
- MappingModelBuildProvider.cs
- ObjectTypeMapping.cs
- ComponentEvent.cs
- Composition.cs
- StylusTip.cs
- StylusPointPropertyInfo.cs
- NetPipeSection.cs
- DragDeltaEventArgs.cs
- EventLogPermissionAttribute.cs
- BuildProviderAppliesToAttribute.cs
- DiffuseMaterial.cs
- TypeGeneratedEventArgs.cs
- Shape.cs
- ProcessHostMapPath.cs
- Track.cs
- SequentialOutput.cs
- BaseProcessProtocolHandler.cs
- FakeModelPropertyImpl.cs
- FlowDocumentView.cs
- CompiledXpathExpr.cs
- AttributeXamlType.cs
- SmiContext.cs
- OutputScope.cs
- DispatcherEventArgs.cs
- IntAverageAggregationOperator.cs
- XmlTextAttribute.cs
- XmlJsonWriter.cs
- validation.cs
- DataObjectEventArgs.cs
- NullableConverter.cs
- FormsAuthenticationUserCollection.cs
- ThousandthOfEmRealDoubles.cs
- WebPartEventArgs.cs
- GlobalizationSection.cs
- ResXDataNode.cs
- Int32Converter.cs
- PlatformCulture.cs
- Helpers.cs
- SqlGenericUtil.cs
- RoleManagerEventArgs.cs
- MarkupCompiler.cs
- ManipulationStartedEventArgs.cs
- AudioFormatConverter.cs
- BaseProcessor.cs
- SecurityResources.cs
- SqlRowUpdatedEvent.cs
- InfoCardRSAPKCS1KeyExchangeDeformatter.cs
- DropShadowBitmapEffect.cs
- MenuItem.cs
- _ShellExpression.cs
- ObjectStorage.cs
- FtpRequestCacheValidator.cs
- Sequence.cs
- XsdValidatingReader.cs
- TreeSet.cs
- BufferedStream.cs
- GorillaCodec.cs
- CalculatedColumn.cs
- GeneratedView.cs
- GeometryConverter.cs
- ArrayElementGridEntry.cs
- SafeRightsManagementHandle.cs
- FilterUserControlBase.cs
- COM2IDispatchConverter.cs
- StateMachineDesignerPaint.cs
- WbemException.cs
- AtomServiceDocumentSerializer.cs
- Rule.cs
- GeometryModel3D.cs
- Directory.cs
- FixUp.cs
- SiteMapSection.cs
- ConstantSlot.cs
- EqualityArray.cs
- StringCollectionEditor.cs
- CircleHotSpot.cs
- AggregationMinMaxHelpers.cs