aboutsummaryrefslogtreecommitdiffstats
path: root/cshared/auth_method_cshared.go
blob: 2219edb98d552ef5d89128f26243dc75e700615e (plain) (blame)
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
// +build ignore
package main

import (
	"C"
	"strings"

	"golang.org/x/crypto/ssh"
	"gopkg.in/src-d/go-git.v4/clients/http"
	gssh "gopkg.in/src-d/go-git.v4/clients/ssh"
)

//export c_NewBasicAuth
func c_NewBasicAuth(username, password string) uint64 {
	auth := http.NewBasicAuth(CopyString(username), CopyString(password))
	return uint64(RegisterObject(auth))
}

//export c_ParseRawPrivateKey
func c_ParseRawPrivateKey(pemBytes []byte) (uint64, int, *C.char) {
	pkey, err := ssh.ParseRawPrivateKey(pemBytes)
	if err != nil {
		return IH, ErrorCodeInternal, C.CString(err.Error())
	}
	// pointer is received - no need for &
	return uint64(RegisterObject(pkey)), ErrorCodeSuccess, nil
}

//export c_ParsePrivateKey
func c_ParsePrivateKey(pemBytes []byte) (uint64, int, *C.char) {
	signer, err := ssh.ParsePrivateKey(pemBytes)
	if err != nil {
		return IH, ErrorCodeInternal, C.CString(err.Error())
	}
	return uint64(RegisterObject(&signer)), ErrorCodeSuccess, nil
}

//export c_NewPublicKey
func c_NewPublicKey(key uint64) (uint64, int, *C.char) {
	obj, ok := GetObject(Handle(key))
	if !ok {
		return IH, ErrorCodeNotFound, C.CString(MessageNotFound)
	}
	key_obj := obj.(ssh.PublicKey)
	pkey, err := ssh.NewPublicKey(key_obj)
	if err != nil {
		return IH, ErrorCodeInternal, C.CString(err.Error())
	}
	return uint64(RegisterObject(&pkey)), ErrorCodeSuccess, nil
}

//export c_NewSignerFromKey
func c_NewSignerFromKey(key uint64) (uint64, int, *C.char) {
	obj, ok := GetObject(Handle(key))
	if !ok {
		return IH, ErrorCodeNotFound, C.CString(MessageNotFound)
	}
	signer, err := ssh.NewSignerFromKey(obj)
	if err != nil {
		return IH, ErrorCodeInternal, C.CString(err.Error())
	}
	return uint64(RegisterObject(&signer)), ErrorCodeSuccess, nil
}

//export c_MarshalAuthorizedKey
func c_MarshalAuthorizedKey(key uint64) (*C.char, int) {
	obj, ok := GetObject(Handle(key))
	if !ok {
		return nil, 0
	}
	obj_key := obj.(ssh.PublicKey)
	mak := ssh.MarshalAuthorizedKey(obj_key)
	return C.CString(string(mak)), len(mak)
}

//export c_ParsePublicKey
func c_ParsePublicKey(in []byte) (uint64, int, *C.char) {
	pkey, err := ssh.ParsePublicKey(in)
	if err != nil {
		return IH, ErrorCodeInternal, C.CString(err.Error())
	}
	return uint64(RegisterObject(&pkey)), ErrorCodeSuccess, nil
}

//export c_ParseAuthorizedKey
func c_ParseAuthorizedKey(in []byte) (uint64, *C.char, *C.char, *C.char, int, int, *C.char) {
	pkey, comment, options, rest, err := ssh.ParseAuthorizedKey(in)
	if err != nil {
		return IH, nil, nil, nil, 0, ErrorCodeInternal,
			C.CString(err.Error())
	}
	pkey_handle := RegisterObject(&pkey)
	mopt := strings.Join(options, "\xff")
	return uint64(pkey_handle), C.CString(comment), C.CString(mopt),
		C.CString(string(rest)), len(rest), ErrorCodeSuccess, nil
}

//export c_ssh_Password_New
func c_ssh_Password_New(user, pass string) uint64 {
	obj := gssh.Password{User: CopyString(user), Pass: CopyString(pass)}
	return uint64(RegisterObject(&obj))
}

//export c_ssh_Password_get_User
func c_ssh_Password_get_User(p uint64) *C.char {
	obj, ok := GetObject(Handle(p))
	if !ok {
		return nil
	}
	return C.CString(obj.(*gssh.Password).User)
}

//export c_ssh_Password_set_User
func c_ssh_Password_set_User(p uint64, v string) {
	obj, ok := GetObject(Handle(p))
	if !ok {
		return
	}
	obj.(*gssh.Password).User = CopyString(v)
}

//export c_ssh_Password_get_Pass
func c_ssh_Password_get_Pass(p uint64) *C.char {
	obj, ok := GetObject(Handle(p))
	if !ok {
		return nil
	}
	return C.CString(obj.(*gssh.Password).Pass)
}

//export c_ssh_Password_set_Pass
func c_ssh_Password_set_Pass(p uint64, v string) {
	obj, ok := GetObject(Handle(p))
	if !ok {
		return
	}
	obj.(*gssh.Password).Pass = CopyString(v)
}

//c_ssh_PublicKeys_New
func c_ssh_PublicKeys_New(user string, signer uint64) uint64 {
	obj, ok := GetObject(Handle(signer))
	if !ok {
		return IH
	}
	pk := gssh.PublicKeys{User: CopyString(user), Signer: obj.(ssh.Signer)}
	return uint64(RegisterObject(&pk))
}

//export c_ssh_PublicKeys_get_User
func c_ssh_PublicKeys_get_User(p uint64) *C.char {
	obj, ok := GetObject(Handle(p))
	if !ok {
		return nil
	}
	return C.CString(obj.(*gssh.PublicKeys).User)
}

//export c_ssh_PublicKeys_set_User
func c_ssh_PublicKeys_set_User(p uint64, v string) {
	obj, ok := GetObject(Handle(p))
	if !ok {
		return
	}
	obj.(*gssh.PublicKeys).User = CopyString(v)
}

//export c_ssh_PublicKeys_get_Signer
func c_ssh_PublicKeys_get_Signer(p uint64) uint64 {
	obj, ok := GetObject(Handle(p))
	if !ok {
		return IH
	}
	handle, ok := GetHandle(&obj.(*gssh.PublicKeys).Signer)
	if !ok {
		return IH
	}
	return uint64(handle)
}

//export c_ssh_PublicKeys_set_Signer
func c_ssh_PublicKeys_set_Signer(p uint64, v uint64) {
	obj, ok := GetObject(Handle(p))
	if !ok {
		return
	}
	signer, ok := GetObject(Handle(v))
	if !ok {
		return
	}
	obj.(*gssh.PublicKeys).Signer = *signer.(*ssh.Signer)
}