| | | 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.Diagnostics; |
| | | 5 | | using System.Runtime.InteropServices; |
| | | 6 | | using System.Security.Authentication.ExtendedProtection; |
| | | 7 | | |
| | | 8 | | namespace System.Net.Security |
| | | 9 | | { |
| | | 10 | | [StructLayout(LayoutKind.Sequential)] |
| | | 11 | | internal ref struct InputSecurityBuffers |
| | | 12 | | { |
| | | 13 | | internal int Count; |
| | | 14 | | internal InputSecurityBuffer _item0; |
| | | 15 | | internal InputSecurityBuffer _item1; |
| | | 16 | | internal InputSecurityBuffer _item2; |
| | | 17 | | |
| | | 18 | | internal void SetNextBuffer(InputSecurityBuffer buffer) |
| | 0 | 19 | | { |
| | 0 | 20 | | Debug.Assert(Count >= 0 && Count < 3); |
| | 0 | 21 | | if (Count == 0) |
| | 0 | 22 | | { |
| | 0 | 23 | | _item0 = buffer; |
| | 0 | 24 | | } |
| | 0 | 25 | | else if (Count == 1) |
| | 0 | 26 | | { |
| | 0 | 27 | | _item1 = buffer; |
| | 0 | 28 | | } |
| | | 29 | | else |
| | 0 | 30 | | { |
| | 0 | 31 | | _item2 = buffer; |
| | 0 | 32 | | } |
| | | 33 | | |
| | 0 | 34 | | Count++; |
| | 0 | 35 | | } |
| | | 36 | | } |
| | | 37 | | |
| | | 38 | | [StructLayout(LayoutKind.Auto)] |
| | | 39 | | internal ref struct InputSecurityBuffer |
| | | 40 | | { |
| | | 41 | | public SecurityBufferType Type; |
| | | 42 | | public ReadOnlySpan<byte> Token; |
| | | 43 | | public SafeHandle? UnmanagedToken; |
| | | 44 | | |
| | | 45 | | public InputSecurityBuffer(ReadOnlySpan<byte> data, SecurityBufferType tokentype) |
| | | 46 | | { |
| | | 47 | | Token = data; |
| | | 48 | | Type = tokentype; |
| | | 49 | | UnmanagedToken = null; |
| | | 50 | | } |
| | | 51 | | |
| | | 52 | | public InputSecurityBuffer(ChannelBinding binding) |
| | | 53 | | { |
| | | 54 | | Type = SecurityBufferType.SECBUFFER_CHANNEL_BINDINGS; |
| | | 55 | | Token = default; |
| | | 56 | | UnmanagedToken = binding; |
| | | 57 | | } |
| | | 58 | | } |
| | | 59 | | |
| | | 60 | | [StructLayout(LayoutKind.Auto)] |
| | | 61 | | internal struct SecurityBuffer |
| | | 62 | | { |
| | | 63 | | public int offset; |
| | | 64 | | public int size; |
| | | 65 | | public SecurityBufferType type; |
| | | 66 | | public byte[]? token; |
| | | 67 | | public SafeHandle? unmanagedToken; |
| | | 68 | | |
| | | 69 | | public SecurityBuffer(byte[]? data, int offset, int size, SecurityBufferType tokentype) |
| | | 70 | | { |
| | | 71 | | Debug.Assert(offset >= 0 && offset <= (data == null ? 0 : data.Length), $"'offset' out of range. [{offset}] |
| | | 72 | | Debug.Assert(size >= 0 && size <= (data == null ? 0 : data.Length - offset), $"'size' out of range. [{size} |
| | | 73 | | |
| | | 74 | | this.offset = data == null || offset < 0 ? 0 : Math.Min(offset, data.Length); |
| | | 75 | | this.size = data == null || size < 0 ? 0 : Math.Min(size, data.Length - this.offset); |
| | | 76 | | this.type = tokentype; |
| | | 77 | | this.token = size == 0 ? null : data; |
| | | 78 | | this.unmanagedToken = null; |
| | | 79 | | } |
| | | 80 | | |
| | | 81 | | public SecurityBuffer(byte[]? data, SecurityBufferType tokentype) |
| | | 82 | | { |
| | | 83 | | this.offset = 0; |
| | | 84 | | this.size = data == null ? 0 : data.Length; |
| | | 85 | | this.type = tokentype; |
| | | 86 | | this.token = size == 0 ? null : data; |
| | | 87 | | this.unmanagedToken = null; |
| | | 88 | | } |
| | | 89 | | |
| | | 90 | | public SecurityBuffer(int size, SecurityBufferType tokentype) |
| | | 91 | | { |
| | | 92 | | Debug.Assert(size >= 0, $"'size' out of range. [{size}]"); |
| | | 93 | | |
| | | 94 | | this.offset = 0; |
| | | 95 | | this.size = size; |
| | | 96 | | this.type = tokentype; |
| | | 97 | | this.token = size == 0 ? null : new byte[size]; |
| | | 98 | | this.unmanagedToken = null; |
| | | 99 | | } |
| | | 100 | | |
| | | 101 | | public SecurityBuffer(ChannelBinding binding) |
| | | 102 | | { |
| | | 103 | | this.offset = 0; |
| | | 104 | | this.size = (binding == null ? 0 : binding.Size); |
| | | 105 | | this.type = SecurityBufferType.SECBUFFER_CHANNEL_BINDINGS; |
| | | 106 | | this.token = null; |
| | | 107 | | this.unmanagedToken = binding; |
| | | 108 | | } |
| | | 109 | | } |
| | | 110 | | } |
| | | 111 | | |