summaryrefslogtreecommitdiff
path: root/vchat-tls.c
blob: 205f0e059cfa4636ba111b5aa6ccdb60a98f2c45 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/*
 * vchat-client - alpha version
 * vchat-tls.c - handling of SSL connection and X509 certificate
 * verification
 *
 * Copyright (C) 2007 Thorsten Schroeder <ths@berlin.ccc.de>
 *
 * This program is free software. It can be redistributed and/or modified,
 * provided that this copyright notice is kept intact. This program is
 * distributed in the hope that it will be useful, but without any warranty;
 * without even the implied warranty of merchantability or fitness for a
 * particular purpose. In no event shall the copyright holder be liable for
 * any direct, indirect, incidental or special damages arising in any way out
 * of the use of this software.
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

#include <readline/readline.h>

#include "vchat.h"
#include "vchat-tls.h"

const char *vchat_tls_version = "vchat-tls.c      $Id$";
const char *vchat_tls_version_external = "Unknown implementation; version unknown";

void vc_cleanup_x509store(vc_x509store_t *store)
{
   free(store->cafile);
   free(store->capath);
   free(store->crlfile);
   free(store->certfile);
   free(store->keyfile);
   memset(store, 0, sizeof(vc_x509store_t));
}

void vc_x509store_setflags(vc_x509store_t *store, int flags) { store->flags |= flags; }
void vc_x509store_clearflags(vc_x509store_t *store, int flags) { store->flags &= ~flags; }
void vc_x509store_set_pkeycb(vc_x509store_t *store, vc_askpass_cb_t callback) { store->askpass_callback = callback; }

void vc_x509store_setcafile(vc_x509store_t *store, char *file)
{
   free(store->cafile);
   store->cafile = ( file ? strdup(file) : 0 );
   store->flags |= VC_X509S_USE_CAFILE;
}

void vc_x509store_setcapath(vc_x509store_t *store, char *path)
{
   free(store->capath);
   store->capath = ( path ? strdup(path) : 0 );
}

void vc_x509store_setcrlfile(vc_x509store_t *store, char *file)
{
   free(store->crlfile);
   store->crlfile = ( file ? strdup(file) : 0 );
}

void vc_x509store_setkeyfile(vc_x509store_t *store, char *file)
{
   free(store->keyfile);
   store->keyfile = ( file ? strdup(file) : 0 );
}

void vc_x509store_setcertfile(vc_x509store_t *store, char *file)
{
   free(store->certfile);
   store->certfile = ( file ? strdup(file) : 0 );
   store->flags |= VC_X509S_USE_CERTIFICATE;
}

//// OPENSSL SPECIFIC

#include <openssl/err.h>
#include <openssl/ssl.h>
#include <openssl/bio.h>
#include <openssl/evp.h>
#include <openssl/x509.h>
#include <openssl/x509v3.h>
#include <openssl/conf.h>

void vchat_tls_get_version_external()
{
   snprintf(tmpstr, sizeof(tmpstr), "OpenSSL %s with %s", SSLeay_version(SSLEAY_VERSION), SSLeay_version(SSLEAY_CFLAGS));
   vchat_tls_version_external = strdup(tmpstr);
}

/* Helpers to work with vc_x509store_t used by all tls libs */
void vc_init_x509store(vc_x509store_t *store)
{
   static int sslinit;
   if (!sslinit++) {
      SSL_library_init ();
      SSL_load_error_strings();
   }
   memset(store, 0, sizeof(vc_x509store_t));

   /* We want to make verifying the peer the default */
   store->flags |= VC_X509S_SSL_VERIFY_PEER;
}

/* connection BIO for openssl */
static BIO *server_conn = NULL;

static SSL_CTX * vc_create_sslctx( vc_x509store_t *vc_store );
static int vc_verify_callback(int, X509_STORE_CTX *);
static X509_STORE * vc_x509store_create(vc_x509store_t *);

static SSL_CTX * vc_create_sslctx( vc_x509store_t *vc_store )
{
   int flags = 0;

   /* Explicitly use TLSv1 (or maybe later) */
   SSL_CTX    *ctx   = SSL_CTX_new(TLS_client_method());
   X509_STORE *store = vc_x509store_create(vc_store);

   if (!ctx || !store) {
      snprintf(tmpstr, sizeof(tmpstr), "CREATE CTX: %s",ERR_error_string (ERR_get_error (), NULL));
      writecf(FS_ERR, tmpstr);
      if (store)
         X509_STORE_free(store);
      if (ctx)
         SSL_CTX_free(ctx);
      return NULL;
   }

   SSL_CTX_set_cert_store(ctx, store);

   /* Disable some insecure protocols explicitly */
   SSL_CTX_set_options(ctx, SSL_OP_ALL | SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3);
   if (getstroption(CF_CIPHERSUITE))
     SSL_CTX_set_cipher_list(ctx, getstroption(CF_CIPHERSUITE));
   else
     SSL_CTX_set_cipher_list(ctx, "ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES256-SHA");

   SSL_CTX_set_verify_depth (ctx, getintoption(CF_VERIFYSSL));

   if( !(vc_store->flags & VC_X509S_SSL_VERIFY_MASK) ) {
      writecf(FS_DBG, tmpstr);
      flags = SSL_VERIFY_NONE;
   } else {
      if(vc_store->flags & VC_X509S_SSL_VERIFY_PEER)
         flags |= SSL_VERIFY_PEER;
      if(vc_store->flags & VC_X509S_SSL_VERIFY_FAIL_IF_NO_PEER_CERT)
         flags |= SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
      if(vc_store->flags & VC_X509S_SSL_VERIFY_CLIENT_ONCE)
         flags |= SSL_VERIFY_CLIENT_ONCE;
   }

   SSL_CTX_set_verify(ctx, flags, vc_verify_callback);

   if(vc_store->flags & VC_X509S_USE_CERTIFICATE) {
      int r = 0;
      if(vc_store->certfile)
         SSL_CTX_use_certificate_chain_file(ctx, vc_store->certfile);

      SSL_CTX_set_default_passwd_cb(ctx, vc_store->askpass_callback);

      if(vc_store->keyfile)
         r=SSL_CTX_use_PrivateKey_file(ctx, vc_store->keyfile,
               SSL_FILETYPE_PEM);

      if( r!=1 || !SSL_CTX_check_private_key(ctx)) {
         snprintf(tmpstr, sizeof(tmpstr), "CREATE CTX: Load private key failed");
         writecf(FS_ERR, tmpstr);
         SSL_CTX_free(ctx);
         return NULL;
      }
   }

   SSL_CTX_set_app_data(ctx, vc_store);
   return(ctx);
}

int vc_tls_connect( int serverfd, vc_x509store_t *vc_store )
{
  SSL_CTX * ctx = vc_create_sslctx(vc_store);
  X509 *peercert = NULL;
  BIO *ssl_conn = NULL;
  const SSL *sslp = NULL;
  const SSL_CIPHER * cipher = NULL;

  server_conn = BIO_new_socket( serverfd, 1 );

  /* To display and check server fingerprint */
  char fingerprint[EVP_MAX_MD_SIZE*4];
  unsigned char fingerprint_bin[EVP_MAX_MD_SIZE];
  unsigned int fingerprint_len;

  FILE *fingerprint_file = NULL;
  char * fp = fingerprint;

  long result, j;

  if( !ctx )
    goto all_errors;

  ssl_conn = BIO_new_ssl(ctx, 1);
  SSL_CTX_free(ctx);

  if( !ssl_conn )
    goto ssl_error;

  BIO_push( ssl_conn, server_conn );
  server_conn = ssl_conn;
  fflush(stdout);

  if( BIO_do_handshake( server_conn ) <= 0 )
    goto ssl_error;

  /* Show information about cipher used */
  /* Get cipher object */
  BIO_get_ssl(ssl_conn, &sslp);
  if (!sslp)
    goto ssl_error;

  cipher = SSL_get_current_cipher(sslp);
  if (cipher) {
    char cipher_desc[TMPSTRSIZE];
    snprintf(tmpstr, TMPSTRSIZE, "[SSL CIPHER       ] %s", SSL_CIPHER_description(cipher, cipher_desc, TMPSTRSIZE));
    writecf(FS_SERV, tmpstr);
  } else {
    snprintf(tmpstr, TMPSTRSIZE, "[SSL ERROR        ] Cipher not known / SSL object can't be queried!");
    writecf(FS_ERR, tmpstr);
  }

  /* Accept being connected, _if_ verification passed */
  peercert = SSL_get_peer_certificate(sslp);
  if (!peercert)
    goto ssl_error;

  /* show basic information about peer cert */
  snprintf(tmpstr, TMPSTRSIZE, "[SSL SUBJECT      ] %s", X509_NAME_oneline(X509_get_subject_name(peercert),0,0));
  writecf(FS_SERV, tmpstr);
  snprintf(tmpstr, TMPSTRSIZE, "[SSL ISSUER       ] %s", X509_NAME_oneline(X509_get_issuer_name(peercert),0,0));
  writecf(FS_SERV, tmpstr);

  /* calculate fingerprint */
  if (!X509_digest(peercert,EVP_sha1(),fingerprint_bin,&fingerprint_len))
    goto ssl_error;

  assert ( ( fingerprint_len > 1 ) && (fingerprint_len <= EVP_MAX_MD_SIZE ));
  for (j=0; j<(int)fingerprint_len; j++)
    fp += sprintf(fp, "%02X:", fingerprint_bin[j]);
  assert ( fp > fingerprint );
  fp[-1] = 0;
  snprintf(tmpstr, TMPSTRSIZE, "[SSL FINGERPRINT  ] %s (from server)", fingerprint);
  writecf(FS_SERV, tmpstr);

  /* we don't need the peercert anymore */
  X509_free(peercert);

  /* verify fingerprint */
  if (getintoption(CF_PINFINGER)) {

    fingerprint_file = fopen(tilde_expand(getstroption(CF_FINGERPRINT)), "r");
    if (fingerprint_file) {

      /* Read fingerprint from file */
      char old_fingerprint[EVP_MAX_MD_SIZE*4];
      char * r = fgets(old_fingerprint, sizeof(old_fingerprint), fingerprint_file);
      fclose(fingerprint_file);

      if (r) {
        /* chomp */
        char *nl = strchr(r, '\n');
        if (nl) *nl = 0;

        /* verify fingerprint matches stored version */
        if (!strcmp(fingerprint, old_fingerprint))
          return 0;
      }

      snprintf(tmpstr, TMPSTRSIZE, "[SSL FINGERPRINT  ] %s (from %s)", r ? old_fingerprint : "<FILE READ ERROR>", getstroption(CF_FINGERPRINT));
      writecf(FS_ERR, tmpstr);
      writecf(FS_ERR, "[SSL CONNECT ERROR] Fingerprint mismatch! Server cert updated?");
      return 1;
    }

    fingerprint_file = fopen(tilde_expand(getstroption(CF_FINGERPRINT)), "w");
    if (!fingerprint_file) {
      snprintf (tmpstr, TMPSTRSIZE, "[WARNING] Can't write fingerprint file, %s.", strerror(errno));
      writecf(FS_ERR, tmpstr);
    } else {
      fputs(fingerprint, fingerprint_file);
      fclose(fingerprint_file);
      writecf(FS_SERV, "Stored fingerprint.");
    }
    return 0;
  }

  /* If verify of x509 chain was requested, do the check here */
  result = SSL_get_verify_result(sslp);

  if (result == X509_V_OK)
    return 0;

  if (getintoption(CF_IGNSSL)) {
    writecf(FS_ERR, "[SSL VERIFY ERROR ] FAILURE IGNORED!!!");
    return 0;
  }

ssl_error:
  snprintf(tmpstr, TMPSTRSIZE, "[SSL CONNECT ERROR] %s", ERR_error_string (ERR_get_error (), NULL));
  writecf(FS_ERR, tmpstr);
all_errors:
  BIO_free_all( server_conn );
  server_conn = NULL;
  return 1;
}

#define VC_STORE_ERR_EXIT(s) do { \
      fprintf(stderr, "[E] SSL_STORE: %s\n", ERR_error_string (ERR_get_error (), NULL)); \
      if(s)  X509_STORE_free(s); \
      return(0); \
   } while(0)

X509_STORE *vc_x509store_create(vc_x509store_t *vc_store)
{
   X509_STORE *store    = NULL;
   X509_LOOKUP *lookup  = NULL;

   store = X509_STORE_new();

   X509_STORE_set_verify_cb_func(store, vc_verify_callback);

   if( !(lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file())) )
      VC_STORE_ERR_EXIT(store);

   if (!vc_store->cafile) {
      if( !(vc_store->flags & VC_X509S_USE_CAFILE) )
         X509_LOOKUP_load_file(lookup, 0, X509_FILETYPE_DEFAULT);
   } else if( !X509_LOOKUP_load_file(lookup, vc_store->cafile,
            X509_FILETYPE_PEM) )
      VC_STORE_ERR_EXIT(store);

   if (vc_store->crlfile) {
      if( !X509_load_crl_file(lookup, vc_store->crlfile,
               X509_FILETYPE_PEM) )
         VC_STORE_ERR_EXIT(store);

      X509_STORE_set_flags( store,
            X509_V_FLAG_CRL_CHECK | X509_V_FLAG_CRL_CHECK_ALL );
   }

   if ( !(lookup = X509_STORE_add_lookup(store, X509_LOOKUP_hash_dir())) )
      VC_STORE_ERR_EXIT(store);

   if ( !vc_store->capath ) {
      if( !(vc_store->flags & VC_X509S_USE_CAPATH) )
         X509_LOOKUP_add_dir(lookup, 0, X509_FILETYPE_DEFAULT);
   } else if( !X509_LOOKUP_add_dir(lookup, vc_store->capath,
            X509_FILETYPE_PEM) )
      VC_STORE_ERR_EXIT(store);

   return(store);
}

int vc_verify_callback(int ok, X509_STORE_CTX *store)
{
   if(!ok) {
      snprintf(tmpstr, TMPSTRSIZE, "[SSL VERIFY ERROR ] %s",
               X509_verify_cert_error_string(X509_STORE_CTX_get_error(store)));
      writecf(FS_ERR, tmpstr);
   }
   return (ok | getintoption(CF_IGNSSL));
}

ssize_t vc_tls_sendmessage(const void *buf, size_t size) {
   return BIO_write(server_conn, buf, size);
}

ssize_t vc_tls_receivemessage(void *buf, size_t size) {
   ssize_t received = (ssize_t)BIO_read (server_conn, buf, size);
   if (received != 0)
      return received;
   if (BIO_should_retry(server_conn))
      return -2;
   return 0;
}

void vc_tls_cleanup() {
  BIO_free_all( server_conn );
  server_conn = NULL;
}