Christian Grothoff | 90e9f83 | 2012-02-07 11:16:47 +0100 | [diff] [blame] | 1 | /* Copyright (c) 2012, Jacob Appelbaum. |
Jacob Appelbaum | 63e548e | 2012-07-29 16:25:30 -0700 | [diff] [blame] | 2 | * Copyright (c) 2012, The Tor Project, Inc. |
| 3 | * Copyright (c) 2012, Christian Grothoff. */ |
Christian Grothoff | 90e9f83 | 2012-02-07 11:16:47 +0100 | [diff] [blame] | 4 | /* See LICENSE for licensing information */ |
| 5 | /* |
| 6 | This file contains the license for tlsdate, |
| 7 | a free software project to set your system clock securely. |
| 8 | |
| 9 | It also lists the licenses for other components used by tlsdate. |
| 10 | |
| 11 | For more information about tlsdate, see https://github.com/ioerror/tlsdate |
| 12 | |
| 13 | If you got this file as a part of a larger bundle, |
| 14 | there may be other license terms that you should be aware of. |
| 15 | |
| 16 | =============================================================================== |
| 17 | tlsdate is distributed under this license: |
| 18 | |
| 19 | Copyright (c) 2011-2012, Jacob Appelbaum <jacob@appelbaum.net> |
| 20 | Copyright (c) 2011-2012, The Tor Project, Inc. |
| 21 | |
| 22 | Redistribution and use in source and binary forms, with or without |
| 23 | modification, are permitted provided that the following conditions are |
| 24 | met: |
| 25 | |
| 26 | * Redistributions of source code must retain the above copyright |
| 27 | notice, this list of conditions and the following disclaimer. |
| 28 | |
| 29 | * Redistributions in binary form must reproduce the above |
| 30 | copyright notice, this list of conditions and the following disclaimer |
| 31 | in the documentation and/or other materials provided with the |
| 32 | distribution. |
| 33 | |
| 34 | * Neither the names of the copyright owners nor the names of its |
| 35 | contributors may be used to endorse or promote products derived from |
| 36 | this software without specific prior written permission. |
| 37 | |
| 38 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 39 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 40 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 41 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 42 | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 43 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 44 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 45 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 46 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 47 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 48 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 49 | =============================================================================== |
| 50 | If you got tlsdate as a static binary with OpenSSL included, then you should |
| 51 | know: |
| 52 | |
| 53 | "This product includes software developed by the OpenSSL Project for use in |
| 54 | the OpenSSL Toolkit (http://www.openssl.org/)" |
| 55 | |
| 56 | =============================================================================== |
| 57 | */ |
| 58 | |
| 59 | /** |
Christian Grothoff | e267c35 | 2012-02-14 01:10:54 +0100 | [diff] [blame] | 60 | * \file tlsdate-helper.c |
| 61 | * \brief Helper program that does the actual work of setting the system clock. |
Christian Grothoff | 90e9f83 | 2012-02-07 11:16:47 +0100 | [diff] [blame] | 62 | **/ |
| 63 | |
| 64 | /* |
| 65 | * tlsdate is a tool for setting the system clock by hand or by communication |
| 66 | * with the network. It does not set the RTC. It is designed to be as secure as |
| 67 | * TLS (RFC 2246) but of course the security of TLS is often reduced to |
| 68 | * whichever CA racket you believe is trustworthy. By default, tlsdate trusts |
| 69 | * your local CA root store - so any of these companies could assist in a MITM |
| 70 | * attack against you and you'd be screwed. |
| 71 | |
| 72 | * This tool is designed to be run by hand or as a system daemon. It must be |
| 73 | * run as root or otherwise have the proper caps; it will not be able to set |
| 74 | * the system time without running as root or another privileged user. |
| 75 | */ |
| 76 | |
Jacob Appelbaum | 9639003 | 2012-07-15 15:53:13 -0400 | [diff] [blame] | 77 | #include "../config/tlsdate-config.h" |
Jacob Appelbaum | 8355d73 | 2012-07-30 01:29:05 -0700 | [diff] [blame] | 78 | #include "tlsdate-helper.h" |
Christian Grothoff | 90e9f83 | 2012-02-07 11:16:47 +0100 | [diff] [blame] | 79 | |
Elly Jones | 9ae3aa6 | 2012-06-20 15:32:46 -0400 | [diff] [blame] | 80 | |
Christian Grothoff | 191cd98 | 2012-02-14 00:48:45 +0100 | [diff] [blame] | 81 | /** helper function to print message and die */ |
Christian Grothoff | 90e9f83 | 2012-02-07 11:16:47 +0100 | [diff] [blame] | 82 | static void |
Jacob Appelbaum | 42ccf9d | 2012-07-29 16:30:15 -0700 | [diff] [blame] | 83 | die (const char *fmt, ...) |
Christian Grothoff | 90e9f83 | 2012-02-07 11:16:47 +0100 | [diff] [blame] | 84 | { |
| 85 | va_list ap; |
| 86 | |
| 87 | va_start(ap, fmt); |
| 88 | vfprintf(stderr, fmt, ap); |
| 89 | va_end(ap); |
| 90 | exit(1); |
| 91 | } |
| 92 | |
| 93 | |
Christian Grothoff | 90e9f83 | 2012-02-07 11:16:47 +0100 | [diff] [blame] | 94 | /** helper function for 'verbose' output */ |
| 95 | static void |
| 96 | verb (const char *fmt, ...) |
| 97 | { |
| 98 | va_list ap; |
| 99 | |
Christian Grothoff | afbae37 | 2012-02-07 14:17:53 +0100 | [diff] [blame] | 100 | if (! verbose) return; |
Christian Grothoff | 90e9f83 | 2012-02-07 11:16:47 +0100 | [diff] [blame] | 101 | va_start(ap, fmt); |
| 102 | // FIXME: stdout or stderr for verbose messages? |
| 103 | vfprintf(stderr, fmt, ap); |
| 104 | va_end(ap); |
Christian Grothoff | 90e9f83 | 2012-02-07 11:16:47 +0100 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | |
Jacob Appelbaum | 123bb3e | 2012-07-16 17:25:34 -0700 | [diff] [blame] | 108 | void |
Jacob Appelbaum | 42ccf9d | 2012-07-29 16:30:15 -0700 | [diff] [blame] | 109 | openssl_time_callback (const SSL* ssl, int where, int ret) |
Jacob Appelbaum | 9f80729 | 2012-07-16 18:24:37 -0700 | [diff] [blame] | 110 | { |
Jacob Appelbaum | 123bb3e | 2012-07-16 17:25:34 -0700 | [diff] [blame] | 111 | if (where == SSL_CB_CONNECT_LOOP && ssl->state == SSL3_ST_CR_CERT_A) |
| 112 | { |
Jacob Appelbaum | 9f80729 | 2012-07-16 18:24:37 -0700 | [diff] [blame] | 113 | // XXX TODO: If we want to trust the remote system for time, |
| 114 | // can we just read that time out of the remote system and if the |
| 115 | // cert verifies, decide that the time is reasonable? |
| 116 | // Such a process seems to indicate that a once valid cert would be |
| 117 | // forever valid - we stopgap that by ensuring it isn't less than |
| 118 | // the latest compiled_time and isn't above max_reasonable_time... |
| 119 | // XXX TODO: Solve eternal question about the Chicken and the Egg... |
| 120 | uint32_t compiled_time = RECENT_COMPILE_DATE; |
| 121 | uint32_t max_reasonable_time = MAX_REASONABLE_TIME; |
Jacob Appelbaum | 123bb3e | 2012-07-16 17:25:34 -0700 | [diff] [blame] | 122 | uint32_t server_time; |
Jacob Appelbaum | 9f80729 | 2012-07-16 18:24:37 -0700 | [diff] [blame] | 123 | verb("V: freezing time for x509 verification\n"); |
Jacob Appelbaum | 123bb3e | 2012-07-16 17:25:34 -0700 | [diff] [blame] | 124 | memcpy(&server_time, ssl->s3->server_random, sizeof(uint32_t)); |
Jacob Appelbaum | 9f80729 | 2012-07-16 18:24:37 -0700 | [diff] [blame] | 125 | if (compiled_time < ntohl(server_time) |
| 126 | && |
| 127 | ntohl(server_time) < max_reasonable_time) |
| 128 | { |
| 129 | verb("V: remote peer provided: %d, prefered over compile time: %d\n", |
| 130 | ntohl(server_time), compiled_time); |
| 131 | verb("V: freezing time with X509_VERIFY_PARAM_set_time\n"); |
| 132 | X509_VERIFY_PARAM_set_time(ssl->ctx->param, (time_t) ntohl(server_time)); |
| 133 | } else { |
| 134 | die("V: the remote server is a false ticker! server: %d compile: %d\n", |
| 135 | ntohl(server_time), compiled_time); |
| 136 | } |
Jacob Appelbaum | 123bb3e | 2012-07-16 17:25:34 -0700 | [diff] [blame] | 137 | } |
| 138 | } |
| 139 | |
Jacob Appelbaum | c4d3f9f | 2012-07-17 18:20:56 -0700 | [diff] [blame] | 140 | uint32_t |
Jacob Appelbaum | 42ccf9d | 2012-07-29 16:30:15 -0700 | [diff] [blame] | 141 | get_certificate_keybits (EVP_PKEY *public_key) |
Jacob Appelbaum | c4d3f9f | 2012-07-17 18:20:56 -0700 | [diff] [blame] | 142 | { |
| 143 | /* |
| 144 | In theory, we could use check_bitlen_dsa() and check_bitlen_rsa() |
| 145 | */ |
| 146 | uint32_t key_bits; |
| 147 | switch (public_key->type) |
| 148 | { |
| 149 | case EVP_PKEY_RSA: |
| 150 | verb("V: key type: EVP_PKEY_RSA\n"); |
| 151 | key_bits = BN_num_bits(public_key->pkey.rsa->n); |
| 152 | break; |
| 153 | case EVP_PKEY_RSA2: |
| 154 | verb("V: key type: EVP_PKEY_RSA2\n"); |
| 155 | key_bits = BN_num_bits(public_key->pkey.rsa->n); |
| 156 | break; |
| 157 | case EVP_PKEY_DSA: |
| 158 | verb("V: key type: EVP_PKEY_DSA\n"); |
| 159 | key_bits = BN_num_bits(public_key->pkey.dsa->p); |
| 160 | break; |
| 161 | case EVP_PKEY_DSA1: |
| 162 | verb("V: key type: EVP_PKEY_DSA1\n"); |
| 163 | key_bits = BN_num_bits(public_key->pkey.dsa->p); |
| 164 | break; |
| 165 | case EVP_PKEY_DSA2: |
| 166 | verb("V: key type: EVP_PKEY_DSA2\n"); |
| 167 | key_bits = BN_num_bits(public_key->pkey.dsa->p); |
| 168 | break; |
| 169 | case EVP_PKEY_DSA3: |
| 170 | verb("V: key type: EVP_PKEY_DSA3\n"); |
| 171 | key_bits = BN_num_bits(public_key->pkey.dsa->p); |
| 172 | break; |
| 173 | case EVP_PKEY_DSA4: |
| 174 | verb("V: key type: EVP_PKEY_DSA4\n"); |
| 175 | key_bits = BN_num_bits(public_key->pkey.dsa->p); |
| 176 | break; |
| 177 | case EVP_PKEY_DH: |
| 178 | verb("V: key type: EVP_PKEY_DH\n"); |
| 179 | key_bits = BN_num_bits(public_key->pkey.dh->pub_key); |
| 180 | break; |
| 181 | case EVP_PKEY_EC: |
| 182 | verb("V: key type: EVP_PKEY_EC\n"); |
| 183 | key_bits = EVP_PKEY_bits(public_key); |
| 184 | break; |
| 185 | // Should we also care about EVP_PKEY_HMAC and EVP_PKEY_CMAC? |
| 186 | default: |
| 187 | key_bits = 0; |
| 188 | die ("unknown public key type\n"); |
| 189 | break; |
| 190 | } |
Jacob Appelbaum | c4d3f9f | 2012-07-17 18:20:56 -0700 | [diff] [blame] | 191 | verb ("V: keybits: %d\n", key_bits); |
| 192 | return key_bits; |
| 193 | } |
| 194 | |
Jacob Appelbaum | 42ccf9d | 2012-07-29 16:30:15 -0700 | [diff] [blame] | 195 | /** |
| 196 | This extracts the first commonName and checks it against hostname. |
| 197 | */ |
| 198 | uint32_t |
| 199 | check_cn (SSL *ssl, const char *hostname) |
Jacob Appelbaum | c4d3f9f | 2012-07-17 18:20:56 -0700 | [diff] [blame] | 200 | { |
Jacob Appelbaum | 42ccf9d | 2012-07-29 16:30:15 -0700 | [diff] [blame] | 201 | uint32_t ret; |
| 202 | char *cn_buf; |
| 203 | X509 *certificate; |
Jacob Appelbaum | 6bb4b81 | 2012-07-30 02:27:37 -0700 | [diff] [blame^] | 204 | cn_buf = malloc(MAX_CN_NAME_LENGTH); |
Jacob Appelbaum | 42ccf9d | 2012-07-29 16:30:15 -0700 | [diff] [blame] | 205 | if (NULL == cn_buf) |
| 206 | { |
| 207 | die ("Unable to allocate memory for cn_buf\n"); |
Jacob Appelbaum | f3ef638 | 2012-07-18 10:34:54 -0700 | [diff] [blame] | 208 | } |
Jacob Appelbaum | 42ccf9d | 2012-07-29 16:30:15 -0700 | [diff] [blame] | 209 | certificate = SSL_get_peer_certificate(ssl); |
| 210 | |
| 211 | memset(cn_buf, '\0', (strlen(hostname) + 1)); |
| 212 | ret = X509_NAME_get_text_by_NID(X509_get_subject_name(certificate), |
| 213 | NID_commonName, cn_buf, (strlen(hostname) + 1)); |
| 214 | |
| 215 | if (-1 == ret && ret != strlen(hostname)) |
| 216 | { |
| 217 | die ("Unable to extract commonName\n"); |
| 218 | } |
| 219 | if (strcasecmp(cn_buf, hostname)) |
| 220 | { |
Jacob Appelbaum | 6bb4b81 | 2012-07-30 02:27:37 -0700 | [diff] [blame^] | 221 | verb ("V: commonName mismatch! Expected: %s - received: %s\n", |
Jacob Appelbaum | 42ccf9d | 2012-07-29 16:30:15 -0700 | [diff] [blame] | 222 | hostname, cn_buf); |
Jacob Appelbaum | c548c29 | 2012-07-30 02:04:35 -0700 | [diff] [blame] | 223 | return 0; |
Jacob Appelbaum | 42ccf9d | 2012-07-29 16:30:15 -0700 | [diff] [blame] | 224 | } else { |
| 225 | verb ("V: commonName match\n"); |
| 226 | return 1; |
| 227 | } |
| 228 | return 0; |
| 229 | } |
| 230 | |
| 231 | /** |
| 232 | Search for a hostname match in the SubjectAlternativeNames. |
| 233 | */ |
| 234 | uint32_t |
| 235 | check_san (SSL *ssl, const char *hostname) |
| 236 | { |
Jacob Appelbaum | fc9761e | 2012-07-30 01:29:49 -0700 | [diff] [blame] | 237 | X509 *cert; |
Jacob Appelbaum | fc9761e | 2012-07-30 01:29:49 -0700 | [diff] [blame] | 238 | int extcount, ok = 0; |
| 239 | /* What an OpenSSL mess ... */ |
| 240 | if (NULL == (cert = SSL_get_peer_certificate(ssl))) |
| 241 | { |
Jacob Appelbaum | 145886f | 2012-07-30 01:56:53 -0700 | [diff] [blame] | 242 | die ("Getting certificate failed\n"); |
Jacob Appelbaum | fc9761e | 2012-07-30 01:29:49 -0700 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | if ((extcount = X509_get_ext_count(cert)) > 0) |
| 246 | { |
| 247 | int i; |
| 248 | for (i = 0; i < extcount; ++i) |
| 249 | { |
| 250 | const char *extstr; |
| 251 | X509_EXTENSION *ext; |
| 252 | ext = X509_get_ext(cert, i); |
| 253 | extstr = OBJ_nid2sn(OBJ_obj2nid(X509_EXTENSION_get_object(ext))); |
| 254 | |
| 255 | if (!strcmp(extstr, "subjectAltName")) |
| 256 | { |
| 257 | |
| 258 | int j; |
| 259 | void *extvalstr; |
| 260 | const unsigned char *tmp; |
| 261 | |
| 262 | STACK_OF(CONF_VALUE) *val; |
| 263 | CONF_VALUE *nval; |
| 264 | X509V3_EXT_METHOD *method; |
| 265 | |
| 266 | if (!(method = X509V3_EXT_get(ext))) |
Jacob Appelbaum | 145886f | 2012-07-30 01:56:53 -0700 | [diff] [blame] | 267 | { |
Jacob Appelbaum | fc9761e | 2012-07-30 01:29:49 -0700 | [diff] [blame] | 268 | break; |
Jacob Appelbaum | 145886f | 2012-07-30 01:56:53 -0700 | [diff] [blame] | 269 | } |
| 270 | |
Jacob Appelbaum | fc9761e | 2012-07-30 01:29:49 -0700 | [diff] [blame] | 271 | tmp = ext->value->data; |
Jacob Appelbaum | 145886f | 2012-07-30 01:56:53 -0700 | [diff] [blame] | 272 | if (method->it) |
| 273 | { |
| 274 | extvalstr = ASN1_item_d2i(NULL, &tmp, ext->value->length, |
| 275 | ASN1_ITEM_ptr(method->it)); |
| 276 | } else { |
| 277 | extvalstr = method->d2i(NULL, &tmp, ext->value->length); |
| 278 | } |
| 279 | |
Jacob Appelbaum | fc9761e | 2012-07-30 01:29:49 -0700 | [diff] [blame] | 280 | if (!extvalstr) |
Jacob Appelbaum | 145886f | 2012-07-30 01:56:53 -0700 | [diff] [blame] | 281 | { |
| 282 | break; |
| 283 | } |
Jacob Appelbaum | fc9761e | 2012-07-30 01:29:49 -0700 | [diff] [blame] | 284 | |
| 285 | if (method->i2v) |
| 286 | { |
| 287 | val = method->i2v(method, extvalstr, NULL); |
| 288 | for (j = 0; j < sk_CONF_VALUE_num(val); ++j) |
| 289 | { |
| 290 | nval = sk_CONF_VALUE_value(val, j); |
| 291 | if (!strcasecmp(nval->name, "DNS") && |
Jacob Appelbaum | 145886f | 2012-07-30 01:56:53 -0700 | [diff] [blame] | 292 | !strcasecmp(nval->value, host)) |
Jacob Appelbaum | fc9761e | 2012-07-30 01:29:49 -0700 | [diff] [blame] | 293 | { |
Jacob Appelbaum | 145886f | 2012-07-30 01:56:53 -0700 | [diff] [blame] | 294 | verb ("V: subjectAltName matched\n"); |
Jacob Appelbaum | fc9761e | 2012-07-30 01:29:49 -0700 | [diff] [blame] | 295 | ok = 1; |
| 296 | break; |
| 297 | } |
| 298 | } |
| 299 | } |
| 300 | } |
| 301 | if (ok) |
| 302 | break; |
| 303 | } |
| 304 | } |
Jacob Appelbaum | fc9761e | 2012-07-30 01:29:49 -0700 | [diff] [blame] | 305 | X509_free(cert); |
Jacob Appelbaum | 145886f | 2012-07-30 01:56:53 -0700 | [diff] [blame] | 306 | return ok; |
Jacob Appelbaum | 42ccf9d | 2012-07-29 16:30:15 -0700 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | uint32_t |
| 310 | check_name (SSL *ssl, const char *hostname) |
| 311 | { |
| 312 | uint32_t ret; |
| 313 | ret = check_cn(ssl, hostname); |
| 314 | ret += check_san(ssl, hostname); |
Jacob Appelbaum | 145886f | 2012-07-30 01:56:53 -0700 | [diff] [blame] | 315 | if (0 != ret) |
| 316 | { |
| 317 | verb ("V: subjectAltName host verification passed\n"); |
| 318 | } else { |
| 319 | die ("subjectAltName host verification failed for host %s!\n", host); |
| 320 | } |
Jacob Appelbaum | 42ccf9d | 2012-07-29 16:30:15 -0700 | [diff] [blame] | 321 | return ret; |
| 322 | } |
| 323 | |
| 324 | uint32_t |
| 325 | verify_signature (SSL *ssl, const char *hostname) |
| 326 | { |
| 327 | long ssl_verify_result; |
| 328 | X509 *certificate; |
| 329 | |
| 330 | certificate = SSL_get_peer_certificate(ssl); |
| 331 | if (NULL == certificate) |
| 332 | { |
Jacob Appelbaum | 145886f | 2012-07-30 01:56:53 -0700 | [diff] [blame] | 333 | die ("Getting certificate failed\n"); |
Jacob Appelbaum | 42ccf9d | 2012-07-29 16:30:15 -0700 | [diff] [blame] | 334 | } |
| 335 | // In theory, we verify that the cert is valid |
| 336 | ssl_verify_result = SSL_get_verify_result(ssl); |
| 337 | switch (ssl_verify_result) |
| 338 | { |
| 339 | case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT: |
| 340 | case X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN: |
Jacob Appelbaum | 145886f | 2012-07-30 01:56:53 -0700 | [diff] [blame] | 341 | die ("certificate is self signed\n"); |
Jacob Appelbaum | 42ccf9d | 2012-07-29 16:30:15 -0700 | [diff] [blame] | 342 | case X509_V_OK: |
Jacob Appelbaum | 145886f | 2012-07-30 01:56:53 -0700 | [diff] [blame] | 343 | verb ("V: certificate verification passed\n"); |
Jacob Appelbaum | 42ccf9d | 2012-07-29 16:30:15 -0700 | [diff] [blame] | 344 | break; |
| 345 | default: |
Jacob Appelbaum | 145886f | 2012-07-30 01:56:53 -0700 | [diff] [blame] | 346 | die ("certification verification error: %ld\n", |
Jacob Appelbaum | 42ccf9d | 2012-07-29 16:30:15 -0700 | [diff] [blame] | 347 | ssl_verify_result); |
| 348 | } |
| 349 | return 0; |
| 350 | } |
| 351 | |
| 352 | void |
| 353 | check_key_length (SSL *ssl) |
| 354 | { |
| 355 | uint32_t key_bits; |
| 356 | X509 *certificate; |
| 357 | EVP_PKEY *public_key; |
| 358 | certificate = SSL_get_peer_certificate (ssl); |
| 359 | public_key = X509_get_pubkey (certificate); |
| 360 | if (NULL == public_key) |
| 361 | { |
| 362 | die ("public key extraction failure\n"); |
| 363 | } else { |
| 364 | verb ("V: public key is ready for inspection\n"); |
| 365 | } |
| 366 | |
| 367 | key_bits = get_certificate_keybits (public_key); |
| 368 | if (MIN_PUB_KEY_LEN >= key_bits && public_key->type != EVP_PKEY_EC) |
| 369 | { |
| 370 | die ("Unsafe public key size: %d bits\n", key_bits); |
| 371 | } else { |
| 372 | if (public_key->type == EVP_PKEY_EC) |
| 373 | if(key_bits >= MIN_ECC_PUB_KEY_LEN |
| 374 | && key_bits <= MAX_ECC_PUB_KEY_LEN) |
| 375 | { |
| 376 | verb ("V: ECC key length appears safe\n"); |
| 377 | } else { |
| 378 | die ("Unsafe ECC key size: %d bits\n", key_bits); |
| 379 | } else { |
| 380 | verb ("V: key length appears safe\n"); |
| 381 | } |
| 382 | } |
| 383 | EVP_PKEY_free (public_key); |
| 384 | } |
| 385 | |
| 386 | void |
| 387 | inspect_key (SSL *ssl, const char *hostname) |
| 388 | { |
| 389 | |
| 390 | verify_signature (ssl, hostname); |
| 391 | check_name (ssl, hostname); |
Jacob Appelbaum | c4d3f9f | 2012-07-17 18:20:56 -0700 | [diff] [blame] | 392 | } |
| 393 | |
Christian Grothoff | 88b422b | 2012-02-14 00:35:09 +0100 | [diff] [blame] | 394 | /** |
| 395 | * Run SSL handshake and store the resulting time value in the |
| 396 | * 'time_map'. |
| 397 | * |
| 398 | * @param time_map where to store the current time |
| 399 | */ |
| 400 | static void |
Jacob Appelbaum | c732f4e | 2012-07-15 22:38:46 -0400 | [diff] [blame] | 401 | run_ssl (uint32_t *time_map, int time_is_an_illusion) |
Christian Grothoff | 90e9f83 | 2012-02-07 11:16:47 +0100 | [diff] [blame] | 402 | { |
Christian Grothoff | 90e9f83 | 2012-02-07 11:16:47 +0100 | [diff] [blame] | 403 | BIO *s_bio; |
Christian Grothoff | 90e9f83 | 2012-02-07 11:16:47 +0100 | [diff] [blame] | 404 | SSL_CTX *ctx; |
| 405 | SSL *ssl; |
| 406 | |
Christian Grothoff | 90e9f83 | 2012-02-07 11:16:47 +0100 | [diff] [blame] | 407 | SSL_load_error_strings(); |
| 408 | SSL_library_init(); |
| 409 | |
| 410 | ctx = NULL; |
| 411 | if (0 == strcmp("sslv23", protocol)) |
| 412 | { |
| 413 | verb ("V: using SSLv23_client_method()\n"); |
| 414 | ctx = SSL_CTX_new(SSLv23_client_method()); |
| 415 | } else if (0 == strcmp("sslv3", protocol)) |
| 416 | { |
| 417 | verb ("V: using SSLv3_client_method()\n"); |
| 418 | ctx = SSL_CTX_new(SSLv3_client_method()); |
| 419 | } else if (0 == strcmp("tlsv1", protocol)) |
| 420 | { |
| 421 | verb ("V: using TLSv1_client_method()\n"); |
| 422 | ctx = SSL_CTX_new(TLSv1_client_method()); |
| 423 | } else |
| 424 | die("Unsupported protocol `%s'\n", protocol); |
| 425 | |
| 426 | if (ctx == NULL) |
| 427 | die("OpenSSL failed to support protocol `%s'\n", protocol); |
| 428 | |
| 429 | if (ca_racket) |
| 430 | { |
Elly Jones | 9ae3aa6 | 2012-06-20 15:32:46 -0400 | [diff] [blame] | 431 | if (1 != SSL_CTX_load_verify_locations(ctx, NULL, certdir)) |
Christian Grothoff | 90e9f83 | 2012-02-07 11:16:47 +0100 | [diff] [blame] | 432 | fprintf(stderr, "SSL_CTX_load_verify_locations failed\n"); |
| 433 | } |
| 434 | |
| 435 | if (NULL == (s_bio = BIO_new_ssl_connect(ctx))) |
| 436 | die ("SSL BIO setup failed\n"); |
| 437 | BIO_get_ssl(s_bio, &ssl); |
| 438 | if (NULL == ssl) |
| 439 | die ("SSL setup failed\n"); |
Jacob Appelbaum | 9f80729 | 2012-07-16 18:24:37 -0700 | [diff] [blame] | 440 | |
| 441 | if (time_is_an_illusion) |
| 442 | { |
| 443 | SSL_set_info_callback(ssl, openssl_time_callback); |
| 444 | } |
| 445 | |
Christian Grothoff | 90e9f83 | 2012-02-07 11:16:47 +0100 | [diff] [blame] | 446 | SSL_set_mode(ssl, SSL_MODE_AUTO_RETRY); |
| 447 | if ( (1 != BIO_set_conn_hostname(s_bio, host)) || |
| 448 | (1 != BIO_set_conn_port(s_bio, port)) ) |
| 449 | die ("Failed to initialize connection to `%s:%s'\n", host, port); |
| 450 | |
Stewart Smith | 3aa3f38 | 2012-06-29 14:36:34 +1000 | [diff] [blame] | 451 | if (NULL == BIO_new_fp(stdout, BIO_NOCLOSE)) |
| 452 | die ("BIO_new_fp returned error, possibly: %s", strerror(errno)); |
Christian Grothoff | 90e9f83 | 2012-02-07 11:16:47 +0100 | [diff] [blame] | 453 | |
Jacob Appelbaum | 405a793 | 2012-02-15 15:22:02 -0800 | [diff] [blame] | 454 | // This should run in seccomp |
| 455 | // eg: prctl(PR_SET_SECCOMP, 1); |
| 456 | if (1 != BIO_do_connect(s_bio)) // XXX TODO: BIO_should_retry() later? |
Jacob Appelbaum | 9a15957 | 2012-02-20 12:39:44 -0800 | [diff] [blame] | 457 | die ("SSL connection failed\n"); |
Philipp Winter | d96af62 | 2012-02-15 22:15:50 +0100 | [diff] [blame] | 458 | if (1 != BIO_do_handshake(s_bio)) |
| 459 | die ("SSL handshake failed\n"); |
Jacob Appelbaum | 42ccf9d | 2012-07-29 16:30:15 -0700 | [diff] [blame] | 460 | |
Christian Grothoff | 90e9f83 | 2012-02-07 11:16:47 +0100 | [diff] [blame] | 461 | // Verify the peer certificate against the CA certs on the local system |
| 462 | if (ca_racket) { |
Jacob Appelbaum | 42ccf9d | 2012-07-29 16:30:15 -0700 | [diff] [blame] | 463 | inspect_key (ssl, host); |
Christian Grothoff | 90e9f83 | 2012-02-07 11:16:47 +0100 | [diff] [blame] | 464 | } else { |
| 465 | verb ("V: Certificate verification skipped!\n"); |
| 466 | } |
Jacob Appelbaum | 42ccf9d | 2012-07-29 16:30:15 -0700 | [diff] [blame] | 467 | check_key_length(ssl); |
Christian Grothoff | 90e9f83 | 2012-02-07 11:16:47 +0100 | [diff] [blame] | 468 | // from /usr/include/openssl/ssl3.h |
Jacob Appelbaum | ca64b50 | 2012-06-28 21:54:14 -0700 | [diff] [blame] | 469 | // ssl->s3->server_random is an unsigned char of 32 bits |
Jacob Appelbaum | 9a15957 | 2012-02-20 12:39:44 -0800 | [diff] [blame] | 470 | memcpy(time_map, ssl->s3->server_random, sizeof (uint32_t)); |
Jacob Appelbaum | 8355d73 | 2012-07-30 01:29:05 -0700 | [diff] [blame] | 471 | SSL_free(ssl); |
Jacob Appelbaum | b79b0eb | 2012-07-29 23:00:21 -0700 | [diff] [blame] | 472 | SSL_CTX_free(ctx); |
Christian Grothoff | 88b422b | 2012-02-14 00:35:09 +0100 | [diff] [blame] | 473 | } |
Christian Grothoff | 90e9f83 | 2012-02-07 11:16:47 +0100 | [diff] [blame] | 474 | |
Christian Grothoff | 191cd98 | 2012-02-14 00:48:45 +0100 | [diff] [blame] | 475 | /** drop root rights and become 'nobody' */ |
Christian Grothoff | bd15a22 | 2012-02-14 00:40:57 +0100 | [diff] [blame] | 476 | static void |
Jacob Appelbaum | 42ccf9d | 2012-07-29 16:30:15 -0700 | [diff] [blame] | 477 | become_nobody (void) |
Christian Grothoff | bd15a22 | 2012-02-14 00:40:57 +0100 | [diff] [blame] | 478 | { |
| 479 | uid_t uid; |
Jacob Appelbaum | 2c385d2 | 2012-02-21 14:04:15 -0800 | [diff] [blame] | 480 | gid_t gid; |
Christian Grothoff | bd15a22 | 2012-02-14 00:40:57 +0100 | [diff] [blame] | 481 | struct passwd *pw; |
Jacob Appelbaum | 2c385d2 | 2012-02-21 14:04:15 -0800 | [diff] [blame] | 482 | struct group *gr; |
Christian Grothoff | bd15a22 | 2012-02-14 00:40:57 +0100 | [diff] [blame] | 483 | |
Christian Grothoff | e267c35 | 2012-02-14 01:10:54 +0100 | [diff] [blame] | 484 | if (0 != getuid ()) |
| 485 | return; /* not running as root to begin with; should (!) be harmless to continue |
Jacob Appelbaum | d84456c | 2012-02-15 16:02:31 -0800 | [diff] [blame] | 486 | without dropping to 'nobody' (setting time will fail in the end) */ |
Christian Grothoff | bd15a22 | 2012-02-14 00:40:57 +0100 | [diff] [blame] | 487 | pw = getpwnam(UNPRIV_USER); |
Jacob Appelbaum | 2c385d2 | 2012-02-21 14:04:15 -0800 | [diff] [blame] | 488 | gr = getgrnam(UNPRIV_GROUP); |
Christian Grothoff | bd15a22 | 2012-02-14 00:40:57 +0100 | [diff] [blame] | 489 | if (NULL == pw) |
| 490 | die ("Failed to obtain UID for `%s'\n", UNPRIV_USER); |
Jacob Appelbaum | 2c385d2 | 2012-02-21 14:04:15 -0800 | [diff] [blame] | 491 | if (NULL == gr) |
| 492 | die ("Failed to obtain GID for `%s'\n", UNPRIV_GROUP); |
Christian Grothoff | bd15a22 | 2012-02-14 00:40:57 +0100 | [diff] [blame] | 493 | uid = pw->pw_uid; |
| 494 | if (0 == uid) |
| 495 | die ("UID for `%s' is 0, refusing to run SSL\n", UNPRIV_USER); |
Jacob Appelbaum | 2c385d2 | 2012-02-21 14:04:15 -0800 | [diff] [blame] | 496 | gid = pw->pw_gid; |
| 497 | if (0 == gid || 0 == gr->gr_gid) |
| 498 | die ("GID for `%s' is 0, refusing to run SSL\n", UNPRIV_USER); |
| 499 | if (pw->pw_gid != gr->gr_gid) |
Jacob Appelbaum | ad873f3 | 2012-02-21 14:36:10 -0800 | [diff] [blame] | 500 | die ("GID for `%s' is not `%s' as expected, refusing to run SSL\n", |
| 501 | UNPRIV_USER, UNPRIV_GROUP); |
| 502 | |
| 503 | if (0 != initgroups((const char *)UNPRIV_USER, gr->gr_gid)) |
| 504 | die ("Unable to initgroups for `%s' in group `%s' as expected\n", |
| 505 | UNPRIV_USER, UNPRIV_GROUP); |
Jacob Appelbaum | 2c385d2 | 2012-02-21 14:04:15 -0800 | [diff] [blame] | 506 | |
| 507 | #ifdef HAVE_SETRESGID |
| 508 | if (0 != setresgid (gid, gid, gid)) |
| 509 | die ("Failed to setresgid: %s\n", strerror (errno)); |
| 510 | #else |
| 511 | if (0 != (setgid (gid) | setegid (gid))) |
| 512 | die ("Failed to setgid: %s\n", strerror (errno)); |
| 513 | #endif |
Christian Grothoff | bd15a22 | 2012-02-14 00:40:57 +0100 | [diff] [blame] | 514 | #ifdef HAVE_SETRESUID |
| 515 | if (0 != setresuid (uid, uid, uid)) |
| 516 | die ("Failed to setresuid: %s\n", strerror (errno)); |
| 517 | #else |
| 518 | if (0 != (setuid (uid) | seteuid (uid))) |
| 519 | die ("Failed to setuid: %s\n", strerror (errno)); |
Jacob Appelbaum | 2c385d2 | 2012-02-21 14:04:15 -0800 | [diff] [blame] | 520 | #endif |
Christian Grothoff | bd15a22 | 2012-02-14 00:40:57 +0100 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | |
Christian Grothoff | 88b422b | 2012-02-14 00:35:09 +0100 | [diff] [blame] | 524 | int |
| 525 | main(int argc, char **argv) |
| 526 | { |
| 527 | uint32_t *time_map; |
| 528 | struct timeval start_timeval; |
| 529 | struct timeval end_timeval; |
Jacob Appelbaum | 894d527 | 2012-07-15 14:32:39 -0400 | [diff] [blame] | 530 | struct timeval warp_time; |
Christian Grothoff | 88b422b | 2012-02-14 00:35:09 +0100 | [diff] [blame] | 531 | int status; |
| 532 | pid_t ssl_child; |
Christian Grothoff | e267c35 | 2012-02-14 01:10:54 +0100 | [diff] [blame] | 533 | long long rt_time_ms; |
| 534 | uint32_t server_time_s; |
Jeroen Massar | c27a92a | 2012-07-13 11:49:05 +0200 | [diff] [blame] | 535 | int setclock; |
| 536 | int showtime; |
Jacob Appelbaum | 894d527 | 2012-07-15 14:32:39 -0400 | [diff] [blame] | 537 | int timewarp; |
Jacob Appelbaum | c732f4e | 2012-07-15 22:38:46 -0400 | [diff] [blame] | 538 | int leap; |
Christian Grothoff | 88b422b | 2012-02-14 00:35:09 +0100 | [diff] [blame] | 539 | |
Jacob Appelbaum | c732f4e | 2012-07-15 22:38:46 -0400 | [diff] [blame] | 540 | if (argc != 11) |
Christian Grothoff | 88b422b | 2012-02-14 00:35:09 +0100 | [diff] [blame] | 541 | return 1; |
| 542 | host = argv[1]; |
| 543 | port = argv[2]; |
| 544 | protocol = argv[3]; |
Elly Jones | 9ae3aa6 | 2012-06-20 15:32:46 -0400 | [diff] [blame] | 545 | certdir = argv[6]; |
Christian Grothoff | 88b422b | 2012-02-14 00:35:09 +0100 | [diff] [blame] | 546 | ca_racket = (0 != strcmp ("unchecked", argv[4])); |
| 547 | verbose = (0 != strcmp ("quiet", argv[5])); |
Jeroen Massar | c27a92a | 2012-07-13 11:49:05 +0200 | [diff] [blame] | 548 | setclock = (0 == strcmp ("setclock", argv[7])); |
| 549 | showtime = (0 == strcmp ("showtime", argv[8])); |
Jacob Appelbaum | 894d527 | 2012-07-15 14:32:39 -0400 | [diff] [blame] | 550 | timewarp = (0 == strcmp ("timewarp", argv[9])); |
Jacob Appelbaum | c732f4e | 2012-07-15 22:38:46 -0400 | [diff] [blame] | 551 | leap = (0 == strcmp ("leapaway", argv[10])); |
Jacob Appelbaum | 894d527 | 2012-07-15 14:32:39 -0400 | [diff] [blame] | 552 | |
| 553 | if (timewarp) |
| 554 | { |
| 555 | warp_time.tv_sec = RECENT_COMPILE_DATE; |
| 556 | warp_time.tv_usec = 0; |
Jacob Appelbaum | 9639003 | 2012-07-15 15:53:13 -0400 | [diff] [blame] | 557 | verb ("V: RECENT_COMPILE_DATE is %lu.%06lu\n", |
| 558 | (unsigned long)warp_time.tv_sec, |
| 559 | (unsigned long)warp_time.tv_usec); |
Jacob Appelbaum | 894d527 | 2012-07-15 14:32:39 -0400 | [diff] [blame] | 560 | } |
Jeroen Massar | c27a92a | 2012-07-13 11:49:05 +0200 | [diff] [blame] | 561 | |
| 562 | /* We are not going to set the clock, thus no need to stay root */ |
Jacob Appelbaum | 894d527 | 2012-07-15 14:32:39 -0400 | [diff] [blame] | 563 | if (0 == setclock && 0 == timewarp) |
| 564 | { |
Jeroen Massar | c27a92a | 2012-07-13 11:49:05 +0200 | [diff] [blame] | 565 | become_nobody (); |
Jacob Appelbaum | 894d527 | 2012-07-15 14:32:39 -0400 | [diff] [blame] | 566 | } |
Christian Grothoff | 88b422b | 2012-02-14 00:35:09 +0100 | [diff] [blame] | 567 | |
| 568 | time_map = mmap (NULL, sizeof (uint32_t), |
Jacob Appelbaum | d84456c | 2012-02-15 16:02:31 -0800 | [diff] [blame] | 569 | PROT_READ | PROT_WRITE, |
| 570 | MAP_SHARED | MAP_ANONYMOUS, -1, 0); |
Christian Grothoff | 88b422b | 2012-02-14 00:35:09 +0100 | [diff] [blame] | 571 | if (MAP_FAILED == time_map) |
| 572 | { |
| 573 | fprintf (stderr, "mmap failed: %s\n", |
Jacob Appelbaum | 894d527 | 2012-07-15 14:32:39 -0400 | [diff] [blame] | 574 | strerror (errno)); |
Christian Grothoff | 88b422b | 2012-02-14 00:35:09 +0100 | [diff] [blame] | 575 | return 1; |
| 576 | } |
Christian Grothoff | 191cd98 | 2012-02-14 00:48:45 +0100 | [diff] [blame] | 577 | |
| 578 | /* Get the current time from the system clock. */ |
Christian Grothoff | 88b422b | 2012-02-14 00:35:09 +0100 | [diff] [blame] | 579 | if (0 != gettimeofday(&start_timeval, NULL)) |
Jacob Appelbaum | 894d527 | 2012-07-15 14:32:39 -0400 | [diff] [blame] | 580 | { |
Christian Grothoff | 88b422b | 2012-02-14 00:35:09 +0100 | [diff] [blame] | 581 | die ("Failed to read current time of day: %s\n", strerror (errno)); |
Jacob Appelbaum | 894d527 | 2012-07-15 14:32:39 -0400 | [diff] [blame] | 582 | } |
| 583 | |
Christian Grothoff | 191cd98 | 2012-02-14 00:48:45 +0100 | [diff] [blame] | 584 | verb ("V: time is currently %lu.%06lu\n", |
Jacob Appelbaum | 894d527 | 2012-07-15 14:32:39 -0400 | [diff] [blame] | 585 | (unsigned long)start_timeval.tv_sec, |
| 586 | (unsigned long)start_timeval.tv_usec); |
| 587 | |
| 588 | if (((unsigned long)start_timeval.tv_sec) < ((unsigned long)warp_time.tv_sec)) |
| 589 | { |
| 590 | verb ("V: local clock time is less than RECENT_COMPILE_DATE\n"); |
| 591 | if (timewarp) |
| 592 | { |
| 593 | verb ("V: Attempting to warp local clock into the future\n"); |
| 594 | if (0 != settimeofday(&warp_time, NULL)) |
| 595 | { |
| 596 | die ("setting time failed: %s (Attempted to set clock to %lu.%06lu)\n", |
| 597 | strerror (errno), |
| 598 | (unsigned long)warp_time.tv_sec, |
| 599 | (unsigned long)warp_time.tv_usec); |
| 600 | } |
| 601 | if (0 != gettimeofday(&start_timeval, NULL)) |
| 602 | { |
| 603 | die ("Failed to read current time of day: %s\n", strerror (errno)); |
| 604 | } |
| 605 | verb ("V: time is currently %lu.%06lu\n", |
| 606 | (unsigned long)start_timeval.tv_sec, |
| 607 | (unsigned long)start_timeval.tv_usec); |
| 608 | verb ("V: It's just a step to the left...\n"); |
| 609 | } |
| 610 | } else { |
| 611 | verb ("V: time is greater than RECENT_COMPILE_DATE\n"); |
| 612 | } |
Christian Grothoff | 191cd98 | 2012-02-14 00:48:45 +0100 | [diff] [blame] | 613 | |
| 614 | /* initialize to bogus value, just to be on the safe side */ |
Christian Grothoff | 88b422b | 2012-02-14 00:35:09 +0100 | [diff] [blame] | 615 | *time_map = 0; |
Christian Grothoff | e267c35 | 2012-02-14 01:10:54 +0100 | [diff] [blame] | 616 | |
| 617 | /* Run SSL interaction in separate process (and not as 'root') */ |
Christian Grothoff | 88b422b | 2012-02-14 00:35:09 +0100 | [diff] [blame] | 618 | ssl_child = fork (); |
| 619 | if (-1 == ssl_child) |
| 620 | die ("fork failed: %s\n", strerror (errno)); |
| 621 | if (0 == ssl_child) |
| 622 | { |
Christian Grothoff | bd15a22 | 2012-02-14 00:40:57 +0100 | [diff] [blame] | 623 | become_nobody (); |
Jacob Appelbaum | c732f4e | 2012-07-15 22:38:46 -0400 | [diff] [blame] | 624 | run_ssl (time_map, leap); |
Christian Grothoff | 88b422b | 2012-02-14 00:35:09 +0100 | [diff] [blame] | 625 | (void) munmap (time_map, sizeof (uint32_t)); |
| 626 | _exit (0); |
| 627 | } |
| 628 | if (ssl_child != waitpid (ssl_child, &status, 0)) |
| 629 | die ("waitpid failed: %s\n", strerror (errno)); |
Christian Grothoff | 191cd98 | 2012-02-14 00:48:45 +0100 | [diff] [blame] | 630 | if (! (WIFEXITED (status) && (0 == WEXITSTATUS (status)) )) |
| 631 | die ("child process failed in SSL handshake\n"); |
Christian Grothoff | 88b422b | 2012-02-14 00:35:09 +0100 | [diff] [blame] | 632 | |
| 633 | if (0 != gettimeofday(&end_timeval, NULL)) |
| 634 | die ("Failed to read current time of day: %s\n", strerror (errno)); |
Christian Grothoff | e267c35 | 2012-02-14 01:10:54 +0100 | [diff] [blame] | 635 | |
| 636 | /* calculate RTT */ |
| 637 | rt_time_ms = (end_timeval.tv_sec - start_timeval.tv_sec) * 1000 + (end_timeval.tv_usec - start_timeval.tv_usec) / 1000; |
| 638 | if (rt_time_ms < 0) |
| 639 | rt_time_ms = 0; /* non-linear time... */ |
| 640 | server_time_s = ntohl (*time_map); |
| 641 | munmap (time_map, sizeof (uint32_t)); |
Christian Grothoff | 88b422b | 2012-02-14 00:35:09 +0100 | [diff] [blame] | 642 | |
Jacob Appelbaum | 9a15957 | 2012-02-20 12:39:44 -0800 | [diff] [blame] | 643 | verb ("V: server time %u (difference is about %d s) was fetched in %lld ms\n", |
Jacob Appelbaum | d84456c | 2012-02-15 16:02:31 -0800 | [diff] [blame] | 644 | (unsigned int) server_time_s, |
| 645 | start_timeval.tv_sec - server_time_s, |
| 646 | rt_time_ms); |
Christian Grothoff | 191cd98 | 2012-02-14 00:48:45 +0100 | [diff] [blame] | 647 | |
Philipp Winter | b3ca577 | 2012-02-16 11:56:11 +0100 | [diff] [blame] | 648 | /* warning if the handshake took too long */ |
| 649 | if (rt_time_ms > TLS_RTT_THRESHOLD) { |
| 650 | verb ("V: the TLS handshake took more than %d msecs - consider using a different " \ |
| 651 | "server or run it again\n", TLS_RTT_THRESHOLD); |
| 652 | } |
| 653 | |
Jeroen Massar | c27a92a | 2012-07-13 11:49:05 +0200 | [diff] [blame] | 654 | if (showtime) |
| 655 | { |
Jacob Appelbaum | 699a276 | 2012-07-15 16:14:14 -0400 | [diff] [blame] | 656 | struct tm ltm; |
| 657 | time_t tim = server_time_s; |
Jeroen Massar | c27a92a | 2012-07-13 11:49:05 +0200 | [diff] [blame] | 658 | char buf[256]; |
| 659 | |
| 660 | localtime_r(&tim, <m); |
| 661 | (void) strftime(buf, sizeof buf, "%a %b %e %H:%M:%S %Z %Y", <m); |
| 662 | fprintf(stdout, "%s\n", buf); |
| 663 | } |
| 664 | |
Christian Grothoff | 191cd98 | 2012-02-14 00:48:45 +0100 | [diff] [blame] | 665 | /* finally, actually set the time */ |
Jeroen Massar | c27a92a | 2012-07-13 11:49:05 +0200 | [diff] [blame] | 666 | if (setclock) |
Christian Grothoff | 90e9f83 | 2012-02-07 11:16:47 +0100 | [diff] [blame] | 667 | { |
| 668 | struct timeval server_time; |
Christian Grothoff | 90e9f83 | 2012-02-07 11:16:47 +0100 | [diff] [blame] | 669 | |
Christian Grothoff | e267c35 | 2012-02-14 01:10:54 +0100 | [diff] [blame] | 670 | /* correct server time by half of RTT */ |
| 671 | server_time.tv_sec = server_time_s + (rt_time_ms / 2 / 1000); |
| 672 | server_time.tv_usec = (rt_time_ms / 2) % 1000; |
| 673 | |
Christian Grothoff | 90e9f83 | 2012-02-07 11:16:47 +0100 | [diff] [blame] | 674 | // We should never receive a time that is before the time we were last |
| 675 | // compiled; we subscribe to the linear theory of time for this program |
| 676 | // and this program alone! |
Christian Grothoff | f5098b4 | 2012-02-07 12:20:33 +0100 | [diff] [blame] | 677 | if (server_time.tv_sec >= MAX_REASONABLE_TIME) |
Jacob Appelbaum | d84456c | 2012-02-15 16:02:31 -0800 | [diff] [blame] | 678 | die("remote server is a false ticker from the future!\n"); |
Christian Grothoff | 90e9f83 | 2012-02-07 11:16:47 +0100 | [diff] [blame] | 679 | if (server_time.tv_sec <= RECENT_COMPILE_DATE) |
Jacob Appelbaum | d84456c | 2012-02-15 16:02:31 -0800 | [diff] [blame] | 680 | die ("remote server is a false ticker!\n"); |
Christian Grothoff | 90e9f83 | 2012-02-07 11:16:47 +0100 | [diff] [blame] | 681 | if (0 != settimeofday(&server_time, NULL)) |
Stewart Smith | f478044 | 2012-06-29 14:31:49 +1000 | [diff] [blame] | 682 | die ("setting time failed: %s (Difference from server is about %d)\n", |
Jacob Appelbaum | 699a276 | 2012-07-15 16:14:14 -0400 | [diff] [blame] | 683 | strerror (errno), |
| 684 | start_timeval.tv_sec - server_time_s); |
Jeroen Massar | c27a92a | 2012-07-13 11:49:05 +0200 | [diff] [blame] | 685 | verb ("V: setting time succeeded\n"); |
Christian Grothoff | 90e9f83 | 2012-02-07 11:16:47 +0100 | [diff] [blame] | 686 | } |
Christian Grothoff | 90e9f83 | 2012-02-07 11:16:47 +0100 | [diff] [blame] | 687 | return 0; |
| 688 | } |