1 /* passphrase.c - Get a passphrase
2 * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004,
3 * 2005, 2006, 2007, 2009 Free Software Foundation, Inc.
5 * This file is part of GnuPG.
7 * GnuPG is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
12 * GnuPG is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, see <http://www.gnu.org/licenses/>.
32 #ifdef HAVE_LANGINFO_CODESET
45 #include "call-agent.h"
48 static char *fd_passwd
= NULL
;
49 static char *next_pw
= NULL
;
50 static char *last_pw
= NULL
;
53 /* Hash a passphrase using the supplied s2k.
54 Always needs: dek->algo, s2k->mode, s2k->hash_algo. */
56 hash_passphrase ( DEK
*dek
, char *pw
, STRING2KEY
*s2k
)
61 int pwlen
= strlen(pw
);
63 assert ( s2k
->hash_algo
);
64 dek
->keylen
= openpgp_cipher_get_algo_keylen (dek
->algo
);
65 if ( !(dek
->keylen
> 0 && dek
->keylen
<= DIM(dek
->key
)) )
68 if (gcry_md_open (&md
, s2k
->hash_algo
, 1))
70 for (pass
=0; used
< dek
->keylen
; pass
++ )
75 for (i
=0; i
< pass
; i
++ ) /* Preset the hash context. */
76 gcry_md_putc (md
, 0 );
79 if ( s2k
->mode
== 1 || s2k
->mode
== 3 )
86 count
= S2K_DECODE_COUNT(s2k
->count
);
91 /* Fixme: To avoid DoS attacks by sending an sym-encrypted
92 packet with a very high S2K count, we should either cap
93 the iteration count or CPU seconds based timeout. */
95 /* A little bit complicated because we need a ulong for count. */
96 while ( count
> len2
) /* maybe iterated+salted */
98 gcry_md_write ( md
, s2k
->salt
, 8 );
99 gcry_md_write ( md
, pw
, pwlen
);
103 gcry_md_write ( md
, s2k
->salt
, count
);
106 gcry_md_write ( md
, s2k
->salt
, 8 );
108 gcry_md_write ( md
, pw
, count
);
112 gcry_md_write ( md
, pw
, pwlen
);
115 i
= gcry_md_get_algo_dlen ( s2k
->hash_algo
);
116 if ( i
> dek
->keylen
- used
)
117 i
= dek
->keylen
- used
;
119 memcpy (dek
->key
+used
, gcry_md_read (md
, s2k
->hash_algo
), i
);
128 have_static_passphrase()
130 return !!fd_passwd
&& opt
.batch
;
134 * Set the passphrase to be used for the next query and only for the next
138 set_next_passphrase( const char *s
)
144 next_pw
= xmalloc_secure( strlen(s
)+1 );
145 strcpy (next_pw
, s
);
150 * Get the last passphrase used in passphrase_to_dek.
151 * Note: This removes the passphrase from this modules and
152 * the caller must free the result. May return NULL:
155 get_last_passphrase()
162 /* As if we had used the passphrase - make it the last_pw. */
164 next_to_last_passphrase(void)
173 /* Here's an interesting question: since this passphrase was passed in
174 on the command line, is there really any point in using secure
175 memory for it? I'm going with 'yes', since it doesn't hurt, and
176 might help in some small way (swapping). */
179 set_passphrase_from_string(const char *pass
)
182 fd_passwd
= xmalloc_secure(strlen(pass
)+1);
183 strcpy (fd_passwd
, pass
);
188 read_passphrase_from_fd( int fd
)
194 { /* Not used but we have to do a dummy read, so that it won't end
195 up at the begin of the message if the quite usual trick to
196 prepend the passphtrase to the message is used. */
199 while (!(read (fd
, buf
, 1) != 1 || *buf
== '\n' ))
205 for (pw
= NULL
, i
= len
= 100; ; i
++ )
211 pw
= xmalloc_secure( len
);
220 if (read( fd
, pw
+i
, 1) != 1 || pw
[i
] == '\n' )
225 tty_printf("\b\b\b \n" );
233 * Ask the GPG Agent for the passphrase.
234 * Mode 0: Allow cached passphrase
235 * 1: No cached passphrase; that is we are asking for a new passphrase
236 * FIXME: Only partially implemented
238 * Note that TRYAGAIN_TEXT must not be translated. If CANCELED is not
239 * NULL, the function does set it to 1 if the user canceled the
240 * operation. If CACHEID is not NULL, it will be used as the cacheID
241 * for the gpg-agent; if is NULL and a key fingerprint can be
242 * computed, this will be used as the cacheid.
245 passphrase_get ( u32
*keyid
, int mode
, const char *cacheid
, int repeat
,
246 const char *tryagain_text
,
247 const char *custom_description
,
248 const char *custom_prompt
, int *canceled
)
253 PKT_public_key
*pk
= xmalloc_clear( sizeof *pk
);
254 byte fpr
[MAX_FINGERPRINT_LEN
];
258 char hexfprbuf
[20*2+1];
259 const char *my_cacheid
;
260 int check
= (mode
== 1);
265 #if MAX_FINGERPRINT_LEN < 20
266 #error agent needs a 20 byte fingerprint
269 memset (fpr
, 0, MAX_FINGERPRINT_LEN
);
270 if( keyid
&& get_pubkey( pk
, keyid
) )
273 free_public_key( pk
);
274 pk
= NULL
; /* oops: no key for some reason */
277 orig_codeset
= i18n_switchto_utf8 ();
279 if (custom_description
)
280 atext
= native_to_utf8 (custom_description
);
281 else if ( !mode
&& pk
&& keyid
)
285 const char *algo_name
= gcry_pk_algo_name ( pk
->pubkey_algo
);
292 #define KEYIDSTRING _(" (main key ID %s)")
294 maink
= xmalloc ( strlen (KEYIDSTRING
) + keystrlen() + 20 );
295 if( keyid
[2] && keyid
[3] && keyid
[0] != keyid
[2]
296 && keyid
[1] != keyid
[3] )
297 sprintf( maink
, KEYIDSTRING
, keystr(&keyid
[2]) );
301 uid
= get_user_id ( keyid
, &uidlen
);
302 timestr
= strtimestamp (pk
->timestamp
);
306 #define PROMPTSTRING _("Please enter the passphrase to unlock the" \
307 " secret key for the OpenPGP certificate:\n" \
309 "%u-bit %s key, ID %s,\n" \
312 atext
= xmalloc ( 100 + strlen (PROMPTSTRING
)
313 + uidlen
+ 15 + strlen(algo_name
) + keystrlen()
314 + strlen (timestr
) + strlen (maink
) );
315 sprintf (atext
, PROMPTSTRING
,
317 nbits_from_pk (pk
), algo_name
, keystr(&keyid
[0]), timestr
,
326 fingerprint_from_pk( pk
, fpr
, &dummy
);
332 atext
= xstrdup ( _("Enter passphrase\n") );
335 if (!mode
&& cacheid
)
336 my_cacheid
= cacheid
;
337 else if (!mode
&& have_fpr
)
338 my_cacheid
= bin2hex (fpr
, 20, hexfprbuf
);
343 tryagain_text
= _(tryagain_text
);
345 my_prompt
= custom_prompt
? native_to_utf8 (custom_prompt
): NULL
;
347 rc
= agent_get_passphrase (my_cacheid
, tryagain_text
, my_prompt
, atext
,
351 xfree (atext
); atext
= NULL
;
353 i18n_switchback (orig_codeset
);
358 else if ( gpg_err_code (rc
) == GPG_ERR_CANCELED
)
360 log_info (_("cancelled by user\n") );
366 log_error (_("problem with the agent: %s\n"), gpg_strerror (rc
));
367 /* Due to limitations in the API of the upper layers they
368 consider an error as no passphrase entered. This works in
369 most cases but not during key creation where this should
370 definitely not happen and let it continue without requiring a
371 passphrase. Given that now all the upper layers handle a
372 cancel correctly, we simply set the cancel flag now for all
373 errors from the agent. */
377 write_status_error ("get_passphrase", rc
);
381 free_public_key( pk
);
392 * Clear the cached passphrase. If CACHEID is not NULL, it will be
393 * used instead of a cache ID derived from KEYID.
396 passphrase_clear_cache ( u32
*keyid
, const char *cacheid
, int algo
)
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
));
433 /* Return a new DEK object Using the string-to-key sepcifier S2K. Use
434 KEYID and PUBKEY_ALGO to prompt the user. Returns NULL is the user
435 selected to cancel the passphrase entry and if CANCELED is not
436 NULL, sets it to true.
438 MODE 0: Allow cached passphrase
439 1: Ignore cached passphrase
440 2: Ditto, but create a new key
441 3: Allow cached passphrase; use the S2K salt as the cache ID
442 4: Ditto, but create a new key
445 passphrase_to_dek_ext (u32
*keyid
, int pubkey_algo
,
446 int cipher_algo
, STRING2KEY
*s2k
, int mode
,
447 const char *tryagain_text
,
448 const char *custdesc
, const char *custprompt
,
455 char s2k_cacheidbuf
[1+16+1], *s2k_cacheid
= NULL
;
458 canceled
= &dummy_canceled
;
463 assert (mode
!= 3 && mode
!= 4);
464 /* This is used for the old rfc1991 mode
465 * Note: This must match the code in encode.c with opt.rfc1991 set */
468 s2k
->hash_algo
= S2K_DIGEST_ALGO
;
471 /* Create a new salt or what else to be filled into the s2k for a
473 if ((mode
== 2 || mode
== 4) && (s2k
->mode
== 1 || s2k
->mode
== 3))
475 gcry_randomize (s2k
->salt
, 8, GCRY_STRONG_RANDOM
);
476 if ( s2k
->mode
== 3 )
477 s2k
->count
= opt
.s2k_count
;
480 /* If we do not have a passphrase available in NEXT_PW and status
481 information are request, we print them now. */
482 if ( !next_pw
&& is_status_enabled() )
491 if ( keyid
[2] && keyid
[3] )
493 used_kid
[0] = keyid
[2];
494 used_kid
[1] = keyid
[3];
498 used_kid
[0] = keyid
[0];
499 used_kid
[1] = keyid
[1];
502 us
= get_long_user_id_string ( keyid
);
503 write_status_text ( STATUS_USERID_HINT
, us
);
506 snprintf (buf
, sizeof buf
-1, "%08lX%08lX %08lX%08lX %d 0",
507 (ulong
)keyid
[0], (ulong
)keyid
[1],
508 (ulong
)used_kid
[0], (ulong
)used_kid
[1],
511 write_status_text ( STATUS_NEED_PASSPHRASE
, buf
);
515 snprintf (buf
, sizeof buf
-1, "%d %d %d",
516 cipher_algo
, s2k
->mode
, s2k
->hash_algo
);
517 write_status_text ( STATUS_NEED_PASSPHRASE_SYM
, buf
);
521 /* If we do have a keyID, we do not have a passphrase available in
522 NEXT_PW, we are not running in batch mode and we do not want to
523 ignore the passphrase cache (mode!=1), print a prompt with
524 information on that key. */
525 if ( keyid
&& !opt
.batch
&& !next_pw
&& mode
!=1 )
527 PKT_public_key
*pk
= xmalloc_clear( sizeof *pk
);
530 p
= get_user_id_native(keyid
);
532 tty_printf (_("You need a passphrase to unlock the secret key for\n"
533 "user: \"%s\"\n"),p
);
536 if ( !get_pubkey( pk
, keyid
) )
538 const char *s
= gcry_pk_algo_name ( pk
->pubkey_algo
);
540 tty_printf (_("%u-bit %s key, ID %s, created %s"),
541 nbits_from_pk( pk
), s
?s
:"?", keystr(keyid
),
542 strtimestamp(pk
->timestamp
) );
543 if ( keyid
[2] && keyid
[3]
544 && keyid
[0] != keyid
[2] && keyid
[1] != keyid
[3] )
546 if ( keystrlen () > 10 )
549 tty_printf (_(" (subkey on main key ID %s)"),
553 tty_printf ( _(" (main key ID %s)"), keystr(&keyid
[2]) );
560 free_public_key( pk
);
565 /* Simply return the passphrase we already have in NEXT_PW. */
569 else if ( have_static_passphrase () )
571 /* Return the passphrase we have stored in FD_PASSWD. */
572 pw
= xmalloc_secure ( strlen(fd_passwd
)+1 );
573 strcpy ( pw
, fd_passwd
);
577 if ((mode
== 3 || mode
== 4) && (s2k
->mode
== 1 || s2k
->mode
== 3))
579 memset (s2k_cacheidbuf
, 0, sizeof s2k_cacheidbuf
);
580 *s2k_cacheidbuf
= 'S';
581 bin2hex (s2k
->salt
, 8, s2k_cacheidbuf
+ 1);
582 s2k_cacheid
= s2k_cacheidbuf
;
585 /* Divert to the gpg-agent. */
586 pw
= passphrase_get (keyid
, mode
== 2, s2k_cacheid
,
587 (mode
== 2 || mode
== 4)? opt
.passwd_repeat
: 0,
588 tryagain_text
, custdesc
, custprompt
, canceled
);
592 write_status( STATUS_MISSING_PASSPHRASE
);
598 write_status( STATUS_MISSING_PASSPHRASE
);
600 /* Hash the passphrase and store it in a newly allocated DEK object.
601 Keep a copy of the passphrase in LAST_PW for use by
602 get_last_passphrase(). */
603 dek
= xmalloc_secure_clear ( sizeof *dek
);
604 dek
->algo
= cipher_algo
;
605 if ( (!pw
|| !*pw
) && (mode
== 2 || mode
== 4))
608 hash_passphrase (dek
, pw
, s2k
);
610 memcpy (dek
->s2k_cacheid
, s2k_cacheid
, sizeof dek
->s2k_cacheid
);
618 passphrase_to_dek (u32
*keyid
, int pubkey_algo
,
619 int cipher_algo
, STRING2KEY
*s2k
, int mode
,
620 const char *tryagain_text
, int *canceled
)
622 return passphrase_to_dek_ext (keyid
, pubkey_algo
, cipher_algo
,
623 s2k
, mode
, tryagain_text
, NULL
, NULL
,