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  
18  package org.apache.commons.vfs2.provider.sftp;
19  
20  import com.jcraft.jsch.JSch;
21  import com.jcraft.jsch.JSchException;
22  
23  /**
24   * Structure for an identity based on byte arrays.
25   *
26   * @since 2.4
27   */
28  public class BytesIdentityInfo implements IdentityProvider {
29  
30      private final byte[] passPhrase;
31  
32      private final byte[] privateKey;
33  
34      private final byte[] publicKey;
35  
36      /**
37       * Constructs an identity info with private and passphrase for the private key.
38       *
39       * @param privateKey Private key bytes
40       * @param passPhrase The passphrase to decrypt the private key (can be {@code null} if no passphrase is used)
41       */
42      public BytesIdentityInfo(final byte[] privateKey, final byte[] passPhrase) {
43          this.privateKey = privateKey;
44          this.publicKey = null;
45          this.passPhrase = passPhrase;
46      }
47  
48      /**
49       * Constructs an identity info with private and public key and passphrase for the private key.
50       *
51       * @param privateKey Private key bytes
52       * @param publicKey  The public key part used for connections with exchange of certificates (can be {@code null})
53       * @param passPhrase The passphrase to decrypt the private key (can be {@code null} if no passphrase is used)
54       */
55      public BytesIdentityInfo(final byte[] privateKey, final byte[] publicKey, final byte[] passPhrase) {
56          this.privateKey = privateKey;
57          this.publicKey = publicKey;
58          this.passPhrase = passPhrase;
59      }
60  
61      @Override
62      public void addIdentity(final JSch jsch) throws JSchException {
63          jsch.addIdentity("PrivateKey", privateKey, publicKey, passPhrase);
64      }
65  
66      public byte[] getPassPhrase() {
67          return passPhrase;
68      }
69  
70      public byte[] getPrivateKeyBytes() {
71          return privateKey;
72      }
73  
74      public byte[] getPublicKeyBytes() {
75          return publicKey;
76      }
77  }