-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathssl.go
249 lines (205 loc) · 7.28 KB
/
ssl.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
/* ssl.go
*
* Copyright (C) 2006-2022 wolfSSL Inc.
*
* This file is part of wolfSSL.
*
* wolfSSL is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* wolfSSL is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
*/
package wolfSSL
// #include <wolfssl/options.h>
// #include <wolfssl/ssl.h>
// #ifdef NO_PSK
// typedef unsigned int (*pskCb)();
// int wolfSSL_CTX_use_psk_identity_hint(WOLFSSL_CTX* ctx, const char* hint) {
// return -174;
// }
// void wolfSSL_CTX_set_psk_server_callback(WOLFSSL_CTX* ctx, pskCb cb) {}
// void wolfSSL_CTX_set_psk_client_callback(WOLFSSL_CTX* ctx, pskCb cb) {}
// void wolfSSL_CTX_set_psk_server_tls13_callback(WOLFSSL_CTX* ctx, pskCb cb) {}
// void wolfSSL_CTX_set_psk_client_tls13_callback(WOLFSSL_CTX* ctx, pskCb cb) {}
// #endif
// #ifndef WOLFSSL_DTLS
// WOLFSSL_METHOD* wolfDTLSv1_2_server_method(void) {
// return NULL;
// }
// WOLFSSL_METHOD* wolfDTLSv1_2_client_method(void) {
// return NULL;
// }
// void* wolfSSL_dtls_create_peer(int port, char* ip) {
// return NULL;
// }
// int wolfSSL_dtls_free_peer(void* addr) {
// return -174;
// }
// #endif
// #ifndef WOLFSSL_DTLS13
// WOLFSSL_METHOD* wolfDTLSv1_3_server_method(void) {
// return NULL;
// }
// WOLFSSL_METHOD* wolfDTLSv1_3_client_method(void) {
// return NULL;
// }
// #endif
// #ifndef HAVE_WRITE_DUP
// WOLFSSL* wolfSSL_write_dup(WOLFSSL* ssl) {
// return NULL;
// }
// #endif
import "C"
import (
"unsafe"
)
const SSL_FILETYPE_PEM = 1
const WOLFSSL_SUCCESS = 1
type WOLFSSL = C.struct_WOLFSSL
type WOLFSSL_CTX = C.struct_WOLFSSL_CTX
func WolfSSL_Init() {
C.wolfSSL_Init()
}
func WolfSSL_Cleanup() {
C.wolfSSL_Cleanup()
}
func WolfSSL_CTX_new(method *C.struct_WOLFSSL_METHOD) *C.struct_WOLFSSL_CTX {
return C.wolfSSL_CTX_new(method)
}
func WolfSSL_CTX_free(ctx *C.struct_WOLFSSL_CTX) {
C.wolfSSL_CTX_free(ctx)
}
func WolfSSL_CTX_set_cipher_list(ctx *C.struct_WOLFSSL_CTX, list string) int {
c_list := C.CString(list)
defer C.free(unsafe.Pointer(c_list))
return int(C.wolfSSL_CTX_set_cipher_list(ctx, c_list))
}
func WolfSSL_new(ctx *C.struct_WOLFSSL_CTX) *C.struct_WOLFSSL {
return C.wolfSSL_new(ctx)
}
func WolfSSL_write_dup(ssl *C.struct_WOLFSSL) *C.struct_WOLFSSL {
return C.wolfSSL_write_dup(ssl)
}
func WolfSSL_connect(ssl *C.struct_WOLFSSL) int {
return int(C.wolfSSL_connect(ssl))
}
func WolfSSL_shutdown(ssl *C.struct_WOLFSSL) {
C.wolfSSL_shutdown(ssl)
}
func WolfSSL_free(ssl *C.struct_WOLFSSL) {
C.wolfSSL_free(ssl)
}
func WolfTLSv1_2_server_method() *C.struct_WOLFSSL_METHOD {
return C.wolfTLSv1_2_server_method()
}
func WolfTLSv1_2_client_method() *C.struct_WOLFSSL_METHOD {
return C.wolfTLSv1_2_client_method()
}
func WolfTLSv1_3_server_method() *C.struct_WOLFSSL_METHOD {
return C.wolfTLSv1_3_server_method()
}
func WolfTLSv1_3_client_method() *C.struct_WOLFSSL_METHOD {
return C.wolfTLSv1_3_client_method()
}
func WolfDTLSv1_2_server_method() *C.struct_WOLFSSL_METHOD {
return C.wolfDTLSv1_2_server_method()
}
func WolfDTLSv1_2_client_method() *C.struct_WOLFSSL_METHOD {
return C.wolfDTLSv1_2_client_method()
}
func WolfDTLSv1_3_server_method() *C.struct_WOLFSSL_METHOD {
return C.wolfDTLSv1_3_server_method()
}
func WolfDTLSv1_3_client_method() *C.struct_WOLFSSL_METHOD {
return C.wolfDTLSv1_3_client_method()
}
func WolfSSL_dtls_create_peer(port int, ip string) unsafe.Pointer {
c_ip := C.CString(ip)
defer C.free(unsafe.Pointer(c_ip))
return C.wolfSSL_dtls_create_peer(C.int(port), c_ip)
}
func WolfSSL_dtls_set_peer(ssl *C.struct_WOLFSSL, addr unsafe.Pointer, peerSz int) int {
return int(C.wolfSSL_dtls_set_peer(ssl, addr, C.uint(peerSz)))
}
func WolfSSL_dtls_free_peer(addr unsafe.Pointer) int {
return int(C.wolfSSL_dtls_free_peer(addr))
}
func WolfSSL_CTX_set_psk_server_callback(ctx *C.struct_WOLFSSL_CTX, cb unsafe.Pointer) {
C.wolfSSL_CTX_set_psk_server_callback(ctx, (*[0]byte)(cb))
}
func WolfSSL_CTX_set_psk_client_callback(ctx *C.struct_WOLFSSL_CTX, cb unsafe.Pointer) {
C.wolfSSL_CTX_set_psk_client_callback(ctx, (*[0]byte)(cb))
}
func WolfSSL_CTX_set_psk_server_tls13_callback(ctx *C.struct_WOLFSSL_CTX, cb unsafe.Pointer) {
C.wolfSSL_CTX_set_psk_server_tls13_callback(ctx, (*[0]byte)(cb))
}
func WolfSSL_CTX_set_psk_client_tls13_callback(ctx *C.struct_WOLFSSL_CTX, cb unsafe.Pointer) {
C.wolfSSL_CTX_set_psk_client_tls13_callback(ctx, (*[0]byte)(cb))
}
func WolfSSL_CTX_use_psk_identity_hint(ctx *C.struct_WOLFSSL_CTX, hint string) int {
c_hint := C.CString(hint)
defer C.free(unsafe.Pointer(c_hint))
return int(C.wolfSSL_CTX_use_psk_identity_hint(ctx, c_hint))
}
func WolfSSL_CTX_load_verify_locations(ctx *C.struct_WOLFSSL_CTX, cert string,
path []byte) int {
cert_file := C.CString(cert)
defer C.free(unsafe.Pointer(cert_file))
/* TODO: HANDLE NON NIL PATH */
return int(C.wolfSSL_CTX_load_verify_locations(ctx, cert_file,
(*C.char)(unsafe.Pointer(nil))))
}
func WolfSSL_CTX_use_certificate_file(ctx *C.struct_WOLFSSL_CTX, cert string,
format int) int {
cert_file := C.CString(cert)
defer C.free(unsafe.Pointer(cert_file))
return int(C.wolfSSL_CTX_use_certificate_file(ctx, cert_file, C.int(format)))
}
func WolfSSL_CTX_use_PrivateKey_file(ctx *C.struct_WOLFSSL_CTX, key string,
format int) int {
key_file := C.CString(key)
defer C.free(unsafe.Pointer(key_file))
return int(C.wolfSSL_CTX_use_PrivateKey_file(ctx, key_file, C.int(format)))
}
func WolfSSL_set_fd(ssl *C.struct_WOLFSSL, fd int) {
C.wolfSSL_set_fd(ssl, C.int(fd))
}
func WolfSSL_accept(ssl *C.struct_WOLFSSL) int {
return int(C.wolfSSL_accept(ssl))
}
func WolfSSL_read(ssl *C.struct_WOLFSSL, data []byte, sz uintptr) int {
return int(C.wolfSSL_read(ssl, unsafe.Pointer(&data[0]), C.int(sz)))
}
func WolfSSL_write(ssl *C.struct_WOLFSSL, data []byte, sz uintptr) int {
return int(C.wolfSSL_write(ssl, unsafe.Pointer(&data[0]), C.int(sz)))
}
func WolfSSL_get_error(ssl *C.struct_WOLFSSL, ret int) int {
return int(C.wolfSSL_get_error(ssl, C.int(ret)))
}
func WolfSSL_ERR_error_string(ret int, data []byte) string {
return C.GoString(C.wolfSSL_ERR_error_string(C.ulong(ret), (*C.char)(unsafe.Pointer(&data[0]))))
}
func WolfSSL_get_cipher_name(ssl *C.struct_WOLFSSL) string {
return C.GoString(C.wolfSSL_get_cipher_name(ssl))
}
func WolfSSL_get_version(ssl *C.struct_WOLFSSL) string {
return C.GoString(C.wolfSSL_get_version(ssl))
}
func WolfSSL_lib_version() string {
return C.GoString(C.wolfSSL_lib_version())
}
func WolfSSL_Debugging_ON() {
C.wolfSSL_Debugging_ON()
}
func WolfSSL_Debugging_OFF() {
C.wolfSSL_Debugging_OFF()
}