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