WIP: Fix the Proxy-State issue.
[libradsec.git] / radsecproxy-hash.c
1 /* Copyright (c) 2011,2013, NORDUnet A/S */
2 /* See LICENSE for licensing information. */
3
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <unistd.h>
7
8 #include "radsecproxy.h"
9 #include "fticks_hashmac.h"
10
11 void
12 usage()
13 {
14   fprintf(stderr,
15           "usage: radsecproxy-hash [-h] [-k key] [-t type]\n"
16 #if defined (READ_CONFIG)
17           "   -c configfile\tuse configuration from CONFIGFILE\n"
18 #endif
19           "   -h\t\t\tdisplay this help and exit\n"
20           "   -k key\t\tuse KEY for HMAC\n"
21           "   -t type\t\tprint digest of type TYPE [hash|hmac]\n");
22   exit(1);
23 }
24
25 #define MYNAME "radsecproxy-hash"
26
27 int
28 main(int argc, char *argv[])
29 {
30   int opt;
31 #if defined(READ_CONFIG)
32   char *config = NULL;
33 #endif
34   uint8_t buf[64+1];
35   char mac[80+1];
36   uint8_t *key = NULL;
37   enum { TYPE_HASH, TYPE_HMAC } type = TYPE_HASH;
38
39   while ((opt = getopt(argc, argv, "hk:t:")) != -1) {
40     switch (opt) {
41 #if defined(READ_CONFIG)
42     case 'c':
43       config = optarg;
44       break;
45 #endif
46     case 'h':
47       usage();
48     case 'k':
49       key = (uint8_t *) optarg;
50       break;
51     case 't':
52       if (strcmp(optarg, "hash") == 0)
53         type = TYPE_HASH;
54       else if (strcmp(optarg, "hmac") == 0)
55         type = TYPE_HMAC;
56       else
57         usage();
58       break;
59     default:
60       usage();
61     }
62   }
63
64   while (fgets(mac, sizeof(mac), stdin) != NULL) {
65     if (type == TYPE_HASH) {
66       if (fticks_hashmac((uint8_t *) mac, NULL, sizeof(buf), buf) != 0) {
67         fprintf(stderr, "%s: out of memory\n", MYNAME);
68         return 3;
69       }
70     }
71     else if (type == TYPE_HMAC) {
72       if (key == NULL) {
73         fprintf(stderr, "%s: generating HMAC requires a key, use `-k'\n",
74                 MYNAME);
75         return 2;
76       }
77       if (fticks_hashmac((uint8_t *) mac, key, sizeof(buf), buf) != 0) {
78         fprintf(stderr, "%s: out of memory\n", MYNAME);
79         return 3;
80       }
81     }
82     puts((const char *) buf);
83   }
84
85   return 0;
86 }
87
88 /* Local Variables: */
89 /* c-file-style: "stroustrup" */
90 /* End: */