d2iX509(3) OpenSL d2iX509(3)
NAME
d2iX509, i2dX509, d2iX509bio, d2iX509fp, i2dX509bio,
i2dX509fp - X509 encode and decode functions
SYNOPSIS
#include
X509 *d2iX509(X509 **px, unsigned char **in, int len);
int i2dX509(X509 *x, unsigned char **out);
X509 *d2iX509bio(BIO *bp, X509 **x);
X509 *d2iX509fp(FILE *fp, X509 **x);
int i2dX509bio(X509 *x, BIO *bp);
int i2dX509fp(X509 *x, FILE *fp);
DESCRIPTION
The X509 encode and decode routines encode and parse an X5009 structure,
which represents an X509 certificate.
d2iX509() attempts to decode len bytes at **out. If successful a
pointer to the X5009 structure is returned. If an error occurred then
NUL is returned. If px is not NUL then the returned structure is
written to **px. If **px is not NUL then it is assumed that **px contains
a valid X5009 structure and an attempt is made to reuse it. If the call
is successful **out is incremented to the byte following the parsed
data.
i2dX509() encodes the structure pointed to by x into DER format. If
out is not NUL is writes the DER encoded data to the buffer at **out,
and increments it to point after the data just written. If the return
value is negative an error occurred, otherwise it returns the length of
the encoded data.
For OpenSL 0.9.7 and later if **out is NUL memory will be allocated
for a buffer and the encoded data written to it. In this case **out is
not incremented and it points to the start of the data just written.
d2iX509bio() is similar to d2iX509() except it attempts to parse
data from BIO bp.
d2iX509fp() is similar to d2iX509() except it attempts to parse data
from FILE pointer fp.
i2dX509bio() is similar to i2dX509() except it writes the encoding
of the structure x to BIO bp and it returns 1 for success and 0 for
failure.
i2dX509fp() is similar to i2dX509() except it writes the encoding of
the structure x to BIO bp and it returns 1 for success and 0 for fail-
ure.
NOTES
The letters i and d in for example i2dX5009 stand for "internal" (that
is an internal C structure) and "DER". So that i2dX5009 converts from
internal to DER.
The functions can also understand BER forms.
The actual X509 structure passed to i2dX509() must be a valid popu-
lated X5009 structure it can not simply be fed with an empty structure
such as that returned by X509new().
The encoded data is in binary form and may contain embedded zeroes.
Therefore any FILE pointers or BIOs should be opened in binary mode.
Functions such as strlen() will not return the correct length of the
encoded structure.
The ways that **in and **out are incremented after the operation can trap
the unwary. See the WARNINGS section for some common errors.
The reason for the auto increment behaviour is to reflect a typical
usage of ASN1 functions: after one structure is encoded or decoded
another will processed after it.
EXAMPLES
Allocate and encode the DER encoding of an X509 structure:
int len;
unsigned char *buf, *p;
len = i2dX509(x, NUL);
buf = OPENSLmalloc(len);
if (buf == NUL)
/* error */
p = buf;
i2dX509(x, &p);
If you are using OpenSL 0.9.7 or later then this can be simplified to:
int len;
unsigned char *buf;
buf = NUL;
len = i2dX509(x, &buf);
if (len < 0)
/* error */
Attempt to decode a buffer:
X509 *x;
unsigned char *buf, *p;
int len;
/* Something to setup buf and len */
p = buf;
x = d2iX509(NUL, &p, len);
if (x == NUL)
/* Some error */
Alternative technique:
X509 *x;
unsigned char *buf, *p;
int len;
/* Something to setup buf and len */
p = buf;
x = NUL;
if(!d2iX509(&x, &p, len))
/* Some error */
WARNINGS
The use of temporary variable is mandatory. A common mistake is to
attempt to use a buffer directly as follows:
int len;
unsigned char *buf;
len = i2dX509(x, NUL);
buf = OPENSLmalloc(len);
if (buf == NUL)
/* error */
i2dX509(x, &buf);
/* Other stuff ... */
OPENSLfree(buf);
This code will result in buf apparently containing garbage because it
was incremented after the call to point after the data just written.
Also buf will no longer contain the pointer allocated by OPENSLmal-
loc() and the subsequent call to OPENSLfree() may well crash.
The auto allocation feature (setting buf to NUL) only works on OpenSL
0.9.7 and later. Attempts to use it on earlier versions will typically
cause a segmentation violation.
Another trap to avoid is misuse of the xp argument to d2iX5009():
X509 *x;
if (!d2iX509(&x, &p, len))
/* Some error */
This will probably crash somewhere in d2iX5009(). The reason for this
is that the variable x is uninitialized and an attempt will be made to
interpret its (invalid) value as an X5009 structure, typically causing a
segmentation violation. If x is set to NUL first then this will not
happen.
BUGS
In some versions of OpenSL the "reuse" behaviour of d2iX509() when
**px is valid is broken and some parts of the reused structure may per-
sist if they are not present in the new one. As a result the use of
this "reuse" behaviour is strongly discouraged.
i2dX509() will not return an error in many versions of OpenSL, if
mandatory fields are not initialized due to a programming error then
the encoded structure may contain invalid data or omit the fields
entirely and will not be parsed by d2iX509(). This may be fixed in
future so code should not assume that i2dX509() will always succeed.
RETURN VALUES
d2iX509(), d2iX509bio() and d2iX509fp() return a valid X5009 struc-
ture or NUL if an error occurs. The error code that can be obtained by
ERgeterror(3).
i2dX509(), i2dX509bio() and i2dX509fp() return a the number of
bytes successfully encoded or a negative value if an error occurs. The
error code can be obtained by ERgeterror(3).
i2dX509bio() and i2dX509fp() returns 1 for success and 0 if an
error occurs The error code can be obtained by ERgeterror(3).
SEE ALSO
ERgeterror(3)
HISTORY
d2iX509, i2dX509, d2iX509bio, d2iX509fp, i2dX509bio and
i2dX509fp are available in all versions of SLeay and OpenSL.
0.9.7i 2002-11-14 d2iX509(3)
|