You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SecretKeysTest.java 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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.assertFalse;
  13. import static org.junit.Assert.assertNotNull;
  14. import static org.junit.Assert.assertTrue;
  15. import java.io.BufferedInputStream;
  16. import java.io.IOException;
  17. import java.io.InputStream;
  18. import java.security.Security;
  19. import java.util.Iterator;
  20. import javax.crypto.Cipher;
  21. import org.bouncycastle.jce.provider.BouncyCastleProvider;
  22. import org.bouncycastle.openpgp.PGPException;
  23. import org.bouncycastle.openpgp.PGPPublicKey;
  24. import org.bouncycastle.openpgp.PGPPublicKeyRing;
  25. import org.bouncycastle.openpgp.PGPPublicKeyRingCollection;
  26. import org.bouncycastle.openpgp.PGPSecretKey;
  27. import org.bouncycastle.openpgp.PGPUtil;
  28. import org.bouncycastle.openpgp.operator.PGPDigestCalculatorProvider;
  29. import org.bouncycastle.openpgp.operator.jcajce.JcaKeyFingerprintCalculator;
  30. import org.bouncycastle.openpgp.operator.jcajce.JcaPGPDigestCalculatorProviderBuilder;
  31. import org.junit.BeforeClass;
  32. import org.junit.Test;
  33. import org.junit.runner.RunWith;
  34. import org.junit.runners.Parameterized;
  35. import org.junit.runners.Parameterized.Parameter;
  36. import org.junit.runners.Parameterized.Parameters;
  37. @RunWith(Parameterized.class)
  38. public class SecretKeysTest {
  39. @BeforeClass
  40. public static void ensureBC() {
  41. if (Security.getProvider(BouncyCastleProvider.PROVIDER_NAME) == null) {
  42. Security.addProvider(new BouncyCastleProvider());
  43. }
  44. }
  45. private static volatile Boolean haveOCB;
  46. private static boolean ocbAvailable() {
  47. Boolean haveIt = haveOCB;
  48. if (haveIt != null) {
  49. return haveIt.booleanValue();
  50. }
  51. try {
  52. Cipher c = Cipher.getInstance("AES/OCB/NoPadding"); //$NON-NLS-1$
  53. if (c == null) {
  54. haveOCB = Boolean.FALSE;
  55. return false;
  56. }
  57. } catch (NoClassDefFoundError | Exception e) {
  58. haveOCB = Boolean.FALSE;
  59. return false;
  60. }
  61. haveOCB = Boolean.TRUE;
  62. return true;
  63. }
  64. private static class TestData {
  65. final String name;
  66. final boolean encrypted;
  67. TestData(String name, boolean encrypted) {
  68. this.name = name;
  69. this.encrypted = encrypted;
  70. }
  71. @Override
  72. public String toString() {
  73. return name;
  74. }
  75. }
  76. @Parameters(name = "{0}")
  77. public static TestData[] initTestData() {
  78. return new TestData[] {
  79. new TestData("2FB05DBB70FC07CB84C13431F640CA6CEA1DBF8A", false),
  80. new TestData("66CCECEC2AB46A9735B10FEC54EDF9FD0F77BAF9", true),
  81. new TestData("F727FAB884DA3BD402B6E0F5472E108D21033124", true),
  82. new TestData("faked", false) };
  83. }
  84. private static byte[] readTestKey(String filename) throws Exception {
  85. try (InputStream in = new BufferedInputStream(
  86. SecretKeysTest.class.getResourceAsStream(filename))) {
  87. return SecretKeys.keyFromNameValueFormat(in);
  88. }
  89. }
  90. private static PGPPublicKey readAsc(InputStream in)
  91. throws IOException, PGPException {
  92. PGPPublicKeyRingCollection pgpPub = new PGPPublicKeyRingCollection(
  93. PGPUtil.getDecoderStream(in), new JcaKeyFingerprintCalculator());
  94. Iterator<PGPPublicKeyRing> keyRings = pgpPub.getKeyRings();
  95. while (keyRings.hasNext()) {
  96. PGPPublicKeyRing keyRing = keyRings.next();
  97. Iterator<PGPPublicKey> keys = keyRing.getPublicKeys();
  98. if (keys.hasNext()) {
  99. return keys.next();
  100. }
  101. }
  102. return null;
  103. }
  104. // Injected by JUnit
  105. @Parameter
  106. public TestData data;
  107. @Test
  108. public void testKeyRead() throws Exception {
  109. byte[] bytes = readTestKey(data.name + ".key");
  110. assertEquals('(', bytes[0]);
  111. assertEquals(')', bytes[bytes.length - 1]);
  112. try (InputStream pubIn = this.getClass()
  113. .getResourceAsStream(data.name + ".asc")) {
  114. if (pubIn != null) {
  115. PGPPublicKey publicKey = readAsc(pubIn);
  116. // Do a full test trying to load the secret key.
  117. PGPDigestCalculatorProvider calculatorProvider = new JcaPGPDigestCalculatorProviderBuilder()
  118. .build();
  119. try (InputStream in = new BufferedInputStream(this.getClass()
  120. .getResourceAsStream(data.name + ".key"))) {
  121. PGPSecretKey secretKey = SecretKeys.readSecretKey(in,
  122. calculatorProvider, () -> "nonsense".toCharArray(),
  123. publicKey);
  124. assertNotNull(secretKey);
  125. } catch (PGPException e) {
  126. // Currently we may not be able to load OCB-encrypted keys.
  127. assertTrue(e.getMessage().contains("OCB"));
  128. assertTrue(data.encrypted);
  129. assertFalse(ocbAvailable());
  130. }
  131. }
  132. }
  133. }
  134. }