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