]> git.ipfire.org Git - thirdparty/gnutls.git/commitdiff
Make y parameter optional in gnutls_privkey_import_dsa_raw().
authorHans Leidekker <hans@codeweavers.com>
Tue, 3 Nov 2020 11:31:38 +0000 (12:31 +0100)
committerHans Leidekker <hans@codeweavers.com>
Tue, 3 Nov 2020 11:31:38 +0000 (12:31 +0100)
Signed-off-by: Hans Leidekker <hans@codeweavers.com>
lib/nettle/pk.c
lib/privkey_raw.c
lib/x509/privkey.c
tests/key-import-export.c

index 0c91aac493577bf492c622217b6c651bd3038500..952562c72bf86798589e8cbe497e21bde1b2efbb 100644 (file)
@@ -3338,6 +3338,27 @@ static int calc_rsa_priv(gnutls_pk_params_st * params)
        return 0;
 }
 
+static int calc_dsa_pub(gnutls_pk_params_st * params)
+{
+       int ret;
+
+       params->params[DSA_Y] = NULL;
+
+       ret = _gnutls_mpi_init(&params->params[DSA_Y]);
+       if (ret < 0)
+               return gnutls_assert_val(ret);
+
+       /* y = g^x mod p */
+       ret = _gnutls_mpi_powm(params->params[DSA_Y], params->params[DSA_G],
+                       params->params[DSA_X], params->params[DSA_P]);
+       if (ret < 0) {
+               zrelease_mpi_key(&params->params[DSA_Y]);
+               return gnutls_assert_val(ret);
+       }
+
+       return 0;
+}
+
 static int
 wrap_nettle_pk_fixup(gnutls_pk_algorithm_t algo,
                     gnutls_direction_t direction,
@@ -3433,7 +3454,13 @@ wrap_nettle_pk_fixup(gnutls_pk_algorithm_t algo,
                                                     params->spki.salt_size, pub_size,
                                                     GNUTLS_E_PK_INVALID_PUBKEY_PARAMS);
                }
-
+       } else if (algo == GNUTLS_PK_DSA) {
+               if (params->params[DSA_Y] == NULL) {
+                       ret = calc_dsa_pub(params);
+                       if (ret < 0)
+                               return gnutls_assert_val(ret);
+                       params->params_nr++;
+               }
        }
 #if ENABLE_GOST
        else if (algo == GNUTLS_PK_GOST_01 ||
index 27327fc6d129cdff19218e14dbae026bc7a76dc6..ba6d86d40ca2388944b95fe829f116dc9ed42480 100644 (file)
@@ -381,7 +381,7 @@ error:
  * @p: holds the p
  * @q: holds the q
  * @g: holds the g
- * @y: holds the y
+ * @y: holds the y (optional)
  * @x: holds the x
  *
  * This function will convert the given DSA raw parameters to the
index bb86e02ac83c1b66fa1793a527636105a0c1f325..1b3be77b8929d2e578fc47316a1111caf8fb86ec 100644 (file)
@@ -977,7 +977,7 @@ gnutls_x509_privkey_import_rsa_raw2(gnutls_x509_privkey_t key,
  * @p: holds the p
  * @q: holds the q
  * @g: holds the g
- * @y: holds the y
+ * @y: holds the y (optional)
  * @x: holds the x
  *
  * This function will convert the given DSA raw parameters to the
@@ -1026,11 +1026,13 @@ gnutls_x509_privkey_import_dsa_raw(gnutls_x509_privkey_t key,
                goto cleanup;
        }
 
-       siz = y->size;
-       if (_gnutls_mpi_init_scan_nz(&key->params.params[3], y->data, siz)) {
-               gnutls_assert();
-               ret = GNUTLS_E_MPI_SCAN_FAILED;
-               goto cleanup;
+       if (y) {
+               siz = y->size;
+               if (_gnutls_mpi_init_scan_nz(&key->params.params[3], y->data, siz)) {
+                       gnutls_assert();
+                       ret = GNUTLS_E_MPI_SCAN_FAILED;
+                       goto cleanup;
+               }
        }
 
        siz = x->size;
index fc6c25e6a70959116e38ad06691293e6aa9e1e0f..18de0fdc120643ed2cbd00224e594f9d91a208ba 100644 (file)
@@ -351,6 +351,31 @@ int check_privkey_import_export(void)
        gnutls_free(x.data);
        gnutls_privkey_deinit(key);
 
+       /* Optional y argument */
+       ret = gnutls_privkey_init(&key);
+       if (ret < 0)
+               fail("error\n");
+
+       ret = gnutls_privkey_import_dsa_raw(key, &_dsa_p, &_dsa_q, &_dsa_g, NULL, &_dsa_x);
+       if (ret < 0)
+               fail("error\n");
+
+       ret = gnutls_privkey_export_dsa_raw2(key, &p, &q, &g, &y, &x, 0);
+       if (ret < 0)
+               fail("error: %s\n", gnutls_strerror(ret));
+
+       CMP("p", &p, dsa_p);
+       CMP("q", &q, dsa_q);
+       CMP("g", &g, dsa_g);
+       CMP("y", &y, dsa_y);
+       CMP("x", &x, dsa_x);
+       gnutls_free(p.data);
+       gnutls_free(q.data);
+       gnutls_free(g.data);
+       gnutls_free(y.data);
+       gnutls_free(x.data);
+       gnutls_privkey_deinit(key);
+
        /* RSA */
 
        /* Optional arguments */