forked from jcmturner/gokrb5
/
NegotiationToken.go
148 lines (135 loc) · 4.27 KB
/
NegotiationToken.go
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
package gssapi
import (
"errors"
"fmt"
"github.com/jcmturner/gofork/encoding/asn1"
"gopkg.in/jcmturner/gokrb5.v4/credentials"
"gopkg.in/jcmturner/gokrb5.v4/messages"
"gopkg.in/jcmturner/gokrb5.v4/types"
)
/*
https://msdn.microsoft.com/en-us/library/ms995330.aspx
NegotiationToken ::= CHOICE {
negTokenInit [0] NegTokenInit, This is the Negotiation token sent from the client to the server.
negTokenResp [1] NegTokenResp
}
NegTokenInit ::= SEQUENCE {
mechTypes [0] MechTypeList,
reqFlags [1] ContextFlags OPTIONAL,
-- inherited from RFC 2478 for backward compatibility,
-- RECOMMENDED to be left out
mechToken [2] OCTET STRING OPTIONAL,
mechListMIC [3] OCTET STRING OPTIONAL,
...
}
NegTokenResp ::= SEQUENCE {
negState [0] ENUMERATED {
accept-completed (0),
accept-incomplete (1),
reject (2),
request-mic (3)
} OPTIONAL,
-- REQUIRED in the first reply from the target
supportedMech [1] MechType OPTIONAL,
-- present only in the first reply from the target
responseToken [2] OCTET STRING OPTIONAL,
mechListMIC [3] OCTET STRING OPTIONAL,
...
}
*/
// NegTokenInit implements Negotiation Token of type Init
type NegTokenInit struct {
MechTypes []asn1.ObjectIdentifier `asn1:"explicit,tag:0"`
ReqFlags ContextFlags `asn1:"explicit,optional,tag:1"`
MechToken []byte `asn1:"explicit,optional,tag:2"`
MechTokenMIC []byte `asn1:"explicit,optional,tag:3"`
}
// NegTokenResp implements Negotiation Token of type Resp/Targ
type NegTokenResp struct {
NegState asn1.Enumerated `asn1:"explicit,tag:0"`
SupportedMech asn1.ObjectIdentifier `asn1:"explicit,optional,tag:1"`
ResponseToken []byte `asn1:"explicit,optional,tag:2"`
MechListMIC []byte `asn1:"explicit,optional,tag:3"`
}
// NegTokenTarg implements Negotiation Token of type Resp/Targ
type NegTokenTarg NegTokenResp
// UnmarshalNegToken umarshals and returns either a NegTokenInit or a NegTokenResp.
//
// The boolean indicates if the response is a NegTokenInit.
// If error is nil and the boolean is false the response is a NegTokenResp.
func UnmarshalNegToken(b []byte) (bool, interface{}, error) {
var a asn1.RawValue
_, err := asn1.Unmarshal(b, &a)
if err != nil {
return false, nil, fmt.Errorf("Error unmarshalling NegotiationToken: %v", err)
}
switch a.Tag {
case 0:
var negToken NegTokenInit
_, err = asn1.Unmarshal(a.Bytes, &negToken)
if err != nil {
return false, nil, fmt.Errorf("Error unmarshalling NegotiationToken type %d (Init): %v", a.Tag, err)
}
return true, negToken, nil
case 1:
var negToken NegTokenResp
_, err = asn1.Unmarshal(a.Bytes, &negToken)
if err != nil {
return false, nil, fmt.Errorf("Error unmarshalling NegotiationToken type %d (Resp/Targ): %v", a.Tag, err)
}
return false, negToken, nil
default:
return false, nil, errors.New("Unknown choice type for NegotiationToken")
}
}
// Marshal an Init negotiation token
func (n *NegTokenInit) Marshal() ([]byte, error) {
b, err := asn1.Marshal(*n)
if err != nil {
return nil, err
}
nt := asn1.RawValue{
Tag: 0,
Class: 2,
IsCompound: true,
Bytes: b,
}
nb, err := asn1.Marshal(nt)
if err != nil {
return nil, err
}
return nb, nil
}
// Marshal a Resp/Targ negotiation token
func (n *NegTokenResp) Marshal() ([]byte, error) {
b, err := asn1.Marshal(*n)
if err != nil {
return nil, err
}
nt := asn1.RawValue{
Tag: 1,
Class: 2,
IsCompound: true,
Bytes: b,
}
nb, err := asn1.Marshal(nt)
if err != nil {
return nil, err
}
return nb, nil
}
// NewNegTokenInitKrb5 creates new Init negotiation token for Kerberos 5
func NewNegTokenInitKrb5(creds credentials.Credentials, tkt messages.Ticket, sessionKey types.EncryptionKey) (NegTokenInit, error) {
mt, err := NewAPREQMechToken(creds, tkt, sessionKey, []int{GSS_C_INTEG_FLAG, GSS_C_CONF_FLAG}, []int{})
if err != nil {
return NegTokenInit{}, fmt.Errorf("error getting MechToken; %v", err)
}
mtb, err := mt.Marshal()
if err != nil {
return NegTokenInit{}, fmt.Errorf("error marshalling MechToken; %v", err)
}
return NegTokenInit{
MechTypes: []asn1.ObjectIdentifier{MechTypeOIDKRB5},
MechToken: mtb,
}, nil
}