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.
18 #include <folly/portability/Sockets.h>
20 #include <openssl/x509v3.h>
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.
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
38 // TODO(agartrell): Add support for things like common name when
40 static bool validatePeerCertNames(X509* cert,
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.
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
54 static bool getPeerAddressFromX509StoreCtx(X509_STORE_CTX* ctx,
55 sockaddr_storage* addrStorage,