1d2i_X509(3)                         OpenSSL                        d2i_X509(3)
2
3
4

NAME

6       d2i_X509, i2d_X509, d2i_X509_bio, d2i_X509_fp, i2d_X509_bio,
7       i2d_X509_fp - X509 encode and decode functions
8

SYNOPSIS

10        #include <openssl/x509.h>
11
12        X509 *d2i_X509(X509 **px, const unsigned char **in, long len);
13        X509 *d2i_X509_AUX(X509 **px, const unsigned char **in, long len);
14        int i2d_X509(X509 *x, unsigned char **out);
15        int i2d_X509_AUX(X509 *x, unsigned char **out);
16
17        X509 *d2i_X509_bio(BIO *bp, X509 **x);
18        X509 *d2i_X509_fp(FILE *fp, X509 **x);
19
20        int i2d_X509_bio(BIO *bp, X509 *x);
21        int i2d_X509_fp(FILE *fp, X509 *x);
22
23        int i2d_re_X509_tbs(X509 *x, unsigned char **out);
24

DESCRIPTION

26       The X509 encode and decode routines encode and parse an X509 structure,
27       which represents an X509 certificate.
28
29       d2i_X509() attempts to decode len bytes at *in. If successful a pointer
30       to the X509 structure is returned. If an error occurred then NULL is
31       returned. If px is not NULL then the returned structure is written to
32       *px. If *px is not NULL then it is assumed that *px contains a valid
33       X509 structure and an attempt is made to reuse it. This "reuse"
34       capability is present for historical compatibility but its use is
35       strongly discouraged (see BUGS below, and the discussion in the RETURN
36       VALUES section).
37
38       If the call is successful *in is incremented to the byte following the
39       parsed data.
40
41       d2i_X509_AUX() is similar to d2i_X509() but the input is expected to
42       consist of an X509 certificate followed by auxiliary trust information.
43       This is used by the PEM routines to read "TRUSTED CERTIFICATE" objects.
44       This function should not be called on untrusted input.
45
46       i2d_X509() encodes the structure pointed to by x into DER format.  If
47       out is not NULL is writes the DER encoded data to the buffer at *out,
48       and increments it to point after the data just written.  If the return
49       value is negative an error occurred, otherwise it returns the length of
50       the encoded data.
51
52       For OpenSSL 0.9.7 and later if *out is NULL memory will be allocated
53       for a buffer and the encoded data written to it. In this case *out is
54       not incremented and it points to the start of the data just written.
55
56       i2d_X509_AUX() is similar to i2d_X509(), but the encoded output
57       contains both the certificate and any auxiliary trust information.
58       This is used by the PEM routines to write "TRUSTED CERTIFICATE"
59       objects.  Note, this is a non-standard OpenSSL-specific data format.
60
61       d2i_X509_bio() is similar to d2i_X509() except it attempts to parse
62       data from BIO bp.
63
64       d2i_X509_fp() is similar to d2i_X509() except it attempts to parse data
65       from FILE pointer fp.
66
67       i2d_X509_bio() is similar to i2d_X509() except it writes the encoding
68       of the structure x to BIO bp and it returns 1 for success and 0 for
69       failure.
70
71       i2d_X509_fp() is similar to i2d_X509() except it writes the encoding of
72       the structure x to BIO bp and it returns 1 for success and 0 for
73       failure.
74
75       i2d_re_X509_tbs() is similar to i2d_X509() except it encodes only the
76       TBSCertificate portion of the certificate.
77

NOTES

79       The letters i and d in for example i2d_X509 stand for "internal" (that
80       is an internal C structure) and "DER". So i2d_X509 converts from
81       internal to DER. The "re" in i2d_re_X509_tbs stands for "re-encode",
82       and ensures that a fresh encoding is generated in case the object has
83       been modified after creation (see the BUGS section).
84
85       The functions can also understand BER forms.
86
87       The actual X509 structure passed to i2d_X509() must be a valid
88       populated X509 structure it can not simply be fed with an empty
89       structure such as that returned by X509_new().
90
91       The encoded data is in binary form and may contain embedded zeroes.
92       Therefore any FILE pointers or BIOs should be opened in binary mode.
93       Functions such as strlen() will not return the correct length of the
94       encoded structure.
95
96       The ways that *in and *out are incremented after the operation can trap
97       the unwary. See the WARNINGS section for some common errors.
98
99       The reason for the auto increment behaviour is to reflect a typical
100       usage of ASN1 functions: after one structure is encoded or decoded
101       another will processed after it.
102

EXAMPLES

104       Allocate and encode the DER encoding of an X509 structure:
105
106        int len;
107        unsigned char *buf, *p;
108
109        len = i2d_X509(x, NULL);
110
111        buf = OPENSSL_malloc(len);
112
113        if (buf == NULL)
114               /* error */
115
116        p = buf;
117
118        i2d_X509(x, &p);
119
120       If you are using OpenSSL 0.9.7 or later then this can be simplified to:
121
122        int len;
123        unsigned char *buf;
124
125        buf = NULL;
126
127        len = i2d_X509(x, &buf);
128
129        if (len < 0)
130               /* error */
131
132       Attempt to decode a buffer:
133
134        X509 *x;
135
136        unsigned char *buf, *p;
137
138        int len;
139
140        /* Something to setup buf and len */
141
142        p = buf;
143
144        x = d2i_X509(NULL, &p, len);
145
146        if (x == NULL)
147           /* Some error */
148
149       Alternative technique:
150
151        X509 *x;
152
153        unsigned char *buf, *p;
154
155        int len;
156
157        /* Something to setup buf and len */
158
159        p = buf;
160
161        x = NULL;
162
163        if(!d2i_X509(&x, &p, len))
164           /* Some error */
165

WARNINGS

167       The use of temporary variable is mandatory. A common mistake is to
168       attempt to use a buffer directly as follows:
169
170        int len;
171        unsigned char *buf;
172
173        len = i2d_X509(x, NULL);
174
175        buf = OPENSSL_malloc(len);
176
177        if (buf == NULL)
178               /* error */
179
180        i2d_X509(x, &buf);
181
182        /* Other stuff ... */
183
184        OPENSSL_free(buf);
185
186       This code will result in buf apparently containing garbage because it
187       was incremented after the call to point after the data just written.
188       Also buf will no longer contain the pointer allocated by
189       OPENSSL_malloc() and the subsequent call to OPENSSL_free() may well
190       crash.
191
192       The auto allocation feature (setting buf to NULL) only works on OpenSSL
193       0.9.7 and later. Attempts to use it on earlier versions will typically
194       cause a segmentation violation.
195
196       Another trap to avoid is misuse of the xp argument to d2i_X509():
197
198        X509 *x;
199
200        if (!d2i_X509(&x, &p, len))
201               /* Some error */
202
203       This will probably crash somewhere in d2i_X509(). The reason for this
204       is that the variable x is uninitialized and an attempt will be made to
205       interpret its (invalid) value as an X509 structure, typically causing a
206       segmentation violation. If x is set to NULL first then this will not
207       happen.
208

BUGS

210       In some versions of OpenSSL the "reuse" behaviour of d2i_X509() when
211       *px is valid is broken and some parts of the reused structure may
212       persist if they are not present in the new one. As a result the use of
213       this "reuse" behaviour is strongly discouraged.
214
215       i2d_X509() will not return an error in many versions of OpenSSL, if
216       mandatory fields are not initialized due to a programming error then
217       the encoded structure may contain invalid data or omit the fields
218       entirely and will not be parsed by d2i_X509(). This may be fixed in
219       future so code should not assume that i2d_X509() will always succeed.
220
221       The encoding of the TBSCertificate portion of a certificate is cached
222       in the X509 structure internally to improve encoding performance and to
223       ensure certificate signatures are verified correctly in some
224       certificates with broken (non-DER) encodings.
225
226       Any function which encodes an X509 structure such as i2d_X509(),
227       i2d_X509_fp() or i2d_X509_bio() may return a stale encoding if the X509
228       structure has been modified after deserialization or previous
229       serialization.
230
231       If, after modification, the X509 object is re-signed with X509_sign(),
232       the encoding is automatically renewed. Otherwise, the encoding of the
233       TBSCertificate portion of the X509 can be manually renewed by calling
234       i2d_re_X509_tbs().
235

RETURN VALUES

237       d2i_X509(), d2i_X509_bio() and d2i_X509_fp() return a valid X509
238       structure or NULL if an error occurs. The error code that can be
239       obtained by ERR_get_error(3). If the "reuse" capability has been used
240       with a valid X509 structure being passed in via px then the object is
241       not freed in the event of error but may be in a potentially invalid or
242       inconsistent state.
243
244       i2d_X509() returns the number of bytes successfully encoded or a
245       negative value if an error occurs. The error code can be obtained by
246       ERR_get_error(3).
247
248       i2d_X509_bio() and i2d_X509_fp() return 1 for success and 0 if an error
249       occurs The error code can be obtained by ERR_get_error(3).
250

SEE ALSO

252       ERR_get_error(3)
253

HISTORY

255       d2i_X509, i2d_X509, d2i_X509_bio, d2i_X509_fp, i2d_X509_bio and
256       i2d_X509_fp are available in all versions of SSLeay and OpenSSL.
257
258
259
2601.0.2k                            2017-01-26                       d2i_X509(3)
Impressum