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>
18 #include <folly/portability/Sockets.h>
20 #include <openssl/err.h>
21 #include <openssl/rand.h>
22 #include <openssl/ssl.h>
23 #include <openssl/x509v3.h>
25 #include <glog/logging.h>
30 bool OpenSSLUtils::getPeerAddressFromX509StoreCtx(X509_STORE_CTX* ctx,
31 sockaddr_storage* addrStorage,
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);
39 LOG(ERROR) << "Inexplicably couldn't get fd from SSL";
43 *addrLen = sizeof(*addrStorage);
44 if (getpeername(fd, reinterpret_cast<sockaddr*>(addrStorage), addrLen) != 0) {
45 PLOG(ERROR) << "Unable to get peer name";
48 CHECK(*addrLen <= sizeof(*addrStorage));
52 bool OpenSSLUtils::validatePeerCertNames(X509* cert,
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));
59 if (altNames != nullptr) {
60 sk_GENERAL_NAME_pop_free(altNames, GENERAL_NAME_free);
63 if (altNames == nullptr) {
64 LOG(WARNING) << "No subjectAltName provided and we only support ip auth";
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);
76 LOG(FATAL) << "Unsupported sockaddr family: " << addr->sa_family;
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;
87 if (rawIpLen == 4 && addr4 != nullptr) {
88 if (::memcmp(rawIpStr, &addr4->sin_addr, rawIpLen) == 0) {
91 } else if (rawIpLen == 16 && addr6 != nullptr) {
92 if (::memcmp(rawIpStr, &addr6->sin6_addr, rawIpLen) == 0) {
95 } else if (rawIpLen != 4 && rawIpLen != 16) {
96 LOG(WARNING) << "Unexpected IP length: " << rawIpLen;
101 LOG(WARNING) << "Unable to match client cert against alt name ip";