Skip to content

Commit

Permalink
Merge pull request #31 from eu-digital-green-certificates/feat/add-su…
Browse files Browse the repository at this point in the history
…pport-for-explicit-ec-private-key

Add support for explicit EC PrivateKey
  • Loading branch information
f11h authored Nov 7, 2022
2 parents 3ea2896 + 30f0c55 commit c3fe33b
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 20 deletions.
2 changes: 1 addition & 1 deletion codestyle/checkstyle.xml
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@
value="CLASS_DEF, INTERFACE_DEF, ENUM_DEF, METHOD_DEF, CTOR_DEF, VARIABLE_DEF"/>
</module>
<module name="JavadocMethod">
<property name="scope" value="public"/>
<property name="accessModifiers" value="public"/>
<property name="allowMissingParamTags" value="true"/>
<property name="allowMissingReturnTag" value="true"/>
<property name="allowedAnnotations" value="Override, Test"/>
Expand Down
17 changes: 8 additions & 9 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<maven.compiler.plugin-version>3.8.1</maven.compiler.plugin-version>
<dgclib.version>1.2.0</dgclib.version>
<plugin.checkstyle.version>3.1.2</plugin.checkstyle.version>
<dgclib.version>1.3.2</dgclib.version>
<plugin.checkstyle.version>3.2.0</plugin.checkstyle.version>
<plugin.license.version>2.0.0</plugin.license.version>
<owasp.version>6.5.2</owasp.version>
<owasp.version>7.3.0</owasp.version>
<!-- license -->
<license.projectName>EU Digital Green Certificate Gateway Service / dgc-cli</license.projectName>
<license.inceptionYear>2021</license.inceptionYear>
Expand Down Expand Up @@ -57,7 +57,7 @@
<dependency>
<groupId>info.picocli</groupId>
<artifactId>picocli</artifactId>
<version>4.6.3</version>
<version>4.7.0</version>
</dependency>
<dependency>
<groupId>eu.europa.ec.dgc</groupId>
Expand All @@ -77,7 +77,7 @@
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.36</version>
<version>2.0.3</version>
</dependency>

</dependencies>
Expand Down Expand Up @@ -116,7 +116,7 @@
<path>
<groupId>info.picocli</groupId>
<artifactId>picocli-codegen</artifactId>
<version>4.6.2</version>
<version>4.7.0</version>
</path>
</annotationProcessorPaths>
<compilerArgs>
Expand All @@ -130,7 +130,6 @@
<configuration>
<configLocation>codestyle/checkstyle.xml</configLocation>
<excludes>target/**/*</excludes>
<encoding>UTF-8</encoding>
<consoleOutput>true</consoleOutput>
<failsOnError>true</failsOnError>
<violationSeverity>warning</violationSeverity>
Expand All @@ -150,7 +149,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
<version>3.3.0</version>
<configuration>
<archive>
<manifest>
Expand All @@ -165,7 +164,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<version>3.4.1</version>
<executions>
<execution>
<phase>package</phase>
Expand Down
41 changes: 31 additions & 10 deletions src/main/java/eu/europa/ec/dgc/cli/utils/CliUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,19 @@

import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import org.bouncycastle.asn1.pkcs.PrivateKeyInfo;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.util.Base64;
import org.apache.commons.io.FileUtils;
import org.bouncycastle.jcajce.provider.asymmetric.x509.CertificateFactory;
import org.bouncycastle.openssl.PEMParser;
import org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter;

public class CliUtils {

Expand All @@ -42,14 +45,32 @@ public class CliUtils {
* @return Private Key
* @throws IOException if reading failed.
*/
public static PrivateKey readKeyFromFile(File inputFile) throws IOException {
FileReader fileReader = new FileReader(inputFile);
PEMParser pemParser = new PEMParser(fileReader);
public static PrivateKey readKeyFromFile(File inputFile)
throws IOException, NoSuchAlgorithmException, InvalidKeySpecException {
String pem = FileUtils.readFileToString(inputFile, StandardCharsets.UTF_8);
int beginPrivateKey = pem.indexOf("-----BEGIN PRIVATE KEY-----");
int endPrivateKey = pem.indexOf("-----END PRIVATE KEY-----");

JcaPEMKeyConverter converter = new JcaPEMKeyConverter();
PrivateKeyInfo privateKeyInfo = PrivateKeyInfo.getInstance(pemParser.readObject());
if (beginPrivateKey == -1 || endPrivateKey == -1) {
System.err.println("Could not find PKCS#8 Private Key");
return null;
}


String privateKeyBase64 = pem
.substring(beginPrivateKey + 27, endPrivateKey - 1)
.replaceAll("[\n\r ]", "");
byte[] keyBytes = Base64.getDecoder().decode(privateKeyBase64);

return converter.getPrivateKey(privateKeyInfo);
PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(keyBytes);
KeyFactory ecKeyFactory = KeyFactory.getInstance("ECDSA");

try {
return ecKeyFactory.generatePrivate(spec);
} catch (InvalidKeySpecException e) {
KeyFactory rsaKeyFactory = KeyFactory.getInstance("RSA");
return rsaKeyFactory.generatePrivate(spec);
}
}

/**
Expand Down

0 comments on commit c3fe33b

Please sign in to comment.