⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 keymanagetest.c

📁 snmp up7
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * keymanagetest.c * * Expected SUCCESSes:  2 + 2 + 3 for all tests. * * Returns: *      Number of FAILUREs. *  * * FIX  Or how about passing a usmUser name and looking up the entry as *      a means of getting key material?  This means the userList is *      available from an application... * * ASSUMES  No key management functions return non-zero success codes. * * Test of generate_Ku().                       SUCCESSes: 2 * Test of generate_kul().                      SUCCESSes: 2 * Test of {encode,decode}_keychange().         SUCCESSes: 3 */static char    *rcsid = "$Id: keymanagetest.c,v 5.0 2002/04/20 07:30:22 hardaker Exp $";        /* */#include <net-snmp/net-snmp-config.h>#include <stdio.h>#ifdef HAVE_NETINET_IN_H#include <netinet/in.h>#endif#include "asn1.h"#include "snmp_api.h"#include "keytools.h"#include "tools.h"#include "scapi.h"#include "transform_oids.h"#include "callback.h"#include <stdlib.h>extern char    *optarg;extern int      optind, optopt, opterr;/* * Globals, &c... */char           *local_progname;#define USAGE	"Usage: %s [-h][-aklu][-E <engineID>][-N <newkey>][-O <oldkey>][-P <passphrase>]"#define OPTIONLIST	"aqE:hklN:O:P:u"int             doalltests = 0, dogenKu = 0, dogenkul = 0, dokeychange = 0;#define	ALLOPTIONS	(doalltests + dogenKu + dogenkul + dokeychange)#define LOCAL_MAXBUF	(1024 * 8)#define NL		"\n"#define OUTPUTALWAYS(o)	fprintf(stdout, "\n\n%s\n\n", o);#define OUTPUT(o)	if (!bequiet) { OUTPUTALWAYS(o); }#define SUCCESS(s)					\{							\	if (!failcount && !bequiet)					\		fprintf(stdout, "\nSUCCESS: %s\n", s);	\}#define FAILED(e, f)					\{							\	if (e != SNMPERR_SUCCESS && !bequiet) {			\		fprintf(stdout, "\nFAILED: %s\n", f);	\		failcount += 1;				\	}						\}/* * Test specific globals. */#define	ENGINEID_DEFAULT	"1.2.3.4wild"#define PASSPHRASE_DEFAULT	"Clay's Conclusion: Creativity is great, " \					"but plagiarism is faster."#define OLDKEY_DEFAULT		"This is a very old key."#define NEWKEY_DEFAULT		"This key, on the other hand, is very new."char           *engineID = NULL;char           *passphrase = NULL;char           *oldkey = NULL;char           *newkey = NULL;int             bequiet = 0;/* * Prototypes. */void            usage(FILE * ofp);int             test_genkul(void);int             test_genKu(void);int             test_keychange(void);intmain(int argc, char **argv){    int             rval = SNMPERR_SUCCESS, failcount = 0;    char            ch;    local_progname = argv[0];    optarg = NULL;    /*     * Parse.     */    while ((ch = getopt(argc, argv, OPTIONLIST)) != EOF) {        switch (ch) {        case 'a':            doalltests = 1;            break;        case 'E':            engineID = optarg;            break;        case 'k':            dokeychange = 1;            break;        case 'l':            dogenkul = 1;            break;        case 'N':            newkey = optarg;            break;        case 'O':            oldkey = optarg;            break;        case 'P':            passphrase = optarg;            break;        case 'u':            dogenKu = 1;            break;        case 'q':            bequiet = 1;            break;        case 'h':            rval = 0;        default:            usage(stdout);            exit(rval);        }        argc -= 1;        argv += 1;        if (optarg) {            argc -= 1;            argv += 1;        }        optind = 1;        optarg = NULL;    }                           /* endwhile getopt */    if ((argc > 1)) {        usage(stdout);        exit(1000);    } else if (ALLOPTIONS != 1) {        usage(stdout);        exit(1000);    }    /*     * Test stuff.     */    rval = sc_init();    FAILED(rval, "sc_init().");    if (dogenKu || doalltests) {        failcount += test_genKu();    }    if (dogenkul || doalltests) {        failcount += test_genkul();    }    if (dokeychange || doalltests) {        failcount += test_keychange();    }    /*     * Cleanup.     */    rval = sc_shutdown(SNMP_CALLBACK_LIBRARY, SNMP_CALLBACK_SHUTDOWN,                       NULL, NULL);    FAILED(rval, "sc_shutdown().");    return failcount;}                               /* end main() */voidusage(FILE * ofp){    fprintf(ofp,            USAGE            "" NL            "    -a			All tests." NL            "    -E [0x]<engineID>	snmpEngineID string."            NL            "    -k			Test {encode,decode}_keychange()."            NL            "    -l			generate_kul()."            NL            "    -h			Help."            NL            "    -N [0x]<newkey>	New key (for testing KeyChange TC)."            NL            "    -O [0x]<oldkey>	Old key (for testing KeyChange TC)."            NL            "    -P <passphrase>	Source string for usmUser master key."            NL            "    -u			generate_Ku()."            NL            "    -q			be quiet."            NL "" NL, local_progname);}                               /* end usage() */#ifdef EXAMPLE/*******************************************************************-o-****** * test_dosomething * * Test template. * * Returns: *	Number of failures. */inttest_dosomething(void){    int             rval = SNMPERR_SUCCESS, failcount = 0;    EM0(1, "UNIMPLEMENTED");    /* EM(1); /* */  test_dosomething_quit:    return failcount;}                               /* end test_dosomething() */#endif                          /* EXAMPLE *//*******************************************************************-o-****** * test_genKu * * Returns: *	Number of failures. * * * Test generation of usmUser master key from a passphrase. * * ASSUMES  Passphrase is made of printable characters! */inttest_genKu(void){    int             rval = SNMPERR_SUCCESS,        failcount = 0,        properlength = BYTESIZE(SNMP_TRANS_AUTHLEN_HMACMD5), kulen;    char           *hashname = "usmHMACMD5AuthProtocol.", *s;    u_char          Ku[LOCAL_MAXBUF];    oid            *hashtype = usmHMACMD5AuthProtocol;    OUTPUT("Test of generate_Ku --");    /*     * Set passphrase.     */    if (!passphrase) {        passphrase = PASSPHRASE_DEFAULT;    }    if (!bequiet)        fprintf(stdout, "Passphrase%s:\n\t%s\n\n",                (passphrase == PASSPHRASE_DEFAULT) ? " (default)" : "",                passphrase);  test_genKu_again:    memset(Ku, 0, LOCAL_MAXBUF);    kulen = LOCAL_MAXBUF;    rval = generate_Ku(hashtype, USM_LENGTH_OID_TRANSFORM,                       passphrase, strlen(passphrase), Ku, &kulen);    FAILED(rval, "generate_Ku().");    if (kulen != properlength) {        FAILED(SNMPERR_GENERR, "Ku length is wrong for this hashtype.");    }    binary_to_hex(Ku, kulen, &s);    if (!bequiet)        fprintf(stdout, "Ku (len=%d):  %s\n", kulen, s);    free_zero(s, kulen);    SUCCESS(hashname);    if (!bequiet)        fprintf(stdout, "\n");    if (hashtype == usmHMACMD5AuthProtocol) {        hashtype = usmHMACSHA1AuthProtocol;        hashname = "usmHMACSHA1AuthProtocol.";        properlength = BYTESIZE(SNMP_TRANS_AUTHLEN_HMACSHA1);        goto test_genKu_again;    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -