summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorschwarze <>2016-12-28 03:35:32 +0000
committerschwarze <>2016-12-28 03:35:32 +0000
commitbe95f3af5f415b3f044066b1516eea3b5f3325a0 (patch)
tree5918e6d31e4d34ce1d890b18b9cf58ef9b9a4706
parent3c97a7c50200d5ef4681d620bf76fcea2ce72811 (diff)
downloadopenbsd-be95f3af5f415b3f044066b1516eea3b5f3325a0.tar.gz
openbsd-be95f3af5f415b3f044066b1516eea3b5f3325a0.tar.bz2
openbsd-be95f3af5f415b3f044066b1516eea3b5f3325a0.zip
Basic cleanup:
Improve .Nd. Sort functions. Use the same parameter names as in ASN1_item_d2i(3). Point to ASN1_item_d2i(3) for all he details. Delete all the information that's now in ASN1_item_d2i(3). Add missing entries to the RETURN VALUES section. Add STANDARDS section.
-rw-r--r--src/lib/libcrypto/man/d2i_X509.3371
1 files changed, 60 insertions, 311 deletions
diff --git a/src/lib/libcrypto/man/d2i_X509.3 b/src/lib/libcrypto/man/d2i_X509.3
index 7ca3abf2d9..1b716d2fbb 100644
--- a/src/lib/libcrypto/man/d2i_X509.3
+++ b/src/lib/libcrypto/man/d2i_X509.3
@@ -1,4 +1,4 @@
1.\" $OpenBSD: d2i_X509.3,v 1.4 2016/12/08 20:22:08 jmc Exp $ 1.\" $OpenBSD: d2i_X509.3,v 1.5 2016/12/28 03:35:32 schwarze Exp $
2.\" OpenSSL 94480b57 Sep 12 23:34:41 2009 +0000 2.\" OpenSSL 94480b57 Sep 12 23:34:41 2009 +0000
3.\" 3.\"
4.\" This file was written by Dr. Stephen Henson <steve@openssl.org>. 4.\" This file was written by Dr. Stephen Henson <steve@openssl.org>.
@@ -49,103 +49,86 @@
49.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 49.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50.\" OF THE POSSIBILITY OF SUCH DAMAGE. 50.\" OF THE POSSIBILITY OF SUCH DAMAGE.
51.\" 51.\"
52.Dd $Mdocdate: December 8 2016 $ 52.Dd $Mdocdate: December 28 2016 $
53.Dt D2I_X509 3 53.Dt D2I_X509 3
54.Os 54.Os
55.Sh NAME 55.Sh NAME
56.Nm d2i_X509 , 56.Nm d2i_X509 ,
57.Nm d2i_X509_AUX ,
58.Nm i2d_X509 , 57.Nm i2d_X509 ,
59.Nm i2d_X509_AUX ,
60.Nm d2i_X509_bio , 58.Nm d2i_X509_bio ,
61.Nm d2i_X509_fp , 59.Nm d2i_X509_fp ,
62.Nm i2d_X509_bio , 60.Nm i2d_X509_bio ,
63.Nm i2d_X509_fp 61.Nm i2d_X509_fp ,
64.Nd X509 encode and decode functions 62.Nm d2i_X509_AUX ,
63.Nm i2d_X509_AUX
64.Nd decode and encode X.509 certificates
65.Sh SYNOPSIS 65.Sh SYNOPSIS
66.In openssl/x509.h 66.In openssl/x509.h
67.Ft X509 * 67.Ft X509 *
68.Fo d2i_X509 68.Fo d2i_X509
69.Fa "X509 **px" 69.Fa "X509 **val_out"
70.Fa "const unsigned char **in" 70.Fa "const unsigned char **der_in"
71.Fa "long len" 71.Fa "long length"
72.Fc
73.Ft X509 *
74.Fo d2i_X509_AUX
75.Fa "X509 **px"
76.Fa "const unsigned char **in"
77.Fa "long len"
78.Fc 72.Fc
79.Ft int 73.Ft int
80.Fo i2d_X509 74.Fo i2d_X509
81.Fa "X509 *x" 75.Fa "X509 *val_in"
82.Fa "unsigned char **out" 76.Fa "unsigned char **der_out"
83.Fc
84.Ft int
85.Fo i2d_X509_AUX
86.Fa "X509 *x"
87.Fa "unsigned char **out"
88.Fc 77.Fc
89.Ft X509 * 78.Ft X509 *
90.Fo d2i_X509_bio 79.Fo d2i_X509_bio
91.Fa "BIO *bp" 80.Fa "BIO *in_bio"
92.Fa "X509 **x" 81.Fa "X509 **val_out"
93.Fc 82.Fc
94.Ft X509 * 83.Ft X509 *
95.Fo d2i_X509_fp 84.Fo d2i_X509_fp
96.Fa "FILE *fp" 85.Fa "FILE *in_fp"
97.Fa "X509 **x" 86.Fa "X509 **val_out"
98.Fc 87.Fc
99.Ft int 88.Ft int
100.Fo i2d_X509_bio 89.Fo i2d_X509_bio
101.Fa "BIO *bp" 90.Fa "BIO *out_bio"
102.Fa "X509 *x" 91.Fa "X509 *val_in"
103.Fc 92.Fc
104.Ft int 93.Ft int
105.Fo i2d_X509_fp 94.Fo i2d_X509_fp
106.Fa "FILE *fp" 95.Fa "FILE *out_fp"
107.Fa "X509 *x" 96.Fa "X509 *val_in"
97.Fc
98.Ft X509 *
99.Fo d2i_X509_AUX
100.Fa "X509 **val_out"
101.Fa "const unsigned char **der_in"
102.Fa "long length"
103.Fc
104.Ft int
105.Fo i2d_X509_AUX
106.Fa "X509 *val_in"
107.Fa "unsigned char **der_out"
108.Fc 108.Fc
109.Sh DESCRIPTION 109.Sh DESCRIPTION
110The X509 encode and decode routines encode and parse an 110These functions decode and encode X.509 certificates
111.Vt X509 111and some of their substructures.
112structure, which represents an X509 certificate. 112For details about the semantics, examples, caveats, and bugs, see
113.Xr ASN1_item_d2i 3 .
113.Pp 114.Pp
114.Fn d2i_X509 115.Fn d2i_X509
115attempts to decode
116.Fa len
117bytes at
118.Pf * Fa in .
119If successful, a pointer to the
120.Vt X509
121structure is returned.
122If an error occurred,
123.Dv NULL
124is returned.
125If
126.Fa px
127is not
128.Dv NULL ,
129the returned structure is written to
130.Pf * Fa px .
131If
132.Pf * Fa px
133is not
134.Dv NULL ,
135then it is assumed that
136.Pf * Fa px
137contains a valid
138.Vt X509
139structure and an attempt is made to reuse it.
140This "reuse" capability is present for historical compatibility,
141but its use is strongly discouraged; see the
142.Sx BUGS
143and 116and
144.Sx RETURN VALUES 117.Fn i2d_X509
145sections. 118decode and encode an ASN.1
146If the call is successful, 119.Vt Certificate
147.Pf * Fa in 120structure defined in RFC 5280 section 4.1.
148is incremented to the byte following the parsed data. 121.Pp
122.Fn d2i_X509_bio ,
123.Fn d2i_X509_fp ,
124.Fn i2d_X509_bio ,
125and
126.Fn i2d_X509_fp
127are similar except that they decode or encode using a
128.Vt BIO
129or
130.Vt FILE
131pointer.
149.Pp 132.Pp
150.Fn d2i_X509_AUX 133.Fn d2i_X509_AUX
151is similar to 134is similar to
@@ -155,29 +138,6 @@ by auxiliary trust information.
155This is used by the PEM routines to read TRUSTED CERTIFICATE objects. 138This is used by the PEM routines to read TRUSTED CERTIFICATE objects.
156This function should not be called on untrusted input. 139This function should not be called on untrusted input.
157.Pp 140.Pp
158.Fn i2d_X509
159encodes the structure pointed to by
160.Fa x
161into DER format.
162If
163.Fa out
164is not
165.Dv NULL ,
166it writes the DER encoded data to the buffer at
167.Pf * Fa out
168and increments it to point after the data just written.
169If the return value is negative an error occurred, otherwise it returns
170the length of the encoded data.
171.Pp
172For OpenSSL 0.9.7 and later if
173.Pf * Fa out
174is
175.Dv NULL ,
176memory will be allocated for a buffer and the encoded data written to it.
177In this case
178.Pf * Fa out
179is not incremented and it points to the start of the data just written.
180.Pp
181.Fn i2d_X509_AUX 141.Fn i2d_X509_AUX
182is similar to 142is similar to
183.Fn i2d_X509 , 143.Fn i2d_X509 ,
@@ -185,94 +145,12 @@ but the encoded output contains both the certificate and any auxiliary
185trust information. 145trust information.
186This is used by the PEM routines to write TRUSTED CERTIFICATE objects. 146This is used by the PEM routines to write TRUSTED CERTIFICATE objects.
187Note that this is a non-standard OpenSSL-specific data format. 147Note that this is a non-standard OpenSSL-specific data format.
188.Pp
189.Fn d2i_X509_bio
190is similar to
191.Fn d2i_X509
192except it attempts to parse data from
193.Vt BIO
194.Fa bp .
195.Pp
196.Fn d2i_X509_fp
197is similar to
198.Fn d2i_X509
199except it attempts to parse data from the
200.Vt FILE
201pointer
202.Fa fp .
203.Pp
204.Fn i2d_X509_bio
205is similar to
206.Fn i2d_X509
207except it writes the encoding of the structure
208.Fa x
209to
210.Vt BIO
211.Fa bp
212and it returns 1 for success or 0 for failure.
213.Pp
214.Fn i2d_X509_fp
215is similar to
216.Fn i2d_X509
217except it writes the encoding of the structure
218.Fa x
219to
220.Vt BIO
221.Fa bp
222and it returns 1 for success or 0 for failure.
223.Pp
224The letters
225.Sy i
226and
227.Sy d
228in, for example,
229.Fn i2d_X509
230stand for "internal" (that is an internal C structure) and "DER",
231so that
232.Fn i2d_X509
233converts from internal to DER.
234.Pp
235The functions can also understand BER forms.
236.Pp
237The actual
238.Vt X509
239structure passed to
240.Fn i2d_X509
241must be a valid populated
242.Vt X509
243structure.
244It cannot simply be fed with an empty structure such as that returned by
245.Xr X509_new 3 .
246.Pp
247The encoded data is in binary form and may contain embedded zeroes.
248Therefore any
249.Vt FILE
250pointers or
251.Vt BIO Ns s
252should be opened in binary mode.
253Functions such as
254.Xr strlen 3
255will
256.Sy not
257return the correct length of the encoded structure.
258.Pp
259The ways that
260.Pf * Fa in
261and
262.Pf * Fa out
263are incremented after the operation can trap the unwary.
264See the
265.Sx CAVEATS
266section for some common errors.
267.Pp
268The reason for the auto increment behaviour is to reflect a typical
269usage of ASN.1 functions: after one structure is encoded or decoded,
270another will be processed after it.
271.Sh RETURN VALUES 148.Sh RETURN VALUES
272.Fn d2i_X509 , 149.Fn d2i_X509 ,
273.Fn d2i_X509_bio , 150.Fn d2i_X509_bio ,
151.Fn d2i_X509_fp ,
274and 152and
275.Fn d2i_X509_fp 153.Fn d2i_X509_AUX
276return a valid 154return a valid
277.Vt X509 155.Vt X509
278structure or 156structure or
@@ -280,8 +158,10 @@ structure or
280if an error occurs. 158if an error occurs.
281.Pp 159.Pp
282.Fn i2d_X509 160.Fn i2d_X509
283returns the number of bytes successfully encoded or a negative value if 161and
284an error occurs. 162.Fn i2d_X509_AUX
163return the number of bytes successfully encoded or a negative value
164if an error occurs.
285.Pp 165.Pp
286.Fn i2d_X509_bio 166.Fn i2d_X509_bio
287and 167and
@@ -290,64 +170,12 @@ return 1 for success or 0 if an error occurs.
290.Pp 170.Pp
291For all functions, the error code can be obtained by 171For all functions, the error code can be obtained by
292.Xr ERR_get_error 3 . 172.Xr ERR_get_error 3 .
293If the "reuse" capability has been used with a valid
294.Vt X509
295structure being passed in via
296.Fa px ,
297then the object is not freed in the event of an error, but may be
298in a potentially invalid or inconsistent state.
299.Sh EXAMPLES
300Allocate and encode the DER encoding of an X509 structure:
301.Bd -literal -offset indent
302int len;
303unsigned char *buf, *p;
304
305len = i2d_X509(x, NULL);
306buf = malloc(len);
307if (buf == NULL)
308 /* error */
309p = buf;
310i2d_X509(x, &p);
311.Ed
312.Pp
313Using OpenSSL 0.9.7 or later this can be simplified to:
314.Bd -literal -offset indent
315int len;
316unsigned char *buf;
317
318buf = NULL;
319len = i2d_X509(x, &buf);
320if (len < 0)
321 /* error */
322.Ed
323.Pp
324Attempt to decode a buffer:
325.Bd -literal -offset indent
326X509 *x;
327unsigned char *buf, *p;
328int len;
329
330/* Something to setup buf and len */
331p = buf;
332x = d2i_X509(NULL, &p, len);
333if (x == NULL)
334 /* Some error */
335.Ed
336.Pp
337Alternative technique:
338.Bd -literal -offset indent
339X509 *x;
340unsigned char *buf, *p;
341int len;
342
343/* Something to setup buf and len */
344p = buf;
345x = NULL;
346if(!d2i_X509(&x, &p, len))
347 /* Some error */
348.Ed
349.Sh SEE ALSO 173.Sh SEE ALSO
350.Xr ERR_get_error 3 174.Xr ASN1_item_d2i 3 ,
175.Xr X509_new 3
176.Sh STANDARDS
177RFC 5280: Internet X.509 Public Key Infrastructure Certificate and
178Certificate Revocation List (CRL) Profile
351.Sh HISTORY 179.Sh HISTORY
352.Fn d2i_X509 , 180.Fn d2i_X509 ,
353.Fn i2d_X509 , 181.Fn i2d_X509 ,
@@ -357,82 +185,3 @@ if(!d2i_X509(&x, &p, len))
357and 185and
358.Fn i2d_X509_fp 186.Fn i2d_X509_fp
359are available in all versions of SSLeay and OpenSSL. 187are available in all versions of SSLeay and OpenSSL.
360.Sh CAVEATS
361The use of a temporary variable is mandatory.
362A common mistake is to attempt to use a buffer directly as follows:
363.Bd -literal -offset indent
364int len;
365unsigned char *buf;
366
367len = i2d_X509(x, NULL);
368buf = malloc(len);
369if (buf == NULL)
370 /* error */
371i2d_X509(x, &buf);
372/* Other stuff ... */
373free(buf);
374.Ed
375.Pp
376This code will result in
377.Fa buf
378apparently containing garbage because it was incremented after the
379call to point after the data just written.
380Also
381.Fa buf
382will no longer contain the pointer allocated by
383.Xr malloc 3
384and the subsequent call to
385.Xr free 3
386may well crash.
387.Pp
388The auto allocation feature (setting
389.Fa buf
390to
391.Dv NULL )
392only works on OpenSSL 0.9.7 and later.
393Attempts to use it on earlier versions will typically cause a
394segmentation violation.
395.Pp
396Another trap to avoid is misuse of the
397.Fa px
398argument to
399.Sy d2i_X509() :
400.Bd -literal -offset indent
401X509 *x;
402
403if (!d2i_X509(&x, &p, len))
404 /* Some error */
405.Ed
406.Pp
407This will probably crash somewhere in
408.Fn d2i_X509 .
409The reason for this is that the variable
410.Fa x
411is uninitialized and an attempt will be made to interpret its (invalid)
412value as an
413.Vt X509
414structure, typically causing a segmentation violation.
415If
416.Fa x
417is set to
418.Dv NULL
419first then this will not happen.
420.Sh BUGS
421In some versions of OpenSSL the "reuse" behaviour of
422.Fn d2i_X509
423when
424.Pf * Fa px
425is valid is broken and some parts of the reused structure may persist
426if they are not present in the new one.
427As a result the use of this "reuse" behaviour is strongly discouraged.
428.Pp
429In many versions of OpenSSL,
430.Fn i2d_X509
431will not return an error if mandatory fields are not initialized
432due to a programming error.
433Then the encoded structure may contain invalid data or omit the
434fields entirely and will not be parsed by
435.Fn d2i_X509 .
436This may be fixed in future so code should not assume that
437.Fn i2d_X509
438will always succeed.