~ubuntu-branches/ubuntu/raring/gnutls26/raring

« back to all changes in this revision

Viewing changes to src/cli.c

  • Committer: Bazaar Package Importer
  • Author(s): Steve Langasek
  • Date: 2011-05-20 13:07:18 UTC
  • mfrom: (12.1.11 sid)
  • Revision ID: james.westby@ubuntu.com-20110520130718-db41dybbanzfvlji
Tags: 2.10.5-1ubuntu1
* Merge from Debian unstable, remaining changes:
  - Fix build failure with --no-add-needed.
  - Build for multiarch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009 Free Software Foundation
3
 
 * Copyright (C) 2000,2001,2002,2003 Nikos Mavrogiannopoulos
4
 
 *
5
 
 * This file is part of GNUTLS.
6
 
 *
7
 
 * GNUTLS is free software: you can redistribute it and/or modify
 
2
 * Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
 
3
 * 2009, 2010  Free Software Foundation, Inc.
 
4
 *
 
5
 * This file is part of GnuTLS.
 
6
 *
 
7
 * GnuTLS is free software: you can redistribute it and/or modify
8
8
 * it under the terms of the GNU General Public License as published by
9
9
 * the Free Software Foundation, either version 3 of the License, or
10
10
 * (at your option) any later version.
11
11
 *
12
 
 * GNUTLS is distributed in the hope that it will be useful,
 
12
 * GnuTLS is distributed in the hope that it will be useful,
13
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
15
 * GNU General Public License for more details.
53
53
#define MAX_BUF 4096
54
54
 
55
55
/* global stuff here */
56
 
int resume, starttls, insecure;
 
56
int resume, starttls, insecure, rehandshake;
57
57
const char *hostname = NULL;
58
58
char *service;
59
59
int record_max_size;
318
318
 
319
319
}
320
320
 
321
 
 
 
321
static int
 
322
cert_verify_callback (gnutls_session_t session)
 
323
{
 
324
  int rc;
 
325
  unsigned int status;
 
326
 
 
327
  if (!x509_cafile && !pgp_keyring)
 
328
    return 0;
 
329
 
 
330
  rc = gnutls_certificate_verify_peers2 (session, &status);
 
331
  if (rc != 0 || status != 0)
 
332
    {
 
333
      printf ("*** Verifying server certificate failed...\n");
 
334
      if (!insecure)
 
335
        return -1;
 
336
    }
 
337
 
 
338
  return 0;
 
339
}
322
340
 
323
341
/* This callback should be associated with a session by calling
324
342
 * gnutls_certificate_client_set_retrieve_function( session, cert_callback),
365
383
 
366
384
  st->type = gnutls_certificate_type_get (session);
367
385
 
 
386
 
368
387
  st->ncerts = 0;
369
388
 
370
389
  if (st->type == GNUTLS_CRT_X509)
371
390
    {
 
391
      gnutls_sign_algorithm_t cert_algo, req_algo;
 
392
      int i, match = 0;
 
393
 
 
394
      if (x509_crt[0] != NULL)
 
395
        {
 
396
          ret = gnutls_x509_crt_get_signature_algorithm (x509_crt[0]);
 
397
          if (ret < 0)
 
398
            {
 
399
              /* error reading signature algorithm */
 
400
              return -1;
 
401
            }
 
402
          cert_algo = ret;
 
403
 
 
404
          i = 0;
 
405
          do
 
406
            {
 
407
              ret =
 
408
                gnutls_sign_algorithm_get_requested (session, i, &req_algo);
 
409
              if (ret >= 0 && cert_algo == req_algo)
 
410
                {
 
411
                  match = 1;
 
412
                  break;
 
413
                }
 
414
 
 
415
              /* server has not requested anything specific */
 
416
              if (i == 0 && ret == GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE)
 
417
                {
 
418
                  match = 1;
 
419
                  break;
 
420
                }
 
421
              i++;
 
422
            }
 
423
          while (ret >= 0);
 
424
 
 
425
          if (match == 0)
 
426
            {
 
427
              printf
 
428
                ("- Could not find a suitable certificate to send to server\n");
 
429
              return -1;
 
430
            }
 
431
        }
 
432
 
372
433
      if (x509_crt != NULL && x509_key != NULL)
373
434
        {
374
435
          st->ncerts = x509_crt_size;
450
511
  gnutls_credentials_set (session, GNUTLS_CRD_CERTIFICATE, xcred);
451
512
 
452
513
  gnutls_certificate_client_set_retrieve_function (xcred, cert_callback);
 
514
  gnutls_certificate_set_verify_function (xcred, cert_verify_callback);
 
515
  gnutls_certificate_set_verify_flags (xcred, 0);
453
516
 
454
517
  /* send the fingerprint */
455
518
#ifdef ENABLE_OPENPGP
476
539
                                info.opaque_prf_input);
477
540
#endif
478
541
 
 
542
#ifdef ENABLE_SESSION_TICKET
 
543
  if (!info.noticket)
 
544
    gnutls_session_ticket_enable_client (session);
 
545
#endif
 
546
 
479
547
  return session;
480
548
}
481
549
 
558
626
  struct timeval tv;
559
627
  int user_term = 0, retval = 0;
560
628
  socket_st hd;
 
629
  ssize_t bytes;
561
630
 
562
631
  set_program_name (argv[0]);
563
632
 
564
633
  gcry_control (GCRYCTL_ENABLE_QUICK_RANDOM, 0);
565
634
 
566
635
#ifdef gcry_fips_mode_active
 
636
  /* Libgcrypt manual says that gcry_version_check must be called
 
637
     before calling gcry_fips_mode_active. */
 
638
  gcry_check_version (NULL);
567
639
  if (gcry_fips_mode_active ())
568
640
    {
569
641
      ret = gnutls_register_md5_handler ();
674
746
     programs to search for when gnutls-cli has reached this point. */
675
747
  printf ("\n- Simple Client Mode:\n\n");
676
748
 
 
749
  if (rehandshake)
 
750
    {
 
751
      ret = do_handshake (&hd);
 
752
 
 
753
      if (ret < 0)
 
754
        {
 
755
          fprintf (stderr, "*** ReHandshake has failed\n");
 
756
          gnutls_perror (ret);
 
757
          gnutls_deinit (hd.session);
 
758
          return 1;
 
759
        }
 
760
      else
 
761
        {
 
762
          printf ("- ReHandshake was completed\n");
 
763
        }
 
764
    }
 
765
 
677
766
#ifndef _WIN32
678
767
  signal (SIGALRM, &starttls_alarm);
679
768
#endif
680
769
 
 
770
  fflush(stdout);
 
771
  fflush(stderr);
 
772
 
681
773
  /* do not buffer */
682
774
#if !(defined _WIN32 || defined __WIN32__)
683
775
  setbuf (stdin, NULL);
722
814
 
723
815
          if (ret == 0)
724
816
            {
725
 
              printf ("- Peer has closed the GNUTLS connection\n");
 
817
              printf ("- Peer has closed the GnuTLS connection\n");
726
818
              break;
727
819
            }
728
820
          else if (handle_error (&hd, ret) < 0 && user_term == 0)
749
841
 
750
842
      if (FD_ISSET (fileno (stdin), &rset))
751
843
        {
752
 
          if (fgets (buffer, MAX_BUF, stdin) == NULL)
 
844
          if ((bytes = read (fileno (stdin), buffer, MAX_BUF - 1)) <= 0)
753
845
            {
754
846
              if (hd.secure == 0)
755
847
                {
779
871
            {
780
872
              char *b = strchr (buffer, '\n');
781
873
              if (b != NULL)
782
 
                strcpy (b, "\r\n");
 
874
                {
 
875
                  strcpy (b, "\r\n");
 
876
                  bytes++;
 
877
                }
783
878
            }
784
879
 
785
 
          ret = socket_send (&hd, buffer, strlen (buffer));
 
880
          ret = socket_send (&hd, buffer, bytes);
786
881
 
787
882
          if (ret > 0)
788
883
            {
838
933
  print_cert = info.print_cert;
839
934
  starttls = info.starttls;
840
935
  resume = info.resume;
 
936
  rehandshake = info.rehandshake;
841
937
  insecure = info.insecure;
842
938
  service = info.port;
843
939
  record_max_size = info.record_size;
945
1041
      /* print some information */
946
1042
      print_info (socket->session, socket->hostname, info.insecure);
947
1043
 
948
 
      if ((x509_cafile || pgp_keyring) && !insecure)
949
 
        {
950
 
          int rc;
951
 
          unsigned int status;
952
 
 
953
 
          /* abort if verification fail  */
954
 
          rc = gnutls_certificate_verify_peers2 (socket->session, &status);
955
 
          if (rc != 0 || status != 0)
956
 
            {
957
 
              printf ("*** Verifying server certificate failed...\n");
958
 
              exit (1);
959
 
            }
960
 
        }
961
1044
 
962
1045
      socket->secure = 1;
963
1046
 
964
1047
    }
 
1048
  else
 
1049
    {
 
1050
      gnutls_alert_send_appropriate (socket->session, ret);
 
1051
      shutdown (socket->fd, SHUT_RDWR);
 
1052
    }
965
1053
  return ret;
966
1054
}
967
1055