<html><head></head><body><div style="font-family: Verdana;font-size: 12.0px;"><div>Hello,</div>

<div> </div>

<div>I'am writing a mailing solution with S/MIME support - especially with signing the messages.</div>

<div>Usually I would use the following code sequence to geht the MIME encoded message body.</div>

<div>(I'am working with Delphi and OpenSSL 1.1.1q)</div>

<div> </div>

<div>//  ---- First try --</div>

<div>
<div>PBIO InputBioFile   := BIO_new_file('c:\mySourceMail.txt','rb');</div>

<div>PBIO OutputBioFile := BIO_new_file('c:\mySingedMail.txt','wb');</div>
</div>

<div>flags := 0;</div>

<div>
<div>p7 := PKCS7_sign( FCertificate, FPrivateKey, nil, InputBioFile, flags );</div>

<div>
<div>SMIME_write_PKCS7( OutputBioFile, p7, InputBioFile, flags );</div>

<div>
<div>PKCS7_free(p7);</div>
</div>

<div>
<div>BIO_Free(InputBioFile);</div>

<div>
<div>BIO_Free(OutputBioFile );</div>

<div>
<div>//  ----</div>
</div>
</div>
</div>

<div>output is in the case the ready to use mimeblock, but in my case I need</div>

<div>the binary version of the "p7m" data, because codebase for mailing will</div>

<div>encode this its own base64 encoder.</div>

<div> </div>

<div>// so I tried this</div>

<div>// -- Second try -- getting binary p7m file --</div>

<div>//</div>

<div>
<div>PBIO InputBioFile   := BIO_new_file('c:\mySourceMail.txt','rb');</div>

<div>PBIO OutputBioFile := BIO_new_file('c:\smime.p7m','wb');</div>
</div>

<div>
<div>flags := PKCS7_BINARY or PKCS7_STREAM;</div>

<div>
<div>p7 := PKCS7_sign( FCertificate, FPrivateKey, nil, InputBioFile, flags );</div>

<div>
<div>i2d_PKCS7_bio_stream( OutputBioFile, p7, InputBioFile, flags )</div>
</div>
</div>

<div>
<div>PKCS7_free(p7);</div>
</div>

<div>
<div>BIO_Free(InputBioFile);</div>

<div>
<div>BIO_Free(OutputBioFile );</div>

<div>// ---</div>

<div> </div>
</div>
</div>
</div>

<div>To check myself I decoded the Mimedata from the first function to the smime.p7m file,</div>

<div>and compared this with the output from the second try --- I KNOW that there is a lot</div>

<div>of binarydata which I can't compare - but the also contained "mySourceMail.txt" should</div>

<div>be the same in both files I assume. But this isn't the case the file from the second try</div>

<div>ist slightly larger than the first and contains some random bytes at every 4k of data.</div>

<div>Verfication of the sigature fails for that.</div>

<div> </div>

<div>I think the problem may arise from useing the PKCS7_STREAM flag, but without only</div>

<div>the signature is written to the output - and not the data?</div>

<div> </div>

<div>In i2d_ASN1_bio_stream() the flag PKCS7_STREAM leads to SMIME_crlf_copy(..) that</div>

<div>will try to copy the source directly to the dest (PKCS7_BINARY) which just reads 1k and</div>

<div>writes 1k ...  the output is buffered through BIO_f_buffer() which uses a 4k buffer ...</div>

<div>may be the buffering is buggy? so that the data gets corrupted every 4k?</div>

<div> </div>

<div> </div>

<div>Any idea or hints are welcome ...</div>

<div> </div>

<div>André Weber</div>

<div> </div>
</div>
</div></div></body></html>