1 /* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
2 /*
3 Copyright (c) 2002-2015 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 
34   private static final byte[][] names = {
35     Util.str2byte("ssh-dss"),
36     Util.str2byte("ssh-rsa"),
37     Util.str2byte("ecdsa-sha2-nistp256"),
38     Util.str2byte("ecdsa-sha2-nistp384"),
39     Util.str2byte("ecdsa-sha2-nistp521")
40   };
41 
42   protected static final int GUESS=0;
43   public static final int SSHDSS=1;
44   public static final int SSHRSA=2;
45   public static final int ECDSA256=3;
46   public static final int ECDSA384=4;
47   public static final int ECDSA521=5;
48   static final int UNKNOWN=6;
49 
50   protected String marker;
51   protected String host;
52   protected int type;
53   protected byte[] key;
54   protected String comment;
55 
HostKey(String host, byte[] key)56   public HostKey(String host, byte[] key) throws JSchException {
57     this(host, GUESS, key);
58   }
59 
HostKey(String host, int type, byte[] key)60   public HostKey(String host, int type, byte[] key) throws JSchException {
61     this(host, type, key, null);
62   }
HostKey(String host, int type, byte[] key, String comment)63   public HostKey(String host, int type, byte[] key, String comment) throws JSchException {
64     this("", host, type, key, comment);
65   }
HostKey(String marker, String host, int type, byte[] key, String comment)66   public HostKey(String marker, String host, int type, byte[] key, String comment) throws JSchException {
67     this.marker=marker;
68     this.host=host;
69     if(type==GUESS){
70       if(key[8]=='d'){ this.type=SSHDSS; }
71       else if(key[8]=='r'){ this.type=SSHRSA; }
72       else if(key[8]=='a' && key[20]=='2'){ this.type=ECDSA256; }
73       else if(key[8]=='a' && key[20]=='3'){ this.type=ECDSA384; }
74       else if(key[8]=='a' && key[20]=='5'){ this.type=ECDSA521; }
75       else { throw new JSchException("invalid key type");}
76     }
77     else{
78       this.type=type;
79     }
80     this.key=key;
81     this.comment=comment;
82   }
83 
getHost()84   public String getHost(){ return host; }
getType()85   public String getType(){
86     if(type==SSHDSS ||
87        type==SSHRSA ||
88        type==ECDSA256 ||
89        type==ECDSA384 ||
90        type==ECDSA521){
91       return Util.byte2str(names[type-1]);
92     }
93     return "UNKNOWN";
94   }
name2type(String name)95   protected static int name2type(String name){
96     for(int i = 0; i < names.length; i++){
97       if(Util.byte2str(names[i]).equals(name)){
98         return i + 1;
99       }
100     }
101     return UNKNOWN;
102   }
getKey()103   public String getKey(){
104     return Util.byte2str(Util.toBase64(key, 0, key.length));
105   }
getFingerPrint(JSch jsch)106   @SuppressWarnings({"static"}) public String getFingerPrint(JSch jsch){
107     HASH hash=null;
108     try{
109       Class c=Class.forName(jsch.getConfig("md5"));
110       hash=(HASH)(c.newInstance());
111     }
112     catch(Exception e){ System.err.println("getFingerPrint: "+e); }
113     return Util.getFingerPrint(hash, key);
114   }
getComment()115   public String getComment(){ return comment; }
getMarker()116   public String getMarker(){ return marker; }
117 
isMatched(String _host)118   boolean isMatched(String _host){
119     return isIncluded(_host);
120   }
121 
isIncluded(String _host)122   private boolean isIncluded(String _host){
123     int i=0;
124     String hosts=this.host;
125     int hostslen=hosts.length();
126     int hostlen=_host.length();
127     int j;
128     while(i<hostslen){
129       j=hosts.indexOf(',', i);
130       if(j==-1){
131        if(hostlen!=hostslen-i) return false;
132        return hosts.regionMatches(true, i, _host, 0, hostlen);
133       }
134       if(hostlen==(j-i)){
135 	if(hosts.regionMatches(true, i, _host, 0, hostlen)) return true;
136       }
137       i=j+1;
138     }
139     return false;
140   }
141 }
142