forked from oviva-ag/ehealthid-relying-party
-
Notifications
You must be signed in to change notification settings - Fork 0
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
Draft upgrade commands #1
Draft
eduardOrthopy
wants to merge
3
commits into
main
Choose a base branch
from
certificate-upgrades
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 |
---|---|---|
@@ -1,6 +1,7 @@ | ||
.idea/ | ||
.vscode/ | ||
target/ | ||
local/ | ||
gesundheitsid/env.properties | ||
*.iml | ||
.flattened-pom.xml | ||
|
130 changes: 130 additions & 0 deletions
130
ehealthid-cli/src/main/java/com/oviva/ehealthid/cli/MTlsRefreshCommand.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,130 @@ | ||
package com.oviva.ehealthid.cli; | ||
|
||
import com.nimbusds.jose.JOSEException; | ||
import com.nimbusds.jose.jwk.*; | ||
import com.nimbusds.jose.jwk.gen.ECKeyGenerator; | ||
import com.nimbusds.jose.util.Base64; | ||
import com.nimbusds.oauth2.sdk.id.Issuer; | ||
import com.nimbusds.oauth2.sdk.util.X509CertificateUtils; | ||
import java.io.IOException; | ||
import java.net.URI; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.security.cert.CertificateEncodingException; | ||
import java.time.Duration; | ||
import java.time.Instant; | ||
import java.util.Date; | ||
import java.util.List; | ||
import java.util.concurrent.Callable; | ||
import org.bouncycastle.operator.OperatorCreationException; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import picocli.CommandLine; | ||
|
||
@CommandLine.Command( | ||
name = "tls-refresh", | ||
mixinStandardHelpOptions = true, | ||
description = "Generate or Refresh the mTLS certificates in the Signing Key.") | ||
public class MTlsRefreshCommand implements Callable<Integer> { | ||
|
||
@CommandLine.Option( | ||
names = {"-i", "--iss", "--issuer-uri"}, | ||
description = "the issuer uri of the 'Fachdienst' identiy provider", | ||
required = true) | ||
private URI issuerUri; | ||
|
||
@CommandLine.Option( | ||
names = {"-e", "--existing"}, | ||
description = "the existing signing key", | ||
required = true) | ||
private String existingKey; | ||
|
||
@CommandLine.Option( | ||
names = {"-r", "--refresh"}, | ||
description = "refresh the existing mTLS certificate", | ||
defaultValue = "false") | ||
private boolean refresh; | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(MTlsRefreshCommand.class); | ||
|
||
public Integer call() throws Exception { | ||
|
||
var sigName = "sig"; | ||
|
||
logger.atInfo().log("using existing signing key '%s'".formatted(existingKey)); | ||
var inputStream = Files.newInputStream(Path.of(existingKey)); | ||
var jwks = JWKSet.load(inputStream); | ||
var key = jwks.getKeys().get(0); | ||
if (refresh) { | ||
var newKey = generateCertificate(key); | ||
System.out.println(newKey); | ||
logger.atInfo().log("refreshing mTLS certificate"); | ||
} else { | ||
var newKey = generateCertificateFromExistingKey(key); | ||
logger.atInfo().log("generating mTLS certificate"); | ||
System.out.println(newKey); | ||
} | ||
return 0; | ||
} | ||
|
||
private JWK generateCertificateFromExistingKey(JWK key) | ||
throws JOSEException, IOException, OperatorCreationException, CertificateEncodingException { | ||
// The idea here is to give users that currently have keys, that do not have | ||
// the relevant x5c field, the ability to generate a certificate for mTLS | ||
// without going through the fuzz of generating a new key and talking to | ||
// Gematik/BfArM about it. Which just generates work for everyone. | ||
Comment on lines
+72
to
+75
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. 👍 |
||
|
||
var now = Instant.now(); | ||
var nbf = now.minus(Duration.ofDays(1)); | ||
var exp = now.plus(Duration.ofDays(180)); | ||
|
||
var cert = | ||
X509CertificateUtils.generateSelfSigned( | ||
new Issuer(issuerUri), | ||
Date.from(nbf), | ||
Date.from(exp), | ||
key.toECKey().toPublicKey(), | ||
key.toECKey().toPrivateKey()); | ||
|
||
return new ECKey.Builder(key.toECKey()) | ||
.x509CertChain(List.of(Base64.encode(cert.getEncoded()))) | ||
.build(); | ||
} | ||
|
||
private JWK generateCertificate(JWK key) | ||
throws JOSEException, IOException, OperatorCreationException, CertificateEncodingException { | ||
// TODO: This is one huge playground method | ||
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. That's the starting point I used: https://connect2id.com/products/nimbus-oauth-openid-connect-sdk/examples/utils/self-signed-client-cert |
||
// Non of this is 'real' in the sense that it should work or is | ||
// what the standard would require. | ||
// My current understanding is that is that to keep in line with the | ||
// standard and the intended process, we should generate a new certificate | ||
// sign it with the existing key and then persist the new key and the new | ||
// certificate for use with mTLS. | ||
// Again, not what this currently does. | ||
var certKey = | ||
new ECKeyGenerator(Curve.P_256) | ||
.keyUse(KeyUse.SIGNATURE) | ||
.keyIDFromThumbprint(true) | ||
.generate(); | ||
|
||
var now = Instant.now(); | ||
var nbf = now.minus(Duration.ofDays(1)); | ||
var exp = now.plus(Duration.ofDays(180)); | ||
|
||
var cert = | ||
X509CertificateUtils.generateSelfSigned( | ||
new Issuer(issuerUri), | ||
Date.from(nbf), | ||
Date.from(exp), | ||
certKey.toPublicKey(), | ||
certKey.toPrivateKey()); | ||
|
||
var chain = key.getX509CertChain(); | ||
var newChain = new java.util.ArrayList<>(chain); | ||
newChain.add(Base64.encode(cert.getEncoded())); | ||
|
||
// TODO also persist the new signing key | ||
|
||
return new ECKey.Builder(key.toECKey()).x509CertChain(newChain).build(); | ||
} | ||
} |
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
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.
What I though about before: Maybe we should split the signing keys for JWTs and the mTLS client key. That would also reduce the risk of breaking existing tokens, no?
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.
My thoughts exactly. Personally, I think we will need to do that anyway for the exchange of mTLS keys.
I do not want to run through the whole Process every 5-6 months.