<div dir="ltr"><div>Hello,</div><div><br></div><div>I've been struggling with using gpgme_set_passphrase_cb() in an automated environment (<span style="color:rgb(0,0,128)">#include</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,128,0)"><gpgme.h> C gpgme in a </span>C++ program) - it doesn't seem to have any effect, I still get the system prompts for passphrases. The files encrypt and decrypt as one would expect, but due to the automated end-use case, the user prompts are not acceptable.</div>
<div><br></div><div>I've tried adding: <span style="color:rgb(0,128,0)">gpgme_set_pinentry_mode(</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,128,0)">ctx,</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,128,0)">GPGME_PINENTRY_MODE_LOOPBACK</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,128,0)">); </span>to the code, and then I don't get the prompts anymore, but the encrypt function returns without an error code, and the output (cipher) file is zero length.</div><div><br></div><div>This is my encrypt function meat:</div><div><br></div>{{{<br><div>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:rgb(0,0,128)">LOG_FAIL_IF_GPGERR</span>(<span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,103,124)">initGpgme</span>()<span style="color:rgb(192,192,192)"> </span>)</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:rgb(0,0,128)">LOG_FAIL_IF_GPGERR</span>(<span style="color:rgb(192,192,192)"> </span>gpgme_new(<span style="color:rgb(192,192,192)"> </span>&<span style="color:rgb(9,46,100)">ctx</span><span style="color:rgb(192,192,192)"> </span>)<span style="color:rgb(192,192,192)"> </span>)</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:rgb(0,128,0)">//</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,128,0)">gpgme_set_pinentry_mode(</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,128,0)">ctx,</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,128,0)">GPGME_PINENTRY_MODE_LOOPBACK</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,128,0)">);</span></pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:rgb(0,103,124)">gpgme_set_passphrase_cb</span>(<span style="color:rgb(192,192,192)"> </span><span style="color:rgb(9,46,100)">ctx</span>,<span style="color:rgb(192,192,192)"> </span>passphraseCallback,<span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,0,128)">NULL</span><span style="color:rgb(192,192,192)"> </span>);</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:rgb(0,0,128)">LOG_FAIL_IF_GPGERR</span>(<span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,103,124)">gpgme_data_new_from_file</span>(<span style="color:rgb(192,192,192)"> </span>&<span style="color:rgb(9,46,100)">plain</span>,<span style="color:rgb(192,192,192)"> </span><span style="color:rgb(9,46,100)">fi</span>.<span style="color:rgb(0,103,124)">filePath</span>().<span style="color:rgb(0,103,124)">toLatin1</span>().<span style="color:rgb(0,103,124)">data</span>(),<span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,0,128)">1</span><span style="color:rgb(192,192,192)"> </span>)<span style="color:rgb(192,192,192)"> </span>)</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:rgb(0,0,128)">LOG_FAIL_IF_GPGERR</span>(<span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,103,124)">gpgme_data_set_encoding</span><span style="color:rgb(192,192,192)"> </span>(<span style="color:rgb(192,192,192)"> </span><span style="color:rgb(9,46,100)">plain</span>,<span style="color:rgb(192,192,192)"> </span><span style="color:rgb(128,0,128)">GPGME_DATA_ENCODING_BINARY</span><span style="color:rgb(192,192,192)"> </span>)<span style="color:rgb(192,192,192)"> </span>)</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:rgb(0,0,128)">LOG_FAIL_IF_GPGERR</span>(<span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,103,124)">gpgme_data_new_from_fd</span><span style="color:rgb(192,192,192)"> </span>(<span style="color:rgb(192,192,192)"> </span>&<span style="color:rgb(9,46,100)">cipher</span>,<span style="color:rgb(192,192,192)"> </span><span style="color:rgb(9,46,100)">outFile</span>.<span style="color:rgb(0,103,124)">handle</span>()<span style="color:rgb(192,192,192)"> </span>)<span style="color:rgb(192,192,192)"> </span>)</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:rgb(0,0,128)">LOG_FAIL_IF_GPGERR</span>(<span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,103,124)">gpgme_data_set_encoding</span><span style="color:rgb(192,192,192)"> </span>(<span style="color:rgb(192,192,192)"> </span><span style="color:rgb(9,46,100)">cipher</span>,<span style="color:rgb(192,192,192)"> </span><span style="color:rgb(128,0,128)">GPGME_DATA_ENCODING_BINARY</span><span style="color:rgb(192,192,192)"> </span>)<span style="color:rgb(192,192,192)"> </span>)</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:rgb(0,128,0)">//</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,128,0)">recp[0]</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,128,0)">=</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,128,0)">settingsKey;</span></pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:rgb(0,128,0)">//</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,128,0)">recp[1]</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,128,0)">=</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,128,0)">NULL;</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,128,0)">//</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,128,0)">using</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,128,0)">symmetric</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,128,0)">encryption</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,128,0)">instead</span></pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:rgb(0,0,128)">LOG_FAIL_IF_GPGERR</span>(<span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,103,124)">gpgme_op_encrypt</span>(<span style="color:rgb(192,192,192)"> </span><span style="color:rgb(9,46,100)">ctx</span>,<span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,0,128)">NULL</span>,<span style="color:rgb(192,192,192)"> </span><span style="color:rgb(9,46,100)">flags</span>,<span style="color:rgb(192,192,192)"> </span><span style="color:rgb(9,46,100)">plain</span>,<span style="color:rgb(192,192,192)"> </span><span style="color:rgb(9,46,100)">cipher</span><span style="color:rgb(192,192,192)"> </span>)<span style="color:rgb(192,192,192)"> </span>);</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:rgb(0,103,124)">gpgme_data_release</span>(<span style="color:rgb(192,192,192)"> </span><span style="color:rgb(9,46,100)">plain</span><span style="color:rgb(192,192,192)"> </span>);</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:rgb(0,103,124)">gpgme_data_release</span>(<span style="color:rgb(192,192,192)"> </span><span style="color:rgb(9,46,100)">cipher</span><span style="color:rgb(192,192,192)"> </span>);</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="color:rgb(0,103,124)">gpgme_release</span>(<span style="color:rgb(192,192,192)"> </span><span style="color:rgb(9,46,100)">ctx</span><span style="color:rgb(192,192,192)"> </span>);</pre>
<pre style="margin-top:0px;margin-bottom:0px"><span style="font-family:arial,sans-serif;color:rgb(9,46,100)">outFile</span><span style="font-family:arial,sans-serif">.</span><span style="font-family:arial,sans-serif;font-style:italic;color:rgb(0,103,124)">close</span><span style="font-family:arial,sans-serif">();</span><br></pre><pre style="margin-top:0px;margin-bottom:0px"><span style="font-family:arial,sans-serif">}}}</span></pre><pre style="margin-top:0px;margin-bottom:0px"><span style="font-family:arial,sans-serif"><br></span></pre><pre style="margin-top:0px;margin-bottom:0px"><font face="arial, sans-serif">and, for the moment, the passphrase callback returns a fixed string, but as far as I can tell, it never gets called in either case:</font></pre><pre style="margin-top:0px;margin-bottom:0px"><font face="arial, sans-serif"><br></font></pre><pre style="margin-top:0px;margin-bottom:0px"><font face="arial, sans-serif">{{{</font></pre><pre style="margin-top:0px;margin-bottom:0px"><span style="font-family:arial,sans-serif;color:rgb(128,128,0)">extern</span><span style="font-family:arial,sans-serif;color:rgb(192,192,192)"> </span><span style="font-family:arial,sans-serif;color:rgb(0,128,0)">"C" </span><span style="font-family:arial,sans-serif">{</span><span style="font-family:arial,sans-serif;color:rgb(192,192,192)"> </span><br></pre><pre style="margin-top:0px;margin-bottom:0px"><pre style="margin-top:0px;margin-bottom:0px"><span style="font-family:arial,sans-serif;color:rgb(128,0,128)">gpgme_error_t</span><span style="font-family:arial,sans-serif;color:rgb(192,192,192)"> </span><span style="font-family:arial,sans-serif;font-weight:600;color:rgb(0,103,124)">passphraseCallback</span><span style="font-family:arial,sans-serif">(</span><span style="font-family:arial,sans-serif;color:rgb(128,128,0)">void</span><span style="font-family:arial,sans-serif;color:rgb(192,192,192)"> </span><span style="font-family:arial,sans-serif">*hook,</span><span style="font-family:arial,sans-serif;color:rgb(192,192,192)"> </span><span style="font-family:arial,sans-serif;color:rgb(128,128,0)">const</span><span style="font-family:arial,sans-serif;color:rgb(192,192,192)"> </span><span style="font-family:arial,sans-serif;color:rgb(128,128,0)">char</span><span style="font-family:arial,sans-serif;color:rgb(192,192,192)"> </span><span style="font-family:arial,sans-serif">*uid_hint,</span><span style="font-family:arial,sans-serif;color:rgb(192,192,192)"> </span><span style="font-family:arial,sans-serif;color:rgb(128,128,0)">const</span><span style="font-family:arial,sans-serif;color:rgb(192,192,192)"> </span><span style="font-family:arial,sans-serif;color:rgb(128,128,0)">char</span><span style="font-family:arial,sans-serif;color:rgb(192,192,192)"> </span><span style="font-family:arial,sans-serif">*passphrase_info,</span><span style="font-family:arial,sans-serif;color:rgb(192,192,192)"> </span><span style="font-family:arial,sans-serif;color:rgb(128,128,0)">int</span><span style="font-family:arial,sans-serif;color:rgb(192,192,192)"> </span><span style="font-family:arial,sans-serif">prev_was_bad,</span><span style="font-family:arial,sans-serif;color:rgb(192,192,192)"> </span><span style="font-family:arial,sans-serif;color:rgb(128,128,0)">int</span><span style="font-family:arial,sans-serif;color:rgb(192,192,192)"> </span><span style="font-family:arial,sans-serif">fd);</span></pre><pre style="margin-top:0px;margin-bottom:0px">}</pre><pre style="margin-top:0px;margin-bottom:0px"><br></pre></pre><pre style="margin-top:0px;margin-bottom:0px"><pre style="margin-top:0px;margin-bottom:0px"><span style="color:rgb(128,0,128)">gpgme_error_t</span><span style="color:rgb(192,192,192)"> </span><span style="font-weight:600;color:rgb(0,103,124)">passphraseCallback</span>(<span style="color:rgb(128,128,0)">void</span><span style="color:rgb(192,192,192)"> </span>*<span style="color:rgb(9,46,100)">hook</span>,<span style="color:rgb(192,192,192)"> </span><span style="color:rgb(128,128,0)">const</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(128,128,0)">char</span><span style="color:rgb(192,192,192)"> </span>*<span style="color:rgb(9,46,100)">uid_hint</span>,<span style="color:rgb(192,192,192)"> </span><span style="color:rgb(128,128,0)">const</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(128,128,0)">char</span><span style="color:rgb(192,192,192)"> </span>*<span style="color:rgb(9,46,100)">passphrase_info</span>,<span style="color:rgb(192,192,192)"> </span><span style="color:rgb(128,128,0)">int</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(9,46,100)">prev_was_bad</span>,<span style="color:rgb(192,192,192)"> </span><span style="color:rgb(128,128,0)">int</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(9,46,100)">fd</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)">qInfo</span>(<span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,128,0)">"passphraseCallback(</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,128,0)">hook:%llx</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,128,0)">uid_hint:%s</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,128,0)">passphrase_info:%s</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,128,0)">prev_was_bad:%d"</span>,<span style="color:rgb(192,192,192)"> </span>(<span style="color:rgb(128,128,0)">long</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(128,128,0)">long</span>)<span style="color:rgb(9,46,100)">hook</span>,<span style="color:rgb(192,192,192)"> </span><span style="color:rgb(9,46,100)">uid_hint</span>,<span style="color:rgb(192,192,192)"> </span><span style="color:rgb(9,46,100)">passphrase_info</span>,<span style="color:rgb(192,192,192)"> </span><span style="color:rgb(9,46,100)">prev_was_bad</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(128,128,0)">char</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(9,46,100)">phrase</span>[<span style="color:rgb(0,0,128)">103</span>];</pre><pre style="margin-top:0px;margin-bottom:0px"><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,103,124)">strncpy</span>(<span style="color:rgb(9,46,100)">phrase</span>,<span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,128,0)">"CorrectHorseBatteryStaple"</span>,<span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,0,128)">100</span>);</pre><pre style="margin-top:0px;margin-bottom:0px"><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,103,124)">strcat</span>(<span style="color:rgb(9,46,100)">phrase</span>,<span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,128,0)">"\n"</span>);</pre><pre style="margin-top:0px;margin-bottom:0px"><br></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>(<span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,103,124)">gpgme_io_writen</span>(<span style="color:rgb(192,192,192)"> </span><span style="color:rgb(9,46,100)">fd</span>,<span style="color:rgb(192,192,192)"> </span><span style="color:rgb(9,46,100)">phrase</span>,<span style="color:rgb(192,192,192)"> </span><span style="color:rgb(0,103,124)">strlen</span>(<span style="color:rgb(9,46,100)">phrase</span>)<span style="color:rgb(192,192,192)"> </span>)<span style="color:rgb(192,192,192)"> </span>!=<span style="color:rgb(192,192,192)"> </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(128,128,0)">return</span><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(128,0,128)">GPG_ERR_USER_1</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><span style="color:rgb(192,192,192)"> </span><span style="color:rgb(128,0,128)">GPG_ERR_NO_ERROR</span>;</pre><pre style="margin-top:0px;margin-bottom:0px">}</pre><pre style="margin-top:0px;margin-bottom:0px"><span style="font-family:arial,sans-serif">}}}</span><br></pre><pre style="margin-top:0px;margin-bottom:0px"><span style="font-family:arial,sans-serif"><br></span></pre><pre style="margin-top:0px;margin-bottom:0px"><span style="font-family:arial,sans-serif">I have used similar code to work with private/public key pairs that have no passphrase assigned and they seem to be working as expected, but I think in this application I'd rather use symmetric encryption with the passphrase obscured in my executable code.</span></pre><pre style="margin-top:0px;margin-bottom:0px"><span style="font-family:arial,sans-serif"><br></span></pre><pre style="margin-top:0px;margin-bottom:0px"><span style="font-family:arial,sans-serif">Which versions of gpg/gpgme support passphrase callback setting for symmetric encryption? My </span><span style="font-family:arial,sans-serif">gpgme_check_version returns 1.5.5 and gpg --version returns 1.4.18 in Ubuntu 15.10</span><br></pre><pre style="margin-top:0px;margin-bottom:0px"><span style="font-family:arial,sans-serif"><br></span></pre><pre style="margin-top:0px;margin-bottom:0px"><span style="font-family:arial,sans-serif">Any help would be appreciated.</span></pre><pre style="margin-top:0px;margin-bottom:0px"><span style="font-family:arial,sans-serif"><br></span></pre><pre style="margin-top:0px;margin-bottom:0px"><span style="font-family:arial,sans-serif">Thanks,</span></pre><pre style="margin-top:0px;margin-bottom:0px"><span style="font-family:arial,sans-serif"><br></span></pre><pre style="margin-top:0px;margin-bottom:0px"><span style="font-family:arial,sans-serif">Mike Inman</span></pre></pre></div></div>