Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / clr / src / BCL / System / Reflection / StrongNameKeyPair.cs / 1305376 / StrongNameKeyPair.cs
// ==++== // // Copyright (c) Microsoft Corporation. All rights reserved. // // ==--== /*============================================================ ** ** File: StrongNameKeyPair.cs ** **[....] ** ** ** Purpose: Encapsulate access to a public/private key pair ** used to sign strong name assemblies. ** ** ===========================================================*/ namespace System.Reflection { using System; using System.IO; using System.Runtime.CompilerServices; using System.Runtime.ConstrainedExecution; using System.Runtime.InteropServices; using System.Runtime.Serialization; using System.Security; using System.Security.Permissions; using System.Runtime.Versioning; using Microsoft.Win32; using System.Diagnostics.Contracts; #if !FEATURE_CORECLR using Microsoft.Runtime.Hosting; #endif [Serializable] [System.Runtime.InteropServices.ComVisible(true)] public class StrongNameKeyPair : IDeserializationCallback, ISerializable { private bool _keyPairExported; private byte[] _keyPairArray; private String _keyPairContainer; private byte[] _publicKey; // Build key pair from file. [System.Security.SecuritySafeCritical] // auto-generated [SecurityPermissionAttribute(SecurityAction.Demand, Flags=SecurityPermissionFlag.UnmanagedCode)] public StrongNameKeyPair(FileStream keyPairFile) { if (keyPairFile == null) throw new ArgumentNullException("keyPairFile"); Contract.EndContractBlock(); int length = (int)keyPairFile.Length; _keyPairArray = new byte[length]; keyPairFile.Read(_keyPairArray, 0, length); _keyPairExported = true; } // Build key pair from byte array in memory. [System.Security.SecuritySafeCritical] // auto-generated [SecurityPermissionAttribute(SecurityAction.Demand, Flags=SecurityPermissionFlag.UnmanagedCode)] public StrongNameKeyPair(byte[] keyPairArray) { if (keyPairArray == null) throw new ArgumentNullException("keyPairArray"); Contract.EndContractBlock(); _keyPairArray = new byte[keyPairArray.Length]; Array.Copy(keyPairArray, _keyPairArray, keyPairArray.Length); _keyPairExported = true; } // Reference key pair in named key container. [System.Security.SecuritySafeCritical] // auto-generated [SecurityPermissionAttribute(SecurityAction.Demand, Flags=SecurityPermissionFlag.UnmanagedCode)] public StrongNameKeyPair(String keyPairContainer) { if (keyPairContainer == null) throw new ArgumentNullException("keyPairContainer"); Contract.EndContractBlock(); _keyPairContainer = keyPairContainer; _keyPairExported = false; } [System.Security.SecuritySafeCritical] // auto-generated [SecurityPermissionAttribute(SecurityAction.Demand, Flags=SecurityPermissionFlag.UnmanagedCode)] protected StrongNameKeyPair (SerializationInfo info, StreamingContext context) { _keyPairExported = (bool) info.GetValue("_keyPairExported", typeof(bool)); _keyPairArray = (byte[]) info.GetValue("_keyPairArray", typeof(byte[])); _keyPairContainer = (string) info.GetValue("_keyPairContainer", typeof(string)); _publicKey = (byte[]) info.GetValue("_publicKey", typeof(byte[])); } // Get the public portion of the key pair. public byte[] PublicKey { [System.Security.SecuritySafeCritical] // auto-generated get { if (_publicKey == null) { _publicKey = ComputePublicKey(); } byte[] publicKey = new byte[_publicKey.Length]; Array.Copy(_publicKey, publicKey, _publicKey.Length); return publicKey; } } #if FEATURE_CORECLR [System.Security.SecurityCritical] // auto-generated private unsafe byte[] ComputePublicKey() { byte[] publicKey = null; // Make sure pbPublicKey is not leaked with async exceptions RuntimeHelpers.PrepareConstrainedRegions(); try { } finally { byte* pbPublicKey = null; int cbPublicKey = 0; try { bool result; if (_keyPairExported) { result = Win32Native.StrongNameGetPublicKey(null, _keyPairArray, _keyPairArray.Length, out pbPublicKey, out cbPublicKey); } else { result = Win32Native.StrongNameGetPublicKey(_keyPairContainer, null, 0, out pbPublicKey, out cbPublicKey); } if (!result) throw new ArgumentException(Environment.GetResourceString("Argument_StrongNameGetPublicKey")); publicKey = new byte[cbPublicKey]; Buffer.memcpy(pbPublicKey, 0, publicKey, 0, cbPublicKey); } finally { if (pbPublicKey != null) Win32Native.StrongNameFreeBuffer(pbPublicKey); } } return publicKey; } #else // FEATURE_CORECLR [System.Security.SecurityCritical] // auto-generated private unsafe byte[] ComputePublicKey() { byte[] publicKey = null; // Make sure pbPublicKey is not leaked with async exceptions RuntimeHelpers.PrepareConstrainedRegions(); try { } finally { IntPtr pbPublicKey = IntPtr.Zero; int cbPublicKey = 0; try { bool result; if (_keyPairExported) { result = StrongNameHelpers.StrongNameGetPublicKey(null, _keyPairArray, _keyPairArray.Length, out pbPublicKey, out cbPublicKey); } else { result = StrongNameHelpers.StrongNameGetPublicKey(_keyPairContainer, null, 0, out pbPublicKey, out cbPublicKey); } if (!result) throw new ArgumentException(Environment.GetResourceString("Argument_StrongNameGetPublicKey")); publicKey = new byte[cbPublicKey]; Buffer.memcpy((byte*) (pbPublicKey.ToPointer()), 0, publicKey, 0, cbPublicKey); } finally { if (pbPublicKey != IntPtr.Zero) StrongNameHelpers.StrongNameFreeBuffer(pbPublicKey); } } return publicKey; } #endif // FEATURE_CORECLR #if FEATURE_SERIALIZATION ///[System.Security.SecurityCritical] void ISerializable.GetObjectData (SerializationInfo info, StreamingContext context) { info.AddValue("_keyPairExported", _keyPairExported); info.AddValue("_keyPairArray", _keyPairArray); info.AddValue("_keyPairContainer", _keyPairContainer); info.AddValue("_publicKey", _publicKey); } /// void IDeserializationCallback.OnDeserialization (Object sender) {} #endif // Internal routine used to retrieve key pair info from unmanaged code. private bool GetKeyPair(out Object arrayOrContainer) { arrayOrContainer = _keyPairExported ? (Object)_keyPairArray : (Object)_keyPairContainer; return _keyPairExported; } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // ==++== // // Copyright (c) Microsoft Corporation. All rights reserved. // // ==--== /*============================================================ ** ** File: StrongNameKeyPair.cs ** ** [....] ** ** ** Purpose: Encapsulate access to a public/private key pair ** used to sign strong name assemblies. ** ** ===========================================================*/ namespace System.Reflection { using System; using System.IO; using System.Runtime.CompilerServices; using System.Runtime.ConstrainedExecution; using System.Runtime.InteropServices; using System.Runtime.Serialization; using System.Security; using System.Security.Permissions; using System.Runtime.Versioning; using Microsoft.Win32; using System.Diagnostics.Contracts; #if !FEATURE_CORECLR using Microsoft.Runtime.Hosting; #endif [Serializable] [System.Runtime.InteropServices.ComVisible(true)] public class StrongNameKeyPair : IDeserializationCallback, ISerializable { private bool _keyPairExported; private byte[] _keyPairArray; private String _keyPairContainer; private byte[] _publicKey; // Build key pair from file. [System.Security.SecuritySafeCritical] // auto-generated [SecurityPermissionAttribute(SecurityAction.Demand, Flags=SecurityPermissionFlag.UnmanagedCode)] public StrongNameKeyPair(FileStream keyPairFile) { if (keyPairFile == null) throw new ArgumentNullException("keyPairFile"); Contract.EndContractBlock(); int length = (int)keyPairFile.Length; _keyPairArray = new byte[length]; keyPairFile.Read(_keyPairArray, 0, length); _keyPairExported = true; } // Build key pair from byte array in memory. [System.Security.SecuritySafeCritical] // auto-generated [SecurityPermissionAttribute(SecurityAction.Demand, Flags=SecurityPermissionFlag.UnmanagedCode)] public StrongNameKeyPair(byte[] keyPairArray) { if (keyPairArray == null) throw new ArgumentNullException("keyPairArray"); Contract.EndContractBlock(); _keyPairArray = new byte[keyPairArray.Length]; Array.Copy(keyPairArray, _keyPairArray, keyPairArray.Length); _keyPairExported = true; } // Reference key pair in named key container. [System.Security.SecuritySafeCritical] // auto-generated [SecurityPermissionAttribute(SecurityAction.Demand, Flags=SecurityPermissionFlag.UnmanagedCode)] public StrongNameKeyPair(String keyPairContainer) { if (keyPairContainer == null) throw new ArgumentNullException("keyPairContainer"); Contract.EndContractBlock(); _keyPairContainer = keyPairContainer; _keyPairExported = false; } [System.Security.SecuritySafeCritical] // auto-generated [SecurityPermissionAttribute(SecurityAction.Demand, Flags=SecurityPermissionFlag.UnmanagedCode)] protected StrongNameKeyPair (SerializationInfo info, StreamingContext context) { _keyPairExported = (bool) info.GetValue("_keyPairExported", typeof(bool)); _keyPairArray = (byte[]) info.GetValue("_keyPairArray", typeof(byte[])); _keyPairContainer = (string) info.GetValue("_keyPairContainer", typeof(string)); _publicKey = (byte[]) info.GetValue("_publicKey", typeof(byte[])); } // Get the public portion of the key pair. public byte[] PublicKey { [System.Security.SecuritySafeCritical] // auto-generated get { if (_publicKey == null) { _publicKey = ComputePublicKey(); } byte[] publicKey = new byte[_publicKey.Length]; Array.Copy(_publicKey, publicKey, _publicKey.Length); return publicKey; } } #if FEATURE_CORECLR [System.Security.SecurityCritical] // auto-generated private unsafe byte[] ComputePublicKey() { byte[] publicKey = null; // Make sure pbPublicKey is not leaked with async exceptions RuntimeHelpers.PrepareConstrainedRegions(); try { } finally { byte* pbPublicKey = null; int cbPublicKey = 0; try { bool result; if (_keyPairExported) { result = Win32Native.StrongNameGetPublicKey(null, _keyPairArray, _keyPairArray.Length, out pbPublicKey, out cbPublicKey); } else { result = Win32Native.StrongNameGetPublicKey(_keyPairContainer, null, 0, out pbPublicKey, out cbPublicKey); } if (!result) throw new ArgumentException(Environment.GetResourceString("Argument_StrongNameGetPublicKey")); publicKey = new byte[cbPublicKey]; Buffer.memcpy(pbPublicKey, 0, publicKey, 0, cbPublicKey); } finally { if (pbPublicKey != null) Win32Native.StrongNameFreeBuffer(pbPublicKey); } } return publicKey; } #else // FEATURE_CORECLR [System.Security.SecurityCritical] // auto-generated private unsafe byte[] ComputePublicKey() { byte[] publicKey = null; // Make sure pbPublicKey is not leaked with async exceptions RuntimeHelpers.PrepareConstrainedRegions(); try { } finally { IntPtr pbPublicKey = IntPtr.Zero; int cbPublicKey = 0; try { bool result; if (_keyPairExported) { result = StrongNameHelpers.StrongNameGetPublicKey(null, _keyPairArray, _keyPairArray.Length, out pbPublicKey, out cbPublicKey); } else { result = StrongNameHelpers.StrongNameGetPublicKey(_keyPairContainer, null, 0, out pbPublicKey, out cbPublicKey); } if (!result) throw new ArgumentException(Environment.GetResourceString("Argument_StrongNameGetPublicKey")); publicKey = new byte[cbPublicKey]; Buffer.memcpy((byte*) (pbPublicKey.ToPointer()), 0, publicKey, 0, cbPublicKey); } finally { if (pbPublicKey != IntPtr.Zero) StrongNameHelpers.StrongNameFreeBuffer(pbPublicKey); } } return publicKey; } #endif // FEATURE_CORECLR #if FEATURE_SERIALIZATION ///[System.Security.SecurityCritical] void ISerializable.GetObjectData (SerializationInfo info, StreamingContext context) { info.AddValue("_keyPairExported", _keyPairExported); info.AddValue("_keyPairArray", _keyPairArray); info.AddValue("_keyPairContainer", _keyPairContainer); info.AddValue("_publicKey", _publicKey); } /// void IDeserializationCallback.OnDeserialization (Object sender) {} #endif // Internal routine used to retrieve key pair info from unmanaged code. private bool GetKeyPair(out Object arrayOrContainer) { arrayOrContainer = _keyPairExported ? (Object)_keyPairArray : (Object)_keyPairContainer; return _keyPairExported; } } } // 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
- ConvertEvent.cs
- DBConnectionString.cs
- SqlDataSource.cs
- MoveSizeWinEventHandler.cs
- DataGridViewControlCollection.cs
- PropertyTabChangedEvent.cs
- IndexerNameAttribute.cs
- PackagingUtilities.cs
- WebPartZoneBaseDesigner.cs
- CodeDirectiveCollection.cs
- DataBinder.cs
- Int32AnimationBase.cs
- ControlAdapter.cs
- ComponentGuaranteesAttribute.cs
- SimpleMailWebEventProvider.cs
- SkewTransform.cs
- Version.cs
- TextBlockAutomationPeer.cs
- BindingContext.cs
- ParsedAttributeCollection.cs
- ISO2022Encoding.cs
- CheckBoxStandardAdapter.cs
- CustomCredentialPolicy.cs
- _DomainName.cs
- NetworkInformationPermission.cs
- NumberFunctions.cs
- ImageMap.cs
- WebPartEditorApplyVerb.cs
- httpapplicationstate.cs
- SimpleMailWebEventProvider.cs
- SqlCacheDependencyDatabaseCollection.cs
- AttachedAnnotation.cs
- NullableFloatAverageAggregationOperator.cs
- CodeDirectoryCompiler.cs
- Page.cs
- ISAPIWorkerRequest.cs
- Highlights.cs
- CqlGenerator.cs
- AdCreatedEventArgs.cs
- WindowsListViewItemStartMenu.cs
- ToolZone.cs
- IisTraceListener.cs
- BitmapEffectInputData.cs
- Int32CollectionConverter.cs
- DataControlFieldCollection.cs
- DataGridViewCellCancelEventArgs.cs
- Descriptor.cs
- XmlUtf8RawTextWriter.cs
- LockRenewalTask.cs
- DataGridViewBand.cs
- WebPart.cs
- XmlBindingWorker.cs
- EpmCustomContentWriterNodeData.cs
- DataPager.cs
- TypeDelegator.cs
- CompositeDispatchFormatter.cs
- DebugHandleTracker.cs
- PathSegmentCollection.cs
- BinaryQueryOperator.cs
- ProfileInfo.cs
- SafeRightsManagementPubHandle.cs
- SingleKeyFrameCollection.cs
- GZipObjectSerializer.cs
- SqlCommandBuilder.cs
- Point4D.cs
- SourceElementsCollection.cs
- ObjectPersistData.cs
- _NegotiateClient.cs
- OleDbPermission.cs
- TextEditorDragDrop.cs
- MULTI_QI.cs
- ToolStripSettings.cs
- RecognizerBase.cs
- NumberSubstitution.cs
- DataListItem.cs
- ContainerActivationHelper.cs
- SecureEnvironment.cs
- Mappings.cs
- SqlConnectionPoolProviderInfo.cs
- ParamArrayAttribute.cs
- COM2Enum.cs
- Delegate.cs
- ArraySubsetEnumerator.cs
- NameNode.cs
- CmsInterop.cs
- xamlnodes.cs
- KnowledgeBase.cs
- BufferedStream.cs
- TimeSpan.cs
- FileDataSourceCache.cs
- WebPartConnectionsCancelEventArgs.cs
- TcpConnectionPoolSettings.cs
- WindowsAuthenticationEventArgs.cs
- JoinCqlBlock.cs
- NamespaceTable.cs
- SrgsDocumentParser.cs
- LifetimeServices.cs
- SafePEFileHandle.cs
- MasterPageParser.cs
- XmlNodeReader.cs