~moody/libdp9ik

libdp9ik/ticket.go -rw-r--r-- 3.3 KiB
4efa26b9 — Jacob Moody Progress made towards authpak_new and authpak_finish 9 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package libdp9ik

import (
	"errors"
	"fmt"
	"io"
	"strconv"
	"strings"
)

const (
	AuthTreq = iota + 1
	AuthChal
	AuthPass
	AuthOK
	AuthErr
	AuthMod
	AuthApop
	_
	AuthOKvar
	AuthChap
	AuthMSchap
	AuthCram
	AuthHttp
	AuthVNC = 14
	AuthPAK = 19
	AuthTs  = iota + 64
	AuthTc
	AuthAs
	AuthAc
	AuthTp
	AuthHr
)

type TicketReq struct {
	Which   byte
	Authid  [Anamelen]byte
	Authdom [Domlen]byte
	Chal    [Challen]byte
	Hostid  [Anamelen]byte
	Uid     [Anamelen]byte
}

func (t *TicketReq) String() string {
	b := &strings.Builder{}
	b.WriteString(fmt.Sprintf("Which: %v\n", t.Which))
	b.WriteString("AuthID: " + string(t.Authid[:]) + "\n")
	b.WriteString("AuthDom: " + string(t.Authdom[:]) + "\n")
	b.WriteString(fmt.Sprintf("Chal: %v\n", t.Chal))
	b.WriteString(fmt.Sprintf("HostID: %v\n", t.Hostid))
	b.WriteString(fmt.Sprintf("UID: %v\n", t.Uid))

	return b.String()
}

func (t *TicketReq) UnmarshalBinary(data []byte) error {
	n := 0
	move := func(dst []byte, width int) {
		copy(dst, data[n:n+width])
		n += width
	}
	t.Which = data[n]
	n++
	move(t.Authid[:], Anamelen)
	move(t.Authdom[:], Domlen)
	move(t.Chal[:], Challen)
	move(t.Hostid[:], Anamelen)
	move(t.Uid[:], Anamelen)

	return nil
}

func (t *TicketReq) MarshalBinary() ([]byte, error) {
	size := 1 + Anamelen + Domlen + Challen + Anamelen + Anamelen
	out := make([]byte, size)
	n := 0

	out[0] = t.Which
	n++
	move := func(src []byte, width int) {
		copy(out[n:n+width], src)
		n += width
	}
	move(t.Authid[:], Anamelen)
	move(t.Authdom[:], Domlen)
	move(t.Chal[:], Challen)
	move(t.Hostid[:], Anamelen)
	move(t.Uid[:], Anamelen)
	return out, nil
}

type Ticket struct {
	Num  byte           /* replay protection */
	Chal [Challen]byte  /* server challenge */
	Cuid [Anamelen]byte /* uid on client */
	Suid [Anamelen]byte /* uid on server */
	Key  [Noncelen]byte /* nonce key */

	Form byte /* (not transmitted) format (0 = des, 1 = ccpoly) */
}

func (t *Ticket) UnmarshalBinary(data []byte) error {
	n := 0
	move := func(dst []byte, width int) {
		copy(dst, data[n:n+width])
		n += width
	}
	t.Num = data[n]
	n++
	move(t.Chal[:], Challen)
	move(t.Cuid[:], Anamelen)
	move(t.Suid[:], Anamelen)
	move(t.Key[:], Noncelen)
	return nil
}

func asrdresp(fd io.ReadWriter, buf []byte) error {
	errmsg := make([]byte, 64)
	_, err := fd.Read(buf[:1])
	if err != nil {
		return err
	}
	switch buf[0] {
	case AuthOK:
		_, err = fd.Read(buf)
		return err
	case AuthErr:
		_, err = fd.Read(errmsg)
		if err != nil {
			return err
		}
		return errors.New(string(errmsg))
	case AuthOKvar:
		_, err = fd.Read(errmsg[:5])
		if err != nil {
			return err
		}
		n, err := strconv.Atoi(string(errmsg[:5]))
		if err != nil {
			return err
		}
		_, err = fd.Read(buf[:n])
		if err != nil {
			return err
		}
	default:
		return errors.New("unknown Which from auth server")
	}
	return nil
}

func (tr *TicketReq) asgetticket(asfd io.ReadWriter, tbuf []byte) error {
	err := tr.asrequest(asfd)
	if err != nil {
		return err
	}
	err = asrdresp(asfd, tbuf)
	if err != nil {
		return err
	}

	//According to libauthsrv/ticket.c:20 in drawterm this is suppsoed
	//to try and pull some more data, however it seems like in the
	//case of a client(our case) it is never needed so we simply ignore
	//it as being a dp9ik server is outside our scope

	return nil
}

func (tr *TicketReq) asrequest(asfd io.ReadWriter) error {
	b, _ := tr.MarshalBinary()
	_, err := asfd.Write(b)
	return err
}