-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ARC-1234: Keygenerator for Signing and Encryption Keys #11
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,4 @@ gesundheitsid/dependency-reduced-pom.xml | |
.flattened-pom.xml | ||
*_jwks.json | ||
env.properties | ||
*.pem |
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 |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<parent> | ||
<groupId>com.oviva.gesundheitsid</groupId> | ||
<artifactId>gesundheitsid-parent</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
</parent> | ||
|
||
<artifactId>esgen</artifactId> | ||
<packaging>jar</packaging> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.slf4j</groupId> | ||
<artifactId>slf4j-api</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>ch.qos.logback</groupId> | ||
<artifactId>logback-classic</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>com.nimbusds</groupId> | ||
<artifactId>nimbus-jose-jwt</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.junit.jupiter</groupId> | ||
<artifactId>junit-jupiter</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.junit.jupiter</groupId> | ||
<artifactId>junit-jupiter-params</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.hamcrest</groupId> | ||
<artifactId>hamcrest</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.mockito</groupId> | ||
<artifactId>mockito-junit-jupiter</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.mockito</groupId> | ||
<artifactId>mockito-core</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
</dependencies> | ||
|
||
</project> |
94 changes: 94 additions & 0 deletions
94
esgen/src/main/java/com/oviva/gesundheitsid/esgen/Main.java
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 | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,94 @@ | ||||||
package com.oviva.gesundheitsid.esgen; | ||||||
|
||||||
import com.nimbusds.jose.JOSEException; | ||||||
import com.nimbusds.jose.jwk.Curve; | ||||||
import com.nimbusds.jose.jwk.ECKey; | ||||||
import com.nimbusds.jose.jwk.JWKSet; | ||||||
import com.nimbusds.jose.jwk.KeyUse; | ||||||
import com.nimbusds.jose.jwk.gen.ECKeyGenerator; | ||||||
import java.io.IOException; | ||||||
import java.nio.file.Files; | ||||||
import java.nio.file.Path; | ||||||
import java.nio.file.StandardOpenOption; | ||||||
import java.security.Key; | ||||||
import java.util.Base64; | ||||||
import org.slf4j.Logger; | ||||||
import org.slf4j.LoggerFactory; | ||||||
|
||||||
public class Main { | ||||||
|
||||||
private static final Logger logger = LoggerFactory.getLogger(Main.class); | ||||||
|
||||||
public static void main(String[] args) { | ||||||
|
||||||
var main = new Main(); | ||||||
try { | ||||||
main.run(); | ||||||
} catch (Exception e) { | ||||||
logger.atError().setCause(e).log("key generator failed"); | ||||||
System.exit(1); | ||||||
} | ||||||
} | ||||||
|
||||||
public void run() throws JOSEException, IOException { // your business logic goes here... | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
leftover? |
||||||
|
||||||
logger.atInfo().log("generating ES256 keys"); | ||||||
|
||||||
var federationSigningKey = | ||||||
new ECKeyGenerator(Curve.P_256) | ||||||
.keyUse(KeyUse.SIGNATURE) | ||||||
.keyIDFromThumbprint(true) | ||||||
.generate(); | ||||||
|
||||||
var sigName = "sig"; | ||||||
var encName = "enc"; | ||||||
|
||||||
saveJwks(sigName, new JWKSet(federationSigningKey)); | ||||||
savePublicEncoded(sigName, federationSigningKey); | ||||||
|
||||||
var federationIdTokenEncryptionKey = | ||||||
new ECKeyGenerator(Curve.P_256) | ||||||
.keyUse(KeyUse.ENCRYPTION) | ||||||
.keyIDFromThumbprint(true) | ||||||
.generate(); | ||||||
|
||||||
saveJwks(encName, new JWKSet(federationIdTokenEncryptionKey)); | ||||||
savePublicEncoded(encName, federationIdTokenEncryptionKey); | ||||||
} | ||||||
|
||||||
private void saveJwks(String name, JWKSet set) throws IOException { | ||||||
var jwks = set.toString(false); | ||||||
writeString(Path.of("%s_jwks.json".formatted(name)), jwks); | ||||||
} | ||||||
|
||||||
private void savePublicEncoded(String name, ECKey jwk) throws JOSEException, IOException { | ||||||
|
||||||
var publicEncodedPem = encodeAsPem(jwk.toPublicKey(), "PUBLIC KEY"); | ||||||
var p = Path.of("%s_pub.pem".formatted(name)); | ||||||
writeString(p, publicEncodedPem); | ||||||
} | ||||||
|
||||||
private String encodeAsPem(Key k, String type) { | ||||||
|
||||||
var encoded = Base64.getEncoder().encodeToString(k.getEncoded()); | ||||||
|
||||||
var sb = new StringBuilder(); | ||||||
sb.append("-----BEGIN %s-----%n".formatted(type)); | ||||||
|
||||||
while (!encoded.isEmpty()) { | ||||||
var end = Math.min(64, encoded.length()); | ||||||
var line = encoded.substring(0, end); | ||||||
sb.append(line).append('\n'); | ||||||
encoded = encoded.substring(end); | ||||||
} | ||||||
|
||||||
sb.append("-----END %s-----%n".formatted(type)); | ||||||
return sb.toString(); | ||||||
} | ||||||
|
||||||
private void writeString(Path p, String contents) throws IOException { | ||||||
|
||||||
Files.writeString(p, contents, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.CREATE); | ||||||
logger.atInfo().log("written '%s'".formatted(p)); | ||||||
} | ||||||
} |
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<configuration> | ||
<appender name="stdout" class="ch.qos.logback.core.ConsoleAppender"> | ||
<encoder> | ||
<pattern>%d{HH:mm:ss} %highlight(%-5level) %msg %kvp%n</pattern> | ||
</encoder> | ||
</appender> | ||
<root level="INFO"> | ||
<appender-ref ref="stdout"/> | ||
</root> | ||
</configuration> |
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#!/bin/bash | ||
|
||
mvn --quiet clean compile exec:java -Dexec.mainClass="com.oviva.gesundheitsid.esgen.Main" -f esgen |
This file was deleted.
Oops, something went wrong.
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd add a description, it is not clear to me what this is about at a first glance
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
will do in follow up!