/contrib/bind9/bin/tools/nsec3hash.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 122 lines · 90 code · 16 blank · 16 comment · 12 complexity · 22376bbc167ae29be21c607df165a5f5 MD5 · raw file

  1. /*
  2. * Copyright (C) 2006, 2008, 2009, 2011, 2012 Internet Systems Consortium, Inc. ("ISC")
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  9. * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  10. * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  11. * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  12. * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  13. * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  14. * PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. /* $Id$ */
  17. #include <config.h>
  18. #include <stdlib.h>
  19. #include <stdarg.h>
  20. #include <isc/base32.h>
  21. #include <isc/buffer.h>
  22. #include <isc/hex.h>
  23. #include <isc/iterated_hash.h>
  24. #include <isc/print.h>
  25. #include <isc/result.h>
  26. #include <isc/string.h>
  27. #include <isc/types.h>
  28. #include <dns/fixedname.h>
  29. #include <dns/name.h>
  30. #include <dns/nsec3.h>
  31. #include <dns/types.h>
  32. const char *program = "nsec3hash";
  33. ISC_PLATFORM_NORETURN_PRE static void
  34. fatal(const char *format, ...) ISC_PLATFORM_NORETURN_POST;
  35. static void
  36. fatal(const char *format, ...) {
  37. va_list args;
  38. fprintf(stderr, "%s: ", program);
  39. va_start(args, format);
  40. vfprintf(stderr, format, args);
  41. va_end(args);
  42. fprintf(stderr, "\n");
  43. exit(1);
  44. }
  45. static void
  46. check_result(isc_result_t result, const char *message) {
  47. if (result != ISC_R_SUCCESS)
  48. fatal("%s: %s", message, isc_result_totext(result));
  49. }
  50. static void
  51. usage() {
  52. printf("Usage: %s salt algorithm iterations domain\n", program);
  53. exit(1);
  54. }
  55. int
  56. main(int argc, char **argv) {
  57. dns_fixedname_t fixed;
  58. dns_name_t *name;
  59. isc_buffer_t buffer;
  60. isc_region_t region;
  61. isc_result_t result;
  62. unsigned char hash[NSEC3_MAX_HASH_LENGTH];
  63. unsigned char salt[DNS_NSEC3_SALTSIZE];
  64. unsigned char text[1024];
  65. unsigned int hash_alg;
  66. unsigned int length;
  67. unsigned int iterations;
  68. unsigned int salt_length;
  69. if (argc != 5)
  70. usage();
  71. if (strcmp(argv[1], "-") == 0) {
  72. salt_length = 0;
  73. salt[0] = 0;
  74. } else {
  75. isc_buffer_init(&buffer, salt, sizeof(salt));
  76. result = isc_hex_decodestring(argv[1], &buffer);
  77. check_result(result, "isc_hex_decodestring(salt)");
  78. salt_length = isc_buffer_usedlength(&buffer);
  79. if (salt_length > DNS_NSEC3_SALTSIZE)
  80. fatal("salt too long");
  81. }
  82. hash_alg = atoi(argv[2]);
  83. if (hash_alg > 255U)
  84. fatal("hash algorithm too large");
  85. iterations = atoi(argv[3]);
  86. if (iterations > 0xffffU)
  87. fatal("iterations to large");
  88. dns_fixedname_init(&fixed);
  89. name = dns_fixedname_name(&fixed);
  90. isc_buffer_init(&buffer, argv[4], strlen(argv[4]));
  91. isc_buffer_add(&buffer, strlen(argv[4]));
  92. result = dns_name_fromtext(name, &buffer, dns_rootname, 0, NULL);
  93. check_result(result, "dns_name_fromtext() failed");
  94. dns_name_downcase(name, name, NULL);
  95. length = isc_iterated_hash(hash, hash_alg, iterations, salt,
  96. salt_length, name->ndata, name->length);
  97. if (length == 0)
  98. fatal("isc_iterated_hash failed");
  99. region.base = hash;
  100. region.length = length;
  101. isc_buffer_init(&buffer, text, sizeof(text));
  102. isc_base32hex_totext(&region, 1, "", &buffer);
  103. fprintf(stdout, "%.*s (salt=%s, hash=%u, iterations=%u)\n",
  104. (int)isc_buffer_usedlength(&buffer), text, argv[1], hash_alg, iterations);
  105. return(0);
  106. }