Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / cdf / src / WCF / infocard / Client / System / IdentityModel / Selectors / InfoCardRSACryptoProvider.cs / 1305376 / InfoCardRSACryptoProvider.cs
//------------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
namespace System.IdentityModel.Selectors
{
using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Runtime.ConstrainedExecution;
using System.Runtime.CompilerServices;
using System.Security.Cryptography;
using IDT = Microsoft.InfoCards.Diagnostics.InfoCardTrace;
using DiagnosticUtility = Microsoft.InfoCards.Diagnostics.DiagnosticUtility;
//
// For common & resources
//
using Microsoft.InfoCards;
//
// Summary:
// This class provides indirect access to the private key associated with a released token via InfoCard
// native crypto functions.
//
internal class InfoCardRSACryptoProvider : RSA
{
AsymmetricCryptoHandle m_cryptoHandle;
RpcAsymmetricCryptoParameters m_params;
//
// Summary:
// Given a pointer to a CryptoHandle create a new instance of this class.
//
public InfoCardRSACryptoProvider( AsymmetricCryptoHandle cryptoHandle ) : base()
{
m_cryptoHandle = (AsymmetricCryptoHandle)cryptoHandle.Duplicate();
try
{
m_params = (RpcAsymmetricCryptoParameters) m_cryptoHandle.Parameters;
int keySize = m_params.keySize;
LegalKeySizesValue = new KeySizes[ 1 ];
KeySizeValue = keySize;
LegalKeySizesValue[ 0 ] = new KeySizes( keySize, keySize, 0 );
}
catch
{
m_cryptoHandle.Dispose();
m_cryptoHandle = null;
throw;
}
}
public override String SignatureAlgorithm {
get { return m_params.signatureAlgorithm; }
}
public override String KeyExchangeAlgorithm {
get { return m_params.keyExchangeAlgorithm; }
}
public override byte[] EncryptValue( byte[] rgb )
{
throw IDT.ThrowHelperError( new NotSupportedException() );
}
public override byte[] DecryptValue( byte[] rgb )
{
throw IDT.ThrowHelperError( new NotSupportedException() );
}
public byte[] Decrypt(byte[] inData, bool fAOEP )
{
GlobalAllocSafeHandle pOutData = null;
int cbOutData = 0;
byte[] outData;
IDT.ThrowInvalidArgumentConditional( null == inData, "indata" );
using( HGlobalSafeHandle pInData = HGlobalSafeHandle.Construct( inData.Length ) )
{
Marshal.Copy( inData, 0, pInData.DangerousGetHandle(), inData.Length );
int status = CardSpaceSelector.GetShim().m_csShimDecrypt( m_cryptoHandle.InternalHandle,
fAOEP,
inData.Length,
pInData,
out cbOutData,
out pOutData );
if( 0 != status )
{
ExceptionHelper.ThrowIfCardSpaceException( status );
throw IDT.ThrowHelperError( new Win32Exception( status ) );
}
pOutData.Length = cbOutData;
outData = DiagnosticUtility.Utility.AllocateByteArray( pOutData.Length );
using( pOutData )
{
Marshal.Copy( pOutData.DangerousGetHandle(), outData, 0, pOutData.Length );
}
}
return outData;
}
public byte[] Encrypt(byte[] inData, bool fAOEP )
{
GlobalAllocSafeHandle pOutData = null;
int cbOutData = 0;
byte[] outData;
IDT.ThrowInvalidArgumentConditional( null == inData, "indata" );
using( HGlobalSafeHandle pInData = HGlobalSafeHandle.Construct( inData.Length ) )
{
Marshal.Copy( inData, 0, pInData.DangerousGetHandle(), inData.Length );
int status = CardSpaceSelector.GetShim().m_csShimEncrypt( m_cryptoHandle.InternalHandle,
fAOEP,
inData.Length,
pInData,
out cbOutData,
out pOutData );
if( 0 != status )
{
ExceptionHelper.ThrowIfCardSpaceException( status );
throw IDT.ThrowHelperError( new Win32Exception( status ) );
}
pOutData.Length = cbOutData;
outData = DiagnosticUtility.Utility.AllocateByteArray( pOutData.Length );
Marshal.Copy( pOutData.DangerousGetHandle(), outData, 0, pOutData.Length );
}
return outData;
}
public byte[] SignHash( byte[] hash, string hashAlgOid )
{
IDT.ThrowInvalidArgumentConditional( null == hash || 0 == hash.Length, "hash" );
IDT.ThrowInvalidArgumentConditional( String.IsNullOrEmpty( hashAlgOid ), "hashAlgOid" );
int cbSig = 0;
GlobalAllocSafeHandle pSig = null;
byte[] sig;
using( HGlobalSafeHandle pHash = HGlobalSafeHandle.Construct( hash.Length ) )
{
using( HGlobalSafeHandle pHashAlgOid = HGlobalSafeHandle.Construct( hashAlgOid ) )
{
Marshal.Copy( hash, 0, pHash.DangerousGetHandle(), hash.Length );
RuntimeHelpers.PrepareConstrainedRegions();
int status = CardSpaceSelector.GetShim().m_csShimSignHash( m_cryptoHandle.InternalHandle,
hash.Length,
pHash,
pHashAlgOid,
out cbSig,
out pSig );
if( 0 != status )
{
ExceptionHelper.ThrowIfCardSpaceException( status );
throw IDT.ThrowHelperError( new Win32Exception( status ) );
}
pSig.Length = cbSig;
sig = DiagnosticUtility.Utility.AllocateByteArray( pSig.Length);
using( pSig )
{
Marshal.Copy( pSig.DangerousGetHandle(), sig, 0, pSig.Length );
}
}
}
return sig;
}
public bool VerifyHash( byte[] hash, string hashAlgOid, byte[] sig )
{
IDT.ThrowInvalidArgumentConditional( null == hash || 0 == hash.Length, "hash" );
IDT.ThrowInvalidArgumentConditional( String.IsNullOrEmpty( hashAlgOid ), "hashAlgOid" );
IDT.ThrowInvalidArgumentConditional( null == sig || 0 == sig.Length, "sig" );
bool verified = false;
using( HGlobalSafeHandle pHash = HGlobalSafeHandle.Construct( hash.Length ) )
{
using( HGlobalSafeHandle pHashAlgOid = HGlobalSafeHandle.Construct( hashAlgOid ) )
{
Marshal.Copy( hash, 0, pHash.DangerousGetHandle(), hash.Length );
int status = 0;
using( HGlobalSafeHandle pSig = HGlobalSafeHandle.Construct( sig.Length ) )
{
Marshal.Copy( sig, 0, pSig.DangerousGetHandle(), sig.Length );
status = CardSpaceSelector.GetShim().m_csShimVerifyHash( m_cryptoHandle.InternalHandle,
hash.Length,
pHash,
pHashAlgOid,
sig.Length,
pSig,
out verified);
}
if( 0 != status )
{
ExceptionHelper.ThrowIfCardSpaceException( status );
throw IDT.ThrowHelperError( new Win32Exception( status ) );
}
}
}
return verified;
}
public override RSAParameters ExportParameters(bool includePrivateParameters)
{
throw IDT.ThrowHelperError( new NotSupportedException() );
}
public override string ToXmlString(bool includePrivateParameters)
{
throw IDT.ThrowHelperError( new NotSupportedException() );
}
public override void FromXmlString(string xmlString)
{
throw IDT.ThrowHelperError( new NotSupportedException() );
}
public override void ImportParameters(System.Security.Cryptography.RSAParameters parameters)
{
throw IDT.ThrowHelperError( new NotSupportedException() );
}
protected override void Dispose(bool disposing)
{
m_cryptoHandle.Dispose();
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
//------------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
namespace System.IdentityModel.Selectors
{
using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Runtime.ConstrainedExecution;
using System.Runtime.CompilerServices;
using System.Security.Cryptography;
using IDT = Microsoft.InfoCards.Diagnostics.InfoCardTrace;
using DiagnosticUtility = Microsoft.InfoCards.Diagnostics.DiagnosticUtility;
//
// For common & resources
//
using Microsoft.InfoCards;
//
// Summary:
// This class provides indirect access to the private key associated with a released token via InfoCard
// native crypto functions.
//
internal class InfoCardRSACryptoProvider : RSA
{
AsymmetricCryptoHandle m_cryptoHandle;
RpcAsymmetricCryptoParameters m_params;
//
// Summary:
// Given a pointer to a CryptoHandle create a new instance of this class.
//
public InfoCardRSACryptoProvider( AsymmetricCryptoHandle cryptoHandle ) : base()
{
m_cryptoHandle = (AsymmetricCryptoHandle)cryptoHandle.Duplicate();
try
{
m_params = (RpcAsymmetricCryptoParameters) m_cryptoHandle.Parameters;
int keySize = m_params.keySize;
LegalKeySizesValue = new KeySizes[ 1 ];
KeySizeValue = keySize;
LegalKeySizesValue[ 0 ] = new KeySizes( keySize, keySize, 0 );
}
catch
{
m_cryptoHandle.Dispose();
m_cryptoHandle = null;
throw;
}
}
public override String SignatureAlgorithm {
get { return m_params.signatureAlgorithm; }
}
public override String KeyExchangeAlgorithm {
get { return m_params.keyExchangeAlgorithm; }
}
public override byte[] EncryptValue( byte[] rgb )
{
throw IDT.ThrowHelperError( new NotSupportedException() );
}
public override byte[] DecryptValue( byte[] rgb )
{
throw IDT.ThrowHelperError( new NotSupportedException() );
}
public byte[] Decrypt(byte[] inData, bool fAOEP )
{
GlobalAllocSafeHandle pOutData = null;
int cbOutData = 0;
byte[] outData;
IDT.ThrowInvalidArgumentConditional( null == inData, "indata" );
using( HGlobalSafeHandle pInData = HGlobalSafeHandle.Construct( inData.Length ) )
{
Marshal.Copy( inData, 0, pInData.DangerousGetHandle(), inData.Length );
int status = CardSpaceSelector.GetShim().m_csShimDecrypt( m_cryptoHandle.InternalHandle,
fAOEP,
inData.Length,
pInData,
out cbOutData,
out pOutData );
if( 0 != status )
{
ExceptionHelper.ThrowIfCardSpaceException( status );
throw IDT.ThrowHelperError( new Win32Exception( status ) );
}
pOutData.Length = cbOutData;
outData = DiagnosticUtility.Utility.AllocateByteArray( pOutData.Length );
using( pOutData )
{
Marshal.Copy( pOutData.DangerousGetHandle(), outData, 0, pOutData.Length );
}
}
return outData;
}
public byte[] Encrypt(byte[] inData, bool fAOEP )
{
GlobalAllocSafeHandle pOutData = null;
int cbOutData = 0;
byte[] outData;
IDT.ThrowInvalidArgumentConditional( null == inData, "indata" );
using( HGlobalSafeHandle pInData = HGlobalSafeHandle.Construct( inData.Length ) )
{
Marshal.Copy( inData, 0, pInData.DangerousGetHandle(), inData.Length );
int status = CardSpaceSelector.GetShim().m_csShimEncrypt( m_cryptoHandle.InternalHandle,
fAOEP,
inData.Length,
pInData,
out cbOutData,
out pOutData );
if( 0 != status )
{
ExceptionHelper.ThrowIfCardSpaceException( status );
throw IDT.ThrowHelperError( new Win32Exception( status ) );
}
pOutData.Length = cbOutData;
outData = DiagnosticUtility.Utility.AllocateByteArray( pOutData.Length );
Marshal.Copy( pOutData.DangerousGetHandle(), outData, 0, pOutData.Length );
}
return outData;
}
public byte[] SignHash( byte[] hash, string hashAlgOid )
{
IDT.ThrowInvalidArgumentConditional( null == hash || 0 == hash.Length, "hash" );
IDT.ThrowInvalidArgumentConditional( String.IsNullOrEmpty( hashAlgOid ), "hashAlgOid" );
int cbSig = 0;
GlobalAllocSafeHandle pSig = null;
byte[] sig;
using( HGlobalSafeHandle pHash = HGlobalSafeHandle.Construct( hash.Length ) )
{
using( HGlobalSafeHandle pHashAlgOid = HGlobalSafeHandle.Construct( hashAlgOid ) )
{
Marshal.Copy( hash, 0, pHash.DangerousGetHandle(), hash.Length );
RuntimeHelpers.PrepareConstrainedRegions();
int status = CardSpaceSelector.GetShim().m_csShimSignHash( m_cryptoHandle.InternalHandle,
hash.Length,
pHash,
pHashAlgOid,
out cbSig,
out pSig );
if( 0 != status )
{
ExceptionHelper.ThrowIfCardSpaceException( status );
throw IDT.ThrowHelperError( new Win32Exception( status ) );
}
pSig.Length = cbSig;
sig = DiagnosticUtility.Utility.AllocateByteArray( pSig.Length);
using( pSig )
{
Marshal.Copy( pSig.DangerousGetHandle(), sig, 0, pSig.Length );
}
}
}
return sig;
}
public bool VerifyHash( byte[] hash, string hashAlgOid, byte[] sig )
{
IDT.ThrowInvalidArgumentConditional( null == hash || 0 == hash.Length, "hash" );
IDT.ThrowInvalidArgumentConditional( String.IsNullOrEmpty( hashAlgOid ), "hashAlgOid" );
IDT.ThrowInvalidArgumentConditional( null == sig || 0 == sig.Length, "sig" );
bool verified = false;
using( HGlobalSafeHandle pHash = HGlobalSafeHandle.Construct( hash.Length ) )
{
using( HGlobalSafeHandle pHashAlgOid = HGlobalSafeHandle.Construct( hashAlgOid ) )
{
Marshal.Copy( hash, 0, pHash.DangerousGetHandle(), hash.Length );
int status = 0;
using( HGlobalSafeHandle pSig = HGlobalSafeHandle.Construct( sig.Length ) )
{
Marshal.Copy( sig, 0, pSig.DangerousGetHandle(), sig.Length );
status = CardSpaceSelector.GetShim().m_csShimVerifyHash( m_cryptoHandle.InternalHandle,
hash.Length,
pHash,
pHashAlgOid,
sig.Length,
pSig,
out verified);
}
if( 0 != status )
{
ExceptionHelper.ThrowIfCardSpaceException( status );
throw IDT.ThrowHelperError( new Win32Exception( status ) );
}
}
}
return verified;
}
public override RSAParameters ExportParameters(bool includePrivateParameters)
{
throw IDT.ThrowHelperError( new NotSupportedException() );
}
public override string ToXmlString(bool includePrivateParameters)
{
throw IDT.ThrowHelperError( new NotSupportedException() );
}
public override void FromXmlString(string xmlString)
{
throw IDT.ThrowHelperError( new NotSupportedException() );
}
public override void ImportParameters(System.Security.Cryptography.RSAParameters parameters)
{
throw IDT.ThrowHelperError( new NotSupportedException() );
}
protected override void Dispose(bool disposing)
{
m_cryptoHandle.Dispose();
}
}
}
// 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
- assemblycache.cs
- ServiceModelSecurityTokenTypes.cs
- LinqDataSourceUpdateEventArgs.cs
- FixedHyperLink.cs
- DetailsViewDeletedEventArgs.cs
- ExpressionBuilderCollection.cs
- MetafileHeaderWmf.cs
- ProcessManager.cs
- CompilerGeneratedAttribute.cs
- _DomainName.cs
- HtmlInputFile.cs
- SafeMemoryMappedViewHandle.cs
- SiteOfOriginPart.cs
- DockPanel.cs
- ObjectDataSourceFilteringEventArgs.cs
- DataGridClipboardHelper.cs
- ZoomingMessageFilter.cs
- Transform3DGroup.cs
- LayoutUtils.cs
- GB18030Encoding.cs
- XmlConvert.cs
- PointConverter.cs
- DataRelationCollection.cs
- GeneralTransform3DGroup.cs
- HandlerBase.cs
- URLMembershipCondition.cs
- SamlAuthorizationDecisionClaimResource.cs
- ClickablePoint.cs
- BuildTopDownAttribute.cs
- Baml2006ReaderSettings.cs
- InkCanvasAutomationPeer.cs
- TraceSwitch.cs
- SqlWorkflowInstanceStoreLock.cs
- ErrorInfoXmlDocument.cs
- StringFreezingAttribute.cs
- RestHandlerFactory.cs
- FixedPage.cs
- selecteditemcollection.cs
- CheckBoxPopupAdapter.cs
- TdsRecordBufferSetter.cs
- Config.cs
- _FixedSizeReader.cs
- TranslateTransform.cs
- ProcessHostFactoryHelper.cs
- RemoteWebConfigurationHost.cs
- QilTargetType.cs
- AnnotationResourceCollection.cs
- GridItemPattern.cs
- ProxyWebPartManagerDesigner.cs
- SqlCacheDependencyDatabase.cs
- PerformanceCounter.cs
- PolicyLevel.cs
- Mutex.cs
- RightNameExpirationInfoPair.cs
- RIPEMD160.cs
- BindingsCollection.cs
- ComplexBindingPropertiesAttribute.cs
- DataGridViewCellFormattingEventArgs.cs
- NavigationService.cs
- SoapHeader.cs
- XmlSchemas.cs
- SupportsEventValidationAttribute.cs
- Stroke2.cs
- XamlPathDataSerializer.cs
- Thickness.cs
- FusionWrap.cs
- DataListItemEventArgs.cs
- OdbcConnectionOpen.cs
- DataKeyCollection.cs
- Expressions.cs
- LineGeometry.cs
- AuthenticationConfig.cs
- RTLAwareMessageBox.cs
- DataContractSerializerServiceBehavior.cs
- HttpCachePolicyWrapper.cs
- TypeLoader.cs
- EntityDataSourceContainerNameItem.cs
- TagMapInfo.cs
- TextContainer.cs
- IgnorePropertiesAttribute.cs
- XamlSerializerUtil.cs
- GroupBoxAutomationPeer.cs
- DesignerHierarchicalDataSourceView.cs
- Image.cs
- StackSpiller.Temps.cs
- DataGridViewCellLinkedList.cs
- BoundPropertyEntry.cs
- TraceContextRecord.cs
- WpfSharedXamlSchemaContext.cs
- XdrBuilder.cs
- StringFormat.cs
- PenContext.cs
- TypeHelper.cs
- ToolStripContentPanelDesigner.cs
- DataGridViewCellParsingEventArgs.cs
- CfgSemanticTag.cs
- UdpChannelFactory.cs
- ComponentDispatcherThread.cs
- ScriptDescriptor.cs
- WorkflowMarkupElementEventArgs.cs