OpenSSL中文手册之ERR库详解

本文详细介绍了OpenSSL中ERR库的功能,包括错误代码的结构、数据结构ERR_STRING_DATA和ERR_STATE,以及如何获取和处理错误信息。内容涵盖错误代码的计算、错误堆栈、错误信息的加载和清除,强调了错误处理在程序设计中的重要性。
摘要由CSDN通过智能技术生成

  版权声明本文根据DragonKing牛,E-Mail:wzhah@263.NET发布在https://openssl.126.com的系列文章整理修改而成(这个网站已经不能访问了),我自己所做的工作主要是针对新的1.0.2版本进行验证,修改错别字,和错误,重新排版,以及整理分类,配图。 未经作者允许,严禁用于商业出版,否则追究法律责任。网络转载请注明出处,这是对原创者的起码的尊重!!!


1 概述

  程序设计时,一般通过函数的返回值来判断是否调用成功。设计良好的函数以及好的错误处理能帮助调用者快速找到错误原因。错误处理应该尽可能多的包含各种信息,包括:

  • 错误码;
  • 出错文件以及行号;
  • 错误原因;
  • 出错函数;
  • 出错库;
  • 出错模块与类别信息;
  • 错误堆栈信息等。
  • 出错信息最好能支持多种输出。可以是输出在标准输出上,也可以是文件等形式。

2 相关数据结构

2.1 错误代码

  openssl中,通过unsigned long类型来存放错误信息。它包含三部分内容:库代码、函数代码以及错误原因代码。其中,库代码在openssl/err.h中定义,比如err.h中为库定义如下库代码:

        /* library */
        # define ERR_LIB_NONE            1
        # define ERR_LIB_SYS             2
        # define ERR_LIB_BN              3
        # define ERR_LIB_RSA             4
        # define ERR_LIB_DH              5
        # define ERR_LIB_EVP             6
        # define ERR_LIB_BUF             7
        # define ERR_LIB_OBJ             8
        # define ERR_LIB_PEM             9
        # define ERR_LIB_DSA             10
        # define ERR_LIB_X509            11
        /* #define ERR_LIB_METH         12 */
        # define ERR_LIB_ASN1            13
        # define ERR_LIB_CONF            14
        # define ERR_LIB_CRYPTO          15
        # define ERR_LIB_EC              16
        # define ERR_LIB_SSL             20
        /* #define ERR_LIB_SSL23        21 */
        /* #define ERR_LIB_SSL2         22 */
        /* #define ERR_LIB_SSL3         23 */
        /* #define ERR_LIB_RSAREF       30 */
        /* #define ERR_LIB_PROXY        31 */
        # define ERR_LIB_BIO             32
        # define ERR_LIB_PKCS7           33
        # define ERR_LIB_X509V3          34
        # define ERR_LIB_PKCS12          35
        # define ERR_LIB_RAND            36
        # define ERR_LIB_DSO             37
        # define ERR_LIB_ENGINE          38
        # define ERR_LIB_OCSP            39
        # define ERR_LIB_UI              40
        # define ERR_LIB_COMP            41
        # define ERR_LIB_ECDSA           42
        # define ERR_LIB_ECDH            43
        # define ERR_LIB_STORE           44
        # define ERR_LIB_FIPS            45
        # define ERR_LIB_CMS             46
        # define ERR_LIB_TS              47
        # define ERR_LIB_HMAC            48
        /* # define ERR_LIB_JPAKE       49 */
        # define ERR_LIB_CT              50
        # define ERR_LIB_ASYNC           51
        # define ERR_LIB_KDF             52

        # define ERR_LIB_USER            128

  函数代码以及错误原因代码由各个功能模块定义(同类代码不能与其他的重复,也不能超过一定的大小)。如:openssl/bio.h中定义了如下函数和错误原因代号:

          /* Error codes for the BIO functions. */

          /* Function codes. */
          # define BIO_F_ACPT_STATE                                 100
          # define BIO_F_ADDR_STRINGS                               134
          # define BIO_F_BIO_ACCEPT                                 101
          # define BIO_F_BIO_ACCEPT_EX                              137
          # define BIO_F_BIO_ADDR_NEW                               144
          # define BIO_F_BIO_CALLBACK_CTRL                          131
          # define BIO_F_BIO_CONNECT                                138
          # define BIO_F_BIO_CTRL                                   103
          # define BIO_F_BIO_GETS                                   104
          # define BIO_F_BIO_GET_HOST_IP                            106
          # define BIO_F_BIO_GET_NEW_INDEX                          102
          # define BIO_F_BIO_GET_PORT                               107
          # define BIO_F_BIO_LISTEN                                 139
          # define BIO_F_BIO_LOOKUP                                 135
          # define BIO_F_BIO_MAKE_PAIR                              121
          # define BIO_F_BIO_NEW                                    108
          # define BIO_F_BIO_NEW_FILE                               109
          # define BIO_F_BIO_NEW_MEM_BUF                            126
          # define BIO_F_BIO_NREAD                                  123
          # define BIO_F_BIO_NREAD0                                 124
          # define BIO_F_BIO_NWRITE          
  • 3
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蓝月心语

你的鼓励是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值