免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1445 | 回复: 1
打印 上一主题 下一主题

Linux安全体系学习笔记之四:OpenSSL源代码分析(3) . [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-12-28 10:18 |只看该作者 |倒序浏览
Linux安全体系学习笔记之四:OpenSSL源代码分析(3) .







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

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

BIO结构:


view plaincopy to clipboardprint?
  1. 01.struct bio_st  
  2. 02.    {  
  3. 03.    BIO_METHOD *method;  
  4. 04.    /* bio, mode, argp, argi, argl, ret */  
  5. 05.    long (*callback)(struct bio_st *,int,const char *,int, long,long);  
  6. 06.    char *cb_arg; /* first argument for the callback */  
  7. 07.  
  8. 08.    int init;  
  9. 09.    int shutdown;  
  10. 10.    int flags;  /* extra storage */  
  11. 11.    int retry_reason;  
  12. 12.    int num;  
  13. 13.    void *ptr;  
  14. 14.    struct bio_st *next_bio;    /* used by filter BIOs */  
  15. 15.    struct bio_st *prev_bio;    /* used by filter BIOs */  
  16. 16.    int references;  
  17. 17.    unsigned long num_read;  
  18. 18.    unsigned long num_write;  
  19. 19.  
  20. 20.    CRYPTO_EX_DATA ex_data;  
  21. 21.    };  
  22. struct bio_st
  23.         {
  24.         BIO_METHOD *method;
  25.         /* bio, mode, argp, argi, argl, ret */
  26.         long (*callback)(struct bio_st *,int,const char *,int, long,long);
  27.         char *cb_arg; /* first argument for the callback */

  28.         int init;
  29.         int shutdown;
  30.         int flags;        /* extra storage */
  31.         int retry_reason;
  32.         int num;
  33.         void *ptr;
  34.         struct bio_st *next_bio;        /* used by filter BIOs */
  35.         struct bio_st *prev_bio;        /* used by filter BIOs */
  36.         int references;
  37.         unsigned long num_read;
  38.         unsigned long num_write;

  39.         CRYPTO_EX_DATA ex_data;
  40.         };
复制代码
BIO操作的结构:


view plaincopy to clipboardprint?
  1. 01.typedef struct bio_method_st  
  2. 02.    {  
  3. 03.    int type;  
  4. 04.    const char *name;  
  5. 05.    int (*bwrite)(BIO *, const char *, int);  
  6. 06.    int (*bread)(BIO *, char *, int);  
  7. 07.    int (*bputs)(BIO *, const char *);  
  8. 08.    int (*bgets)(BIO *, char *, int);  
  9. 09.    long (*ctrl)(BIO *, int, long, void *);  
  10. 10.    int (*create)(BIO *);  
  11. 11.    int (*destroy)(BIO *);  
  12. 12.        long (*callback_ctrl)(BIO *, int, bio_info_cb *);  
  13. 13.    } BIO_METHOD;  
  14. typedef struct bio_method_st
  15.         {
  16.         int type;
  17.         const char *name;
  18.         int (*bwrite)(BIO *, const char *, int);
  19.         int (*bread)(BIO *, char *, int);
  20.         int (*bputs)(BIO *, const char *);
  21.         int (*bgets)(BIO *, char *, int);
  22.         long (*ctrl)(BIO *, int, long, void *);
  23.         int (*create)(BIO *);
  24.         int (*destroy)(BIO *);
  25.         long (*callback_ctrl)(BIO *, int, bio_info_cb *);
  26.         } BIO_METHOD;
复制代码
BIO接口类型分为源/接收类型和过滤类型两种。

view plaincopy to clipboardprint?
  1. 01.#define BIO_TYPE_DESCRIPTOR0x0100 /* socket, fd, connect or accept */   
  2. 02.#define BIO_TYPE_FILTER 0x0200   
  3. 03.#define BIO_TYPE_SOURCE_SINK 0x0400  
  4. #define BIO_TYPE_DESCRIPTOR0x0100 /* socket, fd, connect or accept */
  5. #define BIO_TYPE_FILTER 0x0200
  6. #define BIO_TYPE_SOURCE_SINK 0x0400
复制代码
1、源/接收类型

view plaincopy to clipboardprint?
  1. 01.#define BIO_TYPE_MEM(1|0x0400)   
  2. 02.#define BIO_TYPE_FILE (2|0x0400)   
  3. 03.#define BIO_TYPE_FD (4|0x0400|0x0100)   
  4. 04.#define BIO_TYPE_SOCKET (5|0x0400|0x0100)   
  5. 05.#define BIO_TYPE_NULL (6|0x0400)   
  6. 06.#define BIO_TYPE_CONNECT(12|0x0400|0x0100)/* socket - connect */   
  7. 07.#define BIO_TYPE_ACCEPT(13|0x0400|0x0100)/* socket for accept */   
  8. 08.#define BIO_TYPE_BIO(19|0x0400)/* (half a) BIO pair */   
  9. 09.#define BIO_TYPE_DGRAM(21|0x0400|0x0100)  
  10. #define BIO_TYPE_MEM(1|0x0400)
  11. #define BIO_TYPE_FILE (2|0x0400)
  12. #define BIO_TYPE_FD (4|0x0400|0x0100)
  13. #define BIO_TYPE_SOCKET (5|0x0400|0x0100)
  14. #define BIO_TYPE_NULL (6|0x0400)
  15. #define BIO_TYPE_CONNECT(12|0x0400|0x0100)/* socket - connect */
  16. #define BIO_TYPE_ACCEPT(13|0x0400|0x0100)/* socket for accept */
  17. #define BIO_TYPE_BIO(19|0x0400)/* (half a) BIO pair */
  18. #define BIO_TYPE_DGRAM(21|0x0400|0x0100)
复制代码
2、过滤类型

view plaincopy to clipboardprint?
  1. 01.#define BIO_TYPE_SSL(7|0x0200)   
  2. 02.#define BIO_TYPE_MD(8|0x0200) /* passive filter */   
  3. 03.#define BIO_TYPE_BUFFER (9|0x0200)/* filter */   
  4. 04.#define BIO_TYPE_CIPHER (10|0x0200)/* filter */   
  5. 05.#define BIO_TYPE_BASE64 (11|0x0200)/* filter */   
  6. 06.#define BIO_TYPE_PROXY_CLIENT (14|0x0200)/* client proxy BIO */   
  7. 07.#define BIO_TYPE_PROXY_SERVER (15|0x0200)/* server proxy BIO */   
  8. 08.#define BIO_TYPE_NBIO_TEST (16|0x0200)/* server proxy BIO */   
  9. 09.#define BIO_TYPE_NULL_FILTER (17|0x0200)   
  10. 10.#define BIO_TYPE_BER (18|0x0200)/* BER -> bin filter */   
  11. 11.#define BIO_TYPE_LINEBUFFER (20|0x0200)/* filter */   
  12. 12.#define BIO_TYPE_ASN1 (22|0x0200)/* filter */   
  13. 13.#define BIO_TYPE_COMP (23|0x0200)/* filter */  
  14. #define BIO_TYPE_SSL(7|0x0200)
  15. #define BIO_TYPE_MD(8|0x0200) /* passive filter */
  16. #define BIO_TYPE_BUFFER (9|0x0200)/* filter */
  17. #define BIO_TYPE_CIPHER (10|0x0200)/* filter */
  18. #define BIO_TYPE_BASE64 (11|0x0200)/* filter */
  19. #define BIO_TYPE_PROXY_CLIENT (14|0x0200)/* client proxy BIO */
  20. #define BIO_TYPE_PROXY_SERVER (15|0x0200)/* server proxy BIO */
  21. #define BIO_TYPE_NBIO_TEST (16|0x0200)/* server proxy BIO */
  22. #define BIO_TYPE_NULL_FILTER (17|0x0200)
  23. #define BIO_TYPE_BER (18|0x0200)/* BER -> bin filter */
  24. #define BIO_TYPE_LINEBUFFER (20|0x0200)/* filter */
  25. #define BIO_TYPE_ASN1 (22|0x0200)/* filter */
  26. #define BIO_TYPE_COMP (23|0x0200)/* filter */
复制代码
BIO过滤缓冲结构:


view plaincopy to clipboardprint?
  1. 01.typedef struct bio_f_buffer_ctx_struct  
  2. 02.    {  
  3. 03.    /* BIO *bio; */ /* this is now in the BIO struct */  
  4. 04.    int ibuf_size;  /* how big is the input buffer */  
  5. 05.    int obuf_size;  /* how big is the output buffer */  
  6. 06.  
  7. 07.    char *ibuf;     /* the char array */  
  8. 08.    int ibuf_len;       /* how many bytes are in it */  
  9. 09.    int ibuf_off;       /* write/read offset */  
  10. 10.  
  11. 11.    char *obuf;     /* the char array */  
  12. 12.    int obuf_len;       /* how many bytes are in it */  
  13. 13.    int obuf_off;       /* write/read offset */  
  14. 14.    } BIO_F_BUFFER_CTX;
复制代码

论坛徽章:
0
2 [报告]
发表于 2011-12-28 10:18 |只看该作者
谢谢分享
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP