Changeset 36349 in osm for applications/editors/josm/plugins/trustosm/src/org/openstreetmap
- Timestamp:
- 2024-10-28T19:18:41+01:00 (3 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/trustosm/src/org/openstreetmap/josm/plugins/trustosm/util/TrustGPG.java
r34599 r36349 7 7 import java.awt.Dimension; 8 8 import java.awt.GridBagLayout; 9 import java.awt.event.ActionEvent;10 import java.awt.event.ActionListener;11 9 import java.awt.event.ComponentAdapter; 12 10 import java.awt.event.ComponentEvent; … … 15 13 import java.io.FileOutputStream; 16 14 import java.io.IOException; 17 import java.nio.charset.Charset;18 15 import java.nio.charset.StandardCharsets; 19 16 import java.security.KeyPair; … … 106 103 private PGPSecretKeyRingCollection pgpSec; 107 104 private PGPPublicKeyRingCollection pgpPub; 108 private static int digest = PGPUtil.SHA1; 105 private static final int digest = PGPUtil.SHA1; 109 106 private PGPSecretKey pgpSecKey; 110 107 public boolean keepkey = false; … … 122 119 123 120 public PGPPublicKey getPublicKeyFromRing(long keyID) { 124 try { 125 if (pgpPub.contains(keyID)) 126 return pgpPub.getPublicKey(keyID); 127 else if (pgpSec.contains(keyID)) 128 return pgpSec.getSecretKey(keyID).getPublicKey(); 129 } catch (PGPException e) { 130 System.err.println("Could not read a PGPPublic key from your KeyRingCollectionFile. Stacktrace:"); 131 e.printStackTrace(); 121 if (pgpPub.contains(keyID)) { 122 return pgpPub.getPublicKey(keyID); 123 } else if (pgpSec.contains(keyID)) { 124 return pgpSec.getSecretKey(keyID).getPublicKey(); 132 125 } 133 126 return null; … … 216 209 JButton detailsButton = new JButton(tr("Details"), ImageProvider.get("keydetails")); 217 210 detailsButton.setAlignmentX(Component.LEFT_ALIGNMENT); 218 detailsButton.addActionListener(new ActionListener() { 219 @Override 220 public void actionPerformed(ActionEvent arg0) { 221 PGPSecretKey sk = sigKeys.get(keyBox.getSelectedIndex()); 222 showKeyDetails(getPublicKeyFromRing(sk.getKeyID())); 223 } }); 211 detailsButton.addActionListener(arg0 -> { 212 PGPSecretKey sk = sigKeys.get(keyBox.getSelectedIndex()); 213 showKeyDetails(getPublicKeyFromRing(sk.getKeyID())); 214 }); 224 215 p.add(detailsButton); 225 216 … … 232 223 JButton createButton = new JButton(tr("Create new Key"), ImageProvider.get("key")); 233 224 createButton.setAlignmentX(Component.LEFT_ALIGNMENT); 234 createButton.addActionListener(new ActionListener() { 235 236 @Override 237 public void actionPerformed(ActionEvent arg0) { 238 try { 239 PGPSecretKey secKey = generateKey(); 240 if (secKey != null) { 241 keyBox.addItem(secKeytoString(secKey)); 242 sigKeys.add(secKey); 243 } 244 } catch (NoSuchAlgorithmException | NoSuchProviderException | PGPException | IOException e) { 245 Logging.error(e); 225 createButton.addActionListener(arg0 -> { 226 try { 227 PGPSecretKey secKey = generateKey(); 228 if (secKey != null) { 229 keyBox.addItem(secKeytoString(secKey)); 230 sigKeys.add(secKey); 246 231 } 247 } }); 232 } catch (NoSuchAlgorithmException | NoSuchProviderException | PGPException | IOException e) { 233 Logging.error(e); 234 } 235 }); 248 236 p.add(createButton); 249 237 p.add(Box.createRigidArea(d)); … … 265 253 } 266 254 267 public void readGpgFiles() throws PGPException, IOException , NoSuchAlgorithmException, NoSuchProviderException{255 public void readGpgFiles() throws PGPException, IOException { 268 256 FileInputStream pubIn; 269 257 FileInputStream secIn; … … 283 271 } 284 272 285 public void writeGpgFiles() throws FileNotFoundException,IOException {273 public void writeGpgFiles() throws IOException { 286 274 String path = Preferences.main().getPluginsDirectory().getPath(); 287 275 try (FileOutputStream pubOut = new FileOutputStream(path + "/trustosm/gnupg/pubring.gpg"); … … 450 438 */ 451 439 public static double searchTolerance(PGPSignature sig) { 452 /* *Take the first NotationData packet that seems to have Tolerance information */440 /* Take the first NotationData packet that seems to have Tolerance information */ 453 441 for (NotationData nd : sig.getHashedSubPackets().getNotationDataOccurrences()) { 454 442 if (nd.getNotationName().equals(TrustGPG.NOTATION_DATA_KEY)) { … … 470 458 471 459 JFormattedTextField meters = new JFormattedTextField(NumberFormat.getNumberInstance()); 472 meters.setValue( Double.valueOf(10));460 meters.setValue(10.0); 473 461 meters.setColumns(5); 474 462 … … 480 468 481 469 if (n == JOptionPane.OK_OPTION) { 482 spGen. setNotationData(false, true, TrustGPG.NOTATION_DATA_KEY, "Tolerance:"+meters.getValue()+"m");470 spGen.addNotationData(false, true, TrustGPG.NOTATION_DATA_KEY, "Tolerance:" + meters.getValue() + "m"); 483 471 return spGen; 484 472 } … … 512 500 if (web.isSelected()) sources += ":web"; 513 501 if (trusted.isSelected()) sources += ":trusted"; 514 spGen. setNotationData(false, true, TrustGPG.NOTATION_DATA_KEY, sources);502 spGen.addNotationData(false, true, TrustGPG.NOTATION_DATA_KEY, sources); 515 503 return spGen; 516 504 } … … 548 536 Iterator<?> it = pgpSecKey.getPublicKey().getUserIDs(); 549 537 if (it.hasNext()) { 550 spGen. setSignerUserID(false, (String) it.next());538 spGen.addSignerUserID(false, (String) it.next()); 551 539 } 552 540 sGen.setHashedSubpackets(spGen.generate()); 553 sGen.update(tosign.getBytes(Charset .forName("UTF-8")));541 sGen.update(tosign.getBytes(StandardCharsets.UTF_8)); 554 542 sig = sGen.generate(); 555 543 //System.out.println(new String(sGen.generateOnePassVersion(false).getEncoded(),Charset.forName("UTF-8"))); … … 586 574 PGPContentVerifierBuilderProvider contentVerifierBuilderProvider = new JcaPGPContentVerifierBuilderProvider().setProvider(provider); 587 575 sig.init(contentVerifierBuilderProvider, pgpPub.getPublicKey(sig.getKeyID())); 588 sig.update(sigtext.getBytes(Charset .forName("UTF-8")));576 sig.update(sigtext.getBytes(StandardCharsets.UTF_8)); 589 577 return sig.verify(); 590 578 } catch (Exception e) { … … 630 618 String keyid = "0x"+Long.toHexString(key.getKeyID()).substring(8).toUpperCase(Locale.ENGLISH); 631 619 632 String algorithm = "";620 final String algorithm; 633 621 int algo = key.getAlgorithm(); 634 622 switch(algo) { … … 637 625 case PGPPublicKey.DSA: 638 626 algorithm = "Digital Signature Algorithm (DSA)"; break; 639 case PGPPublicKey.EC: 627 case PGPPublicKey.ECDH: 640 628 algorithm = "Elliptic Curve (EC)"; break; 641 629 case PGPPublicKey.ECDSA: … … 703 691 704 692 public PGPSecretKey generateKey() 705 throws NoSuchAlgorithmException, NoSuchProviderException, PGPException, FileNotFoundException,IOException {693 throws NoSuchAlgorithmException, NoSuchProviderException, PGPException, IOException { 706 694 707 695 JTextField userId = new JTextField(); … … 738 726 final String[] algos = {"DSA", "RSA"}; 739 727 final JComboBox<?> algoBox = new JComboBox<Object>(algos); 740 algoBox.addActionListener(new ActionListener() { 741 742 @Override 743 public void actionPerformed(ActionEvent e) { 744 JComboBox<?> cb = (JComboBox<?>) e.getSource(); 745 String alg = (String) cb.getSelectedItem(); 746 if (alg.equals("DSA")) { 747 strengthBox.setSelectedItem("1024"); 748 strengthBox.setEnabled(false); 749 } else 750 strengthBox.setEnabled(true); 751 /*if (alg.equals("ECDSA")) { 752 curveBox.setEnabled(true); 753 strengthBox.setModel(new DefaultComboBoxModel(curvesizes)); 754 strengthBox.setSelectedItem(curvesizes[curveBox.getSelectedIndex()]); 755 strengthBox.setEnabled(false); 756 } else { 757 curveBox.setEnabled(false); 758 strengthBox.setModel(new DefaultComboBoxModel(sizes)); 759 strengthBox.setEnabled(true); 760 }*/ 761 } 728 algoBox.addActionListener(e -> { 729 JComboBox<?> cb = (JComboBox<?>) e.getSource(); 730 String alg = (String) cb.getSelectedItem(); 731 if ("DSA".equals(alg)) { 732 strengthBox.setSelectedItem("1024"); 733 strengthBox.setEnabled(false); 734 } else 735 strengthBox.setEnabled(true); 736 /*if (alg.equals("ECDSA")) { 737 curveBox.setEnabled(true); 738 strengthBox.setModel(new DefaultComboBoxModel(curvesizes)); 739 strengthBox.setSelectedItem(curvesizes[curveBox.getSelectedIndex()]); 740 strengthBox.setEnabled(false); 741 } else { 742 curveBox.setEnabled(false); 743 strengthBox.setModel(new DefaultComboBoxModel(sizes)); 744 strengthBox.setEnabled(true); 745 }*/ 762 746 }); 763 747 … … 836 820 837 821 PGPSignatureSubpacketVector subPck = null; 838 PGPSignatureSubpacketGenerator spGen = null;839 822 Date expire = cal.getDate(); 840 823 if (expire != null && expire.after(now)) { 841 spGen = new PGPSignatureSubpacketGenerator(); 824 final PGPSignatureSubpacketGenerator spGen = new PGPSignatureSubpacketGenerator(); 842 825 spGen.setKeyExpirationTime(true, (expire.getTime()-now.getTime())/1000); 843 826 subPck = spGen.generate();
Note:
See TracChangeset
for help on using the changeset viewer.