3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
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".
12 * Copyright (c) 2000 Markus Friedl. All rights reserved.
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
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.
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.
37 #include <openssl/rsa.h>
38 #include <openssl/dsa.h>
39 #include <openssl/evp.h>
49 RCSID("$OpenBSD: key.c,v 1.12 2000/11/12 19:50:37 markus Exp $");
57 k = xmalloc(sizeof(*k));
74 dsa->pub_key = BN_new();
80 fatal("key_new: bad key type %d", k->type);
86 key_new_private(int type)
88 Key *k = key_new(type);
93 k->rsa->iqmp = BN_new();
96 k->rsa->dmq1 = BN_new();
97 k->rsa->dmp1 = BN_new();
100 k->dsa->priv_key = BN_new();
127 fatal("key_free: bad key type %d", k->type);
133 key_equal(Key *a, Key *b)
135 if (a == NULL || b == NULL || a->type != b->type)
140 return a->rsa != NULL && b->rsa != NULL &&
141 BN_cmp(a->rsa->e, b->rsa->e) == 0 &&
142 BN_cmp(a->rsa->n, b->rsa->n) == 0;
145 return a->dsa != NULL && b->dsa != NULL &&
146 BN_cmp(a->dsa->p, b->dsa->p) == 0 &&
147 BN_cmp(a->dsa->q, b->dsa->q) == 0 &&
148 BN_cmp(a->dsa->g, b->dsa->g) == 0 &&
149 BN_cmp(a->dsa->pub_key, b->dsa->pub_key) == 0;
152 fatal("key_equal: bad key type %d", a->type);
159 * Generate key fingerprint in ascii format.
160 * Based on ideas and code from Bjoern Groenvall <bg@sics.se>
163 key_fingerprint(Key *k)
165 static char retval[(EVP_MAX_MD_SIZE+1)*3];
166 unsigned char *blob = NULL;
173 nlen = BN_num_bytes(k->rsa->n);
174 elen = BN_num_bytes(k->rsa->e);
177 BN_bn2bin(k->rsa->n, blob);
178 BN_bn2bin(k->rsa->e, blob + nlen);
182 key_to_blob(k, &blob, &len);
188 fatal("key_fingerprint: bad key type %d", k->type);
193 unsigned char digest[EVP_MAX_MD_SIZE];
194 EVP_MD *md = EVP_md5();
196 EVP_DigestInit(&ctx, md);
197 EVP_DigestUpdate(&ctx, blob, len);
198 EVP_DigestFinal(&ctx, digest, NULL);
199 for(i = 0; i < md->md_size; i++) {
201 snprintf(hex, sizeof(hex), "%02x:", digest[i]);
202 strlcat(retval, hex, sizeof(retval));
204 retval[strlen(retval) - 1] = '\0';
205 memset(blob, 0, len);
212 * Reads a multiple-precision integer in decimal from the buffer, and advances
213 * the pointer. The integer must already be initialized. This function is
214 * permitted to modify the buffer. This leaves *cpp to point just beyond the
215 * last processed (and maybe modified) character. Note that this may modify
216 * the buffer containing the number.
219 read_bignum(char **cpp, BIGNUM * value)
224 /* Skip any leading whitespace. */
225 for (; *cp == ' ' || *cp == '\t'; cp++)
228 /* Check that it begins with a decimal digit. */
229 if (*cp < '0' || *cp > '9')
232 /* Save starting position. */
235 /* Move forward until all decimal digits skipped. */
236 for (; *cp >= '0' && *cp <= '9'; cp++)
239 /* Save the old terminating character, and replace it by \0. */
243 /* Parse the number. */
244 if (BN_dec2bn(&value, *cpp) == 0)
247 /* Restore old terminating character. */
250 /* Move beyond the number and return success. */
255 write_bignum(FILE *f, BIGNUM *num)
257 char *buf = BN_bn2dec(num);
259 error("write_bignum: BN_bn2dec() failed");
262 fprintf(f, " %s", buf);
267 /* returns 1 ok, -1 error, 0 type mismatch */
269 key_read(Key *ret, char **cpp)
282 /* Get number of bits. */
283 if (*cp < '0' || *cp > '9')
284 return -1; /* Bad bit count... */
285 for (bits = 0; *cp >= '0' && *cp <= '9'; cp++)
286 bits = 10 * bits + *cp - '0';
290 /* Get public exponent, public modulus. */
291 if (!read_bignum(cpp, ret->rsa->e))
293 if (!read_bignum(cpp, ret->rsa->n))
300 space = strchr(cp, ' ');
302 debug3("key_read: no space");
306 type = key_type_from_name(cp);
308 if (type == KEY_UNSPEC) {
309 debug3("key_read: no key found");
314 debug3("key_read: short string");
317 if (ret->type == KEY_UNSPEC) {
319 } else if (ret->type != type) {
320 /* is a key, but different type */
321 debug3("key_read: type mismatch");
326 n = uudecode(cp, blob, len);
328 error("key_read: uudecode %s failed", cp);
331 k = key_from_blob(blob, n);
333 error("key_read: key_from_blob %s failed", cp);
337 if (k->type != type) {
338 error("key_read: type mismatch: encoding error");
343 if (ret->type == KEY_RSA) {
344 if (ret->rsa != NULL)
350 RSA_print_fp(stderr, ret->rsa, 8);
353 if (ret->dsa != NULL)
359 DSA_print_fp(stderr, ret->dsa, 8);
366 /* advance cp: skip whitespace and data */
367 while (*cp == ' ' || *cp == '\t')
369 while (*cp != '\0' && *cp != ' ' && *cp != '\t')
374 fatal("key_read: bad key type: %d", ret->type);
380 key_write(Key *key, FILE *f)
383 unsigned int bits = 0;
385 if (key->type == KEY_RSA1 && key->rsa != NULL) {
386 /* size of modulus 'n' */
387 bits = BN_num_bits(key->rsa->n);
388 fprintf(f, "%u", bits);
389 if (write_bignum(f, key->rsa->e) &&
390 write_bignum(f, key->rsa->n)) {
393 error("key_write: failed for RSA key");
395 } else if ((key->type == KEY_DSA && key->dsa != NULL) ||
396 (key->type == KEY_RSA && key->rsa != NULL)) {
398 unsigned char *blob, *uu;
399 key_to_blob(key, &blob, &len);
401 n = uuencode(blob, len, uu, 2*len);
403 fprintf(f, "%s %s", key_ssh_name(key), uu);
438 return "ssh-unknown";
445 return BN_num_bits(k->rsa->n);
448 return BN_num_bits(k->dsa->p);
455 rsa_generate_private_key(unsigned int bits)
458 private = RSA_generate_key(bits, 35, NULL, NULL);
460 fatal("rsa_generate_private_key: key generation failed.");
465 dsa_generate_private_key(unsigned int bits)
467 DSA *private = DSA_generate_parameters(bits, NULL, 0, NULL, NULL, NULL, NULL);
469 fatal("dsa_generate_private_key: DSA_generate_parameters failed");
470 if (!DSA_generate_key(private))
471 fatal("dsa_generate_private_key: DSA_generate_key failed.");
473 fatal("dsa_generate_private_key: NULL.");
478 key_generate(int type, unsigned int bits)
480 Key *k = key_new(KEY_UNSPEC);
483 k->dsa = dsa_generate_private_key(bits);
487 k->rsa = rsa_generate_private_key(bits);
490 fatal("key_generate: unknown type %d", type);
497 key_from_private(Key *k)
502 n = key_new(k->type);
503 BN_copy(n->dsa->p, k->dsa->p);
504 BN_copy(n->dsa->q, k->dsa->q);
505 BN_copy(n->dsa->g, k->dsa->g);
506 BN_copy(n->dsa->pub_key, k->dsa->pub_key);
510 n = key_new(k->type);
511 BN_copy(n->rsa->n, k->rsa->n);
512 BN_copy(n->rsa->e, k->rsa->e);
515 fatal("key_from_private: unknown type %d", k->type);
522 key_type_from_name(char *name)
524 if (strcmp(name, "rsa1") == 0){
526 } else if (strcmp(name, "rsa") == 0){
528 } else if (strcmp(name, "dsa") == 0){
530 } else if (strcmp(name, "ssh-rsa") == 0){
532 } else if (strcmp(name, "ssh-dss") == 0){
535 debug("key_type_from_name: unknown key type '%s'", name);
540 key_from_blob(char *blob, int blen)
548 dump_base64(stderr, blob, blen);
551 buffer_append(&b, blob, blen);
552 ktype = buffer_get_string(&b, NULL);
553 type = key_type_from_name(ktype);
558 buffer_get_bignum2(&b, key->rsa->n);
559 buffer_get_bignum2(&b, key->rsa->e);
561 RSA_print_fp(stderr, key->rsa, 8);
566 buffer_get_bignum2(&b, key->dsa->p);
567 buffer_get_bignum2(&b, key->dsa->q);
568 buffer_get_bignum2(&b, key->dsa->g);
569 buffer_get_bignum2(&b, key->dsa->pub_key);
571 DSA_print_fp(stderr, key->dsa, 8);
578 error("key_from_blob: cannot handle type %s", ktype);
581 rlen = buffer_len(&b);
582 if (key != NULL && rlen != 0)
583 error("key_from_blob: remaining bytes in key blob %d", rlen);
590 key_to_blob(Key *key, unsigned char **blobp, unsigned int *lenp)
597 error("key_to_blob: key == NULL");
603 buffer_put_cstring(&b, key_ssh_name(key));
604 buffer_put_bignum2(&b, key->dsa->p);
605 buffer_put_bignum2(&b, key->dsa->q);
606 buffer_put_bignum2(&b, key->dsa->g);
607 buffer_put_bignum2(&b, key->dsa->pub_key);
610 buffer_put_cstring(&b, key_ssh_name(key));
611 buffer_put_bignum2(&b, key->rsa->n);
612 buffer_put_bignum2(&b, key->rsa->e);
615 error("key_to_blob: illegal key type %d", key->type);
618 len = buffer_len(&b);
620 memcpy(buf, buffer_ptr(&b), len);
621 memset(buffer_ptr(&b), 0, len);
633 unsigned char **sigp, int *lenp,
634 unsigned char *data, int datalen)
638 return ssh_dss_sign(key, sigp, lenp, data, datalen);
641 return ssh_rsa_sign(key, sigp, lenp, data, datalen);
644 error("key_sign: illegal key type %d", key->type);
653 unsigned char *signature, int signaturelen,
654 unsigned char *data, int datalen)
658 return ssh_dss_verify(key, signature, signaturelen, data, datalen);
661 return ssh_rsa_verify(key, signature, signaturelen, data, datalen);
664 error("key_verify: illegal key type %d", key->type);