perl -i -npe "s/[ \t]+$//g" `find src -name "*.[ch]" -print`
[freeradius.git] / src / modules / rlm_eap / types / rlm_eap_tls / rlm_eap_tls.h
1 /*
2  * rlm_eap_tls.h
3  *
4  * Version:     $Id$
5  *
6  *   This program is free software; you can redistribute it and/or modify
7  *   it under the terms of the GNU General Public License as published by
8  *   the Free Software Foundation; either version 2 of the License, or
9  *   (at your option) any later version.
10  *
11  *   This program is distributed in the hope that it will be useful,
12  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *   GNU General Public License for more details.
15  *
16  *   You should have received a copy of the GNU General Public License
17  *   along with this program; if not, write to the Free Software
18  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  * Copyright 2001  hereUare Communications, Inc. <raghud@hereuare.com>
21  * Copyright 2003  Alan DeKok <aland@freeradius.org>
22  */
23 #ifndef _RLM_EAP_TLS_H
24 #define _RLM_EAP_TLS_H
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <errno.h>
30 #include <sys/types.h>
31 #include <sys/socket.h>
32 #include <netinet/in.h>
33 #include <netinet/tcp.h>
34 #include <netdb.h>
35 #include <fcntl.h>
36 #include <signal.h>
37
38 #include <ctype.h>
39 #include <sys/time.h>
40 #include <arpa/inet.h>
41
42 #ifdef HAVE_LIMITS_H
43 #include <limits.h>
44 #endif
45
46 #ifdef HAVE_UNISTD_H
47 #include <unistd.h>
48 #endif
49
50 #include "config.h"
51
52 #ifndef NO_OPENSSL
53 /*
54  *      For RH 9, which apparently needs this.
55  */
56 #ifndef OPENSSL_NO_KRB5
57 #define OPENSSL_NO_KRB5
58 #endif
59 #include <openssl/err.h>
60 #ifdef HAVE_OPENSSL_ENGINE_H
61 #include <openssl/engine.h>
62 #endif
63 #include <openssl/ssl.h>
64 #endif /* !defined(NO_OPENSSL) */
65
66 #include "eap.h"
67
68 typedef enum {
69         EAPTLS_INVALID = 0,             /* invalid, don't reply */
70         EAPTLS_REQUEST,                 /* request, ok to send, invalid to receive */
71         EAPTLS_RESPONSE,                /* response, ok to receive, invalid to send */
72         EAPTLS_SUCCESS,                 /* success, send success */
73         EAPTLS_FAIL,                    /* fail, send fail */
74         EAPTLS_NOOP,                    /* noop, continue */
75
76         EAPTLS_START,                   /* start, ok to send, invalid to receive */
77         EAPTLS_OK,                      /* ok, continue */
78         EAPTLS_ACK,                     /* acknowledge, continue */
79         EAPTLS_FIRST_FRAGMENT,          /* first fragment */
80         EAPTLS_MORE_FRAGMENTS,          /* more fragments, to send/receive */
81         EAPTLS_LENGTH_INCLUDED,                 /* length included */
82         EAPTLS_MORE_FRAGMENTS_WITH_LENGTH,   /* more fragments with length */
83         EAPTLS_HANDLED                  /* tls code has handled it */
84 } eaptls_status_t;
85
86 #define MAX_RECORD_SIZE 16384
87
88 /*
89  *      A single TLS record may be up to 16384 octets in length, but a
90  *      TLS message may span multiple TLS records, and a TLS
91  *      certificate message may in principle be as long as 16MB.
92  *
93  *      However, note that in order to protect against reassembly
94  *      lockup and denial of service attacks, it may be desirable for
95  *      an implementation to set a maximum size for one such group of
96  *      TLS messages.
97  *
98  *      The TLS Message Length field is four octets, and provides the
99  *      total length of the TLS message or set of messages that is
100  *      being fragmented; this simplifies buffer allocation.
101  */
102
103 /*
104  * FIXME: Dynamic allocation of buffer to overcome MAX_RECORD_SIZE overflows.
105  *      or configure TLS not to exceed MAX_RECORD_SIZE.
106  */
107 typedef struct _record_t {
108         unsigned char data[MAX_RECORD_SIZE];
109         unsigned int  used;
110 } record_t;
111
112 typedef struct _tls_info_t {
113         unsigned char   origin;
114         unsigned char   content_type;
115         unsigned char   handshake_type;
116         unsigned char   alert_level;
117         unsigned char   alert_description;
118         char            info_description[256];
119         size_t          record_len;
120         int             version;
121         char            initialized;
122 } tls_info_t;
123
124 /*
125  * tls_session_t Structure gets stored as opaque in EAP_HANDLER
126  * This contains EAP-REQUEST specific data
127  * (ie EAPTLS_DATA(fragment), EAPTLS-ALERT, EAPTLS-REQUEST ...)
128  *
129  * clean_in  - data that needs to be sent but only after it is soiled.
130  * dirty_in  - data EAP server receives.
131  * clean_out - data that is cleaned after receiving.
132  * dirty_out - data EAP server sends.
133  * offset    - current fragment size transmitted
134  * fragment  - Flag, In fragment mode or not.
135  * tls_msg_len - Actual/Total TLS message length.
136  * length_flag - A flag to include length in every TLS Data/Alert packet
137  *                                      if set to no then only the first fragment contains length
138  */
139 typedef struct _tls_session_t {
140         SSL             *ssl;
141         tls_info_t      info;
142
143         BIO             *into_ssl;
144         BIO             *from_ssl;
145         record_t        clean_in;
146         record_t        clean_out;
147         record_t        dirty_in;
148         record_t        dirty_out;
149
150         /*
151          * Framed-MTU attribute in RADIUS,
152          * if present, can also be used to set this
153          */
154         unsigned int    offset;
155         unsigned int    tls_msg_len;
156         int             fragment;
157         int             length_flag;
158         int             peap_flag;
159
160         /*
161          *      Used by TTLS & PEAP to keep track of other per-session
162          *      data.
163          */
164         void            *opaque;
165         void            (*free_opaque)(void *opaque);
166 } tls_session_t;
167
168
169 /*
170  *      Externally exported TLS functions.
171  */
172 eaptls_status_t eaptls_process(EAP_HANDLER *handler);
173
174 int             eaptls_success(EAP_DS *eap_ds, int peap_flag);
175 int             eaptls_fail(EAP_DS *eap_ds, int peap_flag);
176 int             eaptls_request(EAP_DS *eap_ds, tls_session_t *ssn);
177
178
179 /* MPPE key generation */
180 void            eaptls_gen_mppe_keys(VALUE_PAIR **reply_vps, SSL *s,
181                                      const char *prf_label);
182 void            eapttls_gen_challenge(SSL *s, char *buffer, int size);
183
184 #endif /* _RLM_EAP_TLS_H */