BA / Mbed OS BaBoRo1
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers chap-new.h Source File

chap-new.h

00001 /*
00002  * chap-new.c - New CHAP implementation.
00003  *
00004  * Copyright (c) 2003 Paul Mackerras. All rights reserved.
00005  *
00006  * Redistribution and use in source and binary forms, with or without
00007  * modification, are permitted provided that the following conditions
00008  * are met:
00009  *
00010  * 1. Redistributions of source code must retain the above copyright
00011  *    notice, this list of conditions and the following disclaimer.
00012  *
00013  * 2. The name(s) of the authors of this software must not be used to
00014  *    endorse or promote products derived from this software without
00015  *    prior written permission.
00016  *
00017  * 3. Redistributions of any form whatsoever must retain the following
00018  *    acknowledgment:
00019  *    "This product includes software developed by Paul Mackerras
00020  *     <paulus@samba.org>".
00021  *
00022  * THE AUTHORS OF THIS SOFTWARE DISCLAIM ALL WARRANTIES WITH REGARD TO
00023  * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
00024  * AND FITNESS, IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
00025  * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
00026  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
00027  * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
00028  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
00029  */
00030 
00031 #include "netif/ppp/ppp_opts.h"
00032 #if PPP_SUPPORT && CHAP_SUPPORT  /* don't build if not configured for use in lwipopts.h */
00033 
00034 #ifndef CHAP_H
00035 #define CHAP_H
00036 
00037 #include "ppp.h"
00038 
00039 /*
00040  * CHAP packets begin with a standard header with code, id, len (2 bytes).
00041  */
00042 #define CHAP_HDRLEN 4
00043 
00044 /*
00045  * Values for the code field.
00046  */
00047 #define CHAP_CHALLENGE  1
00048 #define CHAP_RESPONSE   2
00049 #define CHAP_SUCCESS    3
00050 #define CHAP_FAILURE    4
00051 
00052 /*
00053  * CHAP digest codes.
00054  */
00055 #define CHAP_MD5        5
00056 #if MSCHAP_SUPPORT
00057 #define CHAP_MICROSOFT      0x80
00058 #define CHAP_MICROSOFT_V2   0x81
00059 #endif /* MSCHAP_SUPPORT */
00060 
00061 /*
00062  * Semi-arbitrary limits on challenge and response fields.
00063  */
00064 #define MAX_CHALLENGE_LEN   64
00065 #define MAX_RESPONSE_LEN    64
00066 
00067 /*
00068  * These limits apply to challenge and response packets we send.
00069  * The +4 is the +1 that we actually need rounded up.
00070  */
00071 #define CHAL_MAX_PKTLEN (PPP_HDRLEN + CHAP_HDRLEN + 4 + MAX_CHALLENGE_LEN + MAXNAMELEN)
00072 #define RESP_MAX_PKTLEN (PPP_HDRLEN + CHAP_HDRLEN + 4 + MAX_RESPONSE_LEN + MAXNAMELEN)
00073 
00074 /* bitmask of supported algorithms */
00075 #if MSCHAP_SUPPORT
00076 #define MDTYPE_MICROSOFT_V2 0x1
00077 #define MDTYPE_MICROSOFT    0x2
00078 #endif /* MSCHAP_SUPPORT */
00079 #define MDTYPE_MD5      0x4
00080 #define MDTYPE_NONE     0
00081 
00082 #if MSCHAP_SUPPORT
00083 /* Return the digest alg. ID for the most preferred digest type. */
00084 #define CHAP_DIGEST(mdtype) \
00085     ((mdtype) & MDTYPE_MD5)? CHAP_MD5: \
00086     ((mdtype) & MDTYPE_MICROSOFT_V2)? CHAP_MICROSOFT_V2: \
00087     ((mdtype) & MDTYPE_MICROSOFT)? CHAP_MICROSOFT: \
00088     0
00089 #else /* !MSCHAP_SUPPORT */
00090 #define CHAP_DIGEST(mdtype) \
00091     ((mdtype) & MDTYPE_MD5)? CHAP_MD5: \
00092     0
00093 #endif /* MSCHAP_SUPPORT */
00094 
00095 /* Return the bit flag (lsb set) for our most preferred digest type. */
00096 #define CHAP_MDTYPE(mdtype) ((mdtype) ^ ((mdtype) - 1)) & (mdtype)
00097 
00098 /* Return the bit flag for a given digest algorithm ID. */
00099 #if MSCHAP_SUPPORT
00100 #define CHAP_MDTYPE_D(digest) \
00101     ((digest) == CHAP_MICROSOFT_V2)? MDTYPE_MICROSOFT_V2: \
00102     ((digest) == CHAP_MICROSOFT)? MDTYPE_MICROSOFT: \
00103     ((digest) == CHAP_MD5)? MDTYPE_MD5: \
00104     0
00105 #else /* !MSCHAP_SUPPORT */
00106 #define CHAP_MDTYPE_D(digest) \
00107     ((digest) == CHAP_MD5)? MDTYPE_MD5: \
00108     0
00109 #endif /* MSCHAP_SUPPORT */
00110 
00111 /* Can we do the requested digest? */
00112 #if MSCHAP_SUPPORT
00113 #define CHAP_CANDIGEST(mdtype, digest) \
00114     ((digest) == CHAP_MICROSOFT_V2)? (mdtype) & MDTYPE_MICROSOFT_V2: \
00115     ((digest) == CHAP_MICROSOFT)? (mdtype) & MDTYPE_MICROSOFT: \
00116     ((digest) == CHAP_MD5)? (mdtype) & MDTYPE_MD5: \
00117     0
00118 #else /* !MSCHAP_SUPPORT */
00119 #define CHAP_CANDIGEST(mdtype, digest) \
00120     ((digest) == CHAP_MD5)? (mdtype) & MDTYPE_MD5: \
00121     0
00122 #endif /* MSCHAP_SUPPORT */
00123 
00124 /*
00125  * The code for each digest type has to supply one of these.
00126  */
00127 struct chap_digest_type {
00128     int code;
00129 
00130 #if PPP_SERVER
00131     /*
00132      * Note: challenge and response arguments below are formatted as
00133      * a length byte followed by the actual challenge/response data.
00134      */
00135     void (*generate_challenge)(ppp_pcb *pcb, unsigned char *challenge);
00136     int (*verify_response)(ppp_pcb *pcb, int id, const char *name,
00137         const unsigned char *secret, int secret_len,
00138         const unsigned char *challenge, const unsigned char *response,
00139         char *message, int message_space);
00140 #endif /* PPP_SERVER */
00141     void (*make_response)(ppp_pcb *pcb, unsigned char *response, int id, const char *our_name,
00142         const unsigned char *challenge, const char *secret, int secret_len,
00143         unsigned char *priv);
00144     int (*check_success)(ppp_pcb *pcb, unsigned char *pkt, int len, unsigned char *priv);
00145     void (*handle_failure)(ppp_pcb *pcb, unsigned char *pkt, int len);
00146 };
00147 
00148 /*
00149  * Each interface is described by chap structure.
00150  */
00151 #if CHAP_SUPPORT
00152 typedef struct chap_client_state {
00153     u8_t flags;
00154     const char *name;
00155     const struct chap_digest_type *digest;
00156     unsigned char priv[64];     /* private area for digest's use */
00157 } chap_client_state;
00158 
00159 #if PPP_SERVER
00160 typedef struct chap_server_state {
00161     u8_t flags;
00162     u8_t id;
00163     const char *name;
00164     const struct chap_digest_type *digest;
00165     int challenge_xmits;
00166     int challenge_pktlen;
00167     unsigned char challenge[CHAL_MAX_PKTLEN];
00168 } chap_server_state;
00169 #endif /* PPP_SERVER */
00170 #endif /* CHAP_SUPPORT */
00171 
00172 #if 0 /* UNUSED */
00173 /* Hook for a plugin to validate CHAP challenge */
00174 extern int (*chap_verify_hook)(char *name, char *ourname, int id,
00175             const struct chap_digest_type *digest,
00176             unsigned char *challenge, unsigned char *response,
00177             char *message, int message_space);
00178 #endif /* UNUSED */
00179 
00180 #if PPP_SERVER
00181 /* Called by authentication code to start authenticating the peer. */
00182 extern void chap_auth_peer(ppp_pcb *pcb, const char *our_name, int digest_code);
00183 #endif /* PPP_SERVER */
00184 
00185 /* Called by auth. code to start authenticating us to the peer. */
00186 extern void chap_auth_with_peer(ppp_pcb *pcb, const char *our_name, int digest_code);
00187 
00188 /* Represents the CHAP protocol to the main pppd code */
00189 extern const struct protent chap_protent;
00190 
00191 #endif /* CHAP_H */
00192 #endif /* PPP_SUPPORT && CHAP_SUPPORT */