]>
Commit | Line | Data |
---|---|---|
d4487008 | 1 | /* $OpenBSD: key.c,v 1.69 2007/07/12 05:48:05 ray Exp $ */ |
3c0ef626 | 2 | /* |
3 | * 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 | * | |
13 | * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved. | |
14 | * | |
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. | |
23 | * | |
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 | */ | |
9108f8d9 | 35 | |
3c0ef626 | 36 | #include "includes.h" |
9108f8d9 | 37 | |
38 | #include <sys/types.h> | |
3c0ef626 | 39 | |
40 | #include <openssl/evp.h> | |
47686178 | 41 | #include <openbsd-compat/openssl-compat.h> |
3c0ef626 | 42 | |
9108f8d9 | 43 | #include <stdarg.h> |
44 | #include <stdio.h> | |
45 | #include <string.h> | |
46 | ||
3c0ef626 | 47 | #include "xmalloc.h" |
48 | #include "key.h" | |
49 | #include "rsa.h" | |
3c0ef626 | 50 | #include "uuencode.h" |
51 | #include "buffer.h" | |
3c0ef626 | 52 | #include "log.h" |
53 | ||
54 | Key * | |
55 | key_new(int type) | |
56 | { | |
57 | Key *k; | |
58 | RSA *rsa; | |
59 | DSA *dsa; | |
9108f8d9 | 60 | k = xcalloc(1, sizeof(*k)); |
3c0ef626 | 61 | k->type = type; |
3c0ef626 | 62 | k->dsa = NULL; |
63 | k->rsa = NULL; | |
64 | switch (k->type) { | |
65 | case KEY_RSA1: | |
66 | case KEY_RSA: | |
e9a17296 | 67 | if ((rsa = RSA_new()) == NULL) |
68 | fatal("key_new: RSA_new failed"); | |
69 | if ((rsa->n = BN_new()) == NULL) | |
70 | fatal("key_new: BN_new failed"); | |
71 | if ((rsa->e = BN_new()) == NULL) | |
72 | fatal("key_new: BN_new failed"); | |
3c0ef626 | 73 | k->rsa = rsa; |
74 | break; | |
75 | case KEY_DSA: | |
e9a17296 | 76 | if ((dsa = DSA_new()) == NULL) |
77 | fatal("key_new: DSA_new failed"); | |
78 | if ((dsa->p = BN_new()) == NULL) | |
79 | fatal("key_new: BN_new failed"); | |
80 | if ((dsa->q = BN_new()) == NULL) | |
81 | fatal("key_new: BN_new failed"); | |
82 | if ((dsa->g = BN_new()) == NULL) | |
83 | fatal("key_new: BN_new failed"); | |
84 | if ((dsa->pub_key = BN_new()) == NULL) | |
85 | fatal("key_new: BN_new failed"); | |
3c0ef626 | 86 | k->dsa = dsa; |
87 | break; | |
88 | case KEY_UNSPEC: | |
89 | break; | |
90 | default: | |
91 | fatal("key_new: bad key type %d", k->type); | |
92 | break; | |
93 | } | |
94 | return k; | |
95 | } | |
680cee3b | 96 | |
3c0ef626 | 97 | Key * |
98 | key_new_private(int type) | |
99 | { | |
100 | Key *k = key_new(type); | |
101 | switch (k->type) { | |
102 | case KEY_RSA1: | |
103 | case KEY_RSA: | |
e9a17296 | 104 | if ((k->rsa->d = BN_new()) == NULL) |
105 | fatal("key_new_private: BN_new failed"); | |
106 | if ((k->rsa->iqmp = BN_new()) == NULL) | |
107 | fatal("key_new_private: BN_new failed"); | |
108 | if ((k->rsa->q = BN_new()) == NULL) | |
109 | fatal("key_new_private: BN_new failed"); | |
110 | if ((k->rsa->p = BN_new()) == NULL) | |
111 | fatal("key_new_private: BN_new failed"); | |
112 | if ((k->rsa->dmq1 = BN_new()) == NULL) | |
113 | fatal("key_new_private: BN_new failed"); | |
114 | if ((k->rsa->dmp1 = BN_new()) == NULL) | |
115 | fatal("key_new_private: BN_new failed"); | |
3c0ef626 | 116 | break; |
117 | case KEY_DSA: | |
e9a17296 | 118 | if ((k->dsa->priv_key = BN_new()) == NULL) |
119 | fatal("key_new_private: BN_new failed"); | |
3c0ef626 | 120 | break; |
121 | case KEY_UNSPEC: | |
122 | break; | |
123 | default: | |
124 | break; | |
125 | } | |
126 | return k; | |
127 | } | |
680cee3b | 128 | |
3c0ef626 | 129 | void |
130 | key_free(Key *k) | |
131 | { | |
9108f8d9 | 132 | if (k == NULL) |
133 | fatal("key_free: key is NULL"); | |
3c0ef626 | 134 | switch (k->type) { |
135 | case KEY_RSA1: | |
136 | case KEY_RSA: | |
137 | if (k->rsa != NULL) | |
138 | RSA_free(k->rsa); | |
139 | k->rsa = NULL; | |
140 | break; | |
141 | case KEY_DSA: | |
142 | if (k->dsa != NULL) | |
143 | DSA_free(k->dsa); | |
144 | k->dsa = NULL; | |
145 | break; | |
146 | case KEY_UNSPEC: | |
147 | break; | |
148 | default: | |
149 | fatal("key_free: bad key type %d", k->type); | |
150 | break; | |
151 | } | |
152 | xfree(k); | |
153 | } | |
cdd66111 | 154 | |
3c0ef626 | 155 | int |
cdd66111 | 156 | key_equal(const Key *a, const Key *b) |
3c0ef626 | 157 | { |
158 | if (a == NULL || b == NULL || a->type != b->type) | |
159 | return 0; | |
160 | switch (a->type) { | |
161 | case KEY_RSA1: | |
162 | case KEY_RSA: | |
163 | return a->rsa != NULL && b->rsa != NULL && | |
164 | BN_cmp(a->rsa->e, b->rsa->e) == 0 && | |
165 | BN_cmp(a->rsa->n, b->rsa->n) == 0; | |
3c0ef626 | 166 | case KEY_DSA: |
167 | return a->dsa != NULL && b->dsa != NULL && | |
168 | BN_cmp(a->dsa->p, b->dsa->p) == 0 && | |
169 | BN_cmp(a->dsa->q, b->dsa->q) == 0 && | |
170 | BN_cmp(a->dsa->g, b->dsa->g) == 0 && | |
171 | BN_cmp(a->dsa->pub_key, b->dsa->pub_key) == 0; | |
3c0ef626 | 172 | default: |
173 | fatal("key_equal: bad key type %d", a->type); | |
3c0ef626 | 174 | } |
3c0ef626 | 175 | } |
176 | ||
0fff78ff | 177 | u_char* |
cdd66111 | 178 | key_fingerprint_raw(const Key *k, enum fp_type dgst_type, |
179 | u_int *dgst_raw_length) | |
3c0ef626 | 180 | { |
e9a17296 | 181 | const EVP_MD *md = NULL; |
3c0ef626 | 182 | EVP_MD_CTX ctx; |
183 | u_char *blob = NULL; | |
184 | u_char *retval = NULL; | |
e9a17296 | 185 | u_int len = 0; |
3c0ef626 | 186 | int nlen, elen; |
187 | ||
188 | *dgst_raw_length = 0; | |
189 | ||
190 | 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 | } | |
201 | switch (k->type) { | |
202 | case KEY_RSA1: | |
203 | nlen = BN_num_bytes(k->rsa->n); | |
204 | elen = BN_num_bytes(k->rsa->e); | |
205 | len = nlen + elen; | |
206 | blob = xmalloc(len); | |
207 | BN_bn2bin(k->rsa->n, blob); | |
208 | BN_bn2bin(k->rsa->e, blob + nlen); | |
209 | break; | |
210 | case KEY_DSA: | |
211 | case KEY_RSA: | |
212 | key_to_blob(k, &blob, &len); | |
213 | break; | |
214 | case KEY_UNSPEC: | |
215 | return retval; | |
3c0ef626 | 216 | default: |
217 | fatal("key_fingerprint_raw: bad key type %d", k->type); | |
218 | break; | |
219 | } | |
220 | if (blob != NULL) { | |
221 | retval = xmalloc(EVP_MAX_MD_SIZE); | |
222 | EVP_DigestInit(&ctx, md); | |
223 | EVP_DigestUpdate(&ctx, blob, len); | |
e9a17296 | 224 | EVP_DigestFinal(&ctx, retval, dgst_raw_length); |
3c0ef626 | 225 | memset(blob, 0, len); |
226 | xfree(blob); | |
227 | } else { | |
228 | fatal("key_fingerprint_raw: blob is null"); | |
229 | } | |
230 | return retval; | |
231 | } | |
232 | ||
41b2f314 | 233 | static char * |
234 | key_fingerprint_hex(u_char *dgst_raw, u_int dgst_raw_len) | |
3c0ef626 | 235 | { |
236 | char *retval; | |
665a873d | 237 | u_int i; |
3c0ef626 | 238 | |
9108f8d9 | 239 | retval = xcalloc(1, dgst_raw_len * 3 + 1); |
e9a17296 | 240 | for (i = 0; i < dgst_raw_len; i++) { |
3c0ef626 | 241 | char hex[4]; |
242 | snprintf(hex, sizeof(hex), "%02x:", dgst_raw[i]); | |
0fff78ff | 243 | strlcat(retval, hex, dgst_raw_len * 3 + 1); |
3c0ef626 | 244 | } |
0fff78ff | 245 | |
246 | /* Remove the trailing ':' character */ | |
3c0ef626 | 247 | retval[(dgst_raw_len * 3) - 1] = '\0'; |
248 | return retval; | |
249 | } | |
250 | ||
41b2f314 | 251 | static char * |
252 | key_fingerprint_bubblebabble(u_char *dgst_raw, u_int dgst_raw_len) | |
3c0ef626 | 253 | { |
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' }; | |
257 | u_int i, j = 0, rounds, seed = 1; | |
258 | char *retval; | |
259 | ||
260 | rounds = (dgst_raw_len / 2) + 1; | |
9108f8d9 | 261 | retval = xcalloc((rounds * 6), sizeof(char)); |
3c0ef626 | 262 | retval[j++] = 'x'; |
263 | for (i = 0; i < rounds; i++) { | |
264 | u_int idx0, idx1, idx2, idx3, idx4; | |
265 | if ((i + 1 < rounds) || (dgst_raw_len % 2 != 0)) { | |
266 | idx0 = (((((u_int)(dgst_raw[2 * i])) >> 6) & 3) + | |
267 | seed) % 6; | |
268 | idx1 = (((u_int)(dgst_raw[2 * i])) >> 2) & 15; | |
269 | idx2 = ((((u_int)(dgst_raw[2 * i])) & 3) + | |
270 | (seed / 6)) % 6; | |
271 | 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]; | |
280 | seed = ((seed * 5) + | |
281 | ((((u_int)(dgst_raw[2 * i])) * 7) + | |
282 | ((u_int)(dgst_raw[(2 * i) + 1])))) % 36; | |
283 | } | |
284 | } else { | |
285 | idx0 = seed % 6; | |
286 | idx1 = 16; | |
287 | idx2 = seed / 6; | |
288 | retval[j++] = vowels[idx0]; | |
289 | retval[j++] = consonants[idx1]; | |
290 | retval[j++] = vowels[idx2]; | |
291 | } | |
292 | } | |
293 | retval[j++] = 'x'; | |
294 | retval[j++] = '\0'; | |
295 | return retval; | |
296 | } | |
297 | ||
41b2f314 | 298 | char * |
cdd66111 | 299 | key_fingerprint(const Key *k, enum fp_type dgst_type, enum fp_rep dgst_rep) |
3c0ef626 | 300 | { |
301 | char *retval = NULL; | |
302 | u_char *dgst_raw; | |
e9a17296 | 303 | u_int dgst_raw_len; |
304 | ||
3c0ef626 | 305 | dgst_raw = key_fingerprint_raw(k, dgst_type, &dgst_raw_len); |
306 | if (!dgst_raw) | |
307 | fatal("key_fingerprint: null from key_fingerprint_raw()"); | |
e9a17296 | 308 | switch (dgst_rep) { |
3c0ef626 | 309 | 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 | ||
325 | /* | |
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 | */ | |
332 | static int | |
333 | read_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 | } | |
680cee3b | 368 | |
3c0ef626 | 369 | static int |
370 | write_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); | |
378 | OPENSSL_free(buf); | |
379 | return 1; | |
380 | } | |
381 | ||
382 | /* returns 1 ok, -1 error */ | |
383 | int | |
384 | key_read(Key *ret, char **cpp) | |
385 | { | |
386 | Key *k; | |
387 | int success = -1; | |
388 | char *cp, *space; | |
389 | int len, n, type; | |
390 | u_int bits; | |
391 | u_char *blob; | |
392 | ||
393 | cp = *cpp; | |
394 | ||
e9a17296 | 395 | switch (ret->type) { |
3c0ef626 | 396 | case KEY_RSA1: |
397 | /* Get number of bits. */ | |
398 | if (*cp < '0' || *cp > '9') | |
399 | return -1; /* Bad bit count... */ | |
400 | for (bits = 0; *cp >= '0' && *cp <= '9'; cp++) | |
401 | bits = 10 * bits + *cp - '0'; | |
402 | if (bits == 0) | |
403 | return -1; | |
404 | *cpp = cp; | |
405 | /* Get public exponent, public modulus. */ | |
406 | if (!read_bignum(cpp, ret->rsa->e)) | |
407 | return -1; | |
408 | if (!read_bignum(cpp, ret->rsa->n)) | |
409 | return -1; | |
410 | success = 1; | |
411 | break; | |
412 | case KEY_UNSPEC: | |
413 | case KEY_RSA: | |
414 | case KEY_DSA: | |
415 | space = strchr(cp, ' '); | |
416 | if (space == NULL) { | |
6a9b3198 | 417 | debug3("key_read: missing whitespace"); |
3c0ef626 | 418 | return -1; |
419 | } | |
420 | *space = '\0'; | |
421 | type = key_type_from_name(cp); | |
422 | *space = ' '; | |
423 | if (type == KEY_UNSPEC) { | |
6a9b3198 | 424 | debug3("key_read: missing keytype"); |
3c0ef626 | 425 | 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"); | |
437 | return -1; | |
438 | } | |
439 | len = 2*strlen(cp); | |
440 | blob = xmalloc(len); | |
441 | n = uudecode(cp, blob, len); | |
442 | if (n < 0) { | |
443 | error("key_read: uudecode %s failed", cp); | |
e9a17296 | 444 | xfree(blob); |
3c0ef626 | 445 | return -1; |
446 | } | |
0fff78ff | 447 | k = key_from_blob(blob, (u_int)n); |
e9a17296 | 448 | xfree(blob); |
3c0ef626 | 449 | if (k == NULL) { |
450 | error("key_read: key_from_blob %s failed", cp); | |
451 | return -1; | |
452 | } | |
3c0ef626 | 453 | 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*/ | |
e9a17296 | 479 | key_free(k); |
3c0ef626 | 480 | if (success != 1) |
481 | break; | |
3c0ef626 | 482 | /* 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; | |
488 | break; | |
489 | default: | |
490 | fatal("key_read: bad key type: %d", ret->type); | |
491 | break; | |
492 | } | |
493 | return success; | |
494 | } | |
680cee3b | 495 | |
3c0ef626 | 496 | int |
cdd66111 | 497 | key_write(const Key *key, FILE *f) |
3c0ef626 | 498 | { |
e9a17296 | 499 | int n, success = 0; |
500 | u_int len, bits = 0; | |
41b2f314 | 501 | u_char *blob; |
502 | char *uu; | |
3c0ef626 | 503 | |
504 | if (key->type == KEY_RSA1 && key->rsa != NULL) { | |
505 | /* 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 | } | |
514 | } else if ((key->type == KEY_DSA && key->dsa != NULL) || | |
515 | (key->type == KEY_RSA && key->rsa != NULL)) { | |
3c0ef626 | 516 | key_to_blob(key, &blob, &len); |
517 | uu = xmalloc(2*len); | |
518 | n = uuencode(blob, len, uu, 2*len); | |
519 | if (n > 0) { | |
520 | fprintf(f, "%s %s", key_ssh_name(key), uu); | |
521 | success = 1; | |
522 | } | |
523 | xfree(blob); | |
524 | xfree(uu); | |
525 | } | |
526 | return success; | |
527 | } | |
680cee3b | 528 | |
cdd66111 | 529 | const char * |
530 | key_type(const Key *k) | |
3c0ef626 | 531 | { |
532 | switch (k->type) { | |
533 | case KEY_RSA1: | |
534 | return "RSA1"; | |
3c0ef626 | 535 | case KEY_RSA: |
536 | return "RSA"; | |
3c0ef626 | 537 | case KEY_DSA: |
538 | return "DSA"; | |
3c0ef626 | 539 | } |
540 | return "unknown"; | |
541 | } | |
680cee3b | 542 | |
cdd66111 | 543 | const char * |
544 | key_ssh_name(const Key *k) | |
3c0ef626 | 545 | { |
546 | switch (k->type) { | |
547 | case KEY_RSA: | |
548 | return "ssh-rsa"; | |
3c0ef626 | 549 | case KEY_DSA: |
550 | return "ssh-dss"; | |
3c0ef626 | 551 | } |
552 | return "ssh-unknown"; | |
553 | } | |
680cee3b | 554 | |
3c0ef626 | 555 | u_int |
cdd66111 | 556 | key_size(const Key *k) |
e9a17296 | 557 | { |
3c0ef626 | 558 | switch (k->type) { |
559 | case KEY_RSA1: | |
560 | case KEY_RSA: | |
561 | return BN_num_bits(k->rsa->n); | |
3c0ef626 | 562 | case KEY_DSA: |
563 | return BN_num_bits(k->dsa->p); | |
3c0ef626 | 564 | } |
565 | return 0; | |
566 | } | |
567 | ||
568 | static RSA * | |
569 | rsa_generate_private_key(u_int bits) | |
570 | { | |
571 | RSA *private; | |
9108f8d9 | 572 | |
3c0ef626 | 573 | private = RSA_generate_key(bits, 35, NULL, NULL); |
574 | if (private == NULL) | |
575 | fatal("rsa_generate_private_key: key generation failed."); | |
576 | return private; | |
577 | } | |
578 | ||
579 | static DSA* | |
580 | dsa_generate_private_key(u_int bits) | |
581 | { | |
582 | DSA *private = DSA_generate_parameters(bits, NULL, 0, NULL, NULL, NULL, NULL); | |
9108f8d9 | 583 | |
3c0ef626 | 584 | if (private == NULL) |
585 | fatal("dsa_generate_private_key: DSA_generate_parameters failed"); | |
586 | if (!DSA_generate_key(private)) | |
587 | fatal("dsa_generate_private_key: DSA_generate_key failed."); | |
588 | if (private == NULL) | |
589 | fatal("dsa_generate_private_key: NULL."); | |
590 | return private; | |
591 | } | |
592 | ||
593 | Key * | |
594 | key_generate(int type, u_int bits) | |
595 | { | |
596 | Key *k = key_new(KEY_UNSPEC); | |
597 | switch (type) { | |
598 | case KEY_DSA: | |
599 | 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: | |
606 | fatal("key_generate: unknown type %d", type); | |
607 | } | |
608 | k->type = type; | |
609 | return k; | |
610 | } | |
611 | ||
612 | Key * | |
cdd66111 | 613 | key_from_private(const Key *k) |
3c0ef626 | 614 | { |
615 | Key *n = NULL; | |
616 | switch (k->type) { | |
617 | case KEY_DSA: | |
618 | n = key_new(k->type); | |
ff7ec503 | 619 | if ((BN_copy(n->dsa->p, k->dsa->p) == NULL) || |
620 | (BN_copy(n->dsa->q, k->dsa->q) == NULL) || | |
621 | (BN_copy(n->dsa->g, k->dsa->g) == NULL) || | |
622 | (BN_copy(n->dsa->pub_key, k->dsa->pub_key) == NULL)) | |
623 | fatal("key_from_private: BN_copy failed"); | |
3c0ef626 | 624 | break; |
625 | case KEY_RSA: | |
626 | case KEY_RSA1: | |
627 | n = key_new(k->type); | |
ff7ec503 | 628 | if ((BN_copy(n->rsa->n, k->rsa->n) == NULL) || |
629 | (BN_copy(n->rsa->e, k->rsa->e) == NULL)) | |
630 | fatal("key_from_private: BN_copy failed"); | |
3c0ef626 | 631 | break; |
632 | default: | |
633 | fatal("key_from_private: unknown type %d", k->type); | |
634 | break; | |
635 | } | |
636 | return n; | |
637 | } | |
638 | ||
639 | int | |
640 | key_type_from_name(char *name) | |
641 | { | |
e9a17296 | 642 | if (strcmp(name, "rsa1") == 0) { |
3c0ef626 | 643 | return KEY_RSA1; |
e9a17296 | 644 | } else if (strcmp(name, "rsa") == 0) { |
3c0ef626 | 645 | return KEY_RSA; |
e9a17296 | 646 | } else if (strcmp(name, "dsa") == 0) { |
3c0ef626 | 647 | return KEY_DSA; |
e9a17296 | 648 | } else if (strcmp(name, "ssh-rsa") == 0) { |
3c0ef626 | 649 | return KEY_RSA; |
e9a17296 | 650 | } else if (strcmp(name, "ssh-dss") == 0) { |
3c0ef626 | 651 | return KEY_DSA; |
652 | } | |
653 | debug2("key_type_from_name: unknown key type '%s'", name); | |
654 | return KEY_UNSPEC; | |
655 | } | |
656 | ||
657 | int | |
658 | key_names_valid2(const char *names) | |
659 | { | |
660 | char *s, *cp, *p; | |
661 | ||
662 | if (names == NULL || strcmp(names, "") == 0) | |
663 | return 0; | |
664 | s = cp = xstrdup(names); | |
665 | for ((p = strsep(&cp, ",")); p && *p != '\0'; | |
e9a17296 | 666 | (p = strsep(&cp, ","))) { |
3c0ef626 | 667 | switch (key_type_from_name(p)) { |
668 | case KEY_RSA1: | |
669 | case KEY_UNSPEC: | |
670 | xfree(s); | |
671 | return 0; | |
672 | } | |
673 | } | |
674 | debug3("key names ok: [%s]", names); | |
675 | xfree(s); | |
676 | return 1; | |
677 | } | |
678 | ||
679 | Key * | |
cdd66111 | 680 | key_from_blob(const u_char *blob, u_int blen) |
3c0ef626 | 681 | { |
682 | Buffer b; | |
3c0ef626 | 683 | int rlen, type; |
996d5e62 | 684 | char *ktype = NULL; |
3c0ef626 | 685 | Key *key = NULL; |
686 | ||
687 | #ifdef DEBUG_PK | |
688 | dump_base64(stderr, blob, blen); | |
689 | #endif | |
690 | buffer_init(&b); | |
691 | buffer_append(&b, blob, blen); | |
996d5e62 | 692 | if ((ktype = buffer_get_string_ret(&b, NULL)) == NULL) { |
693 | error("key_from_blob: can't read key type"); | |
694 | goto out; | |
695 | } | |
696 | ||
3c0ef626 | 697 | type = key_type_from_name(ktype); |
698 | ||
e9a17296 | 699 | switch (type) { |
3c0ef626 | 700 | case KEY_RSA: |
701 | key = key_new(type); | |
996d5e62 | 702 | if (buffer_get_bignum2_ret(&b, key->rsa->e) == -1 || |
703 | buffer_get_bignum2_ret(&b, key->rsa->n) == -1) { | |
704 | error("key_from_blob: can't read rsa key"); | |
705 | key_free(key); | |
706 | key = NULL; | |
707 | goto out; | |
708 | } | |
3c0ef626 | 709 | #ifdef DEBUG_PK |
710 | RSA_print_fp(stderr, key->rsa, 8); | |
711 | #endif | |
712 | break; | |
713 | case KEY_DSA: | |
714 | key = key_new(type); | |
996d5e62 | 715 | if (buffer_get_bignum2_ret(&b, key->dsa->p) == -1 || |
716 | buffer_get_bignum2_ret(&b, key->dsa->q) == -1 || | |
717 | buffer_get_bignum2_ret(&b, key->dsa->g) == -1 || | |
718 | buffer_get_bignum2_ret(&b, key->dsa->pub_key) == -1) { | |
719 | error("key_from_blob: can't read dsa key"); | |
720 | key_free(key); | |
721 | key = NULL; | |
722 | goto out; | |
723 | } | |
3c0ef626 | 724 | #ifdef DEBUG_PK |
725 | DSA_print_fp(stderr, key->dsa, 8); | |
726 | #endif | |
727 | break; | |
728 | case KEY_UNSPEC: | |
729 | key = key_new(type); | |
730 | break; | |
731 | default: | |
732 | error("key_from_blob: cannot handle type %s", ktype); | |
996d5e62 | 733 | goto out; |
3c0ef626 | 734 | } |
735 | rlen = buffer_len(&b); | |
736 | if (key != NULL && rlen != 0) | |
737 | error("key_from_blob: remaining bytes in key blob %d", rlen); | |
996d5e62 | 738 | out: |
739 | if (ktype != NULL) | |
740 | xfree(ktype); | |
3c0ef626 | 741 | buffer_free(&b); |
742 | return key; | |
743 | } | |
744 | ||
745 | int | |
cdd66111 | 746 | key_to_blob(const Key *key, u_char **blobp, u_int *lenp) |
3c0ef626 | 747 | { |
748 | Buffer b; | |
749 | int len; | |
3c0ef626 | 750 | |
751 | if (key == NULL) { | |
752 | error("key_to_blob: key == NULL"); | |
753 | return 0; | |
754 | } | |
755 | buffer_init(&b); | |
e9a17296 | 756 | switch (key->type) { |
3c0ef626 | 757 | case KEY_DSA: |
758 | buffer_put_cstring(&b, key_ssh_name(key)); | |
759 | buffer_put_bignum2(&b, key->dsa->p); | |
760 | buffer_put_bignum2(&b, key->dsa->q); | |
761 | buffer_put_bignum2(&b, key->dsa->g); | |
762 | buffer_put_bignum2(&b, key->dsa->pub_key); | |
763 | break; | |
764 | case KEY_RSA: | |
765 | buffer_put_cstring(&b, key_ssh_name(key)); | |
766 | buffer_put_bignum2(&b, key->rsa->e); | |
767 | buffer_put_bignum2(&b, key->rsa->n); | |
768 | break; | |
769 | default: | |
770 | error("key_to_blob: unsupported key type %d", key->type); | |
771 | buffer_free(&b); | |
772 | return 0; | |
773 | } | |
774 | len = buffer_len(&b); | |
3c0ef626 | 775 | if (lenp != NULL) |
776 | *lenp = len; | |
41b2f314 | 777 | if (blobp != NULL) { |
778 | *blobp = xmalloc(len); | |
779 | memcpy(*blobp, buffer_ptr(&b), len); | |
780 | } | |
781 | memset(buffer_ptr(&b), 0, len); | |
782 | buffer_free(&b); | |
3c0ef626 | 783 | return len; |
784 | } | |
785 | ||
786 | int | |
787 | key_sign( | |
cdd66111 | 788 | const Key *key, |
e9a17296 | 789 | u_char **sigp, u_int *lenp, |
cdd66111 | 790 | const u_char *data, u_int datalen) |
3c0ef626 | 791 | { |
e9a17296 | 792 | switch (key->type) { |
3c0ef626 | 793 | case KEY_DSA: |
794 | return ssh_dss_sign(key, sigp, lenp, data, datalen); | |
3c0ef626 | 795 | case KEY_RSA: |
796 | return ssh_rsa_sign(key, sigp, lenp, data, datalen); | |
3c0ef626 | 797 | default: |
c9f39d2c | 798 | error("key_sign: invalid key type %d", key->type); |
3c0ef626 | 799 | return -1; |
3c0ef626 | 800 | } |
801 | } | |
802 | ||
f5799ae1 | 803 | /* |
804 | * key_verify returns 1 for a correct signature, 0 for an incorrect signature | |
805 | * and -1 on error. | |
806 | */ | |
3c0ef626 | 807 | int |
808 | key_verify( | |
cdd66111 | 809 | const Key *key, |
810 | const u_char *signature, u_int signaturelen, | |
811 | const u_char *data, u_int datalen) | |
3c0ef626 | 812 | { |
813 | if (signaturelen == 0) | |
814 | return -1; | |
815 | ||
e9a17296 | 816 | switch (key->type) { |
3c0ef626 | 817 | case KEY_DSA: |
818 | return ssh_dss_verify(key, signature, signaturelen, data, datalen); | |
3c0ef626 | 819 | case KEY_RSA: |
820 | return ssh_rsa_verify(key, signature, signaturelen, data, datalen); | |
3c0ef626 | 821 | default: |
c9f39d2c | 822 | error("key_verify: invalid key type %d", key->type); |
3c0ef626 | 823 | return -1; |
3c0ef626 | 824 | } |
825 | } | |
700318f3 | 826 | |
827 | /* Converts a private to a public key */ | |
700318f3 | 828 | Key * |
cdd66111 | 829 | key_demote(const Key *k) |
700318f3 | 830 | { |
831 | Key *pk; | |
832 | ||
9108f8d9 | 833 | pk = xcalloc(1, sizeof(*pk)); |
700318f3 | 834 | pk->type = k->type; |
835 | pk->flags = k->flags; | |
836 | pk->dsa = NULL; | |
837 | pk->rsa = NULL; | |
838 | ||
839 | switch (k->type) { | |
840 | case KEY_RSA1: | |
841 | case KEY_RSA: | |
842 | if ((pk->rsa = RSA_new()) == NULL) | |
843 | fatal("key_demote: RSA_new failed"); | |
844 | if ((pk->rsa->e = BN_dup(k->rsa->e)) == NULL) | |
845 | fatal("key_demote: BN_dup failed"); | |
846 | if ((pk->rsa->n = BN_dup(k->rsa->n)) == NULL) | |
847 | fatal("key_demote: BN_dup failed"); | |
848 | break; | |
849 | case KEY_DSA: | |
850 | if ((pk->dsa = DSA_new()) == NULL) | |
851 | fatal("key_demote: DSA_new failed"); | |
852 | if ((pk->dsa->p = BN_dup(k->dsa->p)) == NULL) | |
853 | fatal("key_demote: BN_dup failed"); | |
854 | if ((pk->dsa->q = BN_dup(k->dsa->q)) == NULL) | |
855 | fatal("key_demote: BN_dup failed"); | |
856 | if ((pk->dsa->g = BN_dup(k->dsa->g)) == NULL) | |
857 | fatal("key_demote: BN_dup failed"); | |
858 | if ((pk->dsa->pub_key = BN_dup(k->dsa->pub_key)) == NULL) | |
859 | fatal("key_demote: BN_dup failed"); | |
860 | break; | |
861 | default: | |
862 | fatal("key_free: bad key type %d", k->type); | |
863 | break; | |
864 | } | |
865 | ||
866 | return (pk); | |
867 | } |