<div dir="ltr">Hello everyone, I've been reading smime.c and trying to work my way up from a command that does work.<div>However, I've reached this stage, and I get an error code I don-t know how to diagnose.</div><div>The source is this(BEWARE: very little error handling, this is just a first informed attempt at the problem):</div><div><pre style="margin-top:0px;margin-bottom:0px"><span style="color:rgb(128,0,128)">    X509</span><span style="color:rgb(192,192,192)"> </span>*<span style="color:rgb(192,192,192)"> </span>certificado<span style="color:rgb(192,192,192)"> </span>=<span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,0,128)">NULL</span>;</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:rgb(192,192,192)">    </span><span style="color:rgb(128,0,128)">FILE</span><span style="color:rgb(192,192,192)"> </span>*<span style="color:rgb(192,192,192)"> </span>archivoCertificado<span style="color:rgb(192,192,192)"> </span>=<span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,0,128)">NULL</span>;</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:rgb(192,192,192)">    </span>archivoCertificado<span style="color:rgb(192,192,192)"> </span>=<span style="color:rgb(192,192,192)"> </span>fopen(<span style="color:rgb(0,128,0)">"cert.crt"</span>,<span style="color:rgb(0,128,0)">"rb"</span>);</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:rgb(192,192,192)">    </span><span style="color:rgb(128,128,0)">if</span><span style="color:rgb(192,192,192)"> </span>(!archivoCertificado)<span style="color:rgb(192,192,192)"> </span>{</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:rgb(192,192,192)">        </span><span style="color:rgb(0,0,128)">qDebug</span>()<span style="color:rgb(192,192,192)"> </span><<<span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,128,0)">"Fallo</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,128,0)">abrir</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,128,0)">el</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,128,0)">archivo</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,128,0)">del</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,128,0)">certificado"</span>;</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:rgb(192,192,192)">        </span><span style="color:rgb(128,128,0)">return</span>;</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:rgb(192,192,192)">    </span>}</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:rgb(192,192,192)">    </span>PEM_read_X509(archivoCertificado,&certificado,<span style="color:rgb(0,0,128)">NULL</span>,<span style="color:rgb(0,0,128)">NULL</span>);</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:rgb(192,192,192)">    </span><span style="color:rgb(128,128,0)">if</span><span style="color:rgb(192,192,192)"> </span>(!certificado)<span style="color:rgb(192,192,192)"> </span>{</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:rgb(192,192,192)">        </span><span style="color:rgb(0,0,128)">qDebug</span>()<span style="color:rgb(192,192,192)"> </span><<<span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,128,0)">"Fallo</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,128,0)">al</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,128,0)">generar</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,128,0)">la</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,128,0)">estructura</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,128,0)">X509"</span>;</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:rgb(192,192,192)">        </span><span style="color:rgb(128,128,0)">return</span>;</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:rgb(192,192,192)">    </span>}</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:rgb(192,192,192)">    </span><span style="color:rgb(128,0,128)">FILE</span>*<span style="color:rgb(192,192,192)"> </span>archivoLlave<span style="color:rgb(192,192,192)"> </span>=<span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,0,128)">NULL</span>;</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:rgb(192,192,192)">    </span>archivoLlave<span style="color:rgb(192,192,192)"> </span>=<span style="color:rgb(192,192,192)"> </span>fopen(<span style="color:rgb(0,128,0)">"key.key"</span>,<span style="color:rgb(0,128,0)">"rb"</span>);</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:rgb(192,192,192)">    </span><span style="color:rgb(128,0,128)">EVP_PKEY</span><span style="color:rgb(192,192,192)"> </span>*<span style="color:rgb(192,192,192)"> </span>llave;</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:rgb(192,192,192)">    </span>PEM_read_PrivateKey(archivoLlave,&llave,<span style="color:rgb(0,0,128)">NULL</span>,<span style="color:rgb(0,0,128)">NULL</span>);</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:rgb(192,192,192)">    </span><span style="color:rgb(128,128,0)">if</span><span style="color:rgb(192,192,192)"> </span>(!llave)<span style="color:rgb(192,192,192)"> </span>{</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:rgb(192,192,192)">        </span><span style="color:rgb(0,0,128)">qDebug</span>()<span style="color:rgb(192,192,192)"> </span><<<span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,128,0)">"Fallo</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,128,0)">la</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,128,0)">lectura</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,128,0)">de</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,128,0)">la</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,128,0)">llave"</span>;</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:rgb(192,192,192)">        </span><span style="color:rgb(128,128,0)">return</span>;</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:rgb(192,192,192)">    </span>}</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:rgb(192,192,192)">    </span><span style="color:rgb(128,0,128)">BIO</span><span style="color:rgb(192,192,192)"> </span>*<span style="color:rgb(192,192,192)"> </span>datos<span style="color:rgb(192,192,192)"> </span>=<span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,0,128)">NULL</span>;</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:rgb(192,192,192)">    </span><span style="color:rgb(128,0,128)">FILE</span><span style="color:rgb(192,192,192)"> </span>*<span style="color:rgb(192,192,192)"> </span>fDatos<span style="color:rgb(192,192,192)"> </span>=<span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,0,128)">NULL</span>;</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:rgb(192,192,192)">    </span>fDatos<span style="color:rgb(192,192,192)"> </span>=<span style="color:rgb(192,192,192)"> </span>fopen(<span style="color:rgb(0,128,0)">"Prueba.xml"</span>,<span style="color:rgb(0,128,0)">"rb"</span>);</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:rgb(192,192,192)">    </span><span style="color:rgb(128,128,0)">if</span><span style="color:rgb(192,192,192)"> </span>(!fDatos)<span style="color:rgb(192,192,192)"> </span>{</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:rgb(192,192,192)">        </span><span style="color:rgb(0,0,128)">qDebug</span>()<span style="color:rgb(192,192,192)"> </span><<<span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,128,0)">"Fallo</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,128,0)">la</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,128,0)">apertura</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,128,0)">del</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,128,0)">archivo</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,128,0)">de</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,128,0)">prueba."</span>;</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:rgb(192,192,192)">        </span><span style="color:rgb(128,128,0)">return</span>;</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:rgb(192,192,192)">    </span>}</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:rgb(192,192,192)">    </span>datos<span style="color:rgb(192,192,192)"> </span>=<span style="color:rgb(192,192,192)"> </span>BIO_new_fp(fDatos,<span style="color:rgb(0,0,128)">NULL</span>);</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:rgb(192,192,192)">    </span><span style="color:rgb(128,128,0)">if</span><span style="color:rgb(192,192,192)"> </span>(!datos)<span style="color:rgb(192,192,192)"> </span>{</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:rgb(192,192,192)">        </span><span style="color:rgb(0,0,128)">qDebug</span>()<span style="color:rgb(192,192,192)"> </span><<<span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,128,0)">"Error</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,128,0)">al</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,128,0)">leer</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,128,0)">el</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,128,0)">archivo</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,128,0)">de</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,128,0)">prueba.xml"</span>;</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:rgb(192,192,192)">        </span><span style="color:rgb(128,128,0)">return</span>;</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:rgb(192,192,192)">    </span>}</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:rgb(192,192,192)">    </span><span style="color:rgb(128,0,128)">PKCS7</span><span style="color:rgb(192,192,192)"> </span>*estructura<span style="color:rgb(192,192,192)"> </span>=<span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,0,128)">NULL</span>;</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:rgb(192,192,192)">    </span><span style="color:rgb(128,128,0)">if</span><span style="color:rgb(192,192,192)"> </span>(!PKCS7_sign_add_signer(estructura,certificado,llave,<span style="color:rgb(0,0,128)">NULL</span>,<span style="color:rgb(0,0,128)">0</span>))<span style="color:rgb(192,192,192)"> </span>{</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:rgb(192,192,192)">        </span><span style="color:rgb(0,0,128)">qDebug</span>()<span style="color:rgb(192,192,192)"> </span><<<span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,128,0)">"PKCS7_sign_add_signer</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,128,0)">fallo:"</span><span style="color:rgb(192,192,192)"> </span><<<span style="color:rgb(192,192,192)"> </span>ERR_get_error();</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:rgb(192,192,192)">        </span><span style="color:rgb(128,128,0)">return</span>;</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:rgb(192,192,192)">    </span>}</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:rgb(192,192,192)">    </span>estructura<span style="color:rgb(192,192,192)"> </span>=<span style="color:rgb(192,192,192)"> </span>PKCS7_sign(certificado,llave,<span style="color:rgb(0,0,128)">NULL</span>,datos,<span style="color:rgb(0,0,128)">PKCS7_TEXT</span>);</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:rgb(192,192,192)">    </span><span style="color:rgb(128,128,0)">if</span><span style="color:rgb(192,192,192)"> </span>(!estructura)<span style="color:rgb(192,192,192)"> </span>{</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:rgb(192,192,192)">        </span><span style="color:rgb(0,0,128)">qDebug</span>()<span style="color:rgb(192,192,192)"> </span><<<span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,128,0)">"Fallo</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,128,0)">la</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,128,0)">creacion</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,128,0)">de</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,128,0)">la</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,128,0)">estructura."</span>;</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:rgb(192,192,192)">        </span><span style="color:rgb(128,128,0)">return</span>;</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:rgb(192,192,192)">    </span>}</pre>
<pre style="margin-top:0px;margin-bottom:0px"><br></pre></div><div>Failure comes at PKCS7_sign_add_signer..</div><div>Sorry for the main language used in the code, let me know if its of best practices to keep it to english or if it wouldn't be a real issue for the time being.</div><div><br></div><div>Kind regards,</div><div>Anibal.-</div></div>