Switch uses of networking headers to <folly/portability/Sockets.h>
[folly.git] / folly / io / async / ssl / OpenSSLUtils.cpp
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 #include <folly/io/async/ssl/OpenSSLUtils.h>
17 #include <folly/ScopeGuard.h>
18 #include <folly/portability/Sockets.h>
19
20 #include <openssl/err.h>
21 #include <openssl/rand.h>
22 #include <openssl/ssl.h>
23 #include <openssl/x509v3.h>
24
25 #include <glog/logging.h>
26
27 namespace folly {
28 namespace ssl {
29
30 bool OpenSSLUtils::getPeerAddressFromX509StoreCtx(X509_STORE_CTX* ctx,
31                                                   sockaddr_storage* addrStorage,
32                                                   socklen_t* addrLen) {
33   // Grab the ssl idx and then the ssl object so that we can get the peer
34   // name to compare against the ips in the subjectAltName
35   auto sslIdx = SSL_get_ex_data_X509_STORE_CTX_idx();
36   auto ssl = reinterpret_cast<SSL*>(X509_STORE_CTX_get_ex_data(ctx, sslIdx));
37   int fd = SSL_get_fd(ssl);
38   if (fd < 0) {
39     LOG(ERROR) << "Inexplicably couldn't get fd from SSL";
40     return false;
41   }
42
43   *addrLen = sizeof(*addrStorage);
44   if (getpeername(fd, reinterpret_cast<sockaddr*>(addrStorage), addrLen) != 0) {
45     PLOG(ERROR) << "Unable to get peer name";
46     return false;
47   }
48   CHECK(*addrLen <= sizeof(*addrStorage));
49   return true;
50 }
51
52 bool OpenSSLUtils::validatePeerCertNames(X509* cert,
53                                          const sockaddr* addr,
54                                          socklen_t /* addrLen */) {
55   // Try to extract the names within the SAN extension from the certificate
56   auto altNames = reinterpret_cast<STACK_OF(GENERAL_NAME)*>(
57       X509_get_ext_d2i(cert, NID_subject_alt_name, nullptr, nullptr));
58   SCOPE_EXIT {
59     if (altNames != nullptr) {
60       sk_GENERAL_NAME_pop_free(altNames, GENERAL_NAME_free);
61     }
62   };
63   if (altNames == nullptr) {
64     LOG(WARNING) << "No subjectAltName provided and we only support ip auth";
65     return false;
66   }
67
68   const sockaddr_in* addr4 = nullptr;
69   const sockaddr_in6* addr6 = nullptr;
70   if (addr != nullptr) {
71     if (addr->sa_family == AF_INET) {
72       addr4 = reinterpret_cast<const sockaddr_in*>(addr);
73     } else if (addr->sa_family == AF_INET6) {
74       addr6 = reinterpret_cast<const sockaddr_in6*>(addr);
75     } else {
76       LOG(FATAL) << "Unsupported sockaddr family: " << addr->sa_family;
77     }
78   }
79
80   for (int i = 0; i < sk_GENERAL_NAME_num(altNames); i++) {
81     auto name = sk_GENERAL_NAME_value(altNames, i);
82     if ((addr4 != nullptr || addr6 != nullptr) && name->type == GEN_IPADD) {
83       // Extra const-ness for paranoia
84       unsigned char const* const rawIpStr = name->d.iPAddress->data;
85       int const rawIpLen = name->d.iPAddress->length;
86
87       if (rawIpLen == 4 && addr4 != nullptr) {
88         if (::memcmp(rawIpStr, &addr4->sin_addr, rawIpLen) == 0) {
89           return true;
90         }
91       } else if (rawIpLen == 16 && addr6 != nullptr) {
92         if (::memcmp(rawIpStr, &addr6->sin6_addr, rawIpLen) == 0) {
93           return true;
94         }
95       } else if (rawIpLen != 4 && rawIpLen != 16) {
96         LOG(WARNING) << "Unexpected IP length: " << rawIpLen;
97       }
98     }
99   }
100
101   LOG(WARNING) << "Unable to match client cert against alt name ip";
102   return false;
103 }
104
105 } // ssl
106 } // folly