File: sslhelper.c

package info (click to toggle)
sbnc 1.2-26
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 6,216 kB
  • sloc: cpp: 17,556; ansic: 15,514; sh: 13,419; tcl: 5,567; php: 448; makefile: 284
file content (247 lines) | stat: -rw-r--r-- 7,161 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/*******************************************************************************
 * shroudBNC - an object-oriented framework for IRC                            *
 * Copyright (C) 2005-2007 Gunnar Beutner                                      *
 *                                                                             *
 * This program is free software; you can redistribute it and/or               *
 * modify it under the terms of the GNU General Public License                 *
 * as published by the Free Software Foundation; either version 2              *
 * of the License, or (at your option) any later version.                      *
 *                                                                             *
 * This program is distributed in the hope that it will be useful,             *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of              *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               *
 * GNU General Public License for more details.                                *
 *                                                                             *
 * You should have received a copy of the GNU General Public License           *
 * along with this program; if not, write to the Free Software                 *
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. *
 *******************************************************************************/

/* ====================================================================
 * Copyright (c) 1998-2007 The OpenSSL Project.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. All advertising materials mentioning features or use of this
 *    software must display the following acknowledgment:
 *    "This product includes software developed by the OpenSSL Project
 *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
 *
 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
 *    endorse or promote products derived from this software without
 *    prior written permission. For written permission, please contact
 *    openssl-core@openssl.org.
 *
 * 5. Products derived from this software may not be called "OpenSSL"
 *    nor may "OpenSSL" appear in their names without prior written
 *    permission of the OpenSSL Project.
 *
 * 6. Redistributions of any form whatsoever must retain the following
 *    acknowledgment:
 *    "This product includes software developed by the OpenSSL Project
 *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
 *
 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 * ====================================================================
 *
 * This product includes cryptographic software written by Eric Young
 * (eay@cryptsoft.com).  This product includes software written by Tim
 * Hudson (tjh@cryptsoft.com).
 *
 */

#include "StdAfx.h"

#ifdef USESSL
#ifdef _WIN32
#include <openssl/applink.c>
#endif

#include <openssl/bio.h>

#define get_last_socket_error WSAGetLastError()

#undef shutdown

static int safe_sock_new(BIO *bio);
static int safe_sock_free(BIO *bio);
static int safe_sock_read(BIO *bio, char *Buffer, int Len);
static int safe_sock_write(BIO *bio, const char *Buffer, int Len);
static long safe_sock_ctrl(BIO *bio, int cmd, long num, void *ptr);
static int safe_sock_puts(BIO *bio, const char *String);

static BIO_METHOD methods_safe_sockp = {
	BIO_TYPE_SOCKET,
	"safe_socket",
	safe_sock_write,
	safe_sock_read,
	safe_sock_puts,
	NULL, /* gets */
	safe_sock_ctrl,
	safe_sock_new,
	safe_sock_free,
	NULL
};

BIO_METHOD *BIO_s_safe_sock(void) {
	return &methods_safe_sockp;
}

BIO *BIO_new_safe_socket(SOCKET Socket, int CloseFlag) {
	BIO *Result;

	Result = BIO_new(BIO_s_safe_sock());

	if (Result == NULL) {
		return NULL;
	}

	BIO_set_fd(Result, Socket, CloseFlag);

	return Result;
}

static int safe_sock_new(BIO *bio) {
	bio->init = 0;
	bio->num = 0;
	bio->ptr = NULL;
	bio->flags = 0;

	return 1;
}

static int safe_sock_free(BIO *bio) {
	if (bio == NULL) {
		return 0;
	}

	if (bio->shutdown) {
		if (bio->init) {
			safe_shutdown(bio->num, SD_BOTH);
			safe_closesocket(bio->num);
		}

		bio->init = 0;
		bio->flags = 0;
	}

	return 1;
}

static int safe_sock_read(BIO *bio, char *Buffer, int Len) {
	int Result = 0;

	if (Buffer != NULL) {
		Result = safe_recv(bio->num, Buffer, Len, 0);
		WSASetLastError(safe_errno());
		BIO_clear_retry_flags(bio);

		if (Result <= 0) {
			if (BIO_sock_should_retry(Result)) {
				BIO_set_retry_read(bio);
			}
		}
	}

	return Result;
}

static int safe_sock_write(BIO *bio, const char *Buffer, int Len) {
	int Result;

	Result = safe_send(bio->num, Buffer, Len, 0);
	WSASetLastError(safe_errno());
	BIO_clear_retry_flags(bio);

	if (Result <= 0) {
		if (BIO_sock_should_retry(Result)) {
			BIO_set_retry_write(bio);
		}
	}

	return Result;
}

static long safe_sock_ctrl(BIO *bio, int cmd, long num, void *ptr) {
	long Result=1;
	int *ip;

	switch (cmd) {
		case BIO_CTRL_RESET:
			num = 0;
		case BIO_C_FILE_SEEK:
			Result=0;
			break;
		case BIO_C_FILE_TELL:
		case BIO_CTRL_INFO:
			Result = 0;
			break;
		case BIO_C_SET_FD:
			safe_sock_free(bio);
			bio->num = *((int *)ptr);
			bio->shutdown = (int)num;
			bio->init = 1;
			break;
		case BIO_C_GET_FD:
			if (bio->init) {
				ip = (int *)ptr;
				if (ip != NULL) {
					*ip = bio->num;
				}
				Result = bio->num;
			} else {
				Result = -1;
			}
			break;
		case BIO_CTRL_GET_CLOSE:
			Result = bio->shutdown;
			break;
		case BIO_CTRL_SET_CLOSE:
			bio->shutdown = (int)num;
			break;
		case BIO_CTRL_PENDING:
		case BIO_CTRL_WPENDING:
			Result = 0;
			break;
		case BIO_CTRL_DUP:
		case BIO_CTRL_FLUSH:
			Result = 1;
			break;
		default:
			Result = 0;
			break;
	}

	return Result;
}

static int safe_sock_puts(BIO *bio, const char *String) {
	int Len, Result;

	Len = strlen(String);
	Result = safe_sock_write(bio, String, Len);

	return Len;
}
#endif