2 Copyright (C) 2000-2004 SKYRIX Software AG
4 This file is part of OpenGroupware.org.
6 OGo is free software; you can redistribute it and/or modify it under
7 the terms of the GNU Lesser General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 OGo is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14 License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with OGo; see the file COPYING. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
23 #include <NGStreams/NGActiveSSLSocket.h>
27 # define id openssl_id
28 # include <openssl/ssl.h>
32 @interface NGActiveSocket(UsedPrivates)
33 - (BOOL)primaryConnectToAddress:(id<NGSocketAddress>)_address;
36 @implementation NGActiveSSLSocket
41 static int streamBIO_bwrite(BIO *, const char *, int) {
43 static int streamBIO_bread(BIO *, char *, int) {
45 static int streamBIO_bputs(BIO *, const char *) {
47 static int streamBIO_bgets(BIO *, char *, int) {
49 static long streamBIO_ctrl(BIO *, int, long, void *) {
51 static int streamBIO_create(BIO *) {
53 static int streamBIO_destroy(BIO *) {
55 static long streamBIO_callback_ctrl(BIO *, int, bio_info_cb *) {
58 static BIO_METHOD streamBIO = {
60 "NGActiveSocket" /* name */,
68 streamBIO_callback_ctrl
71 // create: BIO_new(&streamBIO);
73 #endif /* STREAM_BIO */
75 - (id)initWithDomain:(id<NGSocketDomain>)_domain {
76 if ((self = [super initWithDomain:_domain])) {
78 static BOOL didGlobalInit = NO;
81 /* Global system initialization*/
83 SSL_load_error_strings();
87 /* An error write context */
88 //bio_err = BIO_new_fp(stderr, BIO_NOCLOSE);
90 /* Create our context*/
92 if ((self->ctx = SSL_CTX_new(SSLv23_method())) == NULL) {
93 NSLog(@"ERROR(%s): couldn't create SSL context for v23 method !",
104 SSL_CTX_free(self->ctx);
110 /* basic IO, reading and writing bytes */
112 - (unsigned)readBytes:(void *)_buf count:(unsigned)_len {
113 if (self->ssl == NULL)
114 // should throw error
115 return NGStreamError;
117 return SSL_read(self->ssl, _buf, _len);
119 - (unsigned)writeBytes:(const void *)_buf count:(unsigned)_len {
120 return SSL_write(self->ssl, _buf, _len);
123 /* connection and shutdown */
125 - (BOOL)markNonblockingAfterConnect {
128 - (BOOL)primaryConnectToAddress:(id<NGSocketAddress>)_address {
129 if (self->ctx == NULL) {
130 NSLog(@"ERROR(%s): ctx isn't setup yet !",
131 __PRETTY_FUNCTION__);
135 if ((self->ssl = SSL_new(self->ctx)) == NULL) {
136 // should set exception !
137 NSLog(@"ERROR(%s): couldn't create SSL socket structure ...",
138 __PRETTY_FUNCTION__);
142 if (![super primaryConnectToAddress:_address])
143 /* could not connect to Unix socket ... */
146 /* probably we should create a BIO for streams !!! */
147 if ((self->sbio = BIO_new_socket(self->fd, BIO_NOCLOSE)) == NULL) {
148 NSLog(@"ERROR(%s): couldn't create SSL socket IO structure ...",
149 __PRETTY_FUNCTION__);
154 NSAssert(self->ctx, @"missing SSL context ...");
155 NSAssert(self->ssl, @"missing SSL socket ...");
156 NSAssert(self->sbio, @"missing SSL BIO ...");
158 SSL_set_bio(self->ssl, self->sbio, self->sbio);
159 if (SSL_connect(self->ssl) <= 0) {
160 NSLog(@"ERROR(%s): couldn't setup SSL connection on socket ...",
161 __PRETTY_FUNCTION__);
170 SSL_CTX_free(self->ctx);
173 return [super shutdown];
176 #else /* no OpenSSL available */
179 NSLog(@"WARNING: The NGActiveSSLSocket class was accessed, "
180 @"but OpenSSL support is turned off.");
182 - (id)initWithDomain:(id<NGSocketDomain>)_domain {
189 @end /* NGActiveSSLSocket */