001package demo.smime.ess;
002
003import iaik.cms.CMSException;
004import iaik.cms.SignerInfo;
005import iaik.smime.ess.SigningCertificate;
006import iaik.smime.ess.SigningCertificateV2;
007
008import java.io.IOException;
009import java.security.cert.Certificate;
010
011import demo.DemoUtil;
012
013/**
014 * Demonstrates how to add and parse a {@link iaik.smime.ess.SigningCertificateV2
015 * SigningCertificateV2} attribute to the SignerInfo of a {@link iaik.cms.SignedDataStream} or
016 * {@link iaik.cms.SignedData} object. The SigningCertificateV2 attributes maybe used
017 * to include certificate identification information into the signed attributes of a 
018 * CMS {@link iaik.cms.SignerInfo SignerInfo} object. It has been introduced by 
019 * RFC 5035 to allow to use the {@link iaik.smime.ess.SigningCertificate
020 * SigningCertificate} attribute with other hash algorithms than SHA-1.
021 *
022 * @see iaik.smime.ess.SigningCertificate
023 * @see iaik.smime.ess.SigningCertificateV2
024 * @see iaik.cms.SignerInfo
025 * @see iaik.cms.SignedDataStream
026 * @see iaik.cms.SignedData
027 */
028public class SigningCertificateV2Demo extends SigningCertificateDemo {
029
030  /**
031   * Setups the demo certificate chains.
032   * 
033   * Keys and certificate are retrieved from the demo KeyStore.
034   * 
035   * @throws IOException if an file read error occurs
036   */
037  public SigningCertificateV2Demo() throws IOException {
038    super();
039  }
040  
041  /**
042   * Creates a SigningCertificateV2 attribute for the given certificates.
043   * 
044   * @param certs the certificates for which to create the SigningCertificateV2
045   *              attribute
046   *              
047   * @return the SigningCertificate attribute just created             
048   *              
049   * @throws CMSException if an error occurs when creating the
050   *                      SigningCertificateV2 attribute             
051   */
052  protected SigningCertificate createSigningCertificate(Certificate[] certs) 
053    throws CMSException {
054    
055    try {
056      // we use the default hash algorithm (SHA-256)
057      return new SigningCertificateV2(certs, true);
058    } catch (Exception ex) {
059      throw new CMSException("Error creating SigningCertificateV2 attribute: " + ex.toString());
060    }
061  }
062  
063  /**
064   * Gets the SigningCertificateV2 attribute from the given SignerInfo.
065   * 
066   * @param signerInfo the SignerInfo from which to get the
067   *                   SigningCertificateV2 attribute
068   *                   
069   * @return the SigningCertificateV2 attribute, or <code>null</code>
070   *         if no SigningCertificate attribute is included  
071   *         
072   * @throws CMSException if an error occurs when getting the
073   *                         SigningCertificateV2 attribute                              
074   */
075  protected SigningCertificate getSigningCertificate(SignerInfo signerInfo)
076    throws CMSException {
077    
078    return signerInfo.getSigningCertificateV2Attribute();
079  }
080  
081  /**
082   * Prints some header lines to System.out.
083   */
084  protected void printHeader() {
085    System.out.println();
086    System.out.println("**********************************************************************************");
087    System.out.println("*                       SigningCertificateV2Demo demo                            *");
088    System.out.println("*          (shows the usage of the ESS SigningCertificateV2 attribute)           *");
089    System.out.println("**********************************************************************************");
090    System.out.println();
091  }
092
093  /**
094   * The main method.
095   * 
096   * @throws IOException 
097   *            if an I/O error occurs when reading required keys
098   *            and certificates from files
099   */
100  public static void main(String[] args) throws Exception {
101    DemoUtil.initDemos();
102    (new SigningCertificateV2Demo()).start();
103    System.out.println("\nReady!");
104    DemoUtil.waitKey();
105
106  }
107
108}