-
Notifications
You must be signed in to change notification settings - Fork 11
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
base: master
Are you sure you want to change the base?
Changes from 6 commits
9d0572d
0009463
a44c43e
8304689
8f92420
da088ef
9489e58
8453843
7abca81
7e13185
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,7 @@ | |
|
||
#include <syslog.h> | ||
#include <curl.h> | ||
#include <openssl/hmac.h> | ||
|
||
/* Timestamp when last successful push occurred. */ | ||
static time_t last_push_at = 0; | ||
|
@@ -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"); | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why this empty line? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
|
@@ -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"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would rename this to There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this inslide There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see, so you are using |
||
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]; | ||
|
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.