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

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


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


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

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

      if (content != null) {
        return this.verifyAttached(data, signature);
      }
      return this.verifyDetached(data, signature);
    } 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

    try {
      CMSSignedData signedData = new CMSSignedData(data);
      CMSProcessable processable = signedData.getSignedContent();
      return this.getContent(processable);
    } catch (Exception 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

    try {
      Store store = this.parameters.getStore();
      PrivateKeyEntry privateEntry = (PrivateKeyEntry) store.get(alias, StoreEntryType.PRIVATE_KEY);
     
      if (privateEntry == null) {
        throw new SignerException("Private key '" + alias.getName() + " not found in store");
      }

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

      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

          }
        }
      }
      return true;
    } catch (Exception 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

    try {
      Store store = this.parameters.getStore();
      PrivateKeyEntry privateEntry = (PrivateKeyEntry) store.get(alias, StoreEntryType.PRIVATE_KEY);

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

      ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

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

      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();

      if (this.parameters.getSignatureInfo() != null) {
        appearance.setContact(this.parameters.getSignatureInfo().getContactInfo());
        appearance.setLocation(this.parameters.getSignatureInfo().getLocation());
        appearance.setReason(this.parameters.getSignatureInfo().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 ((this.parameters.getSignatureInfo() != null) && (ConditionUtils.isNotEmpty(this.parameters.getSignatureInfo().getName()))) {
        signature.setName(this.parameters.getSignatureInfo().getName());
      } else {
        signature.setName(BouncyCastleProviderHelper.getName(certificate.getSubjectX500Principal()));
      }

      appearance.setCryptoDictionary(signature);

      String providerName = null;
      if (ConditionUtils.isNotEmpty(this.parameters.getProvider())) {
        providerName = this.parameters.getProvider();
      } else {
        providerName = BouncyCastleProviderHelper.PROVIDER_NAME;
      }

      ExternalSignature externalSignature = new PrivateKeySignature(privateKey, digestType.getAlgorithm(), providerName);

      ExternalDigest externalDigest = new BouncyCastleDigest();

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

      OcspClient ocsp = null;
      if (ConditionUtils.isNotEmpty(chain)) {
        String ocspUrl = CertificateUtil.getOCSPURL(certificate);
        X509Certificate parentCertificate = null;

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

        if (parentCertificate != null) {
          if ((ocspUrl != null) && (ocspUrl.trim().length() > 0)) {
            ocsp = new OcspClientBouncyCastle();
          }
        }
      }

      MakeSignature.signDetached(appearance, externalDigest, externalSignature, chain, null, ocsp, tsc, 0, CryptoStandard.CMS);

      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.