Tom Joseph | 8e832ee | 2016-12-06 17:47:08 +0530 | [diff] [blame^] | 1 | #include "open_session.hpp" |
| 2 | |
| 3 | #include <iostream> |
| 4 | |
| 5 | #include "comm_module.hpp" |
| 6 | #include "endian.hpp" |
| 7 | #include "main.hpp" |
| 8 | |
| 9 | namespace command |
| 10 | { |
| 11 | |
| 12 | std::vector<uint8_t> openSession(std::vector<uint8_t>& inPayload, |
| 13 | const message::Handler& handler) |
| 14 | { |
| 15 | std::cout << ">> openSession\n"; |
| 16 | |
| 17 | std::vector<uint8_t> outPayload(sizeof(OpenSessionResponse)); |
| 18 | auto request = reinterpret_cast<OpenSessionRequest*>(inPayload.data()); |
| 19 | auto response = reinterpret_cast<OpenSessionResponse*>(outPayload.data()); |
| 20 | |
| 21 | // Check for valid Authentication Algorithms |
| 22 | if (request->authAlgo != static_cast<uint8_t> |
| 23 | (cipher::rakp_auth::Algorithms::RAKP_HMAC_SHA1)) |
| 24 | { |
| 25 | response->status_code = |
| 26 | static_cast<uint8_t>(RAKP_ReturnCode::INVALID_AUTH_ALGO); |
| 27 | return outPayload; |
| 28 | } |
| 29 | |
| 30 | // Check for valid Integrity Algorithms |
| 31 | if (request->intAlgo != 0) |
| 32 | { |
| 33 | response->status_code = |
| 34 | static_cast<uint8_t>(RAKP_ReturnCode::INVALID_INTEGRITY_ALGO); |
| 35 | return outPayload; |
| 36 | } |
| 37 | |
| 38 | // Check for valid Confidentiality Algorithms |
| 39 | if (request->confAlgo != 0) |
| 40 | { |
| 41 | response->status_code = |
| 42 | static_cast<uint8_t>(RAKP_ReturnCode::INVALID_CONF_ALGO); |
| 43 | return outPayload; |
| 44 | } |
| 45 | |
| 46 | std::shared_ptr<session::Session> session; |
| 47 | try |
| 48 | { |
| 49 | // Start an IPMI session |
| 50 | session = (std::get<session::Manager&>(singletonPool).startSession( |
| 51 | endian::from_ipmi<>(request->remoteConsoleSessionID), |
| 52 | static_cast<session::Privilege>(request->maxPrivLevel), |
| 53 | static_cast<cipher::rakp_auth::Algorithms>(request->authAlgo) |
| 54 | )).lock(); |
| 55 | } |
| 56 | catch (std::exception& e) |
| 57 | { |
| 58 | std::cerr << e.what() << "\n"; |
| 59 | response->status_code = static_cast<uint8_t> |
| 60 | (RAKP_ReturnCode::INSUFFICIENT_RESOURCE); |
| 61 | std::cerr << "openSession : Problem opening a session\n"; |
| 62 | return outPayload; |
| 63 | } |
| 64 | |
| 65 | response->messageTag = request->messageTag; |
| 66 | response->status_code = static_cast<uint8_t>(RAKP_ReturnCode::NO_ERROR); |
| 67 | response->maxPrivLevel = static_cast<uint8_t>(session->curPrivLevel); |
| 68 | response->remoteConsoleSessionID = request->remoteConsoleSessionID; |
| 69 | response->managedSystemSessionID = endian::to_ipmi<> |
| 70 | (session->getBMCSessionID()); |
| 71 | |
| 72 | response->authPayload = request->authPayload ; |
| 73 | response->authPayloadLen = request->authPayloadLen ; |
| 74 | response->authAlgo = request->authAlgo; |
| 75 | |
| 76 | response->intPayload = request->intPayload ; |
| 77 | response->intPayloadLen = request->intPayloadLen ; |
| 78 | response->intAlgo = request->intAlgo; |
| 79 | |
| 80 | response->confPayload = request->confPayload ; |
| 81 | response->confPayloadLen = request->confPayloadLen ; |
| 82 | response->confAlgo = request->confAlgo; |
| 83 | |
| 84 | session->updateLastTransactionTime(); |
| 85 | |
| 86 | // Session state is Setup in progress |
| 87 | session->state = session::State::SETUP_IN_PROGRESS; |
| 88 | |
| 89 | std::cout << "<< openSession\n"; |
| 90 | return outPayload; |
| 91 | } |
| 92 | |
| 93 | } // namespace command |