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