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
|
/* lsh-export-key.c
*
* Reads an sexp in given form, and writes it in ssh2 form.
*
*/
/* lsh, an implementation of the ssh protocol
*
* Copyright (C) 2000 Jean-Pierre Stierlin, Niels Mller
*
* This program 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 2 of the
* License, or (at your option) any later version.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/* Test output:
[nisse@cuckoo src]$ ./lsh-export-key < testkey.pub
---- BEGIN SSH2 PUBLIC KEY ----
Comment: "768-bit dsa"
AAAAB3NzaC1kc3MAAABhAJw3J7CMyAKiX8F1Mz1dNguVQi7VZQQrLl8DeWNQaSkqmIPjsc
zSn4Cjv9BOt8FM46AZbw+aSou0jpiFPJJiQjpT5U1ArPLoMqRpopqcZqcVubRKALTzytgw
vvXyoHb84wAAABUAmm14nnnHQtwx5ZUgRrjv98iv4KcAAABgENZmq1qm4jdJJB7IAC5Ecr
vcjhlACNcPD4UQ0Bgk66/MJOxvrwf0V+ZtTfb8ZaQlKdu84vB2VxVcB8zo0ds01I6eLG2f
/nDENvwp0TkNKf1uyEWPjNQGI/ImAqukiSWjAAAAYDe6o/C8faYCpuduLPQrl8Co6z7HgC
yIaRCzBjD8bY6L5qZp4G//8PVJVhxXh3vAS6LbgDCFoa2HZ1/vxHpML+gl3FPjAOxZPs27
B2CTISEmV3KYx5NJpyKC3IBw/ckP6Q==
---- END SSH2 PUBLIC KEY ----
*/
#if HAVE_CONFIG_H
#include "config.h"
#endif
#include <assert.h>
#include <fcntl.h>
/* #include <string.h> */
#include <unistd.h>
#include <nettle/base64.h>
#include <nettle/sexp.h>
#include "algorithms.h"
#include "alist.h"
#include "atoms.h"
#include "crypto.h"
#include "format.h"
#include "io.h"
#include "lsh.h"
#include "lsh_argp.h"
#include "lsh_string.h"
#include "spki.h"
#include "version.h"
#include "werror.h"
#include "xalloc.h"
enum output_mode
{
OUTPUT_STANDARD = 0,
OUTPUT_FINGERPRINT = 1,
OUTPUT_OPENSSH = 2,
};
#include "lsh-export-key.c.x"
static struct lsh_string *
make_header(const char *name, const char *value)
{
return value
? ssh_format("%lz: %lz\n", name, value)
: ssh_format("");
}
#define BLOCKS 10
#define LINE_LENGTH (BASE64_TEXT_BLOCK_SIZE * BLOCKS)
#define BINARY_LENGTH (BASE64_BINARY_BLOCK_SIZE * BLOCKS)
/* Includes a newline at the end. */
static struct lsh_string *
encode_base64(const struct lsh_string *s)
{
uint32_t input_length = lsh_string_length(s);
const uint8_t *data = lsh_string_data(s);
unsigned encoded_length = BASE64_ENCODE_RAW_LENGTH(input_length);
unsigned lines = (encoded_length + LINE_LENGTH - 1) / LINE_LENGTH;
struct base64_encode_ctx ctx;
unsigned length = encoded_length + lines;
unsigned line;
unsigned final;
unsigned out;
struct lsh_string *res = lsh_string_alloc(length+3);
base64_encode_init(&ctx);
for (out = 0, line = 0; line + 1 < lines; line++)
{
lsh_string_base64_encode_update(res, out,
&ctx,
BINARY_LENGTH, data + line * BINARY_LENGTH);
out += LINE_LENGTH;
lsh_string_putc(res, out++, '\n');
}
final = lsh_string_base64_encode_update(res, out,
&ctx,
input_length - line * BINARY_LENGTH,
data + line * BINARY_LENGTH);
final += lsh_string_base64_encode_final(res, out+final, &ctx);
if (final)
{
out += final;
lsh_string_putc(res, out++, '\n');
}
assert(out == length);
lsh_string_trunc(res, length);
lsh_string_free(s);
return res;
}
static struct lsh_string *
encode_base64_line(const struct lsh_string *s)
{
uint32_t input_length = lsh_string_length(s);
const uint8_t *data = lsh_string_data(s);
unsigned encoded_length = BASE64_ENCODE_RAW_LENGTH(input_length);
struct base64_encode_ctx ctx;
unsigned out;
struct lsh_string *res = lsh_string_alloc(encoded_length + BASE64_ENCODE_FINAL_LENGTH);
base64_encode_init(&ctx);
out = lsh_string_base64_encode_update(res, 0,
&ctx,
input_length, data);
out += lsh_string_base64_encode_final(res, out, &ctx);
assert (out == encoded_length);
lsh_string_trunc(res, out);
lsh_string_free(s);
return res;
}
static struct lsh_string *
sexp_to_ssh2_key(struct lsh_string *expr,
struct export_key_options *options)
{
struct sexp_iterator i;
struct verifier *v;
int algorithm_name;
struct lsh_string *key;
if (!(lsh_string_transport_iterator_first(expr, &i)
&& sexp_iterator_check_type(&i, "public-key")))
{
werror("Only conversion of public keys implemented.\n");
return NULL;
}
v = spki_make_verifier(options->algorithms, &i, &algorithm_name);
if (!v)
{
werror("Unsupported algorithm\n");
return NULL;
}
key = PUBLIC_KEY(v);
switch (options->mode)
{
default:
fatal("Internal error.\n");
case OUTPUT_STANDARD:
return ssh_format("---- BEGIN SSH2 PUBLIC KEY ----\n"
"%lfS"
"%lfS"
"\n%lfS"
"---- END SSH2 PUBLIC KEY ----\n",
make_header("Subject", options->subject),
make_header("Comment", options->comment),
encode_base64(key));
case OUTPUT_FINGERPRINT:
{
struct lsh_string *output
= ssh_format("MD5 fingerprint: %lfS\n"
"Bubble Babble: %lfS\n",
lsh_string_colonize(
ssh_format("%lfxS",
hash_string(&crypto_md5_algorithm, key, 0)),
2, 1),
lsh_string_bubblebabble(
hash_string(&crypto_sha1_algorithm, key, 0), 1));
lsh_string_free(key);
return output;
}
case OUTPUT_OPENSSH:
{
key = encode_base64_line(key);
switch (algorithm_name)
{
default:
werror("Keys of type %a not supported.\n");
lsh_string_free(key);
return NULL;
case ATOM_RSA_PKCS1_SHA1:
case ATOM_RSA_PKCS1_MD5:
case ATOM_RSA_PKCS1:
return ssh_format("%la %lfS\n", ATOM_SSH_RSA, key);
case ATOM_DSA:
return ssh_format("%la %lfS\n", ATOM_SSH_DSS, key);
}
}
}
}
/* Option parsing */
const char *argp_program_version
= "lsh-export-key-" VERSION;
const char *argp_program_bug_address = BUG_ADDRESS;
#define OPT_INFILE 'r'
#define OPT_OUTFILE 'o'
#define OPT_SUBJECT 's'
#define OPT_COMMENT 'c'
#define OPT_OPENSSH 0x100
#define OPT_FINGERPRINT 0x101
static const struct argp_option
main_options[] =
{
/* Name, key, arg-name, flags, doc, group */
{ "input-file", OPT_INFILE, "Filename", 0, "Default is stdin", 0 },
{ "output-file", OPT_OUTFILE, "Filename", 0, "Default is stdout", 0 },
{ "subject", OPT_SUBJECT, "subject string", 0, "Add subject to output key.", 0 },
{ "comment", OPT_COMMENT, "comment string", 0, "Add comment to output key.", 0 },
{ "fingerprint", OPT_FINGERPRINT, NULL, 0, "Show key fingerprint.", 0 },
{ "openssh", OPT_OPENSSH, NULL, 0, "Output key in openssh single-line format.", 0 },
{ NULL, 0, NULL, 0, NULL, 0 }
};
/* GABA:
(class
(name export_key_options)
(vars
(algorithms object alist)
(mode . "enum output_mode")
(infile . "const char *")
(outfile . "const char *")
(subject . "const char *")
(comment . "const char *")))
*/
static struct export_key_options *
make_options(void)
{
NEW(export_key_options, self);
self->infile = NULL;
self->subject = NULL;
self->comment = NULL;
self->mode = OUTPUT_STANDARD;
self->algorithms = all_signature_algorithms(NULL);
return self;
}
static const struct argp_child
main_argp_children[] =
{
{ &werror_argp, 0, "", 0 },
{ NULL, 0, NULL, 0}
};
static error_t
main_argp_parser(int key, char *arg, struct argp_state *state)
{
CAST(export_key_options, self, state->input);
switch(key)
{
default:
return ARGP_ERR_UNKNOWN;
case ARGP_KEY_INIT:
state->child_inputs[0] = NULL;
break;
case ARGP_KEY_END:
break;
case OPT_INFILE:
self->infile = arg;
break;
case OPT_OUTFILE:
self->outfile = arg;
break;
case OPT_SUBJECT:
self->subject = arg;
break;
case OPT_COMMENT:
self->comment = arg;
break;
case OPT_FINGERPRINT:
self->mode = OUTPUT_FINGERPRINT;
break;
case OPT_OPENSSH:
self->mode = OUTPUT_OPENSSH;
break;
}
return 0;
}
static const struct argp
main_argp =
{ main_options, main_argp_parser, NULL,
"Reads an s-expression on stdin, and outputs the same "
"s-expression on stdout, using OpenSSH/SSH2 encoding format.",
main_argp_children,
NULL, NULL
};
#define SEXP_BUFFER_SIZE 1024
#ifdef MACOS
char *applname = "lsh-export-key";
//char *defargstr = "-r identity.pub";
char *defargstr = "";
int appl_main(int argc, char **argv);
#define main appl_main
#endif
#define MAX_KEY_SIZE 10000
int main(int argc, char **argv)
{
struct export_key_options *options = make_options();
const struct exception *e;
int in = STDIN_FILENO;
int out = STDOUT_FILENO;
struct lsh_string *input;
struct lsh_string *output;
argp_parse(&main_argp, argc, argv, 0, NULL, options);
if (options->infile)
{
in = open(options->infile, O_RDONLY);
if (in < 0)
{
werror("Failed to open '%z' for reading %e\n",
options->infile, errno);
return EXIT_FAILURE;
}
}
if (options->outfile)
{
out = open(options->outfile,
O_WRONLY | O_CREAT, 0666);
if (out < 0)
{
werror("Failed to open '%z' for writing %e\n",
options->outfile, errno);
return EXIT_FAILURE;
}
}
/* Guess size 5000 */
input = io_read_file_raw(in, 5000);
if (!input)
{
werror("Failed to read '%z' %e\n",
options->infile, errno);
return EXIT_FAILURE;
}
output = sexp_to_ssh2_key(input, options);
lsh_string_free(input);
if (!output)
return EXIT_FAILURE;
e = write_raw(out, STRING_LD(output));
lsh_string_free(output);
if (e)
{
werror("%z\n", e->msg);
return EXIT_FAILURE;
}
gc_final();
return EXIT_SUCCESS;
}
|