-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GPG: use BC PGP secret key parsing out of the box
Remove the custom S-expression parsing; BC has gotten many improvements in 1.79 regarding PGP ed25519 keys, AES/OCB encryption, and generally parsing key files. It now can do all we need. Change-Id: I392443e040cce150a9575d18795a7cb8195a3515 Signed-off-by: Thomas Wolf <[email protected]>
- Loading branch information
Showing
11 changed files
with
94 additions
and
1,585 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
/* | ||
* Copyright (C) 2021 Thomas Wolf <[email protected]> and others | ||
* Copyright (C) 2021, 2024 Thomas Wolf <[email protected]> and others | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Distribution License v. 1.0 which is available at | ||
|
@@ -9,19 +9,14 @@ | |
*/ | ||
package org.eclipse.jgit.gpg.bc.internal.keys; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertNotNull; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
import java.io.BufferedInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.security.Security; | ||
import java.util.Iterator; | ||
|
||
import javax.crypto.Cipher; | ||
|
||
import org.bouncycastle.jce.provider.BouncyCastleProvider; | ||
import org.bouncycastle.openpgp.PGPException; | ||
import org.bouncycastle.openpgp.PGPPublicKey; | ||
|
@@ -49,39 +44,15 @@ public static void ensureBC() { | |
} | ||
} | ||
|
||
private static volatile Boolean haveOCB; | ||
|
||
private static boolean ocbAvailable() { | ||
Boolean haveIt = haveOCB; | ||
if (haveIt != null) { | ||
return haveIt.booleanValue(); | ||
} | ||
try { | ||
Cipher c = Cipher.getInstance("AES/OCB/NoPadding"); //$NON-NLS-1$ | ||
if (c == null) { | ||
haveOCB = Boolean.FALSE; | ||
return false; | ||
} | ||
} catch (NoClassDefFoundError | Exception e) { | ||
haveOCB = Boolean.FALSE; | ||
return false; | ||
} | ||
haveOCB = Boolean.TRUE; | ||
return true; | ||
} | ||
|
||
private static class TestData { | ||
|
||
final String name; | ||
|
||
final boolean encrypted; | ||
|
||
final boolean keyValue; | ||
|
||
TestData(String name, boolean encrypted, boolean keyValue) { | ||
TestData(String name, boolean encrypted) { | ||
this.name = name; | ||
this.encrypted = encrypted; | ||
this.keyValue = keyValue; | ||
} | ||
|
||
@Override | ||
|
@@ -93,19 +64,12 @@ public String toString() { | |
@Parameters(name = "{0}") | ||
public static TestData[] initTestData() { | ||
return new TestData[] { | ||
new TestData("AFDA8EA10E185ACF8C0D0F8885A0EF61A72ECB11", false, false), | ||
new TestData("2FB05DBB70FC07CB84C13431F640CA6CEA1DBF8A", false, true), | ||
new TestData("66CCECEC2AB46A9735B10FEC54EDF9FD0F77BAF9", true, true), | ||
new TestData("F727FAB884DA3BD402B6E0F5472E108D21033124", true, true), | ||
new TestData("62D43D7F117F7A5E4998ECB6617EE9942D069C14", true, true), | ||
new TestData("faked", false, true) }; | ||
} | ||
|
||
private static byte[] readTestKey(String filename) throws Exception { | ||
try (InputStream in = new BufferedInputStream( | ||
SecretKeysTest.class.getResourceAsStream(filename))) { | ||
return SecretKeys.keyFromNameValueFormat(in); | ||
} | ||
new TestData("AFDA8EA10E185ACF8C0D0F8885A0EF61A72ECB11", false), | ||
new TestData("2FB05DBB70FC07CB84C13431F640CA6CEA1DBF8A", false), | ||
new TestData("66CCECEC2AB46A9735B10FEC54EDF9FD0F77BAF9", true), | ||
new TestData("F727FAB884DA3BD402B6E0F5472E108D21033124", true), | ||
new TestData("62D43D7F117F7A5E4998ECB6617EE9942D069C14", true), | ||
new TestData("faked", false) }; | ||
} | ||
|
||
private static PGPPublicKey readAsc(InputStream in) | ||
|
@@ -131,11 +95,6 @@ private static PGPPublicKey readAsc(InputStream in) | |
|
||
@Test | ||
public void testKeyRead() throws Exception { | ||
if (data.keyValue) { | ||
byte[] bytes = readTestKey(data.name + ".key"); | ||
assertEquals('(', bytes[0]); | ||
assertEquals(')', bytes[bytes.length - 1]); | ||
} | ||
try (InputStream pubIn = this.getClass() | ||
.getResourceAsStream(data.name + ".asc")) { | ||
if (pubIn != null) { | ||
|
@@ -151,11 +110,6 @@ public void testKeyRead() throws Exception { | |
: null, | ||
publicKey); | ||
assertNotNull(secretKey); | ||
} catch (PGPException e) { | ||
// Currently we may not be able to load OCB-encrypted keys. | ||
assertTrue(e.toString(), e.getMessage().contains("OCB")); | ||
assertTrue(data.encrypted); | ||
assertFalse(ocbAvailable()); | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 2 additions & 12 deletions
14
org.eclipse.jgit.gpg.bc/resources/org/eclipse/jgit/gpg/bc/internal/BCText.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.