blob: 94e8ac51a7160126aee9748220136fe3d4c25e18 [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);
Michal Vaskob83a3fa2021-05-26 09:53:42 +020038int nc_session_new_ctx(struct nc_session *session, struct ly_ctx *ctx);
Radek Krejci62aa0642017-05-25 16:33:49 +020039
40#define client_opts nc_client_context_location()->opts
41#define tls_opts nc_client_context_location()->tls_opts
42#define tls_ch_opts nc_client_context_location()->tls_ch_opts
Michal Vasko3031aae2016-01-27 16:07:18 +010043
44static int tlsauth_ch;
Radek Krejci9f03b482015-10-22 16:02:10 +020045
Michal Vasko18aeb5d2017-02-17 09:23:56 +010046#if OPENSSL_VERSION_NUMBER >= 0x10100000L // >= 1.1.0
47
48static int
49tlsauth_verify_callback(int preverify_ok, X509_STORE_CTX *x509_ctx)
50{
51 X509_STORE_CTX *store_ctx;
52 X509_OBJECT *obj;
53 X509_NAME *subject, *issuer;
54 X509 *cert;
55 X509_CRL *crl;
56 X509_REVOKED *revoked;
57 EVP_PKEY *pubkey;
58 int i, n, rc;
59 const ASN1_TIME *next_update = NULL;
60 struct nc_client_tls_opts *opts;
61
62 if (!preverify_ok) {
63 return 0;
64 }
65
66 opts = (tlsauth_ch ? &tls_ch_opts : &tls_opts);
67
68 if (!opts->crl_store) {
69 /* nothing to check */
70 return 1;
71 }
72
73 cert = X509_STORE_CTX_get_current_cert(x509_ctx);
74 subject = X509_get_subject_name(cert);
75 issuer = X509_get_issuer_name(cert);
76
77 /* try to retrieve a CRL corresponding to the _subject_ of
78 * the current certificate in order to verify it's integrity */
79 store_ctx = X509_STORE_CTX_new();
80 obj = X509_OBJECT_new();
81 X509_STORE_CTX_init(store_ctx, opts->crl_store, NULL, NULL);
Rosen Penev4f552d62019-06-26 16:10:43 -070082 rc = X509_STORE_CTX_get_by_subject(store_ctx, X509_LU_CRL, subject, obj);
Michal Vasko18aeb5d2017-02-17 09:23:56 +010083 X509_STORE_CTX_free(store_ctx);
84 crl = X509_OBJECT_get0_X509_CRL(obj);
Michal Vaskob83a3fa2021-05-26 09:53:42 +020085 if ((rc > 0) && crl) {
Michal Vasko18aeb5d2017-02-17 09:23:56 +010086 next_update = X509_CRL_get0_nextUpdate(crl);
87
88 /* verify the signature on this CRL */
89 pubkey = X509_get_pubkey(cert);
90 if (X509_CRL_verify(crl, pubkey) <= 0) {
91 X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_CRL_SIGNATURE_FAILURE);
92 X509_OBJECT_free(obj);
93 if (pubkey) {
94 EVP_PKEY_free(pubkey);
95 }
96 return 0; /* fail */
97 }
98 if (pubkey) {
99 EVP_PKEY_free(pubkey);
100 }
101
102 /* check date of CRL to make sure it's not expired */
103 if (!next_update) {
104 X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD);
105 X509_OBJECT_free(obj);
106 return 0; /* fail */
107 }
108 if (X509_cmp_current_time(next_update) < 0) {
109 X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_CRL_HAS_EXPIRED);
110 X509_OBJECT_free(obj);
111 return 0; /* fail */
112 }
113 X509_OBJECT_free(obj);
114 }
115
116 /* try to retrieve a CRL corresponding to the _issuer_ of
117 * the current certificate in order to check for revocation */
118 store_ctx = X509_STORE_CTX_new();
119 obj = X509_OBJECT_new();
120 X509_STORE_CTX_init(store_ctx, opts->crl_store, NULL, NULL);
Rosen Penev4f552d62019-06-26 16:10:43 -0700121 rc = X509_STORE_CTX_get_by_subject(store_ctx, X509_LU_CRL, issuer, obj);
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100122 X509_STORE_CTX_free(store_ctx);
123 crl = X509_OBJECT_get0_X509_CRL(obj);
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200124 if ((rc > 0) && crl) {
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100125 /* check if the current certificate is revoked by this CRL */
126 n = sk_X509_REVOKED_num(X509_CRL_get_REVOKED(crl));
127 for (i = 0; i < n; i++) {
128 revoked = sk_X509_REVOKED_value(X509_CRL_get_REVOKED(crl), i);
129 if (ASN1_INTEGER_cmp(X509_REVOKED_get0_serialNumber(revoked), X509_get_serialNumber(cert)) == 0) {
Michal Vasko05532772021-06-03 12:12:38 +0200130 ERR(NULL, "Certificate revoked!");
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100131 X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_CERT_REVOKED);
132 X509_OBJECT_free(obj);
133 return 0; /* fail */
134 }
135 }
136 X509_OBJECT_free(obj);
137 }
138
139 return 1; /* success */
140}
141
142#else
143
Radek Krejci9f03b482015-10-22 16:02:10 +0200144static int
Michal Vasko11d4cdb2015-10-29 11:42:52 +0100145tlsauth_verify_callback(int preverify_ok, X509_STORE_CTX *x509_ctx)
Radek Krejci9f03b482015-10-22 16:02:10 +0200146{
Michal Vasko11d4cdb2015-10-29 11:42:52 +0100147 X509_STORE_CTX store_ctx;
148 X509_OBJECT obj;
149 X509_NAME *subject, *issuer;
150 X509 *cert;
151 X509_CRL *crl;
152 X509_REVOKED *revoked;
153 EVP_PKEY *pubkey;
154 int i, n, rc;
155 ASN1_TIME *next_update = NULL;
Michal Vasko3031aae2016-01-27 16:07:18 +0100156 struct nc_client_tls_opts *opts;
Radek Krejci9f03b482015-10-22 16:02:10 +0200157
Michal Vasko11d4cdb2015-10-29 11:42:52 +0100158 if (!preverify_ok) {
159 return 0;
160 }
Radek Krejci9f03b482015-10-22 16:02:10 +0200161
Michal Vasko3031aae2016-01-27 16:07:18 +0100162 opts = (tlsauth_ch ? &tls_ch_opts : &tls_opts);
163
164 if (!opts->crl_store) {
165 /* nothing to check */
166 return 1;
167 }
168
Michal Vasko11d4cdb2015-10-29 11:42:52 +0100169 cert = X509_STORE_CTX_get_current_cert(x509_ctx);
170 subject = X509_get_subject_name(cert);
171 issuer = X509_get_issuer_name(cert);
172
173 /* try to retrieve a CRL corresponding to the _subject_ of
174 * the current certificate in order to verify it's integrity */
175 memset((char *)&obj, 0, sizeof obj);
Michal Vasko3031aae2016-01-27 16:07:18 +0100176 X509_STORE_CTX_init(&store_ctx, opts->crl_store, NULL, NULL);
Rosen Penev4f552d62019-06-26 16:10:43 -0700177 rc = X509_STORE_CTX_get_by_subject(&store_ctx, X509_LU_CRL, subject, &obj);
Michal Vasko11d4cdb2015-10-29 11:42:52 +0100178 X509_STORE_CTX_cleanup(&store_ctx);
179 crl = obj.data.crl;
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200180 if ((rc > 0) && crl) {
Michal Vasko11d4cdb2015-10-29 11:42:52 +0100181 next_update = X509_CRL_get_nextUpdate(crl);
182
183 /* verify the signature on this CRL */
184 pubkey = X509_get_pubkey(cert);
185 if (X509_CRL_verify(crl, pubkey) <= 0) {
186 X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_CRL_SIGNATURE_FAILURE);
187 X509_OBJECT_free_contents(&obj);
188 if (pubkey) {
189 EVP_PKEY_free(pubkey);
190 }
191 return 0; /* fail */
192 }
193 if (pubkey) {
194 EVP_PKEY_free(pubkey);
195 }
196
197 /* check date of CRL to make sure it's not expired */
198 if (!next_update) {
199 X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD);
200 X509_OBJECT_free_contents(&obj);
201 return 0; /* fail */
202 }
203 if (X509_cmp_current_time(next_update) < 0) {
204 X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_CRL_HAS_EXPIRED);
205 X509_OBJECT_free_contents(&obj);
206 return 0; /* fail */
207 }
208 X509_OBJECT_free_contents(&obj);
209 }
210
211 /* try to retrieve a CRL corresponding to the _issuer_ of
212 * the current certificate in order to check for revocation */
213 memset((char *)&obj, 0, sizeof obj);
Michal Vasko3031aae2016-01-27 16:07:18 +0100214 X509_STORE_CTX_init(&store_ctx, opts->crl_store, NULL, NULL);
Rosen Penev4f552d62019-06-26 16:10:43 -0700215 rc = X509_STORE_CTX_get_by_subject(&store_ctx, X509_LU_CRL, issuer, &obj);
Michal Vasko11d4cdb2015-10-29 11:42:52 +0100216 X509_STORE_CTX_cleanup(&store_ctx);
217 crl = obj.data.crl;
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200218 if ((rc > 0) && crl) {
Michal Vasko11d4cdb2015-10-29 11:42:52 +0100219 /* check if the current certificate is revoked by this CRL */
220 n = sk_X509_REVOKED_num(X509_CRL_get_REVOKED(crl));
221 for (i = 0; i < n; i++) {
222 revoked = sk_X509_REVOKED_value(X509_CRL_get_REVOKED(crl), i);
223 if (ASN1_INTEGER_cmp(revoked->serialNumber, X509_get_serialNumber(cert)) == 0) {
Michal Vasko05532772021-06-03 12:12:38 +0200224 ERR(NULL, "Certificate revoked!");
Michal Vasko11d4cdb2015-10-29 11:42:52 +0100225 X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_CERT_REVOKED);
226 X509_OBJECT_free_contents(&obj);
227 return 0; /* fail */
228 }
229 }
230 X509_OBJECT_free_contents(&obj);
231 }
232
233 return 1; /* success */
234}
235
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100236#endif
237
Kevin Barrettd37d2fb2019-08-28 14:25:34 -0400238void
Michal Vaskoe22c6732016-01-29 11:03:02 +0100239_nc_client_tls_destroy_opts(struct nc_client_tls_opts *opts)
240{
241 free(opts->cert_path);
242 free(opts->key_path);
243 free(opts->ca_file);
244 free(opts->ca_dir);
245 SSL_CTX_free(opts->tls_ctx);
246
247 free(opts->crl_file);
248 free(opts->crl_dir);
249 X509_STORE_free(opts->crl_store);
Radek Krejci5cebc6b2017-05-26 13:24:38 +0200250
251 memset(opts, 0, sizeof *opts);
Michal Vaskoe22c6732016-01-29 11:03:02 +0100252}
253
Michal Vaskob7558c52016-02-26 15:04:19 +0100254void
Michal Vaskoe22c6732016-01-29 11:03:02 +0100255nc_client_tls_destroy_opts(void)
256{
257 _nc_client_tls_destroy_opts(&tls_opts);
258 _nc_client_tls_destroy_opts(&tls_ch_opts);
259}
260
Michal Vasko3031aae2016-01-27 16:07:18 +0100261static int
Michal Vaskoe22c6732016-01-29 11:03:02 +0100262_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 +0100263{
Michal Vasko11d4cdb2015-10-29 11:42:52 +0100264 if (!client_cert) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200265 ERRARG("client_cert");
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100266 return -1;
Michal Vasko11d4cdb2015-10-29 11:42:52 +0100267 }
268
Michal Vasko3031aae2016-01-27 16:07:18 +0100269 free(opts->cert_path);
270 free(opts->key_path);
Michal Vasko11d4cdb2015-10-29 11:42:52 +0100271
Michal Vasko9db2a6f2016-02-01 13:26:03 +0100272 opts->cert_path = strdup(client_cert);
273 if (!opts->cert_path) {
274 ERRMEM;
275 return -1;
Michal Vasko11d4cdb2015-10-29 11:42:52 +0100276 }
277
Michal Vasko3031aae2016-01-27 16:07:18 +0100278 if (client_key) {
279 opts->key_path = strdup(client_key);
Michal Vasko9db2a6f2016-02-01 13:26:03 +0100280 if (!opts->key_path) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100281 ERRMEM;
282 return -1;
283 }
284 } else {
285 opts->key_path = NULL;
Michal Vasko11d4cdb2015-10-29 11:42:52 +0100286 }
287
Michal Vasko3031aae2016-01-27 16:07:18 +0100288 opts->tls_ctx_change = 1;
Michal Vasko11d4cdb2015-10-29 11:42:52 +0100289
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100290 return 0;
Michal Vasko11d4cdb2015-10-29 11:42:52 +0100291}
292
Michal Vasko3031aae2016-01-27 16:07:18 +0100293API int
Michal Vaskoe22c6732016-01-29 11:03:02 +0100294nc_client_tls_set_cert_key_paths(const char *client_cert, const char *client_key)
Michal Vasko11d4cdb2015-10-29 11:42:52 +0100295{
Michal Vaskoe22c6732016-01-29 11:03:02 +0100296 return _nc_client_tls_set_cert_key_paths(client_cert, client_key, &tls_opts);
Michal Vasko3031aae2016-01-27 16:07:18 +0100297}
298
299API int
Michal Vaskoe22c6732016-01-29 11:03:02 +0100300nc_client_tls_ch_set_cert_key_paths(const char *client_cert, const char *client_key)
Michal Vasko3031aae2016-01-27 16:07:18 +0100301{
Michal Vaskoe22c6732016-01-29 11:03:02 +0100302 return _nc_client_tls_set_cert_key_paths(client_cert, client_key, &tls_ch_opts);
303}
304
305static void
306_nc_client_tls_get_cert_key_paths(const char **client_cert, const char **client_key, struct nc_client_tls_opts *opts)
307{
308 if (!client_cert && !client_key) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200309 ERRARG("client_cert and client_key");
Michal Vaskoe22c6732016-01-29 11:03:02 +0100310 return;
311 }
312
313 if (client_cert) {
314 *client_cert = opts->cert_path;
315 }
316 if (client_key) {
317 *client_key = opts->key_path;
318 }
319}
320
321API void
322nc_client_tls_get_cert_key_paths(const char **client_cert, const char **client_key)
323{
324 _nc_client_tls_get_cert_key_paths(client_cert, client_key, &tls_opts);
325}
326
327API void
328nc_client_tls_ch_get_cert_key_paths(const char **client_cert, const char **client_key)
329{
330 _nc_client_tls_get_cert_key_paths(client_cert, client_key, &tls_ch_opts);
Michal Vasko3031aae2016-01-27 16:07:18 +0100331}
332
333static int
Michal Vaskoe22c6732016-01-29 11:03:02 +0100334_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 +0100335{
Michal Vasko3031aae2016-01-27 16:07:18 +0100336 if (!ca_file && !ca_dir) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200337 ERRARG("ca_file and ca_dir");
Michal Vasko3031aae2016-01-27 16:07:18 +0100338 return -1;
339 }
340
Michal Vasko3031aae2016-01-27 16:07:18 +0100341 free(opts->ca_file);
342 free(opts->ca_dir);
343
344 if (ca_file) {
345 opts->ca_file = strdup(ca_file);
346 if (!opts->ca_file) {
347 ERRMEM;
348 return -1;
349 }
350 } else {
351 opts->ca_file = NULL;
352 }
353
354 if (ca_dir) {
355 opts->ca_dir = strdup(ca_dir);
356 if (!opts->ca_dir) {
357 ERRMEM;
358 return -1;
359 }
360 } else {
361 opts->ca_dir = NULL;
362 }
363
364 opts->tls_ctx_change = 1;
365
366 return 0;
367}
368
369API int
Michal Vaskoe22c6732016-01-29 11:03:02 +0100370nc_client_tls_set_trusted_ca_paths(const char *ca_file, const char *ca_dir)
Michal Vasko3031aae2016-01-27 16:07:18 +0100371{
Michal Vaskoe22c6732016-01-29 11:03:02 +0100372 return _nc_client_tls_set_trusted_ca_paths(ca_file, ca_dir, &tls_opts);
Michal Vasko3031aae2016-01-27 16:07:18 +0100373}
374
375API int
Michal Vaskoe22c6732016-01-29 11:03:02 +0100376nc_client_tls_ch_set_trusted_ca_paths(const char *ca_file, const char *ca_dir)
Michal Vasko3031aae2016-01-27 16:07:18 +0100377{
Michal Vaskoe22c6732016-01-29 11:03:02 +0100378 return _nc_client_tls_set_trusted_ca_paths(ca_file, ca_dir, &tls_ch_opts);
379}
380
381static void
382_nc_client_tls_get_trusted_ca_paths(const char **ca_file, const char **ca_dir, struct nc_client_tls_opts *opts)
383{
384 if (!ca_file && !ca_dir) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200385 ERRARG("ca_file and ca_dir");
Michal Vaskoe22c6732016-01-29 11:03:02 +0100386 return;
387 }
388
389 if (ca_file) {
390 *ca_file = opts->ca_file;
391 }
392 if (ca_dir) {
393 *ca_dir = opts->ca_dir;
394 }
395}
396
397API void
398nc_client_tls_get_trusted_ca_paths(const char **ca_file, const char **ca_dir)
399{
400 _nc_client_tls_get_trusted_ca_paths(ca_file, ca_dir, &tls_opts);
401}
402
403API void
404nc_client_tls_ch_get_trusted_ca_paths(const char **ca_file, const char **ca_dir)
405{
406 _nc_client_tls_get_trusted_ca_paths(ca_file, ca_dir, &tls_ch_opts);
Michal Vasko3031aae2016-01-27 16:07:18 +0100407}
408
409static int
Michal Vaskoe22c6732016-01-29 11:03:02 +0100410_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 +0100411{
Michal Vasko3031aae2016-01-27 16:07:18 +0100412 if (!crl_file && !crl_dir) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200413 ERRARG("crl_file and crl_dir");
Michal Vasko3031aae2016-01-27 16:07:18 +0100414 return -1;
415 }
416
Michal Vasko3031aae2016-01-27 16:07:18 +0100417 free(opts->crl_file);
418 free(opts->crl_dir);
419
420 if (crl_file) {
421 opts->crl_file = strdup(crl_file);
422 if (!opts->crl_file) {
423 ERRMEM;
424 return -1;
425 }
426 } else {
427 opts->crl_file = NULL;
428 }
429
430 if (crl_dir) {
431 opts->crl_dir = strdup(crl_dir);
432 if (!opts->crl_dir) {
433 ERRMEM;
434 return -1;
435 }
436 } else {
437 opts->crl_dir = NULL;
438 }
439
440 opts->crl_store_change = 1;
441
442 return 0;
443}
444
445API int
Michal Vaskoe22c6732016-01-29 11:03:02 +0100446nc_client_tls_set_crl_paths(const char *crl_file, const char *crl_dir)
Michal Vasko3031aae2016-01-27 16:07:18 +0100447{
Michal Vaskoe22c6732016-01-29 11:03:02 +0100448 return _nc_client_tls_set_crl_paths(crl_file, crl_dir, &tls_opts);
Michal Vasko3031aae2016-01-27 16:07:18 +0100449}
450
451API int
Michal Vaskoe22c6732016-01-29 11:03:02 +0100452nc_client_tls_ch_set_crl_paths(const char *crl_file, const char *crl_dir)
Michal Vasko3031aae2016-01-27 16:07:18 +0100453{
Michal Vaskoe22c6732016-01-29 11:03:02 +0100454 return _nc_client_tls_set_crl_paths(crl_file, crl_dir, &tls_ch_opts);
455}
456
457static void
458_nc_client_tls_get_crl_paths(const char **crl_file, const char **crl_dir, struct nc_client_tls_opts *opts)
459{
460 if (!crl_file && !crl_dir) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200461 ERRARG("crl_file and crl_dir");
Michal Vaskoe22c6732016-01-29 11:03:02 +0100462 return;
463 }
464
465 if (crl_file) {
466 *crl_file = opts->crl_file;
467 }
468 if (crl_dir) {
469 *crl_dir = opts->crl_dir;
470 }
471}
472
473API void
474nc_client_tls_get_crl_paths(const char **crl_file, const char **crl_dir)
475{
476 _nc_client_tls_get_crl_paths(crl_file, crl_dir, &tls_opts);
477}
478
479API void
480nc_client_tls_ch_get_crl_paths(const char **crl_file, const char **crl_dir)
481{
482 _nc_client_tls_get_crl_paths(crl_file, crl_dir, &tls_ch_opts);
Michal Vasko3031aae2016-01-27 16:07:18 +0100483}
484
485API int
486nc_client_tls_ch_add_bind_listen(const char *address, uint16_t port)
487{
488 return nc_client_ch_add_bind_listen(address, port, NC_TI_OPENSSL);
489}
490
491API int
492nc_client_tls_ch_del_bind(const char *address, uint16_t port)
493{
494 return nc_client_ch_del_bind(address, port, NC_TI_OPENSSL);
495}
496
Michal Vasko3031aae2016-01-27 16:07:18 +0100497static int
Michal Vaskoaf58cd92016-01-28 11:56:02 +0100498nc_client_tls_update_opts(struct nc_client_tls_opts *opts)
Michal Vasko3031aae2016-01-27 16:07:18 +0100499{
500 char *key;
501 X509_LOOKUP *lookup;
Michal Vasko3031aae2016-01-27 16:07:18 +0100502
503 if (!opts->tls_ctx || opts->tls_ctx_change) {
Michal Vaskoe22c6732016-01-29 11:03:02 +0100504 SSL_CTX_free(opts->tls_ctx);
Michal Vasko3031aae2016-01-27 16:07:18 +0100505
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100506#if OPENSSL_VERSION_NUMBER >= 0x10100000L // >= 1.1.0
507 /* prepare global SSL context, highest available method is negotiated autmatically */
508 if (!(opts->tls_ctx = SSL_CTX_new(TLS_client_method())))
509#else
Michal Vasko3031aae2016-01-27 16:07:18 +0100510 /* prepare global SSL context, allow only mandatory TLS 1.2 */
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100511 if (!(opts->tls_ctx = SSL_CTX_new(TLSv1_2_client_method())))
512#endif
513 {
Michal Vasko05532772021-06-03 12:12:38 +0200514 ERR(NULL, "Unable to create OpenSSL context (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vasko3031aae2016-01-27 16:07:18 +0100515 return -1;
516 }
517 SSL_CTX_set_verify(opts->tls_ctx, SSL_VERIFY_PEER, tlsauth_verify_callback);
518
519 /* get peer certificate */
520 if (SSL_CTX_use_certificate_file(opts->tls_ctx, opts->cert_path, SSL_FILETYPE_PEM) != 1) {
Michal Vasko05532772021-06-03 12:12:38 +0200521 ERR(NULL, "Loading the client certificate from \'%s\' failed (%s).", opts->cert_path,
522 ERR_reason_error_string(ERR_get_error()));
Michal Vasko3031aae2016-01-27 16:07:18 +0100523 return -1;
524 }
525
526 /* if the file with private key not specified, expect that the private key is stored with the certificate */
527 if (!opts->key_path) {
528 key = opts->cert_path;
529 } else {
530 key = opts->key_path;
531 }
532 if (SSL_CTX_use_PrivateKey_file(opts->tls_ctx, key, SSL_FILETYPE_PEM) != 1) {
Michal Vasko05532772021-06-03 12:12:38 +0200533 ERR(NULL, "Loading the client priavte key from \'%s\' failed (%s).", key,
534 ERR_reason_error_string(ERR_get_error()));
Michal Vasko3031aae2016-01-27 16:07:18 +0100535 return -1;
536 }
537
538 if (!SSL_CTX_load_verify_locations(opts->tls_ctx, opts->ca_file, opts->ca_dir)) {
Michal Vasko05532772021-06-03 12:12:38 +0200539 ERR(NULL, "Failed to load the locations of trusted CA certificates (%s).",
540 ERR_reason_error_string(ERR_get_error()));
Michal Vasko3031aae2016-01-27 16:07:18 +0100541 return -1;
542 }
543 }
544
545 if (opts->crl_store_change || (!opts->crl_store && (opts->crl_file || opts->crl_dir))) {
546 /* set the revocation store with the correct paths for the callback */
547 X509_STORE_free(opts->crl_store);
548
549 opts->crl_store = X509_STORE_new();
550 if (!opts->crl_store) {
Michal Vasko05532772021-06-03 12:12:38 +0200551 ERR(NULL, "Unable to create a certificate store (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vasko3031aae2016-01-27 16:07:18 +0100552 return -1;
553 }
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100554
555#if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0
556 /* whaveter this does... */
Michal Vasko3031aae2016-01-27 16:07:18 +0100557 opts->crl_store->cache = 0;
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100558#endif
Michal Vasko3031aae2016-01-27 16:07:18 +0100559
560 if (opts->crl_file) {
561 if (!(lookup = X509_STORE_add_lookup(opts->crl_store, X509_LOOKUP_file()))) {
Michal Vasko05532772021-06-03 12:12:38 +0200562 ERR(NULL, "Failed to add lookup method to CRL checking.");
Michal Vasko3031aae2016-01-27 16:07:18 +0100563 return -1;
564 }
565 if (X509_LOOKUP_add_dir(lookup, opts->crl_file, X509_FILETYPE_PEM) != 1) {
Michal Vasko05532772021-06-03 12:12:38 +0200566 ERR(NULL, "Failed to add the revocation lookup file \"%s\".", opts->crl_file);
Michal Vasko3031aae2016-01-27 16:07:18 +0100567 return -1;
568 }
569 }
570
571 if (opts->crl_dir) {
572 if (!(lookup = X509_STORE_add_lookup(opts->crl_store, X509_LOOKUP_hash_dir()))) {
Michal Vasko05532772021-06-03 12:12:38 +0200573 ERR(NULL, "Failed to add lookup method to CRL checking.");
Michal Vasko3031aae2016-01-27 16:07:18 +0100574 return -1;
575 }
576 if (X509_LOOKUP_add_dir(lookup, opts->crl_dir, X509_FILETYPE_PEM) != 1) {
Michal Vasko05532772021-06-03 12:12:38 +0200577 ERR(NULL, "Failed to add the revocation lookup directory \"%s\".", opts->crl_dir);
Michal Vasko3031aae2016-01-27 16:07:18 +0100578 return -1;
579 }
580 }
581 }
582
583 return 0;
Radek Krejci9f03b482015-10-22 16:02:10 +0200584}
585
586API struct nc_session *
Michal Vasko9bee18d2015-12-08 14:41:42 +0100587nc_connect_tls(const char *host, unsigned short port, struct ly_ctx *ctx)
Radek Krejci9f03b482015-10-22 16:02:10 +0200588{
Radek Krejci9f03b482015-10-22 16:02:10 +0200589 struct nc_session *session = NULL;
Michal Vasko0190bc32016-03-02 15:47:49 +0100590 int sock, verify, ret;
Michal Vasko157de8a2021-09-03 13:28:07 +0200591 unsigned long tls_err;
Michal Vasko36c7be82017-02-22 13:37:59 +0100592 struct timespec ts_timeout, ts_cur;
Michal Vasko157de8a2021-09-03 13:28:07 +0200593 const char *peername;
Michal Vasko66032bc2019-01-22 15:03:12 +0100594 char *ip_host = NULL;
Michal Vasko11d4cdb2015-10-29 11:42:52 +0100595
Michal Vasko3031aae2016-01-27 16:07:18 +0100596 if (!tls_opts.cert_path || (!tls_opts.ca_file && !tls_opts.ca_dir)) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200597 ERRINIT;
Michal Vasko11d4cdb2015-10-29 11:42:52 +0100598 return NULL;
599 }
Radek Krejci9f03b482015-10-22 16:02:10 +0200600
601 /* process parameters */
602 if (!host || strisempty(host)) {
603 host = "localhost";
604 }
605
606 if (!port) {
607 port = NC_PORT_TLS;
608 }
609
Michal Vasko3031aae2016-01-27 16:07:18 +0100610 /* create/update TLS structures */
Michal Vaskoaf58cd92016-01-28 11:56:02 +0100611 if (nc_client_tls_update_opts(&tls_opts)) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100612 return NULL;
613 }
614
Radek Krejci9f03b482015-10-22 16:02:10 +0200615 /* prepare session structure */
Michal Vasko131120a2018-05-29 15:44:02 +0200616 session = nc_new_session(NC_CLIENT, 0);
Radek Krejci9f03b482015-10-22 16:02:10 +0200617 if (!session) {
Michal Vasko9e2d3a32015-11-10 13:09:18 +0100618 ERRMEM;
Radek Krejci9f03b482015-10-22 16:02:10 +0200619 return NULL;
620 }
Michal Vasko9e2d3a32015-11-10 13:09:18 +0100621 session->status = NC_STATUS_STARTING;
Michal Vasko11d4cdb2015-10-29 11:42:52 +0100622
623 /* fill the session */
Michal Vasko9e2d3a32015-11-10 13:09:18 +0100624 session->ti_type = NC_TI_OPENSSL;
Michal Vasko11d4cdb2015-10-29 11:42:52 +0100625 if (!(session->ti.tls = SSL_new(tls_opts.tls_ctx))) {
Michal Vasko05532772021-06-03 12:12:38 +0200626 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 +0100627 goto fail;
628 }
Radek Krejci9f03b482015-10-22 16:02:10 +0200629
Michal Vasko11d4cdb2015-10-29 11:42:52 +0100630 /* create and assign socket */
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200631 sock = nc_sock_connect(host, port, -1, &client_opts.ka, NULL, &ip_host);
Michal Vasko11d4cdb2015-10-29 11:42:52 +0100632 if (sock == -1) {
Michal Vasko05532772021-06-03 12:12:38 +0200633 ERR(NULL, "Unable to connect to %s:%u (%s).", host, port, strerror(errno));
Michal Vasko11d4cdb2015-10-29 11:42:52 +0100634 goto fail;
635 }
636 SSL_set_fd(session->ti.tls, sock);
637
638 /* set the SSL_MODE_AUTO_RETRY flag to allow OpenSSL perform re-handshake automatically */
639 SSL_set_mode(session->ti.tls, SSL_MODE_AUTO_RETRY);
640
Michal Vaskod2871972021-09-14 11:59:00 +0200641#if OPENSSL_VERSION_NUMBER >= 0x10100000L // >= 1.1.0
Michal Vaskoad26f992021-09-03 13:03:57 +0200642 /* server identity (hostname) verification */
643 if (!SSL_set1_host(session->ti.tls, host)) {
644 ERR(NULL, "Failed to set expected server hostname.");
645 goto fail;
646 }
Michal Vaskod2871972021-09-14 11:59:00 +0200647#endif
Michal Vaskoad26f992021-09-03 13:03:57 +0200648
Michal Vasko11d4cdb2015-10-29 11:42:52 +0100649 /* connect and perform the handshake */
Michal Vaskoe852c8b2017-10-05 10:02:20 +0200650 nc_gettimespec_mono(&ts_timeout);
Michal Vasko36c7be82017-02-22 13:37:59 +0100651 nc_addtimespec(&ts_timeout, NC_TRANSPORT_TIMEOUT);
Michal Vasko3031aae2016-01-27 16:07:18 +0100652 tlsauth_ch = 0;
Michal Vaskod51a2642021-09-03 13:03:24 +0200653 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 +0100654 usleep(NC_TIMEOUT_STEP);
Michal Vaskoe852c8b2017-10-05 10:02:20 +0200655 nc_gettimespec_mono(&ts_cur);
Michal Vasko36c7be82017-02-22 13:37:59 +0100656 if (nc_difftimespec(&ts_cur, &ts_timeout) < 1) {
Michal Vasko05532772021-06-03 12:12:38 +0200657 ERR(NULL, "SSL_connect timeout.");
Michal Vasko0190bc32016-03-02 15:47:49 +0100658 goto fail;
659 }
660 }
661 if (ret != 1) {
662 switch (SSL_get_error(session->ti.tls, ret)) {
663 case SSL_ERROR_SYSCALL:
Michal Vasko0272dc32021-09-03 13:02:20 +0200664 ERR(NULL, "SSL_connect failed (%s).", errno ? strerror(errno) : "unexpected EOF");
Michal Vasko0190bc32016-03-02 15:47:49 +0100665 break;
666 case SSL_ERROR_SSL:
Michal Vasko157de8a2021-09-03 13:28:07 +0200667 tls_err = ERR_get_error();
Michal Vaskod51a2642021-09-03 13:03:24 +0200668 ERR(NULL, "SSL_connect failed (%s).", ERR_reason_error_string(tls_err));
Michal Vasko0190bc32016-03-02 15:47:49 +0100669 break;
670 default:
Michal Vasko05532772021-06-03 12:12:38 +0200671 ERR(NULL, "SSL_connect failed.");
Michal Vasko0190bc32016-03-02 15:47:49 +0100672 break;
673 }
Michal Vasko11d4cdb2015-10-29 11:42:52 +0100674 goto fail;
675 }
676
677 /* check certificate verification result */
678 verify = SSL_get_verify_result(session->ti.tls);
679 switch (verify) {
680 case X509_V_OK:
Michal Vaskod2871972021-09-14 11:59:00 +0200681#if OPENSSL_VERSION_NUMBER >= 0x10100000L // >= 1.1.0
Michal Vasko157de8a2021-09-03 13:28:07 +0200682 peername = SSL_get0_peername(session->ti.tls);
Michal Vaskoad26f992021-09-03 13:03:57 +0200683 VRB(NULL, "Server certificate successfully verified (domain \"%s\").", peername ? peername : "<unknown>");
Michal Vaskod2871972021-09-14 11:59:00 +0200684#else
685 (void)peername;
686 VRB(NULL, "Server certificate successfully verified.");
687#endif
Michal Vasko11d4cdb2015-10-29 11:42:52 +0100688 break;
689 default:
Michal Vasko05532772021-06-03 12:12:38 +0200690 WRN(NULL, "Server certificate verification problem (%s).", X509_verify_cert_error_string(verify));
Radek Krejci9f03b482015-10-22 16:02:10 +0200691 }
692
Radek Krejcifd5b6682017-06-13 15:52:53 +0200693 if (nc_session_new_ctx(session, ctx) != EXIT_SUCCESS) {
694 goto fail;
Michal Vasko9e2d3a32015-11-10 13:09:18 +0100695 }
Radek Krejcifd5b6682017-06-13 15:52:53 +0200696 ctx = session->ctx;
Michal Vasko9e2d3a32015-11-10 13:09:18 +0100697
Radek Krejci9f03b482015-10-22 16:02:10 +0200698 /* NETCONF handshake */
Michal Vasko131120a2018-05-29 15:44:02 +0200699 if (nc_handshake_io(session) != NC_MSG_HELLO) {
Michal Vasko11d4cdb2015-10-29 11:42:52 +0100700 goto fail;
Radek Krejci9f03b482015-10-22 16:02:10 +0200701 }
Michal Vaskoad611702015-12-03 13:41:51 +0100702 session->status = NC_STATUS_RUNNING;
Radek Krejci9f03b482015-10-22 16:02:10 +0200703
Michal Vaskoef578332016-01-25 13:20:09 +0100704 if (nc_ctx_check_and_fill(session) == -1) {
Michal Vasko57eb9402015-12-08 14:38:12 +0100705 goto fail;
Michal Vasko9e2d3a32015-11-10 13:09:18 +0100706 }
707
708 /* store information into session and the dictionary */
Michal Vasko93224072021-11-09 12:14:28 +0100709 session->host = ip_host;
Michal Vasko9e2d3a32015-11-10 13:09:18 +0100710 session->port = port;
Michal Vasko93224072021-11-09 12:14:28 +0100711 session->username = strdup("certificate-based");
Michal Vasko9e2d3a32015-11-10 13:09:18 +0100712
Radek Krejci9f03b482015-10-22 16:02:10 +0200713 return session;
714
Michal Vasko11d4cdb2015-10-29 11:42:52 +0100715fail:
Michal Vasko66032bc2019-01-22 15:03:12 +0100716 free(ip_host);
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100717 nc_session_free(session, NULL);
Radek Krejci9f03b482015-10-22 16:02:10 +0200718 return NULL;
719}
720
721API struct nc_session *
722nc_connect_libssl(SSL *tls, struct ly_ctx *ctx)
723{
Michal Vasko11d4cdb2015-10-29 11:42:52 +0100724 struct nc_session *session;
Radek Krejci9f03b482015-10-22 16:02:10 +0200725
Michal Vasko45e53ae2016-04-07 11:46:03 +0200726 if (!tls) {
727 ERRARG("tls");
728 return NULL;
729 } else if (!SSL_is_init_finished(tls)) {
Michal Vasko05532772021-06-03 12:12:38 +0200730 ERR(NULL, "Supplied TLS session is not fully connected!");
Michal Vasko11d4cdb2015-10-29 11:42:52 +0100731 return NULL;
732 }
733
734 /* prepare session structure */
Michal Vasko131120a2018-05-29 15:44:02 +0200735 session = nc_new_session(NC_CLIENT, 0);
Michal Vasko11d4cdb2015-10-29 11:42:52 +0100736 if (!session) {
Michal Vasko9e2d3a32015-11-10 13:09:18 +0100737 ERRMEM;
Michal Vasko11d4cdb2015-10-29 11:42:52 +0100738 return NULL;
739 }
Michal Vasko9e2d3a32015-11-10 13:09:18 +0100740 session->status = NC_STATUS_STARTING;
Michal Vasko9e2d3a32015-11-10 13:09:18 +0100741 session->ti_type = NC_TI_OPENSSL;
Michal Vasko11d4cdb2015-10-29 11:42:52 +0100742 session->ti.tls = tls;
743
Radek Krejcifd5b6682017-06-13 15:52:53 +0200744 if (nc_session_new_ctx(session, ctx) != EXIT_SUCCESS) {
745 goto fail;
Michal Vasko11d4cdb2015-10-29 11:42:52 +0100746 }
Radek Krejcifd5b6682017-06-13 15:52:53 +0200747 ctx = session->ctx;
Michal Vasko11d4cdb2015-10-29 11:42:52 +0100748
749 /* NETCONF handshake */
Michal Vasko131120a2018-05-29 15:44:02 +0200750 if (nc_handshake_io(session) != NC_MSG_HELLO) {
Michal Vasko11d4cdb2015-10-29 11:42:52 +0100751 goto fail;
752 }
Michal Vaskoad611702015-12-03 13:41:51 +0100753 session->status = NC_STATUS_RUNNING;
Michal Vasko11d4cdb2015-10-29 11:42:52 +0100754
Michal Vaskoef578332016-01-25 13:20:09 +0100755 if (nc_ctx_check_and_fill(session) == -1) {
Michal Vasko57eb9402015-12-08 14:38:12 +0100756 goto fail;
Michal Vasko9e2d3a32015-11-10 13:09:18 +0100757 }
758
Michal Vasko11d4cdb2015-10-29 11:42:52 +0100759 return session;
760
761fail:
Michal Vaskoa42a7f02021-07-02 08:43:12 +0200762 session->ti.tls = NULL;
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100763 nc_session_free(session, NULL);
Radek Krejci9f03b482015-10-22 16:02:10 +0200764 return NULL;
765}
766
Michal Vasko3031aae2016-01-27 16:07:18 +0100767struct nc_session *
Michal Vasko0190bc32016-03-02 15:47:49 +0100768nc_accept_callhome_tls_sock(int sock, const char *host, uint16_t port, struct ly_ctx *ctx, int timeout)
Michal Vasko80cad7f2015-12-08 14:42:27 +0100769{
Michal Vasko36c7be82017-02-22 13:37:59 +0100770 int verify, ret;
Michal Vaskoc64c38c2021-07-02 08:43:53 +0200771 SSL *tls = NULL;
772 struct nc_session *session = NULL;
Michal Vasko36c7be82017-02-22 13:37:59 +0100773 struct timespec ts_timeout, ts_cur;
Michal Vasko80cad7f2015-12-08 14:42:27 +0100774
Michal Vaskoaf58cd92016-01-28 11:56:02 +0100775 if (nc_client_tls_update_opts(&tls_ch_opts)) {
Michal Vaskoc64c38c2021-07-02 08:43:53 +0200776 goto cleanup;
Michal Vaskoc61c4492016-01-25 11:13:34 +0100777 }
778
Michal Vasko3031aae2016-01-27 16:07:18 +0100779 if (!(tls = SSL_new(tls_ch_opts.tls_ctx))) {
Michal Vasko05532772021-06-03 12:12:38 +0200780 ERR(NULL, "Failed to create new TLS session structure (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vaskoc64c38c2021-07-02 08:43:53 +0200781 goto cleanup;
Michal Vasko80cad7f2015-12-08 14:42:27 +0100782 }
783
784 SSL_set_fd(tls, sock);
785
786 /* set the SSL_MODE_AUTO_RETRY flag to allow OpenSSL perform re-handshake automatically */
787 SSL_set_mode(tls, SSL_MODE_AUTO_RETRY);
788
789 /* connect and perform the handshake */
Michal Vasko36c7be82017-02-22 13:37:59 +0100790 if (timeout > -1) {
Michal Vaskoe852c8b2017-10-05 10:02:20 +0200791 nc_gettimespec_mono(&ts_timeout);
Michal Vasko36c7be82017-02-22 13:37:59 +0100792 nc_addtimespec(&ts_timeout, timeout);
793 }
Michal Vasko3031aae2016-01-27 16:07:18 +0100794 tlsauth_ch = 1;
Michal Vasko0190bc32016-03-02 15:47:49 +0100795 while (((ret = SSL_connect(tls)) == -1) && (SSL_get_error(tls, ret) == SSL_ERROR_WANT_READ)) {
796 usleep(NC_TIMEOUT_STEP);
Michal Vasko36c7be82017-02-22 13:37:59 +0100797 if (timeout > -1) {
Michal Vaskoe852c8b2017-10-05 10:02:20 +0200798 nc_gettimespec_mono(&ts_cur);
Michal Vasko36c7be82017-02-22 13:37:59 +0100799 if (nc_difftimespec(&ts_cur, &ts_timeout) < 1) {
Michal Vasko05532772021-06-03 12:12:38 +0200800 ERR(NULL, "SSL_connect timeout.");
Michal Vaskoc64c38c2021-07-02 08:43:53 +0200801 goto cleanup;
Michal Vasko36c7be82017-02-22 13:37:59 +0100802 }
Michal Vasko0190bc32016-03-02 15:47:49 +0100803 }
804 }
805 if (ret != 1) {
806 switch (SSL_get_error(tls, ret)) {
807 case SSL_ERROR_SYSCALL:
Michal Vasko05532772021-06-03 12:12:38 +0200808 ERR(NULL, "SSL_connect failed (%s).", strerror(errno));
Michal Vasko0190bc32016-03-02 15:47:49 +0100809 break;
810 case SSL_ERROR_SSL:
Michal Vasko05532772021-06-03 12:12:38 +0200811 ERR(NULL, "SSL_connect failed (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vasko0190bc32016-03-02 15:47:49 +0100812 break;
813 default:
Michal Vasko05532772021-06-03 12:12:38 +0200814 ERR(NULL, "SSL_connect failed.");
Michal Vasko0190bc32016-03-02 15:47:49 +0100815 break;
816 }
Michal Vaskoc64c38c2021-07-02 08:43:53 +0200817 goto cleanup;
Michal Vasko80cad7f2015-12-08 14:42:27 +0100818 }
819
820 /* check certificate verification result */
821 verify = SSL_get_verify_result(tls);
822 switch (verify) {
823 case X509_V_OK:
Michal Vasko05532772021-06-03 12:12:38 +0200824 VRB(NULL, "Server certificate successfully verified.");
Michal Vasko80cad7f2015-12-08 14:42:27 +0100825 break;
826 default:
Michal Vasko05532772021-06-03 12:12:38 +0200827 WRN(NULL, "Server certificate verification problem (%s).", X509_verify_cert_error_string(verify));
Michal Vasko80cad7f2015-12-08 14:42:27 +0100828 }
829
Michal Vaskoc64c38c2021-07-02 08:43:53 +0200830 /* connect */
Michal Vasko80cad7f2015-12-08 14:42:27 +0100831 session = nc_connect_libssl(tls, ctx);
Michal Vaskoc64c38c2021-07-02 08:43:53 +0200832 if (!session) {
833 goto cleanup;
Michal Vasko80cad7f2015-12-08 14:42:27 +0100834 }
835
Michal Vaskoc64c38c2021-07-02 08:43:53 +0200836 session->flags |= NC_SESSION_CALLHOME;
837
838 /* store information into session and the dictionary */
Michal Vasko93224072021-11-09 12:14:28 +0100839 session->host = strdup(host);
Michal Vaskoc64c38c2021-07-02 08:43:53 +0200840 session->port = port;
Michal Vasko93224072021-11-09 12:14:28 +0100841 session->username = strdup("certificate-based");
Michal Vaskoc64c38c2021-07-02 08:43:53 +0200842
843cleanup:
844 if (!session) {
845 SSL_free(tls);
846 close(sock);
847 }
Michal Vasko80cad7f2015-12-08 14:42:27 +0100848 return session;
849}