Explore Enterprise Education Gitee Premium Gitee AI AI teammates
Fetch the repository succeeded.
Open Source > Development Lib > Common Toolkit &&
Create your Gitee Account
Explore and code with more than 14 million developers,Free private repositories !:)
Sign up
Already have an account? Sign in
文件
master
Branches (1)
Tags (2)
master
ZCC0.01
some-old-version
master
Branches (1)
Tags (2)
master
ZCC0.01
some-old-version
Clone or Download
Clone/Download
Prompt
To download the code, please copy the following command and execute it in the terminal
To ensure that your submitted code identity is correctly recognized by Gitee, please execute the following command.
When using the SSH protocol for the first time to clone or push code, follow the prompts below to complete the SSH configuration.
1 Generate RSA keys.
2 Obtain the content of the RSA public key and configure it in SSH Public Keys
To use SVN on Gitee, please visit the usage guide
When using the HTTPS protocol, the command line will prompt for account and password verification as follows. For security reasons, Gitee recommends configure and use personal access tokens instead of login passwords for cloning, pushing, and other operations.
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # Private Token
master
Branches (1)
Tags (2)
master
ZCC0.01
some-old-version
lib-zc
/
cpp_src
/
stdlib
/
openssl.cpp
lib-zc
/
cpp_src
/
stdlib
/
openssl.cpp
openssl.cpp 13.31 KB
Copy Edit Raw Blame History
linuxmail authored 2026年03月04日 20:11 +08:00 . 语法检查
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451
/*
* ================================
* eli960@qq.com
* http:/linuxmail.cn/
* 2015年10月18日
* ================================
*/
#include "zcc/zcc_stdlib.h"
#include "zcc/zcc_openssl.h"
#include <thread>
#include <mutex>
#include <openssl/ssl.h>
#include <openssl/err.h>
#include <openssl/conf.h>
#ifndef CRYPTO_num_locks
extern "C"
{
struct CRYPTO_dynlock_value
{
std::mutex mutex;
};
}
#endif // CRYPTO_num_locks
zcc_namespace_begin;
zcc_general_namespace_begin(openssl);
bool var_debug_mode = false;
bool var_disable_server_tls1_0 = 0;
bool var_disable_client_tls1_0 = 0;
#ifdef CRYPTO_num_locks
#define thread_safe_setup()
#define thread_safe_cleanup()
#else // CRYPTO_num_locks
static std::mutex **thread_safe_lock_vec = 0;
static void thread_safe_locking_fn(int mode, int n, const char *file, int line)
{
if (mode & CRYPTO_LOCK)
{
thread_safe_lock_vec[n]->lock();
}
else
{
thread_safe_lock_vec[n]->unlock();
}
}
static unsigned long thread_safe_id_fn(void)
{
return get_thread_id();
}
static struct CRYPTO_dynlock_value *thread_safe_dynlock_create_fn(const char *file, int line)
{
struct CRYPTO_dynlock_value *v = new CRYPTO_dynlock_value();
return v;
}
static void thread_safe_dynlock_lock_fn(int mode, struct CRYPTO_dynlock_value *value, const char *file, int line)
{
if (mode & CRYPTO_LOCK)
{
value->mutex.lock();
}
else
{
value->mutex.unlock();
}
}
static void thread_safe_dynlock_destroy_fn(struct CRYPTO_dynlock_value *value, const char *file, int line)
{
if (value)
{
delete value;
}
}
static void thread_safe_setup(void)
{
thread_safe_lock_vec = new std::mutex *[CRYPTO_num_locks()];
for (int i = 0; i < CRYPTO_num_locks(); i++)
{
thread_safe_lock_vec[i] = new std::mutex();
}
CRYPTO_set_id_callback(thread_safe_id_fn);
CRYPTO_set_locking_callback(thread_safe_locking_fn);
CRYPTO_set_dynlock_create_callback(thread_safe_dynlock_create_fn);
CRYPTO_set_dynlock_lock_callback(thread_safe_dynlock_lock_fn);
CRYPTO_set_dynlock_destroy_callback(thread_safe_dynlock_destroy_fn);
}
static void thread_safe_cleanup(void)
{
if (!thread_safe_lock_vec)
{
return;
}
CRYPTO_set_id_callback(0);
CRYPTO_set_locking_callback(0);
CRYPTO_set_dynlock_create_callback(0);
CRYPTO_set_dynlock_lock_callback(0);
CRYPTO_set_dynlock_destroy_callback(0);
for (int i = 0; i < CRYPTO_num_locks(); i++)
{
delete thread_safe_lock_vec[i];
}
delete thread_safe_lock_vec;
thread_safe_lock_vec = 0;
}
#endif // CRYPTO_num_locks
// 应该在程序启动时初始化, 应该在主进程执行
static bool ___openssl_init = false;
void env_init(void)
{
if (___openssl_init)
{
return;
}
___openssl_init = true;
#if (OPENSSL_VERSION_NUMBER >= 0x10100003L)
OPENSSL_init_ssl(OPENSSL_INIT_LOAD_CONFIG, NULL);
thread_safe_setup();
SSL_library_init();
OpenSSL_add_all_ciphers();
OpenSSL_add_all_algorithms();
OpenSSL_add_all_digests();
ERR_load_crypto_strings();
SSL_load_error_strings();
#else
OPENSSL_config(NULL);
SSL_library_init();
SSL_load_error_strings();
OpenSSL_add_all_algorithms();
#endif
}
void env_fini(void)
{
if (!___openssl_init)
{
return;
}
#if 1
___openssl_init = false;
thread_safe_cleanup();
return;
#else
#if 0
CONF_modules_unload(1)
ENGINE_cleanup();
#endif
ERR_free_strings();
EVP_cleanup();
CRYPTO_cleanup_all_ex_data();
#if 0
/* Deprecated */
ERR_remove_state(0);
#endif
sk_SSL_COMP_free(SSL_COMP_get_compression_methods());
thread_safe_cleanup();
___openssl_init = 0;
#endif
}
SSL_CTX *SSL_CTX_create_client(void)
{
#if (OPENSSL_VERSION_NUMBER >= 0x10100000L)
const SSL_METHOD *method = TLS_client_method();
#else
const SSL_METHOD *method = SSLv23_client_method();
#endif
SSL_CTX *ctx = SSL_CTX_new(method);
if (!ctx)
{
zcc_error("SSL_CTX_new");
return nullptr;
}
SSL_CTX_set_options(ctx, SSL_OP_ALL);
#ifdef SSL_CTX_set_min_proto_version
if (var_disable_client_tls1_0)
{
SSL_CTX_set_min_proto_version(ctx, TLS1_1_VERSION);
}
else
{
SSL_CTX_set_min_proto_version(ctx, TLS1_VERSION);
}
SSL_CTX_set_max_proto_version(ctx, TLS_MAX_VERSION);
#endif
#if OPENSSL_VERSION_NUMBER > 0x10100003L
SSL_CTX_set_security_level(ctx, 1);
#endif
return ctx;
}
SSL_CTX *SSL_CTX_create_server(const char *cert_file, const char *key_file)
{
#if (OPENSSL_VERSION_NUMBER >= 0x10100000L)
const SSL_METHOD *method = TLS_server_method();
#else
const SSL_METHOD *method = SSLv23_server_method();
#endif
SSL_CTX *ctx = SSL_CTX_new(method);
if (!ctx)
{
zcc_error("SSL_CTX_new");
return nullptr;
}
SSL_CTX_set_options(ctx, SSL_OP_ALL);
#ifdef SSL_CTX_set_min_proto_version
if (var_disable_server_tls1_0)
{
SSL_CTX_set_min_proto_version(ctx, TLS1_1_VERSION);
}
else
{
SSL_CTX_set_min_proto_version(ctx, TLS1_VERSION);
}
SSL_CTX_set_max_proto_version(ctx, TLS_MAX_VERSION);
#endif
ERR_clear_error();
if ((!cert_file) || (!key_file))
{
zcc_error("cert_file or key_file is null");
goto err;
}
if (SSL_CTX_load_verify_locations(ctx, cert_file, NULL) != 1)
{
zcc_error("SSL_CTX_load_verify_locations");
goto err;
}
if ((SSL_CTX_use_certificate_chain_file(ctx, cert_file) != 1) && (SSL_CTX_use_certificate_file(ctx, cert_file, SSL_FILETYPE_PEM) != 1))
{
zcc_error("SSL_CTX_use_certificate_chain_file AND SSL_CTX_use_certificate_file");
goto err;
}
if (SSL_CTX_use_PrivateKey_file(ctx, key_file, SSL_FILETYPE_PEM) != 1)
{
zcc_error("SSL_CTX_use_PrivateKey_file");
goto err;
}
if (SSL_CTX_check_private_key(ctx) != 1)
{
zcc_error("SSL_CTX_check_private_key");
goto err;
}
return ctx;
err:
::SSL_CTX_free(ctx);
return 0;
}
typedef SSL_CTX *(*get_ssl_ctx_by_server_name_fn_t)(const char *servername);
static int _change_ssl_ctx_by_servername(SSL *ssl, int *ad, void *arg)
{
const char *servername = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name);
if (empty(servername))
{
return SSL_TLSEXT_ERR_NOACK;
}
get_ssl_ctx_by_server_name_fn_t get_ssl_ctx_by_server_name = (get_ssl_ctx_by_server_name_fn_t)arg;
if (!get_ssl_ctx_by_server_name)
{
return SSL_TLSEXT_ERR_NOACK;
}
SSL_CTX *my_ctx = SSL_get_SSL_CTX(ssl);
if (!my_ctx)
{
return SSL_TLSEXT_ERR_NOACK;
}
SSL_CTX *another_ctx = get_ssl_ctx_by_server_name(servername);
if (!another_ctx)
{
return SSL_TLSEXT_ERR_NOACK;
}
if (another_ctx != my_ctx)
{
SSL_set_SSL_CTX(ssl, another_ctx);
}
return SSL_TLSEXT_ERR_OK;
}
void SSL_CTX_set_sni_handler(SSL_CTX *ctx, SSL_CTX *(*get_ssl_ctx_by_server_name)(const char *servername))
{
do
{
if (SSL_CTX_set_tlsext_servername_callback(ctx, _change_ssl_ctx_by_servername) == 0)
{
zcc_warning("SSL_CTX_set_tlsext_servername_callback failure");
break;
}
if (SSL_CTX_set_tlsext_servername_arg(ctx, (void *)get_ssl_ctx_by_server_name) == 0)
{
zcc_warning("SSL_CTX_set_tlsext_servername_arg failure");
break;
}
} while (0);
}
void SSL_CTX_free(SSL_CTX *ctx)
{
::SSL_CTX_free(ctx);
}
void get_error(unsigned long *ecode, char *buf, int buf_len)
{
unsigned long ec;
ec = ERR_get_error();
if (ecode)
{
*ecode = ec;
}
if (buf)
{
ERR_error_string_n(ec, buf, buf_len);
}
}
SSL *SSL_create(SSL_CTX *ctx, int fd)
{
SSL *ssl;
ssl = SSL_new(ctx);
SSL_set_fd(ssl, fd);
return ssl;
}
void SSL_free(SSL *ssl)
{
if (ssl)
{
SSL_shutdown(ssl);
::SSL_free(ssl);
}
}
int SSL_get_fd(SSL *ssl)
{
return ::SSL_get_fd(ssl);
}
#define ___Z_SSL_TIMED_DO(excute_sentence, need_check_read_close) \
if (wait_timeout == 0) \
{ \
return excute_sentence; \
} \
int _fd = SSL_get_fd(ssl); \
int ret, err; \
for (;;) \
{ \
ret = excute_sentence; \
if (ret > 0) \
{ \
break; \
} \
err = SSL_get_error(ssl, ret); \
if (err == SSL_ERROR_WANT_WRITE) \
{ \
if (timed_write_wait(_fd, wait_timeout) == 0) \
{ \
ret = -1; \
break; \
} \
} \
else if (err == SSL_ERROR_WANT_READ) \
{ \
if (timed_read_wait(_fd, wait_timeout) == 0) \
{ \
ret = -1; \
break; \
} \
} \
else \
{ \
/* FIXME 这里有问题 */ \
if (need_check_read_close && (ret == 0) /* && (err == SSL_ERROR_ZERO_RETURN) */) \
{ \
{ \
ret = 0; \
break; \
} \
} \
if (var_debug_mode) \
{ \
zcc_debug_output("openssl: found error ret=%d, status=%d", ret, err); \
} \
{ \
ret = -1; \
break; \
} \
} \
}
int timed_connect(SSL *ssl, int wait_timeout)
{
___Z_SSL_TIMED_DO(::SSL_connect(ssl), 0);
return (ret == 1 ? 1 : -1);
}
int timed_accept(SSL *ssl, int wait_timeout)
{
___Z_SSL_TIMED_DO(::SSL_accept(ssl), 0);
return (ret == 1 ? 1 : -1);
}
int timed_shutdown(SSL *ssl, int wait_timeout)
{
___Z_SSL_TIMED_DO(::SSL_shutdown(ssl), 0);
return (ret == 1 ? 1 : -1);
}
int timed_read(SSL *ssl, void *buf, int len, int wait_timeout)
{
___Z_SSL_TIMED_DO(::SSL_read(ssl, buf, len), 1);
return ret;
}
int timed_write(SSL *ssl, const void *buf, int len, int wait_timeout)
{
___Z_SSL_TIMED_DO(::SSL_write(ssl, buf, len), 0);
return ret;
}
zcc_general_namespace_end(openssl);
zcc_namespace_end;
Loading...
Report
Report success
We will send you the feedback within 2 working days through the letter!
Please fill in the reason for the report carefully. Provide as detailed a description as possible.
Please select a report type
Cancel
Send
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

About

LIB-ZC 是一个Linux平台通用C/C++扩展库。兼容 Windows/Macos。 支持 GCC/CLANG/MINGW/MSVC。 支持 X64/AMD64/ARM64。 基本的数据结构封装,master/server服务框架,异步IO,协程,JSON解析,邮件解析,字符集探测,redis、memcache客户端,imap、pop客户端,httpd,websocket,sqlite3
Cancel

Releases

No release

The Open Source Evaluation Index is derived from the OSS Compass evaluation system, which evaluates projects around the following three dimensions

1. Open source ecosystem

  • Productivity: To evaluate the ability of open-source projects to output software artifacts and open-source value.
  • Innovation: Used to evaluate the degree of diversity of open source software and its ecosystem.
  • Robustness: Used to evaluate the ability of open-source projects to resist internal and external interference and self recover in the face of changing development environments.

2. Collaboration, People, Software

  • Collaboration: represents the degree and depth of collaboration in open source development behavior.
  • Observe the influence of core personnel in open source projects, and examine the evaluations of users and developers on open source projects from a third-party perspective.
  • Software: Evaluate the value of products exported from open-source projects and their ultimate destination. It is also a concrete manifestation of "open source software", one of the oldest mainstream directions in open source evaluation.

3. Evaluation model

    Based on the dimensions of "open source ecosystem" and "collaboration, people, and software", identify quantifiable indicators directly or indirectly related to this goal, quantitatively evaluate the health and ecology of open source projects, and ultimately form an open source evaluation index.

Contributors

All

Language(Optional)

Activities

can not load any more
Edit
About
Homepage
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C
1
https://gitee.com/linuxmail/lib-zc.git
git@gitee.com:linuxmail/lib-zc.git
linuxmail
lib-zc
lib-zc
master
Going to Help Center

Search

Comment
Repository Report
Back to the top
Login prompt
This operation requires login to the code cloud account. Please log in before operating.
Go to login
No account. Register

AltStyle によって変換されたページ (->オリジナル) /