001// Copyright (C) 2002 IAIK
002// https://sic.tech/
003//
004// Copyright (C) 2003 - 2025 Stiftung Secure Information and 
005//                           Communication Technologies SIC
006// https://sic.tech/
007//
008// All rights reserved.
009//
010// This source is provided for inspection purposes and recompilation only,
011// unless specified differently in a contract with IAIK. This source has to
012// be kept in strict confidence and must not be disclosed to any third party
013// under any circumstances. Redistribution in source and binary forms, with
014// or without modification, are <not> permitted in any case!
015//
016// THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
017// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
018// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
019// ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
020// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
021// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
022// OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
023// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
024// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
025// OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
026// SUCH DAMAGE.
027//
028// $Header: /IAIK-CMS/current/src/demo/cms/envelopedData/ExplicitAESEnvelopedDataDemo.java 3     12.02.25 17:58 Dbratko $
029// $Revision: 3 $
030//
031
032
033package demo.cms.envelopedData;
034
035import iaik.asn1.structures.AlgorithmID;
036
037import java.io.IOException;
038import java.security.NoSuchAlgorithmException;
039
040import demo.DemoUtil;
041
042
043/**
044 * Demonstrates the usage of class {@link iaik.cms.EnvelopedDataStream} and
045 * {@link iaik.cms.EnvelopedData} in explicit mode for encrypting data using the CMS type
046 * EnvelopedData with the AES cipher algorithm. The encrypted data is not included in 
047 * the EncryptedContentInfo (transferred by other means).
048 * <br>
049 * AES is used for both content encryption (according to RFC 3565) and
050 * content encryption key wrapping (according to RFC 3394).
051 * <p>
052 * This demo creates an EnvelopedData object and subsequently shows several
053 * ways that may be used for decrypting the content for some particular 
054 * recipient.
055 * <p>
056 * Keys and certificates are retrieved from the demo KeyStore ("cms.keystore") 
057 * which has to be located in your current working directory and may be
058 * created by running the {@link demo.keystore.SetupCMSKeyStore
059 * SetupCMSKeyStore} program.
060 * 
061 * @see iaik.cms.EnvelopedDataStream
062 * @see iaik.cms.EnvelopedData
063 * @see iaik.cms.RecipientInfo
064 * @see iaik.cms.KeyTransRecipientInfo
065 * @see iaik.cms.KeyAgreeRecipientInfo
066 * @see iaik.cms.KEKRecipientInfo
067 */
068public class ExplicitAESEnvelopedDataDemo extends ExplicitEnvelopedDataDemo {
069
070  
071  /**
072   * Creates an AESEnvelopedDataDemo and setups the demo certificates.
073   * <br>
074   * Keys and certificates are retrieved from the demo KeyStore ("cms.keystore")
075   * file which has to be located in your current working directory and may be
076   * created by running {@link demo.keystore.SetupCMSKeyStore
077   * SetupCMSKeyStore}.
078   * <br>
079   * AES and AES KeyWrap are used for content encryption and
080   * content encryption key wrapping.
081   *
082   * @throws IOException if an file read error occurs
083   * @throws NoSuchAlgorithmException if the requested algorithms are not supported
084   */
085  public ExplicitAESEnvelopedDataDemo() throws IOException, NoSuchAlgorithmException {
086    super((AlgorithmID)AlgorithmID.aes128_CBC.clone(),
087         (AlgorithmID)AlgorithmID.cms_aes128_wrap.clone(),
088         128);
089  }
090      
091  /**
092   * Main method.
093   *
094   * @throws IOException
095   *            if an I/O error occurs when reading required keys
096   *            and certificates from files
097   */
098  public static void main(String argv[]) throws Exception {
099
100        DemoUtil.initDemos();
101        
102        // AES with 128 bit keys 
103    System.out.println("\n***** AES-128 demo *****\n");
104    (new ExplicitAESEnvelopedDataDemo()).start();
105    // AES with 192 bit keys
106    System.out.println("\n***** AES-192 demo *****\n");
107    (new ExplicitEnvelopedDataDemo((AlgorithmID)AlgorithmID.aes192_CBC.clone(),
108                                   (AlgorithmID)AlgorithmID.cms_aes192_wrap.clone(),
109                                   192)).start();
110    // AES with 256 bit keys
111    System.out.println("\n***** AES-256 demo *****\n");
112    (new ExplicitEnvelopedDataDemo((AlgorithmID)AlgorithmID.aes256_CBC.clone(),
113                                   (AlgorithmID)AlgorithmID.cms_aes256_wrap.clone(),
114                                   256)).start();
115    
116    
117    System.out.println("\nReady!");
118    DemoUtil.waitKey();
119  }
120}