Java tutorial
/* * DSS - Digital Signature Services * * Copyright (C) 2011 European Commission, Directorate-General Internal Market and Services (DG MARKT), B-1049 Bruxelles/Brussel * * Developed by: 2011 ARHS Developments S.A. (rue Nicolas Bov 2B, L-1253 Luxembourg) http://www.arhs-developments.com * * This file is part of the "DSS - Digital Signature Services" project. * * "DSS - Digital Signature Services" is free software: you can redistribute it and/or modify it under the terms of * the GNU Lesser General Public License as published by the Free Software Foundation, either version 2.1 of the * License, or (at your option) any later version. * * DSS is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License along with * "DSS - Digital Signature Services". If not, see <http://www.gnu.org/licenses/>. */ package eu.europa.ec.markt.dss.applet; import eu.europa.ec.markt.dss.DigestAlgorithm; import eu.europa.ec.markt.dss.SignatureAlgorithm; import eu.europa.ec.markt.dss.applet.model.SignatureWizardModel; import eu.europa.ec.markt.dss.applet.model.WizardUsage; import eu.europa.ec.markt.dss.applet.wizard.AbstractWizardPanel; import eu.europa.ec.markt.dss.signature.Document; import eu.europa.ec.markt.dss.signature.DocumentSignatureService; import eu.europa.ec.markt.dss.signature.FileDocument; import eu.europa.ec.markt.dss.signature.InMemoryDocument; import eu.europa.ec.markt.dss.signature.SignaturePackaging; import eu.europa.ec.markt.dss.signature.SignatureParameters; import eu.europa.ec.markt.dss.signature.cades.CAdESService; import eu.europa.ec.markt.dss.signature.token.SignatureTokenConnection; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.security.NoSuchAlgorithmException; import java.security.cert.X509Certificate; import java.util.Arrays; import java.util.Date; import org.apache.commons.io.IOUtils; import org.apache.commons.io.output.ByteArrayOutputStream; import org.bouncycastle.cms.CMSException; import org.bouncycastle.cms.CMSSignedData; /** * Display to the user a icons showing the success or failure of the process of signature. * * @version $Revision: 1867 $ - $Date: 2013-04-08 13:44:56 +0200 (Mon, 08 Apr 2013) $ */ @SuppressWarnings("serial") public class WizardFinishedPanel extends AbstractWizardPanel { public static final String ID = "DOCUMENT_SIGNED"; private SignatureWizardModel model; /** Creates new form WizardFinishedPanel */ public WizardFinishedPanel(SignatureWizardModel signatureModel) { this.model = signatureModel; initComponents(); } @Override public Object getPanelDescriptorIdentifier() { return ID; } /** * This method is called from within the constructor to initialize the form. WARNING: Do NOT modify this code. The * content of this method is always regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { jLabel1 = new javax.swing.JLabel(); setBackground(new java.awt.Color(255, 255, 255)); jLabel1.setIcon(new javax.swing.ImageIcon( getClass().getResource("/eu/europa/ec/markt/dss/applet/wizard/big_ok.png"))); // NOI18N java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("eu/europa/ec/markt/dss/applet/i18n"); // NOI18N jLabel1.setText(bundle.getString("SIGNED_FILE_SAVED")); // NOI18N jLabel1.setName("result"); // NOI18N javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); this.setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addContainerGap() .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE) .addContainerGap())); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addContainerGap() .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 278, Short.MAX_VALUE) .addContainerGap())); }// </editor-fold>//GEN-END:initComponents @Override public Object getNextPanelDescriptor() { return ActivityPanel.ID; } @Override public Object getBackPanelDescriptor() { return ActivityPanel.ID; } @Override public void aboutToDisplayPanel() { try { getWizard().removeStepsProgression(); switch (model.getWizardUsage()) { case SIGN: signAndSaveFile(); break; case EXTEND: extendAndSaveFile(); break; } jLabel1.setIcon(new javax.swing.ImageIcon( getClass().getResource("/eu/europa/ec/markt/dss/applet/wizard/big_ok.png"))); // NOI18N if (model.getWizardUsage() == WizardUsage.SIGN) { jLabel1.setText(java.util.ResourceBundle.getBundle("eu/europa/ec/markt/dss/applet/i18n") .getString("SIGNED_FILE_SAVED")); } else { jLabel1.setText(java.util.ResourceBundle.getBundle("eu/europa/ec/markt/dss/applet/i18n") .getString("EXTENDED_FILE_SAVED")); } } catch (Exception ex) { jLabel1.setIcon(new javax.swing.ImageIcon( getClass().getResource("/eu/europa/ec/markt/dss/applet/wizard/big_error.png"))); // NOI18N if (model.getWizardUsage() == WizardUsage.SIGN) { jLabel1.setText(java.util.ResourceBundle.getBundle("eu/europa/ec/markt/dss/applet/i18n") .getString("ERROR_WHILE_SIGNING")); } else { jLabel1.setText(java.util.ResourceBundle.getBundle("eu/europa/ec/markt/dss/applet/i18n") .getString("ERROR_WHILE_EXTENDING")); } ex.printStackTrace(); } } private void signAndSaveFile() throws NoSuchAlgorithmException, IOException { SignatureTokenConnection connection = null; DocumentSignatureService service = model.createDocumentSignatureService(); Document document = model.getOriginalFile(); SignatureParameters parameters = new SignatureParameters(); parameters.setSigningDate(new Date()); parameters.setSigningCertificate((X509Certificate) model.getPrivateKey().getCertificate()); if (model.getPrivateKey().getCertificateChain() != null) { parameters.setCertificateChain( Arrays.asList((X509Certificate[]) model.getPrivateKey().getCertificateChain())); } parameters.setSignatureFormat(model.getSignatureFormat()); parameters.setSignaturePackaging(model.getPackaging()); parameters.setClaimedSignerRole(model.getClaimedRole()); parameters.setSignaturePolicy(model.getSignaturePolicyType()); parameters.setSignaturePolicyId(model.getSignaturePolicy()); parameters.setSignaturePolicyHashValue(model.getSignaturePolicyValue()); parameters.setSignaturePolicyHashAlgo(model.getSignaturePolicyAlgo()); connection = model.createTokenConnection(getWizard()); Document contentInCMS = null; if (service instanceof CAdESService && parameters.getSignaturePackaging() == SignaturePackaging.ENVELOPING) { FileInputStream original = null; try { CMSSignedData cmsData = new CMSSignedData(model.getOriginalFile().openStream()); if (cmsData != null && cmsData.getSignedContent() != null && cmsData.getSignedContent().getContent() != null) { ByteArrayOutputStream buf = new ByteArrayOutputStream(); cmsData.getSignedContent().write(buf); contentInCMS = new InMemoryDocument(buf.toByteArray()); } } catch (CMSException ex) { } finally { if (original != null) { original.close(); } } } Document signedDocument = null; if (contentInCMS != null) { byte[] signatureValue = connection.sign(service.toBeSigned(contentInCMS, parameters), DigestAlgorithm.SHA1, model.getPrivateKey()); CAdESService cadesService = (CAdESService) service; signedDocument = cadesService.addASignatureToDocument(document, parameters, signatureValue); } else { byte[] signatureValue = connection.sign(service.toBeSigned(document, parameters), DigestAlgorithm.SHA1, model.getPrivateKey()); signedDocument = service.signDocument(document, parameters, signatureValue); } FileOutputStream output = new FileOutputStream(model.getSignedFile()); IOUtils.copy(signedDocument.openStream(), output); output.close(); } private void extendAndSaveFile() throws IOException { DocumentSignatureService service = model.createDocumentSignatureService(); Document document = new FileDocument(model.getSignedFile()); SignatureParameters parameters = new SignatureParameters(); parameters.setSignatureFormat(model.getSignatureFormat()); Document originaDocument = null; if (model.getOriginalFile() != null && model.getOriginalFile().exists()) { originaDocument = model.getOriginalFile(); } Document signedDocument = service.extendDocument(document, originaDocument, parameters); FileOutputStream output = new FileOutputStream(model.getExtendedFile()); IOUtils.copy(signedDocument.openStream(), output); output.close(); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JLabel jLabel1; // End of variables declaration//GEN-END:variables }