Radek Krejci | 9f03b48 | 2015-10-22 16:02:10 +0200 | [diff] [blame] | 1 | /** |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 2 | * \file session_client_tls.c |
Radek Krejci | 9f03b48 | 2015-10-22 16:02:10 +0200 | [diff] [blame] | 3 | * \author Radek Krejci <rkrejci@cesnet.cz> |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 4 | * \author Michal Vasko <mvasko@cesnet.cz> |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 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 | * |
| 9 | * Copyright (c) 2015 CESNET, z.s.p.o. |
| 10 | * |
Radek Krejci | 9b81f5b | 2016-02-24 13:14:49 +0100 | [diff] [blame] | 11 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 12 | * You may not use this file except in compliance with the License. |
| 13 | * You may obtain a copy of the License at |
| 14 | * |
| 15 | * https://opensource.org/licenses/BSD-3-Clause |
Radek Krejci | 9f03b48 | 2015-10-22 16:02:10 +0200 | [diff] [blame] | 16 | */ |
| 17 | |
| 18 | #include <assert.h> |
| 19 | #include <errno.h> |
Radek Krejci | 9f03b48 | 2015-10-22 16:02:10 +0200 | [diff] [blame] | 20 | #include <string.h> |
| 21 | #include <unistd.h> |
| 22 | |
| 23 | #include <libyang/libyang.h> |
Michal Vasko | 086311b | 2016-01-08 09:53:11 +0100 | [diff] [blame] | 24 | #include <openssl/err.h> |
Michal Vasko | dae4832 | 2016-02-25 14:49:48 +0100 | [diff] [blame^] | 25 | #include <openssl/x509.h> |
Radek Krejci | 9f03b48 | 2015-10-22 16:02:10 +0200 | [diff] [blame] | 26 | |
Michal Vasko | e22c673 | 2016-01-29 11:03:02 +0100 | [diff] [blame] | 27 | #include "session_client.h" |
| 28 | #include "session_client_ch.h" |
Radek Krejci | 9f03b48 | 2015-10-22 16:02:10 +0200 | [diff] [blame] | 29 | #include "libnetconf.h" |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 30 | |
Michal Vasko | daf9a09 | 2016-02-09 10:42:05 +0100 | [diff] [blame] | 31 | extern struct nc_client_opts client_opts; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 32 | static struct nc_client_tls_opts tls_opts; |
| 33 | static struct nc_client_tls_opts tls_ch_opts; |
| 34 | |
| 35 | static int tlsauth_ch; |
Radek Krejci | 9f03b48 | 2015-10-22 16:02:10 +0200 | [diff] [blame] | 36 | |
Radek Krejci | 9f03b48 | 2015-10-22 16:02:10 +0200 | [diff] [blame] | 37 | static int |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 38 | tlsauth_verify_callback(int preverify_ok, X509_STORE_CTX *x509_ctx) |
Radek Krejci | 9f03b48 | 2015-10-22 16:02:10 +0200 | [diff] [blame] | 39 | { |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 40 | X509_STORE_CTX store_ctx; |
| 41 | X509_OBJECT obj; |
| 42 | X509_NAME *subject, *issuer; |
| 43 | X509 *cert; |
| 44 | X509_CRL *crl; |
| 45 | X509_REVOKED *revoked; |
| 46 | EVP_PKEY *pubkey; |
| 47 | int i, n, rc; |
| 48 | ASN1_TIME *next_update = NULL; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 49 | struct nc_client_tls_opts *opts; |
Radek Krejci | 9f03b48 | 2015-10-22 16:02:10 +0200 | [diff] [blame] | 50 | |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 51 | if (!preverify_ok) { |
| 52 | return 0; |
| 53 | } |
Radek Krejci | 9f03b48 | 2015-10-22 16:02:10 +0200 | [diff] [blame] | 54 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 55 | opts = (tlsauth_ch ? &tls_ch_opts : &tls_opts); |
| 56 | |
| 57 | if (!opts->crl_store) { |
| 58 | /* nothing to check */ |
| 59 | return 1; |
| 60 | } |
| 61 | |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 62 | cert = X509_STORE_CTX_get_current_cert(x509_ctx); |
| 63 | subject = X509_get_subject_name(cert); |
| 64 | issuer = X509_get_issuer_name(cert); |
| 65 | |
| 66 | /* try to retrieve a CRL corresponding to the _subject_ of |
| 67 | * the current certificate in order to verify it's integrity */ |
| 68 | memset((char *)&obj, 0, sizeof obj); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 69 | X509_STORE_CTX_init(&store_ctx, opts->crl_store, NULL, NULL); |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 70 | rc = X509_STORE_get_by_subject(&store_ctx, X509_LU_CRL, subject, &obj); |
| 71 | X509_STORE_CTX_cleanup(&store_ctx); |
| 72 | crl = obj.data.crl; |
| 73 | if (rc > 0 && crl) { |
| 74 | next_update = X509_CRL_get_nextUpdate(crl); |
| 75 | |
| 76 | /* verify the signature on this CRL */ |
| 77 | pubkey = X509_get_pubkey(cert); |
| 78 | if (X509_CRL_verify(crl, pubkey) <= 0) { |
| 79 | X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_CRL_SIGNATURE_FAILURE); |
| 80 | X509_OBJECT_free_contents(&obj); |
| 81 | if (pubkey) { |
| 82 | EVP_PKEY_free(pubkey); |
| 83 | } |
| 84 | return 0; /* fail */ |
| 85 | } |
| 86 | if (pubkey) { |
| 87 | EVP_PKEY_free(pubkey); |
| 88 | } |
| 89 | |
| 90 | /* check date of CRL to make sure it's not expired */ |
| 91 | if (!next_update) { |
| 92 | X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD); |
| 93 | X509_OBJECT_free_contents(&obj); |
| 94 | return 0; /* fail */ |
| 95 | } |
| 96 | if (X509_cmp_current_time(next_update) < 0) { |
| 97 | X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_CRL_HAS_EXPIRED); |
| 98 | X509_OBJECT_free_contents(&obj); |
| 99 | return 0; /* fail */ |
| 100 | } |
| 101 | X509_OBJECT_free_contents(&obj); |
| 102 | } |
| 103 | |
| 104 | /* try to retrieve a CRL corresponding to the _issuer_ of |
| 105 | * the current certificate in order to check for revocation */ |
| 106 | memset((char *)&obj, 0, sizeof obj); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 107 | X509_STORE_CTX_init(&store_ctx, opts->crl_store, NULL, NULL); |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 108 | rc = X509_STORE_get_by_subject(&store_ctx, X509_LU_CRL, issuer, &obj); |
| 109 | X509_STORE_CTX_cleanup(&store_ctx); |
| 110 | crl = obj.data.crl; |
| 111 | if (rc > 0 && crl) { |
| 112 | /* check if the current certificate is revoked by this CRL */ |
| 113 | n = sk_X509_REVOKED_num(X509_CRL_get_REVOKED(crl)); |
| 114 | for (i = 0; i < n; i++) { |
| 115 | revoked = sk_X509_REVOKED_value(X509_CRL_get_REVOKED(crl), i); |
| 116 | if (ASN1_INTEGER_cmp(revoked->serialNumber, X509_get_serialNumber(cert)) == 0) { |
| 117 | ERR("Certificate revoked!"); |
| 118 | X509_STORE_CTX_set_error(x509_ctx, X509_V_ERR_CERT_REVOKED); |
| 119 | X509_OBJECT_free_contents(&obj); |
| 120 | return 0; /* fail */ |
| 121 | } |
| 122 | } |
| 123 | X509_OBJECT_free_contents(&obj); |
| 124 | } |
| 125 | |
| 126 | return 1; /* success */ |
| 127 | } |
| 128 | |
Michal Vasko | e22c673 | 2016-01-29 11:03:02 +0100 | [diff] [blame] | 129 | static void |
| 130 | _nc_client_tls_destroy_opts(struct nc_client_tls_opts *opts) |
| 131 | { |
| 132 | free(opts->cert_path); |
| 133 | free(opts->key_path); |
| 134 | free(opts->ca_file); |
| 135 | free(opts->ca_dir); |
| 136 | SSL_CTX_free(opts->tls_ctx); |
| 137 | |
| 138 | free(opts->crl_file); |
| 139 | free(opts->crl_dir); |
| 140 | X509_STORE_free(opts->crl_store); |
| 141 | } |
| 142 | |
| 143 | API void |
| 144 | nc_client_tls_destroy_opts(void) |
| 145 | { |
| 146 | _nc_client_tls_destroy_opts(&tls_opts); |
| 147 | _nc_client_tls_destroy_opts(&tls_ch_opts); |
| 148 | } |
| 149 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 150 | static int |
Michal Vasko | e22c673 | 2016-01-29 11:03:02 +0100 | [diff] [blame] | 151 | _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] | 152 | { |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 153 | if (!client_cert) { |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 154 | ERRARG; |
| 155 | return -1; |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 156 | } |
| 157 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 158 | free(opts->cert_path); |
| 159 | free(opts->key_path); |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 160 | |
Michal Vasko | 9db2a6f | 2016-02-01 13:26:03 +0100 | [diff] [blame] | 161 | opts->cert_path = strdup(client_cert); |
| 162 | if (!opts->cert_path) { |
| 163 | ERRMEM; |
| 164 | return -1; |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 165 | } |
| 166 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 167 | if (client_key) { |
| 168 | opts->key_path = strdup(client_key); |
Michal Vasko | 9db2a6f | 2016-02-01 13:26:03 +0100 | [diff] [blame] | 169 | if (!opts->key_path) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 170 | ERRMEM; |
| 171 | return -1; |
| 172 | } |
| 173 | } else { |
| 174 | opts->key_path = NULL; |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 175 | } |
| 176 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 177 | opts->tls_ctx_change = 1; |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 178 | |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 179 | return 0; |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 180 | } |
| 181 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 182 | API int |
Michal Vasko | e22c673 | 2016-01-29 11:03:02 +0100 | [diff] [blame] | 183 | 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] | 184 | { |
Michal Vasko | e22c673 | 2016-01-29 11:03:02 +0100 | [diff] [blame] | 185 | 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] | 186 | } |
| 187 | |
| 188 | API int |
Michal Vasko | e22c673 | 2016-01-29 11:03:02 +0100 | [diff] [blame] | 189 | 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] | 190 | { |
Michal Vasko | e22c673 | 2016-01-29 11:03:02 +0100 | [diff] [blame] | 191 | return _nc_client_tls_set_cert_key_paths(client_cert, client_key, &tls_ch_opts); |
| 192 | } |
| 193 | |
| 194 | static void |
| 195 | _nc_client_tls_get_cert_key_paths(const char **client_cert, const char **client_key, struct nc_client_tls_opts *opts) |
| 196 | { |
| 197 | if (!client_cert && !client_key) { |
| 198 | ERRARG; |
| 199 | return; |
| 200 | } |
| 201 | |
| 202 | if (client_cert) { |
| 203 | *client_cert = opts->cert_path; |
| 204 | } |
| 205 | if (client_key) { |
| 206 | *client_key = opts->key_path; |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | API void |
| 211 | nc_client_tls_get_cert_key_paths(const char **client_cert, const char **client_key) |
| 212 | { |
| 213 | _nc_client_tls_get_cert_key_paths(client_cert, client_key, &tls_opts); |
| 214 | } |
| 215 | |
| 216 | API void |
| 217 | nc_client_tls_ch_get_cert_key_paths(const char **client_cert, const char **client_key) |
| 218 | { |
| 219 | _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] | 220 | } |
| 221 | |
| 222 | static int |
Michal Vasko | e22c673 | 2016-01-29 11:03:02 +0100 | [diff] [blame] | 223 | _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] | 224 | { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 225 | if (!ca_file && !ca_dir) { |
| 226 | ERRARG; |
| 227 | return -1; |
| 228 | } |
| 229 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 230 | free(opts->ca_file); |
| 231 | free(opts->ca_dir); |
| 232 | |
| 233 | if (ca_file) { |
| 234 | opts->ca_file = strdup(ca_file); |
| 235 | if (!opts->ca_file) { |
| 236 | ERRMEM; |
| 237 | return -1; |
| 238 | } |
| 239 | } else { |
| 240 | opts->ca_file = NULL; |
| 241 | } |
| 242 | |
| 243 | if (ca_dir) { |
| 244 | opts->ca_dir = strdup(ca_dir); |
| 245 | if (!opts->ca_dir) { |
| 246 | ERRMEM; |
| 247 | return -1; |
| 248 | } |
| 249 | } else { |
| 250 | opts->ca_dir = NULL; |
| 251 | } |
| 252 | |
| 253 | opts->tls_ctx_change = 1; |
| 254 | |
| 255 | return 0; |
| 256 | } |
| 257 | |
| 258 | API int |
Michal Vasko | e22c673 | 2016-01-29 11:03:02 +0100 | [diff] [blame] | 259 | 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] | 260 | { |
Michal Vasko | e22c673 | 2016-01-29 11:03:02 +0100 | [diff] [blame] | 261 | 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] | 262 | } |
| 263 | |
| 264 | API int |
Michal Vasko | e22c673 | 2016-01-29 11:03:02 +0100 | [diff] [blame] | 265 | 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] | 266 | { |
Michal Vasko | e22c673 | 2016-01-29 11:03:02 +0100 | [diff] [blame] | 267 | return _nc_client_tls_set_trusted_ca_paths(ca_file, ca_dir, &tls_ch_opts); |
| 268 | } |
| 269 | |
| 270 | static void |
| 271 | _nc_client_tls_get_trusted_ca_paths(const char **ca_file, const char **ca_dir, struct nc_client_tls_opts *opts) |
| 272 | { |
| 273 | if (!ca_file && !ca_dir) { |
| 274 | ERRARG; |
| 275 | return; |
| 276 | } |
| 277 | |
| 278 | if (ca_file) { |
| 279 | *ca_file = opts->ca_file; |
| 280 | } |
| 281 | if (ca_dir) { |
| 282 | *ca_dir = opts->ca_dir; |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | API void |
| 287 | nc_client_tls_get_trusted_ca_paths(const char **ca_file, const char **ca_dir) |
| 288 | { |
| 289 | _nc_client_tls_get_trusted_ca_paths(ca_file, ca_dir, &tls_opts); |
| 290 | } |
| 291 | |
| 292 | API void |
| 293 | nc_client_tls_ch_get_trusted_ca_paths(const char **ca_file, const char **ca_dir) |
| 294 | { |
| 295 | _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] | 296 | } |
| 297 | |
| 298 | static int |
Michal Vasko | e22c673 | 2016-01-29 11:03:02 +0100 | [diff] [blame] | 299 | _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] | 300 | { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 301 | if (!crl_file && !crl_dir) { |
| 302 | ERRARG; |
| 303 | return -1; |
| 304 | } |
| 305 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 306 | free(opts->crl_file); |
| 307 | free(opts->crl_dir); |
| 308 | |
| 309 | if (crl_file) { |
| 310 | opts->crl_file = strdup(crl_file); |
| 311 | if (!opts->crl_file) { |
| 312 | ERRMEM; |
| 313 | return -1; |
| 314 | } |
| 315 | } else { |
| 316 | opts->crl_file = NULL; |
| 317 | } |
| 318 | |
| 319 | if (crl_dir) { |
| 320 | opts->crl_dir = strdup(crl_dir); |
| 321 | if (!opts->crl_dir) { |
| 322 | ERRMEM; |
| 323 | return -1; |
| 324 | } |
| 325 | } else { |
| 326 | opts->crl_dir = NULL; |
| 327 | } |
| 328 | |
| 329 | opts->crl_store_change = 1; |
| 330 | |
| 331 | return 0; |
| 332 | } |
| 333 | |
| 334 | API int |
Michal Vasko | e22c673 | 2016-01-29 11:03:02 +0100 | [diff] [blame] | 335 | 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] | 336 | { |
Michal Vasko | e22c673 | 2016-01-29 11:03:02 +0100 | [diff] [blame] | 337 | 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] | 338 | } |
| 339 | |
| 340 | API int |
Michal Vasko | e22c673 | 2016-01-29 11:03:02 +0100 | [diff] [blame] | 341 | 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] | 342 | { |
Michal Vasko | e22c673 | 2016-01-29 11:03:02 +0100 | [diff] [blame] | 343 | return _nc_client_tls_set_crl_paths(crl_file, crl_dir, &tls_ch_opts); |
| 344 | } |
| 345 | |
| 346 | static void |
| 347 | _nc_client_tls_get_crl_paths(const char **crl_file, const char **crl_dir, struct nc_client_tls_opts *opts) |
| 348 | { |
| 349 | if (!crl_file && !crl_dir) { |
| 350 | ERRARG; |
| 351 | return; |
| 352 | } |
| 353 | |
| 354 | if (crl_file) { |
| 355 | *crl_file = opts->crl_file; |
| 356 | } |
| 357 | if (crl_dir) { |
| 358 | *crl_dir = opts->crl_dir; |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | API void |
| 363 | nc_client_tls_get_crl_paths(const char **crl_file, const char **crl_dir) |
| 364 | { |
| 365 | _nc_client_tls_get_crl_paths(crl_file, crl_dir, &tls_opts); |
| 366 | } |
| 367 | |
| 368 | API void |
| 369 | nc_client_tls_ch_get_crl_paths(const char **crl_file, const char **crl_dir) |
| 370 | { |
| 371 | _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] | 372 | } |
| 373 | |
| 374 | API int |
| 375 | nc_client_tls_ch_add_bind_listen(const char *address, uint16_t port) |
| 376 | { |
| 377 | return nc_client_ch_add_bind_listen(address, port, NC_TI_OPENSSL); |
| 378 | } |
| 379 | |
| 380 | API int |
| 381 | nc_client_tls_ch_del_bind(const char *address, uint16_t port) |
| 382 | { |
| 383 | return nc_client_ch_del_bind(address, port, NC_TI_OPENSSL); |
| 384 | } |
| 385 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 386 | static int |
Michal Vasko | af58cd9 | 2016-01-28 11:56:02 +0100 | [diff] [blame] | 387 | nc_client_tls_update_opts(struct nc_client_tls_opts *opts) |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 388 | { |
| 389 | char *key; |
| 390 | X509_LOOKUP *lookup; |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 391 | |
| 392 | if (!opts->tls_ctx || opts->tls_ctx_change) { |
Michal Vasko | e22c673 | 2016-01-29 11:03:02 +0100 | [diff] [blame] | 393 | SSL_CTX_free(opts->tls_ctx); |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 394 | |
| 395 | /* prepare global SSL context, allow only mandatory TLS 1.2 */ |
| 396 | if (!(opts->tls_ctx = SSL_CTX_new(TLSv1_2_client_method()))) { |
| 397 | ERR("Unable to create OpenSSL context (%s).", ERR_reason_error_string(ERR_get_error())); |
| 398 | return -1; |
| 399 | } |
| 400 | SSL_CTX_set_verify(opts->tls_ctx, SSL_VERIFY_PEER, tlsauth_verify_callback); |
| 401 | |
| 402 | /* get peer certificate */ |
| 403 | if (SSL_CTX_use_certificate_file(opts->tls_ctx, opts->cert_path, SSL_FILETYPE_PEM) != 1) { |
| 404 | ERR("Loading the client certificate from \'%s\' failed (%s).", opts->cert_path, ERR_reason_error_string(ERR_get_error())); |
| 405 | return -1; |
| 406 | } |
| 407 | |
| 408 | /* if the file with private key not specified, expect that the private key is stored with the certificate */ |
| 409 | if (!opts->key_path) { |
| 410 | key = opts->cert_path; |
| 411 | } else { |
| 412 | key = opts->key_path; |
| 413 | } |
| 414 | if (SSL_CTX_use_PrivateKey_file(opts->tls_ctx, key, SSL_FILETYPE_PEM) != 1) { |
| 415 | ERR("Loading the client priavte key from \'%s\' failed (%s).", key, ERR_reason_error_string(ERR_get_error())); |
| 416 | return -1; |
| 417 | } |
| 418 | |
| 419 | if (!SSL_CTX_load_verify_locations(opts->tls_ctx, opts->ca_file, opts->ca_dir)) { |
| 420 | ERR("Failed to load the locations of trusted CA certificates (%s).", ERR_reason_error_string(ERR_get_error())); |
| 421 | return -1; |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | if (opts->crl_store_change || (!opts->crl_store && (opts->crl_file || opts->crl_dir))) { |
| 426 | /* set the revocation store with the correct paths for the callback */ |
| 427 | X509_STORE_free(opts->crl_store); |
| 428 | |
| 429 | opts->crl_store = X509_STORE_new(); |
| 430 | if (!opts->crl_store) { |
| 431 | ERR("Unable to create a certificate store (%s).", ERR_reason_error_string(ERR_get_error())); |
| 432 | return -1; |
| 433 | } |
| 434 | opts->crl_store->cache = 0; |
| 435 | |
| 436 | if (opts->crl_file) { |
| 437 | if (!(lookup = X509_STORE_add_lookup(opts->crl_store, X509_LOOKUP_file()))) { |
| 438 | ERR("Failed to add lookup method to CRL checking."); |
| 439 | return -1; |
| 440 | } |
| 441 | if (X509_LOOKUP_add_dir(lookup, opts->crl_file, X509_FILETYPE_PEM) != 1) { |
| 442 | ERR("Failed to add the revocation lookup file \"%s\".", opts->crl_file); |
| 443 | return -1; |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | if (opts->crl_dir) { |
| 448 | if (!(lookup = X509_STORE_add_lookup(opts->crl_store, X509_LOOKUP_hash_dir()))) { |
| 449 | ERR("Failed to add lookup method to CRL checking."); |
| 450 | return -1; |
| 451 | } |
| 452 | if (X509_LOOKUP_add_dir(lookup, opts->crl_dir, X509_FILETYPE_PEM) != 1) { |
| 453 | ERR("Failed to add the revocation lookup directory \"%s\".", opts->crl_dir); |
| 454 | return -1; |
| 455 | } |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | return 0; |
Radek Krejci | 9f03b48 | 2015-10-22 16:02:10 +0200 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | API struct nc_session * |
Michal Vasko | 9bee18d | 2015-12-08 14:41:42 +0100 | [diff] [blame] | 463 | 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] | 464 | { |
Radek Krejci | 9f03b48 | 2015-10-22 16:02:10 +0200 | [diff] [blame] | 465 | struct nc_session *session = NULL; |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 466 | int sock, verify; |
| 467 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 468 | if (!tls_opts.cert_path || (!tls_opts.ca_file && !tls_opts.ca_dir)) { |
Michal Vasko | 7f1c78b | 2016-01-19 09:52:14 +0100 | [diff] [blame] | 469 | ERRARG; |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 470 | return NULL; |
| 471 | } |
Radek Krejci | 9f03b48 | 2015-10-22 16:02:10 +0200 | [diff] [blame] | 472 | |
| 473 | /* process parameters */ |
| 474 | if (!host || strisempty(host)) { |
| 475 | host = "localhost"; |
| 476 | } |
| 477 | |
| 478 | if (!port) { |
| 479 | port = NC_PORT_TLS; |
| 480 | } |
| 481 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 482 | /* create/update TLS structures */ |
Michal Vasko | af58cd9 | 2016-01-28 11:56:02 +0100 | [diff] [blame] | 483 | if (nc_client_tls_update_opts(&tls_opts)) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 484 | return NULL; |
| 485 | } |
| 486 | |
Radek Krejci | 9f03b48 | 2015-10-22 16:02:10 +0200 | [diff] [blame] | 487 | /* prepare session structure */ |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 488 | session = calloc(1, sizeof *session); |
Radek Krejci | 9f03b48 | 2015-10-22 16:02:10 +0200 | [diff] [blame] | 489 | if (!session) { |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 490 | ERRMEM; |
Radek Krejci | 9f03b48 | 2015-10-22 16:02:10 +0200 | [diff] [blame] | 491 | return NULL; |
| 492 | } |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 493 | session->status = NC_STATUS_STARTING; |
| 494 | session->side = NC_CLIENT; |
Radek Krejci | 9f03b48 | 2015-10-22 16:02:10 +0200 | [diff] [blame] | 495 | |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 496 | /* transport lock */ |
| 497 | session->ti_lock = malloc(sizeof *session->ti_lock); |
| 498 | if (!session->ti_lock) { |
| 499 | ERRMEM; |
| 500 | goto fail; |
| 501 | } |
| 502 | pthread_mutex_init(session->ti_lock, NULL); |
| 503 | |
| 504 | /* fill the session */ |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 505 | session->ti_type = NC_TI_OPENSSL; |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 506 | if (!(session->ti.tls = SSL_new(tls_opts.tls_ctx))) { |
Michal Vasko | d083db6 | 2016-01-19 10:31:29 +0100 | [diff] [blame] | 507 | ERR("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] | 508 | goto fail; |
| 509 | } |
Radek Krejci | 9f03b48 | 2015-10-22 16:02:10 +0200 | [diff] [blame] | 510 | |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 511 | /* create and assign socket */ |
Michal Vasko | f05562c | 2016-01-20 12:06:43 +0100 | [diff] [blame] | 512 | sock = nc_sock_connect(host, port); |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 513 | if (sock == -1) { |
| 514 | goto fail; |
| 515 | } |
| 516 | SSL_set_fd(session->ti.tls, sock); |
| 517 | |
| 518 | /* set the SSL_MODE_AUTO_RETRY flag to allow OpenSSL perform re-handshake automatically */ |
| 519 | SSL_set_mode(session->ti.tls, SSL_MODE_AUTO_RETRY); |
| 520 | |
| 521 | /* connect and perform the handshake */ |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 522 | tlsauth_ch = 0; |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 523 | if (SSL_connect(session->ti.tls) != 1) { |
| 524 | ERR("Connecting over TLS failed (%s).", ERR_reason_error_string(ERR_get_error())); |
| 525 | goto fail; |
| 526 | } |
| 527 | |
| 528 | /* check certificate verification result */ |
| 529 | verify = SSL_get_verify_result(session->ti.tls); |
| 530 | switch (verify) { |
| 531 | case X509_V_OK: |
| 532 | VRB("Server certificate successfully verified."); |
| 533 | break; |
| 534 | default: |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 535 | WRN("Server certificate verification problem (%s).", X509_verify_cert_error_string(verify)); |
Radek Krejci | 9f03b48 | 2015-10-22 16:02:10 +0200 | [diff] [blame] | 536 | } |
| 537 | |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 538 | /* assign context (dicionary needed for handshake) */ |
| 539 | if (!ctx) { |
Michal Vasko | daf9a09 | 2016-02-09 10:42:05 +0100 | [diff] [blame] | 540 | if (client_opts.schema_searchpath) { |
| 541 | ctx = ly_ctx_new(client_opts.schema_searchpath); |
| 542 | } else { |
| 543 | ctx = ly_ctx_new(SCHEMAS_DIR); |
| 544 | } |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 545 | } else { |
| 546 | session->flags |= NC_SESSION_SHAREDCTX; |
| 547 | } |
| 548 | session->ctx = ctx; |
| 549 | |
Radek Krejci | 9f03b48 | 2015-10-22 16:02:10 +0200 | [diff] [blame] | 550 | /* NETCONF handshake */ |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 551 | if (nc_handshake(session)) { |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 552 | goto fail; |
Radek Krejci | 9f03b48 | 2015-10-22 16:02:10 +0200 | [diff] [blame] | 553 | } |
Michal Vasko | ad61170 | 2015-12-03 13:41:51 +0100 | [diff] [blame] | 554 | session->status = NC_STATUS_RUNNING; |
Radek Krejci | 9f03b48 | 2015-10-22 16:02:10 +0200 | [diff] [blame] | 555 | |
Michal Vasko | ef57833 | 2016-01-25 13:20:09 +0100 | [diff] [blame] | 556 | if (nc_ctx_check_and_fill(session) == -1) { |
Michal Vasko | 57eb940 | 2015-12-08 14:38:12 +0100 | [diff] [blame] | 557 | goto fail; |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | /* store information into session and the dictionary */ |
| 561 | session->host = lydict_insert(ctx, host, 0); |
| 562 | session->port = port; |
Michal Vasko | 9bee18d | 2015-12-08 14:41:42 +0100 | [diff] [blame] | 563 | session->username = lydict_insert(ctx, "certificate-based", 0); |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 564 | |
Radek Krejci | 9f03b48 | 2015-10-22 16:02:10 +0200 | [diff] [blame] | 565 | return session; |
| 566 | |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 567 | fail: |
Radek Krejci | 9f03b48 | 2015-10-22 16:02:10 +0200 | [diff] [blame] | 568 | nc_session_free(session); |
| 569 | return NULL; |
| 570 | } |
| 571 | |
| 572 | API struct nc_session * |
| 573 | nc_connect_libssl(SSL *tls, struct ly_ctx *ctx) |
| 574 | { |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 575 | struct nc_session *session; |
Radek Krejci | 9f03b48 | 2015-10-22 16:02:10 +0200 | [diff] [blame] | 576 | |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 577 | /* check TLS session status */ |
| 578 | if (!tls || !SSL_is_init_finished(tls)) { |
| 579 | ERR("Supplied TLS session is not fully connected!"); |
| 580 | return NULL; |
| 581 | } |
| 582 | |
| 583 | /* prepare session structure */ |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 584 | session = calloc(1, sizeof *session); |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 585 | if (!session) { |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 586 | ERRMEM; |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 587 | return NULL; |
| 588 | } |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 589 | session->status = NC_STATUS_STARTING; |
| 590 | session->side = NC_CLIENT; |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 591 | |
| 592 | /* transport lock */ |
| 593 | session->ti_lock = malloc(sizeof *session->ti_lock); |
| 594 | if (!session->ti_lock) { |
| 595 | ERRMEM; |
| 596 | goto fail; |
| 597 | } |
| 598 | pthread_mutex_init(session->ti_lock, NULL); |
| 599 | |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 600 | session->ti_type = NC_TI_OPENSSL; |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 601 | session->ti.tls = tls; |
| 602 | |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 603 | /* assign context (dicionary needed for handshake) */ |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 604 | if (!ctx) { |
Michal Vasko | daf9a09 | 2016-02-09 10:42:05 +0100 | [diff] [blame] | 605 | if (client_opts.schema_searchpath) { |
| 606 | ctx = ly_ctx_new(client_opts.schema_searchpath); |
| 607 | } else { |
| 608 | ctx = ly_ctx_new(SCHEMAS_DIR); |
| 609 | } |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 610 | } else { |
| 611 | session->flags |= NC_SESSION_SHAREDCTX; |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 612 | } |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 613 | session->ctx = ctx; |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 614 | |
| 615 | /* NETCONF handshake */ |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 616 | if (nc_handshake(session)) { |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 617 | goto fail; |
| 618 | } |
Michal Vasko | ad61170 | 2015-12-03 13:41:51 +0100 | [diff] [blame] | 619 | session->status = NC_STATUS_RUNNING; |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 620 | |
Michal Vasko | ef57833 | 2016-01-25 13:20:09 +0100 | [diff] [blame] | 621 | if (nc_ctx_check_and_fill(session) == -1) { |
Michal Vasko | 57eb940 | 2015-12-08 14:38:12 +0100 | [diff] [blame] | 622 | goto fail; |
Michal Vasko | 9e2d3a3 | 2015-11-10 13:09:18 +0100 | [diff] [blame] | 623 | } |
| 624 | |
Michal Vasko | 11d4cdb | 2015-10-29 11:42:52 +0100 | [diff] [blame] | 625 | return session; |
| 626 | |
| 627 | fail: |
| 628 | nc_session_free(session); |
Radek Krejci | 9f03b48 | 2015-10-22 16:02:10 +0200 | [diff] [blame] | 629 | return NULL; |
| 630 | } |
| 631 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 632 | struct nc_session * |
Michal Vasko | af58cd9 | 2016-01-28 11:56:02 +0100 | [diff] [blame] | 633 | nc_accept_callhome_tls_sock(int sock, const char *host, uint16_t port, struct ly_ctx *ctx) |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 634 | { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 635 | int verify; |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 636 | SSL *tls; |
| 637 | struct nc_session *session; |
| 638 | |
Michal Vasko | af58cd9 | 2016-01-28 11:56:02 +0100 | [diff] [blame] | 639 | if (nc_client_tls_update_opts(&tls_ch_opts)) { |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 640 | close(sock); |
Michal Vasko | c61c449 | 2016-01-25 11:13:34 +0100 | [diff] [blame] | 641 | return NULL; |
| 642 | } |
| 643 | |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 644 | if (!(tls = SSL_new(tls_ch_opts.tls_ctx))) { |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 645 | ERR("Failed to create new TLS session structure (%s).", ERR_reason_error_string(ERR_get_error())); |
| 646 | close(sock); |
| 647 | return NULL; |
| 648 | } |
| 649 | |
| 650 | SSL_set_fd(tls, sock); |
| 651 | |
| 652 | /* set the SSL_MODE_AUTO_RETRY flag to allow OpenSSL perform re-handshake automatically */ |
| 653 | SSL_set_mode(tls, SSL_MODE_AUTO_RETRY); |
| 654 | |
| 655 | /* connect and perform the handshake */ |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 656 | tlsauth_ch = 1; |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 657 | if (SSL_connect(tls) != 1) { |
| 658 | ERR("Connecting over TLS failed (%s).", ERR_reason_error_string(ERR_get_error())); |
| 659 | SSL_free(tls); |
| 660 | return NULL; |
| 661 | } |
| 662 | |
| 663 | /* check certificate verification result */ |
| 664 | verify = SSL_get_verify_result(tls); |
| 665 | switch (verify) { |
| 666 | case X509_V_OK: |
| 667 | VRB("Server certificate successfully verified."); |
| 668 | break; |
| 669 | default: |
Michal Vasko | b48aa81 | 2016-01-18 14:13:09 +0100 | [diff] [blame] | 670 | WRN("Server certificate verification problem (%s).", X509_verify_cert_error_string(verify)); |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 671 | } |
| 672 | |
| 673 | session = nc_connect_libssl(tls, ctx); |
| 674 | if (session) { |
Michal Vasko | 4282fae | 2016-02-18 10:03:42 +0100 | [diff] [blame] | 675 | session->flags |= NC_SESSION_CALLHOME; |
| 676 | |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 677 | /* store information into session and the dictionary */ |
Michal Vasko | 3031aae | 2016-01-27 16:07:18 +0100 | [diff] [blame] | 678 | session->host = lydict_insert(session->ctx, host, 0); |
Michal Vasko | 80cad7f | 2015-12-08 14:42:27 +0100 | [diff] [blame] | 679 | session->port = port; |
| 680 | session->username = lydict_insert(session->ctx, "certificate-based", 0); |
| 681 | } |
| 682 | |
| 683 | return session; |
| 684 | } |