]> git.ozlabs.org Git - ppp.git/blob - pppd/mppe.h
pppd: Expose the MPPE keys generated through an API (#267)
[ppp.git] / pppd / mppe.h
1 /*
2  * mppe.h - Definitions for MPPE
3  *
4  * Copyright (c) 2008 Paul Mackerras. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  *
18  * 3. The name(s) of the authors of this software must not be used to
19  *    endorse or promote products derived from this software without
20  *    prior written permission.
21  *
22  * 4. Redistributions of any form whatsoever must retain the following
23  *    acknowledgment:
24  *    "This product includes software developed by Paul Mackerras
25  *     <paulus@samba.org>".
26  *
27  * THE AUTHORS OF THIS SOFTWARE DISCLAIM ALL WARRANTIES WITH REGARD TO
28  * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
29  * AND FITNESS, IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
30  * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
31  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
32  * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
33  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
34  */
35 #ifndef __MPPE_H__
36 #define __MPPE_H__
37
38 #define MPPE_PAD                4       /* MPPE growth per frame */
39 #define MPPE_MAX_KEY_SIZE       32      /* Largest key length */
40 #define MPPE_MAX_KEY_LEN       16      /* Largest key size accepted by the kernel */
41
42 /* option bits for ccp_options.mppe */
43 #define MPPE_OPT_40             0x01    /* 40 bit */
44 #define MPPE_OPT_128            0x02    /* 128 bit */
45 #define MPPE_OPT_STATEFUL       0x04    /* stateful mode */
46 /* unsupported opts */
47 #define MPPE_OPT_56             0x08    /* 56 bit */
48 #define MPPE_OPT_MPPC           0x10    /* MPPC compression */
49 #define MPPE_OPT_D              0x20    /* Unknown */
50 #define MPPE_OPT_UNSUPPORTED (MPPE_OPT_56|MPPE_OPT_MPPC|MPPE_OPT_D)
51 #define MPPE_OPT_UNKNOWN        0x40    /* Bits !defined in RFC 3078 were set */
52
53 /*
54  * This is not nice ... the alternative is a bitfield struct though.
55  * And unfortunately, we cannot share the same bits for the option
56  * names above since C and H are the same bit.  We could do a u_int32
57  * but then we have to do a htonl() all the time and/or we still need
58  * to know which octet is which.
59  */
60 #define MPPE_C_BIT              0x01    /* MPPC */
61 #define MPPE_D_BIT              0x10    /* Obsolete, usage unknown */
62 #define MPPE_L_BIT              0x20    /* 40-bit */
63 #define MPPE_S_BIT              0x40    /* 128-bit */
64 #define MPPE_M_BIT              0x80    /* 56-bit, not supported */
65 #define MPPE_H_BIT              0x01    /* Stateless (in a different byte) */
66
67 /* Does not include H bit; used for least significant octet only. */
68 #define MPPE_ALL_BITS (MPPE_D_BIT|MPPE_L_BIT|MPPE_S_BIT|MPPE_M_BIT|MPPE_H_BIT)
69
70 /* Build a CI from mppe opts (see RFC 3078) */
71 #define MPPE_OPTS_TO_CI(opts, ci)               \
72     do {                                        \
73         u_char *ptr = ci; /* u_char[4] */       \
74                                                 \
75         /* H bit */                             \
76         if (opts & MPPE_OPT_STATEFUL)           \
77             *ptr++ = 0x0;                       \
78         else                                    \
79             *ptr++ = MPPE_H_BIT;                \
80         *ptr++ = 0;                             \
81         *ptr++ = 0;                             \
82                                                 \
83         /* S,L bits */                          \
84         *ptr = 0;                               \
85         if (opts & MPPE_OPT_128)                \
86             *ptr |= MPPE_S_BIT;                 \
87         if (opts & MPPE_OPT_40)                 \
88             *ptr |= MPPE_L_BIT;                 \
89         /* M,D,C bits not supported */          \
90     } while (/* CONSTCOND */ 0)
91
92 /* The reverse of the above */
93 #define MPPE_CI_TO_OPTS(ci, opts)               \
94     do {                                        \
95         u_char *ptr = ci; /* u_char[4] */       \
96                                                 \
97         opts = 0;                               \
98                                                 \
99         /* H bit */                             \
100         if (!(ptr[0] & MPPE_H_BIT))             \
101             opts |= MPPE_OPT_STATEFUL;          \
102                                                 \
103         /* S,L bits */                          \
104         if (ptr[3] & MPPE_S_BIT)                \
105             opts |= MPPE_OPT_128;               \
106         if (ptr[3] & MPPE_L_BIT)                \
107             opts |= MPPE_OPT_40;                \
108                                                 \
109         /* M,D,C bits */                        \
110         if (ptr[3] & MPPE_M_BIT)                \
111             opts |= MPPE_OPT_56;                \
112         if (ptr[3] & MPPE_D_BIT)                \
113             opts |= MPPE_OPT_D;                 \
114         if (ptr[3] & MPPE_C_BIT)                \
115             opts |= MPPE_OPT_MPPC;              \
116                                                 \
117         /* Other bits */                        \
118         if (ptr[0] & ~MPPE_H_BIT)               \
119             opts |= MPPE_OPT_UNKNOWN;           \
120         if (ptr[1] || ptr[2])                   \
121             opts |= MPPE_OPT_UNKNOWN;           \
122         if (ptr[3] & ~MPPE_ALL_BITS)            \
123             opts |= MPPE_OPT_UNKNOWN;           \
124     } while (/* CONSTCOND */ 0)
125
126
127 #if MPPE
128
129 /*
130  * NOTE:
131  *   Access to these variables directly is discuraged. Please
132  *   change your code to use below accessor functions.
133  */
134
135 /* The key material generated which is used for MPPE send key */
136 extern u_char mppe_send_key[MPPE_MAX_KEY_SIZE];
137 /* The key material generated which is used for MPPE recv key */
138 extern u_char mppe_recv_key[MPPE_MAX_KEY_SIZE];
139 /* Keys are set if value is non-zero */
140 extern int mppe_keys_set;
141
142 /* These values are the RADIUS attribute values--see RFC 2548. */
143 #define MPPE_ENC_POL_ENC_ALLOWED 1
144 #define MPPE_ENC_POL_ENC_REQUIRED 2
145 #define MPPE_ENC_TYPES_RC4_40 2
146 #define MPPE_ENC_TYPES_RC4_128 4
147
148 /* used by plugins (using above values) */
149 void mppe_set_enc_types (int policy, int types);
150
151 /*
152  * Set the MPPE send and recv keys. NULL values for keys are ignored
153  *   and input values are cleared to avoid leaving them on the stack
154  */
155 void mppe_set_keys(u_char *send_key, u_char *recv_key, int keylen);
156
157 /*
158  * Get the MPPE recv key
159  */
160 int mppe_get_recv_key(u_char *recv_key, int length);
161
162 /*
163  * Get the MPPE send key
164  */
165 int mppe_get_send_key(u_char *send_key, int length);
166
167 /*
168  * Clear the MPPE keys
169  */
170 void mppe_clear_keys(void);
171
172 /*
173  * Check if the MPPE keys are set
174  */
175 bool mppe_keys_isset(void);
176
177 /*
178  * Set mppe_xxxx_key from NT Password Hash Hash (MSCHAPv1), see RFC3079
179  */
180 void mppe_set_chapv1(u_char *rchallenge, u_char PasswordHashHash[MD4_SIGNATURE_SIZE]);
181
182 /*
183  * Set the mppe_xxxx_key from MS-CHAP-v2 credentials, see RFC3079
184  */
185 void mppe_set_chapv2(u_char PasswordHashHash[MD4_SIGNATURE_SIZE],
186                     u_char NTResponse[MS_AUTH_NTRESP_LEN], int IsServer);
187
188 #endif  // #ifdef MPPE
189 #endif  // #ifdef __MPPE_H__