summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/man/d2i_X509.3
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/lib/libcrypto/man/d2i_X509.3346
1 files changed, 346 insertions, 0 deletions
diff --git a/src/lib/libcrypto/man/d2i_X509.3 b/src/lib/libcrypto/man/d2i_X509.3
new file mode 100644
index 0000000000..09c65afa58
--- /dev/null
+++ b/src/lib/libcrypto/man/d2i_X509.3
@@ -0,0 +1,346 @@
1.Dd $Mdocdate: November 4 2016 $
2.Dt D2I_X509 3
3.Os
4.Sh NAME
5.Nm d2i_X509 ,
6.Nm i2d_X509 ,
7.Nm d2i_X509_bio ,
8.Nm d2i_X509_fp ,
9.Nm i2d_X509_bio ,
10.Nm i2d_X509_fp
11.Nd X509 encode and decode functions
12.Sh SYNOPSIS
13.In openssl/x509.h
14.Ft X509 *
15.Fo d2i_X509
16.Fa "X509 **px"
17.Fa "const unsigned char **in"
18.Fa "int len"
19.Fc
20.Ft int
21.Fo i2d_X509
22.Fa "X509 *x"
23.Fa "unsigned char **out"
24.Fc
25.Ft X509 *
26.Fo d2i_X509_bio
27.Fa "BIO *bp"
28.Fa "X509 **x"
29.Fc
30.Ft X509 *
31.Fo d2i_X509_fp
32.Fa "FILE *fp"
33.Fa "X509 **x"
34.Fc
35.Ft int
36.Fo i2d_X509_bio
37.Fa "BIO *bp"
38.Fa "X509 *x"
39.Fc
40.Ft int
41.Fo i2d_X509_fp
42.Fa "FILE *fp"
43.Fa "X509 *x"
44.Fc
45.Sh DESCRIPTION
46The X509 encode and decode routines encode and parse an
47.Vt X509
48structure, which represents an X509 certificate.
49.Pp
50.Fn d2i_X509
51attempts to decode
52.Fa len
53bytes at
54.Pf * Fa in .
55If successful, a pointer to the
56.Vt X509
57structure is returned.
58If an error occurred,
59.Dv NULL
60is returned.
61If
62.Fa px
63is not
64.Dv NULL ,
65the returned structure is written to
66.Pf * Fa px .
67If
68.Pf * Fa px
69is not
70.Dv NULL ,
71then it is assumed that
72.Pf * Fa px
73contains a valid
74.Vt X509
75structure and an attempt is made to reuse it.
76If the call is successful,
77.Pf * Fa in
78is incremented to the byte following the parsed data.
79.Pp
80.Fn i2d_X509
81encodes the structure pointed to by
82.Fa x
83into DER format.
84If
85.Fa out
86is not
87.Dv NULL ,
88it writes the DER encoded data to the buffer at
89.Pf * Fa out
90and increments it to point after the data just written.
91If the return value is negative an error occurred, otherwise it returns
92the length of the encoded data.
93.Pp
94For OpenSSL 0.9.7 and later if
95.Pf * Fa out
96is
97.Dv NULL ,
98memory will be allocated for a buffer and the encoded data written to it.
99In this case
100.Pf * Fa out
101is not incremented and it points to the start of the data just written.
102.Pp
103.Fn d2i_X509_bio
104is similar to
105.Fn d2i_X509
106except it attempts to parse data from
107.Vt BIO
108.Fa bp .
109.Pp
110.Fn d2i_X509_fp
111is similar to
112.Fn d2i_X509
113except it attempts to parse data from the
114.Vt FILE
115pointer
116.Fa fp .
117.Pp
118.Fn i2d_X509_bio
119is similar to
120.Fn i2d_X509
121except it writes the encoding of the structure
122.Fa x
123to
124.Vt BIO
125.Fa bp
126and it returns 1 for success or 0 for failure.
127.Pp
128.Fn i2d_X509_fp
129is similar to
130.Fn i2d_X509
131except it writes the encoding of the structure
132.Fa x
133to
134.Vt BIO
135.Fa bp
136and it returns 1 for success or 0 for failure.
137.Pp
138The letters
139.Sy i
140and
141.Sy d
142in for example
143.Fn i2d_X509
144stand for "internal" (that is an internal C structure) and "DER",
145so that
146.Fn i2d_X509
147converts from internal to DER.
148.Pp
149The functions can also understand BER forms.
150.Pp
151The actual
152.Vt X509
153structure passed to
154.Fn i2d_X509
155must be a valid populated
156.Vt X509
157structure.
158It cannot simply be fed with an empty structure such as that returned by
159.Xr X509_new 3 .
160.Pp
161The encoded data is in binary form and may contain embedded zeroes.
162Therefore any
163.Vt FILE
164pointers or
165.Vt BIO Ns s
166should be opened in binary mode.
167Functions such as
168.Xr strlen 3
169will
170.Sy not
171return the correct length of the encoded structure.
172.Pp
173The ways that
174.Pf * Fa in
175and
176.Pf * Fa out
177are incremented after the operation can trap the unwary.
178See the
179.Sx CAVEATS
180section for some common errors.
181.Pp
182The reason for the auto increment behaviour is to reflect a typical
183usage of ASN1 functions: after one structure is encoded or decoded,
184another will processed after it.
185.Sh RETURN VALUES
186.Fn d2i_X509 ,
187.Fn d2i_X509_bio ,
188and
189.Fn d2i_X509_fp
190return a valid
191.Vt X509
192structure or
193.Dv NULL
194if an error occurs.
195.Pp
196.Fn i2d_X509
197returns the number of bytes successfully encoded or a negative value if
198an error occurs.
199.Pp
200.Fn i2d_X509_bio
201and
202.Fn i2d_X509_fp
203return 1 for success or 0 if an error occurs.
204.Pp
205For all functions, the error code can be obtained by
206.Xr ERR_get_error 3 .
207.Sh EXAMPLES
208Allocate and encode the DER encoding of an X509 structure:
209.Bd -literal -offset indent
210int len;
211unsigned char *buf, *p;
212
213len = i2d_X509(x, NULL);
214buf = malloc(len);
215if (buf == NULL)
216 /* error */
217p = buf;
218i2d_X509(x, &p);
219.Ed
220.Pp
221If you are using OpenSSL 0.9.7 or later then this can be simplified to:
222.Bd -literal -offset indent
223int len;
224unsigned char *buf;
225
226buf = NULL;
227len = i2d_X509(x, &buf);
228if (len < 0)
229 /* error */
230.Ed
231.Pp
232Attempt to decode a buffer:
233.Bd -literal -offset indent
234X509 *x;
235unsigned char *buf, *p;
236int len;
237
238/* Something to setup buf and len */
239p = buf;
240x = d2i_X509(NULL, &p, len);
241if (x == NULL)
242 /* Some error */
243.Ed
244.Pp
245Alternative technique:
246.Bd -literal -offset indent
247X509 *x;
248unsigned char *buf, *p;
249int len;
250
251/* Something to setup buf and len */
252p = buf;
253x = NULL;
254if(!d2i_X509(&x, &p, len))
255 /* Some error */
256.Ed
257.Sh SEE ALSO
258.Xr ERR_get_error 3
259.Sh HISTORY
260.Fn d2i_X509 ,
261.Fn i2d_X509 ,
262.Fn d2i_X509_bio ,
263.Fn d2i_X509_fp ,
264.Fn i2d_X509_bio ,
265and
266.Fn i2d_X509_fp
267are available in all versions of SSLeay and OpenSSL.
268.Sh CAVEATS
269The use of a temporary variable is mandatory.
270A common mistake is to attempt to use a buffer directly as follows:
271.Bd -literal -offset indent
272int len;
273unsigned char *buf;
274
275len = i2d_X509(x, NULL);
276buf = malloc(len);
277if (buf == NULL)
278 /* error */
279i2d_X509(x, &buf);
280/* Other stuff ... */
281free(buf);
282.Ed
283.Pp
284This code will result in
285.Fa buf
286apparently containing garbage because it was incremented after the
287call to point after the data just written.
288Also
289.Fa buf
290will no longer contain the pointer allocated by
291.Xr malloc 3
292and the subsequent call to
293.Xr free 3
294may well crash.
295.Pp
296The auto allocation feature (setting
297.Fa buf
298to
299.Dv NULL
300only works on OpenSSL 0.9.7 and later.
301Attempts to use it on earlier versions will typically cause a
302segmentation violation.
303.Pp
304Another trap to avoid is misuse of the
305.Fa px
306argument to
307.Sy d2i_X509() :
308.Bd -literal -offset indent
309X509 *x;
310
311if (!d2i_X509(&x, &p, len))
312 /* Some error */
313.Ed
314.Pp
315This will probably crash somewhere in
316.Fn d2i_X509 .
317The reason for this is that the variable
318.Fa x
319is uninitialized and an attempt will be made to interpret its (invalid)
320value as an
321.Vt X509
322structure, typically causing a segmentation violation.
323If
324.Fa x
325is set to
326.Dv NULL
327first then this will not happen.
328.Sh BUGS
329In some versions of OpenSSL the "reuse" behaviour of
330.Fn d2i_X509
331when
332.Pf * Fa px
333is valid is broken and some parts of the reused structure may persist
334if they are not present in the new one.
335As a result the use of this "reuse" behaviour is strongly discouraged.
336.Pp
337In many versions of OpenSSL,
338.Fn i2d_X509
339will not return an error if mandatory fields are not initialized
340due to a programming error.
341Then the encoded structure may contain invalid data or omit the
342fields entirely and will not be parsed by
343.Fn d2i_X509 .
344This may be fixed in future so code should not assume that
345.Fn i2d_X509
346will always succeed.