Repository navigation
Expand file tree
/
Copy pathKeyAlgorithm.java
More file actions
109 lines (100 loc) · 5.84 KB
/
Copy pathKeyAlgorithm.java
File metadata and controls
109 lines (100 loc) · 5.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
* Copyright (C) 2021 jsonwebtoken.io
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.jsonwebtoken.security;
import io.jsonwebtoken.Identifiable;
import io.jsonwebtoken.JweHeader;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.lang.Assert;
import javax.crypto.SecretKey;
import java.security.Key;
import java.util.function.Consumer;
/**
* A {@code KeyAlgorithm} produces the {@link SecretKey} used to encrypt or decrypt a JWE. The {@code KeyAlgorithm}
* used for a particular JWE is {@link #getId() identified} in the JWE's
* <a href="https://tools.ietf.org/html/rfc7516#section-4.1.1">{@code alg} header</a>. The {@code KeyAlgorithm}
* interface is JJWT's idiomatic approach to the JWE specification's
* <a href="https://tools.ietf.org/html/rfc7516#section-2">{@code Key Management Mode}</a> concept.
*
* <p>All standard Key Algorithms are defined in
* <a href="https://www.rfc-editor.org/rfc/rfc7518.html#section-4.1">JWA (RFC 7518), Section 4.1</a>,
* and they are all available as concrete instances via {@link Jwts.KEY}.</p>
*
* <p><b>"alg" identifier</b></p>
*
* <p>{@code KeyAlgorithm} extends {@code Identifiable}: the value returned from
* {@link Identifiable#getId() keyAlgorithm.getId()} will be used as the
* <a href="https://www.rfc-editor.org/rfc/rfc7516.html#section-4.1.1">JWE "alg" protected header</a> value.</p>
*
* @param <E> The type of key to use to obtain the AEAD encryption key
* @param <D> The type of key to use to obtain the AEAD decryption key
* @see Jwts.KEY
* @see <a href="https://tools.ietf.org/html/rfc7516#section-2">RFC 7561, Section 2: JWE Key (Management) Algorithms</a>
* @since 0.12.0
*/
@SuppressWarnings("JavadocLinkAsPlainText")
public interface KeyAlgorithm<E extends Key, D extends Key> extends Identifiable {
/**
* Return the {@link SecretKey} that should be used to encrypt a JWE via the request's specified
* {@link KeyRequest#getEncryptionAlgorithm() AeadAlgorithm}. The encryption key will
* be available via the result's {@link KeyResult#getKey() result.getKey()} method.
*
* <p>If the key algorithm uses key encryption or key agreement to produce an encrypted key value that must be
* included in the JWE, the encrypted key ciphertext will be available via the result's
* {@link KeyResult#getPayload() result.getPayload()} method. If the key algorithm does not produce encrypted
* key ciphertext, {@link KeyResult#getPayload() result.getPayload()} will be a non-null empty byte array.</p>
*
* @param request the {@code KeyRequest} containing information necessary to produce a {@code SecretKey} for
* {@link AeadAlgorithm AEAD} encryption.
* @return the {@link SecretKey} that should be used to encrypt a JWE via the request's specified
* {@link KeyRequest#getEncryptionAlgorithm() AeadAlgorithm}, along with any optional encrypted key ciphertext.
* @throws SecurityException if there is a problem obtaining or encrypting the AEAD {@code SecretKey}.
*/
KeyResult getEncryptionKey(KeyRequest<E> request) throws SecurityException;
default KeyResult getEncryptionKey(Consumer<KeyRequest.Params<E, ?>> p) throws SecurityException {
Assert.notNull(p, "Consumer cannot be null");
KeyRequest.Builder<E> builder = KeyRequest.builder();
p.accept(builder);
return getEncryptionKey(builder.build());
}
default KeyResult getEncryptionKey(E key, JweHeader header, AeadAlgorithm enc) throws SecurityException {
return getEncryptionKey(p -> p.payload(key).header(header).encryptionAlgorithm(enc));
}
/**
* Return the {@link SecretKey} that should be used to decrypt a JWE via the request's specified
* {@link DecryptionKeyRequest#getEncryptionAlgorithm() AeadAlgorithm}.
*
* <p>If the key algorithm used key encryption or key agreement to produce an encrypted key value, the encrypted
* key ciphertext will be available via the request's {@link DecryptionKeyRequest#getPayload() result.getPayload()}
* method. If the key algorithm did not produce encrypted key ciphertext,
* {@link DecryptionKeyRequest#getPayload() request.getPayload()} will return a non-null empty byte array.</p>
*
* @param request the {@code DecryptionKeyRequest} containing information necessary to obtain a
* {@code SecretKey} for {@link AeadAlgorithm AEAD} decryption.
* @return the {@link SecretKey} that should be used to decrypt a JWE via the request's specified
* {@link DecryptionKeyRequest#getEncryptionAlgorithm() AeadAlgorithm}.
* @throws SecurityException if there is a problem obtaining or decrypting the AEAD {@code SecretKey}.
*/
SecretKey getDecryptionKey(DecryptionKeyRequest<D> request) throws SecurityException;
default SecretKey getDecryptionKey(Consumer<DecryptionKeyRequest.Params<D, ?>> p) throws SecurityException {
Assert.notNull(p, "Consumer cannot be null");
DecryptionKeyRequest.Builder<D> builder = DecryptionKeyRequest.builder();
p.accept(builder);
return getDecryptionKey(builder.build());
}
default SecretKey getDecryptionKey(byte[] cekCiphertext, D decryptionKey, JweHeader header, AeadAlgorithm enc) throws SecurityException {
return getDecryptionKey(p -> p.payload(cekCiphertext).key(decryptionKey).header(header).encryptionAlgorithm(enc));
}
}