如何将 Ed25519 / Curve25519 与 SpringBoot 一起使用以实现 JWK、JWE、JWS 和 JWT 安全性?

问题描述 投票:0回答:1

tl;博士

  • 需要确保并展示如何使用名为 Ed25519 / Curve25519 以及 Spring-Boot 授权服务器的椭圆曲线的资源。

目标

设置

  • Java:21
  • Spring 启动:v3.2.x
  • spring-security-oauth2-授权服务器:1.2.x

预研究

  • Spring-Boot在内部使用 com.nimbusds.jose 库(自 ~2020 年起,~v5.1)。
    • “2018 年之前”也有人提出过同样的问题(据我所知)
    • 支持RSA
    • 支持椭圆曲线(一般)
      • “Ed25519 / Curve25519”的使用 - 未知

仍然有多个类提供围绕“Ed25519 / Curve25519”的 EC 功能

密钥生成,兼容
java.security.KeyPair

    static KeyPair generateEcKeyPairUsingEd25519() {
        KeyPair keyPair;
        try {
            KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("Ed25519");
            keyPair = keyPairGenerator.generateKeyPair();
        } catch (NoSuchAlgorithmException e) {
            throw new IllegalStateException("JVM does not support Ed25519 algorithm", e);
        }
        return keyPair;
    }

还提供接口

        import java.security.interfaces.EdECPrivateKey;
        import java.security.interfaces.EdECPublicKey;

        KeyPair ecKeyPair = generateEcKeyPairUsingEd25519()

        EdECPublicKey edEcPublicKey = (EdECPublicKey) ecKeyPair.getPublic();
        EdECPrivateKey edEcPrivateKey = (EdECPrivateKey) ecKeyPair.getPrivate();

再放心

  • 这已经是兔子洞太深了吗?
  • Spring-Boot是否已在我没有注意到的情况下将 Ed25519 用于 JWK、JWE、JWS 和 JWT?
  • 有合适的
  • Ed25519 / Curve25519 参考实现吗?
问题

Spring 启动:v3.2.x

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-oauth2-resource-server</artifactId> <version>${spring-boot.version}</version> </dependency>
据我所知,所使用的

com.nimbusds.jose.*

支持JWK、JWE、JWS和JWT以及RSA和EC一般。

可以生成如上所示的密钥对,但据我所知,它们不能在下面的示例中使用。

示例

@Bean public JwtDecoder jwtDecoder() { KeyPair keyPair = this.getKeyPair(); RSAPublicKey rsaPublicKey = (RSAPublicKey) keyPair.getPublic(); return NimbusJwtDecoder.withPublicKey(rsaPublicKey).build(); } @Bean JwtEncoder jwtEncoder() { KeyPair keyPair = this.getKeyPair(); RSAPublicKey rsaPublicKey = (RSAPublicKey) keyPair.getPublic(); RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) keyPair.getPrivate(); JWK jwk = new RSAKey.Builder(rsaPublicKey) .privateKey(rsaPrivateKey).build(); JWKSource<SecurityContext> jwks = new ImmutableJWKSet<>(new JWKSet(jwk)); return new NimbusJwtEncoder(jwks); }
终于

    如何将 Ed25519 / Curve25519 与 SpringBoot 结合使用以实现 JWK、JWE、JWS 和 JWT 安全性?
java spring-boot jwt cryptography ed25519
1个回答
1
投票
您可以通过添加

Ed25519

 的配置来应用集成

import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.NoSuchAlgorithmException; import java.security.interfaces.EdECPublicKey; import java.security.interfaces.EdECPrivateKey; import java.security.spec.EdECPublicKeySpec; import java.security.spec.EdECPrivateKeySpec; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.security.oauth2.jwt.JwtDecoder; import org.springframework.security.oauth2.jwt.NimbusJwtDecoder; import org.springframework.security.oauth2.jwt.NimbusJwtEncoder; import org.springframework.security.oauth2.jwt.JwtEncoder; import org.springframework.security.oauth2.jwt.JWKSource; import org.springframework.security.oauth2.jwt.JWKSet; import org.springframework.security.oauth2.jwt.JWK; import org.springframework.security.core.context.SecurityContext; import org.springframework.security.oauth2.jose.jwk.JWKSource; import org.springframework.security.oauth2.jose.jwk.JWKSet; import org.springframework.security.oauth2.jose.jwk.JWK; import org.springframework.security.core.context.SecurityContext; import java.security.spec.InvalidKeySpecException; @Configuration public class JwtConfig { @Bean public JwtDecoder jwtDecoder() { KeyPair keyPair = Ed25519KeyPairGenerator.generateEcKeyPairUsingEd25519(); EdECPublicKey edEcPublicKey = (EdECPublicKey) keyPair.getPublic(); return NimbusJwtDecoder.withPublicKey(edEcPublicKey).build(); } @Bean public JwtEncoder jwtEncoder() { KeyPair keyPair = Ed25519KeyPairGenerator.generateEcKeyPairUsingEd25519(); EdECPublicKey edEcPublicKey = (EdECPublicKey) keyPair.getPublic(); EdECPrivateKey edEcPrivateKey = (EdECPrivateKey) keyPair.getPrivate(); try { EdECPublicKeySpec publicKeySpec = new EdECPublicKeySpec(edEcPublicKey.getEncoded()); EdECPrivateKeySpec privateKeySpec = new EdECPrivateKeySpec(edEcPrivateKey.getS(), publicKeySpec); JWK jwk = new JWK.Builder(publicKeySpec).privateKey(privateKeySpec).build(); JWKSource<SecurityContext> jwks = new ImmutableJWKSet<>(new JWKSet(jwk)); return new NimbusJwtEncoder(jwks); } catch (InvalidKeySpecException e) { throw new IllegalStateException("Error while creating EdDSA key spec", e); } } }
并生成

KeyPairs

并与bean集成。我记得 NullPointerExceptions 有风险,但我现在找不到资源。

import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.NoSuchAlgorithmException; public class Ed25519KeyPairGenerator { public static KeyPair generateEcKeyPairUsingEd25519() { try { KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("Ed25519"); return keyPairGenerator.generateKeyPair(); } catch (NoSuchAlgorithmException e) { throw new IllegalStateException("JVM does not support Ed25519 algorithm", e); } } }
    
© www.soinside.com 2019 - 2024. All rights reserved.