+ /* Reformat cert so that it has line breaks every 64 characters.
+ See http://comments.gmane.org/gmane.comp.encryption.openssl.user/55593
+ */
+
+ list<string> lines;
+ string line;
+
+ for (size_t i = 0; i < cert.length(); ++i) {
+ line += cert[i];
+ if (cert[i] == '\r' || cert[i] == '\n') {
+ boost::algorithm::trim (line);
+ lines.push_back (line);
+ line = "";
+ }
+ }
+
+ if (!line.empty()) {
+ boost::algorithm::trim (line);
+ lines.push_back (line);
+ }
+
+ list<string>::iterator i = lines.begin ();
+
+ /* BEGIN */
+ while (i != lines.end() && *i != begin_certificate) {
+ ++i;
+ }
+
+ if (i == lines.end()) {
+ throw MiscError ("missing BEGIN line in certificate");
+ }
+
+ /* Skip over the BEGIN line */
+ ++i;
+
+ /* The base64 data */
+ bool got_end = false;
+ string base64 = "";
+ while (i != lines.end()) {
+ if (*i == end_certificate) {
+ got_end = true;
+ break;
+ }
+ base64 += *i;
+ ++i;
+ }
+
+ if (!got_end) {
+ throw MiscError ("missing END line in certificate");
+ }
+
+ /* Skip over the END line */
+ ++i;
+
+ /* Make up the fixed version */
+
+ string fixed = begin_certificate + "\n";
+ while (!base64.empty ()) {
+ size_t const t = min (size_t(64), base64.length());
+ fixed += base64.substr (0, t) + "\n";
+ base64 = base64.substr (t, base64.length() - t);
+ }
+
+ fixed += end_certificate;
+
+ BIO* bio = BIO_new_mem_buf (const_cast<char *> (fixed.c_str ()), -1);