| | | 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.IO; |
| | | 5 | | using System.IO.Compression; |
| | | 6 | | using CoreWCF.IdentityModel.Tokens; |
| | | 7 | | |
| | | 8 | | namespace CoreWCF.IdentityModel |
| | | 9 | | { |
| | | 10 | | /// <summary> |
| | | 11 | | /// Provides cookie compression using <see cref="DeflateStream"/>. |
| | | 12 | | /// </summary> |
| | | 13 | | public sealed class DeflateCookieTransform : CookieTransform |
| | | 14 | | { |
| | 6 | 15 | | private int _maxDecompressedSize = 1024 * 1024; // Default maximum of 1MB |
| | | 16 | | |
| | | 17 | | /// <summary> |
| | | 18 | | /// Creates a new instance of <see cref="DeflateCookieTransform"/>. |
| | | 19 | | /// </summary> |
| | 6 | 20 | | public DeflateCookieTransform() |
| | | 21 | | { |
| | 6 | 22 | | } |
| | | 23 | | |
| | | 24 | | /// <summary> |
| | | 25 | | /// Gets or sets the maximum size (in bytes) of a decompressed cookie. |
| | | 26 | | /// </summary> |
| | | 27 | | public int MaxDecompressedSize |
| | | 28 | | { |
| | 0 | 29 | | get { return _maxDecompressedSize; } |
| | 0 | 30 | | set { _maxDecompressedSize = value; } |
| | | 31 | | } |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// Inflates data. |
| | | 35 | | /// </summary> |
| | | 36 | | /// <param name="encoded">Data previously returned from <see cref="Encode"/></param> |
| | | 37 | | /// <returns>Decoded data.</returns> |
| | | 38 | | /// <exception cref="ArgumentNullException">The argument 'value' is null.</exception> |
| | | 39 | | /// <exception cref="ArgumentException">The argument 'value' contains zero bytes.</exception> |
| | | 40 | | /// <exception cref="SecurityTokenException">The decompressed length is larger than MaxDecompressedSize.</except |
| | | 41 | | public override byte[] Decode( byte[] encoded ) |
| | | 42 | | { |
| | 0 | 43 | | if ( null == encoded ) |
| | | 44 | | { |
| | 0 | 45 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(encoded)); |
| | | 46 | | } |
| | | 47 | | |
| | 0 | 48 | | if ( 0 == encoded.Length ) |
| | | 49 | | { |
| | 0 | 50 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument( nameof(encoded), SR.Format( SR.ID6045 ) ); |
| | | 51 | | } |
| | | 52 | | |
| | 0 | 53 | | MemoryStream compressedStream = new MemoryStream( encoded ); |
| | 0 | 54 | | using ( DeflateStream deflateStream = new DeflateStream( compressedStream, CompressionMode.Decompress, false |
| | | 55 | | { |
| | 0 | 56 | | using ( MemoryStream decompressedStream = new MemoryStream() ) |
| | | 57 | | { |
| | 0 | 58 | | byte[] buffer = new byte[1024]; |
| | | 59 | | int bytesRead; |
| | | 60 | | |
| | | 61 | | do |
| | | 62 | | { |
| | 0 | 63 | | bytesRead = deflateStream.Read( buffer, 0, buffer.Length ); |
| | 0 | 64 | | decompressedStream.Write( buffer, 0, bytesRead ); |
| | | 65 | | |
| | | 66 | | // check length against configured maximum to prevevent decompression bomb attacks |
| | 0 | 67 | | if ( decompressedStream.Length > MaxDecompressedSize ) |
| | | 68 | | { |
| | 0 | 69 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError( new SecurityTokenException( SR.Fo |
| | | 70 | | } |
| | 0 | 71 | | } while ( bytesRead > 0 ); |
| | | 72 | | |
| | 0 | 73 | | return decompressedStream.ToArray(); |
| | | 74 | | } |
| | | 75 | | } |
| | 0 | 76 | | } |
| | | 77 | | |
| | | 78 | | /// <summary> |
| | | 79 | | /// Deflates data. |
| | | 80 | | /// </summary> |
| | | 81 | | /// <param name=nameof(value)>Data to be compressed.</param> |
| | | 82 | | /// <returns>Compressed data.</returns> |
| | | 83 | | /// <exception cref="ArgumentNullException">The argument 'value' is null.</exception> |
| | | 84 | | /// <exception cref="ArgumentException">The argument 'value' contains zero bytes.</exception> |
| | | 85 | | public override byte[] Encode( byte[] value ) |
| | | 86 | | { |
| | 0 | 87 | | if ( null == value ) |
| | | 88 | | { |
| | 0 | 89 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull( nameof(value) ); |
| | | 90 | | } |
| | | 91 | | |
| | 0 | 92 | | if ( 0 == value.Length ) |
| | | 93 | | { |
| | 0 | 94 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument( nameof(value), SR.Format( SR.ID6044 ) ); |
| | | 95 | | } |
| | | 96 | | |
| | 0 | 97 | | using ( MemoryStream compressedStream = new MemoryStream() ) |
| | | 98 | | { |
| | 0 | 99 | | using ( DeflateStream deflateStream = new DeflateStream( compressedStream, CompressionMode.Compress, tru |
| | | 100 | | { |
| | 0 | 101 | | deflateStream.Write( value, 0, value.Length ); |
| | 0 | 102 | | } |
| | | 103 | | |
| | 0 | 104 | | byte[] encoded = compressedStream.ToArray(); |
| | | 105 | | |
| | | 106 | | //if ( DiagnosticUtility.ShouldTrace( TraceEventType.Information ) ) |
| | | 107 | | //{ |
| | | 108 | | // TraceUtility.TraceEvent( |
| | | 109 | | // TraceEventType.Information, |
| | | 110 | | // TraceCode.Diagnostics, |
| | | 111 | | // SR.Format(SR.TraceDeflateCookieEncode), |
| | | 112 | | // new DeflateCookieTraceRecord( value.Length, encoded.Length ), |
| | | 113 | | // null, |
| | | 114 | | // null ); |
| | | 115 | | //} |
| | | 116 | | |
| | 0 | 117 | | return encoded; |
| | | 118 | | } |
| | 0 | 119 | | } |
| | | 120 | | } |
| | | 121 | | } |