Skip to content
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 1 commit into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ gesundheitsid/dependency-reduced-pom.xml
.flattened-pom.xml
*_jwks.json
env.properties
*.pem
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,25 @@ Setting up a proxy with a header filter can get around that limitation though.
/~q & ~d gsi.dev.gematik.solutions/X-Authorization/<value goes here>
```

## Setup Test VM

```shell

sudo apt update
sudo apt install jq openjdk-17-jre-headless

# install caddy
sudo apt install -y debian-keyring debian-archive-keyring apt-transport-https curl
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | sudo gpg --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' | sudo tee /etc/apt/sources.list.d/caddy-stable.list
sudo apt update
sudo apt install caddy

sudo systemctl disable --now caddy

sudo caddy reverse-proxy --from=t.oviva.io --to=:1234
```


## Helpful Links

Expand Down
59 changes: 59 additions & 0 deletions esgen/pom.xml
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>
Copy link
Contributor

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

Copy link
Contributor Author

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!

<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 esgen/src/main/java/com/oviva/gesundheitsid/esgen/Main.java
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...
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public void run() throws JOSEException, IOException { // your business logic goes here...
public void run() throws JOSEException, IOException {

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));
}
}
11 changes: 11 additions & 0 deletions esgen/src/main/resources/logback.xml
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>
3 changes: 3 additions & 0 deletions gen_keys.sh
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
4 changes: 0 additions & 4 deletions gesundheitsid/src/test/resources/log4j.properties

This file was deleted.

46 changes: 34 additions & 12 deletions oidc-server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,18 +66,9 @@
<artifactId>logback-classic</artifactId>
</dependency>
<dependency>
<groupId>ch.qos.logback.contrib</groupId>
<artifactId>logback-json-classic</artifactId>
<exclusions>
<exclusion>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>ch.qos.logback.contrib</groupId>
<artifactId>logback-jackson</artifactId>
<groupId>net.logstash.logback</groupId>
<artifactId>logstash-logback-encoder</artifactId>
<version>7.4</version>
</dependency>
<!-- END logging-->

Expand Down Expand Up @@ -150,4 +141,35 @@

</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>

<configuration>
<archive>
<manifest>
<mainClass>com.oviva.gesundheitsid.relyingparty.Main</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>

<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>

</plugin>
</plugins>
</build>

</project>
Loading
Loading