2 * Copyright 2017-present 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/ssl/OpenSSLCertUtils.h>
18 #include <openssl/x509.h>
19 #include <openssl/x509v3.h>
21 #include <folly/ScopeGuard.h>
26 Optional<std::string> OpenSSLCertUtils::getCommonName(X509& x509) {
27 auto subject = X509_get_subject_name(&x509);
32 auto cnLoc = X509_NAME_get_index_by_NID(subject, NID_commonName, -1);
37 auto cnEntry = X509_NAME_get_entry(subject, cnLoc);
42 auto cnAsn = X509_NAME_ENTRY_get_data(cnEntry);
47 auto cnData = reinterpret_cast<const char*>(ASN1_STRING_data(cnAsn));
48 auto cnLen = ASN1_STRING_length(cnAsn);
49 if (!cnData || cnLen <= 0) {
53 return Optional<std::string>(std::string(cnData, cnLen));
56 std::vector<std::string> OpenSSLCertUtils::getSubjectAltNames(X509& x509) {
57 auto names = reinterpret_cast<STACK_OF(GENERAL_NAME)*>(
58 X509_get_ext_d2i(&x509, NID_subject_alt_name, nullptr, nullptr));
63 sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free);
66 std::vector<std::string> ret;
67 auto count = sk_GENERAL_NAME_num(names);
68 for (int i = 0; i < count; i++) {
69 auto genName = sk_GENERAL_NAME_value(names, i);
70 if (!genName || genName->type != GEN_DNS) {
74 reinterpret_cast<const char*>(ASN1_STRING_data(genName->d.dNSName));
75 auto nameLen = ASN1_STRING_length(genName->d.dNSName);
76 if (!nameData || nameLen <= 0) {
79 ret.emplace_back(nameData, nameLen);