Skip to content

Commit

Permalink
Assign name to certs when confirming PIN
Browse files Browse the repository at this point in the history
  • Loading branch information
xanderfrangos committed Jan 19, 2024
1 parent 21e4450 commit 9f6a485
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 8 deletions.
3 changes: 2 additions & 1 deletion src/confighttp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -673,7 +673,8 @@ namespace confighttp {
// TODO: Input Validation
pt::read_json(ss, inputTree);
std::string pin = inputTree.get<std::string>("pin");
outputTree.put("status", nvhttp::pin(pin));
std::string name = inputTree.get<std::string>("name");
outputTree.put("status", nvhttp::pin(pin, name));
}
catch (std::exception &e) {
BOOST_LOG(warning) << "SavePin: "sv << e.what();
Expand Down
46 changes: 42 additions & 4 deletions src/nvhttp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,15 @@ namespace nvhttp {
std::string pkey;
} conf_intern;

struct named_cert_t {
std::string name;
std::string cert;
};

struct client_t {
std::string uniqueID;
std::vector<std::string> certs;
std::vector<named_cert_t> named_certs;
};

struct pair_session_t {
Expand Down Expand Up @@ -195,7 +201,15 @@ namespace nvhttp {
cert_node.put_value(cert);
cert_nodes.push_back(std::make_pair(""s, cert_node));
}
node.add_child("certs"s, cert_nodes);

pt::ptree named_cert_nodes;
for (auto &named_cert : client.named_certs) {
pt::ptree named_cert_node;
named_cert_node.put("name"s, named_cert.name);
named_cert_node.put("cert"s, named_cert.cert);
named_cert_nodes.push_back(std::make_pair(""s, named_cert_node));
}
node.add_child("named_certs"s, named_cert_nodes);

nodes.push_back(std::make_pair(""s, node));
}
Expand Down Expand Up @@ -243,8 +257,21 @@ namespace nvhttp {

client.uniqueID = uniqID;

// Import from old format
for (auto &[_, el] : device_node.get_child("certs")) {
client.certs.emplace_back(el.get_value<std::string>());
named_cert_t named_cert;
named_cert.name = ""s;
named_cert.cert = el.get_value<std::string>();
client.named_certs.emplace_back(named_cert);
client.certs.emplace_back(named_cert.cert);
}

for (auto &[_, el] : device_node.get_child("named_certs")) {
named_cert_t named_cert;
named_cert.name = el.get_child("name").get_value<std::string>();
named_cert.cert = el.get_child("cert").get_value<std::string>();
client.named_certs.emplace_back(named_cert);
client.certs.emplace_back(named_cert.cert);
}
}
}
Expand Down Expand Up @@ -560,15 +587,16 @@ namespace nvhttp {
/**
* @brief Compare the user supplied pin to the Moonlight pin.
* @param pin The user supplied pin.
* @param name The user supplied name.
* @return `true` if the pin is correct, `false` otherwise.
*
* EXAMPLES:
* ```cpp
* bool pin_status = nvhttp::pin("1234");
* bool pin_status = nvhttp::pin("1234", "laptop");
* ```
*/
bool
pin(std::string pin) {
pin(std::string pin, std::string name) {
pt::ptree tree;
if (map_id_sess.empty()) {
return false;
Expand All @@ -594,6 +622,13 @@ namespace nvhttp {
auto &sess = std::begin(map_id_sess)->second;
getservercert(sess, tree, pin);

// set up named cert
auto &client = map_id_client[sess.client.uniqueID];
named_cert_t named_cert;
named_cert.name = name;
named_cert.cert = sess.client.cert;
client.named_certs.emplace_back(named_cert);

// response to the request for pin
std::ostringstream data;
pt::write_xml(data, tree);
Expand Down Expand Up @@ -970,6 +1005,9 @@ namespace nvhttp {
for (auto &cert : client.certs) {
cert_chain.add(crypto::x509(cert));
}
for (auto &named_cert : client.named_certs) {
cert_chain.add(crypto::x509(named_cert.cert));
}
}

auto add_cert = std::make_shared<safe::queue_t<crypto::x509_t>>(30);
Expand Down
2 changes: 1 addition & 1 deletion src/nvhttp.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ namespace nvhttp {
void
start();
bool
pin(std::string pin);
pin(std::string pin, std::string name);
void
erase_all_clients();
} // namespace nvhttp
7 changes: 5 additions & 2 deletions src_assets/common/assets/web/pin.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
<h1 class="my-4">PIN Pairing</h1>
<form action="" class="form d-flex flex-column align-items-center" id="form">
<div class="card flex-column d-flex p-4 mb-4">
<input type="text" pattern="\d*" placeholder="PIN" id="pin-input" class="form-control my-4" />
<input type="number" pattern="\d*" placeholder="PIN" id="pin-input" class="form-control mt-2" />
<input type="text" placeholder="Name (optional)" id="name-input" class="form-control my-4" />
<button class="btn btn-primary">Send</button>
</div>
<div class="alert alert-warning">
Expand All @@ -37,8 +38,9 @@ <h1 class="my-4">PIN Pairing</h1>
document.querySelector("#form").addEventListener("submit", (e) => {
e.preventDefault();
let pin = document.querySelector("#pin-input").value;
let name = document.querySelector("#name-input").value;
document.querySelector("#status").innerHTML = "";
let b = JSON.stringify({ pin: pin });
let b = JSON.stringify({ pin: pin, name: name });
fetch("/api/pin", { method: "POST", body: b })
.then((response) => response.json())
.then((response) => {
Expand All @@ -47,6 +49,7 @@ <h1 class="my-4">PIN Pairing</h1>
"#status"
).innerHTML = `<div class="alert alert-success" role="alert">Success! Please check Moonlight to continue</div>`;
document.querySelector("#pin-input").value = "";
document.querySelector("#name-input").value = "";
} else {
document.querySelector(
"#status"
Expand Down

0 comments on commit 9f6a485

Please sign in to comment.