| 273 | } |
| 274 | |
| 275 | SSL_CTX * |
| 276 | SSLCreateClientContext(const struct SSLConfigParams *params, const char *ca_bundle_path, const char *ca_bundle_file, |
| 277 | const char *cert_path, const char *key_path) |
| 278 | { |
| 279 | std::unique_ptr<SSL_CTX, decltype(&SSL_CTX_free)> ctx(nullptr, &SSL_CTX_free); |
| 280 | |
| 281 | if (nullptr == params || nullptr == cert_path) { |
| 282 | return nullptr; |
| 283 | } |
| 284 | |
| 285 | ctx.reset(SSLInitClientContext(params)); |
| 286 | |
| 287 | if (!ctx) { |
| 288 | return nullptr; |
| 289 | } |
| 290 | |
| 291 | if (!SSL_CTX_use_certificate_chain_file(ctx.get(), cert_path)) { |
| 292 | SSLError("SSLCreateClientContext(): failed to load client certificate: %s", |
| 293 | (!cert_path || cert_path[0] == '\0') ? "[empty file name]" : cert_path); |
| 294 | return nullptr; |
| 295 | } |
| 296 | |
| 297 | if (!key_path || key_path[0] == '\0') { |
| 298 | key_path = cert_path; |
| 299 | } |
| 300 | |
| 301 | if (!SSL_CTX_use_PrivateKey_file(ctx.get(), key_path, SSL_FILETYPE_PEM)) { |
| 302 | SSLError("SSLCreateClientContext(): failed to load client private key: %s", |
| 303 | (!key_path || key_path[0] == '\0') ? "[empty file]" : key_path); |
| 304 | return nullptr; |
| 305 | } |
| 306 | |
| 307 | if (!SSL_CTX_check_private_key(ctx.get())) { |
| 308 | SSLError("SSLCreateClientContext(): client private key: %s does not match client certificate: %s", |
| 309 | (!key_path || key_path[0] == '\0') ? "[empty file]" : key_path, |
| 310 | (!cert_path || cert_path[0] == '\0') ? "[empty file]" : cert_path); |
| 311 | return nullptr; |
| 312 | } |
| 313 | |
| 314 | if (ca_bundle_file || ca_bundle_path) { |
| 315 | if (!SSL_CTX_load_verify_locations(ctx.get(), ca_bundle_file, ca_bundle_path)) { |
| 316 | SSLError("SSLCreateClientContext(): Invalid CA Certificate file: %s or CA Certificate path: %s", |
| 317 | (!ca_bundle_file || ca_bundle_file[0] == '\0') ? "[empty file name]" : ca_bundle_file, |
| 318 | (!ca_bundle_path || ca_bundle_path[0] == '\0') ? "[empty path]" : ca_bundle_path); |
| 319 | SSLError("SSLCreateClientContext(): Invalid client CA cert file/CA path."); |
| 320 | return nullptr; |
| 321 | } |
| 322 | } else if (!SSL_CTX_set_default_verify_paths(ctx.get())) { |
| 323 | SSLError("SSLCreateClientContext(): failed to set the default verify paths."); |
| 324 | return nullptr; |
| 325 | } |
| 326 | return ctx.release(); |
| 327 | } |
no test coverage detected