d2i_X509.pod 6.6 KB

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