import from HEAD
[freeradius.git] / src / modules / rlm_eap / libeap / cb.c
1 /*
2  * cb.c
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  */
22 #include "eap_tls.h"
23
24 #ifndef NO_OPENSSL
25
26 void cbtls_info(const SSL *s, int where, int ret)
27 {
28         const char *str, *state;
29         int w;
30
31         w = where & ~SSL_ST_MASK;
32         if (w & SSL_ST_CONNECT) str="    TLS_connect";
33         else if (w & SSL_ST_ACCEPT) str="    TLS_accept";
34         else str="    (other)";
35
36         state = SSL_state_string_long(s);
37         state = state ? state : "NULL";
38
39         if (where & SSL_CB_LOOP) {
40                 DEBUG2("%s: %s\n", str, state);
41         } else if (where & SSL_CB_HANDSHAKE_START) {
42                 DEBUG2("%s: %s\n", str, state);
43         } else if (where & SSL_CB_HANDSHAKE_DONE) {
44                 DEBUG2("%s: %s\n", str, state);
45         } else if (where & SSL_CB_ALERT) {
46                 str=(where & SSL_CB_READ)?"read":"write";
47                 radlog(L_ERR,"TLS Alert %s:%s:%s\n", str,
48                         SSL_alert_type_string_long(ret),
49                         SSL_alert_desc_string_long(ret));
50         } else if (where & SSL_CB_EXIT) {
51                 if (ret == 0)
52                         radlog(L_ERR, "%s:failed in %s\n", str, state);
53                 else if (ret < 0)
54                         radlog(L_ERR, "%s:error in %s\n", str, state);
55         }
56 }
57
58 /*
59  *      Fill in our 'info' with TLS data.
60  */
61 void cbtls_msg(int write_p, int msg_version, int content_type,
62                const void *buf, size_t len,
63                SSL *ssl UNUSED, void *arg)
64 {
65         tls_session_t *state = (tls_session_t *)arg;
66
67         /*
68          *      Work around bug #298, where we may be called with a NULL
69          *      argument.  We should really log a serious error
70          */
71         if (!arg) return;
72
73         state->info.origin = (unsigned char)write_p;
74         state->info.content_type = (unsigned char)content_type;
75         state->info.record_len = len;
76         state->info.version = msg_version;
77         state->info.initialized = 1;
78
79         if (content_type == SSL3_RT_ALERT) {
80                 state->info.alert_level = ((const unsigned char*)buf)[0];
81                 state->info.alert_description = ((const unsigned char*)buf)[1];
82                 state->info.handshake_type = 0x00;
83
84         } else if (content_type == SSL3_RT_HANDSHAKE) {
85                 state->info.handshake_type = ((const unsigned char*)buf)[0];
86                 state->info.alert_level = 0x00;
87                 state->info.alert_description = 0x00;
88         }
89         tls_session_information(state);
90 }
91
92 int cbtls_password(char *buf,
93                    int num UNUSED,
94                    int rwflag UNUSED,
95                    void *userdata)
96 {
97         strcpy(buf, (char *)userdata);
98         return(strlen((char *)userdata));
99 }
100
101 RSA *cbtls_rsa(SSL *s UNUSED, int is_export UNUSED, int keylength)
102 {
103         static RSA *rsa_tmp=NULL;
104
105         if (rsa_tmp == NULL) {
106                 DEBUG2("Generating temp (%d bit) RSA key...", keylength);
107                 rsa_tmp=RSA_generate_key(keylength, RSA_F4, NULL, NULL);
108         }
109         return(rsa_tmp);
110 }
111
112 #endif /* !defined(NO_OPENSSL) */