opsick
Macros | Functions
util.h File Reference

Opsick utility functions and pre-allocated content. More...

#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#include <http.h>
Include dependency graph for util.h:

Go to the source code of this file.

Macros

#define OPSICK_MIN(x, y)   (((x) < (y)) ? (x) : (y))
 
#define OPSICK_MAX(x, y)   (((x) > (y)) ? (x) : (y))
 

Functions

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)
 

Detailed Description

Opsick utility functions and pre-allocated content.

Author
Raphael Beck

Macro Definition Documentation

◆ OPSICK_MAX

#define OPSICK_MAX (   x,
 
)    (((x) > (y)) ? (x) : (y))

x > y ? x : y

◆ OPSICK_MIN

#define OPSICK_MIN (   x,
 
)    (((x) < (y)) ? (x) : (y))

x < y ? x : y

Function Documentation

◆ 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
binThe binary data to convert into hex string.
bin_lengthLength of the bin array.
outputWhere to write the hex string into.
output_sizeMaximum 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!
uppercaseShould 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
requestThe HTTP request to decrypt.
outWhere 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
idThe 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
hexstrThe hex string to convert.
hexstr_lengthLength of the hexstr
outputWhere to write the converted binary data into.
output_sizeSize 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
requestThe 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
stringThe NUL-terminated string to sign.
string_lengthLength of string argument (passing 0 will result in usage of strlen(string)).
outA 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
requestThe HTTP request.
bodyThe response body to sign and send.
body_lengthLength 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
str1String to compare.
str2String to compare to.
nHow 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
requestThe 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
requestThe HTTP request whose body signature you want to verify.
public_keyThe public key to use for verifying the signature.
Returns
1 if the signature is valid; 0 otherwise.