f0dc9d726ceb1a21fd9e75d9510c9a3520063ad8
[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
18 #include <stdexcept>
19
20 namespace folly {
21 namespace portability {
22 namespace ssl {
23
24 #if OPENSSL_IS_BORINGSSL
25 int SSL_CTX_set1_sigalgs_list(SSL_CTX*, const char*) {
26   return 1; // 0 implies error
27 }
28
29 int TLS1_get_client_version(SSL* s) {
30   // Note that this isn't the client version, and the API to
31   // get this has been hidden. It may be found by parsing the
32   // ClientHello (there is a callback via the SSL_HANDSHAKE struct)
33   return s->version;
34 }
35 #endif
36
37 #if FOLLY_OPENSSL_IS_100
38 uint32_t SSL_CIPHER_get_id(const SSL_CIPHER* c) {
39   return c->id;
40 }
41
42 int TLS1_get_client_version(const SSL* s) {
43   return (s->client_version >> 8) == TLS1_VERSION_MAJOR ? s->client_version : 0;
44 }
45 #endif
46
47 #if FOLLY_OPENSSL_IS_100 || FOLLY_OPENSSL_IS_101
48 int X509_get_signature_nid(X509* cert) {
49   return OBJ_obj2nid(cert->sig_alg->algorithm);
50 }
51 #endif
52
53 #if FOLLY_OPENSSL_IS_100 || FOLLY_OPENSSL_IS_101 || FOLLY_OPENSSL_IS_102
54 int SSL_CTX_up_ref(SSL_CTX* ctx) {
55   return CRYPTO_add(&ctx->references, 1, CRYPTO_LOCK_SSL_CTX);
56 }
57
58 int SSL_SESSION_up_ref(SSL_SESSION* session) {
59   return CRYPTO_add(&session->references, 1, CRYPTO_LOCK_SSL_SESSION);
60 }
61
62 int X509_up_ref(X509* x) {
63   return CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509);
64 }
65
66 int EVP_PKEY_up_ref(EVP_PKEY* evp) {
67   return CRYPTO_add(&evp->references, 1, CRYPTO_LOCK_EVP_PKEY);
68 }
69
70 void RSA_get0_key(
71     const RSA* r,
72     const BIGNUM** n,
73     const BIGNUM** e,
74     const BIGNUM** d) {
75   if (n != nullptr) {
76     *n = r->n;
77   }
78   if (e != nullptr) {
79     *e = r->e;
80   }
81   if (d != nullptr) {
82     *d = r->d;
83   }
84 }
85
86 RSA* EVP_PKEY_get0_RSA(EVP_PKEY* pkey) {
87   if (pkey->type != EVP_PKEY_RSA) {
88     return nullptr;
89   }
90   return pkey->pkey.rsa;
91 }
92
93 DSA* EVP_PKEY_get0_DSA(EVP_PKEY* pkey) {
94   if (pkey->type != EVP_PKEY_DSA) {
95     return nullptr;
96   }
97   return pkey->pkey.dsa;
98 }
99
100 DH* EVP_PKEY_get0_DH(EVP_PKEY* pkey) {
101   if (pkey->type != EVP_PKEY_DH) {
102     return nullptr;
103   }
104   return pkey->pkey.dh;
105 }
106
107 EC_KEY* EVP_PKEY_get0_EC_KEY(EVP_PKEY* pkey) {
108   if (pkey->type != EVP_PKEY_EC) {
109     return nullptr;
110   }
111   return pkey->pkey.ec;
112 }
113 #endif
114
115 #if !FOLLY_OPENSSL_IS_110
116 void BIO_meth_free(BIO_METHOD* biom) {
117   OPENSSL_free((void*)biom);
118 }
119
120 int BIO_meth_set_read(BIO_METHOD* biom, int (*read)(BIO*, char*, int)) {
121   biom->bread = read;
122   return 1;
123 }
124
125 int BIO_meth_set_write(BIO_METHOD* biom, int (*write)(BIO*, const char*, int)) {
126   biom->bwrite = write;
127   return 1;
128 }
129
130 const char* SSL_SESSION_get0_hostname(const SSL_SESSION* s) {
131   return s->tlsext_hostname;
132 }
133
134 unsigned char* ASN1_STRING_get0_data(const ASN1_STRING* x) {
135   return ASN1_STRING_data((ASN1_STRING*)x);
136 }
137
138 int SSL_SESSION_has_ticket(const SSL_SESSION* s) {
139   return (s->tlsext_ticklen > 0) ? 1 : 0;
140 }
141
142 unsigned long SSL_SESSION_get_ticket_lifetime_hint(const SSL_SESSION* s) {
143   return s->tlsext_tick_lifetime_hint;
144 }
145
146 // This is taken from OpenSSL 1.1.0
147 int DH_set0_pqg(DH* dh, BIGNUM* p, BIGNUM* q, BIGNUM* g) {
148   /* If the fields p and g in d are nullptr, the corresponding input
149    * parameters MUST not be nullptr.  q may remain nullptr.
150    */
151   if (dh == nullptr || (dh->p == nullptr && p == nullptr) ||
152       (dh->g == nullptr && g == nullptr)) {
153     return 0;
154   }
155
156   if (p != nullptr) {
157     BN_free(dh->p);
158     dh->p = p;
159   }
160   if (q != nullptr) {
161     BN_free(dh->q);
162     dh->q = q;
163   }
164   if (g != nullptr) {
165     BN_free(dh->g);
166     dh->g = g;
167   }
168
169   // In OpenSSL 1.1.0, DH_set0_pqg also sets
170   //   dh->length = BN_num_bits(q)
171   // With OpenSSL 1.0.2, the output of openssl dhparam -C 2048 doesn't set
172   // the length field. So as far as the compat lib is concerned, this wrapper
173   // mimics the functionality of OpenSSL 1.0.2
174   // Note: BoringSSL doesn't even have a length field anymore, just something
175   // called 'priv_length'. Let's not mess with that for now.
176
177   return 1;
178 }
179
180 void DH_get0_pqg(
181     const DH* dh,
182     const BIGNUM** p,
183     const BIGNUM** q,
184     const BIGNUM** g) {
185   // Based off of https://wiki.openssl.org/index.php/OpenSSL_1.1.0_Changes
186   if (p != nullptr) {
187     *p = dh->p;
188   }
189   if (q != nullptr) {
190     *q = dh->q;
191   }
192   if (g != nullptr) {
193     *g = dh->g;
194   }
195 }
196
197 void DH_get0_key(
198     const DH* dh,
199     const BIGNUM** pub_key,
200     const BIGNUM** priv_key) {
201   // Based off of https://wiki.openssl.org/index.php/OpenSSL_1.1.0_Changes
202   if (pub_key != nullptr) {
203     *pub_key = dh->pub_key;
204   }
205   if (priv_key != nullptr) {
206     *priv_key = dh->priv_key;
207   }
208 }
209
210 void DSA_get0_pqg(
211     const DSA* dsa,
212     const BIGNUM** p,
213     const BIGNUM** q,
214     const BIGNUM** g) {
215   // Based off of https://wiki.openssl.org/index.php/OpenSSL_1.1.0_Changes
216   if (p != nullptr) {
217     *p = dsa->p;
218   }
219   if (q != nullptr) {
220     *q = dsa->q;
221   }
222   if (g != nullptr) {
223     *g = dsa->g;
224   }
225 }
226
227 void DSA_get0_key(
228     const DSA* dsa,
229     const BIGNUM** pub_key,
230     const BIGNUM** priv_key) {
231   // Based off of https://wiki.openssl.org/index.php/OpenSSL_1.1.0_Changes
232   if (pub_key != nullptr) {
233     *pub_key = dsa->pub_key;
234   }
235   if (priv_key != nullptr) {
236     *priv_key = dsa->priv_key;
237   }
238 }
239
240 X509* X509_STORE_CTX_get0_cert(X509_STORE_CTX* ctx) {
241   return ctx->cert;
242 }
243
244 STACK_OF(X509) * X509_STORE_CTX_get0_chain(X509_STORE_CTX* ctx) {
245   return X509_STORE_CTX_get_chain(ctx);
246 }
247
248 STACK_OF(X509) * X509_STORE_CTX_get0_untrusted(X509_STORE_CTX* ctx) {
249   return ctx->untrusted;
250 }
251
252 EVP_MD_CTX* EVP_MD_CTX_new() {
253   EVP_MD_CTX* ctx = (EVP_MD_CTX*)OPENSSL_malloc(sizeof(EVP_MD_CTX));
254   if (!ctx) {
255     throw std::runtime_error("Cannot allocate EVP_MD_CTX");
256   }
257   EVP_MD_CTX_init(ctx);
258   return ctx;
259 }
260
261 void EVP_MD_CTX_free(EVP_MD_CTX* ctx) {
262   if (ctx) {
263     EVP_MD_CTX_cleanup(ctx);
264     OPENSSL_free(ctx);
265   }
266 }
267
268 HMAC_CTX* HMAC_CTX_new() {
269   HMAC_CTX* ctx = (HMAC_CTX*)OPENSSL_malloc(sizeof(HMAC_CTX));
270   if (!ctx) {
271     throw std::runtime_error("Cannot allocate HMAC_CTX");
272   }
273   HMAC_CTX_init(ctx);
274   return ctx;
275 }
276
277 void HMAC_CTX_free(HMAC_CTX* ctx) {
278   if (ctx) {
279     HMAC_CTX_cleanup(ctx);
280     OPENSSL_free(ctx);
281   }
282 }
283
284 bool RSA_set0_key(RSA* r, BIGNUM* n, BIGNUM* e, BIGNUM* d) {
285   // Based off of https://wiki.openssl.org/index.php/OpenSSL_1.1.0_Changes
286   /**
287    * If the fields n and e in r are nullptr, the corresponding input parameters
288    * MUST be non-nullptr for n and e. d may be left NULL (in case only the
289    * public key is used).
290    */
291   if ((r->n == nullptr && n == nullptr) || (r->e == nullptr && e == nullptr)) {
292     return false;
293   }
294   if (n != nullptr) {
295     BN_free(r->n);
296     r->n = n;
297   }
298   if (e != nullptr) {
299     BN_free(r->e);
300     r->e = e;
301   }
302   if (d != nullptr) {
303     BN_free(r->d);
304     r->d = d;
305   }
306   return true;
307 }
308
309 void RSA_get0_factors(const RSA* r, const BIGNUM** p, const BIGNUM** q) {
310   // Based off of https://wiki.openssl.org/index.php/OpenSSL_1.1.0_Changes
311   if (p != nullptr) {
312     *p = r->p;
313   }
314   if (q != nullptr) {
315     *q = r->q;
316   }
317 }
318
319 void RSA_get0_crt_params(
320     const RSA* r,
321     const BIGNUM** dmp1,
322     const BIGNUM** dmq1,
323     const BIGNUM** iqmp) {
324   // Based off of https://wiki.openssl.org/index.php/OpenSSL_1.1.0_Changes
325   if (dmp1 != nullptr) {
326     *dmp1 = r->dmp1;
327   }
328   if (dmq1 != nullptr) {
329     *dmq1 = r->dmq1;
330   }
331   if (iqmp != nullptr) {
332     *iqmp = r->iqmp;
333   }
334 }
335
336 int ECDSA_SIG_set0(ECDSA_SIG* sig, BIGNUM* r, BIGNUM* s) {
337   // Based off of https://wiki.openssl.org/index.php/OpenSSL_1.1.0_Changes
338   if (r == nullptr || s == nullptr) {
339     return 0;
340   }
341   BN_clear_free(sig->r);
342   BN_clear_free(sig->s);
343   sig->r = r;
344   sig->s = s;
345   return 1;
346 }
347
348 void ECDSA_SIG_get0(
349     const ECDSA_SIG* sig,
350     const BIGNUM** pr,
351     const BIGNUM** ps) {
352   // Based off of https://wiki.openssl.org/index.php/OpenSSL_1.1.0_Changes
353   if (pr != nullptr) {
354     *pr = sig->r;
355   }
356   if (ps != nullptr) {
357     *ps = sig->s;
358   }
359 }
360 #endif
361 }
362 }
363 }