blob: 8fef9b40f659d3607fdf9d821fcde61c757a8e7f [file] [log] [blame]
Damien Miller93204022007-08-08 14:28:26 +10001/* $OpenBSD: key.c,v 1.69 2007/07/12 05:48:05 ray 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 Millerded319c2006-09-01 15:38:36 +100042#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100043#include <stdio.h>
Damien Millere3476ed2006-07-24 14:13:33 +100044#include <string.h>
45
Damien Miller450a7a12000-03-26 13:04:51 +100046#include "xmalloc.h"
47#include "key.h"
Damien Miller0bc1bd82000-11-13 22:57:25 +110048#include "rsa.h"
Damien Millereba71ba2000-04-29 23:57:08 +100049#include "uuencode.h"
Damien Miller0bc1bd82000-11-13 22:57:25 +110050#include "buffer.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000051#include "log.h"
Damien Miller450a7a12000-03-26 13:04:51 +100052
53Key *
54key_new(int type)
55{
56 Key *k;
57 RSA *rsa;
58 DSA *dsa;
Damien Miller07d86be2006-03-26 14:19:21 +110059 k = xcalloc(1, sizeof(*k));
Damien Miller450a7a12000-03-26 13:04:51 +100060 k->type = type;
Damien Millereba71ba2000-04-29 23:57:08 +100061 k->dsa = NULL;
62 k->rsa = NULL;
Damien Miller450a7a12000-03-26 13:04:51 +100063 switch (k->type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +110064 case KEY_RSA1:
Damien Miller450a7a12000-03-26 13:04:51 +100065 case KEY_RSA:
Damien Millerda755162002-01-22 23:09:22 +110066 if ((rsa = RSA_new()) == NULL)
67 fatal("key_new: RSA_new failed");
68 if ((rsa->n = BN_new()) == NULL)
69 fatal("key_new: BN_new failed");
70 if ((rsa->e = BN_new()) == NULL)
71 fatal("key_new: BN_new failed");
Damien Miller450a7a12000-03-26 13:04:51 +100072 k->rsa = rsa;
73 break;
74 case KEY_DSA:
Damien Millerda755162002-01-22 23:09:22 +110075 if ((dsa = DSA_new()) == NULL)
76 fatal("key_new: DSA_new failed");
77 if ((dsa->p = BN_new()) == NULL)
78 fatal("key_new: BN_new failed");
79 if ((dsa->q = BN_new()) == NULL)
80 fatal("key_new: BN_new failed");
81 if ((dsa->g = BN_new()) == NULL)
82 fatal("key_new: BN_new failed");
83 if ((dsa->pub_key = BN_new()) == NULL)
84 fatal("key_new: BN_new failed");
Damien Miller450a7a12000-03-26 13:04:51 +100085 k->dsa = dsa;
86 break;
Damien Miller0bc1bd82000-11-13 22:57:25 +110087 case KEY_UNSPEC:
Damien Miller450a7a12000-03-26 13:04:51 +100088 break;
89 default:
90 fatal("key_new: bad key type %d", k->type);
91 break;
92 }
93 return k;
94}
Ben Lindstrom836f0e92002-06-23 21:21:30 +000095
Damien Miller0bc1bd82000-11-13 22:57:25 +110096Key *
97key_new_private(int type)
98{
99 Key *k = key_new(type);
100 switch (k->type) {
101 case KEY_RSA1:
102 case KEY_RSA:
Damien Millerda755162002-01-22 23:09:22 +1100103 if ((k->rsa->d = BN_new()) == NULL)
104 fatal("key_new_private: BN_new failed");
105 if ((k->rsa->iqmp = BN_new()) == NULL)
106 fatal("key_new_private: BN_new failed");
107 if ((k->rsa->q = BN_new()) == NULL)
108 fatal("key_new_private: BN_new failed");
109 if ((k->rsa->p = BN_new()) == NULL)
110 fatal("key_new_private: BN_new failed");
111 if ((k->rsa->dmq1 = BN_new()) == NULL)
112 fatal("key_new_private: BN_new failed");
113 if ((k->rsa->dmp1 = BN_new()) == NULL)
114 fatal("key_new_private: BN_new failed");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100115 break;
116 case KEY_DSA:
Damien Millerda755162002-01-22 23:09:22 +1100117 if ((k->dsa->priv_key = BN_new()) == NULL)
118 fatal("key_new_private: BN_new failed");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100119 break;
120 case KEY_UNSPEC:
121 break;
122 default:
123 break;
124 }
125 return k;
126}
Ben Lindstrom836f0e92002-06-23 21:21:30 +0000127
Damien Miller450a7a12000-03-26 13:04:51 +1000128void
129key_free(Key *k)
130{
Damien Miller429fcc22006-03-26 14:02:16 +1100131 if (k == NULL)
Damien Millerbbaad772006-03-26 14:03:03 +1100132 fatal("key_free: key is NULL");
Damien Miller450a7a12000-03-26 13:04:51 +1000133 switch (k->type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100134 case KEY_RSA1:
Damien Miller450a7a12000-03-26 13:04:51 +1000135 case KEY_RSA:
136 if (k->rsa != NULL)
137 RSA_free(k->rsa);
138 k->rsa = NULL;
139 break;
140 case KEY_DSA:
141 if (k->dsa != NULL)
142 DSA_free(k->dsa);
143 k->dsa = NULL;
144 break;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100145 case KEY_UNSPEC:
146 break;
Damien Miller450a7a12000-03-26 13:04:51 +1000147 default:
148 fatal("key_free: bad key type %d", k->type);
149 break;
150 }
151 xfree(k);
152}
Damien Millerf58b58c2003-11-17 21:18:23 +1100153
Damien Miller450a7a12000-03-26 13:04:51 +1000154int
Damien Millerf58b58c2003-11-17 21:18:23 +1100155key_equal(const Key *a, const Key *b)
Damien Miller450a7a12000-03-26 13:04:51 +1000156{
157 if (a == NULL || b == NULL || a->type != b->type)
158 return 0;
159 switch (a->type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100160 case KEY_RSA1:
Damien Miller450a7a12000-03-26 13:04:51 +1000161 case KEY_RSA:
162 return a->rsa != NULL && b->rsa != NULL &&
163 BN_cmp(a->rsa->e, b->rsa->e) == 0 &&
164 BN_cmp(a->rsa->n, b->rsa->n) == 0;
Damien Miller450a7a12000-03-26 13:04:51 +1000165 case KEY_DSA:
166 return a->dsa != NULL && b->dsa != NULL &&
167 BN_cmp(a->dsa->p, b->dsa->p) == 0 &&
168 BN_cmp(a->dsa->q, b->dsa->q) == 0 &&
169 BN_cmp(a->dsa->g, b->dsa->g) == 0 &&
170 BN_cmp(a->dsa->pub_key, b->dsa->pub_key) == 0;
Damien Miller450a7a12000-03-26 13:04:51 +1000171 default:
Damien Millereba71ba2000-04-29 23:57:08 +1000172 fatal("key_equal: bad key type %d", a->type);
Damien Miller450a7a12000-03-26 13:04:51 +1000173 }
Damien Miller450a7a12000-03-26 13:04:51 +1000174}
175
Damien Miller37876e92003-05-15 10:19:46 +1000176u_char*
Damien Millerf58b58c2003-11-17 21:18:23 +1100177key_fingerprint_raw(const Key *k, enum fp_type dgst_type,
178 u_int *dgst_raw_length)
Damien Miller450a7a12000-03-26 13:04:51 +1000179{
Ben Lindstrom80cb27d2002-03-05 01:33:36 +0000180 const EVP_MD *md = NULL;
Ben Lindstromf0b48532001-03-12 02:59:31 +0000181 EVP_MD_CTX ctx;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000182 u_char *blob = NULL;
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000183 u_char *retval = NULL;
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000184 u_int len = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000185 int nlen, elen;
Damien Miller450a7a12000-03-26 13:04:51 +1000186
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000187 *dgst_raw_length = 0;
188
Ben Lindstromf0b48532001-03-12 02:59:31 +0000189 switch (dgst_type) {
190 case SSH_FP_MD5:
191 md = EVP_md5();
192 break;
193 case SSH_FP_SHA1:
194 md = EVP_sha1();
195 break;
196 default:
197 fatal("key_fingerprint_raw: bad digest type %d",
198 dgst_type);
199 }
Damien Miller450a7a12000-03-26 13:04:51 +1000200 switch (k->type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100201 case KEY_RSA1:
Damien Miller450a7a12000-03-26 13:04:51 +1000202 nlen = BN_num_bytes(k->rsa->n);
203 elen = BN_num_bytes(k->rsa->e);
204 len = nlen + elen;
Damien Millereba71ba2000-04-29 23:57:08 +1000205 blob = xmalloc(len);
206 BN_bn2bin(k->rsa->n, blob);
207 BN_bn2bin(k->rsa->e, blob + nlen);
Damien Miller450a7a12000-03-26 13:04:51 +1000208 break;
209 case KEY_DSA:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100210 case KEY_RSA:
211 key_to_blob(k, &blob, &len);
212 break;
213 case KEY_UNSPEC:
214 return retval;
Damien Miller450a7a12000-03-26 13:04:51 +1000215 default:
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000216 fatal("key_fingerprint_raw: bad key type %d", k->type);
Damien Miller450a7a12000-03-26 13:04:51 +1000217 break;
218 }
Damien Millereba71ba2000-04-29 23:57:08 +1000219 if (blob != NULL) {
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000220 retval = xmalloc(EVP_MAX_MD_SIZE);
Damien Miller6536c7d2000-06-22 21:32:31 +1000221 EVP_DigestInit(&ctx, md);
222 EVP_DigestUpdate(&ctx, blob, len);
Damien Miller3672e4b2002-02-05 11:54:07 +1100223 EVP_DigestFinal(&ctx, retval, dgst_raw_length);
Damien Millereba71ba2000-04-29 23:57:08 +1000224 memset(blob, 0, len);
225 xfree(blob);
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000226 } else {
227 fatal("key_fingerprint_raw: blob is null");
Damien Miller450a7a12000-03-26 13:04:51 +1000228 }
229 return retval;
230}
231
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000232static char *
233key_fingerprint_hex(u_char *dgst_raw, u_int dgst_raw_len)
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000234{
235 char *retval;
Damien Millereccb9de2005-06-17 12:59:34 +1000236 u_int i;
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000237
Damien Miller07d86be2006-03-26 14:19:21 +1100238 retval = xcalloc(1, dgst_raw_len * 3 + 1);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100239 for (i = 0; i < dgst_raw_len; i++) {
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000240 char hex[4];
241 snprintf(hex, sizeof(hex), "%02x:", dgst_raw[i]);
Darren Tucker29588612003-07-14 17:28:34 +1000242 strlcat(retval, hex, dgst_raw_len * 3 + 1);
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000243 }
Darren Tucker29588612003-07-14 17:28:34 +1000244
245 /* Remove the trailing ':' character */
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000246 retval[(dgst_raw_len * 3) - 1] = '\0';
247 return retval;
248}
249
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000250static char *
251key_fingerprint_bubblebabble(u_char *dgst_raw, u_int dgst_raw_len)
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000252{
253 char vowels[] = { 'a', 'e', 'i', 'o', 'u', 'y' };
254 char consonants[] = { 'b', 'c', 'd', 'f', 'g', 'h', 'k', 'l', 'm',
255 'n', 'p', 'r', 's', 't', 'v', 'z', 'x' };
Ben Lindstromcbe3ad22001-03-11 20:06:59 +0000256 u_int i, j = 0, rounds, seed = 1;
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000257 char *retval;
258
259 rounds = (dgst_raw_len / 2) + 1;
Damien Miller07d86be2006-03-26 14:19:21 +1100260 retval = xcalloc((rounds * 6), sizeof(char));
Ben Lindstromcbe3ad22001-03-11 20:06:59 +0000261 retval[j++] = 'x';
262 for (i = 0; i < rounds; i++) {
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000263 u_int idx0, idx1, idx2, idx3, idx4;
Ben Lindstromcbe3ad22001-03-11 20:06:59 +0000264 if ((i + 1 < rounds) || (dgst_raw_len % 2 != 0)) {
265 idx0 = (((((u_int)(dgst_raw[2 * i])) >> 6) & 3) +
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000266 seed) % 6;
Ben Lindstromcbe3ad22001-03-11 20:06:59 +0000267 idx1 = (((u_int)(dgst_raw[2 * i])) >> 2) & 15;
268 idx2 = ((((u_int)(dgst_raw[2 * i])) & 3) +
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000269 (seed / 6)) % 6;
Ben Lindstromcbe3ad22001-03-11 20:06:59 +0000270 retval[j++] = vowels[idx0];
271 retval[j++] = consonants[idx1];
272 retval[j++] = vowels[idx2];
273 if ((i + 1) < rounds) {
274 idx3 = (((u_int)(dgst_raw[(2 * i) + 1])) >> 4) & 15;
275 idx4 = (((u_int)(dgst_raw[(2 * i) + 1]))) & 15;
276 retval[j++] = consonants[idx3];
277 retval[j++] = '-';
278 retval[j++] = consonants[idx4];
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000279 seed = ((seed * 5) +
Ben Lindstromcbe3ad22001-03-11 20:06:59 +0000280 ((((u_int)(dgst_raw[2 * i])) * 7) +
281 ((u_int)(dgst_raw[(2 * i) + 1])))) % 36;
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000282 }
283 } else {
284 idx0 = seed % 6;
285 idx1 = 16;
286 idx2 = seed / 6;
Ben Lindstromcbe3ad22001-03-11 20:06:59 +0000287 retval[j++] = vowels[idx0];
288 retval[j++] = consonants[idx1];
289 retval[j++] = vowels[idx2];
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000290 }
291 }
Ben Lindstromcbe3ad22001-03-11 20:06:59 +0000292 retval[j++] = 'x';
293 retval[j++] = '\0';
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000294 return retval;
295}
296
Ben Lindstroma962c2f2002-07-04 00:14:17 +0000297char *
Damien Millerf58b58c2003-11-17 21:18:23 +1100298key_fingerprint(const Key *k, enum fp_type dgst_type, enum fp_rep dgst_rep)
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000299{
Ben Lindstroma3700052001-04-05 23:26:32 +0000300 char *retval = NULL;
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000301 u_char *dgst_raw;
Damien Miller3672e4b2002-02-05 11:54:07 +1100302 u_int dgst_raw_len;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100303
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000304 dgst_raw = key_fingerprint_raw(k, dgst_type, &dgst_raw_len);
305 if (!dgst_raw)
Ben Lindstromcfccef92001-03-13 04:57:58 +0000306 fatal("key_fingerprint: null from key_fingerprint_raw()");
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000307 switch (dgst_rep) {
Ben Lindstrom96e8ea62001-03-11 20:03:44 +0000308 case SSH_FP_HEX:
309 retval = key_fingerprint_hex(dgst_raw, dgst_raw_len);
310 break;
311 case SSH_FP_BUBBLEBABBLE:
312 retval = key_fingerprint_bubblebabble(dgst_raw, dgst_raw_len);
313 break;
314 default:
315 fatal("key_fingerprint_ex: bad digest representation %d",
316 dgst_rep);
317 break;
318 }
319 memset(dgst_raw, 0, dgst_raw_len);
320 xfree(dgst_raw);
321 return retval;
322}
323
Damien Miller450a7a12000-03-26 13:04:51 +1000324/*
325 * Reads a multiple-precision integer in decimal from the buffer, and advances
326 * the pointer. The integer must already be initialized. This function is
327 * permitted to modify the buffer. This leaves *cpp to point just beyond the
328 * last processed (and maybe modified) character. Note that this may modify
329 * the buffer containing the number.
330 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000331static int
Damien Miller450a7a12000-03-26 13:04:51 +1000332read_bignum(char **cpp, BIGNUM * value)
333{
334 char *cp = *cpp;
335 int old;
336
337 /* Skip any leading whitespace. */
338 for (; *cp == ' ' || *cp == '\t'; cp++)
339 ;
340
341 /* Check that it begins with a decimal digit. */
342 if (*cp < '0' || *cp > '9')
343 return 0;
344
345 /* Save starting position. */
346 *cpp = cp;
347
348 /* Move forward until all decimal digits skipped. */
349 for (; *cp >= '0' && *cp <= '9'; cp++)
350 ;
351
352 /* Save the old terminating character, and replace it by \0. */
353 old = *cp;
354 *cp = 0;
355
356 /* Parse the number. */
357 if (BN_dec2bn(&value, *cpp) == 0)
358 return 0;
359
360 /* Restore old terminating character. */
361 *cp = old;
362
363 /* Move beyond the number and return success. */
364 *cpp = cp;
365 return 1;
366}
Ben Lindstrom836f0e92002-06-23 21:21:30 +0000367
Ben Lindstrombba81212001-06-25 05:01:22 +0000368static int
Damien Miller450a7a12000-03-26 13:04:51 +1000369write_bignum(FILE *f, BIGNUM *num)
370{
371 char *buf = BN_bn2dec(num);
372 if (buf == NULL) {
373 error("write_bignum: BN_bn2dec() failed");
374 return 0;
375 }
376 fprintf(f, " %s", buf);
Damien Milleraf3030f2001-10-10 15:00:49 +1000377 OPENSSL_free(buf);
Damien Miller450a7a12000-03-26 13:04:51 +1000378 return 1;
379}
Damien Miller0bc1bd82000-11-13 22:57:25 +1100380
Ben Lindstrom309f3d12001-09-20 00:55:53 +0000381/* returns 1 ok, -1 error */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100382int
Damien Millereba71ba2000-04-29 23:57:08 +1000383key_read(Key *ret, char **cpp)
Damien Miller450a7a12000-03-26 13:04:51 +1000384{
Damien Millereba71ba2000-04-29 23:57:08 +1000385 Key *k;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100386 int success = -1;
387 char *cp, *space;
388 int len, n, type;
389 u_int bits;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000390 u_char *blob;
Damien Millereba71ba2000-04-29 23:57:08 +1000391
392 cp = *cpp;
393
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000394 switch (ret->type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100395 case KEY_RSA1:
Damien Millereba71ba2000-04-29 23:57:08 +1000396 /* Get number of bits. */
397 if (*cp < '0' || *cp > '9')
Damien Miller0bc1bd82000-11-13 22:57:25 +1100398 return -1; /* Bad bit count... */
Damien Millereba71ba2000-04-29 23:57:08 +1000399 for (bits = 0; *cp >= '0' && *cp <= '9'; cp++)
400 bits = 10 * bits + *cp - '0';
Damien Miller450a7a12000-03-26 13:04:51 +1000401 if (bits == 0)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100402 return -1;
Damien Millereba71ba2000-04-29 23:57:08 +1000403 *cpp = cp;
Damien Miller450a7a12000-03-26 13:04:51 +1000404 /* Get public exponent, public modulus. */
405 if (!read_bignum(cpp, ret->rsa->e))
Damien Miller0bc1bd82000-11-13 22:57:25 +1100406 return -1;
Damien Miller450a7a12000-03-26 13:04:51 +1000407 if (!read_bignum(cpp, ret->rsa->n))
Damien Miller0bc1bd82000-11-13 22:57:25 +1100408 return -1;
409 success = 1;
Damien Miller450a7a12000-03-26 13:04:51 +1000410 break;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100411 case KEY_UNSPEC:
412 case KEY_RSA:
Damien Miller450a7a12000-03-26 13:04:51 +1000413 case KEY_DSA:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100414 space = strchr(cp, ' ');
415 if (space == NULL) {
Damien Miller386f1f32003-02-24 11:54:57 +1100416 debug3("key_read: missing whitespace");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100417 return -1;
418 }
419 *space = '\0';
420 type = key_type_from_name(cp);
421 *space = ' ';
422 if (type == KEY_UNSPEC) {
Damien Miller386f1f32003-02-24 11:54:57 +1100423 debug3("key_read: missing keytype");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100424 return -1;
425 }
426 cp = space+1;
427 if (*cp == '\0') {
428 debug3("key_read: short string");
429 return -1;
430 }
431 if (ret->type == KEY_UNSPEC) {
432 ret->type = type;
433 } else if (ret->type != type) {
434 /* is a key, but different type */
435 debug3("key_read: type mismatch");
Ben Lindstrom309f3d12001-09-20 00:55:53 +0000436 return -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100437 }
Damien Millereba71ba2000-04-29 23:57:08 +1000438 len = 2*strlen(cp);
439 blob = xmalloc(len);
440 n = uudecode(cp, blob, len);
Damien Millere247cc42000-05-07 12:03:14 +1000441 if (n < 0) {
Damien Millerb1715dc2000-05-30 13:44:51 +1000442 error("key_read: uudecode %s failed", cp);
Ben Lindstrom4cbc1812001-12-06 16:41:41 +0000443 xfree(blob);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100444 return -1;
Damien Millere247cc42000-05-07 12:03:14 +1000445 }
Darren Tucker502d3842003-06-28 12:38:01 +1000446 k = key_from_blob(blob, (u_int)n);
Ben Lindstrom4cbc1812001-12-06 16:41:41 +0000447 xfree(blob);
Damien Millerb1715dc2000-05-30 13:44:51 +1000448 if (k == NULL) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100449 error("key_read: key_from_blob %s failed", cp);
450 return -1;
Damien Millerb1715dc2000-05-30 13:44:51 +1000451 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100452 if (k->type != type) {
453 error("key_read: type mismatch: encoding error");
454 key_free(k);
455 return -1;
456 }
457/*XXXX*/
458 if (ret->type == KEY_RSA) {
459 if (ret->rsa != NULL)
460 RSA_free(ret->rsa);
461 ret->rsa = k->rsa;
462 k->rsa = NULL;
463 success = 1;
464#ifdef DEBUG_PK
465 RSA_print_fp(stderr, ret->rsa, 8);
466#endif
467 } else {
468 if (ret->dsa != NULL)
469 DSA_free(ret->dsa);
470 ret->dsa = k->dsa;
471 k->dsa = NULL;
472 success = 1;
473#ifdef DEBUG_PK
474 DSA_print_fp(stderr, ret->dsa, 8);
475#endif
476 }
477/*XXXX*/
Ben Lindstrom4cbc1812001-12-06 16:41:41 +0000478 key_free(k);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100479 if (success != 1)
480 break;
Damien Millerb1715dc2000-05-30 13:44:51 +1000481 /* advance cp: skip whitespace and data */
482 while (*cp == ' ' || *cp == '\t')
483 cp++;
484 while (*cp != '\0' && *cp != ' ' && *cp != '\t')
485 cp++;
486 *cpp = cp;
Damien Miller450a7a12000-03-26 13:04:51 +1000487 break;
488 default:
Damien Millereba71ba2000-04-29 23:57:08 +1000489 fatal("key_read: bad key type: %d", ret->type);
Damien Miller450a7a12000-03-26 13:04:51 +1000490 break;
491 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100492 return success;
Damien Miller450a7a12000-03-26 13:04:51 +1000493}
Ben Lindstrom836f0e92002-06-23 21:21:30 +0000494
Damien Miller450a7a12000-03-26 13:04:51 +1000495int
Damien Millerf58b58c2003-11-17 21:18:23 +1100496key_write(const Key *key, FILE *f)
Damien Miller450a7a12000-03-26 13:04:51 +1000497{
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000498 int n, success = 0;
499 u_int len, bits = 0;
Damien Millera10f5612002-09-12 09:49:15 +1000500 u_char *blob;
501 char *uu;
Damien Miller450a7a12000-03-26 13:04:51 +1000502
Damien Miller0bc1bd82000-11-13 22:57:25 +1100503 if (key->type == KEY_RSA1 && key->rsa != NULL) {
Damien Miller450a7a12000-03-26 13:04:51 +1000504 /* size of modulus 'n' */
505 bits = BN_num_bits(key->rsa->n);
506 fprintf(f, "%u", bits);
507 if (write_bignum(f, key->rsa->e) &&
508 write_bignum(f, key->rsa->n)) {
509 success = 1;
510 } else {
511 error("key_write: failed for RSA key");
512 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100513 } else if ((key->type == KEY_DSA && key->dsa != NULL) ||
514 (key->type == KEY_RSA && key->rsa != NULL)) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100515 key_to_blob(key, &blob, &len);
Damien Millereba71ba2000-04-29 23:57:08 +1000516 uu = xmalloc(2*len);
Damien Millere247cc42000-05-07 12:03:14 +1000517 n = uuencode(blob, len, uu, 2*len);
518 if (n > 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100519 fprintf(f, "%s %s", key_ssh_name(key), uu);
Damien Millere247cc42000-05-07 12:03:14 +1000520 success = 1;
521 }
Damien Millereba71ba2000-04-29 23:57:08 +1000522 xfree(blob);
523 xfree(uu);
Damien Miller450a7a12000-03-26 13:04:51 +1000524 }
525 return success;
526}
Ben Lindstrom836f0e92002-06-23 21:21:30 +0000527
Damien Millerf58b58c2003-11-17 21:18:23 +1100528const char *
529key_type(const Key *k)
Damien Millere247cc42000-05-07 12:03:14 +1000530{
531 switch (k->type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100532 case KEY_RSA1:
533 return "RSA1";
Damien Millere247cc42000-05-07 12:03:14 +1000534 case KEY_RSA:
535 return "RSA";
Damien Millere247cc42000-05-07 12:03:14 +1000536 case KEY_DSA:
537 return "DSA";
Damien Millere247cc42000-05-07 12:03:14 +1000538 }
539 return "unknown";
540}
Ben Lindstrom836f0e92002-06-23 21:21:30 +0000541
Damien Millerf58b58c2003-11-17 21:18:23 +1100542const char *
543key_ssh_name(const Key *k)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100544{
545 switch (k->type) {
546 case KEY_RSA:
547 return "ssh-rsa";
Damien Miller0bc1bd82000-11-13 22:57:25 +1100548 case KEY_DSA:
549 return "ssh-dss";
Damien Miller0bc1bd82000-11-13 22:57:25 +1100550 }
551 return "ssh-unknown";
552}
Ben Lindstrom836f0e92002-06-23 21:21:30 +0000553
Damien Miller0bc1bd82000-11-13 22:57:25 +1100554u_int
Damien Millerf58b58c2003-11-17 21:18:23 +1100555key_size(const Key *k)
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000556{
Damien Millerad833b32000-08-23 10:46:23 +1000557 switch (k->type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100558 case KEY_RSA1:
Damien Millerad833b32000-08-23 10:46:23 +1000559 case KEY_RSA:
560 return BN_num_bits(k->rsa->n);
Damien Millerad833b32000-08-23 10:46:23 +1000561 case KEY_DSA:
562 return BN_num_bits(k->dsa->p);
Damien Millerad833b32000-08-23 10:46:23 +1000563 }
564 return 0;
565}
Damien Miller0bc1bd82000-11-13 22:57:25 +1100566
Ben Lindstrombba81212001-06-25 05:01:22 +0000567static RSA *
Ben Lindstrom46c16222000-12-22 01:43:59 +0000568rsa_generate_private_key(u_int bits)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100569{
Kevin Stevesef4eea92001-02-05 12:42:17 +0000570 RSA *private;
Damien Miller69b72032006-03-26 14:02:35 +1100571
Kevin Stevesef4eea92001-02-05 12:42:17 +0000572 private = RSA_generate_key(bits, 35, NULL, NULL);
573 if (private == NULL)
574 fatal("rsa_generate_private_key: key generation failed.");
575 return private;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100576}
577
Ben Lindstrombba81212001-06-25 05:01:22 +0000578static DSA*
Ben Lindstrom46c16222000-12-22 01:43:59 +0000579dsa_generate_private_key(u_int bits)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100580{
581 DSA *private = DSA_generate_parameters(bits, NULL, 0, NULL, NULL, NULL, NULL);
Damien Miller69b72032006-03-26 14:02:35 +1100582
Damien Miller0bc1bd82000-11-13 22:57:25 +1100583 if (private == NULL)
584 fatal("dsa_generate_private_key: DSA_generate_parameters failed");
585 if (!DSA_generate_key(private))
Kevin Stevesef4eea92001-02-05 12:42:17 +0000586 fatal("dsa_generate_private_key: DSA_generate_key failed.");
587 if (private == NULL)
588 fatal("dsa_generate_private_key: NULL.");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100589 return private;
590}
591
592Key *
Ben Lindstrom46c16222000-12-22 01:43:59 +0000593key_generate(int type, u_int bits)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100594{
595 Key *k = key_new(KEY_UNSPEC);
596 switch (type) {
Kevin Stevesef4eea92001-02-05 12:42:17 +0000597 case KEY_DSA:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100598 k->dsa = dsa_generate_private_key(bits);
599 break;
600 case KEY_RSA:
601 case KEY_RSA1:
602 k->rsa = rsa_generate_private_key(bits);
603 break;
604 default:
Kevin Stevesef4eea92001-02-05 12:42:17 +0000605 fatal("key_generate: unknown type %d", type);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100606 }
Kevin Stevesef4eea92001-02-05 12:42:17 +0000607 k->type = type;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100608 return k;
609}
610
611Key *
Damien Millerf58b58c2003-11-17 21:18:23 +1100612key_from_private(const Key *k)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100613{
614 Key *n = NULL;
615 switch (k->type) {
Kevin Stevesef4eea92001-02-05 12:42:17 +0000616 case KEY_DSA:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100617 n = key_new(k->type);
Darren Tucker0bc85572006-11-07 23:14:41 +1100618 if ((BN_copy(n->dsa->p, k->dsa->p) == NULL) ||
619 (BN_copy(n->dsa->q, k->dsa->q) == NULL) ||
620 (BN_copy(n->dsa->g, k->dsa->g) == NULL) ||
621 (BN_copy(n->dsa->pub_key, k->dsa->pub_key) == NULL))
622 fatal("key_from_private: BN_copy failed");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100623 break;
624 case KEY_RSA:
625 case KEY_RSA1:
626 n = key_new(k->type);
Darren Tucker0bc85572006-11-07 23:14:41 +1100627 if ((BN_copy(n->rsa->n, k->rsa->n) == NULL) ||
628 (BN_copy(n->rsa->e, k->rsa->e) == NULL))
629 fatal("key_from_private: BN_copy failed");
Damien Miller0bc1bd82000-11-13 22:57:25 +1100630 break;
631 default:
Kevin Stevesef4eea92001-02-05 12:42:17 +0000632 fatal("key_from_private: unknown type %d", k->type);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100633 break;
634 }
635 return n;
636}
637
638int
639key_type_from_name(char *name)
640{
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000641 if (strcmp(name, "rsa1") == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100642 return KEY_RSA1;
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000643 } else if (strcmp(name, "rsa") == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100644 return KEY_RSA;
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000645 } else if (strcmp(name, "dsa") == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100646 return KEY_DSA;
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000647 } else if (strcmp(name, "ssh-rsa") == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100648 return KEY_RSA;
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000649 } else if (strcmp(name, "ssh-dss") == 0) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100650 return KEY_DSA;
651 }
Ben Lindstromb54873a2001-03-11 20:01:55 +0000652 debug2("key_type_from_name: unknown key type '%s'", name);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100653 return KEY_UNSPEC;
654}
655
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000656int
657key_names_valid2(const char *names)
658{
659 char *s, *cp, *p;
660
661 if (names == NULL || strcmp(names, "") == 0)
662 return 0;
663 s = cp = xstrdup(names);
664 for ((p = strsep(&cp, ",")); p && *p != '\0';
Damien Miller9f0f5c62001-12-21 14:45:46 +1100665 (p = strsep(&cp, ","))) {
Ben Lindstrom982dbbc2001-04-17 18:11:36 +0000666 switch (key_type_from_name(p)) {
667 case KEY_RSA1:
668 case KEY_UNSPEC:
669 xfree(s);
670 return 0;
671 }
672 }
673 debug3("key names ok: [%s]", names);
674 xfree(s);
675 return 1;
676}
677
Damien Miller0bc1bd82000-11-13 22:57:25 +1100678Key *
Damien Millerf58b58c2003-11-17 21:18:23 +1100679key_from_blob(const u_char *blob, u_int blen)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100680{
681 Buffer b;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100682 int rlen, type;
Darren Tucker08d04fa2004-11-05 20:42:28 +1100683 char *ktype = NULL;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100684 Key *key = NULL;
685
686#ifdef DEBUG_PK
687 dump_base64(stderr, blob, blen);
688#endif
689 buffer_init(&b);
690 buffer_append(&b, blob, blen);
Darren Tucker08d04fa2004-11-05 20:42:28 +1100691 if ((ktype = buffer_get_string_ret(&b, NULL)) == NULL) {
692 error("key_from_blob: can't read key type");
693 goto out;
694 }
695
Damien Miller0bc1bd82000-11-13 22:57:25 +1100696 type = key_type_from_name(ktype);
697
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000698 switch (type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100699 case KEY_RSA:
700 key = key_new(type);
Darren Tucker08d04fa2004-11-05 20:42:28 +1100701 if (buffer_get_bignum2_ret(&b, key->rsa->e) == -1 ||
702 buffer_get_bignum2_ret(&b, key->rsa->n) == -1) {
703 error("key_from_blob: can't read rsa key");
704 key_free(key);
705 key = NULL;
706 goto out;
707 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100708#ifdef DEBUG_PK
709 RSA_print_fp(stderr, key->rsa, 8);
710#endif
711 break;
712 case KEY_DSA:
713 key = key_new(type);
Darren Tucker08d04fa2004-11-05 20:42:28 +1100714 if (buffer_get_bignum2_ret(&b, key->dsa->p) == -1 ||
715 buffer_get_bignum2_ret(&b, key->dsa->q) == -1 ||
716 buffer_get_bignum2_ret(&b, key->dsa->g) == -1 ||
717 buffer_get_bignum2_ret(&b, key->dsa->pub_key) == -1) {
718 error("key_from_blob: can't read dsa key");
719 key_free(key);
720 key = NULL;
721 goto out;
722 }
Damien Miller0bc1bd82000-11-13 22:57:25 +1100723#ifdef DEBUG_PK
724 DSA_print_fp(stderr, key->dsa, 8);
725#endif
726 break;
727 case KEY_UNSPEC:
728 key = key_new(type);
729 break;
730 default:
731 error("key_from_blob: cannot handle type %s", ktype);
Darren Tucker08d04fa2004-11-05 20:42:28 +1100732 goto out;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100733 }
734 rlen = buffer_len(&b);
735 if (key != NULL && rlen != 0)
736 error("key_from_blob: remaining bytes in key blob %d", rlen);
Darren Tucker08d04fa2004-11-05 20:42:28 +1100737 out:
738 if (ktype != NULL)
739 xfree(ktype);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100740 buffer_free(&b);
741 return key;
742}
743
744int
Damien Millerf58b58c2003-11-17 21:18:23 +1100745key_to_blob(const Key *key, u_char **blobp, u_int *lenp)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100746{
747 Buffer b;
748 int len;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100749
750 if (key == NULL) {
751 error("key_to_blob: key == NULL");
752 return 0;
753 }
754 buffer_init(&b);
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000755 switch (key->type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100756 case KEY_DSA:
757 buffer_put_cstring(&b, key_ssh_name(key));
758 buffer_put_bignum2(&b, key->dsa->p);
759 buffer_put_bignum2(&b, key->dsa->q);
760 buffer_put_bignum2(&b, key->dsa->g);
761 buffer_put_bignum2(&b, key->dsa->pub_key);
762 break;
763 case KEY_RSA:
764 buffer_put_cstring(&b, key_ssh_name(key));
Damien Miller0bc1bd82000-11-13 22:57:25 +1100765 buffer_put_bignum2(&b, key->rsa->e);
Ben Lindstrombf555ba2001-01-18 02:04:35 +0000766 buffer_put_bignum2(&b, key->rsa->n);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100767 break;
768 default:
Ben Lindstrom99a30f12001-09-18 05:49:14 +0000769 error("key_to_blob: unsupported key type %d", key->type);
770 buffer_free(&b);
771 return 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100772 }
773 len = buffer_len(&b);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100774 if (lenp != NULL)
775 *lenp = len;
Ben Lindstrom2bf759c2002-07-07 22:13:31 +0000776 if (blobp != NULL) {
777 *blobp = xmalloc(len);
778 memcpy(*blobp, buffer_ptr(&b), len);
779 }
780 memset(buffer_ptr(&b), 0, len);
781 buffer_free(&b);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100782 return len;
783}
784
785int
786key_sign(
Damien Millerf58b58c2003-11-17 21:18:23 +1100787 const Key *key,
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000788 u_char **sigp, u_int *lenp,
Damien Millerf58b58c2003-11-17 21:18:23 +1100789 const u_char *data, u_int datalen)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100790{
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000791 switch (key->type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100792 case KEY_DSA:
793 return ssh_dss_sign(key, sigp, lenp, data, datalen);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100794 case KEY_RSA:
795 return ssh_rsa_sign(key, sigp, lenp, data, datalen);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100796 default:
Darren Tucker5cb30ad2004-08-12 22:40:24 +1000797 error("key_sign: invalid key type %d", key->type);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100798 return -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100799 }
800}
801
Ben Lindstrom01fff0c2002-06-06 20:54:07 +0000802/*
803 * key_verify returns 1 for a correct signature, 0 for an incorrect signature
804 * and -1 on error.
805 */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100806int
807key_verify(
Damien Millerf58b58c2003-11-17 21:18:23 +1100808 const Key *key,
809 const u_char *signature, u_int signaturelen,
810 const u_char *data, u_int datalen)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100811{
Ben Lindstrom5363aee2001-06-25 04:42:20 +0000812 if (signaturelen == 0)
813 return -1;
814
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000815 switch (key->type) {
Damien Miller0bc1bd82000-11-13 22:57:25 +1100816 case KEY_DSA:
817 return ssh_dss_verify(key, signature, signaturelen, data, datalen);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100818 case KEY_RSA:
819 return ssh_rsa_verify(key, signature, signaturelen, data, datalen);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100820 default:
Darren Tucker5cb30ad2004-08-12 22:40:24 +1000821 error("key_verify: invalid key type %d", key->type);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100822 return -1;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100823 }
824}
Ben Lindstroma674e8d2002-03-22 01:45:53 +0000825
826/* Converts a private to a public key */
Ben Lindstroma674e8d2002-03-22 01:45:53 +0000827Key *
Damien Millerf58b58c2003-11-17 21:18:23 +1100828key_demote(const Key *k)
Ben Lindstroma674e8d2002-03-22 01:45:53 +0000829{
830 Key *pk;
Ben Lindstrom6328ab32002-03-22 02:54:23 +0000831
Damien Miller07d86be2006-03-26 14:19:21 +1100832 pk = xcalloc(1, sizeof(*pk));
Ben Lindstroma674e8d2002-03-22 01:45:53 +0000833 pk->type = k->type;
834 pk->flags = k->flags;
835 pk->dsa = NULL;
836 pk->rsa = NULL;
837
838 switch (k->type) {
839 case KEY_RSA1:
840 case KEY_RSA:
841 if ((pk->rsa = RSA_new()) == NULL)
842 fatal("key_demote: RSA_new failed");
843 if ((pk->rsa->e = BN_dup(k->rsa->e)) == NULL)
844 fatal("key_demote: BN_dup failed");
845 if ((pk->rsa->n = BN_dup(k->rsa->n)) == NULL)
846 fatal("key_demote: BN_dup failed");
847 break;
848 case KEY_DSA:
849 if ((pk->dsa = DSA_new()) == NULL)
850 fatal("key_demote: DSA_new failed");
851 if ((pk->dsa->p = BN_dup(k->dsa->p)) == NULL)
852 fatal("key_demote: BN_dup failed");
853 if ((pk->dsa->q = BN_dup(k->dsa->q)) == NULL)
854 fatal("key_demote: BN_dup failed");
855 if ((pk->dsa->g = BN_dup(k->dsa->g)) == NULL)
856 fatal("key_demote: BN_dup failed");
857 if ((pk->dsa->pub_key = BN_dup(k->dsa->pub_key)) == NULL)
858 fatal("key_demote: BN_dup failed");
859 break;
860 default:
861 fatal("key_free: bad key type %d", k->type);
862 break;
863 }
864
865 return (pk);
866}