Chr*_*ris 3 iphone openssl smime ios
我想将一个 BIO 保存(管道/复制)到一个字符数组中。当我知道它的大小时,它可以工作,否则就不行。
例如,我可以使用这个将我的 char* 的内容存储到一个 BIO 中
const unsigned char* data = ...
myBio = BIO_new_mem_buf((void*)data, strlen(data));
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试使用 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!");
Run Code Online (Sandbox Code Playgroud)
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;
Run Code Online (Sandbox Code Playgroud)
大家有什么建议吗?
我建议尝试使用 SIZE-1,这样你就可以保证它是 NULL 终止的。否则,它可能只是过度运行缓冲区。
out = BIO_new_mem_buf((void*)temp, SIZE-1);
Run Code Online (Sandbox Code Playgroud)
如果这有帮助,请告诉我。
编辑:
使用时BIO_new_mem_buf()它是只读缓冲区,因此您无法写入它。如果要写入内存使用:
BIO *bio = BIO_new(BIO_s_mem());
Run Code Online (Sandbox Code Playgroud)