Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

KeyGripTest.java 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * Copyright (C) 2021, Thomas Wolf <thomas.wolf@paranor.ch> and others
  3. *
  4. * This program and the accompanying materials are made available under the
  5. * terms of the Eclipse Distribution License v. 1.0 which is available at
  6. * https://www.eclipse.org/org/documents/edl-v10.php.
  7. *
  8. * SPDX-License-Identifier: BSD-3-Clause
  9. */
  10. package org.eclipse.jgit.gpg.bc.internal.keys;
  11. import static org.junit.Assert.assertEquals;
  12. import static org.junit.Assert.assertTrue;
  13. import java.io.IOException;
  14. import java.io.InputStream;
  15. import java.security.Security;
  16. import java.util.Iterator;
  17. import java.util.Locale;
  18. import java.util.function.Consumer;
  19. import org.bouncycastle.jce.provider.BouncyCastleProvider;
  20. import org.bouncycastle.openpgp.PGPException;
  21. import org.bouncycastle.openpgp.PGPPublicKey;
  22. import org.bouncycastle.openpgp.PGPPublicKeyRing;
  23. import org.bouncycastle.openpgp.PGPPublicKeyRingCollection;
  24. import org.bouncycastle.openpgp.PGPUtil;
  25. import org.bouncycastle.openpgp.operator.jcajce.JcaKeyFingerprintCalculator;
  26. import org.bouncycastle.util.encoders.Hex;
  27. import org.junit.BeforeClass;
  28. import org.junit.Test;
  29. import org.junit.runner.RunWith;
  30. import org.junit.runners.Parameterized;
  31. import org.junit.runners.Parameterized.Parameter;
  32. import org.junit.runners.Parameterized.Parameters;
  33. @RunWith(Parameterized.class)
  34. public class KeyGripTest {
  35. @BeforeClass
  36. public static void ensureBC() {
  37. if (Security.getProvider(BouncyCastleProvider.PROVIDER_NAME) == null) {
  38. Security.addProvider(new BouncyCastleProvider());
  39. }
  40. }
  41. protected static class TestData {
  42. String filename;
  43. String[] expectedKeyGrips;
  44. TestData(String filename, String... keyGrips) {
  45. this.filename = filename;
  46. this.expectedKeyGrips = keyGrips;
  47. }
  48. @Override
  49. public String toString() {
  50. return filename;
  51. }
  52. }
  53. @Parameters(name = "{0}")
  54. public static TestData[] initTestData() {
  55. return new TestData[] {
  56. new TestData("rsa.asc",
  57. "D148210FAF36468055B83D0F5A6DEB83FBC8E864",
  58. "A5E4CD2CBBE44A16E4D6EC05C2E3C3A599DC763C"),
  59. new TestData("dsa-elgamal.asc",
  60. "552286BEB2999F0A9E26A50385B90D9724001187",
  61. "CED7034A8EB5F4CE90DF99147EC33D86FCD3296C"),
  62. new TestData("brainpool256.asc",
  63. "A01BAA22A72F09A0FF0A1D4CBCE70844DD52DDD7",
  64. "C1678B7DE5F144C93B89468D5F9764ACE182ED36"),
  65. new TestData("brainpool384.asc",
  66. "2F25DB025DEBF3EA2715350209B985829B04F50A",
  67. "B6BD8B81F75AF914163D97DF8DE8F6FC64C283F8"),
  68. new TestData("brainpool512.asc",
  69. "5A484F56AB4B8B6583B6365034999F6543FAE1AE",
  70. "9133E4A7E8FC8515518DF444C3F2F247EEBBADEC"),
  71. new TestData("nistp256.asc",
  72. "FC81AECE90BCE6E54D0D637D266109783AC8DAC0",
  73. "A56DC8DB8355747A809037459B4258B8A743EAB5"),
  74. new TestData("nistp384.asc",
  75. "A1338230AED1C9C125663518470B49056C9D1733",
  76. "797A83FE041FFE06A7F4B1D32C6F4AE0F6D87ADF"),
  77. new TestData("nistp521.asc",
  78. "D91B789603EC9138AA20342A2B6DC86C81B70F5D",
  79. "FD048B2CA1919CB241DC8A2C7FA3E742EF343DCA"),
  80. new TestData("secp256k1.asc",
  81. "498B89C485489BA16B40755C0EBA580166393074",
  82. "48FFED40D018747363BDEFFDD404D1F4870F8064"),
  83. new TestData("ed25519.asc",
  84. "940D97D75C306D737A59A98EAFF1272832CEDC0B"),
  85. new TestData("x25519.asc",
  86. "A77DC8173DA6BEE126F5BD6F5A14E01200B52FCE",
  87. "636C983EDB558527BA82780B52CB5DAE011BE46B")
  88. };
  89. }
  90. // Injected by JUnit
  91. @Parameter
  92. public TestData data;
  93. private void readAsc(InputStream in, Consumer<PGPPublicKey> process)
  94. throws IOException, PGPException {
  95. PGPPublicKeyRingCollection pgpPub = new PGPPublicKeyRingCollection(
  96. PGPUtil.getDecoderStream(in), new JcaKeyFingerprintCalculator());
  97. Iterator<PGPPublicKeyRing> keyRings = pgpPub.getKeyRings();
  98. while (keyRings.hasNext()) {
  99. PGPPublicKeyRing keyRing = keyRings.next();
  100. Iterator<PGPPublicKey> keys = keyRing.getPublicKeys();
  101. while (keys.hasNext()) {
  102. process.accept(keys.next());
  103. }
  104. }
  105. }
  106. @Test
  107. public void testGrip() throws Exception {
  108. try (InputStream in = this.getClass()
  109. .getResourceAsStream(data.filename)) {
  110. int index[] = { 0 };
  111. readAsc(in, key -> {
  112. byte[] keyGrip = null;
  113. try {
  114. keyGrip = KeyGrip.getKeyGrip(key);
  115. } catch (PGPException e) {
  116. throw new RuntimeException(e);
  117. }
  118. assertTrue("More keys than expected",
  119. index[0] < data.expectedKeyGrips.length);
  120. assertEquals("Wrong keygrip", data.expectedKeyGrips[index[0]++],
  121. Hex.toHexString(keyGrip).toUpperCase(Locale.ROOT));
  122. });
  123. assertEquals("Missing keys", data.expectedKeyGrips.length,
  124. index[0]);
  125. }
  126. }
  127. }