summaryrefslogtreecommitdiff
path: root/lib/libcrypto/man/d2i_X509.3
blob: 7ca3abf2d96849e5369585b7ee32b63db461d6e2 (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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
.\"	$OpenBSD: d2i_X509.3,v 1.4 2016/12/08 20:22:08 jmc Exp $
.\"	OpenSSL 94480b57 Sep 12 23:34:41 2009 +0000
.\"
.\" This file was written by Dr. Stephen Henson <steve@openssl.org>.
.\" Copyright (c) 2002, 2003, 2005, 2009, 2016 The OpenSSL Project.
.\" All rights reserved.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\"
.\" 1. Redistributions of source code must retain the above copyright
.\"    notice, this list of conditions and the following disclaimer.
.\"
.\" 2. Redistributions in binary form must reproduce the above copyright
.\"    notice, this list of conditions and the following disclaimer in
.\"    the documentation and/or other materials provided with the
.\"    distribution.
.\"
.\" 3. All advertising materials mentioning features or use of this
.\"    software must display the following acknowledgment:
.\"    "This product includes software developed by the OpenSSL Project
.\"    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
.\"
.\" 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
.\"    endorse or promote products derived from this software without
.\"    prior written permission. For written permission, please contact
.\"    openssl-core@openssl.org.
.\"
.\" 5. Products derived from this software may not be called "OpenSSL"
.\"    nor may "OpenSSL" appear in their names without prior written
.\"    permission of the OpenSSL Project.
.\"
.\" 6. Redistributions of any form whatsoever must retain the following
.\"    acknowledgment:
.\"    "This product includes software developed by the OpenSSL Project
.\"    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
.\" EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
.\" ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
.\" SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
.\" LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
.\" STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
.\" OF THE POSSIBILITY OF SUCH DAMAGE.
.\"
.Dd $Mdocdate: December 8 2016 $
.Dt D2I_X509 3
.Os
.Sh NAME
.Nm d2i_X509 ,
.Nm d2i_X509_AUX ,
.Nm i2d_X509 ,
.Nm i2d_X509_AUX ,
.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 "long len"
.Fc
.Ft X509 *
.Fo d2i_X509_AUX
.Fa "X509 **px"
.Fa "const unsigned char **in"
.Fa "long len"
.Fc
.Ft int
.Fo i2d_X509
.Fa "X509 *x"
.Fa "unsigned char **out"
.Fc
.Ft int
.Fo i2d_X509_AUX
.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.
This "reuse" capability is present for historical compatibility,
but its use is strongly discouraged; see the
.Sx BUGS
and
.Sx RETURN VALUES
sections.
If the call is successful,
.Pf * Fa in
is incremented to the byte following the parsed data.
.Pp
.Fn d2i_X509_AUX
is similar to
.Fn d2i_X509 ,
but the input is expected to consist of an X.509 certificate followed
by auxiliary trust information.
This is used by the PEM routines to read TRUSTED CERTIFICATE objects.
This function should not be called on untrusted input.
.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 i2d_X509_AUX
is similar to
.Fn i2d_X509 ,
but the encoded output contains both the certificate and any auxiliary
trust information.
This is used by the PEM routines to write TRUSTED CERTIFICATE objects.
Note that this is a non-standard OpenSSL-specific data format.
.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 ASN.1 functions: after one structure is encoded or decoded,
another will be 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 .
If the "reuse" capability has been used with a valid
.Vt X509
structure being passed in via
.Fa px ,
then the object is not freed in the event of an error, but may be
in a potentially invalid or inconsistent state.
.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
Using OpenSSL 0.9.7 or later 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.