Opsick utility functions and pre-allocated content.
More...
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#include <http.h>
Go to the source code of this file.
|
#define | OPSICK_MIN(x, y) (((x) < (y)) ? (x) : (y)) |
|
#define | OPSICK_MAX(x, y) (((x) > (y)) ? (x) : (y)) |
|
|
FIOBJ | opsick_get_preallocated_string (uint32_t id) |
|
void | opsick_util_init () |
|
void | opsick_util_free () |
|
int | opsick_hexstr2bin (const char *hexstr, size_t hexstr_length, uint8_t *output, size_t output_size, size_t *output_length) |
|
int | opsick_bin2hexstr (const uint8_t *bin, size_t bin_length, char *output, size_t output_size, size_t *output_length, uint8_t uppercase) |
|
int | opsick_strncmpic (const char *str1, const char *str2, size_t n) |
|
void | opsick_sign (const char *string, size_t string_length, char *out) |
|
void | opsick_sign_and_send (http_s *request, char *body, size_t body_length) |
|
int | opsick_request_has_signature (http_s *request) |
|
int | opsick_verify_api_request_signature (http_s *request) |
|
int | opsick_verify_request_signature (http_s *request, const char *public_key) |
|
int | opsick_decrypt (http_s *request, char **out) |
|
Opsick utility functions and pre-allocated content.
- Author
- Raphael Beck
◆ OPSICK_MAX
#define OPSICK_MAX |
( |
|
x, |
|
|
|
y |
|
) |
| (((x) > (y)) ? (x) : (y)) |
◆ OPSICK_MIN
#define OPSICK_MIN |
( |
|
x, |
|
|
|
y |
|
) |
| (((x) < (y)) ? (x) : (y)) |
◆ opsick_bin2hexstr()
int opsick_bin2hexstr |
( |
const uint8_t * |
bin, |
|
|
size_t |
bin_length, |
|
|
char * |
output, |
|
|
size_t |
output_size, |
|
|
size_t * |
output_length, |
|
|
uint8_t |
uppercase |
|
) |
| |
Converts a byte array to a hex string.
A NUL-terminator is appended at the end of the output buffer, so make sure to allocate at least (bin_length * 2) + 1
bytes!
- Parameters
-
bin | The binary data to convert into hex string. |
bin_length | Length of the bin array. |
output | Where to write the hex string into. |
output_size | Maximum capacity of the output buffer. Make sure to allocate at least (bin_length * 2) + 1 bytes! |
output_length | [OPTIONAL] Where to write the output string length into. This is always gonna be bin_length * 2 , but you can still choose to write it out just to be sure. If you want to omit this: no problem.. just pass NULL ! |
uppercase | Should the output string characters be UPPER- or lowercase? 0 == false; anything else == true |
- Returns
0
if conversion succeeded. 1
if one or more required arguments were NULL
or invalid. 2
if the output buffer size is insufficient: please allocate at least (bin_length * 2) + 1
bytes!
◆ opsick_decrypt()
int opsick_decrypt |
( |
http_s * |
request, |
|
|
char ** |
out |
|
) |
| |
Decrypts an HTTP request's body that was encrypted for the Opsick server.
- Parameters
-
request | The HTTP request to decrypt. |
out | Where to write the decrypted HTTP request body into (this will be allocated on success, so remember to free() this). |
- Returns
0
if decryption succeeded. Non-zero error code if decryption failed.
◆ opsick_get_preallocated_string()
FIOBJ opsick_get_preallocated_string |
( |
uint32_t |
id | ) |
|
Gets a pre-allocated string (e.g. for response header names).
- Parameters
-
id | The string id to use for retrieval. |
- Returns
- The pre-allocated string.
◆ opsick_hexstr2bin()
int opsick_hexstr2bin |
( |
const char * |
hexstr, |
|
|
size_t |
hexstr_length, |
|
|
uint8_t * |
output, |
|
|
size_t |
output_size, |
|
|
size_t * |
output_length |
|
) |
| |
Converts a hex string to binary array.
A NUL-terminator is appended at the end of the output buffer, so make sure to allocate at least (hexstr_length / 2) + 1
bytes!
- Parameters
-
hexstr | The hex string to convert. |
hexstr_length | Length of the hexstr |
output | Where to write the converted binary data into. |
output_size | Size of the output buffer (make sure to allocate at least (hexstr_length / 2) + 1 bytes!). |
output_length | [OPTIONAL] Where to write the output array length into. This is always gonna be hexstr_length / 2 , but you can still choose to write it out just to be sure. If you want to omit this: no problem.. just pass NULL ! |
- Returns
0
if conversion succeeded. 1
if one or more required arguments were NULL
or invalid. 2
if the hexadecimal string is in an invalid format (e.g. not divisible by 2). 3
if output buffer size was insufficient (needs to be at least (hexstr_length / 2) + 1
bytes).
◆ opsick_request_has_signature()
int opsick_request_has_signature |
( |
http_s * |
request | ) |
|
Checks whether a given request has a signature header or not.
- Parameters
-
request | The request to check. |
- Returns
1
if the request has a signature header; 0
if request
is NULL
or has no signature header.
◆ opsick_sign()
void opsick_sign |
( |
const char * |
string, |
|
|
size_t |
string_length, |
|
|
char * |
out |
|
) |
| |
Signs a string using the opsick server's private signing key.
- Parameters
-
string | The NUL-terminated string to sign. |
string_length | Length of string argument (passing 0 will result in usage of strlen(string) ). |
out | A writable output buffer of at least 129B size (128 characters + 1 NUL-terminator). |
◆ opsick_sign_and_send()
void opsick_sign_and_send |
( |
http_s * |
request, |
|
|
char * |
body, |
|
|
size_t |
body_length |
|
) |
| |
Signs a string using the opsick server's private signing key and sets the HTTP response signature header + response body accordingly.
- Parameters
-
request | The HTTP request. |
body | The response body to sign and send. |
body_length | Length of the passed body string parameter (passing 0 will result in usage of strlen(body) ). |
◆ opsick_strncmpic()
int opsick_strncmpic |
( |
const char * |
str1, |
|
|
const char * |
str2, |
|
|
size_t |
n |
|
) |
| |
Compares two strings ignoring UPPER vs. lowercase.
- Parameters
-
str1 | String to compare. |
str2 | String to compare to. |
n | How many characters of the string should be compared (starting from index 0)? |
- Returns
- If the strings are equal,
0
is returned. Otherwise, something else.
◆ opsick_util_free()
void opsick_util_free |
( |
| ) |
|
Deallocate the Opsick utility functions (freeing their used memory).
◆ opsick_util_init()
void opsick_util_init |
( |
| ) |
|
Initialize the Opsick utility functions.
◆ opsick_verify_api_request_signature()
int opsick_verify_api_request_signature |
( |
http_s * |
request | ) |
|
Verifies an HTTP request's signature.
- Parameters
-
request | The HTTP request whose body signature you want to verify. |
- Returns
1
if the signature is valid; 0
otherwise.
◆ opsick_verify_request_signature()
int opsick_verify_request_signature |
( |
http_s * |
request, |
|
|
const char * |
public_key |
|
) |
| |
Verifies an HTTP request's signature.
- Parameters
-
request | The HTTP request whose body signature you want to verify. |
public_key | The public key to use for verifying the signature. |
- Returns
1
if the signature is valid; 0
otherwise.