File: radsecproxy-hash.c

package info (click to toggle)
radsecproxy 1.6.5-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,216 kB
  • ctags: 627
  • sloc: ansic: 7,779; sh: 4,145; xml: 1,012; makefile: 75
file content (91 lines) | stat: -rw-r--r-- 1,998 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
 * Copyright (C) 2011,2013 NORDUnet A/S
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include "radsecproxy.h"
#include "fticks_hashmac.h"

void
usage()
{
  fprintf(stderr,
	  "usage: radsecproxy-hash [-h] [-k key] [-t type]\n"
#if defined (READ_CONFIG)
	  "   -c configfile\tuse configuration from CONFIGFILE\n"
#endif
	  "   -h\t\t\tdisplay this help and exit\n"
	  "   -k key\t\tuse KEY for HMAC\n"
	  "   -t type\t\tprint digest of type TYPE [hash|hmac]\n");
  exit(1);
}

#define MYNAME "radsecproxy-hash"

int
main(int argc, char *argv[])
{
  int opt;
#if defined(READ_CONFIG)
  char *config = NULL;
#endif
  uint8_t buf[64+1];
  char mac[80+1];
  uint8_t *key = NULL;
  enum { TYPE_HASH, TYPE_HMAC } type = TYPE_HASH;

  while ((opt = getopt(argc, argv, "hk:t:")) != -1) {
    switch (opt) {
#if defined(READ_CONFIG)
    case 'c':
      config = optarg;
      break;
#endif
    case 'h':
      usage();
    case 'k':
      key = (uint8_t *) optarg;
      break;
    case 't':
      if (strcmp(optarg, "hash") == 0)
	type = TYPE_HASH;
      else if (strcmp(optarg, "hmac") == 0)
	type = TYPE_HMAC;
      else
	usage();
      break;
    default:
      usage();
    }
  }

  while (fgets(mac, sizeof(mac), stdin) != NULL) {
    if (type == TYPE_HASH) {
      if (fticks_hashmac((uint8_t *) mac, NULL, sizeof(buf), buf) != 0) {
	fprintf(stderr, "%s: out of memory\n", MYNAME);
	return 3;
      }
    }
    else if (type == TYPE_HMAC) {
      if (key == NULL) {
	fprintf(stderr, "%s: generating HMAC requires a key, use `-k'\n",
		MYNAME);
	return 2;
      }
      if (fticks_hashmac((uint8_t *) mac, key, sizeof(buf), buf) != 0) {
	fprintf(stderr, "%s: out of memory\n", MYNAME);
	return 3;
      }
    }
    puts((const char *) buf);
  }

  return 0;
}