bio_ssl.cc
Go to the documentation of this file.
1 /*
2  * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (the "License"). You may not use
5  * this file except in compliance with the License. You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9 
10 #include <openssl/ssl.h>
11 
12 #include <openssl/bio.h>
13 
14 
15 static SSL *get_ssl(BIO *bio) {
16  return reinterpret_cast<SSL *>(bio->ptr);
17 }
18 
19 static int ssl_read(BIO *bio, char *out, int outl) {
20  SSL *ssl = get_ssl(bio);
21  if (ssl == NULL) {
22  return 0;
23  }
24 
26 
27  const int ret = SSL_read(ssl, out, outl);
28 
29  switch (SSL_get_error(ssl, ret)) {
31  BIO_set_retry_read(bio);
32  break;
33 
36  break;
37 
41  break;
42 
46  break;
47 
48  case SSL_ERROR_NONE:
49  case SSL_ERROR_SYSCALL:
50  case SSL_ERROR_SSL:
52  default:
53  break;
54  }
55 
56  return ret;
57 }
58 
59 static int ssl_write(BIO *bio, const char *out, int outl) {
60  SSL *ssl = get_ssl(bio);
61  if (ssl == NULL) {
62  return 0;
63  }
64 
66 
67  const int ret = SSL_write(ssl, out, outl);
68 
69  switch (SSL_get_error(ssl, ret)) {
72  break;
73 
75  BIO_set_retry_read(bio);
76  break;
77 
81  break;
82 
83  case SSL_ERROR_NONE:
84  case SSL_ERROR_SYSCALL:
85  case SSL_ERROR_SSL:
86  default:
87  break;
88  }
89 
90  return ret;
91 }
92 
93 static long ssl_ctrl(BIO *bio, int cmd, long num, void *ptr) {
94  SSL *ssl = get_ssl(bio);
95  if (ssl == NULL && cmd != BIO_C_SET_SSL) {
96  return 0;
97  }
98 
99  switch (cmd) {
100  case BIO_C_SET_SSL:
101  if (ssl != NULL) {
102  // OpenSSL allows reusing an SSL BIO with a different SSL object. We do
103  // not support this.
105  return 0;
106  }
107 
108  // Note this differs from upstream OpenSSL, which synchronizes
109  // |bio->next_bio| with |ssl|'s rbio here, and on |BIO_CTRL_PUSH|. We call
110  // into the corresponding |BIO| directly. (We can implement the upstream
111  // behavior if it ends up necessary.)
112  bio->shutdown = num;
113  bio->ptr = ptr;
114  bio->init = 1;
115  return 1;
116 
117  case BIO_CTRL_GET_CLOSE:
118  return bio->shutdown;
119 
120  case BIO_CTRL_SET_CLOSE:
121  bio->shutdown = num;
122  return 1;
123 
124  case BIO_CTRL_WPENDING:
125  return BIO_ctrl(SSL_get_wbio(ssl), cmd, num, ptr);
126 
127  case BIO_CTRL_PENDING:
128  return SSL_pending(ssl);
129 
130  case BIO_CTRL_FLUSH: {
131  BIO *wbio = SSL_get_wbio(ssl);
133  long ret = BIO_ctrl(wbio, cmd, num, ptr);
136  return ret;
137  }
138 
139  case BIO_CTRL_PUSH:
140  case BIO_CTRL_POP:
141  case BIO_CTRL_DUP:
142  return -1;
143 
144  default:
145  return BIO_ctrl(SSL_get_rbio(ssl), cmd, num, ptr);
146  }
147 }
148 
149 static int ssl_new(BIO *bio) {
150  return 1;
151 }
152 
153 static int ssl_free(BIO *bio) {
154  SSL *ssl = get_ssl(bio);
155 
156  if (ssl == NULL) {
157  return 1;
158  }
159 
160  SSL_shutdown(ssl);
161  if (bio->shutdown) {
162  SSL_free(ssl);
163  }
164 
165  return 1;
166 }
167 
168 static long ssl_callback_ctrl(BIO *bio, int cmd, bio_info_cb fp) {
169  SSL *ssl = get_ssl(bio);
170  if (ssl == NULL) {
171  return 0;
172  }
173 
174  switch (cmd) {
176  return -1;
177 
178  default:
179  return BIO_callback_ctrl(SSL_get_rbio(ssl), cmd, fp);
180  }
181 }
182 
183 static const BIO_METHOD ssl_method = {
184  BIO_TYPE_SSL, "SSL", ssl_write, ssl_read, NULL,
186 };
187 
188 const BIO_METHOD *BIO_f_ssl(void) { return &ssl_method; }
189 
190 long BIO_set_ssl(BIO *bio, SSL *ssl, int take_owership) {
191  return BIO_ctrl(bio, BIO_C_SET_SSL, take_owership, ssl);
192 }
bio_method_st
Definition: bio.h:808
ptr
char * ptr
Definition: abseil-cpp/absl/base/internal/low_level_alloc_test.cc:45
bio_info_cb
long(* bio_info_cb)(BIO *bio, int event, const char *parg, int cmd, long larg, long return_value)
Definition: bio.h:246
BIO_CTRL_WPENDING
#define BIO_CTRL_WPENDING
Definition: bio.h:715
gen_build_yaml.out
dictionary out
Definition: src/benchmark/gen_build_yaml.py:24
SSL_shutdown
#define SSL_shutdown
Definition: boringssl_prefix_symbols.h:515
BIO_set_ssl
long BIO_set_ssl(BIO *bio, SSL *ssl, int take_owership)
Definition: bio_ssl.cc:190
BIO_RR_ACCEPT
#define BIO_RR_ACCEPT
Definition: bio.h:196
ssl_write
static int ssl_write(BIO *bio, const char *out, int outl)
Definition: bio_ssl.cc:59
ssl_read
static int ssl_read(BIO *bio, char *out, int outl)
Definition: bio_ssl.cc:19
bio_st
Definition: bio.h:822
bio_st::ptr
void * ptr
Definition: bio.h:838
BIO_set_retry_write
#define BIO_set_retry_write
Definition: boringssl_prefix_symbols.h:856
ssl_ctrl
static long ssl_ctrl(BIO *bio, int cmd, long num, void *ptr)
Definition: bio_ssl.cc:93
SSL_ERROR_WANT_READ
#define SSL_ERROR_WANT_READ
Definition: ssl.h:494
ssl_method
static const BIO_METHOD ssl_method
Definition: bio_ssl.cc:183
OPENSSL_PUT_ERROR
#define OPENSSL_PUT_ERROR(library, reason)
Definition: err.h:423
SSL_ERROR_SSL
#define SSL_ERROR_SSL
Definition: ssl.h:485
bio.h
bio_st::init
int init
Definition: bio.h:826
BIO_CTRL_PUSH
#define BIO_CTRL_PUSH
Definition: bio.h:727
SSL_ERROR_NONE
#define SSL_ERROR_NONE
Definition: ssl.h:481
BIO_set_retry_special
#define BIO_set_retry_special
Definition: boringssl_prefix_symbols.h:855
BIO_C_SET_SSL
#define BIO_C_SET_SSL
Definition: bio.h:854
BIO_get_retry_flags
#define BIO_get_retry_flags
Definition: boringssl_prefix_symbols.h:796
SSL_ERROR_WANT_ACCEPT
#define SSL_ERROR_WANT_ACCEPT
Definition: ssl.h:530
ssl_callback_ctrl
static long ssl_callback_ctrl(BIO *bio, int cmd, bio_info_cb fp)
Definition: bio_ssl.cc:168
SSL_get_error
#define SSL_get_error
Definition: boringssl_prefix_symbols.h:340
ssl_st
Definition: third_party/boringssl-with-bazel/src/ssl/internal.h:3698
ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
#define ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED
Definition: err.h:372
BIO_ctrl
#define BIO_ctrl
Definition: boringssl_prefix_symbols.h:779
BIO_CTRL_DUP
#define BIO_CTRL_DUP
Definition: bio.h:729
BIO_CTRL_SET_CALLBACK
#define BIO_CTRL_SET_CALLBACK
Definition: bio.h:719
BIO_get_retry_reason
#define BIO_get_retry_reason
Definition: boringssl_prefix_symbols.h:797
BIO_set_flags
#define BIO_set_flags
Definition: boringssl_prefix_symbols.h:847
SSL_get_rbio
#define SSL_get_rbio
Definition: boringssl_prefix_symbols.h:366
regen-readme.cmd
cmd
Definition: regen-readme.py:21
ssl_new
static int ssl_new(BIO *bio)
Definition: bio_ssl.cc:149
BIO_TYPE_SSL
#define BIO_TYPE_SSL
Definition: bio.h:780
BIO_callback_ctrl
#define BIO_callback_ctrl
Definition: boringssl_prefix_symbols.h:775
BIO_CTRL_POP
#define BIO_CTRL_POP
Definition: bio.h:728
BIO_RR_CONNECT
#define BIO_RR_CONNECT
Definition: bio.h:193
bio_st::shutdown
int shutdown
Definition: bio.h:831
ssl.h
SSL_ERROR_WANT_WRITE
#define SSL_ERROR_WANT_WRITE
Definition: ssl.h:499
BIO_CTRL_GET_CLOSE
#define BIO_CTRL_GET_CLOSE
Definition: bio.h:702
SSL_free
#define SSL_free
Definition: boringssl_prefix_symbols.h:308
BIO_CTRL_FLUSH
#define BIO_CTRL_FLUSH
Definition: bio.h:712
BIO_CTRL_PENDING
#define BIO_CTRL_PENDING
Definition: bio.h:709
get_ssl
static SSL * get_ssl(BIO *bio)
Definition: bio_ssl.cc:15
BIO_CTRL_SET_CLOSE
#define BIO_CTRL_SET_CLOSE
Definition: bio.h:706
SSL_ERROR_ZERO_RETURN
#define SSL_ERROR_ZERO_RETURN
Definition: ssl.h:518
ret
UniquePtr< SSL_SESSION > ret
Definition: ssl_x509.cc:1029
BIO_set_retry_reason
#define BIO_set_retry_reason
Definition: boringssl_prefix_symbols.h:854
SSL_pending
#define SSL_pending
Definition: boringssl_prefix_symbols.h:417
SSL_ERROR_WANT_CONNECT
#define SSL_ERROR_WANT_CONNECT
Definition: ssl.h:523
xds_manager.num
num
Definition: xds_manager.py:56
SSL_get_wbio
#define SSL_get_wbio
Definition: boringssl_prefix_symbols.h:394
SSL_write
#define SSL_write
Definition: boringssl_prefix_symbols.h:533
ssl_free
static int ssl_free(BIO *bio)
Definition: bio_ssl.cc:153
BIO_f_ssl
const BIO_METHOD * BIO_f_ssl(void)
Definition: bio_ssl.cc:188
BIO_set_retry_read
#define BIO_set_retry_read
Definition: boringssl_prefix_symbols.h:853
SSL_read
#define SSL_read
Definition: boringssl_prefix_symbols.h:424
BIO_clear_retry_flags
#define BIO_clear_retry_flags
Definition: boringssl_prefix_symbols.h:777
SSL_ERROR_SYSCALL
#define SSL_ERROR_SYSCALL
Definition: ssl.h:514


grpc
Author(s):
autogenerated on Fri May 16 2025 02:57:48