2 * Copyright (C) 2000-2012 Free Software Foundation, Inc.
4 * This file is part of GnuTLS.
6 * GnuTLS is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * GnuTLS is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include <sys/types.h>
30 # include <sys/socket.h>
32 # include <ws2tcpip.h>
34 #include <sys/select.h>
41 #include <gnutls/gnutls.h>
42 #include <gnutls/abstract.h>
43 #include <gnutls/dtls.h>
44 #include <gnutls/x509.h>
45 #include <gnutls/openpgp.h>
46 #include <gnutls/pkcs11.h>
48 /* Gnulib portability files. */
50 #include <version-etc.h>
51 #include <read-file.h>
56 #include "benchmark.h"
63 #include <ocsptool-common.h>
67 /* global stuff here */
68 int resume
, starttls
, insecure
, rehandshake
, udp
, mtu
;
69 const char *hostname
= NULL
;
70 const char *service
= NULL
;
74 unsigned int verbose
= 0;
77 #define DEFAULT_CA_FILE "/etc/ssl/certs/ca-certificates.crt"
79 const char *srp_passwd
= NULL
;
80 const char *srp_username
= NULL
;
81 const char *pgp_keyfile
= NULL
;
82 const char *pgp_certfile
= NULL
;
83 const char *pgp_keyring
= NULL
;
84 const char *x509_keyfile
= NULL
;
85 const char *x509_certfile
= NULL
;
86 const char *x509_cafile
= NULL
;
87 const char *x509_crlfile
= NULL
;
89 static int disable_extensions
;
90 static const char * priorities
= NULL
;
92 const char *psk_username
= NULL
;
93 gnutls_datum_t psk_key
= { NULL
, 0 };
95 static gnutls_srp_client_credentials_t srp_cred
;
96 static gnutls_psk_client_credentials_t psk_cred
;
97 static gnutls_anon_client_credentials_t anon_cred
;
98 static gnutls_certificate_credentials_t xcred
;
100 /* end of global stuff */
104 static void check_rehandshake (socket_st
* socket
, int ret
);
105 static int do_handshake (socket_st
* socket
);
106 static void init_global_tls_stuff (void);
107 static int cert_verify_ocsp (gnutls_session_t session
);
109 /* Helper functions to load a certificate and key
112 static gnutls_datum_t
113 load_file (const char *file
)
115 gnutls_datum_t loaded_file
= { NULL
, 0 };
118 loaded_file
.data
= (void*)read_binary_file (file
, &length
);
119 if (loaded_file
.data
)
120 loaded_file
.size
= (unsigned int) length
;
126 unload_file (gnutls_datum_t
* data
)
132 static unsigned int x509_crt_size
;
133 static gnutls_pcert_st x509_crt
[MAX_CRT
];
134 static gnutls_privkey_t x509_key
= NULL
;
136 static gnutls_pcert_st pgp_crt
;
137 static gnutls_privkey_t pgp_key
= NULL
;
140 get_keyid (gnutls_openpgp_keyid_t keyid
, const char *str
)
142 size_t keyid_size
= sizeof (keyid
);
144 if (strlen (str
) != 16)
147 "The OpenPGP subkey ID has to be 16 hexadecimal characters.\n");
151 if (gnutls_hex2bin (str
, strlen (str
), keyid
, &keyid_size
) < 0)
153 fprintf (stderr
, "Error converting hex string: %s.\n", str
);
160 /* Load the certificate and the private key.
165 unsigned int crt_num
;
168 gnutls_datum_t data
= { NULL
, 0 };
169 gnutls_x509_crt_t crt_list
[MAX_CRT
];
171 gnutls_pkcs11_privkey_t pkcs11_key
;
173 gnutls_x509_privkey_t tmp_key
;
174 unsigned char keyid
[GNUTLS_OPENPGP_KEYID_SIZE
];
176 if (x509_certfile
!= NULL
&& x509_keyfile
!= NULL
)
179 if (strncmp (x509_certfile
, "pkcs11:", 7) == 0)
182 gnutls_x509_crt_init (&crt_list
[0]);
185 gnutls_x509_crt_import_pkcs11_url (crt_list
[0], x509_certfile
, 0);
187 if (ret
== GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE
)
189 gnutls_x509_crt_import_pkcs11_url (crt_list
[0], x509_certfile
,
190 GNUTLS_PKCS11_OBJ_FLAG_LOGIN
);
194 fprintf (stderr
, "*** Error loading cert file.\n");
200 #endif /* ENABLE_PKCS11 */
203 data
= load_file (x509_certfile
);
204 if (data
.data
== NULL
)
206 fprintf (stderr
, "*** Error loading cert file.\n");
212 gnutls_x509_crt_list_import (crt_list
, &crt_num
, &data
,
214 GNUTLS_X509_CRT_LIST_IMPORT_FAIL_IF_EXCEED
);
217 if (ret
== GNUTLS_E_SHORT_MEMORY_BUFFER
)
220 "*** Error loading cert file: Too many certs %d\n",
227 "*** Error loading cert file: %s\n",
228 gnutls_strerror (ret
));
235 for (i
=0;i
<x509_crt_size
;i
++)
237 ret
= gnutls_pcert_import_x509(&x509_crt
[i
], crt_list
[i
], 0);
240 fprintf(stderr
, "*** Error importing crt to pcert: %s\n",
241 gnutls_strerror(ret
));
244 gnutls_x509_crt_deinit(crt_list
[i
]);
249 ret
= gnutls_privkey_init(&x509_key
);
252 fprintf (stderr
, "*** Error initializing key: %s\n",
253 gnutls_strerror (ret
));
258 if (strncmp (x509_keyfile
, "pkcs11:", 7) == 0)
260 gnutls_pkcs11_privkey_init (&pkcs11_key
);
263 gnutls_pkcs11_privkey_import_url (pkcs11_key
, x509_keyfile
, 0);
266 fprintf (stderr
, "*** Error loading url: %s\n",
267 gnutls_strerror (ret
));
271 ret
= gnutls_privkey_import_pkcs11( x509_key
, pkcs11_key
, GNUTLS_PRIVKEY_IMPORT_AUTO_RELEASE
);
274 fprintf (stderr
, "*** Error loading url: %s\n",
275 gnutls_strerror (ret
));
280 #endif /* ENABLE_PKCS11 */
282 data
= load_file (x509_keyfile
);
283 if (data
.data
== NULL
)
285 fprintf (stderr
, "*** Error loading key file.\n");
289 gnutls_x509_privkey_init (&tmp_key
);
292 gnutls_x509_privkey_import (tmp_key
, &data
, x509ctype
);
295 fprintf (stderr
, "*** Error loading key file: %s\n",
296 gnutls_strerror (ret
));
300 ret
= gnutls_privkey_import_x509( x509_key
, tmp_key
, GNUTLS_PRIVKEY_IMPORT_AUTO_RELEASE
);
303 fprintf (stderr
, "*** Error loading url: %s\n",
304 gnutls_strerror (ret
));
311 fprintf (stdout
, "Processed %d client X.509 certificates...\n",
316 #ifdef ENABLE_OPENPGP
317 if (HAVE_OPT(PGPSUBKEY
))
319 get_keyid (keyid
, OPT_ARG(PGPSUBKEY
));
322 if (pgp_certfile
!= NULL
&& pgp_keyfile
!= NULL
)
324 gnutls_openpgp_crt_t tmp_pgp_crt
;
326 data
= load_file (pgp_certfile
);
327 if (data
.data
== NULL
)
329 fprintf (stderr
, "*** Error loading PGP cert file.\n");
333 gnutls_openpgp_crt_init (&tmp_pgp_crt
);
336 gnutls_pcert_import_openpgp_raw (&pgp_crt
, &data
, GNUTLS_OPENPGP_FMT_BASE64
, HAVE_OPT(PGPSUBKEY
)?keyid
:NULL
, 0);
340 "*** Error loading PGP cert file: %s\n",
341 gnutls_strerror (ret
));
347 ret
= gnutls_privkey_init(&pgp_key
);
350 fprintf (stderr
, "*** Error initializing key: %s\n",
351 gnutls_strerror (ret
));
356 if (strncmp (pgp_keyfile
, "pkcs11:", 7) == 0)
358 gnutls_pkcs11_privkey_init (&pkcs11_key
);
360 ret
= gnutls_pkcs11_privkey_import_url (pkcs11_key
, pgp_keyfile
, 0);
363 fprintf (stderr
, "*** Error loading url: %s\n",
364 gnutls_strerror (ret
));
368 ret
= gnutls_privkey_import_pkcs11( pgp_key
, pkcs11_key
, GNUTLS_PRIVKEY_IMPORT_AUTO_RELEASE
);
371 fprintf (stderr
, "*** Error loading url: %s\n",
372 gnutls_strerror (ret
));
377 #endif /* ENABLE_PKCS11 */
379 gnutls_openpgp_privkey_t tmp_pgp_key
;
381 data
= load_file (pgp_keyfile
);
382 if (data
.data
== NULL
)
384 fprintf (stderr
, "*** Error loading PGP key file.\n");
388 gnutls_openpgp_privkey_init (&tmp_pgp_key
);
391 gnutls_openpgp_privkey_import (tmp_pgp_key
, &data
,
392 GNUTLS_OPENPGP_FMT_BASE64
, NULL
,
397 "*** Error loading PGP key file: %s\n",
398 gnutls_strerror (ret
));
402 if (HAVE_OPT(PGPSUBKEY
))
405 gnutls_openpgp_privkey_set_preferred_key_id (tmp_pgp_key
, keyid
);
409 "*** Error setting preferred sub key id (%s): %s\n",
410 OPT_ARG(PGPSUBKEY
), gnutls_strerror (ret
));
415 ret
= gnutls_privkey_import_openpgp( pgp_key
, tmp_pgp_key
, GNUTLS_PRIVKEY_IMPORT_AUTO_RELEASE
);
418 fprintf (stderr
, "*** Error loading url: %s\n",
419 gnutls_strerror (ret
));
427 fprintf (stdout
, "Processed 1 client PGP certificate...\n");
433 #define IS_NEWLINE(x) ((x[0] == '\n') || (x[0] == '\r'))
435 read_yesno (const char *input_str
)
439 fputs (input_str
, stderr
);
440 if (fgets (input
, sizeof (input
), stdin
) == NULL
)
443 if (IS_NEWLINE(input
))
446 if (input
[0] == 'y' || input
[0] == 'Y')
452 /* converts a textual service or port to
455 static const char* port_to_service(const char* sport
)
461 if (port
== 0) return sport
;
465 sr
= getservbyport(port
, udp
?"udp":"tcp");
468 fprintf(stderr
, "Warning: getservbyport() failed. Using port number as service.\n");
476 cert_verify_callback (gnutls_session_t session
)
479 unsigned int status
= 0;
480 int ssh
= ENABLED_OPT(TOFU
);
481 const char* txt_service
;
483 if (!x509_cafile
&& !pgp_keyring
)
486 rc
= cert_verify(session
, hostname
);
489 printf ("*** Verifying server certificate failed...\n");
490 if (!insecure
&& !ssh
)
493 else if (ENABLED_OPT(OCSP
))
494 { /* off-line verification succeeded. Try OCSP */
495 rc
= cert_verify_ocsp(session
);
498 printf ("*** Verifying (with OCSP) server certificate failed...\n");
499 if (!insecure
&& !ssh
)
503 printf("*** OCSP response ignored\n");
506 if (ssh
) /* try ssh auth */
508 unsigned int list_size
;
509 const gnutls_datum_t
* cert
;
511 cert
= gnutls_certificate_get_peers(session
, &list_size
);
514 fprintf(stderr
, "Cannot obtain peer's certificate!\n");
518 txt_service
= port_to_service(service
);
520 rc
= gnutls_verify_stored_pubkey(NULL
, NULL
, hostname
, txt_service
,
521 GNUTLS_CRT_X509
, cert
, 0);
522 if (rc
== GNUTLS_E_NO_CERTIFICATE_FOUND
)
524 print_cert_info_compact(session
);
525 fprintf(stderr
, "Host %s (%s) has never been contacted before.\n", hostname
, txt_service
);
527 fprintf(stderr
, "Its certificate is valid for %s.\n", hostname
);
529 rc
= read_yesno("Are you sure you want to trust it? (y/N): ");
533 else if (rc
== GNUTLS_E_CERTIFICATE_KEY_MISMATCH
)
535 print_cert_info_compact(session
);
536 fprintf(stderr
, "Warning: host %s is known and it is associated with a different key.\n", hostname
);
537 fprintf(stderr
, "It might be that the server has multiple keys, or an attacker replaced the key to eavesdrop this connection .\n");
539 fprintf(stderr
, "Its certificate is valid for %s.\n", hostname
);
541 rc
= read_yesno("Do you trust the received key? (y/N): ");
547 fprintf(stderr
, "gnutls_verify_stored_pubkey: %s\n", gnutls_strerror(rc
));
553 rc
= gnutls_store_pubkey(NULL
, NULL
, hostname
, txt_service
,
554 GNUTLS_CRT_X509
, cert
, 0, 0);
556 fprintf(stderr
, "Could not store key: %s\n", gnutls_strerror(rc
));
563 /* This callback should be associated with a session by calling
564 * gnutls_certificate_client_set_retrieve_function( session, cert_callback),
565 * before a handshake.
569 cert_callback (gnutls_session_t session
,
570 const gnutls_datum_t
* req_ca_rdn
, int nreqs
,
571 const gnutls_pk_algorithm_t
* sign_algos
,
572 int sign_algos_length
, gnutls_pcert_st
**pcert
,
573 unsigned int *pcert_length
, gnutls_privkey_t
* pkey
)
576 int i
, ret
, cert_type
;
581 /* Print the server's trusted CAs
584 printf ("- Server's trusted authorities:\n");
586 printf ("- Server did not send us any trusted authorities names.\n");
588 /* print the names (if any) */
589 for (i
= 0; i
< nreqs
; i
++)
591 len
= sizeof (issuer_dn
);
592 ret
= gnutls_x509_rdn_get (&req_ca_rdn
[i
], issuer_dn
, &len
);
595 printf (" [%d]: ", i
);
596 printf ("%s\n", issuer_dn
);
601 /* Select a certificate and return it.
602 * The certificate must be of any of the "sign algorithms"
603 * supported by the server.
606 cert_type
= gnutls_certificate_type_get (session
);
610 if (cert_type
== GNUTLS_CRT_X509
)
612 if (x509_crt_size
> 0)
614 if (x509_key
!= NULL
)
620 printf ("- Could not find a suitable key to send to server\n");
624 *pcert_length
= x509_crt_size
;
629 else if (cert_type
== GNUTLS_CRT_OPENPGP
)
640 printf ("- Successfully sent %u certificate(s) to server.\n", *pcert_length
);
645 /* initializes a gnutls_session_t with some defaults.
647 static gnutls_session_t
648 init_tls_session (const char *hostname
)
652 gnutls_session_t session
;
654 if (priorities
== NULL
)
655 priorities
= "NORMAL";
659 gnutls_init (&session
, GNUTLS_CLIENT
|GNUTLS_DATAGRAM
);
661 gnutls_dtls_set_mtu(session
, mtu
);
664 gnutls_init (&session
, GNUTLS_CLIENT
);
666 if ((ret
= gnutls_priority_set_direct (session
, priorities
, &err
)) < 0)
668 if (ret
== GNUTLS_E_INVALID_REQUEST
) fprintf (stderr
, "Syntax error at: %s\n", err
);
670 fprintf(stderr
, "Error in priorities: %s\n", gnutls_strerror(ret
));
674 /* allow the use of private ciphersuites.
676 if (disable_extensions
== 0)
678 if (!isdigit(hostname
[0]) && strchr(hostname
, ':') == 0)
679 gnutls_server_name_set (session
, GNUTLS_NAME_DNS
, hostname
,
683 gnutls_dh_set_prime_bits (session
, 512);
685 gnutls_credentials_set (session
, GNUTLS_CRD_ANON
, anon_cred
);
687 gnutls_credentials_set (session
, GNUTLS_CRD_SRP
, srp_cred
);
689 gnutls_credentials_set (session
, GNUTLS_CRD_PSK
, psk_cred
);
690 gnutls_credentials_set (session
, GNUTLS_CRD_CERTIFICATE
, xcred
);
692 gnutls_certificate_set_retrieve_function2 (xcred
, cert_callback
);
693 gnutls_certificate_set_verify_function (xcred
, cert_verify_callback
);
694 gnutls_certificate_set_verify_flags (xcred
, 0);
696 /* send the fingerprint */
697 #ifdef ENABLE_OPENPGP
698 if (fingerprint
!= 0)
699 gnutls_openpgp_send_cert (session
, GNUTLS_OPENPGP_CERT_FINGERPRINT
);
702 /* use the max record size extension */
703 if (record_max_size
> 0 && disable_extensions
== 0)
705 if (gnutls_record_set_max_size (session
, record_max_size
) < 0)
708 "Cannot set the maximum record size to %d.\n",
710 fprintf (stderr
, "Possible values: 512, 1024, 2048, 4096.\n");
715 #ifdef ENABLE_SESSION_TICKET
716 if (disable_extensions
== 0 && !HAVE_OPT(NOTICKET
)t
)
717 gnutls_session_ticket_enable_client (session
);
723 static void cmd_parser (int argc
, char **argv
);
725 /* Returns zero if the error code was successfully handled.
728 handle_error (socket_st
* hd
, int err
)
731 const char *err_type
, *str
;
733 if (err
>= 0 || err
== GNUTLS_E_AGAIN
|| err
== GNUTLS_E_INTERRUPTED
)
736 if (gnutls_error_is_fatal (err
) == 0)
739 err_type
= "Non fatal";
747 str
= gnutls_strerror (err
);
750 fprintf (stderr
, "*** %s error: %s\n", err_type
, str
);
752 if (err
== GNUTLS_E_WARNING_ALERT_RECEIVED
753 || err
== GNUTLS_E_FATAL_ALERT_RECEIVED
)
755 alert
= gnutls_alert_get (hd
->session
);
756 str
= gnutls_alert_get_name (alert
);
759 printf ("*** Received alert [%d]: %s\n", alert
, str
);
762 check_rehandshake (hd
, err
);
767 int starttls_alarmed
= 0;
771 starttls_alarm (int signum
)
773 starttls_alarmed
= 1;
778 tls_log_func (int level
, const char *str
)
780 fprintf (stderr
, "|<%d>| %s", level
, str
);
783 #define IN_KEYBOARD 1
786 /* returns IN_KEYBOARD for keyboard input and IN_NET for network input
788 static int check_net_or_keyboard_input(socket_st
* hd
)
798 FD_SET (hd
->fd
, &rset
);
801 FD_SET (fileno (stdin
), &rset
);
802 maxfd
= MAX (fileno (stdin
), hd
->fd
);
808 tv
.tv_usec
= 50 * 1000;
811 if (gnutls_record_check_pending(hd
->session
))
814 err
= select (maxfd
+ 1, &rset
, NULL
, NULL
, &tv
);
818 if (FD_ISSET (hd
->fd
, &rset
))
824 state
= WaitForSingleObject(GetStdHandle(STD_INPUT_HANDLE
), 200);
826 if (state
== WAIT_OBJECT_0
)
830 if (FD_ISSET (fileno (stdin
), &rset
))
840 main (int argc
, char **argv
)
844 char buffer
[MAX_BUF
+ 1];
845 char *session_data
= NULL
;
846 char *session_id
= NULL
;
847 size_t session_data_size
;
848 size_t session_id_size
= 0;
849 int user_term
= 0, retval
= 0;
853 set_program_name (argv
[0]);
854 cmd_parser (argc
, argv
);
856 gnutls_global_set_log_function (tls_log_func
);
857 gnutls_global_set_log_level (OPT_VALUE_DEBUG
);
859 if ((ret
= gnutls_global_init ()) < 0)
861 fprintf (stderr
, "global_init: %s\n", gnutls_strerror (ret
));
869 if (hostname
== NULL
)
871 fprintf (stderr
, "No hostname given\n");
877 init_global_tls_stuff ();
879 socket_open (&hd
, hostname
, service
, udp
);
880 socket_connect (&hd
);
882 hd
.session
= init_tls_session (hostname
);
884 goto after_handshake
;
886 for (i
= 0; i
< 2; i
++)
892 hd
.session
= init_tls_session (hostname
);
893 gnutls_session_set_data (hd
.session
, session_data
,
898 ret
= do_handshake (&hd
);
902 fprintf (stderr
, "*** Handshake has failed\n");
904 gnutls_deinit (hd
.session
);
909 printf ("- Handshake was completed\n");
910 if (gnutls_session_is_resumed (hd
.session
) != 0)
911 printf ("*** This is a resumed session\n");
914 if (resume
!= 0 && i
== 0)
917 gnutls_session_get_data (hd
.session
, NULL
, &session_data_size
);
918 session_data
= malloc (session_data_size
);
920 gnutls_session_get_data (hd
.session
, session_data
,
923 gnutls_session_get_id (hd
.session
, NULL
, &session_id_size
);
925 session_id
= malloc (session_id_size
);
926 gnutls_session_get_id (hd
.session
, session_id
, &session_id_size
);
928 printf ("- Disconnecting\n");
932 ("\n\n- Connecting again- trying to resume previous session\n");
933 socket_open (&hd
, hostname
, service
, udp
);
934 socket_connect (&hd
);
944 /* Warning! Do not touch this text string, it is used by external
945 programs to search for when gnutls-cli has reached this point. */
946 printf ("\n- Simple Client Mode:\n\n");
950 ret
= do_handshake (&hd
);
954 fprintf (stderr
, "*** ReHandshake has failed\n");
956 gnutls_deinit (hd
.session
);
961 printf ("- ReHandshake was completed\n");
966 signal (SIGALRM
, &starttls_alarm
);
973 #if !(defined _WIN32 || defined __WIN32__)
974 setbuf (stdin
, NULL
);
976 setbuf (stdout
, NULL
);
977 setbuf (stderr
, NULL
);
981 if (starttls_alarmed
&& !hd
.secure
)
983 /* Warning! Do not touch this text string, it is used by
984 external programs to search for when gnutls-cli has
985 reached this point. */
986 fprintf (stderr
, "*** Starting TLS handshake\n");
987 ret
= do_handshake (&hd
);
990 fprintf (stderr
, "*** Handshake has failed\n");
997 inp
= check_net_or_keyboard_input(&hd
);
1001 memset (buffer
, 0, MAX_BUF
+ 1);
1002 ret
= socket_recv (&hd
, buffer
, MAX_BUF
);
1006 printf ("- Peer has closed the GnuTLS connection\n");
1009 else if (handle_error (&hd
, ret
) < 0 && user_term
== 0)
1012 "*** Server has terminated the connection abnormally.\n");
1019 printf ("- Received[%d]: ", ret
);
1020 for (ii
= 0; ii
< ret
; ii
++)
1022 fputc (buffer
[ii
], stdout
);
1031 if (inp
== IN_KEYBOARD
)
1033 if ((bytes
= read (fileno (stdin
), buffer
, MAX_BUF
- 1)) <= 0)
1037 /* Warning! Do not touch this text string, it is
1038 used by external programs to search for when
1039 gnutls-cli has reached this point. */
1040 fprintf (stderr
, "*** Starting TLS handshake\n");
1041 ret
= do_handshake (&hd
);
1045 fprintf (stderr
, "*** Handshake has failed\n");
1062 char *b
= strchr (buffer
, '\n');
1070 ret
= socket_send (&hd
, buffer
, bytes
);
1075 printf ("- Sent: %d bytes\n", ret
);
1078 handle_error (&hd
, ret
);
1086 gnutls_deinit (hd
.session
);
1090 gnutls_srp_free_client_credentials (srp_cred
);
1094 gnutls_psk_free_client_credentials (psk_cred
);
1097 gnutls_certificate_free_credentials (xcred
);
1100 gnutls_anon_free_client_credentials (anon_cred
);
1103 gnutls_global_deinit ();
1109 cmd_parser (int argc
, char **argv
)
1111 const char* rest
= NULL
;
1113 int optct
= optionProcess( &gnutls_cliOptions
, argc
, argv
);
1117 if (rest
== NULL
&& argc
> 0)
1120 if (HAVE_OPT(BENCHMARK_CIPHERS
))
1122 benchmark_cipher(1, OPT_VALUE_DEBUG
);
1126 if (HAVE_OPT(BENCHMARK_SOFT_CIPHERS
))
1128 benchmark_cipher(0, OPT_VALUE_DEBUG
);
1132 if (HAVE_OPT(BENCHMARK_TLS
))
1134 benchmark_tls(OPT_VALUE_DEBUG
);
1138 if (HAVE_OPT(PRIORITY
))
1140 priorities
= OPT_ARG(PRIORITY
);
1142 verbose
= HAVE_OPT( VERBOSE
);
1146 print_cert
= HAVE_OPT( PRINT_CERT
);
1150 print_list(priorities
, verbose
);
1154 disable_extensions
= HAVE_OPT( DISABLE_EXTENSIONS
);
1155 starttls
= HAVE_OPT(STARTTLS
);
1156 resume
= HAVE_OPT(RESUME
);
1157 rehandshake
= HAVE_OPT(REHANDSHAKE
);
1158 insecure
= HAVE_OPT(INSECURE
);
1160 udp
= HAVE_OPT(UDP
);
1161 mtu
= OPT_VALUE_MTU
;
1165 service
= OPT_ARG(PORT
);
1172 record_max_size
= OPT_VALUE_RECORDSIZE
;
1173 fingerprint
= HAVE_OPT(FINGERPRINT
);
1175 if (HAVE_OPT(X509FMTDER
))
1176 x509ctype
= GNUTLS_X509_FMT_DER
;
1178 x509ctype
= GNUTLS_X509_FMT_PEM
;
1180 if (HAVE_OPT(SRPUSERNAME
))
1181 srp_username
= OPT_ARG(SRPUSERNAME
);
1183 if (HAVE_OPT(SRPPASSWD
))
1184 srp_passwd
= OPT_ARG(SRPPASSWD
);
1186 if (HAVE_OPT(X509CAFILE
))
1187 x509_cafile
= OPT_ARG(X509CAFILE
);
1190 if (access(DEFAULT_CA_FILE
, R_OK
) == 0)
1191 x509_cafile
= DEFAULT_CA_FILE
;
1194 if (HAVE_OPT(X509CRLFILE
))
1195 x509_crlfile
= OPT_ARG(X509CRLFILE
);
1197 if (HAVE_OPT(X509KEYFILE
))
1198 x509_keyfile
= OPT_ARG(X509KEYFILE
);
1200 if (HAVE_OPT(X509CERTFILE
))
1201 x509_certfile
= OPT_ARG(X509CERTFILE
);
1203 if (HAVE_OPT(PGPKEYFILE
))
1204 pgp_keyfile
= OPT_ARG(PGPKEYFILE
);
1206 if (HAVE_OPT(PGPCERTFILE
))
1207 pgp_certfile
= OPT_ARG(PGPCERTFILE
);
1209 if (HAVE_OPT(PSKUSERNAME
))
1210 psk_username
= OPT_ARG(PSKUSERNAME
);
1212 if (HAVE_OPT(PSKKEY
))
1214 psk_key
.data
= (unsigned char *) OPT_ARG(PSKKEY
);
1215 psk_key
.size
= strlen (OPT_ARG(PSKKEY
));
1220 if (HAVE_OPT(PGPKEYRING
))
1221 pgp_keyring
= OPT_ARG(PGPKEYRING
);
1223 crlf
= HAVE_OPT(CRLF
);
1228 if (hostname
== NULL
)
1230 fprintf(stderr
, "No hostname specified\n");
1235 void cli_version (void);
1240 const char *p
= PACKAGE_NAME
;
1241 if (strcmp (gnutls_check_version (NULL
), PACKAGE_VERSION
) != 0)
1243 version_etc (stdout
, program_name
, p
, gnutls_check_version (NULL
),
1244 "Nikos Mavrogiannopoulos", (char *) NULL
);
1249 check_rehandshake (socket_st
* socket
, int ret
)
1251 if (socket
->secure
&& ret
== GNUTLS_E_REHANDSHAKE
)
1253 /* There is a race condition here. If application
1254 * data is sent after the rehandshake request,
1255 * the server thinks we ignored his request.
1256 * This is a bad design of this client.
1258 printf ("*** Received rehandshake request\n");
1259 /* gnutls_alert_send( session, GNUTLS_AL_WARNING, GNUTLS_A_NO_RENEGOTIATION); */
1261 ret
= do_handshake (socket
);
1265 printf ("*** Rehandshake was performed.\n");
1269 printf ("*** Rehandshake Failed.\n");
1276 do_handshake (socket_st
* socket
)
1280 gnutls_transport_set_ptr (socket
->session
,
1281 (gnutls_transport_ptr_t
)
1282 gl_fd_to_handle (socket
->fd
));
1285 ret
= gnutls_handshake (socket
->session
);
1289 handle_error (socket
, ret
);
1292 while (ret
< 0 && gnutls_error_is_fatal (ret
) == 0);
1296 /* print some information */
1297 print_info (socket
->session
, print_cert
);
1302 gnutls_alert_send_appropriate (socket
->session
, ret
);
1303 shutdown (socket
->fd
, SHUT_RDWR
);
1309 srp_username_callback (gnutls_session_t session
,
1310 char **username
, char **password
)
1312 if (srp_username
== NULL
|| srp_passwd
== NULL
)
1317 *username
= gnutls_strdup (srp_username
);
1318 *password
= gnutls_strdup (srp_passwd
);
1324 psk_callback (gnutls_session_t session
, char **username
, gnutls_datum_t
* key
)
1326 const char *hint
= gnutls_psk_client_get_hint (session
);
1333 printf ("- PSK client callback. ");
1335 printf ("PSK hint '%s'\n", hint
);
1337 printf ("No PSK hint\n");
1339 if (HAVE_OPT(PSKUSERNAME
))
1340 *username
= gnutls_strdup (OPT_ARG(PSKUSERNAME
));
1346 printf ("Enter PSK identity: ");
1348 getline (&tmp
, &n
, stdin
);
1352 fprintf (stderr
, "No username given, aborting...\n");
1353 return GNUTLS_E_INSUFFICIENT_CREDENTIALS
;
1356 if (tmp
[strlen (tmp
) - 1] == '\n')
1357 tmp
[strlen (tmp
) - 1] = '\0';
1358 if (tmp
[strlen (tmp
) - 1] == '\r')
1359 tmp
[strlen (tmp
) - 1] = '\0';
1361 *username
= gnutls_strdup (tmp
);
1365 return GNUTLS_E_MEMORY_ERROR
;
1367 passwd
= getpass ("Enter key: ");
1370 fprintf (stderr
, "No key given, aborting...\n");
1371 return GNUTLS_E_INSUFFICIENT_CREDENTIALS
;
1374 tmp
.data
= (void*)passwd
;
1375 tmp
.size
= strlen (passwd
);
1377 res_size
= tmp
.size
/ 2 + 1;
1378 rawkey
= gnutls_malloc (res_size
);
1380 return GNUTLS_E_MEMORY_ERROR
;
1382 ret
= gnutls_hex_decode (&tmp
, rawkey
, &res_size
);
1385 fprintf (stderr
, "Error deriving password: %s\n",
1386 gnutls_strerror (ret
));
1387 gnutls_free (*username
);
1391 key
->data
= (void*)rawkey
;
1392 key
->size
= res_size
;
1394 if (HAVE_OPT(DEBUG
))
1397 res_size
= sizeof (hexkey
);
1398 gnutls_hex_encode (key
, hexkey
, &res_size
);
1399 fprintf (stderr
, "PSK username: %s\n", *username
);
1400 fprintf (stderr
, "PSK hint: %s\n", hint
);
1401 fprintf (stderr
, "PSK key: %s\n", hexkey
);
1408 init_global_tls_stuff (void)
1413 if (gnutls_certificate_allocate_credentials (&xcred
) < 0)
1415 fprintf (stderr
, "Certificate allocation memory error\n");
1419 if (x509_cafile
!= NULL
)
1421 ret
= gnutls_certificate_set_x509_trust_file (xcred
,
1422 x509_cafile
, x509ctype
);
1425 fprintf (stderr
, "Error setting the x509 trust file\n");
1429 printf ("Processed %d CA certificate(s).\n", ret
);
1432 if (x509_crlfile
!= NULL
)
1434 ret
= gnutls_certificate_set_x509_crl_file (xcred
, x509_crlfile
,
1438 fprintf (stderr
, "Error setting the x509 CRL file\n");
1442 printf ("Processed %d CRL(s).\n", ret
);
1448 #ifdef ENABLE_OPENPGP
1449 if (pgp_keyring
!= NULL
)
1452 gnutls_certificate_set_openpgp_keyring_file (xcred
, pgp_keyring
,
1453 GNUTLS_OPENPGP_FMT_BASE64
);
1456 fprintf (stderr
, "Error setting the OpenPGP keyring file\n");
1462 if (srp_username
&& srp_passwd
)
1465 if (gnutls_srp_allocate_client_credentials (&srp_cred
) < 0)
1467 fprintf (stderr
, "SRP authentication error\n");
1470 gnutls_srp_set_client_credentials_function (srp_cred
,
1471 srp_username_callback
);
1477 if (gnutls_psk_allocate_client_credentials (&psk_cred
) < 0)
1479 fprintf (stderr
, "PSK authentication error\n");
1482 if (psk_username
&& psk_key
.data
)
1484 ret
= gnutls_psk_set_client_credentials (psk_cred
,
1485 psk_username
, &psk_key
,
1486 GNUTLS_PSK_KEY_HEX
);
1489 fprintf (stderr
, "Error setting the PSK credentials: %s\n",
1490 gnutls_strerror (ret
));
1494 gnutls_psk_set_client_credentials_function (psk_cred
, psk_callback
);
1499 if (gnutls_anon_allocate_client_credentials (&anon_cred
) < 0)
1501 fprintf (stderr
, "Anonymous authentication error\n");
1507 /* OCSP check for the peer's certificate. Should be called
1508 * only after the certificate list verication is complete.
1510 * 0: certificate is revoked
1511 * 1: certificate is ok
1515 cert_verify_ocsp (gnutls_session_t session
)
1517 gnutls_x509_crt_t crt
, issuer
;
1518 const gnutls_datum_t
*cert_list
;
1519 unsigned int cert_list_size
= 0;
1520 int deinit_issuer
= 0;
1521 gnutls_datum_t resp
;
1524 cert_list
= gnutls_certificate_get_peers (session
, &cert_list_size
);
1525 if (cert_list_size
== 0)
1527 fprintf (stderr
, "No certificates found!\n");
1531 gnutls_x509_crt_init (&crt
);
1533 gnutls_x509_crt_import (crt
, &cert_list
[0],
1534 GNUTLS_X509_FMT_DER
);
1537 fprintf (stderr
, "Decoding error: %s\n",
1538 gnutls_strerror (ret
));
1542 ret
= gnutls_certificate_get_issuer(xcred
, crt
, &issuer
, 0);
1543 if (ret
< 0 && cert_list_size
> 1)
1545 gnutls_x509_crt_init(&issuer
);
1546 ret
= gnutls_x509_crt_import(issuer
, &cert_list
[1], GNUTLS_X509_FMT_DER
);
1549 fprintf (stderr
, "Decoding error: %s\n",
1550 gnutls_strerror (ret
));
1557 fprintf(stderr
, "Cannot find issuer\n");
1562 ret
= send_ocsp_request(NULL
, crt
, issuer
, &resp
, 1);
1565 fprintf(stderr
, "Cannot contact OCSP server\n");
1570 /* verify and check the response for revoked cert */
1571 ret
= check_ocsp_response(issuer
, &resp
);
1575 gnutls_x509_crt_deinit (issuer
);
1576 gnutls_x509_crt_deinit (crt
);