• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_OPENSSLERROR_H_
18 #define CONSCRYPT_OPENSSLERROR_H_
19 
20 #include <openssl/ssl.h>
21 
22 namespace conscrypt {
23 
24 /**
25  * Manages the freeing of the OpenSSL error stack. This allows you to
26  * instantiate this object during an SSL call that may fail and not worry
27  * about manually calling ERR_clear_error() later.
28  *
29  * As an optimization, you can also call .release() for passing as an
30  * argument to things that free the error stack state as a side-effect.
31  */
32 class OpenSslError {
33 public:
OpenSslError()34     OpenSslError() : sslError_(SSL_ERROR_NONE), released_(false) {}
35 
OpenSslError(SSL * ssl,int returnCode)36     OpenSslError(SSL* ssl, int returnCode) : sslError_(SSL_ERROR_NONE), released_(false) {
37         reset(ssl, returnCode);
38     }
39 
~OpenSslError()40     ~OpenSslError() {
41         if (!released_ && sslError_ != SSL_ERROR_NONE) {
42             ERR_clear_error();
43         }
44     }
45 
get()46     int get() const {
47         return sslError_;
48     }
49 
reset(SSL * ssl,int returnCode)50     void reset(SSL* ssl, int returnCode) {
51         if (returnCode <= 0) {
52             sslError_ = SSL_get_error(ssl, returnCode);
53         } else {
54             sslError_ = SSL_ERROR_NONE;
55         }
56     }
57 
release()58     int release() {
59         released_ = true;
60         return sslError_;
61     }
62 
63 private:
64     int sslError_;
65     bool released_;
66 };
67 
68 }  // namespace conscrypt
69 
70 #endif  // CONSCRYPT_OPENSSLERROR_H_
71