Pregunta

I want to save (pipe/copy) a BIO into a char array. When I know the size it works, but otherwise not.

For example, I can store the content of my char* into a BIO using this

const unsigned char* data = ...    
myBio = BIO_new_mem_buf((void*)data, strlen(data));

But when I try to use SMIME_write_CMS which takes a BIO (what I've created before) for the output it doesn't work.

const int SIZE = 50000;
unsigned char *temp = malloc(SIZE);
memset(temp, 0, SIZE);

out = BIO_new_mem_buf((void*)temp, SIZE);
if (!out) {
    NSLog(@"Couldn't create new file!");
    assert(false);
}


int finished = SMIME_write_CMS(out, cms, in, flags);
if (!finished) {
    NSLog(@"SMIME write CMS didn't succeed!");
    assert(false);
}

printf("cms encrypted: %s\n", temp);

NSLog(@"All succeeded!");

The OpenSSL reference uses a direct file output with the BIO. This works but I can't use BIO_new_file() in objective-c... :-/

out = BIO_new_file("smencr.txt", "w");
if (!out)
    goto err;

/* Write out S/MIME message */
if (!SMIME_write_CMS(out, cms, in, flags))
    goto err;

Do you guys have any suggestion?

¿Fue útil?

Solución

I would suggest trying to use SIZE-1, that way you are guaranteed that it is NULL terminated. Otherwise, it is possible that it is just over running the buffer.

out = BIO_new_mem_buf((void*)temp, SIZE-1);

Let me know if that helps.

Edit:

When using BIO_new_mem_buf() it is a read only buffer, so you cannot write to it. If you want to write to memory use:

BIO *bio = BIO_new(BIO_s_mem());
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top