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 <folly/ScopeGuard.h>
19 #include <folly/String.h>
20 #include <folly/io/async/ssl/OpenSSLPtrTypes.h>
25 Optional<std::string> OpenSSLCertUtils::getCommonName(X509& x509) {
26 auto subject = X509_get_subject_name(&x509);
31 auto cnLoc = X509_NAME_get_index_by_NID(subject, NID_commonName, -1);
36 auto cnEntry = X509_NAME_get_entry(subject, cnLoc);
41 auto cnAsn = X509_NAME_ENTRY_get_data(cnEntry);
46 auto cnData = reinterpret_cast<const char*>(ASN1_STRING_data(cnAsn));
47 auto cnLen = ASN1_STRING_length(cnAsn);
48 if (!cnData || cnLen <= 0) {
52 return Optional<std::string>(std::string(cnData, cnLen));
55 std::vector<std::string> OpenSSLCertUtils::getSubjectAltNames(X509& x509) {
56 auto names = reinterpret_cast<STACK_OF(GENERAL_NAME)*>(
57 X509_get_ext_d2i(&x509, NID_subject_alt_name, nullptr, nullptr));
62 sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free);
65 std::vector<std::string> ret;
66 auto count = sk_GENERAL_NAME_num(names);
67 for (int i = 0; i < count; i++) {
68 auto genName = sk_GENERAL_NAME_value(names, i);
69 if (!genName || genName->type != GEN_DNS) {
73 reinterpret_cast<const char*>(ASN1_STRING_data(genName->d.dNSName));
74 auto nameLen = ASN1_STRING_length(genName->d.dNSName);
75 if (!nameData || nameLen <= 0) {
78 ret.emplace_back(nameData, nameLen);
83 Optional<std::string> OpenSSLCertUtils::getSubject(X509& x509) {
84 auto subject = X509_get_subject_name(&x509);
89 auto bio = BioUniquePtr(BIO_new(BIO_s_mem()));
91 throw std::runtime_error("Cannot allocate bio");
93 if (X509_NAME_print_ex(bio.get(), subject, 0, XN_FLAG_ONELINE) <= 0) {
97 char* bioData = nullptr;
98 size_t bioLen = BIO_get_mem_data(bio.get(), &bioData);
99 return std::string(bioData, bioLen);
102 Optional<std::string> OpenSSLCertUtils::getIssuer(X509& x509) {
103 auto issuer = X509_get_issuer_name(&x509);
108 auto bio = BioUniquePtr(BIO_new(BIO_s_mem()));
109 if (bio == nullptr) {
110 throw std::runtime_error("Cannot allocate bio");
113 if (X509_NAME_print_ex(bio.get(), issuer, 0, XN_FLAG_ONELINE) <= 0) {
117 char* bioData = nullptr;
118 size_t bioLen = BIO_get_mem_data(bio.get(), &bioData);
119 return std::string(bioData, bioLen);
122 folly::Optional<std::string> OpenSSLCertUtils::toString(X509& x509) {
123 auto in = BioUniquePtr(BIO_new(BIO_s_mem()));
125 throw std::runtime_error("Cannot allocate bio");
130 flags |= X509_FLAG_NO_HEADER | /* A few bytes of cert and data */
131 X509_FLAG_NO_PUBKEY | /* Public key */
132 X509_FLAG_NO_AUX | /* Auxiliary info? */
133 X509_FLAG_NO_SIGDUMP | /* Prints the signature */
134 X509_FLAG_NO_SIGNAME; /* Signature algorithms */
136 #ifdef X509_FLAG_NO_IDS
137 flags |= X509_FLAG_NO_IDS; /* Issuer/subject IDs */
140 if (X509_print_ex(in.get(), &x509, XN_FLAG_ONELINE, flags) > 0) {
141 char* bioData = nullptr;
142 size_t bioLen = BIO_get_mem_data(in.get(), &bioData);
143 return std::string(bioData, bioLen);
149 std::string OpenSSLCertUtils::getNotAfterTime(X509& x509) {
150 return getDateTimeStr(X509_get_notAfter(&x509));
153 std::string OpenSSLCertUtils::getNotBeforeTime(X509& x509) {
154 return getDateTimeStr(X509_get_notBefore(&x509));
157 std::string OpenSSLCertUtils::getDateTimeStr(const ASN1_TIME* time) {
162 std::array<char, 32> buf;
164 auto bio = BioUniquePtr(BIO_new(BIO_s_mem()));
165 if (bio == nullptr) {
166 throw std::runtime_error("Cannot allocate bio");
169 if (ASN1_TIME_print(bio.get(), time) <= 0) {
170 throw std::runtime_error("Cannot print ASN1_TIME");
173 char* bioData = nullptr;
174 size_t bioLen = BIO_get_mem_data(bio.get(), &bioData);
175 return std::string(bioData, bioLen);