Base64 Help

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

Base64 Help

Adam Jones-2
Below is the code I am using to try and test the base64 encode in openssl. I am using rand to generate a binary and then encoding that to base64. Instead of using a file, I want to use memory to output the base64 encoded buffer. This code compiles and runs, but my output buffer is all 0. Any help would be appreciated. What have I missed?
 
The variable written does show 16 like it should......help!
 
#include <iostream>
#include <memory.h>
#include "evp.h"
#include "rand.h"
#include "bio.h"
 
using namespace std;
 
int main()
{
 BIO *b64;
 unsigned char *pbuffer = new unsigned char [16];
 unsigned char *pOutput = new unsigned char [100];
 int written;
 
 memset(pOutput, '0', 100);
 RAND_bytes(pbuffer, 16);
 b64 = BIO_new(BIO_f_base64());
 written = BIO_write(b64, pbuffer, 16);
 
 cout << written << endl;
 
 BIO_get_mem_ptr(b64, pOutput);
 
 for ( int nLoop = 0; nLoop< 16; nLoop++)
 {
 cout << pOutput[nLoop];
 }
 cout << "\n" << endl;
 
 BIO_free_all(b64);
Reply | Threaded
Open this post in threaded view
|

Re: Base64 Help

Joseph Oreste Bruni-2
"b64" is a filter BIO, it won't hold on to your data. You need to  
append a "memory BIO" to the back end of the filter bio so that your  
output can be accumulated.

There are samples on how to do this in the OpenSSL book as well as a  
rather lengthy discussion on BIO's in general.

Also "BIO_get_mem_ptr()" gives you a pointer to BUF_MEM structure,  
not a char*. Your compiler should have yelled at you for that.


On Oct 13, 2005, at 12:41 PM, Adam Jones wrote:

> Below is the code I am using to try and test the base64 encode in  
> openssl. I am using rand to generate a binary and then encoding  
> that to base64. Instead of using a file, I want to use memory to  
> output the base64 encoded buffer. This code compiles and runs, but  
> my output buffer is all 0. Any help would be appreciated. What have  
> I missed?
>
> The variable written does show 16 like it should......help!
>
> #include <iostream>
> #include <memory.h>
> #include "evp.h"
> #include "rand.h"
> #include "bio.h"
>
> using namespace std;
>
> int main()
> {
>  BIO *b64;
>  unsigned char *pbuffer = new unsigned char [16];
>  unsigned char *pOutput = new unsigned char [100];
>  int written;
>
>  memset(pOutput, '0', 100);
>  RAND_bytes(pbuffer, 16);
>  b64 = BIO_new(BIO_f_base64());
>  written = BIO_write(b64, pbuffer, 16);
>
>  cout << written << endl;
>
>  BIO_get_mem_ptr(b64, pOutput);
>
>  for ( int nLoop = 0; nLoop< 16; nLoop++)
>  {
>  cout << pOutput[nLoop];
>  }
>  cout << "\n" << endl;
>
>  BIO_free_all(b64);


smime.p7s (3K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

RE: Base64 Help

Adam Jones-2
Visual C++ did not complain nor did it error out when it ran, but you are
correct it does take a BUF_MEM structure. I also added another BIO method to
the code. I also read that section in the book you suggested. I also made
the code simple, but it appears that it still does not give me the base64
encoding. Any suggestions...

int main()
{
        BIO *bmem, *b64;
        char message[] = "Hello World \n";
        int written = 0;

        b64 = BIO_new(BIO_f_base64());
        bmem = BIO_new(BIO_s_mem());
        bmem = BIO_push(b64, bmem);
        written = BIO_write(b64, message, strlen(message));
        cout << written << endl;
        BIO_flush(b64);
        BIO_free_all(b64);
}

 

-----Original Message-----
From: [hidden email]
[mailto:[hidden email]] On Behalf Of Joseph Oreste Bruni
Sent: Thursday, October 13, 2005 2:46 PM
To: [hidden email]
Subject: Re: Base64 Help

"b64" is a filter BIO, it won't hold on to your data. You need to append a
"memory BIO" to the back end of the filter bio so that your output can be
accumulated.

There are samples on how to do this in the OpenSSL book as well as a rather
lengthy discussion on BIO's in general.

Also "BIO_get_mem_ptr()" gives you a pointer to BUF_MEM structure, not a
char*. Your compiler should have yelled at you for that.


On Oct 13, 2005, at 12:41 PM, Adam Jones wrote:

> Below is the code I am using to try and test the base64 encode in
> openssl. I am using rand to generate a binary and then encoding that
> to base64. Instead of using a file, I want to use memory to output the
> base64 encoded buffer. This code compiles and runs, but my output
> buffer is all 0. Any help would be appreciated. What have I missed?
>
> The variable written does show 16 like it should......help!
>
> #include <iostream>
> #include <memory.h>
> #include "evp.h"
> #include "rand.h"
> #include "bio.h"
>
> using namespace std;
>
> int main()
> {
>  BIO *b64;
>  unsigned char *pbuffer = new unsigned char [16];  unsigned char
> *pOutput = new unsigned char [100];  int written;
>
>  memset(pOutput, '0', 100);
>  RAND_bytes(pbuffer, 16);
>  b64 = BIO_new(BIO_f_base64());
>  written = BIO_write(b64, pbuffer, 16);
>
>  cout << written << endl;
>
>  BIO_get_mem_ptr(b64, pOutput);
>
>  for ( int nLoop = 0; nLoop< 16; nLoop++)  {  cout << pOutput[nLoop];  
> }  cout << "\n" << endl;
>
>  BIO_free_all(b64);



Confidentiality Notice:

This e-mail and any files transmitted with it are confidential and intended solely for the use of the individual or entity to whom they are addressed.  If you are not the named addressee you should not disseminate, distribute or copy this e-mail. Please notify the sender immediately by e-mail if you have received this e-mail by mistake and delete this e-mail from your system.  If you are not the intended recipient you are notified that disclosing, copying, distributing or taking any action in reliance on the contents of this information is strictly prohibited.
______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
User Support Mailing List                    [hidden email]
Automated List Manager                           [hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: Base64 Help

Thomas J. Hruska
Adam Jones wrote:

> Visual C++ did not complain nor did it error out when it ran, but you are
> correct it does take a BUF_MEM structure. I also added another BIO method to
> the code. I also read that section in the book you suggested. I also made
> the code simple, but it appears that it still does not give me the base64
> encoding. Any suggestions...
>
> int main()
> {
> BIO *bmem, *b64;
> char message[] = "Hello World \n";
> int written = 0;
>
> b64 = BIO_new(BIO_f_base64());
> bmem = BIO_new(BIO_s_mem());
> bmem = BIO_push(b64, bmem);
> written = BIO_write(b64, message, strlen(message));
> cout << written << endl;
> BIO_flush(b64);
> BIO_free_all(b64);
> }

Take a good look at that code...you create the memory BIO, but then
never bother to use it.  Specifically, you probably mean:

b64 = BIO_push(b64, bmem);

--
Thomas Hruska
Shining Light Productions

Home of BMP2AVI, Nuclear Vision, ProtoNova, and Win32 OpenSSL.
http://www.slproweb.com/

Ask me about discounts on any Shining Light Productions product!

______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
User Support Mailing List                    [hidden email]
Automated List Manager                           [hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: Base64 Help

Joseph Oreste Bruni-2
In reply to this post by Adam Jones-2
Using VC++ is your first problem... :)

The call to BIO_push is your problem. You overwrite your reference to  
the b64 BIO with another reference to the mem BIO. So now, b64 points  
to mem.

Do this:

int main()
{
     BIO *bmem, *b64;
     char message[] = "Hello World \n";
     int written = 0;

     b64 = BIO_new(BIO_f_base64());
     bmem = BIO_new_fp(stdout,0);
     b64 = BIO_push(b64, bmem);
     written = BIO_write(b64, message, strlen(message));
     cout << written << endl;
     BIO_flush(b64);
     BIO_free_all(b64);
}

This will output your Base64 to stdout. Then if you need to use a mem  
BIO just change out the call to what you had, but you'll need to get  
pointers to the data to display it (or whatever).




On Oct 13, 2005, at 1:55 PM, Adam Jones wrote:

> Visual C++ did not complain nor did it error out when it ran, but  
> you are
> correct it does take a BUF_MEM structure. I also added another BIO  
> method to
> the code. I also read that section in the book you suggested. I  
> also made
> the code simple, but it appears that it still does not give me the  
> base64
> encoding. Any suggestions...
>
> int main()
> {
>     BIO *bmem, *b64;
>     char message[] = "Hello World \n";
>     int written = 0;
>
>     b64 = BIO_new(BIO_f_base64());
>     bmem = BIO_new(BIO_s_mem());
>     bmem = BIO_push(b64, bmem);
>     written = BIO_write(b64, message, strlen(message));
>     cout << written << endl;
>     BIO_flush(b64);
>     BIO_free_all(b64);
> }
>
>
>
> -----Original Message-----
> From: [hidden email]
> [mailto:[hidden email]] On Behalf Of Joseph Oreste  
> Bruni
> Sent: Thursday, October 13, 2005 2:46 PM
> To: [hidden email]
> Subject: Re: Base64 Help
>
> "b64" is a filter BIO, it won't hold on to your data. You need to  
> append a
> "memory BIO" to the back end of the filter bio so that your output  
> can be
> accumulated.
>
> There are samples on how to do this in the OpenSSL book as well as  
> a rather
> lengthy discussion on BIO's in general.
>
> Also "BIO_get_mem_ptr()" gives you a pointer to BUF_MEM structure,  
> not a
> char*. Your compiler should have yelled at you for that.
>
>
> On Oct 13, 2005, at 12:41 PM, Adam Jones wrote:
>
>
>> Below is the code I am using to try and test the base64 encode in
>> openssl. I am using rand to generate a binary and then encoding that
>> to base64. Instead of using a file, I want to use memory to output  
>> the
>> base64 encoded buffer. This code compiles and runs, but my output
>> buffer is all 0. Any help would be appreciated. What have I missed?
>>
>> The variable written does show 16 like it should......help!
>>
>> #include <iostream>
>> #include <memory.h>
>> #include "evp.h"
>> #include "rand.h"
>> #include "bio.h"
>>
>> using namespace std;
>>
>> int main()
>> {
>>  BIO *b64;
>>  unsigned char *pbuffer = new unsigned char [16];  unsigned char
>> *pOutput = new unsigned char [100];  int written;
>>
>>  memset(pOutput, '0', 100);
>>  RAND_bytes(pbuffer, 16);
>>  b64 = BIO_new(BIO_f_base64());
>>  written = BIO_write(b64, pbuffer, 16);
>>
>>  cout << written << endl;
>>
>>  BIO_get_mem_ptr(b64, pOutput);
>>
>>  for ( int nLoop = 0; nLoop< 16; nLoop++)  {  cout << pOutput[nLoop];
>> }  cout << "\n" << endl;
>>
>>  BIO_free_all(b64);
>>
>
>
>
> Confidentiality Notice:
>
> This e-mail and any files transmitted with it are confidential and  
> intended solely for the use of the individual or entity to whom  
> they are addressed.  If you are not the named addressee you should  
> not disseminate, distribute or copy this e-mail. Please notify the  
> sender immediately by e-mail if you have received this e-mail by  
> mistake and delete this e-mail from your system.  If you are not  
> the intended recipient you are notified that disclosing, copying,  
> distributing or taking any action in reliance on the contents of  
> this information is strictly prohibited.
> ______________________________________________________________________
> OpenSSL Project                                 http://www.openssl.org
> User Support Mailing List                    [hidden email]
> Automated List Manager                           [hidden email]
>


smime.p7s (3K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

RE: Base64 Help

Adam Jones-2
int main (void)
{
 BIO *bmem, *b64;
 BUF_MEM *bptr;
 char message[] = "Hello World \n";
 int written = 0;
 
 b64 = BIO_new(BIO_f_base64());
 bmem = BIO_new(BIO_s_mem());
 b64 = BIO_push(b64, bmem);
 written = BIO_write(b64, message, strlen(message));
 cout << written << endl;
 BIO_get_mem_ptr(b64, &bptr);
 BIO_flush(b64);
 BIO_free_all(b64);
}
 
This should be straight forward, but it is not. It appears that base64
encoding to something other than stdout or a file does not work. I am using
vc++ 6.0, on windows platform. I can't get it to work with stdout either. I
get no compile or runtime errors. The &bptr gets a valid address, but the
address has nothing in it. Also the system tells me that 13 characters were
written, which is the correct number.Does anyone see why this is not working
correctly? I have even change the line BIO_get_mem_ptr(b64, &bptr); to
BIO_get_mem_ptr(bmem, &bptr); which does nothing.

Thanks in Advance.......

-----Original Message-----
From: [hidden email]
[mailto:[hidden email]] On Behalf Of Joseph Oreste Bruni
Sent: Thursday, October 13, 2005 4:36 PM
To: [hidden email]
Subject: Re: Base64 Help

Using VC++ is your first problem... :)

The call to BIO_push is your problem. You overwrite your reference to the
b64 BIO with another reference to the mem BIO. So now, b64 points to mem.

Do this:

int main()
{
     BIO *bmem, *b64;
     char message[] = "Hello World \n";
     int written = 0;

     b64 = BIO_new(BIO_f_base64());
     bmem = BIO_new_fp(stdout,0);
     b64 = BIO_push(b64, bmem);
     written = BIO_write(b64, message, strlen(message));
     cout << written << endl;
     BIO_flush(b64);
     BIO_free_all(b64);
}

This will output your Base64 to stdout. Then if you need to use a mem BIO
just change out the call to what you had, but you'll need to get pointers to
the data to display it (or whatever).




On Oct 13, 2005, at 1:55 PM, Adam Jones wrote:

> Visual C++ did not complain nor did it error out when it ran, but you
> are correct it does take a BUF_MEM structure. I also added another BIO
> method to the code. I also read that section in the book you
> suggested. I also made the code simple, but it appears that it still
> does not give me the
> base64
> encoding. Any suggestions...
>
> int main()
> {
>     BIO *bmem, *b64;
>     char message[] = "Hello World \n";
>     int written = 0;
>
>     b64 = BIO_new(BIO_f_base64());
>     bmem = BIO_new(BIO_s_mem());
>     bmem = BIO_push(b64, bmem);
>     written = BIO_write(b64, message, strlen(message));
>     cout << written << endl;
>     BIO_flush(b64);
>     BIO_free_all(b64);
> }
>
>
>
> -----Original Message-----
> From: [hidden email]
> [mailto:[hidden email]] On Behalf Of Joseph Oreste
> Bruni
> Sent: Thursday, October 13, 2005 2:46 PM
> To: [hidden email]
> Subject: Re: Base64 Help
>
> "b64" is a filter BIO, it won't hold on to your data. You need to
> append a "memory BIO" to the back end of the filter bio so that your
> output can be accumulated.
>
> There are samples on how to do this in the OpenSSL book as well as a
> rather lengthy discussion on BIO's in general.
>
> Also "BIO_get_mem_ptr()" gives you a pointer to BUF_MEM structure, not
> a char*. Your compiler should have yelled at you for that.
>
>
> On Oct 13, 2005, at 12:41 PM, Adam Jones wrote:
>
>
>> Below is the code I am using to try and test the base64 encode in
>> openssl. I am using rand to generate a binary and then encoding that
>> to base64. Instead of using a file, I want to use memory to output
>> the
>> base64 encoded buffer. This code compiles and runs, but my output
>> buffer is all 0. Any help would be appreciated. What have I missed?
>>
>> The variable written does show 16 like it should......help!
>>
>> #include <iostream>
>> #include <memory.h>
>> #include "evp.h"
>> #include "rand.h"
>> #include "bio.h"
>>
>> using namespace std;
>>
>> int main()
>> {
>>  BIO *b64;
>>  unsigned char *pbuffer = new unsigned char [16];  unsigned char
>> *pOutput = new unsigned char [100];  int written;
>>
>>  memset(pOutput, '0', 100);
>>  RAND_bytes(pbuffer, 16);
>>  b64 = BIO_new(BIO_f_base64());
>>  written = BIO_write(b64, pbuffer, 16);
>>
>>  cout << written << endl;
>>
>>  BIO_get_mem_ptr(b64, pOutput);
>>
>>  for ( int nLoop = 0; nLoop< 16; nLoop++)  {  cout << pOutput[nLoop];
>> }  cout << "\n" << endl;
>>
>>  BIO_free_all(b64);
>>
>
>
>
> Confidentiality Notice:
>
> This e-mail and any files transmitted with it are confidential and
> intended solely for the use of the individual or entity to whom they
> are addressed.  If you are not the named addressee you should not
> disseminate, distribute or copy this e-mail. Please notify the sender
> immediately by e-mail if you have received this e-mail by mistake and
> delete this e-mail from your system.  If you are not the intended
> recipient you are notified that disclosing, copying, distributing or
> taking any action in reliance on the contents of this information is
> strictly prohibited.
> ______________________________________________________________________
> OpenSSL Project                                 http://www.openssl.org
> User Support Mailing List                    [hidden email]
> Automated List Manager                           [hidden email]
>



Confidentiality Notice:

This e-mail and any files transmitted with it are confidential and intended solely for the use of the individual or entity to whom they are addressed.  If you are not the named addressee you should not disseminate, distribute or copy this e-mail. Please notify the sender immediately by e-mail if you have received this e-mail by mistake and delete this e-mail from your system.  If you are not the intended recipient you are notified that disclosing, copying, distributing or taking any action in reliance on the contents of this information is strictly prohibited.
______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
User Support Mailing List                    [hidden email]
Automated List Manager                           [hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: Base64 Help

Dr. Stephen Henson
On Mon, Oct 17, 2005, Adam Jones wrote:

> int main (void)
> {
>  BIO *bmem, *b64;
>  BUF_MEM *bptr;
>  char message[] = "Hello World \n";
>  int written = 0;
>  
>  b64 = BIO_new(BIO_f_base64());
>  bmem = BIO_new(BIO_s_mem());
>  b64 = BIO_push(b64, bmem);
>  written = BIO_write(b64, message, strlen(message));
>  cout << written << endl;
>  BIO_get_mem_ptr(b64, &bptr);
>  BIO_flush(b64);
>  BIO_free_all(b64);
> }
>  
> This should be straight forward, but it is not. It appears that base64
> encoding to something other than stdout or a file does not work. I am using
> vc++ 6.0, on windows platform. I can't get it to work with stdout either. I
> get no compile or runtime errors. The &bptr gets a valid address, but the
> address has nothing in it. Also the system tells me that 13 characters were
> written, which is the correct number.Does anyone see why this is not working
> correctly? I have even change the line BIO_get_mem_ptr(b64, &bptr); to
> BIO_get_mem_ptr(bmem, &bptr); which does nothing.
>
> Thanks in Advance.......
>

Have you tried looking at the contents of bptr before you free the BIOs? The
BIO_free_all() calls will free up the memory buffer.

Steve.
--
Dr Stephen N. Henson. Email, S/MIME and PGP keys: see homepage
OpenSSL project core developer and freelance consultant.
Funding needed! Details on homepage.
Homepage: http://www.drh-consultancy.demon.co.uk
______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
User Support Mailing List                    [hidden email]
Automated List Manager                           [hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: Base64 Help

Rich Salz
In reply to this post by Adam Jones-2
         //  Set up a base64 encoding BIO that writes to a memory BIO.
         BIO* b64 = BIO_new(BIO_f_base64());
         BIO* out = BIO_new(BIO_s_mem());
         BIO_set_flags(out, BIO_CLOSE);  // probably redundant
         b64 = BIO_push(b64, out);

         //  Send the data.
         // e.g., i2d_X509_bio(b64, mX509);
        BIO_write(b64, message, strlen(message));

         //  Collect the encoded data.
         BIO_flush(b64);
         char* temp;
         int count = BIO_get_mem_data(out, &temp);

        // ... use the data, and then:
         BIO_free_all(b64);
        //  temp is now invalid!

--
Rich Salz, Chief Security Architect
DataPower Technology                           http://www.datapower.com
XS40 XML Security Gateway   http://www.datapower.com/products/xs40.html
______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
User Support Mailing List                    [hidden email]
Automated List Manager                           [hidden email]
Reply | Threaded
Open this post in threaded view
|

RE: Base64 Help

Adam Jones-2
Thank you! It finally works.......It appears you have to flush the BIO
before you get a pointer to it (as shown in your code below. Thanks!

-----Original Message-----
From: [hidden email]
[mailto:[hidden email]] On Behalf Of Rich Salz
Sent: Monday, October 17, 2005 12:41 PM
To: Adam Jones
Cc: [hidden email]
Subject: Re: Base64 Help

         //  Set up a base64 encoding BIO that writes to a memory BIO.
         BIO* b64 = BIO_new(BIO_f_base64());
         BIO* out = BIO_new(BIO_s_mem());
         BIO_set_flags(out, BIO_CLOSE);  // probably redundant
         b64 = BIO_push(b64, out);

         //  Send the data.
         // e.g., i2d_X509_bio(b64, mX509);
        BIO_write(b64, message, strlen(message));

         //  Collect the encoded data.
         BIO_flush(b64);
         char* temp;
         int count = BIO_get_mem_data(out, &temp);

        // ... use the data, and then:
         BIO_free_all(b64);
        //  temp is now invalid!

--
Rich Salz, Chief Security Architect
DataPower Technology                           http://www.datapower.com
XS40 XML Security Gateway   http://www.datapower.com/products/xs40.html
______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
User Support Mailing List                    [hidden email]
Automated List Manager                           [hidden email]




Confidentiality Notice:

This e-mail and any files transmitted with it are confidential and intended solely for the use of the individual or entity to whom they are addressed.  If you are not the named addressee you should not disseminate, distribute or copy this e-mail. Please notify the sender immediately by e-mail if you have received this e-mail by mistake and delete this e-mail from your system.  If you are not the intended recipient you are notified that disclosing, copying, distributing or taking any action in reliance on the contents of this information is strictly prohibited.
______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
User Support Mailing List                    [hidden email]
Automated List Manager                           [hidden email]
Reply | Threaded
Open this post in threaded view
|

How do I restart the same SSL session after calling SSL_shutdown()?

Ning Ke

Hi!

II want to restart the same SSL session after calling SSL_shutdown(). I tried the following but it got stuck at SSL_connect(). I wonder if I can do either of the following on the client:

1) Call SSL_shutdown() but then reuse the same SSL object for a later SSL_connect().

or

2) Call SSL_shutdown() then SSL_free() then SSL_new() but reuse the old socket that I didn't close.

I tried the following but it doesn't work:

sock = new_socket_connect();   /* create and connect socket */
bio = BIO_new_socket(sock, BIO_NOCLOSE);
ssl = SSL_new(my_ssl_ctx);
SSL_set_bio(ssl, bio, bio);
SSL_connect(ssl);
/* SSL_write() */
/* shut down */
SSL_shutdown(ssl);
if(SSL_shutdown(ssl) != 1) {error_print("bad shutdown\n");}

/* Try to restart */
/* code for case 1) */
{
        SSL_clear(ssl);
        SSL_connect(ssl);      /* This call hangs ?????? */
        /* SSL_do_handshake(ssl) succeeds, but the following SSL_write(ssl) fails with error "SSL object shutdown" ??? */
}

/* code for case 2) */
{
        sess = SSL_get1_session(ssl);
        SSL_free(ssl);
        ssl = SSL_new(my_ssl_ctx);
        bio = BIO_new_socket(sock, BIO_NOCLOSE);
        SSL_set_bio(ssl, bio, bio);
        SSL_set_session(sess);
        SSL_connect(ssl);             /* This now hangs ???? */
}

Any suggestions?

Thank you in advance,
Ning