File:  [ELWIX - Embedded LightWeight unIX -] / embedtools / src / voucher.c
Revision 1.1.2.3: download - view: text, annotated - select for diffs - revision graph
Wed Jul 4 16:15:53 2012 UTC (11 years, 11 months ago) by misho
Branches: tools1_1
add openssl support

    1: #include "global.h"
    2: #include <openssl/pem.h>
    3: #include <openssl/rsa.h>
    4: #include <openssl/err.h>
    5: 
    6: io_enableDEBUG;
    7: 
    8: cfg_root_t cfg;
    9: FILE *output;
   10: char szConfig[MAXPATHLEN] = VOUCHER_CFG;
   11: extern char compiled[], compiledby[], compilehost[];
   12: 
   13: 
   14: static void
   15: Usage()
   16: {
   17: 	printf(	" -= VOUCHER =- management tool\n"
   18: 		"=== %s === %s@%s ===\n\n"
   19: 		"Syntax: voucher [options] -r <RollID> [count]\n"
   20: 		"\tvoucher [options] -t <voucher> [voucher [voucher ...]]\n\n"
   21: 		"\t-v\t\tVerbose (more -v more verbosity)\n"
   22: 		"\t-r\t\tRequest new voucher(s) mode\n"
   23: 		"\t-t\t\tTest voucher(s) mode\n"
   24: 		"\t-g\t\tRequest new RSA pair mode\n"
   25: 		"\t-c <config>\tConfig file\n"
   26: 		"\t-o <output>\tOutput file [default=-]\n"
   27: 		"\n", compiled, compiledby, compilehost);
   28: }
   29: 
   30: static void
   31: AtExit()
   32: {
   33: 	if (output != stdout)
   34: 		fclose(output);
   35: }
   36: 
   37: static inline int
   38: RedirOutput(const char *name)
   39: {
   40: 	AtExit();
   41: 
   42: 	if (strcmp(name, "-")) {
   43: 		output = fopen(name, "w+");
   44: 		if (!output) {
   45: 			printf("Error:: can't redirect output #%d - %s\n", 
   46: 					errno, strerror(errno));
   47: 			return -1;
   48: 		}
   49: 	} else
   50: 		output = stdout;
   51: 	return 0;
   52: }
   53: 
   54: static int
   55: NewRSA()
   56: {
   57: 	RSA *k = RSA_generate_key(VOUCHER_MAX_RSA * 8, 65537, NULL, NULL);
   58: 	if (!k) {
   59: 		printf("Error:: can't generate RSA key\n");
   60: 		return 2;
   61: 	}
   62: 
   63: 	PEM_write_RSAPrivateKey(output, k, NULL, NULL, 0, NULL, NULL);
   64: 	PEM_write_RSA_PUBKEY(output, k);
   65: 	return 0;
   66: }
   67: 
   68: 
   69: int
   70: main(int argc, char **argv)
   71: {
   72: 	char ch, mode = 0;
   73: 	int rid, cnt = 1;
   74: 
   75: 	output = stdout;
   76: 	atexit(AtExit);
   77: 
   78: 	while ((ch = getopt(argc, argv, "hvrtgc:o:")) != -1)
   79: 		switch (ch) {
   80: 			case 'r':
   81: 				mode = 1;
   82: 				break;
   83: 			case 't':
   84: 				mode = 2;
   85: 				break;
   86: 			case 'g':
   87: 				return NewRSA();
   88: 			case 'c':
   89: 				strlcpy(szConfig, optarg, sizeof szConfig);
   90: 				break;
   91: 			case 'o':
   92: 				RedirOutput(optarg);
   93: 				break;
   94: 			case 'v':
   95: 				io_incDebug;
   96: 				break;
   97: 			case 'h':
   98: 			default:
   99: 				Usage();
  100: 				return 1;
  101: 		}
  102: 	argc -= optind;
  103: 	argv += optind;
  104: 	if (!argc || !mode) {
  105: 		printf("Error:: not enough parameter or unspecified mode ...\n\n");
  106: 		Usage();
  107: 		return 1;
  108: 	}
  109: 	if (cfgLoadConfig(szConfig, &cfg)) {
  110: 		printf("Error:: load config #%d - %s\n", cfg_GetErrno(), cfg_GetError());
  111: 		return 3;
  112: 	}
  113: 
  114: 	cfgUnloadConfig(&cfg);
  115: 	return 0;
  116: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>