DataProtectionSecurityStateEncoder.cs 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. //------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------
  4. namespace System.ServiceModel.Security
  5. {
  6. using System.Text;
  7. using System.Security.Cryptography;
  8. public class DataProtectionSecurityStateEncoder : SecurityStateEncoder
  9. {
  10. byte[] entropy;
  11. bool useCurrentUserProtectionScope;
  12. public DataProtectionSecurityStateEncoder()
  13. : this(true)
  14. {
  15. // empty
  16. }
  17. public DataProtectionSecurityStateEncoder(bool useCurrentUserProtectionScope)
  18. : this(useCurrentUserProtectionScope, null)
  19. { }
  20. public DataProtectionSecurityStateEncoder(bool useCurrentUserProtectionScope, byte[] entropy)
  21. {
  22. this.useCurrentUserProtectionScope = useCurrentUserProtectionScope;
  23. if (entropy == null)
  24. {
  25. this.entropy = null;
  26. }
  27. else
  28. {
  29. this.entropy = DiagnosticUtility.Utility.AllocateByteArray(entropy.Length);
  30. Buffer.BlockCopy(entropy, 0, this.entropy, 0, entropy.Length);
  31. }
  32. }
  33. public bool UseCurrentUserProtectionScope
  34. {
  35. get
  36. {
  37. return this.useCurrentUserProtectionScope;
  38. }
  39. }
  40. public byte[] GetEntropy()
  41. {
  42. byte[] result = null;
  43. if (this.entropy != null)
  44. {
  45. result = DiagnosticUtility.Utility.AllocateByteArray(this.entropy.Length);
  46. Buffer.BlockCopy(this.entropy, 0, result, 0, this.entropy.Length);
  47. }
  48. return result;
  49. }
  50. public override string ToString()
  51. {
  52. StringBuilder result = new StringBuilder();
  53. result.Append(this.GetType().ToString());
  54. result.AppendFormat("{0} UseCurrentUserProtectionScope={1}", Environment.NewLine, this.useCurrentUserProtectionScope);
  55. result.AppendFormat("{0} Entropy Length={1}", Environment.NewLine, (this.entropy == null) ? 0 : this.entropy.Length);
  56. return result.ToString();
  57. }
  58. protected internal override byte[] DecodeSecurityState( byte[] data )
  59. {
  60. try
  61. {
  62. return ProtectedData.Unprotect(data, this.entropy, (this.useCurrentUserProtectionScope) ? DataProtectionScope.CurrentUser : DataProtectionScope.LocalMachine);
  63. }
  64. catch (CryptographicException exception)
  65. {
  66. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CryptographicException(SR.GetString(SR.SecurityStateEncoderDecodingFailure), exception));
  67. }
  68. }
  69. protected internal override byte[] EncodeSecurityState( byte[] data )
  70. {
  71. try
  72. {
  73. return ProtectedData.Protect(data, this.entropy, (this.useCurrentUserProtectionScope) ? DataProtectionScope.CurrentUser : DataProtectionScope.LocalMachine);
  74. }
  75. catch (CryptographicException exception)
  76. {
  77. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CryptographicException(SR.GetString(SR.SecurityStateEncoderEncodingFailure), exception));
  78. }
  79. }
  80. }
  81. }