JSch will accept ciphered keys, but some implementations of IdentityRepository can not. For example, IdentityRepository for ssh-agent and pageant only accept plain keys. The following class has been introduced to cache ciphered keys for them, and pass them whenever they are de-ciphered.
| 50 | * whenever they are de-ciphered. |
| 51 | */ |
| 52 | static class Wrapper implements IdentityRepository { |
| 53 | private IdentityRepository ir; |
| 54 | private Vector cache = new Vector(); |
| 55 | private boolean keep_in_cache = false; |
| 56 | Wrapper(IdentityRepository ir){ |
| 57 | this(ir, false); |
| 58 | } |
| 59 | Wrapper(IdentityRepository ir, boolean keep_in_cache){ |
| 60 | this.ir = ir; |
| 61 | this.keep_in_cache = keep_in_cache; |
| 62 | } |
| 63 | public String getName() { |
| 64 | return ir.getName(); |
| 65 | } |
| 66 | public int getStatus() { |
| 67 | return ir.getStatus(); |
| 68 | } |
| 69 | public boolean add(byte[] identity) { |
| 70 | return ir.add(identity); |
| 71 | } |
| 72 | public boolean remove(byte[] blob) { |
| 73 | return ir.remove(blob); |
| 74 | } |
| 75 | public void removeAll() { |
| 76 | cache.removeAllElements(); |
| 77 | ir.removeAll(); |
| 78 | } |
| 79 | public Vector getIdentities() { |
| 80 | Vector result = new Vector(); |
| 81 | for(int i = 0; i< cache.size(); i++){ |
| 82 | Identity identity = (Identity)(cache.elementAt(i)); |
| 83 | result.add(identity); |
| 84 | } |
| 85 | Vector tmp = ir.getIdentities(); |
| 86 | for(int i = 0; i< tmp.size(); i++){ |
| 87 | result.add(tmp.elementAt(i)); |
| 88 | } |
| 89 | return result; |
| 90 | } |
| 91 | void add(Identity identity) { |
| 92 | if(!keep_in_cache && |
| 93 | !identity.isEncrypted() && (identity instanceof IdentityFile)) { |
| 94 | try { |
| 95 | ir.add(((IdentityFile)identity).getKeyPair().forSSHAgent()); |
| 96 | } |
| 97 | catch(JSchException e){ |
| 98 | // an exception will not be thrown. |
| 99 | } |
| 100 | } |
| 101 | else |
| 102 | cache.addElement(identity); |
| 103 | } |
| 104 | void check() { |
| 105 | if(cache.size() > 0){ |
| 106 | Object[] identities = cache.toArray(); |
| 107 | for(int i = 0; i < identities.length; i++){ |
| 108 | Identity identity = (Identity)(identities[i]); |
| 109 | cache.removeElement(identity); |
nothing calls this directly
no outgoing calls
no test coverage detected