blob: 745c474f91c48ed62b1cdc8630b94aac88bb781e [file] [log] [blame]
Damien Millera7a73ee2006-08-05 11:37:59 +10001/* $OpenBSD: key.c,v 1.66 2006/08/01 23:22:47 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.
Damien Miller450a7a12000-03-26 13:04:51 +100014 *
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
17 * are met:
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
Damien Miller450a7a12000-03-26 13:04:51 +100023 *
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
28 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 */
Damien Miller450a7a12000-03-26 13:04:51 +100035#include "includes.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000036
Damien Miller450a7a12000-03-26 13:04:51 +100037#include <openssl/evp.h>
Ben Lindstrom226cfa02001-01-22 05:34:40 +000038
Damien Millera7a73ee2006-08-05 11:37:59 +100039#include <stdio.h>
Damien Millere3476ed2006-07-24 14:13:33 +100040#include <string.h>
41
Damien Miller450a7a12000-03-26 13:04:51 +100042#include "xmalloc.h"
43#include "key.h"
Damien Miller0bc1bd82000-11-13 22:57:25 +110044#include "rsa.h"
Damien Millereba71ba2000-04-29 23:57:08 +100045#include "uuencode.h"
Damien Miller0bc1bd82000-11-13 22:57:25 +110046#include "buffer.h"
47#include "bufaux.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000048#include "log.h"
Damien Miller450a7a12000-03-26 13:04:51 +100049
50Key *
51key_new(int type)
52{
53 Key *k;
54 RSA *rsa;
55 DSA *dsa;
Damien Miller07d86be2006-03-26 14:19:21 +110056 k = xcalloc(1, sizeof(*k));
Damien Miller450a7a12000-03-26 13:04:51 +100057 k->type = type;
Damien Millereba71ba2000-04-29 23:57:08 +100058 k->dsa = NULL;
59 k->rsa = NULL;
Damien Miller450a7a12000-03-26 13:04:51 +100060 switch (k->type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +110061 case KEY_RSA1:
Damien Miller450a7a12000-03-26 13:04:51 +100062 case KEY_RSA:
Damien Millerda755162002-01-22 23:09:22 +110063 if ((rsa = RSA_new()) == NULL)
64 fatal("key_new: RSA_new failed");
65 if ((rsa->n = BN_new()) == NULL)
66 fatal("key_new: BN_new failed");
67 if ((rsa->e = BN_new()) == NULL)
68 fatal("key_new: BN_new failed");
Damien Miller450a7a12000-03-26 13:04:51 +100069 k->rsa = rsa;
70 break;
71 case KEY_DSA:
Damien Millerda755162002-01-22 23:09:22 +110072 if ((dsa = DSA_new()) == NULL)
73 fatal("key_new: DSA_new failed");
74 if ((dsa->p = BN_new()) == NULL)
75 fatal("key_new: BN_new failed");
76 if ((dsa->q = BN_new()) == NULL)
77 fatal("key_new: BN_new failed");
78 if ((dsa->g = BN_new()) == NULL)
79 fatal("key_new: BN_new failed");
80 if ((dsa->pub_key = BN_new()) == NULL)
81 fatal("key_new: BN_new failed");
Damien Miller450a7a12000-03-26 13:04:51 +100082 k->dsa = dsa;
83 break;
Damien Miller0bc1bd82000-11-13 22:57:25 +110084 case KEY_UNSPEC:
Damien Miller450a7a12000-03-26 13:04:51 +100085 break;
86 default:
87 fatal("key_new: bad key type %d", k->type);
88 break;
89 }
90 return k;
91}
Ben Lindstrom836f0e92002-06-23 21:21:30 +000092
Damien Miller0bc1bd82000-11-13 22:57:25 +110093Key *
94key_new_private(int type)
95{
96 Key *k = key_new(type);
97 switch (k->type) {
98 case KEY_RSA1:
99 case KEY_RSA:
Damien Millerda755162002-01-22 23:09:22 +1100100 if ((k->rsa->d = BN_new()) == NULL)
101 fatal("key_new_private: BN_new failed");
102 if ((k->rsa->iqmp = BN_new()) == NULL)
103 fatal("key_new_private: BN_new failed");
104 if ((k->rsa->q = BN_new()) == NULL)
105 fatal("key_new_private: BN_new failed");
106 if ((k->rsa->p = BN_new()) == NULL)
107 fatal("key_new_private: BN_new failed");
108 if ((k->rsa->dmq1 = BN_new()) == NULL)
109 fatal("key_new_private: BN_new failed");
110 if ((k->rsa->dmp1 = BN_new()) == NULL)
111 fatal("key_new_private: BN_new failed");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100112 break;
113 case KEY_DSA:
Damien Millerda755162002-01-22 23:09:22 +1100114 if ((k->dsa->priv_key = BN_new()) == NULL)
115 fatal("key_new_private: BN_new failed");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100116 break;
117 case KEY_UNSPEC:
118 break;
119 default:
120 break;
121 }
122 return k;
123}
Ben Lindstrom836f0e92002-06-23 21:21:30 +0000124
Damien Miller450a7a12000-03-26 13:04:51 +1000125void
126key_free(Key *k)
127{
Damien Miller429fcc22006-03-26 14:02:16 +1100128 if (k == NULL)
Damien Millerbbaad772006-03-26 14:03:03 +1100129 fatal("key_free: key is NULL");
Damien Miller450a7a12000-03-26 13:04:51 +1000130 switch (k->type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100131 case KEY_RSA1:
Damien Miller450a7a12000-03-26 13:04:51 +1000132 case KEY_RSA:
133 if (k->rsa != NULL)
134 RSA_free(k->rsa);
135 k->rsa = NULL;
136 break;
137 case KEY_DSA:
138 if (k->dsa != NULL)
139 DSA_free(k->dsa);
140 k->dsa = NULL;
141 break;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100142 case KEY_UNSPEC:
143 break;
Damien Miller450a7a12000-03-26 13:04:51 +1000144 default:
145 fatal("key_free: bad key type %d", k->type);
146 break;
147 }
148 xfree(k);
149}
Damien Millerf58b58c2003-11-17 21:18:23 +1100150
Damien Miller450a7a12000-03-26 13:04:51 +1000151int
Damien Millerf58b58c2003-11-17 21:18:23 +1100152key_equal(const Key *a, const Key *b)
Damien Miller450a7a12000-03-26 13:04:51 +1000153{
154 if (a == NULL || b == NULL || a->type != b->type)
155 return 0;
156 switch (a->type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100157 case KEY_RSA1:
Damien Miller450a7a12000-03-26 13:04:51 +1000158 case KEY_RSA:
159 return a->rsa != NULL && b->rsa != NULL &&
160 BN_cmp(a->rsa->e, b->rsa->e) == 0 &&
161 BN_cmp(a->rsa->n, b->rsa->n) == 0;
Damien Miller450a7a12000-03-26 13:04:51 +1000162 case KEY_DSA:
163 return a->dsa != NULL && b->dsa != NULL &&
164 BN_cmp(a->dsa->p, b->dsa->p) == 0 &&
165 BN_cmp(a->dsa->q, b->dsa->q) == 0 &&
166 BN_cmp(a->dsa->g, b->dsa->g) == 0 &&
167 BN_cmp(a->dsa->pub_key, b->dsa->pub_key) == 0;
Damien Miller450a7a12000-03-26 13:04:51 +1000168 default:
Damien Millereba71ba2000-04-29 23:57:08 +1000169 fatal("key_equal: bad key type %d", a->type);
Damien Miller450a7a12000-03-26 13:04:51 +1000170 break;
171 }
172 return 0;
173}
174
Damien Miller37876e92003-05-15 10:19:46 +1000175u_char*
Damien Millerf58b58c2003-11-17 21:18:23 +1100176key_fingerprint_raw(const Key *k, enum fp_type dgst_type,
177 u_int *dgst_raw_length)
Damien Miller450a7a12000-03-26 13:04:51 +1000178{
Ben Lindstrom80cb27d2002-03-05 01:33:36 +0000179 const EVP_MD *md = NULL;
Ben Lindstromf0b48532001-03-12 02:59:31 +0000180 EVP_MD_CTX ctx;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000181 u_char *blob = NULL;
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000182 u_char *retval = NULL;
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000183 u_int len = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000184 int nlen, elen;
Damien Miller450a7a12000-03-26 13:04:51 +1000185
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000186 *dgst_raw_length = 0;
187
Ben Lindstromf0b48532001-03-12 02:59:31 +0000188 switch (dgst_type) {
189 case SSH_FP_MD5:
190 md = EVP_md5();
191 break;
192 case SSH_FP_SHA1:
193 md = EVP_sha1();
194 break;
195 default:
196 fatal("key_fingerprint_raw: bad digest type %d",
197 dgst_type);
198 }
Damien Miller450a7a12000-03-26 13:04:51 +1000199 switch (k->type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100200 case KEY_RSA1:
Damien Miller450a7a12000-03-26 13:04:51 +1000201 nlen = BN_num_bytes(k->rsa->n);
202 elen = BN_num_bytes(k->rsa->e);
203 len = nlen + elen;
Damien Millereba71ba2000-04-29 23:57:08 +1000204 blob = xmalloc(len);
205 BN_bn2bin(k->rsa->n, blob);
206 BN_bn2bin(k->rsa->e, blob + nlen);
Damien Miller450a7a12000-03-26 13:04:51 +1000207 break;
208 case KEY_DSA:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100209 case KEY_RSA:
210 key_to_blob(k, &blob, &len);
211 break;
212 case KEY_UNSPEC:
213 return retval;
Damien Miller450a7a12000-03-26 13:04:51 +1000214 default:
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000215 fatal("key_fingerprint_raw: bad key type %d", k->type);
Damien Miller450a7a12000-03-26 13:04:51 +1000216 break;
217 }
Damien Millereba71ba2000-04-29 23:57:08 +1000218 if (blob != NULL) {
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000219 retval = xmalloc(EVP_MAX_MD_SIZE);
Damien Miller6536c7d2000-06-22 21:32:31 +1000220 EVP_DigestInit(&ctx, md);
221 EVP_DigestUpdate(&ctx, blob, len);
Damien Miller3672e4b2002-02-05 11:54:07 +1100222 EVP_DigestFinal(&ctx, retval, dgst_raw_length);
Damien Millereba71ba2000-04-29 23:57:08 +1000223 memset(blob, 0, len);
224 xfree(blob);
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000225 } else {
226 fatal("key_fingerprint_raw: blob is null");
Damien Miller450a7a12000-03-26 13:04:51 +1000227 }
228 return retval;
229}
230
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000231static char *
232key_fingerprint_hex(u_char *dgst_raw, u_int dgst_raw_len)
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000233{
234 char *retval;
Damien Millereccb9de2005-06-17 12:59:34 +1000235 u_int i;
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000236
Damien Miller07d86be2006-03-26 14:19:21 +1100237 retval = xcalloc(1, dgst_raw_len * 3 + 1);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100238 for (i = 0; i < dgst_raw_len; i++) {
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000239 char hex[4];
240 snprintf(hex, sizeof(hex), "%02x:", dgst_raw[i]);
Darren Tucker29588612003-07-14 17:28:34 +1000241 strlcat(retval, hex, dgst_raw_len * 3 + 1);
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000242 }
Darren Tucker29588612003-07-14 17:28:34 +1000243
244 /* Remove the trailing ':' character */
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000245 retval[(dgst_raw_len * 3) - 1] = '\0';
246 return retval;
247}
248
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000249static char *
250key_fingerprint_bubblebabble(u_char *dgst_raw, u_int dgst_raw_len)
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000251{
252 char vowels[] = { 'a', 'e', 'i', 'o', 'u', 'y' };
253 char consonants[] = { 'b', 'c', 'd', 'f', 'g', 'h', 'k', 'l', 'm',
254 'n', 'p', 'r', 's', 't', 'v', 'z', 'x' };
Ben Lindstromcbe3ad22001-03-11 20:06:59 +0000255 u_int i, j = 0, rounds, seed = 1;
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000256 char *retval;
257
258 rounds = (dgst_raw_len / 2) + 1;
Damien Miller07d86be2006-03-26 14:19:21 +1100259 retval = xcalloc((rounds * 6), sizeof(char));
Ben Lindstromcbe3ad22001-03-11 20:06:59 +0000260 retval[j++] = 'x';
261 for (i = 0; i < rounds; i++) {
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000262 u_int idx0, idx1, idx2, idx3, idx4;
Ben Lindstromcbe3ad22001-03-11 20:06:59 +0000263 if ((i + 1 < rounds) || (dgst_raw_len % 2 != 0)) {
264 idx0 = (((((u_int)(dgst_raw[2 * i])) >> 6) & 3) +
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000265 seed) % 6;
Ben Lindstromcbe3ad22001-03-11 20:06:59 +0000266 idx1 = (((u_int)(dgst_raw[2 * i])) >> 2) & 15;
267 idx2 = ((((u_int)(dgst_raw[2 * i])) & 3) +
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000268 (seed / 6)) % 6;
Ben Lindstromcbe3ad22001-03-11 20:06:59 +0000269 retval[j++] = vowels[idx0];
270 retval[j++] = consonants[idx1];
271 retval[j++] = vowels[idx2];
272 if ((i + 1) < rounds) {
273 idx3 = (((u_int)(dgst_raw[(2 * i) + 1])) >> 4) & 15;
274 idx4 = (((u_int)(dgst_raw[(2 * i) + 1]))) & 15;
275 retval[j++] = consonants[idx3];
276 retval[j++] = '-';
277 retval[j++] = consonants[idx4];
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000278 seed = ((seed * 5) +
Ben Lindstromcbe3ad22001-03-11 20:06:59 +0000279 ((((u_int)(dgst_raw[2 * i])) * 7) +
280 ((u_int)(dgst_raw[(2 * i) + 1])))) % 36;
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000281 }
282 } else {
283 idx0 = seed % 6;
284 idx1 = 16;
285 idx2 = seed / 6;
Ben Lindstromcbe3ad22001-03-11 20:06:59 +0000286 retval[j++] = vowels[idx0];
287 retval[j++] = consonants[idx1];
288 retval[j++] = vowels[idx2];
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000289 }
290 }
Ben Lindstromcbe3ad22001-03-11 20:06:59 +0000291 retval[j++] = 'x';
292 retval[j++] = '\0';
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000293 return retval;
294}
295
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000296char *
Damien Millerf58b58c2003-11-17 21:18:23 +1100297key_fingerprint(const Key *k, enum fp_type dgst_type, enum fp_rep dgst_rep)
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000298{
Ben Lindstroma3700052001-04-05 23:26:32 +0000299 char *retval = NULL;
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000300 u_char *dgst_raw;
Damien Miller3672e4b2002-02-05 11:54:07 +1100301 u_int dgst_raw_len;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100302
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000303 dgst_raw = key_fingerprint_raw(k, dgst_type, &dgst_raw_len);
304 if (!dgst_raw)
Ben Lindstromcfccef92001-03-13 04:57:58 +0000305 fatal("key_fingerprint: null from key_fingerprint_raw()");
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000306 switch (dgst_rep) {
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000307 case SSH_FP_HEX:
308 retval = key_fingerprint_hex(dgst_raw, dgst_raw_len);
309 break;
310 case SSH_FP_BUBBLEBABBLE:
311 retval = key_fingerprint_bubblebabble(dgst_raw, dgst_raw_len);
312 break;
313 default:
314 fatal("key_fingerprint_ex: bad digest representation %d",
315 dgst_rep);
316 break;
317 }
318 memset(dgst_raw, 0, dgst_raw_len);
319 xfree(dgst_raw);
320 return retval;
321}
322
Damien Miller450a7a12000-03-26 13:04:51 +1000323/*
324 * Reads a multiple-precision integer in decimal from the buffer, and advances
325 * the pointer. The integer must already be initialized. This function is
326 * permitted to modify the buffer. This leaves *cpp to point just beyond the
327 * last processed (and maybe modified) character. Note that this may modify
328 * the buffer containing the number.
329 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000330static int
Damien Miller450a7a12000-03-26 13:04:51 +1000331read_bignum(char **cpp, BIGNUM * value)
332{
333 char *cp = *cpp;
334 int old;
335
336 /* Skip any leading whitespace. */
337 for (; *cp == ' ' || *cp == '\t'; cp++)
338 ;
339
340 /* Check that it begins with a decimal digit. */
341 if (*cp < '0' || *cp > '9')
342 return 0;
343
344 /* Save starting position. */
345 *cpp = cp;
346
347 /* Move forward until all decimal digits skipped. */
348 for (; *cp >= '0' && *cp <= '9'; cp++)
349 ;
350
351 /* Save the old terminating character, and replace it by \0. */
352 old = *cp;
353 *cp = 0;
354
355 /* Parse the number. */
356 if (BN_dec2bn(&value, *cpp) == 0)
357 return 0;
358
359 /* Restore old terminating character. */
360 *cp = old;
361
362 /* Move beyond the number and return success. */
363 *cpp = cp;
364 return 1;
365}
Ben Lindstrom836f0e92002-06-23 21:21:30 +0000366
Ben Lindstrombba81212001-06-25 05:01:22 +0000367static int
Damien Miller450a7a12000-03-26 13:04:51 +1000368write_bignum(FILE *f, BIGNUM *num)
369{
370 char *buf = BN_bn2dec(num);
371 if (buf == NULL) {
372 error("write_bignum: BN_bn2dec() failed");
373 return 0;
374 }
375 fprintf(f, " %s", buf);
Damien Milleraf3030f2001-10-10 15:00:49 +1000376 OPENSSL_free(buf);
Damien Miller450a7a12000-03-26 13:04:51 +1000377 return 1;
378}
Damien Miller0bc1bd82000-11-13 22:57:25 +1100379
Ben Lindstrom309f3d12001-09-20 00:55:53 +0000380/* returns 1 ok, -1 error */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100381int
Damien Millereba71ba2000-04-29 23:57:08 +1000382key_read(Key *ret, char **cpp)
Damien Miller450a7a12000-03-26 13:04:51 +1000383{
Damien Millereba71ba2000-04-29 23:57:08 +1000384 Key *k;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100385 int success = -1;
386 char *cp, *space;
387 int len, n, type;
388 u_int bits;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000389 u_char *blob;
Damien Millereba71ba2000-04-29 23:57:08 +1000390
391 cp = *cpp;
392
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000393 switch (ret->type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100394 case KEY_RSA1:
Damien Millereba71ba2000-04-29 23:57:08 +1000395 /* Get number of bits. */
396 if (*cp < '0' || *cp > '9')
Damien Miller0bc1bd82000-11-13 22:57:25 +1100397 return -1; /* Bad bit count... */
Damien Millereba71ba2000-04-29 23:57:08 +1000398 for (bits = 0; *cp >= '0' && *cp <= '9'; cp++)
399 bits = 10 * bits + *cp - '0';
Damien Miller450a7a12000-03-26 13:04:51 +1000400 if (bits == 0)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100401 return -1;
Damien Millereba71ba2000-04-29 23:57:08 +1000402 *cpp = cp;
Damien Miller450a7a12000-03-26 13:04:51 +1000403 /* Get public exponent, public modulus. */
404 if (!read_bignum(cpp, ret->rsa->e))
Damien Miller0bc1bd82000-11-13 22:57:25 +1100405 return -1;
Damien Miller450a7a12000-03-26 13:04:51 +1000406 if (!read_bignum(cpp, ret->rsa->n))
Damien Miller0bc1bd82000-11-13 22:57:25 +1100407 return -1;
408 success = 1;
Damien Miller450a7a12000-03-26 13:04:51 +1000409 break;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100410 case KEY_UNSPEC:
411 case KEY_RSA:
Damien Miller450a7a12000-03-26 13:04:51 +1000412 case KEY_DSA:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100413 space = strchr(cp, ' ');
414 if (space == NULL) {
Damien Miller386f1f32003-02-24 11:54:57 +1100415 debug3("key_read: missing whitespace");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100416 return -1;
417 }
418 *space = '\0';
419 type = key_type_from_name(cp);
420 *space = ' ';
421 if (type == KEY_UNSPEC) {
Damien Miller386f1f32003-02-24 11:54:57 +1100422 debug3("key_read: missing keytype");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100423 return -1;
424 }
425 cp = space+1;
426 if (*cp == '\0') {
427 debug3("key_read: short string");
428 return -1;
429 }
430 if (ret->type == KEY_UNSPEC) {
431 ret->type = type;
432 } else if (ret->type != type) {
433 /* is a key, but different type */
434 debug3("key_read: type mismatch");
Ben Lindstrom309f3d12001-09-20 00:55:53 +0000435 return -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100436 }
Damien Millereba71ba2000-04-29 23:57:08 +1000437 len = 2*strlen(cp);
438 blob = xmalloc(len);
439 n = uudecode(cp, blob, len);
Damien Millere247cc42000-05-07 12:03:14 +1000440 if (n < 0) {
Damien Millerb1715dc2000-05-30 13:44:51 +1000441 error("key_read: uudecode %s failed", cp);
Ben Lindstrom4cbc1812001-12-06 16:41:41 +0000442 xfree(blob);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100443 return -1;
Damien Millere247cc42000-05-07 12:03:14 +1000444 }
Darren Tucker502d3842003-06-28 12:38:01 +1000445 k = key_from_blob(blob, (u_int)n);
Ben Lindstrom4cbc1812001-12-06 16:41:41 +0000446 xfree(blob);
Damien Millerb1715dc2000-05-30 13:44:51 +1000447 if (k == NULL) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100448 error("key_read: key_from_blob %s failed", cp);
449 return -1;
Damien Millerb1715dc2000-05-30 13:44:51 +1000450 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100451 if (k->type != type) {
452 error("key_read: type mismatch: encoding error");
453 key_free(k);
454 return -1;
455 }
456/*XXXX*/
457 if (ret->type == KEY_RSA) {
458 if (ret->rsa != NULL)
459 RSA_free(ret->rsa);
460 ret->rsa = k->rsa;
461 k->rsa = NULL;
462 success = 1;
463#ifdef DEBUG_PK
464 RSA_print_fp(stderr, ret->rsa, 8);
465#endif
466 } else {
467 if (ret->dsa != NULL)
468 DSA_free(ret->dsa);
469 ret->dsa = k->dsa;
470 k->dsa = NULL;
471 success = 1;
472#ifdef DEBUG_PK
473 DSA_print_fp(stderr, ret->dsa, 8);
474#endif
475 }
476/*XXXX*/
Ben Lindstrom4cbc1812001-12-06 16:41:41 +0000477 key_free(k);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100478 if (success != 1)
479 break;
Damien Millerb1715dc2000-05-30 13:44:51 +1000480 /* advance cp: skip whitespace and data */
481 while (*cp == ' ' || *cp == '\t')
482 cp++;
483 while (*cp != '\0' && *cp != ' ' && *cp != '\t')
484 cp++;
485 *cpp = cp;
Damien Miller450a7a12000-03-26 13:04:51 +1000486 break;
487 default:
Damien Millereba71ba2000-04-29 23:57:08 +1000488 fatal("key_read: bad key type: %d", ret->type);
Damien Miller450a7a12000-03-26 13:04:51 +1000489 break;
490 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100491 return success;
Damien Miller450a7a12000-03-26 13:04:51 +1000492}
Ben Lindstrom836f0e92002-06-23 21:21:30 +0000493
Damien Miller450a7a12000-03-26 13:04:51 +1000494int
Damien Millerf58b58c2003-11-17 21:18:23 +1100495key_write(const Key *key, FILE *f)
Damien Miller450a7a12000-03-26 13:04:51 +1000496{
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000497 int n, success = 0;
498 u_int len, bits = 0;
Damien Millera10f5612002-09-12 09:49:15 +1000499 u_char *blob;
500 char *uu;
Damien Miller450a7a12000-03-26 13:04:51 +1000501
Damien Miller0bc1bd82000-11-13 22:57:25 +1100502 if (key->type == KEY_RSA1 && key->rsa != NULL) {
Damien Miller450a7a12000-03-26 13:04:51 +1000503 /* size of modulus 'n' */
504 bits = BN_num_bits(key->rsa->n);
505 fprintf(f, "%u", bits);
506 if (write_bignum(f, key->rsa->e) &&
507 write_bignum(f, key->rsa->n)) {
508 success = 1;
509 } else {
510 error("key_write: failed for RSA key");
511 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100512 } else if ((key->type == KEY_DSA && key->dsa != NULL) ||
513 (key->type == KEY_RSA && key->rsa != NULL)) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100514 key_to_blob(key, &blob, &len);
Damien Millereba71ba2000-04-29 23:57:08 +1000515 uu = xmalloc(2*len);
Damien Millere247cc42000-05-07 12:03:14 +1000516 n = uuencode(blob, len, uu, 2*len);
517 if (n > 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100518 fprintf(f, "%s %s", key_ssh_name(key), uu);
Damien Millere247cc42000-05-07 12:03:14 +1000519 success = 1;
520 }
Damien Millereba71ba2000-04-29 23:57:08 +1000521 xfree(blob);
522 xfree(uu);
Damien Miller450a7a12000-03-26 13:04:51 +1000523 }
524 return success;
525}
Ben Lindstrom836f0e92002-06-23 21:21:30 +0000526
Damien Millerf58b58c2003-11-17 21:18:23 +1100527const char *
528key_type(const Key *k)
Damien Millere247cc42000-05-07 12:03:14 +1000529{
530 switch (k->type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100531 case KEY_RSA1:
532 return "RSA1";
Damien Millere247cc42000-05-07 12:03:14 +1000533 case KEY_RSA:
534 return "RSA";
Damien Millere247cc42000-05-07 12:03:14 +1000535 case KEY_DSA:
536 return "DSA";
Damien Millere247cc42000-05-07 12:03:14 +1000537 }
538 return "unknown";
539}
Ben Lindstrom836f0e92002-06-23 21:21:30 +0000540
Damien Millerf58b58c2003-11-17 21:18:23 +1100541const char *
542key_ssh_name(const Key *k)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100543{
544 switch (k->type) {
545 case KEY_RSA:
546 return "ssh-rsa";
Damien Miller0bc1bd82000-11-13 22:57:25 +1100547 case KEY_DSA:
548 return "ssh-dss";
Damien Miller0bc1bd82000-11-13 22:57:25 +1100549 }
550 return "ssh-unknown";
551}
Ben Lindstrom836f0e92002-06-23 21:21:30 +0000552
Damien Miller0bc1bd82000-11-13 22:57:25 +1100553u_int
Damien Millerf58b58c2003-11-17 21:18:23 +1100554key_size(const Key *k)
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000555{
Damien Millerad833b32000-08-23 10:46:23 +1000556 switch (k->type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100557 case KEY_RSA1:
Damien Millerad833b32000-08-23 10:46:23 +1000558 case KEY_RSA:
559 return BN_num_bits(k->rsa->n);
Damien Millerad833b32000-08-23 10:46:23 +1000560 case KEY_DSA:
561 return BN_num_bits(k->dsa->p);
Damien Millerad833b32000-08-23 10:46:23 +1000562 }
563 return 0;
564}
Damien Miller0bc1bd82000-11-13 22:57:25 +1100565
Ben Lindstrombba81212001-06-25 05:01:22 +0000566static RSA *
Ben Lindstrom46c16222000-12-22 01:43:59 +0000567rsa_generate_private_key(u_int bits)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100568{
Kevin Stevesef4eea92001-02-05 12:42:17 +0000569 RSA *private;
Damien Miller69b72032006-03-26 14:02:35 +1100570
Kevin Stevesef4eea92001-02-05 12:42:17 +0000571 private = RSA_generate_key(bits, 35, NULL, NULL);
572 if (private == NULL)
573 fatal("rsa_generate_private_key: key generation failed.");
574 return private;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100575}
576
Ben Lindstrombba81212001-06-25 05:01:22 +0000577static DSA*
Ben Lindstrom46c16222000-12-22 01:43:59 +0000578dsa_generate_private_key(u_int bits)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100579{
580 DSA *private = DSA_generate_parameters(bits, NULL, 0, NULL, NULL, NULL, NULL);
Damien Miller69b72032006-03-26 14:02:35 +1100581
Damien Miller0bc1bd82000-11-13 22:57:25 +1100582 if (private == NULL)
583 fatal("dsa_generate_private_key: DSA_generate_parameters failed");
584 if (!DSA_generate_key(private))
Kevin Stevesef4eea92001-02-05 12:42:17 +0000585 fatal("dsa_generate_private_key: DSA_generate_key failed.");
586 if (private == NULL)
587 fatal("dsa_generate_private_key: NULL.");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100588 return private;
589}
590
591Key *
Ben Lindstrom46c16222000-12-22 01:43:59 +0000592key_generate(int type, u_int bits)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100593{
594 Key *k = key_new(KEY_UNSPEC);
595 switch (type) {
Kevin Stevesef4eea92001-02-05 12:42:17 +0000596 case KEY_DSA:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100597 k->dsa = dsa_generate_private_key(bits);
598 break;
599 case KEY_RSA:
600 case KEY_RSA1:
601 k->rsa = rsa_generate_private_key(bits);
602 break;
603 default:
Kevin Stevesef4eea92001-02-05 12:42:17 +0000604 fatal("key_generate: unknown type %d", type);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100605 }
Kevin Stevesef4eea92001-02-05 12:42:17 +0000606 k->type = type;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100607 return k;
608}
609
610Key *
Damien Millerf58b58c2003-11-17 21:18:23 +1100611key_from_private(const Key *k)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100612{
613 Key *n = NULL;
614 switch (k->type) {
Kevin Stevesef4eea92001-02-05 12:42:17 +0000615 case KEY_DSA:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100616 n = key_new(k->type);
617 BN_copy(n->dsa->p, k->dsa->p);
618 BN_copy(n->dsa->q, k->dsa->q);
619 BN_copy(n->dsa->g, k->dsa->g);
620 BN_copy(n->dsa->pub_key, k->dsa->pub_key);
621 break;
622 case KEY_RSA:
623 case KEY_RSA1:
624 n = key_new(k->type);
625 BN_copy(n->rsa->n, k->rsa->n);
626 BN_copy(n->rsa->e, k->rsa->e);
627 break;
628 default:
Kevin Stevesef4eea92001-02-05 12:42:17 +0000629 fatal("key_from_private: unknown type %d", k->type);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100630 break;
631 }
632 return n;
633}
634
635int
636key_type_from_name(char *name)
637{
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000638 if (strcmp(name, "rsa1") == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100639 return KEY_RSA1;
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000640 } else if (strcmp(name, "rsa") == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100641 return KEY_RSA;
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000642 } else if (strcmp(name, "dsa") == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100643 return KEY_DSA;
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000644 } else if (strcmp(name, "ssh-rsa") == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100645 return KEY_RSA;
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000646 } else if (strcmp(name, "ssh-dss") == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100647 return KEY_DSA;
648 }
Ben Lindstromb54873a2001-03-11 20:01:55 +0000649 debug2("key_type_from_name: unknown key type '%s'", name);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100650 return KEY_UNSPEC;
651}
652
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000653int
654key_names_valid2(const char *names)
655{
656 char *s, *cp, *p;
657
658 if (names == NULL || strcmp(names, "") == 0)
659 return 0;
660 s = cp = xstrdup(names);
661 for ((p = strsep(&cp, ",")); p && *p != '\0';
Damien Miller9f0f5c62001-12-21 14:45:46 +1100662 (p = strsep(&cp, ","))) {
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000663 switch (key_type_from_name(p)) {
664 case KEY_RSA1:
665 case KEY_UNSPEC:
666 xfree(s);
667 return 0;
668 }
669 }
670 debug3("key names ok: [%s]", names);
671 xfree(s);
672 return 1;
673}
674
Damien Miller0bc1bd82000-11-13 22:57:25 +1100675Key *
Damien Millerf58b58c2003-11-17 21:18:23 +1100676key_from_blob(const u_char *blob, u_int blen)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100677{
678 Buffer b;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100679 int rlen, type;
Darren Tucker08d04fa2004-11-05 20:42:28 +1100680 char *ktype = NULL;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100681 Key *key = NULL;
682
683#ifdef DEBUG_PK
684 dump_base64(stderr, blob, blen);
685#endif
686 buffer_init(&b);
687 buffer_append(&b, blob, blen);
Darren Tucker08d04fa2004-11-05 20:42:28 +1100688 if ((ktype = buffer_get_string_ret(&b, NULL)) == NULL) {
689 error("key_from_blob: can't read key type");
690 goto out;
691 }
692
Damien Miller0bc1bd82000-11-13 22:57:25 +1100693 type = key_type_from_name(ktype);
694
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000695 switch (type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100696 case KEY_RSA:
697 key = key_new(type);
Darren Tucker08d04fa2004-11-05 20:42:28 +1100698 if (buffer_get_bignum2_ret(&b, key->rsa->e) == -1 ||
699 buffer_get_bignum2_ret(&b, key->rsa->n) == -1) {
700 error("key_from_blob: can't read rsa key");
701 key_free(key);
702 key = NULL;
703 goto out;
704 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100705#ifdef DEBUG_PK
706 RSA_print_fp(stderr, key->rsa, 8);
707#endif
708 break;
709 case KEY_DSA:
710 key = key_new(type);
Darren Tucker08d04fa2004-11-05 20:42:28 +1100711 if (buffer_get_bignum2_ret(&b, key->dsa->p) == -1 ||
712 buffer_get_bignum2_ret(&b, key->dsa->q) == -1 ||
713 buffer_get_bignum2_ret(&b, key->dsa->g) == -1 ||
714 buffer_get_bignum2_ret(&b, key->dsa->pub_key) == -1) {
715 error("key_from_blob: can't read dsa key");
716 key_free(key);
717 key = NULL;
718 goto out;
719 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100720#ifdef DEBUG_PK
721 DSA_print_fp(stderr, key->dsa, 8);
722#endif
723 break;
724 case KEY_UNSPEC:
725 key = key_new(type);
726 break;
727 default:
728 error("key_from_blob: cannot handle type %s", ktype);
Darren Tucker08d04fa2004-11-05 20:42:28 +1100729 goto out;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100730 }
731 rlen = buffer_len(&b);
732 if (key != NULL && rlen != 0)
733 error("key_from_blob: remaining bytes in key blob %d", rlen);
Darren Tucker08d04fa2004-11-05 20:42:28 +1100734 out:
735 if (ktype != NULL)
736 xfree(ktype);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100737 buffer_free(&b);
738 return key;
739}
740
741int
Damien Millerf58b58c2003-11-17 21:18:23 +1100742key_to_blob(const Key *key, u_char **blobp, u_int *lenp)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100743{
744 Buffer b;
745 int len;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100746
747 if (key == NULL) {
748 error("key_to_blob: key == NULL");
749 return 0;
750 }
751 buffer_init(&b);
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000752 switch (key->type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100753 case KEY_DSA:
754 buffer_put_cstring(&b, key_ssh_name(key));
755 buffer_put_bignum2(&b, key->dsa->p);
756 buffer_put_bignum2(&b, key->dsa->q);
757 buffer_put_bignum2(&b, key->dsa->g);
758 buffer_put_bignum2(&b, key->dsa->pub_key);
759 break;
760 case KEY_RSA:
761 buffer_put_cstring(&b, key_ssh_name(key));
Damien Miller0bc1bd82000-11-13 22:57:25 +1100762 buffer_put_bignum2(&b, key->rsa->e);
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000763 buffer_put_bignum2(&b, key->rsa->n);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100764 break;
765 default:
Ben Lindstrom99a30f12001-09-18 05:49:14 +0000766 error("key_to_blob: unsupported key type %d", key->type);
767 buffer_free(&b);
768 return 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100769 }
770 len = buffer_len(&b);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100771 if (lenp != NULL)
772 *lenp = len;
Ben Lindstrom2bf759c2002-07-07 22:13:31 +0000773 if (blobp != NULL) {
774 *blobp = xmalloc(len);
775 memcpy(*blobp, buffer_ptr(&b), len);
776 }
777 memset(buffer_ptr(&b), 0, len);
778 buffer_free(&b);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100779 return len;
780}
781
782int
783key_sign(
Damien Millerf58b58c2003-11-17 21:18:23 +1100784 const Key *key,
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000785 u_char **sigp, u_int *lenp,
Damien Millerf58b58c2003-11-17 21:18:23 +1100786 const u_char *data, u_int datalen)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100787{
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000788 switch (key->type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100789 case KEY_DSA:
790 return ssh_dss_sign(key, sigp, lenp, data, datalen);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100791 case KEY_RSA:
792 return ssh_rsa_sign(key, sigp, lenp, data, datalen);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100793 default:
Darren Tucker5cb30ad2004-08-12 22:40:24 +1000794 error("key_sign: invalid key type %d", key->type);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100795 return -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100796 }
797}
798
Ben Lindstrom01fff0c2002-06-06 20:54:07 +0000799/*
800 * key_verify returns 1 for a correct signature, 0 for an incorrect signature
801 * and -1 on error.
802 */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100803int
804key_verify(
Damien Millerf58b58c2003-11-17 21:18:23 +1100805 const Key *key,
806 const u_char *signature, u_int signaturelen,
807 const u_char *data, u_int datalen)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100808{
Ben Lindstrom5363aee2001-06-25 04:42:20 +0000809 if (signaturelen == 0)
810 return -1;
811
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000812 switch (key->type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100813 case KEY_DSA:
814 return ssh_dss_verify(key, signature, signaturelen, data, datalen);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100815 case KEY_RSA:
816 return ssh_rsa_verify(key, signature, signaturelen, data, datalen);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100817 default:
Darren Tucker5cb30ad2004-08-12 22:40:24 +1000818 error("key_verify: invalid key type %d", key->type);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100819 return -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100820 }
821}
Ben Lindstroma674e8d2002-03-22 01:45:53 +0000822
823/* Converts a private to a public key */
Ben Lindstroma674e8d2002-03-22 01:45:53 +0000824Key *
Damien Millerf58b58c2003-11-17 21:18:23 +1100825key_demote(const Key *k)
Ben Lindstroma674e8d2002-03-22 01:45:53 +0000826{
827 Key *pk;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000828
Damien Miller07d86be2006-03-26 14:19:21 +1100829 pk = xcalloc(1, sizeof(*pk));
Ben Lindstroma674e8d2002-03-22 01:45:53 +0000830 pk->type = k->type;
831 pk->flags = k->flags;
832 pk->dsa = NULL;
833 pk->rsa = NULL;
834
835 switch (k->type) {
836 case KEY_RSA1:
837 case KEY_RSA:
838 if ((pk->rsa = RSA_new()) == NULL)
839 fatal("key_demote: RSA_new failed");
840 if ((pk->rsa->e = BN_dup(k->rsa->e)) == NULL)
841 fatal("key_demote: BN_dup failed");
842 if ((pk->rsa->n = BN_dup(k->rsa->n)) == NULL)
843 fatal("key_demote: BN_dup failed");
844 break;
845 case KEY_DSA:
846 if ((pk->dsa = DSA_new()) == NULL)
847 fatal("key_demote: DSA_new failed");
848 if ((pk->dsa->p = BN_dup(k->dsa->p)) == NULL)
849 fatal("key_demote: BN_dup failed");
850 if ((pk->dsa->q = BN_dup(k->dsa->q)) == NULL)
851 fatal("key_demote: BN_dup failed");
852 if ((pk->dsa->g = BN_dup(k->dsa->g)) == NULL)
853 fatal("key_demote: BN_dup failed");
854 if ((pk->dsa->pub_key = BN_dup(k->dsa->pub_key)) == NULL)
855 fatal("key_demote: BN_dup failed");
856 break;
857 default:
858 fatal("key_free: bad key type %d", k->type);
859 break;
860 }
861
862 return (pk);
863}