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"
59 #include <gnutls/dane.h>
66 #include <ocsptool-common.h>
70 /* global stuff here */
71 int resume
, starttls
, insecure
, rehandshake
, udp
, mtu
;
72 const char *hostname
= NULL
;
73 const char *service
= NULL
;
77 unsigned int verbose
= 0;
80 const char *srp_passwd
= NULL
;
81 const char *srp_username
= NULL
;
82 const char *pgp_keyfile
= NULL
;
83 const char *pgp_certfile
= NULL
;
84 const char *pgp_keyring
= NULL
;
85 const char *x509_keyfile
= NULL
;
86 const char *x509_certfile
= NULL
;
87 const char *x509_cafile
= NULL
;
88 const char *x509_crlfile
= NULL
;
90 static int disable_extensions
;
91 static unsigned int init_flags
= GNUTLS_CLIENT
;
92 static const char * priorities
= NULL
;
94 const char *psk_username
= NULL
;
95 gnutls_datum_t psk_key
= { NULL
, 0 };
97 static gnutls_srp_client_credentials_t srp_cred
;
98 static gnutls_psk_client_credentials_t psk_cred
;
99 static gnutls_anon_client_credentials_t anon_cred
;
100 static gnutls_certificate_credentials_t xcred
;
102 /* end of global stuff */
106 static void check_rehandshake (socket_st
* socket
, int ret
);
107 static int do_handshake (socket_st
* socket
);
108 static void init_global_tls_stuff (void);
109 static int cert_verify_ocsp (gnutls_session_t session
);
112 static unsigned int x509_crt_size
;
113 static gnutls_pcert_st x509_crt
[MAX_CRT
];
114 static gnutls_privkey_t x509_key
= NULL
;
116 static gnutls_pcert_st pgp_crt
;
117 static gnutls_privkey_t pgp_key
= NULL
;
120 get_keyid (gnutls_openpgp_keyid_t keyid
, const char *str
)
122 size_t keyid_size
= sizeof (keyid
);
124 if (strlen (str
) != 16)
127 "The OpenPGP subkey ID has to be 16 hexadecimal characters.\n");
131 if (gnutls_hex2bin (str
, strlen (str
), keyid
, &keyid_size
) < 0)
133 fprintf (stderr
, "Error converting hex string: %s.\n", str
);
140 /* Load the certificate and the private key.
145 unsigned int crt_num
;
148 gnutls_datum_t data
= { NULL
, 0 };
149 gnutls_x509_crt_t crt_list
[MAX_CRT
];
150 unsigned char keyid
[GNUTLS_OPENPGP_KEYID_SIZE
];
152 if (x509_certfile
!= NULL
&& x509_keyfile
!= NULL
)
155 if (strncmp (x509_certfile
, "pkcs11:", 7) == 0)
158 gnutls_x509_crt_init (&crt_list
[0]);
159 gnutls_x509_crt_set_pin_function(crt_list
[0], pin_callback
, NULL
);
162 gnutls_x509_crt_import_pkcs11_url (crt_list
[0], x509_certfile
, 0);
164 if (ret
== GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE
)
166 gnutls_x509_crt_import_pkcs11_url (crt_list
[0], x509_certfile
,
167 GNUTLS_PKCS11_OBJ_FLAG_LOGIN
);
171 fprintf (stderr
, "*** Error loading cert file.\n");
177 #endif /* ENABLE_PKCS11 */
180 ret
= gnutls_load_file (x509_certfile
, &data
);
183 fprintf (stderr
, "*** Error loading cert file.\n");
189 gnutls_x509_crt_list_import (crt_list
, &crt_num
, &data
,
191 GNUTLS_X509_CRT_LIST_IMPORT_FAIL_IF_EXCEED
);
194 if (ret
== GNUTLS_E_SHORT_MEMORY_BUFFER
)
197 "*** Error loading cert file: Too many certs %d\n",
204 "*** Error loading cert file: %s\n",
205 gnutls_strerror (ret
));
212 for (i
=0;i
<x509_crt_size
;i
++)
214 ret
= gnutls_pcert_import_x509(&x509_crt
[i
], crt_list
[i
], 0);
217 fprintf(stderr
, "*** Error importing crt to pcert: %s\n",
218 gnutls_strerror(ret
));
221 gnutls_x509_crt_deinit(crt_list
[i
]);
224 gnutls_free (data
.data
);
226 ret
= gnutls_privkey_init(&x509_key
);
229 fprintf (stderr
, "*** Error initializing key: %s\n",
230 gnutls_strerror (ret
));
234 gnutls_privkey_set_pin_function(x509_key
, pin_callback
, NULL
);
236 if (gnutls_url_is_supported(x509_keyfile
) != 0)
239 gnutls_privkey_import_url (x509_key
, x509_keyfile
, 0);
242 fprintf (stderr
, "*** Error loading url: %s\n",
243 gnutls_strerror (ret
));
249 ret
= gnutls_load_file (x509_keyfile
, &data
);
252 fprintf (stderr
, "*** Error loading key file.\n");
256 ret
= gnutls_privkey_import_x509_raw( x509_key
, &data
, x509ctype
, NULL
, 0);
259 fprintf (stderr
, "*** Error loading url: %s\n",
260 gnutls_strerror (ret
));
264 gnutls_free(data
.data
);
267 fprintf (stdout
, "Processed %d client X.509 certificates...\n",
272 #ifdef ENABLE_OPENPGP
273 if (HAVE_OPT(PGPSUBKEY
))
275 get_keyid (keyid
, OPT_ARG(PGPSUBKEY
));
278 if (pgp_certfile
!= NULL
&& pgp_keyfile
!= NULL
)
280 gnutls_openpgp_crt_t tmp_pgp_crt
;
282 ret
= gnutls_load_file (pgp_certfile
, &data
);
285 fprintf (stderr
, "*** Error loading PGP cert file.\n");
289 gnutls_openpgp_crt_init (&tmp_pgp_crt
);
292 gnutls_pcert_import_openpgp_raw (&pgp_crt
, &data
, GNUTLS_OPENPGP_FMT_BASE64
, HAVE_OPT(PGPSUBKEY
)?keyid
:NULL
, 0);
296 "*** Error loading PGP cert file: %s\n",
297 gnutls_strerror (ret
));
301 gnutls_free (data
.data
);
303 ret
= gnutls_privkey_init(&pgp_key
);
306 fprintf (stderr
, "*** Error initializing key: %s\n",
307 gnutls_strerror (ret
));
311 gnutls_privkey_set_pin_function(pgp_key
, pin_callback
, NULL
);
313 if (gnutls_url_is_supported (pgp_keyfile
))
315 ret
= gnutls_privkey_import_url( pgp_key
, pgp_keyfile
, 0);
318 fprintf (stderr
, "*** Error loading url: %s\n",
319 gnutls_strerror (ret
));
325 ret
= gnutls_load_file (pgp_keyfile
, &data
);
328 fprintf (stderr
, "*** Error loading key file.\n");
332 if (HAVE_OPT(PGPSUBKEY
))
333 ret
= gnutls_privkey_import_openpgp_raw( pgp_key
, &data
, x509ctype
, keyid
, NULL
);
335 ret
= gnutls_privkey_import_openpgp_raw( pgp_key
, &data
, x509ctype
, NULL
, NULL
);
338 fprintf (stderr
, "*** Error loading url: %s\n",
339 gnutls_strerror (ret
));
343 gnutls_free(data
.data
);
347 fprintf (stdout
, "Processed 1 client PGP certificate...\n");
353 #define IS_NEWLINE(x) ((x[0] == '\n') || (x[0] == '\r'))
355 read_yesno (const char *input_str
)
359 fputs (input_str
, stderr
);
360 if (fgets (input
, sizeof (input
), stdin
) == NULL
)
363 if (IS_NEWLINE(input
))
366 if (input
[0] == 'y' || input
[0] == 'Y')
372 /* converts a textual service or port to
375 static const char* port_to_service(const char* sport
)
381 if (port
== 0) return sport
;
385 sr
= getservbyport(port
, udp
?"udp":"tcp");
388 fprintf(stderr
, "Warning: getservbyport() failed. Using port number as service.\n");
396 cert_verify_callback (gnutls_session_t session
)
399 unsigned int status
= 0;
400 int ssh
= ENABLED_OPT(TOFU
);
402 int dane
= ENABLED_OPT(DANE
);
404 int ca_verify
= ENABLED_OPT(CA_VERIFICATION
);
405 const char* txt_service
;
407 print_cert_info (session
, verbose
, print_cert
);
411 rc
= cert_verify(session
, hostname
);
414 printf ("*** Verifying server certificate failed...\n");
415 if (!insecure
&& !ssh
)
418 else if (ENABLED_OPT(OCSP
) && gnutls_ocsp_status_request_is_checked(session
, 0) == 0)
419 { /* off-line verification succeeded. Try OCSP */
420 rc
= cert_verify_ocsp(session
);
423 printf ("*** Verifying (with OCSP) server certificate failed...\n");
424 if (!insecure
&& !ssh
)
428 printf("*** OCSP response ignored\n");
432 if (ssh
) /* try ssh auth */
434 unsigned int list_size
;
435 const gnutls_datum_t
* cert
;
437 cert
= gnutls_certificate_get_peers(session
, &list_size
);
440 fprintf(stderr
, "Cannot obtain peer's certificate!\n");
444 txt_service
= port_to_service(service
);
446 rc
= gnutls_verify_stored_pubkey(NULL
, NULL
, hostname
, txt_service
,
447 GNUTLS_CRT_X509
, cert
, 0);
448 if (rc
== GNUTLS_E_NO_CERTIFICATE_FOUND
)
450 print_cert_info_compact(session
);
451 fprintf(stderr
, "Host %s (%s) has never been contacted before.\n", hostname
, txt_service
);
453 fprintf(stderr
, "Its certificate is valid for %s.\n", hostname
);
455 rc
= read_yesno("Are you sure you want to trust it? (y/N): ");
459 else if (rc
== GNUTLS_E_CERTIFICATE_KEY_MISMATCH
)
461 print_cert_info_compact(session
);
462 fprintf(stderr
, "Warning: host %s is known and it is associated with a different key.\n", hostname
);
463 fprintf(stderr
, "It might be that the server has multiple keys, or an attacker replaced the key to eavesdrop this connection .\n");
465 fprintf(stderr
, "Its certificate is valid for %s.\n", hostname
);
467 rc
= read_yesno("Do you trust the received key? (y/N): ");
473 fprintf(stderr
, "gnutls_verify_stored_pubkey: %s\n", gnutls_strerror(rc
));
479 rc
= gnutls_store_pubkey(NULL
, NULL
, hostname
, txt_service
,
480 GNUTLS_CRT_X509
, cert
, 0, 0);
482 fprintf(stderr
, "Could not store key: %s\n", gnutls_strerror(rc
));
487 if (dane
) /* try DANE auth */
489 unsigned int sflags
= ENABLED_OPT(LOCAL_DNS
)?0:DANE_F_IGNORE_LOCAL_RESOLVER
;
490 rc
= dane_verify_session_crt( NULL
, session
, hostname
, udp
?"udp":"tcp", atoi(service
),
494 fprintf(stderr
, "*** DANE verification error: %s\n", dane_strerror(rc
));
502 rc
= dane_verification_status_print( status
, &out
, 0);
505 fprintf(stderr
, "*** DANE error: %s\n", dane_strerror(rc
));
510 fprintf(stderr
, "- %s\n", out
.data
);
511 gnutls_free(out
.data
);
520 /* This callback should be associated with a session by calling
521 * gnutls_certificate_client_set_retrieve_function( session, cert_callback),
522 * before a handshake.
526 cert_callback (gnutls_session_t session
,
527 const gnutls_datum_t
* req_ca_rdn
, int nreqs
,
528 const gnutls_pk_algorithm_t
* sign_algos
,
529 int sign_algos_length
, gnutls_pcert_st
**pcert
,
530 unsigned int *pcert_length
, gnutls_privkey_t
* pkey
)
533 int i
, ret
, cert_type
;
538 /* Print the server's trusted CAs
541 printf ("- Server's trusted authorities:\n");
543 printf ("- Server did not send us any trusted authorities names.\n");
545 /* print the names (if any) */
546 for (i
= 0; i
< nreqs
; i
++)
548 len
= sizeof (issuer_dn
);
549 ret
= gnutls_x509_rdn_get (&req_ca_rdn
[i
], issuer_dn
, &len
);
552 printf (" [%d]: ", i
);
553 printf ("%s\n", issuer_dn
);
558 /* Select a certificate and return it.
559 * The certificate must be of any of the "sign algorithms"
560 * supported by the server.
563 cert_type
= gnutls_certificate_type_get (session
);
567 if (cert_type
== GNUTLS_CRT_X509
)
569 if (x509_crt_size
> 0)
571 if (x509_key
!= NULL
)
577 printf ("- Could not find a suitable key to send to server\n");
581 *pcert_length
= x509_crt_size
;
586 else if (cert_type
== GNUTLS_CRT_OPENPGP
)
597 printf ("- Successfully sent %u certificate(s) to server.\n", *pcert_length
);
602 /* initializes a gnutls_session_t with some defaults.
604 static gnutls_session_t
605 init_tls_session (const char *hostname
)
609 gnutls_session_t session
;
611 if (priorities
== NULL
)
612 priorities
= "NORMAL";
616 gnutls_init (&session
, GNUTLS_DATAGRAM
|init_flags
);
618 gnutls_dtls_set_mtu(session
, mtu
);
621 gnutls_init (&session
, init_flags
);
623 if ((ret
= gnutls_priority_set_direct (session
, priorities
, &err
)) < 0)
625 if (ret
== GNUTLS_E_INVALID_REQUEST
) fprintf (stderr
, "Syntax error at: %s\n", err
);
627 fprintf(stderr
, "Error in priorities: %s\n", gnutls_strerror(ret
));
631 /* allow the use of private ciphersuites.
633 if (disable_extensions
== 0)
635 if (!isdigit(hostname
[0]) && strchr(hostname
, ':') == 0)
636 gnutls_server_name_set (session
, GNUTLS_NAME_DNS
, hostname
,
640 if (HAVE_OPT(DH_BITS
))
641 gnutls_dh_set_prime_bits( session
, OPT_VALUE_DH_BITS
);
643 gnutls_credentials_set (session
, GNUTLS_CRD_ANON
, anon_cred
);
645 gnutls_credentials_set (session
, GNUTLS_CRD_SRP
, srp_cred
);
647 gnutls_credentials_set (session
, GNUTLS_CRD_PSK
, psk_cred
);
648 gnutls_credentials_set (session
, GNUTLS_CRD_CERTIFICATE
, xcred
);
650 gnutls_certificate_set_retrieve_function2 (xcred
, cert_callback
);
651 gnutls_certificate_set_verify_function (xcred
, cert_verify_callback
);
653 /* send the fingerprint */
654 #ifdef ENABLE_OPENPGP
655 if (fingerprint
!= 0)
656 gnutls_openpgp_send_cert (session
, GNUTLS_OPENPGP_CERT_FINGERPRINT
);
659 /* use the max record size extension */
660 if (record_max_size
> 0 && disable_extensions
== 0)
662 if (gnutls_record_set_max_size (session
, record_max_size
) < 0)
665 "Cannot set the maximum record size to %d.\n",
667 fprintf (stderr
, "Possible values: 512, 1024, 2048, 4096.\n");
672 if (HAVE_OPT(HEARTBEAT
))
673 gnutls_heartbeat_enable (session
, GNUTLS_HB_PEER_ALLOWED_TO_SEND
);
675 #ifdef ENABLE_DTLS_SRTP
676 if (HAVE_OPT(SRTP_PROFILES
))
678 ret
= gnutls_srtp_set_profile_direct (session
, OPT_ARG(SRTP_PROFILES
), &err
);
679 if (ret
== GNUTLS_E_INVALID_REQUEST
) fprintf (stderr
, "Syntax error at: %s\n", err
);
681 fprintf(stderr
, "Error in profiles: %s\n", gnutls_strerror(ret
));
689 static void cmd_parser (int argc
, char **argv
);
691 /* Returns zero if the error code was successfully handled.
694 handle_error (socket_st
* hd
, int err
)
697 const char *err_type
, *str
;
699 if (err
>= 0 || err
== GNUTLS_E_AGAIN
|| err
== GNUTLS_E_INTERRUPTED
)
702 if (gnutls_error_is_fatal (err
) == 0)
705 err_type
= "Non fatal";
713 str
= gnutls_strerror (err
);
716 fprintf (stderr
, "*** %s error: %s\n", err_type
, str
);
718 if (err
== GNUTLS_E_WARNING_ALERT_RECEIVED
719 || err
== GNUTLS_E_FATAL_ALERT_RECEIVED
)
721 alert
= gnutls_alert_get (hd
->session
);
722 str
= gnutls_alert_get_name (alert
);
725 printf ("*** Received alert [%d]: %s\n", alert
, str
);
728 check_rehandshake (hd
, err
);
733 int starttls_alarmed
= 0;
737 starttls_alarm (int signum
)
739 starttls_alarmed
= 1;
744 tls_log_func (int level
, const char *str
)
746 fprintf (stderr
, "|<%d>| %s", level
, str
);
749 #define IN_KEYBOARD 1
752 /* returns IN_KEYBOARD for keyboard input and IN_NET for network input
754 static int check_net_or_keyboard_input(socket_st
* hd
)
764 FD_SET (hd
->fd
, &rset
);
767 FD_SET (fileno (stdin
), &rset
);
768 maxfd
= MAX (fileno (stdin
), hd
->fd
);
774 tv
.tv_usec
= 50 * 1000;
777 if (gnutls_record_check_pending(hd
->session
))
780 err
= select (maxfd
+ 1, &rset
, NULL
, NULL
, &tv
);
784 if (FD_ISSET (hd
->fd
, &rset
))
790 state
= WaitForSingleObject(GetStdHandle(STD_INPUT_HANDLE
), 200);
792 if (state
== WAIT_OBJECT_0
)
796 if (FD_ISSET (fileno (stdin
), &rset
))
806 main (int argc
, char **argv
)
810 char buffer
[MAX_BUF
+ 1];
811 char *session_data
= NULL
;
812 char *session_id
= NULL
;
813 size_t session_data_size
;
814 size_t session_id_size
= 0;
815 int user_term
= 0, retval
= 0;
819 set_program_name (argv
[0]);
820 cmd_parser (argc
, argv
);
822 gnutls_global_set_log_function (tls_log_func
);
823 gnutls_global_set_log_level (OPT_VALUE_DEBUG
);
825 if ((ret
= gnutls_global_init ()) < 0)
827 fprintf (stderr
, "global_init: %s\n", gnutls_strerror (ret
));
835 if (hostname
== NULL
)
837 fprintf (stderr
, "No hostname given\n");
843 init_global_tls_stuff ();
845 socket_open (&hd
, hostname
, service
, udp
);
846 socket_connect (&hd
);
848 hd
.session
= init_tls_session (hostname
);
850 goto after_handshake
;
852 for (i
= 0; i
< 2; i
++)
858 hd
.session
= init_tls_session (hostname
);
859 gnutls_session_set_data (hd
.session
, session_data
,
864 ret
= do_handshake (&hd
);
868 fprintf (stderr
, "*** Handshake has failed\n");
870 gnutls_deinit (hd
.session
);
875 printf ("- Handshake was completed\n");
876 if (gnutls_session_is_resumed (hd
.session
) != 0)
877 printf ("*** This is a resumed session\n");
880 if (resume
!= 0 && i
== 0)
883 gnutls_session_get_data (hd
.session
, NULL
, &session_data_size
);
884 session_data
= malloc (session_data_size
);
886 gnutls_session_get_data (hd
.session
, session_data
,
889 gnutls_session_get_id (hd
.session
, NULL
, &session_id_size
);
891 session_id
= malloc (session_id_size
);
892 gnutls_session_get_id (hd
.session
, session_id
, &session_id_size
);
894 printf ("- Disconnecting\n");
898 ("\n\n- Connecting again- trying to resume previous session\n");
899 socket_open (&hd
, hostname
, service
, udp
);
900 socket_connect (&hd
);
910 /* Warning! Do not touch this text string, it is used by external
911 programs to search for when gnutls-cli has reached this point. */
912 printf ("\n- Simple Client Mode:\n\n");
916 ret
= do_handshake (&hd
);
920 fprintf (stderr
, "*** ReHandshake has failed\n");
922 gnutls_deinit (hd
.session
);
927 printf ("- ReHandshake was completed\n");
932 signal (SIGALRM
, &starttls_alarm
);
940 setbuf (stdin
, NULL
);
942 setbuf (stdout
, NULL
);
943 setbuf (stderr
, NULL
);
947 if (starttls_alarmed
&& !hd
.secure
)
949 /* Warning! Do not touch this text string, it is used by
950 external programs to search for when gnutls-cli has
951 reached this point. */
952 fprintf (stderr
, "*** Starting TLS handshake\n");
953 ret
= do_handshake (&hd
);
956 fprintf (stderr
, "*** Handshake has failed\n");
963 inp
= check_net_or_keyboard_input(&hd
);
967 memset (buffer
, 0, MAX_BUF
+ 1);
968 ret
= socket_recv (&hd
, buffer
, MAX_BUF
);
972 printf ("- Peer has closed the GnuTLS connection\n");
975 else if (handle_error (&hd
, ret
) < 0 && user_term
== 0)
978 "*** Server has terminated the connection abnormally.\n");
985 printf ("- Received[%d]: ", ret
);
986 for (ii
= 0; ii
< ret
; ii
++)
988 fputc (buffer
[ii
], stdout
);
997 if (inp
== IN_KEYBOARD
)
999 if ((bytes
= read (fileno (stdin
), buffer
, MAX_BUF
- 1)) <= 0)
1003 /* Warning! Do not touch this text string, it is
1004 used by external programs to search for when
1005 gnutls-cli has reached this point. */
1006 fprintf (stderr
, "*** Starting TLS handshake\n");
1007 ret
= do_handshake (&hd
);
1011 fprintf (stderr
, "*** Handshake has failed\n");
1028 char *b
= strchr (buffer
, '\n');
1036 ret
= socket_send (&hd
, buffer
, bytes
);
1041 printf ("- Sent: %d bytes\n", ret
);
1044 handle_error (&hd
, ret
);
1052 gnutls_deinit (hd
.session
);
1056 gnutls_srp_free_client_credentials (srp_cred
);
1060 gnutls_psk_free_client_credentials (psk_cred
);
1063 gnutls_certificate_free_credentials (xcred
);
1066 gnutls_anon_free_client_credentials (anon_cred
);
1069 gnutls_global_deinit ();
1075 cmd_parser (int argc
, char **argv
)
1077 const char* rest
= NULL
;
1079 int optct
= optionProcess( &gnutls_cliOptions
, argc
, argv
);
1083 if (rest
== NULL
&& argc
> 0)
1086 if (HAVE_OPT(BENCHMARK_CIPHERS
))
1088 benchmark_cipher(1, OPT_VALUE_DEBUG
);
1092 if (HAVE_OPT(BENCHMARK_SOFT_CIPHERS
))
1094 benchmark_cipher(0, OPT_VALUE_DEBUG
);
1098 if (HAVE_OPT(BENCHMARK_TLS_CIPHERS
))
1100 benchmark_tls(OPT_VALUE_DEBUG
, 1);
1104 if (HAVE_OPT(BENCHMARK_TLS_KX
))
1106 benchmark_tls(OPT_VALUE_DEBUG
, 0);
1110 if (HAVE_OPT(PRIORITY
))
1112 priorities
= OPT_ARG(PRIORITY
);
1114 verbose
= HAVE_OPT( VERBOSE
);
1118 print_cert
= HAVE_OPT( PRINT_CERT
);
1122 print_list(priorities
, verbose
);
1126 disable_extensions
= HAVE_OPT( DISABLE_EXTENSIONS
);
1127 if (disable_extensions
)
1128 init_flags
|= GNUTLS_NO_EXTENSIONS
;
1130 starttls
= HAVE_OPT(STARTTLS
);
1131 resume
= HAVE_OPT(RESUME
);
1132 rehandshake
= HAVE_OPT(REHANDSHAKE
);
1133 insecure
= HAVE_OPT(INSECURE
);
1135 udp
= HAVE_OPT(UDP
);
1136 mtu
= OPT_VALUE_MTU
;
1140 service
= OPT_ARG(PORT
);
1147 record_max_size
= OPT_VALUE_RECORDSIZE
;
1149 fingerprint
= HAVE_OPT(FINGERPRINT
);
1151 if (HAVE_OPT(X509FMTDER
))
1152 x509ctype
= GNUTLS_X509_FMT_DER
;
1154 x509ctype
= GNUTLS_X509_FMT_PEM
;
1156 if (HAVE_OPT(SRPUSERNAME
))
1157 srp_username
= OPT_ARG(SRPUSERNAME
);
1159 if (HAVE_OPT(SRPPASSWD
))
1160 srp_passwd
= OPT_ARG(SRPPASSWD
);
1162 if (HAVE_OPT(X509CAFILE
))
1163 x509_cafile
= OPT_ARG(X509CAFILE
);
1165 if (HAVE_OPT(X509CRLFILE
))
1166 x509_crlfile
= OPT_ARG(X509CRLFILE
);
1168 if (HAVE_OPT(X509KEYFILE
))
1169 x509_keyfile
= OPT_ARG(X509KEYFILE
);
1171 if (HAVE_OPT(X509CERTFILE
))
1172 x509_certfile
= OPT_ARG(X509CERTFILE
);
1174 if (HAVE_OPT(PGPKEYFILE
))
1175 pgp_keyfile
= OPT_ARG(PGPKEYFILE
);
1177 if (HAVE_OPT(PGPCERTFILE
))
1178 pgp_certfile
= OPT_ARG(PGPCERTFILE
);
1180 if (HAVE_OPT(PSKUSERNAME
))
1181 psk_username
= OPT_ARG(PSKUSERNAME
);
1183 if (HAVE_OPT(PSKKEY
))
1185 psk_key
.data
= (unsigned char *) OPT_ARG(PSKKEY
);
1186 psk_key
.size
= strlen (OPT_ARG(PSKKEY
));
1191 if (HAVE_OPT(PGPKEYRING
))
1192 pgp_keyring
= OPT_ARG(PGPKEYRING
);
1194 crlf
= HAVE_OPT(CRLF
);
1199 if (hostname
== NULL
)
1201 fprintf(stderr
, "No hostname specified\n");
1207 check_rehandshake (socket_st
* socket
, int ret
)
1209 if (socket
->secure
&& ret
== GNUTLS_E_REHANDSHAKE
)
1211 /* There is a race condition here. If application
1212 * data is sent after the rehandshake request,
1213 * the server thinks we ignored his request.
1214 * This is a bad design of this client.
1216 printf ("*** Received rehandshake request\n");
1217 /* gnutls_alert_send( session, GNUTLS_AL_WARNING, GNUTLS_A_NO_RENEGOTIATION); */
1219 ret
= do_handshake (socket
);
1223 printf ("*** Rehandshake was performed.\n");
1227 printf ("*** Rehandshake Failed.\n");
1234 do_handshake (socket_st
* socket
)
1238 gnutls_transport_set_ptr (socket
->session
,
1239 (gnutls_transport_ptr_t
)
1240 gl_fd_to_handle (socket
->fd
));
1243 gnutls_handshake_set_timeout( socket
->session
, GNUTLS_DEFAULT_HANDSHAKE_TIMEOUT
);
1244 ret
= gnutls_handshake (socket
->session
);
1248 handle_error (socket
, ret
);
1251 while (ret
< 0 && gnutls_error_is_fatal (ret
) == 0);
1255 /* print some information */
1256 print_info (socket
->session
, verbose
, 0);
1261 gnutls_alert_send_appropriate (socket
->session
, ret
);
1262 shutdown (socket
->fd
, SHUT_RDWR
);
1268 srp_username_callback (gnutls_session_t session
,
1269 char **username
, char **password
)
1271 if (srp_username
== NULL
|| srp_passwd
== NULL
)
1276 *username
= gnutls_strdup (srp_username
);
1277 *password
= gnutls_strdup (srp_passwd
);
1283 psk_callback (gnutls_session_t session
, char **username
, gnutls_datum_t
* key
)
1285 const char *hint
= gnutls_psk_client_get_hint (session
);
1292 printf ("- PSK client callback. ");
1294 printf ("PSK hint '%s'\n", hint
);
1296 printf ("No PSK hint\n");
1298 if (HAVE_OPT(PSKUSERNAME
))
1299 *username
= gnutls_strdup (OPT_ARG(PSKUSERNAME
));
1305 printf ("Enter PSK identity: ");
1307 getline (&tmp
, &n
, stdin
);
1311 fprintf (stderr
, "No username given, aborting...\n");
1312 return GNUTLS_E_INSUFFICIENT_CREDENTIALS
;
1315 if (tmp
[strlen (tmp
) - 1] == '\n')
1316 tmp
[strlen (tmp
) - 1] = '\0';
1317 if (tmp
[strlen (tmp
) - 1] == '\r')
1318 tmp
[strlen (tmp
) - 1] = '\0';
1320 *username
= gnutls_strdup (tmp
);
1324 return GNUTLS_E_MEMORY_ERROR
;
1326 passwd
= getpass ("Enter key: ");
1329 fprintf (stderr
, "No key given, aborting...\n");
1330 return GNUTLS_E_INSUFFICIENT_CREDENTIALS
;
1333 tmp
.data
= (void*)passwd
;
1334 tmp
.size
= strlen (passwd
);
1336 res_size
= tmp
.size
/ 2 + 1;
1337 rawkey
= gnutls_malloc (res_size
);
1339 return GNUTLS_E_MEMORY_ERROR
;
1341 ret
= gnutls_hex_decode (&tmp
, rawkey
, &res_size
);
1344 fprintf (stderr
, "Error deriving password: %s\n",
1345 gnutls_strerror (ret
));
1346 gnutls_free (*username
);
1350 key
->data
= (void*)rawkey
;
1351 key
->size
= res_size
;
1353 if (HAVE_OPT(DEBUG
))
1356 res_size
= sizeof (hexkey
);
1357 gnutls_hex_encode (key
, hexkey
, &res_size
);
1358 fprintf (stderr
, "PSK username: %s\n", *username
);
1359 fprintf (stderr
, "PSK hint: %s\n", hint
);
1360 fprintf (stderr
, "PSK key: %s\n", hexkey
);
1367 init_global_tls_stuff (void)
1372 if (gnutls_certificate_allocate_credentials (&xcred
) < 0)
1374 fprintf (stderr
, "Certificate allocation memory error\n");
1377 gnutls_certificate_set_pin_function(xcred
, pin_callback
, NULL
);
1379 if (x509_cafile
!= NULL
)
1381 ret
= gnutls_certificate_set_x509_trust_file (xcred
,
1382 x509_cafile
, x509ctype
);
1386 ret
= gnutls_certificate_set_x509_system_trust (xcred
);
1390 fprintf (stderr
, "Error setting the x509 trust file\n");
1394 printf ("Processed %d CA certificate(s).\n", ret
);
1397 if (x509_crlfile
!= NULL
)
1399 ret
= gnutls_certificate_set_x509_crl_file (xcred
, x509_crlfile
,
1403 fprintf (stderr
, "Error setting the x509 CRL file\n");
1407 printf ("Processed %d CRL(s).\n", ret
);
1413 #ifdef ENABLE_OPENPGP
1414 if (pgp_keyring
!= NULL
)
1417 gnutls_certificate_set_openpgp_keyring_file (xcred
, pgp_keyring
,
1418 GNUTLS_OPENPGP_FMT_BASE64
);
1421 fprintf (stderr
, "Error setting the OpenPGP keyring file\n");
1427 if (srp_username
&& srp_passwd
)
1430 if (gnutls_srp_allocate_client_credentials (&srp_cred
) < 0)
1432 fprintf (stderr
, "SRP authentication error\n");
1435 gnutls_srp_set_client_credentials_function (srp_cred
,
1436 srp_username_callback
);
1442 if (gnutls_psk_allocate_client_credentials (&psk_cred
) < 0)
1444 fprintf (stderr
, "PSK authentication error\n");
1447 if (psk_username
&& psk_key
.data
)
1449 ret
= gnutls_psk_set_client_credentials (psk_cred
,
1450 psk_username
, &psk_key
,
1451 GNUTLS_PSK_KEY_HEX
);
1454 fprintf (stderr
, "Error setting the PSK credentials: %s\n",
1455 gnutls_strerror (ret
));
1459 gnutls_psk_set_client_credentials_function (psk_cred
, psk_callback
);
1464 if (gnutls_anon_allocate_client_credentials (&anon_cred
) < 0)
1466 fprintf (stderr
, "Anonymous authentication error\n");
1472 /* OCSP check for the peer's certificate. Should be called
1473 * only after the certificate list verication is complete.
1475 * 0: certificate is revoked
1476 * 1: certificate is ok
1480 cert_verify_ocsp (gnutls_session_t session
)
1482 gnutls_x509_crt_t crt
, issuer
;
1483 const gnutls_datum_t
*cert_list
;
1484 unsigned int cert_list_size
= 0;
1485 int deinit_issuer
= 0;
1486 gnutls_datum_t resp
;
1489 cert_list
= gnutls_certificate_get_peers (session
, &cert_list_size
);
1490 if (cert_list_size
== 0)
1492 fprintf (stderr
, "No certificates found!\n");
1496 gnutls_x509_crt_init (&crt
);
1498 gnutls_x509_crt_import (crt
, &cert_list
[0],
1499 GNUTLS_X509_FMT_DER
);
1502 fprintf (stderr
, "Decoding error: %s\n",
1503 gnutls_strerror (ret
));
1507 ret
= gnutls_certificate_get_issuer(xcred
, crt
, &issuer
, 0);
1508 if (ret
< 0 && cert_list_size
> 1)
1510 gnutls_x509_crt_init(&issuer
);
1511 ret
= gnutls_x509_crt_import(issuer
, &cert_list
[1], GNUTLS_X509_FMT_DER
);
1514 fprintf (stderr
, "Decoding error: %s\n",
1515 gnutls_strerror (ret
));
1522 fprintf(stderr
, "Cannot find issuer\n");
1527 ret
= send_ocsp_request(NULL
, crt
, issuer
, &resp
, 1);
1530 fprintf(stderr
, "Cannot contact OCSP server\n");
1535 /* verify and check the response for revoked cert */
1536 ret
= check_ocsp_response(crt
, issuer
, &resp
);
1540 gnutls_x509_crt_deinit (issuer
);
1541 gnutls_x509_crt_deinit (crt
);