将 BIO 保存到 char*(来自 SMIME_write_CMS)
我想将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议尝试使用 SIZE-1,这样可以保证它以 NULL 终止。否则,可能只是超出了缓冲区。
让我知道这是否有帮助。
编辑:
当使用
BIO_new_mem_buf()
时,它是一个只读缓冲区,因此您无法写入它。如果你想写入内存使用: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.
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: