Adds writer test case for RCU
[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/FileUtil.h>
19 #include <folly/ScopeGuard.h>
20 #include <folly/String.h>
21 #include <folly/ssl/OpenSSLPtrTypes.h>
22
23 namespace folly {
24 namespace ssl {
25
26 Optional<std::string> OpenSSLCertUtils::getCommonName(X509& x509) {
27   auto subject = X509_get_subject_name(&x509);
28   if (!subject) {
29     return none;
30   }
31
32   auto cnLoc = X509_NAME_get_index_by_NID(subject, NID_commonName, -1);
33   if (cnLoc < 0) {
34     return none;
35   }
36
37   auto cnEntry = X509_NAME_get_entry(subject, cnLoc);
38   if (!cnEntry) {
39     return none;
40   }
41
42   auto cnAsn = X509_NAME_ENTRY_get_data(cnEntry);
43   if (!cnAsn) {
44     return none;
45   }
46
47   auto cnData = reinterpret_cast<const char*>(ASN1_STRING_get0_data(cnAsn));
48   auto cnLen = ASN1_STRING_length(cnAsn);
49   if (!cnData || cnLen <= 0) {
50     return none;
51   }
52
53   return Optional<std::string>(std::string(cnData, cnLen));
54 }
55
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));
59   if (!names) {
60     return {};
61   }
62   SCOPE_EXIT {
63     sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free);
64   };
65
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) {
71       continue;
72     }
73     auto nameData = reinterpret_cast<const char*>(
74         ASN1_STRING_get0_data(genName->d.dNSName));
75     auto nameLen = ASN1_STRING_length(genName->d.dNSName);
76     if (!nameData || nameLen <= 0) {
77       continue;
78     }
79     ret.emplace_back(nameData, nameLen);
80   }
81   return ret;
82 }
83
84 Optional<std::string> OpenSSLCertUtils::getSubject(X509& x509) {
85   auto subject = X509_get_subject_name(&x509);
86   if (!subject) {
87     return none;
88   }
89
90   auto bio = BioUniquePtr(BIO_new(BIO_s_mem()));
91   if (bio == nullptr) {
92     throw std::runtime_error("Cannot allocate bio");
93   }
94   if (X509_NAME_print_ex(bio.get(), subject, 0, XN_FLAG_ONELINE) <= 0) {
95     return none;
96   }
97
98   char* bioData = nullptr;
99   size_t bioLen = BIO_get_mem_data(bio.get(), &bioData);
100   return std::string(bioData, bioLen);
101 }
102
103 Optional<std::string> OpenSSLCertUtils::getIssuer(X509& x509) {
104   auto issuer = X509_get_issuer_name(&x509);
105   if (!issuer) {
106     return none;
107   }
108
109   auto bio = BioUniquePtr(BIO_new(BIO_s_mem()));
110   if (bio == nullptr) {
111     throw std::runtime_error("Cannot allocate bio");
112   }
113
114   if (X509_NAME_print_ex(bio.get(), issuer, 0, XN_FLAG_ONELINE) <= 0) {
115     return none;
116   }
117
118   char* bioData = nullptr;
119   size_t bioLen = BIO_get_mem_data(bio.get(), &bioData);
120   return std::string(bioData, bioLen);
121 }
122
123 folly::Optional<std::string> OpenSSLCertUtils::toString(X509& x509) {
124   auto in = BioUniquePtr(BIO_new(BIO_s_mem()));
125   if (in == nullptr) {
126     throw std::runtime_error("Cannot allocate bio");
127   }
128
129   int flags = 0;
130
131   flags |= X509_FLAG_NO_HEADER | /* A few bytes of cert and data */
132       X509_FLAG_NO_PUBKEY | /* Public key */
133       X509_FLAG_NO_AUX | /* Auxiliary info? */
134       X509_FLAG_NO_SIGDUMP | /* Prints the signature */
135       X509_FLAG_NO_SIGNAME; /* Signature algorithms */
136
137 #ifdef X509_FLAG_NO_IDS
138   flags |= X509_FLAG_NO_IDS; /* Issuer/subject IDs */
139 #endif
140
141   if (X509_print_ex(in.get(), &x509, XN_FLAG_ONELINE, flags) > 0) {
142     char* bioData = nullptr;
143     size_t bioLen = BIO_get_mem_data(in.get(), &bioData);
144     return std::string(bioData, bioLen);
145   } else {
146     return none;
147   }
148 }
149
150 std::string OpenSSLCertUtils::getNotAfterTime(X509& x509) {
151   return getDateTimeStr(X509_get_notAfter(&x509));
152 }
153
154 std::string OpenSSLCertUtils::getNotBeforeTime(X509& x509) {
155   return getDateTimeStr(X509_get_notBefore(&x509));
156 }
157
158 std::string OpenSSLCertUtils::getDateTimeStr(const ASN1_TIME* time) {
159   if (!time) {
160     return "";
161   }
162
163   auto bio = BioUniquePtr(BIO_new(BIO_s_mem()));
164   if (bio == nullptr) {
165     throw std::runtime_error("Cannot allocate bio");
166   }
167
168   if (ASN1_TIME_print(bio.get(), time) <= 0) {
169     throw std::runtime_error("Cannot print ASN1_TIME");
170   }
171
172   char* bioData = nullptr;
173   size_t bioLen = BIO_get_mem_data(bio.get(), &bioData);
174   return std::string(bioData, bioLen);
175 }
176
177 X509UniquePtr OpenSSLCertUtils::derDecode(ByteRange range) {
178   auto begin = range.data();
179   X509UniquePtr cert(d2i_X509(nullptr, &begin, range.size()));
180   if (!cert) {
181     throw std::runtime_error("could not read cert");
182   }
183   return cert;
184 }
185
186 std::unique_ptr<IOBuf> OpenSSLCertUtils::derEncode(X509& x509) {
187   auto len = i2d_X509(&x509, nullptr);
188   if (len < 0) {
189     throw std::runtime_error("Error computing length");
190   }
191   auto buf = IOBuf::create(len);
192   auto dataPtr = buf->writableData();
193   len = i2d_X509(&x509, &dataPtr);
194   if (len < 0) {
195     throw std::runtime_error("Error converting cert to DER");
196   }
197   buf->append(len);
198   return buf;
199 }
200
201 std::vector<X509UniquePtr> OpenSSLCertUtils::readCertsFromBuffer(
202     ByteRange range) {
203   BioUniquePtr b(
204       BIO_new_mem_buf(const_cast<unsigned char*>(range.data()), range.size()));
205   if (!b) {
206     throw std::runtime_error("failed to create BIO");
207   }
208   std::vector<X509UniquePtr> certs;
209   while (true) {
210     X509UniquePtr x509(PEM_read_bio_X509(b.get(), nullptr, nullptr, nullptr));
211     if (!x509) {
212       break;
213     }
214     certs.push_back(std::move(x509));
215   }
216
217   return certs;
218 }
219
220 std::array<uint8_t, SHA_DIGEST_LENGTH> OpenSSLCertUtils::getDigestSha1(
221     X509& x509) {
222   unsigned int len;
223   std::array<uint8_t, SHA_DIGEST_LENGTH> md;
224   int rc = X509_digest(&x509, EVP_sha1(), md.data(), &len);
225
226   if (rc <= 0) {
227     throw std::runtime_error("Could not calculate SHA1 digest for cert");
228   }
229   return md;
230 }
231
232 std::array<uint8_t, SHA256_DIGEST_LENGTH> OpenSSLCertUtils::getDigestSha256(
233     X509& x509) {
234   unsigned int len;
235   std::array<uint8_t, SHA256_DIGEST_LENGTH> md;
236   int rc = X509_digest(&x509, EVP_sha256(), md.data(), &len);
237
238   if (rc <= 0) {
239     throw std::runtime_error("Could not calculate SHA256 digest for cert");
240   }
241   return md;
242 }
243
244 X509StoreUniquePtr OpenSSLCertUtils::readStoreFromFile(std::string caFile) {
245   std::string certData;
246   if (!folly::readFile(caFile.c_str(), certData)) {
247     throw std::runtime_error(
248         folly::to<std::string>("Could not read store file: ", caFile));
249   }
250   auto certRange = folly::ByteRange(folly::StringPiece(certData));
251   return readStoreFromBuffer(std::move(certRange));
252 }
253
254 X509StoreUniquePtr OpenSSLCertUtils::readStoreFromBuffer(ByteRange certRange) {
255   auto certs = readCertsFromBuffer(certRange);
256   ERR_clear_error();
257   folly::ssl::X509StoreUniquePtr store(X509_STORE_new());
258   for (auto& caCert : certs) {
259     if (X509_STORE_add_cert(store.get(), caCert.get()) != 1) {
260       auto err = ERR_get_error();
261       if (ERR_GET_LIB(err) != ERR_LIB_X509 ||
262           ERR_GET_REASON(err) != X509_R_CERT_ALREADY_IN_HASH_TABLE) {
263         std::array<char, 256> errBuff;
264         ERR_error_string_n(err, errBuff.data(), errBuff.size());
265         throw std::runtime_error(folly::to<std::string>(
266             "Could not insert CA certificate into store: ",
267             std::string(errBuff.data())));
268       }
269     }
270   }
271   return store;
272 }
273 } // namespace ssl
274 } // namespace folly