|
| 1 | +// ----------------------------------------------------------------------- |
| 2 | +// Licensed to The .NET Foundation under one or more agreements. |
| 3 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 4 | +// ----------------------------------------------------------------------- |
| 5 | + |
| 6 | +using System; |
| 7 | +using System.Buffers.Binary; |
| 8 | +using System.IO; |
| 9 | +using System.Security.Cryptography; |
| 10 | + |
| 11 | +namespace Kerberos.NET.Entities |
| 12 | +{ |
| 13 | + /// <summary> |
| 14 | + /// Represents gss_channel_bindings_struct per RFC 4121 section 4.1.1.2. |
| 15 | + /// </summary> |
| 16 | + public class GssChannelBindings |
| 17 | + { |
| 18 | + private const int SecChannelBindingsHeaderSize = 32; |
| 19 | + |
| 20 | + public int InitiatorAddrType { get; set; } |
| 21 | + |
| 22 | + public ReadOnlyMemory<byte> InitiatorAddress { get; set; } |
| 23 | + |
| 24 | + public int AcceptorAddrType { get; set; } |
| 25 | + |
| 26 | + public ReadOnlyMemory<byte> AcceptorAddress { get; set; } |
| 27 | + |
| 28 | + /// <summary> |
| 29 | + /// Protocol-specific channel binding data |
| 30 | + /// e.g. tls-server-end-point or tls-unique as per RFC 5929 |
| 31 | + /// </summary> |
| 32 | + public ReadOnlyMemory<byte> ApplicationData { get; set; } |
| 33 | + |
| 34 | + /// <summary> |
| 35 | + /// Computes the 16-byte MD5 binding hash (Bnd field) per RFC 4121 section 4.1.1.2. |
| 36 | + /// </summary> |
| 37 | + public ReadOnlyMemory<byte> ComputeBindingHash() |
| 38 | + { |
| 39 | + using var stream = new MemoryStream(); |
| 40 | + using var writer = new BinaryWriter(stream); |
| 41 | + |
| 42 | + writer.Write(this.InitiatorAddrType); |
| 43 | + writer.Write(this.InitiatorAddress.Length); |
| 44 | + |
| 45 | + if (this.InitiatorAddress.Length > 0) |
| 46 | + { |
| 47 | + writer.Write(this.InitiatorAddress.ToArray()); |
| 48 | + } |
| 49 | + |
| 50 | + writer.Write(this.AcceptorAddrType); |
| 51 | + writer.Write(this.AcceptorAddress.Length); |
| 52 | + |
| 53 | + if (this.AcceptorAddress.Length > 0) |
| 54 | + { |
| 55 | + writer.Write(this.AcceptorAddress.ToArray()); |
| 56 | + } |
| 57 | + |
| 58 | + writer.Write(this.ApplicationData.Length); |
| 59 | + |
| 60 | + if (this.ApplicationData.Length > 0) |
| 61 | + { |
| 62 | + writer.Write(this.ApplicationData.ToArray()); |
| 63 | + } |
| 64 | + |
| 65 | + var data = stream.ToArray(); |
| 66 | + |
| 67 | + using var md5 = MD5.Create(); |
| 68 | + return md5.ComputeHash(data); |
| 69 | + } |
| 70 | + |
| 71 | + /// <summary> |
| 72 | + /// Parses a raw SEC_CHANNEL_BINDINGS flat buffer (as returned by Windows SSPI) into a <see cref="GssChannelBindings"/>. |
| 73 | + /// </summary> |
| 74 | + public static GssChannelBindings FromSecChannelBindings(ReadOnlyMemory<byte> rawBuffer) |
| 75 | + { |
| 76 | + if (rawBuffer.Length < SecChannelBindingsHeaderSize) |
| 77 | + { |
| 78 | + throw new ArgumentException( |
| 79 | + $"Buffer is too small to contain a SEC_CHANNEL_BINDINGS header. Expected at least {SecChannelBindingsHeaderSize} bytes.", |
| 80 | + nameof(rawBuffer)); |
| 81 | + } |
| 82 | + |
| 83 | + var span = rawBuffer.Span; |
| 84 | + |
| 85 | + var bindings = new GssChannelBindings |
| 86 | + { |
| 87 | + InitiatorAddrType = BinaryPrimitives.ReadInt32LittleEndian(span.Slice(0)), |
| 88 | + }; |
| 89 | + |
| 90 | + int initiatorLength = BinaryPrimitives.ReadInt32LittleEndian(span.Slice(4)); |
| 91 | + int initiatorOffset = BinaryPrimitives.ReadInt32LittleEndian(span.Slice(8)); |
| 92 | + |
| 93 | + bindings.AcceptorAddrType = BinaryPrimitives.ReadInt32LittleEndian(span.Slice(12)); |
| 94 | + |
| 95 | + int acceptorLength = BinaryPrimitives.ReadInt32LittleEndian(span.Slice(16)); |
| 96 | + int acceptorOffset = BinaryPrimitives.ReadInt32LittleEndian(span.Slice(20)); |
| 97 | + |
| 98 | + int applicationDataLength = BinaryPrimitives.ReadInt32LittleEndian(span.Slice(24)); |
| 99 | + int applicationDataOffset = BinaryPrimitives.ReadInt32LittleEndian(span.Slice(28)); |
| 100 | + |
| 101 | + if (initiatorLength > 0) |
| 102 | + { |
| 103 | + bindings.InitiatorAddress = rawBuffer.Slice(initiatorOffset, initiatorLength); |
| 104 | + } |
| 105 | + |
| 106 | + if (acceptorLength > 0) |
| 107 | + { |
| 108 | + bindings.AcceptorAddress = rawBuffer.Slice(acceptorOffset, acceptorLength); |
| 109 | + } |
| 110 | + |
| 111 | + if (applicationDataLength > 0) |
| 112 | + { |
| 113 | + bindings.ApplicationData = rawBuffer.Slice(applicationDataOffset, applicationDataLength); |
| 114 | + } |
| 115 | + |
| 116 | + return bindings; |
| 117 | + } |
| 118 | + } |
| 119 | +} |
0 commit comments