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 2016 ForgeRock AS. 015 */ 016 017package org.forgerock.json.jose.jwe.handlers.encryption; 018 019import java.security.Key; 020 021import org.forgerock.json.jose.exceptions.JweDecryptionException; 022import org.forgerock.json.jose.jwe.EncryptionMethod; 023import org.forgerock.json.jose.jwe.JweEncryption; 024import org.forgerock.util.annotations.VisibleForTesting; 025 026/** 027 * Supports direct encryption using a shared symmetric key. 028 */ 029public final class DirectEncryptionHandler implements EncryptionHandler { 030 private final ContentEncryptionHandler contentEncryptionHandler; 031 032 /** 033 * Constructs the direct encryption handler for the given content encryption method. 034 * 035 * @param encryptionMethod the content encryption method. 036 */ 037 public DirectEncryptionHandler(final EncryptionMethod encryptionMethod) { 038 this(ContentEncryptionHandler.getInstance(encryptionMethod)); 039 } 040 041 @VisibleForTesting 042 DirectEncryptionHandler(ContentEncryptionHandler contentEncryptionHandler) { 043 this.contentEncryptionHandler = contentEncryptionHandler; 044 } 045 046 @Override 047 public Key getContentEncryptionKey() { 048 return null; 049 } 050 051 @Override 052 public byte[] generateJWEEncryptedKey(final Key key, final Key contentEncryptionKey) { 053 // As per https://tools.ietf.org/html/rfc7518#section-4.5 an empty octet sequence is used as the JWE 054 // Encrypted Key value when using direct encryption. 055 return new byte[0]; 056 } 057 058 @Override 059 public byte[] generateInitialisationVector() { 060 return contentEncryptionHandler.generateInitialisationVector(); 061 } 062 063 @Override 064 public JweEncryption encryptPlaintext(final Key contentEncryptionKey, final byte[] initialisationVector, 065 final byte[] plaintext, final byte[] additionalAuthenticatedData) { 066 return contentEncryptionHandler.encrypt(contentEncryptionKey, initialisationVector, plaintext, 067 additionalAuthenticatedData); 068 } 069 070 @Override 071 public Key decryptContentEncryptionKey(final Key key, final byte[] encryptedContentEncryptionKey) { 072 if (encryptedContentEncryptionKey != null && encryptedContentEncryptionKey.length != 0) { 073 throw new JweDecryptionException(); 074 } 075 return key; 076 } 077 078 @Override 079 public byte[] decryptCiphertext(final Key contentEncryptionKey, final byte[] initialisationVector, 080 final byte[] ciphertext, 081 final byte[] authenticationTag, final byte[] additionalAuthenticatedData) { 082 return contentEncryptionHandler.decrypt(contentEncryptionKey, initialisationVector, 083 new JweEncryption(ciphertext, authenticationTag), additionalAuthenticatedData); 084 } 085}