Fix leak in HMAC_CTX_free compat API
[folly.git] / folly / portability / OpenSSL.cpp
1 /*
2  * Copyright 2017 Facebook, Inc.
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 #include <folly/portability/OpenSSL.h>
17 #include <stdexcept>
18
19 namespace folly {
20 namespace ssl {
21
22 #if FOLLY_OPENSSL_IS_110
23 ////////////////////////////////////////////////////////////////////////////////
24 // APIs needed in 1.1.0 only
25 ////////////////////////////////////////////////////////////////////////////////
26
27 #else
28 ////////////////////////////////////////////////////////////////////////////////
29 // APIs needed in BoringSSL and OpenSSL < 1.1.0 (i.e., 1.0.2, 1.0.1, 1.0.0, etc)
30 ////////////////////////////////////////////////////////////////////////////////
31 void BIO_meth_free(BIO_METHOD* biom) {
32   OPENSSL_free((void*)biom);
33 }
34
35 int BIO_meth_set_read(BIO_METHOD* biom, int (*read)(BIO*, char*, int)) {
36   biom->bread = read;
37   return 1;
38 }
39
40 int BIO_meth_set_write(BIO_METHOD* biom, int (*write)(BIO*, const char*, int)) {
41   biom->bwrite = write;
42   return 1;
43 }
44
45 void EVP_MD_CTX_free(EVP_MD_CTX* ctx) {
46   EVP_MD_CTX_destroy(ctx);
47 }
48
49 const char* SSL_SESSION_get0_hostname(const SSL_SESSION* s) {
50   return s->tlsext_hostname;
51 }
52
53 EVP_MD_CTX* EVP_MD_CTX_new(void) {
54   EVP_MD_CTX* ctx = (EVP_MD_CTX*)OPENSSL_malloc(sizeof(EVP_MD_CTX));
55   if (!ctx) {
56     throw std::runtime_error("Cannot allocate EVP_MD_CTX");
57   }
58   EVP_MD_CTX_init(ctx);
59   return ctx;
60 }
61
62 HMAC_CTX* HMAC_CTX_new(void) {
63   HMAC_CTX* ctx = (HMAC_CTX*)OPENSSL_malloc(sizeof(HMAC_CTX));
64   if (!ctx) {
65     throw std::runtime_error("Cannot allocate HMAC_CTX");
66   }
67   HMAC_CTX_init(ctx);
68   return ctx;
69 }
70
71 void HMAC_CTX_free(HMAC_CTX* ctx) {
72   if (ctx) {
73     HMAC_CTX_cleanup(ctx);
74     OPENSSL_free(ctx);
75   }
76 }
77
78 int SSL_SESSION_has_ticket(const SSL_SESSION* s) {
79   return (s->tlsext_ticklen > 0) ? 1 : 0;
80 }
81
82 unsigned long SSL_SESSION_get_ticket_lifetime_hint(const SSL_SESSION* s) {
83   return s->tlsext_tick_lifetime_hint;
84 }
85
86 // This is taken from OpenSSL 1.1.0
87 int DH_set0_pqg(DH* dh, BIGNUM* p, BIGNUM* q, BIGNUM* g) {
88   /* If the fields p and g in d are NULL, the corresponding input
89    * parameters MUST be non-NULL.  q may remain NULL.
90    */
91   if (dh == nullptr || (dh->p == nullptr && p == nullptr) ||
92       (dh->g == nullptr && g == nullptr)) {
93     return 0;
94   }
95
96   if (p != nullptr) {
97     BN_free(dh->p);
98     dh->p = p;
99   }
100   if (q != nullptr) {
101     BN_free(dh->q);
102     dh->q = q;
103   }
104   if (g != nullptr) {
105     BN_free(dh->g);
106     dh->g = g;
107   }
108
109   // In OpenSSL 1.1.0, DH_set0_pqg also sets
110   //   dh->length = BN_num_bits(q)
111   // With OpenSSL 1.0.2, the output of openssl dhparam -C 2048 doesn't set
112   // the length field. So as far as the compat lib is concerned, this wrapper
113   // mimics the functionality of OpenSSL 1.0.2
114   // Note: BoringSSL doesn't even have a length field anymore, just something
115   // called 'priv_length'. Let's not mess with that for now.
116
117   return 1;
118 }
119
120 #ifdef OPENSSL_IS_BORINGSSL
121 ////////////////////////////////////////////////////////////////////////////////
122 // APIs needed in BoringSSL only
123 ////////////////////////////////////////////////////////////////////////////////
124 int SSL_CTX_set1_sigalgs_list(SSL_CTX*, const char*) {
125   return 1; // 0 implies error
126 }
127
128 int TLS1_get_client_version(SSL* s) {
129   // Note that this isn't the client version, and the API to
130   // get this has been hidden. It may be found by parsing the
131   // ClientHello (there is a callback via the SSL_HANDSHAKE struct)
132   return s->version;
133 }
134
135 #elif FOLLY_OPENSSL_IS_102 || FOLLY_OPENSSL_IS_101 || FOLLY_OPENSSL_IS_100
136 ////////////////////////////////////////////////////////////////////////////////
137 // APIs needed in 1.0.2 and 1.0.1/1.0.0 (both deprecated)
138 ////////////////////////////////////////////////////////////////////////////////
139 int SSL_CTX_up_ref(SSL_CTX* ctx) {
140   return CRYPTO_add(&ctx->references, 1, CRYPTO_LOCK_SSL_CTX);
141 }
142
143 int SSL_SESSION_up_ref(SSL_SESSION* session) {
144   return CRYPTO_add(&session->references, 1, CRYPTO_LOCK_SSL_SESSION);
145 }
146
147 int X509_up_ref(X509* x) {
148   return CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509);
149 }
150
151 #if FOLLY_OPENSSL_IS_101 || FOLLY_OPENSSL_IS_100
152 ////////////////////////////////////////////////////////////////////////////////
153 // APIs needed in 1.0.1/1.0.0 (both deprecated)
154 ////////////////////////////////////////////////////////////////////////////////
155 int X509_get_signature_nid(X509* cert) {
156   return OBJ_obj2nid(cert->sig_alg->algorithm);
157 }
158
159 #endif
160
161 #if FOLLY_OPENSSL_IS_100
162 ////////////////////////////////////////////////////////////////////////////////
163 // APIs needed only in 1.0.0 only (deprecated)
164 ////////////////////////////////////////////////////////////////////////////////
165 uint32_t SSL_CIPHER_get_id(const SSL_CIPHER* c) {
166   return c->id;
167 }
168
169 int TLS1_get_client_version(const SSL* s) {
170   return (s->client_version >> 8) == TLS1_VERSION_MAJOR ? s->client_version : 0;
171 }
172
173 #endif
174
175 #endif // !(OPENSSL_IS_BORINGSSL ||
176 //          FOLLY_OPENSSL_IS_101 ||
177 //          FOLLY_OPENSSL_IS_102 ||
178 //          FOLLY_OPENSSL_IS_100)
179
180 #endif // !FOLLY_OPENSSL_IS_110
181 }
182 }