Code:
/ WCF / WCF / 3.5.30729.1 / untmp / Orcas / SP / ndp / cdf / src / WCF / IdentityModel / System / IdentityModel / Tokens / RsaSecurityKey.cs / 1 / 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: 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: 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) { switch (algorithm) { case SecurityAlgorithms.RsaSha1Signature: return CryptoHelper.NewSha1HashAlgorithm(); default: throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CryptographicException(SR.GetString(SR.UnsupportedAlgorithmForCryptoOperation, algorithm, "GetHashAlgorithmForSignature"))); } } public override AsymmetricSignatureDeformatter GetSignatureDeformatter(string algorithm) { switch (algorithm) { case SecurityAlgorithms.RsaSha1Signature: return new RSAPKCS1SignatureDeformatter(rsa); default: throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CryptographicException(SR.GetString(SR.UnsupportedAlgorithmForCryptoOperation, algorithm, "GetSignatureDeformatter"))); } } public override AsymmetricSignatureFormatter GetSignatureFormatter(string algorithm) { switch (algorithm) { case SecurityAlgorithms.RsaSha1Signature: return new RSAPKCS1SignatureFormatter(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) { return CryptoHelper.IsAsymmetricAlgorithm(algorithm); } public override bool IsSupportedAlgorithm(string algorithm) { switch (algorithm) { case SecurityAlgorithms.RsaV15KeyWrap: case SecurityAlgorithms.RsaOaepKeyWrap: case SecurityAlgorithms.RsaSha1Signature: 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. // Copyright (c) Microsoft Corporation. All rights reserved.
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- ExportOptions.cs
- DataSourceCollectionBase.cs
- DbParameterHelper.cs
- TextUtf8RawTextWriter.cs
- LicenseProviderAttribute.cs
- ModelVisual3D.cs
- EventSinkActivity.cs
- StateWorkerRequest.cs
- TextComposition.cs
- PolicyLevel.cs
- XmlDictionaryReaderQuotas.cs
- RtfNavigator.cs
- CompilationSection.cs
- PerformanceCounterCategory.cs
- TouchPoint.cs
- OleDbErrorCollection.cs
- ValueSerializer.cs
- FieldNameLookup.cs
- PropertyGroupDescription.cs
- VisemeEventArgs.cs
- TextBox.cs
- ForceCopyBuildProvider.cs
- EventLogPermissionEntry.cs
- EventLogEntry.cs
- PageAsyncTaskManager.cs
- OrCondition.cs
- Shared.cs
- FloatSumAggregationOperator.cs
- UInt32Storage.cs
- LinkedDataMemberFieldEditor.cs
- AlignmentYValidation.cs
- CollectionBase.cs
- __ConsoleStream.cs
- HtmlTable.cs
- XmlSchemaSimpleContent.cs
- MemberInitExpression.cs
- UnsafeNativeMethods.cs
- MailAddress.cs
- RemotingConfigParser.cs
- EncryptedXml.cs
- SnapLine.cs
- LocalizedNameDescriptionPair.cs
- WebPartEditorOkVerb.cs
- DataGridViewColumnCollectionDialog.cs
- IdentityNotMappedException.cs
- XmlNavigatorStack.cs
- Base64WriteStateInfo.cs
- Reference.cs
- EditCommandColumn.cs
- _TimerThread.cs
- DataSourceCache.cs
- TextEditorTyping.cs
- _SSPISessionCache.cs
- URLMembershipCondition.cs
- SQLUtility.cs
- SplitterEvent.cs
- DataGridCellInfo.cs
- SkewTransform.cs
- ProfileSection.cs
- WebPartDisplayMode.cs
- SqlBuilder.cs
- EventLogPermissionEntry.cs
- HTTPRemotingHandler.cs
- BindingExpressionUncommonField.cs
- ThousandthOfEmRealPoints.cs
- SiteMapNodeItem.cs
- Subtree.cs
- GrammarBuilderPhrase.cs
- SqlDataSourceQueryEditorForm.cs
- Model3DCollection.cs
- EnumerableCollectionView.cs
- GridItem.cs
- InternalConfigEventArgs.cs
- validationstate.cs
- HttpVersion.cs
- MachineKeyConverter.cs
- PageAsyncTask.cs
- TextEncodedRawTextWriter.cs
- ByteStack.cs
- ProviderException.cs
- Literal.cs
- SecurityChannelFactory.cs
- DesignerValidationSummaryAdapter.cs
- HtmlHead.cs
- OraclePermissionAttribute.cs
- TypeContext.cs
- ProxyElement.cs
- HtmlControlPersistable.cs
- StructuredCompositeActivityDesigner.cs
- ProcessActivityTreeOptions.cs
- CodeValidator.cs
- SafeViewOfFileHandle.cs
- HtmlTernaryTree.cs
- TagNameToTypeMapper.cs
- Vector3D.cs
- FormViewDeletedEventArgs.cs
- ObjectSpanRewriter.cs
- DynamicQueryableWrapper.cs
- StateMachine.cs
- XmlSchemaFacet.cs