SharkSSL C/C++ Reference
TLS, cryptography, certificates, and PSA integration
ASN.1 encoding and parsing

Detailed Description

Bounded representation helpers; parsing does not establish certificate trust.

Files

file  SharkSslASN1.h
 ASN.1 macros.
 

Functions

int SharkSslParseASN1_getLength (SharkSslParseASN1 *o)
 Read an ASN.1 length and advance past its encoding. More...
 
SHARKSSL_API int SharkSslParseASN1_getSetSeq (SharkSslParseASN1 *o, U8 ASN1id)
 Consume an expected tag and its length, leaving the cursor at its content. More...
 
SHARKSSL_API int SharkSslParseASN1_getType (SharkSslParseASN1 *o, U8 ASN1type)
 Read one tagged value and expose its borrowed content in dataptr/datalen. More...
 
int SharkSslParseASN1_getContextSpecific (SharkSslParseASN1 *o, U8 *tag)
 Read a context-specific value and advance past it. More...
 
int SharkSslParseASN1_parseCertSignatureAlgorithm (SharkSslParseASN1 *o)
 Consume a certificate signature AlgorithmIdentifier. More...
 
SHARKSSL_API U8 SharkSslParseASN1_getAlgoID (const SharkSslParseASN1 *o)
 Map an already parsed OID to a SharkSSL algorithm identifier. More...
 
SHARKSSL_API U8 SharkSslParseASN1_getCurveID (const SharkSslParseASN1 *o)
 Map an already parsed OID to a SharkSSL curve identifier. More...
 
SHARKSSL_API int sharkssl_PEM_getSeqVersion (SharkSslParseASN1 *parseKey, U32 len)
 Enter an ASN.1 key sequence and read its one-byte version. More...
 
SHARKSSL_API void SharkSslASN1Create_constructor (SharkSslASN1Create *o, U8 *buf, int bufSize)
 Initialize an encoder that writes backward from the end of a buffer. More...
 
SHARKSSL_API int SharkSslASN1Create_length (SharkSslASN1Create *o, int len)
 Prepend a definite ASN.1 length. More...
 
SHARKSSL_API int SharkSslASN1Create_tag (SharkSslASN1Create *o, U8 ASN1type)
 Prepend a one-byte ASN.1 tag. More...
 
SHARKSSL_API int SharkSslASN1Create_int (SharkSslASN1Create *o, const U8 *intptr, int len)
 Prepend a nonnegative ASN.1 INTEGER. More...
 
SHARKSSL_API int SharkSslASN1Create_raw (SharkSslASN1Create *o, const void *data, int size)
 Prepend raw bytes without a tag or length. More...
 
SHARKSSL_API int SharkSslASN1Create_oid (SharkSslASN1Create *o, const U8 *oid, int oidLen)
 Prepend an ASN.1 OBJECT IDENTIFIER. More...
 
SHARKSSL_API int SharkSslASN1Create_email (SharkSslASN1Create *o, const U8 *oid, int oidLen, const U8 *email, int emailLen)
 Prepend a distinguished-name attribute using IA5String. More...
 
SHARKSSL_API int SharkSslASN1Create_name (SharkSslASN1Create *o, const U8 *oid, int oidLen, const U8 *name, int nameLen)
 Prepend a distinguished-name attribute using PrintableString. More...
 
SHARKSSL_API int SharkSslASN1Create_boolean (SharkSslASN1Create *o, U8 boolean)
 Prepend an ASN.1 BOOLEAN. More...
 

Function Documentation

◆ sharkssl_PEM_getSeqVersion()

int sharkssl_PEM_getSeqVersion ( SharkSslParseASN1 *  parseKey,
U32  len 
)

Enter an ASN.1 key sequence and read its one-byte version.

Requires the PEM API implementation; it does not validate the remaining key fields.

Parameters
parseKeySharkSslParseASN1 *; Required initialized cursor over a borrowed bounded input buffer. Updated in place; discard cursor state after a parse failure.
lenU32; Maximum accepted sequence content length in bytes.
Returns
int: Version byte on success; SHARKSSL_PEM_KEY_PARSE_ERROR on failure.

◆ SharkSslASN1Create_boolean()

int SharkSslASN1Create_boolean ( SharkSslASN1Create *  o,
U8  boolean 
)

Prepend an ASN.1 BOOLEAN.

Requires CSR signing.

Parameters
oSharkSslASN1Create *; Required initialized reverse encoder; output buffer remains caller-owned. On failure discard the partial encoding.
booleanU8; Zero for FALSE; any nonzero value for TRUE.
Returns
int: 0 on success; nonzero on capacity failure.

◆ SharkSslASN1Create_constructor()

void SharkSslASN1Create_constructor ( SharkSslASN1Create *  o,
U8 *  buf,
int  bufSize 
)

Initialize an encoder that writes backward from the end of a buffer.

No allocation is performed. Retrieve the completed bytes with SharkSslASN1Create_getData and SharkSslASN1Create_getLen.

Parameters
oSharkSslASN1Create *; Required uninitialized encoder storage, owned by the caller.
bufU8 *; Required writable caller-owned buffer, retained until encoding ends.
bufSizeint; Nonnegative buffer capacity in bytes.

◆ SharkSslASN1Create_email()

int SharkSslASN1Create_email ( SharkSslASN1Create *  o,
const U8 *  oid,
int  oidLen,
const U8 *  email,
int  emailLen 
)

Prepend a distinguished-name attribute using IA5String.

Writes a SET containing the OID/value SEQUENCE. Requires CSR creation or signing.

Parameters
oSharkSslASN1Create *; Required initialized reverse encoder; output buffer remains caller-owned. On failure discard the partial encoding.
oidconst U8 *; Required encoded attribute OID content, borrowed for the call.
oidLenint; OID content length in bytes.
emailconst U8 *; Required attribute bytes, borrowed for the call; the caller ensures they fit the selected string type.
emailLenint; Nonnegative attribute length in bytes, excluding any terminator.
Returns
int: 0 on success; nonzero on encoding failure.

◆ SharkSslASN1Create_int()

int SharkSslASN1Create_int ( SharkSslASN1Create *  o,
const U8 *  intptr,
int  len 
)

Prepend a nonnegative ASN.1 INTEGER.

Leading zeros are removed and a sign-protecting zero is inserted when necessary. Negative integers are not supported.

Parameters
oSharkSslASN1Create *; Required initialized reverse encoder; output buffer remains caller-owned. On failure discard the partial encoding.
intptrconst U8 *; Big-endian unsigned magnitude, borrowed for the call; required for nonzero len.
lenint; Magnitude length in bytes, 0 through 32767; provide at least one byte for a DER INTEGER value.
Returns
int: 0 on success; nonzero on length or capacity failure.

◆ SharkSslASN1Create_length()

int SharkSslASN1Create_length ( SharkSslASN1Create *  o,
int  len 
)

Prepend a definite ASN.1 length.

Parameters
oSharkSslASN1Create *; Required initialized reverse encoder; output buffer remains caller-owned. On failure discard the partial encoding.
lenint; Content length in bytes, from 0 through 65535.
Returns
int: 0 on success; -1 for insufficient room or a length above 65535.

◆ SharkSslASN1Create_name()

int SharkSslASN1Create_name ( SharkSslASN1Create *  o,
const U8 *  oid,
int  oidLen,
const U8 *  name,
int  nameLen 
)

Prepend a distinguished-name attribute using PrintableString.

Writes a SET containing the OID/value SEQUENCE. Requires CSR creation or signing.

Parameters
oSharkSslASN1Create *; Required initialized reverse encoder; output buffer remains caller-owned. On failure discard the partial encoding.
oidconst U8 *; Required encoded attribute OID content, borrowed for the call.
oidLenint; OID content length in bytes.
nameconst U8 *; Required attribute bytes, borrowed for the call; the caller ensures they fit the selected string type.
nameLenint; Nonnegative attribute length in bytes, excluding any terminator.
Returns
int: 0 on success; nonzero on encoding failure.

◆ SharkSslASN1Create_oid()

int SharkSslASN1Create_oid ( SharkSslASN1Create *  o,
const U8 *  oid,
int  oidLen 
)

Prepend an ASN.1 OBJECT IDENTIFIER.

Available with CSR creation, CSR signing or ASN.1 key creation.

Parameters
oSharkSslASN1Create *; Required initialized reverse encoder; output buffer remains caller-owned. On failure discard the partial encoding.
oidconst U8 *; Required encoded OID content bytes, without tag/length; borrowed for the call.
oidLenint; Nonnegative OID content length in bytes.
Returns
int: 0 on success; nonzero on encoding failure.

◆ SharkSslASN1Create_raw()

int SharkSslASN1Create_raw ( SharkSslASN1Create *  o,
const void *  data,
int  size 
)

Prepend raw bytes without a tag or length.

Available with CSR creation, CSR signing or ASN.1 key creation.

Parameters
oSharkSslASN1Create *; Required initialized reverse encoder; output buffer remains caller-owned. On failure discard the partial encoding.
dataconst void *; Required readable byte buffer, borrowed for the call; must not overlap the destination.
sizeint; Nonnegative number of bytes to copy.
Returns
int: 0 on success; -1 for insufficient capacity.

◆ SharkSslASN1Create_tag()

int SharkSslASN1Create_tag ( SharkSslASN1Create *  o,
U8  ASN1type 
)

Prepend a one-byte ASN.1 tag.

Parameters
oSharkSslASN1Create *; Required initialized reverse encoder; output buffer remains caller-owned. On failure discard the partial encoding.
ASN1typeU8; One-byte tag including class and constructed bits.
Returns
int: 0 on success; -1 when the buffer has no room.

◆ SharkSslParseASN1_getAlgoID()

U8 SharkSslParseASN1_getAlgoID ( const SharkSslParseASN1 *  o)

Map an already parsed OID to a SharkSSL algorithm identifier.

Parameters
oconst SharkSslParseASN1 *; Required cursor whose dataptr/datalen describe the OID content; borrowed and unchanged.
Returns
U8: Recognized enabled algorithm identifier, or 0 when no mapping exists.

◆ SharkSslParseASN1_getContextSpecific()

int SharkSslParseASN1_getContextSpecific ( SharkSslParseASN1 *  o,
U8 *  tag 
)

Read a context-specific value and advance past it.

Parameters
oSharkSslParseASN1 *; Required initialized cursor over a borrowed bounded input buffer. Updated in place; discard cursor state after a parse failure.
tagU8 *; Required output for the tag with the context-specific class bit removed; the constructed bit is retained.
Returns
int: 0 on success; -1 on class or length failure. Content is borrowed through o->dataptr/datalen.

◆ SharkSslParseASN1_getCurveID()

U8 SharkSslParseASN1_getCurveID ( const SharkSslParseASN1 *  o)

Map an already parsed OID to a SharkSSL curve identifier.

Parameters
oconst SharkSslParseASN1 *; Required cursor whose dataptr/datalen describe the OID content; borrowed and unchanged.
Returns
U8: Recognized enabled curve identifier, or 0 when no mapping exists.

◆ SharkSslParseASN1_getLength()

int SharkSslParseASN1_getLength ( SharkSslParseASN1 *  o)

Read an ASN.1 length and advance past its encoding.

Parameters
oSharkSslParseASN1 *; Required initialized cursor over a borrowed bounded input buffer. Updated in place; discard cursor state after a parse failure.
Returns
int: Content length in bytes, or -1 when the encoding or remaining buffer is unacceptable. This helper does not authenticate or fully validate a DER object.

◆ SharkSslParseASN1_getSetSeq()

int SharkSslParseASN1_getSetSeq ( SharkSslParseASN1 *  o,
U8  ASN1id 
)

Consume an expected tag and its length, leaving the cursor at its content.

Parameters
oSharkSslParseASN1 *; Required initialized cursor over a borrowed bounded input buffer. Updated in place; discard cursor state after a parse failure.
ASN1idU8; Expected one-byte tag, including class and constructed bits.
Returns
int: Content byte count, or -1 on tag/length failure.

◆ SharkSslParseASN1_getType()

int SharkSslParseASN1_getType ( SharkSslParseASN1 *  o,
U8  ASN1type 
)

Read one tagged value and expose its borrowed content in dataptr/datalen.

Advances past the content. INTEGER removes one leading zero where applicable; an OID also consumes an immediately following NULL parameter. This is a representation helper, not certificate validation.

Parameters
oSharkSslParseASN1 *; Required initialized cursor over a borrowed bounded input buffer. Updated in place; discard cursor state after a parse failure.
ASN1typeU8; Expected one-byte ASN.1 tag.
Returns
int: 0 on success; -1 on parse failure.

◆ SharkSslParseASN1_parseCertSignatureAlgorithm()

int SharkSslParseASN1_parseCertSignatureAlgorithm ( SharkSslParseASN1 *  o)

Consume a certificate signature AlgorithmIdentifier.

RSA-PSS accepts matching supported hash/MGF1 hash, a digest-length salt and trailer field 1. Parsing does not verify a signature.

Parameters
oSharkSslParseASN1 *; Required initialized cursor over a borrowed bounded input buffer. Updated in place; discard cursor state after a parse failure.
Returns
int: Internal combined signature/hash identifier on success; -1 for malformed or unsupported parameters.