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
|
// Package tls provides a Go interface to the libtls library.
package tls
/*
#cgo LDFLAGS: -ltls -lssl -lcrypto
#include <stdlib.h>
#include <tls.h>
typedef void *tls;
*/
import "C"
import (
"errors"
"fmt"
"unsafe"
)
// TLSConfig provides configuration options for a TLS context.
type TLSConfig struct {
caFile *C.char
tlsCfg *C.struct_tls_config
}
// TLS encapsulates the TLS context.
type TLS struct {
cfg *TLSConfig
ctx *C.struct_tls
}
// Init initialises the TLS library.
func Init() error {
if C.tls_init() != 0 {
return errors.New("initialisation failed")
}
return nil
}
// NewConfig returns a new TLS configuration.
func NewConfig() (*TLSConfig, error) {
cfg := C.tls_config_new()
if cfg == nil {
return nil, errors.New("failed to allocate config")
}
return &TLSConfig{
tlsCfg: cfg,
}, nil
}
// SetCAFile sets the CA file to be used for connections.
func (c *TLSConfig) SetCAFile(filename string) {
if c.caFile != nil {
C.free(unsafe.Pointer(c.caFile))
}
c.caFile = C.CString(filename)
C.tls_config_set_ca_file(c.tlsCfg, c.caFile)
}
// InsecureNoVerifyCert disables certificate verification for the connection.
func (c *TLSConfig) InsecureNoVerifyCert() {
C.tls_config_insecure_noverifycert(c.tlsCfg)
}
// InsecureNoVerifyName disables server name verification for the connection.
func (c *TLSConfig) InsecureNoVerifyName() {
C.tls_config_insecure_noverifyname(c.tlsCfg)
}
// SetSecure enables verification for the connection.
func (c *TLSConfig) SetVerify() {
C.tls_config_verify(c.tlsCfg)
}
// Free frees resources associated with the TLS configuration.
func (c *TLSConfig) Free() {
if c.tlsCfg == nil {
return
}
C.tls_config_free(c.tlsCfg)
c.tlsCfg = nil
}
// NewClient returns a new TLS client context, using the optional configuration.
// If no configuration is specified the default configuration will be used.
func NewClient(config *TLSConfig) (*TLS, error) {
var sslCfg *C.struct_tls_config
if config != nil {
sslCfg = config.tlsCfg
}
ctx := C.tls_client()
if ctx == nil {
return nil, errors.New("tls client failed")
}
if C.tls_configure(ctx, sslCfg) != 0 {
return nil, errors.New("tls configure failed")
}
return &TLS{
cfg: config,
ctx: ctx,
}, nil
}
// Error returns the error message from the TLS context.
func (t *TLS) Error() string {
if msg := C.tls_error(t.ctx); msg != nil {
return C.GoString(msg)
}
return ""
}
// Connect attempts to establish an TLS connection to the specified host on
// the given port. The host may optionally contain a colon separated port
// value if the port string is specified as an empty string.
func (t *TLS) Connect(host, port string) error {
h := C.CString(host)
var p *C.char
if port != "" {
p = C.CString(port)
}
defer C.free(unsafe.Pointer(h))
defer C.free(unsafe.Pointer(p))
if C.tls_connect(t.ctx, h, p) != 0 {
return fmt.Errorf("connect failed: %v", t.Error())
}
return nil
}
// Read reads data the TLS connection into the given buffer.
func (t *TLS) Read(buf []byte) (int, error) {
var inlen C.size_t
if C.tls_read(t.ctx, unsafe.Pointer(&buf[0]), C.size_t(len(buf)), (*C.size_t)(unsafe.Pointer(&inlen))) != 0 {
return -1, fmt.Errorf("read failed: %v", t.Error())
}
return int(inlen), nil
}
// Write writes the given data to the TLS connection.
func (t *TLS) Write(buf []byte) (int, error) {
var outlen C.size_t
p := C.CString(string(buf))
defer C.free(unsafe.Pointer(p))
if C.tls_write(t.ctx, unsafe.Pointer(p), C.size_t(len(buf)), (*C.size_t)(unsafe.Pointer(&outlen))) != 0 {
return -1, fmt.Errorf("write failed: %v", t.Error())
}
return int(outlen), nil
}
// Close closes the TLS connection.
func (t *TLS) Close() error {
if C.tls_close(t.ctx) != 0 {
return fmt.Errorf("close failed: %v", t.Error())
}
return nil
}
// Free frees resources associated with the TLS context.
func (t *TLS) Free() {
if t.ctx == nil {
return
}
C.tls_free(t.ctx)
t.ctx = nil
}
|