Imported Upstream version 2.3.8
[platform/upstream/gpg2.git] / common / ssh-utils.c
1 /* ssh-utils.c - Secure Shell helper functions
2  * Copyright (C) 2011 Free Software Foundation, Inc.
3  *
4  * This file is part of GnuPG.
5  *
6  * This file is free software; you can redistribute it and/or modify
7  * it under the terms of either
8  *
9  *   - the GNU Lesser General Public License as published by the Free
10  *     Software Foundation; either version 3 of the License, or (at
11  *     your option) any later version.
12  *
13  * or
14  *
15  *   - the GNU General Public License as published by the Free
16  *     Software Foundation; either version 2 of the License, or (at
17  *     your option) any later version.
18  *
19  * or both in parallel, as here.
20  *
21  * This file is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  * GNU General Public License for more details.
25  *
26  * You should have received a copy of the GNU General Public License
27  * along with this program; if not, see <https://www.gnu.org/licenses/>.
28  */
29
30 #include <config.h>
31 #include <stdlib.h>
32 #include <errno.h>
33 #include <ctype.h>
34 #include <assert.h>
35
36 #include "util.h"
37 #include "ssh-utils.h"
38 #include "openpgpdefs.h"
39
40
41 /* Return true if KEYPARMS holds an EdDSA key.  */
42 static int
43 is_eddsa (gcry_sexp_t keyparms)
44 {
45   int result = 0;
46   gcry_sexp_t list;
47   const char *s;
48   size_t n;
49   int i;
50
51   list = gcry_sexp_find_token (keyparms, "flags", 0);
52   for (i = list ? gcry_sexp_length (list)-1 : 0; i > 0; i--)
53     {
54       s = gcry_sexp_nth_data (list, i, &n);
55       if (!s)
56         continue; /* Not a data element. */
57
58       if (n == 5 && !memcmp (s, "eddsa", 5))
59         {
60           result = 1;
61           break;
62         }
63     }
64   gcry_sexp_release (list);
65   return result;
66 }
67
68 /* Dummy functions for es_mopen.  */
69 static void *dummy_realloc (void *mem, size_t size) { (void) size; return mem; }
70 static void dummy_free (void *mem) { (void) mem; }
71
72 /* Return the Secure Shell type fingerprint for KEY using digest ALGO.
73    The length of the fingerprint is returned at R_LEN and the
74    fingerprint itself at R_FPR.  In case of a error code is returned
75    and NULL stored at R_FPR.  */
76 static gpg_error_t
77 get_fingerprint (gcry_sexp_t key, int algo,
78                  void **r_fpr, size_t *r_len, int as_string)
79 {
80   gpg_error_t err;
81   gcry_sexp_t list = NULL;
82   gcry_sexp_t l2 = NULL;
83   const char *s;
84   char *name = NULL;
85   int idx;
86   const char *elems;
87   gcry_md_hd_t md = NULL;
88   int blobmode = 0;
89
90   *r_fpr = NULL;
91   *r_len = 0;
92
93   /* Check that the first element is valid. */
94   list = gcry_sexp_find_token (key, "public-key", 0);
95   if (!list)
96     list = gcry_sexp_find_token (key, "private-key", 0);
97   if (!list)
98     list = gcry_sexp_find_token (key, "protected-private-key", 0);
99   if (!list)
100     list = gcry_sexp_find_token (key, "shadowed-private-key", 0);
101   if (!list)
102     {
103       err = gpg_err_make (default_errsource, GPG_ERR_UNKNOWN_SEXP);
104       goto leave;
105     }
106
107   l2 = gcry_sexp_cadr (list);
108   gcry_sexp_release (list);
109   list = l2;
110   l2 = NULL;
111
112   name = gcry_sexp_nth_string (list, 0);
113   if (!name)
114     {
115       err = gpg_err_make (default_errsource, GPG_ERR_INV_SEXP);
116       goto leave;
117     }
118
119   err = gcry_md_open (&md, algo, 0);
120   if (err)
121     goto leave;
122
123   switch (gcry_pk_map_name (name))
124     {
125     case GCRY_PK_RSA:
126       elems = "en";
127       gcry_md_write (md, "\0\0\0\x07ssh-rsa", 11);
128       break;
129
130     case GCRY_PK_DSA:
131       elems = "pqgy";
132       gcry_md_write (md, "\0\0\0\x07ssh-dss", 11);
133       break;
134
135     case GCRY_PK_ECC:
136       if (is_eddsa (list))
137         {
138           elems = "q";
139           blobmode = 1;
140           /* For now there is just one curve, thus no need to switch
141              on it.  */
142           gcry_md_write (md, "\0\0\0\x0b" "ssh-ed25519", 15);
143         }
144       else
145         {
146           /* We only support the 3 standard curves for now.  It is
147              just a quick hack.  */
148           elems = "q";
149           gcry_md_write (md, "\0\0\0\x13" "ecdsa-sha2-nistp", 20);
150           l2 = gcry_sexp_find_token (list, "curve", 0);
151           if (!l2)
152             elems = "";
153           else
154             {
155               gcry_free (name);
156               name = gcry_sexp_nth_string (l2, 1);
157               gcry_sexp_release (l2);
158               l2 = NULL;
159               if (!name)
160                 elems = "";
161               else if (!strcmp (name, "NIST P-256")||!strcmp (name, "nistp256"))
162                 gcry_md_write (md, "256\0\0\0\x08nistp256", 15);
163               else if (!strcmp (name, "NIST P-384")||!strcmp (name, "nistp384"))
164                 gcry_md_write (md, "384\0\0\0\x08nistp384", 15);
165               else if (!strcmp (name, "NIST P-521")||!strcmp (name, "nistp521"))
166                 gcry_md_write (md, "521\0\0\0\x08nistp521", 15);
167               else
168                 elems = "";
169             }
170           if (!*elems)
171             err = gpg_err_make (default_errsource, GPG_ERR_UNKNOWN_CURVE);
172         }
173       break;
174
175     default:
176       elems = "";
177       err = gpg_err_make (default_errsource, GPG_ERR_PUBKEY_ALGO);
178       break;
179     }
180   if (err)
181     goto leave;
182
183
184   for (idx = 0, s = elems; *s; s++, idx++)
185     {
186       l2 = gcry_sexp_find_token (list, s, 1);
187       if (!l2)
188         {
189           err = gpg_err_make (default_errsource, GPG_ERR_INV_SEXP);
190           goto leave;
191         }
192       if (blobmode)
193         {
194           const char *blob;
195           size_t bloblen;
196           unsigned char lenbuf[4];
197
198           blob = gcry_sexp_nth_data (l2, 1, &bloblen);
199           if (!blob)
200             {
201               err = gpg_err_make (default_errsource, GPG_ERR_INV_SEXP);
202               goto leave;
203             }
204           blob++;
205           bloblen--;
206           lenbuf[0] = bloblen >> 24;
207           lenbuf[1] = bloblen >> 16;
208           lenbuf[2] = bloblen >>  8;
209           lenbuf[3] = bloblen;
210           gcry_md_write (md, lenbuf, 4);
211           gcry_md_write (md, blob, bloblen);
212         }
213       else
214         {
215           gcry_mpi_t a;
216           unsigned char *buf;
217           size_t buflen;
218
219           a = gcry_sexp_nth_mpi (l2, 1, GCRYMPI_FMT_USG);
220           gcry_sexp_release (l2);
221           l2 = NULL;
222           if (!a)
223             {
224               err = gpg_err_make (default_errsource, GPG_ERR_INV_SEXP);
225               goto leave;
226             }
227
228           err = gcry_mpi_aprint (GCRYMPI_FMT_SSH, &buf, &buflen, a);
229           gcry_mpi_release (a);
230           if (err)
231             goto leave;
232           gcry_md_write (md, buf, buflen);
233           gcry_free (buf);
234         }
235     }
236
237   if (as_string)
238     {
239       const char *algo_name;
240       char *fpr;
241
242       /* Prefix string with the algorithm name and a colon.  */
243       algo_name = gcry_md_algo_name (algo);
244       *r_fpr = xtrymalloc (strlen (algo_name) + 1 + 3 * gcry_md_get_algo_dlen (algo) + 1);
245       if (*r_fpr == NULL)
246         {
247           err = gpg_err_make (default_errsource, gpg_err_code_from_syserror ());
248           goto leave;
249         }
250
251       memcpy (*r_fpr, algo_name, strlen (algo_name));
252       fpr = (char *) *r_fpr + strlen (algo_name);
253       *fpr++ = ':';
254
255       if (algo == GCRY_MD_MD5)
256         {
257           bin2hexcolon (gcry_md_read (md, algo), gcry_md_get_algo_dlen (algo), fpr);
258           strlwr (fpr);
259         }
260       else
261         {
262           struct b64state b64s;
263           estream_t stream;
264           char *p;
265           long int len;
266
267           /* Write the base64-encoded hash to fpr.  */
268           stream = es_mopen (fpr, 3 * gcry_md_get_algo_dlen (algo) + 1, 0,
269                              0, dummy_realloc, dummy_free, "w");
270           if (stream == NULL)
271             {
272               err = gpg_err_make (default_errsource, gpg_err_code_from_syserror ());
273               goto leave;
274             }
275
276           err = b64enc_start_es (&b64s, stream, "");
277           if (err)
278             {
279               es_fclose (stream);
280               goto leave;
281             }
282
283           err = b64enc_write (&b64s,
284                               gcry_md_read (md, algo), gcry_md_get_algo_dlen (algo));
285           if (err)
286             {
287               es_fclose (stream);
288               goto leave;
289             }
290
291           /* Finish, get the length, and close the stream.  */
292           err = b64enc_finish (&b64s);
293           len = es_ftell (stream);
294           es_fclose (stream);
295           if (err)
296             goto leave;
297
298           /* Terminate.  */
299           fpr[len] = 0;
300
301           /* Strip the trailing padding characters.  */
302           for (p = fpr + len - 1; p > fpr && *p == '='; p--)
303             *p = 0;
304         }
305
306       *r_len = strlen (*r_fpr) + 1;
307     }
308   else
309     {
310       *r_len = gcry_md_get_algo_dlen (algo);
311       *r_fpr = xtrymalloc (*r_len);
312       if (!*r_fpr)
313         {
314           err = gpg_err_make (default_errsource, gpg_err_code_from_syserror ());
315           goto leave;
316         }
317       memcpy (*r_fpr, gcry_md_read (md, algo), *r_len);
318     }
319   err = 0;
320
321  leave:
322   gcry_free (name);
323   gcry_sexp_release (l2);
324   gcry_md_close (md);
325   gcry_sexp_release (list);
326   return err;
327 }
328
329 /* Return the Secure Shell type fingerprint for KEY using digest ALGO.
330    The length of the fingerprint is returned at R_LEN and the
331    fingerprint itself at R_FPR.  In case of an error an error code is
332    returned and NULL stored at R_FPR.  */
333 gpg_error_t
334 ssh_get_fingerprint (gcry_sexp_t key, int algo,
335                      void **r_fpr, size_t *r_len)
336 {
337   return get_fingerprint (key, algo, r_fpr, r_len, 0);
338 }
339
340
341 /* Return the Secure Shell type fingerprint for KEY using digest ALGO
342    as a string.  The fingerprint is mallcoed and stored at R_FPRSTR.
343    In case of an error an error code is returned and NULL stored at
344    R_FPRSTR.  */
345 gpg_error_t
346 ssh_get_fingerprint_string (gcry_sexp_t key, int algo, char **r_fprstr)
347 {
348   gpg_error_t err;
349   size_t dummy;
350   void *string;
351
352   err = get_fingerprint (key, algo, &string, &dummy, 1);
353   *r_fprstr = string;
354   return err;
355 }
356
357
358 /* Write the uint32 contained in UINT32 to STREAM.  */
359 static gpg_error_t
360 stream_write_uint32 (estream_t stream, u32 uint32)
361 {
362   unsigned char buffer[4];
363   gpg_error_t err;
364   int ret;
365
366   buffer[0] = uint32 >> 24;
367   buffer[1] = uint32 >> 16;
368   buffer[2] = uint32 >>  8;
369   buffer[3] = uint32 >>  0;
370
371   ret = es_write (stream, buffer, sizeof (buffer), NULL);
372   if (ret)
373     err = gpg_error_from_syserror ();
374   else
375     err = 0;
376
377   return err;
378 }
379
380 /* Write SIZE bytes from BUFFER to STREAM.  */
381 static gpg_error_t
382 stream_write_data (estream_t stream, const unsigned char *buffer, size_t size)
383 {
384   gpg_error_t err;
385   int ret;
386
387   ret = es_write (stream, buffer, size, NULL);
388   if (ret)
389     err = gpg_error_from_syserror ();
390   else
391     err = 0;
392
393   return err;
394 }
395
396 /* Write a binary string from STRING of size STRING_N to STREAM.  */
397 static gpg_error_t
398 stream_write_string (estream_t stream,
399                      const unsigned char *string, u32 string_n)
400 {
401   gpg_error_t err;
402
403   err = stream_write_uint32 (stream, string_n);
404   if (err)
405     goto out;
406
407   err = stream_write_data (stream, string, string_n);
408
409  out:
410
411   return err;
412 }
413
414 /* Write a C-string from STRING to STREAM.  */
415 static gpg_error_t
416 stream_write_cstring (estream_t stream, const char *string)
417 {
418   gpg_error_t err;
419
420   err = stream_write_string (stream,
421                              (const unsigned char *) string, strlen (string));
422
423   return err;
424 }
425
426 /* Write the MPI contained in MPINT to STREAM.  */
427 static gpg_error_t
428 stream_write_mpi (estream_t stream, gcry_mpi_t mpint)
429 {
430   unsigned char *mpi_buffer;
431   size_t mpi_buffer_n;
432   gpg_error_t err;
433
434   mpi_buffer = NULL;
435
436   err = gcry_mpi_aprint (GCRYMPI_FMT_STD, &mpi_buffer, &mpi_buffer_n, mpint);
437   if (err)
438     goto out;
439
440   err = stream_write_string (stream, mpi_buffer, mpi_buffer_n);
441
442  out:
443
444   xfree (mpi_buffer);
445
446   return err;
447 }
448
449
450 /* Encode a key in SEXP, in SSH format.  */
451 static gpg_error_t
452 sexp_to_sshblob (gcry_sexp_t sexp, const char *identifier, int is_eddsa_flag,
453                  const char *curve, const char *elems,
454                  void **r_blob, size_t *r_blob_size)
455 {
456   gpg_error_t err = 0;
457   gcry_sexp_t value_list = NULL;
458   gcry_sexp_t value_pair = NULL;
459   estream_t stream = NULL;
460   const char *p_elems;
461   const char *data;
462   size_t datalen;
463
464   *r_blob = NULL;
465   *r_blob_size = 0;
466
467   stream = es_fopenmem (0, "r+b");
468   if (!stream)
469     {
470       err = gpg_error_from_syserror ();
471       goto out;
472     }
473
474   /* Get key value list.  */
475   value_list = gcry_sexp_cadr (sexp);
476   if (!value_list)
477     {
478       err = gpg_error (GPG_ERR_INV_SEXP);
479       goto out;
480     }
481
482   err = stream_write_cstring (stream, identifier);
483   if (err)
484     goto out;
485
486   if (curve && !is_eddsa_flag)
487     {
488       /* ECDSA requires the curve name.  */
489       err = stream_write_cstring (stream, curve);
490       if (err)
491         goto out;
492     }
493
494   /* Write the parameters.  */
495   for (p_elems = elems; *p_elems; p_elems++)
496     {
497       gcry_sexp_release (value_pair);
498       value_pair = gcry_sexp_find_token (value_list, p_elems, 1);
499       if (!value_pair)
500         {
501           err = gpg_error (GPG_ERR_INV_SEXP);
502           goto out;
503         }
504       if (is_eddsa_flag)
505         {
506           data = gcry_sexp_nth_data (value_pair, 1, &datalen);
507           if (!data)
508             {
509               err = gpg_error (GPG_ERR_INV_SEXP);
510               goto out;
511             }
512           if ((datalen & 1) && *data == 0x40)
513             { /* Remove the prefix 0x40.  */
514               data++;
515               datalen--;
516             }
517           err = stream_write_string (stream, data, datalen);
518           if (err)
519             goto out;
520         }
521       else
522         {
523           gcry_mpi_t mpi;
524
525           /* Note that we need to use STD format; i.e. prepend a 0x00
526              to indicate a positive number if the high bit is set.  */
527           mpi = gcry_sexp_nth_mpi (value_pair, 1, GCRYMPI_FMT_STD);
528           if (!mpi)
529             {
530               err = gpg_error (GPG_ERR_INV_SEXP);
531               goto out;
532             }
533           err = stream_write_mpi (stream, mpi);
534           gcry_mpi_release (mpi);
535           if (err)
536             goto out;
537         }
538     }
539
540   if (es_fclose_snatch (stream, r_blob, r_blob_size))
541     {
542       err = gpg_error_from_syserror ();
543       goto out;
544     }
545   stream = NULL;
546
547  out:
548   gcry_sexp_release (value_list);
549   gcry_sexp_release (value_pair);
550   es_fclose (stream);
551
552   return err;
553 }
554
555 /* For KEY in S-expression, write it in SSH base64 format to STREAM,
556    adding COMMENT.  */
557 gpg_error_t
558 ssh_public_key_in_base64 (gcry_sexp_t key, estream_t stream,
559                           const char *comment)
560 {
561   gpg_error_t err = 0;
562   int algo;
563   int is_eddsa_flag = 0;
564   const char *curve = NULL;
565   const char *pub_elements = NULL;
566   const char *identifier = NULL;
567   void *blob = NULL;
568   size_t bloblen;
569   struct b64state b64_state;
570
571   algo = get_pk_algo_from_key (key);
572   if (algo == 0)
573     return gpg_error (GPG_ERR_PUBKEY_ALGO);
574
575   if (algo == GCRY_PK_ECC || algo == GCRY_PK_EDDSA)
576     {
577       curve = gcry_pk_get_curve (key, 0, NULL);
578       if (!curve)
579         return gpg_error (GPG_ERR_INV_CURVE);
580     }
581
582   switch (algo)
583     {
584     case GCRY_PK_RSA:
585       identifier = "ssh-rsa";
586       pub_elements = "en";
587       break;
588
589     case GCRY_PK_ECC:
590       if (!strcmp (curve, "NIST P-256"))
591         identifier = "ecdsa-sha2-nistp256";
592       else if (!strcmp (curve, "NIST P-384"))
593         identifier = "ecdsa-sha2-nistp384";
594       else if (!strcmp (curve, "NIST P-521"))
595         identifier = "ecdsa-sha2-nistp521";
596       else
597         err = gpg_error (GPG_ERR_UNKNOWN_CURVE);
598       pub_elements = "q";
599       break;
600
601     case GCRY_PK_EDDSA:
602       is_eddsa_flag = 1;
603       if (!strcmp (curve, "Ed25519"))
604         identifier = "ssh-ed25519";
605       else if (!strcmp (curve, "Ed448"))
606         identifier = "ssh-ed448";
607       else
608         err = gpg_error (GPG_ERR_UNKNOWN_CURVE);
609       pub_elements = "q";
610       break;
611
612     default:
613       err = gpg_error (GPG_ERR_PUBKEY_ALGO);
614       break;
615     }
616
617   if (err)
618     return err;
619
620   err = sexp_to_sshblob (key, identifier, is_eddsa_flag, curve, pub_elements,
621                          &blob, &bloblen);
622   if (err)
623     return err;
624
625   es_fprintf (stream, "%s ", identifier);
626
627   err = b64enc_start_es (&b64_state, stream, "");
628   if (err)
629     {
630       es_free (blob);
631       return err;
632     }
633
634   err = b64enc_write (&b64_state, blob, bloblen);
635   b64enc_finish (&b64_state);
636   es_free (blob);
637   if (err)
638     return err;
639
640   if (comment)
641     es_fprintf (stream, " %s", comment);
642
643   return err;
644 }