Allow IPv6 address entry in tools>ping - Loosens valid character check
[tomato/davidwu.git] / release / src / router / dropbear / signkey.c
blob1d908f4b8a88b2ddf9d6f9595b7b16c462a20f6b
1 /*
2 * Dropbear - a SSH2 server
3 *
4 * Copyright (c) 2002,2003 Matt Johnston
5 * All rights reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE. */
25 #include "includes.h"
26 #include "dbutil.h"
27 #include "signkey.h"
28 #include "buffer.h"
29 #include "ssh.h"
31 /* malloc a new sign_key and set the dss and rsa keys to NULL */
32 sign_key * new_sign_key() {
34 sign_key * ret;
36 ret = (sign_key*)m_malloc(sizeof(sign_key));
37 #ifdef DROPBEAR_DSS
38 ret->dsskey = NULL;
39 #endif
40 #ifdef DROPBEAR_RSA
41 ret->rsakey = NULL;
42 #endif
43 ret->filename = NULL;
44 ret->type = DROPBEAR_SIGNKEY_NONE;
45 ret->source = SIGNKEY_SOURCE_INVALID;
46 return ret;
49 /* Returns "ssh-dss" or "ssh-rsa" corresponding to the type. Exits fatally
50 * if the type is invalid */
51 const char* signkey_name_from_type(int type, int *namelen) {
53 #ifdef DROPBEAR_RSA
54 if (type == DROPBEAR_SIGNKEY_RSA) {
55 *namelen = SSH_SIGNKEY_RSA_LEN;
56 return SSH_SIGNKEY_RSA;
58 #endif
59 #ifdef DROPBEAR_DSS
60 if (type == DROPBEAR_SIGNKEY_DSS) {
61 *namelen = SSH_SIGNKEY_DSS_LEN;
62 return SSH_SIGNKEY_DSS;
64 #endif
65 dropbear_exit("Bad key type %d", type);
66 return NULL; /* notreached */
69 /* Returns DROPBEAR_SIGNKEY_RSA, DROPBEAR_SIGNKEY_DSS,
70 * or DROPBEAR_SIGNKEY_NONE */
71 int signkey_type_from_name(const char* name, int namelen) {
73 #ifdef DROPBEAR_RSA
74 if (namelen == SSH_SIGNKEY_RSA_LEN
75 && memcmp(name, SSH_SIGNKEY_RSA, SSH_SIGNKEY_RSA_LEN) == 0) {
76 return DROPBEAR_SIGNKEY_RSA;
78 #endif
79 #ifdef DROPBEAR_DSS
80 if (namelen == SSH_SIGNKEY_DSS_LEN
81 && memcmp(name, SSH_SIGNKEY_DSS, SSH_SIGNKEY_DSS_LEN) == 0) {
82 return DROPBEAR_SIGNKEY_DSS;
84 #endif
86 TRACE(("signkey_type_from_name unexpected key type."))
88 return DROPBEAR_SIGNKEY_NONE;
91 /* returns DROPBEAR_SUCCESS on success, DROPBEAR_FAILURE on fail.
92 * type should be set by the caller to specify the type to read, and
93 * on return is set to the type read (useful when type = _ANY) */
94 int buf_get_pub_key(buffer *buf, sign_key *key, int *type) {
96 unsigned char* ident;
97 unsigned int len;
98 int keytype;
99 int ret = DROPBEAR_FAILURE;
101 TRACE(("enter buf_get_pub_key"))
103 ident = buf_getstring(buf, &len);
104 keytype = signkey_type_from_name(ident, len);
105 m_free(ident);
107 if (*type != DROPBEAR_SIGNKEY_ANY && *type != keytype) {
108 TRACE(("buf_get_pub_key bad type - got %d, expected %d", keytype, *type))
109 return DROPBEAR_FAILURE;
112 TRACE(("buf_get_pub_key keytype is %d", keytype))
114 *type = keytype;
116 /* Rewind the buffer back before "ssh-rsa" etc */
117 buf_incrpos(buf, -len - 4);
119 #ifdef DROPBEAR_DSS
120 if (keytype == DROPBEAR_SIGNKEY_DSS) {
121 dss_key_free(key->dsskey);
122 key->dsskey = m_malloc(sizeof(*key->dsskey));
123 ret = buf_get_dss_pub_key(buf, key->dsskey);
124 if (ret == DROPBEAR_FAILURE) {
125 m_free(key->dsskey);
128 #endif
129 #ifdef DROPBEAR_RSA
130 if (keytype == DROPBEAR_SIGNKEY_RSA) {
131 rsa_key_free(key->rsakey);
132 key->rsakey = m_malloc(sizeof(*key->rsakey));
133 ret = buf_get_rsa_pub_key(buf, key->rsakey);
134 if (ret == DROPBEAR_FAILURE) {
135 m_free(key->rsakey);
138 #endif
140 TRACE(("leave buf_get_pub_key"))
142 return ret;
146 /* returns DROPBEAR_SUCCESS on success, DROPBEAR_FAILURE on fail.
147 * type should be set by the caller to specify the type to read, and
148 * on return is set to the type read (useful when type = _ANY) */
149 int buf_get_priv_key(buffer *buf, sign_key *key, int *type) {
151 unsigned char* ident;
152 unsigned int len;
153 int keytype;
154 int ret = DROPBEAR_FAILURE;
156 TRACE(("enter buf_get_priv_key"))
158 ident = buf_getstring(buf, &len);
159 keytype = signkey_type_from_name(ident, len);
160 m_free(ident);
162 if (*type != DROPBEAR_SIGNKEY_ANY && *type != keytype) {
163 TRACE(("wrong key type: %d %d", *type, keytype))
164 return DROPBEAR_FAILURE;
167 *type = keytype;
169 /* Rewind the buffer back before "ssh-rsa" etc */
170 buf_incrpos(buf, -len - 4);
172 #ifdef DROPBEAR_DSS
173 if (keytype == DROPBEAR_SIGNKEY_DSS) {
174 dss_key_free(key->dsskey);
175 key->dsskey = m_malloc(sizeof(*key->dsskey));
176 ret = buf_get_dss_priv_key(buf, key->dsskey);
177 if (ret == DROPBEAR_FAILURE) {
178 m_free(key->dsskey);
181 #endif
182 #ifdef DROPBEAR_RSA
183 if (keytype == DROPBEAR_SIGNKEY_RSA) {
184 rsa_key_free(key->rsakey);
185 key->rsakey = m_malloc(sizeof(*key->rsakey));
186 ret = buf_get_rsa_priv_key(buf, key->rsakey);
187 if (ret == DROPBEAR_FAILURE) {
188 m_free(key->rsakey);
191 #endif
193 TRACE(("leave buf_get_priv_key"))
195 return ret;
199 /* type is either DROPBEAR_SIGNKEY_DSS or DROPBEAR_SIGNKEY_RSA */
200 void buf_put_pub_key(buffer* buf, sign_key *key, int type) {
202 buffer *pubkeys;
204 TRACE(("enter buf_put_pub_key"))
205 pubkeys = buf_new(MAX_PUBKEY_SIZE);
207 #ifdef DROPBEAR_DSS
208 if (type == DROPBEAR_SIGNKEY_DSS) {
209 buf_put_dss_pub_key(pubkeys, key->dsskey);
211 #endif
212 #ifdef DROPBEAR_RSA
213 if (type == DROPBEAR_SIGNKEY_RSA) {
214 buf_put_rsa_pub_key(pubkeys, key->rsakey);
216 #endif
217 if (pubkeys->len == 0) {
218 dropbear_exit("Bad key types in buf_put_pub_key");
221 buf_setpos(pubkeys, 0);
222 buf_putstring(buf, buf_getptr(pubkeys, pubkeys->len),
223 pubkeys->len);
225 buf_free(pubkeys);
226 TRACE(("leave buf_put_pub_key"))
229 /* type is either DROPBEAR_SIGNKEY_DSS or DROPBEAR_SIGNKEY_RSA */
230 void buf_put_priv_key(buffer* buf, sign_key *key, int type) {
232 TRACE(("enter buf_put_priv_key"))
233 TRACE(("type is %d", type))
235 #ifdef DROPBEAR_DSS
236 if (type == DROPBEAR_SIGNKEY_DSS) {
237 buf_put_dss_priv_key(buf, key->dsskey);
238 TRACE(("leave buf_put_priv_key: dss done"))
239 return;
241 #endif
242 #ifdef DROPBEAR_RSA
243 if (type == DROPBEAR_SIGNKEY_RSA) {
244 buf_put_rsa_priv_key(buf, key->rsakey);
245 TRACE(("leave buf_put_priv_key: rsa done"))
246 return;
248 #endif
249 dropbear_exit("Bad key types in put pub key");
252 void sign_key_free(sign_key *key) {
254 TRACE(("enter sign_key_free"))
256 #ifdef DROPBEAR_DSS
257 dss_key_free(key->dsskey);
258 key->dsskey = NULL;
259 #endif
260 #ifdef DROPBEAR_RSA
261 rsa_key_free(key->rsakey);
262 key->rsakey = NULL;
263 #endif
265 m_free(key->filename);
267 m_free(key);
268 TRACE(("leave sign_key_free"))
271 static char hexdig(unsigned char x) {
273 if (x > 0xf)
274 return 'X';
276 if (x < 10)
277 return '0' + x;
278 else
279 return 'a' + x - 10;
282 /* Since we're not sure if we'll have md5 or sha1, we present both.
283 * MD5 is used in preference, but sha1 could still be useful */
284 #ifdef DROPBEAR_MD5_HMAC
285 static char * sign_key_md5_fingerprint(unsigned char* keyblob,
286 unsigned int keybloblen) {
288 char * ret;
289 hash_state hs;
290 unsigned char hash[MD5_HASH_SIZE];
291 unsigned int i;
292 unsigned int buflen;
294 md5_init(&hs);
296 /* skip the size int of the string - this is a bit messy */
297 md5_process(&hs, keyblob, keybloblen);
299 md5_done(&hs, hash);
301 /* "md5 hexfingerprinthere\0", each hex digit is "AB:" etc */
302 buflen = 4 + 3*MD5_HASH_SIZE;
303 ret = (char*)m_malloc(buflen);
305 memset(ret, 'Z', buflen);
306 strcpy(ret, "md5 ");
308 for (i = 0; i < MD5_HASH_SIZE; i++) {
309 unsigned int pos = 4 + i*3;
310 ret[pos] = hexdig(hash[i] >> 4);
311 ret[pos+1] = hexdig(hash[i] & 0x0f);
312 ret[pos+2] = ':';
314 ret[buflen-1] = 0x0;
316 return ret;
319 #else /* use SHA1 rather than MD5 for fingerprint */
320 static char * sign_key_sha1_fingerprint(unsigned char* keyblob,
321 unsigned int keybloblen) {
323 char * ret;
324 hash_state hs;
325 unsigned char hash[SHA1_HASH_SIZE];
326 unsigned int i;
327 unsigned int buflen;
329 sha1_init(&hs);
331 /* skip the size int of the string - this is a bit messy */
332 sha1_process(&hs, keyblob, keybloblen);
334 sha1_done(&hs, hash);
336 /* "sha1 hexfingerprinthere\0", each hex digit is "AB:" etc */
337 buflen = 5 + 3*SHA1_HASH_SIZE;
338 ret = (char*)m_malloc(buflen);
340 strcpy(ret, "sha1 ");
342 for (i = 0; i < SHA1_HASH_SIZE; i++) {
343 unsigned int pos = 5 + 3*i;
344 ret[pos] = hexdig(hash[i] >> 4);
345 ret[pos+1] = hexdig(hash[i] & 0x0f);
346 ret[pos+2] = ':';
348 ret[buflen-1] = 0x0;
350 return ret;
353 #endif /* MD5/SHA1 switch */
355 /* This will return a freshly malloced string, containing a fingerprint
356 * in either sha1 or md5 */
357 char * sign_key_fingerprint(unsigned char* keyblob, unsigned int keybloblen) {
359 #ifdef DROPBEAR_MD5_HMAC
360 return sign_key_md5_fingerprint(keyblob, keybloblen);
361 #else
362 return sign_key_sha1_fingerprint(keyblob, keybloblen);
363 #endif
366 void buf_put_sign(buffer* buf, sign_key *key, int type,
367 const unsigned char *data, unsigned int len) {
369 buffer *sigblob;
370 sigblob = buf_new(MAX_PUBKEY_SIZE);
372 #ifdef DROPBEAR_DSS
373 if (type == DROPBEAR_SIGNKEY_DSS) {
374 buf_put_dss_sign(sigblob, key->dsskey, data, len);
376 #endif
377 #ifdef DROPBEAR_RSA
378 if (type == DROPBEAR_SIGNKEY_RSA) {
379 buf_put_rsa_sign(sigblob, key->rsakey, data, len);
381 #endif
382 if (sigblob->len == 0) {
383 dropbear_exit("Non-matching signing type");
385 buf_setpos(sigblob, 0);
386 buf_putstring(buf, buf_getptr(sigblob, sigblob->len),
387 sigblob->len);
389 buf_free(sigblob);
393 #ifdef DROPBEAR_SIGNKEY_VERIFY
394 /* Return DROPBEAR_SUCCESS or DROPBEAR_FAILURE.
395 * If FAILURE is returned, the position of
396 * buf is undefined. If SUCCESS is returned, buf will be positioned after the
397 * signature blob */
398 int buf_verify(buffer * buf, sign_key *key, const unsigned char *data,
399 unsigned int len) {
401 unsigned int bloblen;
402 unsigned char * ident = NULL;
403 unsigned int identlen = 0;
405 TRACE(("enter buf_verify"))
407 bloblen = buf_getint(buf);
408 ident = buf_getstring(buf, &identlen);
410 #ifdef DROPBEAR_DSS
411 if (bloblen == DSS_SIGNATURE_SIZE &&
412 memcmp(ident, SSH_SIGNKEY_DSS, identlen) == 0) {
413 m_free(ident);
414 if (key->dsskey == NULL) {
415 dropbear_exit("No DSS key to verify signature");
417 return buf_dss_verify(buf, key->dsskey, data, len);
419 #endif
421 #ifdef DROPBEAR_RSA
422 if (memcmp(ident, SSH_SIGNKEY_RSA, identlen) == 0) {
423 m_free(ident);
424 if (key->rsakey == NULL) {
425 dropbear_exit("No RSA key to verify signature");
427 return buf_rsa_verify(buf, key->rsakey, data, len);
429 #endif
431 m_free(ident);
432 dropbear_exit("Non-matching signing type");
433 return DROPBEAR_FAILURE;
435 #endif /* DROPBEAR_SIGNKEY_VERIFY */
437 #ifdef DROPBEAR_KEY_LINES /* ie we're using authorized_keys or known_hosts */
439 /* Returns DROPBEAR_SUCCESS or DROPBEAR_FAILURE when given a buffer containing
440 * a key, a key, and a type. The buffer is positioned at the start of the
441 * base64 data, and contains no trailing data */
442 /* If fingerprint is non-NULL, it will be set to a malloc()ed fingerprint
443 of the key if it is successfully decoded */
444 int cmp_base64_key(const unsigned char* keyblob, unsigned int keybloblen,
445 const unsigned char* algoname, unsigned int algolen,
446 buffer * line, char ** fingerprint) {
448 buffer * decodekey = NULL;
449 int ret = DROPBEAR_FAILURE;
450 unsigned int len, filealgolen;
451 unsigned long decodekeylen;
452 unsigned char* filealgo = NULL;
454 /* now we have the actual data */
455 len = line->len - line->pos;
456 decodekeylen = len * 2; /* big to be safe */
457 decodekey = buf_new(decodekeylen);
459 if (base64_decode(buf_getptr(line, len), len,
460 buf_getwriteptr(decodekey, decodekey->size),
461 &decodekeylen) != CRYPT_OK) {
462 TRACE(("checkpubkey: base64 decode failed"))
463 goto out;
465 TRACE(("checkpubkey: base64_decode success"))
466 buf_incrlen(decodekey, decodekeylen);
468 if (fingerprint) {
469 *fingerprint = sign_key_fingerprint(buf_getptr(decodekey, decodekeylen),
470 decodekeylen);
473 /* compare the keys */
474 if ( ( decodekeylen != keybloblen )
475 || memcmp( buf_getptr(decodekey, decodekey->len),
476 keyblob, decodekey->len) != 0) {
477 TRACE(("checkpubkey: compare failed"))
478 goto out;
481 /* ... and also check that the algo specified and the algo in the key
482 * itself match */
483 filealgolen = buf_getint(decodekey);
484 filealgo = buf_getptr(decodekey, filealgolen);
485 if (filealgolen != algolen || memcmp(filealgo, algoname, algolen) != 0) {
486 TRACE(("checkpubkey: algo match failed"))
487 goto out;
490 /* All checks passed */
491 ret = DROPBEAR_SUCCESS;
493 out:
494 buf_free(decodekey);
495 decodekey = NULL;
496 return ret;
498 #endif