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/DataOutputStreamDemo.java 6 12.02.25 17:58 Dbratko $ 029// $Revision: 6 $ 030// 031 032package demo.cms.data; 033 034import iaik.asn1.ObjectID; 035import iaik.cms.CMSException; 036import iaik.cms.ContentInfoOutputStream; 037import iaik.cms.DataOutputStream; 038import iaik.cms.DataStream; 039import iaik.utils.Util; 040 041import java.io.ByteArrayInputStream; 042import java.io.ByteArrayOutputStream; 043import java.io.IOException; 044import java.io.InputStream; 045 046import demo.DemoUtil; 047 048/** 049 * Demonstrates the usage of class {@link iaik.cms.DataOutputStream}. 050 */ 051public class DataOutputStreamDemo { 052 053 /** 054 * Default constructor. 055 */ 056 public DataOutputStreamDemo() throws IOException { 057 System.out.println(); 058 System.out.println("**********************************************************************************"); 059 System.out.println("* DataOutputStream demo *"); 060 System.out.println("* (shows the usage of the DataOutputStream implementation) *"); 061 System.out.println("**********************************************************************************"); 062 System.out.println(); 063 } 064 065 066 /** 067 * Uses the IAIK-CMS DataOutputStream class to encode the given data. 068 * 069 * @param message the message to be encoded, as byte representation 070 * 071 * @return the BER encoding of the <code>Data</code> object just created, 072 * wrapped in a ContentInfo 073 * 074 * @throws CMSException if the <code>Data</code> object cannot 075 * be created 076 * @throws IOException if an I/O error occurs 077 */ 078 public byte[] createData(byte[] message) throws CMSException, IOException { 079 080 System.out.println("Create a new Data message:"); 081 082 // the stream from which to read the content 083 ByteArrayInputStream is = new ByteArrayInputStream(message); 084 085 // the stream to which to write the Data object 086 ByteArrayOutputStream resultStream = new ByteArrayOutputStream(); 087 088 // wrap Data into a ContentInfo 089 ContentInfoOutputStream contentInfoStream = 090 new ContentInfoOutputStream(ObjectID.cms_data, resultStream); 091 // create a new DataOutputStream object 092 DataOutputStream data = new DataOutputStream(contentInfoStream); 093 094 095 int blockSize = 8; // in real world we would use a block size like 2048 096 // write the data 097 byte[] buffer = new byte[blockSize]; 098 int bytesRead; 099 while ((bytesRead = is.read(buffer)) != -1) { 100 data.write(buffer, 0, bytesRead); 101 } 102 103 // closing the stream 104 data.close(); 105 return resultStream.toByteArray(); 106 } 107 108 /** 109 * Parses a CMS <code>Data</code> object. 110 * 111 * @param data the <code>Data</code> object as BER encoded byte array 112 * 113 * @return the inherent content as byte array 114 * 115 * @throws CMSException if an parsing exception occurs 116 * @throws IOException if an I/O error occurs 117 */ 118 public byte[] getData(byte[] data) throws CMSException, IOException { 119 120 // we are testing the stream interface 121 ByteArrayInputStream is = new ByteArrayInputStream(data); 122 // create the Data object 123 DataStream dataStream = new DataStream(is); 124 125 // get an InputStream for reading the signed content 126 InputStream content = dataStream.getInputStream(); 127 ByteArrayOutputStream os = new ByteArrayOutputStream(); 128 Util.copyStream(content, os, null); 129 130 return os.toByteArray(); 131 } 132 133 134 135 /** 136 * Starts the tests. 137 */ 138 public void start() { 139 // the test message 140 String m = "This is the test message."; 141 System.out.println("Test message: \""+m+"\""); 142 System.out.println(); 143 byte[] message = m.getBytes(); 144 145 try { 146 byte[] encoding; 147 byte[] received_message = null; 148 149 System.out.println("\nDataOutputStream demo [create]:\n"); 150 encoding = createData(message); 151 // transmit data 152 System.out.println("\nDataOutputStream demo [parse]:\n"); 153 received_message = getData(encoding); 154 System.out.print("\nContent: "); 155 System.out.println(new String(received_message)); 156 157 } catch (Exception ex) { 158 ex.printStackTrace(); 159 throw new RuntimeException(ex.toString()); 160 } 161 } 162 163 /** 164 * Main method. 165 */ 166 public static void main(String argv[]) throws Exception { 167 168 DemoUtil.initDemos(); 169 170 (new DataOutputStreamDemo()).start(); 171 System.out.println("\nReady!"); 172 DemoUtil.waitKey(); 173 } 174}