d2i_X509(3) OpenSSL d2i_X509(3) NNAAMMEE d2i_X509, i2d_X509, d2i_X509_bio, d2i_X509_fp, i2d_X509_bio, i2d_X509_fp - X509 encode and decode functions SSYYNNOOPPSSIISS #include X509 *d2i_X509(X509 **px, const unsigned char **in, int len); int i2d_X509(X509 *x, unsigned char **out); X509 *d2i_X509_bio(BIO *bp, X509 **x); X509 *d2i_X509_fp(FILE *fp, X509 **x); int i2d_X509_bio(BIO *bp, X509 *x); int i2d_X509_fp(FILE *fp, X509 *x); DDEESSCCRRIIPPTTIIOONN The X509 encode and decode routines encode and parse an XX550099 structure, which represents an X509 certificate. _d_2_i___X_5_0_9_(_) attempts to decode lleenn bytes at **iinn. 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. This "reuse" capa- bility is present for historical compatibility but its use is ssttrroonnggllyy ddiissccoouurraaggeedd (see BUGS below, and the discussion in the RETURN VALUES section). If the call is successful **iinn is incremented to the byte following the parsed data. _i_2_d___X_5_0_9_(_) 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. _d_2_i___X_5_0_9___b_i_o_(_) is similar to _d_2_i___X_5_0_9_(_) except it attempts to parse data from BIO bbpp. _d_2_i___X_5_0_9___f_p_(_) is similar to _d_2_i___X_5_0_9_(_) except it attempts to parse data from FILE pointer ffpp. _i_2_d___X_5_0_9___b_i_o_(_) is similar to _i_2_d___X_5_0_9_(_) except it writes the encoding of the structure xx to BIO bbpp and it returns 1 for success and 0 for failure. _i_2_d___X_5_0_9___f_p_(_) is similar to _i_2_d___X_5_0_9_(_) except it writes the encoding of the structure xx to BIO bbpp and it returns 1 for success and 0 for fail- ure. NNOOTTEESS The letters ii and dd in for example ii22dd__XX550099 stand for "internal" (that is an internal C structure) and "DER". So that ii22dd__XX550099 converts from internal to DER. The functions can also understand BBEERR forms. The actual X509 structure passed to _i_2_d___X_5_0_9_(_) must be a valid popu- lated XX550099 structure it can nnoott simply be fed with an empty structure such as that returned by _X_5_0_9___n_e_w_(_). 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 _ss_tt_rr_ll_ee_nn_((_)) 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 = i2d_X509(x, NULL); buf = OPENSSL_malloc(len); if (buf == NULL) /* error */ p = buf; i2d_X509(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 = i2d_X509(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 = d2i_X509(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(!d2i_X509(&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 = i2d_X509(x, NULL); buf = OPENSSL_malloc(len); if (buf == NULL) /* error */ i2d_X509(x, &buf); /* Other stuff ... */ OPENSSL_free(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 _OO_PP_EE_NN_SS_SS_LL____mm_aa_ll_-- _ll_oo_cc_((_)) and the subsequent call to _OO_PP_EE_NN_SS_SS_LL____ff_rr_ee_ee_((_)) 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 _dd_22_ii____XX_55_00_99_((_)): X509 *x; if (!d2i_X509(&x, &p, len)) /* Some error */ This will probably crash somewhere in _dd_22_ii____XX_55_00_99_((_)). 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. BBUUGGSS In some versions of OpenSSL the "reuse" behaviour of _d_2_i___X_5_0_9_(_) when **ppxx 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. _i_2_d___X_5_0_9_(_) 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 _d_2_i___X_5_0_9_(_). This may be fixed in future so code should not assume that _i_2_d___X_5_0_9_(_) will always succeed. RREETTUURRNN VVAALLUUEESS _d_2_i___X_5_0_9_(_), _d_2_i___X_5_0_9___b_i_o_(_) and _d_2_i___X_5_0_9___f_p_(_) return a valid XX550099 struc- ture or NNUULLLL if an error occurs. The error code that can be obtained by _E_R_R___g_e_t___e_r_r_o_r(3). If the "reuse" capability has been used with a valid X509 structure being passed in via ppxx then the object is not freed in the event of error but may be in a potentially invalid or inconsistent state. _i_2_d___X_5_0_9_(_) returns the number of bytes successfully encoded or a nega- tive value if an error occurs. The error code can be obtained by _E_R_R___g_e_t___e_r_r_o_r(3). _i_2_d___X_5_0_9___b_i_o_(_) and _i_2_d___X_5_0_9___f_p_(_) return 1 for success and 0 if an error occurs The error code can be obtained by _E_R_R___g_e_t___e_r_r_o_r(3). SSEEEE AALLSSOO _E_R_R___g_e_t___e_r_r_o_r(3) HHIISSTTOORRYY d2i_X509, i2d_X509, d2i_X509_bio, d2i_X509_fp, i2d_X509_bio and i2d_X509_fp are available in all versions of SSLeay and OpenSSL. 1.0.1u 2016-09-22 d2i_X509(3)