OGeek|极客世界-中国程序员成长平台

标题: ios - 将 BIO 保存到 char* 中(来自 SMIME_write_CMS) [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 11:22
标题: ios - 将 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;

大家有什么建议吗?



Best Answer-推荐答案


我建议尝试使用 SIZE-1,这样可以保证它以 NULL 终止。否则,它可能刚刚超过运行缓冲区。

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

如果有帮助,请告诉我。

编辑:

当使用 BIO_new_mem_buf() 时,它是一个只读缓冲区,所以你不能写入它。如果你想写入内存使用:

BIO *bio = BIO_new(BIO_s_mem());

关于ios - 将 BIO 保存到 char* 中(来自 SMIME_write_CMS),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9185089/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4