Radek Krejci | 9f03b48 | 2015-10-22 16:02:10 +0200 | [diff] [blame] | 1 | /** |
Michal Vasko | 95ea9ff | 2021-11-09 12:29:14 +0100 | [diff] [blame] | 2 | * @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 Krejci | 9f03b48 | 2015-10-22 16:02:10 +0200 | [diff] [blame] | 6 | * |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 7 | * This source is compiled only with libssl. |
Radek Krejci | 9f03b48 | 2015-10-22 16:02:10 +0200 | [diff] [blame] | 8 | * |
Michal Vasko | 95ea9ff | 2021-11-09 12:29:14 +0100 | [diff] [blame] | 9 | * @copyright |
Radek Krejci | 9f03b48 | 2015-10-22 16:02:10 +0200 | [diff] [blame] | 10 | * Copyright (c) 2015 CESNET, z.s.p.o. |
| 11 | * |
Radek Krejci | 9b81f5b | 2016-02-24 13:14:49 +0100 | [diff] [blame] | 12 | * 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 Vasko | afd416b | 2016-02-25 14:51:46 +0100 | [diff] [blame] | 15 | * |
Radek Krejci | 9b81f5b | 2016-02-24 13:14:49 +0100 | [diff] [blame] | 16 | * https://opensource.org/licenses/BSD-3-Clause |
Radek Krejci | 9f03b48 | 2015-10-22 16:02:10 +0200 | [diff] [blame] | 17 | */ |
| 18 | |
Michal Vasko | ba9f358 | 2023-02-22 10:26:32 +0100 | [diff] [blame] | 19 | #define _GNU_SOURCE /* pthread_rwlock_t, strdup */ |
| 20 | |
Radek Krejci | 9f03b48 | 2015-10-22 16:02:10 +0200 | [diff] [blame] | 21 | #include <assert.h> |
| 22 | #include <errno.h> |
Radek Krejci | 9f03b48 | 2015-10-22 16:02:10 +0200 | [diff] [blame] | 23 | #include <string.h> |
| 24 | #include <unistd.h> |
| 25 | |
| 26 | #include <libyang/libyang.h> |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 27 | #include <openssl/err.h> |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 28 | #include <openssl/ossl_typ.h> |
Michal Vasko | dae4832 | 2016-02-25 14:49:48 +0100 | [diff] [blame] | 29 | #include <openssl/x509.h> |
Radek Krejci | 9f03b48 | 2015-10-22 16:02:10 +0200 | [diff] [blame] | 30 | |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 31 | #include "config.h" |
| 32 | #include "log_p.h" |
Michal Vasko | e22c673 | 2016-01-29 11:03:02 +0100 | [diff] [blame] | 33 | #include "session_client.h" |
| 34 | #include "session_client_ch.h" |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 35 | #include "session_p.h" |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 36 | |
Radek Krejci | 62aa064 | 2017-05-25 16:33:49 +0200 | [diff] [blame] | 37 | struct 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 Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 42 | |
| 43 | static int tlsauth_ch; |
Radek Krejci | 9f03b48 | 2015-10-22 16:02:10 +0200 | [diff] [blame] | 44 | |
Michal Vasko | 18aeb5d | 2017-02-17 09:23:56 +0100 | [diff] [blame] | 45 | static int |
| 46 | tlsauth_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 Penev | 4f552d6 | 2019-06-26 16:10:43 -0700 | [diff] [blame] | 79 | rc = X509_STORE_CTX_get_by_subject(store_ctx, X509_LU_CRL, subject, obj); |
Michal Vasko | 18aeb5d | 2017-02-17 09:23:56 +0100 | [diff] [blame] | 80 | X509_STORE_CTX_free(store_ctx); |
| 81 | crl = X509_OBJECT_get0_X509_CRL(obj); |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 82 | if ((rc > 0) && crl) { |
Michal Vasko | 18aeb5d | 2017-02-17 09:23:56 +0100 | [diff] [blame] | 83 | 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 Penev | 4f552d6 | 2019-06-26 16:10:43 -0700 | [diff] [blame] | 118 | rc = X509_STORE_CTX_get_by_subject(store_ctx, X509_LU_CRL, issuer, obj); |
Michal Vasko | 18aeb5d | 2017-02-17 09:23:56 +0100 | [diff] [blame] | 119 | X509_STORE_CTX_free(store_ctx); |
| 120 | crl = X509_OBJECT_get0_X509_CRL(obj); |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 121 | if ((rc > 0) && crl) { |
Michal Vasko | 18aeb5d | 2017-02-17 09:23:56 +0100 | [diff] [blame] | 122 | /* 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 Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 127 | ERR(NULL, "Certificate revoked!"); |
Michal Vasko | 18aeb5d | 2017-02-17 09:23:56 +0100 | [diff] [blame] | 128 | 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 Barrett | d37d2fb | 2019-08-28 14:25:34 -0400 | [diff] [blame] | 139 | void |
Michal Vasko | e22c673 | 2016-01-29 11:03:02 +0100 | [diff] [blame] | 140 | _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 Krejci | 5cebc6b | 2017-05-26 13:24:38 +0200 | [diff] [blame] | 151 | |
| 152 | memset(opts, 0, sizeof *opts); |
Michal Vasko | e22c673 | 2016-01-29 11:03:02 +0100 | [diff] [blame] | 153 | } |
| 154 | |
Michal Vasko | b7558c5 | 2016-02-26 15:04:19 +0100 | [diff] [blame] | 155 | void |
Michal Vasko | e22c673 | 2016-01-29 11:03:02 +0100 | [diff] [blame] | 156 | nc_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 Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 162 | static int |
Michal Vasko | e22c673 | 2016-01-29 11:03:02 +0100 | [diff] [blame] | 163 | _nc_client_tls_set_cert_key_paths(const char *client_cert, const char *client_key, struct nc_client_tls_opts *opts) |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 164 | { |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 165 | NC_CHECK_ARG_RET(NULL, client_cert, -1); |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 166 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 167 | free(opts->cert_path); |
| 168 | free(opts->key_path); |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 169 | |
Michal Vasko | 9db2a6f | 2016-02-01 13:26:03 +0100 | [diff] [blame] | 170 | opts->cert_path = strdup(client_cert); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame^] | 171 | NC_CHECK_ERRMEM_RET(!opts->cert_path, -1); |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 172 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 173 | if (client_key) { |
| 174 | opts->key_path = strdup(client_key); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame^] | 175 | NC_CHECK_ERRMEM_RET(!opts->key_path, -1); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 176 | } else { |
| 177 | opts->key_path = NULL; |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 178 | } |
| 179 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 180 | opts->tls_ctx_change = 1; |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 181 | |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 182 | return 0; |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 183 | } |
| 184 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 185 | API int |
Michal Vasko | e22c673 | 2016-01-29 11:03:02 +0100 | [diff] [blame] | 186 | nc_client_tls_set_cert_key_paths(const char *client_cert, const char *client_key) |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 187 | { |
Michal Vasko | e22c673 | 2016-01-29 11:03:02 +0100 | [diff] [blame] | 188 | return _nc_client_tls_set_cert_key_paths(client_cert, client_key, &tls_opts); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | API int |
Michal Vasko | e22c673 | 2016-01-29 11:03:02 +0100 | [diff] [blame] | 192 | nc_client_tls_ch_set_cert_key_paths(const char *client_cert, const char *client_key) |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 193 | { |
Michal Vasko | e22c673 | 2016-01-29 11:03:02 +0100 | [diff] [blame] | 194 | return _nc_client_tls_set_cert_key_paths(client_cert, client_key, &tls_ch_opts); |
| 195 | } |
| 196 | |
| 197 | static void |
| 198 | _nc_client_tls_get_cert_key_paths(const char **client_cert, const char **client_key, struct nc_client_tls_opts *opts) |
| 199 | { |
| 200 | if (!client_cert && !client_key) { |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 201 | ERRARG(NULL, "client_cert and client_key"); |
Michal Vasko | e22c673 | 2016-01-29 11:03:02 +0100 | [diff] [blame] | 202 | return; |
| 203 | } |
| 204 | |
| 205 | if (client_cert) { |
| 206 | *client_cert = opts->cert_path; |
| 207 | } |
| 208 | if (client_key) { |
| 209 | *client_key = opts->key_path; |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | API void |
| 214 | nc_client_tls_get_cert_key_paths(const char **client_cert, const char **client_key) |
| 215 | { |
| 216 | _nc_client_tls_get_cert_key_paths(client_cert, client_key, &tls_opts); |
| 217 | } |
| 218 | |
| 219 | API void |
| 220 | nc_client_tls_ch_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_ch_opts); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | static int |
Michal Vasko | e22c673 | 2016-01-29 11:03:02 +0100 | [diff] [blame] | 226 | _nc_client_tls_set_trusted_ca_paths(const char *ca_file, const char *ca_dir, struct nc_client_tls_opts *opts) |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 227 | { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 228 | if (!ca_file && !ca_dir) { |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 229 | ERRARG(NULL, "ca_file and ca_dir"); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 230 | return -1; |
| 231 | } |
| 232 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 233 | free(opts->ca_file); |
| 234 | free(opts->ca_dir); |
| 235 | |
| 236 | if (ca_file) { |
| 237 | opts->ca_file = strdup(ca_file); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame^] | 238 | NC_CHECK_ERRMEM_RET(!opts->ca_file, -1); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 239 | } else { |
| 240 | opts->ca_file = NULL; |
| 241 | } |
| 242 | |
| 243 | if (ca_dir) { |
| 244 | opts->ca_dir = strdup(ca_dir); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame^] | 245 | NC_CHECK_ERRMEM_RET(!opts->ca_dir, -1); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 246 | } else { |
| 247 | opts->ca_dir = NULL; |
| 248 | } |
| 249 | |
| 250 | opts->tls_ctx_change = 1; |
| 251 | |
| 252 | return 0; |
| 253 | } |
| 254 | |
| 255 | API int |
Michal Vasko | e22c673 | 2016-01-29 11:03:02 +0100 | [diff] [blame] | 256 | nc_client_tls_set_trusted_ca_paths(const char *ca_file, const char *ca_dir) |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 257 | { |
Michal Vasko | e22c673 | 2016-01-29 11:03:02 +0100 | [diff] [blame] | 258 | return _nc_client_tls_set_trusted_ca_paths(ca_file, ca_dir, &tls_opts); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | API int |
Michal Vasko | e22c673 | 2016-01-29 11:03:02 +0100 | [diff] [blame] | 262 | nc_client_tls_ch_set_trusted_ca_paths(const char *ca_file, const char *ca_dir) |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 263 | { |
Michal Vasko | e22c673 | 2016-01-29 11:03:02 +0100 | [diff] [blame] | 264 | return _nc_client_tls_set_trusted_ca_paths(ca_file, ca_dir, &tls_ch_opts); |
| 265 | } |
| 266 | |
| 267 | static void |
| 268 | _nc_client_tls_get_trusted_ca_paths(const char **ca_file, const char **ca_dir, struct nc_client_tls_opts *opts) |
| 269 | { |
| 270 | if (!ca_file && !ca_dir) { |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 271 | ERRARG(NULL, "ca_file and ca_dir"); |
Michal Vasko | e22c673 | 2016-01-29 11:03:02 +0100 | [diff] [blame] | 272 | return; |
| 273 | } |
| 274 | |
| 275 | if (ca_file) { |
| 276 | *ca_file = opts->ca_file; |
| 277 | } |
| 278 | if (ca_dir) { |
| 279 | *ca_dir = opts->ca_dir; |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | API void |
| 284 | nc_client_tls_get_trusted_ca_paths(const char **ca_file, const char **ca_dir) |
| 285 | { |
| 286 | _nc_client_tls_get_trusted_ca_paths(ca_file, ca_dir, &tls_opts); |
| 287 | } |
| 288 | |
| 289 | API void |
| 290 | nc_client_tls_ch_get_trusted_ca_paths(const char **ca_file, const char **ca_dir) |
| 291 | { |
| 292 | _nc_client_tls_get_trusted_ca_paths(ca_file, ca_dir, &tls_ch_opts); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | static int |
Michal Vasko | e22c673 | 2016-01-29 11:03:02 +0100 | [diff] [blame] | 296 | _nc_client_tls_set_crl_paths(const char *crl_file, const char *crl_dir, struct nc_client_tls_opts *opts) |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 297 | { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 298 | if (!crl_file && !crl_dir) { |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 299 | ERRARG(NULL, "crl_file and crl_dir"); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 300 | return -1; |
| 301 | } |
| 302 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 303 | free(opts->crl_file); |
| 304 | free(opts->crl_dir); |
| 305 | |
| 306 | if (crl_file) { |
| 307 | opts->crl_file = strdup(crl_file); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame^] | 308 | NC_CHECK_ERRMEM_RET(!opts->crl_file, -1); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 309 | } else { |
| 310 | opts->crl_file = NULL; |
| 311 | } |
| 312 | |
| 313 | if (crl_dir) { |
| 314 | opts->crl_dir = strdup(crl_dir); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame^] | 315 | NC_CHECK_ERRMEM_RET(!opts->crl_dir, -1); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 316 | } else { |
| 317 | opts->crl_dir = NULL; |
| 318 | } |
| 319 | |
| 320 | opts->crl_store_change = 1; |
| 321 | |
| 322 | return 0; |
| 323 | } |
| 324 | |
| 325 | API int |
Michal Vasko | e22c673 | 2016-01-29 11:03:02 +0100 | [diff] [blame] | 326 | nc_client_tls_set_crl_paths(const char *crl_file, const char *crl_dir) |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 327 | { |
Michal Vasko | e22c673 | 2016-01-29 11:03:02 +0100 | [diff] [blame] | 328 | return _nc_client_tls_set_crl_paths(crl_file, crl_dir, &tls_opts); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | API int |
Michal Vasko | e22c673 | 2016-01-29 11:03:02 +0100 | [diff] [blame] | 332 | nc_client_tls_ch_set_crl_paths(const char *crl_file, const char *crl_dir) |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 333 | { |
Michal Vasko | e22c673 | 2016-01-29 11:03:02 +0100 | [diff] [blame] | 334 | return _nc_client_tls_set_crl_paths(crl_file, crl_dir, &tls_ch_opts); |
| 335 | } |
| 336 | |
| 337 | static void |
| 338 | _nc_client_tls_get_crl_paths(const char **crl_file, const char **crl_dir, struct nc_client_tls_opts *opts) |
| 339 | { |
| 340 | if (!crl_file && !crl_dir) { |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 341 | ERRARG(NULL, "crl_file and crl_dir"); |
Michal Vasko | e22c673 | 2016-01-29 11:03:02 +0100 | [diff] [blame] | 342 | return; |
| 343 | } |
| 344 | |
| 345 | if (crl_file) { |
| 346 | *crl_file = opts->crl_file; |
| 347 | } |
| 348 | if (crl_dir) { |
| 349 | *crl_dir = opts->crl_dir; |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | API void |
| 354 | nc_client_tls_get_crl_paths(const char **crl_file, const char **crl_dir) |
| 355 | { |
| 356 | _nc_client_tls_get_crl_paths(crl_file, crl_dir, &tls_opts); |
| 357 | } |
| 358 | |
| 359 | API void |
| 360 | nc_client_tls_ch_get_crl_paths(const char **crl_file, const char **crl_dir) |
| 361 | { |
| 362 | _nc_client_tls_get_crl_paths(crl_file, crl_dir, &tls_ch_opts); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | API int |
| 366 | nc_client_tls_ch_add_bind_listen(const char *address, uint16_t port) |
| 367 | { |
Michal Vasko | 9d4cca5 | 2022-09-07 11:19:57 +0200 | [diff] [blame] | 368 | return nc_client_ch_add_bind_listen(address, port, NULL, NC_TI_OPENSSL); |
| 369 | } |
| 370 | |
| 371 | API int |
| 372 | nc_client_tls_ch_add_bind_hostname_listen(const char *address, uint16_t port, const char *hostname) |
| 373 | { |
| 374 | return nc_client_ch_add_bind_listen(address, port, hostname, NC_TI_OPENSSL); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | API int |
| 378 | nc_client_tls_ch_del_bind(const char *address, uint16_t port) |
| 379 | { |
| 380 | return nc_client_ch_del_bind(address, port, NC_TI_OPENSSL); |
| 381 | } |
| 382 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 383 | static int |
Michal Vasko | 55ce24a | 2022-09-07 09:24:43 +0200 | [diff] [blame] | 384 | nc_client_tls_update_opts(struct nc_client_tls_opts *opts, const char *peername) |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 385 | { |
Michal Vasko | 9af5232 | 2022-07-25 14:39:30 +0200 | [diff] [blame] | 386 | int rc = 0; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 387 | char *key; |
| 388 | X509_LOOKUP *lookup; |
Michal Vasko | 9af5232 | 2022-07-25 14:39:30 +0200 | [diff] [blame] | 389 | X509_VERIFY_PARAM *vpm = NULL; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 390 | |
| 391 | if (!opts->tls_ctx || opts->tls_ctx_change) { |
Michal Vasko | e22c673 | 2016-01-29 11:03:02 +0100 | [diff] [blame] | 392 | SSL_CTX_free(opts->tls_ctx); |
Michal Vasko | 18aeb5d | 2017-02-17 09:23:56 +0100 | [diff] [blame] | 393 | /* prepare global SSL context, highest available method is negotiated autmatically */ |
roman | 7fdc84d | 2023-06-06 13:14:53 +0200 | [diff] [blame] | 394 | if (!(opts->tls_ctx = SSL_CTX_new(TLS_client_method()))) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 395 | ERR(NULL, "Unable to create OpenSSL context (%s).", ERR_reason_error_string(ERR_get_error())); |
Michal Vasko | 9af5232 | 2022-07-25 14:39:30 +0200 | [diff] [blame] | 396 | rc = -1; |
| 397 | goto cleanup; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 398 | } |
| 399 | SSL_CTX_set_verify(opts->tls_ctx, SSL_VERIFY_PEER, tlsauth_verify_callback); |
| 400 | |
| 401 | /* get peer certificate */ |
| 402 | if (SSL_CTX_use_certificate_file(opts->tls_ctx, opts->cert_path, SSL_FILETYPE_PEM) != 1) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 403 | ERR(NULL, "Loading the client certificate from \'%s\' failed (%s).", opts->cert_path, |
| 404 | ERR_reason_error_string(ERR_get_error())); |
Michal Vasko | 9af5232 | 2022-07-25 14:39:30 +0200 | [diff] [blame] | 405 | rc = -1; |
| 406 | goto cleanup; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | /* if the file with private key not specified, expect that the private key is stored with the certificate */ |
| 410 | if (!opts->key_path) { |
| 411 | key = opts->cert_path; |
| 412 | } else { |
| 413 | key = opts->key_path; |
| 414 | } |
| 415 | if (SSL_CTX_use_PrivateKey_file(opts->tls_ctx, key, SSL_FILETYPE_PEM) != 1) { |
Michal Vasko | 9af5232 | 2022-07-25 14:39:30 +0200 | [diff] [blame] | 416 | ERR(NULL, "Loading the client private key from \'%s\' failed (%s).", key, |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 417 | ERR_reason_error_string(ERR_get_error())); |
Michal Vasko | 9af5232 | 2022-07-25 14:39:30 +0200 | [diff] [blame] | 418 | rc = -1; |
| 419 | goto cleanup; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | if (!SSL_CTX_load_verify_locations(opts->tls_ctx, opts->ca_file, opts->ca_dir)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 423 | ERR(NULL, "Failed to load the locations of trusted CA certificates (%s).", |
| 424 | ERR_reason_error_string(ERR_get_error())); |
Michal Vasko | 9af5232 | 2022-07-25 14:39:30 +0200 | [diff] [blame] | 425 | rc = -1; |
| 426 | goto cleanup; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 427 | } |
Michal Vasko | 9af5232 | 2022-07-25 14:39:30 +0200 | [diff] [blame] | 428 | |
Michal Vasko | 55ce24a | 2022-09-07 09:24:43 +0200 | [diff] [blame] | 429 | if (peername) { |
| 430 | /* server identity (hostname) verification */ |
| 431 | vpm = X509_VERIFY_PARAM_new(); |
| 432 | if (!X509_VERIFY_PARAM_set1_host(vpm, peername, 0)) { |
| 433 | ERR(NULL, "Failed to set expected server hostname (%s).", ERR_reason_error_string(ERR_get_error())); |
| 434 | rc = -1; |
| 435 | goto cleanup; |
| 436 | } |
| 437 | if (!SSL_CTX_set1_param(opts->tls_ctx, vpm)) { |
| 438 | ERR(NULL, "Failed to set verify params (%s).", ERR_reason_error_string(ERR_get_error())); |
| 439 | rc = -1; |
| 440 | goto cleanup; |
| 441 | } |
Michal Vasko | 9af5232 | 2022-07-25 14:39:30 +0200 | [diff] [blame] | 442 | } |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | if (opts->crl_store_change || (!opts->crl_store && (opts->crl_file || opts->crl_dir))) { |
| 446 | /* set the revocation store with the correct paths for the callback */ |
| 447 | X509_STORE_free(opts->crl_store); |
| 448 | |
| 449 | opts->crl_store = X509_STORE_new(); |
| 450 | if (!opts->crl_store) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 451 | ERR(NULL, "Unable to create a certificate store (%s).", ERR_reason_error_string(ERR_get_error())); |
Michal Vasko | 9af5232 | 2022-07-25 14:39:30 +0200 | [diff] [blame] | 452 | rc = -1; |
| 453 | goto cleanup; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 454 | } |
Michal Vasko | 18aeb5d | 2017-02-17 09:23:56 +0100 | [diff] [blame] | 455 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 456 | if (opts->crl_file) { |
| 457 | if (!(lookup = X509_STORE_add_lookup(opts->crl_store, X509_LOOKUP_file()))) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 458 | ERR(NULL, "Failed to add lookup method to CRL checking."); |
Michal Vasko | 9af5232 | 2022-07-25 14:39:30 +0200 | [diff] [blame] | 459 | rc = -1; |
| 460 | goto cleanup; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 461 | } |
| 462 | if (X509_LOOKUP_add_dir(lookup, opts->crl_file, X509_FILETYPE_PEM) != 1) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 463 | ERR(NULL, "Failed to add the revocation lookup file \"%s\".", opts->crl_file); |
Michal Vasko | 9af5232 | 2022-07-25 14:39:30 +0200 | [diff] [blame] | 464 | rc = -1; |
| 465 | goto cleanup; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 466 | } |
| 467 | } |
| 468 | |
| 469 | if (opts->crl_dir) { |
| 470 | if (!(lookup = X509_STORE_add_lookup(opts->crl_store, X509_LOOKUP_hash_dir()))) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 471 | ERR(NULL, "Failed to add lookup method to CRL checking."); |
Michal Vasko | 9af5232 | 2022-07-25 14:39:30 +0200 | [diff] [blame] | 472 | rc = -1; |
| 473 | goto cleanup; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 474 | } |
| 475 | if (X509_LOOKUP_add_dir(lookup, opts->crl_dir, X509_FILETYPE_PEM) != 1) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 476 | ERR(NULL, "Failed to add the revocation lookup directory \"%s\".", opts->crl_dir); |
Michal Vasko | 9af5232 | 2022-07-25 14:39:30 +0200 | [diff] [blame] | 477 | rc = -1; |
| 478 | goto cleanup; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 479 | } |
| 480 | } |
| 481 | } |
| 482 | |
Michal Vasko | 9af5232 | 2022-07-25 14:39:30 +0200 | [diff] [blame] | 483 | cleanup: |
| 484 | X509_VERIFY_PARAM_free(vpm); |
| 485 | return rc; |
| 486 | } |
| 487 | |
| 488 | static int |
Michal Vasko | a82745b | 2022-09-07 11:48:12 +0200 | [diff] [blame] | 489 | nc_client_tls_connect_check(int connect_ret, SSL *tls, const char *peername) |
Michal Vasko | 9af5232 | 2022-07-25 14:39:30 +0200 | [diff] [blame] | 490 | { |
| 491 | int verify; |
| 492 | |
| 493 | /* check certificate verification result */ |
| 494 | verify = SSL_get_verify_result(tls); |
| 495 | switch (verify) { |
| 496 | case X509_V_OK: |
| 497 | if (connect_ret == 1) { |
Michal Vasko | 7ffcd14 | 2022-09-07 09:24:22 +0200 | [diff] [blame] | 498 | VRB(NULL, "Server certificate verified (domain \"%s\").", peername); |
Michal Vasko | 9af5232 | 2022-07-25 14:39:30 +0200 | [diff] [blame] | 499 | } |
| 500 | break; |
| 501 | default: |
| 502 | ERR(NULL, "Server certificate error (%s).", X509_verify_cert_error_string(verify)); |
| 503 | } |
| 504 | |
| 505 | /* check TLS connection result */ |
| 506 | if (connect_ret != 1) { |
| 507 | switch (SSL_get_error(tls, connect_ret)) { |
| 508 | case SSL_ERROR_SYSCALL: |
Michal Vasko | 7ffcd14 | 2022-09-07 09:24:22 +0200 | [diff] [blame] | 509 | ERR(NULL, "SSL connect to \"%s\" failed (%s).", peername, errno ? strerror(errno) : "unexpected EOF"); |
Michal Vasko | 9af5232 | 2022-07-25 14:39:30 +0200 | [diff] [blame] | 510 | break; |
| 511 | case SSL_ERROR_SSL: |
Michal Vasko | 7ffcd14 | 2022-09-07 09:24:22 +0200 | [diff] [blame] | 512 | ERR(NULL, "SSL connect to \"%s\" failed (%s).", peername, ERR_reason_error_string(ERR_get_error())); |
Michal Vasko | 9af5232 | 2022-07-25 14:39:30 +0200 | [diff] [blame] | 513 | break; |
| 514 | default: |
Michal Vasko | 7ffcd14 | 2022-09-07 09:24:22 +0200 | [diff] [blame] | 515 | ERR(NULL, "SSL connect to \"%s\" failed.", peername); |
Michal Vasko | 9af5232 | 2022-07-25 14:39:30 +0200 | [diff] [blame] | 516 | break; |
| 517 | } |
| 518 | } |
| 519 | |
| 520 | return connect_ret; |
Radek Krejci | 9f03b48 | 2015-10-22 16:02:10 +0200 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | API struct nc_session * |
Michal Vasko | 9bee18d | 2015-12-08 14:41:42 +0100 | [diff] [blame] | 524 | nc_connect_tls(const char *host, unsigned short port, struct ly_ctx *ctx) |
Radek Krejci | 9f03b48 | 2015-10-22 16:02:10 +0200 | [diff] [blame] | 525 | { |
Radek Krejci | 9f03b48 | 2015-10-22 16:02:10 +0200 | [diff] [blame] | 526 | struct nc_session *session = NULL; |
Michal Vasko | 9af5232 | 2022-07-25 14:39:30 +0200 | [diff] [blame] | 527 | int sock, ret; |
roman | 6ece9c5 | 2022-06-22 09:29:17 +0200 | [diff] [blame] | 528 | struct timespec ts_timeout; |
Michal Vasko | 66032bc | 2019-01-22 15:03:12 +0100 | [diff] [blame] | 529 | char *ip_host = NULL; |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 530 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 531 | if (!tls_opts.cert_path || (!tls_opts.ca_file && !tls_opts.ca_dir)) { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 532 | ERRINIT; |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 533 | return NULL; |
| 534 | } |
Radek Krejci | 9f03b48 | 2015-10-22 16:02:10 +0200 | [diff] [blame] | 535 | |
| 536 | /* process parameters */ |
roman | 3f9b65c | 2023-06-05 14:26:58 +0200 | [diff] [blame] | 537 | if (!host || (host[0] == '\0')) { |
Radek Krejci | 9f03b48 | 2015-10-22 16:02:10 +0200 | [diff] [blame] | 538 | host = "localhost"; |
| 539 | } |
| 540 | |
| 541 | if (!port) { |
| 542 | port = NC_PORT_TLS; |
| 543 | } |
| 544 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 545 | /* create/update TLS structures */ |
Michal Vasko | 9af5232 | 2022-07-25 14:39:30 +0200 | [diff] [blame] | 546 | if (nc_client_tls_update_opts(&tls_opts, host)) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 547 | return NULL; |
| 548 | } |
| 549 | |
Radek Krejci | 9f03b48 | 2015-10-22 16:02:10 +0200 | [diff] [blame] | 550 | /* prepare session structure */ |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 551 | session = nc_new_session(NC_CLIENT, 0); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame^] | 552 | NC_CHECK_ERRMEM_RET(!session, NULL); |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 553 | session->status = NC_STATUS_STARTING; |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 554 | |
| 555 | /* fill the session */ |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 556 | session->ti_type = NC_TI_OPENSSL; |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 557 | if (!(session->ti.tls = SSL_new(tls_opts.tls_ctx))) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 558 | ERR(NULL, "Failed to create a new TLS session structure (%s).", ERR_reason_error_string(ERR_get_error())); |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 559 | goto fail; |
| 560 | } |
Radek Krejci | 9f03b48 | 2015-10-22 16:02:10 +0200 | [diff] [blame] | 561 | |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 562 | /* create and assign socket */ |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 563 | sock = nc_sock_connect(host, port, -1, &client_opts.ka, NULL, &ip_host); |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 564 | if (sock == -1) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 565 | ERR(NULL, "Unable to connect to %s:%u (%s).", host, port, strerror(errno)); |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 566 | goto fail; |
| 567 | } |
| 568 | SSL_set_fd(session->ti.tls, sock); |
| 569 | |
| 570 | /* set the SSL_MODE_AUTO_RETRY flag to allow OpenSSL perform re-handshake automatically */ |
| 571 | SSL_set_mode(session->ti.tls, SSL_MODE_AUTO_RETRY); |
| 572 | |
| 573 | /* connect and perform the handshake */ |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 574 | nc_timeouttime_get(&ts_timeout, NC_TRANSPORT_TIMEOUT); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 575 | tlsauth_ch = 0; |
Michal Vasko | d51a264 | 2021-09-03 13:03:24 +0200 | [diff] [blame] | 576 | while (((ret = SSL_connect(session->ti.tls)) != 1) && (SSL_get_error(session->ti.tls, ret) == SSL_ERROR_WANT_READ)) { |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 577 | usleep(NC_TIMEOUT_STEP); |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 578 | if (nc_timeouttime_cur_diff(&ts_timeout) < 1) { |
Michal Vasko | 7ffcd14 | 2022-09-07 09:24:22 +0200 | [diff] [blame] | 579 | ERR(NULL, "SSL connect timeout."); |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 580 | goto fail; |
| 581 | } |
| 582 | } |
Michal Vasko | 3cd55a7 | 2022-09-06 15:52:39 +0200 | [diff] [blame] | 583 | |
| 584 | /* check for errors */ |
Michal Vasko | a82745b | 2022-09-07 11:48:12 +0200 | [diff] [blame] | 585 | if (nc_client_tls_connect_check(ret, session->ti.tls, host) != 1) { |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 586 | goto fail; |
| 587 | } |
| 588 | |
Michal Vasko | 7893907 | 2022-12-12 07:43:18 +0100 | [diff] [blame] | 589 | if (nc_client_session_new_ctx(session, ctx) != EXIT_SUCCESS) { |
Radek Krejci | fd5b668 | 2017-06-13 15:52:53 +0200 | [diff] [blame] | 590 | goto fail; |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 591 | } |
Radek Krejci | fd5b668 | 2017-06-13 15:52:53 +0200 | [diff] [blame] | 592 | ctx = session->ctx; |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 593 | |
Radek Krejci | 9f03b48 | 2015-10-22 16:02:10 +0200 | [diff] [blame] | 594 | /* NETCONF handshake */ |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 595 | if (nc_handshake_io(session) != NC_MSG_HELLO) { |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 596 | goto fail; |
Radek Krejci | 9f03b48 | 2015-10-22 16:02:10 +0200 | [diff] [blame] | 597 | } |
Michal Vasko | ad61170 | 2015-12-03 13:41:51 +0100 | [diff] [blame] | 598 | session->status = NC_STATUS_RUNNING; |
Radek Krejci | 9f03b48 | 2015-10-22 16:02:10 +0200 | [diff] [blame] | 599 | |
Michal Vasko | ef57833 | 2016-01-25 13:20:09 +0100 | [diff] [blame] | 600 | if (nc_ctx_check_and_fill(session) == -1) { |
Michal Vasko | 57eb940 | 2015-12-08 14:38:12 +0100 | [diff] [blame] | 601 | goto fail; |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 602 | } |
| 603 | |
| 604 | /* store information into session and the dictionary */ |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 605 | session->host = ip_host; |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 606 | session->port = port; |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 607 | session->username = strdup("certificate-based"); |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 608 | |
Radek Krejci | 9f03b48 | 2015-10-22 16:02:10 +0200 | [diff] [blame] | 609 | return session; |
| 610 | |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 611 | fail: |
Michal Vasko | 66032bc | 2019-01-22 15:03:12 +0100 | [diff] [blame] | 612 | free(ip_host); |
Michal Vasko | e1a64ec | 2016-03-01 12:21:58 +0100 | [diff] [blame] | 613 | nc_session_free(session, NULL); |
Radek Krejci | 9f03b48 | 2015-10-22 16:02:10 +0200 | [diff] [blame] | 614 | return NULL; |
| 615 | } |
| 616 | |
| 617 | API struct nc_session * |
| 618 | nc_connect_libssl(SSL *tls, struct ly_ctx *ctx) |
| 619 | { |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 620 | struct nc_session *session; |
Radek Krejci | 9f03b48 | 2015-10-22 16:02:10 +0200 | [diff] [blame] | 621 | |
roman | 4067241 | 2023-05-04 11:10:22 +0200 | [diff] [blame] | 622 | NC_CHECK_ARG_RET(NULL, tls, NULL); |
| 623 | |
| 624 | if (!SSL_is_init_finished(tls)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 625 | ERR(NULL, "Supplied TLS session is not fully connected!"); |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 626 | return NULL; |
| 627 | } |
| 628 | |
| 629 | /* prepare session structure */ |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 630 | session = nc_new_session(NC_CLIENT, 0); |
roman | 3a95bb2 | 2023-10-26 11:07:17 +0200 | [diff] [blame^] | 631 | NC_CHECK_ERRMEM_RET(!session, NULL); |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 632 | session->status = NC_STATUS_STARTING; |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 633 | session->ti_type = NC_TI_OPENSSL; |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 634 | session->ti.tls = tls; |
| 635 | |
Michal Vasko | 7893907 | 2022-12-12 07:43:18 +0100 | [diff] [blame] | 636 | if (nc_client_session_new_ctx(session, ctx) != EXIT_SUCCESS) { |
Radek Krejci | fd5b668 | 2017-06-13 15:52:53 +0200 | [diff] [blame] | 637 | goto fail; |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 638 | } |
Radek Krejci | fd5b668 | 2017-06-13 15:52:53 +0200 | [diff] [blame] | 639 | ctx = session->ctx; |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 640 | |
| 641 | /* NETCONF handshake */ |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 642 | if (nc_handshake_io(session) != NC_MSG_HELLO) { |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 643 | goto fail; |
| 644 | } |
Michal Vasko | ad61170 | 2015-12-03 13:41:51 +0100 | [diff] [blame] | 645 | session->status = NC_STATUS_RUNNING; |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 646 | |
Michal Vasko | ef57833 | 2016-01-25 13:20:09 +0100 | [diff] [blame] | 647 | if (nc_ctx_check_and_fill(session) == -1) { |
Michal Vasko | 57eb940 | 2015-12-08 14:38:12 +0100 | [diff] [blame] | 648 | goto fail; |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 649 | } |
| 650 | |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 651 | return session; |
| 652 | |
| 653 | fail: |
Michal Vasko | b604ed5 | 2022-01-24 09:56:14 +0100 | [diff] [blame] | 654 | session->ti_type = NC_TI_NONE; |
Michal Vasko | a42a7f0 | 2021-07-02 08:43:12 +0200 | [diff] [blame] | 655 | session->ti.tls = NULL; |
Michal Vasko | e1a64ec | 2016-03-01 12:21:58 +0100 | [diff] [blame] | 656 | nc_session_free(session, NULL); |
Radek Krejci | 9f03b48 | 2015-10-22 16:02:10 +0200 | [diff] [blame] | 657 | return NULL; |
| 658 | } |
| 659 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 660 | struct nc_session * |
Michal Vasko | 9d4cca5 | 2022-09-07 11:19:57 +0200 | [diff] [blame] | 661 | nc_accept_callhome_tls_sock(int sock, const char *host, uint16_t port, struct ly_ctx *ctx, int timeout, const char *peername) |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 662 | { |
Michal Vasko | 3cd55a7 | 2022-09-06 15:52:39 +0200 | [diff] [blame] | 663 | int ret; |
Michal Vasko | c64c38c | 2021-07-02 08:43:53 +0200 | [diff] [blame] | 664 | SSL *tls = NULL; |
| 665 | struct nc_session *session = NULL; |
roman | 6ece9c5 | 2022-06-22 09:29:17 +0200 | [diff] [blame] | 666 | struct timespec ts_timeout; |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 667 | |
Michal Vasko | 9d4cca5 | 2022-09-07 11:19:57 +0200 | [diff] [blame] | 668 | /* create/update TLS structures with explicit expected peername, if any set, the host is just the IP */ |
| 669 | if (nc_client_tls_update_opts(&tls_ch_opts, peername)) { |
Michal Vasko | c64c38c | 2021-07-02 08:43:53 +0200 | [diff] [blame] | 670 | goto cleanup; |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 671 | } |
| 672 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 673 | if (!(tls = SSL_new(tls_ch_opts.tls_ctx))) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 674 | ERR(NULL, "Failed to create new TLS session structure (%s).", ERR_reason_error_string(ERR_get_error())); |
Michal Vasko | c64c38c | 2021-07-02 08:43:53 +0200 | [diff] [blame] | 675 | goto cleanup; |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 676 | } |
| 677 | |
| 678 | SSL_set_fd(tls, sock); |
| 679 | |
| 680 | /* set the SSL_MODE_AUTO_RETRY flag to allow OpenSSL perform re-handshake automatically */ |
| 681 | SSL_set_mode(tls, SSL_MODE_AUTO_RETRY); |
| 682 | |
| 683 | /* connect and perform the handshake */ |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 684 | if (timeout > -1) { |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 685 | nc_timeouttime_get(&ts_timeout, timeout); |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 686 | } |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 687 | tlsauth_ch = 1; |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 688 | while (((ret = SSL_connect(tls)) == -1) && (SSL_get_error(tls, ret) == SSL_ERROR_WANT_READ)) { |
| 689 | usleep(NC_TIMEOUT_STEP); |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 690 | if ((timeout > -1) && (nc_timeouttime_cur_diff(&ts_timeout) < 1)) { |
Michal Vasko | 7ffcd14 | 2022-09-07 09:24:22 +0200 | [diff] [blame] | 691 | ERR(NULL, "SSL connect timeout."); |
roman | 6ece9c5 | 2022-06-22 09:29:17 +0200 | [diff] [blame] | 692 | goto cleanup; |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 693 | } |
| 694 | } |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 695 | |
Michal Vasko | 3cd55a7 | 2022-09-06 15:52:39 +0200 | [diff] [blame] | 696 | /* check for errors */ |
Michal Vasko | a82745b | 2022-09-07 11:48:12 +0200 | [diff] [blame] | 697 | if (nc_client_tls_connect_check(ret, tls, peername ? peername : host) != 1) { |
Michal Vasko | 3cd55a7 | 2022-09-06 15:52:39 +0200 | [diff] [blame] | 698 | goto cleanup; |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 699 | } |
| 700 | |
Michal Vasko | c64c38c | 2021-07-02 08:43:53 +0200 | [diff] [blame] | 701 | /* connect */ |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 702 | session = nc_connect_libssl(tls, ctx); |
Michal Vasko | c64c38c | 2021-07-02 08:43:53 +0200 | [diff] [blame] | 703 | if (!session) { |
| 704 | goto cleanup; |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 705 | } |
| 706 | |
Michal Vasko | c64c38c | 2021-07-02 08:43:53 +0200 | [diff] [blame] | 707 | session->flags |= NC_SESSION_CALLHOME; |
| 708 | |
| 709 | /* store information into session and the dictionary */ |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 710 | session->host = strdup(host); |
Michal Vasko | c64c38c | 2021-07-02 08:43:53 +0200 | [diff] [blame] | 711 | session->port = port; |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 712 | session->username = strdup("certificate-based"); |
Michal Vasko | c64c38c | 2021-07-02 08:43:53 +0200 | [diff] [blame] | 713 | |
| 714 | cleanup: |
| 715 | if (!session) { |
| 716 | SSL_free(tls); |
| 717 | close(sock); |
| 718 | } |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 719 | return session; |
| 720 | } |