| 1 | .\" Automatically generated by Pod::Man 2.16 (Pod::Simple 3.05) |
| 2 | .\" |
| 3 | .\" Standard preamble: |
| 4 | .\" ======================================================================== |
| 5 | .de Sh \" Subsection heading |
| 6 | .br |
| 7 | .if t .Sp |
| 8 | .ne 5 |
| 9 | .PP |
| 10 | \fB\\$1\fR |
| 11 | .PP |
| 12 | .. |
| 13 | .de Sp \" Vertical space (when we can't use .PP) |
| 14 | .if t .sp .5v |
| 15 | .if n .sp |
| 16 | .. |
| 17 | .de Vb \" Begin verbatim text |
| 18 | .ft CW |
| 19 | .nf |
| 20 | .ne \\$1 |
| 21 | .. |
| 22 | .de Ve \" End verbatim text |
| 23 | .ft R |
| 24 | .fi |
| 25 | .. |
| 26 | .\" Set up some character translations and predefined strings. \*(-- will |
| 27 | .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left |
| 28 | .\" double quote, and \*(R" will give a right double quote. \*(C+ will |
| 29 | .\" give a nicer C++. Capital omega is used to do unbreakable dashes and |
| 30 | .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, |
| 31 | .\" nothing in troff, for use with C<>. |
| 32 | .tr \(*W- |
| 33 | .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' |
| 34 | .ie n \{\ |
| 35 | . ds -- \(*W- |
| 36 | . ds PI pi |
| 37 | . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch |
| 38 | . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch |
| 39 | . ds L" "" |
| 40 | . ds R" "" |
| 41 | . ds C` "" |
| 42 | . ds C' "" |
| 43 | 'br\} |
| 44 | .el\{\ |
| 45 | . ds -- \|\(em\| |
| 46 | . ds PI \(*p |
| 47 | . ds L" `` |
| 48 | . ds R" '' |
| 49 | 'br\} |
| 50 | .\" |
| 51 | .\" Escape single quotes in literal strings from groff's Unicode transform. |
| 52 | .ie \n(.g .ds Aq \(aq |
| 53 | .el .ds Aq ' |
| 54 | .\" |
| 55 | .\" If the F register is turned on, we'll generate index entries on stderr for |
| 56 | .\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index |
| 57 | .\" entries marked with X<> in POD. Of course, you'll have to process the |
| 58 | .\" output yourself in some meaningful fashion. |
| 59 | .ie \nF \{\ |
| 60 | . de IX |
| 61 | . tm Index:\\$1\t\\n%\t"\\$2" |
| 62 | .. |
| 63 | . nr % 0 |
| 64 | . rr F |
| 65 | .\} |
| 66 | .el \{\ |
| 67 | . de IX |
| 68 | .. |
| 69 | .\} |
| 70 | .\" |
| 71 | .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). |
| 72 | .\" Fear. Run. Save yourself. No user-serviceable parts. |
| 73 | . \" fudge factors for nroff and troff |
| 74 | .if n \{\ |
| 75 | . ds #H 0 |
| 76 | . ds #V .8m |
| 77 | . ds #F .3m |
| 78 | . ds #[ \f1 |
| 79 | . ds #] \fP |
| 80 | .\} |
| 81 | .if t \{\ |
| 82 | . ds #H ((1u-(\\\\n(.fu%2u))*.13m) |
| 83 | . ds #V .6m |
| 84 | . ds #F 0 |
| 85 | . ds #[ \& |
| 86 | . ds #] \& |
| 87 | .\} |
| 88 | . \" simple accents for nroff and troff |
| 89 | .if n \{\ |
| 90 | . ds ' \& |
| 91 | . ds ` \& |
| 92 | . ds ^ \& |
| 93 | . ds , \& |
| 94 | . ds ~ ~ |
| 95 | . ds / |
| 96 | .\} |
| 97 | .if t \{\ |
| 98 | . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" |
| 99 | . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' |
| 100 | . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' |
| 101 | . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' |
| 102 | . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' |
| 103 | . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' |
| 104 | .\} |
| 105 | . \" troff and (daisy-wheel) nroff accents |
| 106 | .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' |
| 107 | .ds 8 \h'\*(#H'\(*b\h'-\*(#H' |
| 108 | .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] |
| 109 | .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' |
| 110 | .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' |
| 111 | .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] |
| 112 | .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] |
| 113 | .ds ae a\h'-(\w'a'u*4/10)'e |
| 114 | .ds Ae A\h'-(\w'A'u*4/10)'E |
| 115 | . \" corrections for vroff |
| 116 | .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' |
| 117 | .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' |
| 118 | . \" for low resolution devices (crt and lpr) |
| 119 | .if \n(.H>23 .if \n(.V>19 \ |
| 120 | \{\ |
| 121 | . ds : e |
| 122 | . ds 8 ss |
| 123 | . ds o a |
| 124 | . ds d- d\h'-1'\(ga |
| 125 | . ds D- D\h'-1'\(hy |
| 126 | . ds th \o'bp' |
| 127 | . ds Th \o'LP' |
| 128 | . ds ae ae |
| 129 | . ds Ae AE |
| 130 | .\} |
| 131 | .rm #[ #] #H #V #F C |
| 132 | .\" ======================================================================== |
| 133 | .\" |
| 134 | .IX Title "d2i_X509 3" |
| 135 | .TH d2i_X509 3 "2009-01-11" "0.9.8j" "OpenSSL" |
| 136 | .\" For nroff, turn off justification. Always turn off hyphenation; it makes |
| 137 | .\" way too many mistakes in technical documents. |
| 138 | .if n .ad l |
| 139 | .nh |
| 140 | .SH "NAME" |
| 141 | d2i_X509, i2d_X509, d2i_X509_bio, d2i_X509_fp, i2d_X509_bio, |
| 142 | i2d_X509_fp \- X509 encode and decode functions |
| 143 | .SH "SYNOPSIS" |
| 144 | .IX Header "SYNOPSIS" |
| 145 | .Vb 1 |
| 146 | \& #include <openssl/x509.h> |
| 147 | \& |
| 148 | \& X509 *d2i_X509(X509 **px, const unsigned char **in, int len); |
| 149 | \& int i2d_X509(X509 *x, unsigned char **out); |
| 150 | \& |
| 151 | \& X509 *d2i_X509_bio(BIO *bp, X509 **x); |
| 152 | \& X509 *d2i_X509_fp(FILE *fp, X509 **x); |
| 153 | \& |
| 154 | \& int i2d_X509_bio(X509 *x, BIO *bp); |
| 155 | \& int i2d_X509_fp(X509 *x, FILE *fp); |
| 156 | .Ve |
| 157 | .SH "DESCRIPTION" |
| 158 | .IX Header "DESCRIPTION" |
| 159 | The X509 encode and decode routines encode and parse an |
| 160 | \&\fBX509\fR structure, which represents an X509 certificate. |
| 161 | .PP |
| 162 | \&\fId2i_X509()\fR attempts to decode \fBlen\fR bytes at \fB*in\fR. If |
| 163 | successful a pointer to the \fBX509\fR structure is returned. If an error |
| 164 | occurred then \fB\s-1NULL\s0\fR is returned. If \fBpx\fR is not \fB\s-1NULL\s0\fR then the |
| 165 | returned structure is written to \fB*px\fR. If \fB*px\fR is not \fB\s-1NULL\s0\fR |
| 166 | then it is assumed that \fB*px\fR contains a valid \fBX509\fR |
| 167 | structure and an attempt is made to reuse it. If the call is |
| 168 | successful \fB*in\fR is incremented to the byte following the |
| 169 | parsed data. |
| 170 | .PP |
| 171 | \&\fIi2d_X509()\fR encodes the structure pointed to by \fBx\fR into \s-1DER\s0 format. |
| 172 | If \fBout\fR is not \fB\s-1NULL\s0\fR is writes the \s-1DER\s0 encoded data to the buffer |
| 173 | at \fB*out\fR, and increments it to point after the data just written. |
| 174 | If the return value is negative an error occurred, otherwise it |
| 175 | returns the length of the encoded data. |
| 176 | .PP |
| 177 | For OpenSSL 0.9.7 and later if \fB*out\fR is \fB\s-1NULL\s0\fR memory will be |
| 178 | allocated for a buffer and the encoded data written to it. In this |
| 179 | case \fB*out\fR is not incremented and it points to the start of the |
| 180 | data just written. |
| 181 | .PP |
| 182 | \&\fId2i_X509_bio()\fR is similar to \fId2i_X509()\fR except it attempts |
| 183 | to parse data from \s-1BIO\s0 \fBbp\fR. |
| 184 | .PP |
| 185 | \&\fId2i_X509_fp()\fR is similar to \fId2i_X509()\fR except it attempts |
| 186 | to parse data from \s-1FILE\s0 pointer \fBfp\fR. |
| 187 | .PP |
| 188 | \&\fIi2d_X509_bio()\fR is similar to \fIi2d_X509()\fR except it writes |
| 189 | the encoding of the structure \fBx\fR to \s-1BIO\s0 \fBbp\fR and it |
| 190 | returns 1 for success and 0 for failure. |
| 191 | .PP |
| 192 | \&\fIi2d_X509_fp()\fR is similar to \fIi2d_X509()\fR except it writes |
| 193 | the encoding of the structure \fBx\fR to \s-1BIO\s0 \fBbp\fR and it |
| 194 | returns 1 for success and 0 for failure. |
| 195 | .SH "NOTES" |
| 196 | .IX Header "NOTES" |
| 197 | The letters \fBi\fR and \fBd\fR in for example \fBi2d_X509\fR stand for |
| 198 | \&\*(L"internal\*(R" (that is an internal C structure) and \*(L"\s-1DER\s0\*(R". So that |
| 199 | \&\fBi2d_X509\fR converts from internal to \s-1DER\s0. |
| 200 | .PP |
| 201 | The functions can also understand \fB\s-1BER\s0\fR forms. |
| 202 | .PP |
| 203 | The actual X509 structure passed to \fIi2d_X509()\fR must be a valid |
| 204 | populated \fBX509\fR structure it can \fBnot\fR simply be fed with an |
| 205 | empty structure such as that returned by \fIX509_new()\fR. |
| 206 | .PP |
| 207 | The encoded data is in binary form and may contain embedded zeroes. |
| 208 | Therefore any \s-1FILE\s0 pointers or BIOs should be opened in binary mode. |
| 209 | Functions such as \fB\f(BIstrlen()\fB\fR will \fBnot\fR return the correct length |
| 210 | of the encoded structure. |
| 211 | .PP |
| 212 | The ways that \fB*in\fR and \fB*out\fR are incremented after the operation |
| 213 | can trap the unwary. See the \fB\s-1WARNINGS\s0\fR section for some common |
| 214 | errors. |
| 215 | .PP |
| 216 | The reason for the auto increment behaviour is to reflect a typical |
| 217 | usage of \s-1ASN1\s0 functions: after one structure is encoded or decoded |
| 218 | another will processed after it. |
| 219 | .SH "EXAMPLES" |
| 220 | .IX Header "EXAMPLES" |
| 221 | Allocate and encode the \s-1DER\s0 encoding of an X509 structure: |
| 222 | .PP |
| 223 | .Vb 2 |
| 224 | \& int len; |
| 225 | \& unsigned char *buf, *p; |
| 226 | \& |
| 227 | \& len = i2d_X509(x, NULL); |
| 228 | \& |
| 229 | \& buf = OPENSSL_malloc(len); |
| 230 | \& |
| 231 | \& if (buf == NULL) |
| 232 | \& /* error */ |
| 233 | \& |
| 234 | \& p = buf; |
| 235 | \& |
| 236 | \& i2d_X509(x, &p); |
| 237 | .Ve |
| 238 | .PP |
| 239 | If you are using OpenSSL 0.9.7 or later then this can be |
| 240 | simplified to: |
| 241 | .PP |
| 242 | .Vb 2 |
| 243 | \& int len; |
| 244 | \& unsigned char *buf; |
| 245 | \& |
| 246 | \& buf = NULL; |
| 247 | \& |
| 248 | \& len = i2d_X509(x, &buf); |
| 249 | \& |
| 250 | \& if (len < 0) |
| 251 | \& /* error */ |
| 252 | .Ve |
| 253 | .PP |
| 254 | Attempt to decode a buffer: |
| 255 | .PP |
| 256 | .Vb 1 |
| 257 | \& X509 *x; |
| 258 | \& |
| 259 | \& unsigned char *buf, *p; |
| 260 | \& |
| 261 | \& int len; |
| 262 | \& |
| 263 | \& /* Something to setup buf and len */ |
| 264 | \& |
| 265 | \& p = buf; |
| 266 | \& |
| 267 | \& x = d2i_X509(NULL, &p, len); |
| 268 | \& |
| 269 | \& if (x == NULL) |
| 270 | \& /* Some error */ |
| 271 | .Ve |
| 272 | .PP |
| 273 | Alternative technique: |
| 274 | .PP |
| 275 | .Vb 1 |
| 276 | \& X509 *x; |
| 277 | \& |
| 278 | \& unsigned char *buf, *p; |
| 279 | \& |
| 280 | \& int len; |
| 281 | \& |
| 282 | \& /* Something to setup buf and len */ |
| 283 | \& |
| 284 | \& p = buf; |
| 285 | \& |
| 286 | \& x = NULL; |
| 287 | \& |
| 288 | \& if(!d2i_X509(&x, &p, len)) |
| 289 | \& /* Some error */ |
| 290 | .Ve |
| 291 | .SH "WARNINGS" |
| 292 | .IX Header "WARNINGS" |
| 293 | The use of temporary variable is mandatory. A common |
| 294 | mistake is to attempt to use a buffer directly as follows: |
| 295 | .PP |
| 296 | .Vb 2 |
| 297 | \& int len; |
| 298 | \& unsigned char *buf; |
| 299 | \& |
| 300 | \& len = i2d_X509(x, NULL); |
| 301 | \& |
| 302 | \& buf = OPENSSL_malloc(len); |
| 303 | \& |
| 304 | \& if (buf == NULL) |
| 305 | \& /* error */ |
| 306 | \& |
| 307 | \& i2d_X509(x, &buf); |
| 308 | \& |
| 309 | \& /* Other stuff ... */ |
| 310 | \& |
| 311 | \& OPENSSL_free(buf); |
| 312 | .Ve |
| 313 | .PP |
| 314 | This code will result in \fBbuf\fR apparently containing garbage because |
| 315 | it was incremented after the call to point after the data just written. |
| 316 | Also \fBbuf\fR will no longer contain the pointer allocated by \fB\f(BIOPENSSL_malloc()\fB\fR |
| 317 | and the subsequent call to \fB\f(BIOPENSSL_free()\fB\fR may well crash. |
| 318 | .PP |
| 319 | The auto allocation feature (setting buf to \s-1NULL\s0) only works on OpenSSL |
| 320 | 0.9.7 and later. Attempts to use it on earlier versions will typically |
| 321 | cause a segmentation violation. |
| 322 | .PP |
| 323 | Another trap to avoid is misuse of the \fBxp\fR argument to \fB\f(BId2i_X509()\fB\fR: |
| 324 | .PP |
| 325 | .Vb 1 |
| 326 | \& X509 *x; |
| 327 | \& |
| 328 | \& if (!d2i_X509(&x, &p, len)) |
| 329 | \& /* Some error */ |
| 330 | .Ve |
| 331 | .PP |
| 332 | This will probably crash somewhere in \fB\f(BId2i_X509()\fB\fR. The reason for this |
| 333 | is that the variable \fBx\fR is uninitialized and an attempt will be made to |
| 334 | interpret its (invalid) value as an \fBX509\fR structure, typically causing |
| 335 | a segmentation violation. If \fBx\fR is set to \s-1NULL\s0 first then this will not |
| 336 | happen. |
| 337 | .SH "BUGS" |
| 338 | .IX Header "BUGS" |
| 339 | In some versions of OpenSSL the \*(L"reuse\*(R" behaviour of \fId2i_X509()\fR when |
| 340 | \&\fB*px\fR is valid is broken and some parts of the reused structure may |
| 341 | persist if they are not present in the new one. As a result the use |
| 342 | of this \*(L"reuse\*(R" behaviour is strongly discouraged. |
| 343 | .PP |
| 344 | \&\fIi2d_X509()\fR will not return an error in many versions of OpenSSL, |
| 345 | if mandatory fields are not initialized due to a programming error |
| 346 | then the encoded structure may contain invalid data or omit the |
| 347 | fields entirely and will not be parsed by \fId2i_X509()\fR. This may be |
| 348 | fixed in future so code should not assume that \fIi2d_X509()\fR will |
| 349 | always succeed. |
| 350 | .SH "RETURN VALUES" |
| 351 | .IX Header "RETURN VALUES" |
| 352 | \&\fId2i_X509()\fR, \fId2i_X509_bio()\fR and \fId2i_X509_fp()\fR return a valid \fBX509\fR structure |
| 353 | or \fB\s-1NULL\s0\fR if an error occurs. The error code that can be obtained by |
| 354 | \&\fIERR_get_error\fR\|(3). |
| 355 | .PP |
| 356 | \&\fIi2d_X509()\fR, \fIi2d_X509_bio()\fR and \fIi2d_X509_fp()\fR return a the number of bytes |
| 357 | successfully encoded or a negative value if an error occurs. The error code |
| 358 | can be obtained by \fIERR_get_error\fR\|(3). |
| 359 | .PP |
| 360 | \&\fIi2d_X509_bio()\fR and \fIi2d_X509_fp()\fR returns 1 for success and 0 if an error |
| 361 | occurs The error code can be obtained by \fIERR_get_error\fR\|(3). |
| 362 | .SH "SEE ALSO" |
| 363 | .IX Header "SEE ALSO" |
| 364 | \&\fIERR_get_error\fR\|(3) |
| 365 | .SH "HISTORY" |
| 366 | .IX Header "HISTORY" |
| 367 | d2i_X509, i2d_X509, d2i_X509_bio, d2i_X509_fp, i2d_X509_bio and i2d_X509_fp |
| 368 | are available in all versions of SSLeay and OpenSSL. |