correct usage of namespace std for coroutines_trait specialization
[folly.git] / folly / ssl / OpenSSLCertUtils.cpp
index d45daa6d2aef34e5b2638df47883f3249baa1fe5..9b9efa3eb3f89401f2e303a485dbda7efb00f207 100644 (file)
@@ -196,5 +196,25 @@ std::unique_ptr<IOBuf> OpenSSLCertUtils::derEncode(X509& x509) {
   buf->append(len);
   return buf;
 }
-} // ssl
-} // folly
+
+std::vector<X509UniquePtr> OpenSSLCertUtils::readCertsFromBuffer(
+    ByteRange range) {
+  BioUniquePtr b(
+      BIO_new_mem_buf(const_cast<unsigned char*>(range.data()), range.size()));
+  if (!b) {
+    throw std::runtime_error("failed to create BIO");
+  }
+  std::vector<X509UniquePtr> certs;
+  while (true) {
+    X509UniquePtr x509(PEM_read_bio_X509(b.get(), nullptr, nullptr, nullptr));
+    if (!x509) {
+      break;
+    }
+    certs.push_back(std::move(x509));
+  }
+
+  return certs;
+}
+
+} // namespace ssl
+} // namespace folly