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/pkcs7cms/PKCS7CMSDataDemo.java 16 12.02.25 17:58 Dbratko $ 059 // $Revision: 16 $ 060 // 061 062 package demo.cms.pkcs7cms; 063 064 import iaik.asn1.ASN1Object; 065 import iaik.cms.CMSException; 066 import iaik.cms.Data; 067 import iaik.cms.DataStream; 068 import iaik.pkcs.PKCSException; 069 import iaik.utils.Util; 070 071 import java.io.ByteArrayInputStream; 072 import java.io.ByteArrayOutputStream; 073 import java.io.IOException; 074 import java.io.InputStream; 075 076 import demo.DemoUtil; 077 078 079 080 /** 081 * Compares the usage of IAIK CMS Data(Stream) with IAIK PKCS7 Data(Stream). 082 */ 083 public class PKCS7CMSDataDemo { 084 085 086 /** 087 * Default constructor. 088 */ 089 public PKCS7CMSDataDemo() { 090 System.out.println(); 091 System.out.println("**********************************************************************************"); 092 System.out.println("* PKCS7CMSDataDemo demo *"); 093 System.out.println("* (tests the CMS Data against the IAIK-JCE PKCS#7 Data type implementation) *"); 094 System.out.println("**********************************************************************************"); 095 System.out.println(); 096 097 } 098 099 /** 100 * Creates a CMS <code>Data</code> object. 101 * <p> 102 * @param message the message to be sent, as byte representation 103 * @return the DER encoding of the <code>Data</code> object just created 104 * @throws CMSException if the <code>Data</code> object cannot 105 * be created 106 * @throws IOException if an I/O error occurs 107 */ 108 public byte[] createDataStream(byte[] message) throws CMSException, IOException { 109 110 System.out.println("Create a new Data message:"); 111 112 // we are testing the stream interface 113 ByteArrayInputStream is = new ByteArrayInputStream(message); 114 115 // create a new Data object which includes the data 116 DataStream data = new DataStream(is, 2048); 117 118 // return the Data as BER encoded byte array with block size 2048 119 ByteArrayOutputStream os = new ByteArrayOutputStream(); 120 data.writeTo(os); 121 return os.toByteArray(); 122 } 123 124 /** 125 * Parses a CMS <code>Data</code> object. 126 * 127 * @param data the <code>Data</code> object as DER encoded byte array 128 * 129 * @return the inherent message as byte array 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 ASN.1 representation of the <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 ASN1Object 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.toASN1Object(); 166 } 167 168 /** 169 * Parses a CMS <code>Data</code> object. 170 * 171 * @param asn1Object the <code>Data</code> object as ASN.1 object 172 * 173 * @return the inherent message as byte array 174 * @throws CMSException if an parsing exception occurs 175 * @throws IOException if an I/O error occurs 176 */ 177 public byte[] getData(ASN1Object asn1Object) throws CMSException, IOException { 178 179 // create the Data object 180 Data data = new Data(asn1Object); 181 182 // get and return the content 183 return data.getData(); 184 } 185 186 // PKCS#7 187 188 /** 189 * Creates a PKCS#7 <code>Data</code> object. 190 * <p> 191 * @param message the message to be sent, as byte representation 192 * @return the DER encoding of the <code>Data</code> object just created 193 * @throws PKCSException if the <code>Data</code> object cannot 194 * be created 195 * @throws IOException if an I/O error occurs 196 */ 197 public byte[] createPKCS7DataStream(byte[] message) 198 throws iaik.pkcs.PKCSException, IOException { 199 200 System.out.println("Create a new Data message:"); 201 202 // we are testing the stream interface 203 ByteArrayInputStream is = new ByteArrayInputStream(message); 204 205 // create a new Data object which includes the data 206 iaik.pkcs.pkcs7.DataStream data = new iaik.pkcs.pkcs7.DataStream(is, 2048); 207 208 // return the Data as BER encoded byte array with block size 2048 209 ByteArrayOutputStream os = new ByteArrayOutputStream(); 210 data.writeTo(os); 211 return os.toByteArray(); 212 } 213 214 /** 215 * Parses a PKCS#7 <code>Data</code> object. 216 * 217 * @param data the <code>Data</code> object as DER encoded byte array 218 * 219 * @return the inherent message as byte array, or <code>null</code> if there 220 * is no message included into the supplied <code>data</code> 221 * object 222 * @throws PKCSException if an parsing exception occurs 223 * @throws IOException if an I/O error occurs 224 */ 225 public byte[] getPKCS7DataStream(byte[] data) throws iaik.pkcs.PKCSException, IOException { 226 227 // we are testing the stream interface 228 ByteArrayInputStream is = new ByteArrayInputStream(data); 229 // create the Data object 230 iaik.pkcs.pkcs7.DataStream dataStream = new iaik.pkcs.pkcs7.DataStream(is); 231 232 // get an InputStream for reading the signed content 233 InputStream content = dataStream.getInputStream(); 234 ByteArrayOutputStream os = new ByteArrayOutputStream(); 235 Util.copyStream(content, os, null); 236 237 return os.toByteArray(); 238 } 239 240 241 /** 242 * Creates a PKCS#7 <code>Data</code> object. 243 * <p> 244 * @param message the message to be sent, as byte representation 245 * @return the ASN.1 representation of the <code>Data</code> object just created 246 * @throws PKCSException if the <code>Data</code> object cannot 247 * be created 248 * @throws IOException if an I/O error occurs 249 */ 250 public ASN1Object createPKCS7Data(byte[] message) throws iaik.pkcs.PKCSException, IOException { 251 252 System.out.println("Create a new Data message:"); 253 254 // create a new DigestedData object which includes the data 255 iaik.pkcs.pkcs7.Data data = new iaik.pkcs.pkcs7.Data(message); 256 // return the ASN.1 representation 257 return data.toASN1Object(); 258 } 259 260 /** 261 * Parses a PKCS#7 <code>Data</code> object. 262 * 263 * @param asn1Object the <code>Data</code> object as ASN.1 object 264 * 265 * @return the inherent message as byte array 266 * @throws PKCSException if an parsing exception occurs 267 * @throws IOException if an IOException occurs 268 */ 269 public byte[] getPKCS7Data(ASN1Object asn1Object) throws iaik.pkcs.PKCSException, IOException { 270 271 // create the Data object 272 iaik.pkcs.pkcs7.Data data = new iaik.pkcs.pkcs7.Data(asn1Object); 273 274 // get and return the content 275 return data.getData(); 276 } 277 278 279 /** 280 * Tests IAIK CMS Data(Stream) against IAIK PKCS7 Data(Stream). 281 */ 282 public void start() { 283 // the test message 284 String m = "This is the test message."; 285 System.out.println("Test message: \""+m+"\""); 286 System.out.println(); 287 byte[] message = m.getBytes(); 288 289 try { 290 byte[] data; 291 byte[] received_message = null; 292 System.out.println("Stream implementation demos"); 293 System.out.println("==========================="); 294 295 // the stream implementation 296 // 297 // test CMS DataStream 298 // 299 System.out.println("\nDataStream demo [create]:\n"); 300 data = createDataStream(message); 301 // transmit data 302 System.out.println("\nDataStream demo [parse]:\n"); 303 received_message = getDataStream(data); 304 System.out.print("\nContent: "); 305 System.out.println(new String(received_message)); 306 307 System.out.println("Testing CMS against PKCS#7...\n"); 308 309 System.out.println("\nCMS DataStream demo [create]:\n"); 310 data = createDataStream(message); 311 // transmit data 312 System.out.println("\nPKCS7 DataStream demo [parse]:\n"); 313 received_message = getPKCS7DataStream(data); 314 System.out.print("\nContent: "); 315 System.out.println(new String(received_message)); 316 317 System.out.println("\nPKCS#7 DataStream demo [create]:\n"); 318 data = createPKCS7DataStream(message); 319 // transmit data 320 System.out.println("\nCMS DataStream demo [parse]:\n"); 321 received_message = getDataStream(data); 322 System.out.print("\nContent: "); 323 System.out.println(new String(received_message)); 324 325 // the non-stream implementation 326 System.out.println("\nNon-stream implementation demos"); 327 System.out.println("==============================="); 328 329 // 330 // test CMS Data 331 // 332 ASN1Object obj = null; 333 334 System.out.println("\nData demo [create]:\n"); 335 obj = createData(message); 336 // transmit data 337 System.out.println("\nData demo [parse]:\n"); 338 339 received_message = getData(obj); 340 System.out.print("\nContent: "); 341 System.out.println(new String(received_message)); 342 343 System.out.println("Testing CMS against PKCS#7...\n"); 344 345 System.out.println("\nCMS Data demo [create]:\n"); 346 obj = createData(message); 347 // transmit data 348 System.out.println("\nPKCS#7 Data demo [parse]:\n"); 349 350 received_message = getPKCS7Data(obj); 351 System.out.print("\nContent: "); 352 System.out.println(new String(received_message)); 353 354 System.out.println("\nPKCS#7 Data demo [create]:\n"); 355 obj = createPKCS7Data(message); 356 // transmit data 357 System.out.println("\nCMS Data demo [parse]:\n"); 358 359 received_message = getData(obj); 360 System.out.print("\nContent: "); 361 System.out.println(new String(received_message)); 362 363 } catch (Exception ex) { 364 ex.printStackTrace(); 365 throw new RuntimeException(ex.toString()); 366 } 367 } 368 369 /** 370 * Starts the CMS - PKCS#7 tests. 371 */ 372 public static void main(String argv[]) throws Exception { 373 374 (new PKCS7CMSDataDemo()).start(); 375 System.out.println("\nReady!"); 376 DemoUtil.waitKey(); 377 } 378 }