| | | 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 System.Security.Cryptography; |
| | | 6 | | using System.Xml; |
| | | 7 | | |
| | | 8 | | namespace CoreWCF.Security |
| | | 9 | | { |
| | | 10 | | internal class EncryptedData : EncryptedType |
| | | 11 | | { |
| | 0 | 12 | | internal static readonly XmlDictionaryString ElementName = XD.XmlEncryptionDictionary.EncryptedData; |
| | 0 | 13 | | internal static readonly string ElementType = XmlEncryptionStrings.ElementType; |
| | 0 | 14 | | internal static readonly string ContentType = XmlEncryptionStrings.ContentType; |
| | | 15 | | private SymmetricAlgorithm _algorithm; |
| | | 16 | | private byte[] _decryptedBuffer; |
| | | 17 | | private ArraySegment<byte> _buffer; |
| | | 18 | | private byte[] _iv; |
| | | 19 | | private byte[] _cipherText; |
| | | 20 | | |
| | | 21 | | protected override XmlDictionaryString OpeningElementName |
| | | 22 | | { |
| | 0 | 23 | | get { return ElementName; } |
| | | 24 | | } |
| | | 25 | | |
| | | 26 | | private void EnsureDecryptionSet() |
| | | 27 | | { |
| | 0 | 28 | | if (State == EncryptionState.DecryptionSetup) |
| | | 29 | | { |
| | 0 | 30 | | SetPlainText(); |
| | | 31 | | } |
| | 0 | 32 | | else if (State != EncryptionState.Decrypted) |
| | | 33 | | { |
| | 0 | 34 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new MessageSecurityException(SR.BadEncryptionS |
| | | 35 | | } |
| | 0 | 36 | | } |
| | | 37 | | |
| | | 38 | | protected override void ForceEncryption() |
| | | 39 | | { |
| | 0 | 40 | | CryptoHelper.GenerateIVAndEncrypt(_algorithm, _buffer, out _iv, out _cipherText); |
| | 0 | 41 | | State = EncryptionState.Encrypted; |
| | 0 | 42 | | _buffer = new ArraySegment<byte>(CryptoHelper.EmptyBuffer); |
| | 0 | 43 | | } |
| | | 44 | | |
| | | 45 | | public byte[] GetDecryptedBuffer() |
| | | 46 | | { |
| | 0 | 47 | | EnsureDecryptionSet(); |
| | 0 | 48 | | return _decryptedBuffer; |
| | | 49 | | } |
| | | 50 | | |
| | | 51 | | protected override void ReadCipherData(XmlDictionaryReader reader) |
| | | 52 | | { |
| | 0 | 53 | | _cipherText = reader.ReadContentAsBase64(); |
| | 0 | 54 | | } |
| | | 55 | | |
| | | 56 | | protected override void ReadCipherData(XmlDictionaryReader reader, long maxBufferSize) |
| | | 57 | | { |
| | 0 | 58 | | _cipherText = SecurityUtils.ReadContentAsBase64(reader, maxBufferSize); |
| | 0 | 59 | | } |
| | | 60 | | |
| | | 61 | | private void SetPlainText() |
| | | 62 | | { |
| | 0 | 63 | | _decryptedBuffer = CryptoHelper.ExtractIVAndDecrypt(_algorithm, _cipherText, 0, _cipherText.Length); |
| | 0 | 64 | | State = EncryptionState.Decrypted; |
| | 0 | 65 | | } |
| | | 66 | | |
| | | 67 | | public void SetUpDecryption(SymmetricAlgorithm algorithm) |
| | | 68 | | { |
| | 0 | 69 | | if (State != EncryptionState.Read) |
| | | 70 | | { |
| | 0 | 71 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new MessageSecurityException(SR.BadEncryptionS |
| | | 72 | | } |
| | | 73 | | |
| | 0 | 74 | | _algorithm = algorithm ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(algorithm) |
| | 0 | 75 | | State = EncryptionState.DecryptionSetup; |
| | 0 | 76 | | } |
| | | 77 | | |
| | | 78 | | public void SetUpEncryption(SymmetricAlgorithm algorithm, ArraySegment<byte> buffer) |
| | | 79 | | { |
| | 0 | 80 | | if (State != EncryptionState.New) |
| | | 81 | | { |
| | 0 | 82 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new MessageSecurityException(SR.BadEncryptionS |
| | | 83 | | } |
| | | 84 | | |
| | 0 | 85 | | _algorithm = algorithm ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(algorithm) |
| | 0 | 86 | | _buffer = buffer; |
| | 0 | 87 | | State = EncryptionState.EncryptionSetup; |
| | 0 | 88 | | } |
| | | 89 | | |
| | | 90 | | protected override void WriteCipherData(XmlDictionaryWriter writer) |
| | | 91 | | { |
| | 0 | 92 | | writer.WriteBase64(_iv, 0, _iv.Length); |
| | 0 | 93 | | writer.WriteBase64(_cipherText, 0, _cipherText.Length); |
| | 0 | 94 | | } |
| | | 95 | | } |
| | | 96 | | } |