Split application in client and library

This commit is contained in:
Iwan Timmer
2015-06-27 17:29:54 +02:00
parent 9bf1ad75e8
commit d4ba4a5f8f
14 changed files with 33 additions and 19 deletions

View File

@@ -1,397 +0,0 @@
/*
* This file is part of Moonlight Embedded.
*
* Copyright (C) 2015 Iwan Timmer
*
* Moonlight is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* Moonlight is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Moonlight; if not, see <http://www.gnu.org/licenses/>.
*/
#include "http.h"
#include "xml.h"
#include "mkcert.h"
#include "client.h"
#include "limelight-common/Limelight.h"
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <openssl/sha.h>
#include <openssl/aes.h>
#include <openssl/rand.h>
#include <openssl/evp.h>
#include <openssl/x509.h>
#include <openssl/pem.h>
#include <openssl/err.h>
static const char *uniqueFileName = "uniqueid.dat";
static const char *certificateFileName = "client.pem";
static const char *p12FileName = "client.p12";
static const char *keyFileName = "key.pem";
#define UNIQUEID_BYTES 8
#define UNIQUEID_CHARS (UNIQUEID_BYTES*2)
static char unique_id[UNIQUEID_CHARS+1];
static X509 *cert;
static char cert_hex[4096];
static EVP_PKEY *privateKey;
static bool paired;
static int currentGame;
static int serverMajorVersion;
static void client_load_unique_id() {
FILE *fd = fopen(uniqueFileName, "r");
if (fd == NULL) {
unsigned char unique_data[UNIQUEID_BYTES];
RAND_bytes(unique_data, UNIQUEID_BYTES);
for (int i = 0; i < UNIQUEID_BYTES; i++) {
sprintf(unique_id + (i * 2), "%02x", unique_data[i]);
}
fd = fopen(uniqueFileName, "w");
fwrite(unique_id, UNIQUEID_CHARS, 1, fd);
} else {
fread(unique_id, UNIQUEID_CHARS, 1, fd);
}
fclose(fd);
unique_id[UNIQUEID_CHARS] = 0;
}
static void client_load_cert() {
FILE *fd = fopen(certificateFileName, "r");
if (fd == NULL) {
printf("Generating certificate...");
struct CertKeyPair cert = generateCertKeyPair();
printf("done\n");
saveCertKeyPair(certificateFileName, p12FileName, keyFileName, cert);
freeCertKeyPair(cert);
fd = fopen(certificateFileName, "r");
}
if (fd == NULL) {
fprintf(stderr, "Can't open certificate file\n");
exit(-1);
}
if (!(cert = PEM_read_X509(fd, NULL, NULL, NULL))) {
fprintf(stderr, "Error loading cert into memory.\n");
exit(-1);
}
rewind(fd);
int c;
int length = 0;
while ((c = fgetc(fd)) != EOF) {
sprintf(cert_hex + length, "%02x", c);
length += 2;
}
cert_hex[length] = 0;
fclose(fd);
fd = fopen(keyFileName, "r");
PEM_read_PrivateKey(fd, &privateKey, NULL, NULL);
fclose(fd);
}
static void client_load_server_status(const char *address) {
char url[4096];
sprintf(url, "https://%s:47984/serverinfo?uniqueid=%s", address, unique_id);
struct http_data *data = http_create_data();
http_request(url, data);
char *pairedText = NULL;
char *currentGameText = NULL;
char *versionText = NULL;
xml_search(data->memory, data->size, "currentgame", &currentGameText);
xml_search(data->memory, data->size, "PairStatus", &pairedText);
xml_search(data->memory, data->size, "appversion", &versionText);
http_free_data(data);
paired = pairedText != NULL && strcmp(pairedText, "1") == 0;
currentGame = currentGameText == NULL ? 0 : atoi(currentGameText);
char *versionSep = strstr(versionText, ".");
if (versionSep != NULL) {
*versionSep = 0;
}
serverMajorVersion = atoi(versionText);
free(pairedText);
free(currentGameText);
free(versionText);
}
static void bytes_to_hex(unsigned char *in, char *out, size_t len) {
for (int i = 0; i < len; i++) {
sprintf(out + i * 2, "%02x", in[i]);
}
out[len * 2] = 0;
}
static int sign_it(const char *msg, size_t mlen, unsigned char **sig, size_t *slen, EVP_PKEY *pkey) {
int result = -1;
*sig = NULL;
*slen = 0;
EVP_MD_CTX *ctx = EVP_MD_CTX_create();
if (ctx == NULL) {
fprintf(stderr, "EVP_MD_CTX_create failed, error 0x%lx\n", ERR_get_error());
return -1;
}
const EVP_MD *md = EVP_get_digestbyname("SHA256");
if (md == NULL) {
fprintf(stderr, "EVP_get_digestbyname failed, error 0x%lx\n", ERR_get_error());
goto cleanup;
}
int rc = EVP_DigestInit_ex(ctx, md, NULL);
if (rc != 1) {
fprintf(stderr, "EVP_DigestInit_ex failed, error 0x%lx\n", ERR_get_error());
goto cleanup;
}
rc = EVP_DigestSignInit(ctx, NULL, md, NULL, pkey);
if (rc != 1) {
fprintf(stderr, "EVP_DigestSignInit failed, error 0x%lx\n", ERR_get_error());
goto cleanup;
}
rc = EVP_DigestSignUpdate(ctx, msg, mlen);
if (rc != 1) {
fprintf(stderr, "EVP_DigestSignUpdate failed, error 0x%lx\n", ERR_get_error());
goto cleanup;
}
size_t req = 0;
rc = EVP_DigestSignFinal(ctx, NULL, &req);
if (rc != 1) {
fprintf(stderr, "EVP_DigestSignFinal failed (1), error 0x%lx\n", ERR_get_error());
goto cleanup;
}
if (!(req > 0)) {
fprintf(stderr, "EVP_DigestSignFinal failed (2), error 0x%lx\n", ERR_get_error());
goto cleanup;
}
*sig = OPENSSL_malloc(req);
if (*sig == NULL) {
fprintf(stderr, "OPENSSL_malloc failed, error 0x%lx\n", ERR_get_error());
goto cleanup;
}
*slen = req;
rc = EVP_DigestSignFinal(ctx, *sig, slen);
if (rc != 1) {
fprintf(stderr, "EVP_DigestSignFinal failed (3), return code %d, error 0x%lx\n", rc,
ERR_get_error());
goto cleanup;
}
if (req != *slen) {
fprintf(stderr, "EVP_DigestSignFinal failed, mismatched signature sizes %ld, %ld\n",
req, *slen);
goto cleanup;
}
result = 1;
cleanup:
EVP_MD_CTX_destroy(ctx);
ctx = NULL;
return !!result;
}
void client_pair(const char *address) {
char url[4096];
if (client_is_paired(NULL)) {
printf("Already paired\n");
return;
}
if (currentGame != 0) {
fprintf(stderr, "The computer is currently in a game. You must close the game before pairing.\n");
exit(-1);
}
char pin[5];
sprintf(pin, "%d%d%d%d", (int)random() % 10, (int)random() % 10, (int)random() % 10, (int)random() % 10);
printf("Please enter the following PIN on the target PC: %s\n", pin);
unsigned char salt_data[16];
char salt_hex[33];
RAND_bytes(salt_data, 16);
bytes_to_hex(salt_data, salt_hex, 16);
sprintf(url, "https://%s:47984/pair?uniqueid=%s&devicename=roth&updateState=1&phrase=getservercert&salt=%s&clientcert=%s", address, unique_id, salt_hex, cert_hex);
struct http_data *data = http_create_data();
http_request(url, data);
unsigned char salt_pin[20];
unsigned char aes_key_hash[20];
AES_KEY aes_key;
memcpy(salt_pin, salt_data, 16);
memcpy(salt_pin+16, salt_pin, 4);
SHA1(salt_pin, 20, aes_key_hash);
AES_set_encrypt_key((unsigned char *)aes_key_hash, 128, &aes_key);
unsigned char challenge_data[16];
unsigned char challenge_enc[16];
char challenge_hex[33];
RAND_bytes(challenge_data, 16);
AES_encrypt(challenge_data, challenge_enc, &aes_key);
bytes_to_hex(challenge_enc, challenge_hex, 16);
sprintf(url, "https://%s:47984/pair?uniqueid=%s&devicename=roth&updateState=1&clientchallenge=%s", address, unique_id, challenge_hex);
http_request(url, data);
char *result;
xml_search(data->memory, data->size, "challengeresponse", &result);
char challenge_response_data_enc[48];
char challenge_response_data[48];
for (int count = 0; count < strlen(result); count++) {
sscanf(&result[count], "%2hhx", &challenge_response_data_enc[count / 2]);
}
free(result);
for (int i = 0; i < 48; i += 16) {
AES_decrypt(&challenge_response_data_enc[i], &challenge_response_data[i], &aes_key);
}
char client_secret_data[16];
RAND_bytes(client_secret_data, 16);
char challenge_response[16 + 256 + 16];
char challenge_response_hash[32];
char challenge_response_hash_enc[32];
char challenge_response_hex[33];
memcpy(challenge_response, challenge_response_data + 20, 16);
memcpy(challenge_response + 16, cert->signature->data, 256);
memcpy(challenge_response + 16 + 256, client_secret_data, 16);
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], &aes_key);
}
bytes_to_hex(challenge_response_hash_enc, challenge_response_hex, 32);
sprintf(url, "https://%s:47984/pair?uniqueid=%s&devicename=roth&updateState=1&serverchallengeresp=%s", address, unique_id, challenge_response_hex);
http_request(url, data);
xml_search(data->memory, data->size, "pairingsecret", &result);
unsigned char *signature = NULL;
size_t s_len;
if (!sign_it(client_secret_data, 16, &signature, &s_len, privateKey)) {
fprintf(stderr, "Failed to sign data\n");
exit(-1);
}
char client_pairing_secret[16 + 256];
char client_pairing_secret_hex[(16 + 256) * 2 + 1];
memcpy(client_pairing_secret, client_secret_data, 16);
memcpy(client_pairing_secret + 16, signature, 256);
bytes_to_hex(client_pairing_secret, client_pairing_secret_hex, 16 + 256);
sprintf(url, "https://%s:47984/pair?uniqueid=%s&devicename=roth&updateState=1&clientpairingsecret=%s", address, unique_id, client_pairing_secret_hex);
http_request(url, data);
sprintf(url, "https://%s:47984/pair?uniqueid=%s&devicename=roth&updateState=1&phrase=pairchallenge", address, unique_id);
http_request(url, data);
http_free_data(data);
printf("Paired\n");
}
struct app_list *client_applist(const char *address) {
char url[4096];
struct http_data *data = http_create_data();
sprintf(url, "https://%s:47984/applist?uniqueid=%s", address, unique_id);
http_request(url, data);
struct app_list *list = xml_applist(data->memory, data->size);
http_free_data(data);
return list;
}
int client_get_app_id(const char *address, const char *name) {
struct app_list *list = client_applist(address);
while (list != NULL) {
if (strcmp(list->name, name) == 0)
return list->id;
list = list->next;
}
return -1;
}
void client_start_app(STREAM_CONFIGURATION *config, const char *address, int appId, bool sops, bool localaudio) {
RAND_bytes(config->remoteInputAesKey, 16);
memset(config->remoteInputAesIv, 0, 16);
srand(time(NULL));
char url[4096];
u_int32_t rikeyid = 1;
char rikey_hex[33];
bytes_to_hex(config->remoteInputAesKey, rikey_hex, 16);
struct http_data *data = http_create_data();
if (currentGame == 0)
sprintf(url, "https://%s:47984/launch?uniqueid=%s&appid=%d&mode=%dx%dx%d&additionalStates=1&sops=%d&rikey=%s&rikeyid=%d&localAudioPlayMode=%d", address, unique_id, appId, config->width, config->height, config->fps, sops, rikey_hex, rikeyid, localaudio);
else
sprintf(url, "https://%s:47984/resume?uniqueid=%s&rikey=%s&rikeyid=%d", address, unique_id, rikey_hex, rikeyid);
http_request(url, data);
http_free_data(data);
}
void client_quit_app(const char *address) {
char url[4096];
struct http_data *data = http_create_data();
sprintf(url, "https://%s:47984/cancel?uniqueid=%s", address, unique_id);
http_request(url, data);
http_free_data(data);
}
bool client_is_paired(const char *address) {
if (address != NULL)
client_load_server_status(address);
return paired;
}
int client_get_current_game(const char *address) {
if (address != NULL)
client_load_server_status(address);
return currentGame;
}
void client_init(const char *address) {
http_init();
client_load_unique_id();
client_load_cert();
client_load_server_status(address);
}
int client_get_server_version(void) {
return serverMajorVersion;
}

View File

@@ -1,34 +0,0 @@
/*
* This file is part of Moonlight Embedded.
*
* Copyright (C) 2015 Iwan Timmer
*
* Moonlight is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* Moonlight is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Moonlight; if not, see <http://www.gnu.org/licenses/>.
*/
#include "xml.h"
#include "limelight-common/Limelight.h"
#include "stdbool.h"
void client_init(const char* serverAddress);
void client_start_app(STREAM_CONFIGURATION *config, const char* serverAddress, int appId, bool sops, bool localaudio);
struct app_list* client_applist(const char* serverAddress);
int client_get_app_id(const char* serverAddress, const char* name);
void client_pair(const char *address);
int client_get_server_version(void);
bool client_is_paired(const char *address);
int client_get_current_game(const char *address);
void client_quit_app(const char *address);

View File

@@ -1,106 +0,0 @@
/*
* This file is part of Moonlight Embedded.
*
* Based on Avahi example client-browse-services
*
* Moonlight is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* Moonlight is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Moonlight; if not, see <http://www.gnu.org/licenses/>.
*/
#include <avahi-client/client.h>
#include <avahi-client/lookup.h>
#include <avahi-common/simple-watch.h>
#include <avahi-common/malloc.h>
#include <avahi-common/error.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <time.h>
static AvahiSimplePoll *simple_poll = NULL;
static void discover_client_callback(AvahiClient *c, AvahiClientState state, void *userdata) {
if (state == AVAHI_CLIENT_FAILURE) {
fprintf(stderr, "Server connection failure: %s\n", avahi_strerror(avahi_client_errno(c)));
avahi_simple_poll_quit(simple_poll);
}
}
static void discover_resolve_callback(AvahiServiceResolver *r, AvahiIfIndex interface, AvahiProtocol protocol, AvahiResolverEvent event, const char *name, const char *type, const char *domain, const char *host_name, const AvahiAddress *address, uint16_t port, AvahiStringList *txt, AvahiLookupResultFlags flags, void *userdata) {
if (event == AVAHI_RESOLVER_FOUND) {
if (userdata != NULL) {
avahi_address_snprint(userdata, AVAHI_ADDRESS_STR_MAX, address);
avahi_simple_poll_quit(simple_poll);
} else {
char strAddress[AVAHI_ADDRESS_STR_MAX];
avahi_address_snprint(strAddress, sizeof(strAddress), address);
printf(" %s (%s)\n", host_name, strAddress);
}
}
avahi_service_resolver_free(r);
}
static void discover_browse_callback(AvahiServiceBrowser *b, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, const char *name, const char *type, const char *domain, AvahiLookupResultFlags flags, void* userdata) {
AvahiClient *c = avahi_service_browser_get_client(b);
switch (event) {
case AVAHI_BROWSER_FAILURE:
fprintf(stderr, "(Discover) %s\n", avahi_strerror(avahi_client_errno(avahi_service_browser_get_client(b))));
avahi_simple_poll_quit(simple_poll);
break;
case AVAHI_BROWSER_NEW:
if (!(avahi_service_resolver_new(c, interface, protocol, name, type, domain, AVAHI_PROTO_UNSPEC, 0, discover_resolve_callback, userdata)))
fprintf(stderr, "Failed to resolve service '%s': %s\n", name, avahi_strerror(avahi_client_errno(c)));
break;
case AVAHI_BROWSER_REMOVE:
break;
}
}
void discover_server(char* dest) {
AvahiClient *client = NULL;
AvahiServiceBrowser *sb = NULL;
if (!(simple_poll = avahi_simple_poll_new())) {
fprintf(stderr, "Failed to create simple poll object.\n");
goto cleanup;
}
int error;
client = avahi_client_new(avahi_simple_poll_get(simple_poll), 0, discover_client_callback, NULL, &error);
if (!client) {
fprintf(stderr, "Failed to create client: %s\n", avahi_strerror(error));
goto cleanup;
}
if (!(sb = avahi_service_browser_new(client, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, "_nvstream._tcp", NULL, 0, discover_browse_callback, dest))) {
fprintf(stderr, "Failed to create service browser: %s\n", avahi_strerror(avahi_client_errno(client)));
goto cleanup;
}
avahi_simple_poll_loop(simple_poll);
cleanup:
if (sb)
avahi_service_browser_free(sb);
if (client)
avahi_client_free(client);
if (simple_poll)
avahi_simple_poll_free(simple_poll);
}

View File

@@ -1,20 +0,0 @@
/*
* This file is part of Moonlight Embedded.
*
* Moonlight is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* Moonlight is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Moonlight; if not, see <http://www.gnu.org/licenses/>.
*/
#define MAX_ADDRESS_SIZE 40
void discover_server(char* dest);

View File

@@ -1,27 +0,0 @@
/*
* This file is part of Moonlight Embedded.
*
* Copyright (C) 2015 Iwan Timmer
*
* Moonlight is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* Moonlight is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Moonlight; if not, see <http://www.gnu.org/licenses/>.
*/
#include <signal.h>
#include <pthread.h>
pthread_t main_thread_id;
void quit() {
pthread_kill(main_thread_id, SIGTERM);
}

View File

@@ -1,24 +0,0 @@
/*
* This file is part of Moonlight Embedded.
*
* Copyright (C) 2015 Iwan Timmer
*
* Moonlight is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* Moonlight is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Moonlight; if not, see <http://www.gnu.org/licenses/>.
*/
#include <pthread.h>
extern pthread_t main_thread_id;
void quit();

View File

@@ -1,104 +0,0 @@
/*
* This file is part of Moonlight Embedded.
*
* Copyright (C) 2015 Iwan Timmer
*
* Moonlight is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* Moonlight is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Moonlight; if not, see <http://www.gnu.org/licenses/>.
*/
#include "http.h"
#include <string.h>
#include <curl/curl.h>
static CURL *curl;
static const char *pCertFile = "./client.pem";
static const char *pKeyFile = "./key.pem";
static size_t _write_curl(void *contents, size_t size, size_t nmemb, void *userp)
{
size_t realsize = size * nmemb;
struct http_data *mem = (struct http_data *)userp;
mem->memory = realloc(mem->memory, mem->size + realsize + 1);
if(mem->memory == NULL) {
fprintf(stderr, "Not enough memory\n");
exit(EXIT_FAILURE);
}
memcpy(&(mem->memory[mem->size]), contents, realsize);
mem->size += realsize;
mem->memory[mem->size] = 0;
return realsize;
}
void http_init() {
curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
curl_easy_setopt(curl, CURLOPT_SSLENGINE_DEFAULT, 1L);
curl_easy_setopt(curl, CURLOPT_SSLCERTTYPE,"PEM");
curl_easy_setopt(curl, CURLOPT_SSLCERT, pCertFile);
curl_easy_setopt(curl, CURLOPT_SSLKEYTYPE, "PEM");
curl_easy_setopt(curl, CURLOPT_SSLKEY, pKeyFile);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, _write_curl);
curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1L);
}
int http_request(char* url, struct http_data* data) {
curl_easy_setopt(curl, CURLOPT_WRITEDATA, data);
curl_easy_setopt(curl, CURLOPT_URL, url);
if (data->size > 0) {
free(data->memory);
data->memory = malloc(1);
if(data->memory == NULL) {
fprintf(stderr, "Not enough memory\n");
exit(EXIT_FAILURE);
}
data->size = 0;
}
CURLcode res = curl_easy_perform(curl);
if(res != CURLE_OK) {
fprintf(stderr, "Connection failed: %s\n", curl_easy_strerror(res));
exit(EXIT_FAILURE);
}
return 0;
}
void http_cleanup() {
curl_easy_cleanup(curl);
}
struct http_data* http_create_data() {
struct http_data* data = malloc(sizeof(struct http_data));
data->memory = malloc(1);
if(data->memory == NULL) {
fprintf(stderr, "Not enough memory\n");
exit(EXIT_FAILURE);
}
data->size = 0;
return data;
}
void http_free_data(struct http_data* data) {
free(data->memory);
free(data);
}

View File

@@ -1,31 +0,0 @@
/*
* This file is part of Moonlight Embedded.
*
* Copyright (C) 2015 Iwan Timmer
*
* Moonlight is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* Moonlight is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Moonlight; if not, see <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
struct http_data {
char *memory;
size_t size;
};
void http_init();
int http_request(char* url, struct http_data* data);
struct http_data* http_create_data();
void http_free_data(struct http_data* data);

View File

@@ -1,176 +0,0 @@
/*
* Moonlight is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* Moonlight is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Moonlight; if not, see <http://www.gnu.org/licenses/>.
*/
#include "mkcert.h"
#include <stdio.h>
#include <stdlib.h>
#include <openssl/pem.h>
#include <openssl/conf.h>
#include <openssl/pkcs12.h>
#ifndef OPENSSL_NO_ENGINE
#include <openssl/engine.h>
#endif
static const int NUM_BITS = 2048;
static const int SERIAL = 0;
static const int NUM_YEARS = 10;
int mkcert(X509 **x509p, EVP_PKEY **pkeyp, int bits, int serial, int years);
int add_ext(X509 *cert, int nid, char *value);
struct CertKeyPair generateCertKeyPair() {
BIO *bio_err;
X509 *x509 = NULL;
EVP_PKEY *pkey = NULL;
PKCS12 *p12 = NULL;
CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
bio_err = BIO_new_fp(stderr, BIO_NOCLOSE);
SSLeay_add_all_algorithms();
ERR_load_crypto_strings();
mkcert(&x509, &pkey, NUM_BITS, SERIAL, NUM_YEARS);
p12 = PKCS12_create("limelight", "GameStream", pkey, x509, NULL, 0, 0, 0, 0, 0);
if (p12 == NULL) {
fprintf(stderr, "Error generating a valid PKCS12 certificate.\n");
}
#ifndef OPENSSL_NO_ENGINE
ENGINE_cleanup();
#endif
CRYPTO_cleanup_all_ex_data();
CRYPTO_mem_leaks(bio_err);
BIO_free(bio_err);
return (CertKeyPair){x509, pkey, p12};
}
void freeCertKeyPair(struct CertKeyPair certKeyPair) {
X509_free(certKeyPair.x509);
EVP_PKEY_free(certKeyPair.pkey);
PKCS12_free(certKeyPair.p12);
}
void saveCertKeyPair(const char* certFile, const char* p12File, const char* keyPairFile, CertKeyPair certKeyPair) {
FILE* certFilePtr = fopen(certFile, "w");
FILE* keyPairFilePtr = fopen(keyPairFile, "w");
FILE* p12FilePtr = fopen(p12File, "wb");
//TODO: error check
PEM_write_PrivateKey(keyPairFilePtr, certKeyPair.pkey, NULL, NULL, 0, NULL, NULL);
PEM_write_X509(certFilePtr, certKeyPair.x509);
i2d_PKCS12_fp(p12FilePtr, certKeyPair.p12);
fclose(p12FilePtr);
fclose(certFilePtr);
fclose(keyPairFilePtr);
}
int mkcert(X509 **x509p, EVP_PKEY **pkeyp, int bits, int serial, int years) {
X509 *x;
EVP_PKEY *pk;
RSA *rsa;
X509_NAME *name = NULL;
if (*pkeyp == NULL) {
if ((pk=EVP_PKEY_new()) == NULL) {
abort();
return(0);
}
} else {
pk = *pkeyp;
}
if (*x509p == NULL) {
if ((x = X509_new()) == NULL) {
goto err;
}
} else {
x = *x509p;
}
rsa = RSA_generate_key(bits, RSA_F4, NULL, NULL);
if (!EVP_PKEY_assign_RSA(pk, rsa)) {
abort();
goto err;
}
X509_set_version(x, 2);
ASN1_INTEGER_set(X509_get_serialNumber(x), serial);
X509_gmtime_adj(X509_get_notBefore(x), 0);
X509_gmtime_adj(X509_get_notAfter(x), (long)60*60*24*365*years);
X509_set_pubkey(x, pk);
name = X509_get_subject_name(x);
/* This function creates and adds the entry, working out the
* correct string type and performing checks on its length.
*/
X509_NAME_add_entry_by_txt(name,"CN", MBSTRING_ASC, (unsigned char*)"NVIDIA GameStream Client", -1, -1, 0);
/* Its self signed so set the issuer name to be the same as the
* subject.
*/
X509_set_issuer_name(x, name);
/* Add various extensions: standard extensions */
add_ext(x, NID_basic_constraints, "critical,CA:TRUE");
add_ext(x, NID_key_usage, "critical,keyCertSign,cRLSign");
add_ext(x, NID_subject_key_identifier, "hash");
if (!X509_sign(x, pk, EVP_sha1())) {
goto err;
}
*x509p = x;
*pkeyp = pk;
return(1);
err:
return(0);
}
/* Add extension using V3 code: we can set the config file as NULL
* because we wont reference any other sections.
*/
int add_ext(X509 *cert, int nid, char *value)
{
X509_EXTENSION *ex;
X509V3_CTX ctx;
/* This sets the 'context' of the extensions. */
/* No configuration database */
X509V3_set_ctx_nodb(&ctx);
/* Issuer and subject certs: both the target since it is self signed,
* no request and no CRL
*/
X509V3_set_ctx(&ctx, cert, cert, NULL, NULL, 0);
ex = X509V3_EXT_conf_nid(NULL, &ctx, nid, value);
if (!ex) {
return 0;
}
X509_add_ext(cert, ex, -1);
X509_EXTENSION_free(ex);
return 1;
}

View File

@@ -1,35 +0,0 @@
/*
* Created by Diego Waxemberg on 10/16/14.
* Copyright (c) 2014 Limelight Stream. All rights reserved.
*
* Moonlight is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* Moonlight is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Moonlight; if not, see <http://www.gnu.org/licenses/>.
*/
#ifndef Limelight_mkcert_h
#define Limelight_mkcert_h
#include <openssl/x509v3.h>
#include <openssl/pkcs12.h>
typedef struct CertKeyPair {
X509 *x509;
EVP_PKEY *pkey;
PKCS12 *p12;
} CertKeyPair;
struct CertKeyPair generateCertKeyPair();
void freeCertKeyPair(CertKeyPair);
void saveCertKeyPair(const char* certFile, const char* p12File, const char* keyPairFile, CertKeyPair certKeyPair);
#endif

128
src/xml.c
View File

@@ -1,128 +0,0 @@
/*
* This file is part of Moonlight Embedded.
*
* Copyright (C) 2015 Iwan Timmer
*
* Moonlight is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* Moonlight is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Moonlight; if not, see <http://www.gnu.org/licenses/>.
*/
#include "xml.h"
#include "expat.h"
#include <string.h>
struct xml_query {
char *memory;
size_t size;
int start;
void* data;
};
static void XMLCALL _xml_start_element(void *userData, const char *name, const char **atts) {
struct xml_query *search = (struct xml_query*) userData;
if (strcmp(search->data, name) == 0)
search->start++;
}
static void XMLCALL _xml_end_element(void *userData, const char *name) {
struct xml_query *search = (struct xml_query*) userData;
if (strcmp(search->data, name) == 0)
search->start--;
}
static void XMLCALL _xml_start_applist_element(void *userData, const char *name, const char **atts) {
struct xml_query *search = (struct xml_query*) userData;
if (strcmp("App", name) == 0) {
struct app_list* app = malloc(sizeof(struct app_list));
if (app == NULL) {
perror("Not enough memory");
exit(EXIT_FAILURE);
}
app->next = (struct app_list*) search->data;
search->data = app;
} else if (strcmp("ID", name) == 0 || strcmp("AppTitle", name) == 0) {
search->memory = malloc(1);
search->size = 0;
search->start = 1;
}
}
static void XMLCALL _xml_end_applist_element(void *userData, const char *name) {
struct xml_query *search = (struct xml_query*) userData;
if (search->start) {
struct app_list* list = (struct app_list*) search->data;
if (strcmp("ID", name) == 0) {
list->id = atoi(search->memory);
free(search->memory);
} else if (strcmp("AppTitle", name) == 0) {
list->name = search->memory;
}
search->start = 0;
}
}
static void XMLCALL _xml_write_data(void *userData, const XML_Char *s, int len) {
struct xml_query *search = (struct xml_query*) userData;
if (search->start > 0) {
search->memory = realloc(search->memory, search->size + len + 1);
if(search->memory == NULL) {
fprintf(stderr, "Not enough memory\n");
exit(EXIT_FAILURE);
}
memcpy(&(search->memory[search->size]), s, len);
search->size += len;
search->memory[search->size] = 0;
}
}
int xml_search(char* data, size_t len, char* node, char** result) {
struct xml_query search;
search.data = node;
search.start = 0;
search.memory = calloc(1, 1);
search.size = 0;
XML_Parser parser = XML_ParserCreate("UTF-8");
XML_SetUserData(parser, &search);
XML_SetElementHandler(parser, _xml_start_element, _xml_end_element);
XML_SetCharacterDataHandler(parser, _xml_write_data);
if (! XML_Parse(parser, data, len, 1)) {
int code = XML_GetErrorCode(parser);
fprintf(stderr, "XML Error: %s\n", XML_ErrorString(code));
return 1;
}
*result = search.memory;
return 0;
}
struct app_list* xml_applist(char* data, size_t len) {
struct xml_query query;
query.memory = calloc(1, 1);
query.size = 0;
query.start = 0;
query.data = NULL;
XML_Parser parser = XML_ParserCreate("UTF-8");
XML_SetUserData(parser, &query);
XML_SetElementHandler(parser, _xml_start_applist_element, _xml_end_applist_element);
XML_SetCharacterDataHandler(parser, _xml_write_data);
if (! XML_Parse(parser, data, len, 1)) {
int code = XML_GetErrorCode(parser);
fprintf(stderr, "XML Error %s\n", XML_ErrorString(code));
exit(-1);
}
return (struct app_list*) query.data;
}

View File

@@ -1,30 +0,0 @@
/*
* This file is part of Moonlight Embedded.
*
* Copyright (C) 2015 Iwan Timmer
*
* Moonlight is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* Moonlight is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Moonlight; if not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <stdio.h>
struct app_list {
char* name;
int id;
struct app_list* next;
};
int xml_search(char* data, size_t len, char* node, char** result);
struct app_list* xml_applist(char* data, size_t len);