blob: 40aab20ea9157ee977f058b567979460be1fd4b3 [file] [log] [blame]
Damien Millerd7834352006-08-05 12:39:39 +10001/* $OpenBSD: key.c,v 1.67 2006/08/03 03:34:42 deraadt 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 Millerd7834352006-08-05 12:39:39 +100035
Damien Miller450a7a12000-03-26 13:04:51 +100036#include "includes.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000037
Damien Millerd7834352006-08-05 12:39:39 +100038#include <sys/types.h>
39
Damien Miller450a7a12000-03-26 13:04:51 +100040#include <openssl/evp.h>
Ben Lindstrom226cfa02001-01-22 05:34:40 +000041
Damien Millera7a73ee2006-08-05 11:37:59 +100042#include <stdio.h>
Damien Millere3476ed2006-07-24 14:13:33 +100043#include <string.h>
44
Damien Miller450a7a12000-03-26 13:04:51 +100045#include "xmalloc.h"
46#include "key.h"
Damien Miller0bc1bd82000-11-13 22:57:25 +110047#include "rsa.h"
Damien Millereba71ba2000-04-29 23:57:08 +100048#include "uuencode.h"
Damien Miller0bc1bd82000-11-13 22:57:25 +110049#include "buffer.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000050#include "log.h"
Damien Miller450a7a12000-03-26 13:04:51 +100051
52Key *
53key_new(int type)
54{
55 Key *k;
56 RSA *rsa;
57 DSA *dsa;
Damien Miller07d86be2006-03-26 14:19:21 +110058 k = xcalloc(1, sizeof(*k));
Damien Miller450a7a12000-03-26 13:04:51 +100059 k->type = type;
Damien Millereba71ba2000-04-29 23:57:08 +100060 k->dsa = NULL;
61 k->rsa = NULL;
Damien Miller450a7a12000-03-26 13:04:51 +100062 switch (k->type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +110063 case KEY_RSA1:
Damien Miller450a7a12000-03-26 13:04:51 +100064 case KEY_RSA:
Damien Millerda755162002-01-22 23:09:22 +110065 if ((rsa = RSA_new()) == NULL)
66 fatal("key_new: RSA_new failed");
67 if ((rsa->n = BN_new()) == NULL)
68 fatal("key_new: BN_new failed");
69 if ((rsa->e = BN_new()) == NULL)
70 fatal("key_new: BN_new failed");
Damien Miller450a7a12000-03-26 13:04:51 +100071 k->rsa = rsa;
72 break;
73 case KEY_DSA:
Damien Millerda755162002-01-22 23:09:22 +110074 if ((dsa = DSA_new()) == NULL)
75 fatal("key_new: DSA_new failed");
76 if ((dsa->p = BN_new()) == NULL)
77 fatal("key_new: BN_new failed");
78 if ((dsa->q = BN_new()) == NULL)
79 fatal("key_new: BN_new failed");
80 if ((dsa->g = BN_new()) == NULL)
81 fatal("key_new: BN_new failed");
82 if ((dsa->pub_key = BN_new()) == NULL)
83 fatal("key_new: BN_new failed");
Damien Miller450a7a12000-03-26 13:04:51 +100084 k->dsa = dsa;
85 break;
Damien Miller0bc1bd82000-11-13 22:57:25 +110086 case KEY_UNSPEC:
Damien Miller450a7a12000-03-26 13:04:51 +100087 break;
88 default:
89 fatal("key_new: bad key type %d", k->type);
90 break;
91 }
92 return k;
93}
Ben Lindstrom836f0e92002-06-23 21:21:30 +000094
Damien Miller0bc1bd82000-11-13 22:57:25 +110095Key *
96key_new_private(int type)
97{
98 Key *k = key_new(type);
99 switch (k->type) {
100 case KEY_RSA1:
101 case KEY_RSA:
Damien Millerda755162002-01-22 23:09:22 +1100102 if ((k->rsa->d = BN_new()) == NULL)
103 fatal("key_new_private: BN_new failed");
104 if ((k->rsa->iqmp = BN_new()) == NULL)
105 fatal("key_new_private: BN_new failed");
106 if ((k->rsa->q = BN_new()) == NULL)
107 fatal("key_new_private: BN_new failed");
108 if ((k->rsa->p = BN_new()) == NULL)
109 fatal("key_new_private: BN_new failed");
110 if ((k->rsa->dmq1 = BN_new()) == NULL)
111 fatal("key_new_private: BN_new failed");
112 if ((k->rsa->dmp1 = BN_new()) == NULL)
113 fatal("key_new_private: BN_new failed");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100114 break;
115 case KEY_DSA:
Damien Millerda755162002-01-22 23:09:22 +1100116 if ((k->dsa->priv_key = BN_new()) == NULL)
117 fatal("key_new_private: BN_new failed");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100118 break;
119 case KEY_UNSPEC:
120 break;
121 default:
122 break;
123 }
124 return k;
125}
Ben Lindstrom836f0e92002-06-23 21:21:30 +0000126
Damien Miller450a7a12000-03-26 13:04:51 +1000127void
128key_free(Key *k)
129{
Damien Miller429fcc22006-03-26 14:02:16 +1100130 if (k == NULL)
Damien Millerbbaad772006-03-26 14:03:03 +1100131 fatal("key_free: key is NULL");
Damien Miller450a7a12000-03-26 13:04:51 +1000132 switch (k->type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100133 case KEY_RSA1:
Damien Miller450a7a12000-03-26 13:04:51 +1000134 case KEY_RSA:
135 if (k->rsa != NULL)
136 RSA_free(k->rsa);
137 k->rsa = NULL;
138 break;
139 case KEY_DSA:
140 if (k->dsa != NULL)
141 DSA_free(k->dsa);
142 k->dsa = NULL;
143 break;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100144 case KEY_UNSPEC:
145 break;
Damien Miller450a7a12000-03-26 13:04:51 +1000146 default:
147 fatal("key_free: bad key type %d", k->type);
148 break;
149 }
150 xfree(k);
151}
Damien Millerf58b58c2003-11-17 21:18:23 +1100152
Damien Miller450a7a12000-03-26 13:04:51 +1000153int
Damien Millerf58b58c2003-11-17 21:18:23 +1100154key_equal(const Key *a, const Key *b)
Damien Miller450a7a12000-03-26 13:04:51 +1000155{
156 if (a == NULL || b == NULL || a->type != b->type)
157 return 0;
158 switch (a->type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100159 case KEY_RSA1:
Damien Miller450a7a12000-03-26 13:04:51 +1000160 case KEY_RSA:
161 return a->rsa != NULL && b->rsa != NULL &&
162 BN_cmp(a->rsa->e, b->rsa->e) == 0 &&
163 BN_cmp(a->rsa->n, b->rsa->n) == 0;
Damien Miller450a7a12000-03-26 13:04:51 +1000164 case KEY_DSA:
165 return a->dsa != NULL && b->dsa != NULL &&
166 BN_cmp(a->dsa->p, b->dsa->p) == 0 &&
167 BN_cmp(a->dsa->q, b->dsa->q) == 0 &&
168 BN_cmp(a->dsa->g, b->dsa->g) == 0 &&
169 BN_cmp(a->dsa->pub_key, b->dsa->pub_key) == 0;
Damien Miller450a7a12000-03-26 13:04:51 +1000170 default:
Damien Millereba71ba2000-04-29 23:57:08 +1000171 fatal("key_equal: bad key type %d", a->type);
Damien Miller450a7a12000-03-26 13:04:51 +1000172 break;
173 }
174 return 0;
175}
176
Damien Miller37876e92003-05-15 10:19:46 +1000177u_char*
Damien Millerf58b58c2003-11-17 21:18:23 +1100178key_fingerprint_raw(const Key *k, enum fp_type dgst_type,
179 u_int *dgst_raw_length)
Damien Miller450a7a12000-03-26 13:04:51 +1000180{
Ben Lindstrom80cb27d2002-03-05 01:33:36 +0000181 const EVP_MD *md = NULL;
Ben Lindstromf0b48532001-03-12 02:59:31 +0000182 EVP_MD_CTX ctx;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000183 u_char *blob = NULL;
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000184 u_char *retval = NULL;
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000185 u_int len = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000186 int nlen, elen;
Damien Miller450a7a12000-03-26 13:04:51 +1000187
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000188 *dgst_raw_length = 0;
189
Ben Lindstromf0b48532001-03-12 02:59:31 +0000190 switch (dgst_type) {
191 case SSH_FP_MD5:
192 md = EVP_md5();
193 break;
194 case SSH_FP_SHA1:
195 md = EVP_sha1();
196 break;
197 default:
198 fatal("key_fingerprint_raw: bad digest type %d",
199 dgst_type);
200 }
Damien Miller450a7a12000-03-26 13:04:51 +1000201 switch (k->type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100202 case KEY_RSA1:
Damien Miller450a7a12000-03-26 13:04:51 +1000203 nlen = BN_num_bytes(k->rsa->n);
204 elen = BN_num_bytes(k->rsa->e);
205 len = nlen + elen;
Damien Millereba71ba2000-04-29 23:57:08 +1000206 blob = xmalloc(len);
207 BN_bn2bin(k->rsa->n, blob);
208 BN_bn2bin(k->rsa->e, blob + nlen);
Damien Miller450a7a12000-03-26 13:04:51 +1000209 break;
210 case KEY_DSA:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100211 case KEY_RSA:
212 key_to_blob(k, &blob, &len);
213 break;
214 case KEY_UNSPEC:
215 return retval;
Damien Miller450a7a12000-03-26 13:04:51 +1000216 default:
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000217 fatal("key_fingerprint_raw: bad key type %d", k->type);
Damien Miller450a7a12000-03-26 13:04:51 +1000218 break;
219 }
Damien Millereba71ba2000-04-29 23:57:08 +1000220 if (blob != NULL) {
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000221 retval = xmalloc(EVP_MAX_MD_SIZE);
Damien Miller6536c7d2000-06-22 21:32:31 +1000222 EVP_DigestInit(&ctx, md);
223 EVP_DigestUpdate(&ctx, blob, len);
Damien Miller3672e4b2002-02-05 11:54:07 +1100224 EVP_DigestFinal(&ctx, retval, dgst_raw_length);
Damien Millereba71ba2000-04-29 23:57:08 +1000225 memset(blob, 0, len);
226 xfree(blob);
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000227 } else {
228 fatal("key_fingerprint_raw: blob is null");
Damien Miller450a7a12000-03-26 13:04:51 +1000229 }
230 return retval;
231}
232
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000233static char *
234key_fingerprint_hex(u_char *dgst_raw, u_int dgst_raw_len)
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000235{
236 char *retval;
Damien Millereccb9de2005-06-17 12:59:34 +1000237 u_int i;
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000238
Damien Miller07d86be2006-03-26 14:19:21 +1100239 retval = xcalloc(1, dgst_raw_len * 3 + 1);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100240 for (i = 0; i < dgst_raw_len; i++) {
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000241 char hex[4];
242 snprintf(hex, sizeof(hex), "%02x:", dgst_raw[i]);
Darren Tucker29588612003-07-14 17:28:34 +1000243 strlcat(retval, hex, dgst_raw_len * 3 + 1);
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000244 }
Darren Tucker29588612003-07-14 17:28:34 +1000245
246 /* Remove the trailing ':' character */
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000247 retval[(dgst_raw_len * 3) - 1] = '\0';
248 return retval;
249}
250
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000251static char *
252key_fingerprint_bubblebabble(u_char *dgst_raw, u_int dgst_raw_len)
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000253{
254 char vowels[] = { 'a', 'e', 'i', 'o', 'u', 'y' };
255 char consonants[] = { 'b', 'c', 'd', 'f', 'g', 'h', 'k', 'l', 'm',
256 'n', 'p', 'r', 's', 't', 'v', 'z', 'x' };
Ben Lindstromcbe3ad22001-03-11 20:06:59 +0000257 u_int i, j = 0, rounds, seed = 1;
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000258 char *retval;
259
260 rounds = (dgst_raw_len / 2) + 1;
Damien Miller07d86be2006-03-26 14:19:21 +1100261 retval = xcalloc((rounds * 6), sizeof(char));
Ben Lindstromcbe3ad22001-03-11 20:06:59 +0000262 retval[j++] = 'x';
263 for (i = 0; i < rounds; i++) {
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000264 u_int idx0, idx1, idx2, idx3, idx4;
Ben Lindstromcbe3ad22001-03-11 20:06:59 +0000265 if ((i + 1 < rounds) || (dgst_raw_len % 2 != 0)) {
266 idx0 = (((((u_int)(dgst_raw[2 * i])) >> 6) & 3) +
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000267 seed) % 6;
Ben Lindstromcbe3ad22001-03-11 20:06:59 +0000268 idx1 = (((u_int)(dgst_raw[2 * i])) >> 2) & 15;
269 idx2 = ((((u_int)(dgst_raw[2 * i])) & 3) +
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000270 (seed / 6)) % 6;
Ben Lindstromcbe3ad22001-03-11 20:06:59 +0000271 retval[j++] = vowels[idx0];
272 retval[j++] = consonants[idx1];
273 retval[j++] = vowels[idx2];
274 if ((i + 1) < rounds) {
275 idx3 = (((u_int)(dgst_raw[(2 * i) + 1])) >> 4) & 15;
276 idx4 = (((u_int)(dgst_raw[(2 * i) + 1]))) & 15;
277 retval[j++] = consonants[idx3];
278 retval[j++] = '-';
279 retval[j++] = consonants[idx4];
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000280 seed = ((seed * 5) +
Ben Lindstromcbe3ad22001-03-11 20:06:59 +0000281 ((((u_int)(dgst_raw[2 * i])) * 7) +
282 ((u_int)(dgst_raw[(2 * i) + 1])))) % 36;
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000283 }
284 } else {
285 idx0 = seed % 6;
286 idx1 = 16;
287 idx2 = seed / 6;
Ben Lindstromcbe3ad22001-03-11 20:06:59 +0000288 retval[j++] = vowels[idx0];
289 retval[j++] = consonants[idx1];
290 retval[j++] = vowels[idx2];
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000291 }
292 }
Ben Lindstromcbe3ad22001-03-11 20:06:59 +0000293 retval[j++] = 'x';
294 retval[j++] = '\0';
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000295 return retval;
296}
297
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000298char *
Damien Millerf58b58c2003-11-17 21:18:23 +1100299key_fingerprint(const Key *k, enum fp_type dgst_type, enum fp_rep dgst_rep)
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000300{
Ben Lindstroma3700052001-04-05 23:26:32 +0000301 char *retval = NULL;
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000302 u_char *dgst_raw;
Damien Miller3672e4b2002-02-05 11:54:07 +1100303 u_int dgst_raw_len;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100304
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000305 dgst_raw = key_fingerprint_raw(k, dgst_type, &dgst_raw_len);
306 if (!dgst_raw)
Ben Lindstromcfccef92001-03-13 04:57:58 +0000307 fatal("key_fingerprint: null from key_fingerprint_raw()");
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000308 switch (dgst_rep) {
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000309 case SSH_FP_HEX:
310 retval = key_fingerprint_hex(dgst_raw, dgst_raw_len);
311 break;
312 case SSH_FP_BUBBLEBABBLE:
313 retval = key_fingerprint_bubblebabble(dgst_raw, dgst_raw_len);
314 break;
315 default:
316 fatal("key_fingerprint_ex: bad digest representation %d",
317 dgst_rep);
318 break;
319 }
320 memset(dgst_raw, 0, dgst_raw_len);
321 xfree(dgst_raw);
322 return retval;
323}
324
Damien Miller450a7a12000-03-26 13:04:51 +1000325/*
326 * Reads a multiple-precision integer in decimal from the buffer, and advances
327 * the pointer. The integer must already be initialized. This function is
328 * permitted to modify the buffer. This leaves *cpp to point just beyond the
329 * last processed (and maybe modified) character. Note that this may modify
330 * the buffer containing the number.
331 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000332static int
Damien Miller450a7a12000-03-26 13:04:51 +1000333read_bignum(char **cpp, BIGNUM * value)
334{
335 char *cp = *cpp;
336 int old;
337
338 /* Skip any leading whitespace. */
339 for (; *cp == ' ' || *cp == '\t'; cp++)
340 ;
341
342 /* Check that it begins with a decimal digit. */
343 if (*cp < '0' || *cp > '9')
344 return 0;
345
346 /* Save starting position. */
347 *cpp = cp;
348
349 /* Move forward until all decimal digits skipped. */
350 for (; *cp >= '0' && *cp <= '9'; cp++)
351 ;
352
353 /* Save the old terminating character, and replace it by \0. */
354 old = *cp;
355 *cp = 0;
356
357 /* Parse the number. */
358 if (BN_dec2bn(&value, *cpp) == 0)
359 return 0;
360
361 /* Restore old terminating character. */
362 *cp = old;
363
364 /* Move beyond the number and return success. */
365 *cpp = cp;
366 return 1;
367}
Ben Lindstrom836f0e92002-06-23 21:21:30 +0000368
Ben Lindstrombba81212001-06-25 05:01:22 +0000369static int
Damien Miller450a7a12000-03-26 13:04:51 +1000370write_bignum(FILE *f, BIGNUM *num)
371{
372 char *buf = BN_bn2dec(num);
373 if (buf == NULL) {
374 error("write_bignum: BN_bn2dec() failed");
375 return 0;
376 }
377 fprintf(f, " %s", buf);
Damien Milleraf3030f2001-10-10 15:00:49 +1000378 OPENSSL_free(buf);
Damien Miller450a7a12000-03-26 13:04:51 +1000379 return 1;
380}
Damien Miller0bc1bd82000-11-13 22:57:25 +1100381
Ben Lindstrom309f3d12001-09-20 00:55:53 +0000382/* returns 1 ok, -1 error */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100383int
Damien Millereba71ba2000-04-29 23:57:08 +1000384key_read(Key *ret, char **cpp)
Damien Miller450a7a12000-03-26 13:04:51 +1000385{
Damien Millereba71ba2000-04-29 23:57:08 +1000386 Key *k;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100387 int success = -1;
388 char *cp, *space;
389 int len, n, type;
390 u_int bits;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000391 u_char *blob;
Damien Millereba71ba2000-04-29 23:57:08 +1000392
393 cp = *cpp;
394
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000395 switch (ret->type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100396 case KEY_RSA1:
Damien Millereba71ba2000-04-29 23:57:08 +1000397 /* Get number of bits. */
398 if (*cp < '0' || *cp > '9')
Damien Miller0bc1bd82000-11-13 22:57:25 +1100399 return -1; /* Bad bit count... */
Damien Millereba71ba2000-04-29 23:57:08 +1000400 for (bits = 0; *cp >= '0' && *cp <= '9'; cp++)
401 bits = 10 * bits + *cp - '0';
Damien Miller450a7a12000-03-26 13:04:51 +1000402 if (bits == 0)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100403 return -1;
Damien Millereba71ba2000-04-29 23:57:08 +1000404 *cpp = cp;
Damien Miller450a7a12000-03-26 13:04:51 +1000405 /* Get public exponent, public modulus. */
406 if (!read_bignum(cpp, ret->rsa->e))
Damien Miller0bc1bd82000-11-13 22:57:25 +1100407 return -1;
Damien Miller450a7a12000-03-26 13:04:51 +1000408 if (!read_bignum(cpp, ret->rsa->n))
Damien Miller0bc1bd82000-11-13 22:57:25 +1100409 return -1;
410 success = 1;
Damien Miller450a7a12000-03-26 13:04:51 +1000411 break;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100412 case KEY_UNSPEC:
413 case KEY_RSA:
Damien Miller450a7a12000-03-26 13:04:51 +1000414 case KEY_DSA:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100415 space = strchr(cp, ' ');
416 if (space == NULL) {
Damien Miller386f1f32003-02-24 11:54:57 +1100417 debug3("key_read: missing whitespace");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100418 return -1;
419 }
420 *space = '\0';
421 type = key_type_from_name(cp);
422 *space = ' ';
423 if (type == KEY_UNSPEC) {
Damien Miller386f1f32003-02-24 11:54:57 +1100424 debug3("key_read: missing keytype");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100425 return -1;
426 }
427 cp = space+1;
428 if (*cp == '\0') {
429 debug3("key_read: short string");
430 return -1;
431 }
432 if (ret->type == KEY_UNSPEC) {
433 ret->type = type;
434 } else if (ret->type != type) {
435 /* is a key, but different type */
436 debug3("key_read: type mismatch");
Ben Lindstrom309f3d12001-09-20 00:55:53 +0000437 return -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100438 }
Damien Millereba71ba2000-04-29 23:57:08 +1000439 len = 2*strlen(cp);
440 blob = xmalloc(len);
441 n = uudecode(cp, blob, len);
Damien Millere247cc42000-05-07 12:03:14 +1000442 if (n < 0) {
Damien Millerb1715dc2000-05-30 13:44:51 +1000443 error("key_read: uudecode %s failed", cp);
Ben Lindstrom4cbc1812001-12-06 16:41:41 +0000444 xfree(blob);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100445 return -1;
Damien Millere247cc42000-05-07 12:03:14 +1000446 }
Darren Tucker502d3842003-06-28 12:38:01 +1000447 k = key_from_blob(blob, (u_int)n);
Ben Lindstrom4cbc1812001-12-06 16:41:41 +0000448 xfree(blob);
Damien Millerb1715dc2000-05-30 13:44:51 +1000449 if (k == NULL) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100450 error("key_read: key_from_blob %s failed", cp);
451 return -1;
Damien Millerb1715dc2000-05-30 13:44:51 +1000452 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100453 if (k->type != type) {
454 error("key_read: type mismatch: encoding error");
455 key_free(k);
456 return -1;
457 }
458/*XXXX*/
459 if (ret->type == KEY_RSA) {
460 if (ret->rsa != NULL)
461 RSA_free(ret->rsa);
462 ret->rsa = k->rsa;
463 k->rsa = NULL;
464 success = 1;
465#ifdef DEBUG_PK
466 RSA_print_fp(stderr, ret->rsa, 8);
467#endif
468 } else {
469 if (ret->dsa != NULL)
470 DSA_free(ret->dsa);
471 ret->dsa = k->dsa;
472 k->dsa = NULL;
473 success = 1;
474#ifdef DEBUG_PK
475 DSA_print_fp(stderr, ret->dsa, 8);
476#endif
477 }
478/*XXXX*/
Ben Lindstrom4cbc1812001-12-06 16:41:41 +0000479 key_free(k);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100480 if (success != 1)
481 break;
Damien Millerb1715dc2000-05-30 13:44:51 +1000482 /* advance cp: skip whitespace and data */
483 while (*cp == ' ' || *cp == '\t')
484 cp++;
485 while (*cp != '\0' && *cp != ' ' && *cp != '\t')
486 cp++;
487 *cpp = cp;
Damien Miller450a7a12000-03-26 13:04:51 +1000488 break;
489 default:
Damien Millereba71ba2000-04-29 23:57:08 +1000490 fatal("key_read: bad key type: %d", ret->type);
Damien Miller450a7a12000-03-26 13:04:51 +1000491 break;
492 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100493 return success;
Damien Miller450a7a12000-03-26 13:04:51 +1000494}
Ben Lindstrom836f0e92002-06-23 21:21:30 +0000495
Damien Miller450a7a12000-03-26 13:04:51 +1000496int
Damien Millerf58b58c2003-11-17 21:18:23 +1100497key_write(const Key *key, FILE *f)
Damien Miller450a7a12000-03-26 13:04:51 +1000498{
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000499 int n, success = 0;
500 u_int len, bits = 0;
Damien Millera10f5612002-09-12 09:49:15 +1000501 u_char *blob;
502 char *uu;
Damien Miller450a7a12000-03-26 13:04:51 +1000503
Damien Miller0bc1bd82000-11-13 22:57:25 +1100504 if (key->type == KEY_RSA1 && key->rsa != NULL) {
Damien Miller450a7a12000-03-26 13:04:51 +1000505 /* size of modulus 'n' */
506 bits = BN_num_bits(key->rsa->n);
507 fprintf(f, "%u", bits);
508 if (write_bignum(f, key->rsa->e) &&
509 write_bignum(f, key->rsa->n)) {
510 success = 1;
511 } else {
512 error("key_write: failed for RSA key");
513 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100514 } else if ((key->type == KEY_DSA && key->dsa != NULL) ||
515 (key->type == KEY_RSA && key->rsa != NULL)) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100516 key_to_blob(key, &blob, &len);
Damien Millereba71ba2000-04-29 23:57:08 +1000517 uu = xmalloc(2*len);
Damien Millere247cc42000-05-07 12:03:14 +1000518 n = uuencode(blob, len, uu, 2*len);
519 if (n > 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100520 fprintf(f, "%s %s", key_ssh_name(key), uu);
Damien Millere247cc42000-05-07 12:03:14 +1000521 success = 1;
522 }
Damien Millereba71ba2000-04-29 23:57:08 +1000523 xfree(blob);
524 xfree(uu);
Damien Miller450a7a12000-03-26 13:04:51 +1000525 }
526 return success;
527}
Ben Lindstrom836f0e92002-06-23 21:21:30 +0000528
Damien Millerf58b58c2003-11-17 21:18:23 +1100529const char *
530key_type(const Key *k)
Damien Millere247cc42000-05-07 12:03:14 +1000531{
532 switch (k->type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100533 case KEY_RSA1:
534 return "RSA1";
Damien Millere247cc42000-05-07 12:03:14 +1000535 case KEY_RSA:
536 return "RSA";
Damien Millere247cc42000-05-07 12:03:14 +1000537 case KEY_DSA:
538 return "DSA";
Damien Millere247cc42000-05-07 12:03:14 +1000539 }
540 return "unknown";
541}
Ben Lindstrom836f0e92002-06-23 21:21:30 +0000542
Damien Millerf58b58c2003-11-17 21:18:23 +1100543const char *
544key_ssh_name(const Key *k)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100545{
546 switch (k->type) {
547 case KEY_RSA:
548 return "ssh-rsa";
Damien Miller0bc1bd82000-11-13 22:57:25 +1100549 case KEY_DSA:
550 return "ssh-dss";
Damien Miller0bc1bd82000-11-13 22:57:25 +1100551 }
552 return "ssh-unknown";
553}
Ben Lindstrom836f0e92002-06-23 21:21:30 +0000554
Damien Miller0bc1bd82000-11-13 22:57:25 +1100555u_int
Damien Millerf58b58c2003-11-17 21:18:23 +1100556key_size(const Key *k)
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000557{
Damien Millerad833b32000-08-23 10:46:23 +1000558 switch (k->type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100559 case KEY_RSA1:
Damien Millerad833b32000-08-23 10:46:23 +1000560 case KEY_RSA:
561 return BN_num_bits(k->rsa->n);
Damien Millerad833b32000-08-23 10:46:23 +1000562 case KEY_DSA:
563 return BN_num_bits(k->dsa->p);
Damien Millerad833b32000-08-23 10:46:23 +1000564 }
565 return 0;
566}
Damien Miller0bc1bd82000-11-13 22:57:25 +1100567
Ben Lindstrombba81212001-06-25 05:01:22 +0000568static RSA *
Ben Lindstrom46c16222000-12-22 01:43:59 +0000569rsa_generate_private_key(u_int bits)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100570{
Kevin Stevesef4eea92001-02-05 12:42:17 +0000571 RSA *private;
Damien Miller69b72032006-03-26 14:02:35 +1100572
Kevin Stevesef4eea92001-02-05 12:42:17 +0000573 private = RSA_generate_key(bits, 35, NULL, NULL);
574 if (private == NULL)
575 fatal("rsa_generate_private_key: key generation failed.");
576 return private;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100577}
578
Ben Lindstrombba81212001-06-25 05:01:22 +0000579static DSA*
Ben Lindstrom46c16222000-12-22 01:43:59 +0000580dsa_generate_private_key(u_int bits)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100581{
582 DSA *private = DSA_generate_parameters(bits, NULL, 0, NULL, NULL, NULL, NULL);
Damien Miller69b72032006-03-26 14:02:35 +1100583
Damien Miller0bc1bd82000-11-13 22:57:25 +1100584 if (private == NULL)
585 fatal("dsa_generate_private_key: DSA_generate_parameters failed");
586 if (!DSA_generate_key(private))
Kevin Stevesef4eea92001-02-05 12:42:17 +0000587 fatal("dsa_generate_private_key: DSA_generate_key failed.");
588 if (private == NULL)
589 fatal("dsa_generate_private_key: NULL.");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100590 return private;
591}
592
593Key *
Ben Lindstrom46c16222000-12-22 01:43:59 +0000594key_generate(int type, u_int bits)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100595{
596 Key *k = key_new(KEY_UNSPEC);
597 switch (type) {
Kevin Stevesef4eea92001-02-05 12:42:17 +0000598 case KEY_DSA:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100599 k->dsa = dsa_generate_private_key(bits);
600 break;
601 case KEY_RSA:
602 case KEY_RSA1:
603 k->rsa = rsa_generate_private_key(bits);
604 break;
605 default:
Kevin Stevesef4eea92001-02-05 12:42:17 +0000606 fatal("key_generate: unknown type %d", type);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100607 }
Kevin Stevesef4eea92001-02-05 12:42:17 +0000608 k->type = type;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100609 return k;
610}
611
612Key *
Damien Millerf58b58c2003-11-17 21:18:23 +1100613key_from_private(const Key *k)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100614{
615 Key *n = NULL;
616 switch (k->type) {
Kevin Stevesef4eea92001-02-05 12:42:17 +0000617 case KEY_DSA:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100618 n = key_new(k->type);
619 BN_copy(n->dsa->p, k->dsa->p);
620 BN_copy(n->dsa->q, k->dsa->q);
621 BN_copy(n->dsa->g, k->dsa->g);
622 BN_copy(n->dsa->pub_key, k->dsa->pub_key);
623 break;
624 case KEY_RSA:
625 case KEY_RSA1:
626 n = key_new(k->type);
627 BN_copy(n->rsa->n, k->rsa->n);
628 BN_copy(n->rsa->e, k->rsa->e);
629 break;
630 default:
Kevin Stevesef4eea92001-02-05 12:42:17 +0000631 fatal("key_from_private: unknown type %d", k->type);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100632 break;
633 }
634 return n;
635}
636
637int
638key_type_from_name(char *name)
639{
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000640 if (strcmp(name, "rsa1") == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100641 return KEY_RSA1;
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000642 } else if (strcmp(name, "rsa") == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100643 return KEY_RSA;
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000644 } else if (strcmp(name, "dsa") == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100645 return KEY_DSA;
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000646 } else if (strcmp(name, "ssh-rsa") == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100647 return KEY_RSA;
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000648 } else if (strcmp(name, "ssh-dss") == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100649 return KEY_DSA;
650 }
Ben Lindstromb54873a2001-03-11 20:01:55 +0000651 debug2("key_type_from_name: unknown key type '%s'", name);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100652 return KEY_UNSPEC;
653}
654
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000655int
656key_names_valid2(const char *names)
657{
658 char *s, *cp, *p;
659
660 if (names == NULL || strcmp(names, "") == 0)
661 return 0;
662 s = cp = xstrdup(names);
663 for ((p = strsep(&cp, ",")); p && *p != '\0';
Damien Miller9f0f5c62001-12-21 14:45:46 +1100664 (p = strsep(&cp, ","))) {
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000665 switch (key_type_from_name(p)) {
666 case KEY_RSA1:
667 case KEY_UNSPEC:
668 xfree(s);
669 return 0;
670 }
671 }
672 debug3("key names ok: [%s]", names);
673 xfree(s);
674 return 1;
675}
676
Damien Miller0bc1bd82000-11-13 22:57:25 +1100677Key *
Damien Millerf58b58c2003-11-17 21:18:23 +1100678key_from_blob(const u_char *blob, u_int blen)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100679{
680 Buffer b;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100681 int rlen, type;
Darren Tucker08d04fa2004-11-05 20:42:28 +1100682 char *ktype = NULL;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100683 Key *key = NULL;
684
685#ifdef DEBUG_PK
686 dump_base64(stderr, blob, blen);
687#endif
688 buffer_init(&b);
689 buffer_append(&b, blob, blen);
Darren Tucker08d04fa2004-11-05 20:42:28 +1100690 if ((ktype = buffer_get_string_ret(&b, NULL)) == NULL) {
691 error("key_from_blob: can't read key type");
692 goto out;
693 }
694
Damien Miller0bc1bd82000-11-13 22:57:25 +1100695 type = key_type_from_name(ktype);
696
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000697 switch (type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100698 case KEY_RSA:
699 key = key_new(type);
Darren Tucker08d04fa2004-11-05 20:42:28 +1100700 if (buffer_get_bignum2_ret(&b, key->rsa->e) == -1 ||
701 buffer_get_bignum2_ret(&b, key->rsa->n) == -1) {
702 error("key_from_blob: can't read rsa key");
703 key_free(key);
704 key = NULL;
705 goto out;
706 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100707#ifdef DEBUG_PK
708 RSA_print_fp(stderr, key->rsa, 8);
709#endif
710 break;
711 case KEY_DSA:
712 key = key_new(type);
Darren Tucker08d04fa2004-11-05 20:42:28 +1100713 if (buffer_get_bignum2_ret(&b, key->dsa->p) == -1 ||
714 buffer_get_bignum2_ret(&b, key->dsa->q) == -1 ||
715 buffer_get_bignum2_ret(&b, key->dsa->g) == -1 ||
716 buffer_get_bignum2_ret(&b, key->dsa->pub_key) == -1) {
717 error("key_from_blob: can't read dsa key");
718 key_free(key);
719 key = NULL;
720 goto out;
721 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100722#ifdef DEBUG_PK
723 DSA_print_fp(stderr, key->dsa, 8);
724#endif
725 break;
726 case KEY_UNSPEC:
727 key = key_new(type);
728 break;
729 default:
730 error("key_from_blob: cannot handle type %s", ktype);
Darren Tucker08d04fa2004-11-05 20:42:28 +1100731 goto out;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100732 }
733 rlen = buffer_len(&b);
734 if (key != NULL && rlen != 0)
735 error("key_from_blob: remaining bytes in key blob %d", rlen);
Darren Tucker08d04fa2004-11-05 20:42:28 +1100736 out:
737 if (ktype != NULL)
738 xfree(ktype);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100739 buffer_free(&b);
740 return key;
741}
742
743int
Damien Millerf58b58c2003-11-17 21:18:23 +1100744key_to_blob(const Key *key, u_char **blobp, u_int *lenp)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100745{
746 Buffer b;
747 int len;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100748
749 if (key == NULL) {
750 error("key_to_blob: key == NULL");
751 return 0;
752 }
753 buffer_init(&b);
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000754 switch (key->type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100755 case KEY_DSA:
756 buffer_put_cstring(&b, key_ssh_name(key));
757 buffer_put_bignum2(&b, key->dsa->p);
758 buffer_put_bignum2(&b, key->dsa->q);
759 buffer_put_bignum2(&b, key->dsa->g);
760 buffer_put_bignum2(&b, key->dsa->pub_key);
761 break;
762 case KEY_RSA:
763 buffer_put_cstring(&b, key_ssh_name(key));
Damien Miller0bc1bd82000-11-13 22:57:25 +1100764 buffer_put_bignum2(&b, key->rsa->e);
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000765 buffer_put_bignum2(&b, key->rsa->n);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100766 break;
767 default:
Ben Lindstrom99a30f12001-09-18 05:49:14 +0000768 error("key_to_blob: unsupported key type %d", key->type);
769 buffer_free(&b);
770 return 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100771 }
772 len = buffer_len(&b);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100773 if (lenp != NULL)
774 *lenp = len;
Ben Lindstrom2bf759c2002-07-07 22:13:31 +0000775 if (blobp != NULL) {
776 *blobp = xmalloc(len);
777 memcpy(*blobp, buffer_ptr(&b), len);
778 }
779 memset(buffer_ptr(&b), 0, len);
780 buffer_free(&b);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100781 return len;
782}
783
784int
785key_sign(
Damien Millerf58b58c2003-11-17 21:18:23 +1100786 const Key *key,
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000787 u_char **sigp, u_int *lenp,
Damien Millerf58b58c2003-11-17 21:18:23 +1100788 const u_char *data, u_int datalen)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100789{
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000790 switch (key->type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100791 case KEY_DSA:
792 return ssh_dss_sign(key, sigp, lenp, data, datalen);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100793 case KEY_RSA:
794 return ssh_rsa_sign(key, sigp, lenp, data, datalen);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100795 default:
Darren Tucker5cb30ad2004-08-12 22:40:24 +1000796 error("key_sign: invalid key type %d", key->type);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100797 return -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100798 }
799}
800
Ben Lindstrom01fff0c2002-06-06 20:54:07 +0000801/*
802 * key_verify returns 1 for a correct signature, 0 for an incorrect signature
803 * and -1 on error.
804 */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100805int
806key_verify(
Damien Millerf58b58c2003-11-17 21:18:23 +1100807 const Key *key,
808 const u_char *signature, u_int signaturelen,
809 const u_char *data, u_int datalen)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100810{
Ben Lindstrom5363aee2001-06-25 04:42:20 +0000811 if (signaturelen == 0)
812 return -1;
813
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000814 switch (key->type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100815 case KEY_DSA:
816 return ssh_dss_verify(key, signature, signaturelen, data, datalen);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100817 case KEY_RSA:
818 return ssh_rsa_verify(key, signature, signaturelen, data, datalen);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100819 default:
Darren Tucker5cb30ad2004-08-12 22:40:24 +1000820 error("key_verify: invalid key type %d", key->type);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100821 return -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100822 }
823}
Ben Lindstroma674e8d2002-03-22 01:45:53 +0000824
825/* Converts a private to a public key */
Ben Lindstroma674e8d2002-03-22 01:45:53 +0000826Key *
Damien Millerf58b58c2003-11-17 21:18:23 +1100827key_demote(const Key *k)
Ben Lindstroma674e8d2002-03-22 01:45:53 +0000828{
829 Key *pk;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000830
Damien Miller07d86be2006-03-26 14:19:21 +1100831 pk = xcalloc(1, sizeof(*pk));
Ben Lindstroma674e8d2002-03-22 01:45:53 +0000832 pk->type = k->type;
833 pk->flags = k->flags;
834 pk->dsa = NULL;
835 pk->rsa = NULL;
836
837 switch (k->type) {
838 case KEY_RSA1:
839 case KEY_RSA:
840 if ((pk->rsa = RSA_new()) == NULL)
841 fatal("key_demote: RSA_new failed");
842 if ((pk->rsa->e = BN_dup(k->rsa->e)) == NULL)
843 fatal("key_demote: BN_dup failed");
844 if ((pk->rsa->n = BN_dup(k->rsa->n)) == NULL)
845 fatal("key_demote: BN_dup failed");
846 break;
847 case KEY_DSA:
848 if ((pk->dsa = DSA_new()) == NULL)
849 fatal("key_demote: DSA_new failed");
850 if ((pk->dsa->p = BN_dup(k->dsa->p)) == NULL)
851 fatal("key_demote: BN_dup failed");
852 if ((pk->dsa->q = BN_dup(k->dsa->q)) == NULL)
853 fatal("key_demote: BN_dup failed");
854 if ((pk->dsa->g = BN_dup(k->dsa->g)) == NULL)
855 fatal("key_demote: BN_dup failed");
856 if ((pk->dsa->pub_key = BN_dup(k->dsa->pub_key)) == NULL)
857 fatal("key_demote: BN_dup failed");
858 break;
859 default:
860 fatal("key_free: bad key type %d", k->type);
861 break;
862 }
863
864 return (pk);
865}