View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.vfs2.provider.sftp;
18  
19  import java.io.File;
20  import java.util.Arrays;
21  import java.util.Objects;
22  
23  import com.jcraft.jsch.JSch;
24  import com.jcraft.jsch.JSchException;
25  
26  /**
27   * Structure for an identity based on Files.
28   *
29   * @since 2.1
30   */
31  public class IdentityInfo implements IdentityProvider {
32  
33      private final byte[] passphrase;
34      private final File privateKey;
35      private final File publicKey;
36  
37      /**
38       * Constructs an identity info with private key.
39       * <p>
40       * The key is not passphrase protected.
41       * </p>
42       * <p>
43       * We use java.io.File because JSch cannot deal with VFS FileObjects.
44       * </p>
45       *
46       * @param privateKey The file with the private key
47       * @since 2.1
48       */
49      public IdentityInfo(final File privateKey) {
50          this(privateKey, null, null);
51      }
52  
53      /**
54       * Constructs an identity info with private key and its passphrase.
55       * <p>
56       * We use java.io.File because JSch cannot deal with VFS FileObjects.
57       * </p>
58       *
59       * @param privateKey The file with the private key
60       * @param passphrase The passphrase to decrypt the private key (can be {@code null} if no passphrase is used)
61       * @since 2.1
62       */
63      public IdentityInfo(final File privateKey, final byte[] passphrase) {
64          this(privateKey, null, passphrase);
65      }
66  
67      /**
68       * Constructs an identity info with private and public key and passphrase for the private key.
69       * <p>
70       * We use java.io.File because JSch cannot deal with VFS FileObjects.
71       * </p>
72       *
73       * @param privateKey The file with the private key
74       * @param publicKey  The public key part used for connections with exchange of certificates (can be {@code null})
75       * @param passphrase The passphrase to decrypt the private key (can be {@code null} if no passphrase is used)
76       * @since 2.1
77       */
78      public IdentityInfo(final File privateKey, final File publicKey, final byte[] passphrase) {
79          this.privateKey = getAbsoluteFile(privateKey);
80          this.publicKey = getAbsoluteFile(publicKey);
81          this.passphrase = Utils.clone(passphrase);
82      }
83  
84      /**
85       * @since 2.4
86       */
87      @Override
88      public void addIdentity(final JSch jsch) throws JSchException {
89          jsch.addIdentity(getAbsolutePath(privateKey), getAbsolutePath(publicKey), passphrase);
90      }
91  
92      @Override
93      public boolean equals(final Object obj) {
94          if (this == obj) {
95              return true;
96          }
97          if (!(obj instanceof IdentityInfo)) {
98              return false;
99          }
100         final IdentityInfo other = (IdentityInfo) obj;
101         return Arrays.equals(passphrase, other.passphrase) && Objects.equals(privateKey, other.privateKey) && Objects.equals(publicKey, other.publicKey);
102     }
103 
104     private File getAbsoluteFile(final File privateKey) {
105         return privateKey != null ? privateKey.getAbsoluteFile() : null;
106     }
107 
108     private String getAbsolutePath(final File file) {
109         return file != null ? file.getAbsolutePath() : null;
110     }
111 
112     /**
113      * Gets the passphrase of the private key.
114      *
115      * @return the passphrase
116      * @since 2.10.0
117      */
118     public byte[] getPassphrase() {
119         return Utils.clone(passphrase);
120     }
121 
122     /**
123      * Gets the passphrase of the private key.
124      *
125      * @return the passphrase
126      * @since 2.1
127      * @deprecated Use {@link #getPassphrase()}.
128      */
129     @Deprecated
130     public byte[] getPassPhrase() {
131         return Utils.clone(passphrase);
132     }
133 
134     /**
135      * Gets the file with the private key.
136      *
137      * @return the file
138      * @since 2.1
139      */
140     public File getPrivateKey() {
141         return privateKey;
142     }
143 
144     /**
145      * Gets the file with the public key.
146      *
147      * @return the file
148      * @since 2.1
149      */
150     public File getPublicKey() {
151         return publicKey;
152     }
153 
154     @Override
155     public int hashCode() {
156         final int prime = 31;
157         int result = 1;
158         result = prime * result + Arrays.hashCode(passphrase);
159         return prime * result + Objects.hash(privateKey, publicKey);
160     }
161 }