Code:
/ 4.0 / 4.0 / untmp / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / cdf / src / WCF / IdentityModel / System / IdentityModel / Tokens / RsaSecurityKey.cs / 1305376 / RsaSecurityKey.cs
//------------------------------------------------------------ // Copyright (c) Microsoft Corporation. All rights reserved. //----------------------------------------------------------- namespace System.IdentityModel.Tokens { using System.IdentityModel.Selectors; using System.Security.Cryptography; using System.Security.Cryptography.Xml; sealed public class RsaSecurityKey : AsymmetricSecurityKey { PrivateKeyStatus privateKeyStatus = PrivateKeyStatus.AvailabilityNotDetermined; readonly RSA rsa; public RsaSecurityKey(RSA rsa) { if (rsa == null) throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("rsa"); this.rsa = rsa; } public override int KeySize { get { return this.rsa.KeySize; } } public override byte[] DecryptKey( string algorithm, byte[] keyData ) { switch ( algorithm ) { case SecurityAlgorithms.RsaV15KeyWrap: return EncryptedXml.DecryptKey( keyData, rsa, false ); case SecurityAlgorithms.RsaOaepKeyWrap: return EncryptedXml.DecryptKey( keyData, rsa, true ); default: if ( IsSupportedAlgorithm( algorithm ) ) return EncryptedXml.DecryptKey( keyData, rsa, false ); throw DiagnosticUtility.ExceptionUtility.ThrowHelperError( new CryptographicException( SR.GetString( SR.UnsupportedAlgorithmForCryptoOperation, algorithm, "DecryptKey" ) ) ); } } public override byte[] EncryptKey( string algorithm, byte[] keyData ) { switch ( algorithm ) { case SecurityAlgorithms.RsaV15KeyWrap: return EncryptedXml.EncryptKey( keyData, rsa, false ); case SecurityAlgorithms.RsaOaepKeyWrap: return EncryptedXml.EncryptKey( keyData, rsa, true ); default: if ( IsSupportedAlgorithm( algorithm ) ) return EncryptedXml.EncryptKey( keyData, rsa, false ); throw DiagnosticUtility.ExceptionUtility.ThrowHelperError( new CryptographicException( SR.GetString( SR.UnsupportedAlgorithmForCryptoOperation, algorithm, "EncryptKey" ) ) ); } } public override AsymmetricAlgorithm GetAsymmetricAlgorithm(string algorithm, bool requiresPrivateKey) { if (requiresPrivateKey && !HasPrivateKey()) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CryptographicException(SR.GetString(SR.NoPrivateKeyAvailable))); } return this.rsa; } public override HashAlgorithm GetHashAlgorithmForSignature( string algorithm ) { if ( String.IsNullOrEmpty( algorithm ) ) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument( algorithm, SR.GetString( SR.EmptyOrNullArgumentString, "algorithm" ) ); } object algorithmObject = CryptoHelper.GetAlgorithmFromConfig( algorithm ); if ( algorithmObject != null ) { SignatureDescription description = algorithmObject as SignatureDescription; if ( description != null ) return description.CreateDigest(); HashAlgorithm hashAlgorithm = algorithmObject as HashAlgorithm; if ( hashAlgorithm != null ) return hashAlgorithm; throw DiagnosticUtility.ExceptionUtility.ThrowHelperError( new CryptographicException( SR.GetString( SR.UnsupportedCryptoAlgorithm, algorithm ) ) ); } switch ( algorithm ) { case SecurityAlgorithms.RsaSha1Signature: return CryptoHelper.NewSha1HashAlgorithm(); case SecurityAlgorithms.RsaSha256Signature: return CryptoHelper.NewSha256HashAlgorithm(); default: throw DiagnosticUtility.ExceptionUtility.ThrowHelperError( new CryptographicException( SR.GetString( SR.UnsupportedAlgorithmForCryptoOperation, algorithm, "GetHashAlgorithmForSignature" ) ) ); } } public override AsymmetricSignatureDeformatter GetSignatureDeformatter(string algorithm) { if ( String.IsNullOrEmpty( algorithm ) ) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument( algorithm, SR.GetString( SR.EmptyOrNullArgumentString, "algorithm" ) ); } object algorithmObject = CryptoHelper.GetAlgorithmFromConfig( algorithm ); if ( algorithmObject != null ) { SignatureDescription description = algorithmObject as SignatureDescription; if ( description != null ) return description.CreateDeformatter( this.rsa ); try { AsymmetricSignatureDeformatter asymmetricSignatureDeformatter = algorithmObject as AsymmetricSignatureDeformatter; if ( asymmetricSignatureDeformatter != null ) { asymmetricSignatureDeformatter.SetKey( this.rsa ); return asymmetricSignatureDeformatter; } } catch ( InvalidCastException e ) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError( new NotSupportedException( SR.GetString( SR.AlgorithmAndKeyMisMatch, algorithm ), e ) ); } throw DiagnosticUtility.ExceptionUtility.ThrowHelperError( new CryptographicException( SR.GetString( SR.UnsupportedAlgorithmForCryptoOperation, algorithm, "GetSignatureDeformatter" ) ) ); } switch (algorithm) { case SecurityAlgorithms.RsaSha1Signature: case SecurityAlgorithms.RsaSha256Signature: return new RSAPKCS1SignatureDeformatter( rsa ); default: throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CryptographicException(SR.GetString(SR.UnsupportedAlgorithmForCryptoOperation, algorithm, "GetSignatureDeformatter"))); } } public override AsymmetricSignatureFormatter GetSignatureFormatter( string algorithm ) { if ( String.IsNullOrEmpty( algorithm ) ) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument( algorithm, SR.GetString( SR.EmptyOrNullArgumentString, "algorithm" ) ); } object algorithmObject = CryptoHelper.GetAlgorithmFromConfig( algorithm ); if ( algorithmObject != null ) { SignatureDescription description = algorithmObject as SignatureDescription; if ( description != null ) return description.CreateFormatter( rsa ); try { AsymmetricSignatureFormatter asymmetricSignatureFormatter = algorithmObject as AsymmetricSignatureFormatter; if ( asymmetricSignatureFormatter != null ) { asymmetricSignatureFormatter.SetKey( rsa ); return asymmetricSignatureFormatter; } } catch ( InvalidCastException e ) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError( new NotSupportedException( SR.GetString( SR.AlgorithmAndKeyMisMatch ), e ) ); } throw DiagnosticUtility.ExceptionUtility.ThrowHelperError( new CryptographicException( SR.GetString( SR.UnsupportedAlgorithmForCryptoOperation, algorithm, "GetSignatureFormatter" ) ) ); } switch ( algorithm ) { case SecurityAlgorithms.RsaSha1Signature: case SecurityAlgorithms.RsaSha256Signature: // Ensure that we have an RSA algorithm object. return new RSAPKCS1SignatureFormatter( this.rsa ); default: throw DiagnosticUtility.ExceptionUtility.ThrowHelperError( new CryptographicException( SR.GetString( SR.UnsupportedAlgorithmForCryptoOperation, algorithm, "GetSignatureFormatter" ) ) ); } } public override bool HasPrivateKey() { if (this.privateKeyStatus == PrivateKeyStatus.AvailabilityNotDetermined) { RSACryptoServiceProvider rsaCryptoServiceProvider = this.rsa as RSACryptoServiceProvider; if (rsaCryptoServiceProvider != null) { this.privateKeyStatus = rsaCryptoServiceProvider.PublicOnly ? PrivateKeyStatus.DoesNotHavePrivateKey : PrivateKeyStatus.HasPrivateKey; } else { try { byte[] hash = new byte[20]; this.rsa.DecryptValue(hash); // imitate signing this.privateKeyStatus = PrivateKeyStatus.HasPrivateKey; } catch (CryptographicException) { this.privateKeyStatus = PrivateKeyStatus.DoesNotHavePrivateKey; } } } return this.privateKeyStatus == PrivateKeyStatus.HasPrivateKey; } public override bool IsAsymmetricAlgorithm(string algorithm) { if ( String.IsNullOrEmpty( algorithm ) ) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument( algorithm, SR.GetString( SR.EmptyOrNullArgumentString, "algorithm" ) ); } return CryptoHelper.IsAsymmetricAlgorithm(algorithm); } public override bool IsSupportedAlgorithm(string algorithm) { if ( String.IsNullOrEmpty( algorithm ) ) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument( algorithm, SR.GetString( SR.EmptyOrNullArgumentString, "algorithm" ) ); } object algorithmObject = null; try { algorithmObject = CryptoHelper.GetAlgorithmFromConfig(algorithm); } catch (InvalidOperationException) { algorithmObject = null; } if ( algorithmObject != null ) { SignatureDescription signatureDescription = algorithmObject as SignatureDescription; if ( signatureDescription != null ) return true; AsymmetricAlgorithm asymmetricAlgorithm = algorithmObject as AsymmetricAlgorithm; if ( asymmetricAlgorithm != null ) return true; return false; } switch (algorithm) { case SecurityAlgorithms.RsaV15KeyWrap: case SecurityAlgorithms.RsaOaepKeyWrap: case SecurityAlgorithms.RsaSha1Signature: case SecurityAlgorithms.RsaSha256Signature: return true; default: return false; } } public override bool IsSymmetricAlgorithm(string algorithm) { return CryptoHelper.IsSymmetricAlgorithm(algorithm); } enum PrivateKeyStatus { AvailabilityNotDetermined, HasPrivateKey, DoesNotHavePrivateKey } } } // 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
- CaseCqlBlock.cs
- UnaryNode.cs
- SqlNodeAnnotations.cs
- ReadOnlyDataSource.cs
- TcpStreams.cs
- httpapplicationstate.cs
- ProtocolsConfiguration.cs
- FieldTemplateFactory.cs
- ListDataBindEventArgs.cs
- PersistChildrenAttribute.cs
- MatchAttribute.cs
- XmlSerializationWriter.cs
- WebBrowserUriTypeConverter.cs
- RegexTree.cs
- HelpFileFileNameEditor.cs
- Knowncolors.cs
- NativeMethods.cs
- HealthMonitoringSection.cs
- DBConnection.cs
- WebPartDisplayModeCancelEventArgs.cs
- InvalidWMPVersionException.cs
- StreamResourceInfo.cs
- XmlExceptionHelper.cs
- SQLBinary.cs
- CapacityStreamGeometryContext.cs
- ClientFormsIdentity.cs
- EntityDataSourceWizardForm.cs
- CellConstantDomain.cs
- TypeDescriptionProvider.cs
- TextCompositionEventArgs.cs
- GridPatternIdentifiers.cs
- SharedPersonalizationStateInfo.cs
- RenderData.cs
- XmlWriterTraceListener.cs
- SaveFileDialog.cs
- WinCategoryAttribute.cs
- DataSysAttribute.cs
- CatalogPartChrome.cs
- ToolStripDropTargetManager.cs
- RightsManagementResourceHelper.cs
- WindowsAltTab.cs
- BeginStoryboard.cs
- InteropExecutor.cs
- DataServiceRequestException.cs
- NonSerializedAttribute.cs
- EntityDataSourceDesigner.cs
- GridViewEditEventArgs.cs
- AccessDataSource.cs
- FileInfo.cs
- TaskHelper.cs
- HMACRIPEMD160.cs
- UrlPath.cs
- Literal.cs
- DefaultIfEmptyQueryOperator.cs
- UriParserTemplates.cs
- DataPagerField.cs
- ImageDrawing.cs
- PageParser.cs
- Transform3D.cs
- mediaeventshelper.cs
- TypeInfo.cs
- Image.cs
- TextProperties.cs
- EdmComplexPropertyAttribute.cs
- SamlNameIdentifierClaimResource.cs
- SQLGuid.cs
- HttpBufferlessInputStream.cs
- DateTime.cs
- HtmlTableRow.cs
- DocumentViewerAutomationPeer.cs
- PersonalizationProviderCollection.cs
- JapaneseLunisolarCalendar.cs
- DataServiceExpressionVisitor.cs
- TypeResolver.cs
- DbParameterCollectionHelper.cs
- SpellerStatusTable.cs
- FileController.cs
- BigInt.cs
- SmiContext.cs
- SharedDp.cs
- ExtractCollection.cs
- SubpageParagraph.cs
- IdentityManager.cs
- MdiWindowListItemConverter.cs
- MailMessage.cs
- SqlRemoveConstantOrderBy.cs
- OleDbDataAdapter.cs
- UnitySerializationHolder.cs
- Geometry3D.cs
- ScrollProviderWrapper.cs
- AppDomainEvidenceFactory.cs
- CompositeFontFamily.cs
- RemotingServices.cs
- RoleManagerModule.cs
- HtmlTableCellCollection.cs
- SiteMapNodeItemEventArgs.cs
- NavigationWindowAutomationPeer.cs
- DeclarativeCatalogPartDesigner.cs
- CrossContextChannel.cs
- TileBrush.cs