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

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


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


        }
      }

      return Collections.toArray(signatures, Signature.class);
    } 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.getAlgorithm());
        if (ConditionUtils.isNotEmpty(this.parameters.getProvider())) {
          contentSignerBuilder.setProvider(this.parameters.getProvider());
        } else {
          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 (this.parameters.isDataDigested()) {
          Attribute attr = new Attribute(CMSAttributes.messageDigest, new DERSet(new DEROctetString(data)));
          ASN1EncodableVector v = new ASN1EncodableVector();
          v.add(attr);
          signerInfoGeneratorBuilder.setSignedAttributeGenerator(new DefaultSignedAttributeTableGenerator(new AttributeTable(v)));
        }

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

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

      CMSTypedData content = null;
      boolean encapsulate = true;

      if (this.parameters.isDataDigested()) {
        content = new CMSAbsentContent();
        encapsulate = false;
      } else {
        if ((this.parameters.isMergeSignatures()) && (this.isSigned(data))) {
          CMSSignedData signedData = new CMSSignedData(data);
          signedDataGenerator.addSigners(signedData.getSignerInfos());
          content = (CMSTypedData) signedData.getSignedContent();
        } else {
          content = new CMSProcessableByteArray(data);
        }

        if (PKCS7SignatureMode.DETACHED.equals(mode)) {
          encapsulate = false;
        }
      }

      CMSSignedData signedData = null;

      if (this.parameters.isDataDigested()) {
        signedData = signedDataGenerator.generate(CMSSignedGenerator.DATA, null, false, Security.getProvider(BouncyCastleProviderHelper.PROVIDER_NAME), true);
      } else {
        signedData = signedDataGenerator.generate(content, encapsulate);
      }

      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());
          ASN1Primitive asn1Primitive = BouncyCastleProviderHelper.toASN1Primitive(timeStamp.getEncoded());
          DERSet derSet = new DERSet(asn1Primitive);

          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

      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

        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

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

          break;
        }
      }
      return valid;
    } 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.