52
44
#include "status.h"
55
enum gpga_protocol_codes {
57
GPGA_PROT_GET_VERSION = 1,
58
GPGA_PROT_GET_PASSPHRASE = 2,
59
GPGA_PROT_CLEAR_PASSPHRASE= 3,
60
GPGA_PROT_SHUTDOWN = 4,
64
GPGA_PROT_REPLY_BASE = 0x10000,
65
GPGA_PROT_OKAY = 0x10001,
66
GPGA_PROT_GOT_PASSPHRASE = 0x10002,
69
GPGA_PROT_ERROR_BASE = 0x20000,
70
GPGA_PROT_PROTOCOL_ERROR = 0x20001,
71
GPGA_PROT_INVALID_REQUEST= 0x20002,
72
GPGA_PROT_CANCELED = 0x20003,
73
GPGA_PROT_NO_PASSPHRASE = 0x20004,
74
GPGA_PROT_BAD_PASSPHRASE = 0x20005,
75
GPGA_PROT_INVALID_DATA = 0x20006,
76
GPGA_PROT_NOT_IMPLEMENTED= 0x20007,
77
GPGA_PROT_UI_PROBLEM = 0x20008
81
#define buftou32( p ) ((*(byte*)(p) << 24) | (*((byte*)(p)+1)<< 16) | \
82
(*((byte*)(p)+2) << 8) | (*((byte*)(p)+3)))
83
#define u32tobuf( p, a ) do { \
84
((byte*)p)[0] = (byte)((a) >> 24); \
85
((byte*)p)[1] = (byte)((a) >> 16); \
86
((byte*)p)[2] = (byte)((a) >> 8); \
87
((byte*)p)[3] = (byte)((a) ); \
90
#define digitp(p) (*(p) >= '0' && *(p) <= '9')
91
#define hexdigitp(a) (digitp (a) \
92
|| (*(a) >= 'A' && *(a) <= 'F') \
93
|| (*(a) >= 'a' && *(a) <= 'f'))
94
#define xtoi_1(p) (*(p) <= '9'? (*(p)- '0'): \
95
*(p) <= 'F'? (*(p)-'A'+10):(*(p)-'a'+10))
96
#define xtoi_2(p) ((xtoi_1(p) * 16) + xtoi_1((p)+1))
45
#include "call-agent.h"
100
48
static char *fd_passwd = NULL;
101
49
static char *next_pw = NULL;
102
50
static char *last_pw = NULL;
105
static int read_fd = 0;
106
static int write_fd = 0;
109
static void hash_passphrase( DEK *dek, char *pw, STRING2KEY *s2k, int create );
53
/* Hash a passphrase using the supplied s2k. If create is true, create
54
a new salt or what else must be filled into the s2k for a new key.
55
always needs: dek->algo, s2k->mode, s2k->hash_algo. */
57
hash_passphrase ( DEK *dek, char *pw, STRING2KEY *s2k, int create )
62
int pwlen = strlen(pw);
64
assert ( s2k->hash_algo );
65
dek->keylen = gcry_cipher_get_algo_keylen (dek->algo);
66
if ( !(dek->keylen > 0 && dek->keylen <= DIM(dek->key)) )
69
if (gcry_md_open (&md, s2k->hash_algo, 1))
71
for (pass=0; used < dek->keylen ; pass++ )
76
for (i=0; i < pass; i++ ) /* Preset the hash context. */
77
gcry_md_putc (md, 0 );
80
if ( s2k->mode == 1 || s2k->mode == 3 )
85
if ( create && !pass )
87
gcry_randomize (s2k->salt, 8, GCRY_STRONG_RANDOM);
89
s2k->count = opt.s2k_count;
94
count = S2K_DECODE_COUNT(s2k->count);
99
/* A little bit complicated because we need a ulong for count. */
100
while ( count > len2 ) /* maybe iterated+salted */
102
gcry_md_write ( md, s2k->salt, 8 );
103
gcry_md_write ( md, pw, pwlen );
107
gcry_md_write ( md, s2k->salt, count );
110
gcry_md_write ( md, s2k->salt, 8 );
112
gcry_md_write ( md, pw, count );
116
gcry_md_write ( md, pw, pwlen );
119
i = gcry_md_get_algo_dlen ( s2k->hash_algo );
120
if ( i > dek->keylen - used )
121
i = dek->keylen - used;
123
memcpy (dek->key+used, gcry_md_read (md, s2k->hash_algo), i);
112
132
have_static_passphrase()
134
return !!fd_passwd && opt.batch;
119
137
/****************
192
writen ( int fd, const void *buf, size_t nbytes )
195
DWORD nwritten, nleft = nbytes;
198
if ( !WriteFile( (HANDLE)write_fd, buf, nleft, &nwritten, NULL) ) {
199
log_error("write failed: ec=%d\n", (int)GetLastError());
202
/*log_info("** WriteFile fd=%d nytes=%d nwritten=%d\n",
203
write_fd, nbytes, (int)nwritten);*/
207
buf = (const BYTE *)buf + nwritten;
209
#elif defined(HAVE_DOSISH_SYSTEM) || defined(__riscos__)
210
/* not implemented */
212
size_t nleft = nbytes;
216
nwritten = write( fd, buf, nleft );
218
if ( errno == EINTR )
221
log_error ( "write() failed: %s\n", strerror (errno) );
226
buf = (const char*)buf + nwritten;
235
readn ( int fd, void *buf, size_t buflen, size_t *ret_nread )
238
DWORD nread, nleft = buflen;
241
if ( !ReadFile( (HANDLE)read_fd, buf, nleft, &nread, NULL) ) {
242
log_error("read() error: ec=%d\n", (int)GetLastError());
245
if (!nread || GetLastError() == ERROR_BROKEN_PIPE)
247
/*log_info("** ReadFile fd=%d buflen=%d nread=%d\n",
248
read_fd, buflen, (int)nread);*/
252
buf = (BYTE *)buf + nread;
255
*ret_nread = buflen - nleft;
257
#elif defined(HAVE_DOSISH_SYSTEM) || defined(__riscos__)
258
/* not implemented */
260
size_t nleft = buflen;
266
nread = read ( fd, buf, nleft );
271
log_error ( "read() error: %s\n", strerror (errno) );
278
buf = (char*)buf + nread;
281
*ret_nread = buflen - nleft;
287
/* read an entire line */
289
readline (int fd, char *buf, size_t buflen)
291
size_t nleft = buflen;
297
int n = read (fd, buf, nleft);
302
return -1; /* read error */
306
return -1; /* incomplete line */
313
for (; n && *p != '\n'; n--, p++)
317
break; /* at least one full line available - that's enough.
318
This function is just a temporary hack until we use
319
the assuna lib in gpg. So it is okay to forget
320
about pending bytes */
329
#if !defined (__riscos__)
331
#if !defined (_WIN32)
332
/* For the new Assuan protocol we may have to send options */
334
agent_send_option (int fd, const char *name, const char *value)
341
line = xmalloc (7 + strlen (name) + 1 + strlen (value) + 2);
342
strcpy (stpcpy (stpcpy (stpcpy (
343
stpcpy (line, "OPTION "), name), "="), value), "\n");
344
i = writen (fd, line, strlen (line));
350
nread = readline (fd, buf, DIM(buf)-1);
354
if (buf[0] == 'O' && buf[1] == 'K' && (buf[2] == ' ' || buf[2] == '\n'))
361
agent_send_all_options (int fd)
363
char *dft_display = NULL;
364
const char *dft_ttyname = NULL;
365
char *dft_ttytype = NULL;
370
dft_display = getenv ("DISPLAY");
371
if (opt.display || dft_display)
373
if (agent_send_option (fd, "display",
374
opt.display ? opt.display : dft_display))
380
dft_ttyname = getenv ("GPG_TTY");
381
if ((!dft_ttyname || !*dft_ttyname) && tty_get_ttyname ())
382
dft_ttyname = tty_get_ttyname ();
384
if (opt.ttyname || dft_ttyname)
386
if (agent_send_option (fd, "ttyname",
387
opt.ttyname ? opt.ttyname : dft_ttyname))
391
dft_ttytype = getenv ("TERM");
392
if (opt.ttytype || (dft_ttyname && dft_ttytype))
394
if (agent_send_option (fd, "ttytype",
395
opt.ttyname ? opt.ttytype : dft_ttytype))
399
#if defined(HAVE_SETLOCALE) && defined(LC_CTYPE)
400
old_lc = setlocale (LC_CTYPE, NULL);
402
old_lc = xstrdup (old_lc);
403
dft_lc = setlocale (LC_CTYPE, "");
405
if (opt.lc_ctype || (dft_ttyname && dft_lc))
407
rc = agent_send_option (fd, "lc-ctype",
408
opt.lc_ctype ? opt.lc_ctype : dft_lc);
410
#if defined(HAVE_SETLOCALE) && defined(LC_CTYPE)
413
setlocale (LC_CTYPE, old_lc);
420
#if defined(HAVE_SETLOCALE) && defined(LC_MESSAGES)
421
old_lc = setlocale (LC_MESSAGES, NULL);
423
old_lc = xstrdup (old_lc);
424
dft_lc = setlocale (LC_MESSAGES, "");
426
if (opt.lc_messages || (dft_ttyname && dft_lc))
428
rc = agent_send_option (fd, "lc-messages",
429
opt.lc_messages ? opt.lc_messages : dft_lc);
431
#if defined(HAVE_SETLOCALE) && defined(LC_MESSAGES)
434
setlocale (LC_MESSAGES, old_lc);
444
* Open a connection to the agent and send the magic string
445
* Returns: -1 on error or an filedescriptor for urther processing
449
agent_open (int *ret_prot)
458
if ( !(infostr = read_w32_registry_string(NULL, "Software\\GNU\\GnuPG",
460
|| *infostr == '0') {
461
log_error( _("gpg-agent is not available in this session\n"));
466
sprintf(pidstr, "%u", (unsigned int)GetCurrentProcessId());
467
if (write_w32_registry_string(NULL, "Software\\GNU\\GnuPG",
468
"agentCID", pidstr)) {
469
log_error( _("can't set client pid for the agent\n") );
472
h = OpenEvent(EVENT_ALL_ACCESS, FALSE, "gpg_agent");
474
Sleep(50); /* some time for the server */
475
if ( !(p = read_w32_registry_string(NULL, "Software\\GNU\\GnuPG",
477
log_error( _("can't get server read FD for the agent\n") );
482
if ( !(p = read_w32_registry_string(NULL, "Software\\GNU\\GnuPG",
484
log_error ( _("can't get server write FD for the agent\n") );
491
if ( writen ( fd, "GPGA\0\0\0\x01", 8 ) ) {
498
struct sockaddr_un client_addr;
502
if (opt.gpg_agent_info)
503
infostr = xstrdup (opt.gpg_agent_info);
506
infostr = getenv ( "GPG_AGENT_INFO" );
507
if ( !infostr || !*infostr ) {
508
log_error (_("gpg-agent is not available in this session\n"));
512
infostr = xstrdup ( infostr );
515
if ( !(p = strchr ( infostr, ':')) || p == infostr
516
|| (p-infostr)+1 >= sizeof client_addr.sun_path ) {
517
log_error( _("malformed GPG_AGENT_INFO environment variable\n"));
523
/* See whether this is the new gpg-agent using the Assuna protocl.
524
This agent identifies itself by have an info string with a
525
version number in the 3rd field. */
526
while (*p && *p != ':')
528
prot = *p? atoi (p+1) : 0;
529
if ( prot < 0 || prot > 1) {
530
log_error (_("gpg-agent protocol version %d is not supported\n"),prot);
537
if( (fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1 ) {
538
log_error ("can't create socket: %s\n", strerror(errno) );
544
memset( &client_addr, 0, sizeof client_addr );
545
client_addr.sun_family = AF_UNIX;
546
strcpy( client_addr.sun_path, infostr );
547
len = offsetof (struct sockaddr_un, sun_path)
548
+ strlen(client_addr.sun_path) + 1;
550
if( connect( fd, (struct sockaddr*)&client_addr, len ) == -1 ) {
551
log_error ( _("can't connect to `%s': %s\n"),
552
infostr, strerror (errno) );
561
if ( writen ( fd, "GPGA\0\0\0\x01", 8 ) ) {
566
else { /* assuan based gpg-agent */
570
nread = readline (fd, line, DIM(line));
571
if (nread < 3 || !(line[0] == 'O' && line[1] == 'K'
572
&& (line[2] == '\n' || line[2] == ' ')) ) {
573
log_error ( _("communication problem with gpg-agent\n"));
579
if (agent_send_all_options (fd)) {
580
log_error (_("problem with the agent - disabling agent use\n"));
594
agent_close ( int fd )
597
HANDLE h = OpenEvent(EVENT_ALL_ACCESS, FALSE, "gpg_agent");
603
#endif /* !__riscos__ */
608
237
* Ask the GPG Agent for the passphrase.
709
337
atext = xstrdup ( _("Enter passphrase\n") );
712
{ /* old style protocol */
713
n = 4 + 20 + strlen (atext);
715
u32tobuf (buf+4, GPGA_PROT_GET_PASSPHRASE );
716
memcpy (buf+8, fpr, 20 );
717
if ( writen ( fd, buf, 28 ) || writen ( fd, atext, strlen (atext) ) )
719
xfree (atext); atext = NULL;
722
if ( readn ( fd, buf, 12, &nread ) )
727
log_error ( "response from agent too short\n" );
730
n = buftou32 ( buf );
731
reply = buftou32 ( buf + 4 );
732
if ( reply == GPGA_PROT_GOT_PASSPHRASE )
737
if ( nread < 12 || n < 8 )
739
log_error ( "response from agent too short\n" );
742
pwlen = buftou32 ( buf + 8 );
745
if ( pwlen > n || n > 1000 )
747
log_error (_("passphrase too long\n"));
748
/* or protocol error */
751
/* we read the whole block in one chunk to give no hints
752
* on how long the passhrase actually is - this wastes some bytes
753
* but because we already have this padding we should not loosen
754
* this by issuing 2 read calls */
755
pw = xmalloc_secure ( n+1 );
756
if ( readn ( fd, pw, n, &nn ) )
760
log_error (_("invalid response from agent\n"));
763
pw[pwlen] = 0; /* make a C String */
765
free_public_key( pk );
768
bind_textdomain_codeset (PACKAGE_GT, orig_codeset);
770
xfree (orig_codeset);
773
else if ( reply == GPGA_PROT_CANCELED )
775
log_info ( _("cancelled by user\n") );
780
log_error ( _("problem with the agent: agent returns 0x%lx\n"),
340
if (!mode && cacheid)
341
my_cacheid = cacheid;
342
else if (!mode && have_fpr)
343
my_cacheid = bin2hex (fpr, 20, hexfprbuf);
784
{ /* The new Assuan protocol */
786
const unsigned char *s;
792
tryagain_text = _(tryagain_text);
794
/* We allocate 2 time the needed space for atext so that there
795
is enough space for escaping */
796
line = xmalloc (15 + 46
797
+ 3*strlen (tryagain_text) + 3*strlen (atext) + 2);
798
strcpy (line, "GET_PASSPHRASE ");
800
if (!mode && have_fpr)
802
for (i=0; i < 20; i++, p +=2 )
803
sprintf (p, "%02X", fpr[i]);
806
*p++ = 'X'; /* no caching */
808
for (i=0, s=tryagain_text; *s; s++)
810
if (*s < ' ' || *s == '+')
812
sprintf (p, "%%%02X", *s);
821
*p++ = 'X'; /* Use the standard prompt */
823
/* copy description */
824
for (i=0, s= atext; *s; s++)
826
if (*s < ' ' || *s == '+')
828
sprintf (p, "%%%02X", *s);
837
i = writen (fd, line, p - line);
841
xfree (atext); atext = NULL;
844
pw = xmalloc_secure (500);
845
nread = readline (fd, pw, 499);
849
if (pw[0] == 'O' && pw[1] == 'K' && pw[2] == ' ')
850
{ /* we got a passphrase - convert it back from hex */
853
for (i=3; i < nread && hexdigitp (pw+i); i+=2)
854
pw[pwlen++] = xtoi_2 (pw+i);
855
pw[pwlen] = 0; /* make a C String */
857
free_public_key( pk );
860
bind_textdomain_codeset (PACKAGE_GT, orig_codeset);
862
xfree (orig_codeset);
865
else if (nread > 4 && !memcmp (pw, "ERR ", 4)
866
&& (0xffff & strtoul (&pw[4], NULL, 0)) == 99)
868
/* 99 is GPG_ERR_CANCELED. FIXME: Check tail and overflow,
869
and use gpg-error. */
870
log_info (_("cancelled by user\n") );
876
log_error (_("problem with the agent - disabling agent use\n"));
885
bind_textdomain_codeset (PACKAGE_GT, orig_codeset);
891
free_public_key( pk );
348
tryagain_text = _(tryagain_text);
350
my_prompt = custom_prompt ? native_to_utf8 (custom_prompt): NULL;
352
rc = agent_get_passphrase (my_cacheid, tryagain_text, my_prompt, atext, &pw);
894
#endif /* Posix or W32 */
355
xfree (atext); atext = NULL;
357
i18n_switchback (orig_codeset);
362
else if ( gpg_err_code (rc) == GPG_ERR_CANCELED )
364
log_info (_("cancelled by user\n") );
370
log_error (_("problem with the agent: %s\n"), gpg_strerror (rc));
371
/* Due to limitations in the API of the upper layers they
372
consider an error as no passphrase entered. This works in
373
most cases but not during key creation where this should
374
definitely not happen and let it continue without requiring a
375
passphrase. Given that now all the upper layers handle a
376
cancel correctly, we simply set the cancel flag now for all
377
errors from the agent. */
383
free_public_key( pk );
898
* Clear the cached passphrase
394
* Clear the cached passphrase. If CACHEID is not NULL, it will be
395
* used instead of a cache ID derived from KEYID.
901
passphrase_clear_cache ( u32 *keyid, int algo )
398
passphrase_clear_cache ( u32 *keyid, const char *cacheid, int algo )
903
#if defined(__riscos__)
912
byte fpr[MAX_FINGERPRINT_LEN];
915
#if MAX_FINGERPRINT_LEN < 20
916
#error agent needs a 20 byte fingerprint
922
pk = xcalloc (1, sizeof *pk );
923
memset (fpr, 0, MAX_FINGERPRINT_LEN );
924
if( !keyid || get_pubkey( pk, keyid ) )
926
log_debug ("oops, no key in passphrase_clear_cache\n");
927
goto failure; /* oops: no key for some reason */
932
fingerprint_from_pk( pk, fpr, &dummy );
935
if ( (fd = agent_open (&prot)) == -1 )
942
u32tobuf (buf+4, GPGA_PROT_CLEAR_PASSPHRASE );
943
memcpy (buf+8, fpr, 20 );
944
if ( writen ( fd, buf, 28 ) )
948
if ( readn ( fd, buf, 8, &nread ) )
952
log_error ( "response from agent too short\n" );
956
reply = buftou32 ( buf + 4 );
957
if ( reply != GPGA_PROT_OKAY && reply != GPGA_PROT_NO_PASSPHRASE )
959
log_error ( _("problem with the agent: agent returns 0x%lx\n"),
964
{ /* The assuan protocol */
968
line = xmalloc (17 + 40 + 2);
969
strcpy (line, "CLEAR_PASSPHRASE ");
971
for (i=0; i < 20; i++, p +=2 )
972
sprintf (p, "%02X", fpr[i]);
974
i = writen (fd, line, p - line);
980
nread = readline (fd, buf, DIM(buf)-1);
984
if (buf[0] == 'O' && buf[1] == 'K' && (buf[2] == ' ' || buf[2] == '\n'))
988
log_error (_("problem with the agent - disabling agent use\n"));
996
free_public_key( pk );
997
#endif /* Posix or W32 */
405
# if MAX_FINGERPRINT_LEN < 20
406
# error agent needs a 20 byte fingerprint
408
byte fpr[MAX_FINGERPRINT_LEN];
409
char hexfprbuf[2*20+1];
412
pk = xcalloc (1, sizeof *pk);
413
if ( !keyid || get_pubkey( pk, keyid ) )
415
log_error ("key not found in passphrase_clear_cache\n");
416
free_public_key (pk);
419
memset (fpr, 0, MAX_FINGERPRINT_LEN );
420
fingerprint_from_pk ( pk, fpr, &dummy );
421
bin2hex (fpr, 20, hexfprbuf);
422
rc = agent_clear_passphrase (hexfprbuf);
423
free_public_key ( pk );
426
rc = agent_clear_passphrase (cacheid);
429
log_error (_("problem with the agent: %s\n"), gpg_strerror (rc));
1003
433
/****************
1004
* Get a passphrase for the secret key with KEYID, display TEXT
1005
* if the user needs to enter the passphrase.
1006
* mode 0 = standard, 1 = same but don't show key info,
1007
* 2 = create new passphrase
1008
* Returns: a DEK with a session key; caller must free
1009
* or NULL if the passphrase was not correctly repeated.
1011
* a dek->keylen of 0 means: no passphrase entered.
1014
* pubkey_algo is only informational. Note that TRYAGAIN_TEXT must
1015
* not be translated as this is done within this function (required to
1016
* switch to utf-8 when the agent is in use). If CANCELED is not
1017
* NULL, it is set to 1 if the user choosed to cancel the operation,
1018
* otherwise it will be set to 0.
434
* Ask for a passphrase and return that string.
437
ask_passphrase (const char *description,
438
const char *tryagain_text,
439
const char *promptid,
441
const char *cacheid, int *canceled)
448
if (!opt.batch && description)
450
if (strchr (description, '%'))
452
char *tmp = unescape_percent_string
453
((const unsigned char*)description);
454
tty_printf ("\n%s\n", tmp);
458
tty_printf ("\n%s\n",description);
461
if (have_static_passphrase ())
463
pw = xmalloc_secure (strlen(fd_passwd)+1);
464
strcpy (pw, fd_passwd);
467
pw = passphrase_get (NULL, 0, cacheid,
468
tryagain_text, description, prompt,
472
write_status( STATUS_MISSING_PASSPHRASE );
478
/* Return a new DEK object Using the string-to-key sepcifier S2K. Use
479
KEYID and PUBKEY_ALGO to prompt the user. Returns NULL is the user
480
selected to cancel the passphrase entry and if CANCELED is not
481
NULL, sets it to true.
483
MODE 0: Allow cached passphrase
484
1: Ignore cached passphrase
485
2: Ditto, but change the text to "repeat entry"
1021
passphrase_to_dek( u32 *keyid, int pubkey_algo,
488
passphrase_to_dek (u32 *keyid, int pubkey_algo,
1022
489
int cipher_algo, STRING2KEY *s2k, int mode,
1023
490
const char *tryagain_text, int *canceled)
1027
STRING2KEY help_s2k;
1033
/* This is used for the old rfc1991 mode
1034
* Note: This must match the code in encode.c with opt.rfc1991 set */
1037
s2k->hash_algo = opt.s2k_digest_algo;
1040
if( !next_pw && is_status_enabled() ) {
1047
if( keyid[2] && keyid[3] ) {
1048
used_kid[0] = keyid[2];
1049
used_kid[1] = keyid[3];
1052
used_kid[0] = keyid[0];
1053
used_kid[1] = keyid[1];
1056
us = get_long_user_id_string( keyid );
1057
write_status_text( STATUS_USERID_HINT, us );
1060
sprintf( buf, "%08lX%08lX %08lX%08lX %d 0",
1061
(ulong)keyid[0], (ulong)keyid[1],
1062
(ulong)used_kid[0], (ulong)used_kid[1],
1065
write_status_text( STATUS_NEED_PASSPHRASE, buf );
1068
sprintf( buf, "%d %d %d", cipher_algo, s2k->mode, s2k->hash_algo );
1069
write_status_text( STATUS_NEED_PASSPHRASE_SYM, buf );
1073
if( keyid && !opt.batch && !next_pw && mode!=1 ) {
1074
PKT_public_key *pk = xcalloc (1, sizeof *pk );
1078
tty_printf(_("\nYou need a passphrase to unlock the secret key for\n"
1080
p = get_user_id( keyid, &n );
1081
tty_print_utf8_string( p, n );
1085
if( !get_pubkey( pk, keyid ) ) {
1086
const char *s = gcry_pk_algo_name ( pk->pubkey_algo );
1087
tty_printf( _("%u-bit %s key, ID %08lX, created %s"),
1088
nbits_from_pk( pk ), s?s:"?", (ulong)keyid[1],
1089
strtimestamp(pk->timestamp) );
1090
if( keyid[2] && keyid[3] && keyid[0] != keyid[2]
1091
&& keyid[1] != keyid[3] )
1092
tty_printf( _(" (main key ID %08lX)"), (ulong)keyid[3] );
1097
free_public_key( pk );
1105
else if ( opt.use_agent ) {
1106
pw = agent_get_passphrase ( keyid, mode == 2? 1: 0,
1107
tryagain_text, canceled );
1114
if( *pw && mode == 2 ) {
1115
char *pw2 = agent_get_passphrase ( keyid, 2, NULL, canceled );
1126
if( strcmp(pw, pw2) ) {
1134
else if( fd_passwd ) {
1135
pw = xmalloc_secure ( strlen(fd_passwd)+1 );
1136
strcpy( pw, fd_passwd );
1138
else if( opt.batch ) {
1139
log_error(_("can't query password in batchmode\n"));
1140
pw = xstrdup ( "" ); /* return an empty passphrase */
1143
pw = cpr_get_hidden("passphrase.enter", _("Enter passphrase: ") );
1145
if( mode == 2 && !cpr_enabled() ) {
1146
char *pw2 = cpr_get_hidden("passphrase.repeat",
1147
_("Repeat passphrase: ") );
1149
if( strcmp(pw, pw2) ) {
1159
write_status( STATUS_MISSING_PASSPHRASE );
1161
dek = xcalloc_secure (1, sizeof *dek );
1162
dek->algo = cipher_algo;
1163
if( !*pw && mode == 2 )
1166
hash_passphrase( dek, pw, s2k, mode==2 );
1174
* Hash a passphrase using the supplied s2k. If create is true, create
1175
* a new salt or what else must be filled into the s2k for a new key.
1176
* always needs: dek->algo, s2k->mode, s2k->hash_algo.
1179
hash_passphrase( DEK *dek, char *pw, STRING2KEY *s2k, int create )
1184
int pwlen = strlen(pw);
1186
assert( s2k->hash_algo );
1187
dek->keylen = gcry_cipher_get_algo_keylen (dek->algo);
1188
if( !(dek->keylen > 0 && dek->keylen <= DIM(dek->key)) )
1191
gcry_md_open (&md, s2k->hash_algo, 1);
1192
for(pass=0; used < dek->keylen ; pass++ ) {
1195
for(i=0; i < pass; i++ ) /* preset the hash context */
1196
gcry_md_putc (md, 0 );
1199
if( s2k->mode == 1 || s2k->mode == 3 ) {
1200
int len2 = pwlen + 8;
1203
if( create && !pass ) {
1204
gcry_randomize(s2k->salt, 8, GCRY_STRONG_RANDOM );
1205
if( s2k->mode == 3 )
1206
s2k->count = 96; /* 65536 iterations */
1209
if( s2k->mode == 3 ) {
1210
count = (16ul + (s2k->count & 15)) << ((s2k->count >> 4) + 6);
1214
/* a little bit complicated because we need a ulong for count */
1215
while( count > len2 ) { /* maybe iterated+salted */
1216
gcry_md_write( md, s2k->salt, 8 );
1217
gcry_md_write( md, pw, pwlen );
1221
gcry_md_write( md, s2k->salt, count );
1223
gcry_md_write( md, s2k->salt, 8 );
1225
gcry_md_write( md, pw, count );
1229
gcry_md_write( md, pw, pwlen );
1230
gcry_md_final ( md );
1231
i = gcry_md_get_algo_dlen (s2k->hash_algo);
1232
if( i > dek->keylen - used )
1233
i = dek->keylen - used;
1234
memcpy( dek->key+used, gcry_md_read (md, s2k->hash_algo), i );
498
canceled = &dummy_canceled;
503
/* This is used for the old rfc1991 mode
504
* Note: This must match the code in encode.c with opt.rfc1991 set */
507
s2k->hash_algo = S2K_DIGEST_ALGO;
510
/* If we do not have a passphrase available in NEXT_PW and status
511
information are request, we print them now. */
512
if ( !next_pw && is_status_enabled() )
521
if ( keyid[2] && keyid[3] )
523
used_kid[0] = keyid[2];
524
used_kid[1] = keyid[3];
528
used_kid[0] = keyid[0];
529
used_kid[1] = keyid[1];
532
us = get_long_user_id_string ( keyid );
533
write_status_text ( STATUS_USERID_HINT, us );
536
snprintf (buf, sizeof buf -1, "%08lX%08lX %08lX%08lX %d 0",
537
(ulong)keyid[0], (ulong)keyid[1],
538
(ulong)used_kid[0], (ulong)used_kid[1],
541
write_status_text ( STATUS_NEED_PASSPHRASE, buf );
545
snprintf (buf, sizeof buf -1, "%d %d %d",
546
cipher_algo, s2k->mode, s2k->hash_algo );
547
write_status_text ( STATUS_NEED_PASSPHRASE_SYM, buf );
551
/* If we do have a keyID, we do not have a passphrase available in
552
NEXT_PW, we are not running in batch mode and we do not want to
553
ignore the passphrase cache (mode!=1), print a prompt with
554
information on that key. */
555
if ( keyid && !opt.batch && !next_pw && mode!=1 )
557
PKT_public_key *pk = xmalloc_clear( sizeof *pk );
560
p = get_user_id_native(keyid);
562
tty_printf (_("You need a passphrase to unlock the secret key for\n"
563
"user: \"%s\"\n"),p);
566
if ( !get_pubkey( pk, keyid ) )
568
const char *s = gcry_pk_algo_name ( pk->pubkey_algo );
570
tty_printf (_("%u-bit %s key, ID %s, created %s"),
571
nbits_from_pk( pk ), s?s:"?", keystr(keyid),
572
strtimestamp(pk->timestamp) );
573
if ( keyid[2] && keyid[3]
574
&& keyid[0] != keyid[2] && keyid[1] != keyid[3] )
576
if ( keystrlen () > 10 )
579
tty_printf (_(" (subkey on main key ID %s)"),
583
tty_printf ( _(" (main key ID %s)"), keystr(&keyid[2]) );
590
free_public_key( pk );
595
/* Simply return the passphrase we already have in NEXT_PW. */
599
else if ( have_static_passphrase () )
601
/* Return the passphrase we have stored in FD_PASSWD. */
602
pw = xmalloc_secure ( strlen(fd_passwd)+1 );
603
strcpy ( pw, fd_passwd );
607
/* Divert to the gpg-agent. */
608
pw = passphrase_get ( keyid, mode == 2? 1: 0, NULL,
609
tryagain_text, NULL, NULL, canceled );
613
write_status( STATUS_MISSING_PASSPHRASE );
618
if ( *pw && mode == 2 )
621
for(i=0;i<opt.passwd_repeat;i++)
623
char *pw2 = passphrase_get ( keyid, 2, NULL, NULL, NULL,
629
write_status( STATUS_MISSING_PASSPHRASE );
634
if ( strcmp(pw, pw2) )
646
write_status( STATUS_MISSING_PASSPHRASE );
648
/* Hash the passphrase and store it in a newly allocated DEK object.
649
Keep a copy of the passphrase in LAST_PW for use by
650
get_last_passphrase(). */
651
dek = xmalloc_secure_clear ( sizeof *dek );
652
dek->algo = cipher_algo;
653
if ( !*pw && mode == 2 )
656
hash_passphrase( dek, pw, s2k, mode==2 );