chiark / gitweb /
gnupg2 (2.1.18-8~deb9u1) stretch; urgency=medium
[gnupg2.git] / debian / patches / 0043-gpg-Allow-creating-keys-using-an-existing-ECC-key.patch
1 From: Werner Koch <wk@gnupg.org>
2 Date: Wed, 1 Mar 2017 13:36:01 +0100
3 Subject: gpg: Allow creating keys using an existing ECC key.
4
5 * common/sexputil.c (get_pk_algo_from_canon_sexp): Remove arg R_ALGO.
6 Change to return the algo id.  Reimplement using get_pk_algo_from_key.
7 * g10/keygen.c (check_keygrip): Adjust for change.
8 * sm/certreqgen-ui.c (check_keygrip): Ditto.
9 --
10
11 GnuPG-bug-id: 2976
12 Signed-off-by: Werner Koch <wk@gnupg.org>
13 (cherry picked from commit 2bbdeb8ee87a6c7ec211be16391a11b7c6030bed)
14 ---
15  common/sexputil.c  | 65 +++++++++++++++---------------------------------------
16  common/util.h      |  6 ++---
17  g10/keygen.c       | 22 +++---------------
18  sm/certreqgen-ui.c | 24 +++++++++-----------
19  4 files changed, 34 insertions(+), 83 deletions(-)
20
21 diff --git a/common/sexputil.c b/common/sexputil.c
22 index 0c5c730..a8dc1a5 100644
23 --- a/common/sexputil.c
24 +++ b/common/sexputil.c
25 @@ -512,53 +512,6 @@ get_rsa_pk_from_canon_sexp (const unsigned char *keydata, size_t keydatalen,
26  }
27  
28  
29 -/* Return the algo of a public RSA expressed as an canonical encoded
30 -   S-expression.  The return value is a statically allocated
31 -   string.  On error that string is set to NULL. */
32 -gpg_error_t
33 -get_pk_algo_from_canon_sexp (const unsigned char *keydata, size_t keydatalen,
34 -                             const char **r_algo)
35 -{
36 -  gpg_error_t err;
37 -  const unsigned char *buf, *tok;
38 -  size_t buflen, toklen;
39 -  int depth;
40 -
41 -  *r_algo = NULL;
42 -
43 -  buf = keydata;
44 -  buflen = keydatalen;
45 -  depth = 0;
46 -  if ((err = parse_sexp (&buf, &buflen, &depth, &tok, &toklen)))
47 -    return err;
48 -  if ((err = parse_sexp (&buf, &buflen, &depth, &tok, &toklen)))
49 -    return err;
50 -  if (!tok || toklen != 10 || memcmp ("public-key", tok, toklen))
51 -    return gpg_error (GPG_ERR_BAD_PUBKEY);
52 -  if ((err = parse_sexp (&buf, &buflen, &depth, &tok, &toklen)))
53 -    return err;
54 -  if ((err = parse_sexp (&buf, &buflen, &depth, &tok, &toklen)))
55 -    return err;
56 -  if (!tok)
57 -    return gpg_error (GPG_ERR_BAD_PUBKEY);
58 -
59 -  if (toklen == 3 && !memcmp ("rsa", tok, toklen))
60 -    *r_algo = "rsa";
61 -  else if (toklen == 3 && !memcmp ("dsa", tok, toklen))
62 -    *r_algo = "dsa";
63 -  else if (toklen == 3 && !memcmp ("elg", tok, toklen))
64 -    *r_algo = "elg";
65 -  else if (toklen == 5 && !memcmp ("ecdsa", tok, toklen))
66 -    *r_algo = "ecdsa";
67 -  else if (toklen == 5 && !memcmp ("eddsa", tok, toklen))
68 -    *r_algo = "eddsa";
69 -  else
70 -    return gpg_error (GPG_ERR_PUBKEY_ALGO);
71 -
72 -  return 0;
73 -}
74 -
75 -
76  /* Return the algo of a public KEY of SEXP. */
77  int
78  get_pk_algo_from_key (gcry_sexp_t key)
79 @@ -606,3 +559,21 @@ get_pk_algo_from_key (gcry_sexp_t key)
80  
81    return algo;
82  }
83 +
84 +
85 +/* This is a variant of get_pk_algo_from_key but takes an canonical
86 + * encoded S-expression as input.  Returns a GCRYPT public key
87 + * identiier or 0 on error.  */
88 +int
89 +get_pk_algo_from_canon_sexp (const unsigned char *keydata, size_t keydatalen)
90 +{
91 +  gcry_sexp_t sexp;
92 +  int algo;
93 +
94 +  if (gcry_sexp_sscan (&sexp, NULL, keydata, keydatalen))
95 +    return 0;
96 +
97 +  algo = get_pk_algo_from_key (sexp);
98 +  gcry_sexp_release (sexp);
99 +  return algo;
100 +}
101 diff --git a/common/util.h b/common/util.h
102 index f7a53e1..b6d7156 100644
103 --- a/common/util.h
104 +++ b/common/util.h
105 @@ -195,10 +195,10 @@ gpg_error_t get_rsa_pk_from_canon_sexp (const unsigned char *keydata,
106                                          size_t *r_nlen,
107                                          unsigned char const **r_e,
108                                          size_t *r_elen);
109 -gpg_error_t get_pk_algo_from_canon_sexp (const unsigned char *keydata,
110 -                                         size_t keydatalen,
111 -                                         const char **r_algo);
112 +
113  int get_pk_algo_from_key (gcry_sexp_t key);
114 +int get_pk_algo_from_canon_sexp (const unsigned char *keydata,
115 +                                 size_t keydatalen);
116  
117  /*-- convert.c --*/
118  int hex2bin (const char *string, void *buffer, size_t length);
119 diff --git a/g10/keygen.c b/g10/keygen.c
120 index 98ef29e..0180581 100644
121 --- a/g10/keygen.c
122 +++ b/g10/keygen.c
123 @@ -1838,7 +1838,7 @@ check_keygrip (ctrl_t ctrl, const char *hexgrip)
124    gpg_error_t err;
125    unsigned char *public;
126    size_t publiclen;
127 -  const char *algostr;
128 +  int algo;
129  
130    if (hexgrip[0] == '&')
131      hexgrip++;
132 @@ -1848,26 +1848,10 @@ check_keygrip (ctrl_t ctrl, const char *hexgrip)
133      return 0;
134    publiclen = gcry_sexp_canon_len (public, 0, NULL, NULL);
135  
136 -  get_pk_algo_from_canon_sexp (public, publiclen, &algostr);
137 +  algo = get_pk_algo_from_canon_sexp (public, publiclen);
138    xfree (public);
139  
140 -  /* FIXME: Mapping of ECC algorithms is probably not correct. */
141 -  if (!algostr)
142 -    return 0;
143 -  else if (!strcmp (algostr, "rsa"))
144 -    return PUBKEY_ALGO_RSA;
145 -  else if (!strcmp (algostr, "dsa"))
146 -    return PUBKEY_ALGO_DSA;
147 -  else if (!strcmp (algostr, "elg"))
148 -    return PUBKEY_ALGO_ELGAMAL_E;
149 -  else if (!strcmp (algostr, "ecc"))
150 -    return PUBKEY_ALGO_ECDH;
151 -  else if (!strcmp (algostr, "ecdsa"))
152 -    return PUBKEY_ALGO_ECDSA;
153 -  else if (!strcmp (algostr, "eddsa"))
154 -    return PUBKEY_ALGO_EDDSA;
155 -  else
156 -    return 0;
157 +  return map_pk_gcry_to_openpgp (algo);
158  }
159  
160  
161 diff --git a/sm/certreqgen-ui.c b/sm/certreqgen-ui.c
162 index ece8668..b50d338 100644
163 --- a/sm/certreqgen-ui.c
164 +++ b/sm/certreqgen-ui.c
165 @@ -95,7 +95,7 @@ check_keygrip (ctrl_t ctrl, const char *hexgrip)
166    gpg_error_t err;
167    ksba_sexp_t public;
168    size_t publiclen;
169 -  const char *algostr;
170 +  int algo;
171  
172    if (hexgrip[0] == '&')
173      hexgrip++;
174 @@ -105,21 +105,17 @@ check_keygrip (ctrl_t ctrl, const char *hexgrip)
175      return NULL;
176    publiclen = gcry_sexp_canon_len (public, 0, NULL, NULL);
177  
178 -  get_pk_algo_from_canon_sexp (public, publiclen, &algostr);
179 +  algo = get_pk_algo_from_canon_sexp (public, publiclen);
180    xfree (public);
181  
182 -  if (!algostr)
183 -    return NULL;
184 -  else if (!strcmp (algostr, "rsa"))
185 -    return "RSA";
186 -  else if (!strcmp (algostr, "dsa"))
187 -    return "DSA";
188 -  else if (!strcmp (algostr, "elg"))
189 -    return "ELG";
190 -  else if (!strcmp (algostr, "ecdsa"))
191 -    return "ECDSA";
192 -  else
193 -    return NULL;
194 +  switch (algo)
195 +    {
196 +    case GCRY_PK_RSA:   return "RSA";
197 +    case GCRY_PK_DSA:   return "DSA";
198 +    case GCRY_PK_ELG:   return "ELG";
199 +    case GCRY_PK_EDDSA: return "ECDSA";
200 +    default: return NULL;
201 +    }
202  }
203  
204