"Fossies" - the Fresh Open Source Software Archive

Member "bind-9.11.23/bin/tools/nsec3hash.c" (7 Sep 2020, 3039 Bytes) of package /linux/misc/dns/bind9/9.11.23/bind-9.11.23.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "nsec3hash.c" see the Fossies "Dox" file reference documentation.

    1 /*
    2  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
    3  *
    4  * This Source Code Form is subject to the terms of the Mozilla Public
    5  * License, v. 2.0. If a copy of the MPL was not distributed with this
    6  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
    7  *
    8  * See the COPYRIGHT file distributed with this work for additional
    9  * information regarding copyright ownership.
   10  */
   11 
   12 #include <config.h>
   13 
   14 #include <stdbool.h>
   15 #include <stdlib.h>
   16 #include <stdarg.h>
   17 
   18 #include <isc/base32.h>
   19 #include <isc/buffer.h>
   20 #include <isc/hex.h>
   21 #include <isc/iterated_hash.h>
   22 #include <isc/print.h>
   23 #include <isc/result.h>
   24 #include <isc/string.h>
   25 #include <isc/types.h>
   26 
   27 #include <dns/fixedname.h>
   28 #include <dns/name.h>
   29 #include <dns/nsec3.h>
   30 #include <dns/types.h>
   31 
   32 const char *program = "nsec3hash";
   33 
   34 ISC_PLATFORM_NORETURN_PRE static void
   35 fatal(const char *format, ...) ISC_PLATFORM_NORETURN_POST;
   36 
   37 static void
   38 fatal(const char *format, ...) {
   39     va_list args;
   40 
   41     fprintf(stderr, "%s: ", program);
   42     va_start(args, format);
   43     vfprintf(stderr, format, args);
   44     va_end(args);
   45     fprintf(stderr, "\n");
   46     exit(1);
   47 }
   48 
   49 static void
   50 check_result(isc_result_t result, const char *message) {
   51     if (result != ISC_R_SUCCESS)
   52         fatal("%s: %s", message, isc_result_totext(result));
   53 }
   54 
   55 static void
   56 usage(void) {
   57     fprintf(stderr, "Usage: %s salt algorithm iterations domain\n",
   58         program);
   59     exit(1);
   60 }
   61 
   62 int
   63 main(int argc, char **argv) {
   64     dns_fixedname_t fixed;
   65     dns_name_t *name;
   66     isc_buffer_t buffer;
   67     isc_region_t region;
   68     isc_result_t result;
   69     unsigned char hash[NSEC3_MAX_HASH_LENGTH];
   70     unsigned char salt[DNS_NSEC3_SALTSIZE];
   71     unsigned char text[1024];
   72     unsigned int hash_alg;
   73     unsigned int length;
   74     unsigned int iterations;
   75     unsigned int salt_length;
   76 
   77     if (argc != 5)
   78         usage();
   79 
   80     if (strcmp(argv[1], "-") == 0) {
   81         salt_length = 0;
   82         salt[0] = 0;
   83     } else {
   84         isc_buffer_init(&buffer, salt, sizeof(salt));
   85         result = isc_hex_decodestring(argv[1], &buffer);
   86         check_result(result, "isc_hex_decodestring(salt)");
   87         salt_length = isc_buffer_usedlength(&buffer);
   88         if (salt_length > DNS_NSEC3_SALTSIZE)
   89             fatal("salt too long");
   90     }
   91     hash_alg = atoi(argv[2]);
   92     if (hash_alg > 255U)
   93         fatal("hash algorithm too large");
   94     iterations = atoi(argv[3]);
   95     if (iterations > 0xffffU)
   96         fatal("iterations to large");
   97 
   98     name = dns_fixedname_initname(&fixed);
   99     isc_buffer_init(&buffer, argv[4], strlen(argv[4]));
  100     isc_buffer_add(&buffer, strlen(argv[4]));
  101     result = dns_name_fromtext(name, &buffer, dns_rootname, 0, NULL);
  102     check_result(result, "dns_name_fromtext() failed");
  103 
  104     dns_name_downcase(name, name, NULL);
  105     length = isc_iterated_hash(hash, hash_alg, iterations,  salt,
  106                    salt_length, name->ndata, name->length);
  107     if (length == 0)
  108         fatal("isc_iterated_hash failed");
  109     region.base = hash;
  110     region.length = length;
  111     isc_buffer_init(&buffer, text, sizeof(text));
  112     isc_base32hexnp_totext(&region, 1, "", &buffer);
  113     fprintf(stdout, "%.*s (salt=%s, hash=%u, iterations=%u)\n",
  114         (int)isc_buffer_usedlength(&buffer), text, argv[1], hash_alg, iterations);
  115     return(0);
  116 }