Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HMAC signing for http push #5

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
54 changes: 43 additions & 11 deletions modules/http_push.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

#include <syslog.h>
#include <curl.h>
#include <openssl/hmac.h>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there some smaller HMAC implementation available? We don't want to depend on OpenSSL.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, will look around if there's something appropriate and ask here if that's ok.


/* Timestamp when last successful push occurred. */
static time_t last_push_at = 0;
Expand Down Expand Up @@ -49,6 +50,7 @@ static int nw_http_push_start_acquire_data(struct nodewatcher_module *module,

/* Get the configured URL from UCI. */
char *url = nw_uci_get_string(uci, "nodewatcher.@agent[0].push_url");

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this empty line?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Readability habit I guess... You want me to stop doing that or just wanted an explanation? :)

if (url) {
/* Collect all the module data and perform the push. */
json_object *data = nw_module_get_output();
Expand All @@ -61,30 +63,60 @@ static int nw_http_push_start_acquire_data(struct nodewatcher_module *module,
/* Default. */
timeout = 5;
}

const char* data_string = json_object_to_json_string(data); // TODO: const?

curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeout);
curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1);
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_POST, 1);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, json_object_to_json_string(data));
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data_string);

#if LIBCURL_VERSION_NUM >= 0x072700
/* Pin server-side public key when configured. */
char *server_pubkey = nw_uci_get_string(uci, "nodewatcher.@agent[0].push_server_pubkey");
char *auth_type = nw_uci_get_string(uci, "nodewatcher.@agent[0].authentication_method");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would rename this to push_authentication_method.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do.


if (server_pubkey) {
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_easy_setopt(curl, CURLOPT_PINNEDPUBLICKEY, server_pubkey);
if (strcmp(auth_type, "hmac") == 0) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this inslide if (server_pubkey) block? It should be possible to configure no public key, only an HMAC (secret) key.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ohhh... My bad. Will add another option and separate block.

unsigned char *hmac_result = HMAC(EVP_sha256(), server_pubkey, strlen(server_pubkey), (unsigned char *)data_string, strlen(data_string), NULL, NULL);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, so you are using server_pubkey as the secret key for HMAC. This is not good, you should instead introduce a new configuration option for this new key.

char signature[((strlen((char *)hmac_result)+2)/3)*4];

if (nw_base64_encode(hmac_result, strlen((char *)hmac_result), signature, sizeof(signature)) == 0) {
char sig_dest[26+sizeof(signature)];
strcat(strcpy(sig_dest, "X-Nodewatcher-Signature: "), signature);

struct curl_slist *chunk = NULL;
chunk = curl_slist_append(chunk, "X-Nodewatcher-Signature-Algorithm: hmac-sha256");
chunk = curl_slist_append(chunk, sig_dest);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
}

free(hmac_result);

} else {
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_easy_setopt(curl, CURLOPT_PINNEDPUBLICKEY, server_pubkey);
}

free(server_pubkey);
free(auth_type);
}
#endif

/* Setup client authentication when configured. */
char *client_certificate = nw_uci_get_string(uci, "nodewatcher.@agent[0].push_client_certificate");
char *client_key = nw_uci_get_string(uci, "nodewatcher.@agent[0].push_client_key");
if (client_certificate && client_key) {
curl_easy_setopt(curl, CURLOPT_SSLCERT, client_certificate);
curl_easy_setopt(curl, CURLOPT_SSLKEY, client_key);
}
if (strcmp(auth_type, "hmac") != 0) {
char *client_certificate = nw_uci_get_string(uci, "nodewatcher.@agent[0].push_client_certificate");
char *client_key = nw_uci_get_string(uci, "nodewatcher.@agent[0].push_client_key");

free(client_certificate);
free(client_key);
if (client_certificate && client_key) {
curl_easy_setopt(curl, CURLOPT_SSLCERT, client_certificate);
curl_easy_setopt(curl, CURLOPT_SSLKEY, client_key);
}

free(client_certificate);
free(client_key);
}

/* Provide a buffer to store errors in. */
char errbuf[CURL_ERROR_SIZE];
Expand Down