Linux安全体系学习笔记之四:OpenSSL源代码分析(3)

BIO是对IO操作的封装,OpenSSL的BIO抽象接口不仅可以对SSL连接的I/O使用,也可以对非加密的网络连接和文件的I/O使用。BIO的相关源代码在crypto/bio文件夹下。

BIO的相关数据结构列出如下。

BIO结构:

struct bio_st
	{
	BIO_METHOD *method;
	/* bio, mode, argp, argi, argl, ret */
	long (*callback)(struct bio_st *,int,const char *,int, long,long);
	char *cb_arg; /* first argument for the callback */

	int init;
	int shutdown;
	int flags;	/* extra storage */
	int retry_reason;
	int num;
	void *ptr;
	struct bio_st *next_bio;	/* used by filter BIOs */
	struct bio_st *prev_bio;	/* used by filter BIOs */
	int references;
	unsigned long num_read;
	unsigned long num_write;

	CRYPTO_EX_DATA ex_data;
	};

BIO操作的结构:

typedef struct bio_method_st
	{
	int type;
	const char *name;
	int (*bwrite)(BIO *, const char *, int);
	int (*bread)(BIO *, char *, int);
	int (*bputs)(BIO *, const char *);
	int (*bgets)(BIO *, char *, int);
	long (*ctrl)(BIO *, int, long, void *);
	int (*create)(BIO *);
	int (*destroy)(BIO *);
        long (*callback_ctrl)(BIO *, int, bio_info_cb *);
	} BIO_METHOD;

BIO接口类型分为源/接收类型和过滤类型两种。

#define BIO_TYPE_DESCRIPTOR0x0100 /* socket, fd, connect or accept */
#define BIO_TYPE_FILTER 0x0200
#define BIO_TYPE_SOURCE_SINK 0x0400

1、源/接收类型

#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_CONNECT(12|0x0400|0x0100)/* socket - connect */
#define BIO_TYPE_ACCEPT(13|0x0400|0x0100)/* socket for accept */
#define BIO_TYPE_BIO(19|0x0400)/* (half a) BIO pair */
#define BIO_TYPE_DGRAM(21|0x0400|0x0100)

2、过滤类型

#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_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_LINEBUFFER (20|0x0200)/* filter */
#define BIO_TYPE_ASN1 (22|0x0200)/* filter */
#define BIO_TYPE_COMP (23|0x0200)/* filter */

BIO过滤缓冲结构:

typedef struct bio_f_buffer_ctx_struct
	{
	/* BIO *bio; */ /* this is now in the BIO struct */
	int ibuf_size;	/* how big is the input buffer */
	int obuf_size;	/* how big is the output buffer */

	char *ibuf;		/* the char array */
	int ibuf_len;		/* how many bytes are in it */
	int ibuf_off;		/* write/read offset */

	char *obuf;		/* the char array */
	int obuf_len;		/* how many bytes are in it */
	int obuf_off;		/* write/read offset */
	} BIO_F_BUFFER_CTX;


你可能感兴趣的:(linux,struct,filter,buffer,callback,代码分析)