2 Copyright (C) 2000-2005 SKYRIX Software AG
4 This file is part of SOPE.
6 SOPE 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 SOPE 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 SOPE; see the file COPYING. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
22 #include <NGStreams/NGActiveSSLSocket.h>
26 # define id openssl_id
27 # include <openssl/ssl.h>
31 @interface NGActiveSocket(UsedPrivates)
32 - (BOOL)primaryConnectToAddress:(id<NGSocketAddress>)_address;
35 @implementation NGActiveSSLSocket
40 static int streamBIO_bwrite(BIO *, const char *, int) {
42 static int streamBIO_bread(BIO *, char *, int) {
44 static int streamBIO_bputs(BIO *, const char *) {
46 static int streamBIO_bgets(BIO *, char *, int) {
48 static long streamBIO_ctrl(BIO *, int, long, void *) {
50 static int streamBIO_create(BIO *) {
52 static int streamBIO_destroy(BIO *) {
54 static long streamBIO_callback_ctrl(BIO *, int, bio_info_cb *) {
57 static BIO_METHOD streamBIO = {
59 "NGActiveSocket" /* name */,
67 streamBIO_callback_ctrl
70 // create: BIO_new(&streamBIO);
72 #endif /* STREAM_BIO */
74 - (id)initWithDomain:(id<NGSocketDomain>)_domain {
75 if ((self = [super initWithDomain:_domain])) {
77 static BOOL didGlobalInit = NO;
80 /* Global system initialization*/
82 SSL_load_error_strings();
86 /* An error write context */
87 //bio_err = BIO_new_fp(stderr, BIO_NOCLOSE);
89 /* Create our context*/
91 if ((self->ctx = SSL_CTX_new(SSLv23_method())) == NULL) {
92 NSLog(@"ERROR(%s): couldn't create SSL context for v23 method !",
103 SSL_CTX_free(self->ctx);
109 /* basic IO, reading and writing bytes */
111 - (unsigned)readBytes:(void *)_buf count:(unsigned)_len {
112 if (self->ssl == NULL)
113 // should throw error
114 return NGStreamError;
116 return SSL_read(self->ssl, _buf, _len);
118 - (unsigned)writeBytes:(const void *)_buf count:(unsigned)_len {
119 return SSL_write(self->ssl, _buf, _len);
122 /* connection and shutdown */
124 - (BOOL)markNonblockingAfterConnect {
127 - (BOOL)primaryConnectToAddress:(id<NGSocketAddress>)_address {
128 if (self->ctx == NULL) {
129 NSLog(@"ERROR(%s): ctx isn't setup yet !",
130 __PRETTY_FUNCTION__);
134 if ((self->ssl = SSL_new(self->ctx)) == NULL) {
135 // should set exception !
136 NSLog(@"ERROR(%s): couldn't create SSL socket structure ...",
137 __PRETTY_FUNCTION__);
141 if (![super primaryConnectToAddress:_address])
142 /* could not connect to Unix socket ... */
145 /* probably we should create a BIO for streams !!! */
146 if ((self->sbio = BIO_new_socket(self->fd, BIO_NOCLOSE)) == NULL) {
147 NSLog(@"ERROR(%s): couldn't create SSL socket IO structure ...",
148 __PRETTY_FUNCTION__);
153 NSAssert(self->ctx, @"missing SSL context ...");
154 NSAssert(self->ssl, @"missing SSL socket ...");
155 NSAssert(self->sbio, @"missing SSL BIO ...");
157 SSL_set_bio(self->ssl, self->sbio, self->sbio);
158 if (SSL_connect(self->ssl) <= 0) {
159 NSLog(@"ERROR(%s): couldn't setup SSL connection on socket ...",
160 __PRETTY_FUNCTION__);
169 SSL_CTX_free(self->ctx);
172 return [super shutdown];
175 #else /* no OpenSSL available */
178 NSLog(@"WARNING: The NGActiveSSLSocket class was accessed, "
179 @"but OpenSSL support is turned off.");
181 - (id)initWithDomain:(id<NGSocketDomain>)_domain {
188 @end /* NGActiveSSLSocket */