Package org.bouncycastle.cms

Examples of org.bouncycastle.cms.CMSSignedData


        gen.addSigner(keyPair.getPrivate(), keyCert, CMSSignedDataGenerator.DIGEST_SHA1);

        gen.addCertificatesAndCRLs(certsAndCrls);

        CMSSignedData s = gen.generate(msg, true, null);

        ByteArrayInputStream bIn = new ByteArrayInputStream(s.getEncoded());
        ASN1InputStream aIn = new ASN1InputStream(bIn);

        s = new CMSSignedData(ContentInfo.getInstance(aIn.readObject()));

        certsAndCrls = s.getCertificatesAndCRLs("Collection", null);

        SignerInformationStore signers = s.getSignerInfos();
        Collection c = signers.getSigners();
        Iterator it = c.iterator();

        while (it.hasNext())
        {
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

      gen.addSignerInfoGenerator(new JcaSignerInfoGeneratorBuilder(new JcaDigestCalculatorProviderBuilder().setProvider("BC").build()).build(sha1Signer, signCert));

      gen.addCertificates(certs);

      CMSSignedData sigData = gen.generate(msg, false);
      return myAlgorithm + ' ' + Base64.encodeBase64String(sigData.getEncoded());
//      return Base64.encodeBase64String(sigData.getEncoded());

    } catch (Exception e) {
      throw new SignatureFailureException(e);
    }
View Full Code Here

      }

      theSignature = theSignature.substring(spaceIndex + 1);

      CMSProcessable content = new CMSProcessableByteArray(theBytes);
      CMSSignedData s = new CMSSignedData(content, Base64.decodeBase64(theSignature));

      ourLog.debug("Verifying message against public key with alias[{}]", myKeyAlias);

      SignerInformationVerifier vib = new JcaSimpleSignerInfoVerifierBuilder().build(pubKey);

      SignerInformationStore signers = s.getSignerInfos();
      boolean verified = false;

      for (Iterator<?> i = signers.getSigners().iterator(); i.hasNext();) {
        SignerInformation signer = (SignerInformation) i.next();
        try {
View Full Code Here

        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

  @SuppressWarnings("unchecked")
  public boolean verify(final byte[] data, final byte[] signature) {
    Assert.notEmpty(data, "data");
    Assert.notEmpty(signature, "signature");
    try {
      CMSSignedData signedData = new CMSSignedData(signature);
      CollectionStore certificatesStore = (CollectionStore) signedData.getCertificates();
      // CollectionStore crlStore = (CollectionStore) signedData.getCRLs();

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

        Collection<Certificate> collection = certificatesStore.getMatches(null);
        if (!collection.isEmpty()) {
          for (Certificate cert : collection) {

            JcaContentVerifierProviderBuilder jcaContentVerifierProviderBuilder = new JcaContentVerifierProviderBuilder();
            jcaContentVerifierProviderBuilder.setProvider(BouncyCastleProviderHelper.PROVIDER_NAME);

            ContentVerifierProvider contentVerifierProvider = jcaContentVerifierProviderBuilder.build((X509Certificate) cert);

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

            SignerInformationVerifier signerInformationVerifier = new SignerInformationVerifier(contentVerifierProvider, digestCalculatorProvider);

            if (!signerInformation.verify(signerInformationVerifier)) {
              verified = false;
            }
          }
        }
      }
      if (verified) {
        CMSProcessable signedContent = signedData.getSignedContent();
        byte[] content = (byte[]) signedContent.getContent();
        verified = Arrays.equals(data, content);
      }
      return verified;
    } catch (Exception e) {
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 (ConditionUtils.isNotEmpty(informations)) {
        for (SignerInformation information : informations) {
          Signature signature = new Signature();
          signature.setEncoded(null); // FIXME
          signature.setLocation(null); // FIXME
          signature.setReason(null); // FIXME
          signature.setSignatories(new ArrayList<Signatory>());
          signature.setValid(Boolean.TRUE);

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

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

          if (timeStampDerSequence != null) {
            if (timeStampDerSequence.size() == 2) {
              DERObject derObjectIdentifier = ((DERObject) timeStampDerSequence.getObjectAt(0)).toASN1Object();
              DERObject derObjectValue = ((DERObject) timeStampDerSequence.getObjectAt(1)).toASN1Object();
              if ((derObjectIdentifier instanceof ASN1ObjectIdentifier) && (derObjectValue instanceof DERSet)) {
                // ASN1ObjectIdentifier asn1ObjectIdentifier = (ASN1ObjectIdentifier) derObjectIdentifier;
                DERSet set = (DERSet) derObjectValue;
                DEREncodable encodable = set.getObjectAt(0);
                TimeStampToken timeStampToken = new TimeStampToken(new CMSSignedData(encodable.getDERObject().getEncoded()));
                TimeStamp timeStamp = BouncyCastleTimeStampHelper.toTimeStamp(timeStampToken);
                signature.setTimeStamp(timeStamp);
              }
            }
          }
View Full Code Here

      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

      throw new SignerException(e);
    }
  }

  private boolean verifyAttached(final byte[] data, final byte[] signature) throws CMSException, IOException, OperatorCreationException, GeneralSecurityException {
    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.equals(data, content);
      }
    }
    return verified;
View Full Code Here

    return verified;
  }

  public boolean isSigned(final byte[] data) {
    try {
      CMSSignedData signedData = new CMSSignedData(data);
      SecurityLog.getInstance().getLogger().warn("Content is signed with version " + signedData.getVersion());
      return true;
    } catch (Exception e) {
      return false;
    }
  }
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.