|
15 | 15 | */
|
16 | 16 | package io.jsonwebtoken;
|
17 | 17 |
|
| 18 | +import io.jsonwebtoken.lang.Classes; |
| 19 | +import io.jsonwebtoken.lang.Registry; |
| 20 | +import io.jsonwebtoken.security.KeyPairBuilderSupplier; |
| 21 | +import io.jsonwebtoken.security.MacAlgorithm; |
| 22 | +import io.jsonwebtoken.security.SecureDigestAlgorithm; |
| 23 | + |
| 24 | +import java.security.Key; |
| 25 | + |
18 | 26 | /**
|
19 | 27 | * An expanded (not compact/serialized) Signed JSON Web Token.
|
20 | 28 | *
|
|
23 | 31 | */
|
24 | 32 | public interface Jws<P> extends ProtectedJwt<JwsHeader, P> {
|
25 | 33 |
|
| 34 | + /** |
| 35 | + * Constants for all JWA (RFC 7518) standard <a href="https://www.rfc-editor.org/rfc/rfc7518.html#section-3"> |
| 36 | + * Cryptographic Algorithms for Digital Signatures and MACs</a> defined in the |
| 37 | + * <a href="https://www.rfc-editor.org/rfc/rfc7518.html#section-7.1">JSON Web Signature and Encryption Algorithms |
| 38 | + * Registry</a>. Each standard algorithm is available as a ({@code public static final}) constant for |
| 39 | + * direct type-safe reference in application code. For example: |
| 40 | + * <blockquote><pre> |
| 41 | + * Jwts.builder() |
| 42 | + * // ... etc ... |
| 43 | + * .signWith(aKey, <b>Jws.alg.HS512</b>) // or RS512, PS256, EdDSA, etc... |
| 44 | + * .build();</pre></blockquote> |
| 45 | + * <p>They are also available together as a {@link Registry} instance via the {@link #registry()} method.</p> |
| 46 | + * |
| 47 | + * @see #registry() |
| 48 | + * @since JJWT_RELEASE_VERSION |
| 49 | + */ |
| 50 | + final class alg { |
| 51 | + |
| 52 | + private static final String IMPL_CLASSNAME = "io.jsonwebtoken.impl.security.StandardSecureDigestAlgorithms"; |
| 53 | + private static final Registry<String, SecureDigestAlgorithm<?, ?>> REGISTRY = Classes.newInstance(IMPL_CLASSNAME); |
| 54 | + |
| 55 | + //prevent instantiation |
| 56 | + private alg() { |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Returns all standard JWA <a href="https://www.rfc-editor.org/rfc/rfc7518.html#section-3">Cryptographic |
| 61 | + * Algorithms for Digital Signatures and MACs</a> defined in the |
| 62 | + * <a href="https://www.rfc-editor.org/rfc/rfc7518.html#section-7.1">JSON Web Signature and Encryption |
| 63 | + * Algorithms Registry</a>. |
| 64 | + * |
| 65 | + * @return all standard JWA digital signature and MAC algorithms. |
| 66 | + */ |
| 67 | + public static Registry<String, SecureDigestAlgorithm<?, ?>> registry() { |
| 68 | + return REGISTRY; |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * The "none" signature algorithm as defined by |
| 73 | + * <a href="https://www.rfc-editor.org/rfc/rfc7518.html#section-3.6">RFC 7518, Section 3.6</a>. This algorithm |
| 74 | + * is used only when creating unsecured (not integrity protected) JWSs and is not usable in any other scenario. |
| 75 | + * Any attempt to call its methods will result in an exception being thrown. |
| 76 | + */ |
| 77 | + public static final SecureDigestAlgorithm<Key, Key> NONE = Jwts.get(REGISTRY, "none"); |
| 78 | + |
| 79 | + /** |
| 80 | + * {@code HMAC using SHA-256} message authentication algorithm as defined by |
| 81 | + * <a href="https://www.rfc-editor.org/rfc/rfc7518.html#section-3.2">RFC 7518, Section 3.2</a>. This algorithm |
| 82 | + * requires a 256-bit (32 byte) key. |
| 83 | + */ |
| 84 | + public static final MacAlgorithm HS256 = Jwts.get(REGISTRY, "HS256"); |
| 85 | + |
| 86 | + /** |
| 87 | + * {@code HMAC using SHA-384} message authentication algorithm as defined by |
| 88 | + * <a href="https://www.rfc-editor.org/rfc/rfc7518.html#section-3.2">RFC 7518, Section 3.2</a>. This algorithm |
| 89 | + * requires a 384-bit (48 byte) key. |
| 90 | + */ |
| 91 | + public static final MacAlgorithm HS384 = Jwts.get(REGISTRY, "HS384"); |
| 92 | + |
| 93 | + /** |
| 94 | + * {@code HMAC using SHA-512} message authentication algorithm as defined by |
| 95 | + * <a href="https://www.rfc-editor.org/rfc/rfc7518.html#section-3.2">RFC 7518, Section 3.2</a>. This algorithm |
| 96 | + * requires a 512-bit (64 byte) key. |
| 97 | + */ |
| 98 | + public static final MacAlgorithm HS512 = Jwts.get(REGISTRY, "HS512"); |
| 99 | + |
| 100 | + /** |
| 101 | + * {@code RSASSA-PKCS1-v1_5 using SHA-256} signature algorithm as defined by |
| 102 | + * <a href="https://www.rfc-editor.org/rfc/rfc7518.html#section-3.3">RFC 7518, Section 3.3</a>. This algorithm |
| 103 | + * requires a 2048-bit key. |
| 104 | + */ |
| 105 | + public static final io.jsonwebtoken.security.SignatureAlgorithm RS256 = Jwts.get(REGISTRY, "RS256"); |
| 106 | + |
| 107 | + /** |
| 108 | + * {@code RSASSA-PKCS1-v1_5 using SHA-384} signature algorithm as defined by |
| 109 | + * <a href="https://www.rfc-editor.org/rfc/rfc7518.html#section-3.3">RFC 7518, Section 3.3</a>. This algorithm |
| 110 | + * requires a 2048-bit key, but the JJWT team recommends a 3072-bit key. |
| 111 | + */ |
| 112 | + public static final io.jsonwebtoken.security.SignatureAlgorithm RS384 = Jwts.get(REGISTRY, "RS384"); |
| 113 | + |
| 114 | + /** |
| 115 | + * {@code RSASSA-PKCS1-v1_5 using SHA-512} signature algorithm as defined by |
| 116 | + * <a href="https://www.rfc-editor.org/rfc/rfc7518.html#section-3.3">RFC 7518, Section 3.3</a>. This algorithm |
| 117 | + * requires a 2048-bit key, but the JJWT team recommends a 4096-bit key. |
| 118 | + */ |
| 119 | + public static final io.jsonwebtoken.security.SignatureAlgorithm RS512 = Jwts.get(REGISTRY, "RS512"); |
| 120 | + |
| 121 | + /** |
| 122 | + * {@code RSASSA-PSS using SHA-256 and MGF1 with SHA-256} signature algorithm as defined by |
| 123 | + * <a href="https://www.rfc-editor.org/rfc/rfc7518.html#section-3.5">RFC 7518, Section 3.5</a><b><sup>1</sup></b>. |
| 124 | + * This algorithm requires a 2048-bit key. |
| 125 | + * |
| 126 | + * <p><b><sup>1</sup></b> Requires Java 11 or a compatible JCA Provider (like BouncyCastle) in the runtime |
| 127 | + * classpath. If on Java 10 or earlier, BouncyCastle will be used automatically if found in the runtime |
| 128 | + * classpath.</p> |
| 129 | + */ |
| 130 | + public static final io.jsonwebtoken.security.SignatureAlgorithm PS256 = Jwts.get(REGISTRY, "PS256"); |
| 131 | + |
| 132 | + /** |
| 133 | + * {@code RSASSA-PSS using SHA-384 and MGF1 with SHA-384} signature algorithm as defined by |
| 134 | + * <a href="https://www.rfc-editor.org/rfc/rfc7518.html#section-3.5">RFC 7518, Section 3.5</a><b><sup>1</sup></b>. |
| 135 | + * This algorithm requires a 2048-bit key, but the JJWT team recommends a 3072-bit key. |
| 136 | + * |
| 137 | + * <p><b><sup>1</sup></b> Requires Java 11 or a compatible JCA Provider (like BouncyCastle) in the runtime |
| 138 | + * classpath. If on Java 10 or earlier, BouncyCastle will be used automatically if found in the runtime |
| 139 | + * classpath.</p> |
| 140 | + */ |
| 141 | + public static final io.jsonwebtoken.security.SignatureAlgorithm PS384 = Jwts.get(REGISTRY, "PS384"); |
| 142 | + |
| 143 | + /** |
| 144 | + * {@code RSASSA-PSS using SHA-512 and MGF1 with SHA-512} signature algorithm as defined by |
| 145 | + * <a href="https://www.rfc-editor.org/rfc/rfc7518.html#section-3.5">RFC 7518, Section 3.5</a><b><sup>1</sup></b>. |
| 146 | + * This algorithm requires a 2048-bit key, but the JJWT team recommends a 4096-bit key. |
| 147 | + * |
| 148 | + * <p><b><sup>1</sup></b> Requires Java 11 or a compatible JCA Provider (like BouncyCastle) in the runtime |
| 149 | + * classpath. If on Java 10 or earlier, BouncyCastle will be used automatically if found in the runtime |
| 150 | + * classpath.</p> |
| 151 | + */ |
| 152 | + public static final io.jsonwebtoken.security.SignatureAlgorithm PS512 = Jwts.get(REGISTRY, "PS512"); |
| 153 | + |
| 154 | + /** |
| 155 | + * {@code ECDSA using P-256 and SHA-256} signature algorithm as defined by |
| 156 | + * <a href="https://www.rfc-editor.org/rfc/rfc7518.html#section-3.4">RFC 7518, Section 3.4</a>. This algorithm |
| 157 | + * requires a 256-bit key. |
| 158 | + */ |
| 159 | + public static final io.jsonwebtoken.security.SignatureAlgorithm ES256 = Jwts.get(REGISTRY, "ES256"); |
| 160 | + |
| 161 | + /** |
| 162 | + * {@code ECDSA using P-384 and SHA-384} signature algorithm as defined by |
| 163 | + * <a href="https://www.rfc-editor.org/rfc/rfc7518.html#section-3.4">RFC 7518, Section 3.4</a>. This algorithm |
| 164 | + * requires a 384-bit key. |
| 165 | + */ |
| 166 | + public static final io.jsonwebtoken.security.SignatureAlgorithm ES384 = Jwts.get(REGISTRY, "ES384"); |
| 167 | + |
| 168 | + /** |
| 169 | + * {@code ECDSA using P-521 and SHA-512} signature algorithm as defined by |
| 170 | + * <a href="https://www.rfc-editor.org/rfc/rfc7518.html#section-3.4">RFC 7518, Section 3.4</a>. This algorithm |
| 171 | + * requires a 521-bit key. |
| 172 | + */ |
| 173 | + public static final io.jsonwebtoken.security.SignatureAlgorithm ES512 = Jwts.get(REGISTRY, "ES512"); |
| 174 | + |
| 175 | + /** |
| 176 | + * {@code EdDSA} signature algorithm defined by |
| 177 | + * <a href="https://www.rfc-editor.org/rfc/rfc8037#section-3.1">RFC 8037, Section 3.1</a> that requires |
| 178 | + * either {@code Ed25519} or {@code Ed448} Edwards Elliptic Curve<sup><b>1</b></sup> keys. |
| 179 | + * |
| 180 | + * <p><b>KeyPair Generation</b></p> |
| 181 | + * |
| 182 | + * <p>This instance's {@link KeyPairBuilderSupplier#keyPair() keyPair()} builder creates {@code Ed448} keys, |
| 183 | + * and is essentially an alias for |
| 184 | + * <code>{@link io.jsonwebtoken.security.Jwks.CRV Jwks.CRV}.{@link io.jsonwebtoken.security.Jwks.CRV#Ed448 Ed448}.{@link KeyPairBuilderSupplier#keyPair() keyPair()}</code>.</p> |
| 185 | + * |
| 186 | + * <p>If you would like to generate an {@code Ed25519} {@code KeyPair} for use with the {@code EdDSA} algorithm, |
| 187 | + * you may use the |
| 188 | + * <code>{@link io.jsonwebtoken.security.Jwks.CRV Jwks.CRV}.{@link io.jsonwebtoken.security.Jwks.CRV#Ed25519 Ed25519}.{@link KeyPairBuilderSupplier#keyPair() keyPair()}</code> |
| 189 | + * builder instead.</p> |
| 190 | + * |
| 191 | + * <p><b><sup>1</sup>This algorithm requires at least JDK 15 or a compatible JCA Provider (like BouncyCastle) in the runtime |
| 192 | + * classpath.</b></p> |
| 193 | + */ |
| 194 | + public static final io.jsonwebtoken.security.SignatureAlgorithm EdDSA = Jwts.get(REGISTRY, "EdDSA"); |
| 195 | + } |
| 196 | + |
26 | 197 | /**
|
27 | 198 | * Visitor implementation that ensures the visited JWT is a JSON Web Signature ('JWS') message with a
|
28 | 199 | * cryptographically authenticated/verified {@code byte[]} array payload, and rejects all others with an
|
|
0 commit comments