]>
Commit | Line | Data |
---|---|---|
1 | /* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */ | |
2 | /* | |
3 | Copyright (c) 2002-2010 ymnk, JCraft,Inc. All rights reserved. | |
4 | ||
5 | Redistribution and use in source and binary forms, with or without | |
6 | modification, are permitted provided that the following conditions are met: | |
7 | ||
8 | 1. Redistributions of source code must retain the above copyright notice, | |
9 | this list of conditions and the following disclaimer. | |
10 | ||
11 | 2. Redistributions in binary form must reproduce the above copyright | |
12 | notice, this list of conditions and the following disclaimer in | |
13 | the documentation and/or other materials provided with the distribution. | |
14 | ||
15 | 3. The names of the authors 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 ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, | |
19 | INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND | |
20 | FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT, | |
21 | INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT, | |
22 | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
23 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, | |
24 | OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | |
25 | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | |
26 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, | |
27 | EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
28 | */ | |
29 | ||
30 | package com.jcraft.jsch; | |
31 | ||
32 | public class HostKey{ | |
33 | private static final byte[] sshdss=Util.str2byte("ssh-dss"); | |
34 | private static final byte[] sshrsa=Util.str2byte("ssh-rsa"); | |
35 | ||
36 | protected static final int GUESS=0; | |
37 | public static final int SSHDSS=1; | |
38 | public static final int SSHRSA=2; | |
39 | static final int UNKNOWN=3; | |
40 | ||
41 | protected String host; | |
42 | protected int type; | |
43 | protected byte[] key; | |
44 | ||
45 | public HostKey(String host, byte[] key) throws JSchException { | |
46 | this(host, GUESS, key); | |
47 | } | |
48 | ||
49 | public HostKey(String host, int type, byte[] key) throws JSchException { | |
50 | this.host=host; | |
51 | if(type==GUESS){ | |
52 | if(key[8]=='d'){ this.type=SSHDSS; } | |
53 | else if(key[8]=='r'){ this.type=SSHRSA; } | |
54 | else { throw new JSchException("invalid key type");} | |
55 | } | |
56 | else{ | |
57 | this.type=type; | |
58 | } | |
59 | this.key=key; | |
60 | } | |
61 | ||
62 | public String getHost(){ return host; } | |
63 | public String getType(){ | |
64 | if(type==SSHDSS){ return Util.byte2str(sshdss); } | |
65 | if(type==SSHRSA){ return Util.byte2str(sshrsa);} | |
66 | return "UNKNOWN"; | |
67 | } | |
68 | public String getKey(){ | |
69 | return Util.byte2str(Util.toBase64(key, 0, key.length)); | |
70 | } | |
71 | public String getFingerPrint(JSch jsch){ | |
72 | HASH hash=null; | |
73 | try{ | |
74 | Class c=Class.forName(jsch.getConfig("md5")); | |
75 | hash=(HASH)(c.newInstance()); | |
76 | } | |
77 | catch(Exception e){ System.err.println("getFingerPrint: "+e); } | |
78 | return Util.getFingerPrint(hash, key); | |
79 | } | |
80 | ||
81 | boolean isMatched(String _host){ | |
82 | return isIncluded(_host); | |
83 | } | |
84 | ||
85 | private boolean isIncluded(String _host){ | |
86 | int i=0; | |
87 | String hosts=this.host; | |
88 | int hostslen=hosts.length(); | |
89 | int hostlen=_host.length(); | |
90 | int j; | |
91 | while(i<hostslen){ | |
92 | j=hosts.indexOf(',', i); | |
93 | if(j==-1){ | |
94 | if(hostlen!=hostslen-i) return false; | |
95 | return hosts.regionMatches(true, i, _host, 0, hostlen); | |
96 | } | |
97 | if(hostlen==(j-i)){ | |
98 | if(hosts.regionMatches(true, i, _host, 0, hostlen)) return true; | |
99 | } | |
100 | i=j+1; | |
101 | } | |
102 | return false; | |
103 | } | |
104 | } |