Skip to content
Snippets Groups Projects
Commit 5d551745 authored by Martin Vogel's avatar Martin Vogel
Browse files

feat: add test key builder from cli (planning#667))

parent 116de61d
No related branches found
No related tags found
1 merge request!321Draft: planning#667 Derive VPKI Certs from PKCS KeyStore
package dev.fitko.fitconnect.tools.jwk;
import com.nimbusds.jose.JWEAlgorithm;
import com.nimbusds.jose.JWSAlgorithm;
import com.nimbusds.jose.jwk.JWK;
import dev.fitko.fitconnect.api.domain.crypto.JWKPair;
import dev.fitko.fitconnect.core.crypto.utils.KeyGenerator;
import java.security.KeyPair;
import java.util.UUID;
import static com.nimbusds.jose.jwk.KeyOperation.SIGN;
import static com.nimbusds.jose.jwk.KeyOperation.UNWRAP_KEY;
import static com.nimbusds.jose.jwk.KeyOperation.VERIFY;
import static com.nimbusds.jose.jwk.KeyOperation.WRAP_KEY;
/**
* JWK Test Key Generator.
* <p>
* Generates key pairs of public and private keys for encryption and signing.
*/
public final class TestKeys {
public static final int DEFAULT_KEY_SIZE = 4096;
private TestKeys() {
}
/**
* Generate a set of public encryption key and private decryption key.
*
* @param keySize size of the RSA key in bits
* @return JWKPair of public and private key
*/
public static JWKPair generateEncryptionKeyPair(final int keySize) {
final String keyId = UUID.randomUUID().toString();
final JWEAlgorithm encryptionAlgorithm = JWEAlgorithm.RSA_OAEP_256;
final KeyPair keyPair = KeyGenerator.buildRSAKeyPair(keySize);
final JWK publicEncryptionKey = KeyGenerator.buildJWK(keyPair, keyId, WRAP_KEY, encryptionAlgorithm);
final JWK privateDecryptionKey = KeyGenerator.buildJWK(keyPair, keyId, UNWRAP_KEY, encryptionAlgorithm);
return new JWKPair(publicEncryptionKey.toPublicJWK(), privateDecryptionKey);
}
/**
* Generate a set of public signature verification key and private signature key.
*
* @param keySize size of the RSA key in bits
* @return JWKPair of signature and verification key
*/
public static JWKPair generateSignatureKeyPair(final int keySize) {
final String keyId = UUID.randomUUID().toString();
final JWSAlgorithm signingAlgorithm = JWSAlgorithm.PS512;
final KeyPair keyPair = KeyGenerator.buildRSAKeyPair(keySize);
final JWK publicSignatureVerificationKey = KeyGenerator.buildJWK(keyPair, keyId, VERIFY, signingAlgorithm);
final JWK privateSignatureKey = KeyGenerator.buildJWK(keyPair, keyId, SIGN, signingAlgorithm);
return new JWKPair(publicSignatureVerificationKey.toPublicJWK(), privateSignatureKey);
}
}
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