codemod: ASN1_STRING_data -> ASN1_STRING_get0_data
[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 #endif
66
67 #if !FOLLY_OPENSSL_IS_110
68 void BIO_meth_free(BIO_METHOD* biom) {
69   OPENSSL_free((void*)biom);
70 }
71
72 int BIO_meth_set_read(BIO_METHOD* biom, int (*read)(BIO*, char*, int)) {
73   biom->bread = read;
74   return 1;
75 }
76
77 int BIO_meth_set_write(BIO_METHOD* biom, int (*write)(BIO*, const char*, int)) {
78   biom->bwrite = write;
79   return 1;
80 }
81
82 const char* SSL_SESSION_get0_hostname(const SSL_SESSION* s) {
83   return s->tlsext_hostname;
84 }
85
86 unsigned char* ASN1_STRING_get0_data(const ASN1_STRING* x) {
87   return ASN1_STRING_data((ASN1_STRING*)x);
88 }
89
90 EVP_MD_CTX* EVP_MD_CTX_new(void) {
91   EVP_MD_CTX* ctx = (EVP_MD_CTX*)OPENSSL_malloc(sizeof(EVP_MD_CTX));
92   if (!ctx) {
93     throw std::runtime_error("Cannot allocate EVP_MD_CTX");
94   }
95   EVP_MD_CTX_init(ctx);
96   return ctx;
97 }
98
99 void EVP_MD_CTX_free(EVP_MD_CTX* ctx) {
100   EVP_MD_CTX_destroy(ctx);
101 }
102
103 HMAC_CTX* HMAC_CTX_new() {
104   HMAC_CTX* ctx = (HMAC_CTX*)OPENSSL_malloc(sizeof(HMAC_CTX));
105   if (!ctx) {
106     throw std::runtime_error("Cannot allocate HMAC_CTX");
107   }
108   HMAC_CTX_init(ctx);
109   return ctx;
110 }
111
112 void HMAC_CTX_free(HMAC_CTX* ctx) {
113   if (ctx) {
114     HMAC_CTX_cleanup(ctx);
115     OPENSSL_free(ctx);
116   }
117 }
118
119 int SSL_SESSION_has_ticket(const SSL_SESSION* s) {
120   return (s->tlsext_ticklen > 0) ? 1 : 0;
121 }
122
123 unsigned long SSL_SESSION_get_ticket_lifetime_hint(const SSL_SESSION* s) {
124   return s->tlsext_tick_lifetime_hint;
125 }
126
127 // This is taken from OpenSSL 1.1.0
128 int DH_set0_pqg(DH* dh, BIGNUM* p, BIGNUM* q, BIGNUM* g) {
129   /* If the fields p and g in d are NULL, the corresponding input
130    * parameters MUST be non-NULL.  q may remain NULL.
131    */
132   if (dh == nullptr || (dh->p == nullptr && p == nullptr) ||
133       (dh->g == nullptr && g == nullptr)) {
134     return 0;
135   }
136
137   if (p != nullptr) {
138     BN_free(dh->p);
139     dh->p = p;
140   }
141   if (q != nullptr) {
142     BN_free(dh->q);
143     dh->q = q;
144   }
145   if (g != nullptr) {
146     BN_free(dh->g);
147     dh->g = g;
148   }
149
150   // In OpenSSL 1.1.0, DH_set0_pqg also sets
151   //   dh->length = BN_num_bits(q)
152   // With OpenSSL 1.0.2, the output of openssl dhparam -C 2048 doesn't set
153   // the length field. So as far as the compat lib is concerned, this wrapper
154   // mimics the functionality of OpenSSL 1.0.2
155   // Note: BoringSSL doesn't even have a length field anymore, just something
156   // called 'priv_length'. Let's not mess with that for now.
157
158   return 1;
159 }
160 #endif
161
162 }
163 }
164 }