2 * Copyright 2016 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include <folly/io/async/ssl/OpenSSLUtils.h>
17 #include <folly/ScopeGuard.h>
19 #include <openssl/err.h>
20 #include <openssl/rand.h>
21 #include <openssl/ssl.h>
22 #include <openssl/x509v3.h>
24 #include <netinet/in.h>
25 #include <sys/socket.h>
27 #include <glog/logging.h>
32 bool OpenSSLUtils::getPeerAddressFromX509StoreCtx(X509_STORE_CTX* ctx,
33 sockaddr_storage* addrStorage,
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);
41 LOG(ERROR) << "Inexplicably couldn't get fd from SSL";
45 *addrLen = sizeof(*addrStorage);
46 if (getpeername(fd, reinterpret_cast<sockaddr*>(addrStorage), addrLen) != 0) {
47 PLOG(ERROR) << "Unable to get peer name";
50 CHECK(*addrLen <= sizeof(*addrStorage));
54 bool OpenSSLUtils::validatePeerCertNames(X509* cert,
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));
61 if (altNames != nullptr) {
62 sk_GENERAL_NAME_pop_free(altNames, GENERAL_NAME_free);
65 if (altNames == nullptr) {
66 LOG(WARNING) << "No subjectAltName provided and we only support ip auth";
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);
78 LOG(FATAL) << "Unsupported sockaddr family: " << addr->sa_family;
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;
89 if (rawIpLen == 4 && addr4 != nullptr) {
90 if (::memcmp(rawIpStr, &addr4->sin_addr, rawIpLen) == 0) {
93 } else if (rawIpLen == 16 && addr6 != nullptr) {
94 if (::memcmp(rawIpStr, &addr6->sin6_addr, rawIpLen) == 0) {
97 } else if (rawIpLen != 4 && rawIpLen != 16) {
98 LOG(WARNING) << "Unexpected IP length: " << rawIpLen;
103 LOG(WARNING) << "Unable to match client cert against alt name ip";