001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.vfs2.provider.sftp;
018
019import java.io.File;
020import java.util.Arrays;
021import java.util.Objects;
022
023import com.jcraft.jsch.JSch;
024import com.jcraft.jsch.JSchException;
025
026/**
027 * Structure for an identity based on Files.
028 *
029 * @since 2.1
030 */
031public class IdentityInfo implements IdentityProvider {
032
033    private final byte[] passphrase;
034    private final File privateKey;
035    private final File publicKey;
036
037    /**
038     * Constructs an identity info with private key.
039     * <p>
040     * The key is not passphrase protected.
041     * </p>
042     * <p>
043     * We use java.io.File because JSch cannot deal with VFS FileObjects.
044     * </p>
045     *
046     * @param privateKey The file with the private key
047     * @since 2.1
048     */
049    public IdentityInfo(final File privateKey) {
050        this(privateKey, null, null);
051    }
052
053    /**
054     * Constructs an identity info with private key and its passphrase.
055     * <p>
056     * We use java.io.File because JSch cannot deal with VFS FileObjects.
057     * </p>
058     *
059     * @param privateKey The file with the private key
060     * @param passphrase The passphrase to decrypt the private key (can be {@code null} if no passphrase is used)
061     * @since 2.1
062     */
063    public IdentityInfo(final File privateKey, final byte[] passphrase) {
064        this(privateKey, null, passphrase);
065    }
066
067    /**
068     * Constructs an identity info with private and public key and passphrase for the private key.
069     * <p>
070     * We use java.io.File because JSch cannot deal with VFS FileObjects.
071     * </p>
072     *
073     * @param privateKey The file with the private key
074     * @param publicKey  The public key part used for connections with exchange of certificates (can be {@code null})
075     * @param passphrase The passphrase to decrypt the private key (can be {@code null} if no passphrase is used)
076     * @since 2.1
077     */
078    public IdentityInfo(final File privateKey, final File publicKey, final byte[] passphrase) {
079        this.privateKey = getAbsoluteFile(privateKey);
080        this.publicKey = getAbsoluteFile(publicKey);
081        this.passphrase = Utils.clone(passphrase);
082    }
083
084    /**
085     * @since 2.4
086     */
087    @Override
088    public void addIdentity(final JSch jsch) throws JSchException {
089        jsch.addIdentity(getAbsolutePath(privateKey), getAbsolutePath(publicKey), passphrase);
090    }
091
092    @Override
093    public boolean equals(final Object obj) {
094        if (this == obj) {
095            return true;
096        }
097        if (!(obj instanceof IdentityInfo)) {
098            return false;
099        }
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}