| 2013 | } |
| 2014 | |
| 2015 | int Server::AddCertificate(const CertInfo& cert) { |
| 2016 | if (!_options.has_ssl_options()) { |
| 2017 | LOG(ERROR) << "ServerOptions.ssl_options is not configured yet"; |
| 2018 | return -1; |
| 2019 | } |
| 2020 | std::string cert_key(cert.certificate); |
| 2021 | cert_key.append(cert.private_key); |
| 2022 | if (_ssl_ctx_map.seek(cert_key) != NULL) { |
| 2023 | LOG(WARNING) << cert << " already exists"; |
| 2024 | return 0; |
| 2025 | } |
| 2026 | |
| 2027 | SSLContext ssl_ctx; |
| 2028 | ssl_ctx.filters = cert.sni_filters; |
| 2029 | ssl_ctx.ctx = std::make_shared<SocketSSLContext>(); |
| 2030 | SSL_CTX* raw_ctx = CreateServerSSLContext( |
| 2031 | cert.certificate, cert.private_key, |
| 2032 | _options.ssl_options(), &_raw_alpns, &ssl_ctx.filters); |
| 2033 | if (raw_ctx == NULL) { |
| 2034 | return -1; |
| 2035 | } |
| 2036 | ssl_ctx.ctx->raw_ctx = raw_ctx; |
| 2037 | |
| 2038 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 2039 | SSL_CTX_set_tlsext_servername_callback(ssl_ctx.ctx->raw_ctx, SSLSwitchCTXByHostname); |
| 2040 | SSL_CTX_set_tlsext_servername_arg(ssl_ctx.ctx->raw_ctx, this); |
| 2041 | #endif |
| 2042 | |
| 2043 | if (!_reload_cert_maps.Modify(AddCertMapping, ssl_ctx)) { |
| 2044 | LOG(ERROR) << "Fail to add mappings into _reload_cert_maps"; |
| 2045 | return -1; |
| 2046 | } |
| 2047 | _ssl_ctx_map[cert_key] = ssl_ctx; |
| 2048 | return 0; |
| 2049 | } |
| 2050 | |
| 2051 | bool Server::AddCertMapping(CertMaps& bg, const SSLContext& ssl_ctx) { |
| 2052 | for (size_t i = 0; i < ssl_ctx.filters.size(); ++i) { |