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 | |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 31 | #include "libnetconf.h" |
Michal Vasko | e22c673 | 2016-01-29 11:03:02 +0100 | [diff] [blame] | 32 | #include "session_client.h" |
| 33 | #include "session_client_ch.h" |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 34 | |
Rosen Penev | 4f552d6 | 2019-06-26 16:10:43 -0700 | [diff] [blame] | 35 | #if OPENSSL_VERSION_NUMBER < 0x10100000L |
| 36 | #define X509_STORE_CTX_get_by_subject X509_STORE_get_by_subject |
| 37 | #endif |
| 38 | |
Radek Krejci | 62aa064 | 2017-05-25 16:33:49 +0200 | [diff] [blame] | 39 | struct nc_client_context *nc_client_context_location(void); |
| 40 | |
| 41 | #define client_opts nc_client_context_location()->opts |
| 42 | #define tls_opts nc_client_context_location()->tls_opts |
| 43 | #define tls_ch_opts nc_client_context_location()->tls_ch_opts |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 44 | |
| 45 | static int tlsauth_ch; |
Radek Krejci | 9f03b48 | 2015-10-22 16:02:10 +0200 | [diff] [blame] | 46 | |
Michal Vasko | 18aeb5d | 2017-02-17 09:23:56 +0100 | [diff] [blame] | 47 | #if OPENSSL_VERSION_NUMBER >= 0x10100000L // >= 1.1.0 |
| 48 | |
| 49 | static int |
| 50 | tlsauth_verify_callback(int preverify_ok, X509_STORE_CTX *x509_ctx) |
| 51 | { |
| 52 | X509_STORE_CTX *store_ctx; |
| 53 | X509_OBJECT *obj; |
| 54 | X509_NAME *subject, *issuer; |
| 55 | X509 *cert; |
| 56 | X509_CRL *crl; |
| 57 | X509_REVOKED *revoked; |
| 58 | EVP_PKEY *pubkey; |
| 59 | int i, n, rc; |
| 60 | const ASN1_TIME *next_update = NULL; |
| 61 | struct nc_client_tls_opts *opts; |
| 62 | |
| 63 | if (!preverify_ok) { |
| 64 | return 0; |
| 65 | } |
| 66 | |
| 67 | opts = (tlsauth_ch ? &tls_ch_opts : &tls_opts); |
| 68 | |
| 69 | if (!opts->crl_store) { |
| 70 | /* nothing to check */ |
| 71 | return 1; |
| 72 | } |
| 73 | |
| 74 | cert = X509_STORE_CTX_get_current_cert(x509_ctx); |
| 75 | subject = X509_get_subject_name(cert); |
| 76 | issuer = X509_get_issuer_name(cert); |
| 77 | |
| 78 | /* try to retrieve a CRL corresponding to the _subject_ of |
| 79 | * the current certificate in order to verify it's integrity */ |
| 80 | store_ctx = X509_STORE_CTX_new(); |
| 81 | obj = X509_OBJECT_new(); |
| 82 | X509_STORE_CTX_init(store_ctx, opts->crl_store, NULL, NULL); |
Rosen Penev | 4f552d6 | 2019-06-26 16:10:43 -0700 | [diff] [blame] | 83 | 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] | 84 | X509_STORE_CTX_free(store_ctx); |
| 85 | crl = X509_OBJECT_get0_X509_CRL(obj); |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 86 | if ((rc > 0) && crl) { |
Michal Vasko | 18aeb5d | 2017-02-17 09:23:56 +0100 | [diff] [blame] | 87 | next_update = X509_CRL_get0_nextUpdate(crl); |
| 88 | |
| 89 | /* verify the signature on this CRL */ |
| 90 | pubkey = X509_get_pubkey(cert); |
| 91 | if (X509_CRL_verify(crl, pubkey) <= 0) { |
| 92 | X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_CRL_SIGNATURE_FAILURE); |
| 93 | X509_OBJECT_free(obj); |
| 94 | if (pubkey) { |
| 95 | EVP_PKEY_free(pubkey); |
| 96 | } |
| 97 | return 0; /* fail */ |
| 98 | } |
| 99 | if (pubkey) { |
| 100 | EVP_PKEY_free(pubkey); |
| 101 | } |
| 102 | |
| 103 | /* check date of CRL to make sure it's not expired */ |
| 104 | if (!next_update) { |
| 105 | X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD); |
| 106 | X509_OBJECT_free(obj); |
| 107 | return 0; /* fail */ |
| 108 | } |
| 109 | if (X509_cmp_current_time(next_update) < 0) { |
| 110 | X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_CRL_HAS_EXPIRED); |
| 111 | X509_OBJECT_free(obj); |
| 112 | return 0; /* fail */ |
| 113 | } |
| 114 | X509_OBJECT_free(obj); |
| 115 | } |
| 116 | |
| 117 | /* try to retrieve a CRL corresponding to the _issuer_ of |
| 118 | * the current certificate in order to check for revocation */ |
| 119 | store_ctx = X509_STORE_CTX_new(); |
| 120 | obj = X509_OBJECT_new(); |
| 121 | X509_STORE_CTX_init(store_ctx, opts->crl_store, NULL, NULL); |
Rosen Penev | 4f552d6 | 2019-06-26 16:10:43 -0700 | [diff] [blame] | 122 | 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] | 123 | X509_STORE_CTX_free(store_ctx); |
| 124 | crl = X509_OBJECT_get0_X509_CRL(obj); |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 125 | if ((rc > 0) && crl) { |
Michal Vasko | 18aeb5d | 2017-02-17 09:23:56 +0100 | [diff] [blame] | 126 | /* check if the current certificate is revoked by this CRL */ |
| 127 | n = sk_X509_REVOKED_num(X509_CRL_get_REVOKED(crl)); |
| 128 | for (i = 0; i < n; i++) { |
| 129 | revoked = sk_X509_REVOKED_value(X509_CRL_get_REVOKED(crl), i); |
| 130 | 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] | 131 | ERR(NULL, "Certificate revoked!"); |
Michal Vasko | 18aeb5d | 2017-02-17 09:23:56 +0100 | [diff] [blame] | 132 | X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_CERT_REVOKED); |
| 133 | X509_OBJECT_free(obj); |
| 134 | return 0; /* fail */ |
| 135 | } |
| 136 | } |
| 137 | X509_OBJECT_free(obj); |
| 138 | } |
| 139 | |
| 140 | return 1; /* success */ |
| 141 | } |
| 142 | |
| 143 | #else |
| 144 | |
Radek Krejci | 9f03b48 | 2015-10-22 16:02:10 +0200 | [diff] [blame] | 145 | static int |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 146 | tlsauth_verify_callback(int preverify_ok, X509_STORE_CTX *x509_ctx) |
Radek Krejci | 9f03b48 | 2015-10-22 16:02:10 +0200 | [diff] [blame] | 147 | { |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 148 | X509_STORE_CTX store_ctx; |
| 149 | X509_OBJECT obj; |
| 150 | X509_NAME *subject, *issuer; |
| 151 | X509 *cert; |
| 152 | X509_CRL *crl; |
| 153 | X509_REVOKED *revoked; |
| 154 | EVP_PKEY *pubkey; |
| 155 | int i, n, rc; |
| 156 | ASN1_TIME *next_update = NULL; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 157 | struct nc_client_tls_opts *opts; |
Radek Krejci | 9f03b48 | 2015-10-22 16:02:10 +0200 | [diff] [blame] | 158 | |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 159 | if (!preverify_ok) { |
| 160 | return 0; |
| 161 | } |
Radek Krejci | 9f03b48 | 2015-10-22 16:02:10 +0200 | [diff] [blame] | 162 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 163 | opts = (tlsauth_ch ? &tls_ch_opts : &tls_opts); |
| 164 | |
| 165 | if (!opts->crl_store) { |
| 166 | /* nothing to check */ |
| 167 | return 1; |
| 168 | } |
| 169 | |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 170 | cert = X509_STORE_CTX_get_current_cert(x509_ctx); |
| 171 | subject = X509_get_subject_name(cert); |
| 172 | issuer = X509_get_issuer_name(cert); |
| 173 | |
| 174 | /* try to retrieve a CRL corresponding to the _subject_ of |
| 175 | * the current certificate in order to verify it's integrity */ |
| 176 | memset((char *)&obj, 0, sizeof obj); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 177 | X509_STORE_CTX_init(&store_ctx, opts->crl_store, NULL, NULL); |
Rosen Penev | 4f552d6 | 2019-06-26 16:10:43 -0700 | [diff] [blame] | 178 | rc = X509_STORE_CTX_get_by_subject(&store_ctx, X509_LU_CRL, subject, &obj); |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 179 | X509_STORE_CTX_cleanup(&store_ctx); |
| 180 | crl = obj.data.crl; |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 181 | if ((rc > 0) && crl) { |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 182 | next_update = X509_CRL_get_nextUpdate(crl); |
| 183 | |
| 184 | /* verify the signature on this CRL */ |
| 185 | pubkey = X509_get_pubkey(cert); |
| 186 | if (X509_CRL_verify(crl, pubkey) <= 0) { |
| 187 | X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_CRL_SIGNATURE_FAILURE); |
| 188 | X509_OBJECT_free_contents(&obj); |
| 189 | if (pubkey) { |
| 190 | EVP_PKEY_free(pubkey); |
| 191 | } |
| 192 | return 0; /* fail */ |
| 193 | } |
| 194 | if (pubkey) { |
| 195 | EVP_PKEY_free(pubkey); |
| 196 | } |
| 197 | |
| 198 | /* check date of CRL to make sure it's not expired */ |
| 199 | if (!next_update) { |
| 200 | X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD); |
| 201 | X509_OBJECT_free_contents(&obj); |
| 202 | return 0; /* fail */ |
| 203 | } |
| 204 | if (X509_cmp_current_time(next_update) < 0) { |
| 205 | X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_CRL_HAS_EXPIRED); |
| 206 | X509_OBJECT_free_contents(&obj); |
| 207 | return 0; /* fail */ |
| 208 | } |
| 209 | X509_OBJECT_free_contents(&obj); |
| 210 | } |
| 211 | |
| 212 | /* try to retrieve a CRL corresponding to the _issuer_ of |
| 213 | * the current certificate in order to check for revocation */ |
| 214 | memset((char *)&obj, 0, sizeof obj); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 215 | X509_STORE_CTX_init(&store_ctx, opts->crl_store, NULL, NULL); |
Rosen Penev | 4f552d6 | 2019-06-26 16:10:43 -0700 | [diff] [blame] | 216 | rc = X509_STORE_CTX_get_by_subject(&store_ctx, X509_LU_CRL, issuer, &obj); |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 217 | X509_STORE_CTX_cleanup(&store_ctx); |
| 218 | crl = obj.data.crl; |
Michal Vasko | b83a3fa | 2021-05-26 09:53:42 +0200 | [diff] [blame] | 219 | if ((rc > 0) && crl) { |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 220 | /* check if the current certificate is revoked by this CRL */ |
| 221 | n = sk_X509_REVOKED_num(X509_CRL_get_REVOKED(crl)); |
| 222 | for (i = 0; i < n; i++) { |
| 223 | revoked = sk_X509_REVOKED_value(X509_CRL_get_REVOKED(crl), i); |
| 224 | if (ASN1_INTEGER_cmp(revoked->serialNumber, X509_get_serialNumber(cert)) == 0) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 225 | ERR(NULL, "Certificate revoked!"); |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 226 | X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_CERT_REVOKED); |
| 227 | X509_OBJECT_free_contents(&obj); |
| 228 | return 0; /* fail */ |
| 229 | } |
| 230 | } |
| 231 | X509_OBJECT_free_contents(&obj); |
| 232 | } |
| 233 | |
| 234 | return 1; /* success */ |
| 235 | } |
| 236 | |
Michal Vasko | 18aeb5d | 2017-02-17 09:23:56 +0100 | [diff] [blame] | 237 | #endif |
| 238 | |
Kevin Barrett | d37d2fb | 2019-08-28 14:25:34 -0400 | [diff] [blame] | 239 | void |
Michal Vasko | e22c673 | 2016-01-29 11:03:02 +0100 | [diff] [blame] | 240 | _nc_client_tls_destroy_opts(struct nc_client_tls_opts *opts) |
| 241 | { |
| 242 | free(opts->cert_path); |
| 243 | free(opts->key_path); |
| 244 | free(opts->ca_file); |
| 245 | free(opts->ca_dir); |
| 246 | SSL_CTX_free(opts->tls_ctx); |
| 247 | |
| 248 | free(opts->crl_file); |
| 249 | free(opts->crl_dir); |
| 250 | X509_STORE_free(opts->crl_store); |
Radek Krejci | 5cebc6b | 2017-05-26 13:24:38 +0200 | [diff] [blame] | 251 | |
| 252 | memset(opts, 0, sizeof *opts); |
Michal Vasko | e22c673 | 2016-01-29 11:03:02 +0100 | [diff] [blame] | 253 | } |
| 254 | |
Michal Vasko | b7558c5 | 2016-02-26 15:04:19 +0100 | [diff] [blame] | 255 | void |
Michal Vasko | e22c673 | 2016-01-29 11:03:02 +0100 | [diff] [blame] | 256 | nc_client_tls_destroy_opts(void) |
| 257 | { |
| 258 | _nc_client_tls_destroy_opts(&tls_opts); |
| 259 | _nc_client_tls_destroy_opts(&tls_ch_opts); |
| 260 | } |
| 261 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 262 | static int |
Michal Vasko | e22c673 | 2016-01-29 11:03:02 +0100 | [diff] [blame] | 263 | _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] | 264 | { |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 265 | if (!client_cert) { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 266 | ERRARG("client_cert"); |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 267 | return -1; |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 268 | } |
| 269 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 270 | free(opts->cert_path); |
| 271 | free(opts->key_path); |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 272 | |
Michal Vasko | 9db2a6f | 2016-02-01 13:26:03 +0100 | [diff] [blame] | 273 | opts->cert_path = strdup(client_cert); |
| 274 | if (!opts->cert_path) { |
| 275 | ERRMEM; |
| 276 | return -1; |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 277 | } |
| 278 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 279 | if (client_key) { |
| 280 | opts->key_path = strdup(client_key); |
Michal Vasko | 9db2a6f | 2016-02-01 13:26:03 +0100 | [diff] [blame] | 281 | if (!opts->key_path) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 282 | ERRMEM; |
| 283 | return -1; |
| 284 | } |
| 285 | } else { |
| 286 | opts->key_path = NULL; |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 287 | } |
| 288 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 289 | opts->tls_ctx_change = 1; |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 290 | |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 291 | return 0; |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 292 | } |
| 293 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 294 | API int |
Michal Vasko | e22c673 | 2016-01-29 11:03:02 +0100 | [diff] [blame] | 295 | 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] | 296 | { |
Michal Vasko | e22c673 | 2016-01-29 11:03:02 +0100 | [diff] [blame] | 297 | 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] | 298 | } |
| 299 | |
| 300 | API int |
Michal Vasko | e22c673 | 2016-01-29 11:03:02 +0100 | [diff] [blame] | 301 | 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] | 302 | { |
Michal Vasko | e22c673 | 2016-01-29 11:03:02 +0100 | [diff] [blame] | 303 | return _nc_client_tls_set_cert_key_paths(client_cert, client_key, &tls_ch_opts); |
| 304 | } |
| 305 | |
| 306 | static void |
| 307 | _nc_client_tls_get_cert_key_paths(const char **client_cert, const char **client_key, struct nc_client_tls_opts *opts) |
| 308 | { |
| 309 | if (!client_cert && !client_key) { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 310 | ERRARG("client_cert and client_key"); |
Michal Vasko | e22c673 | 2016-01-29 11:03:02 +0100 | [diff] [blame] | 311 | return; |
| 312 | } |
| 313 | |
| 314 | if (client_cert) { |
| 315 | *client_cert = opts->cert_path; |
| 316 | } |
| 317 | if (client_key) { |
| 318 | *client_key = opts->key_path; |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | API void |
| 323 | nc_client_tls_get_cert_key_paths(const char **client_cert, const char **client_key) |
| 324 | { |
| 325 | _nc_client_tls_get_cert_key_paths(client_cert, client_key, &tls_opts); |
| 326 | } |
| 327 | |
| 328 | API void |
| 329 | nc_client_tls_ch_get_cert_key_paths(const char **client_cert, const char **client_key) |
| 330 | { |
| 331 | _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] | 332 | } |
| 333 | |
| 334 | static int |
Michal Vasko | e22c673 | 2016-01-29 11:03:02 +0100 | [diff] [blame] | 335 | _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] | 336 | { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 337 | if (!ca_file && !ca_dir) { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 338 | ERRARG("ca_file and ca_dir"); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 339 | return -1; |
| 340 | } |
| 341 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 342 | free(opts->ca_file); |
| 343 | free(opts->ca_dir); |
| 344 | |
| 345 | if (ca_file) { |
| 346 | opts->ca_file = strdup(ca_file); |
| 347 | if (!opts->ca_file) { |
| 348 | ERRMEM; |
| 349 | return -1; |
| 350 | } |
| 351 | } else { |
| 352 | opts->ca_file = NULL; |
| 353 | } |
| 354 | |
| 355 | if (ca_dir) { |
| 356 | opts->ca_dir = strdup(ca_dir); |
| 357 | if (!opts->ca_dir) { |
| 358 | ERRMEM; |
| 359 | return -1; |
| 360 | } |
| 361 | } else { |
| 362 | opts->ca_dir = NULL; |
| 363 | } |
| 364 | |
| 365 | opts->tls_ctx_change = 1; |
| 366 | |
| 367 | return 0; |
| 368 | } |
| 369 | |
| 370 | API int |
Michal Vasko | e22c673 | 2016-01-29 11:03:02 +0100 | [diff] [blame] | 371 | 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] | 372 | { |
Michal Vasko | e22c673 | 2016-01-29 11:03:02 +0100 | [diff] [blame] | 373 | 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] | 374 | } |
| 375 | |
| 376 | API int |
Michal Vasko | e22c673 | 2016-01-29 11:03:02 +0100 | [diff] [blame] | 377 | 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] | 378 | { |
Michal Vasko | e22c673 | 2016-01-29 11:03:02 +0100 | [diff] [blame] | 379 | return _nc_client_tls_set_trusted_ca_paths(ca_file, ca_dir, &tls_ch_opts); |
| 380 | } |
| 381 | |
| 382 | static void |
| 383 | _nc_client_tls_get_trusted_ca_paths(const char **ca_file, const char **ca_dir, struct nc_client_tls_opts *opts) |
| 384 | { |
| 385 | if (!ca_file && !ca_dir) { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 386 | ERRARG("ca_file and ca_dir"); |
Michal Vasko | e22c673 | 2016-01-29 11:03:02 +0100 | [diff] [blame] | 387 | return; |
| 388 | } |
| 389 | |
| 390 | if (ca_file) { |
| 391 | *ca_file = opts->ca_file; |
| 392 | } |
| 393 | if (ca_dir) { |
| 394 | *ca_dir = opts->ca_dir; |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | API void |
| 399 | nc_client_tls_get_trusted_ca_paths(const char **ca_file, const char **ca_dir) |
| 400 | { |
| 401 | _nc_client_tls_get_trusted_ca_paths(ca_file, ca_dir, &tls_opts); |
| 402 | } |
| 403 | |
| 404 | API void |
| 405 | nc_client_tls_ch_get_trusted_ca_paths(const char **ca_file, const char **ca_dir) |
| 406 | { |
| 407 | _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] | 408 | } |
| 409 | |
| 410 | static int |
Michal Vasko | e22c673 | 2016-01-29 11:03:02 +0100 | [diff] [blame] | 411 | _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] | 412 | { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 413 | if (!crl_file && !crl_dir) { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 414 | ERRARG("crl_file and crl_dir"); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 415 | return -1; |
| 416 | } |
| 417 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 418 | free(opts->crl_file); |
| 419 | free(opts->crl_dir); |
| 420 | |
| 421 | if (crl_file) { |
| 422 | opts->crl_file = strdup(crl_file); |
| 423 | if (!opts->crl_file) { |
| 424 | ERRMEM; |
| 425 | return -1; |
| 426 | } |
| 427 | } else { |
| 428 | opts->crl_file = NULL; |
| 429 | } |
| 430 | |
| 431 | if (crl_dir) { |
| 432 | opts->crl_dir = strdup(crl_dir); |
| 433 | if (!opts->crl_dir) { |
| 434 | ERRMEM; |
| 435 | return -1; |
| 436 | } |
| 437 | } else { |
| 438 | opts->crl_dir = NULL; |
| 439 | } |
| 440 | |
| 441 | opts->crl_store_change = 1; |
| 442 | |
| 443 | return 0; |
| 444 | } |
| 445 | |
| 446 | API int |
Michal Vasko | e22c673 | 2016-01-29 11:03:02 +0100 | [diff] [blame] | 447 | 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] | 448 | { |
Michal Vasko | e22c673 | 2016-01-29 11:03:02 +0100 | [diff] [blame] | 449 | 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] | 450 | } |
| 451 | |
| 452 | API int |
Michal Vasko | e22c673 | 2016-01-29 11:03:02 +0100 | [diff] [blame] | 453 | 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] | 454 | { |
Michal Vasko | e22c673 | 2016-01-29 11:03:02 +0100 | [diff] [blame] | 455 | return _nc_client_tls_set_crl_paths(crl_file, crl_dir, &tls_ch_opts); |
| 456 | } |
| 457 | |
| 458 | static void |
| 459 | _nc_client_tls_get_crl_paths(const char **crl_file, const char **crl_dir, struct nc_client_tls_opts *opts) |
| 460 | { |
| 461 | if (!crl_file && !crl_dir) { |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 462 | ERRARG("crl_file and crl_dir"); |
Michal Vasko | e22c673 | 2016-01-29 11:03:02 +0100 | [diff] [blame] | 463 | return; |
| 464 | } |
| 465 | |
| 466 | if (crl_file) { |
| 467 | *crl_file = opts->crl_file; |
| 468 | } |
| 469 | if (crl_dir) { |
| 470 | *crl_dir = opts->crl_dir; |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | API void |
| 475 | nc_client_tls_get_crl_paths(const char **crl_file, const char **crl_dir) |
| 476 | { |
| 477 | _nc_client_tls_get_crl_paths(crl_file, crl_dir, &tls_opts); |
| 478 | } |
| 479 | |
| 480 | API void |
| 481 | nc_client_tls_ch_get_crl_paths(const char **crl_file, const char **crl_dir) |
| 482 | { |
| 483 | _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] | 484 | } |
| 485 | |
| 486 | API int |
| 487 | nc_client_tls_ch_add_bind_listen(const char *address, uint16_t port) |
| 488 | { |
Michal Vasko | 9d4cca5 | 2022-09-07 11:19:57 +0200 | [diff] [blame] | 489 | return nc_client_ch_add_bind_listen(address, port, NULL, NC_TI_OPENSSL); |
| 490 | } |
| 491 | |
| 492 | API int |
| 493 | nc_client_tls_ch_add_bind_hostname_listen(const char *address, uint16_t port, const char *hostname) |
| 494 | { |
| 495 | 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] | 496 | } |
| 497 | |
| 498 | API int |
| 499 | nc_client_tls_ch_del_bind(const char *address, uint16_t port) |
| 500 | { |
| 501 | return nc_client_ch_del_bind(address, port, NC_TI_OPENSSL); |
| 502 | } |
| 503 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 504 | static int |
Michal Vasko | 55ce24a | 2022-09-07 09:24:43 +0200 | [diff] [blame] | 505 | 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] | 506 | { |
Michal Vasko | 9af5232 | 2022-07-25 14:39:30 +0200 | [diff] [blame] | 507 | int rc = 0; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 508 | char *key; |
| 509 | X509_LOOKUP *lookup; |
Michal Vasko | 9af5232 | 2022-07-25 14:39:30 +0200 | [diff] [blame] | 510 | X509_VERIFY_PARAM *vpm = NULL; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 511 | |
| 512 | if (!opts->tls_ctx || opts->tls_ctx_change) { |
Michal Vasko | e22c673 | 2016-01-29 11:03:02 +0100 | [diff] [blame] | 513 | SSL_CTX_free(opts->tls_ctx); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 514 | |
Michal Vasko | 18aeb5d | 2017-02-17 09:23:56 +0100 | [diff] [blame] | 515 | #if OPENSSL_VERSION_NUMBER >= 0x10100000L // >= 1.1.0 |
| 516 | /* prepare global SSL context, highest available method is negotiated autmatically */ |
| 517 | if (!(opts->tls_ctx = SSL_CTX_new(TLS_client_method()))) |
| 518 | #else |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 519 | /* prepare global SSL context, allow only mandatory TLS 1.2 */ |
Michal Vasko | 18aeb5d | 2017-02-17 09:23:56 +0100 | [diff] [blame] | 520 | if (!(opts->tls_ctx = SSL_CTX_new(TLSv1_2_client_method()))) |
| 521 | #endif |
| 522 | { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 523 | 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] | 524 | rc = -1; |
| 525 | goto cleanup; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 526 | } |
| 527 | SSL_CTX_set_verify(opts->tls_ctx, SSL_VERIFY_PEER, tlsauth_verify_callback); |
| 528 | |
| 529 | /* get peer certificate */ |
| 530 | 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] | 531 | ERR(NULL, "Loading the client certificate from \'%s\' failed (%s).", opts->cert_path, |
| 532 | ERR_reason_error_string(ERR_get_error())); |
Michal Vasko | 9af5232 | 2022-07-25 14:39:30 +0200 | [diff] [blame] | 533 | rc = -1; |
| 534 | goto cleanup; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | /* if the file with private key not specified, expect that the private key is stored with the certificate */ |
| 538 | if (!opts->key_path) { |
| 539 | key = opts->cert_path; |
| 540 | } else { |
| 541 | key = opts->key_path; |
| 542 | } |
| 543 | 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] | 544 | ERR(NULL, "Loading the client private key from \'%s\' failed (%s).", key, |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 545 | ERR_reason_error_string(ERR_get_error())); |
Michal Vasko | 9af5232 | 2022-07-25 14:39:30 +0200 | [diff] [blame] | 546 | rc = -1; |
| 547 | goto cleanup; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | 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] | 551 | ERR(NULL, "Failed to load the locations of trusted CA certificates (%s).", |
| 552 | ERR_reason_error_string(ERR_get_error())); |
Michal Vasko | 9af5232 | 2022-07-25 14:39:30 +0200 | [diff] [blame] | 553 | rc = -1; |
| 554 | goto cleanup; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 555 | } |
Michal Vasko | 9af5232 | 2022-07-25 14:39:30 +0200 | [diff] [blame] | 556 | |
| 557 | #if OPENSSL_VERSION_NUMBER >= 0x10100000L // >= 1.1.0 |
Michal Vasko | 55ce24a | 2022-09-07 09:24:43 +0200 | [diff] [blame] | 558 | if (peername) { |
| 559 | /* server identity (hostname) verification */ |
| 560 | vpm = X509_VERIFY_PARAM_new(); |
| 561 | if (!X509_VERIFY_PARAM_set1_host(vpm, peername, 0)) { |
| 562 | ERR(NULL, "Failed to set expected server hostname (%s).", ERR_reason_error_string(ERR_get_error())); |
| 563 | rc = -1; |
| 564 | goto cleanup; |
| 565 | } |
| 566 | if (!SSL_CTX_set1_param(opts->tls_ctx, vpm)) { |
| 567 | ERR(NULL, "Failed to set verify params (%s).", ERR_reason_error_string(ERR_get_error())); |
| 568 | rc = -1; |
| 569 | goto cleanup; |
| 570 | } |
Michal Vasko | 9af5232 | 2022-07-25 14:39:30 +0200 | [diff] [blame] | 571 | } |
| 572 | #endif |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 573 | } |
| 574 | |
| 575 | if (opts->crl_store_change || (!opts->crl_store && (opts->crl_file || opts->crl_dir))) { |
| 576 | /* set the revocation store with the correct paths for the callback */ |
| 577 | X509_STORE_free(opts->crl_store); |
| 578 | |
| 579 | opts->crl_store = X509_STORE_new(); |
| 580 | if (!opts->crl_store) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 581 | 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] | 582 | rc = -1; |
| 583 | goto cleanup; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 584 | } |
Michal Vasko | 18aeb5d | 2017-02-17 09:23:56 +0100 | [diff] [blame] | 585 | |
| 586 | #if OPENSSL_VERSION_NUMBER < 0x10100000L // < 1.1.0 |
| 587 | /* whaveter this does... */ |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 588 | opts->crl_store->cache = 0; |
Michal Vasko | 18aeb5d | 2017-02-17 09:23:56 +0100 | [diff] [blame] | 589 | #endif |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 590 | |
| 591 | if (opts->crl_file) { |
| 592 | if (!(lookup = X509_STORE_add_lookup(opts->crl_store, X509_LOOKUP_file()))) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 593 | ERR(NULL, "Failed to add lookup method to CRL checking."); |
Michal Vasko | 9af5232 | 2022-07-25 14:39:30 +0200 | [diff] [blame] | 594 | rc = -1; |
| 595 | goto cleanup; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 596 | } |
| 597 | 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] | 598 | 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] | 599 | rc = -1; |
| 600 | goto cleanup; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 601 | } |
| 602 | } |
| 603 | |
| 604 | if (opts->crl_dir) { |
| 605 | 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] | 606 | ERR(NULL, "Failed to add lookup method to CRL checking."); |
Michal Vasko | 9af5232 | 2022-07-25 14:39:30 +0200 | [diff] [blame] | 607 | rc = -1; |
| 608 | goto cleanup; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 609 | } |
| 610 | 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] | 611 | 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] | 612 | rc = -1; |
| 613 | goto cleanup; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 614 | } |
| 615 | } |
| 616 | } |
| 617 | |
Michal Vasko | 9af5232 | 2022-07-25 14:39:30 +0200 | [diff] [blame] | 618 | cleanup: |
| 619 | X509_VERIFY_PARAM_free(vpm); |
| 620 | return rc; |
| 621 | } |
| 622 | |
| 623 | static int |
Michal Vasko | a82745b | 2022-09-07 11:48:12 +0200 | [diff] [blame] | 624 | 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] | 625 | { |
| 626 | int verify; |
| 627 | |
| 628 | /* check certificate verification result */ |
| 629 | verify = SSL_get_verify_result(tls); |
| 630 | switch (verify) { |
| 631 | case X509_V_OK: |
| 632 | if (connect_ret == 1) { |
Michal Vasko | 7ffcd14 | 2022-09-07 09:24:22 +0200 | [diff] [blame] | 633 | VRB(NULL, "Server certificate verified (domain \"%s\").", peername); |
Michal Vasko | 9af5232 | 2022-07-25 14:39:30 +0200 | [diff] [blame] | 634 | } |
| 635 | break; |
| 636 | default: |
| 637 | ERR(NULL, "Server certificate error (%s).", X509_verify_cert_error_string(verify)); |
| 638 | } |
| 639 | |
| 640 | /* check TLS connection result */ |
| 641 | if (connect_ret != 1) { |
| 642 | switch (SSL_get_error(tls, connect_ret)) { |
| 643 | case SSL_ERROR_SYSCALL: |
Michal Vasko | 7ffcd14 | 2022-09-07 09:24:22 +0200 | [diff] [blame] | 644 | 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] | 645 | break; |
| 646 | case SSL_ERROR_SSL: |
Michal Vasko | 7ffcd14 | 2022-09-07 09:24:22 +0200 | [diff] [blame] | 647 | 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] | 648 | break; |
| 649 | default: |
Michal Vasko | 7ffcd14 | 2022-09-07 09:24:22 +0200 | [diff] [blame] | 650 | ERR(NULL, "SSL connect to \"%s\" failed.", peername); |
Michal Vasko | 9af5232 | 2022-07-25 14:39:30 +0200 | [diff] [blame] | 651 | break; |
| 652 | } |
| 653 | } |
| 654 | |
| 655 | return connect_ret; |
Radek Krejci | 9f03b48 | 2015-10-22 16:02:10 +0200 | [diff] [blame] | 656 | } |
| 657 | |
| 658 | API struct nc_session * |
Michal Vasko | 9bee18d | 2015-12-08 14:41:42 +0100 | [diff] [blame] | 659 | 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] | 660 | { |
Radek Krejci | 9f03b48 | 2015-10-22 16:02:10 +0200 | [diff] [blame] | 661 | struct nc_session *session = NULL; |
Michal Vasko | 9af5232 | 2022-07-25 14:39:30 +0200 | [diff] [blame] | 662 | int sock, ret; |
roman | 6ece9c5 | 2022-06-22 09:29:17 +0200 | [diff] [blame] | 663 | struct timespec ts_timeout; |
Michal Vasko | 66032bc | 2019-01-22 15:03:12 +0100 | [diff] [blame] | 664 | char *ip_host = NULL; |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 665 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 666 | 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] | 667 | ERRINIT; |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 668 | return NULL; |
| 669 | } |
Radek Krejci | 9f03b48 | 2015-10-22 16:02:10 +0200 | [diff] [blame] | 670 | |
| 671 | /* process parameters */ |
| 672 | if (!host || strisempty(host)) { |
| 673 | host = "localhost"; |
| 674 | } |
| 675 | |
| 676 | if (!port) { |
| 677 | port = NC_PORT_TLS; |
| 678 | } |
| 679 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 680 | /* create/update TLS structures */ |
Michal Vasko | 9af5232 | 2022-07-25 14:39:30 +0200 | [diff] [blame] | 681 | if (nc_client_tls_update_opts(&tls_opts, host)) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 682 | return NULL; |
| 683 | } |
| 684 | |
Radek Krejci | 9f03b48 | 2015-10-22 16:02:10 +0200 | [diff] [blame] | 685 | /* prepare session structure */ |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 686 | session = nc_new_session(NC_CLIENT, 0); |
Radek Krejci | 9f03b48 | 2015-10-22 16:02:10 +0200 | [diff] [blame] | 687 | if (!session) { |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 688 | ERRMEM; |
Radek Krejci | 9f03b48 | 2015-10-22 16:02:10 +0200 | [diff] [blame] | 689 | return NULL; |
| 690 | } |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 691 | session->status = NC_STATUS_STARTING; |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 692 | |
| 693 | /* fill the session */ |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 694 | session->ti_type = NC_TI_OPENSSL; |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 695 | if (!(session->ti.tls = SSL_new(tls_opts.tls_ctx))) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 696 | 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] | 697 | goto fail; |
| 698 | } |
Radek Krejci | 9f03b48 | 2015-10-22 16:02:10 +0200 | [diff] [blame] | 699 | |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 700 | /* create and assign socket */ |
Michal Vasko | e49a15f | 2019-05-27 14:18:36 +0200 | [diff] [blame] | 701 | 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] | 702 | if (sock == -1) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 703 | 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] | 704 | goto fail; |
| 705 | } |
| 706 | SSL_set_fd(session->ti.tls, sock); |
| 707 | |
| 708 | /* set the SSL_MODE_AUTO_RETRY flag to allow OpenSSL perform re-handshake automatically */ |
| 709 | SSL_set_mode(session->ti.tls, SSL_MODE_AUTO_RETRY); |
| 710 | |
| 711 | /* connect and perform the handshake */ |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 712 | nc_timeouttime_get(&ts_timeout, NC_TRANSPORT_TIMEOUT); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 713 | tlsauth_ch = 0; |
Michal Vasko | d51a264 | 2021-09-03 13:03:24 +0200 | [diff] [blame] | 714 | 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] | 715 | usleep(NC_TIMEOUT_STEP); |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 716 | if (nc_timeouttime_cur_diff(&ts_timeout) < 1) { |
Michal Vasko | 7ffcd14 | 2022-09-07 09:24:22 +0200 | [diff] [blame] | 717 | ERR(NULL, "SSL connect timeout."); |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 718 | goto fail; |
| 719 | } |
| 720 | } |
Michal Vasko | 3cd55a7 | 2022-09-06 15:52:39 +0200 | [diff] [blame] | 721 | |
| 722 | /* check for errors */ |
Michal Vasko | a82745b | 2022-09-07 11:48:12 +0200 | [diff] [blame] | 723 | if (nc_client_tls_connect_check(ret, session->ti.tls, host) != 1) { |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 724 | goto fail; |
| 725 | } |
| 726 | |
Michal Vasko | 7893907 | 2022-12-12 07:43:18 +0100 | [diff] [blame] | 727 | if (nc_client_session_new_ctx(session, ctx) != EXIT_SUCCESS) { |
Radek Krejci | fd5b668 | 2017-06-13 15:52:53 +0200 | [diff] [blame] | 728 | goto fail; |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 729 | } |
Radek Krejci | fd5b668 | 2017-06-13 15:52:53 +0200 | [diff] [blame] | 730 | ctx = session->ctx; |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 731 | |
Radek Krejci | 9f03b48 | 2015-10-22 16:02:10 +0200 | [diff] [blame] | 732 | /* NETCONF handshake */ |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 733 | if (nc_handshake_io(session) != NC_MSG_HELLO) { |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 734 | goto fail; |
Radek Krejci | 9f03b48 | 2015-10-22 16:02:10 +0200 | [diff] [blame] | 735 | } |
Michal Vasko | ad61170 | 2015-12-03 13:41:51 +0100 | [diff] [blame] | 736 | session->status = NC_STATUS_RUNNING; |
Radek Krejci | 9f03b48 | 2015-10-22 16:02:10 +0200 | [diff] [blame] | 737 | |
Michal Vasko | ef57833 | 2016-01-25 13:20:09 +0100 | [diff] [blame] | 738 | if (nc_ctx_check_and_fill(session) == -1) { |
Michal Vasko | 57eb940 | 2015-12-08 14:38:12 +0100 | [diff] [blame] | 739 | goto fail; |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 740 | } |
| 741 | |
| 742 | /* store information into session and the dictionary */ |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 743 | session->host = ip_host; |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 744 | session->port = port; |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 745 | session->username = strdup("certificate-based"); |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 746 | |
Radek Krejci | 9f03b48 | 2015-10-22 16:02:10 +0200 | [diff] [blame] | 747 | return session; |
| 748 | |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 749 | fail: |
Michal Vasko | 66032bc | 2019-01-22 15:03:12 +0100 | [diff] [blame] | 750 | free(ip_host); |
Michal Vasko | e1a64ec | 2016-03-01 12:21:58 +0100 | [diff] [blame] | 751 | nc_session_free(session, NULL); |
Radek Krejci | 9f03b48 | 2015-10-22 16:02:10 +0200 | [diff] [blame] | 752 | return NULL; |
| 753 | } |
| 754 | |
| 755 | API struct nc_session * |
| 756 | nc_connect_libssl(SSL *tls, struct ly_ctx *ctx) |
| 757 | { |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 758 | struct nc_session *session; |
Radek Krejci | 9f03b48 | 2015-10-22 16:02:10 +0200 | [diff] [blame] | 759 | |
Michal Vasko | 45e53ae | 2016-04-07 11:46:03 +0200 | [diff] [blame] | 760 | if (!tls) { |
| 761 | ERRARG("tls"); |
| 762 | return NULL; |
| 763 | } else if (!SSL_is_init_finished(tls)) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 764 | ERR(NULL, "Supplied TLS session is not fully connected!"); |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 765 | return NULL; |
| 766 | } |
| 767 | |
| 768 | /* prepare session structure */ |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 769 | session = nc_new_session(NC_CLIENT, 0); |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 770 | if (!session) { |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 771 | ERRMEM; |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 772 | return NULL; |
| 773 | } |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 774 | session->status = NC_STATUS_STARTING; |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 775 | session->ti_type = NC_TI_OPENSSL; |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 776 | session->ti.tls = tls; |
| 777 | |
Michal Vasko | 7893907 | 2022-12-12 07:43:18 +0100 | [diff] [blame] | 778 | if (nc_client_session_new_ctx(session, ctx) != EXIT_SUCCESS) { |
Radek Krejci | fd5b668 | 2017-06-13 15:52:53 +0200 | [diff] [blame] | 779 | goto fail; |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 780 | } |
Radek Krejci | fd5b668 | 2017-06-13 15:52:53 +0200 | [diff] [blame] | 781 | ctx = session->ctx; |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 782 | |
| 783 | /* NETCONF handshake */ |
Michal Vasko | 131120a | 2018-05-29 15:44:02 +0200 | [diff] [blame] | 784 | if (nc_handshake_io(session) != NC_MSG_HELLO) { |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 785 | goto fail; |
| 786 | } |
Michal Vasko | ad61170 | 2015-12-03 13:41:51 +0100 | [diff] [blame] | 787 | session->status = NC_STATUS_RUNNING; |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 788 | |
Michal Vasko | ef57833 | 2016-01-25 13:20:09 +0100 | [diff] [blame] | 789 | if (nc_ctx_check_and_fill(session) == -1) { |
Michal Vasko | 57eb940 | 2015-12-08 14:38:12 +0100 | [diff] [blame] | 790 | goto fail; |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 791 | } |
| 792 | |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 793 | return session; |
| 794 | |
| 795 | fail: |
Michal Vasko | b604ed5 | 2022-01-24 09:56:14 +0100 | [diff] [blame] | 796 | session->ti_type = NC_TI_NONE; |
Michal Vasko | a42a7f0 | 2021-07-02 08:43:12 +0200 | [diff] [blame] | 797 | session->ti.tls = NULL; |
Michal Vasko | e1a64ec | 2016-03-01 12:21:58 +0100 | [diff] [blame] | 798 | nc_session_free(session, NULL); |
Radek Krejci | 9f03b48 | 2015-10-22 16:02:10 +0200 | [diff] [blame] | 799 | return NULL; |
| 800 | } |
| 801 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 802 | struct nc_session * |
Michal Vasko | 9d4cca5 | 2022-09-07 11:19:57 +0200 | [diff] [blame] | 803 | 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] | 804 | { |
Michal Vasko | 3cd55a7 | 2022-09-06 15:52:39 +0200 | [diff] [blame] | 805 | int ret; |
Michal Vasko | c64c38c | 2021-07-02 08:43:53 +0200 | [diff] [blame] | 806 | SSL *tls = NULL; |
| 807 | struct nc_session *session = NULL; |
roman | 6ece9c5 | 2022-06-22 09:29:17 +0200 | [diff] [blame] | 808 | struct timespec ts_timeout; |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 809 | |
Michal Vasko | 9d4cca5 | 2022-09-07 11:19:57 +0200 | [diff] [blame] | 810 | /* create/update TLS structures with explicit expected peername, if any set, the host is just the IP */ |
| 811 | if (nc_client_tls_update_opts(&tls_ch_opts, peername)) { |
Michal Vasko | c64c38c | 2021-07-02 08:43:53 +0200 | [diff] [blame] | 812 | goto cleanup; |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 813 | } |
| 814 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 815 | if (!(tls = SSL_new(tls_ch_opts.tls_ctx))) { |
Michal Vasko | 0553277 | 2021-06-03 12:12:38 +0200 | [diff] [blame] | 816 | 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] | 817 | goto cleanup; |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 818 | } |
| 819 | |
| 820 | SSL_set_fd(tls, sock); |
| 821 | |
| 822 | /* set the SSL_MODE_AUTO_RETRY flag to allow OpenSSL perform re-handshake automatically */ |
| 823 | SSL_set_mode(tls, SSL_MODE_AUTO_RETRY); |
| 824 | |
| 825 | /* connect and perform the handshake */ |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 826 | if (timeout > -1) { |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 827 | nc_timeouttime_get(&ts_timeout, timeout); |
Michal Vasko | 36c7be8 | 2017-02-22 13:37:59 +0100 | [diff] [blame] | 828 | } |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 829 | tlsauth_ch = 1; |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 830 | while (((ret = SSL_connect(tls)) == -1) && (SSL_get_error(tls, ret) == SSL_ERROR_WANT_READ)) { |
| 831 | usleep(NC_TIMEOUT_STEP); |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 832 | if ((timeout > -1) && (nc_timeouttime_cur_diff(&ts_timeout) < 1)) { |
Michal Vasko | 7ffcd14 | 2022-09-07 09:24:22 +0200 | [diff] [blame] | 833 | ERR(NULL, "SSL connect timeout."); |
roman | 6ece9c5 | 2022-06-22 09:29:17 +0200 | [diff] [blame] | 834 | goto cleanup; |
Michal Vasko | 0190bc3 | 2016-03-02 15:47:49 +0100 | [diff] [blame] | 835 | } |
| 836 | } |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 837 | |
Michal Vasko | 3cd55a7 | 2022-09-06 15:52:39 +0200 | [diff] [blame] | 838 | /* check for errors */ |
Michal Vasko | a82745b | 2022-09-07 11:48:12 +0200 | [diff] [blame] | 839 | if (nc_client_tls_connect_check(ret, tls, peername ? peername : host) != 1) { |
Michal Vasko | 3cd55a7 | 2022-09-06 15:52:39 +0200 | [diff] [blame] | 840 | goto cleanup; |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 841 | } |
| 842 | |
Michal Vasko | c64c38c | 2021-07-02 08:43:53 +0200 | [diff] [blame] | 843 | /* connect */ |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 844 | session = nc_connect_libssl(tls, ctx); |
Michal Vasko | c64c38c | 2021-07-02 08:43:53 +0200 | [diff] [blame] | 845 | if (!session) { |
| 846 | goto cleanup; |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 847 | } |
| 848 | |
Michal Vasko | c64c38c | 2021-07-02 08:43:53 +0200 | [diff] [blame] | 849 | session->flags |= NC_SESSION_CALLHOME; |
| 850 | |
| 851 | /* store information into session and the dictionary */ |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 852 | session->host = strdup(host); |
Michal Vasko | c64c38c | 2021-07-02 08:43:53 +0200 | [diff] [blame] | 853 | session->port = port; |
Michal Vasko | 9322407 | 2021-11-09 12:14:28 +0100 | [diff] [blame] | 854 | session->username = strdup("certificate-based"); |
Michal Vasko | c64c38c | 2021-07-02 08:43:53 +0200 | [diff] [blame] | 855 | |
| 856 | cleanup: |
| 857 | if (!session) { |
| 858 | SSL_free(tls); |
| 859 | close(sock); |
| 860 | } |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 861 | return session; |
| 862 | } |