i2a_ASN1_INTEGER() -- SSLeay 0.9.0b -- January 1999

NAME

i2a_ASN1_INTEGER, a2i_ASN1_INTEGER, ASN1_INTEGER_set,
ASN1_INTEGER_get, BN_to_ASN1_INTEGER,
ASN1_INTEGER_to_BN -- ASN1 integer manipulation routines

SYNOPSIS

#include "asn1.h"

int i2a_ASN1_INTEGER(bp, a)
BIO *bp;
ASN1_INTEGER *a;

int a2i_ASN1_INTEGER(bp,bs,buf,size)
BIO *bp;
ASN1_INTEGER *bs;
char *buf;
int size;

int ASN1_INTEGER_set(a,v)
ASN1_INTEGER *a;
long v;

long ASN1_INTEGER_get(a)
ASN1_INTEGER *a;

ASN1_INTEGER *BN_to_ASN1_INTEGER(bn,ai)
BIGNUM *bn;
ASN1_INTEGER *ai;

BIGNUM *ASN1_INTEGER_to_BN(ai,bn)
ASN1_INTEGER *ai;
BIGNUM *bn;

DESCRIPTION

These functions all operate on an ASN1_INTEGER, which is really an ASN1_STRING. See ASN.1 Types and Structures for the definition of the structure.

i2a_ASN1_INTEGER() writes out to BIO output file bp the data element of the ASN1_INTEGER that a points to in hexadecimal notation, breaking up the integer into lines of 70 characters each and a continuation marker '\' at the end of each line, if the integer is that long.

Upon success, this function returns the number of bytes written; on error, it returns -1.

a2i_ASN1_INTEGER() reads from the BIO input file bp an integer which has been written in hexadecimal notation with '\' as a continuation marker if the integer spans more than one line. It then stores the integer in an ASN1_INTEGER structure. buf is the buffer used in conjunction with the bp file pointer and size is the size of the buffer (see the documentation on BIO files and operations for more information). length is set to the number of bytes stored in the data element of the structure.

Upon successful completion 0 is returned; on error 1 is returned.

ASN1_INTEGER_set puts the long integer in v into an ASN1_INTEGER structure pointed to by a. Memory for the data element of the structure will be allocated, and a->data will be freed first if it is not NULL.

The length element of the structure will be set to the length of the data element.

Upon success, it returns 1, and on error, 0.

ASN1_INTEGER_get translates the data element of the ASN1_INTEGER structure that a points to, to a long and returns the long.

If the integer is too large to fit in a long, 0xffffffffL is returned instead.

If the ASN1_INTEGER structure is NULL or the data element is NULL, 0 is returned. Likewise if the type of a is not V_ASN1_INTEGER or V_ASN1_NEG_INTEGER, 0 is returned.

BN_to_ASN1_INTEGER converts the BIGNUM pointed to by bn to an ASN1_INTEGER structure and stores it in the structure pointed to by ai. See the BIGNUM routines documentation for more information.

If ai is NULL a new structure will be allocated.

Upon success it returns a pointer to the newly populated structure. On error, it returns NULL, and frees the structure if one had been allocated.

ASN1_INTEGER_to_BN converts the contents of the ASN1_INTEGER structure pointed to by ai to a BIGNUM type and returns its address. If bn is NULL a new BIGNUM will be allocated and its address will be placed in bn; otherwise the structure pointed to by bn will be used.

The following macros are provided for the convenience of the user:

#define ASN1_INTEGER_dup(a) (ASN1_INTEGER *)ASN1_STRING_dup((ASN1_STRING *)a)
#define ASN1_INTEGER_cmp(a,b)   ASN1_STRING_cmp(\
                (ASN1_STRING *)a,(ASN1_STRING *)b)

ASN1_INTEGER_dup returns a pointer to a copy of the ASN1_INTEGER structure pointed to by a. If a is NULL or if there is an error, NULL is returned.

ASN1_INTEGER_cmp compares the ASN1_INTEGER structures pointed to by a and b; if they are equal (same length, same content byte by byte, same type) 0 is returned, else an integer less than zero or greater than zero is returned depending on whether the length, content, or type of a is less than or greater than b, tested in that order. This means that the contents may be the same but you could get a non-zero number because the types don't match. Here, type had better be V_ASN1_INTEGER rather than something from the long list that ASN1_STRING would handle reasonably. It is of course the responsibility of the caller to ensure that the type makes sense.