Manual Pages for UNIX Darwin command on man d2i_X509_bio
MyWebUniversity

Manual Pages for UNIX Darwin command on man d2i_X509_bio

d2iX509(3) OpenSSL 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 XX550099 structure, which represents an X509 certificate. d2iX509() attempts to decode lleenn bytes at **oouutt. If successful a pointer to the XX550099 structure is returned. If an error occurred then NNUULLLL is returned. If ppxx is not NNUULLLL then the returned structure is written to **ppxx. If **ppxx is not NNUULLLL then it is assumed that **ppxx contains a valid XX550099 structure and an attempt is made to reuse it. If the call is successful **oouutt is incremented to the byte following the parsed data. i2dX509() encodes the structure pointed to by xx into DER format. If oouutt is not NNUULLLL is writes the DER encoded data to the buffer at **oouutt, 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 OpenSSL 0.9.7 and later if **oouutt is NNUULLLL memory will be allocated for a buffer and the encoded data written to it. In this case **oouutt 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 bbpp. d2iX509fp() is similar to d2iX509() except it attempts to parse data from FILE pointer ffpp. i2dX509bio() is similar to i2dX509() except it writes the encoding of the structure xx to BIO bbpp and it returns 1 for success and 0 for failure. i2dX509fp() is similar to i2dX509() except it writes the encoding of the structure xx to BIO bbpp and it returns 1 for success and 0 for failure. NNOOTTEESS The letters ii and dd in for example ii22ddXX550099 stand for "internal" (that is an internal C structure) and "DER". So that ii22ddXX550099 converts from internal to DER. The functions can also understand BBEERR forms. The actual X509 structure passed to i2dX509() must be a valid populated XX550099 structure it can nnoott 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 ssttrrlleenn(()) will nnoott return the correct length of the encoded structure. The ways that **iinn and **oouutt are incremented after the operation can trap the unwary. See the WWAARRNNIINNGGSS 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. EEXXAAMMPPLLEESS Allocate and encode the DER encoding of an X509 structure: int len; unsigned char *buf, *p; len = i2dX509(x, NULL); buf = OPENSSLmalloc(len); if (buf == NULL) /* error */ p = buf; i2dX509(x, &p); If you are using OpenSSL 0.9.7 or later then this can be simplified to: int len; unsigned char *buf; buf = NULL; 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(NULL, &p, len); if (x == NULL) /* Some error */ Alternative technique: X509 *x; unsigned char *buf, *p; int len; /* Something to setup buf and len */ p = buf; x = NULL; if(!d2iX509(&x, &p, len)) /* Some error */ WWAARRNNIINNGGSS 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, NULL); buf = OPENSSLmalloc(len); if (buf == NULL) /* error */ i2dX509(x, &buf); /* Other stuff ... */ OPENSSLfree(buf); This code will result in bbuuff apparently containing garbage because it was incremented after the call to point after the data just written. Also bbuuff will no longer contain the pointer allocated by OOPPEENNSSSSLLmmaalllloocc(()) and the subsequent call to OOPPEENNSSSSLLffrreeee(()) may well crash. The auto allocation feature (setting buf to NULL) only works on OpenSSL 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 xxpp argument to dd22iiXX550099(()): X509 *x; if (!d2iX509(&x, &p, len)) /* Some error */ This will probably crash somewhere in dd22iiXX550099(()). The reason for this is that the variable xx is uninitialized and an attempt will be made to interpret its (invalid) value as an XX550099 structure, typically causing a segmentation violation. If xx is set to NULL first then this will not happen.

BUGS

In some versions of OpenSSL the "reuse" behaviour of d2iX509() when **ppxx is valid is broken and some parts of the reused structure may persist 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 OpenSSL, 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 XX550099 structure or NNUULLLL if an error occurs. The error code that can be obtained by ERRgeterror(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 ERRgeterror(3). i2dX509bio() and i2dX509fp() returns 1 for success and 0 if an error occurs The error code can be obtained by ERRgeterror(3).

SEE ALSO

ERRgeterror(3) HISTORY d2iX509, i2dX509, d2iX509bio, d2iX509fp, i2dX509bio and i2dX509fp are available in all versions of SSLeay and OpenSSL.

0.9.7l 2002-11-14 d2iX509(3)




Contact us      |      About us      |      Term of use      |       Copyright © 2000-2019 MyWebUniversity.com ™