summaryrefslogtreecommitdiff
path: root/usr/src/cmd/cmd-inet/usr.bin/pppd/mschap_test.c
blob: 72b17d65e59527b5d1447e68717d403372e410ee (plain)
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
/*
 * Test MS-CHAPv1 library code.
 *
 * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 *
 * Originally from the README.MSCHAP80 file written by:
 *	Eric Rosenquist          rosenqui@strataware.com
 *	(updated by Paul Mackerras)
 *	(updated by Al Longyear)
 *	(updated by Farrell Woods)
 */

#include <stdio.h>

#include "pppd.h"
#include "chap.h"
#include "chap_ms.h"

static void
show_response(chap_state *cstate, const char *str)
{
    int i;

    printf("%s -- %d bytes:", str, cstate->resp_length);

    for (i = 0; i < cstate->resp_length; i++) {
        if (i % 8 == 0)
            putchar('\n');
        printf("%02X ", (unsigned int)cstate->response[i]);
    }

    putchar('\n');
}

int main(argc, argv)
    int     argc;
    char    *argv[];
{
    u_char          challenge[8];
    int             challengeInt[sizeof(challenge)];
    chap_state      cstate;
    int             i;

    if (argc != 3) {
        fprintf(stderr, "Usage: %s <16-hexchar challenge> <password>\n",
        argv[0]); exit(1);
    }

    sscanf(argv[1], "%2x%2x%2x%2x%2x%2x%2x%2x",
           challengeInt + 0, challengeInt + 1, challengeInt + 2,
           challengeInt + 3, challengeInt + 4, challengeInt + 5,
           challengeInt + 6, challengeInt + 7);

    for (i = 0; i < sizeof(challenge); i++)
        challenge[i] = (u_char)challengeInt[i];

    BZERO(&cstate, sizeof(cstate));
    ChapMS(&cstate, challenge, sizeof(challenge), argv[2], strlen(argv[2]));
#ifdef MSLANMAN
    show_response(&cstate, "MS-CHAPv1 with LAN Manager");
#else
    show_response(&cstate, "MS-CHAPv1");
#endif

    cstate.chal_len = sizeof(challenge);
    BCOPY(challenge, cstate.challenge, cstate.chal_len);
    if (!ChapMSValidate(&cstate, cstate.response, cstate.resp_length,
	argv[2], strlen(argv[2])))
	printf("Cannot validate own MS-CHAPv1 response.\n");

#ifdef MSLANMAN
    cstate.response[MS_CHAP_RESPONSE_LEN-1] = '\0';
    if (!ChapMSValidate(&cstate, cstate.response, cstate.resp_length,
	argv[2], strlen(argv[2])))
	printf("Cannot validate own LAN Manager response.\n");
#endif

#ifdef CHAPMSV2
    cstate.resp_name = "joe user";
    ChapMSv2(&cstate, cstate.challenge, 16, argv[2], strlen(argv[2]));
    show_response(&cstate, "MS-CHAPv2");
    if (!ChapMSv2Validate(&cstate, cstate.resp_name, cstate.response,
	cstate.resp_length, argv[2], strlen(argv[2])))
	printf("Cannot validate own MS-CHAPv2 response.\n");
#endif

    return (0);
}