View Javadoc
1   /*
2    * The contents of this file are subject to the terms of the Common Development and
3    * Distribution License (the License). You may not use this file except in compliance with the
4    * License.
5    *
6    * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
7    * specific language governing permission and limitations under the License.
8    *
9    * When distributing Covered Software, include this CDDL Header Notice in each file and include
10   * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
11   * Header, with the fields enclosed by brackets [] replaced by your own identifying
12   * information: "Portions copyright [year] [name of copyright owner]".
13   *
14   * Copyright 2013-2015 ForgeRock AS.
15   */
16  
17  package org.forgerock.json.jose.jws;
18  
19  import org.forgerock.json.jose.jwe.EncryptedJwt;
20  import org.forgerock.json.jose.jws.handlers.SigningHandler;
21  import org.forgerock.json.jose.jwt.Jwt;
22  import org.forgerock.json.jose.jwt.JwtClaimsSet;
23  
24  import java.security.Key;
25  
26  /**
27   * An implementation of a JWS with a nested JWE as its payload.
28   * <p>
29   * @see SignedJwt
30   * @see EncryptedJwt
31   *
32   * @since 2.0.0
33   */
34  public class EncryptedThenSignedJwt extends SignedJwt {
35  
36      /**
37       * Constructs a fresh, new SignedEncryptedJwt from the given JwsHeader and nested Encrypted JWT.
38       * <p>
39       * The specified private key will be used in the creation of the JWS signature.
40       *
41       * @param header The JwsHeader containing the header parameters of the JWS.
42       * @param nestedJwe The nested Encrypted JWT that will be the payload of this JWS.
43       * @param signingHandler The SigningHandler instance used to sign the JWS.
44       */
45      public EncryptedThenSignedJwt(JwsHeader header, EncryptedJwt nestedJwe, SigningHandler signingHandler) {
46          super(header, nestedJwe, signingHandler);
47      }
48  
49      /**
50       * Constructs a reconstructed SignedEncryptedJwt from its constituent parts, the JwsHeader, nested Encrypted JWT,
51       * signing input and signature.
52       * <p>
53       * For use when a signed nested encrypted JWT has been reconstructed from its base64url encoded string
54       * representation and the signature needs verifying.
55       *
56       * @param header The JwsHeader containing the header parameters of the JWS.
57       * @param nestedJwe The nested Encrypted JWT that is the payload of the JWS.
58       * @param signingInput The original data that was signed, being the base64url encoding of the JWS header and
59       *                     payload concatenated using a "." character.
60       * @param signature The resulting signature of signing the signing input.
61       */
62      public EncryptedThenSignedJwt(JwsHeader header, EncryptedJwt nestedJwe, byte[] signingInput, byte[] signature) {
63          super(header, nestedJwe, signingInput, signature);
64      }
65  
66      /**
67       * Gets the claims set object for the nested Encrypted JWT that is the payload of this JWS.
68       *
69       * @return {@inheritDoc}
70       * @see org.forgerock.json.jose.jwt.Jwt#getClaimsSet()
71       */
72      @Override
73      public JwtClaimsSet getClaimsSet() {
74          return ((Jwt) getPayload()).getClaimsSet();
75      }
76  
77      /**
78       * Decrypts the JWE so that it Claims Set can be accessed.
79       * <p>
80       * The same private key must be given here that is the pair to the public key that was used to encrypt the JWT.
81       *
82       * @param privateKey The private key pair to the public key that encrypted the JWT.
83       */
84      public void decrypt(Key privateKey) {
85          ((EncryptedJwt) getPayload()).decrypt(privateKey);
86      }
87  }