(pk_sign): Fix last change.
[gnupg.git] / g10 / pkglue.c
index 3e37882..abec77d 100644 (file)
@@ -48,6 +48,13 @@ pk_sign (int algo, gcry_mpi_t * data, gcry_mpi_t hash, gcry_mpi_t * skey)
                            "(private-key(dsa(p%m)(q%m)(g%m)(y%m)(x%m)))",
                            skey[0], skey[1], skey[2], skey[3], skey[4]);
     }
+  else if (algo == GCRY_PK_RSA)
+    {
+      rc = gcry_sexp_build (&s_skey, NULL,
+                           "(private-key(rsa(n%m)(e%m)(d%m)(p%m)(q%m)(u%m)))",
+                           skey[0], skey[1], skey[2], skey[3], skey[4],
+                           skey[5]);
+    }
   else if (algo == GCRY_PK_ELG || algo == GCRY_PK_ELG_E)
     {
       rc = gcry_sexp_build (&s_skey, NULL,
@@ -70,6 +77,14 @@ pk_sign (int algo, gcry_mpi_t * data, gcry_mpi_t hash, gcry_mpi_t * skey)
 
   if (rc)
     ;
+  else if (algo == GCRY_PK_RSA)
+    {
+      list = gcry_sexp_find_token (s_sig, "s", 0);
+      assert (list);
+      data[0] = gcry_sexp_nth_mpi (list, 1, 0);
+      assert (data[0]);
+      gcry_sexp_release (list);
+    }
   else
     {
       list = gcry_sexp_find_token (s_sig, "r", 0);
@@ -131,17 +146,26 @@ pk_verify (int algo, gcry_mpi_t hash, gcry_mpi_t * data, gcry_mpi_t * pkey)
   /* put data into a S-Exp s_sig */
   if (algo == GCRY_PK_DSA)
     {
-      rc = gcry_sexp_build (&s_sig, NULL,
-                           "(sig-val(dsa(r%m)(s%m)))", data[0], data[1]);
+      if (!data[0] || !data[1])
+        rc = gpg_error (GPG_ERR_BAD_MPI);
+      else
+        rc = gcry_sexp_build (&s_sig, NULL,
+                              "(sig-val(dsa(r%m)(s%m)))", data[0], data[1]);
     }
   else if (algo == GCRY_PK_ELG || algo == GCRY_PK_ELG_E)
     {
-      rc = gcry_sexp_build (&s_sig, NULL,
-                           "(sig-val(elg(r%m)(s%m)))", data[0], data[1]);
+      if (!data[0] || !data[1])
+        rc = gpg_error (GPG_ERR_BAD_MPI);
+      else
+        rc = gcry_sexp_build (&s_sig, NULL,
+                              "(sig-val(elg(r%m)(s%m)))", data[0], data[1]);
     }
   else if (algo == GCRY_PK_RSA)
     {
-      rc = gcry_sexp_build (&s_sig, NULL, "(sig-val(rsa(s%m)))", data[0]);
+      if (!data[0])
+        rc = gpg_error (GPG_ERR_BAD_MPI);
+      else
+        rc = gcry_sexp_build (&s_sig, NULL, "(sig-val(rsa(s%m)))", data[0]);
     }
   else
     BUG ();
@@ -177,6 +201,12 @@ pk_encrypt (int algo, gcry_mpi_t * resarr, gcry_mpi_t data, gcry_mpi_t * pkey)
                            "(public-key(elg(p%m)(g%m)(y%m)))",
                            pkey[0], pkey[1], pkey[2]);
     }
+  else if (algo == GCRY_PK_RSA)
+    {
+      rc = gcry_sexp_build (&s_pkey, NULL,
+                           "(public-key(rsa(n%m)(e%m)))",
+                           pkey[0], pkey[1]);
+    }
   else
     return GPG_ERR_PUBKEY_ALGO;
 
@@ -202,11 +232,14 @@ pk_encrypt (int algo, gcry_mpi_t * resarr, gcry_mpi_t data, gcry_mpi_t * pkey)
       assert (resarr[0]);
       gcry_sexp_release (list);
 
-      list = gcry_sexp_find_token (s_ciph, "b", 0);
-      assert (list);
-      resarr[1] = gcry_sexp_nth_mpi (list, 1, 0);
-      assert (resarr[1]);
-      gcry_sexp_release (list);
+      if (algo != GCRY_PK_RSA)
+        {
+          list = gcry_sexp_find_token (s_ciph, "b", 0);
+          assert (list);
+          resarr[1] = gcry_sexp_nth_mpi (list, 1, 0);
+          assert (resarr[1]);
+          gcry_sexp_release (list);
+        }
     }
 
   gcry_sexp_release (s_ciph);
@@ -250,12 +283,18 @@ pk_decrypt (int algo, gcry_mpi_t * result, gcry_mpi_t * data,
   /* put data into a S-Exp s_data */
   if (algo == GCRY_PK_ELG || algo == GCRY_PK_ELG_E)
     {
-      rc = gcry_sexp_build (&s_data, NULL,
-                           "(enc-val(elg(a%m)(b%m)))", data[0], data[1]);
+      if (!data[0] || !data[1])
+        rc = gpg_error (GPG_ERR_BAD_MPI);
+      else
+        rc = gcry_sexp_build (&s_data, NULL,
+                              "(enc-val(elg(a%m)(b%m)))", data[0], data[1]);
     }
   else if (algo == GCRY_PK_RSA)
     {
-      rc = gcry_sexp_build (&s_data, NULL, "(enc-val(rsa(a%m)))", data[0]);
+      if (!data[0])
+        rc = gpg_error (GPG_ERR_BAD_MPI);
+      else
+        rc = gcry_sexp_build (&s_data, NULL, "(enc-val(rsa(a%m)))", data[0]);
     }
   else
     BUG ();
@@ -276,3 +315,41 @@ pk_decrypt (int algo, gcry_mpi_t * result, gcry_mpi_t * data,
 
   return 0;
 }
+
+
+/* Check whether SKEY is a suitable secret key. */
+int
+pk_check_secret_key (int algo, gcry_mpi_t *skey)
+{
+  gcry_sexp_t s_skey;
+  int rc;
+
+  if (algo == GCRY_PK_DSA)
+    {
+      rc = gcry_sexp_build (&s_skey, NULL,
+                           "(private-key(dsa(p%m)(q%m)(g%m)(y%m)(x%m)))",
+                           skey[0], skey[1], skey[2], skey[3], skey[4]);
+    }
+  else if (algo == GCRY_PK_ELG || algo == GCRY_PK_ELG_E)
+    {
+      rc = gcry_sexp_build (&s_skey, NULL,
+                           "(private-key(elg(p%m)(g%m)(y%m)(x%m)))",
+                           skey[0], skey[1], skey[2], skey[3]);
+    }
+  else if (algo == GCRY_PK_RSA)
+    {
+      rc = gcry_sexp_build (&s_skey, NULL,
+                           "(private-key(rsa(n%m)(e%m)(d%m)(p%m)(q%m)(u%m)))",
+                           skey[0], skey[1], skey[2], skey[3], skey[4],
+                           skey[5]);
+    }
+  else
+    return GPG_ERR_PUBKEY_ALGO;
+
+  if (!rc)
+    {
+      rc = gcry_pk_testkey (s_skey);
+      gcry_sexp_release (s_skey);
+    }
+  return rc;
+}