1 /*
   2  * read_bignum():
   3  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
   4  *
   5  * As far as I am concerned, the code I have written for this software
   6  * can be used freely for any purpose.  Any derived versions of this
   7  * software must be clearly marked as such, and if the derived work is
   8  * incompatible with the protocol description in the RFC file, it must be
   9  * called by a name other than "ssh" or "Secure Shell".
  10  *
  11  *
  12  * Copyright (c) 2000, 2001 Markus Friedl.  All rights reserved.
  13  *
  14  * Redistribution and use in source and binary forms, with or without
  15  * modification, are permitted provided that the following conditions
  16  * are met:
  17  * 1. Redistributions of source code must retain the above copyright
  18  *    notice, this list of conditions and the following disclaimer.
  19  * 2. Redistributions in binary form must reproduce the above copyright
  20  *    notice, this list of conditions and the following disclaimer in the
  21  *    documentation and/or other materials provided with the distribution.
  22  *
  23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  25  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  26  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  28  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  30  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  32  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33  */
  34 #include "includes.h"
  35 RCSID("$OpenBSD: key.c,v 1.49 2002/09/09 14:54:14 markus Exp $");
  36 
  37 #pragma ident   "%Z%%M% %I%     %E% SMI"
  38 
  39 #include <openssl/opensslconf.h>
  40 #include <openssl/evp.h>
  41 
  42 #include "xmalloc.h"
  43 #include "key.h"
  44 #include "rsa.h"
  45 #include "ssh-dss.h"
  46 #include "ssh-rsa.h"
  47 #include "uuencode.h"
  48 #include "buffer.h"
  49 #include "bufaux.h"
  50 #include "log.h"
  51 
  52 Key *
  53 key_new(int type)
  54 {
  55         Key *k;
  56         RSA *rsa;
  57         DSA *dsa;
  58         k = xmalloc(sizeof(*k));
  59         k->type = type;
  60         k->flags = 0;
  61         k->dsa = NULL;
  62         k->rsa = NULL;
  63         switch (k->type) {
  64         case KEY_RSA1:
  65         case KEY_RSA:
  66                 if ((rsa = RSA_new()) == NULL)
  67                         fatal("key_new: RSA_new failed");
  68                 if ((rsa->n = BN_new()) == NULL)
  69                         fatal("key_new: BN_new failed");
  70                 if ((rsa->e = BN_new()) == NULL)
  71                         fatal("key_new: BN_new failed");
  72                 k->rsa = rsa;
  73                 break;
  74         case KEY_DSA:
  75                 if ((dsa = DSA_new()) == NULL)
  76                         fatal("key_new: DSA_new failed");
  77                 if ((dsa->p = BN_new()) == NULL)
  78                         fatal("key_new: BN_new failed");
  79                 if ((dsa->q = BN_new()) == NULL)
  80                         fatal("key_new: BN_new failed");
  81                 if ((dsa->g = BN_new()) == NULL)
  82                         fatal("key_new: BN_new failed");
  83                 if ((dsa->pub_key = BN_new()) == NULL)
  84                         fatal("key_new: BN_new failed");
  85                 k->dsa = dsa;
  86                 break;
  87         case KEY_UNSPEC:
  88                 break;
  89         default:
  90                 fatal("key_new: bad key type %d", k->type);
  91                 break;
  92         }
  93         return k;
  94 }
  95 
  96 Key *
  97 key_new_private(int type)
  98 {
  99         Key *k = key_new(type);
 100         switch (k->type) {
 101         case KEY_RSA1:
 102         case KEY_RSA:
 103                 if ((k->rsa->d = BN_new()) == NULL)
 104                         fatal("key_new_private: BN_new failed");
 105                 if ((k->rsa->iqmp = BN_new()) == NULL)
 106                         fatal("key_new_private: BN_new failed");
 107                 if ((k->rsa->q = BN_new()) == NULL)
 108                         fatal("key_new_private: BN_new failed");
 109                 if ((k->rsa->p = BN_new()) == NULL)
 110                         fatal("key_new_private: BN_new failed");
 111                 if ((k->rsa->dmq1 = BN_new()) == NULL)
 112                         fatal("key_new_private: BN_new failed");
 113                 if ((k->rsa->dmp1 = BN_new()) == NULL)
 114                         fatal("key_new_private: BN_new failed");
 115                 break;
 116         case KEY_DSA:
 117                 if ((k->dsa->priv_key = BN_new()) == NULL)
 118                         fatal("key_new_private: BN_new failed");
 119                 break;
 120         case KEY_UNSPEC:
 121                 break;
 122         default:
 123                 break;
 124         }
 125         return k;
 126 }
 127 
 128 void
 129 key_free(Key *k)
 130 {
 131         switch (k->type) {
 132         case KEY_RSA1:
 133         case KEY_RSA:
 134                 if (k->rsa != NULL)
 135                         RSA_free(k->rsa);
 136                 k->rsa = NULL;
 137                 break;
 138         case KEY_DSA:
 139                 if (k->dsa != NULL)
 140                         DSA_free(k->dsa);
 141                 k->dsa = NULL;
 142                 break;
 143         case KEY_UNSPEC:
 144                 break;
 145         default:
 146                 fatal("key_free: bad key type %d", k->type);
 147                 break;
 148         }
 149         xfree(k);
 150 }
 151 int
 152 key_equal(const Key *a, const Key *b)
 153 {
 154         if (a == NULL || b == NULL || a->type != b->type)
 155                 return 0;
 156         switch (a->type) {
 157         case KEY_RSA1:
 158         case KEY_RSA:
 159                 return a->rsa != NULL && b->rsa != NULL &&
 160                     BN_cmp(a->rsa->e, b->rsa->e) == 0 &&
 161                     BN_cmp(a->rsa->n, b->rsa->n) == 0;
 162                 break;
 163         case KEY_DSA:
 164                 return a->dsa != NULL && b->dsa != NULL &&
 165                     BN_cmp(a->dsa->p, b->dsa->p) == 0 &&
 166                     BN_cmp(a->dsa->q, b->dsa->q) == 0 &&
 167                     BN_cmp(a->dsa->g, b->dsa->g) == 0 &&
 168                     BN_cmp(a->dsa->pub_key, b->dsa->pub_key) == 0;
 169                 break;
 170         default:
 171                 fatal("key_equal: bad key type %d", a->type);
 172                 break;
 173         }
 174         return 0;
 175 }
 176 
 177 static u_char *
 178 key_fingerprint_raw(Key *k, enum fp_type dgst_type, u_int *dgst_raw_length)
 179 {
 180         const EVP_MD *md = NULL;
 181         EVP_MD_CTX ctx;
 182         u_char *blob = NULL;
 183         u_char *retval = NULL;
 184         u_int len = 0;
 185         int nlen, elen;
 186 
 187         *dgst_raw_length = 0;
 188 
 189         switch (dgst_type) {
 190         case SSH_FP_MD5:
 191                 md = EVP_md5();
 192                 break;
 193         case SSH_FP_SHA1:
 194                 md = EVP_sha1();
 195                 break;
 196         default:
 197                 fatal("key_fingerprint_raw: bad digest type %d",
 198                     dgst_type);
 199         }
 200         switch (k->type) {
 201         case KEY_RSA1:
 202                 nlen = BN_num_bytes(k->rsa->n);
 203                 elen = BN_num_bytes(k->rsa->e);
 204                 len = nlen + elen;
 205                 blob = xmalloc(len);
 206                 BN_bn2bin(k->rsa->n, blob);
 207                 BN_bn2bin(k->rsa->e, blob + nlen);
 208                 break;
 209         case KEY_DSA:
 210         case KEY_RSA:
 211                 key_to_blob(k, &blob, &len);
 212                 break;
 213         case KEY_UNSPEC:
 214                 return retval;
 215                 break;
 216         default:
 217                 fatal("key_fingerprint_raw: bad key type %d", k->type);
 218                 break;
 219         }
 220         if (blob != NULL) {
 221                 retval = xmalloc(EVP_MAX_MD_SIZE);
 222                 EVP_DigestInit(&ctx, md);
 223                 EVP_DigestUpdate(&ctx, blob, len);
 224                 EVP_DigestFinal(&ctx, retval, dgst_raw_length);
 225                 memset(blob, 0, len);
 226                 xfree(blob);
 227         } else {
 228                 fatal("key_fingerprint_raw: blob is null");
 229         }
 230         return retval;
 231 }
 232 
 233 static char *
 234 key_fingerprint_hex(u_char *dgst_raw, u_int dgst_raw_len)
 235 {
 236         char *retval;
 237         int i;
 238 
 239         retval = xmalloc(dgst_raw_len * 3 + 1);
 240         retval[0] = '\0';
 241         for (i = 0; i < dgst_raw_len; i++) {
 242                 char hex[4];
 243                 snprintf(hex, sizeof(hex), "%02x:", dgst_raw[i]);
 244                 strlcat(retval, hex, dgst_raw_len * 3);
 245         }
 246         retval[(dgst_raw_len * 3) - 1] = '\0';
 247         return retval;
 248 }
 249 
 250 static char *
 251 key_fingerprint_bubblebabble(u_char *dgst_raw, u_int dgst_raw_len)
 252 {
 253         char vowels[] = { 'a', 'e', 'i', 'o', 'u', 'y' };
 254         char consonants[] = { 'b', 'c', 'd', 'f', 'g', 'h', 'k', 'l', 'm',
 255             'n', 'p', 'r', 's', 't', 'v', 'z', 'x' };
 256         u_int i, j = 0, rounds, seed = 1;
 257         char *retval;
 258 
 259         rounds = (dgst_raw_len / 2) + 1;
 260         retval = xmalloc(sizeof(char) * (rounds*6));
 261         retval[j++] = 'x';
 262         for (i = 0; i < rounds; i++) {
 263                 u_int idx0, idx1, idx2, idx3, idx4;
 264                 if ((i + 1 < rounds) || (dgst_raw_len % 2 != 0)) {
 265                         idx0 = (((((u_int)(dgst_raw[2 * i])) >> 6) & 3) +
 266                             seed) % 6;
 267                         idx1 = (((u_int)(dgst_raw[2 * i])) >> 2) & 15;
 268                         idx2 = ((((u_int)(dgst_raw[2 * i])) & 3) +
 269                             (seed / 6)) % 6;
 270                         retval[j++] = vowels[idx0];
 271                         retval[j++] = consonants[idx1];
 272                         retval[j++] = vowels[idx2];
 273                         if ((i + 1) < rounds) {
 274                                 idx3 = (((u_int)(dgst_raw[(2 * i) + 1])) >> 4) & 15;
 275                                 idx4 = (((u_int)(dgst_raw[(2 * i) + 1]))) & 15;
 276                                 retval[j++] = consonants[idx3];
 277                                 retval[j++] = '-';
 278                                 retval[j++] = consonants[idx4];
 279                                 seed = ((seed * 5) +
 280                                     ((((u_int)(dgst_raw[2 * i])) * 7) +
 281                                     ((u_int)(dgst_raw[(2 * i) + 1])))) % 36;
 282                         }
 283                 } else {
 284                         idx0 = seed % 6;
 285                         idx1 = 16;
 286                         idx2 = seed / 6;
 287                         retval[j++] = vowels[idx0];
 288                         retval[j++] = consonants[idx1];
 289                         retval[j++] = vowels[idx2];
 290                 }
 291         }
 292         retval[j++] = 'x';
 293         retval[j++] = '\0';
 294         return retval;
 295 }
 296 
 297 char *
 298 key_fingerprint(Key *k, enum fp_type dgst_type, enum fp_rep dgst_rep)
 299 {
 300         char *retval = NULL;
 301         u_char *dgst_raw;
 302         u_int dgst_raw_len;
 303 
 304         dgst_raw = key_fingerprint_raw(k, dgst_type, &dgst_raw_len);
 305         if (!dgst_raw)
 306                 fatal("key_fingerprint: null from key_fingerprint_raw()");
 307         switch (dgst_rep) {
 308         case SSH_FP_HEX:
 309                 retval = key_fingerprint_hex(dgst_raw, dgst_raw_len);
 310                 break;
 311         case SSH_FP_BUBBLEBABBLE:
 312                 retval = key_fingerprint_bubblebabble(dgst_raw, dgst_raw_len);
 313                 break;
 314         default:
 315                 fatal("key_fingerprint_ex: bad digest representation %d",
 316                     dgst_rep);
 317                 break;
 318         }
 319         memset(dgst_raw, 0, dgst_raw_len);
 320         xfree(dgst_raw);
 321         return retval;
 322 }
 323 
 324 /*
 325  * Reads a multiple-precision integer in decimal from the buffer, and advances
 326  * the pointer.  The integer must already be initialized.  This function is
 327  * permitted to modify the buffer.  This leaves *cpp to point just beyond the
 328  * last processed (and maybe modified) character.  Note that this may modify
 329  * the buffer containing the number.
 330  */
 331 static int
 332 read_bignum(char **cpp, BIGNUM * value)
 333 {
 334         char *cp = *cpp;
 335         int old;
 336 
 337         /* Skip any leading whitespace. */
 338         for (; *cp == ' ' || *cp == '\t'; cp++)
 339                 ;
 340 
 341         /* Check that it begins with a decimal digit. */
 342         if (*cp < '0' || *cp > '9')
 343                 return 0;
 344 
 345         /* Save starting position. */
 346         *cpp = cp;
 347 
 348         /* Move forward until all decimal digits skipped. */
 349         for (; *cp >= '0' && *cp <= '9'; cp++)
 350                 ;
 351 
 352         /* Save the old terminating character, and replace it by \0. */
 353         old = *cp;
 354         *cp = 0;
 355 
 356         /* Parse the number. */
 357         if (BN_dec2bn(&value, *cpp) == 0)
 358                 return 0;
 359 
 360         /* Restore old terminating character. */
 361         *cp = old;
 362 
 363         /* Move beyond the number and return success. */
 364         *cpp = cp;
 365         return 1;
 366 }
 367 
 368 static int
 369 write_bignum(FILE *f, BIGNUM *num)
 370 {
 371         char *buf = BN_bn2dec(num);
 372         if (buf == NULL) {
 373                 error("write_bignum: BN_bn2dec() failed");
 374                 return 0;
 375         }
 376         fprintf(f, " %s", buf);
 377         OPENSSL_free(buf);
 378         return 1;
 379 }
 380 
 381 /* returns 1 ok, -1 error */
 382 int
 383 key_read(Key *ret, char **cpp)
 384 {
 385         Key *k;
 386         int success = -1;
 387         char *cp, *space;
 388         int len, n, type;
 389         u_int bits;
 390         u_char *blob;
 391 
 392         cp = *cpp;
 393 
 394         switch (ret->type) {
 395         case KEY_RSA1:
 396                 /* Get number of bits. */
 397                 if (*cp < '0' || *cp > '9')
 398                         return -1;      /* Bad bit count... */
 399                 for (bits = 0; *cp >= '0' && *cp <= '9'; cp++)
 400                         bits = 10 * bits + *cp - '0';
 401                 if (bits == 0)
 402                         return -1;
 403                 *cpp = cp;
 404                 /* Get public exponent, public modulus. */
 405                 if (!read_bignum(cpp, ret->rsa->e))
 406                         return -1;
 407                 if (!read_bignum(cpp, ret->rsa->n))
 408                         return -1;
 409                 success = 1;
 410                 break;
 411         case KEY_UNSPEC:
 412         case KEY_RSA:
 413         case KEY_DSA:
 414                 space = strchr(cp, ' ');
 415                 if (space == NULL) {
 416                         debug3("key_read: no space");
 417                         return -1;
 418                 }
 419                 *space = '\0';
 420                 type = key_type_from_name(cp);
 421                 *space = ' ';
 422                 if (type == KEY_UNSPEC) {
 423                         debug3("key_read: no key found");
 424                         return -1;
 425                 }
 426                 cp = space+1;
 427                 if (*cp == '\0') {
 428                         debug3("key_read: short string");
 429                         return -1;
 430                 }
 431                 if (ret->type == KEY_UNSPEC) {
 432                         ret->type = type;
 433                 } else if (ret->type != type) {
 434                         /* is a key, but different type */
 435                         debug3("key_read: type mismatch");
 436                         return -1;
 437                 }
 438                 len = 2*strlen(cp);
 439                 blob = xmalloc(len);
 440                 n = uudecode(cp, blob, len);
 441                 if (n < 0) {
 442                         error("key_read: uudecode %s failed", cp);
 443                         xfree(blob);
 444                         return -1;
 445                 }
 446                 k = key_from_blob(blob, n);
 447                 xfree(blob);
 448                 if (k == NULL) {
 449                         error("key_read: key_from_blob %s failed", cp);
 450                         return -1;
 451                 }
 452                 if (k->type != type) {
 453                         error("key_read: type mismatch: encoding error");
 454                         key_free(k);
 455                         return -1;
 456                 }
 457 /*XXXX*/
 458                 if (ret->type == KEY_RSA) {
 459                         if (ret->rsa != NULL)
 460                                 RSA_free(ret->rsa);
 461                         ret->rsa = k->rsa;
 462                         k->rsa = NULL;
 463                         success = 1;
 464 #ifdef DEBUG_PK
 465                         RSA_print_fp(stderr, ret->rsa, 8);
 466 #endif
 467                 } else {
 468                         if (ret->dsa != NULL)
 469                                 DSA_free(ret->dsa);
 470                         ret->dsa = k->dsa;
 471                         k->dsa = NULL;
 472                         success = 1;
 473 #ifdef DEBUG_PK
 474                         DSA_print_fp(stderr, ret->dsa, 8);
 475 #endif
 476                 }
 477 /*XXXX*/
 478                 key_free(k);
 479                 if (success != 1)
 480                         break;
 481                 /* advance cp: skip whitespace and data */
 482                 while (*cp == ' ' || *cp == '\t')
 483                         cp++;
 484                 while (*cp != '\0' && *cp != ' ' && *cp != '\t')
 485                         cp++;
 486                 *cpp = cp;
 487                 break;
 488         default:
 489                 fatal("key_read: bad key type: %d", ret->type);
 490                 break;
 491         }
 492         return success;
 493 }
 494 
 495 int
 496 key_write(const Key *key, FILE *f)
 497 {
 498         int n, success = 0;
 499         u_int len, bits = 0;
 500         u_char *blob;
 501         char *uu;
 502 
 503         if (key->type == KEY_RSA1 && key->rsa != NULL) {
 504                 /* size of modulus 'n' */
 505                 bits = BN_num_bits(key->rsa->n);
 506                 fprintf(f, "%u", bits);
 507                 if (write_bignum(f, key->rsa->e) &&
 508                     write_bignum(f, key->rsa->n)) {
 509                         success = 1;
 510                 } else {
 511                         error("key_write: failed for RSA key");
 512                 }
 513         } else if ((key->type == KEY_DSA && key->dsa != NULL) ||
 514             (key->type == KEY_RSA && key->rsa != NULL)) {
 515                 key_to_blob(key, &blob, &len);
 516                 uu = xmalloc(2*len);
 517                 n = uuencode(blob, len, uu, 2*len);
 518                 if (n > 0) {
 519                         fprintf(f, "%s %s", key_ssh_name(key), uu);
 520                         success = 1;
 521                 }
 522                 xfree(blob);
 523                 xfree(uu);
 524         }
 525         return success;
 526 }
 527 
 528 char *
 529 key_type(Key *k)
 530 {
 531         switch (k->type) {
 532         case KEY_RSA1:
 533                 return "RSA1";
 534                 break;
 535         case KEY_RSA:
 536                 return "RSA";
 537                 break;
 538         case KEY_DSA:
 539                 return "DSA";
 540                 break;
 541         }
 542         return "unknown";
 543 }
 544 
 545 char *
 546 key_ssh_name(const Key *k)
 547 {
 548         switch (k->type) {
 549         case KEY_RSA:
 550                 return "ssh-rsa";
 551                 break;
 552         case KEY_DSA:
 553                 return "ssh-dss";
 554                 break;
 555         }
 556         return "ssh-unknown";
 557 }
 558 
 559 u_int
 560 key_size(Key *k)
 561 {
 562         switch (k->type) {
 563         case KEY_RSA1:
 564         case KEY_RSA:
 565                 return BN_num_bits(k->rsa->n);
 566                 break;
 567         case KEY_DSA:
 568                 return BN_num_bits(k->dsa->p);
 569                 break;
 570         }
 571         return 0;
 572 }
 573 
 574 static RSA *
 575 rsa_generate_private_key(u_int bits)
 576 {
 577         RSA *private;
 578         private = RSA_generate_key(bits, 35, NULL, NULL);
 579         if (private == NULL)
 580                 fatal("rsa_generate_private_key: key generation failed.");
 581         return private;
 582 }
 583 
 584 static DSA*
 585 dsa_generate_private_key(u_int bits)
 586 {
 587         DSA *private = DSA_generate_parameters(bits, NULL, 0, NULL, NULL, NULL, NULL);
 588         if (private == NULL)
 589                 fatal("dsa_generate_private_key: DSA_generate_parameters failed");
 590         if (!DSA_generate_key(private))
 591                 fatal("dsa_generate_private_key: DSA_generate_key failed.");
 592         if (private == NULL)
 593                 fatal("dsa_generate_private_key: NULL.");
 594         return private;
 595 }
 596 
 597 Key *
 598 key_generate(int type, u_int bits)
 599 {
 600         Key *k = key_new(KEY_UNSPEC);
 601         switch (type) {
 602         case KEY_DSA:
 603                 k->dsa = dsa_generate_private_key(bits);
 604                 break;
 605         case KEY_RSA:
 606         case KEY_RSA1:
 607                 k->rsa = rsa_generate_private_key(bits);
 608                 break;
 609         default:
 610                 fatal("key_generate: unknown type %d", type);
 611         }
 612         k->type = type;
 613         return k;
 614 }
 615 
 616 Key *
 617 key_from_private(Key *k)
 618 {
 619         Key *n = NULL;
 620         switch (k->type) {
 621         case KEY_DSA:
 622                 n = key_new(k->type);
 623                 BN_copy(n->dsa->p, k->dsa->p);
 624                 BN_copy(n->dsa->q, k->dsa->q);
 625                 BN_copy(n->dsa->g, k->dsa->g);
 626                 BN_copy(n->dsa->pub_key, k->dsa->pub_key);
 627                 break;
 628         case KEY_RSA:
 629         case KEY_RSA1:
 630                 n = key_new(k->type);
 631                 BN_copy(n->rsa->n, k->rsa->n);
 632                 BN_copy(n->rsa->e, k->rsa->e);
 633                 break;
 634         default:
 635                 fatal("key_from_private: unknown type %d", k->type);
 636                 break;
 637         }
 638         return n;
 639 }
 640 
 641 int
 642 key_type_from_name(char *name)
 643 {
 644         if (strcmp(name, "rsa1") == 0) {
 645                 return KEY_RSA1;
 646         } else if (strcmp(name, "rsa") == 0) {
 647                 return KEY_RSA;
 648         } else if (strcmp(name, "dsa") == 0) {
 649                 return KEY_DSA;
 650         } else if (strcmp(name, "ssh-rsa") == 0) {
 651                 return KEY_RSA;
 652         } else if (strcmp(name, "ssh-dss") == 0) {
 653                 return KEY_DSA;
 654         } else if (strcmp(name, "null") == 0){
 655                 return KEY_NULL;
 656         }
 657         debug2("key_type_from_name: unknown key type '%s'", name);
 658         return KEY_UNSPEC;
 659 }
 660 
 661 int
 662 key_names_valid2(const char *names)
 663 {
 664         char *s, *cp, *p;
 665 
 666         if (names == NULL || strcmp(names, "") == 0)
 667                 return 0;
 668         s = cp = xstrdup(names);
 669         for ((p = strsep(&cp, ",")); p && *p != '\0';
 670             (p = strsep(&cp, ","))) {
 671                 switch (key_type_from_name(p)) {
 672                 case KEY_RSA1:
 673                 case KEY_UNSPEC:
 674                         xfree(s);
 675                         return 0;
 676                 }
 677         }
 678         debug3("key names ok: [%s]", names);
 679         xfree(s);
 680         return 1;
 681 }
 682 
 683 Key *
 684 key_from_blob(u_char *blob, int blen)
 685 {
 686         Buffer b;
 687         char *ktype;
 688         int rlen, type;
 689         Key *key = NULL;
 690 
 691 #ifdef DEBUG_PK
 692         dump_base64(stderr, blob, blen);
 693 #endif
 694         buffer_init(&b);
 695         buffer_append(&b, blob, blen);
 696         if ((ktype = buffer_get_string_ret(&b, NULL)) == NULL) {
 697                 error("key_from_blob: can't read key type");
 698                 goto out;
 699         }
 700 
 701         type = key_type_from_name(ktype);
 702 
 703         switch (type) {
 704         case KEY_RSA:
 705                 key = key_new(type);
 706                 if (buffer_get_bignum2_ret(&b, key->rsa->e) == -1 ||
 707                     buffer_get_bignum2_ret(&b, key->rsa->n) == -1) {
 708                         error("key_from_blob: can't read rsa key");
 709                         key_free(key);
 710                         key = NULL;
 711                         goto out;
 712                 }
 713 #ifdef DEBUG_PK
 714                 RSA_print_fp(stderr, key->rsa, 8);
 715 #endif
 716                 break;
 717         case KEY_DSA:
 718                 key = key_new(type);
 719                 if (buffer_get_bignum2_ret(&b, key->dsa->p) == -1 ||
 720                     buffer_get_bignum2_ret(&b, key->dsa->q) == -1 ||
 721                     buffer_get_bignum2_ret(&b, key->dsa->g) == -1 ||
 722                     buffer_get_bignum2_ret(&b, key->dsa->pub_key) == -1) {
 723                         error("key_from_blob: can't read dsa key");
 724                         key_free(key);
 725                         key = NULL;
 726                         goto out;
 727                 }
 728 #ifdef DEBUG_PK
 729                 DSA_print_fp(stderr, key->dsa, 8);
 730 #endif
 731                 break;
 732         case KEY_UNSPEC:
 733                 key = key_new(type);
 734                 break;
 735         default:
 736                 error("key_from_blob: cannot handle type %s", ktype);
 737                 goto out;
 738         }
 739         rlen = buffer_len(&b);
 740         if (key != NULL && rlen != 0)
 741                 error("key_from_blob: remaining bytes in key blob %d", rlen);
 742  out:
 743         if (ktype != NULL)
 744                 xfree(ktype);
 745         buffer_free(&b);
 746         return key;
 747 }
 748 
 749 int
 750 key_to_blob(const Key *key, u_char **blobp, u_int *lenp)
 751 {
 752         Buffer b;
 753         int len;
 754 
 755         if (key == NULL) {
 756                 error("key_to_blob: key == NULL");
 757                 return 0;
 758         }
 759         buffer_init(&b);
 760         switch (key->type) {
 761         case KEY_DSA:
 762                 buffer_put_cstring(&b, key_ssh_name(key));
 763                 buffer_put_bignum2(&b, key->dsa->p);
 764                 buffer_put_bignum2(&b, key->dsa->q);
 765                 buffer_put_bignum2(&b, key->dsa->g);
 766                 buffer_put_bignum2(&b, key->dsa->pub_key);
 767                 break;
 768         case KEY_RSA:
 769                 buffer_put_cstring(&b, key_ssh_name(key));
 770                 buffer_put_bignum2(&b, key->rsa->e);
 771                 buffer_put_bignum2(&b, key->rsa->n);
 772                 break;
 773         default:
 774                 error("key_to_blob: unsupported key type %d", key->type);
 775                 buffer_free(&b);
 776                 return 0;
 777         }
 778         len = buffer_len(&b);
 779         if (lenp != NULL)
 780                 *lenp = len;
 781         if (blobp != NULL) {
 782                 *blobp = xmalloc(len);
 783                 memcpy(*blobp, buffer_ptr(&b), len);
 784         }
 785         memset(buffer_ptr(&b), 0, len);
 786         buffer_free(&b);
 787         return len;
 788 }
 789 
 790 int
 791 key_sign(
 792     Key *key,
 793     u_char **sigp, u_int *lenp,
 794     u_char *data, u_int datalen)
 795 {
 796         switch (key->type) {
 797         case KEY_DSA:
 798                 return ssh_dss_sign(key, sigp, lenp, data, datalen);
 799                 break;
 800         case KEY_RSA:
 801                 return ssh_rsa_sign(key, sigp, lenp, data, datalen);
 802                 break;
 803         default:
 804                 error("key_sign: illegal key type %d", key->type);
 805                 return -1;
 806                 break;
 807         }
 808 }
 809 
 810 /*
 811  * key_verify returns 1 for a correct signature, 0 for an incorrect signature
 812  * and -1 on error.
 813  */
 814 int
 815 key_verify(
 816     Key *key,
 817     u_char *signature, u_int signaturelen,
 818     u_char *data, u_int datalen)
 819 {
 820         if (signaturelen == 0)
 821                 return -1;
 822 
 823         switch (key->type) {
 824         case KEY_DSA:
 825                 return ssh_dss_verify(key, signature, signaturelen, data, datalen);
 826                 break;
 827         case KEY_RSA:
 828                 return ssh_rsa_verify(key, signature, signaturelen, data, datalen);
 829                 break;
 830         default:
 831                 error("key_verify: illegal key type %d", key->type);
 832                 return -1;
 833                 break;
 834         }
 835 }
 836 
 837 /* Converts a private to a public key */
 838 Key *
 839 key_demote(Key *k)
 840 {
 841         Key *pk;
 842 
 843         pk = xmalloc(sizeof(*pk));
 844         pk->type = k->type;
 845         pk->flags = k->flags;
 846         pk->dsa = NULL;
 847         pk->rsa = NULL;
 848 
 849         switch (k->type) {
 850         case KEY_RSA1:
 851         case KEY_RSA:
 852                 if ((pk->rsa = RSA_new()) == NULL)
 853                         fatal("key_demote: RSA_new failed");
 854                 if ((pk->rsa->e = BN_dup(k->rsa->e)) == NULL)
 855                         fatal("key_demote: BN_dup failed");
 856                 if ((pk->rsa->n = BN_dup(k->rsa->n)) == NULL)
 857                         fatal("key_demote: BN_dup failed");
 858                 break;
 859         case KEY_DSA:
 860                 if ((pk->dsa = DSA_new()) == NULL)
 861                         fatal("key_demote: DSA_new failed");
 862                 if ((pk->dsa->p = BN_dup(k->dsa->p)) == NULL)
 863                         fatal("key_demote: BN_dup failed");
 864                 if ((pk->dsa->q = BN_dup(k->dsa->q)) == NULL)
 865                         fatal("key_demote: BN_dup failed");
 866                 if ((pk->dsa->g = BN_dup(k->dsa->g)) == NULL)
 867                         fatal("key_demote: BN_dup failed");
 868                 if ((pk->dsa->pub_key = BN_dup(k->dsa->pub_key)) == NULL)
 869                         fatal("key_demote: BN_dup failed");
 870                 break;
 871         default:
 872                 fatal("key_free: bad key type %d", k->type);
 873                 break;
 874         }
 875 
 876         return (pk);
 877 }