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/data/DataDemo.java 18 12.02.25 17:58 Dbratko $ 029// $Revision: 18 $ 030// 031 032package demo.cms.data; 033 034import iaik.cms.CMSException; 035import iaik.cms.Data; 036import iaik.cms.DataStream; 037import iaik.utils.Util; 038 039import java.io.ByteArrayInputStream; 040import java.io.ByteArrayOutputStream; 041import java.io.IOException; 042import java.io.InputStream; 043 044import demo.DemoUtil; 045 046 047 048/** 049 * Shows the usage of the IAIK-CMS Data(Stream) implementation. 050 */ 051public class DataDemo { 052 053 054 /** 055 * Default constructor. 056 */ 057 public DataDemo() { 058 System.out.println(); 059 System.out.println("**********************************************************************************"); 060 System.out.println("* DataDemo *"); 061 System.out.println("* (shows the usage of the CMS Data type implementation) *"); 062 System.out.println("**********************************************************************************"); 063 System.out.println(); 064 065 } 066 067 /** 068 * Creates a CMS <code>Data</code> object. 069 * <p> 070 * @param message the message to be sent, as byte representation 071 * @return the BER encoding of the <code>Data</code> object just created 072 * @throws CMSException if the <code>Data</code> object cannot 073 * be created 074 * @throws IOException if an I/O error occurs 075 */ 076 public byte[] createDataStream(byte[] message) throws CMSException, IOException { 077 078 System.out.println("Create a new Data message:"); 079 080 // we are testing the stream interface 081 ByteArrayInputStream is = new ByteArrayInputStream(message); 082 083 // create a new Data object which includes the data 084 DataStream data = new DataStream(is, 2048); 085 086 087 // return the Data as BER encoded byte array with block size 2048 088 ByteArrayOutputStream os = new ByteArrayOutputStream(); 089 data.writeTo(os); 090 return os.toByteArray(); 091 } 092 093 /** 094 * Parses a CMS <code>Data</code> object. 095 * 096 * @param data the <code>Data</code> object as BER encoded byte array 097 * 098 * @return the inherent content as byte array 099 * 100 * @throws CMSException if an parsing exception occurs 101 * @throws IOException if an I/O error occurs 102 */ 103 public byte[] getDataStream(byte[] data) throws CMSException, IOException { 104 105 // we are testing the stream interface 106 ByteArrayInputStream is = new ByteArrayInputStream(data); 107 // create the Data object 108 DataStream dataStream = new DataStream(is); 109 110 // get an InputStream for reading the signed content 111 InputStream content = dataStream.getInputStream(); 112 ByteArrayOutputStream os = new ByteArrayOutputStream(); 113 Util.copyStream(content, os, null); 114 115 return os.toByteArray(); 116 } 117 118 119 /** 120 * Creates a CMS <code>Data</code> object. 121 * <p> 122 * @param message the message to be sent, as byte representation 123 * @return the the DER encoded <code>Data</code> object just created 124 * @throws CMSException if the <code>Data</code> object cannot 125 * be created 126 * @throws IOException if an I/O error occurs 127 */ 128 public byte[] createData(byte[] message) throws CMSException, IOException { 129 130 System.out.println("Create a new Data message:"); 131 132 // create a new DigestedData object which includes the data 133 Data data = new Data(message); 134 // return the ASN.1 representation 135 return data.getEncoded(); 136 } 137 138 /** 139 * Parses a CMS <code>Data</code> object. 140 * 141 * @param encoding the DER encoded <code>Data</code> object 142 * 143 * @return the inherent content as byte array 144 * 145 * @throws CMSException if an parsing exception occurs 146 * @throws IOException if an I/O error occurs 147 */ 148 public byte[] getData(byte[] encoding) throws CMSException, IOException { 149 150 // create the Data object 151 Data data = new Data(new ByteArrayInputStream(encoding)); 152 153 // get and return the content 154 return data.getData(); 155 } 156 157 158 /** 159 * Tests IAIK CMS Data(Stream). 160 */ 161 public void start() { 162 // the test message 163 String m = "This is the test message."; 164 System.out.println("Test message: \""+m+"\""); 165 System.out.println(); 166 byte[] message = m.getBytes(); 167 168 try { 169 byte[] encoding; 170 byte[] received_message = null; 171 System.out.println("Stream implementation demos"); 172 System.out.println("==========================="); 173 174 // the stream implementation 175 // 176 // test CMS DataStream 177 // 178 System.out.println("\nDataStream demo [create]:\n"); 179 encoding = createDataStream(message); 180 // transmit data 181 System.out.println("\nDataStream demo [parse]:\n"); 182 received_message = getDataStream(encoding); 183 System.out.print("\nContent: "); 184 System.out.println(new String(received_message)); 185 186 187 // the non-stream implementation 188 System.out.println("\nNon-stream implementation demos"); 189 System.out.println("==============================="); 190 191 // 192 // test CMS Data 193 // 194 195 System.out.println("\nData demo [create]:\n"); 196 encoding = createData(message); 197 // transmit data 198 System.out.println("\nData demo [parse]:\n"); 199 200 received_message = getData(encoding); 201 System.out.print("\nContent: "); 202 System.out.println(new String(received_message)); 203 204 } catch (Exception ex) { 205 ex.printStackTrace(); 206 throw new RuntimeException(ex.toString()); 207 } 208 } 209 210 /** 211 * Main method. 212 */ 213 public static void main(String argv[]) throws Exception { 214 215 (new DataDemo()).start(); 216 System.out.println("\nReady!"); 217 DemoUtil.waitKey(); 218 } 219}