1 /* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
2 /*
3 Copyright (c) 2002-2018 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 import java.io.*;
33 
34 class IdentityFile implements Identity{
35   private JSch jsch;
36   private KeyPair kpair;
37   private String identity;
38 
newInstance(String prvfile, String pubfile, JSch jsch)39   static IdentityFile newInstance(String prvfile, String pubfile, JSch jsch) throws JSchException{
40     KeyPair kpair = KeyPair.load(jsch, prvfile, pubfile);
41     return new IdentityFile(jsch, prvfile, kpair);
42   }
43 
newInstance(String name, byte[] prvkey, byte[] pubkey, JSch jsch)44   static IdentityFile newInstance(String name, byte[] prvkey, byte[] pubkey, JSch jsch) throws JSchException{
45 
46     KeyPair kpair = KeyPair.load(jsch, prvkey, pubkey);
47     return new IdentityFile(jsch, name, kpair);
48   }
49 
IdentityFile(JSch jsch, String name, KeyPair kpair)50   private IdentityFile(JSch jsch, String name, KeyPair kpair) throws JSchException{
51     this.jsch = jsch;
52     this.identity = name;
53     this.kpair = kpair;
54   }
55 
56   /**
57    * Decrypts this identity with the specified pass-phrase.
58    * @param passphrase the pass-phrase for this identity.
59    * @return <tt>true</tt> if the decryption is succeeded
60    * or this identity is not cyphered.
61    */
setPassphrase(byte[] passphrase)62   public boolean setPassphrase(byte[] passphrase) throws JSchException{
63     return kpair.decrypt(passphrase);
64   }
65 
66   /**
67    * Returns the public-key blob.
68    * @return the public-key blob
69    */
getPublicKeyBlob()70   public byte[] getPublicKeyBlob(){
71     return kpair.getPublicKeyBlob();
72   }
73 
74   /**
75    * Signs on data with this identity, and returns the result.
76    * @param data data to be signed
77    * @return the signature
78    */
getSignature(byte[] data)79   public byte[] getSignature(byte[] data){
80     return kpair.getSignature(data);
81   }
82 
83   /**
84    * @deprecated This method should not be invoked.
85    * @see #setPassphrase(byte[] passphrase)
86    */
decrypt()87   public boolean decrypt(){
88     throw new RuntimeException("not implemented");
89   }
90 
91   /**
92    * Returns the name of the key algorithm.
93    * @return "ssh-rsa" or "ssh-dss"
94    */
getAlgName()95   public String getAlgName(){
96     byte[] name = kpair.getKeyTypeName();
97     try {
98       return new String(name, "UTF-8");
99     }
100     catch (UnsupportedEncodingException e){
101       return new String(name);
102     }
103   }
104 
105   /**
106    * Returns the name of this identity.
107    * It will be useful to identify this object in the {@link IdentityRepository}.
108    */
getName()109   public String getName(){
110     return identity;
111   }
112 
113   /**
114    * Returns <tt>true</tt> if this identity is cyphered.
115    * @return <tt>true</tt> if this identity is cyphered.
116    */
isEncrypted()117   public boolean isEncrypted(){
118     return kpair.isEncrypted();
119   }
120 
121   /**
122    * Disposes internally allocated data, like byte array for the private key.
123    */
clear()124   public void clear(){
125     kpair.dispose();
126     kpair = null;
127   }
128 
129   /**
130    * Returns an instance of {@link KeyPair} used in this {@link Identity}.
131    * @return an instance of {@link KeyPair} used in this {@link Identity}.
132    */
getKeyPair()133   public KeyPair getKeyPair(){
134     return kpair;
135   }
136 }
137