将 BIO 保存到 char*(来自 SMIME_write_CMS)

发布于 2025-01-04 04:00:58 字数 980 浏览 1 评论 0原文

我想将 BIO 保存(管道/复制)到字符数组中。 当我知道尺寸时它可以工作,但否则就不行。

例如,我可以使用这个将 char* 的内容存储到 BIO 中,

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

但是当我尝试使用 SMIME_write_CMS 时,它需要 BIO (我之前创建的)作为输出,但它不起作用。

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!");

OpenSSL 参考使用 BIO 的直接文件输出。 这可行,但我不能在 Objective-c 中使用 BIO_new_file()... :-/

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;

你们有什么建议吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北风几吹夏 2025-01-11 04:00:58

我建议尝试使用 SIZE-1,这样可以保证它以 NULL 终止。否则,可能只是超出了缓冲区。

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

让我知道这是否有帮助。

编辑:

当使用 BIO_new_mem_buf() 时,它是一个只读缓冲区,因此您无法写入它。如果你想写入内存使用:

BIO *bio = BIO_new(BIO_s_mem());

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());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文