blob: 3e17da601621b6ae5b8107e5b550c88f62f1532a [file] [log] [blame]
Damien Miller2f54ada2008-11-03 19:24:16 +11001/* $OpenBSD: key.c,v 1.80 2008/10/10 05:00:12 stevesk Exp $ */
Damien Miller450a7a12000-03-26 13:04:51 +10002/*
Damien Millere4340be2000-09-16 13:29:08 +11003 * read_bignum():
4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 *
6 * As far as I am concerned, the code I have written for this software
7 * can be used freely for any purpose. Any derived versions of this
8 * software must be clearly marked as such, and if the derived work is
9 * incompatible with the protocol description in the RFC file, it must be
10 * called by a name other than "ssh" or "Secure Shell".
11 *
12 *
Ben Lindstrom44697232001-07-04 03:32:30 +000013 * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
Darren Tucker0f0ef0a2008-06-13 08:58:05 +100014 * Copyright (c) 2008 Alexander von Gernler. All rights reserved.
Damien Miller450a7a12000-03-26 13:04:51 +100015 *
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
18 * are met:
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
Damien Miller450a7a12000-03-26 13:04:51 +100024 *
25 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
26 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
29 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 */
Damien Millerd7834352006-08-05 12:39:39 +100036
Damien Miller450a7a12000-03-26 13:04:51 +100037#include "includes.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000038
Darren Tucker9c16ac92008-06-13 04:40:35 +100039#include <sys/param.h>
Damien Millerd7834352006-08-05 12:39:39 +100040#include <sys/types.h>
41
Damien Miller450a7a12000-03-26 13:04:51 +100042#include <openssl/evp.h>
Darren Tucker3d295a62008-02-28 19:22:04 +110043#include <openbsd-compat/openssl-compat.h>
Ben Lindstrom226cfa02001-01-22 05:34:40 +000044
Damien Millerded319c2006-09-01 15:38:36 +100045#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100046#include <stdio.h>
Damien Millere3476ed2006-07-24 14:13:33 +100047#include <string.h>
48
Damien Miller450a7a12000-03-26 13:04:51 +100049#include "xmalloc.h"
50#include "key.h"
Damien Miller0bc1bd82000-11-13 22:57:25 +110051#include "rsa.h"
Damien Millereba71ba2000-04-29 23:57:08 +100052#include "uuencode.h"
Damien Miller0bc1bd82000-11-13 22:57:25 +110053#include "buffer.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000054#include "log.h"
Damien Miller450a7a12000-03-26 13:04:51 +100055
56Key *
57key_new(int type)
58{
59 Key *k;
60 RSA *rsa;
61 DSA *dsa;
Damien Miller07d86be2006-03-26 14:19:21 +110062 k = xcalloc(1, sizeof(*k));
Damien Miller450a7a12000-03-26 13:04:51 +100063 k->type = type;
Damien Millereba71ba2000-04-29 23:57:08 +100064 k->dsa = NULL;
65 k->rsa = NULL;
Damien Miller450a7a12000-03-26 13:04:51 +100066 switch (k->type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +110067 case KEY_RSA1:
Damien Miller450a7a12000-03-26 13:04:51 +100068 case KEY_RSA:
Damien Millerda755162002-01-22 23:09:22 +110069 if ((rsa = RSA_new()) == NULL)
70 fatal("key_new: RSA_new failed");
71 if ((rsa->n = BN_new()) == NULL)
72 fatal("key_new: BN_new failed");
73 if ((rsa->e = BN_new()) == NULL)
74 fatal("key_new: BN_new failed");
Damien Miller450a7a12000-03-26 13:04:51 +100075 k->rsa = rsa;
76 break;
77 case KEY_DSA:
Damien Millerda755162002-01-22 23:09:22 +110078 if ((dsa = DSA_new()) == NULL)
79 fatal("key_new: DSA_new failed");
80 if ((dsa->p = BN_new()) == NULL)
81 fatal("key_new: BN_new failed");
82 if ((dsa->q = BN_new()) == NULL)
83 fatal("key_new: BN_new failed");
84 if ((dsa->g = BN_new()) == NULL)
85 fatal("key_new: BN_new failed");
86 if ((dsa->pub_key = BN_new()) == NULL)
87 fatal("key_new: BN_new failed");
Damien Miller450a7a12000-03-26 13:04:51 +100088 k->dsa = dsa;
89 break;
Damien Miller0bc1bd82000-11-13 22:57:25 +110090 case KEY_UNSPEC:
Damien Miller450a7a12000-03-26 13:04:51 +100091 break;
92 default:
93 fatal("key_new: bad key type %d", k->type);
94 break;
95 }
96 return k;
97}
Ben Lindstrom836f0e92002-06-23 21:21:30 +000098
Damien Miller0bc1bd82000-11-13 22:57:25 +110099Key *
100key_new_private(int type)
101{
102 Key *k = key_new(type);
103 switch (k->type) {
104 case KEY_RSA1:
105 case KEY_RSA:
Damien Millerda755162002-01-22 23:09:22 +1100106 if ((k->rsa->d = BN_new()) == NULL)
107 fatal("key_new_private: BN_new failed");
108 if ((k->rsa->iqmp = BN_new()) == NULL)
109 fatal("key_new_private: BN_new failed");
110 if ((k->rsa->q = BN_new()) == NULL)
111 fatal("key_new_private: BN_new failed");
112 if ((k->rsa->p = BN_new()) == NULL)
113 fatal("key_new_private: BN_new failed");
114 if ((k->rsa->dmq1 = BN_new()) == NULL)
115 fatal("key_new_private: BN_new failed");
116 if ((k->rsa->dmp1 = BN_new()) == NULL)
117 fatal("key_new_private: BN_new failed");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100118 break;
119 case KEY_DSA:
Damien Millerda755162002-01-22 23:09:22 +1100120 if ((k->dsa->priv_key = BN_new()) == NULL)
121 fatal("key_new_private: BN_new failed");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100122 break;
123 case KEY_UNSPEC:
124 break;
125 default:
126 break;
127 }
128 return k;
129}
Ben Lindstrom836f0e92002-06-23 21:21:30 +0000130
Damien Miller450a7a12000-03-26 13:04:51 +1000131void
132key_free(Key *k)
133{
Damien Miller429fcc22006-03-26 14:02:16 +1100134 if (k == NULL)
Damien Millerbbaad772006-03-26 14:03:03 +1100135 fatal("key_free: key is NULL");
Damien Miller450a7a12000-03-26 13:04:51 +1000136 switch (k->type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100137 case KEY_RSA1:
Damien Miller450a7a12000-03-26 13:04:51 +1000138 case KEY_RSA:
139 if (k->rsa != NULL)
140 RSA_free(k->rsa);
141 k->rsa = NULL;
142 break;
143 case KEY_DSA:
144 if (k->dsa != NULL)
145 DSA_free(k->dsa);
146 k->dsa = NULL;
147 break;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100148 case KEY_UNSPEC:
149 break;
Damien Miller450a7a12000-03-26 13:04:51 +1000150 default:
151 fatal("key_free: bad key type %d", k->type);
152 break;
153 }
154 xfree(k);
155}
Damien Millerf58b58c2003-11-17 21:18:23 +1100156
Damien Miller450a7a12000-03-26 13:04:51 +1000157int
Damien Millerf58b58c2003-11-17 21:18:23 +1100158key_equal(const Key *a, const Key *b)
Damien Miller450a7a12000-03-26 13:04:51 +1000159{
160 if (a == NULL || b == NULL || a->type != b->type)
161 return 0;
162 switch (a->type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100163 case KEY_RSA1:
Damien Miller450a7a12000-03-26 13:04:51 +1000164 case KEY_RSA:
165 return a->rsa != NULL && b->rsa != NULL &&
166 BN_cmp(a->rsa->e, b->rsa->e) == 0 &&
167 BN_cmp(a->rsa->n, b->rsa->n) == 0;
Damien Miller450a7a12000-03-26 13:04:51 +1000168 case KEY_DSA:
169 return a->dsa != NULL && b->dsa != NULL &&
170 BN_cmp(a->dsa->p, b->dsa->p) == 0 &&
171 BN_cmp(a->dsa->q, b->dsa->q) == 0 &&
172 BN_cmp(a->dsa->g, b->dsa->g) == 0 &&
173 BN_cmp(a->dsa->pub_key, b->dsa->pub_key) == 0;
Damien Miller450a7a12000-03-26 13:04:51 +1000174 default:
Damien Millereba71ba2000-04-29 23:57:08 +1000175 fatal("key_equal: bad key type %d", a->type);
Damien Miller450a7a12000-03-26 13:04:51 +1000176 }
Damien Miller87dd5f22008-07-11 17:35:09 +1000177 /* NOTREACHED */
Damien Miller450a7a12000-03-26 13:04:51 +1000178}
179
Damien Miller37876e92003-05-15 10:19:46 +1000180u_char*
Damien Millerf58b58c2003-11-17 21:18:23 +1100181key_fingerprint_raw(const Key *k, enum fp_type dgst_type,
182 u_int *dgst_raw_length)
Damien Miller450a7a12000-03-26 13:04:51 +1000183{
Ben Lindstrom80cb27d2002-03-05 01:33:36 +0000184 const EVP_MD *md = NULL;
Ben Lindstromf0b48532001-03-12 02:59:31 +0000185 EVP_MD_CTX ctx;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000186 u_char *blob = NULL;
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000187 u_char *retval = NULL;
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000188 u_int len = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000189 int nlen, elen;
Damien Miller450a7a12000-03-26 13:04:51 +1000190
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000191 *dgst_raw_length = 0;
192
Ben Lindstromf0b48532001-03-12 02:59:31 +0000193 switch (dgst_type) {
194 case SSH_FP_MD5:
195 md = EVP_md5();
196 break;
197 case SSH_FP_SHA1:
198 md = EVP_sha1();
199 break;
200 default:
201 fatal("key_fingerprint_raw: bad digest type %d",
202 dgst_type);
203 }
Damien Miller450a7a12000-03-26 13:04:51 +1000204 switch (k->type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100205 case KEY_RSA1:
Damien Miller450a7a12000-03-26 13:04:51 +1000206 nlen = BN_num_bytes(k->rsa->n);
207 elen = BN_num_bytes(k->rsa->e);
208 len = nlen + elen;
Damien Millereba71ba2000-04-29 23:57:08 +1000209 blob = xmalloc(len);
210 BN_bn2bin(k->rsa->n, blob);
211 BN_bn2bin(k->rsa->e, blob + nlen);
Damien Miller450a7a12000-03-26 13:04:51 +1000212 break;
213 case KEY_DSA:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100214 case KEY_RSA:
215 key_to_blob(k, &blob, &len);
216 break;
217 case KEY_UNSPEC:
218 return retval;
Damien Miller450a7a12000-03-26 13:04:51 +1000219 default:
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000220 fatal("key_fingerprint_raw: bad key type %d", k->type);
Damien Miller450a7a12000-03-26 13:04:51 +1000221 break;
222 }
Damien Millereba71ba2000-04-29 23:57:08 +1000223 if (blob != NULL) {
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000224 retval = xmalloc(EVP_MAX_MD_SIZE);
Damien Miller6536c7d2000-06-22 21:32:31 +1000225 EVP_DigestInit(&ctx, md);
226 EVP_DigestUpdate(&ctx, blob, len);
Damien Miller3672e4b2002-02-05 11:54:07 +1100227 EVP_DigestFinal(&ctx, retval, dgst_raw_length);
Damien Millereba71ba2000-04-29 23:57:08 +1000228 memset(blob, 0, len);
229 xfree(blob);
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000230 } else {
231 fatal("key_fingerprint_raw: blob is null");
Damien Miller450a7a12000-03-26 13:04:51 +1000232 }
233 return retval;
234}
235
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000236static char *
237key_fingerprint_hex(u_char *dgst_raw, u_int dgst_raw_len)
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000238{
239 char *retval;
Damien Millereccb9de2005-06-17 12:59:34 +1000240 u_int i;
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000241
Damien Miller07d86be2006-03-26 14:19:21 +1100242 retval = xcalloc(1, dgst_raw_len * 3 + 1);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100243 for (i = 0; i < dgst_raw_len; i++) {
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000244 char hex[4];
245 snprintf(hex, sizeof(hex), "%02x:", dgst_raw[i]);
Darren Tucker29588612003-07-14 17:28:34 +1000246 strlcat(retval, hex, dgst_raw_len * 3 + 1);
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000247 }
Darren Tucker29588612003-07-14 17:28:34 +1000248
249 /* Remove the trailing ':' character */
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000250 retval[(dgst_raw_len * 3) - 1] = '\0';
251 return retval;
252}
253
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000254static char *
255key_fingerprint_bubblebabble(u_char *dgst_raw, u_int dgst_raw_len)
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000256{
257 char vowels[] = { 'a', 'e', 'i', 'o', 'u', 'y' };
258 char consonants[] = { 'b', 'c', 'd', 'f', 'g', 'h', 'k', 'l', 'm',
259 'n', 'p', 'r', 's', 't', 'v', 'z', 'x' };
Ben Lindstromcbe3ad22001-03-11 20:06:59 +0000260 u_int i, j = 0, rounds, seed = 1;
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000261 char *retval;
262
263 rounds = (dgst_raw_len / 2) + 1;
Damien Miller07d86be2006-03-26 14:19:21 +1100264 retval = xcalloc((rounds * 6), sizeof(char));
Ben Lindstromcbe3ad22001-03-11 20:06:59 +0000265 retval[j++] = 'x';
266 for (i = 0; i < rounds; i++) {
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000267 u_int idx0, idx1, idx2, idx3, idx4;
Ben Lindstromcbe3ad22001-03-11 20:06:59 +0000268 if ((i + 1 < rounds) || (dgst_raw_len % 2 != 0)) {
269 idx0 = (((((u_int)(dgst_raw[2 * i])) >> 6) & 3) +
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000270 seed) % 6;
Ben Lindstromcbe3ad22001-03-11 20:06:59 +0000271 idx1 = (((u_int)(dgst_raw[2 * i])) >> 2) & 15;
272 idx2 = ((((u_int)(dgst_raw[2 * i])) & 3) +
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000273 (seed / 6)) % 6;
Ben Lindstromcbe3ad22001-03-11 20:06:59 +0000274 retval[j++] = vowels[idx0];
275 retval[j++] = consonants[idx1];
276 retval[j++] = vowels[idx2];
277 if ((i + 1) < rounds) {
278 idx3 = (((u_int)(dgst_raw[(2 * i) + 1])) >> 4) & 15;
279 idx4 = (((u_int)(dgst_raw[(2 * i) + 1]))) & 15;
280 retval[j++] = consonants[idx3];
281 retval[j++] = '-';
282 retval[j++] = consonants[idx4];
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000283 seed = ((seed * 5) +
Ben Lindstromcbe3ad22001-03-11 20:06:59 +0000284 ((((u_int)(dgst_raw[2 * i])) * 7) +
285 ((u_int)(dgst_raw[(2 * i) + 1])))) % 36;
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000286 }
287 } else {
288 idx0 = seed % 6;
289 idx1 = 16;
290 idx2 = seed / 6;
Ben Lindstromcbe3ad22001-03-11 20:06:59 +0000291 retval[j++] = vowels[idx0];
292 retval[j++] = consonants[idx1];
293 retval[j++] = vowels[idx2];
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000294 }
295 }
Ben Lindstromcbe3ad22001-03-11 20:06:59 +0000296 retval[j++] = 'x';
297 retval[j++] = '\0';
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000298 return retval;
299}
300
Darren Tucker9c16ac92008-06-13 04:40:35 +1000301/*
302 * Draw an ASCII-Art representing the fingerprint so human brain can
303 * profit from its built-in pattern recognition ability.
304 * This technique is called "random art" and can be found in some
305 * scientific publications like this original paper:
306 *
307 * "Hash Visualization: a New Technique to improve Real-World Security",
308 * Perrig A. and Song D., 1999, International Workshop on Cryptographic
309 * Techniques and E-Commerce (CrypTEC '99)
310 * sparrow.ece.cmu.edu/~adrian/projects/validation/validation.pdf
311 *
312 * The subject came up in a talk by Dan Kaminsky, too.
313 *
314 * If you see the picture is different, the key is different.
315 * If the picture looks the same, you still know nothing.
316 *
317 * The algorithm used here is a worm crawling over a discrete plane,
318 * leaving a trace (augmenting the field) everywhere it goes.
319 * Movement is taken from dgst_raw 2bit-wise. Bumping into walls
320 * makes the respective movement vector be ignored for this turn.
321 * Graphs are not unambiguous, because circles in graphs can be
322 * walked in either direction.
323 */
Darren Tucker987ac842008-06-13 04:54:40 +1000324
325/*
326 * Field sizes for the random art. Have to be odd, so the starting point
327 * can be in the exact middle of the picture, and FLDBASE should be >=8 .
328 * Else pictures would be too dense, and drawing the frame would
329 * fail, too, because the key type would not fit in anymore.
330 */
331#define FLDBASE 8
332#define FLDSIZE_Y (FLDBASE + 1)
333#define FLDSIZE_X (FLDBASE * 2 + 1)
Darren Tucker9c16ac92008-06-13 04:40:35 +1000334static char *
Darren Tucker987ac842008-06-13 04:54:40 +1000335key_fingerprint_randomart(u_char *dgst_raw, u_int dgst_raw_len, const Key *k)
Darren Tucker9c16ac92008-06-13 04:40:35 +1000336{
337 /*
338 * Chars to be used after each other every time the worm
339 * intersects with itself. Matter of taste.
340 */
Darren Tucker4b3b9772008-06-13 04:55:10 +1000341 char *augmentation_string = " .o+=*BOX@%&#/^SE";
Darren Tucker9c16ac92008-06-13 04:40:35 +1000342 char *retval, *p;
Darren Tucker014d76f2008-06-13 04:43:51 +1000343 u_char field[FLDSIZE_X][FLDSIZE_Y];
Darren Tucker9c16ac92008-06-13 04:40:35 +1000344 u_int i, b;
345 int x, y;
Darren Tuckerd32b28a2008-06-13 04:45:50 +1000346 size_t len = strlen(augmentation_string) - 1;
Darren Tucker9c16ac92008-06-13 04:40:35 +1000347
348 retval = xcalloc(1, (FLDSIZE_X + 3) * (FLDSIZE_Y + 2));
349
350 /* initialize field */
Darren Tucker014d76f2008-06-13 04:43:51 +1000351 memset(field, 0, FLDSIZE_X * FLDSIZE_Y * sizeof(char));
Darren Tucker9c16ac92008-06-13 04:40:35 +1000352 x = FLDSIZE_X / 2;
353 y = FLDSIZE_Y / 2;
Darren Tucker9c16ac92008-06-13 04:40:35 +1000354
355 /* process raw key */
356 for (i = 0; i < dgst_raw_len; i++) {
357 int input;
358 /* each byte conveys four 2-bit move commands */
359 input = dgst_raw[i];
360 for (b = 0; b < 4; b++) {
361 /* evaluate 2 bit, rest is shifted later */
362 x += (input & 0x1) ? 1 : -1;
363 y += (input & 0x2) ? 1 : -1;
364
365 /* assure we are still in bounds */
366 x = MAX(x, 0);
367 y = MAX(y, 0);
368 x = MIN(x, FLDSIZE_X - 1);
369 y = MIN(y, FLDSIZE_Y - 1);
370
371 /* augment the field */
Damien Millerc6aadd92008-11-03 19:16:20 +1100372 if (field[x][y] < len - 2)
373 field[x][y]++;
Darren Tucker9c16ac92008-06-13 04:40:35 +1000374 input = input >> 2;
375 }
376 }
Darren Tucker4b3b9772008-06-13 04:55:10 +1000377
378 /* mark starting point and end point*/
379 field[FLDSIZE_X / 2][FLDSIZE_Y / 2] = len - 1;
380 field[x][y] = len;
Darren Tucker9c16ac92008-06-13 04:40:35 +1000381
382 /* fill in retval */
Damien Miller007132a2008-06-29 22:45:37 +1000383 snprintf(retval, FLDSIZE_X, "+--[%4s %4u]", key_type(k), key_size(k));
Darren Tucker987ac842008-06-13 04:54:40 +1000384 p = strchr(retval, '\0');
Darren Tucker9c16ac92008-06-13 04:40:35 +1000385
386 /* output upper border */
Damien Miller007132a2008-06-29 22:45:37 +1000387 for (i = p - retval - 1; i < FLDSIZE_X; i++)
Darren Tucker9c16ac92008-06-13 04:40:35 +1000388 *p++ = '-';
389 *p++ = '+';
390 *p++ = '\n';
391
392 /* output content */
393 for (y = 0; y < FLDSIZE_Y; y++) {
394 *p++ = '|';
395 for (x = 0; x < FLDSIZE_X; x++)
Darren Tuckerd32b28a2008-06-13 04:45:50 +1000396 *p++ = augmentation_string[MIN(field[x][y], len)];
Darren Tucker9c16ac92008-06-13 04:40:35 +1000397 *p++ = '|';
398 *p++ = '\n';
399 }
400
401 /* output lower border */
402 *p++ = '+';
403 for (i = 0; i < FLDSIZE_X; i++)
404 *p++ = '-';
405 *p++ = '+';
406
407 return retval;
408}
409
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000410char *
Damien Millerf58b58c2003-11-17 21:18:23 +1100411key_fingerprint(const Key *k, enum fp_type dgst_type, enum fp_rep dgst_rep)
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000412{
Ben Lindstroma3700052001-04-05 23:26:32 +0000413 char *retval = NULL;
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000414 u_char *dgst_raw;
Damien Miller3672e4b2002-02-05 11:54:07 +1100415 u_int dgst_raw_len;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100416
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000417 dgst_raw = key_fingerprint_raw(k, dgst_type, &dgst_raw_len);
418 if (!dgst_raw)
Ben Lindstromcfccef92001-03-13 04:57:58 +0000419 fatal("key_fingerprint: null from key_fingerprint_raw()");
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000420 switch (dgst_rep) {
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000421 case SSH_FP_HEX:
422 retval = key_fingerprint_hex(dgst_raw, dgst_raw_len);
423 break;
424 case SSH_FP_BUBBLEBABBLE:
425 retval = key_fingerprint_bubblebabble(dgst_raw, dgst_raw_len);
426 break;
Darren Tucker9c16ac92008-06-13 04:40:35 +1000427 case SSH_FP_RANDOMART:
Darren Tucker987ac842008-06-13 04:54:40 +1000428 retval = key_fingerprint_randomart(dgst_raw, dgst_raw_len, k);
Darren Tucker9c16ac92008-06-13 04:40:35 +1000429 break;
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000430 default:
Damien Miller2f54ada2008-11-03 19:24:16 +1100431 fatal("key_fingerprint: bad digest representation %d",
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000432 dgst_rep);
433 break;
434 }
435 memset(dgst_raw, 0, dgst_raw_len);
436 xfree(dgst_raw);
437 return retval;
438}
439
Damien Miller450a7a12000-03-26 13:04:51 +1000440/*
441 * Reads a multiple-precision integer in decimal from the buffer, and advances
442 * the pointer. The integer must already be initialized. This function is
443 * permitted to modify the buffer. This leaves *cpp to point just beyond the
444 * last processed (and maybe modified) character. Note that this may modify
445 * the buffer containing the number.
446 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000447static int
Damien Miller450a7a12000-03-26 13:04:51 +1000448read_bignum(char **cpp, BIGNUM * value)
449{
450 char *cp = *cpp;
451 int old;
452
453 /* Skip any leading whitespace. */
454 for (; *cp == ' ' || *cp == '\t'; cp++)
455 ;
456
457 /* Check that it begins with a decimal digit. */
458 if (*cp < '0' || *cp > '9')
459 return 0;
460
461 /* Save starting position. */
462 *cpp = cp;
463
464 /* Move forward until all decimal digits skipped. */
465 for (; *cp >= '0' && *cp <= '9'; cp++)
466 ;
467
468 /* Save the old terminating character, and replace it by \0. */
469 old = *cp;
470 *cp = 0;
471
472 /* Parse the number. */
473 if (BN_dec2bn(&value, *cpp) == 0)
474 return 0;
475
476 /* Restore old terminating character. */
477 *cp = old;
478
479 /* Move beyond the number and return success. */
480 *cpp = cp;
481 return 1;
482}
Ben Lindstrom836f0e92002-06-23 21:21:30 +0000483
Ben Lindstrombba81212001-06-25 05:01:22 +0000484static int
Damien Miller450a7a12000-03-26 13:04:51 +1000485write_bignum(FILE *f, BIGNUM *num)
486{
487 char *buf = BN_bn2dec(num);
488 if (buf == NULL) {
489 error("write_bignum: BN_bn2dec() failed");
490 return 0;
491 }
492 fprintf(f, " %s", buf);
Damien Milleraf3030f2001-10-10 15:00:49 +1000493 OPENSSL_free(buf);
Damien Miller450a7a12000-03-26 13:04:51 +1000494 return 1;
495}
Damien Miller0bc1bd82000-11-13 22:57:25 +1100496
Ben Lindstrom309f3d12001-09-20 00:55:53 +0000497/* returns 1 ok, -1 error */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100498int
Damien Millereba71ba2000-04-29 23:57:08 +1000499key_read(Key *ret, char **cpp)
Damien Miller450a7a12000-03-26 13:04:51 +1000500{
Damien Millereba71ba2000-04-29 23:57:08 +1000501 Key *k;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100502 int success = -1;
503 char *cp, *space;
504 int len, n, type;
505 u_int bits;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000506 u_char *blob;
Damien Millereba71ba2000-04-29 23:57:08 +1000507
508 cp = *cpp;
509
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000510 switch (ret->type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100511 case KEY_RSA1:
Damien Millereba71ba2000-04-29 23:57:08 +1000512 /* Get number of bits. */
513 if (*cp < '0' || *cp > '9')
Damien Miller0bc1bd82000-11-13 22:57:25 +1100514 return -1; /* Bad bit count... */
Damien Millereba71ba2000-04-29 23:57:08 +1000515 for (bits = 0; *cp >= '0' && *cp <= '9'; cp++)
516 bits = 10 * bits + *cp - '0';
Damien Miller450a7a12000-03-26 13:04:51 +1000517 if (bits == 0)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100518 return -1;
Damien Millereba71ba2000-04-29 23:57:08 +1000519 *cpp = cp;
Damien Miller450a7a12000-03-26 13:04:51 +1000520 /* Get public exponent, public modulus. */
521 if (!read_bignum(cpp, ret->rsa->e))
Damien Miller0bc1bd82000-11-13 22:57:25 +1100522 return -1;
Damien Miller450a7a12000-03-26 13:04:51 +1000523 if (!read_bignum(cpp, ret->rsa->n))
Damien Miller0bc1bd82000-11-13 22:57:25 +1100524 return -1;
525 success = 1;
Damien Miller450a7a12000-03-26 13:04:51 +1000526 break;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100527 case KEY_UNSPEC:
528 case KEY_RSA:
Damien Miller450a7a12000-03-26 13:04:51 +1000529 case KEY_DSA:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100530 space = strchr(cp, ' ');
531 if (space == NULL) {
Damien Miller386f1f32003-02-24 11:54:57 +1100532 debug3("key_read: missing whitespace");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100533 return -1;
534 }
535 *space = '\0';
536 type = key_type_from_name(cp);
537 *space = ' ';
538 if (type == KEY_UNSPEC) {
Damien Miller386f1f32003-02-24 11:54:57 +1100539 debug3("key_read: missing keytype");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100540 return -1;
541 }
542 cp = space+1;
543 if (*cp == '\0') {
544 debug3("key_read: short string");
545 return -1;
546 }
547 if (ret->type == KEY_UNSPEC) {
548 ret->type = type;
549 } else if (ret->type != type) {
550 /* is a key, but different type */
551 debug3("key_read: type mismatch");
Ben Lindstrom309f3d12001-09-20 00:55:53 +0000552 return -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100553 }
Damien Millereba71ba2000-04-29 23:57:08 +1000554 len = 2*strlen(cp);
555 blob = xmalloc(len);
556 n = uudecode(cp, blob, len);
Damien Millere247cc42000-05-07 12:03:14 +1000557 if (n < 0) {
Damien Millerb1715dc2000-05-30 13:44:51 +1000558 error("key_read: uudecode %s failed", cp);
Ben Lindstrom4cbc1812001-12-06 16:41:41 +0000559 xfree(blob);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100560 return -1;
Damien Millere247cc42000-05-07 12:03:14 +1000561 }
Darren Tucker502d3842003-06-28 12:38:01 +1000562 k = key_from_blob(blob, (u_int)n);
Ben Lindstrom4cbc1812001-12-06 16:41:41 +0000563 xfree(blob);
Damien Millerb1715dc2000-05-30 13:44:51 +1000564 if (k == NULL) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100565 error("key_read: key_from_blob %s failed", cp);
566 return -1;
Damien Millerb1715dc2000-05-30 13:44:51 +1000567 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100568 if (k->type != type) {
569 error("key_read: type mismatch: encoding error");
570 key_free(k);
571 return -1;
572 }
573/*XXXX*/
574 if (ret->type == KEY_RSA) {
575 if (ret->rsa != NULL)
576 RSA_free(ret->rsa);
577 ret->rsa = k->rsa;
578 k->rsa = NULL;
579 success = 1;
580#ifdef DEBUG_PK
581 RSA_print_fp(stderr, ret->rsa, 8);
582#endif
583 } else {
584 if (ret->dsa != NULL)
585 DSA_free(ret->dsa);
586 ret->dsa = k->dsa;
587 k->dsa = NULL;
588 success = 1;
589#ifdef DEBUG_PK
590 DSA_print_fp(stderr, ret->dsa, 8);
591#endif
592 }
593/*XXXX*/
Ben Lindstrom4cbc1812001-12-06 16:41:41 +0000594 key_free(k);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100595 if (success != 1)
596 break;
Damien Millerb1715dc2000-05-30 13:44:51 +1000597 /* advance cp: skip whitespace and data */
598 while (*cp == ' ' || *cp == '\t')
599 cp++;
600 while (*cp != '\0' && *cp != ' ' && *cp != '\t')
601 cp++;
602 *cpp = cp;
Damien Miller450a7a12000-03-26 13:04:51 +1000603 break;
604 default:
Damien Millereba71ba2000-04-29 23:57:08 +1000605 fatal("key_read: bad key type: %d", ret->type);
Damien Miller450a7a12000-03-26 13:04:51 +1000606 break;
607 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100608 return success;
Damien Miller450a7a12000-03-26 13:04:51 +1000609}
Ben Lindstrom836f0e92002-06-23 21:21:30 +0000610
Damien Miller450a7a12000-03-26 13:04:51 +1000611int
Damien Millerf58b58c2003-11-17 21:18:23 +1100612key_write(const Key *key, FILE *f)
Damien Miller450a7a12000-03-26 13:04:51 +1000613{
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000614 int n, success = 0;
615 u_int len, bits = 0;
Damien Millera10f5612002-09-12 09:49:15 +1000616 u_char *blob;
617 char *uu;
Damien Miller450a7a12000-03-26 13:04:51 +1000618
Damien Miller0bc1bd82000-11-13 22:57:25 +1100619 if (key->type == KEY_RSA1 && key->rsa != NULL) {
Damien Miller450a7a12000-03-26 13:04:51 +1000620 /* size of modulus 'n' */
621 bits = BN_num_bits(key->rsa->n);
622 fprintf(f, "%u", bits);
623 if (write_bignum(f, key->rsa->e) &&
624 write_bignum(f, key->rsa->n)) {
625 success = 1;
626 } else {
627 error("key_write: failed for RSA key");
628 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100629 } else if ((key->type == KEY_DSA && key->dsa != NULL) ||
630 (key->type == KEY_RSA && key->rsa != NULL)) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100631 key_to_blob(key, &blob, &len);
Damien Millereba71ba2000-04-29 23:57:08 +1000632 uu = xmalloc(2*len);
Damien Millere247cc42000-05-07 12:03:14 +1000633 n = uuencode(blob, len, uu, 2*len);
634 if (n > 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100635 fprintf(f, "%s %s", key_ssh_name(key), uu);
Damien Millere247cc42000-05-07 12:03:14 +1000636 success = 1;
637 }
Damien Millereba71ba2000-04-29 23:57:08 +1000638 xfree(blob);
639 xfree(uu);
Damien Miller450a7a12000-03-26 13:04:51 +1000640 }
641 return success;
642}
Ben Lindstrom836f0e92002-06-23 21:21:30 +0000643
Damien Millerf58b58c2003-11-17 21:18:23 +1100644const char *
645key_type(const Key *k)
Damien Millere247cc42000-05-07 12:03:14 +1000646{
647 switch (k->type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100648 case KEY_RSA1:
649 return "RSA1";
Damien Millere247cc42000-05-07 12:03:14 +1000650 case KEY_RSA:
651 return "RSA";
Damien Millere247cc42000-05-07 12:03:14 +1000652 case KEY_DSA:
653 return "DSA";
Damien Millere247cc42000-05-07 12:03:14 +1000654 }
655 return "unknown";
656}
Ben Lindstrom836f0e92002-06-23 21:21:30 +0000657
Damien Millerf58b58c2003-11-17 21:18:23 +1100658const char *
659key_ssh_name(const Key *k)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100660{
661 switch (k->type) {
662 case KEY_RSA:
663 return "ssh-rsa";
Damien Miller0bc1bd82000-11-13 22:57:25 +1100664 case KEY_DSA:
665 return "ssh-dss";
Damien Miller0bc1bd82000-11-13 22:57:25 +1100666 }
667 return "ssh-unknown";
668}
Ben Lindstrom836f0e92002-06-23 21:21:30 +0000669
Damien Miller0bc1bd82000-11-13 22:57:25 +1100670u_int
Damien Millerf58b58c2003-11-17 21:18:23 +1100671key_size(const Key *k)
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000672{
Damien Millerad833b32000-08-23 10:46:23 +1000673 switch (k->type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100674 case KEY_RSA1:
Damien Millerad833b32000-08-23 10:46:23 +1000675 case KEY_RSA:
676 return BN_num_bits(k->rsa->n);
Damien Millerad833b32000-08-23 10:46:23 +1000677 case KEY_DSA:
678 return BN_num_bits(k->dsa->p);
Damien Millerad833b32000-08-23 10:46:23 +1000679 }
680 return 0;
681}
Damien Miller0bc1bd82000-11-13 22:57:25 +1100682
Ben Lindstrombba81212001-06-25 05:01:22 +0000683static RSA *
Ben Lindstrom46c16222000-12-22 01:43:59 +0000684rsa_generate_private_key(u_int bits)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100685{
Kevin Stevesef4eea92001-02-05 12:42:17 +0000686 RSA *private;
Damien Miller69b72032006-03-26 14:02:35 +1100687
Kevin Stevesef4eea92001-02-05 12:42:17 +0000688 private = RSA_generate_key(bits, 35, NULL, NULL);
689 if (private == NULL)
690 fatal("rsa_generate_private_key: key generation failed.");
691 return private;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100692}
693
Ben Lindstrombba81212001-06-25 05:01:22 +0000694static DSA*
Ben Lindstrom46c16222000-12-22 01:43:59 +0000695dsa_generate_private_key(u_int bits)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100696{
697 DSA *private = DSA_generate_parameters(bits, NULL, 0, NULL, NULL, NULL, NULL);
Damien Miller69b72032006-03-26 14:02:35 +1100698
Damien Miller0bc1bd82000-11-13 22:57:25 +1100699 if (private == NULL)
700 fatal("dsa_generate_private_key: DSA_generate_parameters failed");
701 if (!DSA_generate_key(private))
Kevin Stevesef4eea92001-02-05 12:42:17 +0000702 fatal("dsa_generate_private_key: DSA_generate_key failed.");
703 if (private == NULL)
704 fatal("dsa_generate_private_key: NULL.");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100705 return private;
706}
707
708Key *
Ben Lindstrom46c16222000-12-22 01:43:59 +0000709key_generate(int type, u_int bits)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100710{
711 Key *k = key_new(KEY_UNSPEC);
712 switch (type) {
Kevin Stevesef4eea92001-02-05 12:42:17 +0000713 case KEY_DSA:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100714 k->dsa = dsa_generate_private_key(bits);
715 break;
716 case KEY_RSA:
717 case KEY_RSA1:
718 k->rsa = rsa_generate_private_key(bits);
719 break;
720 default:
Kevin Stevesef4eea92001-02-05 12:42:17 +0000721 fatal("key_generate: unknown type %d", type);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100722 }
Kevin Stevesef4eea92001-02-05 12:42:17 +0000723 k->type = type;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100724 return k;
725}
726
727Key *
Damien Millerf58b58c2003-11-17 21:18:23 +1100728key_from_private(const Key *k)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100729{
730 Key *n = NULL;
731 switch (k->type) {
Kevin Stevesef4eea92001-02-05 12:42:17 +0000732 case KEY_DSA:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100733 n = key_new(k->type);
Darren Tucker0bc85572006-11-07 23:14:41 +1100734 if ((BN_copy(n->dsa->p, k->dsa->p) == NULL) ||
735 (BN_copy(n->dsa->q, k->dsa->q) == NULL) ||
736 (BN_copy(n->dsa->g, k->dsa->g) == NULL) ||
737 (BN_copy(n->dsa->pub_key, k->dsa->pub_key) == NULL))
738 fatal("key_from_private: BN_copy failed");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100739 break;
740 case KEY_RSA:
741 case KEY_RSA1:
742 n = key_new(k->type);
Darren Tucker0bc85572006-11-07 23:14:41 +1100743 if ((BN_copy(n->rsa->n, k->rsa->n) == NULL) ||
744 (BN_copy(n->rsa->e, k->rsa->e) == NULL))
745 fatal("key_from_private: BN_copy failed");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100746 break;
747 default:
Kevin Stevesef4eea92001-02-05 12:42:17 +0000748 fatal("key_from_private: unknown type %d", k->type);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100749 break;
750 }
751 return n;
752}
753
754int
755key_type_from_name(char *name)
756{
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000757 if (strcmp(name, "rsa1") == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100758 return KEY_RSA1;
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000759 } else if (strcmp(name, "rsa") == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100760 return KEY_RSA;
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000761 } else if (strcmp(name, "dsa") == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100762 return KEY_DSA;
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000763 } else if (strcmp(name, "ssh-rsa") == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100764 return KEY_RSA;
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000765 } else if (strcmp(name, "ssh-dss") == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100766 return KEY_DSA;
767 }
Ben Lindstromb54873a2001-03-11 20:01:55 +0000768 debug2("key_type_from_name: unknown key type '%s'", name);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100769 return KEY_UNSPEC;
770}
771
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000772int
773key_names_valid2(const char *names)
774{
775 char *s, *cp, *p;
776
777 if (names == NULL || strcmp(names, "") == 0)
778 return 0;
779 s = cp = xstrdup(names);
780 for ((p = strsep(&cp, ",")); p && *p != '\0';
Damien Miller9f0f5c62001-12-21 14:45:46 +1100781 (p = strsep(&cp, ","))) {
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000782 switch (key_type_from_name(p)) {
783 case KEY_RSA1:
784 case KEY_UNSPEC:
785 xfree(s);
786 return 0;
787 }
788 }
789 debug3("key names ok: [%s]", names);
790 xfree(s);
791 return 1;
792}
793
Damien Miller0bc1bd82000-11-13 22:57:25 +1100794Key *
Damien Millerf58b58c2003-11-17 21:18:23 +1100795key_from_blob(const u_char *blob, u_int blen)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100796{
797 Buffer b;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100798 int rlen, type;
Darren Tucker08d04fa2004-11-05 20:42:28 +1100799 char *ktype = NULL;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100800 Key *key = NULL;
801
802#ifdef DEBUG_PK
803 dump_base64(stderr, blob, blen);
804#endif
805 buffer_init(&b);
806 buffer_append(&b, blob, blen);
Darren Tucker08d04fa2004-11-05 20:42:28 +1100807 if ((ktype = buffer_get_string_ret(&b, NULL)) == NULL) {
808 error("key_from_blob: can't read key type");
809 goto out;
810 }
811
Damien Miller0bc1bd82000-11-13 22:57:25 +1100812 type = key_type_from_name(ktype);
813
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000814 switch (type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100815 case KEY_RSA:
816 key = key_new(type);
Darren Tucker08d04fa2004-11-05 20:42:28 +1100817 if (buffer_get_bignum2_ret(&b, key->rsa->e) == -1 ||
818 buffer_get_bignum2_ret(&b, key->rsa->n) == -1) {
819 error("key_from_blob: can't read rsa key");
820 key_free(key);
821 key = NULL;
822 goto out;
823 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100824#ifdef DEBUG_PK
825 RSA_print_fp(stderr, key->rsa, 8);
826#endif
827 break;
828 case KEY_DSA:
829 key = key_new(type);
Darren Tucker08d04fa2004-11-05 20:42:28 +1100830 if (buffer_get_bignum2_ret(&b, key->dsa->p) == -1 ||
831 buffer_get_bignum2_ret(&b, key->dsa->q) == -1 ||
832 buffer_get_bignum2_ret(&b, key->dsa->g) == -1 ||
833 buffer_get_bignum2_ret(&b, key->dsa->pub_key) == -1) {
834 error("key_from_blob: can't read dsa key");
835 key_free(key);
836 key = NULL;
837 goto out;
838 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100839#ifdef DEBUG_PK
840 DSA_print_fp(stderr, key->dsa, 8);
841#endif
842 break;
843 case KEY_UNSPEC:
844 key = key_new(type);
845 break;
846 default:
847 error("key_from_blob: cannot handle type %s", ktype);
Darren Tucker08d04fa2004-11-05 20:42:28 +1100848 goto out;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100849 }
850 rlen = buffer_len(&b);
851 if (key != NULL && rlen != 0)
852 error("key_from_blob: remaining bytes in key blob %d", rlen);
Darren Tucker08d04fa2004-11-05 20:42:28 +1100853 out:
854 if (ktype != NULL)
855 xfree(ktype);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100856 buffer_free(&b);
857 return key;
858}
859
860int
Damien Millerf58b58c2003-11-17 21:18:23 +1100861key_to_blob(const Key *key, u_char **blobp, u_int *lenp)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100862{
863 Buffer b;
864 int len;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100865
866 if (key == NULL) {
867 error("key_to_blob: key == NULL");
868 return 0;
869 }
870 buffer_init(&b);
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000871 switch (key->type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100872 case KEY_DSA:
873 buffer_put_cstring(&b, key_ssh_name(key));
874 buffer_put_bignum2(&b, key->dsa->p);
875 buffer_put_bignum2(&b, key->dsa->q);
876 buffer_put_bignum2(&b, key->dsa->g);
877 buffer_put_bignum2(&b, key->dsa->pub_key);
878 break;
879 case KEY_RSA:
880 buffer_put_cstring(&b, key_ssh_name(key));
Damien Miller0bc1bd82000-11-13 22:57:25 +1100881 buffer_put_bignum2(&b, key->rsa->e);
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000882 buffer_put_bignum2(&b, key->rsa->n);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100883 break;
884 default:
Ben Lindstrom99a30f12001-09-18 05:49:14 +0000885 error("key_to_blob: unsupported key type %d", key->type);
886 buffer_free(&b);
887 return 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100888 }
889 len = buffer_len(&b);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100890 if (lenp != NULL)
891 *lenp = len;
Ben Lindstrom2bf759c2002-07-07 22:13:31 +0000892 if (blobp != NULL) {
893 *blobp = xmalloc(len);
894 memcpy(*blobp, buffer_ptr(&b), len);
895 }
896 memset(buffer_ptr(&b), 0, len);
897 buffer_free(&b);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100898 return len;
899}
900
901int
902key_sign(
Damien Millerf58b58c2003-11-17 21:18:23 +1100903 const Key *key,
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000904 u_char **sigp, u_int *lenp,
Damien Millerf58b58c2003-11-17 21:18:23 +1100905 const u_char *data, u_int datalen)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100906{
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000907 switch (key->type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100908 case KEY_DSA:
909 return ssh_dss_sign(key, sigp, lenp, data, datalen);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100910 case KEY_RSA:
911 return ssh_rsa_sign(key, sigp, lenp, data, datalen);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100912 default:
Darren Tucker5cb30ad2004-08-12 22:40:24 +1000913 error("key_sign: invalid key type %d", key->type);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100914 return -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100915 }
916}
917
Ben Lindstrom01fff0c2002-06-06 20:54:07 +0000918/*
919 * key_verify returns 1 for a correct signature, 0 for an incorrect signature
920 * and -1 on error.
921 */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100922int
923key_verify(
Damien Millerf58b58c2003-11-17 21:18:23 +1100924 const Key *key,
925 const u_char *signature, u_int signaturelen,
926 const u_char *data, u_int datalen)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100927{
Ben Lindstrom5363aee2001-06-25 04:42:20 +0000928 if (signaturelen == 0)
929 return -1;
930
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000931 switch (key->type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100932 case KEY_DSA:
933 return ssh_dss_verify(key, signature, signaturelen, data, datalen);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100934 case KEY_RSA:
935 return ssh_rsa_verify(key, signature, signaturelen, data, datalen);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100936 default:
Darren Tucker5cb30ad2004-08-12 22:40:24 +1000937 error("key_verify: invalid key type %d", key->type);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100938 return -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100939 }
940}
Ben Lindstroma674e8d2002-03-22 01:45:53 +0000941
942/* Converts a private to a public key */
Ben Lindstroma674e8d2002-03-22 01:45:53 +0000943Key *
Damien Millerf58b58c2003-11-17 21:18:23 +1100944key_demote(const Key *k)
Ben Lindstroma674e8d2002-03-22 01:45:53 +0000945{
946 Key *pk;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000947
Damien Miller07d86be2006-03-26 14:19:21 +1100948 pk = xcalloc(1, sizeof(*pk));
Ben Lindstroma674e8d2002-03-22 01:45:53 +0000949 pk->type = k->type;
950 pk->flags = k->flags;
951 pk->dsa = NULL;
952 pk->rsa = NULL;
953
954 switch (k->type) {
955 case KEY_RSA1:
956 case KEY_RSA:
957 if ((pk->rsa = RSA_new()) == NULL)
958 fatal("key_demote: RSA_new failed");
959 if ((pk->rsa->e = BN_dup(k->rsa->e)) == NULL)
960 fatal("key_demote: BN_dup failed");
961 if ((pk->rsa->n = BN_dup(k->rsa->n)) == NULL)
962 fatal("key_demote: BN_dup failed");
963 break;
964 case KEY_DSA:
965 if ((pk->dsa = DSA_new()) == NULL)
966 fatal("key_demote: DSA_new failed");
967 if ((pk->dsa->p = BN_dup(k->dsa->p)) == NULL)
968 fatal("key_demote: BN_dup failed");
969 if ((pk->dsa->q = BN_dup(k->dsa->q)) == NULL)
970 fatal("key_demote: BN_dup failed");
971 if ((pk->dsa->g = BN_dup(k->dsa->g)) == NULL)
972 fatal("key_demote: BN_dup failed");
973 if ((pk->dsa->pub_key = BN_dup(k->dsa->pub_key)) == NULL)
974 fatal("key_demote: BN_dup failed");
975 break;
976 default:
977 fatal("key_free: bad key type %d", k->type);
978 break;
979 }
980
981 return (pk);
982}