b1bdf180e737e8dbdfa5e4f1d0566d38e905aafa
[folly.git] / folly / ssl / OpenSSLCertUtils.cpp
1 /*
2  * Copyright 2017-present Facebook, Inc.
3  *
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
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 #include <folly/ssl/OpenSSLCertUtils.h>
17
18 #include <folly/ScopeGuard.h>
19 #include <folly/String.h>
20 #include <folly/ssl/OpenSSLPtrTypes.h>
21
22 namespace folly {
23 namespace ssl {
24
25 Optional<std::string> OpenSSLCertUtils::getCommonName(X509& x509) {
26   auto subject = X509_get_subject_name(&x509);
27   if (!subject) {
28     return none;
29   }
30
31   auto cnLoc = X509_NAME_get_index_by_NID(subject, NID_commonName, -1);
32   if (cnLoc < 0) {
33     return none;
34   }
35
36   auto cnEntry = X509_NAME_get_entry(subject, cnLoc);
37   if (!cnEntry) {
38     return none;
39   }
40
41   auto cnAsn = X509_NAME_ENTRY_get_data(cnEntry);
42   if (!cnAsn) {
43     return none;
44   }
45
46   auto cnData = reinterpret_cast<const char*>(ASN1_STRING_get0_data(cnAsn));
47   auto cnLen = ASN1_STRING_length(cnAsn);
48   if (!cnData || cnLen <= 0) {
49     return none;
50   }
51
52   return Optional<std::string>(std::string(cnData, cnLen));
53 }
54
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));
58   if (!names) {
59     return {};
60   }
61   SCOPE_EXIT {
62     sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free);
63   };
64
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) {
70       continue;
71     }
72     auto nameData = reinterpret_cast<const char*>(
73         ASN1_STRING_get0_data(genName->d.dNSName));
74     auto nameLen = ASN1_STRING_length(genName->d.dNSName);
75     if (!nameData || nameLen <= 0) {
76       continue;
77     }
78     ret.emplace_back(nameData, nameLen);
79   }
80   return ret;
81 }
82
83 Optional<std::string> OpenSSLCertUtils::getSubject(X509& x509) {
84   auto subject = X509_get_subject_name(&x509);
85   if (!subject) {
86     return none;
87   }
88
89   auto bio = BioUniquePtr(BIO_new(BIO_s_mem()));
90   if (bio == nullptr) {
91     throw std::runtime_error("Cannot allocate bio");
92   }
93   if (X509_NAME_print_ex(bio.get(), subject, 0, XN_FLAG_ONELINE) <= 0) {
94     return none;
95   }
96
97   char* bioData = nullptr;
98   size_t bioLen = BIO_get_mem_data(bio.get(), &bioData);
99   return std::string(bioData, bioLen);
100 }
101
102 Optional<std::string> OpenSSLCertUtils::getIssuer(X509& x509) {
103   auto issuer = X509_get_issuer_name(&x509);
104   if (!issuer) {
105     return none;
106   }
107
108   auto bio = BioUniquePtr(BIO_new(BIO_s_mem()));
109   if (bio == nullptr) {
110     throw std::runtime_error("Cannot allocate bio");
111   }
112
113   if (X509_NAME_print_ex(bio.get(), issuer, 0, XN_FLAG_ONELINE) <= 0) {
114     return none;
115   }
116
117   char* bioData = nullptr;
118   size_t bioLen = BIO_get_mem_data(bio.get(), &bioData);
119   return std::string(bioData, bioLen);
120 }
121
122 folly::Optional<std::string> OpenSSLCertUtils::toString(X509& x509) {
123   auto in = BioUniquePtr(BIO_new(BIO_s_mem()));
124   if (in == nullptr) {
125     throw std::runtime_error("Cannot allocate bio");
126   }
127
128   int flags = 0;
129
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 */
135
136 #ifdef X509_FLAG_NO_IDS
137   flags |= X509_FLAG_NO_IDS; /* Issuer/subject IDs */
138 #endif
139
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);
144   } else {
145     return none;
146   }
147 }
148
149 std::string OpenSSLCertUtils::getNotAfterTime(X509& x509) {
150   return getDateTimeStr(X509_get_notAfter(&x509));
151 }
152
153 std::string OpenSSLCertUtils::getNotBeforeTime(X509& x509) {
154   return getDateTimeStr(X509_get_notBefore(&x509));
155 }
156
157 std::string OpenSSLCertUtils::getDateTimeStr(const ASN1_TIME* time) {
158   if (!time) {
159     return "";
160   }
161
162   auto bio = BioUniquePtr(BIO_new(BIO_s_mem()));
163   if (bio == nullptr) {
164     throw std::runtime_error("Cannot allocate bio");
165   }
166
167   if (ASN1_TIME_print(bio.get(), time) <= 0) {
168     throw std::runtime_error("Cannot print ASN1_TIME");
169   }
170
171   char* bioData = nullptr;
172   size_t bioLen = BIO_get_mem_data(bio.get(), &bioData);
173   return std::string(bioData, bioLen);
174 }
175
176 } // ssl
177 } // folly