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
- JournalEntryStack.cs
- Substitution.cs
- LayoutTable.cs
- CacheMode.cs
- CompensationExtension.cs
- UpdateTranslator.cs
- hresults.cs
- ExternalCalls.cs
- CodeRemoveEventStatement.cs
- AutomationPropertyInfo.cs
- EntityContainerEntitySetDefiningQuery.cs
- MeshGeometry3D.cs
- _ShellExpression.cs
- TypeDelegator.cs
- DelegateTypeInfo.cs
- CodeCastExpression.cs
- DataContractSerializerOperationBehavior.cs
- SQLDateTime.cs
- GridViewPageEventArgs.cs
- ManipulationDeltaEventArgs.cs
- ExceptionHandlerDesigner.cs
- InputMethodStateTypeInfo.cs
- DomNameTable.cs
- PeerNameRecord.cs
- IncrementalReadDecoders.cs
- MergeEnumerator.cs
- Pts.cs
- DataGridHyperlinkColumn.cs
- WebSysDefaultValueAttribute.cs
- EditModeSwitchButton.cs
- MediaElement.cs
- ParserOptions.cs
- CachedRequestParams.cs
- Compiler.cs
- SqlComparer.cs
- TypeHelper.cs
- UndoUnit.cs
- FileLevelControlBuilderAttribute.cs
- ImmutablePropertyDescriptorGridEntry.cs
- sapiproxy.cs
- TraceFilter.cs
- ZipFileInfoCollection.cs
- AsyncContentLoadedEventArgs.cs
- AuthenticationService.cs
- StsCommunicationException.cs
- FieldNameLookup.cs
- XmlSchemaSimpleTypeRestriction.cs
- LockCookie.cs
- BitHelper.cs
- MsmqChannelFactory.cs
- PropertyGrid.cs
- BitmapData.cs
- Pkcs7Recipient.cs
- RepeaterCommandEventArgs.cs
- IPEndPoint.cs
- SourceLineInfo.cs
- StandardBindingOptionalReliableSessionElement.cs
- UnsafeNativeMethods.cs
- ISAPIRuntime.cs
- CodeArrayIndexerExpression.cs
- ProfilePropertySettingsCollection.cs
- Int64KeyFrameCollection.cs
- ViewCellRelation.cs
- PtsPage.cs
- XPathNavigator.cs
- IssuanceLicense.cs
- Set.cs
- XmlILIndex.cs
- PermissionListSet.cs
- ResourcePool.cs
- CLRBindingWorker.cs
- ImageResources.Designer.cs
- X509Extension.cs
- CodeConstructor.cs
- Serializer.cs
- LocationSectionRecord.cs
- HelpFileFileNameEditor.cs
- OledbConnectionStringbuilder.cs
- ContractUtils.cs
- FileEnumerator.cs
- ColorKeyFrameCollection.cs
- ProcessManager.cs
- LiteralTextContainerControlBuilder.cs
- HostingPreferredMapPath.cs
- cryptoapiTransform.cs
- InstanceDataCollection.cs
- MemoryStream.cs
- DataSvcMapFile.cs
- HiddenFieldPageStatePersister.cs
- ResourceDictionary.cs
- AndCondition.cs
- Grammar.cs
- ResizeGrip.cs
- DecryptedHeader.cs
- XmlComplianceUtil.cs
- LoginAutoFormat.cs
- RootNamespaceAttribute.cs
- ResourceReader.cs
- ButtonChrome.cs
- ErrorInfoXmlDocument.cs