Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / cdf / src / WCF / IdentityModel / System / IdentityModel / SafeCryptHandles.cs / 1305376 / 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. //----------------------------------------------------------------------------- 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.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- Misc.cs
- SqlRetyper.cs
- GACMembershipCondition.cs
- RectangleGeometry.cs
- SizeAnimationBase.cs
- CodeDefaultValueExpression.cs
- MergeFailedEvent.cs
- RegexCompiler.cs
- AsymmetricAlgorithm.cs
- PropertyEmitter.cs
- SharedStatics.cs
- CalloutQueueItem.cs
- SchemaComplexType.cs
- SrgsElementFactory.cs
- SqlTypesSchemaImporter.cs
- PageTheme.cs
- ViewStateModeByIdAttribute.cs
- AtomMaterializerLog.cs
- XmlCharCheckingReader.cs
- PrintDialogException.cs
- BitSet.cs
- Control.cs
- OleDbPropertySetGuid.cs
- TTSEvent.cs
- LocatorGroup.cs
- FrameworkPropertyMetadata.cs
- BuildResult.cs
- EntityStoreSchemaFilterEntry.cs
- EmptyControlCollection.cs
- PerspectiveCamera.cs
- GPPOINT.cs
- EntityDesignerUtils.cs
- PersonalizationEntry.cs
- TemplateNameScope.cs
- CatalogPartChrome.cs
- ToolStripLabel.cs
- ToolBarButton.cs
- DuplicateWaitObjectException.cs
- SubclassTypeValidatorAttribute.cs
- PersistenceProviderDirectory.cs
- FactoryMaker.cs
- XmlProcessingInstruction.cs
- PlatformNotSupportedException.cs
- MSAANativeProvider.cs
- Light.cs
- DbCommandTree.cs
- TextMessageEncodingElement.cs
- CodeBlockBuilder.cs
- Button.cs
- HybridCollection.cs
- GeneralTransform2DTo3D.cs
- XmlNodeChangedEventArgs.cs
- TargetParameterCountException.cs
- EntityDataSourceDataSelectionPanel.cs
- XmlUtilWriter.cs
- CRYPTPROTECT_PROMPTSTRUCT.cs
- Variant.cs
- SpoolingTask.cs
- BaseTemplateBuildProvider.cs
- WebPartHelpVerb.cs
- SchemaMapping.cs
- HttpHandlerActionCollection.cs
- ZipIOFileItemStream.cs
- WebBrowser.cs
- ColumnMapProcessor.cs
- InternalDispatchObject.cs
- DocumentGridContextMenu.cs
- ParenthesizePropertyNameAttribute.cs
- figurelength.cs
- InteropAutomationProvider.cs
- XmlSerializerOperationFormatter.cs
- HierarchicalDataSourceControl.cs
- SingleObjectCollection.cs
- ListViewItem.cs
- CheckBox.cs
- RtfToXamlLexer.cs
- WindowsFormsHost.cs
- PropertyValue.cs
- ServiceMetadataExtension.cs
- ZoomPercentageConverter.cs
- BrushConverter.cs
- RefreshEventArgs.cs
- Int32KeyFrameCollection.cs
- HtmlHistory.cs
- SignedPkcs7.cs
- CompiledRegexRunnerFactory.cs
- Image.cs
- Thickness.cs
- Utils.cs
- BinaryUtilClasses.cs
- WindowsGraphicsWrapper.cs
- ControlCachePolicy.cs
- Enlistment.cs
- DataTableExtensions.cs
- BackgroundWorker.cs
- DataGridBeginningEditEventArgs.cs
- AuthenticationServiceManager.cs
- SortedDictionary.cs
- XPathPatternBuilder.cs
- _OSSOCK.cs