summaryrefslogtreecommitdiff
path: root/other/sslmim/session.cc
diff options
context:
space:
mode:
authorRoot THC2026-02-24 12:42:47 +0000
committerRoot THC2026-02-24 12:42:47 +0000
commitc9cbeced5b3f2bdd7407e29c0811e65954132540 (patch)
treeaefc355416b561111819de159ccbd86c3004cf88 /other/sslmim/session.cc
parent073fe4bf9fca6bf40cef2886d75df832ef4b6fca (diff)
initial
Diffstat (limited to 'other/sslmim/session.cc')
-rw-r--r--other/sslmim/session.cc184
1 files changed, 184 insertions, 0 deletions
diff --git a/other/sslmim/session.cc b/other/sslmim/session.cc
new file mode 100644
index 0000000..ecaabd3
--- /dev/null
+++ b/other/sslmim/session.cc
@@ -0,0 +1,184 @@
1/*
2 * Copyright (C) 2001 Sebastian Krahmer.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by Sebastian Krahmer.
16 * 4. The name Sebastian Krahmer may not be used to endorse or promote
17 * products derived from this software without specific prior written
18 * permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY
21 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32#include "session.h"
33
34#ifdef fileno
35#undef fileno
36#endif
37
38extern "C" {
39#include <openssl/ssl.h>
40}
41
42Session::Session()
43{
44 SSL_load_error_strings();
45 SSLeay_add_all_algorithms();
46
47 d_ssl = NULL;
48 d_ctx = NULL;
49 d_method = NULL;
50 d_socket = -1;
51}
52
53Session::~Session()
54{
55 shutdown();
56 SSL_CTX_free(d_ctx);
57}
58
59int Session::read(char *buf, int len)
60{
61 return SSL_read(d_ssl, buf, len);
62}
63
64int Session::write(char *buf, int len)
65{
66 return SSL_write(d_ssl, buf, len);
67}
68
69int Session::shutdown()
70{
71 if (d_ssl) {
72 SSL_shutdown(d_ssl);
73 SSL_free(d_ssl);
74 d_ssl = NULL;
75 }
76 return 0;
77}
78
79int Session::start()
80{
81 shutdown();
82 d_ssl = SSL_new(d_ctx);
83 if (!d_ssl) {
84 error = "Session::start::SSL_new() returned NULL";
85 return -1;
86 }
87 return 0;
88}
89
90int Session::fileno(int fd)
91{
92 SSL_set_fd(d_ssl, fd);
93 d_socket = fd;
94 return fd;
95}
96
97int Session::fileno()
98{
99 return d_socket;
100}
101
102//-----
103
104CSession::CSession()
105 : Session()
106{
107 d_method = SSLv23_client_method();
108
109 if (!d_method) {
110 error = "CSession::CSession::SSLv23_client_method() returned NULL";
111 throw -1;
112 }
113
114 d_ctx = SSL_CTX_new(d_method);
115
116 if (!d_ctx) {
117 error = "CSession::CSession::SSL_CTX_new() returned NULL";
118 throw -1;
119 }
120
121}
122
123CSession::~CSession()
124{
125}
126
127int CSession::connect()
128{
129 if (!d_ssl)
130 return -1;
131 return SSL_connect(d_ssl);
132}
133
134SSession::SSession()
135 : Session()
136{
137 d_method = SSLv23_server_method();
138
139 if (!d_method) {
140 error = "SSession::SSession::SSLv23_server_method() returned NULL";
141 throw -1;
142 }
143
144 d_ctx = SSL_CTX_new(d_method);
145
146 if (!d_ctx) {
147 error = "SSession::SSession::SSL_CTX_new() returned NULL";
148 throw -1;
149 }
150
151}
152
153SSession::~SSession()
154{
155}
156
157int SSession::accept()
158{
159 return SSL_accept(d_ssl);
160}
161
162int SSession::load_files(const char *key_file, const char *cert_file)
163{
164 if (SSL_CTX_use_certificate_file(d_ctx, cert_file,
165 SSL_FILETYPE_PEM)<0) {
166 error = "SSession::load_key_file::SSL_CTX_use_certificate()"
167 " returned < 0";
168 return -1;
169 }
170
171 if (SSL_CTX_use_PrivateKey_file(d_ctx, key_file,
172 SSL_FILETYPE_PEM) < 0) {
173 error = "SSession::load_key_file::SSL_CTX_use_PrivateKey_file()"
174 " returned < 0";
175 return -1;
176 }
177
178 if (SSL_CTX_check_private_key(d_ctx) < 0) {
179 error = "SSession::SSL_CTX_check_private_key() returned < 0";
180 return -1;
181 }
182 return 0;
183}
184