aboutsummaryrefslogtreecommitdiff
path: root/src/supplemental/tls/tls_stream.c
blob: d3dd9497c0094145bf2bc5ffa0aae71b4967495a (plain)
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
//
// Copyright 2025 Staysail Systems, Inc. <info@staysail.tech>
// Copyright 2018 Capitar IT Group BV <info@capitar.com>
// Copyright 2019 Devolutions <info@devolutions.net>
//
// This software is supplied under the terms of the MIT License, a
// copy of which should be located in the distribution where this
// file was obtained (LICENSE.txt).  A copy of the license may also be
// found online at https://opensource.org/licenses/MIT.
//

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "../../core/nng_impl.h"

#include "nng/nng.h"
#include "tls_common.h"
#include "tls_engine.h"
#include "tls_stream.h"

static void
tls_bio_stream_free(void *bio)
{
	nng_stream_free(bio);
}

static void
tls_bio_stream_stop(void *bio)
{
	nng_stream_stop(bio);
}

static void
tls_bio_stream_close(void *bio)
{
	nng_stream_close(bio);
}

static void
tls_bio_stream_send(void *bio, nng_aio *aio)
{
	nng_stream_send(bio, aio);
}

static void
tls_bio_stream_recv(void *bio, nng_aio *aio)
{
	nng_stream_recv(bio, aio);
}

static const nni_tls_bio_ops tls_stream_bio = {
	.bio_send  = tls_bio_stream_send,
	.bio_recv  = tls_bio_stream_recv,
	.bio_free  = tls_bio_stream_free,
	.bio_stop  = tls_bio_stream_stop,
	.bio_close = tls_bio_stream_close,
};

static void
tls_stream_reap(void *arg)
{
	tls_stream *ts = arg;

	nni_tls_fini(&ts->conn);
	NNI_FREE_STRUCT(ts);
}

static nni_reap_list tls_stream_reap_list = {
	.rl_offset = offsetof(tls_stream, reap),
	.rl_func   = tls_stream_reap,
};

void
nni_tls_stream_free(void *arg)
{
	tls_stream *ts = arg;

	nni_reap(&tls_stream_reap_list, ts);
}

static void
tls_stream_stop(void *arg)
{
	tls_stream *ts = arg;
	nni_tls_stop(&ts->conn);
}

static void
tls_stream_close(void *arg)
{
	tls_stream *ts = arg;
	nni_tls_close(&ts->conn);
}

static void
tls_stream_send(void *arg, nng_aio *aio)
{
	tls_stream *ts = arg;
	nni_tls_send(&ts->conn, aio);
}

static void
tls_stream_recv(void *arg, nng_aio *aio)
{
	tls_stream *ts = arg;
	nni_tls_recv(&ts->conn, aio);
}

static void
tls_stream_conn_cb(void *arg)
{
	tls_stream  *ts = arg;
	nng_stream  *bio;
	int          rv;
	nng_sockaddr sa;

	if ((rv = nni_aio_result(&ts->conn_aio)) != 0) {
		nni_aio_finish_error(ts->user_aio, rv);
		nni_tls_stream_free(ts);
		return;
	}

	bio = nni_aio_get_output(&ts->conn_aio, 0);
	if ((rv = nng_stream_get_addr(bio, NNG_OPT_REMADDR, &sa)) != 0) {
		nni_aio_finish_error(ts->user_aio, rv);
		nni_tls_stream_free(ts);
		return;
	};

	if ((rv = nni_tls_start(&ts->conn, &tls_stream_bio, bio, &sa)) != 0) {
		// NB: if this fails, it *will* have set the bio either way.
		// So nni_tls_stream_free will also free the bio.
		nni_aio_finish_error(ts->user_aio, rv);
		nni_tls_stream_free(ts);
		return;
	}

	nni_aio_set_output(ts->user_aio, 0, &ts->stream);
	nni_aio_finish(ts->user_aio, 0, 0);
}

static nng_err tls_stream_get(
    void *arg, const char *name, void *buf, size_t *szp, nni_type t);
static nng_err tls_stream_peer_cert(void *arg, nng_tls_cert **);

int
nni_tls_stream_alloc(tls_stream **tsp, nng_tls_config *cfg, nng_aio *user_aio)
{
	tls_stream *ts;
	size_t      size;
	int         rv;

	size = NNI_ALIGN_UP(sizeof(*ts)) +
	    NNI_ALIGN_UP(nni_tls_engine_conn_size());

	if ((ts = nni_zalloc(size)) == NULL) {
		return (NNG_ENOMEM);
	}

	ts->user_aio = user_aio;

	// NB: free is exposed for benefit of dialer/listener
	ts->stream.s_free      = nni_tls_stream_free;
	ts->stream.s_close     = tls_stream_close;
	ts->stream.s_stop      = tls_stream_stop;
	ts->stream.s_send      = tls_stream_send;
	ts->stream.s_recv      = tls_stream_recv;
	ts->stream.s_get       = tls_stream_get;
	ts->stream.s_peer_cert = tls_stream_peer_cert;

	nni_aio_init(&ts->conn_aio, tls_stream_conn_cb, ts);

	if ((rv = nni_tls_init(&ts->conn, cfg)) != 0) {
		nni_tls_stream_free(ts);
		return (rv);
	}

	*tsp = ts;
	return (0);
}

static nng_err
tls_get_verified(void *arg, void *buf, size_t *szp, nni_type t)
{
	tls_stream *ts = arg;

	return (nni_copyout_bool(nni_tls_verified(&ts->conn), buf, szp, t));
}

static nng_err
tls_get_peer_cn(void *arg, void *buf, size_t *szp, nni_type t)
{
	NNI_ARG_UNUSED(szp);

	if (t != NNI_TYPE_STRING) {
		return (NNG_EBADTYPE);
	}

	tls_stream *ts = arg;
	*(char **) buf = (char *) nni_tls_peer_cn(&ts->conn);
	return (NNG_OK);
}

static nng_err
tls_stream_peer_cert(void *arg, nng_tls_cert **certp)
{
	tls_stream *ts = arg;
	return (nni_tls_peer_cert(&ts->conn, certp));
}

static const nni_option tls_stream_options[] = {
	{
	    .o_name = NNG_OPT_TLS_VERIFIED,
	    .o_get  = tls_get_verified,
	},
	{
	    .o_name = NNG_OPT_TLS_PEER_CN,
	    .o_get  = tls_get_peer_cn,
	},
	{
	    .o_name = NULL,
	},
};

static nng_err
tls_stream_get(void *arg, const char *name, void *buf, size_t *szp, nni_type t)
{
	tls_stream *ts = arg;
	nng_err     rv;

	if ((rv = nni_stream_get(ts->conn.bio, name, buf, szp, t)) !=
	    NNG_ENOTSUP) {
		return (rv);
	}
	return (nni_getopt(tls_stream_options, name, ts, buf, szp, t));
}