Debian Codegraph @

messages/Ticket.go

raw symbols SCIP
1 package messages
2
3 import (
4 "crypto/rand"
5 "fmt"
6 "strings"
7 "time"
8
9 "github.com/jcmturner/gofork/encoding/asn1"
10 "gopkg.in/jcmturner/gokrb5.v5/asn1tools"
11 "gopkg.in/jcmturner/gokrb5.v5/crypto"
12 "gopkg.in/jcmturner/gokrb5.v5/iana"
13 "gopkg.in/jcmturner/gokrb5.v5/iana/adtype"
14 "gopkg.in/jcmturner/gokrb5.v5/iana/asnAppTag"
15 "gopkg.in/jcmturner/gokrb5.v5/iana/errorcode"
16 "gopkg.in/jcmturner/gokrb5.v5/iana/keyusage"
17 "gopkg.in/jcmturner/gokrb5.v5/keytab"
18 "gopkg.in/jcmturner/gokrb5.v5/krberror"
19 "gopkg.in/jcmturner/gokrb5.v5/pac"
20 "gopkg.in/jcmturner/gokrb5.v5/types"
21 )
22
23 // Reference: https://www.ietf.org/rfc/rfc4120.txt
24 // Section: 5.3
25
26 // Ticket implements the Kerberos ticket.
27 type Ticket struct {
28 TktVNO int `asn1:"explicit,tag:0"`
29 Realm string `asn1:"generalstring,explicit,tag:1"`
30 SName types.PrincipalName `asn1:"explicit,tag:2"`
31 EncPart types.EncryptedData `asn1:"explicit,tag:3"`
32 DecryptedEncPart EncTicketPart `asn1:"optional"` // Not part of ASN1 bytes so marked as optional so unmarshalling works
33 }
34
35 // EncTicketPart is the encrypted part of the Ticket.
36 type EncTicketPart struct {
37 Flags asn1.BitString `asn1:"explicit,tag:0"`
38 Key types.EncryptionKey `asn1:"explicit,tag:1"`
39 CRealm string `asn1:"generalstring,explicit,tag:2"`
40 CName types.PrincipalName `asn1:"explicit,tag:3"`
41 Transited TransitedEncoding `asn1:"explicit,tag:4"`
42 AuthTime time.Time `asn1:"generalized,explicit,tag:5"`
43 StartTime time.Time `asn1:"generalized,explicit,optional,tag:6"`
44 EndTime time.Time `asn1:"generalized,explicit,tag:7"`
45 RenewTill time.Time `asn1:"generalized,explicit,optional,tag:8"`
46 CAddr types.HostAddresses `asn1:"explicit,optional,tag:9"`
47 AuthorizationData types.AuthorizationData `asn1:"explicit,optional,tag:10"`
48 }
49
50 // TransitedEncoding part of the ticket's encrypted part.
51 type TransitedEncoding struct {
52 TRType int32 `asn1:"explicit,tag:0"`
53 Contents []byte `asn1:"explicit,tag:1"`
54 }
55
56 // NewTicket creates a new Ticket instance.
57 func NewTicket(cname types.PrincipalName, crealm string, sname types.PrincipalName, srealm string, flags asn1.BitString, sktab keytab.Keytab, eTypeID int32, kvno int, authTime, startTime, endTime, renewTill time.Time) (Ticket, types.EncryptionKey, error) {
58 etype, err := crypto.GetEtype(eTypeID)
59 if err != nil {
60 return Ticket{}, types.EncryptionKey{}, krberror.Errorf(err, krberror.EncryptingError, "error getting etype for new ticket")
61 }
62 ks := etype.GetKeyByteSize()
63 kv := make([]byte, ks, ks)
64 rand.Read(kv)
65 sessionKey := types.EncryptionKey{
66 KeyType: eTypeID,
67 KeyValue: kv,
68 }
69 etp := EncTicketPart{
70 Flags: flags,
71 Key: sessionKey,
72 CRealm: crealm,
73 CName: cname,
74 Transited: TransitedEncoding{},
75 AuthTime: authTime,
76 StartTime: startTime,
77 EndTime: endTime,
78 RenewTill: renewTill,
79 }
80 b, err := asn1.Marshal(etp)
81 if err != nil {
82 return Ticket{}, types.EncryptionKey{}, krberror.Errorf(err, krberror.EncodingError, "error marshalling ticket encpart")
83 }
84 b = asn1tools.AddASNAppTag(b, asnAppTag.EncTicketPart)
85 skey, err := sktab.GetEncryptionKey(sname.NameString, srealm, kvno, eTypeID)
86 if err != nil {
87 return Ticket{}, types.EncryptionKey{}, krberror.Errorf(err, krberror.EncryptingError, "error getting encryption key for new ticket")
88 }
89 ed, err := crypto.GetEncryptedData(b, skey, keyusage.KDC_REP_TICKET, kvno)
90 if err != nil {
91 return Ticket{}, types.EncryptionKey{}, krberror.Errorf(err, krberror.EncryptingError, "error encrypting ticket encpart")
92 }
93 tkt := Ticket{
94 TktVNO: iana.PVNO,
95 Realm: srealm,
96 SName: sname,
97 EncPart: ed,
98 }
99 return tkt, sessionKey, nil
100 }
101
102 // Unmarshal bytes b into a Ticket struct.
103 func (t *Ticket) Unmarshal(b []byte) error {
104 _, err := asn1.UnmarshalWithParams(b, t, fmt.Sprintf("application,explicit,tag:%d", asnAppTag.Ticket))
105 return err
106 }
107
108 // Marshal the Ticket.
109 func (t *Ticket) Marshal() ([]byte, error) {
110 b, err := asn1.Marshal(*t)
111 if err != nil {
112 return nil, err
113 }
114 b = asn1tools.AddASNAppTag(b, asnAppTag.Ticket)
115 return b, nil
116 }
117
118 // Unmarshal bytes b into the EncTicketPart struct.
119 func (t *EncTicketPart) Unmarshal(b []byte) error {
120 _, err := asn1.UnmarshalWithParams(b, t, fmt.Sprintf("application,explicit,tag:%d", asnAppTag.EncTicketPart))
121 return err
122 }
123
124 // UnmarshalTicket returns a ticket from the bytes provided.
125 func UnmarshalTicket(b []byte) (t Ticket, err error) {
126 _, err = asn1.UnmarshalWithParams(b, &t, fmt.Sprintf("application,explicit,tag:%d", asnAppTag.Ticket))
127 return
128 }
129
130 // UnmarshalTicketsSequence returns a slice of Tickets from a raw ASN1 value.
131 func UnmarshalTicketsSequence(in asn1.RawValue) ([]Ticket, error) {
132 //This is a workaround to a asn1 decoding issue in golang - https://github.com/golang/go/issues/17321. It's not pretty I'm afraid
133 //We pull out raw values from the larger raw value (that is actually the data of the sequence of raw values) and track our position moving along the data.
134 b := in.Bytes
135 // Ignore the head of the asn1 stream (1 byte for tag and those for the length) as this is what tells us its a sequence but we're handling it ourselves
136 p := 1 + asn1tools.GetNumberBytesInLengthHeader(in.Bytes)
137 var tkts []Ticket
138 var raw asn1.RawValue
139 for p < (len(b)) {
140 _, err := asn1.UnmarshalWithParams(b[p:], &raw, fmt.Sprintf("application,tag:%d", asnAppTag.Ticket))
141 if err != nil {
142 return nil, fmt.Errorf("unmarshaling sequence of tickets failed geting length of ticket: %v", err)
143 }
144 t, err := UnmarshalTicket(b[p:])
145 if err != nil {
146 return nil, fmt.Errorf("unmarshaling sequence of tickets failed: %v", err)
147 }
148 p += len(raw.FullBytes)
149 tkts = append(tkts, t)
150 }
151 MarshalTicketSequence(tkts)
152 return tkts, nil
153 }
154
155 // MarshalTicketSequence marshals a slice of Tickets returning an ASN1 raw value containing the ticket sequence.
156 func MarshalTicketSequence(tkts []Ticket) (asn1.RawValue, error) {
157 raw := asn1.RawValue{
158 Class: 2,
159 IsCompound: true,
160 }
161 if len(tkts) < 1 {
162 // There are no tickets to marshal
163 return raw, nil
164 }
165 var btkts []byte
166 for i, t := range tkts {
167 b, err := t.Marshal()
168 if err != nil {
169 return raw, fmt.Errorf("error marshaling ticket number %d in seqence of tickets", i+1)
170 }
171 btkts = append(btkts, b...)
172 }
173 btkts = append(asn1tools.MarshalLengthBytes(len(btkts)), btkts...)
174 btkts = append([]byte{byte(32 + asn1.TagSequence)}, btkts...)
175 raw.Bytes = btkts
176 // If we need to create the full bytes then identifier octet is "context-specific" = 128 + "constructed" + 32 + the wrapping explicit tag (11)
177 //fmt.Fprintf(os.Stderr, "mRaw fb: %v\n", raw.FullBytes)
178 return raw, nil
179 }
180
181 // DecryptEncPart decrypts the encrypted part of the ticket.
182 func (t *Ticket) DecryptEncPart(keytab keytab.Keytab, ktprinc string) error {
183 var upn types.PrincipalName
184 realm := t.Realm
185 if ktprinc != "" {
186 var r string
187 upn, r = types.ParseSPNString(ktprinc)
188 if r != "" {
189 realm = r
190 }
191 } else {
192 upn = t.SName
193 }
194 key, err := keytab.GetEncryptionKey(upn.NameString, realm, t.EncPart.KVNO, t.EncPart.EType)
195 if err != nil {
196 return NewKRBError(t.SName, t.Realm, errorcode.KRB_AP_ERR_NOKEY, fmt.Sprintf("Could not get key from keytab: %v", err))
197 }
198 b, err := crypto.DecryptEncPart(t.EncPart, key, keyusage.KDC_REP_TICKET)
199 if err != nil {
200 return fmt.Errorf("error decrypting Ticket EncPart: %v", err)
201 }
202 var denc EncTicketPart
203 err = denc.Unmarshal(b)
204 if err != nil {
205 return fmt.Errorf("error unmarshaling encrypted part: %v", err)
206 }
207 t.DecryptedEncPart = denc
208 return nil
209 }
210
211 // GetPACType returns a Microsoft PAC that has been extracted from the ticket and processed.
212 func (t *Ticket) GetPACType(keytab keytab.Keytab, ktprinc string) (bool, pac.PACType, error) {
213 var isPAC bool
214 for _, ad := range t.DecryptedEncPart.AuthorizationData {
215 if ad.ADType == adtype.ADIfRelevant {
216 var ad2 types.AuthorizationData
217 err := ad2.Unmarshal(ad.ADData)
218 if err != nil {
219 continue
220 }
221 if ad2[0].ADType == adtype.ADWin2KPAC {
222 isPAC = true
223 var p pac.PACType
224 err = p.Unmarshal(ad2[0].ADData)
225 if err != nil {
226 return isPAC, p, fmt.Errorf("error unmarshaling PAC: %v", err)
227 }
228 var upn []string
229 if ktprinc != "" {
230 upn = strings.Split(ktprinc, "/")
231 } else {
232 upn = t.SName.NameString
233 }
234 key, err := keytab.GetEncryptionKey(upn, t.Realm, t.EncPart.KVNO, t.EncPart.EType)
235 if err != nil {
236 return isPAC, p, NewKRBError(t.SName, t.Realm, errorcode.KRB_AP_ERR_NOKEY, fmt.Sprintf("Could not get key from keytab: %v", err))
237 }
238 err = p.ProcessPACInfoBuffers(key)
239 return isPAC, p, err
240 }
241 }
242 }
243 return isPAC, pac.PACType{}, nil
244 }