forked from DeziderMesko/gssapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
oid.go
159 lines (130 loc) · 4.35 KB
/
oid.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
// Copyright 2013-2015 Apcera Inc. All rights reserved.
package gssapi
/*
#cgo CFLAGS: -I../../../../../installer/krb5/include
#include <stdlib.h>
#include <string.h>
#include <gssapi/gssapi.h>
const size_t gss_OID_size=sizeof(gss_OID_desc);
void helper_gss_OID_desc_free_elements(gss_OID oid) {
free(oid->elements);
}
void helper_gss_OID_desc_set_elements(gss_OID oid, OM_uint32 l, void *p) {
oid->length = l;
oid->elements = p;
}
void helper_gss_OID_desc_get_elements(gss_OID oid, OM_uint32 *l, char **p) {
*l = oid->length;
*p = oid->elements;
}
int
wrap_gss_oid_equal(void *fp, gss_OID oid1, gss_OID oid2)
{
return ((int(*) (gss_OID, gss_OID)) fp)(oid1, oid2);
}
*/
import "C"
import (
"bytes"
"fmt"
"unsafe"
)
// NewOID initializes a new OID. (Object Identifier)
func (lib *Lib) NewOID() *OID {
return &OID{Lib: lib}
}
// MakeOIDBytes makes an OID encapsulating a byte slice. Note that it does not
// duplicate the data, but rather it points to it directly.
func (lib *Lib) MakeOIDBytes(data []byte) (*OID, error) {
oid := lib.NewOID()
s := C.malloc(C.size_t(C.gss_OID_size)) // s for struct
if s == nil {
return nil, ErrMallocFailed
}
C.memset(s, 0, C.size_t(C.gss_OID_size))
l := C.size_t(len(data))
e := C.malloc(l) // c for contents
if e == nil {
return nil, ErrMallocFailed
}
C.memmove(e, (unsafe.Pointer)(&data[0]), l)
oid.C_gss_OID = C.gss_OID(s)
oid.alloc = allocMalloc
// because of the alignment issues I can't access o.oid's fields from go,
// so invoking a C function to do the same as:
// oid.C_gss_OID.length = l
// oid.C_gss_OID.elements = c
C.helper_gss_OID_desc_set_elements(oid.C_gss_OID, C.OM_uint32(l), e)
return oid, nil
}
// MakeOIDString makes an OID from a string.
func (lib *Lib) MakeOIDString(data string) (*OID, error) {
return lib.MakeOIDBytes([]byte(data))
}
// Release safely frees the contents of an OID if it's allocated with malloc by
// MakeOIDBytes.
func (oid *OID) Release() error {
if oid == nil || oid.C_gss_OID == nil {
return nil
}
switch oid.alloc {
case allocMalloc:
// same as with get and set, use a C helper to free(oid.C_gss_OID.elements)
C.helper_gss_OID_desc_free_elements(oid.C_gss_OID)
C.free(unsafe.Pointer(oid.C_gss_OID))
oid.C_gss_OID = nil
oid.alloc = allocNone
}
return nil
}
// Bytes displays the bytes of an OID.
func (oid OID) Bytes() []byte {
var l C.OM_uint32
var p *C.char
C.helper_gss_OID_desc_get_elements(oid.C_gss_OID, &l, &p)
return C.GoBytes(unsafe.Pointer(p), C.int(l))
}
// String displays a string representation of an OID.
func (oid *OID) String() string {
var l C.OM_uint32
var p *C.char
C.helper_gss_OID_desc_get_elements(oid.C_gss_OID, &l, &p)
return fmt.Sprintf(`%x`, C.GoStringN(p, C.int(l)))
}
// Returns a symbolic name for a known OID, or the string. Note that this
// function is intended for debugging and is not at all performant.
func (oid *OID) DebugString() string {
switch {
case bytes.Equal(oid.Bytes(), oid.GSS_C_NT_USER_NAME.Bytes()):
return "GSS_C_NT_USER_NAME"
case bytes.Equal(oid.Bytes(), oid.GSS_C_NT_MACHINE_UID_NAME.Bytes()):
return "GSS_C_NT_MACHINE_UID_NAME"
case bytes.Equal(oid.Bytes(), oid.GSS_C_NT_STRING_UID_NAME.Bytes()):
return "GSS_C_NT_STRING_UID_NAME"
case bytes.Equal(oid.Bytes(), oid.GSS_C_NT_HOSTBASED_SERVICE_X.Bytes()):
return "GSS_C_NT_HOSTBASED_SERVICE_X"
case bytes.Equal(oid.Bytes(), oid.GSS_C_NT_HOSTBASED_SERVICE.Bytes()):
return "GSS_C_NT_HOSTBASED_SERVICE"
case bytes.Equal(oid.Bytes(), oid.GSS_C_NT_ANONYMOUS.Bytes()):
return "GSS_C_NT_ANONYMOUS"
case bytes.Equal(oid.Bytes(), oid.GSS_C_NT_EXPORT_NAME.Bytes()):
return "GSS_C_NT_EXPORT_NAME"
case bytes.Equal(oid.Bytes(), oid.GSS_KRB5_NT_PRINCIPAL_NAME.Bytes()):
return "GSS_KRB5_NT_PRINCIPAL_NAME"
case bytes.Equal(oid.Bytes(), oid.GSS_KRB5_NT_PRINCIPAL.Bytes()):
return "GSS_KRB5_NT_PRINCIPAL"
case bytes.Equal(oid.Bytes(), oid.GSS_MECH_KRB5.Bytes()):
return "GSS_MECH_KRB5"
case bytes.Equal(oid.Bytes(), oid.GSS_MECH_KRB5_LEGACY.Bytes()):
return "GSS_MECH_KRB5_LEGACY"
case bytes.Equal(oid.Bytes(), oid.GSS_MECH_KRB5_OLD.Bytes()):
return "GSS_MECH_KRB5_OLD"
case bytes.Equal(oid.Bytes(), oid.GSS_MECH_SPNEGO.Bytes()):
return "GSS_MECH_SPNEGO"
case bytes.Equal(oid.Bytes(), oid.GSS_MECH_IAKERB.Bytes()):
return "GSS_MECH_IAKERB"
case bytes.Equal(oid.Bytes(), oid.GSS_MECH_NTLMSSP.Bytes()):
return "GSS_MECH_NTLMSSP"
}
return oid.String()
}