gpg: Improve function documentation and some comments.
[gnupg.git] / g10 / seckey-cert.c
index a265a37..02dbb48 100644 (file)
-/* seckey-cert.c -  secret key certifucate packet handling
- *     Copyright (C) 1998 Free Software Foundation, Inc.
+/* seckey-cert.c - Not anymore used
+ * Copyright (C) 1998, 1999, 2000, 2001, 2002,
+ *               2006, 2009 Free Software Foundation, Inc.
  *
- * This file is part of GNUPG.
+ * This file is part of GnuPG.
  *
- * GNUPG is free software; you can redistribute it and/or modify
+ * GnuPG is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
+ * the Free Software Foundation; either version 3 of the License, or
  * (at your option) any later version.
  *
- * GNUPG is distributed in the hope that it will be useful,
+ * GnuPG is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
+ * along with this program; if not, see <http://www.gnu.org/licenses/>.
  */
 
+#error Not anymore used - only kept for reference in the repository.
+
 #include <config.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <assert.h>
+
+#include "gpg.h"
 #include "util.h"
-#include "memory.h"
 #include "packet.h"
-#include "mpi.h"
 #include "keydb.h"
 #include "cipher.h"
 #include "main.h"
 #include "options.h"
-
+#include "i18n.h"
+#include "status.h"
+#include "pkglue.h"
 
 static int
-do_check( PKT_secret_cert *cert )
+xxxx_do_check( PKT_secret_key *sk, const char *tryagain_text, int mode,
+               int *canceled )
 {
+    gpg_error_t err;
     byte *buffer;
     u16 csum=0;
-    int res;
-    unsigned nbytes;
+    int i, res;
+    size_t nbytes;
 
-    if( cert->is_protected ) { /* remove the protection */
+    if( sk->is_protected ) { /* remove the protection */
        DEK *dek = NULL;
-       u32 keyid[2];
-       CIPHER_HANDLE cipher_hd=NULL;
-       PKT_secret_cert *save_cert;
-       char save_iv[8];
+       u32 keyid[4]; /* 4! because we need two of them */
+       gcry_cipher_hd_t cipher_hd=NULL;
+       PKT_secret_key *save_sk;
 
-       switch( cert->protect.algo ) {
-         case CIPHER_ALGO_NONE: BUG(); break;
-         case CIPHER_ALGO_BLOWFISH160:
-         case CIPHER_ALGO_BLOWFISH:
-         case CIPHER_ALGO_CAST:
-           keyid_from_skc( cert, keyid );
-           dek = passphrase_to_dek( keyid, cert->protect.algo,
-                                             &cert->protect.s2k, 0 );
-           cipher_hd = cipher_open( cert->protect.algo,
-                                    CIPHER_MODE_AUTO_CFB, 1);
-           cipher_setkey( cipher_hd, dek->key, dek->keylen );
-           cipher_setiv( cipher_hd, NULL );
-           m_free(dek); /* pw is in secure memory, so m_free() burns it */
-           save_cert = copy_secret_cert( NULL, cert );
-           memcpy(save_iv, cert->protect.iv, 8 );
-           cipher_decrypt( cipher_hd, cert->protect.iv, cert->protect.iv, 8 );
-           switch( cert->pubkey_algo ) {
-             case PUBKEY_ALGO_ELGAMAL:
-             case PUBKEY_ALGO_ELGAMAL_E:
-               buffer = mpi_get_secure_buffer( cert->d.elg.x, &nbytes, NULL );
-               cipher_decrypt( cipher_hd, buffer, buffer, nbytes );
-               mpi_set_buffer( cert->d.elg.x, buffer, nbytes, 0 );
-               csum = checksum_mpi( cert->d.elg.x );
-               m_free( buffer );
-               break;
-             case PUBKEY_ALGO_DSA:
-               buffer = mpi_get_secure_buffer( cert->d.dsa.x, &nbytes, NULL );
-               cipher_decrypt( cipher_hd, buffer, buffer, nbytes );
-               mpi_set_buffer( cert->d.dsa.x, buffer, nbytes, 0 );
-               csum = checksum_mpi( cert->d.dsa.x );
-               m_free( buffer );
-               break;
-           #ifdef HAVE_RSA_CIPHER
-             case PUBKEY_ALGO_RSA:
-             case PUBKEY_ALGO_RSA_E:
-             case PUBKEY_ALGO_RSA_S:
-               csum = 0;
-               #define X(a) do { \
-                   buffer = mpi_get_secure_buffer( cert->d.rsa.##a,     \
-                                                   &nbytes, NULL );     \
-                   csum += checksum_u16( nbytes*8 );                    \
-                   cipher_decrypt( cipher_hd, buffer, buffer, nbytes ); \
-                   csum += checksum( buffer, nbytes );                  \
-                   mpi_set_buffer(cert->d.rsa.##a, buffer, nbytes, 0 ); \
-                   m_free( buffer );                                    \
-                  } while(0)
-               X(d);
-               X(p);
-               X(q);
-               X(u);
-               #undef X
-               break;
-           #endif /* HAVE_RSA_CIPHER */
-
-             default: BUG();
-           }
-           cipher_close( cipher_hd );
-           /* now let's see whether we have used the right passphrase */
-           if( csum != cert->csum ) {
-               if( cert->pubkey_algo == PUBKEY_ALGO_ELGAMAL_E ) {
-                   /* very bad kludge to work around an early bug */
-                   csum -= checksum_u16( mpi_get_nbits(cert->d.elg.x) );
-                   nbytes = mpi_get_nlimbs(cert->d.elg.x) * 4;
-                   csum += checksum_u16( nbytes*8 );
-                   if( !opt.batch && csum == cert->csum )
-                       log_info("Probably you have an old key - use "
-                            "\"--change-passphrase\" to convert.\n");
-               }
-               if( csum != cert->csum ) {
-                   copy_secret_cert( cert, save_cert );
-                   free_secret_cert( save_cert );
-                   memcpy( cert->protect.iv, save_iv, 8 );
-                   return G10ERR_BAD_PASS;
-               }
-           }
-
-           switch( cert->pubkey_algo ) {
-             case PUBKEY_ALGO_ELGAMAL_E:
-             case PUBKEY_ALGO_ELGAMAL:
-               res = elg_check_secret_key( &cert->d.elg );
-               break;
-             case PUBKEY_ALGO_DSA:
-               res = dsa_check_secret_key( &cert->d.dsa );
-               break;
-           #ifdef HAVE_RSA_CIPHER
-             case PUBKEY_ALGO_RSA:
-             case PUBKEY_ALGO_RSA_E:
-             case PUBKEY_ALGO_RSA_S:
-               res = rsa_check_secret_key( &cert->d.rsa );
-               break;
-           #endif
-             default: BUG();
-           }
-           if( !res ) {
-               copy_secret_cert( cert, save_cert );
-               free_secret_cert( save_cert );
-               memcpy( cert->protect.iv, save_iv, 8 );
-               return G10ERR_BAD_PASS;
-           }
-           free_secret_cert( save_cert );
-           cert->is_protected = 0;
-           break;
-
-         default:
-           return G10ERR_CIPHER_ALGO; /* unsupported protection algorithm */
-       }
-    }
-    else { /* not protected */
-       switch( cert->pubkey_algo ) {
-         case PUBKEY_ALGO_ELGAMAL_E:
-         case PUBKEY_ALGO_ELGAMAL:
-           csum = checksum_mpi( cert->d.elg.x );
-           break;
-         case PUBKEY_ALGO_DSA:
-           csum = checksum_mpi( cert->d.dsa.x );
-           break;
-       #ifdef HAVE_RSA_CIPHER
-         case PUBKEY_ALGO_RSA_E:
-         case PUBKEY_ALGO_RSA_S:
-         case PUBKEY_ALGO_RSA:
-           csum =0;
-           buffer = mpi_get_buffer( cert->d.rsa.rsa_d, &nbytes, NULL );
-           csum += checksum_u16( nbytes*8 );
-           csum += checksum( buffer, nbytes );
-           m_free( buffer );
-           buffer = mpi_get_buffer( cert->d.rsa.rsa_p, &nbytes, NULL );
-           csum += checksum_u16( nbytes*8 );
-           csum += checksum( buffer, nbytes );
-           m_free( buffer );
-           buffer = mpi_get_buffer( cert->d.rsa.rsa_q, &nbytes, NULL );
-           csum += checksum_u16( nbytes*8 );
-           csum += checksum( buffer, nbytes );
-           m_free( buffer );
-           buffer = mpi_get_buffer( cert->d.rsa.rsa_u, &nbytes, NULL );
-           csum += checksum_u16( nbytes*8 );
-           csum += checksum( buffer, nbytes );
-           m_free( buffer );
-           break;
-       #endif
-         default: BUG();
+       if( sk->protect.s2k.mode == 1001 ) {
+           log_info(_("secret key parts are not available\n"));
+           return GPG_ERR_UNUSABLE_SECKEY;
        }
-       if( csum != cert->csum ) {
-           if( cert->pubkey_algo == PUBKEY_ALGO_ELGAMAL_E ) {
-               /* very bad kludge to work around an early bug */
-               csum -= checksum_u16( mpi_get_nbits(cert->d.elg.x) );
-               nbytes = mpi_get_nlimbs(cert->d.elg.x) * 4;
-               csum += checksum_u16( nbytes*8 );
-               if( !opt.batch && csum == cert->csum )
-                   log_info("Probably you have an old key - use "
-                        "\"--change-passphrase\" to convert.\n");
-           }
-           if( csum != cert->csum )
-               return G10ERR_CHECKSUM;
+       if( sk->protect.algo == CIPHER_ALGO_NONE )
+           BUG();
+       if( openpgp_cipher_test_algo( sk->protect.algo ) ) {
+           log_info(_("protection algorithm %d%s is not supported\n"),
+                       sk->protect.algo,sk->protect.algo==1?" (IDEA)":"" );
+           return GPG_ERR_CIPHER_ALGO;
        }
-    }
+       if(gcry_md_test_algo (sk->protect.s2k.hash_algo))
+         {
+           log_info(_("protection digest %d is not supported\n"),
+                    sk->protect.s2k.hash_algo);
+           return GPG_ERR_DIGEST_ALGO;
+         }
+       keyid_from_sk( sk, keyid );
+       keyid[2] = keyid[3] = 0;
+       if (!sk->flags.primary)
+          {
+            keyid[2] = sk->main_keyid[0];
+            keyid[3] = sk->main_keyid[1];
+          }
+       dek = passphrase_to_dek( keyid, sk->pubkey_algo, sk->protect.algo,
+                                &sk->protect.s2k, mode,
+                                 tryagain_text, canceled );
+        if (!dek && canceled && *canceled)
+           return GPG_ERR_CANCELED;
 
-    return 0;
-}
 
+       err = openpgp_cipher_open (&cipher_hd, sk->protect.algo,
+                                  GCRY_CIPHER_MODE_CFB,
+                                  (GCRY_CIPHER_SECURE
+                                   | (sk->protect.algo >= 100 ?
+                                      0 : GCRY_CIPHER_ENABLE_SYNC)));
+        if (err)
+          log_fatal ("cipher open failed: %s\n", gpg_strerror (err) );
 
+       err = gcry_cipher_setkey (cipher_hd, dek->key, dek->keylen);
+        if (err)
+          log_fatal ("set key failed: %s\n", gpg_strerror (err) );
 
-/****************
- * Check the secret key certificate
- * Ask up to 3 times for a correct passphrase
- */
-int
-check_secret_key( PKT_secret_cert *cert )
-{
-    int rc = G10ERR_BAD_PASS;
-    int i;
+       xfree(dek);
+       save_sk = copy_secret_key( NULL, sk );
 
-    for(i=0; i < 3 && rc == G10ERR_BAD_PASS; i++ ) {
-       if( i )
-           log_error("Invalid passphrase; please try again ...\n\n");
-       switch( cert->pubkey_algo ) {
-         case PUBKEY_ALGO_ELGAMAL_E:
-         case PUBKEY_ALGO_ELGAMAL:
-         case PUBKEY_ALGO_DSA:
-           rc = do_check( cert );
-         #if 1 /* set to 0 to disable the workaround */
-           if( rc == G10ERR_BAD_PASS && cert->is_protected
-               && cert->protect.algo == CIPHER_ALGO_BLOWFISH
-               && cert->pubkey_algo != PUBKEY_ALGO_ELGAMAL ) {
-               /* Workaround for a bug in 0.2.16 which still used
-                * a 160 bit key for BLOWFISH. */
-     log_info("trying workaround for 0.2.16 passphrase bug ...\n");
-     log_info("If you don't need this, uncomment it in g10/seckey-cert.c\n\n");
-               cert->protect.algo = CIPHER_ALGO_BLOWFISH160;
-               rc = do_check( cert );
-               if( rc )
-                   rc = G10ERR_BAD_PASS;
-               cert->protect.algo = CIPHER_ALGO_BLOWFISH;
-           }
-         #endif
-           break;
-         default: rc = G10ERR_PUBKEY_ALGO;
-       }
-       if( get_passphrase_fd() != -1 )
-           break;
-    }
-    return rc;
-}
+       gcry_cipher_setiv ( cipher_hd, sk->protect.iv, sk->protect.ivlen );
 
-/****************
- * check whether the secret key is protected.
- * Returns: 0 not protected, -1 on error or the protection algorithm
- */
-int
-is_secret_key_protected( PKT_secret_cert *cert )
-{
-    return cert->is_protected? cert->protect.algo : 0;
-}
+       csum = 0;
+       if( sk->version >= 4 ) {
+            int ndata;
+           unsigned int ndatabits;
+           byte *p, *data;
+            u16 csumc = 0;
 
+           i = pubkey_get_npkey(sk->pubkey_algo);
 
-static int
-do_protect( void (*fnc)(CIPHER_HANDLE, byte *, byte *, unsigned),
-           CIPHER_HANDLE fnc_hd, PKT_secret_cert *cert )
-{
-    byte *buffer;
-    unsigned nbytes;
+            assert ( gcry_mpi_get_flag (sk->skey[i], GCRYMPI_FLAG_OPAQUE ));
+            p = gcry_mpi_get_opaque ( sk->skey[i], &ndatabits );
+            ndata = (ndatabits+7)/8;
 
-    switch( cert->pubkey_algo ) {
-      case PUBKEY_ALGO_ELGAMAL_E:
-       /* recalculate the checksum, so that --change-passphrase
-        * can be used to convert from the faulty to the correct one
-        * wk 06.04.98:
-        * fixme: remove this some time in the future.
-        */
-       cert->csum = checksum_mpi( cert->d.elg.x );
-      case PUBKEY_ALGO_ELGAMAL:
-       buffer = mpi_get_buffer( cert->d.elg.x, &nbytes, NULL );
-       (*fnc)( fnc_hd, buffer, buffer, nbytes );
-       mpi_set_buffer( cert->d.elg.x, buffer, nbytes, 0 );
-       m_free( buffer );
-       break;
+            if ( ndata > 1 )
+              csumc = buf16_to_u16 (p+ndata-2);
+           data = xmalloc_secure ( ndata );
+           gcry_cipher_decrypt ( cipher_hd, data, ndata, p, ndata );
+           gcry_mpi_release (sk->skey[i]); sk->skey[i] = NULL ;
 
-      case PUBKEY_ALGO_DSA:
-       buffer = mpi_get_buffer( cert->d.dsa.x, &nbytes, NULL );
-       (*fnc)( fnc_hd, buffer, buffer, nbytes );
-       mpi_set_buffer( cert->d.dsa.x, buffer, nbytes, 0 );
-       m_free( buffer );
-       break;
+           p = data;
+            if (sk->protect.sha1chk) {
+                /* This is the new SHA1 checksum method to detect
+                   tampering with the key as used by the Klima/Rosa
+                   attack */
+                sk->csum = 0;
+                csum = 1;
+                if( ndata < 20 )
+                    log_error("not enough bytes for SHA-1 checksum\n");
+                else {
+                    gcry_md_hd_t h;
 
-      default: return G10ERR_PUBKEY_ALGO;
-    }
-    return 0;
-}
+                    if ( gcry_md_open (&h, DIGEST_ALGO_SHA1, 1))
+                        BUG(); /* Algo not available. */
+                    gcry_md_write (h, data, ndata - 20);
+                    gcry_md_final (h);
+                    if (!memcmp (gcry_md_read (h, DIGEST_ALGO_SHA1),
+                                 data + ndata - 20, 20) )
+                      {
+                        /* Digest does match.  We have to keep the old
+                           style checksum in sk->csum, so that the
+                           test used for unprotected keys does work.
+                           This test gets used when we are adding new
+                           keys. */
+                        sk->csum = csum = checksum (data, ndata-20);
+                      }
+                    gcry_md_close (h);
+                }
+            }
+            else {
+                if( ndata < 2 ) {
+                    log_error("not enough bytes for checksum\n");
+                    sk->csum = 0;
+                    csum = 1;
+                }
+                else {
+                    csum = checksum( data, ndata-2);
+                    sk->csum = data[ndata-2] << 8 | data[ndata-1];
+                    if ( sk->csum != csum ) {
+                        /* This is a PGP 7.0.0 workaround */
+                        sk->csum = csumc; /* take the encrypted one */
+                    }
+                }
+            }
 
+            /* Must check it here otherwise the mpi_read_xx would fail
+               because the length may have an arbitrary value */
+            if( sk->csum == csum ) {
+                for( ; i < pubkey_get_nskey(sk->pubkey_algo); i++ ) {
+                    if ( gcry_mpi_scan( &sk->skey[i], GCRYMPI_FMT_PGP,
+                                        p, ndata, &nbytes))
+                      {
+                        /* Checksum was okay, but not correctly
+                           decrypted.  */
+                        sk->csum = 0;
+                        csum = 1;
+                        break;
+                      }
+                    ndata -= nbytes;
+                    p += nbytes;
+                }
+                /* Note: at this point ndata should be 2 for a simple
+                   checksum or 20 for the sha1 digest */
+            }
+           xfree(data);
+       }
+       else {
+           for(i=pubkey_get_npkey(sk->pubkey_algo);
+                   i < pubkey_get_nskey(sk->pubkey_algo); i++ ) {
+                byte *p;
+                size_t ndata;
+                unsigned int ndatabits;
 
-/****************
- * Protect the secret key certificate with the passphrase from DEK
- */
-int
-protect_secret_key( PKT_secret_cert *cert, DEK *dek )
-{
-    int rc=0;
-
-    if( !dek )
-       return 0;
+                assert (gcry_mpi_get_flag (sk->skey[i], GCRYMPI_FLAG_OPAQUE));
+                p = gcry_mpi_get_opaque (sk->skey[i], &ndatabits);
+                ndata = (ndatabits+7)/8;
+                assert (ndata >= 2);
+                assert (ndata == ((p[0] << 8 | p[1]) + 7)/8 + 2);
+                buffer = xmalloc_secure (ndata);
+               gcry_cipher_sync (cipher_hd);
+                buffer[0] = p[0];
+                buffer[1] = p[1];
+                gcry_cipher_decrypt (cipher_hd, buffer+2, ndata-2,
+                                     p+2, ndata-2);
+                csum += checksum (buffer, ndata);
+                gcry_mpi_release (sk->skey[i]);
 
-    if( !cert->is_protected ) { /* okay, apply the protection */
-       CIPHER_HANDLE cipher_hd=NULL;
+               err = gcry_mpi_scan( &sk->skey[i], GCRYMPI_FMT_PGP,
+                                    buffer, ndata, &ndata );
+               xfree (buffer);
+                if (err)
+                  {
+                    /* Checksum was okay, but not correctly
+                       decrypted.  */
+                    sk->csum = 0;
+                    csum = 1;
+                    break;
+                  }
+/*             csum += checksum_mpi (sk->skey[i]); */
+           }
+       }
+       gcry_cipher_close ( cipher_hd );
 
-       switch( cert->protect.algo ) {
-         case CIPHER_ALGO_NONE: BUG(); break;
-         case CIPHER_ALGO_BLOWFISH160:
-         case CIPHER_ALGO_BLOWFISH:
-         case CIPHER_ALGO_CAST:
-           cipher_hd = cipher_open( cert->protect.algo,
-                                    CIPHER_MODE_AUTO_CFB, 1 );
-           cipher_setkey( cipher_hd, dek->key, dek->keylen );
-           cipher_setiv( cipher_hd, NULL );
-           cipher_encrypt( cipher_hd, cert->protect.iv, cert->protect.iv, 8 );
-           if( !do_protect( &cipher_encrypt, cipher_hd, cert ) )
-               cert->is_protected = 1;
-           cipher_close( cipher_hd );
-           break;
+       /* Now let's see whether we have used the correct passphrase. */
+       if( csum != sk->csum ) {
+           copy_secret_key( sk, save_sk );
+            passphrase_clear_cache ( keyid, NULL, sk->pubkey_algo );
+           free_secret_key( save_sk );
+           return gpg_error (GPG_ERR_BAD_PASSPHRASE);
+       }
 
-         default:
-           rc = G10ERR_CIPHER_ALGO; /* unsupport protection algorithm */
-           break;
+       /* The checksum may fail, so we also check the key itself. */
+       res = pk_check_secret_key ( sk->pubkey_algo, sk->skey );
+       if( res ) {
+           copy_secret_key( sk, save_sk );
+            passphrase_clear_cache ( keyid, NULL, sk->pubkey_algo );
+           free_secret_key( save_sk );
+           return gpg_error (GPG_ERR_BAD_PASSPHRASE);
        }
+       free_secret_key( save_sk );
+       sk->is_protected = 0;
+    }
+    else { /* not protected, assume it is okay if the checksum is okay */
+       csum = 0;
+       for(i=pubkey_get_npkey(sk->pubkey_algo);
+               i < pubkey_get_nskey(sk->pubkey_algo); i++ ) {
+           csum += checksum_mpi( sk->skey[i] );
+       }
+       if( csum != sk->csum )
+           return GPG_ERR_CHECKSUM;
     }
-    return rc;
-}
 
+    return 0;
+}