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/pkcs7cms/PKCS7CMSDataDemo.java 16 12.02.25 17:58 Dbratko $ 029// $Revision: 16 $ 030// 031 032package demo.cms.pkcs7cms; 033 034import iaik.asn1.ASN1Object; 035import iaik.cms.CMSException; 036import iaik.cms.Data; 037import iaik.cms.DataStream; 038import iaik.pkcs.PKCSException; 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 050/** 051 * Compares the usage of IAIK CMS Data(Stream) with IAIK PKCS7 Data(Stream). 052 */ 053public class PKCS7CMSDataDemo { 054 055 056 /** 057 * Default constructor. 058 */ 059 public PKCS7CMSDataDemo() { 060 System.out.println(); 061 System.out.println("**********************************************************************************"); 062 System.out.println("* PKCS7CMSDataDemo demo *"); 063 System.out.println("* (tests the CMS Data against the IAIK-JCE PKCS#7 Data type implementation) *"); 064 System.out.println("**********************************************************************************"); 065 System.out.println(); 066 067 } 068 069 /** 070 * Creates a CMS <code>Data</code> object. 071 * <p> 072 * @param message the message to be sent, as byte representation 073 * @return the DER encoding of the <code>Data</code> object just created 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[] createDataStream(byte[] message) throws CMSException, IOException { 079 080 System.out.println("Create a new Data message:"); 081 082 // we are testing the stream interface 083 ByteArrayInputStream is = new ByteArrayInputStream(message); 084 085 // create a new Data object which includes the data 086 DataStream data = new DataStream(is, 2048); 087 088 // return the Data as BER encoded byte array with block size 2048 089 ByteArrayOutputStream os = new ByteArrayOutputStream(); 090 data.writeTo(os); 091 return os.toByteArray(); 092 } 093 094 /** 095 * Parses a CMS <code>Data</code> object. 096 * 097 * @param data the <code>Data</code> object as DER encoded byte array 098 * 099 * @return the inherent message as byte array 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 ASN.1 representation of the <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 ASN1Object 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.toASN1Object(); 136 } 137 138 /** 139 * Parses a CMS <code>Data</code> object. 140 * 141 * @param asn1Object the <code>Data</code> object as ASN.1 object 142 * 143 * @return the inherent message as byte array 144 * @throws CMSException if an parsing exception occurs 145 * @throws IOException if an I/O error occurs 146 */ 147 public byte[] getData(ASN1Object asn1Object) throws CMSException, IOException { 148 149 // create the Data object 150 Data data = new Data(asn1Object); 151 152 // get and return the content 153 return data.getData(); 154 } 155 156 // PKCS#7 157 158 /** 159 * Creates a PKCS#7 <code>Data</code> object. 160 * <p> 161 * @param message the message to be sent, as byte representation 162 * @return the DER encoding of the <code>Data</code> object just created 163 * @throws PKCSException if the <code>Data</code> object cannot 164 * be created 165 * @throws IOException if an I/O error occurs 166 */ 167 public byte[] createPKCS7DataStream(byte[] message) 168 throws iaik.pkcs.PKCSException, IOException { 169 170 System.out.println("Create a new Data message:"); 171 172 // we are testing the stream interface 173 ByteArrayInputStream is = new ByteArrayInputStream(message); 174 175 // create a new Data object which includes the data 176 iaik.pkcs.pkcs7.DataStream data = new iaik.pkcs.pkcs7.DataStream(is, 2048); 177 178 // return the Data as BER encoded byte array with block size 2048 179 ByteArrayOutputStream os = new ByteArrayOutputStream(); 180 data.writeTo(os); 181 return os.toByteArray(); 182 } 183 184 /** 185 * Parses a PKCS#7 <code>Data</code> object. 186 * 187 * @param data the <code>Data</code> object as DER encoded byte array 188 * 189 * @return the inherent message as byte array, or <code>null</code> if there 190 * is no message included into the supplied <code>data</code> 191 * object 192 * @throws PKCSException if an parsing exception occurs 193 * @throws IOException if an I/O error occurs 194 */ 195 public byte[] getPKCS7DataStream(byte[] data) throws iaik.pkcs.PKCSException, IOException { 196 197 // we are testing the stream interface 198 ByteArrayInputStream is = new ByteArrayInputStream(data); 199 // create the Data object 200 iaik.pkcs.pkcs7.DataStream dataStream = new iaik.pkcs.pkcs7.DataStream(is); 201 202 // get an InputStream for reading the signed content 203 InputStream content = dataStream.getInputStream(); 204 ByteArrayOutputStream os = new ByteArrayOutputStream(); 205 Util.copyStream(content, os, null); 206 207 return os.toByteArray(); 208 } 209 210 211 /** 212 * Creates a PKCS#7 <code>Data</code> object. 213 * <p> 214 * @param message the message to be sent, as byte representation 215 * @return the ASN.1 representation of the <code>Data</code> object just created 216 * @throws PKCSException if the <code>Data</code> object cannot 217 * be created 218 * @throws IOException if an I/O error occurs 219 */ 220 public ASN1Object createPKCS7Data(byte[] message) throws iaik.pkcs.PKCSException, IOException { 221 222 System.out.println("Create a new Data message:"); 223 224 // create a new DigestedData object which includes the data 225 iaik.pkcs.pkcs7.Data data = new iaik.pkcs.pkcs7.Data(message); 226 // return the ASN.1 representation 227 return data.toASN1Object(); 228 } 229 230 /** 231 * Parses a PKCS#7 <code>Data</code> object. 232 * 233 * @param asn1Object the <code>Data</code> object as ASN.1 object 234 * 235 * @return the inherent message as byte array 236 * @throws PKCSException if an parsing exception occurs 237 * @throws IOException if an IOException occurs 238 */ 239 public byte[] getPKCS7Data(ASN1Object asn1Object) throws iaik.pkcs.PKCSException, IOException { 240 241 // create the Data object 242 iaik.pkcs.pkcs7.Data data = new iaik.pkcs.pkcs7.Data(asn1Object); 243 244 // get and return the content 245 return data.getData(); 246 } 247 248 249 /** 250 * Tests IAIK CMS Data(Stream) against IAIK PKCS7 Data(Stream). 251 */ 252 public void start() { 253 // the test message 254 String m = "This is the test message."; 255 System.out.println("Test message: \""+m+"\""); 256 System.out.println(); 257 byte[] message = m.getBytes(); 258 259 try { 260 byte[] data; 261 byte[] received_message = null; 262 System.out.println("Stream implementation demos"); 263 System.out.println("==========================="); 264 265 // the stream implementation 266 // 267 // test CMS DataStream 268 // 269 System.out.println("\nDataStream demo [create]:\n"); 270 data = createDataStream(message); 271 // transmit data 272 System.out.println("\nDataStream demo [parse]:\n"); 273 received_message = getDataStream(data); 274 System.out.print("\nContent: "); 275 System.out.println(new String(received_message)); 276 277 System.out.println("Testing CMS against PKCS#7...\n"); 278 279 System.out.println("\nCMS DataStream demo [create]:\n"); 280 data = createDataStream(message); 281 // transmit data 282 System.out.println("\nPKCS7 DataStream demo [parse]:\n"); 283 received_message = getPKCS7DataStream(data); 284 System.out.print("\nContent: "); 285 System.out.println(new String(received_message)); 286 287 System.out.println("\nPKCS#7 DataStream demo [create]:\n"); 288 data = createPKCS7DataStream(message); 289 // transmit data 290 System.out.println("\nCMS DataStream demo [parse]:\n"); 291 received_message = getDataStream(data); 292 System.out.print("\nContent: "); 293 System.out.println(new String(received_message)); 294 295 // the non-stream implementation 296 System.out.println("\nNon-stream implementation demos"); 297 System.out.println("==============================="); 298 299 // 300 // test CMS Data 301 // 302 ASN1Object obj = null; 303 304 System.out.println("\nData demo [create]:\n"); 305 obj = createData(message); 306 // transmit data 307 System.out.println("\nData demo [parse]:\n"); 308 309 received_message = getData(obj); 310 System.out.print("\nContent: "); 311 System.out.println(new String(received_message)); 312 313 System.out.println("Testing CMS against PKCS#7...\n"); 314 315 System.out.println("\nCMS Data demo [create]:\n"); 316 obj = createData(message); 317 // transmit data 318 System.out.println("\nPKCS#7 Data demo [parse]:\n"); 319 320 received_message = getPKCS7Data(obj); 321 System.out.print("\nContent: "); 322 System.out.println(new String(received_message)); 323 324 System.out.println("\nPKCS#7 Data demo [create]:\n"); 325 obj = createPKCS7Data(message); 326 // transmit data 327 System.out.println("\nCMS Data demo [parse]:\n"); 328 329 received_message = getData(obj); 330 System.out.print("\nContent: "); 331 System.out.println(new String(received_message)); 332 333 } catch (Exception ex) { 334 ex.printStackTrace(); 335 throw new RuntimeException(ex.toString()); 336 } 337 } 338 339 /** 340 * Starts the CMS - PKCS#7 tests. 341 */ 342 public static void main(String argv[]) throws Exception { 343 344 (new PKCS7CMSDataDemo()).start(); 345 System.out.println("\nReady!"); 346 DemoUtil.waitKey(); 347 } 348}