No empty .Rs/.Re
[netbsd-mini2440.git] / external / ibm-public / postfix / dist / src / tls / tls_session.c
blob7da770a886e102777def5c7d35e92618f66b9d98
1 /* $NetBSD$ */
3 /*++
4 /* NAME
5 /* tls_session
6 /* SUMMARY
7 /* TLS client and server session routines
8 /* SYNOPSIS
9 /* #include <tls.h>
11 /* void tls_session_stop(ctx, stream, timeout, failure, TLScontext)
12 /* TLS_APPL_STATE *ctx;
13 /* VSTREAM *stream;
14 /* int timeout;
15 /* int failure;
16 /* TLS_SESS_STATE *TLScontext;
18 /* VSTRING *tls_session_passivate(session)
19 /* SSL_SESSION *session;
21 /* SSL_SESSION *tls_session_activate(session_data, session_data_len)
22 /* char *session_data;
23 /* int session_data_len;
24 /* DESCRIPTION
25 /* tls_session_stop() implements the tls_server_shutdown()
26 /* and the tls_client_shutdown() routines.
28 /* tls_session_passivate() converts an SSL_SESSION object to
29 /* VSTRING. The result is a null pointer in case of problems,
30 /* otherwise it should be disposed of with vstring_free().
32 /* tls_session_activate() reanimates a passivated SSL_SESSION object.
33 /* The result is a null pointer in case of problems,
34 /* otherwise it should be disposed of with SSL_SESSION_free().
35 /* LICENSE
36 /* .ad
37 /* .fi
38 /* This software is free. You can do with it whatever you want.
39 /* The original author kindly requests that you acknowledge
40 /* the use of his software.
41 /* AUTHOR(S)
42 /* Originally written by:
43 /* Lutz Jaenicke
44 /* BTU Cottbus
45 /* Allgemeine Elektrotechnik
46 /* Universitaetsplatz 3-4
47 /* D-03044 Cottbus, Germany
49 /* Updated by:
50 /* Wietse Venema
51 /* IBM T.J. Watson Research
52 /* P.O. Box 704
53 /* Yorktown Heights, NY 10598, USA
55 /* Victor Duchovni
56 /* Morgan Stanley
57 /*--*/
59 /* System library. */
61 #include <sys_defs.h>
63 #ifdef USE_TLS
65 /* Utility library. */
67 #include <vstream.h>
68 #include <msg.h>
69 #include <mymalloc.h>
71 /* TLS library. */
73 #define TLS_INTERNAL
74 #include <tls.h>
76 /* Application-specific. */
78 #define STR vstring_str
80 /* tls_session_stop - shut down the TLS connection and reset state */
82 void tls_session_stop(TLS_APPL_STATE *unused_ctx, VSTREAM *stream, int timeout,
83 int failure, TLS_SESS_STATE *TLScontext)
85 const char *myname = "tls_session_stop";
86 int retval;
89 * Sanity check.
91 if (TLScontext == 0)
92 msg_panic("%s: stream has no active TLS context", myname);
95 * Perform SSL_shutdown() twice, as the first attempt will send out the
96 * shutdown alert but it will not wait for the peer's shutdown alert.
97 * Therefore, when we are the first party to send the alert, we must call
98 * SSL_shutdown() again. On failure we don't want to resume the session,
99 * so we will not perform SSL_shutdown() and the session will be removed
100 * as being bad.
102 if (!failure) {
103 retval = tls_bio_shutdown(vstream_fileno(stream), timeout, TLScontext);
104 if (retval == 0)
105 tls_bio_shutdown(vstream_fileno(stream), timeout, TLScontext);
107 tls_free_context(TLScontext);
108 tls_stream_stop(stream);
111 /* tls_session_passivate - passivate SSL_SESSION object */
113 VSTRING *tls_session_passivate(SSL_SESSION *session)
115 const char *myname = "tls_session_passivate";
116 int estimate;
117 int actual_size;
118 VSTRING *session_data;
119 unsigned char *ptr;
122 * First, find out how much memory is needed for the passivated
123 * SSL_SESSION object.
125 estimate = i2d_SSL_SESSION(session, (unsigned char **) 0);
126 if (estimate <= 0) {
127 msg_warn("%s: i2d_SSL_SESSION failed: unable to cache session", myname);
128 return (0);
132 * Passivate the SSL_SESSION object. The use of a VSTRING is slightly
133 * wasteful but is convenient to combine data and length.
135 session_data = vstring_alloc(estimate);
136 ptr = (unsigned char *) STR(session_data);
137 actual_size = i2d_SSL_SESSION(session, &ptr);
138 if (actual_size != estimate) {
139 msg_warn("%s: i2d_SSL_SESSION failed: unable to cache session", myname);
140 vstring_free(session_data);
141 return (0);
143 VSTRING_AT_OFFSET(session_data, actual_size); /* XXX not public */
145 return (session_data);
148 /* tls_session_activate - activate passivated session */
150 SSL_SESSION *tls_session_activate(const char *session_data, int session_data_len)
152 #if (OPENSSL_VERSION_NUMBER < 0x0090707fL)
153 #define BOGUS_CONST
154 #else
155 #define BOGUS_CONST const
156 #endif
157 SSL_SESSION *session;
158 BOGUS_CONST unsigned char *ptr;
161 * Activate the SSL_SESSION object.
163 ptr = (BOGUS_CONST unsigned char *) session_data;
164 session = d2i_SSL_SESSION((SSL_SESSION **) 0, &ptr, session_data_len);
165 if (!session)
166 tls_print_errors();
168 return (session);
171 #endif