Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* nimbus-jose-jwt
*
* Copyright 2012-2023, Connect2id Ltd and contributors.
*
* 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 com.nimbusds.jose;
import com.nimbusds.jose.JWEObject.State;
import com.nimbusds.jose.util.Base64URL;
import com.nimbusds.jose.util.JSONArrayUtils;
import com.nimbusds.jose.util.JSONObjectUtils;
import net.jcip.annotations.Immutable;
import net.jcip.annotations.ThreadSafe;
import java.nio.charset.StandardCharsets;
import java.text.ParseException;
import java.util.*;
/**
* JSON Web Encryption (JWE) secured object with
* JSON
* serialisation.
*
*
This class is thread-safe.
*
* @author Egor Puzanov
* @author Vladimir Dzhuvinov
* @version 2024-04-20
*/
@ThreadSafe
public class JWEObjectJSON extends JOSEObjectJSON {
private static final long serialVersionUID = 1L;
/**
* Individual recipient in a JWE object serialisable to JSON.
*/
@Immutable
public static final class Recipient {
/**
* The per-recipient unprotected header.
*/
private final UnprotectedHeader unprotectedHeader;
/**
* The encrypted key, {@code null} if none.
*/
private final Base64URL encryptedKey;
/**
* Creates a new parsed recipient.
*
* @param unprotectedHeader The per-recipient unprotected
* header, {@code null} if none.
* @param encryptedKey The encrypted key, {@code null} if
* none.
*/
public Recipient(final UnprotectedHeader unprotectedHeader,
final Base64URL encryptedKey) {
this.unprotectedHeader = unprotectedHeader;
this.encryptedKey = encryptedKey;
}
/**
* Returns the per-recipient unprotected header.
*
* @return The per-recipient unprotected header, {@code null}
* if none.
*/
public UnprotectedHeader getUnprotectedHeader() {
return unprotectedHeader;
}
/**
* Returns the encrypted key.
*
* @return The encryptedKey.
*/
public Base64URL getEncryptedKey() {
return encryptedKey;
}
/**
* Returns a JSON object representation for use in the general
* and flattened serialisations.
*
* @return The JSON object.
*/
public Map toJSONObject() {
Map jsonObject = JSONObjectUtils.newJSONObject();
if (unprotectedHeader != null && ! unprotectedHeader.getIncludedParams().isEmpty()) {
jsonObject.put("header", unprotectedHeader.toJSONObject());
}
if (encryptedKey != null) {
jsonObject.put("encrypted_key", encryptedKey.toString());
}
return jsonObject;
}
/**
* Parses a recipients object from the specified JSON object.
*
* @param jsonObject The JSON object to parse. Must not be
* {@code null}.
*
* @return The recipient object.
*
* @throws ParseException If the string couldn't be parsed to a
* JWE object.
*/
public static Recipient parse(final Map jsonObject)
throws ParseException {
final UnprotectedHeader header = UnprotectedHeader.parse(JSONObjectUtils.getJSONObject(jsonObject, "header"));
final Base64URL encryptedKey = JSONObjectUtils.getBase64URL(jsonObject, "encrypted_key");
return new Recipient(header, encryptedKey);
}
}
/**
* The JWE protected header.
*/
private final JWEHeader header;
/**
* The shared unprotected header.
*/
private UnprotectedHeader unprotectedHeader;
/**
* The recipients list.
*/
private final List recipients = new LinkedList<>();
/**
* The initialisation vector, {@code null} if not generated or
* applicable.
*/
private Base64URL iv;
/**
* The cipher text, {@code null} if not computed.
*/
private Base64URL cipherText;
/**
* The authentication tag, {@code null} if not computed or applicable.
*/
private Base64URL authTag;
/**
* The additional authenticated data, {@code null} if not computed or
* applicable.
*/
private final byte[] aad;
/**
* The JWE object state.
*/
private JWEObject.State state;
/**
* Creates a new JWE JSON object from the specified JWE object with
* compact serialisation. The initial state is copied from the JWE
* object.
*
* @param jweObject The JWE object. Must not be {@code null}.
*/
public JWEObjectJSON(final JWEObject jweObject) {
super(jweObject.getPayload());
this.header = jweObject.getHeader();
this.aad = null;
this.iv = jweObject.getIV();
this.cipherText = jweObject.getCipherText();
this.authTag = jweObject.getAuthTag();
if (jweObject.getState() == JWEObject.State.ENCRYPTED) {
this.recipients.add(new Recipient(null, jweObject.getEncryptedKey()));
this.state = State.ENCRYPTED;
} else if (jweObject.getState() == JWEObject.State.DECRYPTED) {
this.recipients.add(new Recipient(null, jweObject.getEncryptedKey()));
this.state = State.DECRYPTED;
} else {
this.state = State.UNENCRYPTED;
}
}
/**
* Creates a new to-be-encrypted JSON Web Encryption (JWE) object with
* the specified JWE protected header and payload. The initial state
* will be {@link State#UNENCRYPTED unencrypted}.
*
* @param header The JWE protected header. Must not be {@code null}.
* @param payload The payload. Must not be {@code null}.
*/
public JWEObjectJSON(final JWEHeader header, final Payload payload) {
this(header, payload, null, null);
}
/**
* Creates a new to-be-encrypted JSON Web Encryption (JWE) object with
* the specified JWE protected header, payload and Additional
* Authenticated Data (AAD). The initial state will be
* {@link State#UNENCRYPTED unencrypted}.
*
* @param header The JWE protected header. Must not be
* {@code null}.
* @param payload The payload. Must not be {@code null}.
* @param unprotectedHeader The shared unprotected header, empty or
* {@code null} if none.
* @param aad The additional authenticated data (AAD),
* {@code null} if none.
*/
public JWEObjectJSON(final JWEHeader header,
final Payload payload,
final UnprotectedHeader unprotectedHeader,
final byte[] aad) {
super(payload);
this.header = Objects.requireNonNull(header);
setPayload(Objects.requireNonNull(payload));
this.unprotectedHeader = unprotectedHeader;
this.aad = aad;
this.cipherText = null;
this.state = State.UNENCRYPTED;
}
/**
* Creates a new encrypted JSON Web Encryption (JWE) object. The state
* will be {@link State#ENCRYPTED encrypted}.
*
* @param header The JWE protected header. Must not be
* {@code null}.
* @param cipherText The cipher text. Must not be {@code null}.
* @param iv The initialisation vector, empty or
* {@code null} if none.
* @param authTag The authentication tag, empty or
* {@code null} if none.
* @param recipients The recipients list. Must not be
* {@code null}.
* @param unprotectedHeader The shared unprotected header, empty or
* {@code null} if none.
* @param aad The additional authenticated data. Must not
* be {@code null}.
*
*/
public JWEObjectJSON(final JWEHeader header,
final Base64URL cipherText,
final Base64URL iv,
final Base64URL authTag,
final List recipients,
final UnprotectedHeader unprotectedHeader,
final byte[] aad) {
super(null); // Payload not decrypted yet, must be null
this.header = Objects.requireNonNull(header);
this.recipients.addAll(recipients);
this.unprotectedHeader = unprotectedHeader;
this.aad = aad;
this.iv = iv;
this.cipherText = Objects.requireNonNull(cipherText);
this.authTag = authTag;
state = State.ENCRYPTED; // but not decrypted yet!
}
/**
* Returns the JWE protected header of this JWE object.
*
* @return The JWE protected header.
*/
public JWEHeader getHeader() {
return header;
}
/**
* Returns the shared unprotected header of this JWE object.
*
* @return The shared unprotected header, empty or {@code null} if
* none.
*/
public UnprotectedHeader getUnprotectedHeader() {
return unprotectedHeader;
}
/**
* Returns the encrypted key of this JWE object.
*
* @return The encrypted key, {@code null} not applicable or the JWE
* object has not been encrypted yet.
*/
public Base64URL getEncryptedKey() {
if (recipients.isEmpty()) {
return null;
} else if (recipients.size() == 1) {
return recipients.get(0).getEncryptedKey();
}
List