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