1/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef CONSCRYPT_SCOPED_SSL_BIO_H_
18#define CONSCRYPT_SCOPED_SSL_BIO_H_
19
20#include <openssl/ssl.h>
21
22namespace conscrypt {
23
24/*
25 * Sets the read and write BIO for an SSL connection and removes it when it goes out of scope.
26 * We hang on to BIO with a JNI GlobalRef and we want to remove them as soon as possible.
27 */
28class ScopedSslBio {
29 public:
30    ScopedSslBio(SSL* ssl, BIO* rbio, BIO* wbio) : ssl_(ssl) {
31        SSL_set_bio(ssl_, rbio, wbio);
32        BIO_up_ref(rbio);
33        BIO_up_ref(wbio);
34    }
35
36    ~ScopedSslBio() {
37        SSL_set_bio(ssl_, nullptr, nullptr);
38    }
39
40 private:
41    SSL* const ssl_;
42};
43
44}  // namespace conscrypt
45
46#endif  // CONSCRYPT_SCOPED_SSL_BIO_H_
47