Skip to content

Commit

Permalink
Added new P384KeyPair factory to create a keypair from DER or PEM-enc…
Browse files Browse the repository at this point in the history
…oded keys
  • Loading branch information
overheadhunter committed Dec 8, 2021
1 parent f83db3d commit 718639e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/main/java/org/cryptomator/cryptolib/common/P384KeyPair.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,18 @@
import java.nio.file.StandardOpenOption;
import java.security.AlgorithmParameters;
import java.security.InvalidAlgorithmParameterException;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.ECGenParameterSpec;
import java.security.spec.ECParameterSpec;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.InvalidParameterSpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;

public class P384KeyPair extends ECKeyPair {

Expand All @@ -31,6 +37,25 @@ public static P384KeyPair generate() {
return new P384KeyPair(keyPair);
}

/**
* Creates a key pair from the given key specs.
*
* @param publicKeySpec DER formatted public key
* @param privateKeySpec DER formatted private key
* @return created key pair
* @throws InvalidKeySpecException If the supplied key specs are unsuitable for {@value #EC_ALG} keys
*/
public static P384KeyPair create(X509EncodedKeySpec publicKeySpec, PKCS8EncodedKeySpec privateKeySpec) throws InvalidKeySpecException {
try {
KeyFactory factory = KeyFactory.getInstance(EC_ALG);
PublicKey publicKey = factory.generatePublic(publicKeySpec);
PrivateKey privateKey = factory.generatePrivate(privateKeySpec);
return new P384KeyPair(new KeyPair(publicKey, privateKey));
} catch (NoSuchAlgorithmException e) {
throw new IllegalStateException(EC_ALG + " not supported");
}
}

/**
* Loads a key pair from the given file
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@

import java.io.IOException;
import java.nio.file.Path;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;

public class P384KeyPairTest {

Expand All @@ -22,6 +26,16 @@ public void testGenerate() {
Assertions.assertNotEquals(keyPair1, keyPair2);
}

@Test
@DisplayName("create()")
public void testCreate() throws InvalidKeySpecException {
X509EncodedKeySpec publicKey = new X509EncodedKeySpec(Base64.getDecoder().decode("MHYwEAYHKoZIzj0CAQYFK4EEACIDYgAERxQR+NRN6Wga01370uBBzr2NHDbKIC56tPUEq2HX64RhITGhii8Zzbkb1HnRmdF0aq6uqmUy4jUhuxnKxsv59A6JeK7Unn+mpmm3pQAygjoGc9wrvoH4HWJSQYUlsXDu"));
PKCS8EncodedKeySpec privateKey = new PKCS8EncodedKeySpec(Base64.getDecoder().decode("ME8CAQAwEAYHKoZIzj0CAQYFK4EEACIEODA2AgEBBDEA6QybmBitf94veD5aCLr7nlkF5EZpaXHCfq1AXm57AKQyGOjTDAF9EQB28fMywTDQ"));

P384KeyPair keyPair = P384KeyPair.create(publicKey, privateKey);
Assertions.assertNotNull(keyPair);
}

@Test
@DisplayName("store(...)")
public void testStore(@TempDir Path tmpDir) {
Expand Down

0 comments on commit 718639e

Please sign in to comment.