2 * Copyright (c) 2000 Markus Friedl. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 3. All advertising materials mentioning features or use of this software
13 * must display the following acknowledgement:
14 * This product includes software developed by Markus Friedl.
15 * 4. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
36 #include <openssl/rsa.h>
37 #include <openssl/dsa.h>
38 #include <openssl/evp.h>
44 #define SSH_DSS "ssh-dss"
52 k = xmalloc(sizeof(*k));
68 dsa->pub_key = BN_new();
74 fatal("key_new: bad key type %d", k->type);
94 fatal("key_free: bad key type %d", k->type);
100 key_equal(Key *a, Key *b)
102 if (a == NULL || b == NULL || a->type != b->type)
106 return a->rsa != NULL && b->rsa != NULL &&
107 BN_cmp(a->rsa->e, b->rsa->e) == 0 &&
108 BN_cmp(a->rsa->n, b->rsa->n) == 0;
111 return a->dsa != NULL && b->dsa != NULL &&
112 BN_cmp(a->dsa->p, b->dsa->p) == 0 &&
113 BN_cmp(a->dsa->q, b->dsa->q) == 0 &&
114 BN_cmp(a->dsa->g, b->dsa->g) == 0 &&
115 BN_cmp(a->dsa->pub_key, b->dsa->pub_key) == 0;
118 fatal("key_equal: bad key type %d", a->type);
124 #define FPRINT "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x"
127 * Generate key fingerprint in ascii format.
128 * Based on ideas and code from Bjoern Groenvall <bg@sics.se>
131 key_fingerprint(Key *k)
133 static char retval[80];
134 unsigned char *blob = NULL;
140 nlen = BN_num_bytes(k->rsa->n);
141 elen = BN_num_bytes(k->rsa->e);
144 BN_bn2bin(k->rsa->n, blob);
145 BN_bn2bin(k->rsa->e, blob + nlen);
148 dsa_make_key_blob(k, &blob, &len);
151 fatal("key_fingerprint: bad key type %d", k->type);
157 EVP_DigestInit(&md, EVP_md5());
158 EVP_DigestUpdate(&md, blob, len);
159 EVP_DigestFinal(&md, d, NULL);
160 snprintf(retval, sizeof(retval), FPRINT,
161 d[0], d[1], d[2], d[3], d[4], d[5], d[6], d[7],
162 d[8], d[9], d[10], d[11], d[12], d[13], d[14], d[15]);
163 memset(blob, 0, len);
170 * Reads a multiple-precision integer in decimal from the buffer, and advances
171 * the pointer. The integer must already be initialized. This function is
172 * permitted to modify the buffer. This leaves *cpp to point just beyond the
173 * last processed (and maybe modified) character. Note that this may modify
174 * the buffer containing the number.
177 read_bignum(char **cpp, BIGNUM * value)
182 /* Skip any leading whitespace. */
183 for (; *cp == ' ' || *cp == '\t'; cp++)
186 /* Check that it begins with a decimal digit. */
187 if (*cp < '0' || *cp > '9')
190 /* Save starting position. */
193 /* Move forward until all decimal digits skipped. */
194 for (; *cp >= '0' && *cp <= '9'; cp++)
197 /* Save the old terminating character, and replace it by \0. */
201 /* Parse the number. */
202 if (BN_dec2bn(&value, *cpp) == 0)
205 /* Restore old terminating character. */
208 /* Move beyond the number and return success. */
213 write_bignum(FILE *f, BIGNUM *num)
215 char *buf = BN_bn2dec(num);
217 error("write_bignum: BN_bn2dec() failed");
220 fprintf(f, " %s", buf);
225 key_read(Key *ret, char **cpp)
228 unsigned int bits = 0;
237 /* Get number of bits. */
238 if (*cp < '0' || *cp > '9')
239 return 0; /* Bad bit count... */
240 for (bits = 0; *cp >= '0' && *cp <= '9'; cp++)
241 bits = 10 * bits + *cp - '0';
245 /* Get public exponent, public modulus. */
246 if (!read_bignum(cpp, ret->rsa->e))
248 if (!read_bignum(cpp, ret->rsa->n))
252 if (strncmp(cp, SSH_DSS " ", 7) != 0)
257 n = uudecode(cp, blob, len);
259 error("uudecode %s failed", cp);
262 k = dsa_key_from_blob(blob, n);
266 if (ret->dsa != NULL)
271 bits = BN_num_bits(ret->dsa->p);
272 cp = strchr(cp, '=');
278 fatal("key_read: bad key type: %d", ret->type);
284 key_write(Key *key, FILE *f)
287 unsigned int bits = 0;
289 if (key->type == KEY_RSA && key->rsa != NULL) {
290 /* size of modulus 'n' */
291 bits = BN_num_bits(key->rsa->n);
292 fprintf(f, "%u", bits);
293 if (write_bignum(f, key->rsa->e) &&
294 write_bignum(f, key->rsa->n)) {
297 error("key_write: failed for RSA key");
299 } else if (key->type == KEY_DSA && key->dsa != NULL) {
301 unsigned char *blob, *uu;
302 dsa_make_key_blob(key, &blob, &len);
304 n = uuencode(blob, len, uu, 2*len);
306 fprintf(f, "%s %s", SSH_DSS, uu);