d2i_X509.pod 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. =pod
  2. =head1 NAME
  3. d2i_X509, i2d_X509, d2i_X509_bio, d2i_X509_fp, i2d_X509_bio,
  4. i2d_X509_fp - X509 encode and decode functions
  5. =head1 SYNOPSIS
  6. #include <openssl/x509.h>
  7. X509 *d2i_X509(X509 **px, const unsigned char **in, int len);
  8. int i2d_X509(X509 *x, unsigned char **out);
  9. X509 *d2i_X509_bio(BIO *bp, X509 **x);
  10. X509 *d2i_X509_fp(FILE *fp, X509 **x);
  11. int i2d_X509_bio(BIO *bp, X509 *x);
  12. int i2d_X509_fp(FILE *fp, X509 *x);
  13. int i2d_re_X509_tbs(X509 *x, unsigned char **out);
  14. =head1 DESCRIPTION
  15. The X509 encode and decode routines encode and parse an
  16. B<X509> structure, which represents an X509 certificate.
  17. d2i_X509() attempts to decode B<len> bytes at B<*in>. If
  18. successful a pointer to the B<X509> structure is returned. If an error
  19. occurred then B<NULL> is returned. If B<px> is not B<NULL> then the
  20. returned structure is written to B<*px>. If B<*px> is not B<NULL>
  21. then it is assumed that B<*px> contains a valid B<X509>
  22. structure and an attempt is made to reuse it. This "reuse" capability is present
  23. for historical compatibility but its use is B<strongly discouraged> (see BUGS
  24. below, and the discussion in the RETURN VALUES section).
  25. If the call is successful B<*in> is incremented to the byte following the
  26. parsed data.
  27. i2d_X509() encodes the structure pointed to by B<x> into DER format.
  28. If B<out> is not B<NULL> is writes the DER encoded data to the buffer
  29. at B<*out>, and increments it to point after the data just written.
  30. If the return value is negative an error occurred, otherwise it
  31. returns the length of the encoded data.
  32. For OpenSSL 0.9.7 and later if B<*out> is B<NULL> memory will be
  33. allocated for a buffer and the encoded data written to it. In this
  34. case B<*out> is not incremented and it points to the start of the
  35. data just written.
  36. d2i_X509_bio() is similar to d2i_X509() except it attempts
  37. to parse data from BIO B<bp>.
  38. d2i_X509_fp() is similar to d2i_X509() except it attempts
  39. to parse data from FILE pointer B<fp>.
  40. i2d_X509_bio() is similar to i2d_X509() except it writes
  41. the encoding of the structure B<x> to BIO B<bp> and it
  42. returns 1 for success and 0 for failure.
  43. i2d_X509_fp() is similar to i2d_X509() except it writes
  44. the encoding of the structure B<x> to BIO B<bp> and it
  45. returns 1 for success and 0 for failure.
  46. i2d_re_X509_tbs() is similar to i2d_X509() except it encodes
  47. only the TBSCertificate portion of the certificate.
  48. =head1 NOTES
  49. The letters B<i> and B<d> in for example B<i2d_X509> stand for
  50. "internal" (that is an internal C structure) and "DER". So
  51. B<i2d_X509> converts from internal to DER. The "re" in
  52. B<i2d_re_X509_tbs> stands for "re-encode", and ensures that a fresh
  53. encoding is generated in case the object has been modified after
  54. creation (see the BUGS section).
  55. The functions can also understand B<BER> forms.
  56. The actual X509 structure passed to i2d_X509() must be a valid
  57. populated B<X509> structure it can B<not> simply be fed with an
  58. empty structure such as that returned by X509_new().
  59. The encoded data is in binary form and may contain embedded zeroes.
  60. Therefore any FILE pointers or BIOs should be opened in binary mode.
  61. Functions such as B<strlen()> will B<not> return the correct length
  62. of the encoded structure.
  63. The ways that B<*in> and B<*out> are incremented after the operation
  64. can trap the unwary. See the B<WARNINGS> section for some common
  65. errors.
  66. The reason for the auto increment behaviour is to reflect a typical
  67. usage of ASN1 functions: after one structure is encoded or decoded
  68. another will processed after it.
  69. =head1 EXAMPLES
  70. Allocate and encode the DER encoding of an X509 structure:
  71. int len;
  72. unsigned char *buf, *p;
  73. len = i2d_X509(x, NULL);
  74. buf = OPENSSL_malloc(len);
  75. if (buf == NULL)
  76. /* error */
  77. p = buf;
  78. i2d_X509(x, &p);
  79. If you are using OpenSSL 0.9.7 or later then this can be
  80. simplified to:
  81. int len;
  82. unsigned char *buf;
  83. buf = NULL;
  84. len = i2d_X509(x, &buf);
  85. if (len < 0)
  86. /* error */
  87. Attempt to decode a buffer:
  88. X509 *x;
  89. unsigned char *buf, *p;
  90. int len;
  91. /* Something to setup buf and len */
  92. p = buf;
  93. x = d2i_X509(NULL, &p, len);
  94. if (x == NULL)
  95. /* Some error */
  96. Alternative technique:
  97. X509 *x;
  98. unsigned char *buf, *p;
  99. int len;
  100. /* Something to setup buf and len */
  101. p = buf;
  102. x = NULL;
  103. if(!d2i_X509(&x, &p, len))
  104. /* Some error */
  105. =head1 WARNINGS
  106. The use of temporary variable is mandatory. A common
  107. mistake is to attempt to use a buffer directly as follows:
  108. int len;
  109. unsigned char *buf;
  110. len = i2d_X509(x, NULL);
  111. buf = OPENSSL_malloc(len);
  112. if (buf == NULL)
  113. /* error */
  114. i2d_X509(x, &buf);
  115. /* Other stuff ... */
  116. OPENSSL_free(buf);
  117. This code will result in B<buf> apparently containing garbage because
  118. it was incremented after the call to point after the data just written.
  119. Also B<buf> will no longer contain the pointer allocated by B<OPENSSL_malloc()>
  120. and the subsequent call to B<OPENSSL_free()> may well crash.
  121. The auto allocation feature (setting buf to NULL) only works on OpenSSL
  122. 0.9.7 and later. Attempts to use it on earlier versions will typically
  123. cause a segmentation violation.
  124. Another trap to avoid is misuse of the B<xp> argument to B<d2i_X509()>:
  125. X509 *x;
  126. if (!d2i_X509(&x, &p, len))
  127. /* Some error */
  128. This will probably crash somewhere in B<d2i_X509()>. The reason for this
  129. is that the variable B<x> is uninitialized and an attempt will be made to
  130. interpret its (invalid) value as an B<X509> structure, typically causing
  131. a segmentation violation. If B<x> is set to NULL first then this will not
  132. happen.
  133. =head1 BUGS
  134. In some versions of OpenSSL the "reuse" behaviour of d2i_X509() when
  135. B<*px> is valid is broken and some parts of the reused structure may
  136. persist if they are not present in the new one. As a result the use
  137. of this "reuse" behaviour is strongly discouraged.
  138. i2d_X509() will not return an error in many versions of OpenSSL,
  139. if mandatory fields are not initialized due to a programming error
  140. then the encoded structure may contain invalid data or omit the
  141. fields entirely and will not be parsed by d2i_X509(). This may be
  142. fixed in future so code should not assume that i2d_X509() will
  143. always succeed.
  144. The encoding of the TBSCertificate portion of a certificate is cached
  145. in the B<X509> structure internally to improve encoding performance
  146. and to ensure certificate signatures are verified correctly in some
  147. certificates with broken (non-DER) encodings.
  148. Any function which encodes an X509 structure such as i2d_X509(),
  149. i2d_X509_fp() or i2d_X509_bio() may return a stale encoding if the
  150. B<X509> structure has been modified after deserialization or previous
  151. serialization.
  152. If, after modification, the B<X509> object is re-signed with X509_sign(),
  153. the encoding is automatically renewed. Otherwise, the encoding of the
  154. TBSCertificate portion of the B<X509> can be manually renewed by calling
  155. i2d_re_X509_tbs().
  156. =head1 RETURN VALUES
  157. d2i_X509(), d2i_X509_bio() and d2i_X509_fp() return a valid B<X509> structure
  158. or B<NULL> if an error occurs. The error code that can be obtained by
  159. L<ERR_get_error(3)|ERR_get_error(3)>. If the "reuse" capability has been used
  160. with a valid X509 structure being passed in via B<px> then the object is not
  161. freed in the event of error but may be in a potentially invalid or inconsistent
  162. state.
  163. i2d_X509() returns the number of bytes successfully encoded or a negative
  164. value if an error occurs. The error code can be obtained by
  165. L<ERR_get_error(3)|ERR_get_error(3)>.
  166. i2d_X509_bio() and i2d_X509_fp() return 1 for success and 0 if an error
  167. occurs The error code can be obtained by L<ERR_get_error(3)|ERR_get_error(3)>.
  168. =head1 SEE ALSO
  169. L<ERR_get_error(3)|ERR_get_error(3)>
  170. =head1 HISTORY
  171. d2i_X509, i2d_X509, d2i_X509_bio, d2i_X509_fp, i2d_X509_bio and i2d_X509_fp
  172. are available in all versions of SSLeay and OpenSSL.
  173. =cut