[prev in list] [next in list] [prev in thread] [next in thread] 

List:       bouncycastle-crypto-dev
Subject:    [dev-crypto] Re: HOW TO SAVE ATTACHED CMSSIGNEDDATA TO DISK
From:       Niels Stevens <steniels () gmail ! com>
Date:       2010-05-28 10:13:05
Message-ID: 7E239AD7-D9A8-4DC1-91FF-2F63CBDD2125 () gmail ! com
[Download RAW message or body]

I have done it like David has mentioned, I put the CMS into an SMIME.
Afterwards I use an ByteArrayOutputstream something like this :

 CMSSignedDataGenerator generator = new CMSSignedDataGenerator();
        generator.addSigner(privateKey, (X509Certificate) userCert, \
CMSSignedDataGenerator.DIGEST_SHA1);  generator.addCertificatesAndCRLs(store);
        CMSProcessable content = new \
                CMSProcessableByteArray(json.toString().getBytes("ASCII"));
        CMSSignedData signedData = generator.generate(content, true, "BC");
        //END SIGNED PKCS7

        Session session = null;
        MimeMessage msg = new MimeMessage(session);

        msg.setDisposition("attachment; filename=smime.p7m");
        String contentType = "application/x-pkcs7-mime; smime-type=signed-data; \
                name=smime.p7m";
        // If we have a file name, set the content type based on iterSigners.

        msg.setContent(signedData.getEncoded(), contentType);

        msg.saveChanges();
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        msg.writeTo(out);
        out.close();
        smime = new String(out.toByteArray(), "ASCII");

Hopefully this is something useful for you.

Regards,

Niels

Op 28-mei-2010, om 01:03 heeft David Hook-2 [via Bouncy Castle] het volgende \
geschreven:

> 
> Normally, you'd do this using S/MIME. 
> 
> Regards, 
> 
> David 
> 
> On Thu, 2010-05-27 at 14:19 -0300, Toyoaki Ejiri wrote:
> 
> > Hello! 
> > 
> > My problem is that I need to save an attached CMSSignedData to a pkcs7 
> > file. 
> > 
> > When I use the CMSSignedData.getEncoded() method to retrieve the byte 
> > array and save it in the disk, bouncycastle gives me a byte array 
> > without the attached document file (signedContent field of the 
> > CMSSignedData) . 
> > 
> > Does anyone know what I have to do to get a byte[] that has the full 
> > attached CMSSignedData? 
> > 
> > Best Regards, 
> > 
> > --------------------------------------------------- 
> > Toyoaki Ejiri 
> > ---------------------------------------------------
> 
> 
> 
> 
> 
> View message @ http://bouncy-castle.1462172.n4.nabble.com/HOW-TO-SAVE-ATTACHED-CMSSIGNEDDATA-TO-DISK-tp2233493p2233927.html \
>  To unsubscribe from Bouncy Castle, click here.
> 


-- 
View this message in context: \
http://bouncy-castle.1462172.n4.nabble.com/HOW-TO-SAVE-ATTACHED-CMSSIGNEDDATA-TO-DISK-tp2233493p2234371.html
 Sent from the Bouncy Castle - Dev mailing list archive at Nabble.com.


[Attachment #3 (text/html)]

I have done it like David has mentioned, I put the CMS into an SMIME.<div>Afterwards \
I use an ByteArrayOutputstream something like this \
:</div><div><br></div><div><div>&nbsp;CMSSignedDataGenerator generator = new \
CMSSignedDataGenerator();</div><div>&nbsp;&nbsp; &nbsp; &nbsp; \
&nbsp;generator.addSigner(privateKey, (X509Certificate) userCert, \
CMSSignedDataGenerator.DIGEST_SHA1);</div><div>&nbsp;&nbsp; &nbsp; &nbsp; \
&nbsp;generator.addCertificatesAndCRLs(store);</div><div>&nbsp;&nbsp; &nbsp; &nbsp; \
&nbsp;CMSProcessable content = new \
CMSProcessableByteArray(json.toString().getBytes("ASCII"));</div><div>&nbsp;&nbsp; \
&nbsp; &nbsp; &nbsp;CMSSignedData signedData = generator.generate(content, true, \
"BC");</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;//END SIGNED \
PKCS7</div><div><br></div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;Session session = \
null;</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;MimeMessage msg = new \
MimeMessage(session);</div><div><br></div><div>&nbsp;&nbsp; &nbsp; &nbsp; \
&nbsp;msg.setDisposition("attachment; filename=smime.p7m");</div><div>&nbsp;&nbsp; \
&nbsp; &nbsp; &nbsp;String contentType = "application/x-pkcs7-mime; \
smime-type=signed-data; name=smime.p7m";</div><div>&nbsp;&nbsp; &nbsp; &nbsp; \
&nbsp;// If we have a file name, set the content type based on \
iterSigners.</div><div><br></div><div>&nbsp;&nbsp; &nbsp; &nbsp; \
&nbsp;msg.setContent(signedData.getEncoded(), \
contentType);</div><div><br></div><div>&nbsp;&nbsp; &nbsp; &nbsp; \
&nbsp;msg.saveChanges();</div><div>&nbsp;&nbsp; &nbsp; &nbsp; \
&nbsp;ByteArrayOutputStream out = new ByteArrayOutputStream();</div><div>&nbsp;&nbsp; \
&nbsp; &nbsp; &nbsp;msg.writeTo(out);</div><div>&nbsp;&nbsp; &nbsp; &nbsp; \
&nbsp;out.close();</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;smime = new \
String(out.toByteArray(), "ASCII");</div><div><br></div><div>Hopefully this is \
something useful for \
you.</div><div><br></div><div>Regards,</div><div><br></div><div>Niels</div><div><br></div><div><div>Op \
28-mei-2010, om 01:03 heeft David Hook-2 [via Bouncy Castle] het volgende \
geschreven:</div><br class="Apple-interchange-newline"><blockquote type="cite">

<br>Normally, you'd do this using S/MIME.
<br><br>Regards,
<br><br>David
<br><br>On Thu, 2010-05-27 at 14:19 -0300, Toyoaki Ejiri wrote:
<div class="shrinkable-quote"><div class='shrinkable-quote'><br>&gt; Hello!
<br>&gt; 
<br>&gt; My problem is that I need to save an attached CMSSignedData to a pkcs7
<br>&gt; file.
<br>&gt; 
<br>&gt; When I use the CMSSignedData.getEncoded() method to retrieve the byte
<br>&gt; array and save it in the disk, bouncycastle gives me a byte array
<br>&gt; without the attached document file (signedContent field of the
<br>&gt; CMSSignedData) .
<br>&gt; 
<br>&gt; Does anyone know what I have to do to get a byte[] that has the full
<br>&gt; attached CMSSignedData?
<br>&gt; 
<br>&gt; Best Regards,
<br>&gt; 
<br>&gt; ---------------------------------------------------
<br>&gt; Toyoaki Ejiri
<br>&gt; ---------------------------------------------------
</div></div><br><br>
<br><br>
<hr noshade="noshade" size="1" color="#cccccc">
<div style="color:#666666; font: 11px tahoma,geneva,helvetica,arial,sans-serif;">
View message @ <a href="http://bouncy-castle.1462172.n4.nabble.com/HOW-TO-SAVE-ATTACHED-CMSSIGNEDDATA-TO-DISK-tp2233493p2233927.html" \
target="_top" rel="nofollow" \
link="external">http://bouncy-castle.1462172.n4.nabble.com/HOW-TO-SAVE-ATTACHED-CMSSIGNEDDATA-TO-DISK-tp2233493p2233927.html</a>


<br>To unsubscribe from Bouncy Castle, <a target="_top" rel="nofollow" \
link="external">click here</a>. </div>
<br>
</blockquote></div><br></div>
<br><hr align="left" width="300">
View this message in context: <a \
href="http://bouncy-castle.1462172.n4.nabble.com/HOW-TO-SAVE-ATTACHED-CMSSIGNEDDATA-TO-DISK-tp2233493p2234371.html">Re: \
HOW TO SAVE ATTACHED CMSSIGNEDDATA TO DISK</a><br> Sent from the <a \
href="http://bouncy-castle.1462172.n4.nabble.com/Bouncy-Castle-Dev-f1462173.html">Bouncy \
Castle - Dev mailing list archive</a> at Nabble.com.<br>



[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic