예제 #1
0
파일: SSHMain.java 프로젝트: jexp/idea2
 /**
  * Try public key
  *
  * @param c a ssh connection
  * @param keyPath a path to key
  * @return true if authentication is successful
  */
 private boolean tryPublicKey(final Connection c, final String keyPath) {
   try {
     final File file = new File(keyPath);
     if (file.exists()) {
       // if encrypted ask user for passphrase
       String passphrase = null;
       char[] text = FileUtil.loadFileText(file);
       if (isEncryptedKey(text)) {
         // need to ask passphrase from user
         int i;
         for (i = myHost.getNumberOfPasswordPrompts(); i > 0; i--) {
           passphrase =
               myXmlRpcClient.askPassphrase(
                   myHandlerNo, getUserHostString(), keyPath, myLastError);
           if (passphrase == null) {
             // if no passphrase was entered, just return false and try something other
             return false;
           } else {
             try {
               PEMDecoder.decode(text, passphrase);
               myLastError = "";
             } catch (IOException e) {
               // decoding failed
               myLastError = GitBundle.message("sshmain.invalidpassphrase", keyPath);
               continue;
             }
             break;
           }
         }
         if (i == 0) {
           myLastError =
               GitBundle.message(
                   "sshmain.too.mush.passphrase.guesses",
                   keyPath,
                   myHost.getNumberOfPasswordPrompts());
           return false;
         }
       }
       // try authentication
       if (c.authenticateWithPublicKey(myHost.getUser(), text, passphrase)) {
         myLastError = "";
         return true;
       } else {
         if (passphrase != null) {
           // mark as failed authentication only if passphrase were asked
           myLastError = GitBundle.message("sshmain.pk.authenitication.failed", keyPath);
         } else {
           myLastError = "";
         }
       }
     }
     return false;
   } catch (Exception e) {
     return false;
   }
 }
예제 #2
0
파일: CLI.java 프로젝트: Surgo/jenkins
  public static KeyPair loadKey(String pemString) throws IOException, GeneralSecurityException {
    Object key = PEMDecoder.decode(pemString.toCharArray(), null);
    if (key instanceof com.trilead.ssh2.signature.RSAPrivateKey) {
      com.trilead.ssh2.signature.RSAPrivateKey x = (com.trilead.ssh2.signature.RSAPrivateKey) key;
      //            System.out.println("ssh-rsa " + new
      // String(Base64.encode(RSASHA1Verify.encodeSSHRSAPublicKey(x.getPublicKey()))));

      return x.toJCEKeyPair();
    }
    if (key instanceof com.trilead.ssh2.signature.DSAPrivateKey) {
      com.trilead.ssh2.signature.DSAPrivateKey x = (com.trilead.ssh2.signature.DSAPrivateKey) key;
      KeyFactory kf = KeyFactory.getInstance("DSA");
      //            System.out.println("ssh-dsa " + new
      // String(Base64.encode(DSASHA1Verify.encodeSSHDSAPublicKey(x.getPublicKey()))));

      return new KeyPair(
          kf.generatePublic(new DSAPublicKeySpec(x.getY(), x.getP(), x.getQ(), x.getG())),
          kf.generatePrivate(new DSAPrivateKeySpec(x.getX(), x.getP(), x.getQ(), x.getG())));
    }

    throw new UnsupportedOperationException("Unrecognizable key format: " + key);
  }