Client RequestChannels and folly/python/futures.h
[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 #include <folly/String.h>
18 #include <folly/io/async/ssl/OpenSSLPtrTypes.h>
19
20 #include <openssl/x509.h>
21 #include <openssl/x509v3.h>
22
23 #include <folly/ScopeGuard.h>
24
25 namespace folly {
26 namespace ssl {
27
28 Optional<std::string> OpenSSLCertUtils::getCommonName(X509& x509) {
29   auto subject = X509_get_subject_name(&x509);
30   if (!subject) {
31     return none;
32   }
33
34   auto cnLoc = X509_NAME_get_index_by_NID(subject, NID_commonName, -1);
35   if (cnLoc < 0) {
36     return none;
37   }
38
39   auto cnEntry = X509_NAME_get_entry(subject, cnLoc);
40   if (!cnEntry) {
41     return none;
42   }
43
44   auto cnAsn = X509_NAME_ENTRY_get_data(cnEntry);
45   if (!cnAsn) {
46     return none;
47   }
48
49   auto cnData = reinterpret_cast<const char*>(ASN1_STRING_data(cnAsn));
50   auto cnLen = ASN1_STRING_length(cnAsn);
51   if (!cnData || cnLen <= 0) {
52     return none;
53   }
54
55   return Optional<std::string>(std::string(cnData, cnLen));
56 }
57
58 std::vector<std::string> OpenSSLCertUtils::getSubjectAltNames(X509& x509) {
59   auto names = reinterpret_cast<STACK_OF(GENERAL_NAME)*>(
60       X509_get_ext_d2i(&x509, NID_subject_alt_name, nullptr, nullptr));
61   if (!names) {
62     return {};
63   }
64   SCOPE_EXIT {
65     sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free);
66   };
67
68   std::vector<std::string> ret;
69   auto count = sk_GENERAL_NAME_num(names);
70   for (int i = 0; i < count; i++) {
71     auto genName = sk_GENERAL_NAME_value(names, i);
72     if (!genName || genName->type != GEN_DNS) {
73       continue;
74     }
75     auto nameData =
76         reinterpret_cast<const char*>(ASN1_STRING_data(genName->d.dNSName));
77     auto nameLen = ASN1_STRING_length(genName->d.dNSName);
78     if (!nameData || nameLen <= 0) {
79       continue;
80     }
81     ret.emplace_back(nameData, nameLen);
82   }
83   return ret;
84 }
85
86 Optional<std::string> OpenSSLCertUtils::getSubject(X509& x509) {
87   auto subject = X509_get_subject_name(&x509);
88   if (!subject) {
89     return none;
90   }
91
92   auto bio = BioUniquePtr(BIO_new(BIO_s_mem()));
93   if (bio == nullptr) {
94     throw std::runtime_error("Cannot allocate bio");
95   }
96   if (X509_NAME_print_ex(bio.get(), subject, 0, XN_FLAG_ONELINE) <= 0) {
97     return none;
98   }
99
100   char* bioData = nullptr;
101   size_t bioLen = BIO_get_mem_data(bio.get(), &bioData);
102   return std::string(bioData, bioLen);
103 }
104
105 Optional<std::string> OpenSSLCertUtils::getIssuer(X509& x509) {
106   auto issuer = X509_get_issuer_name(&x509);
107   if (!issuer) {
108     return none;
109   }
110
111   auto bio = BioUniquePtr(BIO_new(BIO_s_mem()));
112   if (bio == nullptr) {
113     throw std::runtime_error("Cannot allocate bio");
114   }
115
116   if (X509_NAME_print_ex(bio.get(), issuer, 0, XN_FLAG_ONELINE) <= 0) {
117     return none;
118   }
119
120   char* bioData = nullptr;
121   size_t bioLen = BIO_get_mem_data(bio.get(), &bioData);
122   return std::string(bioData, bioLen);
123 }
124
125 folly::Optional<std::string> OpenSSLCertUtils::toString(X509& x509) {
126   auto in = BioUniquePtr(BIO_new(BIO_s_mem()));
127   if (in == nullptr) {
128     throw std::runtime_error("Cannot allocate bio");
129   }
130
131   int flags = 0;
132
133   flags |= X509_FLAG_NO_HEADER | /* A few bytes of cert and data */
134       X509_FLAG_NO_PUBKEY | /* Public key */
135       X509_FLAG_NO_AUX | /* Auxiliary info? */
136       X509_FLAG_NO_SIGDUMP | /* Prints the signature */
137       X509_FLAG_NO_SIGNAME; /* Signature algorithms */
138
139 #ifdef X509_FLAG_NO_IDS
140   flags |= X509_FLAG_NO_IDS; /* Issuer/subject IDs */
141 #endif
142
143   if (X509_print_ex(in.get(), &x509, XN_FLAG_ONELINE, flags) > 0) {
144     char* bioData = nullptr;
145     size_t bioLen = BIO_get_mem_data(in.get(), &bioData);
146     return std::string(bioData, bioLen);
147   } else {
148     return none;
149   }
150 }
151
152 std::string OpenSSLCertUtils::getNotAfterTime(X509& x509) {
153   return getDateTimeStr(X509_get_notAfter(&x509));
154 }
155
156 std::string OpenSSLCertUtils::getNotBeforeTime(X509& x509) {
157   return getDateTimeStr(X509_get_notBefore(&x509));
158 }
159
160 std::string OpenSSLCertUtils::getDateTimeStr(const ASN1_TIME* time) {
161   if (!time) {
162     return "";
163   }
164
165   std::array<char, 32> buf;
166
167   auto bio = BioUniquePtr(BIO_new(BIO_s_mem()));
168   if (bio == nullptr) {
169     throw std::runtime_error("Cannot allocate bio");
170   }
171
172   if (ASN1_TIME_print(bio.get(), time) <= 0) {
173     throw std::runtime_error("Cannot print ASN1_TIME");
174   }
175
176   char* bioData = nullptr;
177   size_t bioLen = BIO_get_mem_data(bio.get(), &bioData);
178   return std::string(bioData, bioLen);
179 }
180
181 } // ssl
182 } // folly