Package org.bouncycastle.cms

Examples of org.bouncycastle.cms.CMSSignedDataStreamGenerator


     * @throws Exception
     */
    public static void sign(final InputStream is, OutputStream os, PrivateKey key, String providerName, X509Certificate cert) throws Exception {
        final InputStream bis = new BufferedInputStream(is, bufferSize);
        final OutputStream bos = new BufferedOutputStream(os, bufferSize);
        final CMSSignedDataStreamGenerator gen = new CMSSignedDataStreamGenerator();
        final String digest = CMSSignedGenerator.DIGEST_SHA256;
        if ( cert!=null ) {
            gen.addSigner(key, cert, digest, providerName);
        } else {
            gen.addSigner(key, "hej".getBytes(), digest, providerName);
        }
        final OutputStream out = gen.open(bos, true);
        fromInToOut(bis, out);
        bos.close();
        os.close();
    }
View Full Code Here


        }
       
        protected CMSSignedDataStreamGenerator getGenerator()
            throws CMSException, CertStoreException, InvalidKeyException, NoSuchAlgorithmException, NoSuchProviderException
        {
            CMSSignedDataStreamGenerator gen = new CMSSignedDataStreamGenerator();
           
            for (Iterator it = _certStores.iterator(); it.hasNext();)
            {
                gen.addCertificatesAndCRLs((CertStore)it.next());
            }

            for (Iterator it = _attributeCerts.iterator(); it.hasNext();)
            {
                gen.addAttributeCertificates((X509Store)it.next());
            }

            for (Iterator it = _signers.iterator(); it.hasNext();)
            {
                Signer signer = (Signer)it.next();
               
                gen.addSigner(signer.getKey(), signer.getCert(), signer.getDigestOID(), signer.getSignedAttr(), signer.getUnsignedAttr(), _provider);
            }

            gen.addSigners(new SignerInformationStore(_oldSigners));
           
            return gen;
        }
View Full Code Here

        public void write(OutputStream out)
            throws IOException
        {
            try
            {
                CMSSignedDataStreamGenerator gen = getGenerator();
               
                OutputStream signingStream = gen.open(out, _encapsulate);
               
                if (_content != null)
                {
                    if (!_encapsulate)
                    {
                        writeBodyPart(signingStream, _content);
                    }
                    else
                    {
                        _content.writeTo(signingStream);
                    }
                }
               
                signingStream.close();

                _digests = gen.getGeneratedDigests();
            }
            catch (MessagingException e)
            {
                throw new IOException(e.toString());
            }
View Full Code Here

        certList.add(keyCert);

        CertStore           certsAndCrls = CertStore.getInstance("Collection",
                        new CollectionCertStoreParameters(certList), "SUN");

        CMSSignedDataStreamGenerator gen = new CMSSignedDataStreamGenerator();

        gen.addSigner(keyPair.getPrivate(), keyCert, CMSSignedDataStreamGenerator.DIGEST_SHA1, "SunRsaSign");

        gen.addCertificatesAndCRLs(certsAndCrls);

        OutputStream sigOut = gen.open(bOut);

        sigOut.write(TEST_MESSAGE.getBytes());

        sigOut.close();
View Full Code Here

        certList.add(_origCrl);

        CertStore           certsAndCrls = CertStore.getInstance("Collection",
                        new CollectionCertStoreParameters(certList), "BC");
   
        CMSSignedDataStreamGenerator gen = new CMSSignedDataStreamGenerator();
   
        gen.addSigner(_origKP.getPrivate(), _origCert, CMSSignedDataStreamGenerator.DIGEST_SHA1, "BC");
   
        gen.addCertificatesAndCRLs(certsAndCrls);
   
        OutputStream sigOut = gen.open(bOut);
   
        sigOut.write(TEST_MESSAGE.getBytes());
       
        sigOut.close();
       
        CMSSignedDataParser     sp = new CMSSignedDataParser(
                new CMSTypedStream(new ByteArrayInputStream(TEST_MESSAGE.getBytes())), bOut.toByteArray());
   
        sp.getSignedContent().drain();
       
        //
        // compute expected content digest
        //
        MessageDigest md = MessageDigest.getInstance("SHA1", "BC");
       
        verifySignatures(sp, md.digest(TEST_MESSAGE.getBytes()));
       
        //
        // try using existing signer
        //
        gen = new CMSSignedDataStreamGenerator();
   
        gen.addSigners(sp.getSignerInfos());
       
        gen.addCertificatesAndCRLs(sp.getCertificatesAndCRLs("Collection", "BC"));
       
        bOut.reset();
       
        sigOut = gen.open(bOut, true);
   
        sigOut.write(TEST_MESSAGE.getBytes());
       
        sigOut.close();
   
View Full Code Here

        certList.add(_origCrl);

        CertStore           certsAndCrls = CertStore.getInstance("Collection",
                                                       new CollectionCertStoreParameters(certList), "BC");

        CMSSignedDataStreamGenerator gen = new CMSSignedDataStreamGenerator();

        gen.addSigner(_origKP.getPrivate(), _origCert, CMSSignedDataStreamGenerator.DIGEST_SHA1, "BC");

        gen.addCertificatesAndCRLs(certsAndCrls);

        OutputStream sigOut = gen.open(bOut, "1.2.3.4", true);

        sigOut.write(TEST_MESSAGE.getBytes());

        sigOut.close();
View Full Code Here

        certList.add(_signCert);
   
        CertStore           certs = CertStore.getInstance("Collection",
                        new CollectionCertStoreParameters(certList), "BC");
   
        CMSSignedDataStreamGenerator gen = new CMSSignedDataStreamGenerator();
   
        gen.addSigner(_origKP.getPrivate(), _origCert, CMSSignedDataStreamGenerator.DIGEST_SHA1, "BC");
        gen.addSigner(_origKP.getPrivate(), _origCert, CMSSignedDataStreamGenerator.DIGEST_MD5, "BC");
       
        gen.addCertificatesAndCRLs(certs);
   
        OutputStream sigOut = gen.open(bOut);
   
        sigOut.write(TEST_MESSAGE.getBytes());
       
        sigOut.close();
       
View Full Code Here

                               new CollectionCertStoreParameters(certList), "BC");

        //
        // find unbuffered length
        //
        CMSSignedDataStreamGenerator gen = new CMSSignedDataStreamGenerator();

        gen.addSigner(_origKP.getPrivate(), _origCert, CMSSignedDataStreamGenerator.DIGEST_SHA1, "BC");

        gen.addCertificatesAndCRLs(certs);

        OutputStream sigOut = gen.open(bOut, true);
       
        for (int i = 0; i != 2000; i++)
        {
            sigOut.write(i & 0xff);
        }
       
        sigOut.close();
       
        CMSSignedDataParser     sp = new CMSSignedDataParser(bOut.toByteArray());

        sp.getSignedContent().drain();
       
        verifySignatures(sp);
       
        int unbufferedLength = bOut.toByteArray().length;
       
        //
        // find buffered length with buffered stream - should be equal
        //
        bOut = new ByteArrayOutputStream();

        gen = new CMSSignedDataStreamGenerator();
       
        gen.addSigner(_origKP.getPrivate(), _origCert, CMSSignedDataStreamGenerator.DIGEST_SHA1, "BC");

        gen.addCertificatesAndCRLs(certs);

        sigOut = gen.open(bOut, true);

        BufferedOutputStream bfOut = new BufferedOutputStream(sigOut, 300);
       
        for (int i = 0; i != 2000; i++)
        {
View Full Code Here

                               new CollectionCertStoreParameters(certList), "BC");
   
        //
        // find unbuffered length
        //
        CMSSignedDataStreamGenerator gen = new CMSSignedDataStreamGenerator();
   
        gen.addSigner(_origKP.getPrivate(), _origCert, CMSSignedDataStreamGenerator.DIGEST_SHA1, "BC");
   
        gen.addCertificatesAndCRLs(certs);
   
        OutputStream sigOut = gen.open(bOut, true);
       
        for (int i = 0; i != 2000; i++)
        {
            sigOut.write(i & 0xff);
        }
       
        sigOut.close();
       
        CMSSignedDataParser     sp = new CMSSignedDataParser(bOut.toByteArray());
   
        sp.getSignedContent().drain();
       
        verifySignatures(sp);
       
        int unbufferedLength = bOut.toByteArray().length;
       
        //
        // find buffered length - buffer size less than default
        //
        bOut = new ByteArrayOutputStream();
   
        gen = new CMSSignedDataStreamGenerator();
       
        gen.setBufferSize(300);
       
        gen.addSigner(_origKP.getPrivate(), _origCert, CMSSignedDataStreamGenerator.DIGEST_SHA1, "BC");
   
        gen.addCertificatesAndCRLs(certs);
   
        sigOut = gen.open(bOut, true);
   
        for (int i = 0; i != 2000; i++)
        {
            sigOut.write(i & 0xff);
        }
View Full Code Here

        certList.add(_signCert);

        CertStore           certs = CertStore.getInstance("Collection",
                        new CollectionCertStoreParameters(certList), "BC");

        CMSSignedDataStreamGenerator gen = new CMSSignedDataStreamGenerator();

        gen.addSigner(_origKP.getPrivate(), _origCert, CMSSignedDataStreamGenerator.DIGEST_SHA1, "BC");

        gen.addCertificatesAndCRLs(certs);

        OutputStream sigOut = gen.open(bOut, true);

        sigOut.write(TEST_MESSAGE.getBytes());
       
        sigOut.close();
       
        CMSSignedDataParser     sp = new CMSSignedDataParser(bOut.toByteArray());

        sp.getSignedContent().drain();
       
        verifySignatures(sp);
       
        byte[] contentDigest = (byte[])gen.getGeneratedDigests().get(CMSSignedGenerator.DIGEST_SHA1);

        AttributeTable table = ((SignerInformation)sp.getSignerInfos().getSigners().iterator().next()).getSignedAttributes();
        Attribute hash = table.get(CMSAttributes.messageDigest);

        assertTrue(MessageDigest.isEqual(contentDigest, ((ASN1OctetString)hash.getAttrValues().getObjectAt(0)).getOctets()));

        //
        // try using existing signer
        //
        gen = new CMSSignedDataStreamGenerator();

        gen.addSigners(sp.getSignerInfos());
       
        gen.addCertificatesAndCRLs(sp.getCertificatesAndCRLs("Collection", "BC"));
       
        bOut.reset();
       
        sigOut = gen.open(bOut, true);

        sigOut.write(TEST_MESSAGE.getBytes());
       
        sigOut.close();
View Full Code Here

TOP

Related Classes of org.bouncycastle.cms.CMSSignedDataStreamGenerator

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.