asn1write.h
mbedtls/config.h
mbedtls/asn1.h
mbedtls/asn1.h
asn1write.h
mbedtls/bignum.h
mbedtls/check_config.h
mbedtls/config.h
limits.h
stddef.h
stdint.h
stdio.h
MBEDTLS_ASN1_CHK_ADD
g
f
do \ { \ if ((ret = (f)) < 0) \ return ret; \ else \ (g) += ret; \ } while (0)
int int mbedtls_asn1_write_len (unsigned char **p, unsigned char *start, size_t len) mbedtls_asn1_write_len unsigned char ** p unsigned char * start size_t len

Write a length field in ASN.1 format.

This function works backwards in data buffer.

p

The reference to the current position pointer.

start

The start of the buffer, for bounds-checking.

len

The length value to write.

The number of bytes written to `p` on success.

A negative `MBEDTLS_ERR_ASN1_XXX` error code on failure.

int int mbedtls_asn1_write_tag (unsigned char **p, unsigned char *start, unsigned char tag) mbedtls_asn1_write_tag unsigned char ** p unsigned char * start unsigned char tag

Write an ASN.1 tag in ASN.1 format.

This function works backwards in data buffer.

p

The reference to the current position pointer.

start

The start of the buffer, for bounds-checking.

tag

The tag to write.

The number of bytes written to `p` on success.

A negative `MBEDTLS_ERR_ASN1_XXX` error code on failure.

int int mbedtls_asn1_write_raw_buffer (unsigned char **p, unsigned char *start, const unsigned char *buf, size_t size) mbedtls_asn1_write_raw_buffer unsigned char ** p unsigned char * start const unsigned char * buf size_t size

Write raw buffer data.

This function works backwards in data buffer.

p

The reference to the current position pointer.

start

The start of the buffer, for bounds-checking.

buf

The data buffer to write.

size

The length of the data buffer.

The number of bytes written to `p` on success.

A negative `MBEDTLS_ERR_ASN1_XXX` error code on failure.

int int mbedtls_asn1_write_mpi (unsigned char **p, unsigned char *start, const mbedtls_mpi *X) mbedtls_asn1_write_mpi unsigned char ** p unsigned char * start const mbedtls_mpi * X

Write an arbitrary-precision number (MBEDTLS_ASN1_INTEGER) in ASN.1 format.

This function works backwards in data buffer.

p

The reference to the current position pointer.

start

The start of the buffer, for bounds-checking.

X

The MPI to write. It must be non-negative.

The number of bytes written to `p` on success.

A negative `MBEDTLS_ERR_ASN1_XXX` error code on failure.

int int mbedtls_asn1_write_null (unsigned char **p, unsigned char *start) mbedtls_asn1_write_null unsigned char ** p unsigned char * start

Write a NULL tag (MBEDTLS_ASN1_NULL) with zero data in ASN.1 format.

This function works backwards in data buffer.

p

The reference to the current position pointer.

start

The start of the buffer, for bounds-checking.

The number of bytes written to `p` on success.

A negative `MBEDTLS_ERR_ASN1_XXX` error code on failure.

int int mbedtls_asn1_write_oid (unsigned char **p, unsigned char *start, const char *oid, size_t oid_len) mbedtls_asn1_write_oid unsigned char ** p unsigned char * start const char * oid size_t oid_len

Write an OID tag (MBEDTLS_ASN1_OID) and data in ASN.1 format.

This function works backwards in data buffer.

p

The reference to the current position pointer.

start

The start of the buffer, for bounds-checking.

oid

The OID to write.

oid_len

The length of the OID.

The number of bytes written to `p` on success.

A negative `MBEDTLS_ERR_ASN1_XXX` error code on failure.

int int mbedtls_asn1_write_algorithm_identifier (unsigned char **p, unsigned char *start, const char *oid, size_t oid_len, size_t par_len) mbedtls_asn1_write_algorithm_identifier unsigned char ** p unsigned char * start const char * oid size_t oid_len size_t par_len

Write an AlgorithmIdentifier sequence in ASN.1 format.

This function works backwards in data buffer.

p

The reference to the current position pointer.

start

The start of the buffer, for bounds-checking.

oid

The OID of the algorithm to write.

oid_len

The length of the algorithm's OID.

par_len

The length of the parameters, which must be already written. If 0, NULL parameters are added

The number of bytes written to `p` on success.

A negative `MBEDTLS_ERR_ASN1_XXX` error code on failure.

int int mbedtls_asn1_write_algorithm_identifier_ext (unsigned char **p, unsigned char *start, const char *oid, size_t oid_len, size_t par_len, int has_par) mbedtls_asn1_write_algorithm_identifier_ext unsigned char ** p unsigned char * start const char * oid size_t oid_len size_t par_len int has_par

Write an AlgorithmIdentifier sequence in ASN.1 format.

This function works backwards in data buffer.

p

The reference to the current position pointer.

start

The start of the buffer, for bounds-checking.

oid

The OID of the algorithm to write.

oid_len

The length of the algorithm's OID.

par_len

The length of the parameters, which must be already written.

has_par

If there are any parameters. If 0, par_len must be 0. If 1 and `par_len` is 0, NULL parameters are added.

The number of bytes written to `p` on success.

A negative `MBEDTLS_ERR_ASN1_XXX` error code on failure.

int int mbedtls_asn1_write_bool (unsigned char **p, unsigned char *start, int boolean) mbedtls_asn1_write_bool unsigned char ** p unsigned char * start int boolean

Write a boolean tag (MBEDTLS_ASN1_BOOLEAN) and value in ASN.1 format.

This function works backwards in data buffer.

p

The reference to the current position pointer.

start

The start of the buffer, for bounds-checking.

boolean

The boolean value to write, either `0` or `1`.

The number of bytes written to `p` on success.

A negative `MBEDTLS_ERR_ASN1_XXX` error code on failure.

int int mbedtls_asn1_write_int (unsigned char **p, unsigned char *start, int val) mbedtls_asn1_write_int unsigned char ** p unsigned char * start int val

Write an int tag (MBEDTLS_ASN1_INTEGER) and value in ASN.1 format.

This function works backwards in data buffer.

p

The reference to the current position pointer.

start

The start of the buffer, for bounds-checking.

val

The integer value to write. It must be non-negative.

The number of bytes written to `p` on success.

A negative `MBEDTLS_ERR_ASN1_XXX` error code on failure.

int int mbedtls_asn1_write_enum (unsigned char **p, unsigned char *start, int val) mbedtls_asn1_write_enum unsigned char ** p unsigned char * start int val

Write an enum tag (MBEDTLS_ASN1_ENUMERATED) and value in ASN.1 format.

This function works backwards in data buffer.

p

The reference to the current position pointer.

start

The start of the buffer, for bounds-checking.

val

The integer value to write.

The number of bytes written to `p` on success.

A negative `MBEDTLS_ERR_ASN1_XXX` error code on failure.

int int mbedtls_asn1_write_tagged_string (unsigned char **p, unsigned char *start, int tag, const char *text, size_t text_len) mbedtls_asn1_write_tagged_string unsigned char ** p unsigned char * start int tag const char * text size_t text_len

Write a string in ASN.1 format using a specific string encoding tag.

This function works backwards in data buffer.

p

The reference to the current position pointer.

start

The start of the buffer, for bounds-checking.

tag

The string encoding tag to write, e.g. MBEDTLS_ASN1_UTF8_STRING.

text

The string to write.

text_len

The length of `text` in bytes (which might be strictly larger than the number of characters).

The number of bytes written to `p` on success.

A negative error code on failure.

int int mbedtls_asn1_write_printable_string (unsigned char **p, unsigned char *start, const char *text, size_t text_len) mbedtls_asn1_write_printable_string unsigned char ** p unsigned char * start const char * text size_t text_len

Write a string in ASN.1 format using the PrintableString string encoding tag (MBEDTLS_ASN1_PRINTABLE_STRING).

This function works backwards in data buffer.

p

The reference to the current position pointer.

start

The start of the buffer, for bounds-checking.

text

The string to write.

text_len

The length of `text` in bytes (which might be strictly larger than the number of characters).

The number of bytes written to `p` on success.

A negative error code on failure.

int int mbedtls_asn1_write_utf8_string (unsigned char **p, unsigned char *start, const char *text, size_t text_len) mbedtls_asn1_write_utf8_string unsigned char ** p unsigned char * start const char * text size_t text_len

Write a UTF8 string in ASN.1 format using the UTF8String string encoding tag (MBEDTLS_ASN1_UTF8_STRING).

This function works backwards in data buffer.

p

The reference to the current position pointer.

start

The start of the buffer, for bounds-checking.

text

The string to write.

text_len

The length of `text` in bytes (which might be strictly larger than the number of characters).

The number of bytes written to `p` on success.

A negative error code on failure.

int int mbedtls_asn1_write_ia5_string (unsigned char **p, unsigned char *start, const char *text, size_t text_len) mbedtls_asn1_write_ia5_string unsigned char ** p unsigned char * start const char * text size_t text_len

Write a string in ASN.1 format using the IA5String string encoding tag (MBEDTLS_ASN1_IA5_STRING).

This function works backwards in data buffer.

p

The reference to the current position pointer.

start

The start of the buffer, for bounds-checking.

text

The string to write.

text_len

The length of `text` in bytes (which might be strictly larger than the number of characters).

The number of bytes written to `p` on success.

A negative error code on failure.

int int mbedtls_asn1_write_bitstring (unsigned char **p, unsigned char *start, const unsigned char *buf, size_t bits) mbedtls_asn1_write_bitstring unsigned char ** p unsigned char * start const unsigned char * buf size_t bits

Write a bitstring tag (MBEDTLS_ASN1_BIT_STRING) and value in ASN.1 format.

This function works backwards in data buffer.

p

The reference to the current position pointer.

start

The start of the buffer, for bounds-checking.

buf

The bitstring to write.

bits

The total number of bits in the bitstring.

The number of bytes written to `p` on success.

A negative error code on failure.

int int mbedtls_asn1_write_named_bitstring (unsigned char **p, unsigned char *start, const unsigned char *buf, size_t bits) mbedtls_asn1_write_named_bitstring unsigned char ** p unsigned char * start const unsigned char * buf size_t bits

This function writes a named bitstring tag (MBEDTLS_ASN1_BIT_STRING) and value in ASN.1 format.

As stated in RFC 5280 Appendix B, trailing zeroes are omitted when encoding named bitstrings in DER.

This function works backwards within the data buffer.

p

The reference to the current position pointer.

start

The start of the buffer which is used for bounds-checking.

buf

The bitstring to write.

bits

The total number of bits in the bitstring.

The number of bytes written to `p` on success.

A negative error code on failure.

int int mbedtls_asn1_write_octet_string (unsigned char **p, unsigned char *start, const unsigned char *buf, size_t size) mbedtls_asn1_write_octet_string unsigned char ** p unsigned char * start const unsigned char * buf size_t size

Write an octet string tag (MBEDTLS_ASN1_OCTET_STRING) and value in ASN.1 format.

This function works backwards in data buffer.

p

The reference to the current position pointer.

start

The start of the buffer, for bounds-checking.

buf

The buffer holding the data to write.

size

The length of the data buffer `buf`.

The number of bytes written to `p` on success.

A negative error code on failure.

mbedtls_asn1_named_data * mbedtls_asn1_named_data * mbedtls_asn1_store_named_data (mbedtls_asn1_named_data **list, const char *oid, size_t oid_len, const unsigned char *val, size_t val_len) mbedtls_asn1_store_named_data mbedtls_asn1_named_data ** list const char * oid size_t oid_len const unsigned char * val size_t val_len

Create or find a specific named_data entry for writing in a sequence or list based on the OID. If not already in there, a new entry is added to the head of the list. Warning: Destructive behaviour for the val data!

list

The pointer to the location of the head of the list to seek through (will be updated in case of a new entry).

oid

The OID to look for.

oid_len

The size of the OID.

val

The associated data to store. If this is `NULL`, no data is copied to the new or existing buffer.

val_len

The minimum length of the data buffer needed. If this is 0, do not allocate a buffer for the associated data. If the OID was already present, enlarge, shrink or free the existing buffer to fit `val_len`.

A pointer to the new / existing entry on success.

`NULL` if if there was a memory allocation error.

ASN.1 buffer writing functionality.

    /*
    *CopyrightTheMbedTLSContributors
    *SPDX-License-Identifier:Apache-2.0ORGPL-2.0-or-later
    */
    #ifndefMBEDTLS_ASN1_WRITE_H
    #defineMBEDTLS_ASN1_WRITE_H

    #if!defined(MBEDTLS_CONFIG_FILE)
    #include"mbedtls/config.h"
    #else
    #includeMBEDTLS_CONFIG_FILE
    #endif

    #include"mbedtls/asn1.h"

    #defineMBEDTLS_ASN1_CHK_ADD(g,f)\
    do\
    {\
    if((ret=(f))<0)\
    returnret;\
    else\
    (g)+=ret;\
    }while(0)

    #ifdef__cplusplus
    extern"C"{
    #endif

    intmbedtls_asn1_write_len(unsignedchar**p,unsignedchar*start,
    size_tlen);
    intmbedtls_asn1_write_tag(unsignedchar**p,unsignedchar*start,
    unsignedchartag);

    intmbedtls_asn1_write_raw_buffer(unsignedchar**p,unsignedchar*start,
    constunsignedchar*buf,size_tsize);

    #ifdefined(MBEDTLS_BIGNUM_C)
    intmbedtls_asn1_write_mpi(unsignedchar**p,unsignedchar*start,
    constmbedtls_mpi*X);
    #endif/*MBEDTLS_BIGNUM_C*/

    intmbedtls_asn1_write_null(unsignedchar**p,unsignedchar*start);

    intmbedtls_asn1_write_oid(unsignedchar**p,unsignedchar*start,
    constchar*oid,size_toid_len);

    intmbedtls_asn1_write_algorithm_identifier(unsignedchar**p,
    unsignedchar*start,
    constchar*oid,size_toid_len,
    size_tpar_len);

    intmbedtls_asn1_write_algorithm_identifier_ext(unsignedchar**p,
    unsignedchar*start,
    constchar*oid,size_toid_len,
    size_tpar_len,inthas_par);

    intmbedtls_asn1_write_bool(unsignedchar**p,unsignedchar*start,
    intboolean);

    intmbedtls_asn1_write_int(unsignedchar**p,unsignedchar*start,intval);

    intmbedtls_asn1_write_enum(unsignedchar**p,unsignedchar*start,intval);

    intmbedtls_asn1_write_tagged_string(unsignedchar**p,unsignedchar*start,
    inttag,constchar*text,
    size_ttext_len);

    intmbedtls_asn1_write_printable_string(unsignedchar**p,
    unsignedchar*start,
    constchar*text,size_ttext_len);

    intmbedtls_asn1_write_utf8_string(unsignedchar**p,unsignedchar*start,
    constchar*text,size_ttext_len);

    intmbedtls_asn1_write_ia5_string(unsignedchar**p,unsignedchar*start,
    constchar*text,size_ttext_len);

    intmbedtls_asn1_write_bitstring(unsignedchar**p,unsignedchar*start,
    constunsignedchar*buf,size_tbits);

    intmbedtls_asn1_write_named_bitstring(unsignedchar**p,
    unsignedchar*start,
    constunsignedchar*buf,
    size_tbits);

    intmbedtls_asn1_write_octet_string(unsignedchar**p,unsignedchar*start,
    constunsignedchar*buf,size_tsize);

    mbedtls_asn1_named_data*mbedtls_asn1_store_named_data(mbedtls_asn1_named_data**list,
    constchar*oid,size_toid_len,
    constunsignedchar*val,
    size_tval_len);

    #ifdef__cplusplus
    }
    #endif

    #endif/*MBEDTLS_ASN1_WRITE_H*/