Free as in Freedom: Codeberg.org. Create your repos!
Browse Source

Allow different providers than BC

tags/0.0.1-alpha5
Paul Schaub 1 month ago
parent
commit
36c871f198
Signed by: Paul Schaub <vanitasvitae@fsfe.org> GPG Key ID: 62BEE9264BF17311

+ 3
- 0
README.md View File

@@ -40,6 +40,9 @@ The entry point to the API is the `PGPainless` class. Here you can find methods
40 40
 The first thing you probably want to do is generate you some nice tasty Key Pairs. The most straight forward way to do so is by calling
41 41
 
42 42
 ```java
43
+        // Add a suitable cryptographic provider
44
+        Security.insertProviderAt(new BouncyCastleProvider(), 1);
45
+
43 46
         PGPSecretKeyRing keyRing = PGPainless.generateKeyRing()
44 47
                 .simpleRsaKeyRing("Juliet <juliet@montague.lit>", RsaLength._4096);
45 48
 ```

+ 1
- 1
build.gradle View File

@@ -16,7 +16,7 @@ buildscript {
16 16
 }
17 17
 
18 18
 plugins {
19
-    id 'ru.vyarus.animalsniffer' version '1.4.3'
19
+    id 'ru.vyarus.animalsniffer' version '1.5.0'
20 20
 }
21 21
 
22 22
 apply from: 'version.gradle'

+ 7
- 3
pgpainless-core/build.gradle View File

@@ -1,12 +1,16 @@
1
+ext {
2
+    bcVersion = "1.60"
3
+}
4
+
1 5
 
2 6
 dependencies {
3 7
     testCompile group: 'junit', name: 'junit', version: '4.12'
4 8
     /*
5
-    compile 'org.bouncycastle:bcprov-debug-jdk15on:1.60'
9
+    compile "org.bouncycastle:bcprov-debug-jdk15on:$bcVersion"
6 10
     /*/
7
-    compile 'org.bouncycastle:bcprov-jdk15on:1.60'
11
+    compile "org.bouncycastle:bcprov-jdk15on:$bcVersion"
8 12
     //*/
9
-    compile 'org.bouncycastle:bcpg-jdk15on:1.60'
13
+    compile "org.bouncycastle:bcpg-jdk15on:$bcVersion"
10 14
 
11 15
     // https://mvnrepository.com/artifact/com.google.code.findbugs/jsr305
12 16
     compile group: 'com.google.code.findbugs', name: 'jsr305', version: '3.0.2'

+ 6
- 14
pgpainless-core/src/main/java/org/pgpainless/key/generation/KeyRingBuilder.java View File

@@ -22,13 +22,11 @@ import java.security.InvalidAlgorithmParameterException;
22 22
 import java.security.KeyPair;
23 23
 import java.security.KeyPairGenerator;
24 24
 import java.security.NoSuchAlgorithmException;
25
-import java.security.NoSuchProviderException;
26 25
 import java.util.ArrayList;
27 26
 import java.util.Date;
28 27
 import java.util.List;
29 28
 
30 29
 import org.bouncycastle.bcpg.sig.KeyFlags;
31
-import org.bouncycastle.jce.provider.BouncyCastleProvider;
32 30
 import org.bouncycastle.openpgp.PGPEncryptedData;
33 31
 import org.bouncycastle.openpgp.PGPException;
34 32
 import org.bouncycastle.openpgp.PGPKeyPair;
@@ -73,11 +71,10 @@ public class KeyRingBuilder implements KeyRingBuilderInterface {
73 71
      * @return {@link PGPSecretKeyRing} containing the KeyPair.
74 72
      * @throws PGPException
75 73
      * @throws NoSuchAlgorithmException
76
-     * @throws NoSuchProviderException
77 74
      * @throws InvalidAlgorithmParameterException
78 75
      */
79 76
     public PGPKeyRing simpleRsaKeyRing(@Nonnull String userId, @Nonnull RsaLength length)
80
-            throws PGPException, NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException {
77
+            throws PGPException, NoSuchAlgorithmException, InvalidAlgorithmParameterException {
81 78
         return withMasterKey(
82 79
                         KeySpec.getBuilder(RSA_GENERAL.withLength(length))
83 80
                                 .withDefaultKeyFlags()
@@ -96,11 +93,10 @@ public class KeyRingBuilder implements KeyRingBuilderInterface {
96 93
      * @return {@link PGPSecretKeyRing} containing the key pairs.
97 94
      * @throws PGPException
98 95
      * @throws NoSuchAlgorithmException
99
-     * @throws NoSuchProviderException
100 96
      * @throws InvalidAlgorithmParameterException
101 97
      */
102 98
     public PGPKeyRing simpleEcKeyRing(@Nonnull String userId)
103
-            throws PGPException, NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException {
99
+            throws PGPException, NoSuchAlgorithmException, InvalidAlgorithmParameterException {
104 100
         return withSubKey(
105 101
                         KeySpec.getBuilder(ECDH.fromCurve(EllipticCurve._P256))
106 102
                                 .withKeyFlags(KeyFlag.ENCRYPT_STORAGE, KeyFlag.ENCRYPT_COMMS)
@@ -160,12 +156,11 @@ public class KeyRingBuilder implements KeyRingBuilderInterface {
160 156
         class BuildImpl implements Build {
161 157
 
162 158
             @Override
163
-            public PGPKeyRing build() throws NoSuchAlgorithmException, PGPException, NoSuchProviderException,
159
+            public PGPKeyRing build() throws NoSuchAlgorithmException, PGPException,
164 160
                     InvalidAlgorithmParameterException {
165 161
 
166 162
                 // Hash Calculator
167 163
                 PGPDigestCalculator calculator = new JcaPGPDigestCalculatorProviderBuilder()
168
-                        .setProvider(BouncyCastleProvider.PROVIDER_NAME)
169 164
                         .build()
170 165
                         .get(HashAlgorithm.SHA1.getAlgorithmId());
171 166
 
@@ -173,7 +168,6 @@ public class KeyRingBuilder implements KeyRingBuilderInterface {
173 168
                 PBESecretKeyEncryptor encryptor = passphrase == null ?
174 169
                         null : // unencrypted key pair, otherwise AES-256 encrypted
175 170
                         new JcePBESecretKeyEncryptorBuilder(PGPEncryptedData.AES_256, calculator)
176
-                                .setProvider(BouncyCastleProvider.PROVIDER_NAME)
177 171
                                 .build(passphrase != null ? passphrase.getChars() : null);
178 172
 
179 173
                 if (passphrase != null) {
@@ -190,8 +184,7 @@ public class KeyRingBuilder implements KeyRingBuilderInterface {
190 184
 
191 185
                 // Signer for creating self-signature
192 186
                 PGPContentSignerBuilder signer = new JcaPGPContentSignerBuilder(
193
-                        certKey.getPublicKey().getAlgorithm(), HashAlgorithm.SHA512.getAlgorithmId())
194
-                        .setProvider(BouncyCastleProvider.PROVIDER_NAME);
187
+                        certKey.getPublicKey().getAlgorithm(), HashAlgorithm.SHA512.getAlgorithmId());
195 188
 
196 189
                 PGPSignatureSubpacketVector hashedSubPackets = certKeySpec.getSubpackets();
197 190
 
@@ -220,11 +213,10 @@ public class KeyRingBuilder implements KeyRingBuilderInterface {
220 213
             }
221 214
 
222 215
             private PGPKeyPair generateKeyPair(KeySpec spec)
223
-                    throws NoSuchProviderException, NoSuchAlgorithmException, PGPException,
216
+                    throws NoSuchAlgorithmException, PGPException,
224 217
                     InvalidAlgorithmParameterException {
225 218
                 KeyType type = spec.getKeyType();
226
-                KeyPairGenerator certKeyGenerator = KeyPairGenerator.getInstance(
227
-                        type.getName(), BouncyCastleProvider.PROVIDER_NAME);
219
+                KeyPairGenerator certKeyGenerator = KeyPairGenerator.getInstance(type.getName());
228 220
                 certKeyGenerator.initialize(type.getAlgorithmSpec());
229 221
 
230 222
                 // Create raw Key Pair

+ 1
- 2
pgpainless-core/src/main/java/org/pgpainless/key/generation/KeyRingBuilderInterface.java View File

@@ -18,7 +18,6 @@ package org.pgpainless.key.generation;
18 18
 import javax.annotation.Nonnull;
19 19
 import java.security.InvalidAlgorithmParameterException;
20 20
 import java.security.NoSuchAlgorithmException;
21
-import java.security.NoSuchProviderException;
22 21
 
23 22
 import org.bouncycastle.openpgp.PGPException;
24 23
 import org.pgpainless.key.collection.PGPKeyRing;
@@ -47,7 +46,7 @@ public interface KeyRingBuilderInterface {
47 46
 
48 47
     interface Build {
49 48
 
50
-        PGPKeyRing build() throws NoSuchAlgorithmException, PGPException, NoSuchProviderException,
49
+        PGPKeyRing build() throws NoSuchAlgorithmException, PGPException,
51 50
                 InvalidAlgorithmParameterException;
52 51
 
53 52
     }

+ 1
- 1
pgpainless-core/src/test/java/org/pgpainless/AbstractPGPainlessTest.java View File

@@ -24,6 +24,6 @@ public abstract class AbstractPGPainlessTest {
24 24
 
25 25
     @BeforeClass
26 26
     public static void registerProvider() {
27
-        Security.addProvider(new BouncyCastleProvider());
27
+        Security.insertProviderAt(new BouncyCastleProvider(), 1);
28 28
     }
29 29
 }

+ 4
- 8
pgpainless-core/src/test/java/org/pgpainless/BouncycastleExportSubkeys.java View File

@@ -19,7 +19,6 @@ import java.security.InvalidAlgorithmParameterException;
19 19
 import java.security.KeyPair;
20 20
 import java.security.KeyPairGenerator;
21 21
 import java.security.NoSuchAlgorithmException;
22
-import java.security.NoSuchProviderException;
23 22
 import java.util.Date;
24 23
 
25 24
 import org.bouncycastle.bcpg.CompressionAlgorithmTags;
@@ -28,7 +27,6 @@ import org.bouncycastle.bcpg.PublicKeyAlgorithmTags;
28 27
 import org.bouncycastle.bcpg.SymmetricKeyAlgorithmTags;
29 28
 import org.bouncycastle.bcpg.sig.Features;
30 29
 import org.bouncycastle.bcpg.sig.KeyFlags;
31
-import org.bouncycastle.jce.provider.BouncyCastleProvider;
32 30
 import org.bouncycastle.jce.spec.ECNamedCurveGenParameterSpec;
33 31
 import org.bouncycastle.openpgp.PGPException;
34 32
 import org.bouncycastle.openpgp.PGPKeyPair;
@@ -47,13 +45,13 @@ import org.junit.Test;
47 45
 public class BouncycastleExportSubkeys extends AbstractPGPainlessTest {
48 46
 
49 47
     @Test
50
-    public void testExportImport() throws NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException, PGPException {
48
+    public void testExportImport() throws NoSuchAlgorithmException, InvalidAlgorithmParameterException, PGPException {
51 49
         KeyPairGenerator generator;
52 50
         KeyPair pair;
53 51
 
54 52
         // Generate master key
55 53
 
56
-        generator = KeyPairGenerator.getInstance("ECDSA", BouncyCastleProvider.PROVIDER_NAME);
54
+        generator = KeyPairGenerator.getInstance("ECDSA");
57 55
         generator.initialize(new ECNamedCurveGenParameterSpec("P-256"));
58 56
 
59 57
         pair = generator.generateKeyPair();
@@ -79,7 +77,7 @@ public class BouncycastleExportSubkeys extends AbstractPGPainlessTest {
79 77
 
80 78
         // Generate sub key
81 79
 
82
-        generator = KeyPairGenerator.getInstance("ECDH", BouncyCastleProvider.PROVIDER_NAME);
80
+        generator = KeyPairGenerator.getInstance("ECDH");
83 81
         generator.initialize(new ECNamedCurveGenParameterSpec("P-256"));
84 82
 
85 83
         pair = generator.generateKeyPair();
@@ -88,13 +86,11 @@ public class BouncycastleExportSubkeys extends AbstractPGPainlessTest {
88 86
         // Assemble key
89 87
 
90 88
         PGPDigestCalculator calculator = new JcaPGPDigestCalculatorProviderBuilder()
91
-                .setProvider(BouncyCastleProvider.PROVIDER_NAME)
92 89
                 .build()
93 90
                 .get(HashAlgorithmTags.SHA1);
94 91
 
95 92
         PGPContentSignerBuilder signerBuilder = new JcaPGPContentSignerBuilder(
96
-                pgpMasterKey.getPublicKey().getAlgorithm(), HashAlgorithmTags.SHA512)
97
-                .setProvider(BouncyCastleProvider.PROVIDER_NAME);
93
+                pgpMasterKey.getPublicKey().getAlgorithm(), HashAlgorithmTags.SHA512);
98 94
 
99 95
         PGPKeyRingGenerator pgpGenerator = new PGPKeyRingGenerator(PGPSignature.POSITIVE_CERTIFICATION,
100 96
                 pgpMasterKey, "alice@wonderland.lit", calculator, subPackets.generate(), null,

Loading…
Cancel
Save