Debian Codegraph @

testing/fulltests/snmpv3/T040keymanagetest_capp.c

raw
1 /*
2 * keymanagetest.c
3 *
4 * HEADER Testing key management API functionality
5 *
6 * Expected SUCCESSes: 2 + 2 + 3 for all tests.
7 *
8 * Returns:
9 * Number of FAILUREs.
10 *
11 *
12 * FIX Or how about passing a usmUser name and looking up the entry as
13 * a means of getting key material? This means the userList is
14 * available from an application...
15 *
16 * ASSUMES No key management functions return non-zero success codes.
17 *
18 * Test of generate_Ku(). SUCCESSes: 2
19 * Test of generate_kul(). SUCCESSes: 2
20 * Test of {encode,decode}_keychange(). SUCCESSes: 3
21 */
22
23 #include <net-snmp/net-snmp-config.h>
24
25 #include <stdio.h>
26 #ifdef HAVE_STRING_H
27 #include <string.h>
28 #endif
29
30 #ifdef HAVE_NETINET_IN_H
31 #include <netinet/in.h>
32 #endif
33
34 #ifdef HAVE_UNISTD_H
35 #include <unistd.h>
36 #endif
37 #include <ctype.h>
38
39 #include <net-snmp/library/asn1.h>
40 #include <net-snmp/library/snmp_api.h>
41 #include <net-snmp/library/keytools.h>
42 #include <net-snmp/library/tools.h>
43 #include <net-snmp/library/scapi.h>
44 #include <net-snmp/library/transform_oids.h>
45 #include <net-snmp/library/callback.h>
46 #include <net-snmp/library/getopt.h>
47
48 #include <stdlib.h>
49
50 int testcount = 0;
51
52 /*
53 * Globals, &c...
54 */
55 char *local_progname;
56
57 #define USAGE "Usage: %s [-h][-aklu][-E <engineID>][-N <newkey>][-O <oldkey>][-P <passphrase>]"
58 #define OPTIONLIST "aqE:hklN:O:P:u"
59
60 int doalltests = 0, dogenKu = 0, dogenkul = 0, dokeychange = 0;
61
62 #define ALLOPTIONS (doalltests + dogenKu + dogenkul + dokeychange)
63
64
65 #define LOCAL_MAXBUF (1024 * 8)
66 #define NL "\n"
67
68 #define OUTPUTALWAYS(o) fprintf(stdout, "# %s\n", o);
69 #define OUTPUT(o) if (!bequiet) { OUTPUTALWAYS(o); }
70
71 #define TEST_SUCCEEDED(s) do { printf("# Done with %s\n", (s)); } while (0)
72
73 #define TEST_FAILED(e, f) \
74 do { \
75 if ((e) != SNMPERR_SUCCESS) { \
76 fprintf(stdout, "not ok: %d - %s\n", ++testcount, (f)); \
77 failcount += 1; \
78 } else { \
79 fprintf(stdout, "ok: %d - %s\n", ++testcount, (f)); \
80 } \
81 fflush(stdout); \
82 } while (0)
83
84 #define DETAILINT(s, i) \
85 fprintf(stdout, "# %s: %d\n", s, i);
86
87 /*
88 * Test specific globals.
89 */
90 #define ENGINEID_DEFAULT "1.2.3.4wild"
91 #define PASSPHRASE_DEFAULT "Clay's Conclusion: Creativity is great, " \
92 "but plagiarism is faster."
93 #define OLDKEY_DEFAULT "This is a very old key."
94 #define NEWKEY_DEFAULT "This key, on the other hand, is very new."
95
96 #define USM_LENGTH_OID_TRANSFORM OID_LENGTH(usmHMACSHA1AuthProtocol)
97
98 u_char *engineID = NULL;
99 char *passphrase = NULL;
100 const u_char *oldkey = NULL;
101 const u_char *newkey = NULL;
102 int bequiet = 0;
103
104 static void usage(FILE *);
105 static int test_genKu(void);
106 static int test_genkul(void);
107 static int test_keychange(void);
108
109 int
110 main(int argc, char **argv)
111 {
112 int rval = SNMPERR_SUCCESS, failcount = 0;
113 int ch;
114
115 local_progname = argv[0];
116 optarg = NULL;
117
118 /*
119 * Parse.
120 */
121 while ((ch = getopt(argc, argv, OPTIONLIST)) != EOF) {
122 switch (ch) {
123 case 'a':
124 doalltests = 1;
125 break;
126 case 'E':
127 engineID = (u_char *) optarg;
128 break;
129 case 'k':
130 dokeychange = 1;
131 break;
132 case 'l':
133 dogenkul = 1;
134 break;
135 case 'N':
136 newkey = (u_char *) optarg;
137 break;
138 case 'O':
139 oldkey = (u_char *) optarg;
140 break;
141 case 'P':
142 passphrase = optarg;
143 break;
144 case 'u':
145 dogenKu = 1;
146 break;
147 case 'q':
148 bequiet = 1;
149 break;
150 case 'h':
151 rval = 0;
152 /* fall through */
153 default:
154 usage(stdout);
155 exit(rval);
156 }
157
158 argc -= 1;
159 argv += 1;
160 if (optarg) {
161 argc -= 1;
162 argv += 1;
163 }
164
165 optind = 1;
166 optarg = NULL;
167 } /* endwhile getopt */
168
169 if ((argc > 1)) {
170 usage(stdout);
171 exit(1000);
172
173 } else if (ALLOPTIONS != 1) {
174 doalltests = 1;
175 }
176
177
178 /*
179 * Test stuff.
180 */
181 rval = sc_init();
182 TEST_FAILED(rval, "sc_init().");
183
184 if (dogenKu || doalltests) {
185 failcount += test_genKu();
186 }
187 if (dogenkul || doalltests) {
188 failcount += test_genkul();
189 }
190 if (dokeychange || doalltests) {
191 failcount += test_keychange();
192 }
193
194 fprintf(stdout, "1..%d\n", testcount);
195 return failcount;
196
197 } /* end main() */
198
199
200
201
202
203 void
204 usage(FILE * ofp)
205 {
206 fprintf(ofp,
207 USAGE
208 "" NL
209 " -a All tests." NL
210 " -E [0x]<engineID> snmpEngineID string."
211 NL
212 " -k Test {encode,decode}_keychange()."
213 NL
214 " -l generate_kul()."
215 NL
216 " -h Help."
217 NL
218 " -N [0x]<newkey> New key (for testing KeyChange TC)."
219 NL
220 " -O [0x]<oldkey> Old key (for testing KeyChange TC)."
221 NL
222 " -P <passphrase> Source string for usmUser master key."
223 NL
224 " -u generate_Ku()."
225 NL
226 " -q be quiet."
227 NL "" NL, local_progname);
228
229 } /* end usage() */
230
231
232
233
234 #ifdef EXAMPLE
235 /*******************************************************************-o-******
236 * test_dosomething
237 *
238 * Test template.
239 *
240 * Returns:
241 * Number of failures.
242 */
243 int
244 test_dosomething(void)
245 {
246 int rval = SNMPERR_SUCCESS, failcount = 0;
247
248 EM0(1, "UNIMPLEMENTED"); /* EM(1); */
249
250 test_dosomething_quit:
251 return failcount;
252
253 } /* end test_dosomething() */
254 #endif /* EXAMPLE */
255
256
257
258 /*******************************************************************-o-******
259 * test_genKu
260 *
261 * Returns:
262 * Number of failures.
263 *
264 *
265 * Test generation of usmUser master key from a passphrase.
266 *
267 * ASSUMES Passphrase is made of printable characters!
268 */
269 int
270 test_genKu(void)
271 {
272 int rval = SNMPERR_SUCCESS,
273 failcount = 0,
274 properlength = BYTESIZE(SNMP_TRANS_AUTHLEN_HMACMD5);
275 size_t kulen;
276 const char *hashname = "usmHMACMD5AuthProtocol.";
277 char *s;
278 u_char Ku[LOCAL_MAXBUF];
279 oid *hashtype = usmHMACMD5AuthProtocol;
280
281 OUTPUT("Test of generate_Ku --");
282
283 /*
284 * Set passphrase.
285 */
286 if (!passphrase) {
287 passphrase = strdup(PASSPHRASE_DEFAULT);
288 }
289 if (!bequiet)
290 fprintf(stdout, "Passphrase%s: %s\n",
291 (strcmp(passphrase, PASSPHRASE_DEFAULT) == 0) ? " (default)" : "",
292 passphrase);
293
294
295 test_genKu_again:
296 memset(Ku, 0, LOCAL_MAXBUF);
297 kulen = LOCAL_MAXBUF;
298
299 rval = generate_Ku(hashtype, USM_LENGTH_OID_TRANSFORM,
300 (const u_char *) passphrase, strlen(passphrase),
301 Ku, &kulen);
302 TEST_FAILED(rval, "generate_Ku().");
303
304 TEST_FAILED(kulen != properlength,
305 "Ku length is the right length for this hashtype.");
306
307 binary_to_hex(Ku, kulen, &s);
308 if (!bequiet)
309 fprintf(stdout, "# Ku (len=%" NETSNMP_PRIz "u): %s\n", kulen, s);
310 free_zero(s, kulen);
311
312 OUTPUT(hashname);
313
314 if (hashtype == usmHMACMD5AuthProtocol) {
315 hashtype = usmHMACSHA1AuthProtocol;
316 hashname = "usmHMACSHA1AuthProtocol.";
317 properlength = BYTESIZE(SNMP_TRANS_AUTHLEN_HMACSHA1);
318 goto test_genKu_again;
319 }
320
321 return failcount;
322
323 } /* end test_genKu() */
324
325
326
327
328 /*******************************************************************-o-******
329 * test_genkul
330 *
331 * Returns:
332 * Number of failures.
333 *
334 *
335 * Test of generate_kul().
336 *
337 * A passphrase and engineID are hashed into a master key Ku using
338 * both known hash transforms. Localized keys, also using both hash
339 * transforms, are generated from each of these master keys.
340 *
341 * ASSUME generate_Ku is already tested.
342 * ASSUME engineID is initially a NULL terminated string.
343 */
344 int
345 test_genkul(void)
346 {
347 int rval = SNMPERR_SUCCESS,
348 failcount = 0,
349 properlength, engineID_len, isdefault = FALSE;
350
351 size_t kulen, kul_len;
352 char *s = NULL;
353 const char *testname = "Using HMACMD5 to create master key.";
354 const char *hashname_Ku = "usmHMACMD5AuthProtocol";
355
356 u_char Ku[LOCAL_MAXBUF], kul[LOCAL_MAXBUF];
357
358 oid *hashtype_Ku = usmHMACMD5AuthProtocol, *hashtype_kul;
359
360 OUTPUT("Test of generate_kul --");
361
362 /*
363 * Set passphrase and engineID.
364 *
365 * If engineID begins with 0x, assume it is written in (printable)
366 * hex and convert it to binary data.
367 */
368 if (!passphrase) {
369 passphrase = strdup(PASSPHRASE_DEFAULT);
370 }
371 if (!bequiet)
372 fprintf(stdout, "# Passphrase%s: %s\n",
373 (strcmp(passphrase, PASSPHRASE_DEFAULT) == 0) ? " (default)" : "",
374 passphrase);
375
376 if (!engineID) {
377 engineID = (u_char *) strdup(ENGINEID_DEFAULT);
378 isdefault = TRUE;
379 }
380
381 engineID_len = strlen((char *) engineID);
382
383 if (tolower(*(engineID + 1)) == 'x') {
384 engineID_len =
385 hex_to_binary2(engineID + 2, engineID_len - 2, &s);
386 TEST_FAILED(engineID_len < 0,
387 "Could not resolve hex engineID.");
388 engineID = (u_char *) s;
389 binary_to_hex(engineID, engineID_len, &s);
390 }
391
392 if (!bequiet)
393 fprintf(stdout, "# engineID%s (len=%d): %s\n",
394 (isdefault) ? " (default)" : "",
395 engineID_len, ((s != 0) ? ((u_char *) s) : engineID));
396 SNMP_FREE(s);
397
398
399
400 /*
401 * Create a master key using both hash transforms; create localized
402 * keys using both hash transforms from each master key.
403 */
404 test_genkul_again_master:
405 memset(Ku, 0, LOCAL_MAXBUF);
406 kulen = LOCAL_MAXBUF;
407 hashtype_kul = usmHMACMD5AuthProtocol;
408 properlength = BYTESIZE(SNMP_TRANS_AUTHLEN_HMACMD5);
409
410
411 rval = generate_Ku(hashtype_Ku, USM_LENGTH_OID_TRANSFORM,
412 (u_char *) passphrase, strlen(passphrase), Ku, &kulen);
413 TEST_FAILED(rval, "generate_Ku().");
414
415 binary_to_hex(Ku, kulen, &s);
416 if (!bequiet)
417 fprintf(stdout,
418 "# Master Ku using \"%s\": \t%s\n", hashname_Ku, s);
419 free_zero(s, kulen);
420
421
422 test_genkul_again_local:
423 memset(kul, 0, LOCAL_MAXBUF);
424 kul_len = LOCAL_MAXBUF;
425
426 rval = generate_kul(hashtype_kul, USM_LENGTH_OID_TRANSFORM,
427 engineID, engineID_len, Ku, kulen, kul, &kul_len);
428
429 if ((hashtype_Ku == usmHMACMD5AuthProtocol)
430 && (hashtype_kul == usmHMACSHA1AuthProtocol)) {
431 TEST_FAILED(rval == SNMPERR_SUCCESS,
432 "generate_kul SHOULD fail when Ku length is "
433 "less than hash transform length.");
434 } else {
435 TEST_FAILED(rval, "generate_kul().");
436
437 TEST_FAILED(kul_len != properlength,
438 "checking if kul length is right for the given hashtype.");
439
440 binary_to_hex(kul, kul_len, &s);
441 fprintf(stdout, "# kul (%s) (len=%" NETSNMP_PRIz "u): %s\n",
442 ((hashtype_Ku == usmHMACMD5AuthProtocol) ? "MD5" : "SHA"),
443 kul_len, s);
444 free_zero(s, kul_len);
445 }
446
447
448 /*
449 * Create localized key using the other hash transform, but from
450 * * the same master key.
451 */
452 if (hashtype_kul == usmHMACMD5AuthProtocol) {
453 hashtype_kul = usmHMACSHA1AuthProtocol;
454 properlength = BYTESIZE(SNMP_TRANS_AUTHLEN_HMACSHA1);
455 goto test_genkul_again_local;
456 }
457
458 TEST_SUCCEEDED(testname);
459
460
461 /*
462 * Re-create the master key using the other hash transform.
463 */
464 if (hashtype_Ku == usmHMACMD5AuthProtocol) {
465 hashtype_Ku = usmHMACSHA1AuthProtocol;
466 hashname_Ku = "usmHMACSHA1AuthProtocol";
467 testname = "Using HMACSHA1 to create master key.";
468 goto test_genkul_again_master;
469 }
470
471 return failcount;
472
473 } /* end test_genkul() */
474
475
476
477
478 /*******************************************************************-o-******
479 * test_keychange
480 *
481 * Returns:
482 * Number of failures.
483 *
484 *
485 * Test of KeyChange TC implementation.
486 *
487 * ASSUME newkey and oldkey begin as NULL terminated strings.
488 */
489 int
490 test_keychange(void)
491 {
492 int rval = SNMPERR_SUCCESS,
493 failcount = 0,
494 properlength = BYTESIZE(SNMP_TRANS_AUTHLEN_HMACMD5),
495 oldkey_len,
496 newkey_len,
497 isdefault_new = FALSE, isdefault_old = FALSE;
498 size_t keychange_len, temp_len;
499
500 const char *hashname = "usmHMACMD5AuthProtocol.";
501 char *s;
502
503 u_char oldkey_buf[LOCAL_MAXBUF],
504 newkey_buf[LOCAL_MAXBUF],
505 temp_buf[LOCAL_MAXBUF], keychange_buf[LOCAL_MAXBUF];
506
507 oid *hashtype = usmHMACMD5AuthProtocol;
508
509 OUTPUT("Test of KeyChange TC --");
510
511
512 /*
513 * Set newkey and oldkey.
514 */
515 if (!newkey) { /* newkey */
516 newkey = (const u_char *) NEWKEY_DEFAULT;
517 isdefault_new = TRUE;
518 }
519 newkey_len = strlen((const char *) newkey);
520
521 if (tolower(*(newkey + 1)) == 'x') {
522 newkey_len = hex_to_binary2((const u_char *) newkey + 2,
523 newkey_len - 2, &s);
524 TEST_FAILED(newkey_len < 0, "Could not resolve hex newkey.");
525 newkey = (const u_char *) s;
526 binary_to_hex((const u_char *) newkey, newkey_len, &s);
527 }
528
529 if (!oldkey) { /* oldkey */
530 oldkey = (const u_char *) OLDKEY_DEFAULT;
531 isdefault_old = TRUE;
532 }
533 oldkey_len = strlen((const char *) oldkey);
534
535 if (tolower(*(oldkey + 1)) == 'x') {
536 oldkey_len = hex_to_binary2((const u_char *) oldkey + 2,
537 oldkey_len - 2, &s);
538 TEST_FAILED(oldkey_len < 0, "Could not resolve hex oldkey.");
539 oldkey = (const u_char *) s;
540 binary_to_hex(oldkey, oldkey_len, &s);
541 }
542
543
544
545 test_keychange_again:
546 memset(oldkey_buf, 0, LOCAL_MAXBUF);
547 memset(newkey_buf, 0, LOCAL_MAXBUF);
548 memset(keychange_buf, 0, LOCAL_MAXBUF);
549 memset(temp_buf, 0, LOCAL_MAXBUF);
550
551 memcpy(oldkey_buf, oldkey, SNMP_MIN(oldkey_len, properlength));
552 memcpy(newkey_buf, newkey, SNMP_MIN(newkey_len, properlength));
553 keychange_len = LOCAL_MAXBUF;
554
555
556 binary_to_hex(oldkey_buf, properlength, &s);
557 fprintf(stdout, "# oldkey%s (len=%d): %s\n",
558 (isdefault_old) ? " (default)" : "", properlength, s);
559 SNMP_FREE(s);
560
561 binary_to_hex(newkey_buf, properlength, &s);
562 fprintf(stdout, "# newkey%s (len=%d): %s\n",
563 (isdefault_new) ? " (default)" : "", properlength, s);
564 SNMP_FREE(s);
565
566
567 rval = encode_keychange(hashtype, USM_LENGTH_OID_TRANSFORM,
568 oldkey_buf, properlength,
569 newkey_buf, properlength,
570 keychange_buf, &keychange_len);
571 TEST_FAILED(rval, "encode_keychange().");
572
573 TEST_FAILED(keychange_len != properlength * 2,
574 "KeyChange string (encoded) is not proper length "
575 "for this hash transform.");
576
577 binary_to_hex(keychange_buf, keychange_len, &s);
578 fprintf(stdout, "# (%s) KeyChange string: %s\n",
579 ((hashtype == usmHMACMD5AuthProtocol) ? "MD5" : "SHA"), s);
580 SNMP_FREE(s);
581
582 temp_len = properlength;
583 rval = decode_keychange(hashtype, USM_LENGTH_OID_TRANSFORM,
584 oldkey_buf, properlength,
585 keychange_buf, properlength * 2,
586 temp_buf, &temp_len);
587 TEST_FAILED(rval, "decode_keychange().");
588
589 TEST_FAILED(temp_len != properlength,
590 "decoded newkey is not proper length for this hash transform.");
591
592 binary_to_hex(temp_buf, temp_len, &s);
593 fprintf(stdout, "# decoded newkey: %s\n", s);
594 SNMP_FREE(s);
595
596
597 TEST_FAILED(memcmp(newkey_buf, temp_buf, temp_len),
598 "newkey did not decode properly.");
599
600
601 TEST_SUCCEEDED(hashname);
602
603
604 /*
605 * Multiplex different test combinations.
606 *
607 * First clause is for Test #2, second clause is for (last) Test #3.
608 */
609 if (hashtype == usmHMACMD5AuthProtocol) {
610 hashtype = usmHMACSHA1AuthProtocol;
611 hashname = "usmHMACSHA1AuthProtocol (w/DES length kul's).";
612 properlength = BYTESIZE(SNMP_TRANS_PRIVLEN_1DES)
613 + BYTESIZE(SNMP_TRANS_PRIVLEN_1DES_IV);
614 goto test_keychange_again;
615
616 } else if (properlength < BYTESIZE(SNMP_TRANS_AUTHLEN_HMACSHA1)) {
617 hashtype = usmHMACSHA1AuthProtocol;
618 hashname = "usmHMACSHA1AuthProtocol.";
619 properlength = BYTESIZE(SNMP_TRANS_AUTHLEN_HMACSHA1);
620 goto test_keychange_again;
621 }
622
623 return failcount;
624
625 } /* end test_keychange() */