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 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489
|
/*
* scapitest.c
*
* Expected SUCCESSes: 2 + 10 + 1 for all tests.
*
* Returns:
* Number of FAILUREs.
*
*
* ASSUMES No key management functions return non-zero success codes.
*
* XXX Split into individual modules?
* XXX Error/fringe conditions should be tested.
*
*
* Test of sc_random. SUCCESSes: 2.
* REQUIRES a human to spot check for obvious non-randomness...
*
* Test of sc_generate_keyed_hash and sc_check_keyed_hash. SUCCESSes: 10.
*
* Test of sc_encrypt and sc_decrypt. SUCCESSes: 1.
*/
static char *rcsid = "$Id: scapitest.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;
#define DEBUG /* */
/*
* Globals, &c...
*/
char *local_progname;
#define USAGE "Usage: %s [-h][-acHr]"
#define OPTIONLIST "achHr"
int doalltests = 0, docrypt = 0, dokeyedhash = 0, dorandom = 0;
#define ALLOPTIONS (doalltests + docrypt + dokeyedhash + dorandom)
#define LOCAL_MAXBUF (1024 * 8)
#define NL "\n"
#define OUTPUT(o) fprintf(stdout, "\n\n%s\n\n", o);
#define SUCCESS(s) \
{ \
if (!failcount) \
fprintf(stdout, "\nSUCCESS: %s\n", s); \
}
#define FAILED(e, f) \
{ \
if (e != SNMPERR_SUCCESS) { \
fprintf(stdout, "\nFAILED: %s\n", f); \
failcount += 1; \
} \
}
#define BIGSTRING \
" A port may be a pleasant retreat for any mind grown weary of" \
"the struggle for existence. The vast expanse of sky, the" \
"mobile architecture of the clouds, the chameleon coloration" \
"of the sea, the beacons flashing on the shore, together make" \
"a prism which is marvellously calculated to entertain but not" \
"fatigue the eye. The lofty ships with their complex webs of" \
"rigging, swayed to and fro by the swell in harmonious dance," \
"all help to maintain a taste for rhythm and beauty in the" \
"mind. And above all there is a mysterious, aristrocratic kind" \
"of pleasure to be had, for those who have lost all curiosity" \
"or ambition, as they strech on the belvedere or lean over the" \
"mole to watch the arrivals and departures of other men, those" \
"who still have sufficient strength of purpose in them, the" \
"urge to travel or enrich themselves." \
" -- Baudelaire" \
" From _The_Poems_in_Prose_, \"The Port\" (XLI)."
#define BIGSECRET "Shhhh... Don't tell *anyone* about this. Not a soul."
#define BKWDSECRET ".luos a toN .siht tuoba *enoyna* llet t'noD ...hhhhS"
#define MLCOUNT_MAX 6 /* MAC Length Count Maximum. */
/*
* Prototypes.
*/
void usage(FILE * ofp);
int test_docrypt(void);
int test_dokeyedhash(void);
int test_dorandom(void);
int
main(int argc, char **argv)
{
int rval = SNMPERR_SUCCESS, failcount = 0;
char ch;
local_progname = argv[0];
/*
* Parse.
*/
while ((ch = getopt(argc, argv, OPTIONLIST)) != EOF) {
switch (ch) {
case 'a':
doalltests = 1;
break;
case 'c':
docrypt = 1;
break;
case 'H':
dokeyedhash = 1;
break;
case 'r':
dorandom = 1;
break;
case 'h':
rval = 0;
default:
usage(stdout);
exit(rval);
}
argc -= 1;
argv += 1;
optind = 1;
} /* 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 (docrypt || doalltests) {
failcount += test_docrypt();
}
if (dokeyedhash || doalltests) {
failcount += test_dokeyedhash();
}
if (dorandom || doalltests) {
failcount += test_dorandom();
}
/*
* Cleanup.
*/
rval = sc_shutdown(SNMP_CALLBACK_LIBRARY, SNMP_CALLBACK_SHUTDOWN,
NULL, NULL);
FAILED(rval, "sc_shutdown().");
return failcount;
} /* end main() */
void
usage(FILE * ofp)
{
fprintf(ofp,
USAGE
"" NL
" -a All tests." NL
" -c Test of sc_encrypt()/sc_decrypt()."
NL
" -h Help."
NL
" -H Test sc_{generate,check}_keyed_hash()."
NL
" -r Test sc_random()."
NL "" NL, local_progname);
} /* end usage() */
#ifdef EXAMPLE
/*******************************************************************-o-******
* test_dosomething
*
* Test template.
*
* Returns:
* Number of failures.
*/
int
test_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_dorandom
*
* One large request, one set of short requests.
*
* Returns:
* Number of failures.
*
* XXX probably should split up into individual options.
*/
int
test_dorandom(void)
{
int rval = SNMPERR_SUCCESS,
failcount = 0,
origrequest = (1024 * 2),
origrequest_short = 19, nbytes = origrequest, shortcount = 7, i;
char buf[LOCAL_MAXBUF];
OUTPUT("Random test -- large request:");
rval = sc_random(buf, &nbytes);
FAILED(rval, "sc_random().");
if (nbytes != origrequest) {
FAILED(SNMPERR_GENERR,
"sc_random() returned different than requested.");
}
dump_chunk("scapitest", NULL, buf, nbytes);
SUCCESS("Random test -- large request.");
OUTPUT("Random test -- short requests:");
origrequest_short = 16;
for (i = 0; i < shortcount; i++) {
nbytes = origrequest_short;
rval = sc_random(buf, &nbytes);
FAILED(rval, "sc_random().");
if (nbytes != origrequest_short) {
FAILED(SNMPERR_GENERR,
"sc_random() returned different " "than requested.");
}
dump_chunk("scapitest", NULL, buf, nbytes);
} /* endfor */
SUCCESS("Random test -- short requests.");
return failcount;
} /* end test_dorandom() */
/*******************************************************************-o-******
* test_dokeyedhash
*
* Returns:
* Number of failures.
*
*
* Test keyed hashes with a variety of MAC length requests.
*
*
* NOTE Both tests intentionally use the same secret
*
* FIX Get input or output from some other package which hashes...
* XXX Could cut this in half with a little indirection...
*/
int
test_dokeyedhash(void)
{
int rval = SNMPERR_SUCCESS, failcount = 0, bigstring_len = strlen(BIGSTRING), secret_len = strlen(BIGSECRET), properlength, mlcount = 0, /* MAC Length count. */
hblen; /* Hash Buffer length. */
u_int hashbuf_len[MLCOUNT_MAX] = {
LOCAL_MAXBUF,
BYTESIZE(SNMP_TRANS_AUTHLEN_HMACSHA1),
BYTESIZE(SNMP_TRANS_AUTHLEN_HMACMD5),
BYTESIZE(SNMP_TRANS_AUTHLEN_HMAC96),
7,
0,
};
u_char hashbuf[LOCAL_MAXBUF];
char *s;
test_dokeyedhash_again:
OUTPUT("Keyed hash test using MD5 --");
memset(hashbuf, 0, LOCAL_MAXBUF);
hblen = hashbuf_len[mlcount];
properlength = BYTESIZE(SNMP_TRANS_AUTHLEN_HMACMD5);
rval =
sc_generate_keyed_hash(usmHMACMD5AuthProtocol,
USM_LENGTH_OID_TRANSFORM, BIGSECRET,
secret_len, BIGSTRING, bigstring_len,
hashbuf, &hblen);
FAILED(rval, "sc_generate_keyed_hash().");
if (hashbuf_len[mlcount] > properlength) {
if (hblen != properlength) {
FAILED(SNMPERR_GENERR, "Wrong MD5 hash length returned. (1)");
}
} else if (hblen != hashbuf_len[mlcount]) {
FAILED(SNMPERR_GENERR, "Wrong MD5 hash length returned. (2)");
}
rval =
sc_check_keyed_hash(usmHMACMD5AuthProtocol,
USM_LENGTH_OID_TRANSFORM, BIGSECRET,
secret_len, BIGSTRING, bigstring_len, hashbuf,
hblen);
FAILED(rval, "sc_check_keyed_hash().");
binary_to_hex(hashbuf, hblen, &s);
fprintf(stdout, "hash buffer (len=%d, request=%d): %s\n",
hblen, hashbuf_len[mlcount], s);
SNMP_FREE(s);
SUCCESS("Keyed hash test using MD5.");
OUTPUT("Keyed hash test using SHA1 --");
memset(hashbuf, 0, LOCAL_MAXBUF);
hblen = hashbuf_len[mlcount];
properlength = BYTESIZE(SNMP_TRANS_AUTHLEN_HMACSHA1);
rval =
sc_generate_keyed_hash(usmHMACSHA1AuthProtocol,
USM_LENGTH_OID_TRANSFORM, BIGSECRET,
secret_len, BIGSTRING, bigstring_len,
hashbuf, &hblen);
FAILED(rval, "sc_generate_keyed_hash().");
if (hashbuf_len[mlcount] > properlength) {
if (hblen != properlength) {
FAILED(SNMPERR_GENERR,
"Wrong SHA1 hash length returned. (1)");
}
} else if (hblen != hashbuf_len[mlcount]) {
FAILED(SNMPERR_GENERR, "Wrong SHA1 hash length returned. (2)");
}
rval =
sc_check_keyed_hash(usmHMACSHA1AuthProtocol,
USM_LENGTH_OID_TRANSFORM, BIGSECRET,
secret_len, BIGSTRING, bigstring_len, hashbuf,
hblen);
FAILED(rval, "sc_check_keyed_hash().");
binary_to_hex(hashbuf, hblen, &s);
fprintf(stdout, "hash buffer (len=%d, request=%d): %s\n",
hblen, hashbuf_len[mlcount], s);
SNMP_FREE(s);
SUCCESS("Keyed hash test using SHA1.");
/*
* Run the basic hash tests but vary the size MAC requests.
*/
if (hashbuf_len[++mlcount] != 0) {
goto test_dokeyedhash_again;
}
return failcount;
} /* end test_dokeyedhash() */
/*******************************************************************-o-******
* test_docrypt
*
* Returns:
* Number of failures.
*/
int
test_docrypt(void)
{
int rval = SNMPERR_SUCCESS,
failcount = 0,
bigstring_len = strlen(BIGSTRING),
secret_len = BYTESIZE(SNMP_TRANS_PRIVLEN_1DES),
iv_len = BYTESIZE(SNMP_TRANS_PRIVLEN_1DES_IV);
u_int buf_len = LOCAL_MAXBUF, cryptbuf_len = LOCAL_MAXBUF;
char buf[LOCAL_MAXBUF],
cryptbuf[LOCAL_MAXBUF], secret[LOCAL_MAXBUF], iv[LOCAL_MAXBUF];
OUTPUT("Test 1DES-CBC --");
memset(buf, 0, LOCAL_MAXBUF);
memcpy(secret, BIGSECRET, secret_len);
memcpy(iv, BKWDSECRET, iv_len);
rval = sc_encrypt(usmDESPrivProtocol, USM_LENGTH_OID_TRANSFORM,
secret, secret_len,
iv, iv_len,
BIGSTRING, bigstring_len, cryptbuf, &cryptbuf_len);
FAILED(rval, "sc_encrypt().");
rval = sc_decrypt(usmDESPrivProtocol, USM_LENGTH_OID_TRANSFORM,
secret, secret_len,
iv, iv_len, cryptbuf, cryptbuf_len, buf, &buf_len);
FAILED(rval, "sc_decrypt().");
if (buf_len != bigstring_len) {
FAILED(SNMPERR_GENERR, "Decrypted buffer is the wrong length.");
}
if (memcmp(buf, BIGSTRING, bigstring_len)) {
FAILED(SNMPERR_GENERR,
"Decrypted buffer is not equal to original plaintext.");
}
SUCCESS("Test 1DES-CBC --");
return failcount;
} /* end test_docrypt() */
|