| | | 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.Security.Cryptography; |
| | | 5 | | using System.Text; |
| | | 6 | | using System; |
| | | 7 | | using Microsoft.AspNetCore.DataProtection; |
| | | 8 | | |
| | | 9 | | namespace CoreWCF.IdentityModel |
| | | 10 | | { |
| | | 11 | | /// <summary> |
| | | 12 | | /// Provides cookie integrity and confidentiality using <see cref="ProtectedData"/>. |
| | | 13 | | /// </summary> |
| | | 14 | | /// <remarks> |
| | | 15 | | /// Due to the nature of <see cref="ProtectedData"/>, cookies |
| | | 16 | | /// which use this tranform can only be read by the same machine |
| | | 17 | | /// which wrote them. As such, this transform is not appropriate |
| | | 18 | | /// for use in applications that run on a web server farm. |
| | | 19 | | /// </remarks> |
| | | 20 | | public sealed class ProtectedDataCookieTransform : CookieTransform |
| | | 21 | | { |
| | | 22 | | private const string EntropyString = "CoreWCF.IdentityModel.ProtectedDataCookieTransform"; |
| | | 23 | | private IDataProtector _protector; |
| | | 24 | | |
| | 2 | 25 | | public ProtectedDataCookieTransform(IDataProtectionProvider provider) |
| | | 26 | | { |
| | 2 | 27 | | _protector = provider.CreateProtector(EntropyString); |
| | 2 | 28 | | } |
| | | 29 | | /// <summary> |
| | | 30 | | /// Verifies data protection. |
| | | 31 | | /// </summary> |
| | | 32 | | /// <param name=nameof(encoded)>Data previously returned from <see cref="Encode"/></param> |
| | | 33 | | /// <returns>The originally protected data.</returns> |
| | | 34 | | /// <exception cref="ArgumentNullException">The argument 'encoded' is null.</exception> |
| | | 35 | | /// <exception cref="ArgumentException">The argument 'encoded' contains zero bytes.</exception> |
| | | 36 | | public override byte[] Decode(byte[] encoded) |
| | | 37 | | { |
| | 0 | 38 | | if (null == encoded) |
| | | 39 | | { |
| | 0 | 40 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(encoded)); |
| | | 41 | | } |
| | | 42 | | |
| | 0 | 43 | | if (0 == encoded.Length) |
| | | 44 | | { |
| | 0 | 45 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(nameof(encoded), SR.Format(SR.ID6045)); |
| | | 46 | | } |
| | | 47 | | |
| | | 48 | | // CurrentUser is used here, and this has been tested as |
| | | 49 | | // NetworkService. Using CurrentMachine allows anyone on |
| | | 50 | | // the machine to decrypt the data, which isn't what we |
| | | 51 | | // want. |
| | | 52 | | byte[] decoded; |
| | | 53 | | try |
| | | 54 | | { |
| | 0 | 55 | | decoded = _protector.Unprotect(encoded); |
| | 0 | 56 | | } |
| | 0 | 57 | | catch (CryptographicException e) |
| | | 58 | | { |
| | 0 | 59 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR.ID1 |
| | | 60 | | } |
| | | 61 | | |
| | 0 | 62 | | return decoded; |
| | | 63 | | } |
| | | 64 | | |
| | | 65 | | /// <summary> |
| | | 66 | | /// Protects data. |
| | | 67 | | /// </summary> |
| | | 68 | | /// <param name=nameof(value)>Data to be protected.</param> |
| | | 69 | | /// <returns>Protected data.</returns> |
| | | 70 | | /// <exception cref="ArgumentNullException">The argument 'value' is null.</exception> |
| | | 71 | | /// <exception cref="ArgumentException">The argument 'value' contains zero bytes.</exception> |
| | | 72 | | public override byte[] Encode(byte[] value) |
| | | 73 | | { |
| | 0 | 74 | | if (null == value) |
| | | 75 | | { |
| | 0 | 76 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(value)); |
| | | 77 | | } |
| | | 78 | | |
| | 0 | 79 | | if (0 == value.Length) |
| | | 80 | | { |
| | 0 | 81 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(nameof(value), SR.Format(SR.ID6044)); |
| | | 82 | | } |
| | | 83 | | |
| | | 84 | | // See note in Decode about the DataProtectionScope. |
| | | 85 | | byte[] encoded; |
| | | 86 | | try |
| | | 87 | | { |
| | 0 | 88 | | encoded = _protector.Protect(value); |
| | 0 | 89 | | } |
| | 0 | 90 | | catch (CryptographicException e) |
| | | 91 | | { |
| | 0 | 92 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR.ID1 |
| | | 93 | | } |
| | | 94 | | |
| | 0 | 95 | | return encoded; |
| | | 96 | | } |
| | | 97 | | } |
| | | 98 | | } |