mturk       2005/06/01 03:45:03

  Modified:    jni/native/include ssl_private.h
               jni/native/src ssl.c sslcontext.c sslutils.c
  Log:
  Add reference counter to BIO handler, so that multiple context can
  register the same BIO.
  
  Revision  Changes    Path
  1.8       +6 -1      
jakarta-tomcat-connectors/jni/native/include/ssl_private.h
  
  Index: ssl_private.h
  ===================================================================
  RCS file: 
/home/cvs/jakarta-tomcat-connectors/jni/native/include/ssl_private.h,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- ssl_private.h     1 Jun 2005 09:05:08 -0000       1.7
  +++ ssl_private.h     1 Jun 2005 10:45:02 -0000       1.8
  @@ -83,6 +83,9 @@
   #define SSL_PROTOCOL_TLSV1 (1<<2)
   #define SSL_PROTOCOL_ALL   
(SSL_PROTOCOL_SSLV2|SSL_PROTOCOL_SSLV3|SSL_PROTOCOL_TLSV1)
   
  +#define SSL_BIO_FLAG_RDONLY     (1<<0)
  +#define SSL_BIO_FLAG_CALLBACK   (1<<1)
  +
   /* public cert/private key */
   typedef struct {
       /*
  @@ -147,5 +150,7 @@
   void       *SSL_get_app_data2(SSL *);
   void        SSL_set_app_data2(SSL *, void *);
   int         SSL_password_prompt(tcn_ssl_ctxt_t *, char *, int);
  +void        SSL_BIO_close(BIO *);
  +void        SSL_BIO_doref(BIO *);
   
   #endif /* SSL_PRIVATE_H */
  
  
  
  1.16      +42 -9     jakarta-tomcat-connectors/jni/native/src/ssl.c
  
  Index: ssl.c
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat-connectors/jni/native/src/ssl.c,v
  retrieving revision 1.15
  retrieving revision 1.16
  diff -u -r1.15 -r1.16
  --- ssl.c     1 Jun 2005 08:19:39 -0000       1.15
  +++ ssl.c     1 Jun 2005 10:45:03 -0000       1.16
  @@ -400,10 +400,12 @@
   /* OpenSSL Java Stream BIO */
   
   typedef struct  {
  +    int            refcount;
       apr_pool_t     *pool;
       tcn_callback_t cb;
   } BIO_JAVA;
   
  +
   static apr_status_t generic_bio_cleanup(void *data)
   {
       BIO *b = (BIO *)data;
  @@ -414,12 +416,43 @@
       return APR_SUCCESS;
   }
   
  +void SSL_BIO_close(BIO *bi)
  +{
  +    if (bi == NULL)
  +        return;
  +    if (bi->ptr != NULL && (bi->flags & SSL_BIO_FLAG_CALLBACK)) {
  +        BIO_JAVA *j = (BIO_JAVA *)bi->ptr;
  +        j->refcount--;
  +        if (j->refcount == 0) {
  +            if (j->pool)
  +                apr_pool_cleanup_run(j->pool, bi, generic_bio_cleanup);
  +            else
  +                BIO_free(bi);
  +        }
  +    }
  +    else
  +        BIO_free(bi);
  +}
  +
  +void SSL_BIO_doref(BIO *bi)
  +{
  +    if (bi == NULL)
  +        return;
  +    if (bi->ptr != NULL && (bi->flags & SSL_BIO_FLAG_CALLBACK)) {
  +        BIO_JAVA *j = (BIO_JAVA *)bi->ptr;
  +        j->refcount++;
  +    }
  +}
  +
  +
   static int jbs_new(BIO *bi)
   {
       BIO_JAVA *j;
   
       if ((j = OPENSSL_malloc(sizeof(BIO_JAVA))) == NULL)
           return 0;
  +    j->pool      = NULL;
  +    j->refcount  = 1;
       bi->shutdown = 1;
       bi->init     = 0;
       bi->num      = -1;
  @@ -435,9 +468,9 @@
       if (bi->ptr != NULL) {
           BIO_JAVA *j = (BIO_JAVA *)bi->ptr;
           if (bi->init) {
  +            bi->init = 0;
               TCN_UNLOAD_CLASS(j->cb.env, j->cb.obj);
           }
  -        bi->init = 0;
           OPENSSL_free(bi->ptr);
       }
       bi->ptr = NULL;
  @@ -552,6 +585,10 @@
           goto init_failed;
       }
       j = (BIO_JAVA *)bio->ptr;
  +    if ((j = (BIO_JAVA *)bio->ptr) == NULL) {
  +        tcn_ThrowException(e, "Create BIO failed");
  +        goto init_failed;
  +    }
       j->pool = J2P(pool, apr_pool_t *);
       if (j->pool) {
           apr_pool_cleanup_register(j->pool, (const void *)bio,
  @@ -568,7 +605,8 @@
       /* TODO: Check if method id's are valid */
       j->cb.obj    = (*e)->NewGlobalRef(e, callback);
   
  -    bio->init = 1;
  +    bio->init  = 1;
  +    bio->flags = SSL_BIO_FLAG_CALLBACK;
       return P2J(bio);
   init_failed:
       return 0;
  @@ -577,13 +615,8 @@
   TCN_IMPLEMENT_CALL(jint, SSL, closeBIO)(TCN_STDARGS, jlong bio)
   {
       BIO *b = J2P(bio, BIO *);
  -    BIO_JAVA *j;
  -
       UNREFERENCED_STDARGS;
  -    j = (BIO_JAVA *)b->ptr;
  -    if (j->pool) {
  -        apr_pool_cleanup_run(j->pool, b, generic_bio_cleanup);
  -    }
  +    SSL_BIO_close(b);
       return APR_SUCCESS;
   }
   
  
  
  
  1.7       +10 -7     jakarta-tomcat-connectors/jni/native/src/sslcontext.c
  
  Index: sslcontext.c
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat-connectors/jni/native/src/sslcontext.c,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- sslcontext.c      1 Jun 2005 09:05:08 -0000       1.6
  +++ sslcontext.c      1 Jun 2005 10:45:03 -0000       1.7
  @@ -57,11 +57,12 @@
               sk_X509_INFO_pop_free(c->pk.c.certs, X509_INFO_free);
               c->pk.c.certs = NULL;
           }
  +
           if (c->bio_is)
  -            BIO_free(c->bio_is);
  +            SSL_BIO_close(c->bio_is);
           c->bio_is = NULL;
           if (c->bio_os)
  -            BIO_free(c->bio_os);
  +            SSL_BIO_close(c->bio_os);
           c->bio_os = NULL;
       }
       return APR_SUCCESS;
  @@ -111,7 +112,7 @@
           BIO_set_fp(c->bio_os, stderr, BIO_NOCLOSE | BIO_FP_TEXT);
       if (c->bio_is != NULL) {
           BIO_set_fp(c->bio_is, stdin, BIO_NOCLOSE | BIO_FP_TEXT);
  -        c->bio_is->flags = BIO_FLAGS_MEM_RDONLY;
  +        c->bio_is->flags = SSL_BIO_FLAG_RDONLY;
       }
       SSL_CTX_set_options(c->ctx, SSL_OP_ALL);
       if (!(protocol & SSL_PROTOCOL_SSLV2))
  @@ -188,7 +189,7 @@
           BIO_set_fp(c->bio_os, stderr, BIO_NOCLOSE | BIO_FP_TEXT);
       if (c->bio_is != NULL) {
           BIO_set_fp(c->bio_is, stdin, BIO_NOCLOSE | BIO_FP_TEXT);
  -        c->bio_is->flags = BIO_FLAGS_MEM_RDONLY;
  +        c->bio_is->flags = SSL_BIO_FLAG_RDONLY;
       }
       SSL_CTX_set_options(c->ctx, SSL_OP_ALL);
       if (!(protocol & SSL_PROTOCOL_SSLV2))
  @@ -254,7 +255,8 @@
       UNREFERENCED_STDARGS;
       TCN_ASSERT(ctx != 0);
       if (c->bio_os && c->bio_os != bio_os)
  -        BIO_free(c->bio_os);
  +        SSL_BIO_close(c->bio_os);
  +    SSL_BIO_doref(bio_os);
       c->bio_os = bio_os;
   }
   
  @@ -267,7 +269,8 @@
       UNREFERENCED_STDARGS;
       TCN_ASSERT(ctx != 0);
       if (c->bio_is && c->bio_is != bio_is)
  -        BIO_free(c->bio_is);
  +        SSL_BIO_close(c->bio_is);
  +    SSL_BIO_doref(bio_is);
       c->bio_is = bio_is;
   }
   
  
  
  
  1.6       +2 -2      jakarta-tomcat-connectors/jni/native/src/sslutils.c
  
  Index: sslutils.c
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat-connectors/jni/native/src/sslutils.c,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- sslutils.c        1 Jun 2005 09:05:08 -0000       1.5
  +++ sslutils.c        1 Jun 2005 10:45:03 -0000       1.6
  @@ -105,7 +105,7 @@
   {
       int rv = 0;
       if (c && c->bio_is) {
  -        if (c->bio_is->flags & BIO_FLAGS_MEM_RDONLY) {
  +        if (c->bio_is->flags & SSL_BIO_FLAG_RDONLY) {
               /* Use error BIO in case of stdin */
               BIO_printf(c->bio_os, "Enter password: ");
           }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to