-
Notifications
You must be signed in to change notification settings - Fork 25
/
credential.go
309 lines (277 loc) · 6.86 KB
/
credential.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
// Copyright 2013 Apcera Inc. All rights reserved.
package gssapi
/*
#include <gssapi/gssapi.h>
OM_uint32
wrap_gss_acquire_cred(void *fp,
OM_uint32 * minor_status,
const gss_name_t desired_name,
OM_uint32 time_req,
const gss_OID_set desired_mechs,
gss_cred_usage_t cred_usage,
gss_cred_id_t * output_cred_handle,
gss_OID_set * actual_mechs,
OM_uint32 * time_rec)
{
return ((OM_uint32(*) (
OM_uint32 *,
const gss_name_t,
OM_uint32,
const gss_OID_set,
gss_cred_usage_t,
gss_cred_id_t *,
gss_OID_set *,
OM_uint32 *)
) fp)(
minor_status,
desired_name,
time_req,
desired_mechs,
cred_usage,
output_cred_handle,
actual_mechs,
time_rec);
}
OM_uint32
wrap_gss_add_cred(void *fp,
OM_uint32 * minor_status,
const gss_cred_id_t input_cred_handle,
const gss_name_t desired_name,
const gss_OID desired_mech,
gss_cred_usage_t cred_usage,
OM_uint32 initiator_time_req,
OM_uint32 acceptor_time_req,
gss_cred_id_t * output_cred_handle,
gss_OID_set * actual_mechs,
OM_uint32 * initiator_time_rec,
OM_uint32 * acceptor_time_rec)
{
return ((OM_uint32(*) (
OM_uint32 *,
const gss_cred_id_t,
const gss_name_t,
const gss_OID,
gss_cred_usage_t,
OM_uint32,
OM_uint32,
gss_cred_id_t *,
gss_OID_set *,
OM_uint32 *,
OM_uint32 *)
) fp)(
minor_status,
input_cred_handle,
desired_name,
desired_mech,
cred_usage,
initiator_time_req,
acceptor_time_req,
output_cred_handle,
actual_mechs,
initiator_time_rec,
acceptor_time_rec);
}
OM_uint32
wrap_gss_inquire_cred (void *fp,
OM_uint32 *minor_status,
const gss_cred_id_t cred_handle,
gss_name_t *name,
OM_uint32 *lifetime,
gss_cred_usage_t *cred_usage,
gss_OID_set *mechanisms )
{
return ((OM_uint32(*) (
OM_uint32 *,
const gss_cred_id_t,
gss_name_t *,
OM_uint32 *,
gss_cred_usage_t *,
gss_OID_set *)
) fp)(
minor_status,
cred_handle,
name,
lifetime,
cred_usage,
mechanisms);
}
OM_uint32
wrap_gss_inquire_cred_by_mech (void *fp,
OM_uint32 *minor_status,
const gss_cred_id_t cred_handle,
const gss_OID mech_type,
gss_name_t *name,
OM_uint32 *initiator_lifetime,
OM_uint32 *acceptor_lifetime,
gss_cred_usage_t *cred_usage )
{
return ((OM_uint32(*) (
OM_uint32 *,
const gss_cred_id_t,
const gss_OID,
gss_name_t *,
OM_uint32 *,
OM_uint32 *,
gss_cred_usage_t *)
) fp)(
minor_status,
cred_handle,
mech_type,
name,
initiator_lifetime,
acceptor_lifetime,
cred_usage);
}
OM_uint32
wrap_gss_release_cred(void *fp,
OM_uint32 * minor_status,
gss_cred_id_t * cred_handle)
{
return ((OM_uint32(*) (
OM_uint32 *,
gss_cred_id_t *)
) fp)(
minor_status,
cred_handle);
}
*/
import "C"
import (
"time"
)
// NewCredId instantiates a new credential.
func (lib *Lib) NewCredId() *CredId {
return &CredId{
Lib: lib,
}
}
// AcquireCred implements gss_acquire_cred API, as per
// https://tools.ietf.org/html/rfc2743#page-31. outputCredHandle, actualMechs
// must be .Release()-ed by the caller
func (lib *Lib) AcquireCred(desiredName *Name, timeReq time.Duration,
desiredMechs *OIDSet, credUsage CredUsage) (outputCredHandle *CredId,
actualMechs *OIDSet, timeRec time.Duration, err error) {
min := C.OM_uint32(0)
actualMechs = lib.NewOIDSet()
outputCredHandle = lib.NewCredId()
timerec := C.OM_uint32(0)
maj := C.wrap_gss_acquire_cred(lib.Fp_gss_acquire_cred,
&min,
desiredName.C_gss_name_t,
C.OM_uint32(timeReq.Seconds()),
desiredMechs.C_gss_OID_set,
C.gss_cred_usage_t(credUsage),
&outputCredHandle.C_gss_cred_id_t,
&actualMechs.C_gss_OID_set,
&timerec)
err = lib.stashLastStatus(maj, min)
if err != nil {
return nil, nil, 0, err
}
return outputCredHandle, actualMechs, time.Duration(timerec) * time.Second, nil
}
// AddCred implements gss_add_cred API, as per
// https://tools.ietf.org/html/rfc2743#page-36. outputCredHandle, actualMechs
// must be .Release()-ed by the caller
func (lib *Lib) AddCred(inputCredHandle *CredId,
desiredName *Name, desiredMech *OID, credUsage CredUsage,
initiatorTimeReq time.Duration, acceptorTimeReq time.Duration) (
outputCredHandle *CredId, actualMechs *OIDSet,
initiatorTimeRec time.Duration, acceptorTimeRec time.Duration,
err error) {
min := C.OM_uint32(0)
actualMechs = lib.NewOIDSet()
outputCredHandle = lib.NewCredId()
initSeconds := C.OM_uint32(0)
acceptSeconds := C.OM_uint32(0)
maj := C.wrap_gss_add_cred(lib.Fp_gss_add_cred,
&min,
inputCredHandle.C_gss_cred_id_t,
desiredName.C_gss_name_t,
desiredMech.C_gss_OID,
C.gss_cred_usage_t(credUsage),
C.OM_uint32(initiatorTimeReq.Seconds()),
C.OM_uint32(acceptorTimeReq.Seconds()),
&outputCredHandle.C_gss_cred_id_t,
&actualMechs.C_gss_OID_set,
&initSeconds,
&acceptSeconds)
err = lib.stashLastStatus(maj, min)
if err != nil {
return nil, nil, 0, 0, err
}
return outputCredHandle,
actualMechs,
time.Duration(initSeconds) * time.Second,
time.Duration(acceptSeconds) * time.Second,
nil
}
// InquireCred implements gss_inquire_cred API, as per
// https://tools.ietf.org/html/rfc2743#page-34. name and mechanisms must be
// .Release()-ed by the caller
func (lib *Lib) InquireCred(credHandle *CredId) (
name *Name, lifetime time.Duration, credUsage CredUsage, mechanisms *OIDSet,
err error) {
min := C.OM_uint32(0)
name = lib.NewName()
life := C.OM_uint32(0)
credUsage = CredUsage(0)
mechanisms = lib.NewOIDSet()
maj := C.wrap_gss_inquire_cred(lib.Fp_gss_inquire_cred,
&min,
credHandle.C_gss_cred_id_t,
&name.C_gss_name_t,
&life,
(*C.gss_cred_usage_t)(&credUsage),
&mechanisms.C_gss_OID_set)
err = lib.stashLastStatus(maj, min)
if err != nil {
return nil, 0, 0, nil, err
}
return name,
time.Duration(life) * time.Second,
credUsage,
mechanisms,
nil
}
// InquireCredByMech implements gss_inquire_cred_by_mech API, as per
// https://tools.ietf.org/html/rfc2743#page-39. name must be .Release()-ed by
// the caller
func (lib *Lib) InquireCredByMech(credHandle *CredId, mechType *OID) (
name *Name, initiatorLifetime time.Duration, acceptorLifetime time.Duration,
credUsage CredUsage, err error) {
min := C.OM_uint32(0)
name = lib.NewName()
ilife := C.OM_uint32(0)
alife := C.OM_uint32(0)
credUsage = CredUsage(0)
maj := C.wrap_gss_inquire_cred_by_mech(lib.Fp_gss_inquire_cred_by_mech,
&min,
credHandle.C_gss_cred_id_t,
mechType.C_gss_OID,
&name.C_gss_name_t,
&ilife,
&alife,
(*C.gss_cred_usage_t)(&credUsage))
err = lib.stashLastStatus(maj, min)
if err != nil {
return nil, 0, 0, 0, err
}
return name,
time.Duration(ilife) * time.Second,
time.Duration(alife) * time.Second,
credUsage,
nil
}
// Release frees a credential.
func (c *CredId) Release() error {
if c == nil || c.C_gss_cred_id_t == nil {
return nil
}
min := C.OM_uint32(0)
maj := C.wrap_gss_release_cred(c.Fp_gss_release_cred,
&min,
&c.C_gss_cred_id_t)
return c.stashLastStatus(maj, min)
}
//TODO: Test for AddCred with existing cred