diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/lib/libcrypto/man/d2i_X509.3 | 371 |
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 |
| 110 | The X509 encode and decode routines encode and parse an | 110 | These functions decode and encode X.509 certificates |
| 111 | .Vt X509 | 111 | and some of their substructures. |
| 112 | structure, which represents an X509 certificate. | 112 | For 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 |
| 115 | attempts to decode | ||
| 116 | .Fa len | ||
| 117 | bytes at | ||
| 118 | .Pf * Fa in . | ||
| 119 | If successful, a pointer to the | ||
| 120 | .Vt X509 | ||
| 121 | structure is returned. | ||
| 122 | If an error occurred, | ||
| 123 | .Dv NULL | ||
| 124 | is returned. | ||
| 125 | If | ||
| 126 | .Fa px | ||
| 127 | is not | ||
| 128 | .Dv NULL , | ||
| 129 | the returned structure is written to | ||
| 130 | .Pf * Fa px . | ||
| 131 | If | ||
| 132 | .Pf * Fa px | ||
| 133 | is not | ||
| 134 | .Dv NULL , | ||
| 135 | then it is assumed that | ||
| 136 | .Pf * Fa px | ||
| 137 | contains a valid | ||
| 138 | .Vt X509 | ||
| 139 | structure and an attempt is made to reuse it. | ||
| 140 | This "reuse" capability is present for historical compatibility, | ||
| 141 | but its use is strongly discouraged; see the | ||
| 142 | .Sx BUGS | ||
| 143 | and | 116 | and |
| 144 | .Sx RETURN VALUES | 117 | .Fn i2d_X509 |
| 145 | sections. | 118 | decode and encode an ASN.1 |
| 146 | If the call is successful, | 119 | .Vt Certificate |
| 147 | .Pf * Fa in | 120 | structure defined in RFC 5280 section 4.1. |
| 148 | is 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 , | ||
| 125 | and | ||
| 126 | .Fn i2d_X509_fp | ||
| 127 | are similar except that they decode or encode using a | ||
| 128 | .Vt BIO | ||
| 129 | or | ||
| 130 | .Vt FILE | ||
| 131 | pointer. | ||
| 149 | .Pp | 132 | .Pp |
| 150 | .Fn d2i_X509_AUX | 133 | .Fn d2i_X509_AUX |
| 151 | is similar to | 134 | is similar to |
| @@ -155,29 +138,6 @@ by auxiliary trust information. | |||
| 155 | This is used by the PEM routines to read TRUSTED CERTIFICATE objects. | 138 | This is used by the PEM routines to read TRUSTED CERTIFICATE objects. |
| 156 | This function should not be called on untrusted input. | 139 | This function should not be called on untrusted input. |
| 157 | .Pp | 140 | .Pp |
| 158 | .Fn i2d_X509 | ||
| 159 | encodes the structure pointed to by | ||
| 160 | .Fa x | ||
| 161 | into DER format. | ||
| 162 | If | ||
| 163 | .Fa out | ||
| 164 | is not | ||
| 165 | .Dv NULL , | ||
| 166 | it writes the DER encoded data to the buffer at | ||
| 167 | .Pf * Fa out | ||
| 168 | and increments it to point after the data just written. | ||
| 169 | If the return value is negative an error occurred, otherwise it returns | ||
| 170 | the length of the encoded data. | ||
| 171 | .Pp | ||
| 172 | For OpenSSL 0.9.7 and later if | ||
| 173 | .Pf * Fa out | ||
| 174 | is | ||
| 175 | .Dv NULL , | ||
| 176 | memory will be allocated for a buffer and the encoded data written to it. | ||
| 177 | In this case | ||
| 178 | .Pf * Fa out | ||
| 179 | is 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 |
| 182 | is similar to | 142 | is 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 | |||
| 185 | trust information. | 145 | trust information. |
| 186 | This is used by the PEM routines to write TRUSTED CERTIFICATE objects. | 146 | This is used by the PEM routines to write TRUSTED CERTIFICATE objects. |
| 187 | Note that this is a non-standard OpenSSL-specific data format. | 147 | Note that this is a non-standard OpenSSL-specific data format. |
| 188 | .Pp | ||
| 189 | .Fn d2i_X509_bio | ||
| 190 | is similar to | ||
| 191 | .Fn d2i_X509 | ||
| 192 | except it attempts to parse data from | ||
| 193 | .Vt BIO | ||
| 194 | .Fa bp . | ||
| 195 | .Pp | ||
| 196 | .Fn d2i_X509_fp | ||
| 197 | is similar to | ||
| 198 | .Fn d2i_X509 | ||
| 199 | except it attempts to parse data from the | ||
| 200 | .Vt FILE | ||
| 201 | pointer | ||
| 202 | .Fa fp . | ||
| 203 | .Pp | ||
| 204 | .Fn i2d_X509_bio | ||
| 205 | is similar to | ||
| 206 | .Fn i2d_X509 | ||
| 207 | except it writes the encoding of the structure | ||
| 208 | .Fa x | ||
| 209 | to | ||
| 210 | .Vt BIO | ||
| 211 | .Fa bp | ||
| 212 | and it returns 1 for success or 0 for failure. | ||
| 213 | .Pp | ||
| 214 | .Fn i2d_X509_fp | ||
| 215 | is similar to | ||
| 216 | .Fn i2d_X509 | ||
| 217 | except it writes the encoding of the structure | ||
| 218 | .Fa x | ||
| 219 | to | ||
| 220 | .Vt BIO | ||
| 221 | .Fa bp | ||
| 222 | and it returns 1 for success or 0 for failure. | ||
| 223 | .Pp | ||
| 224 | The letters | ||
| 225 | .Sy i | ||
| 226 | and | ||
| 227 | .Sy d | ||
| 228 | in, for example, | ||
| 229 | .Fn i2d_X509 | ||
| 230 | stand for "internal" (that is an internal C structure) and "DER", | ||
| 231 | so that | ||
| 232 | .Fn i2d_X509 | ||
| 233 | converts from internal to DER. | ||
| 234 | .Pp | ||
| 235 | The functions can also understand BER forms. | ||
| 236 | .Pp | ||
| 237 | The actual | ||
| 238 | .Vt X509 | ||
| 239 | structure passed to | ||
| 240 | .Fn i2d_X509 | ||
| 241 | must be a valid populated | ||
| 242 | .Vt X509 | ||
| 243 | structure. | ||
| 244 | It cannot simply be fed with an empty structure such as that returned by | ||
| 245 | .Xr X509_new 3 . | ||
| 246 | .Pp | ||
| 247 | The encoded data is in binary form and may contain embedded zeroes. | ||
| 248 | Therefore any | ||
| 249 | .Vt FILE | ||
| 250 | pointers or | ||
| 251 | .Vt BIO Ns s | ||
| 252 | should be opened in binary mode. | ||
| 253 | Functions such as | ||
| 254 | .Xr strlen 3 | ||
| 255 | will | ||
| 256 | .Sy not | ||
| 257 | return the correct length of the encoded structure. | ||
| 258 | .Pp | ||
| 259 | The ways that | ||
| 260 | .Pf * Fa in | ||
| 261 | and | ||
| 262 | .Pf * Fa out | ||
| 263 | are incremented after the operation can trap the unwary. | ||
| 264 | See the | ||
| 265 | .Sx CAVEATS | ||
| 266 | section for some common errors. | ||
| 267 | .Pp | ||
| 268 | The reason for the auto increment behaviour is to reflect a typical | ||
| 269 | usage of ASN.1 functions: after one structure is encoded or decoded, | ||
| 270 | another 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 , | ||
| 274 | and | 152 | and |
| 275 | .Fn d2i_X509_fp | 153 | .Fn d2i_X509_AUX |
| 276 | return a valid | 154 | return a valid |
| 277 | .Vt X509 | 155 | .Vt X509 |
| 278 | structure or | 156 | structure or |
| @@ -280,8 +158,10 @@ structure or | |||
| 280 | if an error occurs. | 158 | if an error occurs. |
| 281 | .Pp | 159 | .Pp |
| 282 | .Fn i2d_X509 | 160 | .Fn i2d_X509 |
| 283 | returns the number of bytes successfully encoded or a negative value if | 161 | and |
| 284 | an error occurs. | 162 | .Fn i2d_X509_AUX |
| 163 | return the number of bytes successfully encoded or a negative value | ||
| 164 | if an error occurs. | ||
| 285 | .Pp | 165 | .Pp |
| 286 | .Fn i2d_X509_bio | 166 | .Fn i2d_X509_bio |
| 287 | and | 167 | and |
| @@ -290,64 +170,12 @@ return 1 for success or 0 if an error occurs. | |||
| 290 | .Pp | 170 | .Pp |
| 291 | For all functions, the error code can be obtained by | 171 | For all functions, the error code can be obtained by |
| 292 | .Xr ERR_get_error 3 . | 172 | .Xr ERR_get_error 3 . |
| 293 | If the "reuse" capability has been used with a valid | ||
| 294 | .Vt X509 | ||
| 295 | structure being passed in via | ||
| 296 | .Fa px , | ||
| 297 | then the object is not freed in the event of an error, but may be | ||
| 298 | in a potentially invalid or inconsistent state. | ||
| 299 | .Sh EXAMPLES | ||
| 300 | Allocate and encode the DER encoding of an X509 structure: | ||
| 301 | .Bd -literal -offset indent | ||
| 302 | int len; | ||
| 303 | unsigned char *buf, *p; | ||
| 304 | |||
| 305 | len = i2d_X509(x, NULL); | ||
| 306 | buf = malloc(len); | ||
| 307 | if (buf == NULL) | ||
| 308 | /* error */ | ||
| 309 | p = buf; | ||
| 310 | i2d_X509(x, &p); | ||
| 311 | .Ed | ||
| 312 | .Pp | ||
| 313 | Using OpenSSL 0.9.7 or later this can be simplified to: | ||
| 314 | .Bd -literal -offset indent | ||
| 315 | int len; | ||
| 316 | unsigned char *buf; | ||
| 317 | |||
| 318 | buf = NULL; | ||
| 319 | len = i2d_X509(x, &buf); | ||
| 320 | if (len < 0) | ||
| 321 | /* error */ | ||
| 322 | .Ed | ||
| 323 | .Pp | ||
| 324 | Attempt to decode a buffer: | ||
| 325 | .Bd -literal -offset indent | ||
| 326 | X509 *x; | ||
| 327 | unsigned char *buf, *p; | ||
| 328 | int len; | ||
| 329 | |||
| 330 | /* Something to setup buf and len */ | ||
| 331 | p = buf; | ||
| 332 | x = d2i_X509(NULL, &p, len); | ||
| 333 | if (x == NULL) | ||
| 334 | /* Some error */ | ||
| 335 | .Ed | ||
| 336 | .Pp | ||
| 337 | Alternative technique: | ||
| 338 | .Bd -literal -offset indent | ||
| 339 | X509 *x; | ||
| 340 | unsigned char *buf, *p; | ||
| 341 | int len; | ||
| 342 | |||
| 343 | /* Something to setup buf and len */ | ||
| 344 | p = buf; | ||
| 345 | x = NULL; | ||
| 346 | if(!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 | ||
| 177 | RFC 5280: Internet X.509 Public Key Infrastructure Certificate and | ||
| 178 | Certificate 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)) | |||
| 357 | and | 185 | and |
| 358 | .Fn i2d_X509_fp | 186 | .Fn i2d_X509_fp |
| 359 | are available in all versions of SSLeay and OpenSSL. | 187 | are available in all versions of SSLeay and OpenSSL. |
| 360 | .Sh CAVEATS | ||
| 361 | The use of a temporary variable is mandatory. | ||
| 362 | A common mistake is to attempt to use a buffer directly as follows: | ||
| 363 | .Bd -literal -offset indent | ||
| 364 | int len; | ||
| 365 | unsigned char *buf; | ||
| 366 | |||
| 367 | len = i2d_X509(x, NULL); | ||
| 368 | buf = malloc(len); | ||
| 369 | if (buf == NULL) | ||
| 370 | /* error */ | ||
| 371 | i2d_X509(x, &buf); | ||
| 372 | /* Other stuff ... */ | ||
| 373 | free(buf); | ||
| 374 | .Ed | ||
| 375 | .Pp | ||
| 376 | This code will result in | ||
| 377 | .Fa buf | ||
| 378 | apparently containing garbage because it was incremented after the | ||
| 379 | call to point after the data just written. | ||
| 380 | Also | ||
| 381 | .Fa buf | ||
| 382 | will no longer contain the pointer allocated by | ||
| 383 | .Xr malloc 3 | ||
| 384 | and the subsequent call to | ||
| 385 | .Xr free 3 | ||
| 386 | may well crash. | ||
| 387 | .Pp | ||
| 388 | The auto allocation feature (setting | ||
| 389 | .Fa buf | ||
| 390 | to | ||
| 391 | .Dv NULL ) | ||
| 392 | only works on OpenSSL 0.9.7 and later. | ||
| 393 | Attempts to use it on earlier versions will typically cause a | ||
| 394 | segmentation violation. | ||
| 395 | .Pp | ||
| 396 | Another trap to avoid is misuse of the | ||
| 397 | .Fa px | ||
| 398 | argument to | ||
| 399 | .Sy d2i_X509() : | ||
| 400 | .Bd -literal -offset indent | ||
| 401 | X509 *x; | ||
| 402 | |||
| 403 | if (!d2i_X509(&x, &p, len)) | ||
| 404 | /* Some error */ | ||
| 405 | .Ed | ||
| 406 | .Pp | ||
| 407 | This will probably crash somewhere in | ||
| 408 | .Fn d2i_X509 . | ||
| 409 | The reason for this is that the variable | ||
| 410 | .Fa x | ||
| 411 | is uninitialized and an attempt will be made to interpret its (invalid) | ||
| 412 | value as an | ||
| 413 | .Vt X509 | ||
| 414 | structure, typically causing a segmentation violation. | ||
| 415 | If | ||
| 416 | .Fa x | ||
| 417 | is set to | ||
| 418 | .Dv NULL | ||
| 419 | first then this will not happen. | ||
| 420 | .Sh BUGS | ||
| 421 | In some versions of OpenSSL the "reuse" behaviour of | ||
| 422 | .Fn d2i_X509 | ||
| 423 | when | ||
| 424 | .Pf * Fa px | ||
| 425 | is valid is broken and some parts of the reused structure may persist | ||
| 426 | if they are not present in the new one. | ||
| 427 | As a result the use of this "reuse" behaviour is strongly discouraged. | ||
| 428 | .Pp | ||
| 429 | In many versions of OpenSSL, | ||
| 430 | .Fn i2d_X509 | ||
| 431 | will not return an error if mandatory fields are not initialized | ||
| 432 | due to a programming error. | ||
| 433 | Then the encoded structure may contain invalid data or omit the | ||
| 434 | fields entirely and will not be parsed by | ||
| 435 | .Fn d2i_X509 . | ||
| 436 | This may be fixed in future so code should not assume that | ||
| 437 | .Fn i2d_X509 | ||
| 438 | will always succeed. | ||
