2
0

BIO_f_ssl.pod 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. =pod
  2. =head1 NAME
  3. BIO_f_ssl, BIO_set_ssl, BIO_get_ssl, BIO_set_ssl_mode, BIO_set_ssl_renegotiate_bytes,
  4. BIO_get_num_renegotiates, BIO_set_ssl_renegotiate_timeout, BIO_new_ssl,
  5. BIO_new_ssl_connect, BIO_new_buffer_ssl_connect, BIO_ssl_copy_session_id,
  6. BIO_ssl_shutdown - SSL BIO
  7. =head1 SYNOPSIS
  8. #include <openssl/bio.h>
  9. #include <openssl/ssl.h>
  10. BIO_METHOD *BIO_f_ssl(void);
  11. #define BIO_set_ssl(b,ssl,c) BIO_ctrl(b,BIO_C_SET_SSL,c,(char *)ssl)
  12. #define BIO_get_ssl(b,sslp) BIO_ctrl(b,BIO_C_GET_SSL,0,(char *)sslp)
  13. #define BIO_set_ssl_mode(b,client) BIO_ctrl(b,BIO_C_SSL_MODE,client,NULL)
  14. #define BIO_set_ssl_renegotiate_bytes(b,num) \
  15. BIO_ctrl(b,BIO_C_SET_SSL_RENEGOTIATE_BYTES,num,NULL);
  16. #define BIO_set_ssl_renegotiate_timeout(b,seconds) \
  17. BIO_ctrl(b,BIO_C_SET_SSL_RENEGOTIATE_TIMEOUT,seconds,NULL);
  18. #define BIO_get_num_renegotiates(b) \
  19. BIO_ctrl(b,BIO_C_SET_SSL_NUM_RENEGOTIATES,0,NULL);
  20. BIO *BIO_new_ssl(SSL_CTX *ctx,int client);
  21. BIO *BIO_new_ssl_connect(SSL_CTX *ctx);
  22. BIO *BIO_new_buffer_ssl_connect(SSL_CTX *ctx);
  23. int BIO_ssl_copy_session_id(BIO *to,BIO *from);
  24. void BIO_ssl_shutdown(BIO *bio);
  25. #define BIO_do_handshake(b) BIO_ctrl(b,BIO_C_DO_STATE_MACHINE,0,NULL)
  26. =head1 DESCRIPTION
  27. BIO_f_ssl() returns the SSL BIO method. This is a filter BIO which
  28. is a wrapper round the OpenSSL SSL routines adding a BIO "flavour" to
  29. SSL I/O.
  30. I/O performed on an SSL BIO communicates using the SSL protocol with
  31. the SSLs read and write BIOs. If an SSL connection is not established
  32. then an attempt is made to establish one on the first I/O call.
  33. If a BIO is appended to an SSL BIO using BIO_push() it is automatically
  34. used as the SSL BIOs read and write BIOs.
  35. Calling BIO_reset() on an SSL BIO closes down any current SSL connection
  36. by calling SSL_shutdown(). BIO_reset() is then sent to the next BIO in
  37. the chain: this will typically disconnect the underlying transport.
  38. The SSL BIO is then reset to the initial accept or connect state.
  39. If the close flag is set when an SSL BIO is freed then the internal
  40. SSL structure is also freed using SSL_free().
  41. BIO_set_ssl() sets the internal SSL pointer of BIO B<b> to B<ssl> using
  42. the close flag B<c>.
  43. BIO_get_ssl() retrieves the SSL pointer of BIO B<b>, it can then be
  44. manipulated using the standard SSL library functions.
  45. BIO_set_ssl_mode() sets the SSL BIO mode to B<client>. If B<client>
  46. is 1 client mode is set. If B<client> is 0 server mode is set.
  47. BIO_set_ssl_renegotiate_bytes() sets the renegotiate byte count
  48. to B<num>. When set after every B<num> bytes of I/O (read and write)
  49. the SSL session is automatically renegotiated. B<num> must be at
  50. least 512 bytes.
  51. BIO_set_ssl_renegotiate_timeout() sets the renegotiate timeout to
  52. B<seconds>. When the renegotiate timeout elapses the session is
  53. automatically renegotiated.
  54. BIO_get_num_renegotiates() returns the total number of session
  55. renegotiations due to I/O or timeout.
  56. BIO_new_ssl() allocates an SSL BIO using SSL_CTX B<ctx> and using
  57. client mode if B<client> is non zero.
  58. BIO_new_ssl_connect() creates a new BIO chain consisting of an
  59. SSL BIO (using B<ctx>) followed by a connect BIO.
  60. BIO_new_buffer_ssl_connect() creates a new BIO chain consisting
  61. of a buffering BIO, an SSL BIO (using B<ctx>) and a connect
  62. BIO.
  63. BIO_ssl_copy_session_id() copies an SSL session id between
  64. BIO chains B<from> and B<to>. It does this by locating the
  65. SSL BIOs in each chain and calling SSL_copy_session_id() on
  66. the internal SSL pointer.
  67. BIO_ssl_shutdown() closes down an SSL connection on BIO
  68. chain B<bio>. It does this by locating the SSL BIO in the
  69. chain and calling SSL_shutdown() on its internal SSL
  70. pointer.
  71. BIO_do_handshake() attempts to complete an SSL handshake on the
  72. supplied BIO and establish the SSL connection. It returns 1
  73. if the connection was established successfully. A zero or negative
  74. value is returned if the connection could not be established, the
  75. call BIO_should_retry() should be used for non blocking connect BIOs
  76. to determine if the call should be retried. If an SSL connection has
  77. already been established this call has no effect.
  78. =head1 NOTES
  79. SSL BIOs are exceptional in that if the underlying transport
  80. is non blocking they can still request a retry in exceptional
  81. circumstances. Specifically this will happen if a session
  82. renegotiation takes place during a BIO_read() operation, one
  83. case where this happens is when SGC or step up occurs.
  84. In OpenSSL 0.9.6 and later the SSL flag SSL_AUTO_RETRY can be
  85. set to disable this behaviour. That is when this flag is set
  86. an SSL BIO using a blocking transport will never request a
  87. retry.
  88. Since unknown BIO_ctrl() operations are sent through filter
  89. BIOs the servers name and port can be set using BIO_set_host()
  90. on the BIO returned by BIO_new_ssl_connect() without having
  91. to locate the connect BIO first.
  92. Applications do not have to call BIO_do_handshake() but may wish
  93. to do so to separate the handshake process from other I/O
  94. processing.
  95. =head1 RETURN VALUES
  96. TBA
  97. =head1 EXAMPLE
  98. This SSL/TLS client example, attempts to retrieve a page from an
  99. SSL/TLS web server. The I/O routines are identical to those of the
  100. unencrypted example in L<BIO_s_connect(3)|BIO_s_connect(3)>.
  101. BIO *sbio, *out;
  102. int len;
  103. char tmpbuf[1024];
  104. SSL_CTX *ctx;
  105. SSL *ssl;
  106. ERR_load_crypto_strings();
  107. ERR_load_SSL_strings();
  108. OpenSSL_add_all_algorithms();
  109. /* We would seed the PRNG here if the platform didn't
  110. * do it automatically
  111. */
  112. ctx = SSL_CTX_new(SSLv23_client_method());
  113. /* We'd normally set some stuff like the verify paths and
  114. * mode here because as things stand this will connect to
  115. * any server whose certificate is signed by any CA.
  116. */
  117. sbio = BIO_new_ssl_connect(ctx);
  118. BIO_get_ssl(sbio, &ssl);
  119. if(!ssl) {
  120. fprintf(stderr, "Can't locate SSL pointer\n");
  121. /* whatever ... */
  122. }
  123. /* Don't want any retries */
  124. SSL_set_mode(ssl, SSL_MODE_AUTO_RETRY);
  125. /* We might want to do other things with ssl here */
  126. BIO_set_conn_hostname(sbio, "localhost:https");
  127. out = BIO_new_fp(stdout, BIO_NOCLOSE);
  128. if(BIO_do_connect(sbio) <= 0) {
  129. fprintf(stderr, "Error connecting to server\n");
  130. ERR_print_errors_fp(stderr);
  131. /* whatever ... */
  132. }
  133. if(BIO_do_handshake(sbio) <= 0) {
  134. fprintf(stderr, "Error establishing SSL connection\n");
  135. ERR_print_errors_fp(stderr);
  136. /* whatever ... */
  137. }
  138. /* Could examine ssl here to get connection info */
  139. BIO_puts(sbio, "GET / HTTP/1.0\n\n");
  140. for(;;) {
  141. len = BIO_read(sbio, tmpbuf, 1024);
  142. if(len <= 0) break;
  143. BIO_write(out, tmpbuf, len);
  144. }
  145. BIO_free_all(sbio);
  146. BIO_free(out);
  147. Here is a simple server example. It makes use of a buffering
  148. BIO to allow lines to be read from the SSL BIO using BIO_gets.
  149. It creates a pseudo web page containing the actual request from
  150. a client and also echoes the request to standard output.
  151. BIO *sbio, *bbio, *acpt, *out;
  152. int len;
  153. char tmpbuf[1024];
  154. SSL_CTX *ctx;
  155. SSL *ssl;
  156. ERR_load_crypto_strings();
  157. ERR_load_SSL_strings();
  158. OpenSSL_add_all_algorithms();
  159. /* Might seed PRNG here */
  160. ctx = SSL_CTX_new(SSLv23_server_method());
  161. if (!SSL_CTX_use_certificate_file(ctx,"server.pem",SSL_FILETYPE_PEM)
  162. || !SSL_CTX_use_PrivateKey_file(ctx,"server.pem",SSL_FILETYPE_PEM)
  163. || !SSL_CTX_check_private_key(ctx)) {
  164. fprintf(stderr, "Error setting up SSL_CTX\n");
  165. ERR_print_errors_fp(stderr);
  166. return 0;
  167. }
  168. /* Might do other things here like setting verify locations and
  169. * DH and/or RSA temporary key callbacks
  170. */
  171. /* New SSL BIO setup as server */
  172. sbio=BIO_new_ssl(ctx,0);
  173. BIO_get_ssl(sbio, &ssl);
  174. if(!ssl) {
  175. fprintf(stderr, "Can't locate SSL pointer\n");
  176. /* whatever ... */
  177. }
  178. /* Don't want any retries */
  179. SSL_set_mode(ssl, SSL_MODE_AUTO_RETRY);
  180. /* Create the buffering BIO */
  181. bbio = BIO_new(BIO_f_buffer());
  182. /* Add to chain */
  183. sbio = BIO_push(bbio, sbio);
  184. acpt=BIO_new_accept("4433");
  185. /* By doing this when a new connection is established
  186. * we automatically have sbio inserted into it. The
  187. * BIO chain is now 'swallowed' by the accept BIO and
  188. * will be freed when the accept BIO is freed.
  189. */
  190. BIO_set_accept_bios(acpt,sbio);
  191. out = BIO_new_fp(stdout, BIO_NOCLOSE);
  192. /* Setup accept BIO */
  193. if(BIO_do_accept(acpt) <= 0) {
  194. fprintf(stderr, "Error setting up accept BIO\n");
  195. ERR_print_errors_fp(stderr);
  196. return 0;
  197. }
  198. /* Now wait for incoming connection */
  199. if(BIO_do_accept(acpt) <= 0) {
  200. fprintf(stderr, "Error in connection\n");
  201. ERR_print_errors_fp(stderr);
  202. return 0;
  203. }
  204. /* We only want one connection so remove and free
  205. * accept BIO
  206. */
  207. sbio = BIO_pop(acpt);
  208. BIO_free_all(acpt);
  209. if(BIO_do_handshake(sbio) <= 0) {
  210. fprintf(stderr, "Error in SSL handshake\n");
  211. ERR_print_errors_fp(stderr);
  212. return 0;
  213. }
  214. BIO_puts(sbio, "HTTP/1.0 200 OK\r\nContent-type: text/plain\r\n\r\n");
  215. BIO_puts(sbio, "\r\nConnection Established\r\nRequest headers:\r\n");
  216. BIO_puts(sbio, "--------------------------------------------------\r\n");
  217. for(;;) {
  218. len = BIO_gets(sbio, tmpbuf, 1024);
  219. if(len <= 0) break;
  220. BIO_write(sbio, tmpbuf, len);
  221. BIO_write(out, tmpbuf, len);
  222. /* Look for blank line signifying end of headers*/
  223. if((tmpbuf[0] == '\r') || (tmpbuf[0] == '\n')) break;
  224. }
  225. BIO_puts(sbio, "--------------------------------------------------\r\n");
  226. BIO_puts(sbio, "\r\n");
  227. /* Since there is a buffering BIO present we had better flush it */
  228. BIO_flush(sbio);
  229. BIO_free_all(sbio);
  230. =head1 BUGS
  231. In OpenSSL versions before 1.0.0 the BIO_pop() call was handled incorrectly,
  232. the I/O BIO reference count was incorrectly incremented (instead of
  233. decremented) and dissociated with the SSL BIO even if the SSL BIO was not
  234. explicitly being popped (e.g. a pop higher up the chain). Applications which
  235. included workarounds for this bug (e.g. freeing BIOs more than once) should
  236. be modified to handle this fix or they may free up an already freed BIO.
  237. =head1 SEE ALSO
  238. TBA