001: /* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
002: /*
003: Copyright (c) 2002-2008 ymnk, JCraft,Inc. All rights reserved.
004:
005: Redistribution and use in source and binary forms, with or without
006: modification, are permitted provided that the following conditions are met:
007:
008: 1. Redistributions of source code must retain the above copyright notice,
009: this list of conditions and the following disclaimer.
010:
011: 2. Redistributions in binary form must reproduce the above copyright
012: notice, this list of conditions and the following disclaimer in
013: the documentation and/or other materials provided with the distribution.
014:
015: 3. The names of the authors may not be used to endorse or promote products
016: derived from this software without specific prior written permission.
017:
018: THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
019: INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
020: FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
021: INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
022: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
023: LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
024: OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
025: LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
026: NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
027: EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
028: */
029:
030: package com.jcraft.jsch;
031:
032: public class HostKey {
033: private static final byte[] sshdss = "ssh-dss".getBytes();
034: private static final byte[] sshrsa = "ssh-rsa".getBytes();
035:
036: protected static final int GUESS = 0;
037: public static final int SSHDSS = 1;
038: public static final int SSHRSA = 2;
039: static final int UNKNOWN = 3;
040:
041: protected String host;
042: protected int type;
043: protected byte[] key;
044:
045: public HostKey(String host, byte[] key) throws JSchException {
046: this (host, GUESS, key);
047: }
048:
049: public HostKey(String host, int type, byte[] key)
050: throws JSchException {
051: this .host = host;
052: if (type == GUESS) {
053: if (key[8] == 'd') {
054: this .type = SSHDSS;
055: } else if (key[8] == 'r') {
056: this .type = SSHRSA;
057: } else {
058: throw new JSchException("invalid key type");
059: }
060: } else {
061: this .type = type;
062: }
063: this .key = key;
064: }
065:
066: public String getHost() {
067: return host;
068: }
069:
070: public String getType() {
071: if (type == SSHDSS) {
072: return new String(sshdss);
073: }
074: if (type == SSHRSA) {
075: return new String(sshrsa);
076: }
077: return "UNKNOWN";
078: }
079:
080: public String getKey() {
081: return new String(Util.toBase64(key, 0, key.length));
082: }
083:
084: public String getFingerPrint(JSch jsch) {
085: HASH hash = null;
086: try {
087: Class c = Class.forName(jsch.getConfig("md5"));
088: hash = (HASH) (c.newInstance());
089: } catch (Exception e) {
090: System.err.println("getFingerPrint: " + e);
091: }
092: return Util.getFingerPrint(hash, key);
093: }
094:
095: boolean isMatched(String _host) {
096: return isIncluded(_host);
097: }
098:
099: private boolean isIncluded(String _host) {
100: int i = 0;
101: String hosts = this .host;
102: int hostslen = hosts.length();
103: int hostlen = _host.length();
104: int j;
105: while (i < hostslen) {
106: j = hosts.indexOf(',', i);
107: if (j == -1) {
108: if (hostlen != hostslen - i)
109: return false;
110: return hosts.regionMatches(true, i, _host, 0, hostlen);
111: }
112: if (hostlen == (j - i)) {
113: if (hosts.regionMatches(true, i, _host, 0, hostlen))
114: return true;
115: }
116: i = j + 1;
117: }
118: return false;
119: }
120: }
|