blob: f7df0bb1db45b4f839bf91578ca2c8e6364725d0 [file] [log] [blame]
Damien Miller450a7a12000-03-26 13:04:51 +10001/*
Damien Millere4340be2000-09-16 13:29:08 +11002 * 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 *
Damien Miller450a7a12000-03-26 13:04:51 +100012 * Copyright (c) 2000 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.
Damien Miller450a7a12000-03-26 13:04:51 +100022 *
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 */
Damien Miller450a7a12000-03-26 13:04:51 +100034
35#include "includes.h"
Damien Miller5f056372000-04-16 12:31:48 +100036#include "ssh.h"
Damien Miller450a7a12000-03-26 13:04:51 +100037#include <openssl/rsa.h>
38#include <openssl/dsa.h>
39#include <openssl/evp.h>
Damien Miller450a7a12000-03-26 13:04:51 +100040#include "xmalloc.h"
41#include "key.h"
Damien Millereba71ba2000-04-29 23:57:08 +100042#include "dsa.h"
43#include "uuencode.h"
44
Damien Millere4340be2000-09-16 13:29:08 +110045RCSID("$OpenBSD: key.c,v 1.11 2000/09/07 20:27:51 deraadt Exp $");
Damien Millerbf7f4662000-06-23 10:16:38 +100046
Damien Millereba71ba2000-04-29 23:57:08 +100047#define SSH_DSS "ssh-dss"
Damien Miller450a7a12000-03-26 13:04:51 +100048
49Key *
50key_new(int type)
51{
52 Key *k;
53 RSA *rsa;
54 DSA *dsa;
55 k = xmalloc(sizeof(*k));
56 k->type = type;
Damien Millereba71ba2000-04-29 23:57:08 +100057 k->dsa = NULL;
58 k->rsa = NULL;
Damien Miller450a7a12000-03-26 13:04:51 +100059 switch (k->type) {
60 case KEY_RSA:
61 rsa = RSA_new();
62 rsa->n = BN_new();
63 rsa->e = BN_new();
64 k->rsa = rsa;
65 break;
66 case KEY_DSA:
67 dsa = DSA_new();
68 dsa->p = BN_new();
69 dsa->q = BN_new();
70 dsa->g = BN_new();
71 dsa->pub_key = BN_new();
72 k->dsa = dsa;
73 break;
74 case KEY_EMPTY:
Damien Miller450a7a12000-03-26 13:04:51 +100075 break;
76 default:
77 fatal("key_new: bad key type %d", k->type);
78 break;
79 }
80 return k;
81}
82void
83key_free(Key *k)
84{
85 switch (k->type) {
86 case KEY_RSA:
87 if (k->rsa != NULL)
88 RSA_free(k->rsa);
89 k->rsa = NULL;
90 break;
91 case KEY_DSA:
92 if (k->dsa != NULL)
93 DSA_free(k->dsa);
94 k->dsa = NULL;
95 break;
96 default:
97 fatal("key_free: bad key type %d", k->type);
98 break;
99 }
100 xfree(k);
101}
102int
103key_equal(Key *a, Key *b)
104{
105 if (a == NULL || b == NULL || a->type != b->type)
106 return 0;
107 switch (a->type) {
108 case KEY_RSA:
109 return a->rsa != NULL && b->rsa != NULL &&
110 BN_cmp(a->rsa->e, b->rsa->e) == 0 &&
111 BN_cmp(a->rsa->n, b->rsa->n) == 0;
112 break;
113 case KEY_DSA:
114 return a->dsa != NULL && b->dsa != NULL &&
115 BN_cmp(a->dsa->p, b->dsa->p) == 0 &&
116 BN_cmp(a->dsa->q, b->dsa->q) == 0 &&
117 BN_cmp(a->dsa->g, b->dsa->g) == 0 &&
118 BN_cmp(a->dsa->pub_key, b->dsa->pub_key) == 0;
119 break;
120 default:
Damien Millereba71ba2000-04-29 23:57:08 +1000121 fatal("key_equal: bad key type %d", a->type);
Damien Miller450a7a12000-03-26 13:04:51 +1000122 break;
123 }
124 return 0;
125}
126
Damien Miller450a7a12000-03-26 13:04:51 +1000127/*
128 * Generate key fingerprint in ascii format.
129 * Based on ideas and code from Bjoern Groenvall <bg@sics.se>
130 */
131char *
132key_fingerprint(Key *k)
133{
Damien Miller6536c7d2000-06-22 21:32:31 +1000134 static char retval[(EVP_MAX_MD_SIZE+1)*3];
Damien Millereba71ba2000-04-29 23:57:08 +1000135 unsigned char *blob = NULL;
Damien Miller450a7a12000-03-26 13:04:51 +1000136 int len = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000137 int nlen, elen;
Damien Miller450a7a12000-03-26 13:04:51 +1000138
139 switch (k->type) {
140 case KEY_RSA:
141 nlen = BN_num_bytes(k->rsa->n);
142 elen = BN_num_bytes(k->rsa->e);
143 len = nlen + elen;
Damien Millereba71ba2000-04-29 23:57:08 +1000144 blob = xmalloc(len);
145 BN_bn2bin(k->rsa->n, blob);
146 BN_bn2bin(k->rsa->e, blob + nlen);
Damien Miller450a7a12000-03-26 13:04:51 +1000147 break;
148 case KEY_DSA:
Damien Millereba71ba2000-04-29 23:57:08 +1000149 dsa_make_key_blob(k, &blob, &len);
Damien Miller450a7a12000-03-26 13:04:51 +1000150 break;
151 default:
152 fatal("key_fingerprint: bad key type %d", k->type);
153 break;
154 }
Damien Miller6536c7d2000-06-22 21:32:31 +1000155 retval[0] = '\0';
156
Damien Millereba71ba2000-04-29 23:57:08 +1000157 if (blob != NULL) {
Damien Miller6536c7d2000-06-22 21:32:31 +1000158 int i;
159 unsigned char digest[EVP_MAX_MD_SIZE];
160 EVP_MD *md = EVP_md5();
161 EVP_MD_CTX ctx;
162 EVP_DigestInit(&ctx, md);
163 EVP_DigestUpdate(&ctx, blob, len);
164 EVP_DigestFinal(&ctx, digest, NULL);
165 for(i = 0; i < md->md_size; i++) {
166 char hex[4];
167 snprintf(hex, sizeof(hex), "%02x:", digest[i]);
168 strlcat(retval, hex, sizeof(retval));
169 }
170 retval[strlen(retval) - 1] = '\0';
Damien Millereba71ba2000-04-29 23:57:08 +1000171 memset(blob, 0, len);
172 xfree(blob);
Damien Miller450a7a12000-03-26 13:04:51 +1000173 }
174 return retval;
175}
176
177/*
178 * Reads a multiple-precision integer in decimal from the buffer, and advances
179 * the pointer. The integer must already be initialized. This function is
180 * permitted to modify the buffer. This leaves *cpp to point just beyond the
181 * last processed (and maybe modified) character. Note that this may modify
182 * the buffer containing the number.
183 */
184int
185read_bignum(char **cpp, BIGNUM * value)
186{
187 char *cp = *cpp;
188 int old;
189
190 /* Skip any leading whitespace. */
191 for (; *cp == ' ' || *cp == '\t'; cp++)
192 ;
193
194 /* Check that it begins with a decimal digit. */
195 if (*cp < '0' || *cp > '9')
196 return 0;
197
198 /* Save starting position. */
199 *cpp = cp;
200
201 /* Move forward until all decimal digits skipped. */
202 for (; *cp >= '0' && *cp <= '9'; cp++)
203 ;
204
205 /* Save the old terminating character, and replace it by \0. */
206 old = *cp;
207 *cp = 0;
208
209 /* Parse the number. */
210 if (BN_dec2bn(&value, *cpp) == 0)
211 return 0;
212
213 /* Restore old terminating character. */
214 *cp = old;
215
216 /* Move beyond the number and return success. */
217 *cpp = cp;
218 return 1;
219}
220int
221write_bignum(FILE *f, BIGNUM *num)
222{
223 char *buf = BN_bn2dec(num);
224 if (buf == NULL) {
225 error("write_bignum: BN_bn2dec() failed");
226 return 0;
227 }
228 fprintf(f, " %s", buf);
229 free(buf);
230 return 1;
231}
Damien Millereba71ba2000-04-29 23:57:08 +1000232unsigned int
233key_read(Key *ret, char **cpp)
Damien Miller450a7a12000-03-26 13:04:51 +1000234{
Damien Millereba71ba2000-04-29 23:57:08 +1000235 Key *k;
236 unsigned int bits = 0;
237 char *cp;
238 int len, n;
239 unsigned char *blob;
240
241 cp = *cpp;
242
Damien Miller450a7a12000-03-26 13:04:51 +1000243 switch(ret->type) {
244 case KEY_RSA:
Damien Millereba71ba2000-04-29 23:57:08 +1000245 /* Get number of bits. */
246 if (*cp < '0' || *cp > '9')
247 return 0; /* Bad bit count... */
248 for (bits = 0; *cp >= '0' && *cp <= '9'; cp++)
249 bits = 10 * bits + *cp - '0';
Damien Miller450a7a12000-03-26 13:04:51 +1000250 if (bits == 0)
251 return 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000252 *cpp = cp;
Damien Miller450a7a12000-03-26 13:04:51 +1000253 /* Get public exponent, public modulus. */
254 if (!read_bignum(cpp, ret->rsa->e))
255 return 0;
256 if (!read_bignum(cpp, ret->rsa->n))
257 return 0;
258 break;
259 case KEY_DSA:
Damien Millereba71ba2000-04-29 23:57:08 +1000260 if (strncmp(cp, SSH_DSS " ", 7) != 0)
Damien Miller450a7a12000-03-26 13:04:51 +1000261 return 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000262 cp += 7;
263 len = 2*strlen(cp);
264 blob = xmalloc(len);
265 n = uudecode(cp, blob, len);
Damien Millere247cc42000-05-07 12:03:14 +1000266 if (n < 0) {
Damien Millerb1715dc2000-05-30 13:44:51 +1000267 error("key_read: uudecode %s failed", cp);
Damien Millere247cc42000-05-07 12:03:14 +1000268 return 0;
269 }
Damien Millereba71ba2000-04-29 23:57:08 +1000270 k = dsa_key_from_blob(blob, n);
Damien Millerb1715dc2000-05-30 13:44:51 +1000271 if (k == NULL) {
272 error("key_read: dsa_key_from_blob %s failed", cp);
273 return 0;
274 }
Damien Millereba71ba2000-04-29 23:57:08 +1000275 xfree(blob);
276 if (ret->dsa != NULL)
277 DSA_free(ret->dsa);
278 ret->dsa = k->dsa;
279 k->dsa = NULL;
280 key_free(k);
281 bits = BN_num_bits(ret->dsa->p);
Damien Millerb1715dc2000-05-30 13:44:51 +1000282 /* advance cp: skip whitespace and data */
283 while (*cp == ' ' || *cp == '\t')
284 cp++;
285 while (*cp != '\0' && *cp != ' ' && *cp != '\t')
286 cp++;
287 *cpp = cp;
Damien Miller450a7a12000-03-26 13:04:51 +1000288 break;
289 default:
Damien Millereba71ba2000-04-29 23:57:08 +1000290 fatal("key_read: bad key type: %d", ret->type);
Damien Miller450a7a12000-03-26 13:04:51 +1000291 break;
292 }
Damien Millereba71ba2000-04-29 23:57:08 +1000293 return bits;
Damien Miller450a7a12000-03-26 13:04:51 +1000294}
295int
296key_write(Key *key, FILE *f)
297{
298 int success = 0;
299 unsigned int bits = 0;
300
301 if (key->type == KEY_RSA && key->rsa != NULL) {
302 /* size of modulus 'n' */
303 bits = BN_num_bits(key->rsa->n);
304 fprintf(f, "%u", bits);
305 if (write_bignum(f, key->rsa->e) &&
306 write_bignum(f, key->rsa->n)) {
307 success = 1;
308 } else {
309 error("key_write: failed for RSA key");
310 }
311 } else if (key->type == KEY_DSA && key->dsa != NULL) {
Damien Millereba71ba2000-04-29 23:57:08 +1000312 int len, n;
313 unsigned char *blob, *uu;
314 dsa_make_key_blob(key, &blob, &len);
315 uu = xmalloc(2*len);
Damien Millere247cc42000-05-07 12:03:14 +1000316 n = uuencode(blob, len, uu, 2*len);
317 if (n > 0) {
318 fprintf(f, "%s %s", SSH_DSS, uu);
319 success = 1;
320 }
Damien Millereba71ba2000-04-29 23:57:08 +1000321 xfree(blob);
322 xfree(uu);
Damien Miller450a7a12000-03-26 13:04:51 +1000323 }
324 return success;
325}
Damien Millere247cc42000-05-07 12:03:14 +1000326char *
327key_type(Key *k)
328{
329 switch (k->type) {
330 case KEY_RSA:
331 return "RSA";
332 break;
333 case KEY_DSA:
334 return "DSA";
335 break;
336 }
337 return "unknown";
338}
Damien Millerad833b32000-08-23 10:46:23 +1000339unsigned int
340key_size(Key *k){
341 switch (k->type) {
342 case KEY_RSA:
343 return BN_num_bits(k->rsa->n);
344 break;
345 case KEY_DSA:
346 return BN_num_bits(k->dsa->p);
347 break;
348 }
349 return 0;
350}