Am I correctly base64 encoding a buffer with OpenSSL?

I have this buffer:

unsigned char *buffer; int buffer_length; 

This is how I now convert it to a base64 encoded buffer:

 BIO *mem = BIO_new(BIO_s_mem()); BIO *b64 = BIO_new(BIO_f_base64()); mem = BIO_push(b64, mem); int write_length = BIO_write(mem, buffer, buffer_length); if (write_length != buffer_length) //* return -1; int flush_result = BIO_flush(mem); if (flush_result != 1) return -1; unsigned char *result; //** int result_length = BIO_get_mem_data(mem, &result); //use the base64-encoded result to do whatever I need to do BIO_free_all(mem); return 0; 

While this is working. However, is this good and reliable code? I have special questions about code snippets marked with asterisks above:

  • ( //* ) Is it right to assume that BIO_write() always writes the entire base64 encoded string at once or do I need to create a loop here?
  • ( //** ) Is it correct to have an unsigned char* type, or should I use char * instead?
+4
source share
1 answer

// * You must put your BIO_write() in a loop. The man page says quite clearly (that he is trying to write the requested number of bytes), and this is consistent with other writing material in C.

// ** You should use char * since the manual page is specified, although I'm not sure about that.

+1
source

Source: https://habr.com/ru/post/1387072/


All Articles