Merge RegistKey and Auth

This commit is contained in:
Florian Märkl 2019-08-15 22:56:02 +02:00
commit 73ccfb6f22
No known key found for this signature in database
GPG key ID: 125BC8A5A6A1E857
7 changed files with 62 additions and 51 deletions

View file

@ -70,9 +70,7 @@ CHIAKI_EXPORT void chiaki_connect_video_profile_preset(ChiakiConnectVideoProfile
typedef struct chiaki_connect_info_t
{
const char *host; // null terminated
const char *regist_key; // null terminated
const char *ostype; // null terminated
char auth[CHIAKI_SESSION_AUTH_SIZE]; // must be completely filled (pad with \0)
char regist_key[CHIAKI_SESSION_AUTH_SIZE]; // must be completely filled (pad with \0)
uint8_t morning[0x10];
uint8_t did[CHIAKI_RP_DID_SIZE];
ChiakiConnectVideoProfile video_profile;
@ -137,9 +135,7 @@ typedef struct chiaki_session_t
struct addrinfo *host_addrinfos;
struct addrinfo *host_addrinfo_selected;
char hostname[128];
char *regist_key;
char *ostype;
char auth[CHIAKI_RPCRYPT_KEY_SIZE];
char regist_key[CHIAKI_RPCRYPT_KEY_SIZE];
uint8_t morning[CHIAKI_RPCRYPT_KEY_SIZE];
uint8_t did[CHIAKI_RP_DID_SIZE];
ChiakiConnectVideoProfile video_profile;

View file

@ -28,6 +28,8 @@
#include <assert.h>
#define SESSION_OSTYPE "Win10.0.0"
#define SESSION_CTRL_PORT 9295
#define CTRL_EXPECT_TIMEOUT 5000
@ -342,7 +344,7 @@ static ChiakiErrorCode ctrl_connect(ChiakiCtrl *ctrl)
uint8_t auth_enc[CHIAKI_RPCRYPT_KEY_SIZE];
ChiakiErrorCode err = chiaki_rpcrypt_encrypt(&session->rpcrypt, 0, (uint8_t *)session->connect_info.auth, auth_enc, CHIAKI_RPCRYPT_KEY_SIZE);
ChiakiErrorCode err = chiaki_rpcrypt_encrypt(&session->rpcrypt, 0, (uint8_t *)session->connect_info.regist_key, auth_enc, CHIAKI_RPCRYPT_KEY_SIZE);
if(err != CHIAKI_ERR_SUCCESS)
goto error;
char auth_b64[CHIAKI_RPCRYPT_KEY_SIZE*2];
@ -360,10 +362,10 @@ static ChiakiErrorCode ctrl_connect(ChiakiCtrl *ctrl)
goto error;
uint8_t ostype_enc[128];
size_t ostype_len = strlen(session->connect_info.ostype) + 1;
size_t ostype_len = strlen(SESSION_OSTYPE) + 1;
if(ostype_len > sizeof(ostype_enc))
goto error;
err = chiaki_rpcrypt_encrypt(&session->rpcrypt, 2, (uint8_t *)session->connect_info.ostype, ostype_enc, ostype_len);
err = chiaki_rpcrypt_encrypt(&session->rpcrypt, 2, (uint8_t *)SESSION_OSTYPE, ostype_enc, ostype_len);
if(err != CHIAKI_ERR_SUCCESS)
goto error;
char ostype_b64[256];

View file

@ -151,23 +151,9 @@ CHIAKI_EXPORT ChiakiErrorCode chiaki_session_init(ChiakiSession *session, Chiaki
return CHIAKI_ERR_PARSE_ADDR;
}
session->connect_info.regist_key = strdup(connect_info->regist_key);
if(!session->connect_info.regist_key)
{
chiaki_session_fini(session);
return CHIAKI_ERR_MEMORY;
}
session->connect_info.ostype = strdup(connect_info->ostype);
if(!session->connect_info.regist_key)
{
chiaki_session_fini(session);
return CHIAKI_ERR_MEMORY;
}
chiaki_controller_state_set_idle(&session->controller_state);
memcpy(session->connect_info.auth, connect_info->auth, sizeof(session->connect_info.auth));
memcpy(session->connect_info.regist_key, connect_info->regist_key, sizeof(session->connect_info.regist_key));
memcpy(session->connect_info.morning, connect_info->morning, sizeof(session->connect_info.morning));
memcpy(session->connect_info.did, connect_info->did, sizeof(session->connect_info.did));
@ -193,8 +179,6 @@ CHIAKI_EXPORT void chiaki_session_fini(ChiakiSession *session)
chiaki_stop_pipe_fini(&session->stop_pipe);
chiaki_cond_fini(&session->state_cond);
chiaki_mutex_fini(&session->state_mutex);
free(session->connect_info.regist_key);
free(session->connect_info.ostype);
freeaddrinfo(session->connect_info.host_addrinfos);
}
@ -527,9 +511,27 @@ static bool session_thread_request_session(ChiakiSession *session)
"Rp-Version: 8.0\r\n"
"\r\n";
size_t regist_key_len = sizeof(session->connect_info.regist_key);
for(size_t i=0; i<regist_key_len; i++)
{
if(!session->connect_info.regist_key[i])
{
regist_key_len = i;
break;
}
}
char regist_key_hex[sizeof(session->connect_info.regist_key) * 2 + 1];
ChiakiErrorCode err = format_hex(regist_key_hex, sizeof(regist_key_hex), (uint8_t *)session->connect_info.regist_key, regist_key_len);
if(err != CHIAKI_ERR_SUCCESS)
{
close(session_sock);
session->quit_reason = CHIAKI_QUIT_REASON_SESSION_REQUEST_UNKNOWN;
return false;
}
char buf[512];
int request_len = snprintf(buf, sizeof(buf), session_request_fmt,
session->connect_info.hostname, SESSION_PORT, session->connect_info.regist_key);
session->connect_info.hostname, SESSION_PORT, regist_key_hex);
if(request_len < 0 || request_len >= sizeof(buf))
{
close(session_sock);
@ -551,7 +553,7 @@ static bool session_thread_request_session(ChiakiSession *session)
size_t header_size;
size_t received_size;
chiaki_mutex_unlock(&session->state_mutex);
ChiakiErrorCode err = chiaki_recv_http_header(session_sock, buf, sizeof(buf), &header_size, &received_size, &session->stop_pipe, SESSION_EXPECT_TIMEOUT_MS);
err = chiaki_recv_http_header(session_sock, buf, sizeof(buf), &header_size, &received_size, &session->stop_pipe, SESSION_EXPECT_TIMEOUT_MS);
ChiakiErrorCode mutex_err = chiaki_mutex_lock(&session->state_mutex);
assert(mutex_err == CHIAKI_ERR_SUCCESS);
if(err != CHIAKI_ERR_SUCCESS)

View file

@ -76,4 +76,29 @@ static inline ChiakiErrorCode parse_hex(uint8_t *buf, size_t *buf_size, const ch
return CHIAKI_ERR_SUCCESS;
}
static inline char nibble_char(uint8_t v)
{
if(v > 0xf)
return '0';
if(v < 0xa)
return '0' + v;
return 'a' + v;
}
static inline ChiakiErrorCode format_hex(char *hex_buf, size_t hex_buf_size, const uint8_t *buf, size_t buf_size)
{
if(hex_buf_size < buf_size * 2 + 1)
return CHIAKI_ERR_BUF_TOO_SMALL;
for(size_t i=0; i<buf_size; i++)
{
uint8_t v = buf[i];
hex_buf[i*2+0] = nibble_char(v >> 4);
hex_buf[i*2+1] = nibble_char(v & 0xf);
}
hex_buf[buf_size*2] = '\0';
return CHIAKI_ERR_SUCCESS;
}
#endif // CHIAKI_UTILS_H