| 1 | #ifdef _WIN32 |
| 2 | |
| 3 | #include "SspiNegotiateClient.h" |
| 4 | #include "openmsx.h" |
| 5 | |
| 6 | namespace openmsx { |
| 7 | |
| 8 | SspiNegotiateClient::SspiNegotiateClient(StreamWrapper& clientStream) |
| 9 | : SspiPackageBase(clientStream, NEGOSSP_NAME_W) |
| 10 | { |
| 11 | } |
| 12 | |
| 13 | bool SspiNegotiateClient::Authenticate() |
| 14 | { |
| 15 | TimeStamp tsCredsExpiry; |
| 16 | SECURITY_STATUS ss = AcquireCredentialsHandleW( |
| 17 | nullptr, |
| 18 | const_cast<SEC_WCHAR*>(NEGOSSP_NAME_W), |
| 19 | SECPKG_CRED_OUTBOUND, |
| 20 | nullptr, |
| 21 | nullptr, |
| 22 | nullptr, |
| 23 | nullptr, |
| 24 | &hCreds, |
| 25 | &tsCredsExpiry); |
| 26 | |
| 27 | DebugPrintSecurityStatus("AcquireCredentialsHandleW", ss); |
| 28 | if (ss != SEC_E_OK) { |
| 29 | return false; |
| 30 | } |
| 31 | |
| 32 | SecBufferDesc secClientBufferDesc, secServerBufferDesc; |
| 33 | SecBuffer secClientBuffer, secServerBuffer; |
| 34 | InitTokenContextBuffer(&secClientBufferDesc, &secClientBuffer); |
| 35 | InitTokenContextBuffer(&secServerBufferDesc, &secServerBuffer); |
| 36 | |
| 37 | std::vector<char> buffer; |
| 38 | PCtxtHandle phContext = nullptr; |
| 39 | PSecBufferDesc psecServerBufferDesc = nullptr; |
| 40 | while (true) { |
| 41 | |
| 42 | ULONG fContextAttr; |
| 43 | TimeStamp tsContextExpiry; |
| 44 | ss = InitializeSecurityContextA( |
| 45 | &hCreds, |
| 46 | phContext, |
| 47 | nullptr, // To use Kerberos, we'll need an SPN here |
| 48 | ISC_REQ_ALLOCATE_MEMORY | ISC_REQ_CONNECTION | ISC_REQ_STREAM, |
| 49 | 0, |
| 50 | SECURITY_NETWORK_DREP, |
| 51 | psecServerBufferDesc, |
| 52 | 0, |
| 53 | &hContext, |
| 54 | &secClientBufferDesc, |
| 55 | &fContextAttr, |
| 56 | &tsContextExpiry); |
| 57 | |
| 58 | DebugPrintSecurityStatus("InitializeSecurityContextA", ss); |
| 59 | if (ss != SEC_E_OK && ss != SEC_I_CONTINUE_NEEDED) { |
| 60 | return false; |
| 61 | } |
| 62 | |
| 63 | // If we have something for the server, send it |
| 64 | if (secClientBuffer.cbBuffer) { |
| 65 | PRT_DEBUG("Sending " << secClientBuffer.cbBuffer << " bytes to server"); |
| 66 | bool ret = SendChunk(stream, secClientBuffer.pvBuffer, secClientBuffer.cbBuffer); |
| 67 | ClearContextBuffers(&secClientBufferDesc); |
| 68 | if (!ret) { |
| 69 | PRT_DEBUG("SendChunk failed"); |
| 70 | return false; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | // SEC_E_OK means that we're done |
| 75 | if (ss == SEC_E_OK) { |
| 76 | DebugPrintSecurityPackageName(&hContext); |
| 77 | DebugPrintSecurityPrincipalName(&hContext); |
| 78 | return true; |
| 79 | } |
| 80 | |
| 81 | // Receive another buffer from the server |
| 82 | PRT_DEBUG("Receiving server chunk"); |
| 83 | bool ret = RecvChunk(stream, buffer, cbMaxTokenSize); |
| 84 | if (!ret) { |
| 85 | PRT_DEBUG("RecvChunk failed"); |
| 86 | return false; |
| 87 | } |
| 88 | PRT_DEBUG("Received " << buffer.size() << " bytes"); |
| 89 | |
| 90 | // Another time around the loop |
| 91 | secServerBuffer.cbBuffer = static_cast<unsigned long>(buffer.size()); |
| 92 | secServerBuffer.pvBuffer = &buffer[0]; |
| 93 | |
| 94 | phContext = &hContext; |
| 95 | psecServerBufferDesc = &secServerBufferDesc; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | } // namespace openmsx |
| 100 | |
| 101 | #endif // _WIN32 |