| 1 |
/* |
| 2 |
* scapitest.c |
| 3 |
* |
| 4 |
* HEADER Testing SCAPI API |
| 5 |
* |
| 6 |
* Expected SUCCESSes: 2 + 10 + 1 for all tests. |
| 7 |
* |
| 8 |
* Returns: |
| 9 |
* Number of FAILUREs. |
| 10 |
* |
| 11 |
* |
| 12 |
* ASSUMES No key management functions return non-zero success codes. |
| 13 |
* |
| 14 |
* XXX Split into individual modules? |
| 15 |
* XXX Error/fringe conditions should be tested. |
| 16 |
* |
| 17 |
* |
| 18 |
* Test of sc_random. SUCCESSes: 2. |
| 19 |
* REQUIRES a human to spot check for obvious non-randomness... |
| 20 |
* |
| 21 |
* Test of sc_generate_keyed_hash and sc_check_keyed_hash. SUCCESSes: 10. |
| 22 |
* |
| 23 |
* Test of sc_encrypt and sc_decrypt. SUCCESSes: 1. |
| 24 |
*/ |
| 25 |
|
| 26 |
#include <net-snmp/net-snmp-config.h> |
| 27 |
|
| 28 |
#include <stdio.h> |
| 29 |
#ifdef HAVE_NETINET_IN_H |
| 30 |
#include <netinet/in.h> |
| 31 |
#endif |
| 32 |
#include <sys/types.h> |
| 33 |
|
| 34 |
#include <net-snmp/library/asn1.h> |
| 35 |
#include <net-snmp/library/snmp_api.h> |
| 36 |
#include <net-snmp/library/keytools.h> |
| 37 |
#include <net-snmp/library/tools.h> |
| 38 |
#include <net-snmp/library/scapi.h> |
| 39 |
#include <net-snmp/library/transform_oids.h> |
| 40 |
#include <net-snmp/library/callback.h> |
| 41 |
#include <net-snmp/net-snmp-includes.h> |
| 42 |
#include <net-snmp/library/snmpusm.h> |
| 43 |
#include <net-snmp/library/getopt.h> |
| 44 |
|
| 45 |
#include <stdlib.h> |
| 46 |
#ifdef HAVE_UNISTD_H |
| 47 |
#include <unistd.h> |
| 48 |
#endif |
| 49 |
|
| 50 |
|
| 51 |
/* |
| 52 |
* Globals, &c... |
| 53 |
*/ |
| 54 |
char *local_progname; |
| 55 |
int testcount=0; |
| 56 |
int failcount=0; |
| 57 |
|
| 58 |
#define USAGE "Usage: %s [-h][-acHr]" |
| 59 |
#define OPTIONLIST "achHr" |
| 60 |
|
| 61 |
int doalltests = 0, docrypt = 0, dokeyedhash = 0, dorandom = 0; |
| 62 |
|
| 63 |
#define ALLOPTIONS (doalltests + docrypt + dokeyedhash + dorandom) |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
#define LOCAL_MAXBUF (1024 * 8) |
| 68 |
#define NL "\n" |
| 69 |
|
| 70 |
/* #define OUTPUT(o) printf("# %s\n", o); */ |
| 71 |
int |
| 72 |
output(const char *format, ...) |
| 73 |
{ |
| 74 |
va_list ap; |
| 75 |
char *buffer = NULL; |
| 76 |
int length; |
| 77 |
va_start(ap, format); |
| 78 |
length = vasprintf(&buffer, format, ap); |
| 79 |
va_end(ap); |
| 80 |
if (length < 0) { |
| 81 |
fprintf(stderr, "could not allocate output string\n"); |
| 82 |
exit(127); |
| 83 |
} |
| 84 |
|
| 85 |
printf("# %s\n", buffer); |
| 86 |
free(buffer); |
| 87 |
return 0; |
| 88 |
} |
| 89 |
|
| 90 |
#define TEST_SUCCEEDED(s) do { printf("# Done with %s\n", (s)); } while (0) |
| 91 |
|
| 92 |
#define TEST_FAILED(e, f) \ |
| 93 |
do { \ |
| 94 |
if ((e) != SNMPERR_SUCCESS) { \ |
| 95 |
printf("not ok: %d - %s\n", ++testcount, (f)); \ |
| 96 |
failcount += 1; \ |
| 97 |
} else { \ |
| 98 |
printf("ok: %d - %s\n", ++testcount, (f)); \ |
| 99 |
} \ |
| 100 |
fflush(stdout); \ |
| 101 |
} while (0) |
| 102 |
|
| 103 |
#define BIGSTRING \ |
| 104 |
(const u_char *) \ |
| 105 |
" A port may be a pleasant retreat for any mind grown weary of" \ |
| 106 |
"the struggle for existence. The vast expanse of sky, the" \ |
| 107 |
"mobile architecture of the clouds, the chameleon coloration" \ |
| 108 |
"of the sea, the beacons flashing on the shore, together make" \ |
| 109 |
"a prism which is marvellously calculated to entertain but not" \ |
| 110 |
"fatigue the eye. The lofty ships with their complex webs of" \ |
| 111 |
"rigging, swayed to and fro by the swell in harmonious dance," \ |
| 112 |
"all help to maintain a taste for rhythm and beauty in the" \ |
| 113 |
"mind. And above all there is a mysterious, aristrocratic kind" \ |
| 114 |
"of pleasure to be had, for those who have lost all curiosity" \ |
| 115 |
"or ambition, as they strech on the belvedere or lean over the" \ |
| 116 |
"mole to watch the arrivals and departures of other men, those" \ |
| 117 |
"who still have sufficient strength of purpose in them, the" \ |
| 118 |
"urge to travel or enrich themselves." \ |
| 119 |
" -- Baudelaire" \ |
| 120 |
" From _The_Poems_in_Prose_, \"The Port\" (XLI)." |
| 121 |
|
| 122 |
#define BIGSECRET \ |
| 123 |
(const u_char *) \ |
| 124 |
"Shhhh... Don't tell *anyone* about this. Not even a single soul." |
| 125 |
#define BKWDSECRET \ |
| 126 |
(const u_char *) \ |
| 127 |
".luos elgnis a neve toN .siht tuoba *enoyna* llet t'noD ...hhhhS" |
| 128 |
|
| 129 |
|
| 130 |
static void |
| 131 |
usage(void) |
| 132 |
{ |
| 133 |
printf( USAGE |
| 134 |
"" NL |
| 135 |
" -a All tests." NL |
| 136 |
" -c Test of sc_encrypt()/sc_decrypt()." |
| 137 |
NL |
| 138 |
" -h Help." |
| 139 |
NL |
| 140 |
" -H Test sc_{generate,check}_keyed_hash()." |
| 141 |
NL |
| 142 |
" -r Test sc_random()." |
| 143 |
NL "" NL, local_progname); |
| 144 |
|
| 145 |
} /* end usage() */ |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
static int test_docrypt(void); |
| 150 |
static int test_dokeyedhash(void); |
| 151 |
static int test_dorandom(void); |
| 152 |
|
| 153 |
int |
| 154 |
main(int argc, char **argv) |
| 155 |
{ |
| 156 |
int rval = SNMPERR_SUCCESS; |
| 157 |
int ch; |
| 158 |
|
| 159 |
local_progname = argv[0]; |
| 160 |
|
| 161 |
/* |
| 162 |
* Parse. |
| 163 |
*/ |
| 164 |
while ((ch = getopt(argc, argv, OPTIONLIST)) != EOF) { |
| 165 |
switch (ch) { |
| 166 |
case 'a': |
| 167 |
doalltests = 1; |
| 168 |
break; |
| 169 |
case 'c': |
| 170 |
docrypt = 1; |
| 171 |
break; |
| 172 |
case 'H': |
| 173 |
dokeyedhash = 1; |
| 174 |
break; |
| 175 |
case 'r': |
| 176 |
dorandom = 1; |
| 177 |
break; |
| 178 |
case 'h': |
| 179 |
rval = 0; |
| 180 |
/* fall through */ |
| 181 |
default: |
| 182 |
usage(); |
| 183 |
exit(rval); |
| 184 |
} |
| 185 |
|
| 186 |
argc -= 1; |
| 187 |
argv += 1; |
| 188 |
optind = 1; |
| 189 |
} /* endwhile getopt */ |
| 190 |
|
| 191 |
if ((argc > 1)) { |
| 192 |
usage(); |
| 193 |
exit(1000); |
| 194 |
|
| 195 |
} else if (ALLOPTIONS != 1) { |
| 196 |
doalltests = 1; |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
/* |
| 201 |
* Test stuff. |
| 202 |
*/ |
| 203 |
rval = sc_init(); |
| 204 |
TEST_FAILED(rval, "sc_init() return code"); |
| 205 |
|
| 206 |
|
| 207 |
if (docrypt || doalltests) { |
| 208 |
test_docrypt(); |
| 209 |
} |
| 210 |
if (dokeyedhash || doalltests) { |
| 211 |
test_dokeyedhash(); |
| 212 |
} |
| 213 |
if (dorandom || doalltests) { |
| 214 |
test_dorandom(); |
| 215 |
} |
| 216 |
|
| 217 |
printf("1..%d\n", testcount); |
| 218 |
return 0; |
| 219 |
} /* end main() */ |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
/*******************************************************************-o-****** |
| 226 |
* test_dorandom |
| 227 |
* |
| 228 |
* One large request, one set of short requests. |
| 229 |
* |
| 230 |
* Returns: |
| 231 |
* Number of failures. |
| 232 |
* |
| 233 |
* XXX probably should split up into individual options. |
| 234 |
*/ |
| 235 |
int |
| 236 |
test_dorandom(void) |
| 237 |
{ |
| 238 |
int rval = SNMPERR_SUCCESS, |
| 239 |
origrequest = (1024 * 2), |
| 240 |
origrequest_short = 19, shortcount = 7, i; |
| 241 |
size_t nbytes = origrequest; |
| 242 |
u_char buf[LOCAL_MAXBUF]; |
| 243 |
|
| 244 |
output("Random test -- large request:"); |
| 245 |
|
| 246 |
rval = sc_random(buf, &nbytes); |
| 247 |
TEST_FAILED(rval, "sc_random() return code"); |
| 248 |
|
| 249 |
if (nbytes != origrequest) { |
| 250 |
TEST_FAILED(SNMPERR_GENERR, |
| 251 |
"sc_random() returned different than requested."); |
| 252 |
} |
| 253 |
|
| 254 |
dump_chunk("scapitest", NULL, buf, nbytes); |
| 255 |
|
| 256 |
TEST_SUCCEEDED("Random test -- large request."); |
| 257 |
|
| 258 |
|
| 259 |
output("Random test -- short requests:"); |
| 260 |
origrequest_short = 16; |
| 261 |
|
| 262 |
for (i = 0; i < shortcount; i++) { |
| 263 |
nbytes = origrequest_short; |
| 264 |
rval = sc_random(buf, &nbytes); |
| 265 |
TEST_FAILED(rval, "sc_random() return code"); |
| 266 |
|
| 267 |
if (nbytes != origrequest_short) { |
| 268 |
TEST_FAILED(SNMPERR_GENERR, |
| 269 |
"sc_random() returned different " "than requested."); |
| 270 |
} |
| 271 |
|
| 272 |
dump_chunk("scapitest", NULL, buf, nbytes); |
| 273 |
} /* endfor */ |
| 274 |
|
| 275 |
TEST_SUCCEEDED("Random test -- short requests."); |
| 276 |
|
| 277 |
|
| 278 |
return failcount; |
| 279 |
|
| 280 |
} /* end test_dorandom() */ |
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
/*******************************************************************-o-****** |
| 285 |
* test_dokeyedhash |
| 286 |
* |
| 287 |
* Returns: |
| 288 |
* Number of failures. |
| 289 |
* |
| 290 |
* |
| 291 |
* Test keyed hashes with a variety of MAC length requests. |
| 292 |
* |
| 293 |
* |
| 294 |
* NOTE Both tests intentionally use the same secret |
| 295 |
* |
| 296 |
* FIX Get input or output from some other package which hashes... |
| 297 |
*/ |
| 298 |
int |
| 299 |
test_dokeyedhash(void) |
| 300 |
{ |
| 301 |
int rval = SNMPERR_SUCCESS, |
| 302 |
bigstring_len = strlen((const char *) BIGSTRING), |
| 303 |
secret_len = strlen((const char *) BIGSECRET), |
| 304 |
auth_idx = 0, |
| 305 |
mlcount = 0; /* MAC Length count. */ |
| 306 |
size_t hblen; /* Hash Buffer length. */ |
| 307 |
const netsnmp_auth_alg_info *ai; |
| 308 |
u_int hashbuf_len[] = { |
| 309 |
LOCAL_MAXBUF, |
| 310 |
USM_MD5_AND_SHA_AUTH_LEN, |
| 311 |
USM_HMAC128SHA224_AUTH_LEN, |
| 312 |
USM_HMAC192SHA256_AUTH_LEN, |
| 313 |
USM_HMAC256SHA384_AUTH_LEN, |
| 314 |
USM_HMAC384SHA512_AUTH_LEN, |
| 315 |
7, |
| 316 |
0, |
| 317 |
}; |
| 318 |
|
| 319 |
u_char hashbuf[LOCAL_MAXBUF]; |
| 320 |
char *s; |
| 321 |
|
| 322 |
while(1) { |
| 323 |
|
| 324 |
ai = sc_get_auth_alg_byindex(++auth_idx); |
| 325 |
if (NULL == ai) |
| 326 |
break; |
| 327 |
|
| 328 |
mlcount = 0; |
| 329 |
|
| 330 |
test_dokeyedhash_again: |
| 331 |
|
| 332 |
hblen = hashbuf_len[mlcount]; |
| 333 |
|
| 334 |
if (ai->mac_length > hblen) { |
| 335 |
output("Skipping Keyed hash test (len %d, proplen %d maclen %d) using %s --", hblen, ai->proper_length, ai->mac_length, ai->name); |
| 336 |
goto skip; |
| 337 |
} |
| 338 |
|
| 339 |
memset(hashbuf, 0, LOCAL_MAXBUF); |
| 340 |
|
| 341 |
output("Starting Keyed hash test (len %d, proplen %d, maclen %d) using %s --", hblen, ai->proper_length, ai->mac_length, ai->name); |
| 342 |
|
| 343 |
rval = |
| 344 |
sc_generate_keyed_hash(ai->alg_oid, ai->oid_len, BIGSECRET, |
| 345 |
secret_len, BIGSTRING, |
| 346 |
bigstring_len, |
| 347 |
hashbuf, &hblen); |
| 348 |
TEST_FAILED(rval, "sc_generate_keyed_hash() return code"); |
| 349 |
|
| 350 |
if (hashbuf_len[mlcount] > ai->proper_length) { |
| 351 |
if (hblen != ai->proper_length) { |
| 352 |
TEST_FAILED(SNMPERR_GENERR, "Wrong hash length returned. (1)"); |
| 353 |
} |
| 354 |
} else if (hblen != hashbuf_len[mlcount]) { |
| 355 |
TEST_FAILED(SNMPERR_GENERR, "Wrong hash length returned. (2)"); |
| 356 |
} |
| 357 |
if (hblen > ai->mac_length) { |
| 358 |
printf("# TRUNCATING %d length hash to %s mac length %d\n", (int)hblen, |
| 359 |
ai->name, ai->mac_length); |
| 360 |
hblen = ai->mac_length; |
| 361 |
} |
| 362 |
|
| 363 |
rval = |
| 364 |
sc_check_keyed_hash(ai->alg_oid, ai->oid_len, BIGSECRET, |
| 365 |
secret_len, BIGSTRING, bigstring_len, hashbuf, |
| 366 |
hblen); |
| 367 |
TEST_FAILED(rval, "sc_check_keyed_hash() return code"); |
| 368 |
|
| 369 |
binary_to_hex(hashbuf, hblen, &s); |
| 370 |
printf("# hash buffer (len=%" NETSNMP_PRIz "u, request=%d): %s\n", |
| 371 |
hblen, hashbuf_len[mlcount], s); |
| 372 |
SNMP_FREE(s); |
| 373 |
|
| 374 |
/* |
| 375 |
* Run the basic hash tests but vary the size MAC requests. |
| 376 |
*/ |
| 377 |
skip: |
| 378 |
if (hashbuf_len[++mlcount] != 0) { |
| 379 |
goto test_dokeyedhash_again; |
| 380 |
} |
| 381 |
|
| 382 |
} /* whilte(1) */ |
| 383 |
|
| 384 |
return failcount; |
| 385 |
|
| 386 |
} /* end test_dokeyedhash() */ |
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
/*******************************************************************-o-****** |
| 393 |
* test_docrypt |
| 394 |
* |
| 395 |
* Returns: |
| 396 |
* Number of failures. |
| 397 |
*/ |
| 398 |
int |
| 399 |
test_docrypt(void) |
| 400 |
{ |
| 401 |
int rval, index = 0, secret_len, iv_len, |
| 402 |
bigstring_len = strlen((const char *) BIGSTRING); |
| 403 |
const netsnmp_priv_alg_info *pi; |
| 404 |
size_t buf_len, cryptbuf_len; |
| 405 |
|
| 406 |
u_char buf[LOCAL_MAXBUF], |
| 407 |
cryptbuf[LOCAL_MAXBUF], secret[LOCAL_MAXBUF], iv[LOCAL_MAXBUF]; |
| 408 |
|
| 409 |
while(1) { |
| 410 |
pi = sc_get_priv_alg_byindex(++index); |
| 411 |
if (NULL == pi) |
| 412 |
break; |
| 413 |
|
| 414 |
buf_len = sizeof(buf); |
| 415 |
cryptbuf_len = sizeof(cryptbuf); |
| 416 |
secret_len = pi->proper_length; |
| 417 |
iv_len = pi->iv_length; |
| 418 |
|
| 419 |
output("Starting Test %s --", pi->name); |
| 420 |
|
| 421 |
|
| 422 |
memset(buf, 0, LOCAL_MAXBUF); |
| 423 |
|
| 424 |
memcpy(secret, BIGSECRET, secret_len); |
| 425 |
memcpy(iv, BKWDSECRET, iv_len); |
| 426 |
|
| 427 |
rval = sc_encrypt(pi->alg_oid, pi->oid_len, |
| 428 |
secret, secret_len, |
| 429 |
iv, iv_len, |
| 430 |
BIGSTRING, bigstring_len, cryptbuf, &cryptbuf_len); |
| 431 |
TEST_FAILED(rval, "sc_encrypt() return code."); |
| 432 |
|
| 433 |
rval = sc_decrypt(pi->alg_oid, pi->oid_len, |
| 434 |
secret, secret_len, |
| 435 |
iv, iv_len, cryptbuf, cryptbuf_len, buf, &buf_len); |
| 436 |
TEST_FAILED(rval, "sc_decrypt() return code."); |
| 437 |
|
| 438 |
if (pi->pad_size > 0) { |
| 439 |
/* ignore the pad */ |
| 440 |
buf_len -= buf[buf_len-1]; |
| 441 |
} |
| 442 |
|
| 443 |
TEST_FAILED(buf_len != bigstring_len, |
| 444 |
"Decrypted buffer is the right length."); |
| 445 |
printf("# original length: %d\n", bigstring_len); |
| 446 |
printf("# output length: %" NETSNMP_PRIz "u\n", buf_len); |
| 447 |
|
| 448 |
TEST_FAILED((memcmp(buf, BIGSTRING, bigstring_len) != 0), |
| 449 |
"Decrypted buffer is the same as the original plaintext."); |
| 450 |
|
| 451 |
} /* while(1) */ |
| 452 |
|
| 453 |
return failcount; |
| 454 |
} /* end test_docrypt() */ |