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.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. final boolean keyValue;
  68. TestData(String name, boolean encrypted, boolean keyValue) {
  69. this.name = name;
  70. this.encrypted = encrypted;
  71. this.keyValue = keyValue;
  72. }
  73. @Override
  74. public String toString() {
  75. return name;
  76. }
  77. }
  78. @Parameters(name = "{0}")
  79. public static TestData[] initTestData() {
  80. return new TestData[] {
  81. new TestData("AFDA8EA10E185ACF8C0D0F8885A0EF61A72ECB11", false, false),
  82. new TestData("2FB05DBB70FC07CB84C13431F640CA6CEA1DBF8A", false, true),
  83. new TestData("66CCECEC2AB46A9735B10FEC54EDF9FD0F77BAF9", true, true),
  84. new TestData("F727FAB884DA3BD402B6E0F5472E108D21033124", true, true),
  85. new TestData("faked", false, true) };
  86. }
  87. private static byte[] readTestKey(String filename) throws Exception {
  88. try (InputStream in = new BufferedInputStream(
  89. SecretKeysTest.class.getResourceAsStream(filename))) {
  90. return SecretKeys.keyFromNameValueFormat(in);
  91. }
  92. }
  93. private static PGPPublicKey readAsc(InputStream in)
  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. if (keys.hasNext()) {
  102. return keys.next();
  103. }
  104. }
  105. return null;
  106. }
  107. // Injected by JUnit
  108. @Parameter
  109. public TestData data;
  110. @Test
  111. public void testKeyRead() throws Exception {
  112. if (data.keyValue) {
  113. byte[] bytes = readTestKey(data.name + ".key");
  114. assertEquals('(', bytes[0]);
  115. assertEquals(')', bytes[bytes.length - 1]);
  116. }
  117. try (InputStream pubIn = this.getClass()
  118. .getResourceAsStream(data.name + ".asc")) {
  119. if (pubIn != null) {
  120. PGPPublicKey publicKey = readAsc(pubIn);
  121. // Do a full test trying to load the secret key.
  122. PGPDigestCalculatorProvider calculatorProvider = new JcaPGPDigestCalculatorProviderBuilder()
  123. .build();
  124. try (InputStream in = new BufferedInputStream(this.getClass()
  125. .getResourceAsStream(data.name + ".key"))) {
  126. PGPSecretKey secretKey = SecretKeys.readSecretKey(in,
  127. calculatorProvider,
  128. data.encrypted ? () -> "nonsense".toCharArray()
  129. : null,
  130. publicKey);
  131. assertNotNull(secretKey);
  132. } catch (PGPException e) {
  133. // Currently we may not be able to load OCB-encrypted keys.
  134. assertTrue(e.getMessage().contains("OCB"));
  135. assertTrue(data.encrypted);
  136. assertFalse(ocbAvailable());
  137. }
  138. }
  139. }
  140. }
  141. }