1/*
2 * Copyright (c) 2003 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23/*
24 * chap-new.c - New CHAP implementation.
25 *
26 * Copyright (c) 2003 Paul Mackerras. All rights reserved.
27 *
28 * Redistribution and use in source and binary forms, with or without
29 * modification, are permitted provided that the following conditions
30 * are met:
31 *
32 * 1. Redistributions of source code must retain the above copyright
33 *    notice, this list of conditions and the following disclaimer.
34 *
35 * 2. Redistributions in binary form must reproduce the above copyright
36 *    notice, this list of conditions and the following disclaimer in
37 *    the documentation and/or other materials provided with the
38 *    distribution.
39 *
40 * 3. The name(s) of the authors of this software must not be used to
41 *    endorse or promote products derived from this software without
42 *    prior written permission.
43 *
44 * 4. Redistributions of any form whatsoever must retain the following
45 *    acknowledgment:
46 *    "This product includes software developed by Paul Mackerras
47 *     <paulus@samba.org>".
48 *
49 * THE AUTHORS OF THIS SOFTWARE DISCLAIM ALL WARRANTIES WITH REGARD TO
50 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
51 * AND FITNESS, IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
52 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
53 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
54 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
55 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
56 */
57
58/*
59 * CHAP packets begin with a standard header with code, id, len (2 bytes).
60 */
61#define CHAP_HDRLEN	4
62
63/*
64 * Values for the code field.
65 */
66#define CHAP_CHALLENGE	1
67#define CHAP_RESPONSE	2
68#define CHAP_SUCCESS	3
69#define CHAP_FAILURE	4
70
71/*
72 * CHAP digest codes.
73 */
74#define CHAP_MD5		5
75#define CHAP_MICROSOFT		0x80
76#define CHAP_MICROSOFT_V2	0x81
77
78/*
79 * Semi-arbitrary limits on challenge and response fields.
80 */
81#define MAX_CHALLENGE_LEN	64
82#define MAX_RESPONSE_LEN	64
83
84/* bitmask of supported algorithms */
85#define MDTYPE_MICROSOFT_V2	0x1
86#define MDTYPE_MICROSOFT	0x2
87#define MDTYPE_MD5		0x4
88
89#ifdef CHAPMS
90#define MDTYPE_ALL (MDTYPE_MICROSOFT_V2 | MDTYPE_MICROSOFT | MDTYPE_MD5)
91#else
92#define MDTYPE_ALL (MDTYPE_MD5)
93#endif
94#define MDTYPE_NONE 0
95
96/* Return the digest alg. ID for the most preferred digest type. */
97#define CHAP_DIGEST(mdtype) \
98    ((mdtype) & MDTYPE_MICROSOFT_V2)? CHAP_MICROSOFT_V2: \
99    ((mdtype) & MDTYPE_MICROSOFT)? CHAP_MICROSOFT: \
100    ((mdtype) & MDTYPE_MD5)? CHAP_MD5: \
101    0
102
103/* Return the bit flag (lsb set) for our most preferred digest type. */
104#define CHAP_MDTYPE(mdtype) ((mdtype) ^ ((mdtype) - 1)) & (mdtype)
105
106/* Return the bit flag for a given digest algorithm ID. */
107#define CHAP_MDTYPE_D(digest) \
108    ((digest) == CHAP_MICROSOFT_V2)? MDTYPE_MICROSOFT_V2: \
109    ((digest) == CHAP_MICROSOFT)? MDTYPE_MICROSOFT: \
110    ((digest) == CHAP_MD5)? MDTYPE_MD5: \
111    0
112
113/* Can we do the requested digest? */
114#define CHAP_CANDIGEST(mdtype, digest) \
115    ((digest) == CHAP_MICROSOFT_V2)? (mdtype) & MDTYPE_MICROSOFT_V2: \
116    ((digest) == CHAP_MICROSOFT)? (mdtype) & MDTYPE_MICROSOFT: \
117    ((digest) == CHAP_MD5)? (mdtype) & MDTYPE_MD5: \
118    0
119
120/*
121 * The code for each digest type has to supply one of these.
122 */
123struct chap_digest_type {
124	int code;
125
126	/*
127	 * Note: challenge and response arguments below are formatted as
128	 * a length byte followed by the actual challenge/response data.
129	 */
130	void (*generate_challenge)(unsigned char *challenge);
131	int (*verify_response)(int id, char *name,
132		unsigned char *secret, int secret_len,
133		unsigned char *challenge, unsigned char *response,
134		char *message, int message_space);
135	void (*make_response)(unsigned char *response, int id, char *our_name,
136		unsigned char *challenge, u_char *secret, int secret_len,
137		unsigned char *priv);
138	int (*check_success)(unsigned char *pkt, int len, unsigned char *priv);
139	int (*handle_failure)(unsigned char *pkt, int len, char *message, int message_max_len);
140#ifdef __APPLE__
141	int (*make_change_password)(unsigned char *response, char *our_name,
142		unsigned char *status_pkt, u_char *secret, int secret_len,
143		u_char *new_secret, int new_secret_len,
144		unsigned char *priv);
145	int (*make_retry_password)(unsigned char *response, char *our_name,
146		unsigned char *status_pkt, u_char *secret, int secret_len,
147		unsigned char *priv);
148#endif
149
150	struct chap_digest_type *next;
151};
152
153/* Hook for a plugin to validate CHAP challenge */
154extern int (*chap_verify_hook)(u_char *name, u_char *ourname, int id,
155			struct chap_digest_type *digest,
156			unsigned char *challenge, unsigned char *response,
157			unsigned char *message, int message_space);
158
159#ifdef __APPLE__
160/* Hook for a plugin to validate unknown CHAP packets */
161extern int (*chap_unknown_hook)(char *name, char *ourname, int code, int id,
162			struct chap_digest_type *digest,
163			unsigned char *challenge, unsigned char *pkt, int pkt_len,
164			unsigned char *message, int message_space);
165#endif
166
167/* Called by digest code to register a digest type */
168extern void chap_register_digest(struct chap_digest_type *);
169
170/* Called by authentication code to start authenticating the peer. */
171extern void chap_auth_peer(int unit, char *our_name, int digest_code);
172
173/* Called by auth. code to start authenticating us to the peer. */
174extern void chap_auth_with_peer(int unit, char *our_name, int digest_code);
175
176/* Represents the CHAP protocol to the main pppd code */
177extern struct protent chap_protent;
178