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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
|
/*
* Copyright (C) 2001-2005 Rochester Institute of Technology
*
* 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/*
* Author: Matt Campbell
* Descrip: SSL connection routines
*/
#include <sys/types.h>
#include <netinet/in.h>
#if HAVE_INTTYPES_H
#include <inttypes.h>
#else
# if HAVE_STDINT_H
# include <stdint.h>
# else
typedef unsigned int uint32_t;
# endif
#endif
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <openssl/rand.h>
#include <openssl/err.h>
#include <fcntl.h>
#include "nanohttp-ssl.h"
#include "nanohttp-common.h"
#ifdef WIN32
#include <io.h>
typedef unsigned int uint32_t;
#else
#include <unistd.h>
#endif
#define MAXCHUNK 1024
#define HEADER_LEN 5
char HEADER[HEADER_LEN] = {186, 84, 202, 86, 224};
static char *pass;
/*
* superseed
* Creates a 1k random seed and uses it to seed
* the SSL random number generator
*/
void superseed()
{
int buf[256], i;
srand(time(NULL));
for(i = 0; i < 256; i++) {
buf[i] = rand();
} RAND_seed( (unsigned char*)buf, sizeof(buf));
}
static int pw_cb(char* buf, int num, int rwflag, void *userdata)
{
if( num < (int)strlen(pass) + 1)
return(0);
strcpy(buf, pass);
return strlen(pass);
}
int verify_sn(X509* cert, int who, int nid, char* str)
{
char name[256];
char buf[256];
memset(name, '\0', 256);
memset(buf, '\0', 256);
if(who == CERT_SUBJECT) {
X509_NAME_oneline(X509_get_subject_name(cert), name, 256);
} else {
X509_NAME_oneline(X509_get_issuer_name(cert), name, 256);
}
buf[0] = '/';
strcat(buf, OBJ_nid2sn(nid));
strcat(buf, "=");
strcat(buf, str);
if(strstr(name, buf)) {
return 1;
} else {
return 0;
}
}
static int verify_cb(int prev_ok, X509_STORE_CTX* ctx)
{
X509* cert = X509_STORE_CTX_get_current_cert(ctx);
int depth = X509_STORE_CTX_get_error_depth(ctx);
int err = X509_STORE_CTX_get_error(ctx);
if( err = X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN ){
return 1;
}
if(depth == 0) {
return user_verify(cert);
} else {
return prev_ok;
}
}
int user_verify(X509* cert)
{
//TODO: Make sure that the client is providing a client cert,
//or that the Module is providing the Module cert
/* connect to anyone */
log_verbose1("Validating certificate.");
return 1;
}
SSL_CTX *initialize_ctx(char* keyfile, char* password, char* calist)
{
SSL_CTX* ctx = NULL;
if(password == NULL) password = "";
/* Global system initialization */
log_verbose1( "Initializing library");
SSL_library_init();
SSL_load_error_strings();
ERR_load_crypto_strings();
OpenSSL_add_ssl_algorithms();
/* Create our context*/
ctx = SSL_CTX_new( SSLv23_method() );
if(ctx == NULL) {
log_error1( "Cannot create SSL context");
return NULL;
}
log_verbose1( "SSL context created ok");
/* Load our keys and certificates*/
if(keyfile != NULL && password != NULL ){
if(!( SSL_CTX_use_certificate_file(ctx, keyfile, SSL_FILETYPE_PEM))){
log_error2( "Couldn't read certificate file: %s", keyfile);
SSL_CTX_free(ctx);
return ctx = NULL;
}
log_verbose1("Certificate file read ok");
pass = password;
SSL_CTX_set_default_passwd_cb(ctx, pw_cb);
if( !( SSL_CTX_use_PrivateKey_file(ctx, keyfile, SSL_FILETYPE_PEM) ) ) {
log_error2( "Couldn't read key file: %s", keyfile);
SSL_CTX_free(ctx);
return ctx = NULL;
}
log_verbose1("Keyfile read ok");
}
if( calist != NULL) {
/* Load the CAs we trust */
if( !( SSL_CTX_load_verify_locations(ctx, calist, NULL) ) ) {
log_error2("Couldn't read CA list: %s", calist);
SSL_CTX_free(ctx);
return ctx = NULL;
}
SSL_CTX_set_client_CA_list( ctx, SSL_load_client_CA_file(calist) );
log_verbose1("Certificate Authority contacted");
}
SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER | SSL_VERIFY_CLIENT_ONCE,
verify_cb);
log_verbose1("Verify callback registered");
SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_OFF );
/* Load randomness */
superseed();
return ctx;
}
void log_ssl_error(SSL* ssl, int ret)
{
int errqueue;
char errorbuf[256] = "Error: ";
if(ret == 0) {
log_error1("SSL handshake was not successful, contolled shutdown");
} else if(ret == -1) {
log_error1("SSL handshake was not successful, fatal error at protocol");
}
errqueue = SSL_get_error(ssl, ret);
switch(errqueue) {
case SSL_ERROR_NONE:
strcat(errorbuf, "None");
break;
case SSL_ERROR_ZERO_RETURN:
strcat(errorbuf, "Zero return");
break;
case SSL_ERROR_WANT_READ:
strcat(errorbuf, "Want read");
break;
case SSL_ERROR_WANT_WRITE:
strcat(errorbuf, "Want write");
break;
case SSL_ERROR_WANT_X509_LOOKUP:
strcat(errorbuf, "Want x509 lookup");
break;
case SSL_ERROR_SYSCALL:
strcat(errorbuf, "Syscall:");
if(ret == 0) {
strcat(errorbuf, "Protocol violation");
} else if(ret == -1) {
strcat(errorbuf, "BIO reported an I/O error");
} else {
strcat(errorbuf, "Unknown syscall error");
} /* if */
break;
case SSL_ERROR_SSL:
strcat(errorbuf, "SSL library");
while(errqueue=ERR_get_error()){
log_error2("SSL %s", ERR_error_string(errqueue, NULL));
}
break;
} /* switch code */
log_error1( errorbuf);
}
SSL* init_ssl(SSL_CTX* ctx, int sock, int type)
{
int ret;
int status;
SSL* ssl;
#if 0
#ifdef WIN32
BIO* rbio;
BIO* wbio;
#else
BIO* sbio;
#endif
#endif
log_verbose1("Starting SSL Initialization");
ssl = SSL_new(ctx);
if(ssl == NULL) {
log_error1( "Cannot create new ssl object");
return NULL;
}
#if 0
#ifdef WIN32
log_error1("Setting up BIO with socket");
rbio = BIO_new_socket(sock, BIO_NOCLOSE);
if( rbio == NULL ) {
log_error1( "BIO_new_socket failed");
return NULL;
}
SSL_set_bio(ssl, rbio, rbio);
#else
sbio = BIO_new_socket(sock, BIO_NOCLOSE);
if( sbio == NULL ) {
log_error1( "BIO_new_socket failed");
return NULL;
}
SSL_set_bio(ssl, sbio, sbio);
#endif
#endif
SSL_set_fd(ssl, sock);
if(type == SSL_SERVER) {
ret = SSL_accept(ssl);
if(ret <= 0) {
log_error1( "SSL accept error");
log_ssl_error(ssl, ret);
SSL_free(ssl);
return ssl = NULL;
} /* if error */
} else { /* client */
ret = SSL_connect(ssl);
if(ret <= 0) {
log_error1( "SSL connect error");
log_ssl_error(ssl, ret);
SSL_free(ssl);
return ssl = NULL;
} /* if error */
/* SSL_connect should take care of this for us.
if(SSL_get_peer_certificate(ssl) == NULL) {
log_error1( "No certificate provided");
SSL_free(ssl);
return ssl = NULL;
}
if(SSL_get_verify_result(ssl) != X509_V_OK) {
log_error1( "Certificate did not verify");
SSL_free(ssl);
return ssl = NULL;
}
*/
}
log_verbose1("Completed SSL Initialization");
return ssl;
}
void ssl_cleanup(SSL* ssl)
{
/* does nothing to context */
if(ssl != NULL) {
SSL_shutdown(ssl);
// SSL_clear(ssl);
SSL_free(ssl);
ssl = NULL;
}
}
|