]> git.tue.mpg.de Git - paraslash.git/commitdiff
gcrypt: Factor out read_pem_rsa_params().
authorAndre Noll <maan@tuebingen.mpg.de>
Tue, 28 Aug 2018 18:50:53 +0000 (20:50 +0200)
committerAndre Noll <maan@tuebingen.mpg.de>
Tue, 25 Dec 2018 16:45:24 +0000 (17:45 +0100)
We'll soon add a similar function which reads bignums from an RFC4716
key file, so it's good to separate this functionality from the rest
of get_private_key().

The patch also introduces struct rsa_params which contains
the six bignums used for RSA private key operations. This way
read_pem_rsa_params() only needs three arguments.

gcrypt.c

index abc3272a6490ca541f1385c992c6ee99d5ef7986..58fa507b6e1f96303998e0f6ba0ae47f90beb493 100644 (file)
--- a/gcrypt.c
+++ b/gcrypt.c
@@ -256,6 +256,54 @@ static int read_bignum(unsigned char *start, unsigned char *end, gcry_mpi_t *bn,
        return cp - start;
 }
 
+struct rsa_params {
+       gcry_mpi_t n, e, d, p, q, u;
+};
+
+static int read_pem_rsa_params(unsigned char *start, unsigned char *end,
+               struct rsa_params *p)
+{
+       unsigned char *cp = start;
+       unsigned bits;
+       int ret;
+
+       ret = read_bignum(cp, end, &p->n, &bits);
+       if (ret < 0)
+               return ret;
+       cp += ret;
+       ret = read_bignum(cp, end, &p->e, NULL);
+       if (ret < 0)
+               goto release_n;
+       cp += ret;
+       ret = read_bignum(cp, end, &p->d, NULL);
+       if (ret < 0)
+               goto release_e;
+       cp += ret;
+       ret = read_bignum(cp, end, &p->p, NULL);
+       if (ret < 0)
+               goto release_d;
+       cp += ret;
+       ret = read_bignum(cp, end, &p->q, NULL);
+       if (ret < 0)
+               goto release_p;
+       cp += ret;
+       ret = read_bignum(cp, end, &p->u, NULL);
+       if (ret < 0)
+               goto release_q;
+       return bits;
+release_q:
+       gcry_mpi_release(p->q);
+release_p:
+       gcry_mpi_release(p->p);
+release_d:
+       gcry_mpi_release(p->d);
+release_e:
+       gcry_mpi_release(p->e);
+release_n:
+       gcry_mpi_release(p->n);
+       return ret;
+}
+
 static int find_privkey_bignum_offset(const unsigned char *data, int len)
 {
        const unsigned char *p = data, *end = data + len;
@@ -290,9 +338,8 @@ static int find_privkey_bignum_offset(const unsigned char *data, int len)
 
 static int get_private_key(const char *key_file, struct asymmetric_key **result)
 {
-       gcry_mpi_t n = NULL, e = NULL, d = NULL, p = NULL, q = NULL,
-               u = NULL;
-       unsigned char *blob, *cp, *end;
+       struct rsa_params params;
+       unsigned char *blob, *end;
        int ret;
        unsigned bits;
        gcry_error_t gret;
@@ -309,69 +356,40 @@ static int get_private_key(const char *key_file, struct asymmetric_key **result)
        if (ret < 0)
                goto free_blob;
        PARA_INFO_LOG("reading RSA params at offset %d\n", ret);
-       cp = blob + ret;
-
-       ret = read_bignum(cp, end, &n, &bits);
+       ret = read_pem_rsa_params(blob + ret, end, &params);
        if (ret < 0)
                goto free_blob;
-       cp += ret;
-
-       ret = read_bignum(cp, end, &e, NULL);
-       if (ret < 0)
-               goto release_n;
-       cp += ret;
-
-       ret = read_bignum(cp, end, &d, NULL);
-       if (ret < 0)
-               goto release_e;
-       cp += ret;
-
-       ret = read_bignum(cp, end, &p, NULL);
-       if (ret < 0)
-               goto release_d;
-       cp += ret;
-
-       ret = read_bignum(cp, end, &q, NULL);
-       if (ret < 0)
-               goto release_p;
-       cp += ret;
-       ret = read_bignum(cp, end, &u, NULL);
-       if (ret < 0)
-               goto release_q;
+       bits = ret;
        /*
         * OpenSSL uses slightly different parameters than gcrypt. To use these
         * parameters we need to swap the values of p and q and recompute u.
         */
-       if (gcry_mpi_cmp(pq) > 0) {
-               gcry_mpi_swap(pq);
-               gcry_mpi_invm(u, p, q);
+       if (gcry_mpi_cmp(params.p, params.q) > 0) {
+               gcry_mpi_swap(params.p, params.q);
+               gcry_mpi_invm(params.u, params.p, params.q);
        }
-       gret = gcry_sexp_build(&sexp, &erroff, RSA_PRIVKEY_SEXP,
-               n, e, d, p, q, u);
+       gret = gcry_sexp_build(&sexp, &erroff, RSA_PRIVKEY_SEXP, params.n,
+               params.e, params.d, params.p, params.q, params.u);
 
        if (gret) {
                PARA_ERROR_LOG("offset %zu: %s\n", erroff,
                        gcry_strerror(gcry_err_code(gret)));
                ret = -E_SEXP_BUILD;
-               goto release_u;
+               goto free_params;
        }
        key = para_malloc(sizeof(*key));
        key->sexp = sexp;
        *result = key;
        ret = bits;
        PARA_INFO_LOG("succesfully read %d bit private key\n", ret);
-release_u:
-       gcry_mpi_release(u);
-release_q:
-       gcry_mpi_release(q);
-release_p:
-       gcry_mpi_release(p);
-release_d:
-       gcry_mpi_release(d);
-release_e:
-       gcry_mpi_release(e);
-release_n:
-       gcry_mpi_release(n);
+free_params:
+       gcry_mpi_release(params.n);
+       gcry_mpi_release(params.e);
+       gcry_mpi_release(params.d);
+       gcry_mpi_release(params.u);
+       gcry_mpi_release(params.p);
+       gcry_mpi_release(params.q);
+
 free_blob:
        free(blob);
        return ret;