|
| 1 | +// Copyright 2020 Google Inc. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +using NtApiDotNet.Utilities.ASN1; |
| 16 | +using System.Collections.Generic; |
| 17 | +using System.IO; |
| 18 | +using System.Text; |
| 19 | + |
| 20 | +namespace NtApiDotNet.Win32.Security.Authentication.Kerberos |
| 21 | +{ |
| 22 | + /// <summary> |
| 23 | + /// Class representing a KRB-CRED structure. |
| 24 | + /// </summary> |
| 25 | + public class KerberosCredential : KerberosAuthenticationToken |
| 26 | + { |
| 27 | + #region Public Properties |
| 28 | + /// <summary> |
| 29 | + /// List of tickets in this credential. |
| 30 | + /// </summary> |
| 31 | + public IReadOnlyList<KerberosTicket> Tickets { get; private set; } |
| 32 | + /// <summary> |
| 33 | + /// Encrypted part contains sesssion keys etc. |
| 34 | + /// </summary> |
| 35 | + public KerberosEncryptedData EncryptedPart { get; private set; } |
| 36 | + #endregion |
| 37 | + |
| 38 | + private KerberosCredential(byte[] data, DERValue[] values) |
| 39 | + : base(data, values, KerberosMessageType.KRB_CRED) |
| 40 | + { |
| 41 | + } |
| 42 | + |
| 43 | + /// <summary> |
| 44 | + /// Format the Authentication Token. |
| 45 | + /// </summary> |
| 46 | + /// <returns>The Formatted Token.</returns> |
| 47 | + public override string Format() |
| 48 | + { |
| 49 | + StringBuilder builder = new StringBuilder(); |
| 50 | + builder.AppendLine($"<KerberosV{ProtocolVersion} {MessageType}>"); |
| 51 | + for (int i = 0; i < Tickets.Count; ++i) |
| 52 | + { |
| 53 | + builder.AppendLine($"<Ticket {i}>"); |
| 54 | + builder.Append(Tickets[i].Format()); |
| 55 | + } |
| 56 | + builder.AppendLine("<Encrypted Part>"); |
| 57 | + builder.Append(EncryptedPart.Format()); |
| 58 | + return builder.ToString(); |
| 59 | + } |
| 60 | + |
| 61 | + /// <summary> |
| 62 | + /// Decrypt the Authentication Token using a keyset. |
| 63 | + /// </summary> |
| 64 | + /// <param name="keyset">The set of keys to decrypt the </param> |
| 65 | + /// <returns>The decrypted token, or the same token if nothing could be decrypted.</returns> |
| 66 | + public override KerberosAuthenticationToken Decrypt(KerberosKeySet keyset) |
| 67 | + { |
| 68 | + KerberosEncryptedData encdata = null; |
| 69 | + KerberosKeySet tmp_keys = new KerberosKeySet(keyset); |
| 70 | + List<KerberosTicket> dec_tickets = new List<KerberosTicket>(); |
| 71 | + bool decrypted_ticket = false; |
| 72 | + foreach (var ticket in Tickets) |
| 73 | + { |
| 74 | + if (ticket.Decrypt(tmp_keys, KeyUsage.AsRepTgsRepTicket, out KerberosTicket dec_ticket)) |
| 75 | + { |
| 76 | + dec_tickets.Add(dec_ticket); |
| 77 | + decrypted_ticket = true; |
| 78 | + } |
| 79 | + else |
| 80 | + { |
| 81 | + dec_tickets.Add(ticket); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + if (EncryptedPart.Decrypt(tmp_keys, string.Empty, new KerberosPrincipalName(), KeyUsage.KrbCred, out byte[] decrypted)) |
| 86 | + { |
| 87 | + // Needs session key from TGT request which we don't necessarily have. |
| 88 | + } |
| 89 | + |
| 90 | + if (decrypted_ticket || encdata != null) |
| 91 | + { |
| 92 | + KerberosCredential ret = (KerberosCredential)MemberwiseClone(); |
| 93 | + ret.Tickets = dec_tickets.AsReadOnly(); |
| 94 | + ret.EncryptedPart = encdata ?? ret.EncryptedPart; |
| 95 | + return ret; |
| 96 | + } |
| 97 | + return base.Decrypt(keyset); |
| 98 | + } |
| 99 | + |
| 100 | + /// <summary> |
| 101 | + /// Try and parse data into an ASN1 authentication token. |
| 102 | + /// </summary> |
| 103 | + /// <param name="data">The data to parse.</param> |
| 104 | + /// <param name="token">The Negotiate authentication token.</param> |
| 105 | + /// <param name="values">Parsed DER Values.</param> |
| 106 | + internal static bool TryParse(byte[] data, DERValue[] values, out KerberosCredential token) |
| 107 | + { |
| 108 | + token = null; |
| 109 | + try |
| 110 | + { |
| 111 | + var ret = new KerberosCredential(data, values); |
| 112 | + if (values.Length != 1 || !values[0].CheckMsg(KerberosMessageType.KRB_CRED) || !values[0].HasChildren()) |
| 113 | + return false; |
| 114 | + |
| 115 | + values = values[0].Children; |
| 116 | + if (values.Length != 1 || !values[0].CheckSequence() || !values[0].HasChildren()) |
| 117 | + return false; |
| 118 | + |
| 119 | + foreach (var next in values[0].Children) |
| 120 | + { |
| 121 | + if (next.Type != DERTagType.ContextSpecific) |
| 122 | + return false; |
| 123 | + switch (next.Tag) |
| 124 | + { |
| 125 | + case 0: |
| 126 | + if (next.ReadChildInteger() != 5) |
| 127 | + return false; |
| 128 | + break; |
| 129 | + case 1: |
| 130 | + if ((KerberosMessageType)next.ReadChildInteger() != KerberosMessageType.KRB_CRED) |
| 131 | + return false; |
| 132 | + break; |
| 133 | + case 2: |
| 134 | + if (!next.Children[0].CheckSequence()) |
| 135 | + return false; |
| 136 | + List<KerberosTicket> tickets = new List<KerberosTicket>(); |
| 137 | + foreach (var child in next.Children[0].Children) |
| 138 | + { |
| 139 | + tickets.Add(KerberosTicket.Parse(child)); |
| 140 | + } |
| 141 | + ret.Tickets = tickets.AsReadOnly(); |
| 142 | + break; |
| 143 | + case 3: |
| 144 | + if (!next.HasChildren()) |
| 145 | + return false; |
| 146 | + ret.EncryptedPart = KerberosEncryptedData.Parse(next.Children[0]); |
| 147 | + break; |
| 148 | + default: |
| 149 | + return false; |
| 150 | + } |
| 151 | + } |
| 152 | + token = ret; |
| 153 | + return true; |
| 154 | + } |
| 155 | + catch (InvalidDataException) |
| 156 | + { |
| 157 | + return false; |
| 158 | + } |
| 159 | + } |
| 160 | + } |
| 161 | +} |
0 commit comments