| | | 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.Text; |
| | | 7 | | using CoreWCF.Runtime; |
| | | 8 | | |
| | | 9 | | namespace CoreWCF.Security |
| | | 10 | | { |
| | | 11 | | public class DataProtectionSecurityStateEncoder : SecurityStateEncoder |
| | | 12 | | { |
| | | 13 | | private readonly byte[] _entropy; |
| | | 14 | | |
| | 61 | 15 | | public DataProtectionSecurityStateEncoder() : this(true) |
| | | 16 | | { |
| | | 17 | | // empty |
| | 61 | 18 | | } |
| | | 19 | | |
| | 61 | 20 | | public DataProtectionSecurityStateEncoder(bool useCurrentUserProtectionScope) : this(useCurrentUserProtectionSco |
| | 61 | 21 | | { } |
| | | 22 | | |
| | 61 | 23 | | public DataProtectionSecurityStateEncoder(bool useCurrentUserProtectionScope, byte[] entropy) |
| | | 24 | | { |
| | 61 | 25 | | UseCurrentUserProtectionScope = useCurrentUserProtectionScope; |
| | 61 | 26 | | if (entropy == null) |
| | | 27 | | { |
| | 61 | 28 | | _entropy = null; |
| | | 29 | | } |
| | | 30 | | else |
| | | 31 | | { |
| | 0 | 32 | | _entropy = Fx.AllocateByteArray(entropy.Length); |
| | 0 | 33 | | Buffer.BlockCopy(entropy, 0, _entropy, 0, entropy.Length); |
| | | 34 | | } |
| | 0 | 35 | | } |
| | | 36 | | |
| | 0 | 37 | | public bool UseCurrentUserProtectionScope { get; } |
| | | 38 | | |
| | | 39 | | public byte[] GetEntropy() |
| | | 40 | | { |
| | 0 | 41 | | byte[] result = null; |
| | 0 | 42 | | if (_entropy != null) |
| | | 43 | | { |
| | 0 | 44 | | result = Fx.AllocateByteArray(_entropy.Length); |
| | 0 | 45 | | Buffer.BlockCopy(_entropy, 0, result, 0, _entropy.Length); |
| | | 46 | | } |
| | 0 | 47 | | return result; |
| | | 48 | | } |
| | | 49 | | |
| | | 50 | | public override string ToString() |
| | | 51 | | { |
| | 0 | 52 | | StringBuilder result = new StringBuilder(); |
| | 0 | 53 | | result.Append(GetType().ToString()); |
| | 0 | 54 | | result.AppendFormat("{0} UseCurrentUserProtectionScope={1}", Environment.NewLine, UseCurrentUserProtectionS |
| | 0 | 55 | | result.AppendFormat("{0} Entropy Length={1}", Environment.NewLine, (_entropy == null) ? 0 : _entropy.Length |
| | 0 | 56 | | return result.ToString(); |
| | | 57 | | } |
| | | 58 | | |
| | | 59 | | protected internal override byte[] DecodeSecurityState(byte[] data) |
| | | 60 | | { |
| | | 61 | | try |
| | | 62 | | { |
| | 0 | 63 | | return ProtectedData.Unprotect(data, _entropy, (UseCurrentUserProtectionScope) ? DataProtectionScope.Cur |
| | | 64 | | } |
| | 0 | 65 | | catch (CryptographicException exception) |
| | | 66 | | { |
| | 0 | 67 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CryptographicException(SR.SecurityStateEnc |
| | | 68 | | } |
| | 0 | 69 | | } |
| | | 70 | | |
| | | 71 | | protected internal override byte[] EncodeSecurityState(byte[] data) |
| | | 72 | | { |
| | | 73 | | try |
| | | 74 | | { |
| | 0 | 75 | | return ProtectedData.Protect(data, _entropy, (UseCurrentUserProtectionScope) ? DataProtectionScope.Curre |
| | | 76 | | } |
| | 0 | 77 | | catch (CryptographicException exception) |
| | | 78 | | { |
| | 0 | 79 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CryptographicException(SR.SecurityStateEnc |
| | | 80 | | } |
| | 0 | 81 | | } |
| | | 82 | | } |
| | | 83 | | } |