1.\" $OpenBSD: d2i_X509.3,v 1.11 2021/10/27 10:35:43 schwarze Exp $ 2.\" OpenSSL d2i_X509.pod checked up to: 3.\" 256989ce4 Jun 19 15:00:32 2020 +0200 4.\" OpenSSL i2d_re_X509_tbs.pod checked up to: 5.\" 61f805c1 Jan 16 01:01:46 2018 +0800 6.\" 7.\" This file is a derived work. 8.\" The changes are covered by the following Copyright and license: 9.\" 10.\" Copyright (c) 2016 Ingo Schwarze <schwarze@openbsd.org> 11.\" 12.\" Permission to use, copy, modify, and distribute this software for any 13.\" purpose with or without fee is hereby granted, provided that the above 14.\" copyright notice and this permission notice appear in all copies. 15.\" 16.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 17.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 18.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 19.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 20.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 21.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 22.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 23.\" 24.\" The original files were written by Dr. Stephen Henson <steve@openssl.org>, 25.\" Emilia Kasper <emilia@openssl.org>, Viktor Dukhovni <viktor@openssl.org>, 26.\" and Rich Salz <rsalz@openssl.org>. 27.\" Copyright (c) 2002, 2014, 2016 The OpenSSL Project. 28.\" All rights reserved. 29.\" 30.\" Redistribution and use in source and binary forms, with or without 31.\" modification, are permitted provided that the following conditions 32.\" are met: 33.\" 34.\" 1. Redistributions of source code must retain the above copyright 35.\" notice, this list of conditions and the following disclaimer. 36.\" 37.\" 2. Redistributions in binary form must reproduce the above copyright 38.\" notice, this list of conditions and the following disclaimer in 39.\" the documentation and/or other materials provided with the 40.\" distribution. 41.\" 42.\" 3. All advertising materials mentioning features or use of this 43.\" software must display the following acknowledgment: 44.\" "This product includes software developed by the OpenSSL Project 45.\" for use in the OpenSSL Toolkit. (http://www.openssl.org/)" 46.\" 47.\" 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 48.\" endorse or promote products derived from this software without 49.\" prior written permission. For written permission, please contact 50.\" openssl-core@openssl.org. 51.\" 52.\" 5. Products derived from this software may not be called "OpenSSL" 53.\" nor may "OpenSSL" appear in their names without prior written 54.\" permission of the OpenSSL Project. 55.\" 56.\" 6. Redistributions of any form whatsoever must retain the following 57.\" acknowledgment: 58.\" "This product includes software developed by the OpenSSL Project 59.\" for use in the OpenSSL Toolkit (http://www.openssl.org/)" 60.\" 61.\" THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 62.\" EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 63.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 64.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 65.\" ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 66.\" SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 67.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 68.\" LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 69.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 70.\" STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 71.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 72.\" OF THE POSSIBILITY OF SUCH DAMAGE. 73.\" 74.Dd $Mdocdate: October 27 2021 $ 75.Dt D2I_X509 3 76.Os 77.Sh NAME 78.Nm d2i_X509 , 79.Nm i2d_X509 , 80.Nm d2i_X509_bio , 81.Nm d2i_X509_fp , 82.Nm i2d_X509_bio , 83.Nm i2d_X509_fp , 84.Nm d2i_X509_AUX , 85.Nm i2d_X509_AUX , 86.Nm d2i_X509_CERT_AUX , 87.Nm i2d_X509_CERT_AUX , 88.Nm d2i_X509_CINF , 89.Nm i2d_X509_CINF , 90.Nm d2i_X509_VAL , 91.Nm i2d_X509_VAL , 92.Nm i2d_re_X509_tbs , 93.Nm i2d_re_X509_CRL_tbs , 94.Nm i2d_re_X509_REQ_tbs 95.Nd decode and encode X.509 certificates 96.Sh SYNOPSIS 97.In openssl/x509.h 98.Ft X509 * 99.Fo d2i_X509 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 106.Fa "X509 *val_in" 107.Fa "unsigned char **der_out" 108.Fc 109.Ft X509 * 110.Fo d2i_X509_bio 111.Fa "BIO *in_bio" 112.Fa "X509 **val_out" 113.Fc 114.Ft X509 * 115.Fo d2i_X509_fp 116.Fa "FILE *in_fp" 117.Fa "X509 **val_out" 118.Fc 119.Ft int 120.Fo i2d_X509_bio 121.Fa "BIO *out_bio" 122.Fa "X509 *val_in" 123.Fc 124.Ft int 125.Fo i2d_X509_fp 126.Fa "FILE *out_fp" 127.Fa "X509 *val_in" 128.Fc 129.Ft X509 * 130.Fo d2i_X509_AUX 131.Fa "X509 **val_out" 132.Fa "const unsigned char **der_in" 133.Fa "long length" 134.Fc 135.Ft int 136.Fo i2d_X509_AUX 137.Fa "X509 *val_in" 138.Fa "unsigned char **der_out" 139.Fc 140.Ft X509_CERT_AUX * 141.Fo d2i_X509_CERT_AUX 142.Fa "X509_CERT_AUX **val_out" 143.Fa "const unsigned char **der_in" 144.Fa "long length" 145.Fc 146.Ft int 147.Fo i2d_X509_CERT_AUX 148.Fa "X509_CERT_AUX *val_in" 149.Fa "unsigned char **der_out" 150.Fc 151.Ft X509_CINF * 152.Fo d2i_X509_CINF 153.Fa "X509_CINF **val_out" 154.Fa "const unsigned char **der_in" 155.Fa "long length" 156.Fc 157.Ft int 158.Fo i2d_X509_CINF 159.Fa "X509_CINF *val_in" 160.Fa "unsigned char **der_out" 161.Fc 162.Ft X509_VAL * 163.Fo d2i_X509_VAL 164.Fa "X509_VAL **val_out" 165.Fa "const unsigned char **der_in" 166.Fa "long length" 167.Fc 168.Ft int 169.Fo i2d_X509_VAL 170.Fa "X509_VAL *val_in" 171.Fa "unsigned char **der_out" 172.Fc 173.Ft int 174.Fo i2d_re_X509_tbs 175.Fa "X509 *x" 176.Fa "unsigned char **out" 177.Fc 178.Ft int 179.Fo i2d_re_X509_CRL_tbs 180.Fa "X509_CRL *crl" 181.Fa "unsigned char **pp" 182.Fc 183.Ft int 184.Fo i2d_re_X509_REQ_tbs 185.Fa "X509_REQ *req" 186.Fa "unsigned char **pp" 187.Fc 188.Sh DESCRIPTION 189These functions decode and encode X.509 certificates 190and some of their substructures. 191For details about the semantics, examples, caveats, and bugs, see 192.Xr ASN1_item_d2i 3 . 193.Pp 194.Fn d2i_X509 195and 196.Fn i2d_X509 197decode and encode an ASN.1 198.Vt Certificate 199structure defined in RFC 5280 section 4.1. 200.Pp 201.Fn d2i_X509_bio , 202.Fn d2i_X509_fp , 203.Fn i2d_X509_bio , 204and 205.Fn i2d_X509_fp 206are similar except that they decode or encode using a 207.Vt BIO 208or 209.Vt FILE 210pointer. 211.Pp 212.Fn d2i_X509_AUX 213is similar to 214.Fn d2i_X509 , 215but the input is expected to consist of an X.509 certificate followed 216by auxiliary trust information. 217This is used by the PEM routines to read TRUSTED CERTIFICATE objects. 218This function should not be called on untrusted input. 219.Pp 220.Fn i2d_X509_AUX 221is similar to 222.Fn i2d_X509 , 223but the encoded output contains both the certificate and any auxiliary 224trust information. 225This is used by the PEM routines to write TRUSTED CERTIFICATE objects. 226Note that this is a non-standard OpenSSL-specific data format. 227.Pp 228.Fn d2i_X509_CERT_AUX 229and 230.Fn i2d_X509_CERT_AUX 231decode and encode optional non-standard auxiliary data appended to 232a certificate, for example friendly alias names and trust data. 233.Pp 234.Fn d2i_X509_CINF 235and 236.Fn i2d_X509_CINF 237decode and encode an ASN.1 238.Vt TBSCertificate 239structure defined in RFC 5280 section 4.1. 240.Pp 241.Fn d2i_X509_VAL 242and 243.Fn i2d_X509_VAL 244decode and encode an ASN.1 245.Vt Validity 246structure defined in RFC 5280 section 4.1. 247.Pp 248.Fn i2d_re_X509_tbs 249is similar to 250.Fn i2d_X509 , 251except it encodes only the TBSCertificate portion of the certificate. 252.Fn i2d_re_X509_CRL_tbs 253and 254.Fn i2d_re_X509_REQ_tbs 255are analogous for CRL and certificate request, respectively. 256The "re" in 257.Fn i2d_re_X509_tbs 258stands for "re-encode", and ensures that a fresh encoding is generated 259in case the object has been modified after creation. 260.Pp 261The encoding of the TBSCertificate portion of a certificate is cached in 262the 263.Vt X509 264structure internally to improve encoding performance and to ensure 265certificate signatures are verified correctly in some certificates with 266broken (non-DER) encodings. 267.Pp 268If, after modification, the 269.Vt X509 270object is re-signed with 271.Xr X509_sign 3 , 272the encoding is automatically renewed. 273Otherwise, the encoding of the TBSCertificate portion of the 274.Vt X509 275can be manually renewed by calling 276.Fn i2d_re_X509_tbs . 277.Sh RETURN VALUES 278.Fn d2i_X509 , 279.Fn d2i_X509_bio , 280.Fn d2i_X509_fp , 281and 282.Fn d2i_X509_AUX 283return a valid 284.Vt X509 285structure or 286.Dv NULL 287if an error occurs. 288.Pp 289.Fn d2i_X509_CERT_AUX , 290.Fn d2i_X509_CINF , 291and 292.Fn d2i_X509_VAL 293return an 294.Vt X509_CERT_AUX , 295.Vt X509_CINF , 296or 297.Vt X509_VAL 298object, respectively, or 299.Dv NULL 300if an error occurs. 301.Pp 302.Fn i2d_X509 , 303.Fn i2d_X509_AUX , 304.Fn i2d_X509_CERT_AUX , 305.Fn i2d_X509_CINF , 306and 307.Fn i2d_X509_VAL 308return the number of bytes successfully encoded or a negative value 309if an error occurs. 310.Pp 311.Fn i2d_X509_bio 312and 313.Fn i2d_X509_fp 314return 1 for success or 0 if an error occurs. 315.Pp 316.Fn i2d_re_X509_tbs , 317.Fn i2d_re_X509_CRL_tbs , 318and 319.Fn i2d_re_X509_REQ_tbs 320return the number of bytes successfully encoded or 0 if an error occurs. 321.Pp 322For all functions, the error code can be obtained by 323.Xr ERR_get_error 3 . 324.Sh SEE ALSO 325.Xr ASN1_item_d2i 3 , 326.Xr X509_CINF_new 3 , 327.Xr X509_new 3 328.Sh STANDARDS 329RFC 5280: Internet X.509 Public Key Infrastructure Certificate and 330Certificate Revocation List (CRL) Profile 331.Sh HISTORY 332.Fn d2i_X509 , 333.Fn i2d_X509 , 334.Fn d2i_X509_fp , 335.Fn i2d_X509_fp , 336.Fn d2i_X509_CINF , 337.Fn i2d_X509_CINF , 338.Fn d2i_X509_VAL , 339and 340.Fn i2d_X509_VAL 341first appeared in SSLeay 0.5.1. 342.Fn d2i_X509_bio 343and 344.Fn i2d_X509_bio 345first appeared in SSLeay 0.6.0. 346These functions have been available since 347.Ox 2.4 . 348.Pp 349.Fn d2i_X509_AUX , 350.Fn i2d_X509_AUX , 351.Fn d2i_X509_CERT_AUX , 352and 353.Fn i2d_X509_CERT_AUX 354first appeared in OpenSSL 0.9.5 and have been available since 355.Ox 2.7 . 356.Pp 357.Fn i2d_re_X509_tbs , 358.Fn i2d_re_X509_CRL_tbs , 359and 360.Fn i2d_re_X509_REQ_tbs 361first appeared in OpenSSL 1.1.0 and have been available since 362.Ox 7.1 . 363