EncryptedData.cs 3.7 KB

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