1 package SnowMailClient.gnupg.Main; 2 3 import java.util.*; 4 import java.io.*; 5 import javax.swing.*; 6 import javax.swing.event.*; 7 import javax.swing.border.*; 8 import java.awt.*; 9 import java.awt.event.*; 10 11 import SnowMailClient.utils.*; 12 13 import snow.utils.gui.*; 14 import snow.crypto.*; 15 import snow.concurrent.*; 16 17 import SnowMailClient.utils.storage.*; 18 import SnowMailClient.gnupg.LineProcessors.*; 19 import SnowMailClient.gnupg.model.*; 20 import SnowMailClient.crypto.*; 21 import SnowMailClient.Language.Language; 22 23 24 26 public final class GnuPGCommands 27 { 28 29 private GnuPGCommands() 30 { 31 32 } 34 36 public static Vector<GnuPGKeyID> readPublicKeyIDs(String pathToGPG) throws Exception 37 { 38 if(!new File(pathToGPG).exists()) throw new Exception (Language.translate("GnuPG path invalid")); 39 Process p = Runtime.getRuntime().exec(new String []{pathToGPG, "--no-greeting", "--with-fingerprint", "--fixed-list-mode", "--with-colons", "--list-public-keys"}); 40 41 ErrorLineProcessor errorProcessor = new ErrorLineProcessor(); 43 StreamLineGobbler errGobbler = new StreamLineGobbler(p.getErrorStream(), errorProcessor); 44 errGobbler.start(); 45 46 KeyIDReader keyIDLineProcessor = new KeyIDReader(); 47 StreamLineGobbler inGobbler = new StreamLineGobbler(p.getInputStream(), keyIDLineProcessor); 48 inGobbler.start(); 49 50 int rep = p.waitFor(); 51 try 52 { 53 inGobbler.join(5000); 54 } catch(Exception e) {} 55 56 try 57 { 58 errGobbler.join(5000); 59 } catch(Exception e) {} 60 61 if(errorProcessor.hasError()) throw new Exception (errorProcessor.getAllErrorMessage()); 62 63 return keyIDLineProcessor.getKeyIDs(); 64 } 65 66 67 public static Vector<GnuPGKeyID> readSecretKeyIDs(String pathToGPG) throws Exception 68 { 69 if(!new File(pathToGPG).exists()) throw new Exception (Language.translate("GnuPG path invalid")); 70 Process p = Runtime.getRuntime().exec(new String []{pathToGPG, "--no-greeting", "--with-fingerprint", "--fixed-list-mode", "--with-colons", "--list-secret-keys"}); 71 72 ErrorLineProcessor errorProcessor = new ErrorLineProcessor(); 74 StreamLineGobbler errGobbler = new StreamLineGobbler(p.getErrorStream(), errorProcessor); 75 errGobbler.start(); 76 77 KeyIDReader keyIDLineProcessor = new KeyIDReader(); 78 StreamLineGobbler inGobbler = new StreamLineGobbler(p.getInputStream(), keyIDLineProcessor); 79 inGobbler.start(); 80 81 int rep = p.waitFor(); 82 83 try 84 { 85 inGobbler.join(5000); 86 } catch(Exception e) {} 87 88 try 89 { 90 errGobbler.join(5000); 91 } catch(Exception e) {} 92 93 if(errorProcessor.hasError()) throw new Exception (errorProcessor.getAllErrorMessage()); 94 95 return keyIDLineProcessor.getKeyIDs(); 96 } 97 98 100 public static String readGPGVersion(String pathToGPG) throws Exception 101 { 102 if(!new File(pathToGPG).exists()) throw new Exception (Language.translate("GnuPG path invalid")); 103 Process p = Runtime.getRuntime().exec(new String []{pathToGPG, "--version"}); 104 105 ErrorLineProcessor errorProcessor = new ErrorLineProcessor(); 107 StreamLineGobbler errGobbler = new StreamLineGobbler(p.getErrorStream(), errorProcessor); 108 errGobbler.start(); 109 110 VersionLineProcessor vlp = new VersionLineProcessor(); 111 StreamLineGobbler inGobbler = new StreamLineGobbler(p.getInputStream(), vlp); 112 inGobbler.start(); 113 114 int rep = p.waitFor(); 115 116 try 117 { 118 inGobbler.join(5000); 119 } catch(Exception e) {} 120 121 try 122 { 123 errGobbler.join(5000); 124 } catch(Exception e) {} 125 126 if(errorProcessor.hasError()) throw new Exception (errorProcessor.getAllErrorMessage()); 128 129 131 return vlp.getVersion(); } 133 134 135 136 138 public static String encryptBufferForRecipient( 139 String pathToGPG, 140 String message, 141 GnuPGKeyID keyID, 142 boolean ascii, 143 Interrupter interrupter) throws Exception 144 { 145 147 Process p = Runtime.getRuntime().exec(new String []{ 148 pathToGPG, "-e"+(ascii?"a":""), 149 "--quiet", "--yes", "--batch", "--no-greeting", 150 "--trust-model", "always", "-r", keyID.getFingerprint() 152 }); 153 154 if(interrupter!=null) 155 { 156 interrupter.setProcessToOptionalKill(p); 157 } 158 159 160 ErrorLineProcessor errorProcessor = new ErrorLineProcessor(); 162 StreamLineGobbler errGobbler = new StreamLineGobbler(p.getErrorStream(), errorProcessor); 163 errGobbler.start(); 164 165 ByteArrayOutputStream bos = new ByteArrayOutputStream(); 166 StreamGobbler inGobbler = new StreamGobbler(p.getInputStream(), bos); 167 inGobbler.start(); 168 169 ByteArrayInputStream bin = new ByteArrayInputStream(message.getBytes()); 171 byte[] buf = new byte[256]; 172 int read =-1; 173 while(( read=bin.read(buf))!=-1) 174 { 175 p.getOutputStream().write(buf,0,read); 176 } 177 178 try 180 { 181 p.getOutputStream().flush(); 182 p.getOutputStream().close(); 183 } 184 catch(Exception ignored) {} 185 186 int rep = p.waitFor(); 187 188 try 189 { 190 inGobbler.join(5000); 191 } catch(Exception e) {} 192 193 try 194 { 195 errGobbler.join(5000); 196 } catch(Exception e) {} 197 198 if(errorProcessor.hasError()) throw new Exception (errorProcessor.getAllErrorMessage()); 199 203 if(errorProcessor.hasText("failed")) throw new Exception (errorProcessor.getAllErrorMessage()); 204 205 return new String (bos.toByteArray()); 206 } 207 208 209 public static boolean verifySecretKeyPassword(String pathToGPG, GnuPGKeyID keyID, byte[] pass) throws Exception 210 { 211 try 212 { 213 ByteArrayInputStream bin = new ByteArrayInputStream("Hello this is a test".getBytes()); 214 byte[] result = signBuffer(pathToGPG, bin, keyID, pass, null); 215 if(result==null) return false; 216 217 return true; 218 } 219 catch(BadPasswordException be) 220 { 221 return false; 222 } 223 catch(Exception e) 224 { 225 throw e; 226 } 227 } 228 229 230 231 235 public static byte[] signBuffer(String pathToGPG, ByteArrayInputStream bin, GnuPGKeyID keyID, byte[] pass, Interrupter interrupter 236 ) throws Exception 237 { 238 Process p = Runtime.getRuntime().exec(new String []{ 239 pathToGPG, "--clearsign", "--sign", "-a", 240 "--quiet", "--yes", "--no-greeting", 241 "-u", "\""+keyID.getKeyID()+"\"", 242 "--passphrase-fd", "0" }); 244 245 if(interrupter!=null) interrupter.setProcessToOptionalKill(p); 246 247 ErrorLineProcessor errorProcessor = new ErrorLineProcessor(); 248 StreamLineGobbler errGobbler = new StreamLineGobbler(p.getErrorStream(), errorProcessor); 249 errGobbler.start(); 250 251 ByteArrayOutputStream bos = new ByteArrayOutputStream(); 252 StreamGobbler inGobbler = new StreamGobbler(p.getInputStream(), bos); 253 inGobbler.start(); 254 255 p.getOutputStream().write(pass); 257 p.getOutputStream().write((byte)'\n'); 258 p.getOutputStream().flush(); 259 260 byte[] buf = new byte[256]; 262 int read =-1; 263 while(( read=bin.read(buf))!=-1) 264 { 265 p.getOutputStream().write(buf,0,read); 266 } 267 268 p.getOutputStream().flush(); 270 p.getOutputStream().close(); 271 272 int rep = p.waitFor(); 273 274 try 275 { 276 inGobbler.join(5000); 277 } catch(Exception e) {} 278 279 try 280 { 281 errGobbler.join(5000); 282 } catch(Exception e) {} 283 284 if(errorProcessor.hasText("bad passphrase")) 285 { 286 throw new BadPasswordException(Language.translate("Bad passphrase for %",keyID.getKeyID())); 287 } 288 289 if(errorProcessor.hasError()) throw new Exception (errorProcessor.getAllErrorMessage()); 290 291 return bos.toByteArray(); 292 } 293 294 303 304 305 307 public static Vector<SignatureVerificationResult> verifySignature( 308 String pathToGPG, InputStream signedMessage, 309 ProgressModalDialog progress) throws Exception 310 { 311 Process p = Runtime.getRuntime().exec(new String []{ 312 pathToGPG, "--no-greeting", "--quiet", "--batch", "--fixed-list-mode", "--with-colons", "--verify",}); 313 314 if(progress!=null) progress.setProcessToOptionalKill(p); 315 316 318 ErrorLineProcessor errorProcessor = new ErrorLineProcessor(); 319 StreamLineGobbler errGobbler = new StreamLineGobbler(p.getErrorStream(), errorProcessor); 320 errGobbler.start(); 321 322 ByteArrayOutputStream bos = new ByteArrayOutputStream(); 323 StreamGobbler inGobbler = new StreamGobbler(p.getInputStream(), bos); 324 inGobbler.start(); 325 326 byte[] buf = new byte[256]; 328 int read =-1; 329 while(( read=signedMessage.read(buf))!=-1) 330 { 331 p.getOutputStream().write( buf, 0, read); 332 } 333 334 p.getOutputStream().flush(); 336 p.getOutputStream().close(); 337 338 int rep = p.waitFor(); 339 340 try 341 { 342 inGobbler.join(5000); 343 } catch(Exception e) {} 344 345 try 346 { 347 errGobbler.join(5000); 348 } catch(Exception e) {} 349 350 Vector<SignatureVerificationResult> signatures = parseSignaturesFromOutput(errorProcessor.getAllErrorMessage()); 353 354 if(errorProcessor.hasText("invalid")) throw new Exception (errorProcessor.getAllErrorMessage()); 355 if(errorProcessor.hasText("CRC error")) throw new Exception (errorProcessor.getAllErrorMessage()); 356 357 if(errorProcessor.hasError()) throw new Exception (errorProcessor.getAllErrorMessage()); 358 359 return signatures; 360 } 361 362 363 private static Vector<SignatureVerificationResult> parseSignaturesFromOutput(String text) throws Exception 364 { 365 System.out.println("\n========= Parse signature verif from ========\n"+text+"\n\n"); 366 BufferedReader br = new BufferedReader(new StringReader(text)); 367 Vector<SignatureVerificationResult> signatures = new Vector<SignatureVerificationResult>(); 368 369 SignatureVerificationResult actual = null; 370 String line = null; 371 while((line = br.readLine())!=null) 372 { 373 if(line.indexOf("Signature made")>=0) 374 { 375 if(actual!=null) signatures.addElement(actual); 377 actual = new SignatureVerificationResult(); 378 actual.parseLine(line); 379 } 380 else 381 { 382 if(actual!=null) actual.parseLine(line); 383 } 384 } 385 386 if(actual!=null) signatures.addElement(actual); 387 388 return signatures; 389 } 390 391 392 394 public static String decryptBufferForRecipient(String pathToGPG, 395 String encryptedMessage, GnuPGKeyID keyID, byte[] pass, 396 ProgressModalDialog progress) throws Exception 397 { 398 Process p = Runtime.getRuntime().exec(new String []{ 399 pathToGPG, 400 "--no-greeting", 402 "--passphrase-fd", "0", "--decrypt" 405 }); 406 407 if(progress!=null) 408 { 409 progress.setProcessToOptionalKill(p); 410 } 411 412 ErrorLineProcessor errorProcessor = new ErrorLineProcessor(); 413 StreamLineGobbler errGobbler = new StreamLineGobbler(p.getErrorStream(), errorProcessor); 414 errGobbler.start(); 415 416 ByteArrayOutputStream bos = new ByteArrayOutputStream(); 417 StreamGobbler inGobbler = new StreamGobbler(p.getInputStream(), bos); 418 inGobbler.start(); 419 420 p.getOutputStream().write(pass); 422 p.getOutputStream().write((byte)'\n'); 423 p.getOutputStream().flush(); 424 425 ByteArrayInputStream bin = new ByteArrayInputStream(encryptedMessage.getBytes()); 427 byte[] buf = new byte[256]; 428 int read =-1; 429 while(( read=bin.read(buf))!=-1) 430 { 431 p.getOutputStream().write(buf,0,read); 432 } 433 434 p.getOutputStream().flush(); 436 p.getOutputStream().close(); 437 438 int rep = p.waitFor(); 439 440 try 441 { 442 inGobbler.join(5000); 443 } catch(Exception e) {} 444 445 try 446 { 447 errGobbler.join(5000); 448 } catch(Exception e) {} 449 450 if(errorProcessor.hasError()) throw new Exception (errorProcessor.getAllErrorMessage()); 451 if(errorProcessor.hasText("failed")) throw new Exception (errorProcessor.getAllErrorMessage()); 452 453 return new String (bos.toByteArray()); 454 } 455 456 457 464 public static void setTrust(String pathToGPG, 465 GnuPGKeyID keyID, int newTrust, 466 ProgressModalDialog progress) throws Exception 467 { 468 System.out.println("Set trust to "+newTrust+" for "+keyID.getMails()); 469 470 Process p = Runtime.getRuntime().exec(new String []{ 471 pathToGPG, "--yes", "--yes", 472 "--no-comments", "--no-greeting", 473 "--command-fd", "0", 474 "--edit-key", keyID.getFingerprint() 475 }); 476 477 if(progress!=null) 478 { 479 progress.setProcessToOptionalKill(p); 480 } 481 482 ErrorLineProcessor errorProcessor = new ErrorLineProcessor(); 483 StreamLineGobbler errGobbler = new StreamLineGobbler(p.getErrorStream(), errorProcessor); 484 errGobbler.start(); 485 486 ByteArrayOutputStream bos = new ByteArrayOutputStream(); 487 StreamGobbler inGobbler = new StreamGobbler(p.getInputStream(), bos); 488 inGobbler.start(); 489 490 491 p.getOutputStream().write("trust".getBytes()); 493 p.getOutputStream().write((byte)'\n'); 494 p.getOutputStream().flush(); 495 496 p.getOutputStream().write((""+newTrust).getBytes()); 497 p.getOutputStream().write((byte)'\n'); 498 p.getOutputStream().flush(); 499 500 if(newTrust==5) 501 { 502 p.getOutputStream().write("y".getBytes()); 503 p.getOutputStream().write((byte)'\n'); 504 p.getOutputStream().flush(); 505 } 506 507 p.getOutputStream().write("save".getBytes()); 508 p.getOutputStream().write((byte)'\n'); 509 p.getOutputStream().flush(); 510 511 p.getOutputStream().close(); 512 513 int rep = p.waitFor(); 514 515 try 516 { 517 inGobbler.join(5000); 518 } catch(Exception e) {} 519 520 try 521 { 522 errGobbler.join(5000); 523 } catch(Exception e) {} 524 525 526 if(errorProcessor.hasError()) throw new Exception (errorProcessor.getAllErrorMessage()); 527 if(errorProcessor.hasText("failed")) throw new Exception (errorProcessor.getAllErrorMessage()); 528 529 System.out.println("SetTrust err message="+errorProcessor.getAllErrorMessage()); 530 System.out.println("SetTrust response="+new String (bos.toByteArray())); 531 532 } 533 534 538 public static String [] determineUsedKey(String pathToGPG, 539 String encryptedMessage, 540 ProgressModalDialog progress) throws Exception 541 { 542 Process p = Runtime.getRuntime().exec(new String []{ 543 pathToGPG, 544 "--no-greeting", 546 "--passphrase-fd", "0", "--decrypt" 548 }); 549 550 if(progress!=null) 551 { 552 progress.setProcessToOptionalKill(p); 553 } 554 555 ErrorLineProcessor errorProcessor = new ErrorLineProcessor(); 556 StreamLineGobbler errGobbler = new StreamLineGobbler(p.getErrorStream(), errorProcessor); 557 errGobbler.start(); 558 559 ByteArrayOutputStream bos = new ByteArrayOutputStream(); 560 StreamGobbler inGobbler = new StreamGobbler(p.getInputStream(), bos); 561 inGobbler.start(); 562 563 p.getOutputStream().write("---------------------------ç*@\n".getBytes()); p.getOutputStream().flush(); 566 567 ByteArrayInputStream bin = new ByteArrayInputStream(encryptedMessage.getBytes()); 569 byte[] buf = new byte[256]; 570 int read =-1; 571 while(( read=bin.read(buf))!=-1) 572 { 573 p.getOutputStream().write(buf,0,read); 574 } 575 576 p.getOutputStream().flush(); 578 p.getOutputStream().close(); 579 580 int rep = p.waitFor(); 581 582 try 583 { 584 inGobbler.join(5000); 585 } catch(Exception e) {} 586 587 try 588 { 589 errGobbler.join(5000); 590 } catch(Exception e) {} 591 592 String erm = errorProcessor.getAllErrorMessage(); int pos = erm.indexOf("ID "); 596 if(pos==-1) throw new Exception (Language.translate("Cannot find key ID in")+erm); 597 int posEnd=erm.indexOf(",", pos+3); 598 String ID = erm.substring(pos+3,posEnd); 599 601 pos = erm.indexOf(" \""); 602 if(pos==-1) throw new Exception (Language.translate("Cannot find key name in")+erm); 603 posEnd=erm.indexOf("\"", pos+3); 604 String name = erm.substring(pos+3,posEnd); 605 606 return new String []{ID, name}; 607 } 608 609 610 public static String getPublicKey(String pathToGPG, GnuPGKeyID id) throws Exception 611 { 612 Process p = Runtime.getRuntime().exec(new String []{ 613 pathToGPG, "--armor", "--export", id.getKeyID() 614 }); 615 616 617 ErrorLineProcessor errorProcessor = new ErrorLineProcessor(); 619 StreamLineGobbler errGobbler = new StreamLineGobbler(p.getErrorStream(), errorProcessor); 620 errGobbler.start(); 621 622 ByteArrayOutputStream bos = new ByteArrayOutputStream(); 623 StreamGobbler inGobbler = new StreamGobbler(p.getInputStream(), bos); 624 inGobbler.start(); 625 626 int rep = p.waitFor(); 627 try 628 { 629 inGobbler.join(5000); 630 } catch(Exception e) {} 631 632 try 633 { 634 errGobbler.join(5000); 635 } catch(Exception e) {} 636 637 if(errorProcessor.hasError()) throw new Exception (errorProcessor.getAllErrorMessage()); 638 if(errorProcessor.hasText("warning")) throw new Exception (errorProcessor.getAllErrorMessage()); 639 640 return new String (bos.toByteArray()); 641 } 642 643 644 public static String getSecretKey(String pathToGPG, GnuPGKeyID id) throws Exception 645 { 646 Process p = Runtime.getRuntime().exec(new String []{ 647 pathToGPG, "--armor", "--export-secret-keys", id.getKeyID() 648 }); 649 650 651 ErrorLineProcessor errorProcessor = new ErrorLineProcessor(); 653 StreamLineGobbler errGobbler = new StreamLineGobbler(p.getErrorStream(), errorProcessor); 654 errGobbler.start(); 655 656 ByteArrayOutputStream bos = new ByteArrayOutputStream(); 657 StreamGobbler inGobbler = new StreamGobbler(p.getInputStream(), bos); 658 inGobbler.start(); 659 660 int rep = p.waitFor(); 661 try 662 { 663 inGobbler.join(5000); 664 } catch(Exception e) {} 665 666 try 667 { 668 errGobbler.join(5000); 669 } catch(Exception e) {} 670 671 if(errorProcessor.hasError()) throw new Exception (errorProcessor.getAllErrorMessage()); 672 673 return new String (bos.toByteArray()); 674 } 675 676 677 public static void addKey(String pathToGPG, String asciikey) throws Exception 678 { 679 Process p = Runtime.getRuntime().exec(new String []{ 680 pathToGPG, "--batch", "--quiet", "--yes", "--import" 681 }); 682 683 684 ErrorLineProcessor errorProcessor = new ErrorLineProcessor(); 686 StreamLineGobbler errGobbler = new StreamLineGobbler(p.getErrorStream(), errorProcessor); 687 errGobbler.start(); 688 689 ByteArrayOutputStream bos = new ByteArrayOutputStream(); 690 StreamGobbler inGobbler = new StreamGobbler(p.getInputStream(), bos); 691 inGobbler.start(); 692 693 p.getOutputStream().write(asciikey.getBytes()); 695 p.getOutputStream().write("\r\n".getBytes()); 696 p.getOutputStream().flush(); 698 p.getOutputStream().close(); 699 700 701 int rep = p.waitFor(); 702 try 703 { 704 inGobbler.join(5000); 705 } catch(Exception e) {} 706 707 try 708 { 709 errGobbler.join(5000); 710 } catch(Exception e) {} 711 712 if(errorProcessor.hasError()) throw new Exception (errorProcessor.getAllErrorMessage()); 713 714 System.out.println("in: "+new String (bos.toByteArray())); 715 } 716 717 public static void removePublicKey(String pathToGPG, GnuPGKeyID id) throws Exception 718 { 719 Process p = Runtime.getRuntime().exec(new String [] { 720 pathToGPG, "--yes", "--batch", "--delete-key", id.getFingerprint().replaceAll(" ", "")}); 721 722 ErrorLineProcessor errorProcessor = new ErrorLineProcessor(); 724 StreamLineGobbler errGobbler = new StreamLineGobbler(p.getErrorStream(), errorProcessor); 725 errGobbler.start(); 726 727 SimpleLineProcessor simpleLineProcessor = new SimpleLineProcessor("Out: "); 729 StreamLineGobbler inGobbler = new StreamLineGobbler(p.getInputStream(), simpleLineProcessor); 730 inGobbler.start(); 731 732 int rep = p.waitFor(); 733 734 if(errorProcessor.hasError()) throw new Exception (errorProcessor.getAllErrorMessage()); 735 if(errorProcessor.hasText("failed")) throw new Exception (errorProcessor.getAllErrorMessage()); 736 737 } 738 739 740 public static void removePublicAndPrivateKey(String pathToGPG, GnuPGKeyID id) throws Exception 741 { 742 Process p = Runtime.getRuntime().exec(new String [] { 743 pathToGPG, "--no-greeting", "--yes", "--batch", "--delete-secret-and-public-key", id.getFingerprint().replaceAll(" ", "")}); 744 745 ErrorLineProcessor errorProcessor = new ErrorLineProcessor(); 747 StreamLineGobbler errGobbler = new StreamLineGobbler(p.getErrorStream(), errorProcessor); 748 errGobbler.start(); 749 750 SimpleLineProcessor simpleLineProcessor = new SimpleLineProcessor("Out: "); 752 StreamLineGobbler inGobbler = new StreamLineGobbler(p.getInputStream(), simpleLineProcessor); 753 inGobbler.start(); 754 755 int rep = p.waitFor(); 756 try 757 { 758 inGobbler.join(5000); 759 } catch(Exception e) {} 760 761 try 762 { 763 errGobbler.join(5000); 764 } catch(Exception e) {} 765 766 if(errorProcessor.hasError()) throw new Exception (errorProcessor.getAllErrorMessage()); 767 if(errorProcessor.hasText("failed")) throw new Exception (errorProcessor.getAllErrorMessage()); 768 769 } 770 771 894 895 897 public static void generateNew_DSA_ElG_KeyPair(String pathToGPG, 898 String nameReal, String nameComment, String nameEmail, String passphrase, String elgKeyLength, String expires, 899 ProgressModalDialog progress) throws Exception 900 { 901 Process p = Runtime.getRuntime().exec(new String [] { 902 pathToGPG, "--batch", "--gen-key"}); 903 904 if(progress!=null) 905 progress.setProcessToOptionalKill(p); 906 907 ErrorLineProcessor errorProcessor = new ErrorLineProcessor(); 909 StreamLineGobbler errGobbler = new StreamLineGobbler(p.getErrorStream(), errorProcessor); 910 errGobbler.start(); 911 912 ByteArrayOutputStream bos = new ByteArrayOutputStream(); 913 StreamGobbler inGobbler = new StreamGobbler(p.getInputStream(), bos); 914 inGobbler.start(); 915 916 p.getOutputStream().write(("Key-Type: DSA").getBytes()); 919 p.getOutputStream().write(("\r\nKey-Length: 1024").getBytes()); p.getOutputStream().write( "\r\nSubkey-Type: ELG-E".getBytes()); 921 p.getOutputStream().write(("\r\nSubkey-Length: "+elgKeyLength).getBytes()); 923 p.getOutputStream().write(("\r\nName-Real: " +nameReal).getBytes()); 924 p.getOutputStream().write(("\r\nName-Comment: "+nameComment).getBytes()); 925 p.getOutputStream().write(("\r\nName-Email: " +nameEmail).getBytes()); 926 927 p.getOutputStream().write(("\r\nExpire-Date: "+expires).getBytes()); 928 p.getOutputStream().write(("\r\nPassphrase: "+passphrase).getBytes()); 929 932 p.getOutputStream().flush(); 934 p.getOutputStream().close(); 935 936 937 int rep = p.waitFor(); 938 try 939 { 940 inGobbler.join(5000); 941 } catch(Exception e) {} 942 943 try 944 { 945 errGobbler.join(5000); 946 } catch(Exception e) {} 947 948 if(errorProcessor.hasError()) throw new Exception (errorProcessor.getAllErrorMessage()); 949 if(errorProcessor.hasText("invalid")) throw new Exception (errorProcessor.getAllErrorMessage()); 950 if(errorProcessor.hasText("missing")) throw new Exception (errorProcessor.getAllErrorMessage()); 951 } 952 953 954 public static void sendPublicKey(String pathToGPG, GnuPGKeyID id, ProgressModalDialog progress) throws Exception 955 { 956 Process p = Runtime.getRuntime().exec(new String [] { 957 958 pathToGPG, 959 "--no-greeting", 960 "--send-keys", 962 "--keyserver", "subkeys.pgp.net", 963 id.getFingerprint().replaceAll(" ", "")}); 964 965 if(progress!=null) 966 progress.setProcessToOptionalKill(p); 967 968 ErrorLineProcessor errorProcessor = new ErrorLineProcessor(); 970 StreamLineGobbler errGobbler = new StreamLineGobbler(p.getErrorStream(), errorProcessor); 971 errGobbler.start(); 972 973 SimpleLineProcessor simpleLineProcessor = new SimpleLineProcessor("Out: "); 975 StreamLineGobbler inGobbler = new StreamLineGobbler(p.getInputStream(), simpleLineProcessor); 976 inGobbler.start(); 977 978 p.getOutputStream().flush(); 983 p.getOutputStream().close(); 984 985 int rep = p.waitFor(); 986 try 987 { 988 inGobbler.join(5000); 989 } catch(Exception e) {} 990 991 try 992 { 993 errGobbler.join(5000); 994 } catch(Exception e) {} 995 996 997 if(errorProcessor.hasError()) throw new Exception (errorProcessor.getAllErrorMessage()); 998 if(errorProcessor.hasText("failed")) throw new Exception (errorProcessor.getAllErrorMessage()); 999 1000 } 1001 1002 public static Vector<KeyIDFromSearchResult> searchKeysOnServer(String pathToGPG, String text, ProgressModalDialog progress) throws Exception 1003 { 1004 Process p = Runtime.getRuntime().exec(new String [] { 1005 1006 pathToGPG, "--yes", 1007 "--no-greeting", 1009 "--keyserver", "hkp://subkeys.pgp.net", 1010 "--search-keys", text 1011 }); 1012 1013 if(progress!=null) 1014 { 1015 progress.setProcessToOptionalKill(p); 1016 } 1017 1018 ErrorLineProcessor errorProcessor = new ErrorLineProcessor(); 1020 StreamLineGobbler errGobbler = new StreamLineGobbler(p.getErrorStream(), errorProcessor); 1021 errGobbler.start(); 1022 1023 KeySearchResultProcessor keyProcessor = new KeySearchResultProcessor(); 1025 StreamLineGobbler inGobbler = new StreamLineGobbler(p.getInputStream(), keyProcessor); 1026 inGobbler.start(); 1027 1028 1032 1033 int rep = p.waitFor(); 1034 try 1035 { 1036 inGobbler.join(5000); 1037 } catch(Exception e) {} 1038 1039 1040 try 1041 { 1042 errGobbler.join(5000); 1043 } catch(Exception e) {} 1044 1045 if(errorProcessor.hasError()) throw new Exception (errorProcessor.getAllErrorMessage()); 1046 1050 return keyProcessor.getKeyIDs(); 1051 } 1052 1053 public static void importKeyFromServer(String pathToGPG, KeyIDFromSearchResult key, ProgressModalDialog progress) throws Exception 1054 { 1055 Process p = Runtime.getRuntime().exec(new String [] { 1056 1057 pathToGPG, "--yes", "--batch", 1058 "--keyserver", "hkp://subkeys.pgp.net", 1059 "--recv-keys", key.getKeyShortID() 1060 }); 1061 1062 if(progress!=null) 1063 { 1064 progress.setProcessToOptionalKill(p); 1065 } 1066 1067 ErrorLineProcessor errorProcessor = new ErrorLineProcessor(); 1069 StreamLineGobbler errGobbler = new StreamLineGobbler(p.getErrorStream(), errorProcessor); 1070 errGobbler.start(); 1071 1072 StreamLineGobbler inGobbler = new StreamLineGobbler(p.getInputStream(), errorProcessor); 1075 inGobbler.start(); 1076 1077 1081 1082 int rep = p.waitFor(); 1083 try 1084 { 1085 inGobbler.join(5000); 1086 } catch(Exception e) {} 1087 1088 1089 try 1090 { 1091 errGobbler.join(5000); 1092 } catch(Exception e) {} 1093 1094 if(errorProcessor.hasError()) throw new Exception (errorProcessor.getAllErrorMessage()); 1095 1097 } 1098 1099 1100 public static void refreshPublicKeysFromServer(String pathToGPG, ProgressModalDialog progress) throws Exception 1101 { 1102 Process p = Runtime.getRuntime().exec(new String [] { 1103 pathToGPG, "--yes", "--batch", "--no-greeting", 1104 "--keyserver", "hkp://subkeys.pgp.net", 1105 "--refresh-keys" 1106 }); 1107 1108 if(progress!=null) progress.setProcessToOptionalKill(p); 1109 1110 ErrorLineProcessor errorProcessor = new ErrorLineProcessor(); 1112 StreamLineGobbler errGobbler = new StreamLineGobbler(p.getErrorStream(), errorProcessor); 1113 errGobbler.start(); 1114 1115 StreamLineGobbler inGobbler = new StreamLineGobbler(p.getInputStream(), errorProcessor); 1117 inGobbler.start(); 1118 1119 1123 int rep = p.waitFor(); 1124 try 1125 { 1126 inGobbler.join(5000); 1127 } catch(Exception e) {} 1128 1129 1130 try 1131 { 1132 errGobbler.join(5000); 1133 } catch(Exception e) {} 1134 1135 if(errorProcessor.hasError()) throw new Exception (errorProcessor.getAllErrorMessage()); 1136 if(errorProcessor.hasText("failed")) throw new Exception (errorProcessor.getAllErrorMessage()); 1137 } 1138 1139 1140 1141 1145 public static void symmetricEncryptFile(String pathToGPG, File src, File dest, char[] pass, boolean ascii) throws Exception 1146 { 1147 1148 Process p = Runtime.getRuntime().exec(new String [] { 1149 pathToGPG, "-c"+(ascii?"a":""), 1150 "--no-greeting", 1151 "-o", dest.getAbsolutePath(), 1153 "--passphrase-fd", "0", src.getAbsolutePath()} ); 1155 1156 ErrorLineProcessor errorProcessor = new ErrorLineProcessor(); 1158 StreamLineGobbler errGobbler = new StreamLineGobbler(p.getErrorStream(), errorProcessor); 1159 errGobbler.start(); 1160 1161 SimpleLineProcessor simpleLineProcessor = new SimpleLineProcessor("Out: "); 1163 StreamLineGobbler inGobbler = new StreamLineGobbler(p.getInputStream(), simpleLineProcessor); 1164 inGobbler.start(); 1165 1166 for(int i=0; i<pass.length; i++) 1168 { 1169 p.getOutputStream().write((byte) pass[i]); 1170 } 1173 p.getOutputStream().write((byte)'\n'); 1174 p.getOutputStream().flush(); 1175 1176 int rep = p.waitFor(); 1177 1178 if(errorProcessor.hasError()) throw new Exception (errorProcessor.getAllErrorMessage()); 1179 } 1180 1181 1182 public static void encryptFile(String pathToGPG, File src, File dest, GnuPGKeyID keyID, boolean ascii) throws Exception 1183 { 1184 Process p = Runtime.getRuntime().exec(new String []{ 1185 pathToGPG, "-e"+(ascii?"a":""), 1186 "--enable-progress-filter", 1187 "-r", "\""+keyID.getKeyID()+"\"", 1188 "-o", dest.getAbsolutePath(), 1189 src.getAbsolutePath()}); 1190 1191 1192 ErrorLineProcessor errorProcessor = new ErrorLineProcessor(); 1194 StreamLineGobbler errGobbler = new StreamLineGobbler(p.getErrorStream(), errorProcessor); 1195 errGobbler.start(); 1196 1197 SimpleLineProcessor simpleLineProcessor = new SimpleLineProcessor("Out: "); 1198 StreamLineGobbler inGobbler = new StreamLineGobbler(p.getInputStream(), simpleLineProcessor); 1199 inGobbler.start(); 1200 1201 int rep = p.waitFor(); 1202 1203 if(errorProcessor.hasError()) throw new Exception (errorProcessor.getAllErrorMessage()); 1204 } 1205 1206 1207 public static byte[] encryptBuffer(String pathToGPG, ByteArrayInputStream bin, char[] pass, boolean ascii) throws Exception 1208 { 1209 Process p = Runtime.getRuntime().exec(new String []{ 1210 pathToGPG, "-c"+(ascii?"a":""), 1211 "--quiet", "--no-greeting", 1212 "--passphrase-fd", "0" 1215 }); 1216 1217 1218 ErrorLineProcessor errorProcessor = new ErrorLineProcessor(); 1220 StreamLineGobbler errGobbler = new StreamLineGobbler(p.getErrorStream(), errorProcessor); 1221 errGobbler.start(); 1222 1223 ByteArrayOutputStream bos = new ByteArrayOutputStream(); 1224 StreamGobbler inGobbler = new StreamGobbler(p.getInputStream(), bos); 1225 inGobbler.start(); 1226 1227 for(int i=0; i<pass.length; i++) 1229 { 1230 p.getOutputStream().write((byte) pass[i]); 1231 } 1234 p.getOutputStream().write((byte)'\n'); 1235 p.getOutputStream().flush(); 1236 1237 byte[] buf = new byte[256]; 1239 int read =-1; 1240 while(( read=bin.read(buf))!=-1) 1241 { 1242 p.getOutputStream().write(buf,0,read); 1243 } 1244 1245 p.getOutputStream().flush(); 1247 p.getOutputStream().close(); 1248 1249 int rep = p.waitFor(); 1250 try 1251 { 1252 inGobbler.join(5000); 1253 } catch(Exception e) {} 1254 1255 try 1256 { 1257 errGobbler.join(5000); 1258 } catch(Exception e) {} 1259 1260 if(errorProcessor.hasError()) throw new Exception (errorProcessor.getAllErrorMessage()); 1261 1262 return bos.toByteArray(); 1263 } 1264 1265 1266 1267 public static void encryptBuffer(String pathToGPG, InputStream bin, char[] pass, OutputStream bos, boolean ascii, ProgressModalDialog pd) throws Exception 1268 { 1269 Process p = Runtime.getRuntime().exec(new String []{ 1270 pathToGPG, "-c"+(ascii?"a":""), 1271 "--quiet", "--no-greeting", 1272 "--passphrase-fd", "0" }); 1274 1275 1276 ErrorLineProcessor errorProcessor = new ErrorLineProcessor(); 1278 StreamLineGobbler errGobbler = new StreamLineGobbler(p.getErrorStream(), errorProcessor); 1279 errGobbler.start(); 1280 1281 StreamGobbler inGobbler = new StreamGobbler(p.getInputStream(), bos); 1282 inGobbler.start(); 1283 1284 for(int i=0; i<pass.length; i++) 1286 { 1287 p.getOutputStream().write((byte) pass[i]); 1288 } 1291 p.getOutputStream().write((byte)'\n'); 1292 p.getOutputStream().flush(); 1293 1295 byte[] buf = new byte[256]; 1297 int read =-1; 1298 while(( read=bin.read(buf))!=-1) 1299 { 1300 p.getOutputStream().write(buf,0,read); 1301 pd.incrementProgress(read); 1302 1303 if(pd.wasCancelled()) 1304 { 1305 p.destroy(); 1306 throw new Exception ("Encryption Aborted"); 1308 } 1309 } 1310 System.out.println("wrote mess"); 1311 1312 p.getOutputStream().flush(); 1314 p.getOutputStream().close(); 1315 1316 try 1320 { 1321 inGobbler.join(5000); 1322 } catch(Exception e) {} 1323 1324 try 1325 { 1326 errGobbler.join(5000); 1327 } catch(Exception e) {} 1328 1329 if(errorProcessor.hasError()) throw new Exception (errorProcessor.getAllErrorMessage()); 1330 1331 } 1332 1333 1334 public static byte[] decryptBuffer(String pathToGPG, ByteArrayInputStream bin, char[] pass) throws Exception 1335 { 1336 Process p = Runtime.getRuntime().exec(new String []{ 1337 pathToGPG, 1338 "--enable-progress-filter", "--no-greeting", 1339 "--quiet", 1340 "-d", 1341 "--passphrase-fd", "0", }); 1343 1344 1345 ErrorLineProcessor errorProcessor = new ErrorLineProcessor(); 1347 StreamLineGobbler errGobbler = new StreamLineGobbler(p.getErrorStream(), errorProcessor); 1348 errGobbler.start(); 1349 1350 ByteArrayOutputStream bos = new ByteArrayOutputStream(); 1351 StreamGobbler inGobbler = new StreamGobbler(p.getInputStream(), bos); 1352 inGobbler.start(); 1353 1354 for(int i=0; i<pass.length; i++) 1356 { 1357 p.getOutputStream().write((byte) pass[i]); 1358 } 1361 p.getOutputStream().write((byte)'\n'); 1362 p.getOutputStream().flush(); 1363 1365 byte[] buf = new byte[256]; 1367 int read =-1; 1368 while(( read=bin.read(buf))!=-1) 1369 { 1370 p.getOutputStream().write(buf,0,read); 1371 } 1372 1374 p.getOutputStream().flush(); 1376 p.getOutputStream().close(); 1377 1378 try 1381 { 1382 inGobbler.join(5000); 1383 } catch(Exception e) {} 1384 1385 try 1386 { 1387 errGobbler.join(5000); 1388 } catch(Exception e) {} 1389 1390 String err = errorProcessor.getAllErrorMessage(); 1391 String err_ = err.toLowerCase(); 1392 if(err.indexOf("bad key")>=0) throw new Exception (Language.translate("Bad key")); 1393 if(err.indexOf("failed")>=0) throw new Exception (err); 1394 if(err.indexOf("no valid")>=0) throw new Exception (err); 1395 1396 return bos.toByteArray(); 1397 } 1398 1399 1400 1402 public static String infoBuffer(String pathToGPG, ByteArrayInputStream bin) throws Exception 1403 { 1404 Process p = Runtime.getRuntime().exec(new String [] 1405 { 1406 pathToGPG, 1407 "--decrypt", 1408 "--verbose", 1409 "--no-greeting", 1411 "--passphrase-fd", "0", }); 1413 1414 1415 ErrorLineProcessor errorProcessor = new ErrorLineProcessor(); 1417 StreamLineGobbler errGobbler = new StreamLineGobbler(p.getErrorStream(), errorProcessor); 1418 errGobbler.start(); 1419 1420 ByteArrayOutputStream bos = new ByteArrayOutputStream(); 1421 StreamGobbler inGobbler = new StreamGobbler(p.getInputStream(), bos); 1422 inGobbler.start(); 1423 1424 1432 p.getOutputStream().write((byte)'\n'); 1434 p.getOutputStream().flush(); 1435 1436 1438 byte[] buf = new byte[1024]; 1440 int read =-1; 1441 while(( read=bin.read(buf))!=-1) 1442 { 1443 p.getOutputStream().write(buf,0,read); 1444 } 1445 1447 1448 p.getOutputStream().flush(); 1450 p.getOutputStream().close(); 1451 1452 try 1456 { 1457 inGobbler.join(5000); 1458 } catch(Exception e) {} 1459 1460 try 1461 { 1462 errGobbler.join(5000); 1463 } catch(Exception e) {} 1464 1465 String err = errorProcessor.getAllErrorMessage(); 1466 System.out.println("ERROR=\n"+err); 1467 1468 1473 return new String (bos.toByteArray()); 1474 } 1475 1476 1477 public static void decryptFile(String pathToGPG, File src, File dest, char[] pass) throws Exception 1478 { 1479 Process p = Runtime.getRuntime().exec(new String [] { 1480 pathToGPG, 1481 "--enable-progress-filter", 1482 "--quiet", "--no-greeting", 1483 "-o",dest.getAbsolutePath(), 1484 "--passphrase-fd", "0", "-d", 1486 src.getAbsolutePath()}); 1487 1488 System.out.println("Decrypt Started"); 1489 1490 ErrorLineProcessor errorProcessor = new ErrorLineProcessor(); 1492 StreamLineGobbler errGobbler = new StreamLineGobbler(p.getErrorStream(), errorProcessor); 1493 errGobbler.start(); 1494 1495 SimpleLineProcessor simpleLineProcessor = new SimpleLineProcessor("Out: "); 1496 StreamLineGobbler inGobbler = new StreamLineGobbler(p.getInputStream(), simpleLineProcessor); 1497 inGobbler.start(); 1498 1499 p.getOutputStream().write((new String (pass)+"\n").getBytes()); 1501 p.getOutputStream().flush(); 1502 1503 1504 System.out.println("Wait"); 1505 int rep = p.waitFor(); 1506 System.out.println("OK"); 1507 1508 String err = errorProcessor.getAllErrorMessage(); 1509 String err_ = err.toLowerCase(); 1510 if(err.indexOf("bad key")>=0) throw new Exception (Language.translate("Bad key")); 1511 if(err.indexOf("failed")>=0) throw new Exception (err); 1512 if(err.indexOf("no valid")>=0) throw new Exception (err); 1513 1514 } 1516 1517} | Popular Tags |