summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/man/d2i_X509.3
blob: 09c65afa5856b4ed208b723fb60cdaced222fd12 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
.Dd $Mdocdate: November 4 2016 $
.Dt D2I_X509 3
.Os
.Sh NAME
.Nm d2i_X509 ,
.Nm i2d_X509 ,
.Nm d2i_X509_bio ,
.Nm d2i_X509_fp ,
.Nm i2d_X509_bio ,
.Nm i2d_X509_fp
.Nd X509 encode and decode functions
.Sh SYNOPSIS
.In openssl/x509.h
.Ft X509 *
.Fo d2i_X509
.Fa "X509 **px"
.Fa "const unsigned char **in"
.Fa "int len"
.Fc
.Ft int
.Fo i2d_X509
.Fa "X509 *x"
.Fa "unsigned char **out"
.Fc
.Ft X509 *
.Fo d2i_X509_bio
.Fa "BIO *bp"
.Fa "X509 **x"
.Fc
.Ft X509 *
.Fo d2i_X509_fp
.Fa "FILE *fp"
.Fa "X509 **x"
.Fc
.Ft int
.Fo i2d_X509_bio
.Fa "BIO *bp"
.Fa "X509 *x"
.Fc
.Ft int
.Fo i2d_X509_fp
.Fa "FILE *fp"
.Fa "X509 *x"
.Fc
.Sh DESCRIPTION
The X509 encode and decode routines encode and parse an
.Vt X509
structure, which represents an X509 certificate.
.Pp
.Fn d2i_X509
attempts to decode
.Fa len
bytes at
.Pf * Fa in .
If successful, a pointer to the
.Vt X509
structure is returned.
If an error occurred,
.Dv NULL
is returned.
If
.Fa px
is not
.Dv NULL ,
the returned structure is written to
.Pf * Fa px .
If
.Pf * Fa px
is not
.Dv NULL ,
then it is assumed that
.Pf * Fa px
contains a valid
.Vt X509
structure and an attempt is made to reuse it.
If the call is successful,
.Pf * Fa in
is incremented to the byte following the parsed data.
.Pp
.Fn i2d_X509
encodes the structure pointed to by
.Fa x
into DER format.
If
.Fa out
is not
.Dv NULL ,
it writes the DER encoded data to the buffer at
.Pf * Fa 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.
.Pp
For OpenSSL 0.9.7 and later if
.Pf * Fa out
is
.Dv NULL ,
memory will be allocated for a buffer and the encoded data written to it.
In this case
.Pf * Fa out
is not incremented and it points to the start of the data just written.
.Pp
.Fn d2i_X509_bio
is similar to
.Fn d2i_X509
except it attempts to parse data from
.Vt BIO
.Fa bp .
.Pp
.Fn d2i_X509_fp
is similar to
.Fn d2i_X509
except it attempts to parse data from the
.Vt FILE
pointer
.Fa fp .
.Pp
.Fn i2d_X509_bio
is similar to
.Fn i2d_X509
except it writes the encoding of the structure
.Fa x
to
.Vt BIO
.Fa bp
and it returns 1 for success or 0 for failure.
.Pp
.Fn i2d_X509_fp
is similar to
.Fn i2d_X509
except it writes the encoding of the structure
.Fa x
to
.Vt BIO
.Fa bp
and it returns 1 for success or 0 for failure.
.Pp
The letters
.Sy i
and
.Sy d
in for example
.Fn i2d_X509
stand for "internal" (that is an internal C structure) and "DER",
so that
.Fn i2d_X509
converts from internal to DER.
.Pp
The functions can also understand BER forms.
.Pp
The actual
.Vt X509
structure passed to
.Fn i2d_X509
must be a valid populated
.Vt X509
structure.
It cannot simply be fed with an empty structure such as that returned by
.Xr X509_new 3 .
.Pp
The encoded data is in binary form and may contain embedded zeroes.
Therefore any
.Vt FILE
pointers or
.Vt BIO Ns s
should be opened in binary mode.
Functions such as
.Xr strlen 3
will
.Sy not
return the correct length of the encoded structure.
.Pp
The ways that
.Pf * Fa in
and
.Pf * Fa out
are incremented after the operation can trap the unwary.
See the
.Sx CAVEATS
section for some common errors.
.Pp
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.
.Sh RETURN VALUES
.Fn d2i_X509 ,
.Fn d2i_X509_bio ,
and
.Fn d2i_X509_fp
return a valid
.Vt X509
structure or
.Dv NULL
if an error occurs.
.Pp
.Fn i2d_X509
returns the number of bytes successfully encoded or a negative value if
an error occurs.
.Pp
.Fn i2d_X509_bio
and
.Fn i2d_X509_fp
return 1 for success or 0 if an error occurs.
.Pp
For all functions, the error code can be obtained by
.Xr ERR_get_error 3 .
.Sh EXAMPLES
Allocate and encode the DER encoding of an X509 structure:
.Bd -literal -offset indent
int len;
unsigned char *buf, *p;

len = i2d_X509(x, NULL);
buf = malloc(len);
if (buf == NULL)
	/* error */
p = buf;
i2d_X509(x, &p);
.Ed
.Pp
If you are using OpenSSL 0.9.7 or later then this can be simplified to:
.Bd -literal -offset indent
int len;
unsigned char *buf;

buf = NULL;
len = i2d_X509(x, &buf);
if (len < 0)
	/* error */
.Ed
.Pp
Attempt to decode a buffer:
.Bd -literal -offset indent
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 */
.Ed
.Pp
Alternative technique:
.Bd -literal -offset indent
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 */
.Ed
.Sh SEE ALSO
.Xr ERR_get_error 3
.Sh HISTORY
.Fn d2i_X509 ,
.Fn i2d_X509 ,
.Fn d2i_X509_bio ,
.Fn d2i_X509_fp ,
.Fn i2d_X509_bio ,
and
.Fn i2d_X509_fp
are available in all versions of SSLeay and OpenSSL.
.Sh CAVEATS
The use of a temporary variable is mandatory.
A common mistake is to attempt to use a buffer directly as follows:
.Bd -literal -offset indent
int len;
unsigned char *buf;

len = i2d_X509(x, NULL);
buf = malloc(len);
if (buf == NULL)
	/* error */
i2d_X509(x, &buf);
/* Other stuff ... */
free(buf);
.Ed
.Pp
This code will result in
.Fa buf
apparently containing garbage because it was incremented after the
call to point after the data just written.
Also
.Fa buf
will no longer contain the pointer allocated by
.Xr malloc 3
and the subsequent call to
.Xr free 3
may well crash.
.Pp
The auto allocation feature (setting
.Fa buf
to
.Dv NULL
only works on OpenSSL 0.9.7 and later.
Attempts to use it on earlier versions will typically cause a
segmentation violation.
.Pp
Another trap to avoid is misuse of the
.Fa px
argument to
.Sy d2i_X509() :
.Bd -literal -offset indent
X509 *x;

if (!d2i_X509(&x, &p, len))
	/* Some error */
.Ed
.Pp
This will probably crash somewhere in
.Fn d2i_X509 .
The reason for this is that the variable
.Fa x
is uninitialized and an attempt will be made to interpret its (invalid)
value as an
.Vt X509
structure, typically causing a segmentation violation.
If
.Fa x
is set to
.Dv NULL
first then this will not happen.
.Sh BUGS
In some versions of OpenSSL the "reuse" behaviour of
.Fn d2i_X509
when
.Pf * Fa px
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.
.Pp
In many versions of OpenSSL,
.Fn i2d_X509
will not return an error 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
.Fn d2i_X509 .
This may be fixed in future so code should not assume that
.Fn i2d_X509
will always succeed.