- 论坛徽章:
- 0
|
Linux安全体系学习笔记之四:OpenSSL源代码分析(3) .
BIO是对IO操作的封装,OpenSSL的BIO抽象接口不仅可以对SSL连接的I/O使用,也可以对非加密的网络连接和文件的I/O使用。BIO的相关源代码在crypto/bio文件夹下。
BIO的相关数据结构列出如下。
BIO结构:
view plaincopy to clipboardprint?- 01.struct bio_st
- 02. {
- 03. BIO_METHOD *method;
- 04. /* bio, mode, argp, argi, argl, ret */
- 05. long (*callback)(struct bio_st *,int,const char *,int, long,long);
- 06. char *cb_arg; /* first argument for the callback */
- 07.
- 08. int init;
- 09. int shutdown;
- 10. int flags; /* extra storage */
- 11. int retry_reason;
- 12. int num;
- 13. void *ptr;
- 14. struct bio_st *next_bio; /* used by filter BIOs */
- 15. struct bio_st *prev_bio; /* used by filter BIOs */
- 16. int references;
- 17. unsigned long num_read;
- 18. unsigned long num_write;
- 19.
- 20. CRYPTO_EX_DATA ex_data;
- 21. };
- 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操作的结构:
view plaincopy to clipboardprint?- 01.typedef struct bio_method_st
- 02. {
- 03. int type;
- 04. const char *name;
- 05. int (*bwrite)(BIO *, const char *, int);
- 06. int (*bread)(BIO *, char *, int);
- 07. int (*bputs)(BIO *, const char *);
- 08. int (*bgets)(BIO *, char *, int);
- 09. long (*ctrl)(BIO *, int, long, void *);
- 10. int (*create)(BIO *);
- 11. int (*destroy)(BIO *);
- 12. long (*callback_ctrl)(BIO *, int, bio_info_cb *);
- 13. } BIO_METHOD;
- 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接口类型分为源/接收类型和过滤类型两种。
view plaincopy to clipboardprint?- 01.#define BIO_TYPE_DESCRIPTOR0x0100 /* socket, fd, connect or accept */
- 02.#define BIO_TYPE_FILTER 0x0200
- 03.#define BIO_TYPE_SOURCE_SINK 0x0400
- #define BIO_TYPE_DESCRIPTOR0x0100 /* socket, fd, connect or accept */
- #define BIO_TYPE_FILTER 0x0200
- #define BIO_TYPE_SOURCE_SINK 0x0400
复制代码 1、源/接收类型
view plaincopy to clipboardprint?- 01.#define BIO_TYPE_MEM(1|0x0400)
- 02.#define BIO_TYPE_FILE (2|0x0400)
- 03.#define BIO_TYPE_FD (4|0x0400|0x0100)
- 04.#define BIO_TYPE_SOCKET (5|0x0400|0x0100)
- 05.#define BIO_TYPE_NULL (6|0x0400)
- 06.#define BIO_TYPE_CONNECT(12|0x0400|0x0100)/* socket - connect */
- 07.#define BIO_TYPE_ACCEPT(13|0x0400|0x0100)/* socket for accept */
- 08.#define BIO_TYPE_BIO(19|0x0400)/* (half a) BIO pair */
- 09.#define BIO_TYPE_DGRAM(21|0x0400|0x0100)
- #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、过滤类型
view plaincopy to clipboardprint?- 01.#define BIO_TYPE_SSL(7|0x0200)
- 02.#define BIO_TYPE_MD(8|0x0200) /* passive filter */
- 03.#define BIO_TYPE_BUFFER (9|0x0200)/* filter */
- 04.#define BIO_TYPE_CIPHER (10|0x0200)/* filter */
- 05.#define BIO_TYPE_BASE64 (11|0x0200)/* filter */
- 06.#define BIO_TYPE_PROXY_CLIENT (14|0x0200)/* client proxy BIO */
- 07.#define BIO_TYPE_PROXY_SERVER (15|0x0200)/* server proxy BIO */
- 08.#define BIO_TYPE_NBIO_TEST (16|0x0200)/* server proxy BIO */
- 09.#define BIO_TYPE_NULL_FILTER (17|0x0200)
- 10.#define BIO_TYPE_BER (18|0x0200)/* BER -> bin filter */
- 11.#define BIO_TYPE_LINEBUFFER (20|0x0200)/* filter */
- 12.#define BIO_TYPE_ASN1 (22|0x0200)/* filter */
- 13.#define BIO_TYPE_COMP (23|0x0200)/* filter */
- #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过滤缓冲结构:
view plaincopy to clipboardprint?- 01.typedef struct bio_f_buffer_ctx_struct
- 02. {
- 03. /* BIO *bio; */ /* this is now in the BIO struct */
- 04. int ibuf_size; /* how big is the input buffer */
- 05. int obuf_size; /* how big is the output buffer */
- 06.
- 07. char *ibuf; /* the char array */
- 08. int ibuf_len; /* how many bytes are in it */
- 09. int ibuf_off; /* write/read offset */
- 10.
- 11. char *obuf; /* the char array */
- 12. int obuf_len; /* how many bytes are in it */
- 13. int obuf_off; /* write/read offset */
- 14. } BIO_F_BUFFER_CTX;
复制代码 |
|