1 #pragma once 2 #include "bmcweb_config.h" 3 4 #include "authorization.hpp" 5 #include "http_response.hpp" 6 #include "http_utility.hpp" 7 #include "logging.hpp" 8 #include "utility.hpp" 9 10 #include <boost/algorithm/string.hpp> 11 #include <boost/algorithm/string/predicate.hpp> 12 #include <boost/asio/io_context.hpp> 13 #include <boost/asio/ip/tcp.hpp> 14 #include <boost/asio/ssl/stream.hpp> 15 #include <boost/asio/steady_timer.hpp> 16 #include <boost/beast/core/flat_static_buffer.hpp> 17 #include <boost/beast/ssl/ssl_stream.hpp> 18 #include <boost/beast/websocket.hpp> 19 #include <boost/url/url_view.hpp> 20 #include <json_html_serializer.hpp> 21 #include <security_headers.hpp> 22 #include <ssl_key_handler.hpp> 23 24 #include <atomic> 25 #include <chrono> 26 #include <vector> 27 28 namespace crow 29 { 30 31 inline void prettyPrintJson(crow::Response& res) 32 { 33 json_html_util::dumpHtml(res.body(), res.jsonValue); 34 35 res.addHeader("Content-Type", "text/html;charset=UTF-8"); 36 } 37 38 static int connectionCount = 0; 39 40 // request body limit size set by the bmcwebHttpReqBodyLimitMb option 41 constexpr uint64_t httpReqBodyLimit = 42 1024UL * 1024UL * bmcwebHttpReqBodyLimitMb; 43 44 constexpr uint64_t loggedOutPostBodyLimit = 4096; 45 46 constexpr uint32_t httpHeaderLimit = 8192; 47 48 template <typename Adaptor, typename Handler> 49 class Connection : 50 public std::enable_shared_from_this<Connection<Adaptor, Handler>> 51 { 52 public: 53 Connection(Handler* handlerIn, boost::asio::steady_timer&& timerIn, 54 std::function<std::string()>& getCachedDateStrF, 55 Adaptor adaptorIn) : 56 adaptor(std::move(adaptorIn)), 57 handler(handlerIn), timer(std::move(timerIn)), 58 getCachedDateStr(getCachedDateStrF) 59 { 60 parser.emplace(std::piecewise_construct, std::make_tuple()); 61 parser->body_limit(httpReqBodyLimit); 62 parser->header_limit(httpHeaderLimit); 63 64 #ifdef BMCWEB_ENABLE_MUTUAL_TLS_AUTHENTICATION 65 prepareMutualTls(); 66 #endif // BMCWEB_ENABLE_MUTUAL_TLS_AUTHENTICATION 67 68 connectionCount++; 69 70 BMCWEB_LOG_DEBUG << this << " Connection open, total " 71 << connectionCount; 72 } 73 74 ~Connection() 75 { 76 res.setCompleteRequestHandler(nullptr); 77 cancelDeadlineTimer(); 78 79 connectionCount--; 80 BMCWEB_LOG_DEBUG << this << " Connection closed, total " 81 << connectionCount; 82 } 83 84 Connection(const Connection&) = delete; 85 Connection(Connection&&) = delete; 86 Connection& operator=(const Connection&) = delete; 87 Connection& operator=(Connection&&) = delete; 88 89 void prepareMutualTls() 90 { 91 std::error_code error; 92 std::filesystem::path caPath(ensuressl::trustStorePath); 93 auto caAvailable = !std::filesystem::is_empty(caPath, error); 94 caAvailable = caAvailable && !error; 95 if (caAvailable && persistent_data::SessionStore::getInstance() 96 .getAuthMethodsConfig() 97 .tls) 98 { 99 adaptor.set_verify_mode(boost::asio::ssl::verify_peer); 100 std::string id = "bmcweb"; 101 102 const char* cStr = id.c_str(); 103 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 104 const auto* idC = reinterpret_cast<const unsigned char*>(cStr); 105 int ret = SSL_set_session_id_context( 106 adaptor.native_handle(), idC, 107 static_cast<unsigned int>(id.length())); 108 if (ret == 0) 109 { 110 BMCWEB_LOG_ERROR << this << " failed to set SSL id"; 111 } 112 } 113 114 adaptor.set_verify_callback([this]( 115 bool preverified, 116 boost::asio::ssl::verify_context& ctx) { 117 // do nothing if TLS is disabled 118 if (!persistent_data::SessionStore::getInstance() 119 .getAuthMethodsConfig() 120 .tls) 121 { 122 BMCWEB_LOG_DEBUG << this << " TLS auth_config is disabled"; 123 return true; 124 } 125 126 // We always return true to allow full auth flow 127 if (!preverified) 128 { 129 BMCWEB_LOG_DEBUG << this << " TLS preverification failed."; 130 return true; 131 } 132 133 X509_STORE_CTX* cts = ctx.native_handle(); 134 if (cts == nullptr) 135 { 136 BMCWEB_LOG_DEBUG << this << " Cannot get native TLS handle."; 137 return true; 138 } 139 140 // Get certificate 141 X509* peerCert = 142 X509_STORE_CTX_get_current_cert(ctx.native_handle()); 143 if (peerCert == nullptr) 144 { 145 BMCWEB_LOG_DEBUG << this 146 << " Cannot get current TLS certificate."; 147 return true; 148 } 149 150 // Check if certificate is OK 151 int error = X509_STORE_CTX_get_error(cts); 152 if (error != X509_V_OK) 153 { 154 BMCWEB_LOG_INFO << this << " Last TLS error is: " << error; 155 return true; 156 } 157 // Check that we have reached final certificate in chain 158 int32_t depth = X509_STORE_CTX_get_error_depth(cts); 159 if (depth != 0) 160 161 { 162 BMCWEB_LOG_DEBUG 163 << this << " Certificate verification in progress (depth " 164 << depth << "), waiting to reach final depth"; 165 return true; 166 } 167 168 BMCWEB_LOG_DEBUG << this 169 << " Certificate verification of final depth"; 170 171 // Verify KeyUsage 172 bool isKeyUsageDigitalSignature = false; 173 bool isKeyUsageKeyAgreement = false; 174 175 ASN1_BIT_STRING* usage = static_cast<ASN1_BIT_STRING*>( 176 X509_get_ext_d2i(peerCert, NID_key_usage, nullptr, nullptr)); 177 178 if (usage == nullptr) 179 { 180 BMCWEB_LOG_DEBUG << this << " TLS usage is null"; 181 return true; 182 } 183 184 for (int i = 0; i < usage->length; i++) 185 { 186 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic) 187 unsigned char usageChar = usage->data[i]; 188 if (KU_DIGITAL_SIGNATURE & usageChar) 189 { 190 isKeyUsageDigitalSignature = true; 191 } 192 if (KU_KEY_AGREEMENT & usageChar) 193 { 194 isKeyUsageKeyAgreement = true; 195 } 196 } 197 ASN1_BIT_STRING_free(usage); 198 199 if (!isKeyUsageDigitalSignature || !isKeyUsageKeyAgreement) 200 { 201 BMCWEB_LOG_DEBUG << this 202 << " Certificate ExtendedKeyUsage does " 203 "not allow provided certificate to " 204 "be used for user authentication"; 205 return true; 206 } 207 208 // Determine that ExtendedKeyUsage includes Client Auth 209 210 stack_st_ASN1_OBJECT* extUsage = 211 static_cast<stack_st_ASN1_OBJECT*>(X509_get_ext_d2i( 212 peerCert, NID_ext_key_usage, nullptr, nullptr)); 213 214 if (extUsage == nullptr) 215 { 216 BMCWEB_LOG_DEBUG << this << " TLS extUsage is null"; 217 return true; 218 } 219 220 bool isExKeyUsageClientAuth = false; 221 for (int i = 0; i < sk_ASN1_OBJECT_num(extUsage); i++) 222 { 223 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-cstyle-cast) 224 int nid = OBJ_obj2nid(sk_ASN1_OBJECT_value(extUsage, i)); 225 if (NID_client_auth == nid) 226 { 227 isExKeyUsageClientAuth = true; 228 break; 229 } 230 } 231 sk_ASN1_OBJECT_free(extUsage); 232 233 // Certificate has to have proper key usages set 234 if (!isExKeyUsageClientAuth) 235 { 236 BMCWEB_LOG_DEBUG << this 237 << " Certificate ExtendedKeyUsage does " 238 "not allow provided certificate to " 239 "be used for user authentication"; 240 return true; 241 } 242 std::string sslUser; 243 // Extract username contained in CommonName 244 sslUser.resize(256, '\0'); 245 246 int status = X509_NAME_get_text_by_NID( 247 X509_get_subject_name(peerCert), NID_commonName, sslUser.data(), 248 static_cast<int>(sslUser.size())); 249 250 if (status == -1) 251 { 252 BMCWEB_LOG_DEBUG 253 << this << " TLS cannot get username to create session"; 254 return true; 255 } 256 257 size_t lastChar = sslUser.find('\0'); 258 if (lastChar == std::string::npos || lastChar == 0) 259 { 260 BMCWEB_LOG_DEBUG << this << " Invalid TLS user name"; 261 return true; 262 } 263 sslUser.resize(lastChar); 264 std::string unsupportedClientId; 265 sessionIsFromTransport = true; 266 userSession = persistent_data::SessionStore::getInstance() 267 .generateUserSession( 268 sslUser, req->ipAddress, unsupportedClientId, 269 persistent_data::PersistenceType::TIMEOUT); 270 if (userSession != nullptr) 271 { 272 BMCWEB_LOG_DEBUG 273 << this 274 << " Generating TLS session: " << userSession->uniqueId; 275 } 276 return true; 277 }); 278 } 279 280 Adaptor& socket() 281 { 282 return adaptor; 283 } 284 285 void start() 286 { 287 if (connectionCount >= 100) 288 { 289 BMCWEB_LOG_CRITICAL << this << "Max connection count exceeded."; 290 return; 291 } 292 293 startDeadline(); 294 295 // TODO(ed) Abstract this to a more clever class with the idea of an 296 // asynchronous "start" 297 if constexpr (std::is_same_v<Adaptor, 298 boost::beast::ssl_stream< 299 boost::asio::ip::tcp::socket>>) 300 { 301 adaptor.async_handshake(boost::asio::ssl::stream_base::server, 302 [this, self(shared_from_this())]( 303 const boost::system::error_code& ec) { 304 if (ec) 305 { 306 return; 307 } 308 doReadHeaders(); 309 }); 310 } 311 else 312 { 313 doReadHeaders(); 314 } 315 } 316 317 void handle() 318 { 319 std::error_code reqEc; 320 crow::Request& thisReq = req.emplace(parser->release(), reqEc); 321 if (reqEc) 322 { 323 BMCWEB_LOG_DEBUG << "Request failed to construct" << reqEc; 324 return; 325 } 326 thisReq.session = userSession; 327 328 // Fetch the client IP address 329 readClientIp(); 330 331 // Check for HTTP version 1.1. 332 if (thisReq.version() == 11) 333 { 334 if (thisReq.getHeaderValue(boost::beast::http::field::host).empty()) 335 { 336 res.result(boost::beast::http::status::bad_request); 337 completeRequest(res); 338 return; 339 } 340 } 341 342 BMCWEB_LOG_INFO << "Request: " 343 << " " << this << " HTTP/" << thisReq.version() / 10 344 << "." << thisReq.version() % 10 << ' ' 345 << thisReq.methodString() << " " << thisReq.target() 346 << " " << thisReq.ipAddress.to_string(); 347 348 res.isAliveHelper = [this]() -> bool { return isAlive(); }; 349 350 thisReq.ioService = static_cast<decltype(thisReq.ioService)>( 351 &adaptor.get_executor().context()); 352 353 if (res.completed) 354 { 355 completeRequest(res); 356 return; 357 } 358 #ifndef BMCWEB_INSECURE_DISABLE_AUTHENTICATION 359 if (!crow::authorization::isOnAllowlist(req->url, req->method()) && 360 thisReq.session == nullptr) 361 { 362 BMCWEB_LOG_WARNING << "Authentication failed"; 363 forward_unauthorized::sendUnauthorized( 364 req->url, req->getHeaderValue("User-Agent"), 365 req->getHeaderValue("Accept"), res); 366 completeRequest(res); 367 return; 368 } 369 #endif // BMCWEB_INSECURE_DISABLE_AUTHENTICATION 370 auto asyncResp = std::make_shared<bmcweb::AsyncResp>(); 371 BMCWEB_LOG_DEBUG << "Setting completion handler"; 372 asyncResp->res.setCompleteRequestHandler( 373 [self(shared_from_this())](crow::Response& thisRes) { 374 self->completeRequest(thisRes); 375 }); 376 377 if (thisReq.isUpgrade() && 378 boost::iequals( 379 thisReq.getHeaderValue(boost::beast::http::field::upgrade), 380 "websocket")) 381 { 382 handler->handleUpgrade(thisReq, res, std::move(adaptor)); 383 // delete lambda with self shared_ptr 384 // to enable connection destruction 385 asyncResp->res.setCompleteRequestHandler(nullptr); 386 return; 387 } 388 handler->handle(thisReq, asyncResp); 389 } 390 391 bool isAlive() 392 { 393 if constexpr (std::is_same_v<Adaptor, 394 boost::beast::ssl_stream< 395 boost::asio::ip::tcp::socket>>) 396 { 397 return adaptor.next_layer().is_open(); 398 } 399 else 400 { 401 return adaptor.is_open(); 402 } 403 } 404 void close() 405 { 406 if constexpr (std::is_same_v<Adaptor, 407 boost::beast::ssl_stream< 408 boost::asio::ip::tcp::socket>>) 409 { 410 adaptor.next_layer().close(); 411 if (sessionIsFromTransport && userSession != nullptr) 412 { 413 BMCWEB_LOG_DEBUG 414 << this 415 << " Removing TLS session: " << userSession->uniqueId; 416 persistent_data::SessionStore::getInstance().removeSession( 417 userSession); 418 } 419 } 420 else 421 { 422 adaptor.close(); 423 } 424 } 425 426 void completeRequest(crow::Response& thisRes) 427 { 428 if (!req) 429 { 430 return; 431 } 432 res = std::move(thisRes); 433 BMCWEB_LOG_INFO << "Response: " << this << ' ' << req->url << ' ' 434 << res.resultInt() << " keepalive=" << req->keepAlive(); 435 436 addSecurityHeaders(*req, res); 437 438 crow::authorization::cleanupTempSession(*req); 439 440 if (!isAlive()) 441 { 442 // BMCWEB_LOG_DEBUG << this << " delete (socket is closed) " << 443 // isReading 444 // << ' ' << isWriting; 445 // delete this; 446 447 // delete lambda with self shared_ptr 448 // to enable connection destruction 449 res.setCompleteRequestHandler(nullptr); 450 return; 451 } 452 if (res.body().empty() && !res.jsonValue.empty()) 453 { 454 if (http_helpers::requestPrefersHtml(req->getHeaderValue("Accept"))) 455 { 456 prettyPrintJson(res); 457 } 458 else 459 { 460 res.jsonMode(); 461 res.body() = res.jsonValue.dump( 462 2, ' ', true, nlohmann::json::error_handler_t::replace); 463 } 464 } 465 466 if (res.resultInt() >= 400 && res.body().empty()) 467 { 468 res.body() = std::string(res.reason()); 469 } 470 471 if (res.result() == boost::beast::http::status::no_content) 472 { 473 // Boost beast throws if content is provided on a no-content 474 // response. Ideally, this would never happen, but in the case that 475 // it does, we don't want to throw. 476 BMCWEB_LOG_CRITICAL 477 << this << " Response content provided but code was no-content"; 478 res.body().clear(); 479 } 480 481 res.addHeader(boost::beast::http::field::date, getCachedDateStr()); 482 483 res.keepAlive(req->keepAlive()); 484 485 doWrite(res); 486 487 // delete lambda with self shared_ptr 488 // to enable connection destruction 489 res.setCompleteRequestHandler(nullptr); 490 } 491 492 void readClientIp() 493 { 494 boost::asio::ip::address ip; 495 boost::system::error_code ec = getClientIp(ip); 496 if (ec) 497 { 498 return; 499 } 500 req->ipAddress = ip; 501 } 502 503 boost::system::error_code getClientIp(boost::asio::ip::address& ip) 504 { 505 boost::system::error_code ec; 506 BMCWEB_LOG_DEBUG << "Fetch the client IP address"; 507 boost::asio::ip::tcp::endpoint endpoint = 508 boost::beast::get_lowest_layer(adaptor).remote_endpoint(ec); 509 510 if (ec) 511 { 512 // If remote endpoint fails keep going. "ClientOriginIPAddress" 513 // will be empty. 514 BMCWEB_LOG_ERROR << "Failed to get the client's IP Address. ec : " 515 << ec; 516 return ec; 517 } 518 ip = endpoint.address(); 519 return ec; 520 } 521 522 private: 523 void doReadHeaders() 524 { 525 BMCWEB_LOG_DEBUG << this << " doReadHeaders"; 526 527 // Clean up any previous Connection. 528 boost::beast::http::async_read_header( 529 adaptor, buffer, *parser, 530 [this, 531 self(shared_from_this())](const boost::system::error_code& ec, 532 std::size_t bytesTransferred) { 533 BMCWEB_LOG_DEBUG << this << " async_read_header " 534 << bytesTransferred << " Bytes"; 535 bool errorWhileReading = false; 536 if (ec) 537 { 538 errorWhileReading = true; 539 if (ec == boost::asio::error::eof) 540 { 541 BMCWEB_LOG_WARNING 542 << this << " Error while reading: " << ec.message(); 543 } 544 else 545 { 546 BMCWEB_LOG_ERROR 547 << this << " Error while reading: " << ec.message(); 548 } 549 } 550 else 551 { 552 // if the adaptor isn't open anymore, and wasn't handed to a 553 // websocket, treat as an error 554 if (!isAlive() && 555 !boost::beast::websocket::is_upgrade(parser->get())) 556 { 557 errorWhileReading = true; 558 } 559 } 560 561 cancelDeadlineTimer(); 562 563 if (errorWhileReading) 564 { 565 close(); 566 BMCWEB_LOG_DEBUG << this << " from read(1)"; 567 return; 568 } 569 570 readClientIp(); 571 572 boost::asio::ip::address ip; 573 if (getClientIp(ip)) 574 { 575 BMCWEB_LOG_DEBUG << "Unable to get client IP"; 576 } 577 sessionIsFromTransport = false; 578 #ifndef BMCWEB_INSECURE_DISABLE_AUTHENTICATION 579 boost::beast::http::verb method = parser->get().method(); 580 userSession = crow::authorization::authenticate( 581 ip, res, method, parser->get().base(), userSession); 582 583 bool loggedIn = userSession != nullptr; 584 if (!loggedIn) 585 { 586 const boost::optional<uint64_t> contentLength = 587 parser->content_length(); 588 if (contentLength && 589 *contentLength > loggedOutPostBodyLimit) 590 { 591 BMCWEB_LOG_DEBUG << "Content length greater than limit " 592 << *contentLength; 593 close(); 594 return; 595 } 596 597 BMCWEB_LOG_DEBUG << "Starting quick deadline"; 598 } 599 #endif // BMCWEB_INSECURE_DISABLE_AUTHENTICATION 600 601 doRead(); 602 }); 603 } 604 605 void doRead() 606 { 607 BMCWEB_LOG_DEBUG << this << " doRead"; 608 startDeadline(); 609 boost::beast::http::async_read( 610 adaptor, buffer, *parser, 611 [this, 612 self(shared_from_this())](const boost::system::error_code& ec, 613 std::size_t bytesTransferred) { 614 BMCWEB_LOG_DEBUG << this << " async_read " << bytesTransferred 615 << " Bytes"; 616 cancelDeadlineTimer(); 617 if (ec) 618 { 619 BMCWEB_LOG_ERROR 620 << this << " Error while reading: " << ec.message(); 621 close(); 622 BMCWEB_LOG_DEBUG << this << " from read(1)"; 623 return; 624 } 625 handle(); 626 }); 627 } 628 629 void doWrite(crow::Response& thisRes) 630 { 631 BMCWEB_LOG_DEBUG << this << " doWrite"; 632 thisRes.preparePayload(); 633 serializer.emplace(*thisRes.stringResponse); 634 startDeadline(); 635 boost::beast::http::async_write( 636 adaptor, *serializer, 637 [this, 638 self(shared_from_this())](const boost::system::error_code& ec, 639 std::size_t bytesTransferred) { 640 BMCWEB_LOG_DEBUG << this << " async_write " << bytesTransferred 641 << " bytes"; 642 643 cancelDeadlineTimer(); 644 645 if (ec) 646 { 647 BMCWEB_LOG_DEBUG << this << " from write(2)"; 648 return; 649 } 650 if (!res.keepAlive()) 651 { 652 close(); 653 BMCWEB_LOG_DEBUG << this << " from write(1)"; 654 return; 655 } 656 657 serializer.reset(); 658 BMCWEB_LOG_DEBUG << this << " Clearing response"; 659 res.clear(); 660 parser.emplace(std::piecewise_construct, std::make_tuple()); 661 parser->body_limit(httpReqBodyLimit); // reset body limit for 662 // newly created parser 663 buffer.consume(buffer.size()); 664 665 // If the session was built from the transport, we don't need to 666 // clear it. All other sessions are generated per request. 667 if (!sessionIsFromTransport) 668 { 669 userSession = nullptr; 670 } 671 672 // Destroy the Request via the std::optional 673 req.reset(); 674 doReadHeaders(); 675 }); 676 } 677 678 void cancelDeadlineTimer() 679 { 680 timer.cancel(); 681 } 682 683 void startDeadline() 684 { 685 cancelDeadlineTimer(); 686 687 std::chrono::seconds timeout(15); 688 // allow slow uploads for logged in users 689 bool loggedIn = userSession != nullptr; 690 if (loggedIn) 691 { 692 timeout = std::chrono::seconds(60); 693 return; 694 } 695 696 std::weak_ptr<Connection<Adaptor, Handler>> weakSelf = weak_from_this(); 697 timer.expires_after(timeout); 698 timer.async_wait([weakSelf](const boost::system::error_code ec) { 699 // Note, we are ignoring other types of errors here; If the timer 700 // failed for any reason, we should still close the connection 701 702 std::shared_ptr<Connection<Adaptor, Handler>> self = 703 weakSelf.lock(); 704 if (!self) 705 { 706 BMCWEB_LOG_CRITICAL << self << " Failed to capture connection"; 707 return; 708 } 709 if (ec == boost::asio::error::operation_aborted) 710 { 711 // Canceled wait means the path succeeeded. 712 return; 713 } 714 if (ec) 715 { 716 BMCWEB_LOG_CRITICAL << self << " timer failed " << ec; 717 } 718 719 BMCWEB_LOG_WARNING << self << "Connection timed out, closing"; 720 721 self->close(); 722 }); 723 724 BMCWEB_LOG_DEBUG << this << " timer started"; 725 } 726 727 Adaptor adaptor; 728 Handler* handler; 729 // Making this a std::optional allows it to be efficiently destroyed and 730 // re-created on Connection reset 731 std::optional< 732 boost::beast::http::request_parser<boost::beast::http::string_body>> 733 parser; 734 735 boost::beast::flat_static_buffer<8192> buffer; 736 737 std::optional<boost::beast::http::response_serializer< 738 boost::beast::http::string_body>> 739 serializer; 740 741 std::optional<crow::Request> req; 742 crow::Response res; 743 744 bool sessionIsFromTransport = false; 745 std::shared_ptr<persistent_data::UserSession> userSession; 746 747 boost::asio::steady_timer timer; 748 749 std::function<std::string()>& getCachedDateStr; 750 751 using std::enable_shared_from_this< 752 Connection<Adaptor, Handler>>::shared_from_this; 753 754 using std::enable_shared_from_this< 755 Connection<Adaptor, Handler>>::weak_from_this; 756 }; 757 } // namespace crow 758