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/signedData/CertListDemo.java 17    12.02.25 17:58 Dbratko $
029// $Revision: 17 $
030//
031
032package demo.cms.signedData;
033
034import iaik.cms.CMSCertList;
035import iaik.security.provider.IAIK;
036import iaik.x509.X509Certificate;
037
038import java.io.FileInputStream;
039import java.io.IOException;
040
041import demo.DemoUtil;
042
043/**
044 * Reads a CMS (PKCS#7) certifcate list from a file.
045 * <p>
046 * When starting the test, you have to specify
047 * the file name holding the <code>CMSCertList</code> to be parsed:
048 * <pre>
049 * java demo.cms.CertListDemo &lt;file name&gt;
050 * </pre>
051 *
052 * @see iaik.cms.CMSCertList
053 */
054public class CertListDemo {
055
056  /**
057   * Reads a CMS (PKCS#7) certificate chain from a file and dumps the certificates
058   * stored inside.
059   * <p>
060   * Usage:
061   * <p><code>
062   * java demo.cms.CertListDemo &lt;file name&gt;
063   * </code><p>
064   *
065   * @param arg the name of the file holding the certificate chain
066   */
067  public static void main(String arg[]) {
068
069        IAIK.addAsJDK14Provider(true);
070     
071    if (arg.length != 1) {
072      
073      System.out.println("Usage: java demo.cms.CertListDemo <CMS-certificate-chain-file>");
074      
075    } else {
076      
077      FileInputStream fis = null;
078      try {
079        fis = new FileInputStream(arg[0]);
080        CMSCertList cmsCertList = new CMSCertList(fis);
081        X509Certificate[] certs = cmsCertList.getX509Certificates();
082
083        for (int i = 0; i < certs.length; i++) {
084           System.out.println(certs[i]);
085        }   
086
087      } catch (Exception ex) {
088        System.out.println("Error reading certificates: "+ ex.toString());
089      } finally {
090        if (fis != null) {
091          try {
092            fis.close();
093          } catch (IOException ex) {
094            // ignore
095          } 
096        }    
097      }
098      
099    }
100    System.out.println();
101    DemoUtil.waitKey();
102  }     
103}