Skip to content
Snippets Groups Projects
Commit 36b040d9 authored by Pascal Osterwinter's avatar Pascal Osterwinter
Browse files

Moved key verification to the appropriate place.

parent ff7cbabb
No related branches found
No related tags found
1 merge request!197Added Key validation to code samples. (planning#302)
......@@ -57,13 +57,6 @@ Eine Prüfung der Zertifikate kann in diesem Fall zu Testzwecken entfallen.
Weitere Informationen zur Gültigkeitsprüfung finden sich in der technischen Richtlinie [BSI TR-02103](https://www.bsi.bund.de/SharedDocs/Downloads/DE/BSI/Publikationen/TechnischeRichtlinien/TR02103/BSI-TR-02103.pdf?__blob=publicationFile&v=4) des BSI.
:::note Hinweis
An dieser Stelle werden noch detailliertere Informationen und konkrete Implementierungsbeispiele zur Prüfung der JSON Web Keys ergänzt.
:::
## Nutzung des öffentlichen Schlüssels zur Verschlüsselung
<!-- https://git.fitko.de/fit-connect/examples/ -->
<Tabs
defaultValue="js"
values={[
......@@ -72,26 +65,6 @@ An dieser Stelle werden noch detailliertere Informationen und konkrete Implement
]
}>
<TabItem value="js">
Die Umwandelung des JWK in eine Struktur, die für die Bibliothek [panva/jose](https://github.com/panva/jose) nutzbar ist, kann mithilfe einer Methode aus dieser Bibliothek durchgeführt werden.
```js
import { importJWK } from 'jose'
import { CompactEncrypt } from 'jose'
const publicKey = await importJWK({
"kty": "RSA",
"keyops": ["wrapKey"],
"x5c": [
"LS0tLS1CRUd...LS1FTkQgQ0VSVElGSUNBVEUtLS0tLQo="
],
"kid": "787f3a1c-7da7-44d7-9b79-9783b1ea9be8",
"alg": "RSA-OAEP-256",
"n": "sX2DX7rG5BoJd23A0...6ZqjRa1QcFnkq3_M4-tk",
"e": "AQAB"
}, "RSA-OAEP-256")
```
Der [folgende Code](https://git.fitko.de/fit-connect/examples/-/blob/main/javascript/encryption/src/main.js) kann zur validierung der Schlüssel benutzt werden.
```js
......@@ -138,6 +111,68 @@ Der [folgende Code](https://git.fitko.de/fit-connect/examples/-/blob/main/javasc
}
```
</TabItem>
<TabItem value="java">
Der [Folgende Code](https://git.fitko.de/fit-connect/examples/-/blob/main/java/crypto/src/main/java/Encrypt.java) kann zur validierung des Schlüssels benutzt werden.
```
public static void validateRSAKey(RSAKey RSAKey, boolean isPrivate){
validateTrueOrElseThrow((RSAKey.getModulus().decodeToBigInteger().bitLength() >= 4096), "JWK has wrong key length.");
if(isPrivate){
validateTrueOrElseThrow(RSAKey.getKeyOperations().size() == 1 &&
RSAKey.getKeyOperations().contains(KeyOperation.UNWRAP_KEY),
"The specified public key is not intended for 'wrapKey' as specified through key operation.");
}
else{
validateTrueOrElseThrow(RSAKey.getKeyOperations().size() == 1 &&
RSAKey.getKeyOperations().contains(KeyOperation.WRAP_KEY),
"The specified public key is not intended for 'wrapKey' as specified through key operation.");
}
validateTrueOrElseThrow(RSAKey.getAlgorithm().equals(JWEAlgorithm.RSA_OAEP_256), "Key algorithm must be RSA-OAEP-256!");
validateTrueOrElseThrow(RSAKey.getPublicExponent().toString().equals("AQAB"), "Key must have e: \"AQAB\"");
}
private static void validateTrueOrElseThrow(boolean expression, String msg) {
if (!expression) {
throw new RuntimeException(msg);
}
}
```
</TabItem>
## Nutzung des öffentlichen Schlüssels zur Verschlüsselung
<!-- https://git.fitko.de/fit-connect/examples/ -->
<Tabs
defaultValue="js"
values={[
{ label: 'JavaScript', value: 'js', },
{ label: 'Java', value: 'java', },
]
}>
<TabItem value="js">
Die Umwandelung des JWK in eine Struktur, die für die Bibliothek [panva/jose](https://github.com/panva/jose) nutzbar ist, kann mithilfe einer Methode aus dieser Bibliothek durchgeführt werden.
```js
import { importJWK } from 'jose'
import { CompactEncrypt } from 'jose'
const publicKey = await importJWK({
"kty": "RSA",
"keyops": ["wrapKey"],
"x5c": [
"LS0tLS1CRUd...LS1FTkQgQ0VSVElGSUNBVEUtLS0tLQo="
],
"kid": "787f3a1c-7da7-44d7-9b79-9783b1ea9be8",
"alg": "RSA-OAEP-256",
"n": "sX2DX7rG5BoJd23A0...6ZqjRa1QcFnkq3_M4-tk",
"e": "AQAB"
}, "RSA-OAEP-256")
```
Mit dem zuvor eingelesenen Schlüssel können nun Zeichenketten und Binärdaten verschlüsselt werden.
Für die verschlüsselung von Zeichenketten (z.B. serialisierte JSON- oder XML-Objekte) kann die in Browser verfügbare [TextEncoder-API](https://developer.mozilla.org/en-US/docs/Web/API/TextEncoder) verwendet werden, die den String UTF8-kodiert in ein `Uint8Array` umwandelt.
......@@ -187,32 +222,6 @@ String publicKeyAsString = new String(existingPublicKey.readAllBytes());
RSAPublicKey publicKey = RSAKey.parse(publicKeyAsString).toRSAPublicKey();
```
Der [Folgende Code](https://git.fitko.de/fit-connect/examples/-/blob/main/java/crypto/src/main/java/Encrypt.java) kann zur validierung des Schlüssels benutzt werden.
```
public static void validateRSAKey(RSAKey RSAKey, boolean isPrivate){
validateTrueOrElseThrow((RSAKey.getModulus().decodeToBigInteger().bitLength() >= 4096), "JWK has wrong key length.");
if(isPrivate){
validateTrueOrElseThrow(RSAKey.getKeyOperations().size() == 1 &&
RSAKey.getKeyOperations().contains(KeyOperation.UNWRAP_KEY),
"The specified public key is not intended for 'wrapKey' as specified through key operation.");
}
else{
validateTrueOrElseThrow(RSAKey.getKeyOperations().size() == 1 &&
RSAKey.getKeyOperations().contains(KeyOperation.WRAP_KEY),
"The specified public key is not intended for 'wrapKey' as specified through key operation.");
}
validateTrueOrElseThrow(RSAKey.getAlgorithm().equals(JWEAlgorithm.RSA_OAEP_256), "Key algorithm must be RSA-OAEP-256!");
validateTrueOrElseThrow(RSAKey.getPublicExponent().toString().equals("AQAB"), "Key must have e: \"AQAB\"");
}
private static void validateTrueOrElseThrow(boolean expression, String msg) {
if (!expression) {
throw new RuntimeException(msg);
}
}
```
Mit diesem umgewandelten Schlüssel können nun Zeichenketten und Binärdaten verschlüsselt werden.
Über die [Payload-Klasse](https://www.javadoc.io/doc/com.nimbusds/nimbus-jose-jwt/latest/com/nimbusds/jose/Payload.html) können verschiedene Typen, wie `byte[]` oder `String`, verschlüsselt werden.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment