Next: , Previous: ASN.1 field functions, Up: Function reference


3.3 DER functions

asn1_length_der

— Function: void asn1_length_der (unsigned long int len, unsigned char * ans, int * ans_len)

len: value to convert.

ans: string returned.

ans_len: number of meaningful bytes of ANS (ans[0]..ans[ans_len-1]).

Creates the DER coding for the LEN parameter (only the length). The ans buffer is pre-allocated and must have room for the output.

asn1_octet_der

— Function: void asn1_octet_der (const unsigned char * str, int str_len, unsigned char * der, int * der_len)

str: OCTET string.

str_len: STR length (str[0]..str[str_len-1]).

der: string returned.

der_len: number of meaningful bytes of DER (der[0]..der[ans_len-1]).

Creates the DER coding for an OCTET type (length included).

asn1_bit_der

— Function: void asn1_bit_der (const unsigned char * str, int bit_len, unsigned char * der, int * der_len)

str: BIT string.

bit_len: number of meaningful bits in STR.

der: string returned.

der_len: number of meaningful bytes of DER (der[0]..der[ans_len-1]).

Creates the DER coding for a BIT STRING type (length and pad included).

asn1_der_coding

— Function: asn1_retCode asn1_der_coding (ASN1_TYPE element, const char * name, void * ider, int * len, char * ErrorDescription)

element: pointer to an ASN1 element

name: the name of the structure you want to encode (it must be inside *POINTER).

ider: vector that will contain the DER encoding. DER must be a pointer to memory cells already allocated.

len: number of bytes of *ider: ider[0]..ider[len-1], Initialy holds the sizeof of der vector.

Creates the DER encoding for the NAME structure (inside *POINTER structure).

Returns: ASN1_SUCCESS: DER encoding OK.

ASN1_ELEMENT_NOT_FOUND: NAME is not a valid element.

ASN1_VALUE_NOT_FOUND: There is an element without a value.

ASN1_MEM_ERROR: ider vector isn't big enough. Also in this case LEN will contain the length needed.

asn1_get_length_der

— Function: signed long asn1_get_length_der (const unsigned char * der, int der_len, int * len)

der: DER data to decode.

der_len: Length of DER data to decode.

len: Output variable containing the length of the DER length field.

Extract a length field from DER data.

Return value: Return the decoded length value, or -1 on indefinite length, or -2 when the value was too big.

asn1_get_tag_der

— Function: int asn1_get_tag_der (const unsigned char * der, int der_len, unsigned char * class, int * len, unsigned long * tag)

der: DER data to decode.

der_len: Length of DER data to decode.

class: Output variable containing decoded class.

len: Output variable containing the length of the DER TAG data.

tag: Output variable containing the decoded tag.

Decode the class and TAG from DER code.

Return value: Returns ASN1_SUCCESS on success, or an error.

asn1_get_octet_der

— Function: int asn1_get_octet_der (const unsigned char * der, int der_len, int * ret_len, unsigned char * str, int str_size, int * str_len)

der: DER data to decode containing the OCTET SEQUENCE.

der_len: Length of DER data to decode.

ret_len: Output variable containing the length of the DER data.

str: Pre-allocated output buffer to put decoded OCTET SEQUENCE in.

str_size: Length of pre-allocated output buffer.

str_len: Output variable containing the length of the OCTET SEQUENCE.

Extract an OCTET SEQUENCE from DER data.

Return value: Returns ASN1_SUCCESS on success, or an error.

asn1_get_bit_der

— Function: int asn1_get_bit_der (const unsigned char * der, int der_len, int * ret_len, unsigned char * str, int str_size, int * bit_len)

der: DER data to decode containing the BIT SEQUENCE.

der_len: Length of DER data to decode.

ret_len: Output variable containing the length of the DER data.

str: Pre-allocated output buffer to put decoded BIT SEQUENCE in.

str_size: Length of pre-allocated output buffer.

bit_len: Output variable containing the size of the BIT SEQUENCE.

Extract a BIT SEQUENCE from DER data.

Return value: Return ASN1_SUCCESS on success, or an error.

asn1_der_decoding

— Function: asn1_retCode asn1_der_decoding (ASN1_TYPE * element, const void * ider, int len, char * errorDescription)

element: pointer to an ASN1 structure.

ider: vector that contains the DER encoding.

len: number of bytes of *ider: ider[0]..ider[len-1].

errorDescription: null-terminated string contains details when an error occurred.

Fill the structure *ELEMENT with values of a DER encoding string. The sructure must just be created with function 'create_stucture'. If an error occurs during the decoding procedure, the *ELEMENT is deleted and set equal to ASN1_TYPE_EMPTY.

Returns: ASN1_SUCCESS: DER encoding OK.

ASN1_ELEMENT_NOT_FOUND: ELEMENT is ASN1_TYPE_EMPTY.

ASN1_TAG_ERROR,ASN1_DER_ERROR: The der encoding doesn't match the structure NAME. *ELEMENT deleted.

asn1_der_decoding_element

— Function: asn1_retCode asn1_der_decoding_element (ASN1_TYPE * structure, const char * elementName, const void * ider, int len, char * errorDescription)

structure: pointer to an ASN1 structure

elementName: name of the element to fill

ider: vector that contains the DER encoding of the whole structure.

len: number of bytes of *der: der[0]..der[len-1]

errorDescription: null-terminated string contains details when an error occurred.

Fill the element named ELEMENTNAME with values of a DER encoding string. The sructure must just be created with function 'create_stucture'. The DER vector must contain the encoding string of the whole STRUCTURE. If an error occurs during the decoding procedure, the *STRUCTURE is deleted and set equal to ASN1_TYPE_EMPTY.

Returns: ASN1_SUCCESS: DER encoding OK.

ASN1_ELEMENT_NOT_FOUND: ELEMENT is ASN1_TYPE_EMPTY or elementName == NULL.

ASN1_TAG_ERROR,ASN1_DER_ERROR: The der encoding doesn't match the structure STRUCTURE. *ELEMENT deleted.

asn1_der_decoding_startEnd

— Function: asn1_retCode asn1_der_decoding_startEnd (ASN1_TYPE element, const void * ider, int len, const char * name_element, int * start, int * end)

element: pointer to an ASN1 element

ider: vector that contains the DER encoding.

len: number of bytes of *ider: ider[0]..ider[len-1]

name_element: an element of NAME structure.

start: the position of the first byte of NAME_ELEMENT decoding (ider[*start])

end: the position of the last byte of NAME_ELEMENT decoding (ider[*end])

Find the start and end point of an element in a DER encoding string. I mean that if you have a der encoding and you have already used the function "asn1_der_decoding" to fill a structure, it may happen that you want to find the piece of string concerning an element of the structure.

Example: the sequence "tbsCertificate" inside an X509 certificate.

Returns: ASN1_SUCCESS: DER encoding OK.

ASN1_ELEMENT_NOT_FOUND: ELEMENT is ASN1_TYPE EMPTY or NAME_ELEMENT is not a valid element.

ASN1_TAG_ERROR,ASN1_DER_ERROR: the der encoding doesn't match the structure ELEMENT.

asn1_expand_any_defined_by

— Function: asn1_retCode asn1_expand_any_defined_by (ASN1_TYPE definitions, ASN1_TYPE * element)

definitions: ASN1 definitions

element: pointer to an ASN1 structure

Expands every "ANY DEFINED BY" element of a structure created from a DER decoding process (asn1_der_decoding function). The element ANY must be defined by an OBJECT IDENTIFIER. The type used to expand the element ANY is the first one following the definition of the actual value of the OBJECT IDENTIFIER.

Returns: ASN1_SUCCESS: Substitution OK.

ASN1_ERROR_TYPE_ANY: Some "ANY DEFINED BY" element couldn't be expanded due to a problem in OBJECT_ID -> TYPE association.

other errors: Result of der decoding process.

asn1_expand_octet_string

— Function: asn1_retCode asn1_expand_octet_string (ASN1_TYPE definitions, ASN1_TYPE * element, const char * octetName, const char * objectName)

definitions: ASN1 definitions

element: pointer to an ASN1 structure

octetName: name of the OCTECT STRING field to expand.

objectName: name of the OBJECT IDENTIFIER field to use to define the type for expansion.

Expands an "OCTET STRING" element of a structure created from a DER decoding process (asn1_der_decoding function). The type used for expansion is the first one following the definition of the actual value of the OBJECT IDENTIFIER indicated by OBJECTNAME.

Returns: ASN1_SUCCESS: Substitution OK.

ASN1_ELEMENT_NOT_FOUND: OBJECTNAME or OCTETNAME are not correct.

ASN1_VALUE_NOT_VALID: Wasn't possible to find the type to use for expansion.

other errors: result of der decoding process.