Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / Core / System / Security / Cryptography / ECDiffieHellmanCngPublicKey.cs / 1305376 / ECDiffieHellmanCngPublicKey.cs
// ==++== // // Copyright (c) Microsoft Corporation. All rights reserved. // // ==--== using System; using System.Runtime.Serialization; using System.Security; using System.Security.Permissions; using System.Diagnostics.Contracts; namespace System.Security.Cryptography { ////// Public key used to do key exchange with the ECDiffieHellmanCng algorithm /// [Serializable] [System.Security.Permissions.HostProtection(MayLeakOnAbort = true)] public sealed class ECDiffieHellmanCngPublicKey : ECDiffieHellmanPublicKey { [NonSerialized] private CngKey m_key; private CngKeyBlobFormat m_format; ////// Wrap a CNG key /// //// [System.Security.SecurityCritical] internal ECDiffieHellmanCngPublicKey(CngKey key) : base(key.Export(CngKeyBlobFormat.EccPublicBlob)) { Contract.Requires(key != null && key.AlgorithmGroup == CngAlgorithmGroup.ECDiffieHellman); Contract.Ensures(m_format != null); m_format = CngKeyBlobFormat.EccPublicBlob; // // We need to make a copy of the key to prevent the situation where the ECDiffieHellmanCng algorithm // object is disposed (this disposing its key) before the ECDiffieHellmanCngPublic key is disposed. // // Accessing the handle in partial trust is safe because we're not exposing it back out to user code // new SecurityPermission(SecurityPermissionFlag.UnmanagedCode).Assert(); m_key = CngKey.Open(key.Handle, key.IsEphemeral ? CngKeyHandleOpenOptions.EphemeralKey : CngKeyHandleOpenOptions.None); CodeAccessPermission.RevertAssert(); } ///// /// Format the key blob is expressed in /// public CngKeyBlobFormat BlobFormat { get { Contract.Ensures(Contract.Result() != null); Contract.Assert(m_format != null); return m_format; } } /// /// Clean up the key /// protected override void Dispose(bool disposing) { try { if (disposing) { if (m_key != null) { m_key.Dispose(); } } } finally { base.Dispose(disposing); } } ////// Hydrate a public key from a blob /// //// [System.Security.SecurityCritical] public static ECDiffieHellmanPublicKey FromByteArray(byte[] publicKeyBlob, CngKeyBlobFormat format) { if (publicKeyBlob == null) { throw new ArgumentNullException("publicKeyBlob"); } if (format == null) { throw new ArgumentNullException("format"); } using (CngKey imported = CngKey.Import(publicKeyBlob, format)) { if (imported.AlgorithmGroup != CngAlgorithmGroup.ECDiffieHellman) { throw new ArgumentException(SR.GetString(SR.Cryptography_ArgECDHRequiresECDHKey)); } return new ECDiffieHellmanCngPublicKey(imported); } } ///// /// Hydrate a public key from XML /// /// See code:System.Security.Cryptography.Rfc4050KeyFormatter#RFC4050ECKeyFormat for information /// about the XML format used. /// //// [System.Security.SecurityCritical] public static ECDiffieHellmanCngPublicKey FromXmlString(string xml) { if (xml == null) { throw new ArgumentNullException("xml"); } using (CngKey imported = Rfc4050KeyFormatter.FromXml(xml)) { if (imported.AlgorithmGroup != CngAlgorithmGroup.ECDiffieHellman) { throw new ArgumentException(SR.GetString(SR.Cryptography_ArgECDHRequiresECDHKey), "xml"); } return new ECDiffieHellmanCngPublicKey(imported); } } ///// /// Import the public key into CNG /// ///public CngKey Import() { Contract.Ensures(Contract.Result () != null); Contract.Assert(m_format != null); return CngKey.Import(ToByteArray(), BlobFormat); } /// /// Convert the key blob to XML /// /// See code:System.Security.Cryptography.Rfc4050KeyFormatter#RFC4050ECKeyFormat for information /// about the XML format used. /// public override string ToXmlString() { Contract.Ensures(!String.IsNullOrEmpty(Contract.Result())); if (m_key == null) { m_key = Import(); } return Rfc4050KeyFormatter.ToXml(m_key); } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // ==++== // // Copyright (c) Microsoft Corporation. All rights reserved. // // ==--== using System; using System.Runtime.Serialization; using System.Security; using System.Security.Permissions; using System.Diagnostics.Contracts; namespace System.Security.Cryptography { /// /// Public key used to do key exchange with the ECDiffieHellmanCng algorithm /// [Serializable] [System.Security.Permissions.HostProtection(MayLeakOnAbort = true)] public sealed class ECDiffieHellmanCngPublicKey : ECDiffieHellmanPublicKey { [NonSerialized] private CngKey m_key; private CngKeyBlobFormat m_format; ////// Wrap a CNG key /// //// [System.Security.SecurityCritical] internal ECDiffieHellmanCngPublicKey(CngKey key) : base(key.Export(CngKeyBlobFormat.EccPublicBlob)) { Contract.Requires(key != null && key.AlgorithmGroup == CngAlgorithmGroup.ECDiffieHellman); Contract.Ensures(m_format != null); m_format = CngKeyBlobFormat.EccPublicBlob; // // We need to make a copy of the key to prevent the situation where the ECDiffieHellmanCng algorithm // object is disposed (this disposing its key) before the ECDiffieHellmanCngPublic key is disposed. // // Accessing the handle in partial trust is safe because we're not exposing it back out to user code // new SecurityPermission(SecurityPermissionFlag.UnmanagedCode).Assert(); m_key = CngKey.Open(key.Handle, key.IsEphemeral ? CngKeyHandleOpenOptions.EphemeralKey : CngKeyHandleOpenOptions.None); CodeAccessPermission.RevertAssert(); } ///// /// Format the key blob is expressed in /// public CngKeyBlobFormat BlobFormat { get { Contract.Ensures(Contract.Result() != null); Contract.Assert(m_format != null); return m_format; } } /// /// Clean up the key /// protected override void Dispose(bool disposing) { try { if (disposing) { if (m_key != null) { m_key.Dispose(); } } } finally { base.Dispose(disposing); } } ////// Hydrate a public key from a blob /// //// [System.Security.SecurityCritical] public static ECDiffieHellmanPublicKey FromByteArray(byte[] publicKeyBlob, CngKeyBlobFormat format) { if (publicKeyBlob == null) { throw new ArgumentNullException("publicKeyBlob"); } if (format == null) { throw new ArgumentNullException("format"); } using (CngKey imported = CngKey.Import(publicKeyBlob, format)) { if (imported.AlgorithmGroup != CngAlgorithmGroup.ECDiffieHellman) { throw new ArgumentException(SR.GetString(SR.Cryptography_ArgECDHRequiresECDHKey)); } return new ECDiffieHellmanCngPublicKey(imported); } } ///// /// Hydrate a public key from XML /// /// See code:System.Security.Cryptography.Rfc4050KeyFormatter#RFC4050ECKeyFormat for information /// about the XML format used. /// //// [System.Security.SecurityCritical] public static ECDiffieHellmanCngPublicKey FromXmlString(string xml) { if (xml == null) { throw new ArgumentNullException("xml"); } using (CngKey imported = Rfc4050KeyFormatter.FromXml(xml)) { if (imported.AlgorithmGroup != CngAlgorithmGroup.ECDiffieHellman) { throw new ArgumentException(SR.GetString(SR.Cryptography_ArgECDHRequiresECDHKey), "xml"); } return new ECDiffieHellmanCngPublicKey(imported); } } ///// /// Import the public key into CNG /// ///public CngKey Import() { Contract.Ensures(Contract.Result () != null); Contract.Assert(m_format != null); return CngKey.Import(ToByteArray(), BlobFormat); } /// /// Convert the key blob to XML /// /// See code:System.Security.Cryptography.Rfc4050KeyFormatter#RFC4050ECKeyFormat for information /// about the XML format used. /// public override string ToXmlString() { Contract.Ensures(!String.IsNullOrEmpty(Contract.Result())); if (m_key == null) { m_key = Import(); } return Rfc4050KeyFormatter.ToXml(m_key); } } } // 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
- Overlapped.cs
- ValidationHelper.cs
- FileDataSourceCache.cs
- Buffer.cs
- EntityViewContainer.cs
- dataobject.cs
- FrameworkElementFactory.cs
- BinaryFormatterSinks.cs
- NetTcpSectionData.cs
- AutomationPropertyInfo.cs
- Visual3D.cs
- ToolStripSettings.cs
- ObjectItemLoadingSessionData.cs
- KnownBoxes.cs
- SupportsEventValidationAttribute.cs
- ContentValidator.cs
- ApplicationInfo.cs
- ToolStripButton.cs
- ControlCachePolicy.cs
- DiffuseMaterial.cs
- OracleConnection.cs
- TreeBuilderBamlTranslator.cs
- CustomAttributeFormatException.cs
- StylusTip.cs
- AttributeProviderAttribute.cs
- NamespaceMapping.cs
- TextDpi.cs
- UniqueConstraint.cs
- IgnoreFileBuildProvider.cs
- _ContextAwareResult.cs
- CharacterHit.cs
- ClientScriptItemCollection.cs
- PropertyOverridesTypeEditor.cs
- SingleSelectRootGridEntry.cs
- HttpCapabilitiesEvaluator.cs
- storepermission.cs
- GridViewUpdateEventArgs.cs
- InlineObject.cs
- StateDesigner.TransitionInfo.cs
- VirtualizingStackPanel.cs
- SqlCachedBuffer.cs
- ContractValidationHelper.cs
- SplineKeyFrames.cs
- ConditionalWeakTable.cs
- HasCopySemanticsAttribute.cs
- UncommonField.cs
- StringCollection.cs
- DBConcurrencyException.cs
- EntityContainerEmitter.cs
- SystemIPInterfaceStatistics.cs
- MatrixKeyFrameCollection.cs
- LeftCellWrapper.cs
- DockPanel.cs
- MasterPageBuildProvider.cs
- WebServiceClientProxyGenerator.cs
- namescope.cs
- FileLevelControlBuilderAttribute.cs
- DependencyPropertyChangedEventArgs.cs
- SequentialActivityDesigner.cs
- AddInBase.cs
- FlatButtonAppearance.cs
- HtmlTextViewAdapter.cs
- MaskedTextProvider.cs
- TextAction.cs
- AlignmentYValidation.cs
- PkcsMisc.cs
- AppDomain.cs
- Queue.cs
- XmlSchemaGroupRef.cs
- OutputCacheModule.cs
- Soap.cs
- TextModifier.cs
- QueryLifecycle.cs
- GeometryModel3D.cs
- InternalConfigSettingsFactory.cs
- TextBox.cs
- ToolTipService.cs
- ScriptComponentDescriptor.cs
- ExpressionNode.cs
- PointHitTestParameters.cs
- RtfNavigator.cs
- FuncCompletionCallbackWrapper.cs
- Mappings.cs
- SecurityTokenProvider.cs
- GACMembershipCondition.cs
- ListenerSessionConnectionReader.cs
- RemoveStoryboard.cs
- FrameworkElementFactoryMarkupObject.cs
- LineBreakRecord.cs
- ArgIterator.cs
- TreeNode.cs
- XmlObjectSerializerContext.cs
- AttributeProviderAttribute.cs
- IndentedWriter.cs
- ControlTemplate.cs
- PerformanceCounterLib.cs
- DataException.cs
- SqlLiftIndependentRowExpressions.cs
- WindowsRichEdit.cs
- HashAlgorithm.cs