Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

user: imporve #463, impact on the performance of the tested program #471

Merged
merged 4 commits into from
Jan 28, 2024

Conversation

cfc4n
Copy link
Member

@cfc4n cfc4n commented Jan 27, 2024

设计思路

问题

  • 为了读取到TLS握手完成后的client_random等密钥,必需要选择一个合适的HOOK函数。
  • SSL_write\SSL_read时,TLS握手是建立完成的,但调用过于频繁,会带来性能问题,参见 TLS 模式下,对被检测程序的性能影响。 #463
  • 综合来看,合适的HOOK函数需要满足以下几个条件:
    1. 函数是在TLS握手完成后调用
    2. 函数名在动态链接库的符号表中是导出状态
    3. 函数是低频调用

解决思路

  • 在 openssl 类库中,以客户端角色调用 SSL_connect 或者以服务端角色 SSL_accept ,最终都会进入 ssl/statem/statem.cstate_machine 函数进行TLS握手。
  • 所以,可选范围是在这个函数内以大写SSL开头的函数。
  • 当使用openssl的方式为同步调用时,TLS握手成功会返回1,也就是ret = 1,即需要在这个变量赋值后,被调用的函数,才能拿到符合要求的内存数据。 state_machine函数内符合要求的就只有SSL_get_wbio了。
  • 当使用openssl的方式为异步 (BIO)调用时,还需要增加SSL_in_before函数。

Design concept

Question

  • In order to read the client_random and other keys after TLS handshake is completed, it is necessary to select a suitable HOOK function.
  • When SSL_write\SSL_read, the TLS handshake is already established, but calling it too frequently will cause performance issues, see TLS 模式下,对被检测程序的性能影响。 #463 for reference.
  • Overall, a suitable HOOK function needs to meet the following conditions:
    1. The function is called after TLS handshake completion
    2. The function name is exported in the symbol table of the dynamic link library
    3. The function has low-frequency calls

Solution

  • When calling SSL_connect in the OpenSSL library in a client role or SSL_accept in a server role, the execution flow ultimately enters the state_machine function in ssl/statem/statem.c for TLS handshake.
  • Therefore, the optional scope is functions within this state_machine function that start with an uppercase SSL.
  • When using OpenSSL synchronously, a successful TLS handshake returns 1, i.e., ret = 1. Thus, after this variable is assigned, the called functions can obtain the desired memory data. the only function within the state_machine function that meets the requirements is SSL_get_wbio.
  • When using openssl in an asynchronous (BIO) manner, it is also necessary to add the SSL_in_before function.

* When calling `SSL_connect` in the OpenSSL library in a client role or `SSL_accept` in a server role, the execution flow ultimately enters the `state_machine` function in `ssl/statem/statem.c` for TLS handshake.
* Therefore, the optional scope is functions within this `state_machine` function that start with an uppercase `SSL`.
* When using OpenSSL synchronously, a successful TLS handshake returns 1, i.e., `ret = 1`. Thus, after this variable is assigned, the called functions can obtain the desired memory data.
* Under this premise, the only function within the `state_machine` function that meets the requirements is `SSL_get_wbio`.
* Adding an alternate HOOK function, `SSL_in_before`, to the scope.

Signed-off-by: CFC4N <cfc4n.cs@gmail.com>
@cfc4n cfc4n added enhancement New feature or request improve dependencies Pull requests that update a dependency file labels Jan 27, 2024
@cfc4n cfc4n self-assigned this Jan 27, 2024
see readme.md for more detail.

Signed-off-by: CFC4N <cfc4n.cs@gmail.com>
@cfc4n
Copy link
Member Author

cfc4n commented Jan 27, 2024

测试结果 Result

cc_snip_2024-01-27_21-44-07

Signed-off-by: CFC4N <cfc4n.cs@gmail.com>
Signed-off-by: CFC4N <cfc4n.cs@gmail.com>
Copy link
Member Author

@cfc4n cfc4n left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reviewed.

@cfc4n cfc4n merged commit 5348f60 into master Jan 28, 2024
6 checks passed
@cfc4n cfc4n deleted the improve_sslkey_hook_func branch January 28, 2024 04:50
@yifannir
Copy link

最终是选择换了hook点的函数么?可否再测试一种场景,本地大文件wget下载场景,在修改之前,对性能影响会有约20%的下载速度下降,本地nginx,约700m->500m

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dependencies Pull requests that update a dependency file enhancement New feature or request improve
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants