Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 1 | /* |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 2 | * read_bignum(): |
| 3 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
| 4 | * |
| 5 | * As far as I am concerned, the code I have written for this software |
| 6 | * can be used freely for any purpose. Any derived versions of this |
| 7 | * software must be clearly marked as such, and if the derived work is |
| 8 | * incompatible with the protocol description in the RFC file, it must be |
| 9 | * called by a name other than "ssh" or "Secure Shell". |
| 10 | * |
| 11 | * |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 12 | * Copyright (c) 2000 Markus Friedl. All rights reserved. |
| 13 | * |
| 14 | * Redistribution and use in source and binary forms, with or without |
| 15 | * modification, are permitted provided that the following conditions |
| 16 | * are met: |
| 17 | * 1. Redistributions of source code must retain the above copyright |
| 18 | * notice, this list of conditions and the following disclaimer. |
| 19 | * 2. Redistributions in binary form must reproduce the above copyright |
| 20 | * notice, this list of conditions and the following disclaimer in the |
| 21 | * documentation and/or other materials provided with the distribution. |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 22 | * |
| 23 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 24 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 25 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 26 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 27 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 28 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 30 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 31 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 32 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 33 | */ |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 34 | #include "includes.h" |
Ben Lindstrom | cbe3ad2 | 2001-03-11 20:06:59 +0000 | [diff] [blame^] | 35 | RCSID("$OpenBSD: key.c,v 1.20 2001/03/11 15:13:09 jakob Exp $"); |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 36 | |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 37 | #include <openssl/evp.h> |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 38 | |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 39 | #include "xmalloc.h" |
| 40 | #include "key.h" |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 41 | #include "rsa.h" |
| 42 | #include "ssh-dss.h" |
| 43 | #include "ssh-rsa.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 44 | #include "uuencode.h" |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 45 | #include "buffer.h" |
| 46 | #include "bufaux.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 47 | #include "log.h" |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 48 | |
| 49 | Key * |
| 50 | key_new(int type) |
| 51 | { |
| 52 | Key *k; |
| 53 | RSA *rsa; |
| 54 | DSA *dsa; |
| 55 | k = xmalloc(sizeof(*k)); |
| 56 | k->type = type; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 57 | k->dsa = NULL; |
| 58 | k->rsa = NULL; |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 59 | switch (k->type) { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 60 | case KEY_RSA1: |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 61 | case KEY_RSA: |
| 62 | rsa = RSA_new(); |
| 63 | rsa->n = BN_new(); |
| 64 | rsa->e = BN_new(); |
| 65 | k->rsa = rsa; |
| 66 | break; |
| 67 | case KEY_DSA: |
| 68 | dsa = DSA_new(); |
| 69 | dsa->p = BN_new(); |
| 70 | dsa->q = BN_new(); |
| 71 | dsa->g = BN_new(); |
| 72 | dsa->pub_key = BN_new(); |
| 73 | k->dsa = dsa; |
| 74 | break; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 75 | case KEY_UNSPEC: |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 76 | break; |
| 77 | default: |
| 78 | fatal("key_new: bad key type %d", k->type); |
| 79 | break; |
| 80 | } |
| 81 | return k; |
| 82 | } |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 83 | Key * |
| 84 | key_new_private(int type) |
| 85 | { |
| 86 | Key *k = key_new(type); |
| 87 | switch (k->type) { |
| 88 | case KEY_RSA1: |
| 89 | case KEY_RSA: |
| 90 | k->rsa->d = BN_new(); |
| 91 | k->rsa->iqmp = BN_new(); |
| 92 | k->rsa->q = BN_new(); |
| 93 | k->rsa->p = BN_new(); |
| 94 | k->rsa->dmq1 = BN_new(); |
| 95 | k->rsa->dmp1 = BN_new(); |
| 96 | break; |
| 97 | case KEY_DSA: |
| 98 | k->dsa->priv_key = BN_new(); |
| 99 | break; |
| 100 | case KEY_UNSPEC: |
| 101 | break; |
| 102 | default: |
| 103 | break; |
| 104 | } |
| 105 | return k; |
| 106 | } |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 107 | void |
| 108 | key_free(Key *k) |
| 109 | { |
| 110 | switch (k->type) { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 111 | case KEY_RSA1: |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 112 | case KEY_RSA: |
| 113 | if (k->rsa != NULL) |
| 114 | RSA_free(k->rsa); |
| 115 | k->rsa = NULL; |
| 116 | break; |
| 117 | case KEY_DSA: |
| 118 | if (k->dsa != NULL) |
| 119 | DSA_free(k->dsa); |
| 120 | k->dsa = NULL; |
| 121 | break; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 122 | case KEY_UNSPEC: |
| 123 | break; |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 124 | default: |
| 125 | fatal("key_free: bad key type %d", k->type); |
| 126 | break; |
| 127 | } |
| 128 | xfree(k); |
| 129 | } |
| 130 | int |
| 131 | key_equal(Key *a, Key *b) |
| 132 | { |
| 133 | if (a == NULL || b == NULL || a->type != b->type) |
| 134 | return 0; |
| 135 | switch (a->type) { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 136 | case KEY_RSA1: |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 137 | case KEY_RSA: |
| 138 | return a->rsa != NULL && b->rsa != NULL && |
| 139 | BN_cmp(a->rsa->e, b->rsa->e) == 0 && |
| 140 | BN_cmp(a->rsa->n, b->rsa->n) == 0; |
| 141 | break; |
| 142 | case KEY_DSA: |
| 143 | return a->dsa != NULL && b->dsa != NULL && |
| 144 | BN_cmp(a->dsa->p, b->dsa->p) == 0 && |
| 145 | BN_cmp(a->dsa->q, b->dsa->q) == 0 && |
| 146 | BN_cmp(a->dsa->g, b->dsa->g) == 0 && |
| 147 | BN_cmp(a->dsa->pub_key, b->dsa->pub_key) == 0; |
| 148 | break; |
| 149 | default: |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 150 | fatal("key_equal: bad key type %d", a->type); |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 151 | break; |
| 152 | } |
| 153 | return 0; |
| 154 | } |
| 155 | |
Ben Lindstrom | 96e8ea6 | 2001-03-11 20:03:44 +0000 | [diff] [blame] | 156 | u_char* |
| 157 | key_fingerprint_raw(Key *k, enum fp_type dgst_type, size_t *dgst_raw_length) |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 158 | { |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 159 | u_char *blob = NULL; |
Ben Lindstrom | 96e8ea6 | 2001-03-11 20:03:44 +0000 | [diff] [blame] | 160 | u_char *retval = NULL; |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 161 | int len = 0; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 162 | int nlen, elen; |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 163 | |
Ben Lindstrom | 96e8ea6 | 2001-03-11 20:03:44 +0000 | [diff] [blame] | 164 | *dgst_raw_length = 0; |
| 165 | |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 166 | switch (k->type) { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 167 | case KEY_RSA1: |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 168 | nlen = BN_num_bytes(k->rsa->n); |
| 169 | elen = BN_num_bytes(k->rsa->e); |
| 170 | len = nlen + elen; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 171 | blob = xmalloc(len); |
| 172 | BN_bn2bin(k->rsa->n, blob); |
| 173 | BN_bn2bin(k->rsa->e, blob + nlen); |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 174 | break; |
| 175 | case KEY_DSA: |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 176 | case KEY_RSA: |
| 177 | key_to_blob(k, &blob, &len); |
| 178 | break; |
| 179 | case KEY_UNSPEC: |
| 180 | return retval; |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 181 | break; |
| 182 | default: |
Ben Lindstrom | 96e8ea6 | 2001-03-11 20:03:44 +0000 | [diff] [blame] | 183 | fatal("key_fingerprint_raw: bad key type %d", k->type); |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 184 | break; |
| 185 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 186 | if (blob != NULL) { |
Ben Lindstrom | 96e8ea6 | 2001-03-11 20:03:44 +0000 | [diff] [blame] | 187 | EVP_MD *md = NULL; |
Damien Miller | 6536c7d | 2000-06-22 21:32:31 +1000 | [diff] [blame] | 188 | EVP_MD_CTX ctx; |
Ben Lindstrom | 96e8ea6 | 2001-03-11 20:03:44 +0000 | [diff] [blame] | 189 | |
| 190 | retval = xmalloc(EVP_MAX_MD_SIZE); |
| 191 | |
| 192 | switch (dgst_type) { |
| 193 | case SSH_FP_MD5: |
| 194 | md = EVP_md5(); |
| 195 | break; |
| 196 | case SSH_FP_SHA1: |
| 197 | md = EVP_sha1(); |
| 198 | break; |
| 199 | default: |
| 200 | fatal("key_fingerprint_raw: bad digest type %d", |
| 201 | dgst_type); |
| 202 | } |
| 203 | |
Damien Miller | 6536c7d | 2000-06-22 21:32:31 +1000 | [diff] [blame] | 204 | EVP_DigestInit(&ctx, md); |
| 205 | EVP_DigestUpdate(&ctx, blob, len); |
Ben Lindstrom | 96e8ea6 | 2001-03-11 20:03:44 +0000 | [diff] [blame] | 206 | EVP_DigestFinal(&ctx, retval, NULL); |
| 207 | *dgst_raw_length = md->md_size; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 208 | memset(blob, 0, len); |
| 209 | xfree(blob); |
Ben Lindstrom | 96e8ea6 | 2001-03-11 20:03:44 +0000 | [diff] [blame] | 210 | } else { |
| 211 | fatal("key_fingerprint_raw: blob is null"); |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 212 | } |
| 213 | return retval; |
| 214 | } |
| 215 | |
Ben Lindstrom | 96e8ea6 | 2001-03-11 20:03:44 +0000 | [diff] [blame] | 216 | char* |
| 217 | key_fingerprint_hex(u_char* dgst_raw, size_t dgst_raw_len) |
| 218 | { |
| 219 | char *retval; |
| 220 | int i; |
| 221 | |
| 222 | retval = xmalloc(dgst_raw_len * 3); |
| 223 | retval[0] = '\0'; |
| 224 | for(i = 0; i < dgst_raw_len; i++) { |
| 225 | char hex[4]; |
| 226 | snprintf(hex, sizeof(hex), "%02x:", dgst_raw[i]); |
| 227 | strlcat(retval, hex, dgst_raw_len * 3); |
| 228 | } |
| 229 | retval[(dgst_raw_len * 3) - 1] = '\0'; |
| 230 | return retval; |
| 231 | } |
| 232 | |
| 233 | char* |
| 234 | key_fingerprint_bubblebabble(u_char* dgst_raw, size_t dgst_raw_len) |
| 235 | { |
| 236 | char vowels[] = { 'a', 'e', 'i', 'o', 'u', 'y' }; |
| 237 | char consonants[] = { 'b', 'c', 'd', 'f', 'g', 'h', 'k', 'l', 'm', |
| 238 | 'n', 'p', 'r', 's', 't', 'v', 'z', 'x' }; |
Ben Lindstrom | cbe3ad2 | 2001-03-11 20:06:59 +0000 | [diff] [blame^] | 239 | u_int i, j = 0, rounds, seed = 1; |
Ben Lindstrom | 96e8ea6 | 2001-03-11 20:03:44 +0000 | [diff] [blame] | 240 | char *retval; |
| 241 | |
| 242 | rounds = (dgst_raw_len / 2) + 1; |
| 243 | retval = xmalloc(sizeof(char) * (rounds*6)); |
Ben Lindstrom | cbe3ad2 | 2001-03-11 20:06:59 +0000 | [diff] [blame^] | 244 | retval[j++] = 'x'; |
| 245 | for (i = 0; i < rounds; i++) { |
Ben Lindstrom | 96e8ea6 | 2001-03-11 20:03:44 +0000 | [diff] [blame] | 246 | u_int idx0, idx1, idx2, idx3, idx4; |
Ben Lindstrom | cbe3ad2 | 2001-03-11 20:06:59 +0000 | [diff] [blame^] | 247 | if ((i + 1 < rounds) || (dgst_raw_len % 2 != 0)) { |
| 248 | idx0 = (((((u_int)(dgst_raw[2 * i])) >> 6) & 3) + |
Ben Lindstrom | 96e8ea6 | 2001-03-11 20:03:44 +0000 | [diff] [blame] | 249 | seed) % 6; |
Ben Lindstrom | cbe3ad2 | 2001-03-11 20:06:59 +0000 | [diff] [blame^] | 250 | idx1 = (((u_int)(dgst_raw[2 * i])) >> 2) & 15; |
| 251 | idx2 = ((((u_int)(dgst_raw[2 * i])) & 3) + |
Ben Lindstrom | 96e8ea6 | 2001-03-11 20:03:44 +0000 | [diff] [blame] | 252 | (seed / 6)) % 6; |
Ben Lindstrom | cbe3ad2 | 2001-03-11 20:06:59 +0000 | [diff] [blame^] | 253 | retval[j++] = vowels[idx0]; |
| 254 | retval[j++] = consonants[idx1]; |
| 255 | retval[j++] = vowels[idx2]; |
| 256 | if ((i + 1) < rounds) { |
| 257 | idx3 = (((u_int)(dgst_raw[(2 * i) + 1])) >> 4) & 15; |
| 258 | idx4 = (((u_int)(dgst_raw[(2 * i) + 1]))) & 15; |
| 259 | retval[j++] = consonants[idx3]; |
| 260 | retval[j++] = '-'; |
| 261 | retval[j++] = consonants[idx4]; |
Ben Lindstrom | 96e8ea6 | 2001-03-11 20:03:44 +0000 | [diff] [blame] | 262 | seed = ((seed * 5) + |
Ben Lindstrom | cbe3ad2 | 2001-03-11 20:06:59 +0000 | [diff] [blame^] | 263 | ((((u_int)(dgst_raw[2 * i])) * 7) + |
| 264 | ((u_int)(dgst_raw[(2 * i) + 1])))) % 36; |
Ben Lindstrom | 96e8ea6 | 2001-03-11 20:03:44 +0000 | [diff] [blame] | 265 | } |
| 266 | } else { |
| 267 | idx0 = seed % 6; |
| 268 | idx1 = 16; |
| 269 | idx2 = seed / 6; |
Ben Lindstrom | cbe3ad2 | 2001-03-11 20:06:59 +0000 | [diff] [blame^] | 270 | retval[j++] = vowels[idx0]; |
| 271 | retval[j++] = consonants[idx1]; |
| 272 | retval[j++] = vowels[idx2]; |
Ben Lindstrom | 96e8ea6 | 2001-03-11 20:03:44 +0000 | [diff] [blame] | 273 | } |
| 274 | } |
Ben Lindstrom | cbe3ad2 | 2001-03-11 20:06:59 +0000 | [diff] [blame^] | 275 | retval[j++] = 'x'; |
| 276 | retval[j++] = '\0'; |
Ben Lindstrom | 96e8ea6 | 2001-03-11 20:03:44 +0000 | [diff] [blame] | 277 | return retval; |
| 278 | } |
| 279 | |
| 280 | char* |
| 281 | key_fingerprint_ex(Key *k, enum fp_type dgst_type, enum fp_rep dgst_rep) |
| 282 | { |
| 283 | char *retval = NULL; |
| 284 | u_char *dgst_raw; |
| 285 | size_t dgst_raw_len; |
| 286 | |
| 287 | dgst_raw = key_fingerprint_raw(k, dgst_type, &dgst_raw_len); |
| 288 | if (!dgst_raw) |
| 289 | fatal("key_fingerprint_ex: null value returned from key_fingerprint_raw()"); |
| 290 | switch(dgst_rep) { |
| 291 | case SSH_FP_HEX: |
| 292 | retval = key_fingerprint_hex(dgst_raw, dgst_raw_len); |
| 293 | break; |
| 294 | case SSH_FP_BUBBLEBABBLE: |
| 295 | retval = key_fingerprint_bubblebabble(dgst_raw, dgst_raw_len); |
| 296 | break; |
| 297 | default: |
| 298 | fatal("key_fingerprint_ex: bad digest representation %d", |
| 299 | dgst_rep); |
| 300 | break; |
| 301 | } |
| 302 | memset(dgst_raw, 0, dgst_raw_len); |
| 303 | xfree(dgst_raw); |
| 304 | return retval; |
| 305 | } |
| 306 | |
| 307 | char * |
| 308 | key_fingerprint(Key *k) |
| 309 | { |
| 310 | static char retval[(EVP_MAX_MD_SIZE + 1) * 3]; |
| 311 | char *digest; |
| 312 | |
| 313 | digest = key_fingerprint_ex(k, SSH_FP_MD5, SSH_FP_HEX); |
| 314 | strlcpy(retval, digest, sizeof(retval)); |
| 315 | xfree(digest); |
| 316 | return retval; |
| 317 | } |
| 318 | |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 319 | /* |
| 320 | * Reads a multiple-precision integer in decimal from the buffer, and advances |
| 321 | * the pointer. The integer must already be initialized. This function is |
| 322 | * permitted to modify the buffer. This leaves *cpp to point just beyond the |
| 323 | * last processed (and maybe modified) character. Note that this may modify |
| 324 | * the buffer containing the number. |
| 325 | */ |
| 326 | int |
| 327 | read_bignum(char **cpp, BIGNUM * value) |
| 328 | { |
| 329 | char *cp = *cpp; |
| 330 | int old; |
| 331 | |
| 332 | /* Skip any leading whitespace. */ |
| 333 | for (; *cp == ' ' || *cp == '\t'; cp++) |
| 334 | ; |
| 335 | |
| 336 | /* Check that it begins with a decimal digit. */ |
| 337 | if (*cp < '0' || *cp > '9') |
| 338 | return 0; |
| 339 | |
| 340 | /* Save starting position. */ |
| 341 | *cpp = cp; |
| 342 | |
| 343 | /* Move forward until all decimal digits skipped. */ |
| 344 | for (; *cp >= '0' && *cp <= '9'; cp++) |
| 345 | ; |
| 346 | |
| 347 | /* Save the old terminating character, and replace it by \0. */ |
| 348 | old = *cp; |
| 349 | *cp = 0; |
| 350 | |
| 351 | /* Parse the number. */ |
| 352 | if (BN_dec2bn(&value, *cpp) == 0) |
| 353 | return 0; |
| 354 | |
| 355 | /* Restore old terminating character. */ |
| 356 | *cp = old; |
| 357 | |
| 358 | /* Move beyond the number and return success. */ |
| 359 | *cpp = cp; |
| 360 | return 1; |
| 361 | } |
| 362 | int |
| 363 | write_bignum(FILE *f, BIGNUM *num) |
| 364 | { |
| 365 | char *buf = BN_bn2dec(num); |
| 366 | if (buf == NULL) { |
| 367 | error("write_bignum: BN_bn2dec() failed"); |
| 368 | return 0; |
| 369 | } |
| 370 | fprintf(f, " %s", buf); |
Ben Lindstrom | b1985f7 | 2001-01-23 00:19:15 +0000 | [diff] [blame] | 371 | xfree(buf); |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 372 | return 1; |
| 373 | } |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 374 | |
| 375 | /* returns 1 ok, -1 error, 0 type mismatch */ |
| 376 | int |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 377 | key_read(Key *ret, char **cpp) |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 378 | { |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 379 | Key *k; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 380 | int success = -1; |
| 381 | char *cp, *space; |
| 382 | int len, n, type; |
| 383 | u_int bits; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 384 | u_char *blob; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 385 | |
| 386 | cp = *cpp; |
| 387 | |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 388 | switch(ret->type) { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 389 | case KEY_RSA1: |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 390 | /* Get number of bits. */ |
| 391 | if (*cp < '0' || *cp > '9') |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 392 | return -1; /* Bad bit count... */ |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 393 | for (bits = 0; *cp >= '0' && *cp <= '9'; cp++) |
| 394 | bits = 10 * bits + *cp - '0'; |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 395 | if (bits == 0) |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 396 | return -1; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 397 | *cpp = cp; |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 398 | /* Get public exponent, public modulus. */ |
| 399 | if (!read_bignum(cpp, ret->rsa->e)) |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 400 | return -1; |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 401 | if (!read_bignum(cpp, ret->rsa->n)) |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 402 | return -1; |
| 403 | success = 1; |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 404 | break; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 405 | case KEY_UNSPEC: |
| 406 | case KEY_RSA: |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 407 | case KEY_DSA: |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 408 | space = strchr(cp, ' '); |
| 409 | if (space == NULL) { |
| 410 | debug3("key_read: no space"); |
| 411 | return -1; |
| 412 | } |
| 413 | *space = '\0'; |
| 414 | type = key_type_from_name(cp); |
| 415 | *space = ' '; |
| 416 | if (type == KEY_UNSPEC) { |
| 417 | debug3("key_read: no key found"); |
| 418 | return -1; |
| 419 | } |
| 420 | cp = space+1; |
| 421 | if (*cp == '\0') { |
| 422 | debug3("key_read: short string"); |
| 423 | return -1; |
| 424 | } |
| 425 | if (ret->type == KEY_UNSPEC) { |
| 426 | ret->type = type; |
| 427 | } else if (ret->type != type) { |
| 428 | /* is a key, but different type */ |
| 429 | debug3("key_read: type mismatch"); |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 430 | return 0; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 431 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 432 | len = 2*strlen(cp); |
| 433 | blob = xmalloc(len); |
| 434 | n = uudecode(cp, blob, len); |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 435 | if (n < 0) { |
Damien Miller | b1715dc | 2000-05-30 13:44:51 +1000 | [diff] [blame] | 436 | error("key_read: uudecode %s failed", cp); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 437 | return -1; |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 438 | } |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 439 | k = key_from_blob(blob, n); |
Damien Miller | b1715dc | 2000-05-30 13:44:51 +1000 | [diff] [blame] | 440 | if (k == NULL) { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 441 | error("key_read: key_from_blob %s failed", cp); |
| 442 | return -1; |
Damien Miller | b1715dc | 2000-05-30 13:44:51 +1000 | [diff] [blame] | 443 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 444 | xfree(blob); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 445 | if (k->type != type) { |
| 446 | error("key_read: type mismatch: encoding error"); |
| 447 | key_free(k); |
| 448 | return -1; |
| 449 | } |
| 450 | /*XXXX*/ |
| 451 | if (ret->type == KEY_RSA) { |
| 452 | if (ret->rsa != NULL) |
| 453 | RSA_free(ret->rsa); |
| 454 | ret->rsa = k->rsa; |
| 455 | k->rsa = NULL; |
| 456 | success = 1; |
| 457 | #ifdef DEBUG_PK |
| 458 | RSA_print_fp(stderr, ret->rsa, 8); |
| 459 | #endif |
| 460 | } else { |
| 461 | if (ret->dsa != NULL) |
| 462 | DSA_free(ret->dsa); |
| 463 | ret->dsa = k->dsa; |
| 464 | k->dsa = NULL; |
| 465 | success = 1; |
| 466 | #ifdef DEBUG_PK |
| 467 | DSA_print_fp(stderr, ret->dsa, 8); |
| 468 | #endif |
| 469 | } |
| 470 | /*XXXX*/ |
| 471 | if (success != 1) |
| 472 | break; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 473 | key_free(k); |
Damien Miller | b1715dc | 2000-05-30 13:44:51 +1000 | [diff] [blame] | 474 | /* advance cp: skip whitespace and data */ |
| 475 | while (*cp == ' ' || *cp == '\t') |
| 476 | cp++; |
| 477 | while (*cp != '\0' && *cp != ' ' && *cp != '\t') |
| 478 | cp++; |
| 479 | *cpp = cp; |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 480 | break; |
| 481 | default: |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 482 | fatal("key_read: bad key type: %d", ret->type); |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 483 | break; |
| 484 | } |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 485 | return success; |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 486 | } |
| 487 | int |
| 488 | key_write(Key *key, FILE *f) |
| 489 | { |
| 490 | int success = 0; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 491 | u_int bits = 0; |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 492 | |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 493 | if (key->type == KEY_RSA1 && key->rsa != NULL) { |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 494 | /* size of modulus 'n' */ |
| 495 | bits = BN_num_bits(key->rsa->n); |
| 496 | fprintf(f, "%u", bits); |
| 497 | if (write_bignum(f, key->rsa->e) && |
| 498 | write_bignum(f, key->rsa->n)) { |
| 499 | success = 1; |
| 500 | } else { |
| 501 | error("key_write: failed for RSA key"); |
| 502 | } |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 503 | } else if ((key->type == KEY_DSA && key->dsa != NULL) || |
| 504 | (key->type == KEY_RSA && key->rsa != NULL)) { |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 505 | int len, n; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 506 | u_char *blob, *uu; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 507 | key_to_blob(key, &blob, &len); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 508 | uu = xmalloc(2*len); |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 509 | n = uuencode(blob, len, uu, 2*len); |
| 510 | if (n > 0) { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 511 | fprintf(f, "%s %s", key_ssh_name(key), uu); |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 512 | success = 1; |
| 513 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 514 | xfree(blob); |
| 515 | xfree(uu); |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 516 | } |
| 517 | return success; |
| 518 | } |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 519 | char * |
| 520 | key_type(Key *k) |
| 521 | { |
| 522 | switch (k->type) { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 523 | case KEY_RSA1: |
| 524 | return "RSA1"; |
| 525 | break; |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 526 | case KEY_RSA: |
| 527 | return "RSA"; |
| 528 | break; |
| 529 | case KEY_DSA: |
| 530 | return "DSA"; |
| 531 | break; |
| 532 | } |
| 533 | return "unknown"; |
| 534 | } |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 535 | char * |
| 536 | key_ssh_name(Key *k) |
| 537 | { |
| 538 | switch (k->type) { |
| 539 | case KEY_RSA: |
| 540 | return "ssh-rsa"; |
| 541 | break; |
| 542 | case KEY_DSA: |
| 543 | return "ssh-dss"; |
| 544 | break; |
| 545 | } |
| 546 | return "ssh-unknown"; |
| 547 | } |
| 548 | u_int |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 549 | key_size(Key *k){ |
| 550 | switch (k->type) { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 551 | case KEY_RSA1: |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 552 | case KEY_RSA: |
| 553 | return BN_num_bits(k->rsa->n); |
| 554 | break; |
| 555 | case KEY_DSA: |
| 556 | return BN_num_bits(k->dsa->p); |
| 557 | break; |
| 558 | } |
| 559 | return 0; |
| 560 | } |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 561 | |
| 562 | RSA * |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 563 | rsa_generate_private_key(u_int bits) |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 564 | { |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 565 | RSA *private; |
| 566 | private = RSA_generate_key(bits, 35, NULL, NULL); |
| 567 | if (private == NULL) |
| 568 | fatal("rsa_generate_private_key: key generation failed."); |
| 569 | return private; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | DSA* |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 573 | dsa_generate_private_key(u_int bits) |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 574 | { |
| 575 | DSA *private = DSA_generate_parameters(bits, NULL, 0, NULL, NULL, NULL, NULL); |
| 576 | if (private == NULL) |
| 577 | fatal("dsa_generate_private_key: DSA_generate_parameters failed"); |
| 578 | if (!DSA_generate_key(private)) |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 579 | fatal("dsa_generate_private_key: DSA_generate_key failed."); |
| 580 | if (private == NULL) |
| 581 | fatal("dsa_generate_private_key: NULL."); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 582 | return private; |
| 583 | } |
| 584 | |
| 585 | Key * |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 586 | key_generate(int type, u_int bits) |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 587 | { |
| 588 | Key *k = key_new(KEY_UNSPEC); |
| 589 | switch (type) { |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 590 | case KEY_DSA: |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 591 | k->dsa = dsa_generate_private_key(bits); |
| 592 | break; |
| 593 | case KEY_RSA: |
| 594 | case KEY_RSA1: |
| 595 | k->rsa = rsa_generate_private_key(bits); |
| 596 | break; |
| 597 | default: |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 598 | fatal("key_generate: unknown type %d", type); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 599 | } |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 600 | k->type = type; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 601 | return k; |
| 602 | } |
| 603 | |
| 604 | Key * |
| 605 | key_from_private(Key *k) |
| 606 | { |
| 607 | Key *n = NULL; |
| 608 | switch (k->type) { |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 609 | case KEY_DSA: |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 610 | n = key_new(k->type); |
| 611 | BN_copy(n->dsa->p, k->dsa->p); |
| 612 | BN_copy(n->dsa->q, k->dsa->q); |
| 613 | BN_copy(n->dsa->g, k->dsa->g); |
| 614 | BN_copy(n->dsa->pub_key, k->dsa->pub_key); |
| 615 | break; |
| 616 | case KEY_RSA: |
| 617 | case KEY_RSA1: |
| 618 | n = key_new(k->type); |
| 619 | BN_copy(n->rsa->n, k->rsa->n); |
| 620 | BN_copy(n->rsa->e, k->rsa->e); |
| 621 | break; |
| 622 | default: |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 623 | fatal("key_from_private: unknown type %d", k->type); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 624 | break; |
| 625 | } |
| 626 | return n; |
| 627 | } |
| 628 | |
| 629 | int |
| 630 | key_type_from_name(char *name) |
| 631 | { |
| 632 | if (strcmp(name, "rsa1") == 0){ |
| 633 | return KEY_RSA1; |
| 634 | } else if (strcmp(name, "rsa") == 0){ |
| 635 | return KEY_RSA; |
| 636 | } else if (strcmp(name, "dsa") == 0){ |
| 637 | return KEY_DSA; |
| 638 | } else if (strcmp(name, "ssh-rsa") == 0){ |
| 639 | return KEY_RSA; |
| 640 | } else if (strcmp(name, "ssh-dss") == 0){ |
| 641 | return KEY_DSA; |
| 642 | } |
Ben Lindstrom | b54873a | 2001-03-11 20:01:55 +0000 | [diff] [blame] | 643 | debug2("key_type_from_name: unknown key type '%s'", name); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 644 | return KEY_UNSPEC; |
| 645 | } |
| 646 | |
| 647 | Key * |
| 648 | key_from_blob(char *blob, int blen) |
| 649 | { |
| 650 | Buffer b; |
| 651 | char *ktype; |
| 652 | int rlen, type; |
| 653 | Key *key = NULL; |
| 654 | |
| 655 | #ifdef DEBUG_PK |
| 656 | dump_base64(stderr, blob, blen); |
| 657 | #endif |
| 658 | buffer_init(&b); |
| 659 | buffer_append(&b, blob, blen); |
| 660 | ktype = buffer_get_string(&b, NULL); |
| 661 | type = key_type_from_name(ktype); |
| 662 | |
| 663 | switch(type){ |
| 664 | case KEY_RSA: |
| 665 | key = key_new(type); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 666 | buffer_get_bignum2(&b, key->rsa->e); |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 667 | buffer_get_bignum2(&b, key->rsa->n); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 668 | #ifdef DEBUG_PK |
| 669 | RSA_print_fp(stderr, key->rsa, 8); |
| 670 | #endif |
| 671 | break; |
| 672 | case KEY_DSA: |
| 673 | key = key_new(type); |
| 674 | buffer_get_bignum2(&b, key->dsa->p); |
| 675 | buffer_get_bignum2(&b, key->dsa->q); |
| 676 | buffer_get_bignum2(&b, key->dsa->g); |
| 677 | buffer_get_bignum2(&b, key->dsa->pub_key); |
| 678 | #ifdef DEBUG_PK |
| 679 | DSA_print_fp(stderr, key->dsa, 8); |
| 680 | #endif |
| 681 | break; |
| 682 | case KEY_UNSPEC: |
| 683 | key = key_new(type); |
| 684 | break; |
| 685 | default: |
| 686 | error("key_from_blob: cannot handle type %s", ktype); |
| 687 | break; |
| 688 | } |
| 689 | rlen = buffer_len(&b); |
| 690 | if (key != NULL && rlen != 0) |
| 691 | error("key_from_blob: remaining bytes in key blob %d", rlen); |
| 692 | xfree(ktype); |
| 693 | buffer_free(&b); |
| 694 | return key; |
| 695 | } |
| 696 | |
| 697 | int |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 698 | key_to_blob(Key *key, u_char **blobp, u_int *lenp) |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 699 | { |
| 700 | Buffer b; |
| 701 | int len; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 702 | u_char *buf; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 703 | |
| 704 | if (key == NULL) { |
| 705 | error("key_to_blob: key == NULL"); |
| 706 | return 0; |
| 707 | } |
| 708 | buffer_init(&b); |
| 709 | switch(key->type){ |
| 710 | case KEY_DSA: |
| 711 | buffer_put_cstring(&b, key_ssh_name(key)); |
| 712 | buffer_put_bignum2(&b, key->dsa->p); |
| 713 | buffer_put_bignum2(&b, key->dsa->q); |
| 714 | buffer_put_bignum2(&b, key->dsa->g); |
| 715 | buffer_put_bignum2(&b, key->dsa->pub_key); |
| 716 | break; |
| 717 | case KEY_RSA: |
| 718 | buffer_put_cstring(&b, key_ssh_name(key)); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 719 | buffer_put_bignum2(&b, key->rsa->e); |
Ben Lindstrom | bf555ba | 2001-01-18 02:04:35 +0000 | [diff] [blame] | 720 | buffer_put_bignum2(&b, key->rsa->n); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 721 | break; |
| 722 | default: |
| 723 | error("key_to_blob: illegal key type %d", key->type); |
| 724 | break; |
| 725 | } |
| 726 | len = buffer_len(&b); |
| 727 | buf = xmalloc(len); |
| 728 | memcpy(buf, buffer_ptr(&b), len); |
| 729 | memset(buffer_ptr(&b), 0, len); |
| 730 | buffer_free(&b); |
| 731 | if (lenp != NULL) |
| 732 | *lenp = len; |
| 733 | if (blobp != NULL) |
| 734 | *blobp = buf; |
| 735 | return len; |
| 736 | } |
| 737 | |
| 738 | int |
| 739 | key_sign( |
| 740 | Key *key, |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 741 | u_char **sigp, int *lenp, |
| 742 | u_char *data, int datalen) |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 743 | { |
| 744 | switch(key->type){ |
| 745 | case KEY_DSA: |
| 746 | return ssh_dss_sign(key, sigp, lenp, data, datalen); |
| 747 | break; |
| 748 | case KEY_RSA: |
| 749 | return ssh_rsa_sign(key, sigp, lenp, data, datalen); |
| 750 | break; |
| 751 | default: |
| 752 | error("key_sign: illegal key type %d", key->type); |
| 753 | return -1; |
| 754 | break; |
| 755 | } |
| 756 | } |
| 757 | |
| 758 | int |
| 759 | key_verify( |
| 760 | Key *key, |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 761 | u_char *signature, int signaturelen, |
| 762 | u_char *data, int datalen) |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 763 | { |
| 764 | switch(key->type){ |
| 765 | case KEY_DSA: |
| 766 | return ssh_dss_verify(key, signature, signaturelen, data, datalen); |
| 767 | break; |
| 768 | case KEY_RSA: |
| 769 | return ssh_rsa_verify(key, signature, signaturelen, data, datalen); |
| 770 | break; |
| 771 | default: |
| 772 | error("key_verify: illegal key type %d", key->type); |
| 773 | return -1; |
| 774 | break; |
| 775 | } |
| 776 | } |