diff options
Diffstat (limited to 'src/lib/libssl/doc/BIO_f_ssl.3')
| -rw-r--r-- | src/lib/libssl/doc/BIO_f_ssl.3 | 475 |
1 files changed, 475 insertions, 0 deletions
diff --git a/src/lib/libssl/doc/BIO_f_ssl.3 b/src/lib/libssl/doc/BIO_f_ssl.3 new file mode 100644 index 0000000000..31c9963edb --- /dev/null +++ b/src/lib/libssl/doc/BIO_f_ssl.3 | |||
| @@ -0,0 +1,475 @@ | |||
| 1 | .Dd $Mdocdate: October 12 2014 $ | ||
| 2 | .Dt BIO_F_SSL 3 | ||
| 3 | .Os | ||
| 4 | .Sh NAME | ||
| 5 | .Nm BIO_f_ssl , | ||
| 6 | .Nm BIO_set_ssl , | ||
| 7 | .Nm BIO_get_ssl , | ||
| 8 | .Nm BIO_set_ssl_mode , | ||
| 9 | .Nm BIO_set_ssl_renegotiate_bytes , | ||
| 10 | .Nm BIO_get_num_renegotiates , | ||
| 11 | .Nm BIO_set_ssl_renegotiate_timeout , | ||
| 12 | .Nm BIO_new_ssl , | ||
| 13 | .Nm BIO_new_ssl_connect , | ||
| 14 | .Nm BIO_new_buffer_ssl_connect , | ||
| 15 | .Nm BIO_ssl_copy_session_id , | ||
| 16 | .Nm BIO_ssl_shutdown | ||
| 17 | .Nd SSL BIO | ||
| 18 | .Sh SYNOPSIS | ||
| 19 | .In openssl/bio.h | ||
| 20 | .In openssl/ssl.h | ||
| 21 | .Ft BIO_METHOD * | ||
| 22 | .Fn BIO_f_ssl void | ||
| 23 | .Fd #define BIO_set_ssl(b,ssl,c) BIO_ctrl(b,BIO_C_SET_SSL,c,(char *)ssl) | ||
| 24 | .Fd #define BIO_get_ssl(b,sslp) BIO_ctrl(b,BIO_C_GET_SSL,0,(char *)sslp) | ||
| 25 | .Fd #define BIO_set_ssl_mode(b,client) BIO_ctrl(b,BIO_C_SSL_MODE,client,NULL) | ||
| 26 | .Fd #define BIO_set_ssl_renegotiate_bytes(b,num) \ | ||
| 27 | BIO_ctrl(b,BIO_C_SET_SSL_RENEGOTIATE_BYTES,num,NULL) | ||
| 28 | .Fd #define BIO_set_ssl_renegotiate_timeout(b,seconds) \ | ||
| 29 | BIO_ctrl(b,BIO_C_SET_SSL_RENEGOTIATE_TIMEOUT,seconds,NULL) | ||
| 30 | .Fd #define BIO_get_num_renegotiates(b) \ | ||
| 31 | BIO_ctrl(b,BIO_C_SET_SSL_NUM_RENEGOTIATES,0,NULL) | ||
| 32 | .Ft BIO * | ||
| 33 | .Fn BIO_new_ssl "SSL_CTX *ctx" "int client" | ||
| 34 | .Ft BIO * | ||
| 35 | .Fn BIO_new_ssl_connect "SSL_CTX *ctx" | ||
| 36 | .Ft BIO * | ||
| 37 | .Fn BIO_new_buffer_ssl_connect "SSL_CTX *ctx" | ||
| 38 | .Ft int | ||
| 39 | .Fn BIO_ssl_copy_session_id "BIO *to" "BIO *from" | ||
| 40 | .Ft void | ||
| 41 | .Fn BIO_ssl_shutdown "BIO *bio" | ||
| 42 | .Fd #define BIO_do_handshake(b) BIO_ctrl(b,BIO_C_DO_STATE_MACHINE,0,NULL) | ||
| 43 | .Sh DESCRIPTION | ||
| 44 | .Fn BIO_f_ssl | ||
| 45 | returns the | ||
| 46 | .Vt SSL | ||
| 47 | .Vt BIO | ||
| 48 | method. | ||
| 49 | This is a filter | ||
| 50 | .Vt BIO | ||
| 51 | which is a wrapper around the OpenSSL | ||
| 52 | .Vt SSL | ||
| 53 | routines adding a | ||
| 54 | .Vt BIO | ||
| 55 | .Dq flavor | ||
| 56 | to SSL I/O. | ||
| 57 | .Pp | ||
| 58 | I/O performed on an | ||
| 59 | .Vt SSL | ||
| 60 | .Vt BIO | ||
| 61 | communicates using the SSL protocol with | ||
| 62 | the | ||
| 63 | .Vt SSL Ns 's | ||
| 64 | read and write | ||
| 65 | .Vt BIO Ns s. | ||
| 66 | If an SSL connection is not established then an attempt is made to establish | ||
| 67 | one on the first I/O call. | ||
| 68 | .Pp | ||
| 69 | If a | ||
| 70 | .Vt BIO | ||
| 71 | is appended to an | ||
| 72 | .Vt SSL | ||
| 73 | .Vt BIO | ||
| 74 | using | ||
| 75 | .Xr BIO_push 3 | ||
| 76 | it is automatically used as the | ||
| 77 | .Vt SSL | ||
| 78 | .Vt BIO Ns 's read and write | ||
| 79 | .Vt BIO Ns s. | ||
| 80 | .Pp | ||
| 81 | Calling | ||
| 82 | .Xr BIO_reset 3 | ||
| 83 | on an | ||
| 84 | .Vt SSL | ||
| 85 | .Vt BIO | ||
| 86 | closes down any current SSL connection by calling | ||
| 87 | .Xr SSL_shutdown 3 . | ||
| 88 | .Xr BIO_reset | ||
| 89 | is then sent to the next | ||
| 90 | .Vt BIO | ||
| 91 | in the chain; this will typically disconnect the underlying transport. | ||
| 92 | The | ||
| 93 | .Vt SSL | ||
| 94 | .Vt BIO | ||
| 95 | is then reset to the initial accept or connect state. | ||
| 96 | .Pp | ||
| 97 | If the close flag is set when an | ||
| 98 | .Vt SSL | ||
| 99 | .Vt BIO | ||
| 100 | is freed then the internal | ||
| 101 | .Vt SSL | ||
| 102 | structure is also freed using | ||
| 103 | .Xr SSL_free 3 . | ||
| 104 | .Pp | ||
| 105 | .Fn BIO_set_ssl | ||
| 106 | sets the internal | ||
| 107 | .Vt SSL | ||
| 108 | pointer of | ||
| 109 | .Vt BIO | ||
| 110 | .Fa b | ||
| 111 | to | ||
| 112 | .Fa ssl | ||
| 113 | using | ||
| 114 | the close flag | ||
| 115 | .Fa c . | ||
| 116 | .Pp | ||
| 117 | .Fn BIO_get_ssl | ||
| 118 | retrieves the | ||
| 119 | .Vt SSL | ||
| 120 | pointer of | ||
| 121 | .Vt BIO | ||
| 122 | .Fa b ; | ||
| 123 | it can then be manipulated using the standard SSL library functions. | ||
| 124 | .Pp | ||
| 125 | .Fn BIO_set_ssl_mode | ||
| 126 | sets the | ||
| 127 | .Vt SSL | ||
| 128 | .Vt BIO | ||
| 129 | mode to | ||
| 130 | .Fa client . | ||
| 131 | If | ||
| 132 | .Fa client | ||
| 133 | is 1, client mode is set. | ||
| 134 | If | ||
| 135 | .Fa client | ||
| 136 | is 0, server mode is set. | ||
| 137 | .Pp | ||
| 138 | .Fn BIO_set_ssl_renegotiate_bytes | ||
| 139 | sets the renegotiate byte count to | ||
| 140 | .Fa num . | ||
| 141 | When set after every | ||
| 142 | .Fa num | ||
| 143 | bytes of I/O (read and write) the SSL session is automatically renegotiated. | ||
| 144 | .Fa num | ||
| 145 | must be at least 512 bytes. | ||
| 146 | .Pp | ||
| 147 | .Fn BIO_set_ssl_renegotiate_timeout | ||
| 148 | sets the renegotiate timeout to | ||
| 149 | .Fa seconds . | ||
| 150 | When the renegotiate timeout elapses the session is automatically renegotiated. | ||
| 151 | .Pp | ||
| 152 | .Fn BIO_get_num_renegotiates | ||
| 153 | returns the total number of session renegotiations due to I/O or timeout. | ||
| 154 | .Pp | ||
| 155 | .Fn BIO_new_ssl | ||
| 156 | allocates an | ||
| 157 | .Vt SSL | ||
| 158 | .Vt BIO | ||
| 159 | using | ||
| 160 | .Vt SSL_CTX | ||
| 161 | .Va ctx | ||
| 162 | and using client mode if | ||
| 163 | .Fa client | ||
| 164 | is nonzero. | ||
| 165 | .Pp | ||
| 166 | .Fn BIO_new_ssl_connect | ||
| 167 | creates a new | ||
| 168 | .Vt BIO | ||
| 169 | chain consisting of an | ||
| 170 | .Vt SSL | ||
| 171 | .Vt BIO | ||
| 172 | (using | ||
| 173 | .Fa ctx ) | ||
| 174 | followed by a connect BIO. | ||
| 175 | .Pp | ||
| 176 | .Fn BIO_new_buffer_ssl_connect | ||
| 177 | creates a new | ||
| 178 | .Vt BIO | ||
| 179 | chain consisting of a buffering | ||
| 180 | .Vt BIO , | ||
| 181 | an | ||
| 182 | .Vt SSL | ||
| 183 | .Vt BIO | ||
| 184 | (using | ||
| 185 | .Fa ctx ) | ||
| 186 | and a connect | ||
| 187 | .Vt BIO . | ||
| 188 | .Pp | ||
| 189 | .Fn BIO_ssl_copy_session_id | ||
| 190 | copies an SSL session id between | ||
| 191 | .Vt BIO | ||
| 192 | chains | ||
| 193 | .Fa from | ||
| 194 | and | ||
| 195 | .Fa to . | ||
| 196 | It does this by locating the | ||
| 197 | .Vt SSL | ||
| 198 | .Vt BIO Ns s | ||
| 199 | in each chain and calling | ||
| 200 | .Xr SSL_copy_session_id 3 | ||
| 201 | on the internal | ||
| 202 | .Vt SSL | ||
| 203 | pointer. | ||
| 204 | .Pp | ||
| 205 | .Fn BIO_ssl_shutdown | ||
| 206 | closes down an SSL connection on | ||
| 207 | .Vt BIO | ||
| 208 | chain | ||
| 209 | .Fa bio . | ||
| 210 | It does this by locating the | ||
| 211 | .Vt SSL | ||
| 212 | .Vt BIO | ||
| 213 | in the | ||
| 214 | chain and calling | ||
| 215 | .Xr SSL_shutdown 3 | ||
| 216 | on its internal | ||
| 217 | .Vt SSL | ||
| 218 | pointer. | ||
| 219 | .Pp | ||
| 220 | .Fn BIO_do_handshake | ||
| 221 | attempts to complete an SSL handshake on the supplied | ||
| 222 | .Vt BIO | ||
| 223 | and establish the SSL connection. | ||
| 224 | It returns 1 if the connection was established successfully. | ||
| 225 | A zero or negative value is returned if the connection could not be | ||
| 226 | established; the call | ||
| 227 | .Xr BIO_should_retry 3 | ||
| 228 | should be used for non blocking connect | ||
| 229 | .Vt BIO Ns s | ||
| 230 | to determine if the call should be retried. | ||
| 231 | If an SSL connection has already been established this call has no effect. | ||
| 232 | .Sh NOTES | ||
| 233 | .Vt SSL | ||
| 234 | .Vt BIO Ns s | ||
| 235 | are exceptional in that if the underlying transport is non-blocking they can | ||
| 236 | still request a retry in exceptional circumstances. | ||
| 237 | Specifically this will happen if a session renegotiation takes place during a | ||
| 238 | .Xr BIO_read 3 | ||
| 239 | operation. | ||
| 240 | One case where this happens is when SGC or step up occurs. | ||
| 241 | .Pp | ||
| 242 | In OpenSSL 0.9.6 and later the SSL flag | ||
| 243 | .Dv SSL_AUTO_RETRY | ||
| 244 | can be set to disable this behaviour. | ||
| 245 | In other words, when this flag is set an | ||
| 246 | .Vt SSL | ||
| 247 | .Vt BIO | ||
| 248 | using a blocking transport will never request a retry. | ||
| 249 | .Pp | ||
| 250 | Since unknown | ||
| 251 | .Xr BIO_ctrl 3 | ||
| 252 | operations are sent through filter | ||
| 253 | .Vt BIO Ns s | ||
| 254 | the server name and port can be set using | ||
| 255 | .Xr BIO_set_host 3 | ||
| 256 | on the | ||
| 257 | .Vt BIO | ||
| 258 | returned by | ||
| 259 | .Fn BIO_new_ssl_connect | ||
| 260 | without having to locate the connect | ||
| 261 | .Vt BIO | ||
| 262 | first. | ||
| 263 | .Pp | ||
| 264 | Applications do not have to call | ||
| 265 | .Fn BIO_do_handshake | ||
| 266 | but may wish to do so to separate the handshake process from other I/O | ||
| 267 | processing. | ||
| 268 | .Sh RETURN VALUES | ||
| 269 | .\" XXX | ||
| 270 | This section is incomplete. | ||
| 271 | .Sh EXAMPLE | ||
| 272 | This SSL/TLS client example attempts to retrieve a page from an SSL/TLS web | ||
| 273 | server. | ||
| 274 | The I/O routines are identical to those of the unencrypted example in | ||
| 275 | .Xr BIO_s_connect 3 . | ||
| 276 | .Bd -literal | ||
| 277 | BIO *sbio, *out; | ||
| 278 | int len; | ||
| 279 | char tmpbuf[1024]; | ||
| 280 | SSL_CTX *ctx; | ||
| 281 | SSL *ssl; | ||
| 282 | |||
| 283 | ERR_load_crypto_strings(); | ||
| 284 | ERR_load_SSL_strings(); | ||
| 285 | OpenSSL_add_all_algorithms(); | ||
| 286 | |||
| 287 | /* | ||
| 288 | * We would seed the PRNG here if the platform didn't do it automatically | ||
| 289 | */ | ||
| 290 | |||
| 291 | ctx = SSL_CTX_new(SSLv23_client_method()); | ||
| 292 | |||
| 293 | /* | ||
| 294 | * We'd normally set some stuff like the verify paths and mode here because | ||
| 295 | * as things stand this will connect to any server whose certificate is | ||
| 296 | * signed by any CA. | ||
| 297 | */ | ||
| 298 | |||
| 299 | sbio = BIO_new_ssl_connect(ctx); | ||
| 300 | |||
| 301 | BIO_get_ssl(sbio, &ssl); | ||
| 302 | |||
| 303 | if (!ssl) { | ||
| 304 | fprintf(stderr, "Can't locate SSL pointer\en"); | ||
| 305 | /* whatever ... */ | ||
| 306 | } | ||
| 307 | |||
| 308 | /* Don't want any retries */ | ||
| 309 | SSL_set_mode(ssl, SSL_MODE_AUTO_RETRY); | ||
| 310 | |||
| 311 | /* We might want to do other things with ssl here */ | ||
| 312 | |||
| 313 | BIO_set_conn_hostname(sbio, "localhost:https"); | ||
| 314 | |||
| 315 | out = BIO_new_fp(stdout, BIO_NOCLOSE); | ||
| 316 | if (BIO_do_connect(sbio) <= 0) { | ||
| 317 | fprintf(stderr, "Error connecting to server\en"); | ||
| 318 | ERR_print_errors_fp(stderr); | ||
| 319 | /* whatever ... */ | ||
| 320 | } | ||
| 321 | |||
| 322 | if (BIO_do_handshake(sbio) <= 0) { | ||
| 323 | fprintf(stderr, "Error establishing SSL connection\en"); | ||
| 324 | ERR_print_errors_fp(stderr); | ||
| 325 | /* whatever ... */ | ||
| 326 | } | ||
| 327 | |||
| 328 | /* Could examine ssl here to get connection info */ | ||
| 329 | |||
| 330 | BIO_puts(sbio, "GET / HTTP/1.0\en\en"); | ||
| 331 | for (;;) { | ||
| 332 | len = BIO_read(sbio, tmpbuf, 1024); | ||
| 333 | if(len <= 0) break; | ||
| 334 | BIO_write(out, tmpbuf, len); | ||
| 335 | } | ||
| 336 | BIO_free_all(sbio); | ||
| 337 | BIO_free(out); | ||
| 338 | .Ed | ||
| 339 | .Pp | ||
| 340 | Here is a simple server example. | ||
| 341 | It makes use of a buffering | ||
| 342 | .Vt BIO | ||
| 343 | to allow lines to be read from the | ||
| 344 | .Vt SSL | ||
| 345 | .Vt BIO | ||
| 346 | using | ||
| 347 | .Xr BIO_gets 3 . | ||
| 348 | It creates a pseudo web page containing the actual request from a client and | ||
| 349 | also echoes the request to standard output. | ||
| 350 | .Bd -literal | ||
| 351 | BIO *sbio, *bbio, *acpt, *out; | ||
| 352 | int len; | ||
| 353 | char tmpbuf[1024]; | ||
| 354 | SSL_CTX *ctx; | ||
| 355 | SSL *ssl; | ||
| 356 | |||
| 357 | ERR_load_crypto_strings(); | ||
| 358 | ERR_load_SSL_strings(); | ||
| 359 | OpenSSL_add_all_algorithms(); | ||
| 360 | |||
| 361 | /* Might seed PRNG here */ | ||
| 362 | |||
| 363 | ctx = SSL_CTX_new(SSLv23_server_method()); | ||
| 364 | |||
| 365 | if (!SSL_CTX_use_certificate_file(ctx,"server.pem",SSL_FILETYPE_PEM) | ||
| 366 | || !SSL_CTX_use_PrivateKey_file(ctx,"server.pem",SSL_FILETYPE_PEM) | ||
| 367 | || !SSL_CTX_check_private_key(ctx)) { | ||
| 368 | fprintf(stderr, "Error setting up SSL_CTX\en"); | ||
| 369 | ERR_print_errors_fp(stderr); | ||
| 370 | return 0; | ||
| 371 | } | ||
| 372 | |||
| 373 | /* | ||
| 374 | * Might do other things here like setting verify locations and DH and/or | ||
| 375 | * RSA temporary key callbacks | ||
| 376 | */ | ||
| 377 | |||
| 378 | /* New SSL BIO setup as server */ | ||
| 379 | sbio = BIO_new_ssl(ctx,0); | ||
| 380 | |||
| 381 | BIO_get_ssl(sbio, &ssl); | ||
| 382 | |||
| 383 | if (!ssl) { | ||
| 384 | fprintf(stderr, "Can't locate SSL pointer\en"); | ||
| 385 | /* whatever ... */ | ||
| 386 | } | ||
| 387 | |||
| 388 | /* Don't want any retries */ | ||
| 389 | SSL_set_mode(ssl, SSL_MODE_AUTO_RETRY); | ||
| 390 | |||
| 391 | /* Create the buffering BIO */ | ||
| 392 | |||
| 393 | bbio = BIO_new(BIO_f_buffer()); | ||
| 394 | |||
| 395 | /* Add to chain */ | ||
| 396 | sbio = BIO_push(bbio, sbio); | ||
| 397 | |||
| 398 | acpt = BIO_new_accept("4433"); | ||
| 399 | |||
| 400 | /* | ||
| 401 | * By doing this when a new connection is established we automatically | ||
| 402 | * have sbio inserted into it. The BIO chain is now 'swallowed' by the | ||
| 403 | * accept BIO and will be freed when the accept BIO is freed. | ||
| 404 | */ | ||
| 405 | |||
| 406 | BIO_set_accept_bios(acpt,sbio); | ||
| 407 | |||
| 408 | out = BIO_new_fp(stdout, BIO_NOCLOSE); | ||
| 409 | |||
| 410 | /* Setup accept BIO */ | ||
| 411 | if (BIO_do_accept(acpt) <= 0) { | ||
| 412 | fprintf(stderr, "Error setting up accept BIO\en"); | ||
| 413 | ERR_print_errors_fp(stderr); | ||
| 414 | return 0; | ||
| 415 | } | ||
| 416 | |||
| 417 | /* Now wait for incoming connection */ | ||
| 418 | if (BIO_do_accept(acpt) <= 0) { | ||
| 419 | fprintf(stderr, "Error in connection\en"); | ||
| 420 | ERR_print_errors_fp(stderr); | ||
| 421 | return 0; | ||
| 422 | } | ||
| 423 | |||
| 424 | /* We only want one connection so remove and free accept BIO */ | ||
| 425 | |||
| 426 | sbio = BIO_pop(acpt); | ||
| 427 | |||
| 428 | BIO_free_all(acpt); | ||
| 429 | |||
| 430 | if (BIO_do_handshake(sbio) <= 0) { | ||
| 431 | fprintf(stderr, "Error in SSL handshake\en"); | ||
| 432 | ERR_print_errors_fp(stderr); | ||
| 433 | return 0; | ||
| 434 | } | ||
| 435 | |||
| 436 | BIO_puts(sbio, "HTTP/1.0 200 OK\er\enContent-type: text/plain\er\en\er\en"); | ||
| 437 | BIO_puts(sbio, "\er\enConnection Established\er\enRequest headers:\er\en"); | ||
| 438 | BIO_puts(sbio, "--------------------------------------------------\er\en"); | ||
| 439 | |||
| 440 | for (;;) { | ||
| 441 | len = BIO_gets(sbio, tmpbuf, 1024); | ||
| 442 | if (len <= 0) | ||
| 443 | break; | ||
| 444 | BIO_write(sbio, tmpbuf, len); | ||
| 445 | BIO_write(out, tmpbuf, len); | ||
| 446 | /* Look for blank line signifying end of headers */ | ||
| 447 | if ((tmpbuf[0] == '\er') || (tmpbuf[0] == '\en')) | ||
| 448 | break; | ||
| 449 | } | ||
| 450 | |||
| 451 | BIO_puts(sbio, "--------------------------------------------------\er\en"); | ||
| 452 | BIO_puts(sbio, "\er\en"); | ||
| 453 | |||
| 454 | /* Since there is a buffering BIO present we had better flush it */ | ||
| 455 | BIO_flush(sbio); | ||
| 456 | |||
| 457 | BIO_free_all(sbio); | ||
| 458 | .Ed | ||
| 459 | .Sh BUGS | ||
| 460 | In OpenSSL versions before 1.0.0 the | ||
| 461 | .Xr BIO_pop 3 | ||
| 462 | call was handled incorrectly: | ||
| 463 | the I/O BIO reference count was incorrectly incremented (instead of | ||
| 464 | decremented) and dissociated with the | ||
| 465 | .Vt SSL | ||
| 466 | .Vt BIO | ||
| 467 | even if the | ||
| 468 | .Vt SSL | ||
| 469 | .Vt BIO | ||
| 470 | was not | ||
| 471 | explicitly being popped (e.g., a pop higher up the chain). | ||
| 472 | Applications which included workarounds for this bug (e.g., freeing BIOs more | ||
| 473 | than once) should be modified to handle this fix or they may free up an already | ||
| 474 | freed | ||
| 475 | .Vt BIO . | ||
