Functions and data types for multiple encoding schemes. More...
#include "common.h"
Go to the source code of this file.
Data Structures | |
struct | qsc_encoding_ber_element |
Represents a BER (Basic Encoding Rules) element. More... | |
Macros | |
#define | QSC_ENCODING_BER_CLASS_UNIVERSAL 0x00U |
Universal tag class. | |
#define | QSC_ENCODING_BER_CLASS_APPLICATION 0x40U |
Application tag class. | |
#define | QSC_ENCODING_BER_CLASS_CONTEXT_SPECIFIC 0x80U |
Context-specific tag class. | |
#define | QSC_ENCODING_BER_CLASS_PRIVATE 0xC0U |
Private tag class. | |
#define | QSC_BER_ENCODING_INDEFINITE_LENGTH ((size_t)-1) |
Private tag class. | |
Typedefs | |
typedef QSC_EXPORT_API struct qsc_encoding_ber_element | qsc_encoding_ber_element |
Represents a BER (Basic Encoding Rules) element. | |
Functions | |
QSC_EXPORT_API bool | qsc_encoding_base64_decode (uint8_t *output, size_t otplen, const char *input, size_t inlen) |
Decodes a Base64 string to a byte array. | |
QSC_EXPORT_API size_t | qsc_encoding_base64_decoded_size (const char *input, size_t length) |
Gets the expected size of an array required for Base64 decoding. | |
QSC_EXPORT_API void | qsc_encoding_base64_encode (char *output, size_t otplen, const uint8_t *input, size_t inplen) |
Encodes a byte array to a Base64 string. | |
QSC_EXPORT_API size_t | qsc_encoding_base64_encoded_size (size_t length) |
Gets the expected size of a character array required for Base64 encoding. | |
QSC_EXPORT_API bool | qsc_encoding_base64_is_valid_char (char value) |
Tests if an encoded character is a valid Base64 encoding. | |
QSC_EXPORT_API qsc_encoding_ber_element * | qsc_encoding_ber_decode_element (const uint8_t *buffer, size_t buflen, size_t *consumed) |
Decodes a BER element from encoded data. | |
QSC_EXPORT_API size_t | qsc_encoding_ber_decode_length (const uint8_t *buffer, size_t buflen, size_t *length, bool *indef) |
Decodes a BER-encoded length value. | |
QSC_EXPORT_API size_t | qsc_encoding_ber_decode_tag (const uint8_t *buffer, size_t buflen, uint8_t *tagclass, bool *construct, uint32_t *tagnum) |
Decodes an ASN.1 tag from BER-encoded data. | |
QSC_EXPORT_API size_t | qsc_encoding_ber_encode_element (qsc_encoding_ber_element *element, uint8_t *buffer, size_t buflen) |
Encodes a complete BER element. | |
QSC_EXPORT_API size_t | qsc_encoding_ber_encode_length (size_t length, uint8_t *buffer, size_t buflen) |
Encodes a length value into BER format. | |
QSC_EXPORT_API size_t | qsc_encoding_ber_encode_tag (uint8_t tagclass, bool construct, uint32_t tagnum, uint8_t *buffer, size_t buflen) |
Encodes an ASN.1 tag into BER format. | |
QSC_EXPORT_API void | encoding_ber_free_element (qsc_encoding_ber_element *element) |
Free a BER element from the array. | |
QSC_EXPORT_API qsc_encoding_ber_element * | qsc_encoding_der_decode_element (const uint8_t *buffer, size_t buflen, size_t *consumed) |
Decodes an ASN.1 element encoded in DER format. | |
QSC_EXPORT_API size_t | qsc_encoding_der_encode_element (qsc_encoding_ber_element *element, uint8_t *buffer, size_t buflen) |
Encodes an ASN.1 element using DER (Distinguished Encoding Rules). | |
QSC_EXPORT_API bool | qsc_encoding_hex_decode (const char *input, size_t inplen, uint8_t *output, size_t otplen, size_t *declen) |
Decodes a hexadecimal string into binary data. | |
QSC_EXPORT_API bool | qsc_encoding_hex_encode (const uint8_t *input, size_t inplen, char *output, size_t otplen) |
Encodes binary data into a hexadecimal string. | |
QSC_EXPORT_API bool | qsc_encoding_pem_decode (const char *input, uint8_t *output, size_t otplen, size_t *declen) |
Decodes a PEM-formatted string into binary data. | |
QSC_EXPORT_API bool | qsc_encoding_pem_encode (const char *label, char *output, size_t otplen, const uint8_t *data, size_t datalen) |
Encodes binary data in PEM format. | |
Functions and data types for multiple encoding schemes.
This header provides a collection of functions and data types that support several encoding and decoding schemes used in cryptography and data representation. The supported schemes include:
#define QSC_BER_ENCODING_INDEFINITE_LENGTH ((size_t)-1) |
Private tag class.
Use this value for indefinite length encoding.
#define QSC_ENCODING_BER_CLASS_APPLICATION 0x40U |
Application tag class.
The application tag class (0x40) is used for types that are defined by specific applications. These types are not necessarily universal; instead, they have meaning only within a particular application context. The two most significant bits are '01' (0x40 in hexadecimal).
#define QSC_ENCODING_BER_CLASS_CONTEXT_SPECIFIC 0x80U |
Context-specific tag class.
The context-specific tag class (0x80) is used for types whose interpretation depends on their context within a structure. These tags are typically used to differentiate between data elements that occur in the same position in different contexts. The two most significant bits are '10' (0x80 in hexadecimal).
#define QSC_ENCODING_BER_CLASS_PRIVATE 0xC0U |
Private tag class.
The private tag class (0xC0) is used for user-defined or vendor-specific types that do not have a standardized universal meaning. These tags are intended for private use. The two most significant bits are '11' (0xC0 in hexadecimal).
#define QSC_ENCODING_BER_CLASS_UNIVERSAL 0x00U |
Universal tag class.
The universal tag class (0x00) is used for ASN.1 types that are common and standardized, such as INTEGER, BOOLEAN, NULL, OBJECT IDENTIFIER, etc. The two most significant bits in the tag byte for a universal type are '00'.
typedef QSC_EXPORT_API struct qsc_encoding_ber_element qsc_encoding_ber_element |
Represents a BER (Basic Encoding Rules) element.
This structure is used to represent an ASN.1 element encoded using BER. An element may be either:
The structure holds the tag class, the constructed flag, and the tag number. For primitive elements, value and length are used to represent the element's data. For constructed elements, the children pointer is used (typically during decoding), although in some cases the pre-encoded block is placed in value along with its length.
Enumeration of the standard Universal ASN.1 tag numbers used in BER.
These tag numbers are defined in the ASN.1 specification and are used with BER (Basic Encoding Rules) to identify the type of an element.
QSC_EXPORT_API void encoding_ber_free_element | ( | qsc_encoding_ber_element * | element | ) |
Free a BER element from the array.
element | [qsc_encoding_ber_element*] Pointer to the BER element structure to be encoded. |
QSC_EXPORT_API bool qsc_encoding_base64_decode | ( | uint8_t * | output, |
size_t | otplen, | ||
const char * | input, | ||
size_t | inlen ) |
Decodes a Base64 string to a byte array.
output | [uint8_t*] The byte array receiving the decoded output. |
otplen | [size_t] The size of the output byte array. |
input | [const char*] The Base64 encoded input string. |
inlen | [size_t] The length of the input string. |
QSC_EXPORT_API size_t qsc_encoding_base64_decoded_size | ( | const char * | input, |
size_t | length ) |
Gets the expected size of an array required for Base64 decoding.
input | [const char*] The Base64 encoded string. |
length | [size_t] The length of the encoded string. |
QSC_EXPORT_API void qsc_encoding_base64_encode | ( | char * | output, |
size_t | otplen, | ||
const uint8_t * | input, | ||
size_t | inplen ) |
Encodes a byte array to a Base64 string.
output | [char*] The character string receiving the encoded bytes. |
otplen | [size_t] The size of the output character array. |
input | [const uint8_t*] The byte array to encode. |
inplen | [size_t] The size of the byte array. |
QSC_EXPORT_API size_t qsc_encoding_base64_encoded_size | ( | size_t | length | ) |
Gets the expected size of a character array required for Base64 encoding.
length | [size_t] The length of the input byte array. |
QSC_EXPORT_API bool qsc_encoding_base64_is_valid_char | ( | char | value | ) |
Tests if an encoded character is a valid Base64 encoding.
value | [char] The character to test. |
QSC_EXPORT_API qsc_encoding_ber_element * qsc_encoding_ber_decode_element | ( | const uint8_t * | buffer, |
size_t | buflen, | ||
size_t * | consumed ) |
Decodes a BER element from encoded data.
This function parses a BER-encoded element from the provided buffer. It decodes the tag, length, and value (or child elements in the case of constructed types) and returns a pointer to a dynamically allocated BER element structure that represents the decoded element.
buffer | [const uint8_t*] Pointer to the input buffer containing the BER-encoded element. |
buflen | [size_t] The number of bytes available in the input buffer. |
consumed | [size_t*] Pointer to a variable where the number of bytes consumed during decoding will be stored. |
QSC_EXPORT_API size_t qsc_encoding_ber_decode_length | ( | const uint8_t * | buffer, |
size_t | buflen, | ||
size_t * | length, | ||
bool * | indef ) |
Decodes a BER-encoded length value.
This function decodes a length field from BER-encoded data. It supports both the short and long forms of length encoding. If an indefinite length is encountered (indicated by a length byte of 0x80), the output parameter indefinite
is set to true.
buffer | [const uint8_t*] Pointer to the input buffer containing the BER-encoded length. |
buflen | [size_t] The number of bytes available in the input buffer. |
length | [size_t*] Pointer to a variable where the decoded length will be stored. |
indef | [bool*] Pointer to a boolean that will be set to true if the length is encoded in the indefinite form. |
QSC_EXPORT_API size_t qsc_encoding_ber_decode_tag | ( | const uint8_t * | buffer, |
size_t | buflen, | ||
uint8_t * | tagclass, | ||
bool * | construct, | ||
uint32_t * | tagnum ) |
Decodes an ASN.1 tag from BER-encoded data.
This function reads a BER-encoded tag from the provided buffer and decodes it into its constituent parts: tag class, constructed flag, and tag number. It handles both the short and long forms of tag encoding.
buffer | [const uint8_t*] Pointer to the input buffer containing the BER-encoded tag. |
buflen | [size_t] The number of bytes available in the input buffer. |
tagclass | [uint8_t*] Pointer to a variable where the decoded tag class will be stored. |
construct | [bool*] Pointer to a boolean where the decoded constructed flag will be stored. |
tagnum | [uint32_t*] Pointer to a variable where the decoded tag number will be stored. |
QSC_EXPORT_API size_t qsc_encoding_ber_encode_element | ( | qsc_encoding_ber_element * | element, |
uint8_t * | buffer, | ||
size_t | buflen ) |
Encodes a complete BER element.
This function encodes a BER element, including its tag, length, and value, into BER format. For constructed elements, this function handles encoding either a pre-encoded block (definite-length) or a series of child elements (possibly using indefinite-length encoding).
element | [qsc_encoding_ber_element*] Pointer to the BER element structure to be encoded. |
buffer | [uint8_t*] Pointer to the output buffer where the encoded element will be written. |
buflen | [size_t] The size of the output buffer. |
QSC_EXPORT_API size_t qsc_encoding_ber_encode_length | ( | size_t | length, |
uint8_t * | buffer, | ||
size_t | buflen ) |
Encodes a length value into BER format.
This function encodes the given length value according to BER rules. For lengths less than 128, a single byte (short form) is used. For longer lengths, the long-form encoding is applied, with the first byte indicating the number of subsequent bytes used to encode the length.
length | [size_t] The length value to encode. |
buffer | [uint8_t*] Pointer to the output buffer where the encoded length will be written. |
buflen | [size_t] The size of the output buffer. |
QSC_EXPORT_API size_t qsc_encoding_ber_encode_tag | ( | uint8_t | tagclass, |
bool | construct, | ||
uint32_t | tagnum, | ||
uint8_t * | buffer, | ||
size_t | buflen ) |
Encodes an ASN.1 tag into BER format.
This function constructs the BER representation of an ASN.1 tag given the tag class, constructed flag, and tag number. For tag numbers less than 31, the short form is used. For larger tag numbers, the long-form encoding is applied, encoding the tag number in base-128.
tagclass | [uint8_t] The tag class (e.g., BER_CLASS_UNIVERSAL, BER_CLASS_APPLICATION, BER_CLASS_CONTEXT_SPECIFIC, or BER_CLASS_PRIVATE). |
construct | [construct] Set to true if the element is constructed; false if primitive. |
tagnum | [uint32_t] The tag number. If tagnumber is 31 or greater, long-form encoding is used. |
buffer | [uint8_t*] Pointer to the output buffer where the encoded tag will be written. |
buflen | [size_t] The size of the output buffer. |
QSC_EXPORT_API qsc_encoding_ber_element * qsc_encoding_der_decode_element | ( | const uint8_t * | buffer, |
size_t | buflen, | ||
size_t * | consumed ) |
Decodes an ASN.1 element encoded in DER format.
This function calls the BER decode routine to decode an element and then checks that the length is encoded in the definite form (as required by DER). If the element uses an indefinite length, the function frees the element and returns NULL.
buffer | [const uint8_t*] Pointer to the DER-encoded data. |
buflen | [size_t] The number of bytes available in the buffer. |
consumed | [size_t*] Pointer to a variable that receives the number of bytes consumed. |
QSC_EXPORT_API size_t qsc_encoding_der_encode_element | ( | qsc_encoding_ber_element * | element, |
uint8_t * | buffer, | ||
size_t | buflen ) |
Encodes an ASN.1 element using DER (Distinguished Encoding Rules).
This function encodes the provided element in DER format. Unlike BER, DER requires definite-length encoding; therefore, if the element is marked as using an indefinite length, the encoding fails.
For primitive elements, the value is taken from element->value. For constructed elements, the function recursively encodes all child elements into a temporary buffer, computes the total content length, and then encodes the tag and definite-length followed by the content.
element | [qsc_encoding_ber_element*] Pointer to the element to be encoded. |
buffer | [uint8_t*] Pointer to the output buffer. |
buflen | [size_t] Size of the output buffer. |
QSC_EXPORT_API bool qsc_encoding_hex_decode | ( | const char * | input, |
size_t | inplen, | ||
uint8_t * | output, | ||
size_t | otplen, | ||
size_t * | declen ) |
Decodes a hexadecimal string into binary data.
input | [const char*] Pointer to the hex encoded string. |
inplen | [size_t] Length of the input string (should be even). |
output | [uint8_t*] Buffer to receive the decoded binary data. |
otplen | [size_t] Size of the output buffer. |
declen | [size_t*] Pointer to a size_t to receive the number of decoded bytes. |
QSC_EXPORT_API bool qsc_encoding_hex_encode | ( | const uint8_t * | input, |
size_t | inplen, | ||
char * | output, | ||
size_t | otplen ) |
Encodes binary data into a hexadecimal string.
input | [const uint8_t*] Pointer to the binary input data. |
inplen | [size_t] Number of bytes in the input data. |
output | [char*] Buffer to receive the hex encoded string; must be at least (in_len * 2) + 1 bytes long. |
otplen | [size_t] Length of the output buffer. |
QSC_EXPORT_API bool qsc_encoding_pem_decode | ( | const char * | input, |
uint8_t * | output, | ||
size_t | otplen, | ||
size_t * | declen ) |
Decodes a PEM-formatted string into binary data.
input | [const char*] A null-terminated PEM string (including header/footer). |
output | [uint8_t*] Buffer to receive decoded binary data. |
otplen | [size_t] Length of the output buffer. |
declen | [size_t*] Pointer to receive the number of decoded bytes. |
QSC_EXPORT_API bool qsc_encoding_pem_encode | ( | const char * | label, |
char * | output, | ||
size_t | otplen, | ||
const uint8_t * | data, | ||
size_t | datalen ) |
Encodes binary data in PEM format.
label | [const char*] The string label title. |
output | [char*] Buffer to receive the PEM text. |
otplen | [size_t] Length of the output buffer. |
data | [const uint8_t*] Pointer to the binary data to encode. |
datalen | [size_t] Length of the binary data. |