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>
48 k = xmalloc(sizeof(*k));
62 dsa->pub_key = BN_new();
70 fatal("key_new: bad key type %d", k->type);
90 fatal("key_free: bad key type %d", k->type);
96 key_equal(Key *a, Key *b)
98 if (a == NULL || b == NULL || a->type != b->type)
102 return a->rsa != NULL && b->rsa != NULL &&
103 BN_cmp(a->rsa->e, b->rsa->e) == 0 &&
104 BN_cmp(a->rsa->n, b->rsa->n) == 0;
107 return a->dsa != NULL && b->dsa != NULL &&
108 BN_cmp(a->dsa->p, b->dsa->p) == 0 &&
109 BN_cmp(a->dsa->q, b->dsa->q) == 0 &&
110 BN_cmp(a->dsa->g, b->dsa->g) == 0 &&
111 BN_cmp(a->dsa->pub_key, b->dsa->pub_key) == 0;
114 fatal("key_free: bad key type %d", a->type);
120 #define FPRINT "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x"
123 * Generate key fingerprint in ascii format.
124 * Based on ideas and code from Bjoern Groenvall <bg@sics.se>
127 key_fingerprint(Key *k)
129 static char retval[80];
130 unsigned char *buf = NULL;
132 int nlen, elen, plen, qlen, glen, publen;
136 nlen = BN_num_bytes(k->rsa->n);
137 elen = BN_num_bytes(k->rsa->e);
140 BN_bn2bin(k->rsa->n, buf);
141 BN_bn2bin(k->rsa->e, buf + nlen);
144 plen = BN_num_bytes(k->dsa->p);
145 qlen = BN_num_bytes(k->dsa->q);
146 glen = BN_num_bytes(k->dsa->g);
147 publen = BN_num_bytes(k->dsa->pub_key);
148 len = qlen + qlen + glen + publen;
150 BN_bn2bin(k->dsa->p, buf);
151 BN_bn2bin(k->dsa->q, buf + plen);
152 BN_bn2bin(k->dsa->g, buf + plen + qlen);
153 BN_bn2bin(k->dsa->pub_key , buf + plen + qlen + glen);
156 fatal("key_fingerprint: bad key type %d", k->type);
162 EVP_DigestInit(&md, EVP_md5());
163 EVP_DigestUpdate(&md, buf, len);
164 EVP_DigestFinal(&md, d, NULL);
165 snprintf(retval, sizeof(retval), FPRINT,
166 d[0], d[1], d[2], d[3], d[4], d[5], d[6], d[7],
167 d[8], d[9], d[10], d[11], d[12], d[13], d[14], d[15]);
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, unsigned int bits, char **cpp)
236 /* Get public exponent, public modulus. */
237 if (!read_bignum(cpp, ret->rsa->e))
239 if (!read_bignum(cpp, ret->rsa->n))
245 if (!read_bignum(cpp, ret->dsa->p))
247 if (!read_bignum(cpp, ret->dsa->q))
249 if (!read_bignum(cpp, ret->dsa->g))
251 if (!read_bignum(cpp, ret->dsa->pub_key))
255 fatal("bad key type: %d", ret->type);
261 key_write(Key *key, FILE *f)
264 unsigned int bits = 0;
266 if (key->type == KEY_RSA && key->rsa != NULL) {
267 /* size of modulus 'n' */
268 bits = BN_num_bits(key->rsa->n);
269 fprintf(f, "%u", bits);
270 if (write_bignum(f, key->rsa->e) &&
271 write_bignum(f, key->rsa->n)) {
274 error("key_write: failed for RSA key");
276 } else if (key->type == KEY_DSA && key->dsa != NULL) {
277 /* bits == 0 means DSA key */
279 fprintf(f, "%u", bits);
280 if (write_bignum(f, key->dsa->p) &&
281 write_bignum(f, key->dsa->q) &&
282 write_bignum(f, key->dsa->g) &&
283 write_bignum(f, key->dsa->pub_key)) {
286 error("key_write: failed for DSA key");