blob: 73f8cee47e4dcc22133e3d671fad5315c82aecc5 [file] [log] [blame]
Radek Krejci9f03b482015-10-22 16:02:10 +02001/**
Michal Vasko95ea9ff2021-11-09 12:29:14 +01002 * @file session_client_tls.c
3 * @author Radek Krejci <rkrejci@cesnet.cz>
4 * @author Michal Vasko <mvasko@cesnet.cz>
5 * @brief libnetconf2 - TLS specific session client transport functions
Radek Krejci9f03b482015-10-22 16:02:10 +02006 *
Michal Vasko11d4cdb2015-10-29 11:42:52 +01007 * This source is compiled only with libssl.
Radek Krejci9f03b482015-10-22 16:02:10 +02008 *
Michal Vasko95ea9ff2021-11-09 12:29:14 +01009 * @copyright
Radek Krejci9f03b482015-10-22 16:02:10 +020010 * Copyright (c) 2015 CESNET, z.s.p.o.
11 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +010012 * This source code is licensed under BSD 3-Clause License (the "License").
13 * You may not use this file except in compliance with the License.
14 * You may obtain a copy of the License at
Michal Vaskoafd416b2016-02-25 14:51:46 +010015 *
Radek Krejci9b81f5b2016-02-24 13:14:49 +010016 * https://opensource.org/licenses/BSD-3-Clause
Radek Krejci9f03b482015-10-22 16:02:10 +020017 */
18
19#include <assert.h>
20#include <errno.h>
Radek Krejci9f03b482015-10-22 16:02:10 +020021#include <string.h>
22#include <unistd.h>
23
24#include <libyang/libyang.h>
Michal Vasko086311b2016-01-08 09:53:11 +010025#include <openssl/err.h>
Michal Vaskob83a3fa2021-05-26 09:53:42 +020026#include <openssl/ossl_typ.h>
Michal Vaskodae48322016-02-25 14:49:48 +010027#include <openssl/x509.h>
Radek Krejci9f03b482015-10-22 16:02:10 +020028
Michal Vaskob83a3fa2021-05-26 09:53:42 +020029#include "libnetconf.h"
Michal Vaskoe22c6732016-01-29 11:03:02 +010030#include "session_client.h"
31#include "session_client_ch.h"
Michal Vasko11d4cdb2015-10-29 11:42:52 +010032
Rosen Penev4f552d62019-06-26 16:10:43 -070033#if OPENSSL_VERSION_NUMBER < 0x10100000L
34#define X509_STORE_CTX_get_by_subject X509_STORE_get_by_subject
35#endif
36
Radek Krejci62aa0642017-05-25 16:33:49 +020037struct nc_client_context *nc_client_context_location(void);
38
39#define client_opts nc_client_context_location()->opts
40#define tls_opts nc_client_context_location()->tls_opts
41#define tls_ch_opts nc_client_context_location()->tls_ch_opts
Michal Vasko3031aae2016-01-27 16:07:18 +010042
43static int tlsauth_ch;
Radek Krejci9f03b482015-10-22 16:02:10 +020044
Michal Vasko18aeb5d2017-02-17 09:23:56 +010045#if OPENSSL_VERSION_NUMBER >= 0x10100000L // >= 1.1.0
46
47static int
48tlsauth_verify_callback(int preverify_ok, X509_STORE_CTX *x509_ctx)
49{
50 X509_STORE_CTX *store_ctx;
51 X509_OBJECT *obj;
52 X509_NAME *subject, *issuer;
53 X509 *cert;
54 X509_CRL *crl;
55 X509_REVOKED *revoked;
56 EVP_PKEY *pubkey;
57 int i, n, rc;
58 const ASN1_TIME *next_update = NULL;
59 struct nc_client_tls_opts *opts;
60
61 if (!preverify_ok) {
62 return 0;
63 }
64
65 opts = (tlsauth_ch ? &tls_ch_opts : &tls_opts);
66
67 if (!opts->crl_store) {
68 /* nothing to check */
69 return 1;
70 }
71
72 cert = X509_STORE_CTX_get_current_cert(x509_ctx);
73 subject = X509_get_subject_name(cert);
74 issuer = X509_get_issuer_name(cert);
75
76 /* try to retrieve a CRL corresponding to the _subject_ of
77 * the current certificate in order to verify it's integrity */
78 store_ctx = X509_STORE_CTX_new();
79 obj = X509_OBJECT_new();
80 X509_STORE_CTX_init(store_ctx, opts->crl_store, NULL, NULL);
Rosen Penev4f552d62019-06-26 16:10:43 -070081 rc = X509_STORE_CTX_get_by_subject(store_ctx, X509_LU_CRL, subject, obj);
Michal Vasko18aeb5d2017-02-17 09:23:56 +010082 X509_STORE_CTX_free(store_ctx);
83 crl = X509_OBJECT_get0_X509_CRL(obj);
Michal Vaskob83a3fa2021-05-26 09:53:42 +020084 if ((rc > 0) && crl) {
Michal Vasko18aeb5d2017-02-17 09:23:56 +010085 next_update = X509_CRL_get0_nextUpdate(crl);
86
87 /* verify the signature on this CRL */
88 pubkey = X509_get_pubkey(cert);
89 if (X509_CRL_verify(crl, pubkey) <= 0) {
90 X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_CRL_SIGNATURE_FAILURE);
91 X509_OBJECT_free(obj);
92 if (pubkey) {
93 EVP_PKEY_free(pubkey);
94 }
95 return 0; /* fail */
96 }
97 if (pubkey) {
98 EVP_PKEY_free(pubkey);
99 }
100
101 /* check date of CRL to make sure it's not expired */
102 if (!next_update) {
103 X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD);
104 X509_OBJECT_free(obj);
105 return 0; /* fail */
106 }
107 if (X509_cmp_current_time(next_update) < 0) {
108 X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_CRL_HAS_EXPIRED);
109 X509_OBJECT_free(obj);
110 return 0; /* fail */
111 }
112 X509_OBJECT_free(obj);
113 }
114
115 /* try to retrieve a CRL corresponding to the _issuer_ of
116 * the current certificate in order to check for revocation */
117 store_ctx = X509_STORE_CTX_new();
118 obj = X509_OBJECT_new();
119 X509_STORE_CTX_init(store_ctx, opts->crl_store, NULL, NULL);
Rosen Penev4f552d62019-06-26 16:10:43 -0700120 rc = X509_STORE_CTX_get_by_subject(store_ctx, X509_LU_CRL, issuer, obj);
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100121 X509_STORE_CTX_free(store_ctx);
122 crl = X509_OBJECT_get0_X509_CRL(obj);
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200123 if ((rc > 0) && crl) {
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100124 /* check if the current certificate is revoked by this CRL */
125 n = sk_X509_REVOKED_num(X509_CRL_get_REVOKED(crl));
126 for (i = 0; i < n; i++) {
127 revoked = sk_X509_REVOKED_value(X509_CRL_get_REVOKED(crl), i);
128 if (ASN1_INTEGER_cmp(X509_REVOKED_get0_serialNumber(revoked), X509_get_serialNumber(cert)) == 0) {
Michal Vasko05532772021-06-03 12:12:38 +0200129 ERR(NULL, "Certificate revoked!");
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100130 X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_CERT_REVOKED);
131 X509_OBJECT_free(obj);
132 return 0; /* fail */
133 }
134 }
135 X509_OBJECT_free(obj);
136 }
137
138 return 1; /* success */
139}
140
141#else
142
Radek Krejci9f03b482015-10-22 16:02:10 +0200143static int
Michal Vasko11d4cdb2015-10-29 11:42:52 +0100144tlsauth_verify_callback(int preverify_ok, X509_STORE_CTX *x509_ctx)
Radek Krejci9f03b482015-10-22 16:02:10 +0200145{
Michal Vasko11d4cdb2015-10-29 11:42:52 +0100146 X509_STORE_CTX store_ctx;
147 X509_OBJECT obj;
148 X509_NAME *subject, *issuer;
149 X509 *cert;
150 X509_CRL *crl;
151 X509_REVOKED *revoked;
152 EVP_PKEY *pubkey;
153 int i, n, rc;
154 ASN1_TIME *next_update = NULL;
Michal Vasko3031aae2016-01-27 16:07:18 +0100155 struct nc_client_tls_opts *opts;
Radek Krejci9f03b482015-10-22 16:02:10 +0200156
Michal Vasko11d4cdb2015-10-29 11:42:52 +0100157 if (!preverify_ok) {
158 return 0;
159 }
Radek Krejci9f03b482015-10-22 16:02:10 +0200160
Michal Vasko3031aae2016-01-27 16:07:18 +0100161 opts = (tlsauth_ch ? &tls_ch_opts : &tls_opts);
162
163 if (!opts->crl_store) {
164 /* nothing to check */
165 return 1;
166 }
167
Michal Vasko11d4cdb2015-10-29 11:42:52 +0100168 cert = X509_STORE_CTX_get_current_cert(x509_ctx);
169 subject = X509_get_subject_name(cert);
170 issuer = X509_get_issuer_name(cert);
171
172 /* try to retrieve a CRL corresponding to the _subject_ of
173 * the current certificate in order to verify it's integrity */
174 memset((char *)&obj, 0, sizeof obj);
Michal Vasko3031aae2016-01-27 16:07:18 +0100175 X509_STORE_CTX_init(&store_ctx, opts->crl_store, NULL, NULL);
Rosen Penev4f552d62019-06-26 16:10:43 -0700176 rc = X509_STORE_CTX_get_by_subject(&store_ctx, X509_LU_CRL, subject, &obj);
Michal Vasko11d4cdb2015-10-29 11:42:52 +0100177 X509_STORE_CTX_cleanup(&store_ctx);
178 crl = obj.data.crl;
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200179 if ((rc > 0) && crl) {
Michal Vasko11d4cdb2015-10-29 11:42:52 +0100180 next_update = X509_CRL_get_nextUpdate(crl);
181
182 /* verify the signature on this CRL */
183 pubkey = X509_get_pubkey(cert);
184 if (X509_CRL_verify(crl, pubkey) <= 0) {
185 X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_CRL_SIGNATURE_FAILURE);
186 X509_OBJECT_free_contents(&obj);
187 if (pubkey) {
188 EVP_PKEY_free(pubkey);
189 }
190 return 0; /* fail */
191 }
192 if (pubkey) {
193 EVP_PKEY_free(pubkey);
194 }
195
196 /* check date of CRL to make sure it's not expired */
197 if (!next_update) {
198 X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD);
199 X509_OBJECT_free_contents(&obj);
200 return 0; /* fail */
201 }
202 if (X509_cmp_current_time(next_update) < 0) {
203 X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_CRL_HAS_EXPIRED);
204 X509_OBJECT_free_contents(&obj);
205 return 0; /* fail */
206 }
207 X509_OBJECT_free_contents(&obj);
208 }
209
210 /* try to retrieve a CRL corresponding to the _issuer_ of
211 * the current certificate in order to check for revocation */
212 memset((char *)&obj, 0, sizeof obj);
Michal Vasko3031aae2016-01-27 16:07:18 +0100213 X509_STORE_CTX_init(&store_ctx, opts->crl_store, NULL, NULL);
Rosen Penev4f552d62019-06-26 16:10:43 -0700214 rc = X509_STORE_CTX_get_by_subject(&store_ctx, X509_LU_CRL, issuer, &obj);
Michal Vasko11d4cdb2015-10-29 11:42:52 +0100215 X509_STORE_CTX_cleanup(&store_ctx);
216 crl = obj.data.crl;
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200217 if ((rc > 0) && crl) {
Michal Vasko11d4cdb2015-10-29 11:42:52 +0100218 /* check if the current certificate is revoked by this CRL */
219 n = sk_X509_REVOKED_num(X509_CRL_get_REVOKED(crl));
220 for (i = 0; i < n; i++) {
221 revoked = sk_X509_REVOKED_value(X509_CRL_get_REVOKED(crl), i);
222 if (ASN1_INTEGER_cmp(revoked->serialNumber, X509_get_serialNumber(cert)) == 0) {
Michal Vasko05532772021-06-03 12:12:38 +0200223 ERR(NULL, "Certificate revoked!");
Michal Vasko11d4cdb2015-10-29 11:42:52 +0100224 X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_CERT_REVOKED);
225 X509_OBJECT_free_contents(&obj);
226 return 0; /* fail */
227 }
228 }
229 X509_OBJECT_free_contents(&obj);
230 }
231
232 return 1; /* success */
233}
234
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100235#endif
236
Kevin Barrettd37d2fb2019-08-28 14:25:34 -0400237void
Michal Vaskoe22c6732016-01-29 11:03:02 +0100238_nc_client_tls_destroy_opts(struct nc_client_tls_opts *opts)
239{
240 free(opts->cert_path);
241 free(opts->key_path);
242 free(opts->ca_file);
243 free(opts->ca_dir);
244 SSL_CTX_free(opts->tls_ctx);
245
246 free(opts->crl_file);
247 free(opts->crl_dir);
248 X509_STORE_free(opts->crl_store);
Radek Krejci5cebc6b2017-05-26 13:24:38 +0200249
250 memset(opts, 0, sizeof *opts);
Michal Vaskoe22c6732016-01-29 11:03:02 +0100251}
252
Michal Vaskob7558c52016-02-26 15:04:19 +0100253void
Michal Vaskoe22c6732016-01-29 11:03:02 +0100254nc_client_tls_destroy_opts(void)
255{
256 _nc_client_tls_destroy_opts(&tls_opts);
257 _nc_client_tls_destroy_opts(&tls_ch_opts);
258}
259
Michal Vasko3031aae2016-01-27 16:07:18 +0100260static int
Michal Vaskoe22c6732016-01-29 11:03:02 +0100261_nc_client_tls_set_cert_key_paths(const char *client_cert, const char *client_key, struct nc_client_tls_opts *opts)
Michal Vasko11d4cdb2015-10-29 11:42:52 +0100262{
Michal Vasko11d4cdb2015-10-29 11:42:52 +0100263 if (!client_cert) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200264 ERRARG("client_cert");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100265 return -1;
Michal Vasko11d4cdb2015-10-29 11:42:52 +0100266 }
267
Michal Vasko3031aae2016-01-27 16:07:18 +0100268 free(opts->cert_path);
269 free(opts->key_path);
Michal Vasko11d4cdb2015-10-29 11:42:52 +0100270
Michal Vasko9db2a6f2016-02-01 13:26:03 +0100271 opts->cert_path = strdup(client_cert);
272 if (!opts->cert_path) {
273 ERRMEM;
274 return -1;
Michal Vasko11d4cdb2015-10-29 11:42:52 +0100275 }
276
Michal Vasko3031aae2016-01-27 16:07:18 +0100277 if (client_key) {
278 opts->key_path = strdup(client_key);
Michal Vasko9db2a6f2016-02-01 13:26:03 +0100279 if (!opts->key_path) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100280 ERRMEM;
281 return -1;
282 }
283 } else {
284 opts->key_path = NULL;
Michal Vasko11d4cdb2015-10-29 11:42:52 +0100285 }
286
Michal Vasko3031aae2016-01-27 16:07:18 +0100287 opts->tls_ctx_change = 1;
Michal Vasko11d4cdb2015-10-29 11:42:52 +0100288
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100289 return 0;
Michal Vasko11d4cdb2015-10-29 11:42:52 +0100290}
291
Michal Vasko3031aae2016-01-27 16:07:18 +0100292API int
Michal Vaskoe22c6732016-01-29 11:03:02 +0100293nc_client_tls_set_cert_key_paths(const char *client_cert, const char *client_key)
Michal Vasko11d4cdb2015-10-29 11:42:52 +0100294{
Michal Vaskoe22c6732016-01-29 11:03:02 +0100295 return _nc_client_tls_set_cert_key_paths(client_cert, client_key, &tls_opts);
Michal Vasko3031aae2016-01-27 16:07:18 +0100296}
297
298API int
Michal Vaskoe22c6732016-01-29 11:03:02 +0100299nc_client_tls_ch_set_cert_key_paths(const char *client_cert, const char *client_key)
Michal Vasko3031aae2016-01-27 16:07:18 +0100300{
Michal Vaskoe22c6732016-01-29 11:03:02 +0100301 return _nc_client_tls_set_cert_key_paths(client_cert, client_key, &tls_ch_opts);
302}
303
304static void
305_nc_client_tls_get_cert_key_paths(const char **client_cert, const char **client_key, struct nc_client_tls_opts *opts)
306{
307 if (!client_cert && !client_key) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200308 ERRARG("client_cert and client_key");
Michal Vaskoe22c6732016-01-29 11:03:02 +0100309 return;
310 }
311
312 if (client_cert) {
313 *client_cert = opts->cert_path;
314 }
315 if (client_key) {
316 *client_key = opts->key_path;
317 }
318}
319
320API void
321nc_client_tls_get_cert_key_paths(const char **client_cert, const char **client_key)
322{
323 _nc_client_tls_get_cert_key_paths(client_cert, client_key, &tls_opts);
324}
325
326API void
327nc_client_tls_ch_get_cert_key_paths(const char **client_cert, const char **client_key)
328{
329 _nc_client_tls_get_cert_key_paths(client_cert, client_key, &tls_ch_opts);
Michal Vasko3031aae2016-01-27 16:07:18 +0100330}
331
332static int
Michal Vaskoe22c6732016-01-29 11:03:02 +0100333_nc_client_tls_set_trusted_ca_paths(const char *ca_file, const char *ca_dir, struct nc_client_tls_opts *opts)
Michal Vasko3031aae2016-01-27 16:07:18 +0100334{
Michal Vasko3031aae2016-01-27 16:07:18 +0100335 if (!ca_file && !ca_dir) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200336 ERRARG("ca_file and ca_dir");
Michal Vasko3031aae2016-01-27 16:07:18 +0100337 return -1;
338 }
339
Michal Vasko3031aae2016-01-27 16:07:18 +0100340 free(opts->ca_file);
341 free(opts->ca_dir);
342
343 if (ca_file) {
344 opts->ca_file = strdup(ca_file);
345 if (!opts->ca_file) {
346 ERRMEM;
347 return -1;
348 }
349 } else {
350 opts->ca_file = NULL;
351 }
352
353 if (ca_dir) {
354 opts->ca_dir = strdup(ca_dir);
355 if (!opts->ca_dir) {
356 ERRMEM;
357 return -1;
358 }
359 } else {
360 opts->ca_dir = NULL;
361 }
362
363 opts->tls_ctx_change = 1;
364
365 return 0;
366}
367
368API int
Michal Vaskoe22c6732016-01-29 11:03:02 +0100369nc_client_tls_set_trusted_ca_paths(const char *ca_file, const char *ca_dir)
Michal Vasko3031aae2016-01-27 16:07:18 +0100370{
Michal Vaskoe22c6732016-01-29 11:03:02 +0100371 return _nc_client_tls_set_trusted_ca_paths(ca_file, ca_dir, &tls_opts);
Michal Vasko3031aae2016-01-27 16:07:18 +0100372}
373
374API int
Michal Vaskoe22c6732016-01-29 11:03:02 +0100375nc_client_tls_ch_set_trusted_ca_paths(const char *ca_file, const char *ca_dir)
Michal Vasko3031aae2016-01-27 16:07:18 +0100376{
Michal Vaskoe22c6732016-01-29 11:03:02 +0100377 return _nc_client_tls_set_trusted_ca_paths(ca_file, ca_dir, &tls_ch_opts);
378}
379
380static void
381_nc_client_tls_get_trusted_ca_paths(const char **ca_file, const char **ca_dir, struct nc_client_tls_opts *opts)
382{
383 if (!ca_file && !ca_dir) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200384 ERRARG("ca_file and ca_dir");
Michal Vaskoe22c6732016-01-29 11:03:02 +0100385 return;
386 }
387
388 if (ca_file) {
389 *ca_file = opts->ca_file;
390 }
391 if (ca_dir) {
392 *ca_dir = opts->ca_dir;
393 }
394}
395
396API void
397nc_client_tls_get_trusted_ca_paths(const char **ca_file, const char **ca_dir)
398{
399 _nc_client_tls_get_trusted_ca_paths(ca_file, ca_dir, &tls_opts);
400}
401
402API void
403nc_client_tls_ch_get_trusted_ca_paths(const char **ca_file, const char **ca_dir)
404{
405 _nc_client_tls_get_trusted_ca_paths(ca_file, ca_dir, &tls_ch_opts);
Michal Vasko3031aae2016-01-27 16:07:18 +0100406}
407
408static int
Michal Vaskoe22c6732016-01-29 11:03:02 +0100409_nc_client_tls_set_crl_paths(const char *crl_file, const char *crl_dir, struct nc_client_tls_opts *opts)
Michal Vasko3031aae2016-01-27 16:07:18 +0100410{
Michal Vasko3031aae2016-01-27 16:07:18 +0100411 if (!crl_file && !crl_dir) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200412 ERRARG("crl_file and crl_dir");
Michal Vasko3031aae2016-01-27 16:07:18 +0100413 return -1;
414 }
415
Michal Vasko3031aae2016-01-27 16:07:18 +0100416 free(opts->crl_file);
417 free(opts->crl_dir);
418
419 if (crl_file) {
420 opts->crl_file = strdup(crl_file);
421 if (!opts->crl_file) {
422 ERRMEM;
423 return -1;
424 }
425 } else {
426 opts->crl_file = NULL;
427 }
428
429 if (crl_dir) {
430 opts->crl_dir = strdup(crl_dir);
431 if (!opts->crl_dir) {
432 ERRMEM;
433 return -1;
434 }
435 } else {
436 opts->crl_dir = NULL;
437 }
438
439 opts->crl_store_change = 1;
440
441 return 0;
442}
443
444API int
Michal Vaskoe22c6732016-01-29 11:03:02 +0100445nc_client_tls_set_crl_paths(const char *crl_file, const char *crl_dir)
Michal Vasko3031aae2016-01-27 16:07:18 +0100446{
Michal Vaskoe22c6732016-01-29 11:03:02 +0100447 return _nc_client_tls_set_crl_paths(crl_file, crl_dir, &tls_opts);
Michal Vasko3031aae2016-01-27 16:07:18 +0100448}
449
450API int
Michal Vaskoe22c6732016-01-29 11:03:02 +0100451nc_client_tls_ch_set_crl_paths(const char *crl_file, const char *crl_dir)
Michal Vasko3031aae2016-01-27 16:07:18 +0100452{
Michal Vaskoe22c6732016-01-29 11:03:02 +0100453 return _nc_client_tls_set_crl_paths(crl_file, crl_dir, &tls_ch_opts);
454}
455
456static void
457_nc_client_tls_get_crl_paths(const char **crl_file, const char **crl_dir, struct nc_client_tls_opts *opts)
458{
459 if (!crl_file && !crl_dir) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200460 ERRARG("crl_file and crl_dir");
Michal Vaskoe22c6732016-01-29 11:03:02 +0100461 return;
462 }
463
464 if (crl_file) {
465 *crl_file = opts->crl_file;
466 }
467 if (crl_dir) {
468 *crl_dir = opts->crl_dir;
469 }
470}
471
472API void
473nc_client_tls_get_crl_paths(const char **crl_file, const char **crl_dir)
474{
475 _nc_client_tls_get_crl_paths(crl_file, crl_dir, &tls_opts);
476}
477
478API void
479nc_client_tls_ch_get_crl_paths(const char **crl_file, const char **crl_dir)
480{
481 _nc_client_tls_get_crl_paths(crl_file, crl_dir, &tls_ch_opts);
Michal Vasko3031aae2016-01-27 16:07:18 +0100482}
483
484API int
485nc_client_tls_ch_add_bind_listen(const char *address, uint16_t port)
486{
Michal Vasko9d4cca52022-09-07 11:19:57 +0200487 return nc_client_ch_add_bind_listen(address, port, NULL, NC_TI_OPENSSL);
488}
489
490API int
491nc_client_tls_ch_add_bind_hostname_listen(const char *address, uint16_t port, const char *hostname)
492{
493 return nc_client_ch_add_bind_listen(address, port, hostname, NC_TI_OPENSSL);
Michal Vasko3031aae2016-01-27 16:07:18 +0100494}
495
496API int
497nc_client_tls_ch_del_bind(const char *address, uint16_t port)
498{
499 return nc_client_ch_del_bind(address, port, NC_TI_OPENSSL);
500}
501
Michal Vasko3031aae2016-01-27 16:07:18 +0100502static int
Michal Vasko55ce24a2022-09-07 09:24:43 +0200503nc_client_tls_update_opts(struct nc_client_tls_opts *opts, const char *peername)
Michal Vasko3031aae2016-01-27 16:07:18 +0100504{
Michal Vasko9af52322022-07-25 14:39:30 +0200505 int rc = 0;
Michal Vasko3031aae2016-01-27 16:07:18 +0100506 char *key;
507 X509_LOOKUP *lookup;
Michal Vasko9af52322022-07-25 14:39:30 +0200508 X509_VERIFY_PARAM *vpm = NULL;
Michal Vasko3031aae2016-01-27 16:07:18 +0100509
510 if (!opts->tls_ctx || opts->tls_ctx_change) {
Michal Vaskoe22c6732016-01-29 11:03:02 +0100511 SSL_CTX_free(opts->tls_ctx);
Michal Vasko3031aae2016-01-27 16:07:18 +0100512
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100513#if OPENSSL_VERSION_NUMBER >= 0x10100000L // >= 1.1.0
514 /* prepare global SSL context, highest available method is negotiated autmatically */
515 if (!(opts->tls_ctx = SSL_CTX_new(TLS_client_method())))
516#else
Michal Vasko3031aae2016-01-27 16:07:18 +0100517 /* prepare global SSL context, allow only mandatory TLS 1.2 */
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100518 if (!(opts->tls_ctx = SSL_CTX_new(TLSv1_2_client_method())))
519#endif
520 {
Michal Vasko05532772021-06-03 12:12:38 +0200521 ERR(NULL, "Unable to create OpenSSL context (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vasko9af52322022-07-25 14:39:30 +0200522 rc = -1;
523 goto cleanup;
Michal Vasko3031aae2016-01-27 16:07:18 +0100524 }
525 SSL_CTX_set_verify(opts->tls_ctx, SSL_VERIFY_PEER, tlsauth_verify_callback);
526
527 /* get peer certificate */
528 if (SSL_CTX_use_certificate_file(opts->tls_ctx, opts->cert_path, SSL_FILETYPE_PEM) != 1) {
Michal Vasko05532772021-06-03 12:12:38 +0200529 ERR(NULL, "Loading the client certificate from \'%s\' failed (%s).", opts->cert_path,
530 ERR_reason_error_string(ERR_get_error()));
Michal Vasko9af52322022-07-25 14:39:30 +0200531 rc = -1;
532 goto cleanup;
Michal Vasko3031aae2016-01-27 16:07:18 +0100533 }
534
535 /* if the file with private key not specified, expect that the private key is stored with the certificate */
536 if (!opts->key_path) {
537 key = opts->cert_path;
538 } else {
539 key = opts->key_path;
540 }
541 if (SSL_CTX_use_PrivateKey_file(opts->tls_ctx, key, SSL_FILETYPE_PEM) != 1) {
Michal Vasko9af52322022-07-25 14:39:30 +0200542 ERR(NULL, "Loading the client private key from \'%s\' failed (%s).", key,
Michal Vasko05532772021-06-03 12:12:38 +0200543 ERR_reason_error_string(ERR_get_error()));
Michal Vasko9af52322022-07-25 14:39:30 +0200544 rc = -1;
545 goto cleanup;
Michal Vasko3031aae2016-01-27 16:07:18 +0100546 }
547
548 if (!SSL_CTX_load_verify_locations(opts->tls_ctx, opts->ca_file, opts->ca_dir)) {
Michal Vasko05532772021-06-03 12:12:38 +0200549 ERR(NULL, "Failed to load the locations of trusted CA certificates (%s).",
550 ERR_reason_error_string(ERR_get_error()));
Michal Vasko9af52322022-07-25 14:39:30 +0200551 rc = -1;
552 goto cleanup;
Michal Vasko3031aae2016-01-27 16:07:18 +0100553 }
Michal Vasko9af52322022-07-25 14:39:30 +0200554
555#if OPENSSL_VERSION_NUMBER >= 0x10100000L // >= 1.1.0
Michal Vasko55ce24a2022-09-07 09:24:43 +0200556 if (peername) {
557 /* server identity (hostname) verification */
558 vpm = X509_VERIFY_PARAM_new();
559 if (!X509_VERIFY_PARAM_set1_host(vpm, peername, 0)) {
560 ERR(NULL, "Failed to set expected server hostname (%s).", ERR_reason_error_string(ERR_get_error()));
561 rc = -1;
562 goto cleanup;
563 }
564 if (!SSL_CTX_set1_param(opts->tls_ctx, vpm)) {
565 ERR(NULL, "Failed to set verify params (%s).", ERR_reason_error_string(ERR_get_error()));
566 rc = -1;
567 goto cleanup;
568 }
Michal Vasko9af52322022-07-25 14:39:30 +0200569 }
570#endif
Michal Vasko3031aae2016-01-27 16:07:18 +0100571 }
572
573 if (opts->crl_store_change || (!opts->crl_store && (opts->crl_file || opts->crl_dir))) {
574 /* set the revocation store with the correct paths for the callback */
575 X509_STORE_free(opts->crl_store);
576
577 opts->crl_store = X509_STORE_new();
578 if (!opts->crl_store) {
Michal Vasko05532772021-06-03 12:12:38 +0200579 ERR(NULL, "Unable to create a certificate store (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vasko9af52322022-07-25 14:39:30 +0200580 rc = -1;
581 goto cleanup;
Michal Vasko3031aae2016-01-27 16:07:18 +0100582 }
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100583
584#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
585 /* whaveter this does... */
Michal Vasko3031aae2016-01-27 16:07:18 +0100586 opts->crl_store->cache = 0;
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100587#endif
Michal Vasko3031aae2016-01-27 16:07:18 +0100588
589 if (opts->crl_file) {
590 if (!(lookup = X509_STORE_add_lookup(opts->crl_store, X509_LOOKUP_file()))) {
Michal Vasko05532772021-06-03 12:12:38 +0200591 ERR(NULL, "Failed to add lookup method to CRL checking.");
Michal Vasko9af52322022-07-25 14:39:30 +0200592 rc = -1;
593 goto cleanup;
Michal Vasko3031aae2016-01-27 16:07:18 +0100594 }
595 if (X509_LOOKUP_add_dir(lookup, opts->crl_file, X509_FILETYPE_PEM) != 1) {
Michal Vasko05532772021-06-03 12:12:38 +0200596 ERR(NULL, "Failed to add the revocation lookup file \"%s\".", opts->crl_file);
Michal Vasko9af52322022-07-25 14:39:30 +0200597 rc = -1;
598 goto cleanup;
Michal Vasko3031aae2016-01-27 16:07:18 +0100599 }
600 }
601
602 if (opts->crl_dir) {
603 if (!(lookup = X509_STORE_add_lookup(opts->crl_store, X509_LOOKUP_hash_dir()))) {
Michal Vasko05532772021-06-03 12:12:38 +0200604 ERR(NULL, "Failed to add lookup method to CRL checking.");
Michal Vasko9af52322022-07-25 14:39:30 +0200605 rc = -1;
606 goto cleanup;
Michal Vasko3031aae2016-01-27 16:07:18 +0100607 }
608 if (X509_LOOKUP_add_dir(lookup, opts->crl_dir, X509_FILETYPE_PEM) != 1) {
Michal Vasko05532772021-06-03 12:12:38 +0200609 ERR(NULL, "Failed to add the revocation lookup directory \"%s\".", opts->crl_dir);
Michal Vasko9af52322022-07-25 14:39:30 +0200610 rc = -1;
611 goto cleanup;
Michal Vasko3031aae2016-01-27 16:07:18 +0100612 }
613 }
614 }
615
Michal Vasko9af52322022-07-25 14:39:30 +0200616cleanup:
617 X509_VERIFY_PARAM_free(vpm);
618 return rc;
619}
620
621static int
Michal Vaskoa82745b2022-09-07 11:48:12 +0200622nc_client_tls_connect_check(int connect_ret, SSL *tls, const char *peername)
Michal Vasko9af52322022-07-25 14:39:30 +0200623{
624 int verify;
625
626 /* check certificate verification result */
627 verify = SSL_get_verify_result(tls);
628 switch (verify) {
629 case X509_V_OK:
630 if (connect_ret == 1) {
Michal Vasko7ffcd142022-09-07 09:24:22 +0200631 VRB(NULL, "Server certificate verified (domain \"%s\").", peername);
Michal Vasko9af52322022-07-25 14:39:30 +0200632 }
633 break;
634 default:
635 ERR(NULL, "Server certificate error (%s).", X509_verify_cert_error_string(verify));
636 }
637
638 /* check TLS connection result */
639 if (connect_ret != 1) {
640 switch (SSL_get_error(tls, connect_ret)) {
641 case SSL_ERROR_SYSCALL:
Michal Vasko7ffcd142022-09-07 09:24:22 +0200642 ERR(NULL, "SSL connect to \"%s\" failed (%s).", peername, errno ? strerror(errno) : "unexpected EOF");
Michal Vasko9af52322022-07-25 14:39:30 +0200643 break;
644 case SSL_ERROR_SSL:
Michal Vasko7ffcd142022-09-07 09:24:22 +0200645 ERR(NULL, "SSL connect to \"%s\" failed (%s).", peername, ERR_reason_error_string(ERR_get_error()));
Michal Vasko9af52322022-07-25 14:39:30 +0200646 break;
647 default:
Michal Vasko7ffcd142022-09-07 09:24:22 +0200648 ERR(NULL, "SSL connect to \"%s\" failed.", peername);
Michal Vasko9af52322022-07-25 14:39:30 +0200649 break;
650 }
651 }
652
653 return connect_ret;
Radek Krejci9f03b482015-10-22 16:02:10 +0200654}
655
656API struct nc_session *
Michal Vasko9bee18d2015-12-08 14:41:42 +0100657nc_connect_tls(const char *host, unsigned short port, struct ly_ctx *ctx)
Radek Krejci9f03b482015-10-22 16:02:10 +0200658{
Radek Krejci9f03b482015-10-22 16:02:10 +0200659 struct nc_session *session = NULL;
Michal Vasko9af52322022-07-25 14:39:30 +0200660 int sock, ret;
roman6ece9c52022-06-22 09:29:17 +0200661 struct timespec ts_timeout;
Michal Vasko66032bc2019-01-22 15:03:12 +0100662 char *ip_host = NULL;
Michal Vasko11d4cdb2015-10-29 11:42:52 +0100663
Michal Vasko3031aae2016-01-27 16:07:18 +0100664 if (!tls_opts.cert_path || (!tls_opts.ca_file && !tls_opts.ca_dir)) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200665 ERRINIT;
Michal Vasko11d4cdb2015-10-29 11:42:52 +0100666 return NULL;
667 }
Radek Krejci9f03b482015-10-22 16:02:10 +0200668
669 /* process parameters */
670 if (!host || strisempty(host)) {
671 host = "localhost";
672 }
673
674 if (!port) {
675 port = NC_PORT_TLS;
676 }
677
Michal Vasko3031aae2016-01-27 16:07:18 +0100678 /* create/update TLS structures */
Michal Vasko9af52322022-07-25 14:39:30 +0200679 if (nc_client_tls_update_opts(&tls_opts, host)) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100680 return NULL;
681 }
682
Radek Krejci9f03b482015-10-22 16:02:10 +0200683 /* prepare session structure */
Michal Vasko131120a2018-05-29 15:44:02 +0200684 session = nc_new_session(NC_CLIENT, 0);
Radek Krejci9f03b482015-10-22 16:02:10 +0200685 if (!session) {
Michal Vasko9e2d3a32015-11-10 13:09:18 +0100686 ERRMEM;
Radek Krejci9f03b482015-10-22 16:02:10 +0200687 return NULL;
688 }
Michal Vasko9e2d3a32015-11-10 13:09:18 +0100689 session->status = NC_STATUS_STARTING;
Michal Vasko11d4cdb2015-10-29 11:42:52 +0100690
691 /* fill the session */
Michal Vasko9e2d3a32015-11-10 13:09:18 +0100692 session->ti_type = NC_TI_OPENSSL;
Michal Vasko11d4cdb2015-10-29 11:42:52 +0100693 if (!(session->ti.tls = SSL_new(tls_opts.tls_ctx))) {
Michal Vasko05532772021-06-03 12:12:38 +0200694 ERR(NULL, "Failed to create a new TLS session structure (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vasko11d4cdb2015-10-29 11:42:52 +0100695 goto fail;
696 }
Radek Krejci9f03b482015-10-22 16:02:10 +0200697
Michal Vasko11d4cdb2015-10-29 11:42:52 +0100698 /* create and assign socket */
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200699 sock = nc_sock_connect(host, port, -1, &client_opts.ka, NULL, &ip_host);
Michal Vasko11d4cdb2015-10-29 11:42:52 +0100700 if (sock == -1) {
Michal Vasko05532772021-06-03 12:12:38 +0200701 ERR(NULL, "Unable to connect to %s:%u (%s).", host, port, strerror(errno));
Michal Vasko11d4cdb2015-10-29 11:42:52 +0100702 goto fail;
703 }
704 SSL_set_fd(session->ti.tls, sock);
705
706 /* set the SSL_MODE_AUTO_RETRY flag to allow OpenSSL perform re-handshake automatically */
707 SSL_set_mode(session->ti.tls, SSL_MODE_AUTO_RETRY);
708
709 /* connect and perform the handshake */
Michal Vaskod8a74192023-02-06 15:51:50 +0100710 nc_timeouttime_get(&ts_timeout, NC_TRANSPORT_TIMEOUT);
Michal Vasko3031aae2016-01-27 16:07:18 +0100711 tlsauth_ch = 0;
Michal Vaskod51a2642021-09-03 13:03:24 +0200712 while (((ret = SSL_connect(session->ti.tls)) != 1) && (SSL_get_error(session->ti.tls, ret) == SSL_ERROR_WANT_READ)) {
Michal Vasko0190bc32016-03-02 15:47:49 +0100713 usleep(NC_TIMEOUT_STEP);
Michal Vaskod8a74192023-02-06 15:51:50 +0100714 if (nc_timeouttime_cur_diff(&ts_timeout) < 1) {
Michal Vasko7ffcd142022-09-07 09:24:22 +0200715 ERR(NULL, "SSL connect timeout.");
Michal Vasko0190bc32016-03-02 15:47:49 +0100716 goto fail;
717 }
718 }
Michal Vasko3cd55a72022-09-06 15:52:39 +0200719
720 /* check for errors */
Michal Vaskoa82745b2022-09-07 11:48:12 +0200721 if (nc_client_tls_connect_check(ret, session->ti.tls, host) != 1) {
Michal Vasko11d4cdb2015-10-29 11:42:52 +0100722 goto fail;
723 }
724
Michal Vasko78939072022-12-12 07:43:18 +0100725 if (nc_client_session_new_ctx(session, ctx) != EXIT_SUCCESS) {
Radek Krejcifd5b6682017-06-13 15:52:53 +0200726 goto fail;
Michal Vasko9e2d3a32015-11-10 13:09:18 +0100727 }
Radek Krejcifd5b6682017-06-13 15:52:53 +0200728 ctx = session->ctx;
Michal Vasko9e2d3a32015-11-10 13:09:18 +0100729
Radek Krejci9f03b482015-10-22 16:02:10 +0200730 /* NETCONF handshake */
Michal Vasko131120a2018-05-29 15:44:02 +0200731 if (nc_handshake_io(session) != NC_MSG_HELLO) {
Michal Vasko11d4cdb2015-10-29 11:42:52 +0100732 goto fail;
Radek Krejci9f03b482015-10-22 16:02:10 +0200733 }
Michal Vaskoad611702015-12-03 13:41:51 +0100734 session->status = NC_STATUS_RUNNING;
Radek Krejci9f03b482015-10-22 16:02:10 +0200735
Michal Vaskoef578332016-01-25 13:20:09 +0100736 if (nc_ctx_check_and_fill(session) == -1) {
Michal Vasko57eb9402015-12-08 14:38:12 +0100737 goto fail;
Michal Vasko9e2d3a32015-11-10 13:09:18 +0100738 }
739
740 /* store information into session and the dictionary */
Michal Vasko93224072021-11-09 12:14:28 +0100741 session->host = ip_host;
Michal Vasko9e2d3a32015-11-10 13:09:18 +0100742 session->port = port;
Michal Vasko93224072021-11-09 12:14:28 +0100743 session->username = strdup("certificate-based");
Michal Vasko9e2d3a32015-11-10 13:09:18 +0100744
Radek Krejci9f03b482015-10-22 16:02:10 +0200745 return session;
746
Michal Vasko11d4cdb2015-10-29 11:42:52 +0100747fail:
Michal Vasko66032bc2019-01-22 15:03:12 +0100748 free(ip_host);
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100749 nc_session_free(session, NULL);
Radek Krejci9f03b482015-10-22 16:02:10 +0200750 return NULL;
751}
752
753API struct nc_session *
754nc_connect_libssl(SSL *tls, struct ly_ctx *ctx)
755{
Michal Vasko11d4cdb2015-10-29 11:42:52 +0100756 struct nc_session *session;
Radek Krejci9f03b482015-10-22 16:02:10 +0200757
Michal Vasko45e53ae2016-04-07 11:46:03 +0200758 if (!tls) {
759 ERRARG("tls");
760 return NULL;
761 } else if (!SSL_is_init_finished(tls)) {
Michal Vasko05532772021-06-03 12:12:38 +0200762 ERR(NULL, "Supplied TLS session is not fully connected!");
Michal Vasko11d4cdb2015-10-29 11:42:52 +0100763 return NULL;
764 }
765
766 /* prepare session structure */
Michal Vasko131120a2018-05-29 15:44:02 +0200767 session = nc_new_session(NC_CLIENT, 0);
Michal Vasko11d4cdb2015-10-29 11:42:52 +0100768 if (!session) {
Michal Vasko9e2d3a32015-11-10 13:09:18 +0100769 ERRMEM;
Michal Vasko11d4cdb2015-10-29 11:42:52 +0100770 return NULL;
771 }
Michal Vasko9e2d3a32015-11-10 13:09:18 +0100772 session->status = NC_STATUS_STARTING;
Michal Vasko9e2d3a32015-11-10 13:09:18 +0100773 session->ti_type = NC_TI_OPENSSL;
Michal Vasko11d4cdb2015-10-29 11:42:52 +0100774 session->ti.tls = tls;
775
Michal Vasko78939072022-12-12 07:43:18 +0100776 if (nc_client_session_new_ctx(session, ctx) != EXIT_SUCCESS) {
Radek Krejcifd5b6682017-06-13 15:52:53 +0200777 goto fail;
Michal Vasko11d4cdb2015-10-29 11:42:52 +0100778 }
Radek Krejcifd5b6682017-06-13 15:52:53 +0200779 ctx = session->ctx;
Michal Vasko11d4cdb2015-10-29 11:42:52 +0100780
781 /* NETCONF handshake */
Michal Vasko131120a2018-05-29 15:44:02 +0200782 if (nc_handshake_io(session) != NC_MSG_HELLO) {
Michal Vasko11d4cdb2015-10-29 11:42:52 +0100783 goto fail;
784 }
Michal Vaskoad611702015-12-03 13:41:51 +0100785 session->status = NC_STATUS_RUNNING;
Michal Vasko11d4cdb2015-10-29 11:42:52 +0100786
Michal Vaskoef578332016-01-25 13:20:09 +0100787 if (nc_ctx_check_and_fill(session) == -1) {
Michal Vasko57eb9402015-12-08 14:38:12 +0100788 goto fail;
Michal Vasko9e2d3a32015-11-10 13:09:18 +0100789 }
790
Michal Vasko11d4cdb2015-10-29 11:42:52 +0100791 return session;
792
793fail:
Michal Vaskob604ed52022-01-24 09:56:14 +0100794 session->ti_type = NC_TI_NONE;
Michal Vaskoa42a7f02021-07-02 08:43:12 +0200795 session->ti.tls = NULL;
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100796 nc_session_free(session, NULL);
Radek Krejci9f03b482015-10-22 16:02:10 +0200797 return NULL;
798}
799
Michal Vasko3031aae2016-01-27 16:07:18 +0100800struct nc_session *
Michal Vasko9d4cca52022-09-07 11:19:57 +0200801nc_accept_callhome_tls_sock(int sock, const char *host, uint16_t port, struct ly_ctx *ctx, int timeout, const char *peername)
Michal Vasko80cad7f2015-12-08 14:42:27 +0100802{
Michal Vasko3cd55a72022-09-06 15:52:39 +0200803 int ret;
Michal Vaskoc64c38c2021-07-02 08:43:53 +0200804 SSL *tls = NULL;
805 struct nc_session *session = NULL;
roman6ece9c52022-06-22 09:29:17 +0200806 struct timespec ts_timeout;
Michal Vasko80cad7f2015-12-08 14:42:27 +0100807
Michal Vasko9d4cca52022-09-07 11:19:57 +0200808 /* create/update TLS structures with explicit expected peername, if any set, the host is just the IP */
809 if (nc_client_tls_update_opts(&tls_ch_opts, peername)) {
Michal Vaskoc64c38c2021-07-02 08:43:53 +0200810 goto cleanup;
Michal Vaskoc61c4492016-01-25 11:13:34 +0100811 }
812
Michal Vasko3031aae2016-01-27 16:07:18 +0100813 if (!(tls = SSL_new(tls_ch_opts.tls_ctx))) {
Michal Vasko05532772021-06-03 12:12:38 +0200814 ERR(NULL, "Failed to create new TLS session structure (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vaskoc64c38c2021-07-02 08:43:53 +0200815 goto cleanup;
Michal Vasko80cad7f2015-12-08 14:42:27 +0100816 }
817
818 SSL_set_fd(tls, sock);
819
820 /* set the SSL_MODE_AUTO_RETRY flag to allow OpenSSL perform re-handshake automatically */
821 SSL_set_mode(tls, SSL_MODE_AUTO_RETRY);
822
823 /* connect and perform the handshake */
Michal Vasko36c7be82017-02-22 13:37:59 +0100824 if (timeout > -1) {
Michal Vaskod8a74192023-02-06 15:51:50 +0100825 nc_timeouttime_get(&ts_timeout, timeout);
Michal Vasko36c7be82017-02-22 13:37:59 +0100826 }
Michal Vasko3031aae2016-01-27 16:07:18 +0100827 tlsauth_ch = 1;
Michal Vasko0190bc32016-03-02 15:47:49 +0100828 while (((ret = SSL_connect(tls)) == -1) && (SSL_get_error(tls, ret) == SSL_ERROR_WANT_READ)) {
829 usleep(NC_TIMEOUT_STEP);
Michal Vaskod8a74192023-02-06 15:51:50 +0100830 if ((timeout > -1) && (nc_timeouttime_cur_diff(&ts_timeout) < 1)) {
Michal Vasko7ffcd142022-09-07 09:24:22 +0200831 ERR(NULL, "SSL connect timeout.");
roman6ece9c52022-06-22 09:29:17 +0200832 goto cleanup;
Michal Vasko0190bc32016-03-02 15:47:49 +0100833 }
834 }
Michal Vasko80cad7f2015-12-08 14:42:27 +0100835
Michal Vasko3cd55a72022-09-06 15:52:39 +0200836 /* check for errors */
Michal Vaskoa82745b2022-09-07 11:48:12 +0200837 if (nc_client_tls_connect_check(ret, tls, peername ? peername : host) != 1) {
Michal Vasko3cd55a72022-09-06 15:52:39 +0200838 goto cleanup;
Michal Vasko80cad7f2015-12-08 14:42:27 +0100839 }
840
Michal Vaskoc64c38c2021-07-02 08:43:53 +0200841 /* connect */
Michal Vasko80cad7f2015-12-08 14:42:27 +0100842 session = nc_connect_libssl(tls, ctx);
Michal Vaskoc64c38c2021-07-02 08:43:53 +0200843 if (!session) {
844 goto cleanup;
Michal Vasko80cad7f2015-12-08 14:42:27 +0100845 }
846
Michal Vaskoc64c38c2021-07-02 08:43:53 +0200847 session->flags |= NC_SESSION_CALLHOME;
848
849 /* store information into session and the dictionary */
Michal Vasko93224072021-11-09 12:14:28 +0100850 session->host = strdup(host);
Michal Vaskoc64c38c2021-07-02 08:43:53 +0200851 session->port = port;
Michal Vasko93224072021-11-09 12:14:28 +0100852 session->username = strdup("certificate-based");
Michal Vaskoc64c38c2021-07-02 08:43:53 +0200853
854cleanup:
855 if (!session) {
856 SSL_free(tls);
857 close(sock);
858 }
Michal Vasko80cad7f2015-12-08 14:42:27 +0100859 return session;
860}