xref: /illumos-gate/usr/src/cmd/cmd-inet/usr.bin/pppd/mschap_test.c (revision 30165b7f6753bc3d48c52319bed7ec7b3ea36b3c)
1 /*
2  * Test MS-CHAPv1 library code.
3  *
4  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
5  * Use is subject to license terms.
6  *
7  * Originally from the README.MSCHAP80 file written by:
8  *	Eric Rosenquist          rosenqui@strataware.com
9  *	(updated by Paul Mackerras)
10  *	(updated by Al Longyear)
11  *	(updated by Farrell Woods)
12  */
13 
14 #include <stdio.h>
15 
16 #include "pppd.h"
17 #include "chap.h"
18 #include "chap_ms.h"
19 
20 static void
21 show_response(chap_state *cstate, const char *str)
22 {
23     int i;
24 
25     printf("%s -- %d bytes:", str, cstate->resp_length);
26 
27     for (i = 0; i < cstate->resp_length; i++) {
28         if (i % 8 == 0)
29             putchar('\n');
30         printf("%02X ", (unsigned int)cstate->response[i]);
31     }
32 
33     putchar('\n');
34 }
35 
36 int main(argc, argv)
37     int     argc;
38     char    *argv[];
39 {
40     u_char          challenge[8];
41     int             challengeInt[sizeof(challenge)];
42     chap_state      cstate;
43     int             i;
44 
45     if (argc != 3) {
46         fprintf(stderr, "Usage: %s <16-hexchar challenge> <password>\n",
47         argv[0]); exit(1);
48     }
49 
50     sscanf(argv[1], "%2x%2x%2x%2x%2x%2x%2x%2x",
51            challengeInt + 0, challengeInt + 1, challengeInt + 2,
52            challengeInt + 3, challengeInt + 4, challengeInt + 5,
53            challengeInt + 6, challengeInt + 7);
54 
55     for (i = 0; i < sizeof(challenge); i++)
56         challenge[i] = (u_char)challengeInt[i];
57 
58     BZERO(&cstate, sizeof(cstate));
59     ChapMS(&cstate, challenge, sizeof(challenge), argv[2], strlen(argv[2]));
60 #ifdef MSLANMAN
61     show_response(&cstate, "MS-CHAPv1 with LAN Manager");
62 #else
63     show_response(&cstate, "MS-CHAPv1");
64 #endif
65 
66     cstate.chal_len = sizeof(challenge);
67     BCOPY(challenge, cstate.challenge, cstate.chal_len);
68     if (!ChapMSValidate(&cstate, cstate.response, cstate.resp_length,
69 	argv[2], strlen(argv[2])))
70 	printf("Cannot validate own MS-CHAPv1 response.\n");
71 
72 #ifdef MSLANMAN
73     cstate.response[MS_CHAP_RESPONSE_LEN-1] = '\0';
74     if (!ChapMSValidate(&cstate, cstate.response, cstate.resp_length,
75 	argv[2], strlen(argv[2])))
76 	printf("Cannot validate own LAN Manager response.\n");
77 #endif
78 
79 #ifdef CHAPMSV2
80     cstate.resp_name = "joe user";
81     ChapMSv2(&cstate, cstate.challenge, 16, argv[2], strlen(argv[2]));
82     show_response(&cstate, "MS-CHAPv2");
83     if (!ChapMSv2Validate(&cstate, cstate.resp_name, cstate.response,
84 	cstate.resp_length, argv[2], strlen(argv[2])))
85 	printf("Cannot validate own MS-CHAPv2 response.\n");
86 #endif
87 
88     return (0);
89 }
90