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