1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605
|
/* key-func.c -- SSH key manipulation functions.
*
* Copyright (C) 2013-2026 Artyom V. Poptsov <poptsov.artyom@gmail.com>
* Copyright (C) 2026 Nicolas Graves <ngraves@ngraves.fr>
*
* This file is part of Guile-SSH.
*
* Guile-SSH is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* Guile-SSH is distributed in the hope that it will be useful, 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
* along with Guile-SSH. If not, see <http://www.gnu.org/licenses/>.
*/
#include <config.h>
#include <string.h> /* strncpy */
#include <libguile.h>
#include <libssh/libssh.h>
#include "key-type.h"
#include "common.h"
#include "error.h"
SCM_DEFINE (guile_ssh_key_get_type, "get-key-type", 1, 0, 0,
(SCM key),
"\
Get a symbol that represents the type of the SSH key KEY.\n\
Possible types are: 'dss, 'rsa, 'rsa1, 'ecdsa, 'ed25519, 'unknown\
and 'ecdsa variants if libssh >= 0.9.\
")
{
gssh_key_t *data = gssh_key_from_scm (key);
enum ssh_keytypes_e type = ssh_key_type (data->ssh_key);
return _ssh_key_type_to_scm (type);
}
SCM_DEFINE (guile_ssh_make_keypair, "%gssh-make-keypair", 2, 0, 0,
(SCM type, SCM length),
"\
Generate a keypair of specified TYPE and LENGTH. This may take some time.\
Return newly generated private key. Throw `guile-ssh-error' on error.\
")
#define FUNC_NAME s_guile_ssh_make_keypair
{
ssh_key key = NULL;
const gssh_symbol_t *c_type = _scm_to_ssh_key_type (type);
int c_length;
int res;
SCM_ASSERT (scm_is_unsigned_integer (length, 0, UINT32_MAX), length,
SCM_ARG2, FUNC_NAME);
if (! c_type)
guile_ssh_error1 (FUNC_NAME, "Wrong key type", type);
c_length = scm_to_int (length);
res = ssh_pki_generate (c_type->value, c_length, &key);
if (res == SSH_ERROR)
{
guile_ssh_error1 (FUNC_NAME, "Could not generate key",
scm_list_2 (type, length));
}
return gssh_key_to_scm (key, SCM_BOOL_F);
}
#undef FUNC_NAME
/* Predicates */
SCM_DEFINE (guile_ssh_is_key_p, "key?", 1, 0, 0,
(SCM x),
"\
Return #t if X is a SSH key, #f otherwise.\
")
{
return scm_from_bool (SCM_SMOB_PREDICATE (key_tag, x));
}
SCM_DEFINE (guile_ssh_is_public_key_p, "public-key?", 1, 0, 0,
(SCM x),
"\
Return #t if X is a SSH key and it contains a public key, #f otherwise.\
")
{
return scm_from_bool (SCM_SMOB_PREDICATE (key_tag, x)
&& _public_key_p (gssh_key_from_scm (x)));
}
SCM_DEFINE (guile_ssh_is_private_key_p, "private-key?", 1, 0, 0,
(SCM x),
"\
Return #t if X is a SSH private-key, #f otherwise.\
")
{
return scm_from_bool (SCM_SMOB_PREDICATE (key_tag, x)
&& _private_key_p (gssh_key_from_scm (x)));
}
/* Convert SSH public key KEY to a scheme string. */
SCM_DEFINE (guile_ssh_public_key_to_string, "public-key->string", 1, 0, 0,
(SCM key),
"\
Convert SSH public key to a scheme string.\
")
#define FUNC_NAME s_guile_ssh_public_key_to_string
{
gssh_key_t *key_data = gssh_key_from_scm (key);
char *key_str;
SCM_ASSERT (_public_key_p (key_data), key, SCM_ARG1, FUNC_NAME);
int res = ssh_pki_export_pubkey_base64 (key_data->ssh_key, &key_str);
if (res != SSH_OK)
guile_ssh_error1 (FUNC_NAME, "Unable to convert the key to a string", key);
return scm_take_locale_string (key_str);
}
#undef FUNC_NAME
SCM_DEFINE (guile_ssh_string_to_public_key, "string->public-key", 2, 0, 0,
(SCM base64_str, SCM type),
"\
Convert Base64 string to a public key. Return new public key.\n\
Throw `guile-ssh-error' on error.\
")
#define FUNC_NAME s_guile_ssh_string_to_public_key
{
char *c_base64_str = NULL;
const gssh_symbol_t *key_type = NULL;
ssh_key ssh_public_key = NULL;
int res;
SCM_ASSERT (scm_is_string (base64_str), base64_str, SCM_ARG1, FUNC_NAME);
SCM_ASSERT (scm_is_symbol (type), type, SCM_ARG2, FUNC_NAME);
scm_dynwind_begin (0);
c_base64_str = scm_to_locale_string (base64_str);
scm_dynwind_free (c_base64_str);
key_type = _scm_to_ssh_key_type (type);
if (! key_type)
guile_ssh_error1 (FUNC_NAME, "Wrong key type", type);
res = ssh_pki_import_pubkey_base64 (c_base64_str,
key_type->value,
&ssh_public_key);
if (res != SSH_OK)
{
const char *msg = "Could not convert the given string to a public key";
guile_ssh_error1 (FUNC_NAME, msg, scm_list_2 (base64_str, type));
}
scm_dynwind_end ();
return gssh_key_to_scm (ssh_public_key, SCM_BOOL_F);
}
#undef FUNC_NAME
/* The callback procedure that meant to be called by libssh. */
static int
auth_callback (const char *prompt,
char *buf,
size_t len,
int echo,
int verify,
void *userdata)
{
SCM scm_data = (SCM) userdata;
SCM scm_callback
= scm_assoc_ref (scm_data, scm_from_locale_string ("callback"));
SCM scm_userdata
= scm_assoc_ref (scm_data, scm_from_locale_string ("user-data"));
SCM scm_prompt = scm_from_locale_string (prompt);
SCM scm_buf_len = scm_from_int (len);
SCM scm_echo_p = scm_from_bool (echo);
SCM scm_verify_p = scm_from_bool (verify);
SCM result = scm_call_5 (scm_callback,
scm_prompt,
scm_buf_len,
scm_echo_p,
scm_verify_p,
scm_userdata);
if (scm_is_string (result))
{
char* pass = scm_to_locale_string (result);
strncpy (buf, pass, len);
free (pass);
return 0;
}
else if (scm_is_false (result))
{
return 0;
}
else
{
guile_ssh_error1 ("libssh_auth_callback",
"Wrong type of the value returned by a callback",
result);
return 0;
}
}
SCM_DEFINE (guile_ssh_private_key_from_file, "%private-key-from-file", 3, 0, 0,
(SCM filename, SCM callback, SCM user_data),
"\
Read private key from a file FILENAME. If the the key isn encrypted the user\n\
will be asked for passphrase to decrypt the key.\n\
\n\
Return a new SSH key of #f on error.\
")
#define FUNC_NAME s_guile_ssh_private_key_from_file
{
ssh_key ssh_key = NULL;
char *c_filename;
/* NULL means that either the public key is unecrypted or the user
should be asked for the passphrase. */
char *passphrase = NULL;
int res;
scm_dynwind_begin (0);
SCM_ASSERT (scm_is_string (filename), filename, SCM_ARG1, FUNC_NAME);
c_filename = scm_to_locale_string (filename);
scm_dynwind_free (c_filename);
if (scm_is_false (callback))
{
res = ssh_pki_import_privkey_file (c_filename,
passphrase,
NULL, /* auth_fn */
NULL, /* auth_data */
&ssh_key);
}
else
{
SCM data = scm_list_2 (scm_cons (scm_from_locale_string ("callback"),
callback),
scm_cons (scm_from_locale_string ("user-data"),
user_data));
res = ssh_pki_import_privkey_file (c_filename,
passphrase,
auth_callback, /* auth_fn */
data, /* auth_data */
&ssh_key);
}
if (res == SSH_EOF)
{
const char *msg = "The file does not exist or permission denied";
guile_ssh_error1 (FUNC_NAME, msg, filename);
}
else if (res == SSH_ERROR)
{
const char *msg = "Unable to import a key from the file";
guile_ssh_error1 (FUNC_NAME, msg, filename);
}
scm_dynwind_end ();
return gssh_key_to_scm (ssh_key, SCM_BOOL_F);
}
#undef FUNC_NAME
SCM_DEFINE (guile_ssh_private_key_to_file,
"private-key-to-file", 2, 0, 0,
(SCM key, SCM file_name),
"\
Export a private KEY to file FILE_NAME. Throw `guile-ssh-error' on error. \
Return value is undefined.\
")
#define FUNC_NAME s_guile_ssh_private_key_to_file
{
gssh_key_t *kd = gssh_key_from_scm (key);
char *c_file_name = NULL;
int res;
scm_dynwind_begin (0);
SCM_ASSERT (_private_key_p (kd), key, SCM_ARG1, FUNC_NAME);
SCM_ASSERT (scm_is_string (file_name), file_name, SCM_ARG2, FUNC_NAME);
c_file_name = scm_to_locale_string (file_name);
scm_dynwind_free (c_file_name);
res = ssh_pki_export_privkey_file (kd->ssh_key,
NULL, /* passphrase */
NULL, /* auth_fn */
NULL, /* auth_data */
c_file_name);
if (res == SSH_ERROR)
{
guile_ssh_error1 (FUNC_NAME, "Unable to export a key to a file",
scm_list_2 (key, file_name));
}
scm_dynwind_end ();
return SCM_UNDEFINED;
}
#undef FUNC_NAME
SCM_DEFINE (guile_ssh_public_key_from_private_key, "private-key->public-key",
1, 0, 0,
(SCM key),
"\
Get public key from a private key KEY.\
")
#define FUNC_NAME s_guile_ssh_public_key_from_private_key
{
gssh_key_t *private_key_data = gssh_key_from_scm (key);
ssh_key ssh_public_key = NULL;
int res;
SCM_ASSERT (_private_key_p (private_key_data), key, SCM_ARG1, FUNC_NAME);
res = ssh_pki_export_privkey_to_pubkey (private_key_data->ssh_key,
&ssh_public_key);
if (res != SSH_OK)
return SCM_BOOL_F;
return gssh_key_to_scm (ssh_public_key, SCM_BOOL_F);
}
#undef FUNC_NAME
/* Read public key from a file FILENAME.
*
* Return a SSH key smob.
*/
SCM_DEFINE (guile_ssh_public_key_from_file, "public-key-from-file", 1, 0, 0,
(SCM filename),
"\
Read public key from a file FILENAME. Return a SSH key.\
")
#define FUNC_NAME s_guile_ssh_public_key_from_file
{
ssh_key ssh_public_key = NULL;
char *c_filename;
int res;
scm_dynwind_begin (0);
SCM_ASSERT (scm_is_string (filename), filename, SCM_ARG1, FUNC_NAME);
c_filename = scm_to_locale_string (filename);
scm_dynwind_free (c_filename);
res = ssh_pki_import_pubkey_file (c_filename, &ssh_public_key);
if (res == SSH_EOF)
{
const char *msg = "The file does not exist or permission denied";
guile_ssh_error1 (FUNC_NAME, msg, filename);
}
else if (res == SSH_ERROR)
{
const char *msg = "Unable to import a key from the file";
guile_ssh_error1 (FUNC_NAME, msg, filename);
}
scm_dynwind_end ();
return gssh_key_to_scm (ssh_public_key, SCM_BOOL_F);
}
#undef FUNC_NAME
static gssh_symbol_t hash_types[] = {
{ "sha1", SSH_PUBLICKEY_HASH_SHA1 },
{ "sha256", SSH_PUBLICKEY_HASH_SHA256 },
{ "md5", SSH_PUBLICKEY_HASH_MD5 },
{ NULL, -1 }
};
SCM_DEFINE (guile_ssh_get_public_key_hash, "get-public-key-hash", 2, 0, 0,
(SCM key, SCM type),
"\
Get hash of the public KEY as a bytevector.\n\
Possible types are: 'sha1, 'sha256, 'md5\n\
Return a bytevector on success, #f on error.\
")
#define FUNC_NAME s_guile_ssh_get_public_key_hash
{
gssh_key_t *kd = gssh_key_from_scm (key);
unsigned char *hash = NULL;
size_t hash_len;
int res;
SCM ret;
const gssh_symbol_t *hash_type = NULL;
SCM_ASSERT (scm_is_symbol (type), type, SCM_ARG2, FUNC_NAME);
scm_dynwind_begin (0);
hash_type = gssh_symbol_from_scm (hash_types, type);
if (! hash_type)
guile_ssh_error1 (FUNC_NAME, "Wrong type", type);
res = ssh_get_publickey_hash (kd->ssh_key, hash_type->value,
&hash, &hash_len);
scm_dynwind_free (hash);
if (res == SSH_OK)
{
size_t idx;
ret = scm_c_make_bytevector (hash_len);
for (idx = 0; idx < hash_len; ++idx)
scm_c_bytevector_set_x (ret, idx, hash[idx]);
}
else
{
ret = SCM_BOOL_F;
}
scm_dynwind_end ();
return ret;
}
#undef FUNC_NAME
SCM_DEFINE (guile_ssh_get_public_key_fingerprint, "%gssh-get-public-key-fingerprint", 2, 0, 0,
(SCM key, SCM type),
"\
Get fingerprint of the public KEY as a formatted string.\n\
Possible types are: 'sha1, 'sha256, 'md5\n\
Return a fingerprint string on success, #f on error.\
")
#define FUNC_NAME s_guile_ssh_get_public_key_fingerprint
{
gssh_key_t *kd = gssh_key_from_scm (key);
unsigned char *hash = NULL;
size_t hash_len;
char *fingerprint = NULL;
int res;
SCM ret;
const gssh_symbol_t *hash_type = NULL;
SCM_ASSERT (scm_is_symbol (type), type, SCM_ARG2, FUNC_NAME);
scm_dynwind_begin (0);
hash_type = gssh_symbol_from_scm (hash_types, type);
if (! hash_type)
guile_ssh_error1 (FUNC_NAME, "Wrong type", type);
res = ssh_get_publickey_hash (kd->ssh_key, hash_type->value,
&hash, &hash_len);
scm_dynwind_free (hash);
if (res == SSH_OK)
{
fingerprint = ssh_get_fingerprint_hash (hash_type->value, hash, hash_len);
if (fingerprint)
{
ret = scm_take_locale_string (fingerprint);
}
else
{
ret = SCM_BOOL_F;
}
}
else
{
ret = SCM_BOOL_F;
}
scm_dynwind_end ();
return ret;
}
#undef FUNC_NAME
#if HAVE_LIBSSH_0_12
static gssh_symbol_t sshsig_hash_types[] = {
{ "sha256", SSHSIG_DIGEST_SHA2_256 },
{ "sha512", SSHSIG_DIGEST_SHA2_512 },
{ NULL, -1 }
};
SCM_DEFINE (guile_ssh_sign, "%gssh-sign", 4, 0, 0,
(SCM data_bv, SCM private_key, SCM sig_namespace, SCM hash_alg),
"\
Sign binary DATA_BV (bytevector) using a PRIVATE_KEY with specified SIG_NAMESPACE and HASH_ALG.\n\
HASH_ALG should be 'sha256 or 'sha512.\n\
Return the armored signature string on success, #f on error.\
")
#define FUNC_NAME s_guile_ssh_sign
{
gssh_key_t *kd = gssh_key_from_scm (private_key);
char *c_sig_namespace = NULL;
char *armored_sig = NULL;
const void *data;
size_t data_len;
const gssh_symbol_t *hash_type = NULL;
int res;
SCM ret;
ssh_pki_ctx ctx;
SCM_ASSERT (scm_is_bytevector (data_bv), data_bv, SCM_ARG1, FUNC_NAME);
SCM_ASSERT (_private_key_p (kd), private_key, SCM_ARG2, FUNC_NAME);
SCM_ASSERT (scm_is_string (sig_namespace), sig_namespace, SCM_ARG3, FUNC_NAME);
SCM_ASSERT (scm_is_symbol (hash_alg), hash_alg, SCM_ARG4, FUNC_NAME);
scm_dynwind_begin (0);
data_len = scm_c_bytevector_length (data_bv);
data = SCM_BYTEVECTOR_CONTENTS (data_bv);
c_sig_namespace = scm_to_locale_string (sig_namespace);
scm_dynwind_free (c_sig_namespace);
hash_type = gssh_symbol_from_scm (sshsig_hash_types, hash_alg);
if (! hash_type)
guile_ssh_error1 (FUNC_NAME, "Wrong hash type", hash_alg);
ctx = ssh_pki_ctx_new();
res = sshsig_sign (data, data_len, kd->ssh_key, ctx, c_sig_namespace,
hash_type->value, &armored_sig);
if (res == SSH_OK)
{
ret = scm_take_locale_string (armored_sig);
}
else
{
ret = SCM_BOOL_F;
}
scm_dynwind_end ();
return ret;
}
#undef FUNC_NAME
SCM_DEFINE (guile_ssh_verify, "%gssh-verify", 3, 0, 0,
(SCM data_bv, SCM signature, SCM sig_namespace),
"\
Verify a SIGNATURE for binary DATA_BV (bytevector) with SIG_NAMESPACE.\n\
Return the signing key on success, #f on error.\
")
#define FUNC_NAME s_guile_ssh_verify
{
char *c_signature = NULL;
char *c_sig_namespace = NULL;
const void *data;
size_t data_len;
ssh_key sign_key = NULL;
int res;
SCM ret;
SCM_ASSERT (scm_is_bytevector (data_bv), data_bv, SCM_ARG1, FUNC_NAME);
SCM_ASSERT (scm_is_string (signature), signature, SCM_ARG2, FUNC_NAME);
SCM_ASSERT (scm_is_string (sig_namespace), sig_namespace, SCM_ARG3, FUNC_NAME);
scm_dynwind_begin (0);
data_len = scm_c_bytevector_length (data_bv);
data = SCM_BYTEVECTOR_CONTENTS (data_bv);
c_signature = scm_to_locale_string (signature);
scm_dynwind_free (c_signature);
c_sig_namespace = scm_to_locale_string (sig_namespace);
scm_dynwind_free (c_sig_namespace);
res = sshsig_verify (data, data_len, c_signature, c_sig_namespace, &sign_key);
if (res == SSH_OK)
{
ret = gssh_key_to_scm (sign_key, SCM_BOOL_F);
}
else
{
ret = SCM_BOOL_F;
}
scm_dynwind_end ();
return ret;
}
#undef FUNC_NAME
#endif /* HAVE_LIBSSH_0_12 */
/* Initialize Scheme procedures. */
void
init_key_func (void)
{
#include "key-func.x"
}
/* key-func.c ends here */
|