-
Notifications
You must be signed in to change notification settings - Fork 25
/
context.go
372 lines (327 loc) · 8.28 KB
/
context.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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
// Copyright 2013-2015 Apcera Inc. All rights reserved.
package gssapi
// This file provides GSSContext methods
/*
#include <gssapi/gssapi.h>
OM_uint32
wrap_gss_init_sec_context(void *fp,
OM_uint32 * minor_status,
const gss_cred_id_t initiator_cred_handle,
gss_ctx_id_t * context_handle,
const gss_name_t target_name,
const gss_OID mech_type,
OM_uint32 req_flags,
OM_uint32 time_req,
const gss_channel_bindings_t input_chan_bindings,
const gss_buffer_t input_token,
gss_OID * actual_mech_type,
gss_buffer_t output_token,
OM_uint32 * ret_flags,
OM_uint32 * time_rec)
{
return ((OM_uint32(*) (
OM_uint32 *,
const gss_cred_id_t,
gss_ctx_id_t *,
const gss_name_t,
const gss_OID,
OM_uint32,
OM_uint32,
const gss_channel_bindings_t,
const gss_buffer_t,
gss_OID *,
gss_buffer_t,
OM_uint32 *,
OM_uint32 *)
) fp)(
minor_status,
initiator_cred_handle,
context_handle,
target_name,
mech_type,
req_flags,
time_req,
input_chan_bindings,
input_token,
actual_mech_type,
output_token,
ret_flags,
time_rec);
}
OM_uint32
wrap_gss_accept_sec_context(void *fp,
OM_uint32 * minor_status,
gss_ctx_id_t * context_handle,
const gss_cred_id_t acceptor_cred_handle,
const gss_buffer_t input_token_buffer,
const gss_channel_bindings_t input_chan_bindings,
gss_name_t * src_name,
gss_OID * mech_type,
gss_buffer_t output_token,
OM_uint32 * ret_flags,
OM_uint32 * time_rec,
gss_cred_id_t * delegated_cred_handle)
{
return ((OM_uint32(*) (
OM_uint32 *,
gss_ctx_id_t *,
const gss_cred_id_t,
const gss_buffer_t,
const gss_channel_bindings_t,
gss_name_t *,
gss_OID *,
gss_buffer_t,
OM_uint32 *,
OM_uint32 *,
gss_cred_id_t *)
) fp)(
minor_status,
context_handle,
acceptor_cred_handle,
input_token_buffer,
input_chan_bindings,
src_name,
mech_type,
output_token,
ret_flags,
time_rec,
delegated_cred_handle);
}
OM_uint32
wrap_gss_delete_sec_context(void *fp,
OM_uint32 * minor_status,
gss_ctx_id_t * context_handle,
gss_buffer_t output_token)
{
return ((OM_uint32(*) (
OM_uint32 *,
gss_ctx_id_t *,
gss_buffer_t)
) fp)(
minor_status,
context_handle,
output_token);
}
OM_uint32
wrap_gss_inquire_context(void *fp,
OM_uint32 * minor_status,
const gss_ctx_id_t context_handle,
gss_name_t * src_name,
gss_name_t * targ_name,
OM_uint32 * lifetime_rec,
gss_OID * mech_type,
OM_uint32 * ctx_flags,
int * locally_initiated,
int * open)
{
return ((OM_uint32(*) (
OM_uint32 *,
const gss_ctx_id_t,
gss_name_t *,
gss_name_t *,
OM_uint32 *,
gss_OID *,
OM_uint32 *,
int *,
int *)
) fp)(
minor_status,
context_handle,
src_name,
targ_name,
lifetime_rec,
mech_type,
ctx_flags,
locally_initiated,
open);
}
*/
import "C"
import (
"runtime"
"time"
)
func (lib *Lib) NewCtxId() *CtxId {
return &CtxId{
Lib: lib,
}
}
// InitSecContext initiates a security context. Usually invoked by the client.
// A Context (CtxId) describes the state at one end of an authentication
// protocol. May return ErrContinueNeeded if the client is to make another
// iteration of exchanging token with the service
func (lib *Lib) InitSecContext(initiatorCredHandle *CredId, ctxIn *CtxId,
targetName *Name, mechType *OID, reqFlags uint32, timeReq time.Duration,
inputChanBindings ChannelBindings, inputToken *Buffer) (
ctxOut *CtxId, actualMechType *OID, outputToken *Buffer, retFlags uint32,
timeRec time.Duration, err error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
// prepare the input params
C_initiator := C.gss_cred_id_t(nil)
if initiatorCredHandle != nil {
C_initiator = initiatorCredHandle.C_gss_cred_id_t
}
C_mechType := C.gss_OID(nil)
if mechType != nil {
C_mechType = mechType.C_gss_OID
}
C_inputToken := C.gss_buffer_t(nil)
if inputToken != nil {
C_inputToken = inputToken.C_gss_buffer_t
}
// prepare the outputs.
if ctxIn != nil {
ctxCopy := *ctxIn
ctxOut = &ctxCopy
} else {
ctxOut = lib.NewCtxId()
}
min := C.OM_uint32(0)
actualMechType = lib.NewOID()
outputToken, err = lib.MakeBuffer(allocGSSAPI)
if err != nil {
return nil, nil, nil, 0, 0, err
}
flags := C.OM_uint32(0)
timerec := C.OM_uint32(0)
maj := C.wrap_gss_init_sec_context(lib.Fp_gss_init_sec_context,
&min,
C_initiator,
&ctxOut.C_gss_ctx_id_t, // used as both in and out param
targetName.C_gss_name_t,
C_mechType,
C.OM_uint32(reqFlags),
C.OM_uint32(timeReq.Seconds()),
C.gss_channel_bindings_t(inputChanBindings),
C_inputToken,
&actualMechType.C_gss_OID,
outputToken.C_gss_buffer_t,
&flags,
&timerec)
err = lib.stashLastStatus(maj, min)
if err != nil {
return nil, nil, nil, 0, 0, err
}
if MajorStatus(maj).ContinueNeeded() {
err = ErrContinueNeeded
}
return ctxOut, actualMechType, outputToken,
uint32(flags), time.Duration(timerec) * time.Second,
err
}
// AcceptSecContext accepts an initialized security context. Usually called by
// the server. May return ErrContinueNeeded if the client is to make another
// iteration of exchanging token with the service
func (lib *Lib) AcceptSecContext(
ctxIn *CtxId, acceptorCredHandle *CredId, inputToken *Buffer,
inputChanBindings ChannelBindings) (
ctxOut *CtxId, srcName *Name, actualMechType *OID, outputToken *Buffer,
retFlags uint32, timeRec time.Duration, delegatedCredHandle *CredId,
err error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
// prepare the inputs
C_acceptorCredHandle := C.gss_cred_id_t(nil)
if acceptorCredHandle != nil {
C_acceptorCredHandle = acceptorCredHandle.C_gss_cred_id_t
}
C_inputToken := C.gss_buffer_t(nil)
if inputToken != nil {
C_inputToken = inputToken.C_gss_buffer_t
}
// prepare the outputs
if ctxIn != nil {
ctxCopy := *ctxIn
ctxOut = &ctxCopy
} else {
ctxOut = lib.GSS_C_NO_CONTEXT
}
min := C.OM_uint32(0)
srcName = lib.NewName()
actualMechType = lib.NewOID()
outputToken, err = lib.MakeBuffer(allocGSSAPI)
if err != nil {
return nil, nil, nil, nil, 0, 0, nil, err
}
flags := C.OM_uint32(0)
timerec := C.OM_uint32(0)
delegatedCredHandle = lib.NewCredId()
maj := C.wrap_gss_accept_sec_context(lib.Fp_gss_accept_sec_context,
&min,
&ctxOut.C_gss_ctx_id_t, // used as both in and out param
C_acceptorCredHandle,
C_inputToken,
C.gss_channel_bindings_t(inputChanBindings),
&srcName.C_gss_name_t,
&actualMechType.C_gss_OID,
outputToken.C_gss_buffer_t,
&flags,
&timerec,
&delegatedCredHandle.C_gss_cred_id_t)
err = lib.stashLastStatus(maj, min)
if err != nil {
lib.Err("AcceptSecContext: ", err)
return nil, nil, nil, nil, 0, 0, nil, err
}
if MajorStatus(maj).ContinueNeeded() {
err = ErrContinueNeeded
}
return ctxOut, srcName, actualMechType, outputToken, uint32(flags),
time.Duration(timerec) * time.Second, delegatedCredHandle, err
}
// DeleteSecContext frees a security context.
// NB: I decided not to implement the outputToken parameter since its use is no
// longer recommended, and it would have to be Released by the caller
func (ctx *CtxId) DeleteSecContext() error {
if ctx == nil || ctx.C_gss_ctx_id_t == nil {
return nil
}
runtime.LockOSThread()
defer runtime.UnlockOSThread()
min := C.OM_uint32(0)
maj := C.wrap_gss_delete_sec_context(ctx.Fp_gss_delete_sec_context,
&min, &ctx.C_gss_ctx_id_t, nil)
return ctx.stashLastStatus(maj, min)
}
// Release is an alias for DeleteSecContext.
func (ctx *CtxId) Release() error {
return ctx.DeleteSecContext()
}
// InquireContext returns fields about a security context.
func (ctx *CtxId) InquireContext() (
srcName *Name, targetName *Name, lifetimeRec time.Duration, mechType *OID,
ctxFlags uint64, locallyInitiated bool, open bool, err error) {
min := C.OM_uint32(0)
srcName = ctx.NewName()
targetName = ctx.NewName()
rec := C.OM_uint32(0)
mechType = ctx.NewOID()
flags := C.OM_uint32(0)
li := C.int(0)
opn := C.int(0)
maj := C.wrap_gss_inquire_context(ctx.Fp_gss_inquire_context,
&min,
ctx.C_gss_ctx_id_t,
&srcName.C_gss_name_t,
&targetName.C_gss_name_t,
&rec,
&mechType.C_gss_OID,
&flags,
&li,
&opn)
err = ctx.stashLastStatus(maj, min)
if err != nil {
ctx.Err("InquireContext: ", err)
return nil, nil, 0, nil, 0, false, false, err
}
lifetimeRec = time.Duration(rec) * time.Second
ctxFlags = uint64(flags)
if li != 0 {
locallyInitiated = true
}
if opn != 0 {
open = true
}
return srcName, targetName, lifetimeRec, mechType, ctxFlags, locallyInitiated, open, nil
}