diff options
Diffstat (limited to 'key.c')
-rw-r--r-- | key.c | 301 |
1 files changed, 301 insertions, 0 deletions
@@ -0,0 +1,301 @@ | |||
1 | /* | ||
2 | * Copyright (c) 2000 Markus Friedl. All rights reserved. | ||
3 | * | ||
4 | * Redistribution and use in source and binary forms, with or without | ||
5 | * modification, are permitted provided that the following conditions | ||
6 | * are met: | ||
7 | * 1. Redistributions of source code must retain the above copyright | ||
8 | * notice, this list of conditions and the following disclaimer. | ||
9 | * 2. Redistributions in binary form must reproduce the above copyright | ||
10 | * notice, this list of conditions and the following disclaimer in the | ||
11 | * documentation and/or other materials provided with the distribution. | ||
12 | * 3. All advertising materials mentioning features or use of this software | ||
13 | * must display the following acknowledgement: | ||
14 | * This product includes software developed by Markus Friedl. | ||
15 | * 4. The name of the author may not be used to endorse or promote products | ||
16 | * derived from this software without specific prior written permission. | ||
17 | * | ||
18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | ||
19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | ||
20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | ||
21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, | ||
22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | ||
23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | ||
27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
28 | */ | ||
29 | /* | ||
30 | * read_bignum(): | ||
31 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland | ||
32 | */ | ||
33 | |||
34 | #include "includes.h" | ||
35 | |||
36 | #ifdef HAVE_OPENSSL | ||
37 | #include <openssl/bn.h> | ||
38 | #include <openssl/rsa.h> | ||
39 | #include <openssl/dsa.h> | ||
40 | #include <openssl/evp.h> | ||
41 | #endif | ||
42 | #ifdef HAVE_SSL | ||
43 | #include <ssl/bn.h> | ||
44 | #include <ssl/rsa.h> | ||
45 | #include <ssl/dsa.h> | ||
46 | #include <ssl/evp.h> | ||
47 | #endif | ||
48 | |||
49 | #include "ssh.h" | ||
50 | #include "xmalloc.h" | ||
51 | #include "key.h" | ||
52 | |||
53 | Key * | ||
54 | key_new(int type) | ||
55 | { | ||
56 | Key *k; | ||
57 | RSA *rsa; | ||
58 | DSA *dsa; | ||
59 | k = xmalloc(sizeof(*k)); | ||
60 | k->type = type; | ||
61 | switch (k->type) { | ||
62 | case KEY_RSA: | ||
63 | rsa = RSA_new(); | ||
64 | rsa->n = BN_new(); | ||
65 | rsa->e = BN_new(); | ||
66 | k->rsa = rsa; | ||
67 | break; | ||
68 | case KEY_DSA: | ||
69 | dsa = DSA_new(); | ||
70 | dsa->p = BN_new(); | ||
71 | dsa->q = BN_new(); | ||
72 | dsa->g = BN_new(); | ||
73 | dsa->pub_key = BN_new(); | ||
74 | k->dsa = dsa; | ||
75 | break; | ||
76 | case KEY_EMPTY: | ||
77 | k->dsa = NULL; | ||
78 | k->rsa = NULL; | ||
79 | break; | ||
80 | default: | ||
81 | fatal("key_new: bad key type %d", k->type); | ||
82 | break; | ||
83 | } | ||
84 | return k; | ||
85 | } | ||
86 | void | ||
87 | key_free(Key *k) | ||
88 | { | ||
89 | switch (k->type) { | ||
90 | case KEY_RSA: | ||
91 | if (k->rsa != NULL) | ||
92 | RSA_free(k->rsa); | ||
93 | k->rsa = NULL; | ||
94 | break; | ||
95 | case KEY_DSA: | ||
96 | if (k->dsa != NULL) | ||
97 | DSA_free(k->dsa); | ||
98 | k->dsa = NULL; | ||
99 | break; | ||
100 | default: | ||
101 | fatal("key_free: bad key type %d", k->type); | ||
102 | break; | ||
103 | } | ||
104 | xfree(k); | ||
105 | } | ||
106 | int | ||
107 | key_equal(Key *a, Key *b) | ||
108 | { | ||
109 | if (a == NULL || b == NULL || a->type != b->type) | ||
110 | return 0; | ||
111 | switch (a->type) { | ||
112 | case KEY_RSA: | ||
113 | return a->rsa != NULL && b->rsa != NULL && | ||
114 | BN_cmp(a->rsa->e, b->rsa->e) == 0 && | ||
115 | BN_cmp(a->rsa->n, b->rsa->n) == 0; | ||
116 | break; | ||
117 | case KEY_DSA: | ||
118 | return a->dsa != NULL && b->dsa != NULL && | ||
119 | BN_cmp(a->dsa->p, b->dsa->p) == 0 && | ||
120 | BN_cmp(a->dsa->q, b->dsa->q) == 0 && | ||
121 | BN_cmp(a->dsa->g, b->dsa->g) == 0 && | ||
122 | BN_cmp(a->dsa->pub_key, b->dsa->pub_key) == 0; | ||
123 | break; | ||
124 | default: | ||
125 | fatal("key_free: bad key type %d", a->type); | ||
126 | break; | ||
127 | } | ||
128 | return 0; | ||
129 | } | ||
130 | |||
131 | #define FPRINT "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x" | ||
132 | |||
133 | /* | ||
134 | * Generate key fingerprint in ascii format. | ||
135 | * Based on ideas and code from Bjoern Groenvall <bg@sics.se> | ||
136 | */ | ||
137 | char * | ||
138 | key_fingerprint(Key *k) | ||
139 | { | ||
140 | static char retval[80]; | ||
141 | unsigned char *buf = NULL; | ||
142 | int len = 0; | ||
143 | int nlen, elen, plen, qlen, glen, publen; | ||
144 | |||
145 | switch (k->type) { | ||
146 | case KEY_RSA: | ||
147 | nlen = BN_num_bytes(k->rsa->n); | ||
148 | elen = BN_num_bytes(k->rsa->e); | ||
149 | len = nlen + elen; | ||
150 | buf = xmalloc(len); | ||
151 | BN_bn2bin(k->rsa->n, buf); | ||
152 | BN_bn2bin(k->rsa->e, buf + nlen); | ||
153 | break; | ||
154 | case KEY_DSA: | ||
155 | plen = BN_num_bytes(k->dsa->p); | ||
156 | qlen = BN_num_bytes(k->dsa->q); | ||
157 | glen = BN_num_bytes(k->dsa->g); | ||
158 | publen = BN_num_bytes(k->dsa->pub_key); | ||
159 | len = qlen + qlen + glen + publen; | ||
160 | buf = xmalloc(len); | ||
161 | BN_bn2bin(k->dsa->p, buf); | ||
162 | BN_bn2bin(k->dsa->q, buf + plen); | ||
163 | BN_bn2bin(k->dsa->g, buf + plen + qlen); | ||
164 | BN_bn2bin(k->dsa->pub_key , buf + plen + qlen + glen); | ||
165 | break; | ||
166 | default: | ||
167 | fatal("key_fingerprint: bad key type %d", k->type); | ||
168 | break; | ||
169 | } | ||
170 | if (buf != NULL) { | ||
171 | unsigned char d[16]; | ||
172 | EVP_MD_CTX md; | ||
173 | EVP_DigestInit(&md, EVP_md5()); | ||
174 | EVP_DigestUpdate(&md, buf, len); | ||
175 | EVP_DigestFinal(&md, d, NULL); | ||
176 | snprintf(retval, sizeof(retval), FPRINT, | ||
177 | d[0], d[1], d[2], d[3], d[4], d[5], d[6], d[7], | ||
178 | d[8], d[9], d[10], d[11], d[12], d[13], d[14], d[15]); | ||
179 | memset(buf, 0, len); | ||
180 | xfree(buf); | ||
181 | } | ||
182 | return retval; | ||
183 | } | ||
184 | |||
185 | /* | ||
186 | * Reads a multiple-precision integer in decimal from the buffer, and advances | ||
187 | * the pointer. The integer must already be initialized. This function is | ||
188 | * permitted to modify the buffer. This leaves *cpp to point just beyond the | ||
189 | * last processed (and maybe modified) character. Note that this may modify | ||
190 | * the buffer containing the number. | ||
191 | */ | ||
192 | int | ||
193 | read_bignum(char **cpp, BIGNUM * value) | ||
194 | { | ||
195 | char *cp = *cpp; | ||
196 | int old; | ||
197 | |||
198 | /* Skip any leading whitespace. */ | ||
199 | for (; *cp == ' ' || *cp == '\t'; cp++) | ||
200 | ; | ||
201 | |||
202 | /* Check that it begins with a decimal digit. */ | ||
203 | if (*cp < '0' || *cp > '9') | ||
204 | return 0; | ||
205 | |||
206 | /* Save starting position. */ | ||
207 | *cpp = cp; | ||
208 | |||
209 | /* Move forward until all decimal digits skipped. */ | ||
210 | for (; *cp >= '0' && *cp <= '9'; cp++) | ||
211 | ; | ||
212 | |||
213 | /* Save the old terminating character, and replace it by \0. */ | ||
214 | old = *cp; | ||
215 | *cp = 0; | ||
216 | |||
217 | /* Parse the number. */ | ||
218 | if (BN_dec2bn(&value, *cpp) == 0) | ||
219 | return 0; | ||
220 | |||
221 | /* Restore old terminating character. */ | ||
222 | *cp = old; | ||
223 | |||
224 | /* Move beyond the number and return success. */ | ||
225 | *cpp = cp; | ||
226 | return 1; | ||
227 | } | ||
228 | int | ||
229 | write_bignum(FILE *f, BIGNUM *num) | ||
230 | { | ||
231 | char *buf = BN_bn2dec(num); | ||
232 | if (buf == NULL) { | ||
233 | error("write_bignum: BN_bn2dec() failed"); | ||
234 | return 0; | ||
235 | } | ||
236 | fprintf(f, " %s", buf); | ||
237 | free(buf); | ||
238 | return 1; | ||
239 | } | ||
240 | int | ||
241 | key_read(Key *ret, unsigned int bits, char **cpp) | ||
242 | { | ||
243 | switch(ret->type) { | ||
244 | case KEY_RSA: | ||
245 | if (bits == 0) | ||
246 | return 0; | ||
247 | /* Get public exponent, public modulus. */ | ||
248 | if (!read_bignum(cpp, ret->rsa->e)) | ||
249 | return 0; | ||
250 | if (!read_bignum(cpp, ret->rsa->n)) | ||
251 | return 0; | ||
252 | break; | ||
253 | case KEY_DSA: | ||
254 | if (bits != 0) | ||
255 | return 0; | ||
256 | if (!read_bignum(cpp, ret->dsa->p)) | ||
257 | return 0; | ||
258 | if (!read_bignum(cpp, ret->dsa->q)) | ||
259 | return 0; | ||
260 | if (!read_bignum(cpp, ret->dsa->g)) | ||
261 | return 0; | ||
262 | if (!read_bignum(cpp, ret->dsa->pub_key)) | ||
263 | return 0; | ||
264 | break; | ||
265 | default: | ||
266 | fatal("bad key type: %d", ret->type); | ||
267 | break; | ||
268 | } | ||
269 | return 1; | ||
270 | } | ||
271 | int | ||
272 | key_write(Key *key, FILE *f) | ||
273 | { | ||
274 | int success = 0; | ||
275 | unsigned int bits = 0; | ||
276 | |||
277 | if (key->type == KEY_RSA && key->rsa != NULL) { | ||
278 | /* size of modulus 'n' */ | ||
279 | bits = BN_num_bits(key->rsa->n); | ||
280 | fprintf(f, "%u", bits); | ||
281 | if (write_bignum(f, key->rsa->e) && | ||
282 | write_bignum(f, key->rsa->n)) { | ||
283 | success = 1; | ||
284 | } else { | ||
285 | error("key_write: failed for RSA key"); | ||
286 | } | ||
287 | } else if (key->type == KEY_DSA && key->dsa != NULL) { | ||
288 | /* bits == 0 means DSA key */ | ||
289 | bits = 0; | ||
290 | fprintf(f, "%u", bits); | ||
291 | if (write_bignum(f, key->dsa->p) && | ||
292 | write_bignum(f, key->dsa->q) && | ||
293 | write_bignum(f, key->dsa->g) && | ||
294 | write_bignum(f, key->dsa->pub_key)) { | ||
295 | success = 1; | ||
296 | } else { | ||
297 | error("key_write: failed for DSA key"); | ||
298 | } | ||
299 | } | ||
300 | return success; | ||
301 | } | ||