//------------------------------------------------------------ // Copyright (c) Microsoft Corporation. All rights reserved. //------------------------------------------------------------ using System; using System.Diagnostics; using System.IdentityModel.Selectors; using System.IO; using System.Security.Cryptography; using System.ServiceModel.Security; using System.Text; using System.Xml; namespace System.IdentityModel.Tokens { /// /// Token handler for an encrypted type. /// public class EncryptedSecurityTokenHandler : SecurityTokenHandler { static string[] _tokenTypeIdentifiers = new string[] { null }; SecurityTokenSerializer _keyInfoSerializer; object _syncObject = new object(); /// /// Create an instance of /// public EncryptedSecurityTokenHandler() { } /// /// Indicates if the current XML element is pointing to a KeyIdentifierClause that /// can be de-serialized by this instance. /// /// An XML reader positioned at the start element. /// The reader should not be advanced. /// true if the XML reader is positioned at an EncryptedKey xml element /// as defined in section 3.5.1 of 'http://www.w3.org/TR/2002/REC-xmlenc-core-20021210'. /// The is null. public override bool CanReadKeyIdentifierClause(XmlReader reader) { if (reader == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("reader"); } // return reader.IsStartElement(XmlEncryptionConstants.Elements.EncryptedKey, XmlEncryptionConstants.Namespace); } /// /// Returns true if the reader is pointing to an EncryptedData element. /// /// The reader positioned at a security token. /// true if the reader is positioned at EncryptedData else false. /// Does not move the reader when returning false. public override bool CanReadToken(XmlReader reader) { return EncryptedDataElement.CanReadFrom(reader); } /// /// Overrides the base CanWriteToken and returns true always. /// public override bool CanWriteToken { get { return true; } } /// /// Gets or Sets a SecurityTokenSerializers that will be used to serialize and deserializer /// SecurtyKeyIdentifier of the <xenc:EncryptedData> element. /// /// Input parameter 'value' is null. public SecurityTokenSerializer KeyInfoSerializer { get { if ( _keyInfoSerializer == null ) { lock ( _syncObject ) { if ( _keyInfoSerializer == null ) { SecurityTokenHandlerCollection sthc = ( ContainingCollection != null ) ? ContainingCollection : SecurityTokenHandlerCollection.CreateDefaultSecurityTokenHandlerCollection(); _keyInfoSerializer = new SecurityTokenSerializerAdapter(sthc); } } } return _keyInfoSerializer; } set { if (value == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("value"); } _keyInfoSerializer = value; } } /// /// Reads the encrypted security token. /// /// The reader from which to read the token. /// An instance of . /// Input parameter 'reader' is null. /// One of the properties 'Configuration' or 'Configuration.ServiceTokenResolver' is null. This property is required for obtaining keys for decryption. /// A is not found inside the xml pointed to by the reader. /// The found inside the xml cannot be resolved by Configuration.ServiceTokenResolver to a . /// The is not a . /// The ContainingCollection () is unable to find a that is able to read the decrypted xml and return a . public override SecurityToken ReadToken(XmlReader reader) { if (null == reader) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("reader"); } if (this.Configuration == null) { throw DiagnosticUtility.ThrowHelperInvalidOperation(SR.GetString(SR.ID4274)); } if (this.Configuration.ServiceTokenResolver == null) { throw DiagnosticUtility.ThrowHelperInvalidOperation(SR.GetString(SR.ID4276)); } // // Read the encrypted data element // EncryptedDataElement encryptedData = new EncryptedDataElement(KeyInfoSerializer); encryptedData.ReadXml(XmlDictionaryReader.CreateDictionaryReader(reader)); // // All the clauses in a keyinfo must identify the same key, so we // can try each clause in turn and stop when one resolves. // SecurityKey decryptionKey = null; foreach (SecurityKeyIdentifierClause clause in encryptedData.KeyIdentifier) { this.Configuration.ServiceTokenResolver.TryResolveSecurityKey(clause, out decryptionKey); if (null != decryptionKey) { break; } } // // Try to use the SKI to create the key instead. // if (decryptionKey == null && encryptedData.KeyIdentifier.CanCreateKey) { decryptionKey = encryptedData.KeyIdentifier.CreateKey(); } // // Fail if none of the clauses resolved or ski itself cannot create key. // if (null == decryptionKey) { EncryptedKeyIdentifierClause encryptedKeyClause; if (encryptedData.KeyIdentifier.TryFind(out encryptedKeyClause)) { // // System.IdentityModel.Tokens.EncryptedKeyIdentifierClause.ToString() does not print out // very good information except the cipher data in this case. We have worked around that // by using the token serializer to serialize the key identifier clause again. // throw DiagnosticUtility.ExceptionUtility.ThrowHelperError( new EncryptedTokenDecryptionFailedException( SR.GetString(SR.ID4036, XmlUtil.SerializeSecurityKeyIdentifier(encryptedData.KeyIdentifier, base.ContainingCollection.KeyInfoSerializer)))); } else { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError( new EncryptedTokenDecryptionFailedException(SR.GetString(SR.ID4036, encryptedData.KeyIdentifier.ToString()))); } } // // Need a symmetric key // SymmetricSecurityKey symmetricKey = decryptionKey as SymmetricSecurityKey; if (null == symmetricKey) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError( new SecurityTokenException(SR.GetString(SR.ID4023))); } // // Do the actual decryption // byte[] plainText; using (SymmetricAlgorithm decrypter = symmetricKey.GetSymmetricAlgorithm(encryptedData.Algorithm)) { plainText = encryptedData.Decrypt(decrypter); } DebugEncryptedTokenClearText(plainText, Encoding.UTF8); // // Read and return the plaintext token // using (XmlReader innerTokenReader = XmlDictionaryReader.CreateTextReader(plainText, XmlDictionaryReaderQuotas.Max)) { if (this.ContainingCollection != null && this.ContainingCollection.CanReadToken(innerTokenReader)) { return this.ContainingCollection.ReadToken(innerTokenReader); } throw DiagnosticUtility.ThrowHelperInvalidOperation(SR.GetString(SR.ID4014, innerTokenReader.LocalName, innerTokenReader.NamespaceURI)); } } /// /// Reads an EncryptedKeyIdentifierClause from a XML stream. /// /// An XML reader positioned at an EncryptedKey element as defined in 'http://www.w3.org/TR/2002/REC-xmlenc-core-20021210' . /// SecurityKeyIdentifierClause instance of type EncryptedKeyIdentifierClause. /// The is null. /// If the is not positioned at an EncryptedKey element. public override SecurityKeyIdentifierClause ReadKeyIdentifierClause(XmlReader reader) { if (reader == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("reader"); } // if (reader.IsStartElement(XmlEncryptionConstants.Elements.EncryptedKey, XmlEncryptionConstants.Namespace)) { EncryptedKeyElement encryptedKey = new EncryptedKeyElement(KeyInfoSerializer); encryptedKey.ReadXml(XmlDictionaryReader.CreateDictionaryReader(reader)); return new EncryptedKeyIdentifierClause(encryptedKey.CipherData.CipherValue, encryptedKey.Algorithm, encryptedKey.KeyIdentifier); } throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.ID3275, reader.Name, reader.NamespaceURI))); } [Conditional("DEBUG")] static void DebugEncryptedTokenClearText(byte[] bytes, Encoding encoding) { string text = encoding.GetString(bytes); Debug.WriteLine(text.Substring(0, 40)); } /// /// Gets the System.Type of the token that this SecurityTokenHandler handles. /// Returns typeof by default. /// public override Type TokenType { get { return typeof(EncryptedSecurityToken); } } /// /// By default returns an array with a single null string as there isn't any specific TokenType identifier that is /// associated with a . /// public override string[] GetTokenTypeIdentifiers() { return _tokenTypeIdentifiers; } /// /// Writes a using the xmlWriter. /// /// The XmlWriter to which the encrypted token is written. /// The which must be an instance of . /// The input prameter 'writer' is null. /// The input prameter 'token' is null. /// The is not an instance of . /// The property 'Configuration' is null. This property is required for obtaining keys for encryption. /// The ContaingCollection was unable to find a that is able to write /// the returned by 'EncryptedSecurityToken.Token'. /// The property 'EncryptinCredentials.SecurityKey is not a public override void WriteToken(XmlWriter writer, SecurityToken token) { if (null == writer) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("writer"); } if (null == token) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("token"); } EncryptedSecurityToken encryptedToken = token as EncryptedSecurityToken; if (null == encryptedToken) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument("token", SR.GetString(SR.ID4024)); } if (this.ContainingCollection == null) { throw DiagnosticUtility.ThrowHelperInvalidOperation(SR.GetString(SR.ID4279)); } // // This implementation simply wraps the token in xenc:EncryptedData // EncryptedDataElement encryptedData = new EncryptedDataElement(KeyInfoSerializer); using (MemoryStream plaintextStream = new MemoryStream()) { // // Buffer the plaintext // using (XmlDictionaryWriter plaintextWriter = XmlDictionaryWriter.CreateTextWriter(plaintextStream, Encoding.UTF8, false)) { SecurityTokenHandler securityTokenHandler = this.ContainingCollection[encryptedToken.Token.GetType()]; if (securityTokenHandler != null) { securityTokenHandler.WriteToken(plaintextWriter, encryptedToken.Token); } else { throw DiagnosticUtility.ThrowHelperInvalidOperation(SR.GetString(SR.ID4224, encryptedToken.Token.GetType())); } } // // Set up the EncryptedData element // EncryptingCredentials encryptingCredentials = encryptedToken.EncryptingCredentials; encryptedData.Type = XmlEncryptionConstants.EncryptedDataTypes.Element; encryptedData.KeyIdentifier = encryptingCredentials.SecurityKeyIdentifier; encryptedData.Algorithm = encryptingCredentials.Algorithm; // // Get the encryption key, which must be symmetric // SymmetricSecurityKey encryptingKey = encryptingCredentials.SecurityKey as SymmetricSecurityKey; if (encryptingKey == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SecurityTokenException(SR.GetString(SR.ID3064))); } // // Do the actual encryption // using (SymmetricAlgorithm symmetricAlgorithm = encryptingKey.GetSymmetricAlgorithm(encryptingCredentials.Algorithm)) { byte[] plainTextBytes = plaintextStream.GetBuffer(); DebugEncryptedTokenClearText(plainTextBytes, Encoding.UTF8); encryptedData.Encrypt(symmetricAlgorithm, plainTextBytes, 0, (int)plaintextStream.Length); } } // // Write the EncryptedData element // encryptedData.WriteXml(writer, KeyInfoSerializer); } } }