| 1022 | } |
| 1023 | |
| 1024 | bool openssl_conf::add_cert(const char* crt_file, const char* key_file, |
| 1025 | const char* key_pass /* NULL */) |
| 1026 | { |
| 1027 | if (status_ != CONF_INIT_OK) { |
| 1028 | logger_error("OpenSSL not init , status=%d", (int) status_); |
| 1029 | return false; |
| 1030 | } |
| 1031 | |
| 1032 | if (crt_file == NULL || *crt_file == 0) { |
| 1033 | logger_error("crt_file can't be null nor empty"); |
| 1034 | return false; |
| 1035 | } |
| 1036 | |
| 1037 | if (key_file == NULL || *key_file == 0) { |
| 1038 | logger_error("key_file can't be null nor empty"); |
| 1039 | return false; |
| 1040 | } |
| 1041 | |
| 1042 | #ifdef HAS_OPENSSL |
| 1043 | SSL_CTX* ctx; |
| 1044 | if (server_side_ && !ssl_ctx_) { |
| 1045 | ctx = create_ssl_ctx(); |
| 1046 | } else if (ssl_ctx_) { |
| 1047 | ctx = ssl_ctx_; |
| 1048 | } else { |
| 1049 | ctx = ssl_ctx_ = create_ssl_ctx(); |
| 1050 | } |
| 1051 | #if 0 |
| 1052 | if (__ssl_ctx_use_cert_chain(ctx, crt_file) != 1) { |
| 1053 | logger_error("use crt chain file(%s) error", crt_file); |
| 1054 | return false; |
| 1055 | } |
| 1056 | #else |
| 1057 | if (__ssl_ctx_use_cert(ctx, crt_file, SSL_FILETYPE_PEM) != 1) { |
| 1058 | logger_error("use crt file(%s) error", crt_file); |
| 1059 | return false; |
| 1060 | } |
| 1061 | #endif |
| 1062 | |
| 1063 | if (__ssl_ctx_use_pkey(ctx, key_file, SSL_FILETYPE_PEM) != 1) { |
| 1064 | logger_error("load private key(%s) error", key_file); |
| 1065 | return false; |
| 1066 | } |
| 1067 | |
| 1068 | if (!__ssl_ctx_check_pkey(ctx)) { |
| 1069 | logger_error("check private key(%s) error", key_file); |
| 1070 | return false; |
| 1071 | } |
| 1072 | |
| 1073 | if (key_pass && *key_pass) { |
| 1074 | __ssl_ctx_set_def_pass(ctx, (void*) key_pass); |
| 1075 | } |
| 1076 | |
| 1077 | map_ssl_ctx(ctx); |
| 1078 | return true; |
| 1079 | #else |
| 1080 | (void) key_pass; |
| 1081 | logger_error("HAS_OPENSSL not defined!"); |
no outgoing calls
no test coverage detected