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