1

I want to convert X509 data to string format in C++. I tried following method but I can only print one line rather than whole data. Can someone please help me out? Any help will be much appreciated. Please find sample code:

  BIO *bio; string issuer;
  bio = BIO_new(BIO_s_mem());
  if(bio == NULL) {
       throw f5util::Exception("BIO_new failed..");
   }

   // my_Issuer is actually X509 *. 

   if(PEM_write_bio_X509(bio, my_Issuer) == 0) {
        BIO_free(bio);
        throw f5util::Exception("PEM_write_bio_x509() failed..");
    }
    issuer.resize(bio->num_write);
    int bio_len = BIO_read(bio, &issuer[0], bio->num_write);
    issuer.resize(bio_len);

     // here I tried to print the string output and I got only first line 
    //     i.e. "---------BEGIN CERTIFICATE-----------"

    BIO_free(bio);

I will also welcome any alternate approach/suggestions.

jww
  • 97,681
  • 90
  • 411
  • 885
mehtame026
  • 125
  • 5
  • 12
  • You need to get the BIO's `BUF_MEM`, and then use `BUF_MEM.data` and `BUF_MEM.length`. Also see [How to get PKCS7_sign result into a char * or std::string](http://stackoverflow.com/a/38079093/608639). Instead of using `i2d_PKCS7_bio` like in the cited question, you use `PEM_write_bio_X509`. – jww Oct 11 '16 at 00:29
  • I've print all line by using your code. Have you check whether `bio->num_write` and `bio_len` are same? and validation of `my_Issuer`? – hyun Oct 11 '16 at 00:42
  • I have never used `bio->num_write`, so I don't know what it does. I believe its a private field and you should not use it. When I am ready for BIO data, I get it through the public interface and `BUF_MEM`. – jww Oct 11 '16 at 01:07
  • Thanks @jww and CodeDreamer: My code worked fine. It has nothing to do with my code. It turned out to be some different issue. Our logging framework somehow stopped printing multiline messages. Thanks for your time and help. Appreciate it! – mehtame026 Oct 11 '16 at 04:44

0 Answers0