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);
125 * Generate key fingerprint in ascii format.
126 * Based on ideas and code from Bjoern Groenvall <bg@sics.se>
129 key_fingerprint(Key *k)
131 static char retval[(EVP_MAX_MD_SIZE+1)*3];
132 unsigned char *blob = NULL;
138 nlen = BN_num_bytes(k->rsa->n);
139 elen = BN_num_bytes(k->rsa->e);
142 BN_bn2bin(k->rsa->n, blob);
143 BN_bn2bin(k->rsa->e, blob + nlen);
146 dsa_make_key_blob(k, &blob, &len);
149 fatal("key_fingerprint: bad key type %d", k->type);
156 unsigned char digest[EVP_MAX_MD_SIZE];
157 EVP_MD *md = EVP_md5();
159 EVP_DigestInit(&ctx, md);
160 EVP_DigestUpdate(&ctx, blob, len);
161 EVP_DigestFinal(&ctx, digest, NULL);
162 for(i = 0; i < md->md_size; i++) {
164 snprintf(hex, sizeof(hex), "%02x:", digest[i]);
165 strlcat(retval, hex, sizeof(retval));
167 retval[strlen(retval) - 1] = '\0';
168 memset(blob, 0, len);
175 * Reads a multiple-precision integer in decimal from the buffer, and advances
176 * the pointer. The integer must already be initialized. This function is
177 * permitted to modify the buffer. This leaves *cpp to point just beyond the
178 * last processed (and maybe modified) character. Note that this may modify
179 * the buffer containing the number.
182 read_bignum(char **cpp, BIGNUM * value)
187 /* Skip any leading whitespace. */
188 for (; *cp == ' ' || *cp == '\t'; cp++)
191 /* Check that it begins with a decimal digit. */
192 if (*cp < '0' || *cp > '9')
195 /* Save starting position. */
198 /* Move forward until all decimal digits skipped. */
199 for (; *cp >= '0' && *cp <= '9'; cp++)
202 /* Save the old terminating character, and replace it by \0. */
206 /* Parse the number. */
207 if (BN_dec2bn(&value, *cpp) == 0)
210 /* Restore old terminating character. */
213 /* Move beyond the number and return success. */
218 write_bignum(FILE *f, BIGNUM *num)
220 char *buf = BN_bn2dec(num);
222 error("write_bignum: BN_bn2dec() failed");
225 fprintf(f, " %s", buf);
230 key_read(Key *ret, char **cpp)
233 unsigned int bits = 0;
242 /* Get number of bits. */
243 if (*cp < '0' || *cp > '9')
244 return 0; /* Bad bit count... */
245 for (bits = 0; *cp >= '0' && *cp <= '9'; cp++)
246 bits = 10 * bits + *cp - '0';
250 /* Get public exponent, public modulus. */
251 if (!read_bignum(cpp, ret->rsa->e))
253 if (!read_bignum(cpp, ret->rsa->n))
257 if (strncmp(cp, SSH_DSS " ", 7) != 0)
262 n = uudecode(cp, blob, len);
264 error("key_read: uudecode %s failed", cp);
267 k = dsa_key_from_blob(blob, n);
269 error("key_read: dsa_key_from_blob %s failed", cp);
273 if (ret->dsa != NULL)
278 bits = BN_num_bits(ret->dsa->p);
279 /* advance cp: skip whitespace and data */
280 while (*cp == ' ' || *cp == '\t')
282 while (*cp != '\0' && *cp != ' ' && *cp != '\t')
287 fatal("key_read: bad key type: %d", ret->type);
293 key_write(Key *key, FILE *f)
296 unsigned int bits = 0;
298 if (key->type == KEY_RSA && key->rsa != NULL) {
299 /* size of modulus 'n' */
300 bits = BN_num_bits(key->rsa->n);
301 fprintf(f, "%u", bits);
302 if (write_bignum(f, key->rsa->e) &&
303 write_bignum(f, key->rsa->n)) {
306 error("key_write: failed for RSA key");
308 } else if (key->type == KEY_DSA && key->dsa != NULL) {
310 unsigned char *blob, *uu;
311 dsa_make_key_blob(key, &blob, &len);
313 n = uuencode(blob, len, uu, 2*len);
315 fprintf(f, "%s %s", SSH_DSS, uu);