From 8b8d963db5bb619fbada014f294fd09a855a2650 Mon Sep 17 00:00:00 2001 From: Rich Salz Date: Fri, 19 Aug 2016 21:04:41 -0400 Subject: [PATCH] Add BIO_get_new_index() Reviewed-by: Dr. Stephen Henson --- crypto/bio/b_addr.c | 4 ++- crypto/bio/bio_lcl.h | 1 + crypto/bio/bio_lib.c | 2 ++ crypto/bio/bio_meth.c | 12 ++++++++ doc/crypto/BIO_meth_new.pod | 7 ++++- include/openssl/bio.h | 58 +++++++++++++++++++------------------ util/libcrypto.num | 1 + 7 files changed, 55 insertions(+), 30 deletions(-) diff --git a/crypto/bio/b_addr.c b/crypto/bio/b_addr.c index 4e8785fdcd41e..17ab3e4e83e14 100644 --- a/crypto/bio/b_addr.c +++ b/crypto/bio/b_addr.c @@ -19,6 +19,7 @@ #include CRYPTO_RWLOCK *bio_lookup_lock; +extern CRYPTO_RWLOCK *bio_type_lock; static CRYPTO_ONCE bio_lookup_init = CRYPTO_ONCE_STATIC_INIT; /* @@ -605,7 +606,8 @@ static int addrinfo_wrap(int family, int socktype, DEFINE_RUN_ONCE_STATIC(do_bio_lookup_init) { bio_lookup_lock = CRYPTO_THREAD_lock_new(); - return (bio_lookup_lock != NULL); + bio_type_lock = CRYPTO_THREAD_lock_new(); + return bio_lookup_lock != NULL && bio_type_lock != NULL; } /*- diff --git a/crypto/bio/bio_lcl.h b/crypto/bio/bio_lcl.h index f5a886cb7ccf3..5d0b827930727 100644 --- a/crypto/bio/bio_lcl.h +++ b/crypto/bio/bio_lcl.h @@ -137,6 +137,7 @@ typedef unsigned int socklen_t; # endif extern CRYPTO_RWLOCK *bio_lookup_lock; +extern CRYPTO_RWLOCK *bio_type_lock; int BIO_ADDR_make(BIO_ADDR *ap, const struct sockaddr *sa); const struct sockaddr *BIO_ADDR_sockaddr(const BIO_ADDR *ap); diff --git a/crypto/bio/bio_lib.c b/crypto/bio/bio_lib.c index 98f3707ea5881..8a00103e2c49a 100644 --- a/crypto/bio/bio_lib.c +++ b/crypto/bio/bio_lib.c @@ -594,5 +594,7 @@ void bio_cleanup(void) bio_sock_cleanup_int(); CRYPTO_THREAD_lock_free(bio_lookup_lock); bio_lookup_lock = NULL; + CRYPTO_THREAD_lock_free(bio_type_lock); + bio_type_lock = NULL; #endif } diff --git a/crypto/bio/bio_meth.c b/crypto/bio/bio_meth.c index d172047f55773..7754b003947ea 100644 --- a/crypto/bio/bio_meth.c +++ b/crypto/bio/bio_meth.c @@ -9,6 +9,18 @@ #include "bio_lcl.h" +CRYPTO_RWLOCK *bio_type_lock; +static int bio_count = BIO_TYPE_START; + +int BIO_get_new_index() +{ + int newval; + + if (!CRYPTO_atomic_add(&bio_count, 1, &newval, bio_type_lock)) + return -1; + return newval; +} + BIO_METHOD *BIO_meth_new(int type, const char *name) { BIO_METHOD *biom = OPENSSL_zalloc(sizeof(BIO_METHOD)); diff --git a/doc/crypto/BIO_meth_new.pod b/doc/crypto/BIO_meth_new.pod index 65e48cb24fcba..bf3316104d2ee 100644 --- a/doc/crypto/BIO_meth_new.pod +++ b/doc/crypto/BIO_meth_new.pod @@ -2,6 +2,7 @@ =head1 NAME +BIO_get_new_index, BIO_meth_new, BIO_meth_free, BIO_meth_get_write, BIO_meth_set_write, BIO_meth_get_read, BIO_meth_set_read, BIO_meth_get_puts, BIO_meth_set_puts, BIO_meth_get_gets, BIO_meth_set_gets, BIO_meth_get_ctrl, BIO_meth_set_ctrl, @@ -13,6 +14,7 @@ BIO_meth_set_callback_ctrl - Routines to build up BIO methods #include + int BIO_get_new_index(void); BIO_METHOD *BIO_meth_new(int type, const char *name); void BIO_meth_free(BIO_METHOD *biom); int (*BIO_meth_get_write(BIO_METHOD *biom)) (BIO *, const char *, int); @@ -47,7 +49,10 @@ types. It provides a set of of functions used by OpenSSL for the implementation of the various BIO capabilities. See the L page for more information. BIO_meth_new() creates a new B structure. It should be given a -unique integer B and a string that represents its B. The set of +unique integer B and a string that represents its B. +Use BIO_get_new_index() to get the value for B. + +The set of standard OpenSSL provided BIO types is provided in B. Some examples include B and B. Filter BIOs should have a type which have the "filter" bit set (B). Source/sink BIOs diff --git a/include/openssl/bio.h b/include/openssl/bio.h index f847348d5ae4f..d733f4701cc44 100644 --- a/include/openssl/bio.h +++ b/include/openssl/bio.h @@ -31,38 +31,39 @@ extern "C" { #endif +/* There are the classes of BIOs */ +# define BIO_TYPE_DESCRIPTOR 0x0100 /* socket, fd, connect or accept */ +# define BIO_TYPE_FILTER 0x0200 +# define BIO_TYPE_SOURCE_SINK 0x0400 + /* These are the 'types' of BIOs */ -# define BIO_TYPE_NONE 0 -# define BIO_TYPE_MEM (1|0x0400) -# define BIO_TYPE_FILE (2|0x0400) - -# define BIO_TYPE_FD (4|0x0400|0x0100) -# define BIO_TYPE_SOCKET (5|0x0400|0x0100) -# define BIO_TYPE_NULL (6|0x0400) -# define BIO_TYPE_SSL (7|0x0200) -# define BIO_TYPE_MD (8|0x0200)/* passive filter */ -# define BIO_TYPE_BUFFER (9|0x0200)/* filter */ -# define BIO_TYPE_CIPHER (10|0x0200)/* filter */ -# define BIO_TYPE_BASE64 (11|0x0200)/* filter */ -# define BIO_TYPE_CONNECT (12|0x0400|0x0100)/* socket - connect */ -# define BIO_TYPE_ACCEPT (13|0x0400|0x0100)/* socket for accept */ -/* # define BIO_TYPE_PROXY_CLIENT (14|0x0200)*/ /* client proxy BIO */ -/* # define BIO_TYPE_PROXY_SERVER (15|0x0200)*/ /* server proxy BIO */ -# define BIO_TYPE_NBIO_TEST (16|0x0200)/* server proxy BIO */ -# define BIO_TYPE_NULL_FILTER (17|0x0200) -# define BIO_TYPE_BER (18|0x0200)/* BER -> bin filter */ -# define BIO_TYPE_BIO (19|0x0400)/* (half a) BIO pair */ -# define BIO_TYPE_LINEBUFFER (20|0x0200)/* filter */ -# define BIO_TYPE_DGRAM (21|0x0400|0x0100) +# define BIO_TYPE_NONE 0 +# define BIO_TYPE_MEM ( 1|BIO_TYPE_SOURCE_SINK) +# define BIO_TYPE_FILE ( 2|BIO_TYPE_SOURCE_SINK) + +# define BIO_TYPE_FD ( 4|BIO_TYPE_SOURCE_SINK|BIO_TYPE_DESCRIPTOR) +# define BIO_TYPE_SOCKET ( 5|BIO_TYPE_SOURCE_SINK|BIO_TYPE_DESCRIPTOR) +# define BIO_TYPE_NULL ( 6|BIO_TYPE_SOURCE_SINK) +# define BIO_TYPE_SSL ( 7|BIO_TYPE_FILTER) +# define BIO_TYPE_MD ( 8|BIO_TYPE_FILTER) +# define BIO_TYPE_BUFFER ( 9|BIO_TYPE_FILTER) +# define BIO_TYPE_CIPHER (10|BIO_TYPE_FILTER) +# define BIO_TYPE_BASE64 (11|BIO_TYPE_FILTER) +# define BIO_TYPE_CONNECT (12|BIO_TYPE_SOURCE_SINK|BIO_TYPE_DESCRIPTOR) +# define BIO_TYPE_ACCEPT (13|BIO_TYPE_SOURCE_SINK|BIO_TYPE_DESCRIPTOR) + +# define BIO_TYPE_NBIO_TEST (16|BIO_TYPE_FILTER)/* server proxy BIO */ +# define BIO_TYPE_NULL_FILTER (17|BIO_TYPE_FILTER) +# define BIO_TYPE_BIO (19|BIO_TYPE_SOURCE_SINK)/* half a BIO pair */ +# define BIO_TYPE_LINEBUFFER (20|BIO_TYPE_FILTER) +# define BIO_TYPE_DGRAM (21|BIO_TYPE_SOURCE_SINK|BIO_TYPE_DESCRIPTOR) +# define BIO_TYPE_ASN1 (22|BIO_TYPE_FILTER) +# define BIO_TYPE_COMP (23|BIO_TYPE_FILTER) # ifndef OPENSSL_NO_SCTP -# define BIO_TYPE_DGRAM_SCTP (24|0x0400|0x0100) +# define BIO_TYPE_DGRAM_SCTP (24|BIO_TYPE_SOURCE_SINK|BIO_TYPE_DESCRIPTOR) # endif -# define BIO_TYPE_ASN1 (22|0x0200)/* filter */ -# define BIO_TYPE_COMP (23|0x0200)/* filter */ -# define BIO_TYPE_DESCRIPTOR 0x0100/* socket, fd, connect or accept */ -# define BIO_TYPE_FILTER 0x0200 -# define BIO_TYPE_SOURCE_SINK 0x0400 +#define BIO_TYPE_START 128 /* * BIO_FILENAME_READ|BIO_CLOSE to open or close on free. @@ -177,6 +178,7 @@ extern "C" { typedef union bio_addr_st BIO_ADDR; typedef struct bio_addrinfo_st BIO_ADDRINFO; +int BIO_get_new_index(void); void BIO_set_flags(BIO *b, int flags); int BIO_test_flags(const BIO *b, int flags); void BIO_clear_flags(BIO *b, int flags); diff --git a/util/libcrypto.num b/util/libcrypto.num index 83b35effe927a..c0d630942ea37 100644 --- a/util/libcrypto.num +++ b/util/libcrypto.num @@ -4198,3 +4198,4 @@ X509_CRL_get0_lastUpdate 4144 1_1_0 EXIST::FUNCTION: X509_get0_notBefore 4145 1_1_0 EXIST::FUNCTION: X509_get0_notAfter 4146 1_1_0 EXIST::FUNCTION: X509_CRL_get0_nextUpdate 4147 1_1_0 EXIST::FUNCTION: +BIO_get_new_index 4148 1_1_0 EXIST::FUNCTION: