// sign a hash of input using private key void sign (void) { char *p; // initialize crypto API if (open_crypt()) { // import our private key if (open_key (RSA_PRIVATE_BIN)) { // hash the input if (open_hash ()) { // obtain size of signature CryptSignHash (hHash, AT_SIGNATURE, NULL, 0, NULL, &dwSigLen); pbSignature=xmalloc (dwSigLen); // sign the hash to obtain signature if (CryptSignHash (hHash, AT_SIGNATURE, NULL, 0, pbSignature, &dwSigLen)) { p=sig2hex(); if (p) { printf (" [ signature is: %i::%s\n", lstrlen(p), p); } xfree (pbSignature); } else { xstrerror ("CryptSignHash()"); } close_hash(); } else { xstrerror ("open_hash()"); } close_key(); } else { xstrerror ("open_key()"); } close_crypt(); } else { xstrerror ("open_crypt()"); } }
// verify a signature using public key BOOL verify(void) { BOOL bStatus = FALSE; // initialize crypto API if (open_crypt()) { // import public key if (open_key(DSA_PUBLIC_BIN)) { // hash the input if (open_hash()) { // convert signature to binary sig2bin(); if (pbSignature != NULL) { // verify signature bStatus = CryptVerifySignature(hHash, pbSignature, dwSigLen, hKey, NULL, 0); printf(" [ signature is %s\n", bStatus ? "valid" : "invalid"); xfree(pbSignature); } close_hash(); } else { printf("open_hash()"); } close_key(); } else { printf("open_key()"); } close_crypt(); } else { printf("open_crypt()"); } return bStatus; }
int main(int argc, char **argv) { u_char print_version = 0; char hostname[DCC_MAXDOMAINLEN]; int file_num; DCC_CK_TYPES type; char tbuf[80]; const char *cp, *cp0; struct timeval tv1, tv2; int us; struct tm tm; char *p; u_long l; int i; dcc_syslog_init(0, argv[0], 0); while ((i = getopt(argc, argv, "vVHDG:h:s:C:I:A:L:P:T:")) != -1) { switch (i) { case 'v': ++verbose; break; case 'V': dcc_version_print(); print_version = 1; break; case 'G': if (!strcasecmp(optarg, "on")) { grey_on = 1; } else if (!strcasecmp(optarg, "off")) { grey_on = 0; } else { usage(); } break; case 'h': homedir = optarg; break; case 's': l = strtoul(optarg, &p, 10); if ((*p != '\0' && *p != ',') || !DCC_ID_SRVR_NORMAL(l)) dcc_logbad(EX_USAGE, "invalid DCC ID \"-s %s\"", optarg); srvr.clnt_id = l; if (*p != '\0') { ++p; p += strspn(p, DCC_WHITESPACE); } hostname[0] = '\0'; srvr.port = 0; if (*p == '\0') break; cp = dcc_parse_nm_port(dcc_emsg, p, srvr.port, hostname, sizeof(hostname), &srvr.port, 0, 0, 0, 0); if (!cp) dcc_logbad(EX_USAGE, "%s", dcc_emsg); cp += strspn(cp, DCC_WHITESPACE); if (*cp != '\0') dcc_logbad(EX_USAGE, "unrecognized port number in" "\"-s %s\"", optarg); if (hostname[0] != '\0') BUFCPY(srvr.hostname, hostname); break; case 'H': no_hash = 1; break; case 'D': no_data = 1; break; case 'C': if (num_search_cksums >= DIM(search_cksums)) { dcc_error_msg("too many -C checksums"); break; } matching = 1; cp0 = optarg; /* separate checksum type and checksum in cp and tbuf */ cp = dcc_parse_word(0, tbuf, sizeof(tbuf), optarg, "checksum type", 0, 0); if (!cp) exit(1); if (!strcasecmp(tbuf, "hex")) { /* ignore "hex" */ cp0 = cp; cp = dcc_parse_word(0, tbuf, sizeof(tbuf), cp, "checksum type", 0, 0); if (!cp) dcc_logbad(EX_USAGE, "unrecognized checksum" " \"-C %s\"", optarg); } if (*cp == '\0') { /* allow bare checksum type */ type = dcc_str2type_del(tbuf, -1); if (type == DCC_CK_INVALID) dcc_logbad(EX_USAGE, "unrecognized checksum type" " \"-C %s\"", optarg); search_cksums[num_search_cksums].type = type; memset(&search_cksums[num_search_cksums].sum, 0, sizeof(DCC_SUM)); search_cksums[num_search_cksums].type_only = 1; ++num_search_cksums; break; } /* allow missing checksum type */ l = strtoul(tbuf, &p, 16); if (*p == '\0') { if (0 >= dcc_parse_hex_ck(dcc_emsg, 0, "-", DCC_CK_FLOD_PATH, cp0, 0, save_cksum)) dcc_logbad(EX_USAGE, "%s", dcc_emsg); break; } type = dcc_str2type_del(tbuf, -1); if (type == DCC_CK_FLOD_PATH) dcc_logbad(EX_USAGE, "unrecognized checksum type" " \"-C %s\"", optarg); if (1 <= dcc_parse_hex_ck(dcc_emsg, 0, tbuf, type, cp, 0, save_cksum)) break; /* allow strings for server-IDs */ if (type == DCC_CK_SRVR_ID && (i = strlen(cp)) <= ISZ(DCC_SUM)) { DCC_SUM name; memset(&name, 0, sizeof(name)); memcpy(&name, cp, i); save_cksum(0, 0, type, &name, 0); break; } dcc_logbad(EX_USAGE, "%s", dcc_emsg); break; case 'I': if (num_search_ids >= DIM(search_ids)) { dcc_error_msg("too many -I IDs"); break; } search_ids[num_search_ids] = strtoul(optarg, &p, 10); if (search_ids[num_search_ids] > DCC_SRVR_ID_MAX || *p != '\0') dcc_logbad(EX_USAGE, "invalid server-ID \"-I %s\"", optarg); ++num_search_ids; matching = 1; break; case 'A': dbaddr = strtoul(optarg, &p, 16); if (*p != '\0') dcc_logbad(EX_USAGE, "invalid database address \"%s\"", optarg); matching = 1; break; case 'L': max_pathlen = strtoul(optarg, &p, 10); if (*p != '\0') dcc_logbad(EX_USAGE, "invalid path length \"%s\"", optarg); matching = 1; break; case 'P': page_offset = strtoul(optarg, &p, 10); if (*p != '\0') dcc_logbad(EX_USAGE, "invalid number of pages \"%s\"", optarg); matching = 1; break; case 'T': if (num_search_ts >= DIM(search_ts)) { dcc_error_msg("too many -T timestamps"); break; } memset(&tm, 0, sizeof(tm)); i = sscanf(optarg, "%d/%d/%d %d:%d:%d.%d%c", &tm.tm_year, &tm.tm_mon, &tm.tm_mday, &tm.tm_hour, &tm.tm_min, &tm.tm_sec, &us, tbuf); if (i < 6 || i > 7 || tm.tm_mon <= 0) dcc_logbad(EX_USAGE,"bad timestamp \"%s\"", optarg); --tm.tm_mon; tm.tm_year += 100; tv1.tv_sec = DCC_TIMEGM(&tm); if (tv1.tv_sec < 0) dcc_logbad(EX_USAGE, "invalid timestamp \"%s\"", optarg); tv2.tv_sec = tv1.tv_sec; if (i == 7) { if (us >= DCC_US) dcc_logbad(EX_USAGE, "invalid microseconds" " in \"%s\"", optarg); tv1.tv_usec = us; tv2.tv_usec = us; } else { tv1.tv_usec = 0; tv2.tv_usec = DCC_US-1; } timeval2ts(&search_ts[num_search_ts].lo, &tv1, 0); timeval2ts(&search_ts[num_search_ts].hi, &tv2, 0); ++num_search_ts; matching = 1; break; default: usage(); } } argc -= optind; argv += optind; def_argv[0] = grey_on ? grey_db_nm : dcc_db_nm; if (argc == 0) { if (print_version) exit(EX_OK); argv = def_argv; argc = 1; } dcc_clnt_unthread_init(); if (!dcc_cdhome(dcc_emsg, homedir, 1)) dcc_logbad(emsg_ex_code(dcc_emsg), "%s", dcc_emsg); flod_mmap_path_set(); if (matching) { if (no_data && no_hash) dcc_logbad(EX_USAGE, "patterns need data or hash table"); if (!no_data && !no_hash) no_hash = 1; } if (dbaddr != 0 && page_offset != 0) dcc_logbad(EX_USAGE, "-P and -A are incompatible"); if (srvr.clnt_id != 0) { if (argc != 1) dcc_logbad(EX_USAGE, "lock only one file"); i = load_ids(dcc_emsg, srvr.clnt_id, &srvr_clnt_tbl, 1, verbose > 4); if (!srvr_clnt_tbl) dcc_logbad(emsg_ex_code(dcc_emsg), "%s", dcc_emsg); if (i <= 0) dcc_error_msg("%s", dcc_emsg); memcpy(srvr.passwd, srvr_clnt_tbl->cur_passwd, sizeof(srvr.passwd)); if (srvr.hostname[0] == '\0') BUFCPY(srvr.hostname, "127.0.0.1"); if (srvr.port == 0) srvr.port = DCC_GREY2PORT(grey_on); i = DCC_CLNT_FG_SLOW; if (grey_on) i |= DCC_CLNT_FG_GREY; ctxt = dcc_tmp_clnt_init(dcc_emsg, 0, &srvr, 0, 0, i, 0); if (!ctxt) dcc_logbad(emsg_ex_code(dcc_emsg), "%s", dcc_emsg); if (!lock_dbclean(dcc_emsg, *argv)) dcc_logbad(emsg_ex_code(dcc_emsg), "%s: dbclean running?", dcc_emsg); atexit(rel_db); signal(SIGALRM, sigterm); signal(SIGHUP, sigterm); signal(SIGTERM, sigterm); signal(SIGINT, sigterm); if (!dcc_aop_persist(dcc_emsg, ctxt, grey_on ? DCC_CLNT_FG_GREY : 0, verbose != 0, DCC_AOP_DB_UNLOAD, 0, 60*5, &aop_resp)) dcc_logbad(emsg_ex_code(dcc_emsg), "%s", dcc_emsg); } for (file_num = 1; *argv != 0; ++argv, ++file_num) { if (fd_db >= 0) close(fd_db); if (fd_hash >= 0) close(fd_hash); BUFCPY(db_nm, *argv); snprintf(hash_nm, sizeof(hash_nm), "%s"DB_HASH_SUFFIX, db_nm); if (file_num != 1) fputc('\n', stdout); if (verbose || argc > 1) printf(" %s\n", db_nm); /* try to open the hash table and the database * fail only if we cannot open the database */ open_hash(); if (!open_db()) continue; /* print the header of the database followed by its contents */ list_db(); list_hash(); } exit(EX_OK); }