| 1 |
package credentials |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"encoding/binary" |
| 6 |
"errors" |
| 7 |
"io/ioutil" |
| 8 |
"strings" |
| 9 |
"time" |
| 10 |
"unsafe" |
| 11 |
|
| 12 |
"github.com/jcmturner/gofork/encoding/asn1" |
| 13 |
"gopkg.in/jcmturner/gokrb5.v5/types" |
| 14 |
) |
| 15 |
|
| 16 |
const ( |
| 17 |
headerFieldTagKDCOffset = 1 |
| 18 |
) |
| 19 |
|
| 20 |
|
| 21 |
type CCache struct { |
| 22 |
Version uint8 |
| 23 |
Header header |
| 24 |
DefaultPrincipal principal |
| 25 |
Credentials []credential |
| 26 |
Path string |
| 27 |
} |
| 28 |
|
| 29 |
type header struct { |
| 30 |
length uint16 |
| 31 |
fields []headerField |
| 32 |
} |
| 33 |
|
| 34 |
type headerField struct { |
| 35 |
tag uint16 |
| 36 |
length uint16 |
| 37 |
value []byte |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
type principal struct { |
| 42 |
Realm string |
| 43 |
PrincipalName types.PrincipalName |
| 44 |
} |
| 45 |
|
| 46 |
type credential struct { |
| 47 |
Client principal |
| 48 |
Server principal |
| 49 |
Key types.EncryptionKey |
| 50 |
AuthTime time.Time |
| 51 |
StartTime time.Time |
| 52 |
EndTime time.Time |
| 53 |
RenewTill time.Time |
| 54 |
IsSKey bool |
| 55 |
TicketFlags asn1.BitString |
| 56 |
Addresses []types.HostAddress |
| 57 |
AuthData []types.AuthorizationDataEntry |
| 58 |
Ticket []byte |
| 59 |
SecondTicket []byte |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
func LoadCCache(cpath string) (CCache, error) { |
| 64 |
k, err := ioutil.ReadFile(cpath) |
| 65 |
if err != nil { |
| 66 |
return CCache{}, err |
| 67 |
} |
| 68 |
c, err := ParseCCache(k) |
| 69 |
c.Path = cpath |
| 70 |
return c, err |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
func ParseCCache(b []byte) (c CCache, err error) { |
| 75 |
p := 0 |
| 76 |
|
| 77 |
if int8(b[p]) != 5 { |
| 78 |
err = errors.New("Invalid credential cache data. First byte does not equal 5") |
| 79 |
return |
| 80 |
} |
| 81 |
p++ |
| 82 |
|
| 83 |
|
| 84 |
c.Version = uint8(b[p]) |
| 85 |
if c.Version < 1 || c.Version > 4 { |
| 86 |
err = errors.New("Invalid credential cache data. Keytab version is not within 1 to 4") |
| 87 |
if err != nil { |
| 88 |
return |
| 89 |
} |
| 90 |
} |
| 91 |
p++ |
| 92 |
|
| 93 |
var endian binary.ByteOrder |
| 94 |
endian = binary.BigEndian |
| 95 |
if (c.Version == 1 || c.Version == 2) && isNativeEndianLittle() { |
| 96 |
endian = binary.LittleEndian |
| 97 |
} |
| 98 |
if c.Version == 4 { |
| 99 |
err = parseHeader(b, &p, &c, &endian) |
| 100 |
if err != nil { |
| 101 |
return |
| 102 |
} |
| 103 |
} |
| 104 |
c.DefaultPrincipal = parsePrincipal(b, &p, &c, &endian) |
| 105 |
for p < len(b) { |
| 106 |
cred, e := parseCredential(b, &p, &c, &endian) |
| 107 |
if e != nil { |
| 108 |
err = e |
| 109 |
return |
| 110 |
} |
| 111 |
c.Credentials = append(c.Credentials, cred) |
| 112 |
} |
| 113 |
return |
| 114 |
} |
| 115 |
|
| 116 |
func parseHeader(b []byte, p *int, c *CCache, e *binary.ByteOrder) error { |
| 117 |
if c.Version != 4 { |
| 118 |
return errors.New("Credentials cache version is not 4 so there is no header to parse.") |
| 119 |
} |
| 120 |
h := header{} |
| 121 |
h.length = uint16(readInt16(b, p, e)) |
| 122 |
for *p <= int(h.length) { |
| 123 |
f := headerField{} |
| 124 |
f.tag = uint16(readInt16(b, p, e)) |
| 125 |
f.length = uint16(readInt16(b, p, e)) |
| 126 |
f.value = b[*p : *p+int(f.length)] |
| 127 |
*p += int(f.length) |
| 128 |
if !f.valid() { |
| 129 |
return errors.New("Invalid credential cache header found") |
| 130 |
} |
| 131 |
h.fields = append(h.fields, f) |
| 132 |
} |
| 133 |
c.Header = h |
| 134 |
return nil |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
func parsePrincipal(b []byte, p *int, c *CCache, e *binary.ByteOrder) (princ principal) { |
| 139 |
if c.Version != 1 { |
| 140 |
|
| 141 |
princ.PrincipalName.NameType = int32(readInt32(b, p, e)) |
| 142 |
} |
| 143 |
nc := int(readInt32(b, p, e)) |
| 144 |
if c.Version == 1 { |
| 145 |
|
| 146 |
nc-- |
| 147 |
} |
| 148 |
lenRealm := readInt32(b, p, e) |
| 149 |
princ.Realm = string(readBytes(b, p, int(lenRealm), e)) |
| 150 |
for i := 0; i < int(nc); i++ { |
| 151 |
l := readInt32(b, p, e) |
| 152 |
princ.PrincipalName.NameString = append(princ.PrincipalName.NameString, string(readBytes(b, p, int(l), e))) |
| 153 |
} |
| 154 |
return princ |
| 155 |
} |
| 156 |
|
| 157 |
func parseCredential(b []byte, p *int, c *CCache, e *binary.ByteOrder) (cred credential, err error) { |
| 158 |
cred.Client = parsePrincipal(b, p, c, e) |
| 159 |
cred.Server = parsePrincipal(b, p, c, e) |
| 160 |
key := types.EncryptionKey{} |
| 161 |
key.KeyType = int32(readInt16(b, p, e)) |
| 162 |
if c.Version == 3 { |
| 163 |
|
| 164 |
key.KeyType = int32(readInt16(b, p, e)) |
| 165 |
} |
| 166 |
key.KeyValue = readData(b, p, e) |
| 167 |
cred.Key = key |
| 168 |
cred.AuthTime = readTimestamp(b, p, e) |
| 169 |
cred.StartTime = readTimestamp(b, p, e) |
| 170 |
cred.EndTime = readTimestamp(b, p, e) |
| 171 |
cred.RenewTill = readTimestamp(b, p, e) |
| 172 |
if ik := readInt8(b, p, e); ik == 0 { |
| 173 |
cred.IsSKey = false |
| 174 |
} else { |
| 175 |
cred.IsSKey = true |
| 176 |
} |
| 177 |
cred.TicketFlags = types.NewKrbFlags() |
| 178 |
cred.TicketFlags.Bytes = readBytes(b, p, 4, e) |
| 179 |
l := int(readInt32(b, p, e)) |
| 180 |
cred.Addresses = make([]types.HostAddress, l, l) |
| 181 |
for i := range cred.Addresses { |
| 182 |
cred.Addresses[i] = readAddress(b, p, e) |
| 183 |
} |
| 184 |
l = int(readInt32(b, p, e)) |
| 185 |
cred.AuthData = make([]types.AuthorizationDataEntry, l, l) |
| 186 |
for i := range cred.AuthData { |
| 187 |
cred.AuthData[i] = readAuthDataEntry(b, p, e) |
| 188 |
} |
| 189 |
cred.Ticket = readData(b, p, e) |
| 190 |
cred.SecondTicket = readData(b, p, e) |
| 191 |
return |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
func (c *CCache) GetClientPrincipalName() types.PrincipalName { |
| 196 |
return c.DefaultPrincipal.PrincipalName |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
func (c *CCache) GetClientRealm() string { |
| 201 |
return c.DefaultPrincipal.Realm |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
func (c *CCache) GetClientCredentials() *Credentials { |
| 206 |
return &Credentials{ |
| 207 |
Username: c.DefaultPrincipal.PrincipalName.GetPrincipalNameString(), |
| 208 |
Realm: c.GetClientRealm(), |
| 209 |
CName: c.DefaultPrincipal.PrincipalName, |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
func (c *CCache) Contains(p types.PrincipalName) bool { |
| 215 |
for _, cred := range c.Credentials { |
| 216 |
if cred.Server.PrincipalName.Equal(p) { |
| 217 |
return true |
| 218 |
} |
| 219 |
} |
| 220 |
return false |
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
func (c *CCache) GetEntry(p types.PrincipalName) (credential, bool) { |
| 225 |
var cred credential |
| 226 |
var found bool |
| 227 |
for i := range c.Credentials { |
| 228 |
if c.Credentials[i].Server.PrincipalName.Equal(p) { |
| 229 |
cred = c.Credentials[i] |
| 230 |
found = true |
| 231 |
break |
| 232 |
} |
| 233 |
} |
| 234 |
if !found { |
| 235 |
return cred, false |
| 236 |
} |
| 237 |
return cred, true |
| 238 |
} |
| 239 |
|
| 240 |
|
| 241 |
func (c *CCache) GetEntries() []credential { |
| 242 |
var creds []credential |
| 243 |
for _, cred := range c.Credentials { |
| 244 |
|
| 245 |
if strings.HasPrefix(cred.Server.Realm, "X-CACHECONF") { |
| 246 |
continue |
| 247 |
} |
| 248 |
creds = append(creds, cred) |
| 249 |
} |
| 250 |
return creds |
| 251 |
} |
| 252 |
|
| 253 |
func (h *headerField) valid() bool { |
| 254 |
|
| 255 |
switch h.tag { |
| 256 |
case headerFieldTagKDCOffset: |
| 257 |
if h.length != 8 || len(h.value) != 8 { |
| 258 |
return false |
| 259 |
} |
| 260 |
return true |
| 261 |
} |
| 262 |
return false |
| 263 |
} |
| 264 |
|
| 265 |
func readData(b []byte, p *int, e *binary.ByteOrder) []byte { |
| 266 |
l := readInt32(b, p, e) |
| 267 |
return readBytes(b, p, int(l), e) |
| 268 |
} |
| 269 |
|
| 270 |
func readAddress(b []byte, p *int, e *binary.ByteOrder) types.HostAddress { |
| 271 |
a := types.HostAddress{} |
| 272 |
a.AddrType = int32(readInt16(b, p, e)) |
| 273 |
a.Address = readData(b, p, e) |
| 274 |
return a |
| 275 |
} |
| 276 |
|
| 277 |
func readAuthDataEntry(b []byte, p *int, e *binary.ByteOrder) types.AuthorizationDataEntry { |
| 278 |
a := types.AuthorizationDataEntry{} |
| 279 |
a.ADType = int32(readInt16(b, p, e)) |
| 280 |
a.ADData = readData(b, p, e) |
| 281 |
return a |
| 282 |
} |
| 283 |
|
| 284 |
|
| 285 |
func readTimestamp(b []byte, p *int, e *binary.ByteOrder) time.Time { |
| 286 |
return time.Unix(int64(readInt32(b, p, e)), 0) |
| 287 |
} |
| 288 |
|
| 289 |
|
| 290 |
func readInt8(b []byte, p *int, e *binary.ByteOrder) (i int8) { |
| 291 |
buf := bytes.NewBuffer(b[*p : *p+1]) |
| 292 |
binary.Read(buf, *e, &i) |
| 293 |
*p++ |
| 294 |
return |
| 295 |
} |
| 296 |
|
| 297 |
|
| 298 |
func readInt16(b []byte, p *int, e *binary.ByteOrder) (i int16) { |
| 299 |
buf := bytes.NewBuffer(b[*p : *p+2]) |
| 300 |
binary.Read(buf, *e, &i) |
| 301 |
*p += 2 |
| 302 |
return |
| 303 |
} |
| 304 |
|
| 305 |
|
| 306 |
func readInt32(b []byte, p *int, e *binary.ByteOrder) (i int32) { |
| 307 |
buf := bytes.NewBuffer(b[*p : *p+4]) |
| 308 |
binary.Read(buf, *e, &i) |
| 309 |
*p += 4 |
| 310 |
return |
| 311 |
} |
| 312 |
|
| 313 |
func readBytes(b []byte, p *int, s int, e *binary.ByteOrder) []byte { |
| 314 |
buf := bytes.NewBuffer(b[*p : *p+s]) |
| 315 |
r := make([]byte, s) |
| 316 |
binary.Read(buf, *e, &r) |
| 317 |
*p += s |
| 318 |
return r |
| 319 |
} |
| 320 |
|
| 321 |
func isNativeEndianLittle() bool { |
| 322 |
var x = 0x012345678 |
| 323 |
var p = unsafe.Pointer(&x) |
| 324 |
var bp = (*[4]byte)(p) |
| 325 |
|
| 326 |
var endian bool |
| 327 |
if 0x01 == bp[0] { |
| 328 |
endian = false |
| 329 |
} else if (0x78 & 0xff) == (bp[0] & 0xff) { |
| 330 |
endian = true |
| 331 |
} else { |
| 332 |
|
| 333 |
endian = false |
| 334 |
} |
| 335 |
return endian |
| 336 |
} |