From f18dbd88e0c89260aa73f3bcd864bf7ffa9c2d39 Mon Sep 17 00:00:00 2001 From: Cameron Gutman Date: Thu, 31 Mar 2016 11:54:06 -0400 Subject: [PATCH] Implement NaCl-side of pairing with Gen 7 servers --- libgamestream/pairing.c | 20 +++++++++++++++----- libgamestream/pairing.h | 2 +- main.cpp | 2 +- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/libgamestream/pairing.c b/libgamestream/pairing.c index 3d7a758..1a795ee 100644 --- a/libgamestream/pairing.c +++ b/libgamestream/pairing.c @@ -126,7 +126,7 @@ cleanup: return result; } -int gs_pair(const char* address, const char* pin) { +int gs_pair(int serverMajorVersion, const char* address, const char* pin) { int ret = GS_OK; char url[4096]; @@ -143,11 +143,17 @@ int gs_pair(const char* address, const char* pin) { goto cleanup; unsigned char salt_pin[20]; - unsigned char aes_key_hash[20]; + unsigned char aes_key_hash[32]; AES_KEY enc_key, dec_key; memcpy(salt_pin, salt_data, 16); memcpy(salt_pin+16, pin, 4); - SHA1(salt_pin, 20, aes_key_hash); + + int hash_length = serverMajorVersion >= 7 ? 32 : 20; + if (serverMajorVersion >= 7) + SHA256(salt_pin, 20, aes_key_hash); + else + SHA1(salt_pin, 20, aes_key_hash); + AES_set_encrypt_key((unsigned char *)aes_key_hash, 128, &enc_key); AES_set_decrypt_key((unsigned char *)aes_key_hash, 128, &dec_key); @@ -186,10 +192,14 @@ int gs_pair(const char* address, const char* pin) { unsigned char challenge_response_hash[32]; unsigned char challenge_response_hash_enc[32]; char challenge_response_hex[65]; - memcpy(challenge_response, challenge_response_data + 20, 16); + memcpy(challenge_response, challenge_response_data + hash_length, 16); memcpy(challenge_response + 16, g_Cert->signature->data, 256); memcpy(challenge_response + 16 + 256, client_secret_data, 16); - SHA1(challenge_response, 16 + 256 + 16, challenge_response_hash); + + if (serverMajorVersion >= 7) + SHA256(challenge_response, 16 + 256 + 16, challenge_response_hash); + else + SHA1(challenge_response, 16 + 256 + 16, challenge_response_hash); for (int i = 0; i < 32; i += 16) { AES_encrypt(&challenge_response_hash[i], &challenge_response_hash_enc[i], &enc_key); diff --git a/libgamestream/pairing.h b/libgamestream/pairing.h index 2bc1ea1..2619d04 100644 --- a/libgamestream/pairing.h +++ b/libgamestream/pairing.h @@ -23,7 +23,7 @@ extern "C" { #endif -int gs_pair(const char* address, const char* pin); +int gs_pair(int serverMajorVersion, const char* address, const char* pin); #ifdef __cplusplus } diff --git a/main.cpp b/main.cpp index 25d51a9..5318fc5 100644 --- a/main.cpp +++ b/main.cpp @@ -223,7 +223,7 @@ void MoonlightInstance::HandlePair(int32_t callbackId, pp::VarArray args) { } void MoonlightInstance::PairCallback(int32_t /*result*/, int32_t callbackId, pp::VarArray args) { - int err = gs_pair(args.Get(0).AsString().c_str(), args.Get(1).AsString().c_str()); + int err = gs_pair(atoi(args.Get(0).AsString().c_str()), args.Get(1).AsString().c_str(), args.Get(2).AsString().c_str()); pp::VarDictionary ret; ret.Set("callbackId", pp::Var(callbackId));