Package org.bouncycastle.cms

Examples of org.bouncycastle.cms.CMSSignedData


      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 = 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


      throw new SignerException(e);
    }
  }

  private boolean verifyAttached(final byte[] data, final byte[] signature) throws CMSException, IOException, OperatorCreationException {
    CMSSignedData signedData = new CMSSignedData(new CMSProcessableByteArray(data), signature);

    CollectionStore certificatesStore = (CollectionStore) signedData.getCertificates();

    SignerInformationStore signerInformationStore = signedData.getSignerInfos();
    boolean verified = true;
    for (Object o : signerInformationStore.getSigners()) {
      SignerInformation signerInformation = (SignerInformation) o;

      verified = BouncyCastleSignerHelper.verifySignature(signerInformation, certificatesStore, data, false);
      if (!verified) {
        break;
      }
    }
    if (verified) {
      byte[] content = this.getContent(signedData.getSignedContent());
      if (content != null) {
        verified = Arrays.isEquals(data, content);
      }
    }
    return verified;
View Full Code Here

    return verified;
  }

  public boolean isSigned(final byte[] data) {
    try {
      CMSSignedData signedData = new CMSSignedData(data);
      RockFrameworkLogger.getLogger().warn("Content is signed with version " + signedData.getVersion());
      return true;
    } catch (Exception e) {
      return false;
    }
  }
View Full Code Here

  @Override
  @SuppressWarnings("unchecked")
  public Signature[] getSignatures(final byte[] data) {
    try {
      CMSSignedData signedData = new CMSSignedData(data);
      Collection<X509CertificateHolder> certificates = signedData.getCertificates().getMatches(null);
      SignerInformationStore signerInformationStore = signedData.getSignerInfos();
      Collection<SignerInformation> informations = signerInformationStore.getSigners();
      Collection<Signature> signatures = new ArrayList<Signature>();

      // 1.2.840.113549.1.9.3 -- content type
      // 1.2.840.113549.1.9.4 -- messagedigest
      // 1.2.840.113549.1.9.5 -- sign time
      // 1.2.840.113549.1.9.16.2.12 -- signcertificate

      if (Conditions.isNotEmpty(informations)) {
        for (SignerInformation information : informations) {
          Signature signature = new Signature();
          signature.setEncoded(information.getSignature());
          signature.setSignatories(new ArrayList<Signatory>());
          signature.setValid(null);

          // TimeStamp
          AttributeTable signedAttributeTable = information.getSignedAttributes();
          AttributeTable unsignedAttributeTable = information.getUnsignedAttributes();

          // DERSequence contentTypeDerSequence = this.getAttribute(signedAttributeTable,
          // unsignedAttributeTable, PKCSObjectIdentifiers.pkcs_9_at_contentType);
          // DERSequence messageDigestDerSequence = this.getAttribute(signedAttributeTable,
          // unsignedAttributeTable, PKCSObjectIdentifiers.pkcs_9_at_messageDigest);

          DERSequence timeStampDerSequence = this.getAttribute(signedAttributeTable, unsignedAttributeTable, PKCSObjectIdentifiers.id_aa_signatureTimeStampToken);
          if (timeStampDerSequence != null) {
            if (timeStampDerSequence.size() == 2) {
              ASN1Primitive derObjectIdentifier = ((ASN1Primitive) timeStampDerSequence.getObjectAt(0)).toASN1Primitive();
              ASN1Primitive derObjectValue = ((ASN1Primitive) timeStampDerSequence.getObjectAt(1)).toASN1Primitive();
              if ((derObjectIdentifier instanceof ASN1ObjectIdentifier) && (derObjectValue instanceof DERSet)) {
                // ASN1ObjectIdentifier asn1ObjectIdentifier = (ASN1ObjectIdentifier)
                // derObjectIdentifier;
                DERSet set = (DERSet) derObjectValue;
                ASN1Encodable encodable = set.getObjectAt(0);
                TimeStampToken timeStampToken = new TimeStampToken(new CMSSignedData(encodable.toASN1Primitive().getEncoded()));
                TimeStamp timeStamp = BouncyCastleTimeStampHelper.toTimeStamp(timeStampToken);
                signature.setTimeStamp(timeStamp);
              }
            }
          }
View Full Code Here

    return fullContentInfo.getEncoded();
  }

  public byte[] getContent(final byte[] data) {
    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

    timeStamp.setEncoded(timeStampToken.getEncoded());
    timeStamp.setHash(timeStampTokenInfo.getMessageImprintDigest());
    timeStamp.setNonce(timeStampTokenInfo.getNonce());
    timeStamp.setSerialNumber(timeStampTokenInfo.getSerialNumber());

    CMSSignedData signedData = timeStampToken.toCMSSignedData();
    Object signedContent = signedData.getSignedContent().getContent();

    if ((signedContent != null) && (signedContent.getClass().isArray())) {
      timeStamp.setContent((byte[]) signedContent);
    }
View Full Code Here

        if (line == null) {
            throw new IOException(endMarker + " not found");
        }
        try {
            ASN1InputStream aIn = new ASN1InputStream(bOut.toByteArray());
            return new CMSSignedData(ContentInfo.getInstance(aIn.readObject()));
        } catch (Exception e) {
            throw new IOException("problem parsing PKCS7 object: " + e.toString());
        }
    }
View Full Code Here

                .setProvider(sBouncyCastleProvider)
                .build())
            .setDirectSignature(true)
            .build(signer, publicKey));
        gen.addCertificates(certs);
        CMSSignedData sigData = gen.generate(data, false);

        ASN1InputStream asn1 = new ASN1InputStream(sigData.getEncoded());
        DEROutputStream dos = new DEROutputStream(out);
        dos.writeObject(asn1.readObject());
    }
View Full Code Here

                new JcaDigestCalculatorProviderBuilder()
                .build())
            .setDirectSignature(true)
            .build(sha1Signer, publicKey));
        gen.addCertificates(certs);
        CMSSignedData sigData = gen.generate(data, false);

        ASN1InputStream asn1 = new ASN1InputStream(sigData.getEncoded());
        DEROutputStream dos = new DEROutputStream(mOutputJar);
        dos.writeObject(asn1.readObject());
    }
View Full Code Here

   public PKCS7SignatureInput(String base64)
   {
      try
      {
         byte[] bytes = Base64.decode(base64);
         this.data = new CMSSignedData(bytes);
      }
      catch (Exception e)
      {
         throw new RuntimeException(e);
      }
View Full Code Here

TOP

Related Classes of org.bouncycastle.cms.CMSSignedData

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.