1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
|
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------
namespace System.IdentityModel
{
using System;
using System.IdentityModel.Selectors;
using System.IdentityModel.Tokens;
using System.Security.Cryptography;
using System.Xml;
/// <summary>
/// This class implements a deserialization for: EncryptedData as defined in section 3.4 of http://www.w3.org/TR/2002/REC-xmlenc-core-2002120
/// </summary>
internal class EncryptedDataElement : EncryptedTypeElement
{
public static bool CanReadFrom( XmlReader reader )
{
return reader != null && reader.IsStartElement(
XmlEncryptionConstants.Elements.EncryptedData,
XmlEncryptionConstants.Namespace );
}
public EncryptedDataElement()
: this( null )
{
}
public EncryptedDataElement( SecurityTokenSerializer tokenSerializer )
: base( tokenSerializer )
{
KeyIdentifier = new SecurityKeyIdentifier( new EmptySecurityKeyIdentifierClause() );
}
/// <summary>
/// Decrypts the data
/// </summary>
/// <param name="algorithm"></param>
/// <returns></returns>
/// <exception cref="ArgumentNullException">When algorithm is null</exception>
/// <exception cref="InvalidOperationException">When no cipher data has been read</exception>
public byte[] Decrypt( SymmetricAlgorithm algorithm )
{
if ( algorithm == null )
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull( "algorithm" );
}
if ( CipherData == null || CipherData.CipherValue == null )
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError( new InvalidOperationException( SR.GetString( SR.ID6000 ) ) );
}
byte[] cipherText = CipherData.CipherValue;
return ExtractIVAndDecrypt( algorithm, cipherText, 0, cipherText.Length );
}
public void Encrypt( SymmetricAlgorithm algorithm, byte[] buffer, int offset, int length )
{
byte[] iv;
byte[] cipherText;
GenerateIVAndEncrypt( algorithm, buffer, offset, length, out iv, out cipherText );
CipherData.SetCipherValueFragments( iv, cipherText );
}
static byte[] ExtractIVAndDecrypt( SymmetricAlgorithm algorithm, byte[] cipherText, int offset, int count )
{
byte[] iv = new byte[algorithm.BlockSize / 8];
//
// Make sure cipherText has enough bytes after the offset, for Buffer.BlockCopy to copy.
//
if ( cipherText.Length - offset < iv.Length )
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError( new InvalidOperationException( SR.GetString( SR.ID6019, cipherText.Length - offset, iv.Length ) ) );
}
Buffer.BlockCopy( cipherText, offset, iv, 0, iv.Length );
algorithm.Padding = PaddingMode.ISO10126;
algorithm.Mode = CipherMode.CBC;
ICryptoTransform decrTransform = null;
byte[] plainText = null;
try
{
decrTransform = algorithm.CreateDecryptor( algorithm.Key, iv );
plainText = decrTransform.TransformFinalBlock( cipherText, offset + iv.Length, count - iv.Length );
}
finally
{
if ( decrTransform != null )
{
decrTransform.Dispose();
}
}
return plainText;
}
static void GenerateIVAndEncrypt( SymmetricAlgorithm algorithm, byte[] plainText, int offset, int length, out byte[] iv, out byte[] cipherText )
{
RandomNumberGenerator random = CryptoHelper.RandomNumberGenerator;
int ivSize = algorithm.BlockSize / 8;
iv = new byte[ivSize];
random.GetBytes( iv );
algorithm.Padding = PaddingMode.PKCS7;
algorithm.Mode = CipherMode.CBC;
ICryptoTransform encrTransform = algorithm.CreateEncryptor( algorithm.Key, iv );
cipherText = encrTransform.TransformFinalBlock( plainText, offset, length );
encrTransform.Dispose();
}
public override void ReadExtensions( XmlDictionaryReader reader )
{
// nothing to do here
}
/// <summary>
/// Reads an EncryptedData element
/// </summary>
/// <param name="reader"></param>
/// <exception cref="ArgumentNullException">When reader is null</exception>
/// <exception cref="ArgumentNullException">When securityTokenSerializer is null</exception>
public override void ReadXml( XmlDictionaryReader reader )
{
if ( reader == null )
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull( "reader" );
}
reader.MoveToContent();
if ( !reader.IsStartElement( XmlEncryptionConstants.Elements.EncryptedData, XmlEncryptionConstants.Namespace ) )
{
throw DiagnosticUtility.ThrowHelperXml( reader, SR.GetString( SR.ID4193 ) );
}
// <EncryptedData> extends <EncryptedType>
// base will read the start element and the end element.
base.ReadXml( reader );
}
/// <summary>
/// Writes the EncryptedData element
/// </summary>
/// <param name="writer"></param>
/// <param name="securityTokenSerializer"></param>
/// <exception cref="ArgumentNullException">When securityTokenSerializer is null</exception>
/// <exception cref="InvalidOperationException">When KeyIdentifier is null</exception>
public virtual void WriteXml( XmlWriter writer, SecurityTokenSerializer securityTokenSerializer )
{
if ( writer == null )
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull( "writer" );
}
if ( securityTokenSerializer == null )
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull( "securityTokenSerializer" );
}
if ( KeyIdentifier == null )
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError( new InvalidOperationException( SR.GetString( SR.ID6001 ) ) );
}
// <EncryptedData>
writer.WriteStartElement( XmlEncryptionConstants.Prefix, XmlEncryptionConstants.Elements.EncryptedData, XmlEncryptionConstants.Namespace );
if ( !string.IsNullOrEmpty( Id ) )
{
writer.WriteAttributeString( XmlEncryptionConstants.Attributes.Id, null, Id );
}
if ( !string.IsNullOrEmpty( Type ) )
{
writer.WriteAttributeString( XmlEncryptionConstants.Attributes.Type, null, Type );
}
if ( EncryptionMethod != null )
{
EncryptionMethod.WriteXml( writer );
}
if ( KeyIdentifier != null )
{
securityTokenSerializer.WriteKeyIdentifier( XmlDictionaryWriter.CreateDictionaryWriter( writer ), KeyIdentifier );
}
CipherData.WriteXml( writer );
// <EncryptedData>
writer.WriteEndElement();
}
}
}
|