|
| 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; |
| 17 | +using System.IO; |
| 18 | +using System.Linq; |
| 19 | +using System.Runtime.InteropServices; |
| 20 | + |
| 21 | +namespace NtApiDotNet.Win32.Security.Authentication.Kerberos |
| 22 | +{ |
| 23 | + /// <summary> |
| 24 | + /// Class to represent a cached external ticket. |
| 25 | + /// </summary> |
| 26 | + public sealed class KerberosExternalTicket |
| 27 | + { |
| 28 | + private static KerberosPrincipalName ParseName(IntPtr ptr) |
| 29 | + { |
| 30 | + if (ptr == IntPtr.Zero) |
| 31 | + return new KerberosPrincipalName(); |
| 32 | + KerberosNameType name_type = (KerberosNameType)Marshal.ReadInt16(ptr, 0); |
| 33 | + int count = Marshal.ReadInt16(ptr, 2); |
| 34 | + if (count == 0) |
| 35 | + return new KerberosPrincipalName(name_type, new string[0]); |
| 36 | + |
| 37 | + var name = new SafeStructureInOutBuffer<KERB_EXTERNAL_NAME>(ptr, Marshal.SizeOf(typeof(KERB_EXTERNAL_NAME)) |
| 38 | + + Marshal.SizeOf(typeof(UnicodeStringOut)) * count, false); |
| 39 | + UnicodeStringOut[] names = new UnicodeStringOut[count]; |
| 40 | + name.Data.ReadArray(0, names, 0, count); |
| 41 | + return new KerberosPrincipalName(name_type, names.Select(u => u.ToString())); |
| 42 | + } |
| 43 | + |
| 44 | + private static KerberosAuthenticationKey ParseKey(KerberosPrincipalName server_name, string realm, KERB_CRYPTO_KEY key) |
| 45 | + { |
| 46 | + byte[] key_data = new byte[key.Length]; |
| 47 | + Marshal.Copy(key.Value, key_data, 0, key.Length); |
| 48 | + return new KerberosAuthenticationKey(key.KeyType, key_data, server_name.NameType, realm, server_name.Names, DateTime.Now, 0); |
| 49 | + } |
| 50 | + |
| 51 | + /// <summary> |
| 52 | + /// Service name. |
| 53 | + /// </summary> |
| 54 | + public KerberosPrincipalName ServiceName { get; private set; } |
| 55 | + /// <summary> |
| 56 | + /// Target name. |
| 57 | + /// </summary> |
| 58 | + public KerberosPrincipalName TargetName { get; private set; } |
| 59 | + /// <summary> |
| 60 | + /// Client name. |
| 61 | + /// </summary> |
| 62 | + public KerberosPrincipalName ClientName { get; private set; } |
| 63 | + /// <summary> |
| 64 | + /// Domain name. |
| 65 | + /// </summary> |
| 66 | + public string DomainName { get; private set; } |
| 67 | + /// <summary> |
| 68 | + /// Target domain name. |
| 69 | + /// </summary> |
| 70 | + public string TargetDomainName { get; private set; } |
| 71 | + /// <summary> |
| 72 | + /// Alt target domain name. |
| 73 | + /// </summary> |
| 74 | + public string AltTargetDomainName { get; private set; } |
| 75 | + /// <summary> |
| 76 | + /// Session key for ticket. |
| 77 | + /// </summary> |
| 78 | + public KerberosAuthenticationKey SessionKey { get; private set; } |
| 79 | + /// <summary> |
| 80 | + /// Ticket flags. |
| 81 | + /// </summary> |
| 82 | + public KerberosTicketFlags TicketFlags { get; private set; } |
| 83 | + /// <summary> |
| 84 | + /// Additional reserved flags. |
| 85 | + /// </summary> |
| 86 | + public int Flags { get; private set; } |
| 87 | + /// <summary> |
| 88 | + /// Key expiration time. |
| 89 | + /// </summary> |
| 90 | + public DateTime KeyExpirationTime { get; private set; } |
| 91 | + /// <summary> |
| 92 | + /// Ticket start time. |
| 93 | + /// </summary> |
| 94 | + public DateTime StartTime { get; private set; } |
| 95 | + /// <summary> |
| 96 | + /// Ticket end time. |
| 97 | + /// </summary> |
| 98 | + public DateTime EndTime { get; private set; } |
| 99 | + /// <summary> |
| 100 | + /// Ticket renew time. |
| 101 | + /// </summary> |
| 102 | + public DateTime RenewUntil { get; private set; } |
| 103 | + /// <summary> |
| 104 | + /// Time skew. |
| 105 | + /// </summary> |
| 106 | + public TimeSpan TimeSkew { get; private set; } |
| 107 | + /// <summary> |
| 108 | + /// Ticket. |
| 109 | + /// </summary> |
| 110 | + public KerberosTicket Ticket { get; private set; } |
| 111 | + |
| 112 | + internal static bool TryParse(KERB_EXTERNAL_TICKET ticket, out KerberosExternalTicket result) |
| 113 | + { |
| 114 | + result = null; |
| 115 | + try |
| 116 | + { |
| 117 | + var ret = new KerberosExternalTicket(); |
| 118 | + ret.ServiceName = ParseName(ticket.ServiceName); |
| 119 | + ret.TargetName = ParseName(ticket.TargetName); |
| 120 | + ret.ClientName = ParseName(ticket.ClientName); |
| 121 | + ret.DomainName = ticket.DomainName.ToString(); |
| 122 | + ret.TargetDomainName = ticket.TargetDomainName.ToString(); |
| 123 | + ret.AltTargetDomainName = ticket.AltTargetDomainName.ToString(); |
| 124 | + ret.SessionKey = ParseKey(ret.ServiceName, ret.DomainName, ticket.SessionKey); |
| 125 | + ret.TicketFlags = (KerberosTicketFlags)ticket.TicketFlags.SwapEndian(); |
| 126 | + ret.Flags = ticket.Flags; |
| 127 | + ret.KeyExpirationTime = ticket.KeyExpirationTime.ToDateTime(); |
| 128 | + ret.StartTime = ticket.StartTime.ToDateTime(); |
| 129 | + ret.EndTime = ticket.EndTime.ToDateTime(); |
| 130 | + ret.RenewUntil = ticket.RenewUntil.ToDateTime(); |
| 131 | + ret.TimeSkew = new TimeSpan(ticket.TimeSkew.QuadPart); |
| 132 | + byte[] ticket_data = ticket.ReadTicket(); |
| 133 | + DERValue[] values = DERParser.ParseData(ticket_data, 0); |
| 134 | + if (values.Length != 1) |
| 135 | + return false; |
| 136 | + ret.Ticket = KerberosTicket.Parse(values[0], ticket_data); |
| 137 | + result = ret; |
| 138 | + return true; |
| 139 | + } |
| 140 | + catch (InvalidDataException) |
| 141 | + { |
| 142 | + return false; |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | +} |
0 commit comments