2011-07-20 16:40:29 +02:00
|
|
|
/* ssh-utils.c - Secure Shell helper functions
|
|
|
|
* Copyright (C) 2011 Free Software Foundation, Inc.
|
|
|
|
*
|
|
|
|
* This file is part of GnuPG.
|
|
|
|
*
|
Change license for some files in common to LGPLv3+/GPLv2+.
Having the LGPL on the common GnuPG code helps to share code
between GnuPG and related projects (like GPGME and Libassuan). This
is good for interoperability and to reduces bugs.
* common/asshelp.c, common/asshelp.h, common/asshelp2.c, common/b64dec.c
* common/b64enc.c, common/convert.c, common/dns-cert.c
* common/dns-cert.h common/exechelp-posix.c, common/exechelp-w32.c
* common/exechelp-w32ce.c, common/exechelp.h, common/get-passphrase.c
* common/get-passphrase.h, common/gettime.c, common/gpgrlhelp.c
* common/helpfile.c, common/homedir.c, common/http.c, common/http.h
* common/i18n.c, common/init.c, common/init.h, common/iobuf.c
* common/iobuf.h, common/localename.c, common/membuf.c, common/membuf.h
* common/miscellaneous.c, common/openpgp-oid.c, common/openpgpdefs.h
* common/percent.c, common/pka.c, common/pka.h, common/session-env.c
* common/session-env.h, common/sexp-parse.h, common/sexputil.c
* common/signal.c, common/srv.c, common/srv.h, common/ssh-utils.c
* common/ssh-utils.h, common/sysutils.c, common/sysutils.h
* common/tlv.c, common/tlv.h, common/ttyio.c, common/ttyio.h
* common/userids.c, common/userids.h, common/xasprintf.c: Change
license to LGPLv3+/GPLv2+/
2012-04-20 15:43:06 +02:00
|
|
|
* This file is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of either
|
2011-07-20 16:40:29 +02:00
|
|
|
*
|
Change license for some files in common to LGPLv3+/GPLv2+.
Having the LGPL on the common GnuPG code helps to share code
between GnuPG and related projects (like GPGME and Libassuan). This
is good for interoperability and to reduces bugs.
* common/asshelp.c, common/asshelp.h, common/asshelp2.c, common/b64dec.c
* common/b64enc.c, common/convert.c, common/dns-cert.c
* common/dns-cert.h common/exechelp-posix.c, common/exechelp-w32.c
* common/exechelp-w32ce.c, common/exechelp.h, common/get-passphrase.c
* common/get-passphrase.h, common/gettime.c, common/gpgrlhelp.c
* common/helpfile.c, common/homedir.c, common/http.c, common/http.h
* common/i18n.c, common/init.c, common/init.h, common/iobuf.c
* common/iobuf.h, common/localename.c, common/membuf.c, common/membuf.h
* common/miscellaneous.c, common/openpgp-oid.c, common/openpgpdefs.h
* common/percent.c, common/pka.c, common/pka.h, common/session-env.c
* common/session-env.h, common/sexp-parse.h, common/sexputil.c
* common/signal.c, common/srv.c, common/srv.h, common/ssh-utils.c
* common/ssh-utils.h, common/sysutils.c, common/sysutils.h
* common/tlv.c, common/tlv.h, common/ttyio.c, common/ttyio.h
* common/userids.c, common/userids.h, common/xasprintf.c: Change
license to LGPLv3+/GPLv2+/
2012-04-20 15:43:06 +02:00
|
|
|
* - the GNU Lesser General Public License as published by the Free
|
|
|
|
* Software Foundation; either version 3 of the License, or (at
|
|
|
|
* your option) any later version.
|
|
|
|
*
|
|
|
|
* or
|
|
|
|
*
|
|
|
|
* - the GNU General Public License as published by the Free
|
|
|
|
* Software Foundation; either version 2 of the License, or (at
|
|
|
|
* your option) any later version.
|
|
|
|
*
|
|
|
|
* or both in parallel, as here.
|
|
|
|
*
|
|
|
|
* This file is distributed in the hope that it will be useful,
|
2011-07-20 16:40:29 +02:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2016-11-05 12:02:19 +01:00
|
|
|
* along with this program; if not, see <https://www.gnu.org/licenses/>.
|
2011-07-20 16:40:29 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
#include "util.h"
|
|
|
|
#include "ssh-utils.h"
|
2022-03-02 06:07:46 +01:00
|
|
|
#include "openpgpdefs.h"
|
2011-07-20 16:40:29 +02:00
|
|
|
|
|
|
|
|
2014-03-22 20:51:16 +01:00
|
|
|
/* Return true if KEYPARMS holds an EdDSA key. */
|
|
|
|
static int
|
|
|
|
is_eddsa (gcry_sexp_t keyparms)
|
|
|
|
{
|
|
|
|
int result = 0;
|
|
|
|
gcry_sexp_t list;
|
|
|
|
const char *s;
|
|
|
|
size_t n;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
list = gcry_sexp_find_token (keyparms, "flags", 0);
|
|
|
|
for (i = list ? gcry_sexp_length (list)-1 : 0; i > 0; i--)
|
|
|
|
{
|
|
|
|
s = gcry_sexp_nth_data (list, i, &n);
|
|
|
|
if (!s)
|
|
|
|
continue; /* Not a data element. */
|
|
|
|
|
|
|
|
if (n == 5 && !memcmp (s, "eddsa", 5))
|
|
|
|
{
|
|
|
|
result = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
gcry_sexp_release (list);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-05-24 17:03:58 +02:00
|
|
|
/* Dummy functions for es_mopen. */
|
|
|
|
static void *dummy_realloc (void *mem, size_t size) { (void) size; return mem; }
|
|
|
|
static void dummy_free (void *mem) { (void) mem; }
|
2011-07-20 16:40:29 +02:00
|
|
|
|
2015-12-04 15:19:07 +01:00
|
|
|
/* Return the Secure Shell type fingerprint for KEY using digest ALGO.
|
|
|
|
The length of the fingerprint is returned at R_LEN and the
|
|
|
|
fingerprint itself at R_FPR. In case of a error code is returned
|
|
|
|
and NULL stored at R_FPR. */
|
2011-07-20 16:40:29 +02:00
|
|
|
static gpg_error_t
|
2015-12-04 15:19:07 +01:00
|
|
|
get_fingerprint (gcry_sexp_t key, int algo,
|
|
|
|
void **r_fpr, size_t *r_len, int as_string)
|
2011-07-20 16:40:29 +02:00
|
|
|
{
|
|
|
|
gpg_error_t err;
|
|
|
|
gcry_sexp_t list = NULL;
|
|
|
|
gcry_sexp_t l2 = NULL;
|
|
|
|
const char *s;
|
|
|
|
char *name = NULL;
|
|
|
|
int idx;
|
|
|
|
const char *elems;
|
|
|
|
gcry_md_hd_t md = NULL;
|
2014-03-22 20:51:16 +01:00
|
|
|
int blobmode = 0;
|
2011-07-20 16:40:29 +02:00
|
|
|
|
|
|
|
*r_fpr = NULL;
|
|
|
|
*r_len = 0;
|
|
|
|
|
|
|
|
/* Check that the first element is valid. */
|
|
|
|
list = gcry_sexp_find_token (key, "public-key", 0);
|
|
|
|
if (!list)
|
|
|
|
list = gcry_sexp_find_token (key, "private-key", 0);
|
|
|
|
if (!list)
|
|
|
|
list = gcry_sexp_find_token (key, "protected-private-key", 0);
|
|
|
|
if (!list)
|
|
|
|
list = gcry_sexp_find_token (key, "shadowed-private-key", 0);
|
|
|
|
if (!list)
|
|
|
|
{
|
2012-02-07 10:15:57 +01:00
|
|
|
err = gpg_err_make (default_errsource, GPG_ERR_UNKNOWN_SEXP);
|
2011-07-20 16:40:29 +02:00
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
|
|
|
|
l2 = gcry_sexp_cadr (list);
|
|
|
|
gcry_sexp_release (list);
|
|
|
|
list = l2;
|
|
|
|
l2 = NULL;
|
|
|
|
|
|
|
|
name = gcry_sexp_nth_string (list, 0);
|
|
|
|
if (!name)
|
|
|
|
{
|
2012-02-07 10:15:57 +01:00
|
|
|
err = gpg_err_make (default_errsource, GPG_ERR_INV_SEXP);
|
2011-07-20 16:40:29 +02:00
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
|
2015-12-04 15:19:07 +01:00
|
|
|
err = gcry_md_open (&md, algo, 0);
|
2011-07-20 16:40:29 +02:00
|
|
|
if (err)
|
|
|
|
goto leave;
|
|
|
|
|
|
|
|
switch (gcry_pk_map_name (name))
|
|
|
|
{
|
|
|
|
case GCRY_PK_RSA:
|
|
|
|
elems = "en";
|
|
|
|
gcry_md_write (md, "\0\0\0\x07ssh-rsa", 11);
|
|
|
|
break;
|
2014-03-22 20:51:16 +01:00
|
|
|
|
2011-07-20 16:40:29 +02:00
|
|
|
case GCRY_PK_DSA:
|
|
|
|
elems = "pqgy";
|
|
|
|
gcry_md_write (md, "\0\0\0\x07ssh-dss", 11);
|
|
|
|
break;
|
2014-03-22 20:51:16 +01:00
|
|
|
|
|
|
|
case GCRY_PK_ECC:
|
|
|
|
if (is_eddsa (list))
|
|
|
|
{
|
|
|
|
elems = "q";
|
|
|
|
blobmode = 1;
|
|
|
|
/* For now there is just one curve, thus no need to switch
|
|
|
|
on it. */
|
|
|
|
gcry_md_write (md, "\0\0\0\x0b" "ssh-ed25519", 15);
|
|
|
|
}
|
2012-12-12 18:47:21 +01:00
|
|
|
else
|
|
|
|
{
|
2014-03-22 20:51:16 +01:00
|
|
|
/* We only support the 3 standard curves for now. It is
|
|
|
|
just a quick hack. */
|
|
|
|
elems = "q";
|
|
|
|
gcry_md_write (md, "\0\0\0\x13" "ecdsa-sha2-nistp", 20);
|
|
|
|
l2 = gcry_sexp_find_token (list, "curve", 0);
|
|
|
|
if (!l2)
|
2012-12-12 18:47:21 +01:00
|
|
|
elems = "";
|
|
|
|
else
|
2014-03-22 20:51:16 +01:00
|
|
|
{
|
|
|
|
gcry_free (name);
|
|
|
|
name = gcry_sexp_nth_string (l2, 1);
|
|
|
|
gcry_sexp_release (l2);
|
|
|
|
l2 = NULL;
|
|
|
|
if (!name)
|
|
|
|
elems = "";
|
|
|
|
else if (!strcmp (name, "NIST P-256")||!strcmp (name, "nistp256"))
|
|
|
|
gcry_md_write (md, "256\0\0\0\x08nistp256", 15);
|
|
|
|
else if (!strcmp (name, "NIST P-384")||!strcmp (name, "nistp384"))
|
2015-09-22 09:28:35 +02:00
|
|
|
gcry_md_write (md, "384\0\0\0\x08nistp384", 15);
|
2014-03-22 20:51:16 +01:00
|
|
|
else if (!strcmp (name, "NIST P-521")||!strcmp (name, "nistp521"))
|
|
|
|
gcry_md_write (md, "521\0\0\0\x08nistp521", 15);
|
|
|
|
else
|
|
|
|
elems = "";
|
|
|
|
}
|
|
|
|
if (!*elems)
|
|
|
|
err = gpg_err_make (default_errsource, GPG_ERR_UNKNOWN_CURVE);
|
2012-12-12 18:47:21 +01:00
|
|
|
}
|
|
|
|
break;
|
2014-03-22 20:51:16 +01:00
|
|
|
|
2011-07-20 16:40:29 +02:00
|
|
|
default:
|
|
|
|
elems = "";
|
2012-02-07 10:15:57 +01:00
|
|
|
err = gpg_err_make (default_errsource, GPG_ERR_PUBKEY_ALGO);
|
2011-07-20 16:40:29 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (err)
|
|
|
|
goto leave;
|
|
|
|
|
2014-03-22 20:51:16 +01:00
|
|
|
|
2011-07-20 16:40:29 +02:00
|
|
|
for (idx = 0, s = elems; *s; s++, idx++)
|
|
|
|
{
|
|
|
|
l2 = gcry_sexp_find_token (list, s, 1);
|
|
|
|
if (!l2)
|
|
|
|
{
|
2012-02-07 10:15:57 +01:00
|
|
|
err = gpg_err_make (default_errsource, GPG_ERR_INV_SEXP);
|
2011-07-20 16:40:29 +02:00
|
|
|
goto leave;
|
|
|
|
}
|
2014-03-22 20:51:16 +01:00
|
|
|
if (blobmode)
|
2011-07-20 16:40:29 +02:00
|
|
|
{
|
2014-03-22 20:51:16 +01:00
|
|
|
const char *blob;
|
|
|
|
size_t bloblen;
|
|
|
|
unsigned char lenbuf[4];
|
|
|
|
|
|
|
|
blob = gcry_sexp_nth_data (l2, 1, &bloblen);
|
|
|
|
if (!blob)
|
|
|
|
{
|
|
|
|
err = gpg_err_make (default_errsource, GPG_ERR_INV_SEXP);
|
|
|
|
goto leave;
|
|
|
|
}
|
2015-09-29 08:33:59 +02:00
|
|
|
blob++;
|
|
|
|
bloblen--;
|
2014-03-22 20:51:16 +01:00
|
|
|
lenbuf[0] = bloblen >> 24;
|
|
|
|
lenbuf[1] = bloblen >> 16;
|
|
|
|
lenbuf[2] = bloblen >> 8;
|
|
|
|
lenbuf[3] = bloblen;
|
|
|
|
gcry_md_write (md, lenbuf, 4);
|
|
|
|
gcry_md_write (md, blob, bloblen);
|
2011-07-20 16:40:29 +02:00
|
|
|
}
|
2014-03-22 20:51:16 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
gcry_mpi_t a;
|
|
|
|
unsigned char *buf;
|
|
|
|
size_t buflen;
|
2011-07-20 16:40:29 +02:00
|
|
|
|
2014-03-22 20:51:16 +01:00
|
|
|
a = gcry_sexp_nth_mpi (l2, 1, GCRYMPI_FMT_USG);
|
|
|
|
gcry_sexp_release (l2);
|
|
|
|
l2 = NULL;
|
|
|
|
if (!a)
|
|
|
|
{
|
|
|
|
err = gpg_err_make (default_errsource, GPG_ERR_INV_SEXP);
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = gcry_mpi_aprint (GCRYMPI_FMT_SSH, &buf, &buflen, a);
|
|
|
|
gcry_mpi_release (a);
|
|
|
|
if (err)
|
|
|
|
goto leave;
|
|
|
|
gcry_md_write (md, buf, buflen);
|
|
|
|
gcry_free (buf);
|
|
|
|
}
|
2011-07-20 16:40:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (as_string)
|
|
|
|
{
|
2017-05-24 17:03:58 +02:00
|
|
|
const char *algo_name;
|
|
|
|
char *fpr;
|
|
|
|
|
|
|
|
/* Prefix string with the algorithm name and a colon. */
|
|
|
|
algo_name = gcry_md_algo_name (algo);
|
|
|
|
*r_fpr = xtrymalloc (strlen (algo_name) + 1 + 3 * gcry_md_get_algo_dlen (algo) + 1);
|
|
|
|
if (*r_fpr == NULL)
|
|
|
|
{
|
|
|
|
err = gpg_err_make (default_errsource, gpg_err_code_from_syserror ());
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
|
2018-09-06 04:41:13 +02:00
|
|
|
memcpy (*r_fpr, algo_name, strlen (algo_name));
|
2017-05-24 17:03:58 +02:00
|
|
|
fpr = (char *) *r_fpr + strlen (algo_name);
|
|
|
|
*fpr++ = ':';
|
|
|
|
|
|
|
|
if (algo == GCRY_MD_MD5)
|
|
|
|
{
|
|
|
|
bin2hexcolon (gcry_md_read (md, algo), gcry_md_get_algo_dlen (algo), fpr);
|
|
|
|
strlwr (fpr);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-09-26 06:34:50 +02:00
|
|
|
gpgrt_b64state_t b64s;
|
2017-05-24 17:03:58 +02:00
|
|
|
estream_t stream;
|
|
|
|
char *p;
|
|
|
|
long int len;
|
|
|
|
|
|
|
|
/* Write the base64-encoded hash to fpr. */
|
|
|
|
stream = es_mopen (fpr, 3 * gcry_md_get_algo_dlen (algo) + 1, 0,
|
|
|
|
0, dummy_realloc, dummy_free, "w");
|
|
|
|
if (stream == NULL)
|
|
|
|
{
|
|
|
|
err = gpg_err_make (default_errsource, gpg_err_code_from_syserror ());
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
|
2023-09-26 06:34:50 +02:00
|
|
|
b64s = gpgrt_b64enc_start (stream, "");
|
|
|
|
if (!b64s)
|
2017-05-24 17:03:58 +02:00
|
|
|
{
|
|
|
|
es_fclose (stream);
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
|
2023-09-26 06:34:50 +02:00
|
|
|
err = gpgrt_b64enc_write (b64s, gcry_md_read (md, algo),
|
|
|
|
gcry_md_get_algo_dlen (algo));
|
2017-05-24 17:03:58 +02:00
|
|
|
if (err)
|
|
|
|
{
|
|
|
|
es_fclose (stream);
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Finish, get the length, and close the stream. */
|
2023-09-26 06:34:50 +02:00
|
|
|
err = gpgrt_b64enc_finish (b64s);
|
2017-05-24 17:03:58 +02:00
|
|
|
len = es_ftell (stream);
|
|
|
|
es_fclose (stream);
|
|
|
|
if (err)
|
|
|
|
goto leave;
|
|
|
|
|
|
|
|
/* Terminate. */
|
|
|
|
fpr[len] = 0;
|
|
|
|
|
|
|
|
/* Strip the trailing padding characters. */
|
|
|
|
for (p = fpr + len - 1; p > fpr && *p == '='; p--)
|
|
|
|
*p = 0;
|
|
|
|
}
|
|
|
|
|
2015-12-04 15:19:07 +01:00
|
|
|
*r_len = strlen (*r_fpr) + 1;
|
2011-07-20 16:40:29 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-12-04 15:19:07 +01:00
|
|
|
*r_len = gcry_md_get_algo_dlen (algo);
|
|
|
|
*r_fpr = xtrymalloc (*r_len);
|
|
|
|
if (!*r_fpr)
|
|
|
|
{
|
|
|
|
err = gpg_err_make (default_errsource, gpg_err_code_from_syserror ());
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
memcpy (*r_fpr, gcry_md_read (md, algo), *r_len);
|
2011-07-20 16:40:29 +02:00
|
|
|
}
|
|
|
|
err = 0;
|
|
|
|
|
|
|
|
leave:
|
|
|
|
gcry_free (name);
|
|
|
|
gcry_sexp_release (l2);
|
|
|
|
gcry_md_close (md);
|
|
|
|
gcry_sexp_release (list);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2015-12-04 15:19:07 +01:00
|
|
|
/* Return the Secure Shell type fingerprint for KEY using digest ALGO.
|
|
|
|
The length of the fingerprint is returned at R_LEN and the
|
|
|
|
fingerprint itself at R_FPR. In case of an error an error code is
|
|
|
|
returned and NULL stored at R_FPR. */
|
2011-07-20 16:40:29 +02:00
|
|
|
gpg_error_t
|
2015-12-04 15:19:07 +01:00
|
|
|
ssh_get_fingerprint (gcry_sexp_t key, int algo,
|
|
|
|
void **r_fpr, size_t *r_len)
|
2011-07-20 16:40:29 +02:00
|
|
|
{
|
2015-12-04 15:19:07 +01:00
|
|
|
return get_fingerprint (key, algo, r_fpr, r_len, 0);
|
2011-07-20 16:40:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-04 15:19:07 +01:00
|
|
|
/* Return the Secure Shell type fingerprint for KEY using digest ALGO
|
|
|
|
as a string. The fingerprint is mallcoed and stored at R_FPRSTR.
|
|
|
|
In case of an error an error code is returned and NULL stored at
|
|
|
|
R_FPRSTR. */
|
2011-07-20 16:40:29 +02:00
|
|
|
gpg_error_t
|
2015-12-04 15:19:07 +01:00
|
|
|
ssh_get_fingerprint_string (gcry_sexp_t key, int algo, char **r_fprstr)
|
2011-07-20 16:40:29 +02:00
|
|
|
{
|
|
|
|
gpg_error_t err;
|
|
|
|
size_t dummy;
|
|
|
|
void *string;
|
|
|
|
|
2015-12-04 15:19:07 +01:00
|
|
|
err = get_fingerprint (key, algo, &string, &dummy, 1);
|
2011-07-20 16:40:29 +02:00
|
|
|
*r_fprstr = string;
|
|
|
|
return err;
|
|
|
|
}
|
2022-03-02 06:07:46 +01:00
|
|
|
|
|
|
|
|
|
|
|
/* Write the uint32 contained in UINT32 to STREAM. */
|
|
|
|
static gpg_error_t
|
|
|
|
stream_write_uint32 (estream_t stream, u32 uint32)
|
|
|
|
{
|
|
|
|
unsigned char buffer[4];
|
|
|
|
gpg_error_t err;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
buffer[0] = uint32 >> 24;
|
|
|
|
buffer[1] = uint32 >> 16;
|
|
|
|
buffer[2] = uint32 >> 8;
|
|
|
|
buffer[3] = uint32 >> 0;
|
|
|
|
|
|
|
|
ret = es_write (stream, buffer, sizeof (buffer), NULL);
|
|
|
|
if (ret)
|
|
|
|
err = gpg_error_from_syserror ();
|
|
|
|
else
|
|
|
|
err = 0;
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Write SIZE bytes from BUFFER to STREAM. */
|
|
|
|
static gpg_error_t
|
|
|
|
stream_write_data (estream_t stream, const unsigned char *buffer, size_t size)
|
|
|
|
{
|
|
|
|
gpg_error_t err;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = es_write (stream, buffer, size, NULL);
|
|
|
|
if (ret)
|
|
|
|
err = gpg_error_from_syserror ();
|
|
|
|
else
|
|
|
|
err = 0;
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Write a binary string from STRING of size STRING_N to STREAM. */
|
|
|
|
static gpg_error_t
|
|
|
|
stream_write_string (estream_t stream,
|
|
|
|
const unsigned char *string, u32 string_n)
|
|
|
|
{
|
|
|
|
gpg_error_t err;
|
|
|
|
|
|
|
|
err = stream_write_uint32 (stream, string_n);
|
|
|
|
if (err)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
err = stream_write_data (stream, string, string_n);
|
|
|
|
|
|
|
|
out:
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Write a C-string from STRING to STREAM. */
|
|
|
|
static gpg_error_t
|
|
|
|
stream_write_cstring (estream_t stream, const char *string)
|
|
|
|
{
|
|
|
|
gpg_error_t err;
|
|
|
|
|
|
|
|
err = stream_write_string (stream,
|
|
|
|
(const unsigned char *) string, strlen (string));
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Write the MPI contained in MPINT to STREAM. */
|
|
|
|
static gpg_error_t
|
|
|
|
stream_write_mpi (estream_t stream, gcry_mpi_t mpint)
|
|
|
|
{
|
|
|
|
unsigned char *mpi_buffer;
|
|
|
|
size_t mpi_buffer_n;
|
|
|
|
gpg_error_t err;
|
|
|
|
|
|
|
|
mpi_buffer = NULL;
|
|
|
|
|
|
|
|
err = gcry_mpi_aprint (GCRYMPI_FMT_STD, &mpi_buffer, &mpi_buffer_n, mpint);
|
|
|
|
if (err)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
err = stream_write_string (stream, mpi_buffer, mpi_buffer_n);
|
|
|
|
|
|
|
|
out:
|
|
|
|
|
|
|
|
xfree (mpi_buffer);
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Encode a key in SEXP, in SSH format. */
|
|
|
|
static gpg_error_t
|
|
|
|
sexp_to_sshblob (gcry_sexp_t sexp, const char *identifier, int is_eddsa_flag,
|
|
|
|
const char *curve, const char *elems,
|
|
|
|
void **r_blob, size_t *r_blob_size)
|
|
|
|
{
|
|
|
|
gpg_error_t err = 0;
|
|
|
|
gcry_sexp_t value_list = NULL;
|
|
|
|
gcry_sexp_t value_pair = NULL;
|
|
|
|
estream_t stream = NULL;
|
|
|
|
const char *p_elems;
|
|
|
|
const char *data;
|
|
|
|
size_t datalen;
|
|
|
|
|
|
|
|
*r_blob = NULL;
|
|
|
|
*r_blob_size = 0;
|
|
|
|
|
|
|
|
stream = es_fopenmem (0, "r+b");
|
|
|
|
if (!stream)
|
|
|
|
{
|
|
|
|
err = gpg_error_from_syserror ();
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get key value list. */
|
|
|
|
value_list = gcry_sexp_cadr (sexp);
|
|
|
|
if (!value_list)
|
|
|
|
{
|
|
|
|
err = gpg_error (GPG_ERR_INV_SEXP);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = stream_write_cstring (stream, identifier);
|
|
|
|
if (err)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
if (curve && !is_eddsa_flag)
|
|
|
|
{
|
|
|
|
/* ECDSA requires the curve name. */
|
|
|
|
err = stream_write_cstring (stream, curve);
|
|
|
|
if (err)
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Write the parameters. */
|
|
|
|
for (p_elems = elems; *p_elems; p_elems++)
|
|
|
|
{
|
|
|
|
gcry_sexp_release (value_pair);
|
|
|
|
value_pair = gcry_sexp_find_token (value_list, p_elems, 1);
|
|
|
|
if (!value_pair)
|
|
|
|
{
|
|
|
|
err = gpg_error (GPG_ERR_INV_SEXP);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (is_eddsa_flag)
|
|
|
|
{
|
|
|
|
data = gcry_sexp_nth_data (value_pair, 1, &datalen);
|
|
|
|
if (!data)
|
|
|
|
{
|
|
|
|
err = gpg_error (GPG_ERR_INV_SEXP);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if ((datalen & 1) && *data == 0x40)
|
|
|
|
{ /* Remove the prefix 0x40. */
|
|
|
|
data++;
|
|
|
|
datalen--;
|
|
|
|
}
|
|
|
|
err = stream_write_string (stream, data, datalen);
|
|
|
|
if (err)
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
gcry_mpi_t mpi;
|
|
|
|
|
|
|
|
/* Note that we need to use STD format; i.e. prepend a 0x00
|
|
|
|
to indicate a positive number if the high bit is set. */
|
|
|
|
mpi = gcry_sexp_nth_mpi (value_pair, 1, GCRYMPI_FMT_STD);
|
|
|
|
if (!mpi)
|
|
|
|
{
|
|
|
|
err = gpg_error (GPG_ERR_INV_SEXP);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
err = stream_write_mpi (stream, mpi);
|
|
|
|
gcry_mpi_release (mpi);
|
|
|
|
if (err)
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (es_fclose_snatch (stream, r_blob, r_blob_size))
|
|
|
|
{
|
|
|
|
err = gpg_error_from_syserror ();
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
stream = NULL;
|
|
|
|
|
|
|
|
out:
|
|
|
|
gcry_sexp_release (value_list);
|
|
|
|
gcry_sexp_release (value_pair);
|
|
|
|
es_fclose (stream);
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* For KEY in S-expression, write it in SSH base64 format to STREAM,
|
|
|
|
adding COMMENT. */
|
|
|
|
gpg_error_t
|
|
|
|
ssh_public_key_in_base64 (gcry_sexp_t key, estream_t stream,
|
|
|
|
const char *comment)
|
|
|
|
{
|
|
|
|
gpg_error_t err = 0;
|
|
|
|
int algo;
|
|
|
|
int is_eddsa_flag = 0;
|
|
|
|
const char *curve = NULL;
|
|
|
|
const char *pub_elements = NULL;
|
|
|
|
const char *identifier = NULL;
|
|
|
|
void *blob = NULL;
|
|
|
|
size_t bloblen;
|
2023-09-26 06:34:50 +02:00
|
|
|
gpgrt_b64state_t b64_state;
|
2022-03-02 06:07:46 +01:00
|
|
|
|
|
|
|
algo = get_pk_algo_from_key (key);
|
|
|
|
if (algo == 0)
|
|
|
|
return gpg_error (GPG_ERR_PUBKEY_ALGO);
|
|
|
|
|
2022-03-04 02:57:48 +01:00
|
|
|
if (algo == GCRY_PK_ECC || algo == GCRY_PK_EDDSA)
|
2022-03-02 06:07:46 +01:00
|
|
|
{
|
|
|
|
curve = gcry_pk_get_curve (key, 0, NULL);
|
|
|
|
if (!curve)
|
|
|
|
return gpg_error (GPG_ERR_INV_CURVE);
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (algo)
|
|
|
|
{
|
2022-03-04 02:57:48 +01:00
|
|
|
case GCRY_PK_RSA:
|
2022-03-02 06:07:46 +01:00
|
|
|
identifier = "ssh-rsa";
|
|
|
|
pub_elements = "en";
|
|
|
|
break;
|
|
|
|
|
2022-03-04 02:57:48 +01:00
|
|
|
case GCRY_PK_ECC:
|
2022-09-06 06:43:03 +02:00
|
|
|
if (!strcmp (curve, "NIST P-256"))
|
2022-03-02 06:07:46 +01:00
|
|
|
identifier = "ecdsa-sha2-nistp256";
|
2022-09-06 06:43:03 +02:00
|
|
|
else if (!strcmp (curve, "NIST P-384"))
|
2022-03-02 06:07:46 +01:00
|
|
|
identifier = "ecdsa-sha2-nistp384";
|
2022-09-06 06:43:03 +02:00
|
|
|
else if (!strcmp (curve, "NIST P-521"))
|
2022-03-02 06:07:46 +01:00
|
|
|
identifier = "ecdsa-sha2-nistp521";
|
|
|
|
else
|
|
|
|
err = gpg_error (GPG_ERR_UNKNOWN_CURVE);
|
|
|
|
pub_elements = "q";
|
|
|
|
break;
|
|
|
|
|
2022-03-04 02:57:48 +01:00
|
|
|
case GCRY_PK_EDDSA:
|
2022-03-02 06:07:46 +01:00
|
|
|
is_eddsa_flag = 1;
|
|
|
|
if (!strcmp (curve, "Ed25519"))
|
|
|
|
identifier = "ssh-ed25519";
|
|
|
|
else if (!strcmp (curve, "Ed448"))
|
|
|
|
identifier = "ssh-ed448";
|
|
|
|
else
|
|
|
|
err = gpg_error (GPG_ERR_UNKNOWN_CURVE);
|
|
|
|
pub_elements = "q";
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
err = gpg_error (GPG_ERR_PUBKEY_ALGO);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
err = sexp_to_sshblob (key, identifier, is_eddsa_flag, curve, pub_elements,
|
|
|
|
&blob, &bloblen);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
es_fprintf (stream, "%s ", identifier);
|
|
|
|
|
2023-09-26 06:34:50 +02:00
|
|
|
b64_state = gpgrt_b64enc_start (stream, "");
|
|
|
|
if (!b64_state)
|
2022-03-02 06:07:46 +01:00
|
|
|
{
|
|
|
|
es_free (blob);
|
2023-09-26 06:34:50 +02:00
|
|
|
return gpg_error_from_syserror ();
|
2022-03-02 06:07:46 +01:00
|
|
|
}
|
|
|
|
|
2023-09-26 06:34:50 +02:00
|
|
|
err = gpgrt_b64enc_write (b64_state, blob, bloblen);
|
|
|
|
gpgrt_b64enc_finish (b64_state);
|
2022-03-02 06:07:46 +01:00
|
|
|
es_free (blob);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
if (comment)
|
|
|
|
es_fprintf (stream, " %s", comment);
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|