blob: 4cfc5e8fbac1f0fc5b266f1bcff642280827cb62 [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
Michal Vaskoba9f3582023-02-22 10:26:32 +010019#define _GNU_SOURCE /* pthread_rwlock_t, strdup */
20
Radek Krejci9f03b482015-10-22 16:02:10 +020021#include <assert.h>
22#include <errno.h>
Radek Krejci9f03b482015-10-22 16:02:10 +020023#include <string.h>
24#include <unistd.h>
25
26#include <libyang/libyang.h>
Michal Vasko086311b2016-01-08 09:53:11 +010027#include <openssl/err.h>
Michal Vaskob83a3fa2021-05-26 09:53:42 +020028#include <openssl/ossl_typ.h>
Michal Vaskodae48322016-02-25 14:49:48 +010029#include <openssl/x509.h>
Radek Krejci9f03b482015-10-22 16:02:10 +020030
roman3f9b65c2023-06-05 14:26:58 +020031#include "config.h"
32#include "log_p.h"
Michal Vaskoe22c6732016-01-29 11:03:02 +010033#include "session_client.h"
34#include "session_client_ch.h"
roman3f9b65c2023-06-05 14:26:58 +020035#include "session_p.h"
Michal Vasko11d4cdb2015-10-29 11:42:52 +010036
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 +010045static int
46tlsauth_verify_callback(int preverify_ok, X509_STORE_CTX *x509_ctx)
47{
48 X509_STORE_CTX *store_ctx;
49 X509_OBJECT *obj;
50 X509_NAME *subject, *issuer;
51 X509 *cert;
52 X509_CRL *crl;
53 X509_REVOKED *revoked;
54 EVP_PKEY *pubkey;
55 int i, n, rc;
56 const ASN1_TIME *next_update = NULL;
57 struct nc_client_tls_opts *opts;
58
59 if (!preverify_ok) {
60 return 0;
61 }
62
63 opts = (tlsauth_ch ? &tls_ch_opts : &tls_opts);
64
65 if (!opts->crl_store) {
66 /* nothing to check */
67 return 1;
68 }
69
70 cert = X509_STORE_CTX_get_current_cert(x509_ctx);
71 subject = X509_get_subject_name(cert);
72 issuer = X509_get_issuer_name(cert);
73
74 /* try to retrieve a CRL corresponding to the _subject_ of
75 * the current certificate in order to verify it's integrity */
76 store_ctx = X509_STORE_CTX_new();
77 obj = X509_OBJECT_new();
78 X509_STORE_CTX_init(store_ctx, opts->crl_store, NULL, NULL);
Rosen Penev4f552d62019-06-26 16:10:43 -070079 rc = X509_STORE_CTX_get_by_subject(store_ctx, X509_LU_CRL, subject, obj);
Michal Vasko18aeb5d2017-02-17 09:23:56 +010080 X509_STORE_CTX_free(store_ctx);
81 crl = X509_OBJECT_get0_X509_CRL(obj);
Michal Vaskob83a3fa2021-05-26 09:53:42 +020082 if ((rc > 0) && crl) {
Michal Vasko18aeb5d2017-02-17 09:23:56 +010083 next_update = X509_CRL_get0_nextUpdate(crl);
84
85 /* verify the signature on this CRL */
86 pubkey = X509_get_pubkey(cert);
87 if (X509_CRL_verify(crl, pubkey) <= 0) {
88 X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_CRL_SIGNATURE_FAILURE);
89 X509_OBJECT_free(obj);
90 if (pubkey) {
91 EVP_PKEY_free(pubkey);
92 }
93 return 0; /* fail */
94 }
95 if (pubkey) {
96 EVP_PKEY_free(pubkey);
97 }
98
99 /* check date of CRL to make sure it's not expired */
100 if (!next_update) {
101 X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD);
102 X509_OBJECT_free(obj);
103 return 0; /* fail */
104 }
105 if (X509_cmp_current_time(next_update) < 0) {
106 X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_CRL_HAS_EXPIRED);
107 X509_OBJECT_free(obj);
108 return 0; /* fail */
109 }
110 X509_OBJECT_free(obj);
111 }
112
113 /* try to retrieve a CRL corresponding to the _issuer_ of
114 * the current certificate in order to check for revocation */
115 store_ctx = X509_STORE_CTX_new();
116 obj = X509_OBJECT_new();
117 X509_STORE_CTX_init(store_ctx, opts->crl_store, NULL, NULL);
Rosen Penev4f552d62019-06-26 16:10:43 -0700118 rc = X509_STORE_CTX_get_by_subject(store_ctx, X509_LU_CRL, issuer, obj);
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100119 X509_STORE_CTX_free(store_ctx);
120 crl = X509_OBJECT_get0_X509_CRL(obj);
Michal Vaskob83a3fa2021-05-26 09:53:42 +0200121 if ((rc > 0) && crl) {
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100122 /* check if the current certificate is revoked by this CRL */
123 n = sk_X509_REVOKED_num(X509_CRL_get_REVOKED(crl));
124 for (i = 0; i < n; i++) {
125 revoked = sk_X509_REVOKED_value(X509_CRL_get_REVOKED(crl), i);
126 if (ASN1_INTEGER_cmp(X509_REVOKED_get0_serialNumber(revoked), X509_get_serialNumber(cert)) == 0) {
Michal Vasko05532772021-06-03 12:12:38 +0200127 ERR(NULL, "Certificate revoked!");
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100128 X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_CERT_REVOKED);
129 X509_OBJECT_free(obj);
130 return 0; /* fail */
131 }
132 }
133 X509_OBJECT_free(obj);
134 }
135
136 return 1; /* success */
137}
138
Kevin Barrettd37d2fb2019-08-28 14:25:34 -0400139void
Michal Vaskoe22c6732016-01-29 11:03:02 +0100140_nc_client_tls_destroy_opts(struct nc_client_tls_opts *opts)
141{
142 free(opts->cert_path);
143 free(opts->key_path);
144 free(opts->ca_file);
145 free(opts->ca_dir);
146 SSL_CTX_free(opts->tls_ctx);
147
148 free(opts->crl_file);
149 free(opts->crl_dir);
150 X509_STORE_free(opts->crl_store);
Radek Krejci5cebc6b2017-05-26 13:24:38 +0200151
152 memset(opts, 0, sizeof *opts);
Michal Vaskoe22c6732016-01-29 11:03:02 +0100153}
154
Michal Vaskob7558c52016-02-26 15:04:19 +0100155void
Michal Vaskoe22c6732016-01-29 11:03:02 +0100156nc_client_tls_destroy_opts(void)
157{
158 _nc_client_tls_destroy_opts(&tls_opts);
159 _nc_client_tls_destroy_opts(&tls_ch_opts);
160}
161
Michal Vasko3031aae2016-01-27 16:07:18 +0100162static int
Michal Vaskoe22c6732016-01-29 11:03:02 +0100163_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 +0100164{
roman40672412023-05-04 11:10:22 +0200165 NC_CHECK_ARG_RET(NULL, client_cert, -1);
Michal Vasko11d4cdb2015-10-29 11:42:52 +0100166
Michal Vasko3031aae2016-01-27 16:07:18 +0100167 free(opts->cert_path);
168 free(opts->key_path);
Michal Vasko11d4cdb2015-10-29 11:42:52 +0100169
Michal Vasko9db2a6f2016-02-01 13:26:03 +0100170 opts->cert_path = strdup(client_cert);
171 if (!opts->cert_path) {
172 ERRMEM;
173 return -1;
Michal Vasko11d4cdb2015-10-29 11:42:52 +0100174 }
175
Michal Vasko3031aae2016-01-27 16:07:18 +0100176 if (client_key) {
177 opts->key_path = strdup(client_key);
Michal Vasko9db2a6f2016-02-01 13:26:03 +0100178 if (!opts->key_path) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100179 ERRMEM;
180 return -1;
181 }
182 } else {
183 opts->key_path = NULL;
Michal Vasko11d4cdb2015-10-29 11:42:52 +0100184 }
185
Michal Vasko3031aae2016-01-27 16:07:18 +0100186 opts->tls_ctx_change = 1;
Michal Vasko11d4cdb2015-10-29 11:42:52 +0100187
Michal Vasko7f1c78b2016-01-19 09:52:14 +0100188 return 0;
Michal Vasko11d4cdb2015-10-29 11:42:52 +0100189}
190
Michal Vasko3031aae2016-01-27 16:07:18 +0100191API int
Michal Vaskoe22c6732016-01-29 11:03:02 +0100192nc_client_tls_set_cert_key_paths(const char *client_cert, const char *client_key)
Michal Vasko11d4cdb2015-10-29 11:42:52 +0100193{
Michal Vaskoe22c6732016-01-29 11:03:02 +0100194 return _nc_client_tls_set_cert_key_paths(client_cert, client_key, &tls_opts);
Michal Vasko3031aae2016-01-27 16:07:18 +0100195}
196
197API int
Michal Vaskoe22c6732016-01-29 11:03:02 +0100198nc_client_tls_ch_set_cert_key_paths(const char *client_cert, const char *client_key)
Michal Vasko3031aae2016-01-27 16:07:18 +0100199{
Michal Vaskoe22c6732016-01-29 11:03:02 +0100200 return _nc_client_tls_set_cert_key_paths(client_cert, client_key, &tls_ch_opts);
201}
202
203static void
204_nc_client_tls_get_cert_key_paths(const char **client_cert, const char **client_key, struct nc_client_tls_opts *opts)
205{
206 if (!client_cert && !client_key) {
roman40672412023-05-04 11:10:22 +0200207 ERRARG(NULL, "client_cert and client_key");
Michal Vaskoe22c6732016-01-29 11:03:02 +0100208 return;
209 }
210
211 if (client_cert) {
212 *client_cert = opts->cert_path;
213 }
214 if (client_key) {
215 *client_key = opts->key_path;
216 }
217}
218
219API void
220nc_client_tls_get_cert_key_paths(const char **client_cert, const char **client_key)
221{
222 _nc_client_tls_get_cert_key_paths(client_cert, client_key, &tls_opts);
223}
224
225API void
226nc_client_tls_ch_get_cert_key_paths(const char **client_cert, const char **client_key)
227{
228 _nc_client_tls_get_cert_key_paths(client_cert, client_key, &tls_ch_opts);
Michal Vasko3031aae2016-01-27 16:07:18 +0100229}
230
231static int
Michal Vaskoe22c6732016-01-29 11:03:02 +0100232_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 +0100233{
Michal Vasko3031aae2016-01-27 16:07:18 +0100234 if (!ca_file && !ca_dir) {
roman40672412023-05-04 11:10:22 +0200235 ERRARG(NULL, "ca_file and ca_dir");
Michal Vasko3031aae2016-01-27 16:07:18 +0100236 return -1;
237 }
238
Michal Vasko3031aae2016-01-27 16:07:18 +0100239 free(opts->ca_file);
240 free(opts->ca_dir);
241
242 if (ca_file) {
243 opts->ca_file = strdup(ca_file);
244 if (!opts->ca_file) {
245 ERRMEM;
246 return -1;
247 }
248 } else {
249 opts->ca_file = NULL;
250 }
251
252 if (ca_dir) {
253 opts->ca_dir = strdup(ca_dir);
254 if (!opts->ca_dir) {
255 ERRMEM;
256 return -1;
257 }
258 } else {
259 opts->ca_dir = NULL;
260 }
261
262 opts->tls_ctx_change = 1;
263
264 return 0;
265}
266
267API int
Michal Vaskoe22c6732016-01-29 11:03:02 +0100268nc_client_tls_set_trusted_ca_paths(const char *ca_file, const char *ca_dir)
Michal Vasko3031aae2016-01-27 16:07:18 +0100269{
Michal Vaskoe22c6732016-01-29 11:03:02 +0100270 return _nc_client_tls_set_trusted_ca_paths(ca_file, ca_dir, &tls_opts);
Michal Vasko3031aae2016-01-27 16:07:18 +0100271}
272
273API int
Michal Vaskoe22c6732016-01-29 11:03:02 +0100274nc_client_tls_ch_set_trusted_ca_paths(const char *ca_file, const char *ca_dir)
Michal Vasko3031aae2016-01-27 16:07:18 +0100275{
Michal Vaskoe22c6732016-01-29 11:03:02 +0100276 return _nc_client_tls_set_trusted_ca_paths(ca_file, ca_dir, &tls_ch_opts);
277}
278
279static void
280_nc_client_tls_get_trusted_ca_paths(const char **ca_file, const char **ca_dir, struct nc_client_tls_opts *opts)
281{
282 if (!ca_file && !ca_dir) {
roman40672412023-05-04 11:10:22 +0200283 ERRARG(NULL, "ca_file and ca_dir");
Michal Vaskoe22c6732016-01-29 11:03:02 +0100284 return;
285 }
286
287 if (ca_file) {
288 *ca_file = opts->ca_file;
289 }
290 if (ca_dir) {
291 *ca_dir = opts->ca_dir;
292 }
293}
294
295API void
296nc_client_tls_get_trusted_ca_paths(const char **ca_file, const char **ca_dir)
297{
298 _nc_client_tls_get_trusted_ca_paths(ca_file, ca_dir, &tls_opts);
299}
300
301API void
302nc_client_tls_ch_get_trusted_ca_paths(const char **ca_file, const char **ca_dir)
303{
304 _nc_client_tls_get_trusted_ca_paths(ca_file, ca_dir, &tls_ch_opts);
Michal Vasko3031aae2016-01-27 16:07:18 +0100305}
306
307static int
Michal Vaskoe22c6732016-01-29 11:03:02 +0100308_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 +0100309{
Michal Vasko3031aae2016-01-27 16:07:18 +0100310 if (!crl_file && !crl_dir) {
roman40672412023-05-04 11:10:22 +0200311 ERRARG(NULL, "crl_file and crl_dir");
Michal Vasko3031aae2016-01-27 16:07:18 +0100312 return -1;
313 }
314
Michal Vasko3031aae2016-01-27 16:07:18 +0100315 free(opts->crl_file);
316 free(opts->crl_dir);
317
318 if (crl_file) {
319 opts->crl_file = strdup(crl_file);
320 if (!opts->crl_file) {
321 ERRMEM;
322 return -1;
323 }
324 } else {
325 opts->crl_file = NULL;
326 }
327
328 if (crl_dir) {
329 opts->crl_dir = strdup(crl_dir);
330 if (!opts->crl_dir) {
331 ERRMEM;
332 return -1;
333 }
334 } else {
335 opts->crl_dir = NULL;
336 }
337
338 opts->crl_store_change = 1;
339
340 return 0;
341}
342
343API int
Michal Vaskoe22c6732016-01-29 11:03:02 +0100344nc_client_tls_set_crl_paths(const char *crl_file, const char *crl_dir)
Michal Vasko3031aae2016-01-27 16:07:18 +0100345{
Michal Vaskoe22c6732016-01-29 11:03:02 +0100346 return _nc_client_tls_set_crl_paths(crl_file, crl_dir, &tls_opts);
Michal Vasko3031aae2016-01-27 16:07:18 +0100347}
348
349API int
Michal Vaskoe22c6732016-01-29 11:03:02 +0100350nc_client_tls_ch_set_crl_paths(const char *crl_file, const char *crl_dir)
Michal Vasko3031aae2016-01-27 16:07:18 +0100351{
Michal Vaskoe22c6732016-01-29 11:03:02 +0100352 return _nc_client_tls_set_crl_paths(crl_file, crl_dir, &tls_ch_opts);
353}
354
355static void
356_nc_client_tls_get_crl_paths(const char **crl_file, const char **crl_dir, struct nc_client_tls_opts *opts)
357{
358 if (!crl_file && !crl_dir) {
roman40672412023-05-04 11:10:22 +0200359 ERRARG(NULL, "crl_file and crl_dir");
Michal Vaskoe22c6732016-01-29 11:03:02 +0100360 return;
361 }
362
363 if (crl_file) {
364 *crl_file = opts->crl_file;
365 }
366 if (crl_dir) {
367 *crl_dir = opts->crl_dir;
368 }
369}
370
371API void
372nc_client_tls_get_crl_paths(const char **crl_file, const char **crl_dir)
373{
374 _nc_client_tls_get_crl_paths(crl_file, crl_dir, &tls_opts);
375}
376
377API void
378nc_client_tls_ch_get_crl_paths(const char **crl_file, const char **crl_dir)
379{
380 _nc_client_tls_get_crl_paths(crl_file, crl_dir, &tls_ch_opts);
Michal Vasko3031aae2016-01-27 16:07:18 +0100381}
382
383API int
384nc_client_tls_ch_add_bind_listen(const char *address, uint16_t port)
385{
Michal Vasko9d4cca52022-09-07 11:19:57 +0200386 return nc_client_ch_add_bind_listen(address, port, NULL, NC_TI_OPENSSL);
387}
388
389API int
390nc_client_tls_ch_add_bind_hostname_listen(const char *address, uint16_t port, const char *hostname)
391{
392 return nc_client_ch_add_bind_listen(address, port, hostname, NC_TI_OPENSSL);
Michal Vasko3031aae2016-01-27 16:07:18 +0100393}
394
395API int
396nc_client_tls_ch_del_bind(const char *address, uint16_t port)
397{
398 return nc_client_ch_del_bind(address, port, NC_TI_OPENSSL);
399}
400
Michal Vasko3031aae2016-01-27 16:07:18 +0100401static int
Michal Vasko55ce24a2022-09-07 09:24:43 +0200402nc_client_tls_update_opts(struct nc_client_tls_opts *opts, const char *peername)
Michal Vasko3031aae2016-01-27 16:07:18 +0100403{
Michal Vasko9af52322022-07-25 14:39:30 +0200404 int rc = 0;
Michal Vasko3031aae2016-01-27 16:07:18 +0100405 char *key;
406 X509_LOOKUP *lookup;
Michal Vasko9af52322022-07-25 14:39:30 +0200407 X509_VERIFY_PARAM *vpm = NULL;
Michal Vasko3031aae2016-01-27 16:07:18 +0100408
409 if (!opts->tls_ctx || opts->tls_ctx_change) {
Michal Vaskoe22c6732016-01-29 11:03:02 +0100410 SSL_CTX_free(opts->tls_ctx);
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100411 /* prepare global SSL context, highest available method is negotiated autmatically */
412 if (!(opts->tls_ctx = SSL_CTX_new(TLS_client_method())))
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100413 {
Michal Vasko05532772021-06-03 12:12:38 +0200414 ERR(NULL, "Unable to create OpenSSL context (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vasko9af52322022-07-25 14:39:30 +0200415 rc = -1;
416 goto cleanup;
Michal Vasko3031aae2016-01-27 16:07:18 +0100417 }
418 SSL_CTX_set_verify(opts->tls_ctx, SSL_VERIFY_PEER, tlsauth_verify_callback);
419
420 /* get peer certificate */
421 if (SSL_CTX_use_certificate_file(opts->tls_ctx, opts->cert_path, SSL_FILETYPE_PEM) != 1) {
Michal Vasko05532772021-06-03 12:12:38 +0200422 ERR(NULL, "Loading the client certificate from \'%s\' failed (%s).", opts->cert_path,
423 ERR_reason_error_string(ERR_get_error()));
Michal Vasko9af52322022-07-25 14:39:30 +0200424 rc = -1;
425 goto cleanup;
Michal Vasko3031aae2016-01-27 16:07:18 +0100426 }
427
428 /* if the file with private key not specified, expect that the private key is stored with the certificate */
429 if (!opts->key_path) {
430 key = opts->cert_path;
431 } else {
432 key = opts->key_path;
433 }
434 if (SSL_CTX_use_PrivateKey_file(opts->tls_ctx, key, SSL_FILETYPE_PEM) != 1) {
Michal Vasko9af52322022-07-25 14:39:30 +0200435 ERR(NULL, "Loading the client private key from \'%s\' failed (%s).", key,
Michal Vasko05532772021-06-03 12:12:38 +0200436 ERR_reason_error_string(ERR_get_error()));
Michal Vasko9af52322022-07-25 14:39:30 +0200437 rc = -1;
438 goto cleanup;
Michal Vasko3031aae2016-01-27 16:07:18 +0100439 }
440
441 if (!SSL_CTX_load_verify_locations(opts->tls_ctx, opts->ca_file, opts->ca_dir)) {
Michal Vasko05532772021-06-03 12:12:38 +0200442 ERR(NULL, "Failed to load the locations of trusted CA certificates (%s).",
443 ERR_reason_error_string(ERR_get_error()));
Michal Vasko9af52322022-07-25 14:39:30 +0200444 rc = -1;
445 goto cleanup;
Michal Vasko3031aae2016-01-27 16:07:18 +0100446 }
Michal Vasko9af52322022-07-25 14:39:30 +0200447
Michal Vasko55ce24a2022-09-07 09:24:43 +0200448 if (peername) {
449 /* server identity (hostname) verification */
450 vpm = X509_VERIFY_PARAM_new();
451 if (!X509_VERIFY_PARAM_set1_host(vpm, peername, 0)) {
452 ERR(NULL, "Failed to set expected server hostname (%s).", ERR_reason_error_string(ERR_get_error()));
453 rc = -1;
454 goto cleanup;
455 }
456 if (!SSL_CTX_set1_param(opts->tls_ctx, vpm)) {
457 ERR(NULL, "Failed to set verify params (%s).", ERR_reason_error_string(ERR_get_error()));
458 rc = -1;
459 goto cleanup;
460 }
Michal Vasko9af52322022-07-25 14:39:30 +0200461 }
Michal Vasko3031aae2016-01-27 16:07:18 +0100462 }
463
464 if (opts->crl_store_change || (!opts->crl_store && (opts->crl_file || opts->crl_dir))) {
465 /* set the revocation store with the correct paths for the callback */
466 X509_STORE_free(opts->crl_store);
467
468 opts->crl_store = X509_STORE_new();
469 if (!opts->crl_store) {
Michal Vasko05532772021-06-03 12:12:38 +0200470 ERR(NULL, "Unable to create a certificate store (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vasko9af52322022-07-25 14:39:30 +0200471 rc = -1;
472 goto cleanup;
Michal Vasko3031aae2016-01-27 16:07:18 +0100473 }
Michal Vasko18aeb5d2017-02-17 09:23:56 +0100474
Michal Vasko3031aae2016-01-27 16:07:18 +0100475 if (opts->crl_file) {
476 if (!(lookup = X509_STORE_add_lookup(opts->crl_store, X509_LOOKUP_file()))) {
Michal Vasko05532772021-06-03 12:12:38 +0200477 ERR(NULL, "Failed to add lookup method to CRL checking.");
Michal Vasko9af52322022-07-25 14:39:30 +0200478 rc = -1;
479 goto cleanup;
Michal Vasko3031aae2016-01-27 16:07:18 +0100480 }
481 if (X509_LOOKUP_add_dir(lookup, opts->crl_file, X509_FILETYPE_PEM) != 1) {
Michal Vasko05532772021-06-03 12:12:38 +0200482 ERR(NULL, "Failed to add the revocation lookup file \"%s\".", opts->crl_file);
Michal Vasko9af52322022-07-25 14:39:30 +0200483 rc = -1;
484 goto cleanup;
Michal Vasko3031aae2016-01-27 16:07:18 +0100485 }
486 }
487
488 if (opts->crl_dir) {
489 if (!(lookup = X509_STORE_add_lookup(opts->crl_store, X509_LOOKUP_hash_dir()))) {
Michal Vasko05532772021-06-03 12:12:38 +0200490 ERR(NULL, "Failed to add lookup method to CRL checking.");
Michal Vasko9af52322022-07-25 14:39:30 +0200491 rc = -1;
492 goto cleanup;
Michal Vasko3031aae2016-01-27 16:07:18 +0100493 }
494 if (X509_LOOKUP_add_dir(lookup, opts->crl_dir, X509_FILETYPE_PEM) != 1) {
Michal Vasko05532772021-06-03 12:12:38 +0200495 ERR(NULL, "Failed to add the revocation lookup directory \"%s\".", opts->crl_dir);
Michal Vasko9af52322022-07-25 14:39:30 +0200496 rc = -1;
497 goto cleanup;
Michal Vasko3031aae2016-01-27 16:07:18 +0100498 }
499 }
500 }
501
Michal Vasko9af52322022-07-25 14:39:30 +0200502cleanup:
503 X509_VERIFY_PARAM_free(vpm);
504 return rc;
505}
506
507static int
Michal Vaskoa82745b2022-09-07 11:48:12 +0200508nc_client_tls_connect_check(int connect_ret, SSL *tls, const char *peername)
Michal Vasko9af52322022-07-25 14:39:30 +0200509{
510 int verify;
511
512 /* check certificate verification result */
513 verify = SSL_get_verify_result(tls);
514 switch (verify) {
515 case X509_V_OK:
516 if (connect_ret == 1) {
Michal Vasko7ffcd142022-09-07 09:24:22 +0200517 VRB(NULL, "Server certificate verified (domain \"%s\").", peername);
Michal Vasko9af52322022-07-25 14:39:30 +0200518 }
519 break;
520 default:
521 ERR(NULL, "Server certificate error (%s).", X509_verify_cert_error_string(verify));
522 }
523
524 /* check TLS connection result */
525 if (connect_ret != 1) {
526 switch (SSL_get_error(tls, connect_ret)) {
527 case SSL_ERROR_SYSCALL:
Michal Vasko7ffcd142022-09-07 09:24:22 +0200528 ERR(NULL, "SSL connect to \"%s\" failed (%s).", peername, errno ? strerror(errno) : "unexpected EOF");
Michal Vasko9af52322022-07-25 14:39:30 +0200529 break;
530 case SSL_ERROR_SSL:
Michal Vasko7ffcd142022-09-07 09:24:22 +0200531 ERR(NULL, "SSL connect to \"%s\" failed (%s).", peername, ERR_reason_error_string(ERR_get_error()));
Michal Vasko9af52322022-07-25 14:39:30 +0200532 break;
533 default:
Michal Vasko7ffcd142022-09-07 09:24:22 +0200534 ERR(NULL, "SSL connect to \"%s\" failed.", peername);
Michal Vasko9af52322022-07-25 14:39:30 +0200535 break;
536 }
537 }
538
539 return connect_ret;
Radek Krejci9f03b482015-10-22 16:02:10 +0200540}
541
542API struct nc_session *
Michal Vasko9bee18d2015-12-08 14:41:42 +0100543nc_connect_tls(const char *host, unsigned short port, struct ly_ctx *ctx)
Radek Krejci9f03b482015-10-22 16:02:10 +0200544{
Radek Krejci9f03b482015-10-22 16:02:10 +0200545 struct nc_session *session = NULL;
Michal Vasko9af52322022-07-25 14:39:30 +0200546 int sock, ret;
roman6ece9c52022-06-22 09:29:17 +0200547 struct timespec ts_timeout;
Michal Vasko66032bc2019-01-22 15:03:12 +0100548 char *ip_host = NULL;
Michal Vasko11d4cdb2015-10-29 11:42:52 +0100549
Michal Vasko3031aae2016-01-27 16:07:18 +0100550 if (!tls_opts.cert_path || (!tls_opts.ca_file && !tls_opts.ca_dir)) {
Michal Vasko45e53ae2016-04-07 11:46:03 +0200551 ERRINIT;
Michal Vasko11d4cdb2015-10-29 11:42:52 +0100552 return NULL;
553 }
Radek Krejci9f03b482015-10-22 16:02:10 +0200554
555 /* process parameters */
roman3f9b65c2023-06-05 14:26:58 +0200556 if (!host || (host[0] == '\0')) {
Radek Krejci9f03b482015-10-22 16:02:10 +0200557 host = "localhost";
558 }
559
560 if (!port) {
561 port = NC_PORT_TLS;
562 }
563
Michal Vasko3031aae2016-01-27 16:07:18 +0100564 /* create/update TLS structures */
Michal Vasko9af52322022-07-25 14:39:30 +0200565 if (nc_client_tls_update_opts(&tls_opts, host)) {
Michal Vasko3031aae2016-01-27 16:07:18 +0100566 return NULL;
567 }
568
Radek Krejci9f03b482015-10-22 16:02:10 +0200569 /* prepare session structure */
Michal Vasko131120a2018-05-29 15:44:02 +0200570 session = nc_new_session(NC_CLIENT, 0);
Radek Krejci9f03b482015-10-22 16:02:10 +0200571 if (!session) {
Michal Vasko9e2d3a32015-11-10 13:09:18 +0100572 ERRMEM;
Radek Krejci9f03b482015-10-22 16:02:10 +0200573 return NULL;
574 }
Michal Vasko9e2d3a32015-11-10 13:09:18 +0100575 session->status = NC_STATUS_STARTING;
Michal Vasko11d4cdb2015-10-29 11:42:52 +0100576
577 /* fill the session */
Michal Vasko9e2d3a32015-11-10 13:09:18 +0100578 session->ti_type = NC_TI_OPENSSL;
Michal Vasko11d4cdb2015-10-29 11:42:52 +0100579 if (!(session->ti.tls = SSL_new(tls_opts.tls_ctx))) {
Michal Vasko05532772021-06-03 12:12:38 +0200580 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 +0100581 goto fail;
582 }
Radek Krejci9f03b482015-10-22 16:02:10 +0200583
Michal Vasko11d4cdb2015-10-29 11:42:52 +0100584 /* create and assign socket */
Michal Vaskoe49a15f2019-05-27 14:18:36 +0200585 sock = nc_sock_connect(host, port, -1, &client_opts.ka, NULL, &ip_host);
Michal Vasko11d4cdb2015-10-29 11:42:52 +0100586 if (sock == -1) {
Michal Vasko05532772021-06-03 12:12:38 +0200587 ERR(NULL, "Unable to connect to %s:%u (%s).", host, port, strerror(errno));
Michal Vasko11d4cdb2015-10-29 11:42:52 +0100588 goto fail;
589 }
590 SSL_set_fd(session->ti.tls, sock);
591
592 /* set the SSL_MODE_AUTO_RETRY flag to allow OpenSSL perform re-handshake automatically */
593 SSL_set_mode(session->ti.tls, SSL_MODE_AUTO_RETRY);
594
595 /* connect and perform the handshake */
Michal Vaskod8a74192023-02-06 15:51:50 +0100596 nc_timeouttime_get(&ts_timeout, NC_TRANSPORT_TIMEOUT);
Michal Vasko3031aae2016-01-27 16:07:18 +0100597 tlsauth_ch = 0;
Michal Vaskod51a2642021-09-03 13:03:24 +0200598 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 +0100599 usleep(NC_TIMEOUT_STEP);
Michal Vaskod8a74192023-02-06 15:51:50 +0100600 if (nc_timeouttime_cur_diff(&ts_timeout) < 1) {
Michal Vasko7ffcd142022-09-07 09:24:22 +0200601 ERR(NULL, "SSL connect timeout.");
Michal Vasko0190bc32016-03-02 15:47:49 +0100602 goto fail;
603 }
604 }
Michal Vasko3cd55a72022-09-06 15:52:39 +0200605
606 /* check for errors */
Michal Vaskoa82745b2022-09-07 11:48:12 +0200607 if (nc_client_tls_connect_check(ret, session->ti.tls, host) != 1) {
Michal Vasko11d4cdb2015-10-29 11:42:52 +0100608 goto fail;
609 }
610
Michal Vasko78939072022-12-12 07:43:18 +0100611 if (nc_client_session_new_ctx(session, ctx) != EXIT_SUCCESS) {
Radek Krejcifd5b6682017-06-13 15:52:53 +0200612 goto fail;
Michal Vasko9e2d3a32015-11-10 13:09:18 +0100613 }
Radek Krejcifd5b6682017-06-13 15:52:53 +0200614 ctx = session->ctx;
Michal Vasko9e2d3a32015-11-10 13:09:18 +0100615
Radek Krejci9f03b482015-10-22 16:02:10 +0200616 /* NETCONF handshake */
Michal Vasko131120a2018-05-29 15:44:02 +0200617 if (nc_handshake_io(session) != NC_MSG_HELLO) {
Michal Vasko11d4cdb2015-10-29 11:42:52 +0100618 goto fail;
Radek Krejci9f03b482015-10-22 16:02:10 +0200619 }
Michal Vaskoad611702015-12-03 13:41:51 +0100620 session->status = NC_STATUS_RUNNING;
Radek Krejci9f03b482015-10-22 16:02:10 +0200621
Michal Vaskoef578332016-01-25 13:20:09 +0100622 if (nc_ctx_check_and_fill(session) == -1) {
Michal Vasko57eb9402015-12-08 14:38:12 +0100623 goto fail;
Michal Vasko9e2d3a32015-11-10 13:09:18 +0100624 }
625
626 /* store information into session and the dictionary */
Michal Vasko93224072021-11-09 12:14:28 +0100627 session->host = ip_host;
Michal Vasko9e2d3a32015-11-10 13:09:18 +0100628 session->port = port;
Michal Vasko93224072021-11-09 12:14:28 +0100629 session->username = strdup("certificate-based");
Michal Vasko9e2d3a32015-11-10 13:09:18 +0100630
Radek Krejci9f03b482015-10-22 16:02:10 +0200631 return session;
632
Michal Vasko11d4cdb2015-10-29 11:42:52 +0100633fail:
Michal Vasko66032bc2019-01-22 15:03:12 +0100634 free(ip_host);
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100635 nc_session_free(session, NULL);
Radek Krejci9f03b482015-10-22 16:02:10 +0200636 return NULL;
637}
638
639API struct nc_session *
640nc_connect_libssl(SSL *tls, struct ly_ctx *ctx)
641{
Michal Vasko11d4cdb2015-10-29 11:42:52 +0100642 struct nc_session *session;
Radek Krejci9f03b482015-10-22 16:02:10 +0200643
roman40672412023-05-04 11:10:22 +0200644 NC_CHECK_ARG_RET(NULL, tls, NULL);
645
646 if (!SSL_is_init_finished(tls)) {
Michal Vasko05532772021-06-03 12:12:38 +0200647 ERR(NULL, "Supplied TLS session is not fully connected!");
Michal Vasko11d4cdb2015-10-29 11:42:52 +0100648 return NULL;
649 }
650
651 /* prepare session structure */
Michal Vasko131120a2018-05-29 15:44:02 +0200652 session = nc_new_session(NC_CLIENT, 0);
Michal Vasko11d4cdb2015-10-29 11:42:52 +0100653 if (!session) {
Michal Vasko9e2d3a32015-11-10 13:09:18 +0100654 ERRMEM;
Michal Vasko11d4cdb2015-10-29 11:42:52 +0100655 return NULL;
656 }
Michal Vasko9e2d3a32015-11-10 13:09:18 +0100657 session->status = NC_STATUS_STARTING;
Michal Vasko9e2d3a32015-11-10 13:09:18 +0100658 session->ti_type = NC_TI_OPENSSL;
Michal Vasko11d4cdb2015-10-29 11:42:52 +0100659 session->ti.tls = tls;
660
Michal Vasko78939072022-12-12 07:43:18 +0100661 if (nc_client_session_new_ctx(session, ctx) != EXIT_SUCCESS) {
Radek Krejcifd5b6682017-06-13 15:52:53 +0200662 goto fail;
Michal Vasko11d4cdb2015-10-29 11:42:52 +0100663 }
Radek Krejcifd5b6682017-06-13 15:52:53 +0200664 ctx = session->ctx;
Michal Vasko11d4cdb2015-10-29 11:42:52 +0100665
666 /* NETCONF handshake */
Michal Vasko131120a2018-05-29 15:44:02 +0200667 if (nc_handshake_io(session) != NC_MSG_HELLO) {
Michal Vasko11d4cdb2015-10-29 11:42:52 +0100668 goto fail;
669 }
Michal Vaskoad611702015-12-03 13:41:51 +0100670 session->status = NC_STATUS_RUNNING;
Michal Vasko11d4cdb2015-10-29 11:42:52 +0100671
Michal Vaskoef578332016-01-25 13:20:09 +0100672 if (nc_ctx_check_and_fill(session) == -1) {
Michal Vasko57eb9402015-12-08 14:38:12 +0100673 goto fail;
Michal Vasko9e2d3a32015-11-10 13:09:18 +0100674 }
675
Michal Vasko11d4cdb2015-10-29 11:42:52 +0100676 return session;
677
678fail:
Michal Vaskob604ed52022-01-24 09:56:14 +0100679 session->ti_type = NC_TI_NONE;
Michal Vaskoa42a7f02021-07-02 08:43:12 +0200680 session->ti.tls = NULL;
Michal Vaskoe1a64ec2016-03-01 12:21:58 +0100681 nc_session_free(session, NULL);
Radek Krejci9f03b482015-10-22 16:02:10 +0200682 return NULL;
683}
684
Michal Vasko3031aae2016-01-27 16:07:18 +0100685struct nc_session *
Michal Vasko9d4cca52022-09-07 11:19:57 +0200686nc_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 +0100687{
Michal Vasko3cd55a72022-09-06 15:52:39 +0200688 int ret;
Michal Vaskoc64c38c2021-07-02 08:43:53 +0200689 SSL *tls = NULL;
690 struct nc_session *session = NULL;
roman6ece9c52022-06-22 09:29:17 +0200691 struct timespec ts_timeout;
Michal Vasko80cad7f2015-12-08 14:42:27 +0100692
Michal Vasko9d4cca52022-09-07 11:19:57 +0200693 /* create/update TLS structures with explicit expected peername, if any set, the host is just the IP */
694 if (nc_client_tls_update_opts(&tls_ch_opts, peername)) {
Michal Vaskoc64c38c2021-07-02 08:43:53 +0200695 goto cleanup;
Michal Vaskoc61c4492016-01-25 11:13:34 +0100696 }
697
Michal Vasko3031aae2016-01-27 16:07:18 +0100698 if (!(tls = SSL_new(tls_ch_opts.tls_ctx))) {
Michal Vasko05532772021-06-03 12:12:38 +0200699 ERR(NULL, "Failed to create new TLS session structure (%s).", ERR_reason_error_string(ERR_get_error()));
Michal Vaskoc64c38c2021-07-02 08:43:53 +0200700 goto cleanup;
Michal Vasko80cad7f2015-12-08 14:42:27 +0100701 }
702
703 SSL_set_fd(tls, sock);
704
705 /* set the SSL_MODE_AUTO_RETRY flag to allow OpenSSL perform re-handshake automatically */
706 SSL_set_mode(tls, SSL_MODE_AUTO_RETRY);
707
708 /* connect and perform the handshake */
Michal Vasko36c7be82017-02-22 13:37:59 +0100709 if (timeout > -1) {
Michal Vaskod8a74192023-02-06 15:51:50 +0100710 nc_timeouttime_get(&ts_timeout, timeout);
Michal Vasko36c7be82017-02-22 13:37:59 +0100711 }
Michal Vasko3031aae2016-01-27 16:07:18 +0100712 tlsauth_ch = 1;
Michal Vasko0190bc32016-03-02 15:47:49 +0100713 while (((ret = SSL_connect(tls)) == -1) && (SSL_get_error(tls, ret) == SSL_ERROR_WANT_READ)) {
714 usleep(NC_TIMEOUT_STEP);
Michal Vaskod8a74192023-02-06 15:51:50 +0100715 if ((timeout > -1) && (nc_timeouttime_cur_diff(&ts_timeout) < 1)) {
Michal Vasko7ffcd142022-09-07 09:24:22 +0200716 ERR(NULL, "SSL connect timeout.");
roman6ece9c52022-06-22 09:29:17 +0200717 goto cleanup;
Michal Vasko0190bc32016-03-02 15:47:49 +0100718 }
719 }
Michal Vasko80cad7f2015-12-08 14:42:27 +0100720
Michal Vasko3cd55a72022-09-06 15:52:39 +0200721 /* check for errors */
Michal Vaskoa82745b2022-09-07 11:48:12 +0200722 if (nc_client_tls_connect_check(ret, tls, peername ? peername : host) != 1) {
Michal Vasko3cd55a72022-09-06 15:52:39 +0200723 goto cleanup;
Michal Vasko80cad7f2015-12-08 14:42:27 +0100724 }
725
Michal Vaskoc64c38c2021-07-02 08:43:53 +0200726 /* connect */
Michal Vasko80cad7f2015-12-08 14:42:27 +0100727 session = nc_connect_libssl(tls, ctx);
Michal Vaskoc64c38c2021-07-02 08:43:53 +0200728 if (!session) {
729 goto cleanup;
Michal Vasko80cad7f2015-12-08 14:42:27 +0100730 }
731
Michal Vaskoc64c38c2021-07-02 08:43:53 +0200732 session->flags |= NC_SESSION_CALLHOME;
733
734 /* store information into session and the dictionary */
Michal Vasko93224072021-11-09 12:14:28 +0100735 session->host = strdup(host);
Michal Vaskoc64c38c2021-07-02 08:43:53 +0200736 session->port = port;
Michal Vasko93224072021-11-09 12:14:28 +0100737 session->username = strdup("certificate-based");
Michal Vaskoc64c38c2021-07-02 08:43:53 +0200738
739cleanup:
740 if (!session) {
741 SSL_free(tls);
742 close(sock);
743 }
Michal Vasko80cad7f2015-12-08 14:42:27 +0100744 return session;
745}