news update
[gnutls.git] / src / danetool.c
blobcd8211ecd9d090c72dd7a74bb5ee1c52296a208f
1 /*
2 * Copyright (C) 2012 Free Software Foundation, Inc.
4 * This file is part of GnuTLS.
6 * GnuTLS is free software: you can redistribute it and/or modify it
7 * 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, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * 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
18 * <http://www.gnu.org/licenses/>.
21 #include <config.h>
23 #include <gnutls/gnutls.h>
24 #include <gnutls/x509.h>
25 #include <gnutls/openpgp.h>
26 #include <gnutls/pkcs12.h>
27 #include <gnutls/pkcs11.h>
28 #include <gnutls/abstract.h>
29 #include <gnutls/crypto.h>
30 #include <gnutls/dane.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <ctype.h>
36 #include <time.h>
37 #include <unistd.h>
38 #include <errno.h>
39 #include <sys/types.h>
40 #include <sys/stat.h>
41 #include <fcntl.h>
42 #include <error.h>
44 /* Gnulib portability files. */
45 #include <read-file.h>
46 #include <progname.h>
47 #include <version-etc.h>
49 #include <common.h>
50 #include "danetool-args.h"
51 #include "certtool-common.h"
53 static void cmd_parser (int argc, char **argv);
54 static void dane_info(const char* host, const char* proto, unsigned int port,
55 unsigned int ca, unsigned int local, common_info_st * cinfo);
57 static void dane_check(const char* host, const char* proto, unsigned int port,
58 common_info_st * cinfo);
60 FILE *outfile;
61 static gnutls_digest_algorithm_t default_dig;
63 /* non interactive operation if set
65 int batch;
68 static void
69 tls_log_func (int level, const char *str)
71 fprintf (stderr, "|<%d>| %s", level, str);
74 int
75 main (int argc, char **argv)
77 set_program_name (argv[0]);
78 cmd_parser (argc, argv);
80 return 0;
84 static void
85 cmd_parser (int argc, char **argv)
87 int ret, privkey_op = 0;
88 common_info_st cinfo;
89 const char* proto = "tcp";
90 unsigned int port = 443;
92 optionProcess( &danetoolOptions, argc, argv);
94 if (HAVE_OPT(OUTFILE))
96 outfile = safe_open_rw (OPT_ARG(OUTFILE), privkey_op);
97 if (outfile == NULL)
98 error (EXIT_FAILURE, errno, "%s", OPT_ARG(OUTFILE));
100 else
101 outfile = stdout;
103 default_dig = GNUTLS_DIG_UNKNOWN;
104 if (HAVE_OPT(HASH))
106 if (strcasecmp (OPT_ARG(HASH), "md5") == 0)
108 fprintf (stderr,
109 "Warning: MD5 is broken, and should not be used any more for digital signatures.\n");
110 default_dig = GNUTLS_DIG_MD5;
112 else if (strcasecmp (OPT_ARG(HASH), "sha1") == 0)
113 default_dig = GNUTLS_DIG_SHA1;
114 else if (strcasecmp (OPT_ARG(HASH), "sha256") == 0)
115 default_dig = GNUTLS_DIG_SHA256;
116 else if (strcasecmp (OPT_ARG(HASH), "sha224") == 0)
117 default_dig = GNUTLS_DIG_SHA224;
118 else if (strcasecmp (OPT_ARG(HASH), "sha384") == 0)
119 default_dig = GNUTLS_DIG_SHA384;
120 else if (strcasecmp (OPT_ARG(HASH), "sha512") == 0)
121 default_dig = GNUTLS_DIG_SHA512;
122 else if (strcasecmp (OPT_ARG(HASH), "rmd160") == 0)
123 default_dig = GNUTLS_DIG_RMD160;
124 else
125 error (EXIT_FAILURE, 0, "invalid hash: %s", OPT_ARG(HASH));
128 gnutls_global_set_log_function (tls_log_func);
130 if (HAVE_OPT(DEBUG))
132 gnutls_global_set_log_level (OPT_VALUE_DEBUG);
133 printf ("Setting log level to %d\n", (int)OPT_VALUE_DEBUG);
136 if ((ret = gnutls_global_init ()) < 0)
137 error (EXIT_FAILURE, 0, "global_init: %s", gnutls_strerror (ret));
139 #ifdef ENABLE_PKCS11
140 pkcs11_common();
141 #endif
143 memset (&cinfo, 0, sizeof (cinfo));
145 if (HAVE_OPT(INDER) || HAVE_OPT(INRAW))
146 cinfo.incert_format = GNUTLS_X509_FMT_DER;
147 else
148 cinfo.incert_format = GNUTLS_X509_FMT_PEM;
150 if (HAVE_OPT(VERBOSE))
151 cinfo.verbose = 1;
153 if (HAVE_OPT(LOAD_PUBKEY))
154 cinfo.pubkey = OPT_ARG(LOAD_PUBKEY);
156 if (HAVE_OPT(LOAD_CERTIFICATE))
157 cinfo.cert = OPT_ARG(LOAD_CERTIFICATE);
159 if (HAVE_OPT(PORT))
160 port = OPT_VALUE_PORT;
161 if (HAVE_OPT(PROTO))
162 proto = OPT_ARG(PROTO);
164 if (HAVE_OPT(TLSA_RR))
165 dane_info (OPT_ARG(HOST), proto, port,
166 HAVE_OPT(CA), HAVE_OPT(LOCAL), &cinfo);
167 else if (HAVE_OPT(CHECK))
168 dane_check (OPT_ARG(CHECK), proto, port,
169 &cinfo);
170 else
171 USAGE(1);
173 fclose (outfile);
175 #ifdef ENABLE_PKCS11
176 gnutls_pkcs11_deinit ();
177 #endif
178 gnutls_global_deinit ();
181 static void dane_check(const char* host, const char* proto, unsigned int port,
182 common_info_st * cinfo)
184 dane_state_t s;
185 dane_query_t q;
186 int ret;
187 unsigned entries;
188 unsigned int flags = DANE_F_IGNORE_LOCAL_RESOLVER, i;
189 unsigned int usage, type, match;
190 gnutls_datum_t data, file;
191 size_t size;
193 if (ENABLED_OPT(LOCAL_DNS))
194 flags = 0;
196 printf("Querying %s (%s:%d)...\n", host, proto, port);
197 ret = dane_state_init(&s, flags);
198 if (ret < 0)
199 error (EXIT_FAILURE, 0, "dane_state_init: %s", dane_strerror (ret));
201 ret = dane_query_tlsa(s, &q, host, proto, port);
202 if (ret < 0)
203 error (EXIT_FAILURE, 0, "dane_query_tlsa: %s", dane_strerror (ret));
205 entries = dane_query_entries(q);
206 for (i=0;i<entries;i++)
208 ret = dane_query_data(q, i, &usage, &type, &match, &data);
209 if (ret < 0)
210 error (EXIT_FAILURE, 0, "dane_query_data: %s", dane_strerror (ret));
213 size = buffer_size;
214 ret = gnutls_hex_encode(&data, (void*)buffer, &size);
215 if (ret < 0)
216 error (EXIT_FAILURE, 0, "gnutls_hex_encode: %s", dane_strerror (ret));
218 if (entries > 1) printf("\nEntry %d:\n", i+1);
220 fprintf(outfile, "_%u._%s.%s. IN TLSA ( %.2x %.2x %.2x %s )\n", port, proto, host, usage, type, match, buffer);
221 printf("Certificate usage: %s (%.2x)\n", dane_cert_usage_name(usage), usage);
222 printf("Certificate type: %s (%.2x)\n", dane_cert_type_name(type), type);
223 printf("Contents: %s (%.2x)\n", dane_match_type_name(match), match);
224 printf("Data: %s\n", buffer);
226 /* Verify the DANE data */
227 if (cinfo->cert)
229 gnutls_x509_crt_t *clist;
230 unsigned int clist_size, status;
232 ret = gnutls_load_file(cinfo->cert, &file);
233 if (ret < 0)
234 error (EXIT_FAILURE, 0, "gnutls_load_file: %s", gnutls_strerror (ret));
236 ret = gnutls_x509_crt_list_import2( &clist, &clist_size, &file, cinfo->incert_format, 0);
237 if (ret < 0)
238 error (EXIT_FAILURE, 0, "gnutls_x509_crt_list_import2: %s", gnutls_strerror (ret));
240 if (clist_size > 0)
242 gnutls_datum_t certs[clist_size];
243 gnutls_datum_t out;
244 unsigned int i;
246 for (i=0;i<clist_size;i++)
248 ret = gnutls_x509_crt_export2( clist[i], GNUTLS_X509_FMT_DER, &certs[i]);
249 if (ret < 0)
250 error (EXIT_FAILURE, 0, "gnutls_x509_crt_export2: %s", gnutls_strerror (ret));
253 ret = dane_verify_crt( s, certs, clist_size, GNUTLS_CRT_X509,
254 host, proto, port, 0, 0, &status);
255 if (ret < 0)
256 error (EXIT_FAILURE, 0, "dane_verify_crt: %s", dane_strerror (ret));
258 ret = dane_verification_status_print(status, &out, 0);
259 if (ret < 0)
260 error (EXIT_FAILURE, 0, "dane_verification_status_print: %s", dane_strerror (ret));
262 printf("\nVerification: %s\n", out.data);
263 gnutls_free(out.data);
265 for (i=0;i<clist_size;i++)
267 gnutls_free(certs[i].data);
268 gnutls_x509_crt_deinit(clist[i]);
270 gnutls_free(clist);
276 dane_query_deinit(q);
277 dane_state_deinit(s);
281 static void dane_info(const char* host, const char* proto, unsigned int port,
282 unsigned int ca, unsigned int local, common_info_st * cinfo)
284 gnutls_pubkey_t pubkey;
285 gnutls_x509_crt_t crt;
286 unsigned char digest[64];
287 gnutls_datum_t t;
288 int ret;
289 unsigned int usage, selector, type;
290 size_t size;
292 if (proto == NULL)
293 proto = "tcp";
294 if (port == 0)
295 port = 443;
297 crt = load_cert (0, cinfo);
298 if (crt != NULL && HAVE_OPT(X509))
300 selector = 0; /* X.509 */
302 size = buffer_size;
303 ret = gnutls_x509_crt_export (crt, GNUTLS_X509_FMT_DER, buffer, &size);
304 if (ret < 0)
305 error (EXIT_FAILURE, 0, "export error: %s", gnutls_strerror (ret));
307 gnutls_x509_crt_deinit (crt);
309 else /* use public key only */
311 selector = 1;
313 ret = gnutls_pubkey_init (&pubkey);
314 if (ret < 0)
315 error (EXIT_FAILURE, 0, "pubkey_init: %s", gnutls_strerror (ret));
317 if (crt != NULL)
320 ret = gnutls_pubkey_import_x509 (pubkey, crt, 0);
321 if (ret < 0)
323 error (EXIT_FAILURE, 0, "pubkey_import_x509: %s",
324 gnutls_strerror (ret));
327 size = buffer_size;
328 ret = gnutls_pubkey_export (pubkey, GNUTLS_X509_FMT_DER, buffer, &size);
329 if (ret < 0)
331 error (EXIT_FAILURE, 0, "pubkey_export: %s",
332 gnutls_strerror (ret));
335 gnutls_x509_crt_deinit(crt);
337 else
339 pubkey = load_pubkey (1, cinfo);
341 size = buffer_size;
342 ret = gnutls_pubkey_export (pubkey, GNUTLS_X509_FMT_DER, buffer, &size);
343 if (ret < 0)
344 error (EXIT_FAILURE, 0, "export error: %s", gnutls_strerror (ret));
347 gnutls_pubkey_deinit (pubkey);
350 if (default_dig != GNUTLS_DIG_SHA256 && default_dig != GNUTLS_DIG_SHA512)
352 if (default_dig != GNUTLS_DIG_UNKNOWN) fprintf(stderr, "Unsupported digest. Assuming SHA256.\n");
353 default_dig = GNUTLS_DIG_SHA256;
356 ret = gnutls_hash_fast(default_dig, buffer, size, digest);
357 if (ret < 0)
358 error (EXIT_FAILURE, 0, "hash error: %s", gnutls_strerror (ret));
360 if (default_dig == GNUTLS_DIG_SHA256)
361 type = 1;
362 else type = 2;
364 /* DANE certificate classification crap */
365 if (local==0)
367 if (ca) usage = 0;
368 else usage = 1;
370 else
372 if (ca) usage = 2;
373 else usage = 3;
376 t.data = digest;
377 t.size = gnutls_hash_get_len(default_dig);
379 size = buffer_size;
380 ret = gnutls_hex_encode(&t, (void*)buffer, &size);
381 if (ret < 0)
382 error (EXIT_FAILURE, 0, "hex encode error: %s", gnutls_strerror (ret));
384 fprintf(outfile, "_%u._%s.%s. IN TLSA ( %.2x %.2x %.2x %s )\n", port, proto, host, usage, selector, type, buffer);