Add a pair of util functions for getting and setting the BIO fd
[folly.git] / folly / io / async / ssl / OpenSSLUtils.h
1 /*
2  * Copyright 2016 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 #pragma once
17
18 #include <folly/portability/Sockets.h>
19
20 #include <openssl/x509v3.h>
21
22 namespace folly {
23 namespace ssl {
24
25 class OpenSSLUtils {
26  public:
27   /**
28    * Validate that the peer certificate's common name or subject alt names
29    * match what we expect.  Currently this only checks for IPs within
30    * subject alt names but it could easily be expanded to check common name
31    * and hostnames as well.
32    *
33    * @param cert    X509* peer certificate
34    * @param addr    sockaddr object containing sockaddr to verify
35    * @param addrLen length of sockaddr as returned by getpeername or accept
36    * @return true iff a subject altname IP matches addr
37    */
38   // TODO(agartrell): Add support for things like common name when
39   // necessary.
40   static bool validatePeerCertNames(X509* cert,
41                                     const sockaddr* addr,
42                                     socklen_t addrLen);
43
44   /**
45    * Get the peer socket address from an X509_STORE_CTX*.  Unlike the
46    * accept, getsockname, getpeername, etc family of operations, addrLen's
47    * initial value is ignored and reset.
48    *
49    * @param ctx         Context from which to retrieve peer sockaddr
50    * @param addrStorage out param for address
51    * @param addrLen     out param for length of address
52    * @return true on success, false on failure
53    */
54   static bool getPeerAddressFromX509StoreCtx(X509_STORE_CTX* ctx,
55                                              sockaddr_storage* addrStorage,
56                                              socklen_t* addrLen);
57
58   /**
59   * Wrappers for BIO operations that may be different across different
60   * versions/flavors of OpenSSL (including forks like BoringSSL)
61   */
62   static bool setCustomBioReadMethod(
63       BIO_METHOD* bioMeth,
64       int (*meth)(BIO*, char*, int));
65   static bool setCustomBioWriteMethod(
66       BIO_METHOD* bioMeth,
67       int (*meth)(BIO*, const char*, int));
68   static int getBioShouldRetryWrite(int ret);
69   static void setBioAppData(BIO* b, void* ptr);
70   static void* getBioAppData(BIO* b);
71   static void setCustomBioMethod(BIO*, BIO_METHOD*);
72   static int getBioFd(BIO* b, int* fd);
73   static void setBioFd(BIO* b, int fd, int flags);
74 };
75
76 } // ssl
77 } // folly