OutofMemory with CMSEnvelopedData from fileInputStream
Emiliano Latini <[email protected]> Tue, 24 Sep 2019 12:27:18 +0000
| Newsgroups | gmane.comp.encryption.bouncy-castle.devel |
|---|---|
| Message-ID | <PR1PR01MB4795A4DACC2E4A2F3A590A7798840@PR1PR01MB4795.eurprd01.prod.exchangelabs.com> |
--_000_PR1PR01MB4795A4DACC2E4A2F3A590A7798840PR1PR01MB4795eurp_
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: quoted-printable
Hi there,
I've some p7m files which I have to validate, their dimension ranges from 1=
00 Mb to 6Gb.
I'm creating CMSEnvelopedData with a FileInputStream this way:
CMSEnvelopedData envelopedData =3D new CMSEnvelopedData(encEnvelopedData);
Doing this I get a java.lang.OutOfMemoryError: Java heap space
As far as it seems the only way I have is to raise the Heap Space.
Is there a way to get CMSEnveloped data in chunks ?All I have to do is extr=
act the content stream.
Below the full code :
[snip]
CMSEnvelopedData envelopedData =3D new CMSEnvelopedData(encEnve=
lopedData);
RecipientInformationStore recipients =3D envelopedData.getRecip=
ientInfos();
RecipientInformation recipient =3D recipients.get(new JceKeyTra=
nsRecipientId((java.security.cert.X509Certificate)jcaProvider.getKeystore()=
.getCertificate(alias)));
if (recipient !=3D null) {
log.info("recipient!=3Dnull getting trans");
JceKeyTransEnvelopedRecipient trans=3Dnew JceKeyTransEnve=
lopedRecipient((PrivateKey)jcaProvider.getKeystore().getKey(alias,jcaProvid=
er.getPwd().toCharArray()));
trans.setMustProduceEncodableUnwrappedKey(true);
trans.setProvider(provider);
trans.setContentProvider(provider);
log.info("returning content!");
CMSTypedStream cmsTs=3D recipient.getContentStream(trans)=
;
return cmsTs.getContentStream();
}
[\snip]
Thanks a lot...
--_000_PR1PR01MB4795A4DACC2E4A2F3A590A7798840PR1PR01MB4795eurp_
Content-Type: text/html; charset="us-ascii"
Content-Transfer-Encoding: quoted-printable
<html xmlns:v=3D"urn:schemas-microsoft-com:vml" xmlns:o=3D"urn:schemas-micr=
osoft-com:office:office" xmlns:w=3D"urn:schemas-microsoft-com:office:word" =
xmlns:m=3D"http://schemas.microsoft.com/office/2004/12/omml" xmlns=3D"http:=
//www.w3.org/TR/REC-html40">
<head>
<meta http-equiv=3D"Content-Type" content=3D"text/html; charset=3Dus-ascii"=
>
<meta name=3D"Generator" content=3D"Microsoft Word 14 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
{font-family:Tahoma;
panose-1:2 11 6 4 3 5 4 4 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0cm;
margin-bottom:.0001pt;
font-size:11.0pt;
font-family:"Calibri","sans-serif";
mso-fareast-language:EN-US;}
a:link, span.MsoHyperlink
{mso-style-priority:99;
color:blue;
text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
{mso-style-priority:99;
color:purple;
text-decoration:underline;}
span.StileMessaggioDiPostaElettronica17
{mso-style-type:personal-compose;
font-family:"Tahoma","sans-serif";
color:windowtext;
font-weight:normal;
font-style:normal;
text-decoration:none none;}
.MsoChpDefault
{mso-style-type:export-only;
font-family:"Calibri","sans-serif";
mso-fareast-language:EN-US;}
@page WordSection1
{size:612.0pt 792.0pt;
margin:70.85pt 2.0cm 2.0cm 2.0cm;}
div.WordSection1
{page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext=3D"edit" spidmax=3D"1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext=3D"edit">
<o:idmap v:ext=3D"edit" data=3D"1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang=3D"IT" link=3D"blue" vlink=3D"purple">
<div class=3D"WordSection1">
<p class=3D"MsoNormal"><span style=3D"font-size:10.0pt;font-family:"Ta=
homa","sans-serif"">Hi there,<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span style=3D"font-size:10.0pt;font-family:"Ta=
homa","sans-serif""><o:p> </o:p></span></p>
<p class=3D"MsoNormal"><span lang=3D"EN-US" style=3D"font-size:10.0pt;font-=
family:"Tahoma","sans-serif"">I’ve some p7m files=
which I have to validate, their dimension ranges from 100 Mb to 6Gb.<o:p><=
/o:p></span></p>
<p class=3D"MsoNormal"><span lang=3D"EN-US" style=3D"font-size:10.0pt;font-=
family:"Tahoma","sans-serif"">I’m creating CMSEnv=
elopedData with a FileInputStream this way:<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span lang=3D"EN-US" style=3D"font-size:10.0pt;font-=
family:"Tahoma","sans-serif""><o:p> </o:p></span><=
/p>
<p class=3D"MsoNormal"><span lang=3D"EN-US" style=3D"font-size:10.0pt;font-=
family:"Tahoma","sans-serif"">CMSEnvelopedData envelope=
dData =3D new CMSEnvelopedData(encEnvelopedData);<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span lang=3D"EN-US" style=3D"font-size:10.0pt;font-=
family:"Tahoma","sans-serif""><o:p> </o:p></span><=
/p>
<p class=3D"MsoNormal"><span lang=3D"EN-US" style=3D"font-size:10.0pt;font-=
family:"Tahoma","sans-serif"">Doing this I get a =
java.lang.OutOfMemoryError: Java heap space<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span lang=3D"EN-US" style=3D"font-size:10.0pt;font-=
family:"Tahoma","sans-serif""><o:p> </o:p></span><=
/p>
<p class=3D"MsoNormal"><span lang=3D"EN-US" style=3D"font-size:10.0pt;font-=
family:"Tahoma","sans-serif"">As far as it seems the on=
ly way I have is to raise the Heap Space.<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span lang=3D"EN-US" style=3D"font-size:10.0pt;font-=
family:"Tahoma","sans-serif""><o:p> </o:p></span><=
/p>
<p class=3D"MsoNormal"><span lang=3D"EN-US" style=3D"font-size:10.0pt;font-=
family:"Tahoma","sans-serif"">Is there a way to get CMS=
Enveloped data in chunks ?All I have to do is extract the content stream.<o=
:p></o:p></span></p>
<p class=3D"MsoNormal"><span lang=3D"EN-US" style=3D"font-size:10.0pt;font-=
family:"Tahoma","sans-serif""><o:p> </o:p></span><=
/p>
<p class=3D"MsoNormal"><span lang=3D"EN-US" style=3D"font-size:10.0pt;font-=
family:"Tahoma","sans-serif"">Below the full code :<o:p=
></o:p></span></p>
<p class=3D"MsoNormal"><span lang=3D"EN-US" style=3D"font-size:10.0pt;font-=
family:"Tahoma","sans-serif""><o:p> </o:p></span><=
/p>
<p class=3D"MsoNormal"><span lang=3D"EN-US" style=3D"font-size:10.0pt;font-=
family:"Tahoma","sans-serif"">[snip]<o:p></o:p></span><=
/p>
<p class=3D"MsoNormal"><span lang=3D"EN-US" style=3D"font-size:10.0pt;font-=
family:"Tahoma","sans-serif""><o:p> </o:p></span><=
/p>
<p class=3D"MsoNormal"><span lang=3D"EN-US" style=3D"font-size:10.0pt;font-=
family:"Tahoma","sans-serif""> &=
nbsp;
</span><span lang=3D"EN-US" style=3D"font-size:10.0pt;font-family:"Cou=
rier New"">CMSEnvelopedData envelopedData =3D new CMSEnvelopedData(enc=
EnvelopedData);<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span lang=3D"EN-US" style=3D"font-size:10.0pt;font-=
family:"Courier New""> &=
nbsp;
<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span lang=3D"EN-US" style=3D"font-size:10.0pt;font-=
family:"Courier New""> &=
nbsp; RecipientInformationStore recipients =3D envelopedD=
ata.getRecipientInfos();<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span lang=3D"EN-US" style=3D"font-size:10.0pt;font-=
family:"Courier New""> &=
nbsp; RecipientInformation recipient =3D recipients.get(n=
ew JceKeyTransRecipientId((java.security.cert.X509Certificate)jcaProvider.g=
etKeystore().getCertificate(alias)));<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span lang=3D"EN-US" style=3D"font-size:10.0pt;font-=
family:"Courier New""> &=
nbsp; if (recipient !=3D null) {<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span lang=3D"EN-US" style=3D"font-size:10.0pt;font-=
family:"Courier New""> &=
nbsp; log.info("=
recipient!=3Dnull getting trans"); =
&n=
bsp; &nbs=
p;
<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span lang=3D"EN-US" style=3D"font-size:10.0pt;font-=
family:"Courier New""> &=
nbsp;
<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span lang=3D"EN-US" style=3D"font-size:10.0pt;font-=
family:"Courier New""> &=
nbsp; JceKeyTransEnve=
lopedRecipient trans=3Dnew JceKeyTransEnvelopedRecipient((PrivateKey)jcaPro=
vider.getKeystore().getKey(alias,jcaProvider.getPwd().toCharArray()));<o:p>=
</o:p></span></p>
<p class=3D"MsoNormal"><span lang=3D"EN-US" style=3D"font-size:10.0pt;font-=
family:"Courier New""><o:p> </o:p></span></p>
<p class=3D"MsoNormal"><span lang=3D"EN-US" style=3D"font-size:10.0pt;font-=
family:"Courier New""> &=
nbsp; trans.setMustPr=
oduceEncodableUnwrappedKey(true);<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span lang=3D"EN-US" style=3D"font-size:10.0pt;font-=
family:"Courier New""> &=
nbsp; trans.setProvid=
er(provider);<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span lang=3D"EN-US" style=3D"font-size:10.0pt;font-=
family:"Courier New""> &=
nbsp; trans.setConten=
tProvider(provider);<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span lang=3D"EN-US" style=3D"font-size:10.0pt;font-=
family:"Courier New""> &=
nbsp;
<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span lang=3D"EN-US" style=3D"font-size:10.0pt;font-=
family:"Courier New""> &=
nbsp;
<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span lang=3D"EN-US" style=3D"font-size:10.0pt;font-=
family:"Courier New""> &=
nbsp; log.info("=
returning content!");<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span lang=3D"EN-US" style=3D"font-size:10.0pt;font-=
family:"Courier New""> &=
nbsp;
<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span lang=3D"EN-US" style=3D"font-size:10.0pt;font-=
family:"Courier New""> &=
nbsp; CMSTypedStream =
cmsTs=3D recipient.getContentStream(trans);<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span lang=3D"EN-US" style=3D"font-size:10.0pt;font-=
family:"Courier New""> &=
nbsp; return cmsTs.ge=
tContentStream();<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span lang=3D"EN-US" style=3D"font-size:10.0pt;font-=
family:"Courier New""><o:p> </o:p></span></p>
<p class=3D"MsoNormal"><span lang=3D"EN-US" style=3D"font-size:10.0pt;font-=
family:"Courier New""> &=
nbsp;
<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span lang=3D"EN-US" style=3D"font-size:10.0pt;font-=
family:"Courier New""> &=
nbsp; }<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span lang=3D"EN-US" style=3D"font-size:10.0pt;font-=
family:"Courier New"">[\snip]<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span lang=3D"EN-US" style=3D"font-size:10.0pt;font-=
family:"Courier New""><o:p> </o:p></span></p>
<p class=3D"MsoNormal"><span lang=3D"EN-US" style=3D"font-size:10.0pt;font-=
family:"Courier New""><o:p> </o:p></span></p>
<p class=3D"MsoNormal"><span lang=3D"EN-US" style=3D"font-size:10.0pt;font-=
family:"Tahoma","sans-serif"">Thanks a lot…<o:p><=
/o:p></span></p>
<p class=3D"MsoNormal"><span lang=3D"EN-US" style=3D"font-size:10.0pt;font-=
family:"Courier New""><o:p> </o:p></span></p>
</div>
</body>
</html>
--_000_PR1PR01MB4795A4DACC2E4A2F3A590A7798840PR1PR01MB4795eurp_--