mirror of
https://github.com/moonlight-stream/moonlight-embedded.git
synced 2026-06-17 06:11:36 +00:00
Specify directory to load certificate data from
This commit is contained in:
+44
-18
@@ -36,10 +36,8 @@
|
|||||||
#include <openssl/pem.h>
|
#include <openssl/pem.h>
|
||||||
#include <openssl/err.h>
|
#include <openssl/err.h>
|
||||||
|
|
||||||
static const char *uniqueFileName = "uniqueid.dat";
|
#define UNIQUE_FILE_NAME "uniqueid.dat"
|
||||||
static const char *certificateFileName = "client.pem";
|
#define P12_FILE_NAME "client.p12"
|
||||||
static const char *p12FileName = "client.p12";
|
|
||||||
static const char *keyFileName = "key.pem";
|
|
||||||
|
|
||||||
#define UNIQUEID_BYTES 8
|
#define UNIQUEID_BYTES 8
|
||||||
#define UNIQUEID_CHARS (UNIQUEID_BYTES*2)
|
#define UNIQUEID_CHARS (UNIQUEID_BYTES*2)
|
||||||
@@ -49,42 +47,60 @@ static X509 *cert;
|
|||||||
static char cert_hex[4096];
|
static char cert_hex[4096];
|
||||||
static EVP_PKEY *privateKey;
|
static EVP_PKEY *privateKey;
|
||||||
|
|
||||||
static void load_unique_id() {
|
static int load_unique_id(const char* keyDirectory) {
|
||||||
FILE *fd = fopen(uniqueFileName, "r");
|
char uniqueFilePath[4096];
|
||||||
|
sprintf(uniqueFilePath, "%s/%s", keyDirectory, UNIQUE_FILE_NAME);
|
||||||
|
|
||||||
|
FILE *fd = fopen(uniqueFilePath, "r");
|
||||||
if (fd == NULL) {
|
if (fd == NULL) {
|
||||||
unsigned char unique_data[UNIQUEID_BYTES];
|
unsigned char unique_data[UNIQUEID_BYTES];
|
||||||
RAND_bytes(unique_data, UNIQUEID_BYTES);
|
RAND_bytes(unique_data, UNIQUEID_BYTES);
|
||||||
for (int i = 0; i < UNIQUEID_BYTES; i++) {
|
for (int i = 0; i < UNIQUEID_BYTES; i++) {
|
||||||
sprintf(unique_id + (i * 2), "%02x", unique_data[i]);
|
sprintf(unique_id + (i * 2), "%02x", unique_data[i]);
|
||||||
}
|
}
|
||||||
fd = fopen(uniqueFileName, "w");
|
fd = fopen(uniqueFilePath, "w");
|
||||||
|
if (fd == NULL)
|
||||||
|
return GS_FAILED;
|
||||||
|
|
||||||
fwrite(unique_id, UNIQUEID_CHARS, 1, fd);
|
fwrite(unique_id, UNIQUEID_CHARS, 1, fd);
|
||||||
} else {
|
} else {
|
||||||
fread(unique_id, UNIQUEID_CHARS, 1, fd);
|
fread(unique_id, UNIQUEID_CHARS, 1, fd);
|
||||||
}
|
}
|
||||||
fclose(fd);
|
fclose(fd);
|
||||||
unique_id[UNIQUEID_CHARS] = 0;
|
unique_id[UNIQUEID_CHARS] = 0;
|
||||||
|
|
||||||
|
return GS_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void load_cert() {
|
static int load_cert(const char* keyDirectory) {
|
||||||
FILE *fd = fopen(certificateFileName, "r");
|
char certificateFilePath[4096];
|
||||||
|
sprintf(certificateFilePath, "%s/%s", keyDirectory, CERTIFICATE_FILE_NAME);
|
||||||
|
|
||||||
|
char keyFilePath[4096];
|
||||||
|
sprintf(&keyFilePath[0], "%s/%s", keyDirectory, KEY_FILE_NAME);
|
||||||
|
|
||||||
|
FILE *fd = fopen(certificateFilePath, "r");
|
||||||
if (fd == NULL) {
|
if (fd == NULL) {
|
||||||
printf("Generating certificate...");
|
printf("Generating certificate...");
|
||||||
CERT_KEY_PAIR cert = mkcert_generate();
|
CERT_KEY_PAIR cert = mkcert_generate();
|
||||||
printf("done\n");
|
printf("done\n");
|
||||||
mkcert_save(certificateFileName, p12FileName, keyFileName, cert);
|
|
||||||
|
char p12FilePath[4096];
|
||||||
|
sprintf(p12FilePath, "%s/%s", keyDirectory, P12_FILE_NAME);
|
||||||
|
|
||||||
|
mkcert_save(certificateFilePath, p12FilePath, keyFilePath, cert);
|
||||||
mkcert_free(cert);
|
mkcert_free(cert);
|
||||||
fd = fopen(certificateFileName, "r");
|
fd = fopen(certificateFilePath, "r");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (fd == NULL) {
|
if (fd == NULL) {
|
||||||
fprintf(stderr, "Can't open certificate file\n");
|
fprintf(stderr, "Can't open certificate file\n");
|
||||||
exit(-1);
|
return GS_FAILED;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!(cert = PEM_read_X509(fd, NULL, NULL, NULL))) {
|
if (!(cert = PEM_read_X509(fd, NULL, NULL, NULL))) {
|
||||||
fprintf(stderr, "Error loading cert into memory.\n");
|
fprintf(stderr, "Error loading cert into memory.\n");
|
||||||
exit(-1);
|
return GS_FAILED;
|
||||||
}
|
}
|
||||||
|
|
||||||
rewind(fd);
|
rewind(fd);
|
||||||
@@ -99,9 +115,16 @@ static void load_cert() {
|
|||||||
|
|
||||||
fclose(fd);
|
fclose(fd);
|
||||||
|
|
||||||
fd = fopen(keyFileName, "r");
|
fd = fopen(keyFilePath, "r");
|
||||||
|
if (fd == NULL) {
|
||||||
|
fprintf(stderr, "Error loading key into memory.\n");
|
||||||
|
return GS_FAILED;
|
||||||
|
}
|
||||||
|
|
||||||
PEM_read_PrivateKey(fd, &privateKey, NULL, NULL);
|
PEM_read_PrivateKey(fd, &privateKey, NULL, NULL);
|
||||||
fclose(fd);
|
fclose(fd);
|
||||||
|
|
||||||
|
return GS_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int load_server_status(const char *address, PSERVER_DATA server) {
|
static int load_server_status(const char *address, PSERVER_DATA server) {
|
||||||
@@ -419,10 +442,13 @@ int gs_quit_app(PSERVER_DATA server) {
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
int gs_init(PSERVER_DATA server, const char *address) {
|
int gs_init(PSERVER_DATA server, const char *address, const char *keyDirectory) {
|
||||||
http_init();
|
if (load_unique_id(keyDirectory) != GS_OK)
|
||||||
load_unique_id();
|
return GS_FAILED;
|
||||||
load_cert();
|
|
||||||
|
|
||||||
|
if (load_cert(keyDirectory))
|
||||||
|
return GS_FAILED;
|
||||||
|
|
||||||
|
http_init(keyDirectory);
|
||||||
return load_server_status(address, server);
|
return load_server_status(address, server);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ typedef struct _SERVER_DATA {
|
|||||||
int serverMajorVersion;
|
int serverMajorVersion;
|
||||||
} SERVER_DATA, *PSERVER_DATA;
|
} SERVER_DATA, *PSERVER_DATA;
|
||||||
|
|
||||||
int gs_init(PSERVER_DATA server, const char *address);
|
int gs_init(PSERVER_DATA server, const char *address, const char *keyDirectory);
|
||||||
int gs_start_app(PSERVER_DATA server, PSTREAM_CONFIGURATION config, int appId, bool sops, bool localaudio);
|
int gs_start_app(PSERVER_DATA server, PSTREAM_CONFIGURATION config, int appId, bool sops, bool localaudio);
|
||||||
int gs_applist(PSERVER_DATA server, PAPP_LIST app_list);
|
int gs_applist(PSERVER_DATA server, PAPP_LIST app_list);
|
||||||
int gs_pair(PSERVER_DATA server, char* pin);
|
int gs_pair(PSERVER_DATA server, char* pin);
|
||||||
|
|||||||
+15
-14
@@ -34,10 +34,8 @@ static size_t _write_curl(void *contents, size_t size, size_t nmemb, void *userp
|
|||||||
PHTTP_DATA mem = (PHTTP_DATA)userp;
|
PHTTP_DATA mem = (PHTTP_DATA)userp;
|
||||||
|
|
||||||
mem->memory = realloc(mem->memory, mem->size + realsize + 1);
|
mem->memory = realloc(mem->memory, mem->size + realsize + 1);
|
||||||
if(mem->memory == NULL) {
|
if(mem->memory == NULL)
|
||||||
fprintf(stderr, "Not enough memory\n");
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
|
||||||
|
|
||||||
memcpy(&(mem->memory[mem->size]), contents, realsize);
|
memcpy(&(mem->memory[mem->size]), contents, realsize);
|
||||||
mem->size += realsize;
|
mem->size += realsize;
|
||||||
@@ -46,17 +44,23 @@ static size_t _write_curl(void *contents, size_t size, size_t nmemb, void *userp
|
|||||||
return realsize;
|
return realsize;
|
||||||
}
|
}
|
||||||
|
|
||||||
int http_init() {
|
int http_init(const char* keyDirectory) {
|
||||||
curl = curl_easy_init();
|
curl = curl_easy_init();
|
||||||
if (curl)
|
if (curl)
|
||||||
return GS_FAILED;
|
return GS_FAILED;
|
||||||
|
|
||||||
|
char certificateFilePath[4096];
|
||||||
|
sprintf(certificateFilePath, "%s/%s", keyDirectory, CERTIFICATE_FILE_NAME);
|
||||||
|
|
||||||
|
char keyFilePath[4096];
|
||||||
|
sprintf(&keyFilePath[0], "%s/%s", keyDirectory, KEY_FILE_NAME);
|
||||||
|
|
||||||
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
|
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
|
||||||
curl_easy_setopt(curl, CURLOPT_SSLENGINE_DEFAULT, 1L);
|
curl_easy_setopt(curl, CURLOPT_SSLENGINE_DEFAULT, 1L);
|
||||||
curl_easy_setopt(curl, CURLOPT_SSLCERTTYPE,"PEM");
|
curl_easy_setopt(curl, CURLOPT_SSLCERTTYPE,"PEM");
|
||||||
curl_easy_setopt(curl, CURLOPT_SSLCERT, pCertFile);
|
curl_easy_setopt(curl, CURLOPT_SSLCERT, certificateFilePath);
|
||||||
curl_easy_setopt(curl, CURLOPT_SSLKEYTYPE, "PEM");
|
curl_easy_setopt(curl, CURLOPT_SSLKEYTYPE, "PEM");
|
||||||
curl_easy_setopt(curl, CURLOPT_SSLKEY, pKeyFile);
|
curl_easy_setopt(curl, CURLOPT_SSLKEY, keyFilePath);
|
||||||
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
|
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
|
||||||
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, _write_curl);
|
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, _write_curl);
|
||||||
curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1L);
|
curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1L);
|
||||||
@@ -71,16 +75,15 @@ int http_request(char* url, PHTTP_DATA data) {
|
|||||||
if (data->size > 0) {
|
if (data->size > 0) {
|
||||||
free(data->memory);
|
free(data->memory);
|
||||||
data->memory = malloc(1);
|
data->memory = malloc(1);
|
||||||
if(data->memory == NULL) {
|
if(data->memory == NULL)
|
||||||
fprintf(stderr, "Not enough memory\n");
|
|
||||||
return GS_OUT_OF_MEMORY;
|
return GS_OUT_OF_MEMORY;
|
||||||
}
|
|
||||||
data->size = 0;
|
data->size = 0;
|
||||||
}
|
}
|
||||||
CURLcode res = curl_easy_perform(curl);
|
CURLcode res = curl_easy_perform(curl);
|
||||||
|
|
||||||
if(res != CURLE_OK) {
|
if(res != CURLE_OK) {
|
||||||
fprintf(stderr, "Connection failed: %s\n", curl_easy_strerror(res));
|
gs_error = curl_easy_strerror(res);
|
||||||
return GS_FAILED;
|
return GS_FAILED;
|
||||||
} else if (data->memory == NULL) {
|
} else if (data->memory == NULL) {
|
||||||
return GS_OUT_OF_MEMORY;
|
return GS_OUT_OF_MEMORY;
|
||||||
@@ -95,13 +98,11 @@ void http_cleanup() {
|
|||||||
|
|
||||||
PHTTP_DATA http_create_data() {
|
PHTTP_DATA http_create_data() {
|
||||||
PHTTP_DATA data = malloc(sizeof(HTTP_DATA));
|
PHTTP_DATA data = malloc(sizeof(HTTP_DATA));
|
||||||
if (data == NULL) {
|
if (data == NULL)
|
||||||
fprintf(stderr, "Not enough memory\n");
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
|
||||||
data->memory = malloc(1);
|
data->memory = malloc(1);
|
||||||
if(data->memory == NULL) {
|
if(data->memory == NULL) {
|
||||||
fprintf(stderr, "Not enough memory\n");
|
|
||||||
free(data);
|
free(data);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,12 +21,15 @@
|
|||||||
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#define CERTIFICATE_FILE_NAME "client.pem"
|
||||||
|
#define KEY_FILE_NAME "key.pem"
|
||||||
|
|
||||||
typedef struct _HTTP_DATA {
|
typedef struct _HTTP_DATA {
|
||||||
char *memory;
|
char *memory;
|
||||||
size_t size;
|
size_t size;
|
||||||
} HTTP_DATA, *PHTTP_DATA;
|
} HTTP_DATA, *PHTTP_DATA;
|
||||||
|
|
||||||
int http_init();
|
int http_init(const char* keyDirectory);
|
||||||
PHTTP_DATA http_create_data();
|
PHTTP_DATA http_create_data();
|
||||||
int http_request(char* url, PHTTP_DATA data);
|
int http_request(char* url, PHTTP_DATA data);
|
||||||
void http_free_data(PHTTP_DATA data);
|
void http_free_data(PHTTP_DATA data);
|
||||||
|
|||||||
+1
-1
@@ -289,7 +289,7 @@ int main(int argc, char* argv[]) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
PSERVER_DATA server;
|
PSERVER_DATA server;
|
||||||
if (gs_init(server, address) != GS_OK) {
|
if (gs_init(server, address, ".") != GS_OK) {
|
||||||
fprintf(stderr, "Can't connect to server %s\n", address);
|
fprintf(stderr, "Can't connect to server %s\n", address);
|
||||||
exit(-1);
|
exit(-1);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user