Package br.net.woodstock.rockframework.security.sign

Examples of br.net.woodstock.rockframework.security.sign.SignerException


      for (Alias alias : this.request.getAliases()) {
        currentData = this.singleSign(data, alias);
      }
      return currentData;
    } catch (Exception e) {
      throw new SignerException(e);
    }
  }
View Full Code Here


      Store store = this.request.getStore();
      CertificateEntry certificateEntry = (CertificateEntry) store.get(alias, StoreEntryType.CERTIFICATE);
      PrivateKeyEntry privateEntry = (PrivateKeyEntry) store.get(alias, StoreEntryType.PRIVATE_KEY);

      if (certificateEntry == null) {
        throw new SignerException("Certificate '" + alias.getName() + " not found in store");
      }

      if (privateEntry == null) {
        throw new SignerException("Private key '" + alias.getName() + " not found in store");
      }

      ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
      X509Certificate certificate = (X509Certificate) certificateEntry.getValue();
      PrivateKey privateKey = privateEntry.getValue();
      Certificate[] chain = privateEntry.getChain();

      DigestType digestType = this.getDigestTypeFromSignature(certificate.getSigAlgName());
      Calendar calendar = Calendar.getInstance();

      PdfReader reader = new PdfReader(data);
      PdfStamper stamper = PdfStamper.createSignature(reader, outputStream, PDFSigner.PDF_SIGNATURE_VERSION, null, true);

      PdfSignatureAppearance appearance = stamper.getSignatureAppearance();
      appearance.setCrypto(privateKey, chain, null, PdfSignatureAppearance.SELF_SIGNED);
      appearance.setContact(this.request.getContactInfo());
      appearance.setLocation(this.request.getLocation());
      appearance.setReason(this.request.getReason());
      appearance.setSignDate(calendar);

      PdfSignature signature = new PdfSignature(PdfName.ADOBE_PPKLITE, PdfName.ADBE_PKCS7_DETACHED);
      signature.setReason(appearance.getReason());
      signature.setLocation(appearance.getLocation());
      signature.setContact(appearance.getContact());
      signature.setDate(new PdfDate(appearance.getSignDate()));

      if (ConditionUtils.isNotEmpty(this.request.getName())) {
        signature.setName(this.request.getName());
      } else {
        signature.setName(this.getValue(certificate.getSubjectX500Principal()));
      }

      appearance.setCryptoDictionary(signature);

      int contentSize = 0x2502;
      HashMap<PdfName, Integer> exc = new HashMap<PdfName, Integer>();
      exc.put(PdfName.CONTENTS, new Integer(contentSize));
      appearance.preClose(exc);

      Digester digester = new BasicDigester(digestType);
      byte[] rangeStream = IOUtils.toByteArray(appearance.getRangeStream());
      byte[] hash = digester.digest(rangeStream);

      TSAClient tsc = null;
      if (this.request.getTimeStampClient() != null) {
        tsc = new DelegateITextTSAClient(this.request.getTimeStampClient());
      }

      byte[] oscp = null;
      if (ConditionUtils.isNotEmpty(chain)) {
        String oscpUrl = PdfPKCS7.getOCSPURL(certificate);
        X509Certificate parentCertificate = null;

        for (Certificate c : chain) {
          if (!certificate.equals(c)) {
            parentCertificate = (X509Certificate) c;
            break;
          }
        }

        if (parentCertificate != null) {
          if ((oscpUrl != null) && (oscpUrl.trim().length() > 0)) {
            OcspClient ocspClient = new OcspClientBouncyCastle(certificate, parentCertificate, oscpUrl);
            oscp = ocspClient.getEncoded();
          }
        }
      }

      PdfPKCS7 pkcs7 = new PdfPKCS7(privateKey, chain, null, digestType.getAlgorithm(), null, false);
      byte[] authenticatedAttributes = pkcs7.getAuthenticatedAttributeBytes(hash, calendar, oscp);
      pkcs7.update(authenticatedAttributes, 0, authenticatedAttributes.length);
      pkcs7.setLocation(this.request.getLocation());
      pkcs7.setReason(this.request.getReason());

      if (tsc == null) {
        pkcs7.setSignDate(calendar);
      }

      byte[] encodedPkcs7 = pkcs7.getEncodedPKCS7(hash, calendar, tsc, oscp);

      byte[] output = new byte[(contentSize - 2) / 2];

      System.arraycopy(encodedPkcs7, 0, output, 0, encodedPkcs7.length);

      PdfDictionary newDictionary = new PdfDictionary();
      PdfString content = new PdfString(output);
      content.setHexWriting(true);
      newDictionary.put(PdfName.CONTENTS, content);
      appearance.close(newDictionary);

      return outputStream.toByteArray();
    } catch (Exception e) {
      throw new SignerException(e);
    }
  }
View Full Code Here

          }
        }
      }
      return true;
    } catch (Exception e) {
      throw new SignerException(e);
    }
  }
View Full Code Here

      for (Alias alias : this.parameters.getAliases()) {
        PrivateKeyEntry privateKeyEntry = (PrivateKeyEntry) this.parameters.getStore().get(alias, StoreEntryType.PRIVATE_KEY);

        if (privateKeyEntry == null) {
          throw new SignerException("PrivateKey not found for alias '" + alias.getName() + "'");
        }

        PrivateKey privateKey = privateKeyEntry.getValue();
        Certificate[] chain = privateKeyEntry.getChain();
        Certificate certificate = chain[0];

        JcaContentSignerBuilder contentSignerBuilder = new JcaContentSignerBuilder(SignatureType.SHA1_RSA.getAlgorithm());
        contentSignerBuilder.setProvider(BouncyCastleProviderHelper.PROVIDER_NAME);

        ContentSigner contentSigner = contentSignerBuilder.build(privateKey);

        JcaDigestCalculatorProviderBuilder digestCalculatorProviderBuilder = new JcaDigestCalculatorProviderBuilder();
        digestCalculatorProviderBuilder.setProvider(BouncyCastleProviderHelper.PROVIDER_NAME);
        DigestCalculatorProvider digestCalculatorProvider = digestCalculatorProviderBuilder.build();

        JcaSignerInfoGeneratorBuilder signerInfoGeneratorBuilder = new JcaSignerInfoGeneratorBuilder(digestCalculatorProvider);

        // if (timeStampClient == null) {
        // DERObject derObject = new ASN1UTCTime(new Date());
        // DERSet derSet = new DERSet(derObject);
        //
        // Attribute attribute = new Attribute(PKCSObjectIdentifiers.pkcs_9_at_signingTime, derSet);
        // Hashtable hashtable = new Hashtable();
        // hashtable.put(PKCSObjectIdentifiers.pkcs_9_at_signingTime, attribute);
        //
        // AttributeTable table = new AttributeTable(hashtable);
        // CMSAttributeTableGenerator attributeTableGenerator = new DefaultSignedAttributeTableGenerator(table);
        // signerInfoGeneratorBuilder.setSignedAttributeGenerator(attributeTableGenerator);
        // }

        SignerInfoGenerator signerInfoGenerator = signerInfoGeneratorBuilder.build(contentSigner, (X509Certificate) certificate);

        cmsSignedDataGenerator.addSignerInfoGenerator(signerInfoGenerator);
        cmsSignedDataGenerator.addCertificates(this.getCertificateStore(chain));
      }

      CMSTypedData content = new CMSProcessableByteArray(data);

      CMSSignedData signedData = cmsSignedDataGenerator.generate(content, true);

      if (timeStampClient != null) {
        SignerInformationStore signerInformationStore = signedData.getSignerInfos();
        List list = new ArrayList();
        for (Object o : signerInformationStore.getSigners()) {
          SignerInformation signerInformation = (SignerInformation) o;
          TimeStamp timeStamp = timeStampClient.getTimeStamp(signerInformation.getSignature());
          DERObject derObject = new ASN1InputStream(timeStamp.getEncoded()).readObject();
          DERSet derSet = new DERSet(derObject);

          Hashtable hashtable = new Hashtable();
          Attribute attribute = new Attribute(PKCSObjectIdentifiers.id_aa_signatureTimeStampToken, derSet);
          hashtable.put(PKCSObjectIdentifiers.id_aa_signatureTimeStampToken, attribute);

          AttributeTable unsignedAtts = new AttributeTable(hashtable);

          list.add(SignerInformation.replaceUnsignedAttributes(signerInformation, unsignedAtts));
        }

        SignerInformationStore tmpSignerInformationStore = new SignerInformationStore(list);

        signedData = CMSSignedData.replaceSigners(signedData, tmpSignerInformationStore);
      }

      return signedData.getEncoded();
    } catch (Exception e) {
      throw new SignerException(e);
    }
  }
View Full Code Here

        byte[] content = (byte[]) signedContent.getContent();
        verified = Arrays.equals(data, content);
      }
      return verified;
    } catch (Exception e) {
      throw new SignerException(e);
    }
  }
View Full Code Here

        }
      }

      return CollectionUtils.toArray(signatures, Signature.class);
    } catch (Exception e) {
      throw new SignerException(e);
    }
  }
View Full Code Here

      s.update(data);

      boolean ok = s.verify(signature);
      return ok;
    } catch (NoSuchAlgorithmException e) {
      throw new SignerException(e);
    } catch (InvalidKeyException e) {
      throw new SignerException(e);
    } catch (SignatureException e) {
      throw new SignerException(e);
    }
  }
View Full Code Here

      s.update(data);

      boolean ok = s.verify(signature);
      return ok;
    } catch (NoSuchAlgorithmException e) {
      throw new SignerException(e);
    } catch (InvalidKeyException e) {
      throw new SignerException(e);
    } catch (SignatureException e) {
      throw new SignerException(e);
    }
  }
View Full Code Here

      for (Alias alias : this.parameters.getAliases()) {
        currentData = this.singleSign(data, alias);
      }
      return currentData;
    } catch (Exception e) {
      throw new SignerException(e);
    }
  }
View Full Code Here

      Store store = this.parameters.getStore();
      CertificateEntry certificateEntry = (CertificateEntry) store.get(alias, StoreEntryType.CERTIFICATE);
      PrivateKeyEntry privateEntry = (PrivateKeyEntry) store.get(alias, StoreEntryType.PRIVATE_KEY);

      if (certificateEntry == null) {
        throw new SignerException("Certificate '" + alias.getName() + " not found in store");
      }

      if (privateEntry == null) {
        throw new SignerException("Private key '" + alias.getName() + " not found in store");
      }

      ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
      X509Certificate certificate = (X509Certificate) certificateEntry.getValue();
      PrivateKey privateKey = privateEntry.getValue();
      Certificate[] chain = privateEntry.getChain();

      DigestType digestType = this.getDigestTypeFromSignature(certificate.getSigAlgName());
      Calendar calendar = Calendar.getInstance();

      PdfReader reader = new PdfReader(data);
      PdfStamper stamper = PdfStamper.createSignature(reader, outputStream, PDFSigner.PDF_SIGNATURE_VERSION, null, true);

      PdfSignatureAppearance appearance = stamper.getSignatureAppearance();
      appearance.setCrypto(privateKey, chain, null, PdfSignatureAppearance.SELF_SIGNED);
      appearance.setContact(this.parameters.getContactInfo());
      appearance.setLocation(this.parameters.getLocation());
      appearance.setReason(this.parameters.getReason());
      appearance.setSignDate(calendar);

      PdfSignature signature = new PdfSignature(PdfName.ADOBE_PPKLITE, PdfName.ADBE_PKCS7_DETACHED);
      signature.setReason(appearance.getReason());
      signature.setLocation(appearance.getLocation());
      signature.setContact(appearance.getContact());
      signature.setDate(new PdfDate(appearance.getSignDate()));

      if (ConditionUtils.isNotEmpty(this.parameters.getName())) {
        signature.setName(this.parameters.getName());
      } else {
        signature.setName(BouncyCastleProviderHelper.getName(certificate.getSubjectX500Principal()));
      }

      appearance.setCryptoDictionary(signature);

      int contentSize = 0x2502;
      HashMap<PdfName, Integer> exc = new HashMap<PdfName, Integer>();
      exc.put(PdfName.CONTENTS, new Integer(contentSize));
      appearance.preClose(exc);

      Digester digester = new BasicDigester(digestType);
      byte[] rangeStream = IOUtils.toByteArray(appearance.getRangeStream());
      byte[] hash = digester.digest(rangeStream);

      TSAClient tsc = null;
      if (this.parameters.getTimeStampClient() != null) {
        tsc = new DelegateITextTSAClient(this.parameters.getTimeStampClient());
      }

      byte[] oscp = null;
      if (ConditionUtils.isNotEmpty(chain)) {
        String oscpUrl = PdfPKCS7.getOCSPURL(certificate);
        X509Certificate parentCertificate = null;

        for (Certificate c : chain) {
          if (!certificate.equals(c)) {
            parentCertificate = (X509Certificate) c;
            break;
          }
        }

        if (parentCertificate != null) {
          if ((oscpUrl != null) && (oscpUrl.trim().length() > 0)) {
            OcspClient ocspClient = new OcspClientBouncyCastle(certificate, parentCertificate, oscpUrl);
            oscp = ocspClient.getEncoded();
          }
        }
      }

      PdfPKCS7 pkcs7 = new PdfPKCS7(privateKey, chain, null, digestType.getAlgorithm(), null, false);
      byte[] authenticatedAttributes = pkcs7.getAuthenticatedAttributeBytes(hash, calendar, oscp);
      pkcs7.update(authenticatedAttributes, 0, authenticatedAttributes.length);
      pkcs7.setLocation(this.parameters.getLocation());
      pkcs7.setReason(this.parameters.getReason());

      if (tsc == null) {
        pkcs7.setSignDate(calendar);
      }

      byte[] encodedPkcs7 = pkcs7.getEncodedPKCS7(hash, calendar, tsc, oscp);

      byte[] output = new byte[(contentSize - 2) / 2];

      System.arraycopy(encodedPkcs7, 0, output, 0, encodedPkcs7.length);

      PdfDictionary newDictionary = new PdfDictionary();
      PdfString content = new PdfString(output);
      content.setHexWriting(true);
      newDictionary.put(PdfName.CONTENTS, content);
      appearance.close(newDictionary);

      return outputStream.toByteArray();
    } catch (Exception e) {
      throw new SignerException(e);
    }
  }
View Full Code Here

TOP

Related Classes of br.net.woodstock.rockframework.security.sign.SignerException

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.