Increased maximum password len in PKCS #12.
[gnutls.git] / src / common.c
blob75048c621c6464c0d3df9551f3f2184763c0a1c0
1 /*
2 * Copyright (C) 2000-2012 Free Software Foundation, Inc.
3 * Author: Nikos Mavrogiannopoulos
5 * This file is part of GnuTLS.
7 * GnuTLS is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * GnuTLS is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include <config.h>
23 /* Work around problem reported in
24 <http://permalink.gmane.org/gmane.comp.lib.gnulib.bugs/15755>.*/
25 #if GETTIMEOFDAY_CLOBBERS_LOCALTIME
26 #undef localtime
27 #endif
29 #include <getpass.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <gnutls/gnutls.h>
35 #include <gnutls/x509.h>
36 #include <gnutls/openpgp.h>
37 #include <gnutls/crypto.h>
38 #include <time.h>
39 #include <common.h>
41 #ifdef ENABLE_PKCS11
42 # include <gnutls/pkcs11.h>
43 #endif
45 #define SU(x) (x!=NULL?x:"Unknown")
47 const char str_unknown[] = "(unknown)";
49 /* Hex encodes the given data.
51 const char *
52 raw_to_string (const unsigned char *raw, size_t raw_size)
54 static char buf[1024];
55 size_t i;
56 if (raw_size == 0)
57 return "(empty)";
59 if (raw_size * 3 + 1 >= sizeof (buf))
60 return "(too large)";
62 for (i = 0; i < raw_size; i++)
64 sprintf (&(buf[i * 3]), "%02X%s", raw[i],
65 (i == raw_size - 1) ? "" : ":");
67 buf[sizeof (buf) - 1] = '\0';
69 return buf;
72 static void
73 print_x509_info_compact (gnutls_session_t session)
75 gnutls_x509_crt_t crt;
76 const gnutls_datum_t *cert_list;
77 unsigned int cert_list_size = 0;
78 int ret;
79 gnutls_datum_t cinfo;
81 cert_list = gnutls_certificate_get_peers (session, &cert_list_size);
82 if (cert_list_size == 0)
84 fprintf (stderr, "No certificates found!\n");
85 return;
88 gnutls_x509_crt_init (&crt);
89 ret =
90 gnutls_x509_crt_import (crt, &cert_list[0],
91 GNUTLS_X509_FMT_DER);
92 if (ret < 0)
94 fprintf (stderr, "Decoding error: %s\n",
95 gnutls_strerror (ret));
96 return;
99 ret =
100 gnutls_x509_crt_print (crt, GNUTLS_CRT_PRINT_COMPACT, &cinfo);
101 if (ret == 0)
103 printf ("- X.509 cert: %s\n", cinfo.data);
104 gnutls_free (cinfo.data);
107 gnutls_x509_crt_deinit (crt);
110 static void
111 print_x509_info (gnutls_session_t session, int flag, int print_cert)
113 gnutls_x509_crt_t crt;
114 const gnutls_datum_t *cert_list;
115 unsigned int cert_list_size = 0, j;
116 int ret;
118 cert_list = gnutls_certificate_get_peers (session, &cert_list_size);
119 if (cert_list_size == 0)
121 fprintf (stderr, "No certificates found!\n");
122 return;
125 printf ("- Certificate type: X.509\n");
126 printf ("- Got a certificate list of %d certificates.\n",
127 cert_list_size);
129 for (j = 0; j < cert_list_size; j++)
131 gnutls_datum_t cinfo;
133 gnutls_x509_crt_init (&crt);
134 ret =
135 gnutls_x509_crt_import (crt, &cert_list[j],
136 GNUTLS_X509_FMT_DER);
137 if (ret < 0)
139 fprintf (stderr, "Decoding error: %s\n",
140 gnutls_strerror (ret));
141 return;
144 printf ("- Certificate[%d] info:\n - ", j);
145 if (flag == GNUTLS_CRT_PRINT_COMPACT && j > 0) flag = GNUTLS_CRT_PRINT_ONELINE;
147 ret =
148 gnutls_x509_crt_print (crt, flag, &cinfo);
149 if (ret == 0)
151 printf ("%s\n", cinfo.data);
152 gnutls_free (cinfo.data);
155 if (print_cert)
157 size_t size = 0;
158 char *p = NULL;
160 ret =
161 gnutls_x509_crt_export (crt, GNUTLS_X509_FMT_PEM, p,
162 &size);
163 if (ret == GNUTLS_E_SHORT_MEMORY_BUFFER)
165 p = malloc (size+1);
166 if (!p)
168 fprintf (stderr, "gnutls_malloc\n");
169 exit (1);
172 ret =
173 gnutls_x509_crt_export (crt, GNUTLS_X509_FMT_PEM,
174 p, &size);
176 if (ret < 0)
178 fprintf (stderr, "Encoding error: %s\n",
179 gnutls_strerror (ret));
180 return;
183 p[size] = 0;
184 fputs ("\n", stdout);
185 fputs (p, stdout);
186 fputs ("\n", stdout);
188 gnutls_free (p);
191 gnutls_x509_crt_deinit (crt);
195 /* returns true or false, depending on whether the hostname
196 * matches to certificate */
197 static int
198 verify_x509_hostname (gnutls_session_t session, const char *hostname)
200 gnutls_x509_crt_t crt;
201 const gnutls_datum_t *cert_list;
202 unsigned int cert_list_size = 0;
203 int ret;
205 cert_list = gnutls_certificate_get_peers (session, &cert_list_size);
206 if (cert_list_size == 0)
208 fprintf (stderr, "No certificates found!\n");
209 return 0;
212 gnutls_x509_crt_init (&crt);
213 ret =
214 gnutls_x509_crt_import (crt, &cert_list[0],
215 GNUTLS_X509_FMT_DER);
216 if (ret < 0)
218 fprintf (stderr, "Decoding error: %s\n",
219 gnutls_strerror (ret));
220 return 0;
223 /* Check the hostname of the first certificate if it matches
224 * the name of the host we connected to.
226 if (hostname != NULL)
228 if (gnutls_x509_crt_check_hostname (crt, hostname) == 0)
230 printf
231 ("- The hostname in the certificate does NOT match '%s'\n",
232 hostname);
233 ret = 0;
235 else
237 printf ("- The hostname in the certificate matches '%s'.\n",
238 hostname);
239 ret = 1;
243 gnutls_x509_crt_deinit (crt);
245 return ret;
248 #ifdef ENABLE_OPENPGP
249 /* returns true or false, depending on whether the hostname
250 * matches to certificate */
251 static int
252 verify_openpgp_hostname (gnutls_session_t session, const char *hostname)
254 gnutls_openpgp_crt_t crt;
255 const gnutls_datum_t *cert_list;
256 unsigned int cert_list_size = 0;
257 int ret;
259 cert_list = gnutls_certificate_get_peers (session, &cert_list_size);
260 if (cert_list_size == 0)
262 fprintf (stderr, "No certificates found!\n");
263 return 0;
266 gnutls_openpgp_crt_init (&crt);
267 ret =
268 gnutls_openpgp_crt_import (crt, &cert_list[0],
269 GNUTLS_OPENPGP_FMT_RAW);
270 if (ret < 0)
272 fprintf (stderr, "Decoding error: %s\n",
273 gnutls_strerror (ret));
274 return 0;
277 /* Check the hostname of the first certificate if it matches
278 * the name of the host we connected to.
280 if (gnutls_openpgp_crt_check_hostname (crt, hostname) == 0)
282 printf
283 ("- The hostname in the certificate does NOT match '%s'\n",
284 hostname);
285 ret = 0;
287 else
289 printf ("- The hostname in the certificate matches '%s'.\n",
290 hostname);
291 ret = 1;
294 gnutls_openpgp_crt_deinit (crt);
296 return ret;
299 static void
300 print_openpgp_info_compact (gnutls_session_t session)
303 gnutls_openpgp_crt_t crt;
304 const gnutls_datum_t *cert_list;
305 unsigned int cert_list_size = 0;
306 int ret;
308 cert_list = gnutls_certificate_get_peers (session, &cert_list_size);
310 if (cert_list_size > 0)
312 gnutls_datum_t cinfo;
314 gnutls_openpgp_crt_init (&crt);
315 ret = gnutls_openpgp_crt_import (crt, &cert_list[0],
316 GNUTLS_OPENPGP_FMT_RAW);
317 if (ret < 0)
319 fprintf (stderr, "Decoding error: %s\n",
320 gnutls_strerror (ret));
321 return;
324 ret =
325 gnutls_openpgp_crt_print (crt, GNUTLS_CRT_PRINT_COMPACT, &cinfo);
326 if (ret == 0)
328 printf ("- OpenPGP cert: %s\n", cinfo.data);
329 gnutls_free (cinfo.data);
332 gnutls_openpgp_crt_deinit (crt);
336 static void
337 print_openpgp_info (gnutls_session_t session, int flag, int print_cert)
340 gnutls_openpgp_crt_t crt;
341 const gnutls_datum_t *cert_list;
342 unsigned int cert_list_size = 0;
343 int ret;
345 printf ("- Certificate type: OpenPGP\n");
347 cert_list = gnutls_certificate_get_peers (session, &cert_list_size);
349 if (cert_list_size > 0)
351 gnutls_datum_t cinfo;
353 gnutls_openpgp_crt_init (&crt);
354 ret = gnutls_openpgp_crt_import (crt, &cert_list[0],
355 GNUTLS_OPENPGP_FMT_RAW);
356 if (ret < 0)
358 fprintf (stderr, "Decoding error: %s\n",
359 gnutls_strerror (ret));
360 return;
363 ret =
364 gnutls_openpgp_crt_print (crt, flag, &cinfo);
365 if (ret == 0)
367 printf ("- %s\n", cinfo.data);
368 gnutls_free (cinfo.data);
371 if (print_cert)
373 size_t size = 0;
374 char *p = NULL;
376 ret =
377 gnutls_openpgp_crt_export (crt,
378 GNUTLS_OPENPGP_FMT_BASE64,
379 p, &size);
380 if (ret == GNUTLS_E_SHORT_MEMORY_BUFFER)
382 p = malloc (size);
383 if (!p)
385 fprintf (stderr, "gnutls_malloc\n");
386 exit (1);
389 ret =
390 gnutls_openpgp_crt_export (crt,
391 GNUTLS_OPENPGP_FMT_BASE64,
392 p, &size);
394 if (ret < 0)
396 fprintf (stderr, "Encoding error: %s\n",
397 gnutls_strerror (ret));
398 return;
401 fputs (p, stdout);
402 fputs ("\n", stdout);
404 gnutls_free (p);
407 gnutls_openpgp_crt_deinit (crt);
411 #endif
413 /* returns false (0) if not verified, or true (1) otherwise
416 cert_verify (gnutls_session_t session, const char* hostname)
418 int rc;
419 unsigned int status = 0;
420 int type;
422 rc = gnutls_certificate_verify_peers2 (session, &status);
423 if (rc == GNUTLS_E_NO_CERTIFICATE_FOUND)
425 printf ("- Peer did not send any certificate.\n");
426 return 0;
429 if (rc < 0)
431 printf ("- Could not verify certificate (err: %s)\n",
432 gnutls_strerror (rc));
433 return 0;
436 type = gnutls_certificate_type_get (session);
437 if (type == GNUTLS_CRT_X509)
440 if (status & GNUTLS_CERT_REVOKED)
441 printf ("- Peer's certificate chain revoked\n");
442 if (status & GNUTLS_CERT_SIGNER_NOT_FOUND)
443 printf ("- Peer's certificate issuer is unknown\n");
444 if (status & GNUTLS_CERT_SIGNER_NOT_CA)
445 printf ("- Peer's certificate issuer is not a CA\n");
446 if (status & GNUTLS_CERT_INSECURE_ALGORITHM)
447 printf
448 ("- Peer's certificate chain uses insecure algorithm\n");
449 if (status & GNUTLS_CERT_NOT_ACTIVATED)
450 printf
451 ("- Peer's certificate chain uses not yet valid certificate\n");
452 if (status & GNUTLS_CERT_EXPIRED)
453 printf
454 ("- Peer's certificate chain uses expired certificate\n");
455 if (status & GNUTLS_CERT_INVALID)
456 printf ("- Peer's certificate is NOT trusted\n");
457 else
458 printf ("- Peer's certificate is trusted\n");
460 rc = verify_x509_hostname (session, hostname);
461 if (rc == 0) status |= GNUTLS_CERT_INVALID;
463 else if (type == GNUTLS_CRT_OPENPGP)
465 if (status & GNUTLS_CERT_INVALID)
466 printf ("- Peer's key is invalid\n");
467 else
468 printf ("- Peer's key is valid\n");
469 if (status & GNUTLS_CERT_SIGNER_NOT_FOUND)
470 printf ("- Could not find a signer of the peer's key\n");
472 rc = verify_openpgp_hostname (session, hostname);
473 if (rc == 0) status |= GNUTLS_CERT_INVALID;
475 else
477 fprintf(stderr, "Unknown certificate type\n");
478 status |= GNUTLS_CERT_INVALID;
481 if (status)
482 return 0;
484 return 1;
487 static void
488 print_dh_info (gnutls_session_t session, const char *str, int print)
490 printf ("- %sDiffie-Hellman parameters\n", str);
491 printf (" - Using prime: %d bits\n",
492 gnutls_dh_get_prime_bits (session));
493 printf (" - Secret key: %d bits\n",
494 gnutls_dh_get_secret_bits (session));
495 printf (" - Peer's public key: %d bits\n",
496 gnutls_dh_get_peers_public_bits (session));
498 if (print)
500 int ret;
501 gnutls_datum_t raw_gen = { NULL, 0 };
502 gnutls_datum_t raw_prime = { NULL, 0 };
503 gnutls_dh_params_t dh_params = NULL;
504 unsigned char *params_data = NULL;
505 size_t params_data_size = 0;
507 ret = gnutls_dh_get_group (session, &raw_gen, &raw_prime);
508 if (ret)
510 fprintf (stderr, "gnutls_dh_get_group %d\n", ret);
511 goto out;
514 ret = gnutls_dh_params_init (&dh_params);
515 if (ret)
517 fprintf (stderr, "gnutls_dh_params_init %d\n", ret);
518 goto out;
521 ret =
522 gnutls_dh_params_import_raw (dh_params, &raw_prime,
523 &raw_gen);
524 if (ret)
526 fprintf (stderr, "gnutls_dh_params_import_raw %d\n", ret);
527 goto out;
530 ret = gnutls_dh_params_export_pkcs3 (dh_params,
531 GNUTLS_X509_FMT_PEM,
532 params_data,
533 &params_data_size);
534 if (ret != GNUTLS_E_SHORT_MEMORY_BUFFER)
536 fprintf (stderr, "gnutls_dh_params_export_pkcs3 %d\n",
537 ret);
538 goto out;
541 params_data = gnutls_malloc (params_data_size);
542 if (!params_data)
544 fprintf (stderr, "gnutls_malloc %d\n", ret);
545 goto out;
548 ret = gnutls_dh_params_export_pkcs3 (dh_params,
549 GNUTLS_X509_FMT_PEM,
550 params_data,
551 &params_data_size);
552 if (ret)
554 fprintf (stderr, "gnutls_dh_params_export_pkcs3-2 %d\n",
555 ret);
556 goto out;
559 printf (" - PKCS#3 format:\n\n%.*s\n", (int) params_data_size,
560 params_data);
562 out:
563 gnutls_free (params_data);
564 gnutls_free (raw_prime.data);
565 gnutls_free (raw_gen.data);
566 gnutls_dh_params_deinit (dh_params);
570 static void
571 print_ecdh_info (gnutls_session_t session, const char *str)
573 int curve;
575 printf ("- %sEC Diffie-Hellman parameters\n", str);
577 curve = gnutls_ecc_curve_get (session);
579 printf (" - Using curve: %s\n", gnutls_ecc_curve_get_name (curve));
580 printf (" - Curve size: %d bits\n",
581 gnutls_ecc_curve_get_size (curve) * 8);
586 print_info (gnutls_session_t session, int verbose, int print_cert)
588 const char *tmp;
589 gnutls_credentials_type_t cred;
590 gnutls_kx_algorithm_t kx;
591 unsigned char session_id[33];
592 size_t session_id_size = sizeof (session_id);
594 /* print session ID */
595 gnutls_session_get_id (session, session_id, &session_id_size);
596 printf ("- Session ID: %s\n",
597 raw_to_string (session_id, session_id_size));
599 /* print the key exchange's algorithm name
601 kx = gnutls_kx_get (session);
603 cred = gnutls_auth_get_type (session);
604 switch (cred)
606 #ifdef ENABLE_ANON
607 case GNUTLS_CRD_ANON:
608 if (kx == GNUTLS_KX_ANON_ECDH)
609 print_ecdh_info (session, "Anonymous ");
610 else
611 print_dh_info (session, "Anonymous ", verbose);
612 break;
613 #endif
614 #ifdef ENABLE_SRP
615 case GNUTLS_CRD_SRP:
616 /* This should be only called in server
617 * side.
619 if (gnutls_srp_server_get_username (session) != NULL)
620 printf ("- SRP authentication. Connected as '%s'\n",
621 gnutls_srp_server_get_username (session));
622 break;
623 #endif
624 #ifdef ENABLE_PSK
625 case GNUTLS_CRD_PSK:
626 /* This returns NULL in server side.
628 if (gnutls_psk_client_get_hint (session) != NULL)
629 printf ("- PSK authentication. PSK hint '%s'\n",
630 gnutls_psk_client_get_hint (session));
631 /* This returns NULL in client side.
633 if (gnutls_psk_server_get_username (session) != NULL)
634 printf ("- PSK authentication. Connected as '%s'\n",
635 gnutls_psk_server_get_username (session));
636 if (kx == GNUTLS_KX_DHE_PSK)
637 print_dh_info (session, "Ephemeral ", verbose);
638 if (kx == GNUTLS_KX_ECDHE_PSK)
639 print_ecdh_info (session, "Ephemeral ");
640 break;
641 #endif
642 case GNUTLS_CRD_IA:
643 printf ("- TLS/IA authentication\n");
644 break;
645 case GNUTLS_CRD_CERTIFICATE:
647 char dns[256];
648 size_t dns_size = sizeof (dns);
649 unsigned int type;
651 /* This fails in client side */
652 if (gnutls_server_name_get
653 (session, dns, &dns_size, &type, 0) == 0)
655 printf ("- Given server name[%d]: %s\n", type, dns);
659 if (print_cert)
660 print_cert_info (session, verbose, print_cert);
662 if (kx == GNUTLS_KX_DHE_RSA || kx == GNUTLS_KX_DHE_DSS)
663 print_dh_info (session, "Ephemeral ", verbose);
664 else if (kx == GNUTLS_KX_ECDHE_RSA
665 || kx == GNUTLS_KX_ECDHE_ECDSA)
666 print_ecdh_info (session, "Ephemeral ");
669 tmp =
670 SU (gnutls_protocol_get_name
671 (gnutls_protocol_get_version (session)));
672 printf ("- Version: %s\n", tmp);
674 tmp = SU (gnutls_kx_get_name (kx));
675 printf ("- Key Exchange: %s\n", tmp);
677 tmp = SU (gnutls_cipher_get_name (gnutls_cipher_get (session)));
678 printf ("- Cipher: %s\n", tmp);
680 tmp = SU (gnutls_mac_get_name (gnutls_mac_get (session)));
681 printf ("- MAC: %s\n", tmp);
683 tmp =
684 SU (gnutls_compression_get_name
685 (gnutls_compression_get (session)));
686 printf ("- Compression: %s\n", tmp);
688 if (verbose)
690 gnutls_datum_t cb;
691 int rc;
693 rc = gnutls_session_channel_binding (session,
694 GNUTLS_CB_TLS_UNIQUE, &cb);
695 if (rc)
696 fprintf (stderr, "Channel binding error: %s\n",
697 gnutls_strerror (rc));
698 else
700 size_t i;
702 printf ("- Channel binding 'tls-unique': ");
703 for (i = 0; i < cb.size; i++)
704 printf ("%02x", cb.data[i]);
705 printf ("\n");
709 /* Warning: Do not print anything more here. The 'Compression:'
710 output MUST be the last non-verbose output. This is used by
711 Emacs starttls.el code. */
713 fflush (stdout);
715 return 0;
718 void
719 print_cert_info (gnutls_session_t session, int verbose, int print_cert)
721 int flag;
723 if (verbose) flag = GNUTLS_CRT_PRINT_FULL;
724 else flag = GNUTLS_CRT_PRINT_COMPACT;
726 if (gnutls_certificate_client_get_request_status (session) != 0)
727 printf ("- Server has requested a certificate.\n");
729 switch (gnutls_certificate_type_get (session))
731 case GNUTLS_CRT_X509:
732 print_x509_info (session, flag, print_cert);
733 break;
734 #ifdef ENABLE_OPENPGP
735 case GNUTLS_CRT_OPENPGP:
736 print_openpgp_info (session, flag, print_cert);
737 break;
738 #endif
739 default:
740 printf ("Unknown type\n");
741 break;
745 void
746 print_cert_info_compact (gnutls_session_t session)
749 if (gnutls_certificate_client_get_request_status (session) != 0)
750 printf ("- Server has requested a certificate.\n");
752 switch (gnutls_certificate_type_get (session))
754 case GNUTLS_CRT_X509:
755 print_x509_info_compact (session);
756 break;
757 #ifdef ENABLE_OPENPGP
758 case GNUTLS_CRT_OPENPGP:
759 print_openpgp_info_compact (session);
760 break;
761 #endif
762 default:
763 printf ("Unknown type\n");
764 break;
768 void
769 print_list (const char *priorities, int verbose)
771 size_t i;
772 int ret;
773 unsigned int idx;
774 const char *name;
775 const char *err;
776 unsigned char id[2];
777 gnutls_kx_algorithm_t kx;
778 gnutls_cipher_algorithm_t cipher;
779 gnutls_mac_algorithm_t mac;
780 gnutls_protocol_t version;
781 gnutls_priority_t pcache;
782 const unsigned int *list;
784 if (priorities != NULL)
786 printf ("Cipher suites for %s\n", priorities);
788 ret = gnutls_priority_init (&pcache, priorities, &err);
789 if (ret < 0)
791 fprintf (stderr, "Syntax error at: %s\n", err);
792 exit (1);
795 for (i = 0;; i++)
797 ret =
798 gnutls_priority_get_cipher_suite_index (pcache, i,
799 &idx);
800 if (ret == GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE)
801 break;
802 if (ret == GNUTLS_E_UNKNOWN_CIPHER_SUITE)
803 continue;
805 name =
806 gnutls_cipher_suite_info (idx, id, NULL, NULL, NULL,
807 &version);
809 if (name != NULL)
810 printf ("%-50s\t0x%02x, 0x%02x\t%s\n",
811 name, (unsigned char) id[0],
812 (unsigned char) id[1],
813 gnutls_protocol_get_name (version));
816 printf("\n");
818 ret = gnutls_priority_certificate_type_list (pcache, &list);
820 printf ("Certificate types: ");
821 if (ret == 0) printf("none\n");
822 for (i = 0; i < (unsigned)ret; i++)
824 printf ("CTYPE-%s",
825 gnutls_certificate_type_get_name (list[i]));
826 if (i+1!=(unsigned)ret)
827 printf (", ");
828 else
829 printf ("\n");
834 ret = gnutls_priority_protocol_list (pcache, &list);
836 printf ("Protocols: ");
837 if (ret == 0) printf("none\n");
838 for (i = 0; i < (unsigned)ret; i++)
840 printf ("VERS-%s", gnutls_protocol_get_name (list[i]));
841 if (i+1!=(unsigned)ret)
842 printf (", ");
843 else
844 printf ("\n");
849 ret = gnutls_priority_compression_list (pcache, &list);
851 printf ("Compression: ");
852 if (ret == 0) printf("none\n");
853 for (i = 0; i < (unsigned)ret; i++)
855 printf ("COMP-%s",
856 gnutls_compression_get_name (list[i]));
857 if (i+1!=(unsigned)ret)
858 printf (", ");
859 else
860 printf ("\n");
865 ret = gnutls_priority_ecc_curve_list (pcache, &list);
867 printf ("Elliptic curves: ");
868 if (ret == 0) printf("none\n");
869 for (i = 0; i < (unsigned)ret; i++)
871 printf ("CURVE-%s",
872 gnutls_ecc_curve_get_name (list[i]));
873 if (i+1!=(unsigned)ret)
874 printf (", ");
875 else
876 printf ("\n");
881 ret = gnutls_priority_sign_list (pcache, &list);
883 printf ("PK-signatures: ");
884 if (ret == 0) printf("none\n");
885 for (i = 0; i < (unsigned)ret; i++)
887 printf ("SIGN-%s",
888 gnutls_sign_algorithm_get_name (list[i]));
889 if (i+1!=(unsigned)ret)
890 printf (", ");
891 else
892 printf ("\n");
896 return;
899 printf ("Cipher suites:\n");
900 for (i = 0; (name = gnutls_cipher_suite_info
901 (i, id, &kx, &cipher, &mac, &version)); i++)
903 printf ("%-50s\t0x%02x, 0x%02x\t%s\n",
904 name,
905 (unsigned char) id[0], (unsigned char) id[1],
906 gnutls_protocol_get_name (version));
907 if (verbose)
908 printf ("\tKey exchange: %s\n\tCipher: %s\n\tMAC: %s\n\n",
909 gnutls_kx_get_name (kx),
910 gnutls_cipher_get_name (cipher),
911 gnutls_mac_get_name (mac));
914 printf("\n");
916 const gnutls_certificate_type_t *p =
917 gnutls_certificate_type_list ();
919 printf ("Certificate types: ");
920 for (; *p; p++)
922 printf ("CTYPE-%s", gnutls_certificate_type_get_name (*p));
923 if (*(p + 1))
924 printf (", ");
925 else
926 printf ("\n");
931 const gnutls_protocol_t *p = gnutls_protocol_list ();
933 printf ("Protocols: ");
934 for (; *p; p++)
936 printf ("VERS-%s", gnutls_protocol_get_name (*p));
937 if (*(p + 1))
938 printf (", ");
939 else
940 printf ("\n");
945 const gnutls_cipher_algorithm_t *p = gnutls_cipher_list ();
947 printf ("Ciphers: ");
948 for (; *p; p++)
950 printf ("%s", gnutls_cipher_get_name (*p));
951 if (*(p + 1))
952 printf (", ");
953 else
954 printf ("\n");
959 const gnutls_mac_algorithm_t *p = gnutls_mac_list ();
961 printf ("MACs: ");
962 for (; *p; p++)
964 printf ("%s", gnutls_mac_get_name (*p));
965 if (*(p + 1))
966 printf (", ");
967 else
968 printf ("\n");
973 const gnutls_kx_algorithm_t *p = gnutls_kx_list ();
975 printf ("Key exchange algorithms: ");
976 for (; *p; p++)
978 printf ("%s", gnutls_kx_get_name (*p));
979 if (*(p + 1))
980 printf (", ");
981 else
982 printf ("\n");
987 const gnutls_compression_method_t *p = gnutls_compression_list ();
989 printf ("Compression: ");
990 for (; *p; p++)
992 printf ("COMP-%s", gnutls_compression_get_name (*p));
993 if (*(p + 1))
994 printf (", ");
995 else
996 printf ("\n");
1001 const gnutls_ecc_curve_t *p = gnutls_ecc_curve_list ();
1003 printf ("Elliptic curves: ");
1004 for (; *p; p++)
1006 printf ("CURVE-%s", gnutls_ecc_curve_get_name (*p));
1007 if (*(p + 1))
1008 printf (", ");
1009 else
1010 printf ("\n");
1015 const gnutls_pk_algorithm_t *p = gnutls_pk_list ();
1017 printf ("Public Key Systems: ");
1018 for (; *p; p++)
1020 printf ("%s", gnutls_pk_algorithm_get_name (*p));
1021 if (*(p + 1))
1022 printf (", ");
1023 else
1024 printf ("\n");
1029 const gnutls_sign_algorithm_t *p = gnutls_sign_list ();
1031 printf ("PK-signatures: ");
1032 for (; *p; p++)
1034 printf ("SIGN-%s", gnutls_sign_algorithm_get_name (*p));
1035 if (*(p + 1))
1036 printf (", ");
1037 else
1038 printf ("\n");
1043 int check_command(gnutls_session_t session, const char* str)
1045 size_t len = strnlen(str, 128);
1046 int ret;
1048 fprintf (stderr, "*** Processing %zu bytes command: %s\n", len, str);
1049 if (len > 2 && str[0] == str[1] && str[0] == '*')
1051 if (strncmp(str, "**REHANDSHAKE**", sizeof ("**REHANDSHAKE**") - 1) == 0)
1053 fprintf (stderr, "*** Sending rehandshake request\n");
1054 gnutls_rehandshake (session);
1055 return 1;
1056 } else if (strncmp(str, "**HEARTBEAT**", sizeof ("**HEARTBEAT**") - 1) == 0) {
1057 ret = gnutls_heartbeat_ping (session, 300, 5, GNUTLS_HEARTBEAT_WAIT);
1058 if (ret < 0)
1060 if (ret == GNUTLS_E_INVALID_REQUEST)
1062 fprintf(stderr, "No heartbeat in this session\n");
1064 else
1066 fprintf(stderr, "ping: %s\n", gnutls_strerror(ret));
1067 exit(1);
1070 return 2;
1073 return 0;
1076 #define MIN(x,y) ((x)<(y))?(x):(y)
1077 #define MAX_CACHE_TRIES 5
1079 pin_callback (void *user, int attempt, const char *token_url,
1080 const char *token_label, unsigned int flags, char *pin,
1081 size_t pin_max)
1083 const char *password;
1084 const char * desc;
1085 int len, cache = MAX_CACHE_TRIES;
1086 /* allow caching of PIN */
1087 static char *cached_url = NULL;
1088 static char cached_pin[32] = "";
1090 if (flags & GNUTLS_PIN_SO)
1091 desc = "security officer";
1092 else
1093 desc = "user";
1095 if (flags & GNUTLS_PIN_FINAL_TRY)
1097 cache = 0;
1098 printf ("*** This is the final try before locking!\n");
1100 if (flags & GNUTLS_PIN_COUNT_LOW)
1102 cache = 0;
1103 printf ("*** Only few tries left before locking!\n");
1106 if (flags & GNUTLS_PIN_WRONG)
1108 cache = 0;
1109 printf ("*** Wrong PIN has been provided!\n");
1112 if (cache > 0 && cached_url != NULL)
1114 if (strcmp (cached_url, token_url) == 0)
1116 if (strlen(pin) >= sizeof(cached_pin))
1118 fprintf (stderr, "Too long PIN given\n");
1119 exit (1);
1122 fprintf(stderr, "Re-using cached PIN for token '%s'\n", token_label);
1123 strcpy (pin, cached_pin);
1124 cache--;
1125 return 0;
1129 printf ("Token '%s' with URL '%s' ", token_label, token_url);
1130 printf ("requires %s PIN\n", desc);
1132 password = getpass ("Enter PIN: ");
1133 if (password == NULL || password[0] == 0)
1135 fprintf (stderr, "No password given\n");
1136 exit (1);
1139 len = MIN (pin_max, strlen (password));
1140 memcpy (pin, password, len);
1141 pin[len] = 0;
1143 /* cache */
1144 strcpy (cached_pin, pin);
1145 free (cached_url);
1146 cached_url = strdup (token_url);
1147 cache = MAX_CACHE_TRIES;
1149 return 0;
1152 #ifdef ENABLE_PKCS11
1154 static int
1155 token_callback (void *user, const char *label, const unsigned retry)
1157 char buf[32];
1159 if (retry > 0)
1161 fprintf (stderr, "Could not find token %s\n", label);
1162 return -1;
1164 printf ("Please insert token '%s' in slot and press enter\n", label);
1165 fgets (buf, sizeof (buf), stdin);
1167 return 0;
1170 void
1171 pkcs11_common (void)
1174 gnutls_pkcs11_set_pin_function (pin_callback, NULL);
1175 gnutls_pkcs11_set_token_function (token_callback, NULL);
1179 #endif