001 // Copyright (C) 2002 IAIK 002 // https://jce.iaik.tugraz.at 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 // Redistribution and use in source and binary forms, with or without 011 // modification, are permitted provided that the following conditions 012 // are met: 013 // 1. Redistributions of source code must retain the above copyright 014 // notice, this list of conditions and the following disclaimer. 015 // 2. Redistributions in binary form must reproduce the above copyright 016 // notice, this list of conditions and the following disclaimer in the 017 // documentation and/or other materials provided with the distribution. 018 // 019 // THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 020 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 021 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 022 // ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 023 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 024 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 025 // OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 026 // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 027 // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 028 // OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 029 // SUCH DAMAGE. 030 031 // Copyright (C) 2002 IAIK 032 // https://sic.tech/ 033 // 034 // Copyright (C) 2003 - 2025 Stiftung Secure Information and 035 // Communication Technologies SIC 036 // https://sic.tech/ 037 // 038 // All rights reserved. 039 // 040 // This source is provided for inspection purposes and recompilation only, 041 // unless specified differently in a contract with IAIK. This source has to 042 // be kept in strict confidence and must not be disclosed to any third party 043 // under any circumstances. Redistribution in source and binary forms, with 044 // or without modification, are <not> permitted in any case! 045 // 046 // THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 047 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 048 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 049 // ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 050 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 051 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 052 // OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 053 // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 054 // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 055 // OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 056 // SUCH DAMAGE. 057 // 058 // $Header: /IAIK-CMS/current/src/demo/cms/data/DataDemo.java 18 12.02.25 17:58 Dbratko $ 059 // $Revision: 18 $ 060 // 061 062 package demo.cms.data; 063 064 import iaik.cms.CMSException; 065 import iaik.cms.Data; 066 import iaik.cms.DataStream; 067 import iaik.utils.Util; 068 069 import java.io.ByteArrayInputStream; 070 import java.io.ByteArrayOutputStream; 071 import java.io.IOException; 072 import java.io.InputStream; 073 074 import demo.DemoUtil; 075 076 077 078 /** 079 * Shows the usage of the IAIK-CMS Data(Stream) implementation. 080 */ 081 public class DataDemo { 082 083 084 /** 085 * Default constructor. 086 */ 087 public DataDemo() { 088 System.out.println(); 089 System.out.println("**********************************************************************************"); 090 System.out.println("* DataDemo *"); 091 System.out.println("* (shows the usage of the CMS Data type implementation) *"); 092 System.out.println("**********************************************************************************"); 093 System.out.println(); 094 095 } 096 097 /** 098 * Creates a CMS <code>Data</code> object. 099 * <p> 100 * @param message the message to be sent, as byte representation 101 * @return the BER encoding of the <code>Data</code> object just created 102 * @throws CMSException if the <code>Data</code> object cannot 103 * be created 104 * @throws IOException if an I/O error occurs 105 */ 106 public byte[] createDataStream(byte[] message) throws CMSException, IOException { 107 108 System.out.println("Create a new Data message:"); 109 110 // we are testing the stream interface 111 ByteArrayInputStream is = new ByteArrayInputStream(message); 112 113 // create a new Data object which includes the data 114 DataStream data = new DataStream(is, 2048); 115 116 117 // return the Data as BER encoded byte array with block size 2048 118 ByteArrayOutputStream os = new ByteArrayOutputStream(); 119 data.writeTo(os); 120 return os.toByteArray(); 121 } 122 123 /** 124 * Parses a CMS <code>Data</code> object. 125 * 126 * @param data the <code>Data</code> object as BER encoded byte array 127 * 128 * @return the inherent content as byte array 129 * 130 * @throws CMSException if an parsing exception occurs 131 * @throws IOException if an I/O error occurs 132 */ 133 public byte[] getDataStream(byte[] data) throws CMSException, IOException { 134 135 // we are testing the stream interface 136 ByteArrayInputStream is = new ByteArrayInputStream(data); 137 // create the Data object 138 DataStream dataStream = new DataStream(is); 139 140 // get an InputStream for reading the signed content 141 InputStream content = dataStream.getInputStream(); 142 ByteArrayOutputStream os = new ByteArrayOutputStream(); 143 Util.copyStream(content, os, null); 144 145 return os.toByteArray(); 146 } 147 148 149 /** 150 * Creates a CMS <code>Data</code> object. 151 * <p> 152 * @param message the message to be sent, as byte representation 153 * @return the the DER encoded <code>Data</code> object just created 154 * @throws CMSException if the <code>Data</code> object cannot 155 * be created 156 * @throws IOException if an I/O error occurs 157 */ 158 public byte[] createData(byte[] message) throws CMSException, IOException { 159 160 System.out.println("Create a new Data message:"); 161 162 // create a new DigestedData object which includes the data 163 Data data = new Data(message); 164 // return the ASN.1 representation 165 return data.getEncoded(); 166 } 167 168 /** 169 * Parses a CMS <code>Data</code> object. 170 * 171 * @param encoding the DER encoded <code>Data</code> object 172 * 173 * @return the inherent content as byte array 174 * 175 * @throws CMSException if an parsing exception occurs 176 * @throws IOException if an I/O error occurs 177 */ 178 public byte[] getData(byte[] encoding) throws CMSException, IOException { 179 180 // create the Data object 181 Data data = new Data(new ByteArrayInputStream(encoding)); 182 183 // get and return the content 184 return data.getData(); 185 } 186 187 188 /** 189 * Tests IAIK CMS Data(Stream). 190 */ 191 public void start() { 192 // the test message 193 String m = "This is the test message."; 194 System.out.println("Test message: \""+m+"\""); 195 System.out.println(); 196 byte[] message = m.getBytes(); 197 198 try { 199 byte[] encoding; 200 byte[] received_message = null; 201 System.out.println("Stream implementation demos"); 202 System.out.println("==========================="); 203 204 // the stream implementation 205 // 206 // test CMS DataStream 207 // 208 System.out.println("\nDataStream demo [create]:\n"); 209 encoding = createDataStream(message); 210 // transmit data 211 System.out.println("\nDataStream demo [parse]:\n"); 212 received_message = getDataStream(encoding); 213 System.out.print("\nContent: "); 214 System.out.println(new String(received_message)); 215 216 217 // the non-stream implementation 218 System.out.println("\nNon-stream implementation demos"); 219 System.out.println("==============================="); 220 221 // 222 // test CMS Data 223 // 224 225 System.out.println("\nData demo [create]:\n"); 226 encoding = createData(message); 227 // transmit data 228 System.out.println("\nData demo [parse]:\n"); 229 230 received_message = getData(encoding); 231 System.out.print("\nContent: "); 232 System.out.println(new String(received_message)); 233 234 } catch (Exception ex) { 235 ex.printStackTrace(); 236 throw new RuntimeException(ex.toString()); 237 } 238 } 239 240 /** 241 * Main method. 242 */ 243 public static void main(String argv[]) throws Exception { 244 245 (new DataDemo()).start(); 246 System.out.println("\nReady!"); 247 DemoUtil.waitKey(); 248 } 249 }