001/*
002 * The contents of this file are subject to the terms of the Common Development and
003 * Distribution License (the License). You may not use this file except in compliance with the
004 * License.
005 *
006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
007 * specific language governing permission and limitations under the License.
008 *
009 * When distributing Covered Software, include this CDDL Header Notice in each file and include
010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
011 * Header, with the fields enclosed by brackets [] replaced by your own identifying
012 * information: "Portions copyright [year] [name of copyright owner]".
013 *
014 * Copyright 2013-2015 ForgeRock AS.
015 */
016
017package org.forgerock.json.jose.jws;
018
019import org.forgerock.json.jose.jwe.EncryptedJwt;
020import org.forgerock.json.jose.jws.handlers.SigningHandler;
021import org.forgerock.json.jose.jwt.Jwt;
022import org.forgerock.json.jose.jwt.JwtClaimsSet;
023
024import java.security.Key;
025
026/**
027 * An implementation of a JWS with a nested JWE as its payload.
028 * <p>
029 * @see SignedJwt
030 * @see EncryptedJwt
031 *
032 * @since 2.0.0
033 */
034public class EncryptedThenSignedJwt extends SignedJwt {
035
036    /**
037     * Constructs a fresh, new SignedEncryptedJwt from the given JwsHeader and nested Encrypted JWT.
038     * <p>
039     * The specified private key will be used in the creation of the JWS signature.
040     *
041     * @param header The JwsHeader containing the header parameters of the JWS.
042     * @param nestedJwe The nested Encrypted JWT that will be the payload of this JWS.
043     * @param signingHandler The SigningHandler instance used to sign the JWS.
044     */
045    public EncryptedThenSignedJwt(JwsHeader header, EncryptedJwt nestedJwe, SigningHandler signingHandler) {
046        super(header, nestedJwe, signingHandler);
047    }
048
049    /**
050     * Constructs a reconstructed SignedEncryptedJwt from its constituent parts, the JwsHeader, nested Encrypted JWT,
051     * signing input and signature.
052     * <p>
053     * For use when a signed nested encrypted JWT has been reconstructed from its base64url encoded string
054     * representation and the signature needs verifying.
055     *
056     * @param header The JwsHeader containing the header parameters of the JWS.
057     * @param nestedJwe The nested Encrypted JWT that is the payload of the JWS.
058     * @param signingInput The original data that was signed, being the base64url encoding of the JWS header and
059     *                     payload concatenated using a "." character.
060     * @param signature The resulting signature of signing the signing input.
061     */
062    public EncryptedThenSignedJwt(JwsHeader header, EncryptedJwt nestedJwe, byte[] signingInput, byte[] signature) {
063        super(header, nestedJwe, signingInput, signature);
064    }
065
066    /**
067     * Gets the claims set object for the nested Encrypted JWT that is the payload of this JWS.
068     *
069     * @return {@inheritDoc}
070     * @see org.forgerock.json.jose.jwt.Jwt#getClaimsSet()
071     */
072    @Override
073    public JwtClaimsSet getClaimsSet() {
074        return ((Jwt) getPayload()).getClaimsSet();
075    }
076
077    /**
078     * Decrypts the JWE so that it Claims Set can be accessed.
079     * <p>
080     * The same private key must be given here that is the pair to the public key that was used to encrypt the JWT.
081     *
082     * @param privateKey The private key pair to the public key that encrypted the JWT.
083     */
084    public void decrypt(Key privateKey) {
085        ((EncryptedJwt) getPayload()).decrypt(privateKey);
086    }
087}