Re: Facing issue with i2d_CMS_ContentInfo and d2i_CMS_ContentInfo in 3.x.x

madhu bajpai <madhu.bajpai-KrHygFoaG1gqDJ6do+/[email protected]> Mon, 27 Oct 2025 10:42:50 +0530
Newsgroups gmane.comp.encryption.openssl.user,gmane.comp.encryption.openssl.devel
Message-ID <[email protected]>
--Apple-Mail=_AB48D4E1-F798-4561-844D-BE7E8B8E54A9
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain; charset="UTF-8"

Any inputs/suggestions from anyone?

> On 21 Oct 2025, at 3:57=E2=80=AFPM, madhu bajpai <madhu.bajpai@rimus-tech=
.com> wrote:
>=20
> Hello everyone,
>=20
> We have CMS encryption and decryption implemented in two different functi=
ons and using i2d_CMS_ContentInfo and d2i_CMS_ContentInfo for CMS_ContentIn=
fo object serialisation and deserialisation across functions. We are using =
our own PKCS11 engine.
>=20
> Code was working fine with OpenSSL 1.1.1x but failing in 3.x.x.
>=20
> Encryption Code Snippet:
>     OSSL_LIB_CTX* libctx =3D OSSL_LIB_CTX_new();=20
>     CMS_ContentInfo* pCMS =3D CMS_encrypt_ex(pCertStack, inData, EVP_aes_=
256_cbc(), CMS_BINARY, libctx, NULL);
>=20
>     unsigned char *buf =3D NULL;
>     int len =3D i2d_CMS_ContentInfo(pCMS, &buf);
>=20
> Decryption Code Snippet:
>=20
>     pkcs11_set_pin( [userPIN cStringUsingEncoding:NSASCIIStringEncoding] =
);
>     CMS_ContentInfo* p7Env =3D d2i_CMS_ContentInfo(NULL, (unsigned char c=
onst**)&pEncryptedMessage, encryptedMessageLength);
>  =20
>     NSString* idString =3D [keyID toIdString];
>     const char* szParams =3D [idString UTF8String];
>     EVP_PKEY* key =3D ENGINE_load_private_key(engineID, szParams , UI_Ope=
nSSL(), NULL);
>     int error =3D CMS_decrypt(p7Env, key , user, NULL, outData, 0);
>=20
> Additional Notes:
>=20
> * Looks like with serialization/deserialization, it is loosing our engine=
 context.=20
>=20
> * Tried i2d_CMS_bio & d2i_CMS_bio but no success.
>=20
> * If I perform encryption & decryption within same function (and don=E2=
=80=99t use serialisation/deserialization), decryption works.
>=20
> * We do have plans to move from engine to provider implementation but it =
may take some time.
>=20
> Looking for your suggestions to fix this issue.
>=20
> Regards,
>=20
> Madhu
>=20
>=20
>=20
>=20
>=20

--=20
You received this message because you are subscribed to the Google Groups "=
openssl-users" group.
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to openssl-users+unsubscribe-MCmKBN63+Bmbup2nOX2J7Q@public.gmane.org
To view this discussion visit https://groups.google.com/a/openssl.org/d/msg=
id/openssl-users/C7E93A8D-8C63-430D-9310-3F5E9AF1CB28%40rimus-tech.com.

--Apple-Mail=_AB48D4E1-F798-4561-844D-BE7E8B8E54A9
Content-Transfer-Encoding: quoted-printable
Content-Type: text/html; charset="UTF-8"

<html><head><meta http-equiv=3D"content-type" content=3D"text/html; charset=
=3Dutf-8"></head><body style=3D"overflow-wrap: break-word; -webkit-nbsp-mod=
e: space; line-break: after-white-space;">Any inputs/suggestions from anyon=
e?<br id=3D"lineBreakAtBeginningOfMessage"><div><br><blockquote type=3D"cit=
e"><div>On 21 Oct 2025, at 3:57=E2=80=AFPM, madhu bajpai &lt;madhu.bajpai@r=
imus-tech.com&gt; wrote:</div><br class=3D"Apple-interchange-newline"><div>=
<meta http-equiv=3D"content-type" content=3D"text/html; charset=3Dutf-8"><d=
iv style=3D"overflow-wrap: break-word; -webkit-nbsp-mode: space; line-break=
: after-white-space;"><div dir=3D"auto" style=3D"overflow-wrap: break-word;=
 -webkit-nbsp-mode: space; line-break: after-white-space;">Hello everyone,<=
div><br></div><div>We have CMS encryption and decryption implemented in two=
 different functions and using i2d_CMS_ContentInfo and d2i_CMS_ContentInfo =
for CMS_ContentInfo object serialisation and deserialisation across functio=
ns. We are using our own PKCS11 engine.</div><div><br></div><div>Code was w=
orking fine with OpenSSL 1.1.1x but failing in 3.x.x.</div><div><br></div><=
div><b>Encryption Code Snippet:</b></div><div><span data-teams=3D"true"><p>=
&nbsp; &nbsp; OSSL_LIB_CTX* libctx =3D OSSL_LIB_CTX_new();&nbsp;<br>&nbsp; =
&nbsp; CMS_ContentInfo* pCMS =3D <b>CMS_encrypt_ex</b>(pCertStack, inData, =
EVP_aes_256_cbc(), CMS_BINARY, libctx, NULL);</p><p>&nbsp; &nbsp; unsigned =
char *buf =3D NULL;<br>&nbsp; &nbsp; int len =3D <b>i2d_CMS_ContentInfo</b>=
(pCMS, &amp;buf);</p><p><b>Decryption Code Snippet:</b></p><div style=3D"ma=
rgin: 0px; font-style: normal; font-variant-caps: normal; font-stretch: nor=
mal; line-height: normal; font-size-adjust: none; font-kerning: auto; font-=
variant-alternates: normal; font-variant-ligatures: normal; font-variant-nu=
meric: normal; font-variant-east-asian: normal; font-variant-position: norm=
al; font-feature-settings: normal; font-optical-sizing: auto; font-variatio=
n-settings: normal; background-color: rgb(255, 255, 255);"><font color=3D"#=
326d74">&nbsp; &nbsp; </font><b>pkcs11_set_pin</b><span style=3D"font-style=
: normal; font-variant-caps: normal; font-stretch: normal; line-height: nor=
mal; font-size-adjust: none; font-kerning: auto; font-variant-alternates: n=
ormal; font-variant-ligatures: normal; font-variant-numeric: normal; font-v=
ariant-east-asian: normal; font-variant-position: normal; font-feature-sett=
ings: normal; font-optical-sizing: auto; font-variation-settings: normal;">=
( [</span><span style=3D"color: rgb(50, 109, 116);">userPIN</span><span sty=
le=3D"font-style: normal; font-variant-caps: normal; font-stretch: normal; =
line-height: normal; font-size-adjust: none; font-kerning: auto; font-varia=
nt-alternates: normal; font-variant-ligatures: normal; font-variant-numeric=
: normal; font-variant-east-asian: normal; font-variant-position: normal; f=
ont-feature-settings: normal; font-optical-sizing: auto; font-variation-set=
tings: normal;"> </span><font color=3D"#6c36a9">cStringUsingEncoding</font>=
<span style=3D"font-style: normal; font-variant-caps: normal; font-stretch:=
 normal; line-height: normal; font-size-adjust: none; font-kerning: auto; f=
ont-variant-alternates: normal; font-variant-ligatures: normal; font-varian=
t-numeric: normal; font-variant-east-asian: normal; font-variant-position: =
normal; font-feature-settings: normal; font-optical-sizing: auto; font-vari=
ation-settings: normal;">:</span><font color=3D"#6c36a9">NSASCIIStringEncod=
ing</font><span style=3D"font-style: normal; font-variant-caps: normal; fon=
t-stretch: normal; line-height: normal; font-size-adjust: none; font-kernin=
g: auto; font-variant-alternates: normal; font-variant-ligatures: normal; f=
ont-variant-numeric: normal; font-variant-east-asian: normal; font-variant-=
position: normal; font-feature-settings: normal; font-optical-sizing: auto;=
 font-variation-settings: normal;">] );</span></div><div style=3D"margin: 0=
px; font-style: normal; font-variant-caps: normal; font-stretch: normal; li=
ne-height: normal; font-size-adjust: none; font-kerning: auto; font-variant=
-alternates: normal; font-variant-ligatures: normal; font-variant-numeric: =
normal; font-variant-east-asian: normal; font-variant-position: normal; fon=
t-feature-settings: normal; font-optical-sizing: auto; font-variation-setti=
ngs: normal; background-color: rgb(255, 255, 255);"><span style=3D"">&nbsp;=
 &nbsp;&nbsp;</span><span style=3D"color: rgb(28, 70, 74); font-style: norm=
al; font-variant-caps: normal; font-stretch: normal; line-height: normal; f=
ont-size-adjust: none; font-kerning: auto; font-variant-alternates: normal;=
 font-variant-ligatures: normal; font-variant-numeric: normal; font-variant=
-east-asian: normal; font-variant-position: normal; font-feature-settings: =
normal; font-optical-sizing: auto; font-variation-settings: normal;">CMS_Co=
ntentInfo</span><span style=3D"">* p7Env =3D </span><span style=3D"font-sty=
le: normal; font-variant-caps: normal; font-stretch: normal; line-height: n=
ormal; font-size-adjust: none; font-kerning: auto; font-variant-alternates:=
 normal; font-variant-ligatures: normal; font-variant-numeric: normal; font=
-variant-east-asian: normal; font-variant-position: normal; font-feature-se=
ttings: normal; font-optical-sizing: auto; font-variation-settings: normal;=
"><b>d2i_CMS_ContentInfo</b></span><span style=3D"">(</span><span style=3D"=
color: rgb(155, 35, 147); font-style: normal; font-variant-caps: normal; fo=
nt-stretch: normal; line-height: normal; font-size-adjust: none; font-kerni=
ng: auto; font-variant-alternates: normal; font-variant-ligatures: normal; =
font-variant-numeric: normal; font-variant-east-asian: normal; font-variant=
-position: normal; font-feature-settings: normal; font-optical-sizing: auto=
; font-variation-settings: normal;"><b>NULL</b></span><span style=3D"">, (<=
/span><span style=3D"color: rgb(155, 35, 147); font-style: normal; font-var=
iant-caps: normal; font-stretch: normal; line-height: normal; font-size-adj=
ust: none; font-kerning: auto; font-variant-alternates: normal; font-varian=
t-ligatures: normal; font-variant-numeric: normal; font-variant-east-asian:=
 normal; font-variant-position: normal; font-feature-settings: normal; font=
-optical-sizing: auto; font-variation-settings: normal;"><b>unsigned</b></s=
pan> <span style=3D"color: rgb(155, 35, 147); font-style: normal; font-vari=
ant-caps: normal; font-stretch: normal; line-height: normal; font-size-adju=
st: none; font-kerning: auto; font-variant-alternates: normal; font-variant=
-ligatures: normal; font-variant-numeric: normal; font-variant-east-asian: =
normal; font-variant-position: normal; font-feature-settings: normal; font-=
optical-sizing: auto; font-variation-settings: normal;"><b>char</b></span> =
<span style=3D"color: rgb(155, 35, 147); font-style: normal; font-variant-c=
aps: normal; font-stretch: normal; line-height: normal; font-size-adjust: n=
one; font-kerning: auto; font-variant-alternates: normal; font-variant-liga=
tures: normal; font-variant-numeric: normal; font-variant-east-asian: norma=
l; font-variant-position: normal; font-feature-settings: normal; font-optic=
al-sizing: auto; font-variation-settings: normal;"><b>const</b></span><span=
 style=3D"">**)&amp;pEncryptedMessage, encryptedMessageLength);</span></div=
><p style=3D"margin: 0px; font-style: normal; font-variant-caps: normal; fo=
nt-stretch: normal; line-height: normal; font-size-adjust: none; font-kerni=
ng: auto; font-variant-alternates: normal; font-variant-ligatures: normal; =
font-variant-numeric: normal; font-variant-east-asian: normal; font-variant=
-position: normal; font-feature-settings: normal; font-optical-sizing: auto=
; font-variation-settings: normal; background-color: rgb(255, 255, 255);"><=
span style=3D"font-size: 18px; font-family: Menlo;">&nbsp;&nbsp;</span></p>=
<div style=3D"margin: 0px; font-style: normal; font-variant-caps: normal; f=
ont-stretch: normal; line-height: normal; font-size-adjust: none; font-kern=
ing: auto; font-variant-alternates: normal; font-variant-ligatures: normal;=
 font-variant-numeric: normal; font-variant-east-asian: normal; font-varian=
t-position: normal; font-feature-settings: normal; font-optical-sizing: aut=
o; font-variation-settings: normal; background-color: rgb(255, 255, 255);">=
<span style=3D"font-style: normal; font-variant-caps: normal; font-stretch:=
 normal; line-height: normal; font-size-adjust: none; font-kerning: auto; f=
ont-variant-alternates: normal; font-variant-ligatures: normal; font-varian=
t-numeric: normal; font-variant-east-asian: normal; font-variant-position: =
normal; font-feature-settings: normal; font-optical-sizing: auto; font-vari=
ation-settings: normal; color: rgb(57, 0, 160);">&nbsp; &nbsp; NSString</sp=
an>* idString =3D [keyID <span style=3D"font-style: normal; font-variant-ca=
ps: normal; font-stretch: normal; line-height: normal; font-size-adjust: no=
ne; font-kerning: auto; font-variant-alternates: normal; font-variant-ligat=
ures: normal; font-variant-numeric: normal; font-variant-east-asian: normal=
; font-variant-position: normal; font-feature-settings: normal; font-optica=
l-sizing: auto; font-variation-settings: normal; color: rgb(50, 109, 116);"=
>toIdString</span>];</div><div style=3D"margin: 0px; font-style: normal; fo=
nt-variant-caps: normal; font-stretch: normal; line-height: normal; font-si=
ze-adjust: none; font-kerning: auto; font-variant-alternates: normal; font-=
variant-ligatures: normal; font-variant-numeric: normal; font-variant-east-=
asian: normal; font-variant-position: normal; font-feature-settings: normal=
; font-optical-sizing: auto; font-variation-settings: normal; background-co=
lor: rgb(255, 255, 255);">&nbsp; &nbsp; <span style=3D"font-style: normal; =
font-variant-caps: normal; font-stretch: normal; line-height: normal; font-=
size-adjust: none; font-kerning: auto; font-variant-alternates: normal; fon=
t-variant-ligatures: normal; font-variant-numeric: normal; font-variant-eas=
t-asian: normal; font-variant-position: normal; font-feature-settings: norm=
al; font-optical-sizing: auto; font-variation-settings: normal; color: rgb(=
155, 35, 147);"><b>const</b></span> <span style=3D"font-style: normal; font=
-variant-caps: normal; font-stretch: normal; line-height: normal; font-size=
-adjust: none; font-kerning: auto; font-variant-alternates: normal; font-va=
riant-ligatures: normal; font-variant-numeric: normal; font-variant-east-as=
ian: normal; font-variant-position: normal; font-feature-settings: normal; =
font-optical-sizing: auto; font-variation-settings: normal; color: rgb(155,=
 35, 147);"><b>char</b></span>* szParams =3D [idString <span style=3D"font-=
style: normal; font-variant-caps: normal; font-stretch: normal; line-height=
: normal; font-size-adjust: none; font-kerning: auto; font-variant-alternat=
es: normal; font-variant-ligatures: normal; font-variant-numeric: normal; f=
ont-variant-east-asian: normal; font-variant-position: normal; font-feature=
-settings: normal; font-optical-sizing: auto; font-variation-settings: norm=
al; color: rgb(108, 54, 169);">UTF8String</span>];</div><div style=3D"margi=
n: 0px; font-style: normal; font-variant-caps: normal; font-stretch: normal=
; line-height: normal; font-size-adjust: none; font-kerning: auto; font-var=
iant-alternates: normal; font-variant-ligatures: normal; font-variant-numer=
ic: normal; font-variant-east-asian: normal; font-variant-position: normal;=
 font-feature-settings: normal; font-optical-sizing: auto; font-variation-s=
ettings: normal; background-color: rgb(255, 255, 255);"><span style=3D"font=
-style: normal; font-variant-caps: normal; font-stretch: normal; line-heigh=
t: normal; font-size-adjust: none; font-kerning: auto; font-variant-alterna=
tes: normal; font-variant-ligatures: normal; font-variant-numeric: normal; =
font-variant-east-asian: normal; font-variant-position: normal; font-featur=
e-settings: normal; font-optical-sizing: auto; font-variation-settings: nor=
mal;">&nbsp; &nbsp;&nbsp;</span><span style=3D"color: rgb(28, 70, 74);">EVP=
_PKEY</span><span style=3D"font-style: normal; font-variant-caps: normal; f=
ont-stretch: normal; line-height: normal; font-size-adjust: none; font-kern=
ing: auto; font-variant-alternates: normal; font-variant-ligatures: normal;=
 font-variant-numeric: normal; font-variant-east-asian: normal; font-varian=
t-position: normal; font-feature-settings: normal; font-optical-sizing: aut=
o; font-variation-settings: normal;">* key =3D </span><b>ENGINE_load_privat=
e_key</b><span style=3D"font-style: normal; font-variant-caps: normal; font=
-stretch: normal; line-height: normal; font-size-adjust: none; font-kerning=
: auto; font-variant-alternates: normal; font-variant-ligatures: normal; fo=
nt-variant-numeric: normal; font-variant-east-asian: normal; font-variant-p=
osition: normal; font-feature-settings: normal; font-optical-sizing: auto; =
font-variation-settings: normal;">(engineID, szParams , </span><font color=
=3D"#326d74">UI_OpenSSL</font><span style=3D"font-style: normal; font-varia=
nt-caps: normal; font-stretch: normal; line-height: normal; font-size-adjus=
t: none; font-kerning: auto; font-variant-alternates: normal; font-variant-=
ligatures: normal; font-variant-numeric: normal; font-variant-east-asian: n=
ormal; font-variant-position: normal; font-feature-settings: normal; font-o=
ptical-sizing: auto; font-variation-settings: normal;">(), </span><span sty=
le=3D"color: rgb(155, 35, 147);"><b>NULL</b></span><span style=3D"font-styl=
e: normal; font-variant-caps: normal; font-stretch: normal; line-height: no=
rmal; font-size-adjust: none; font-kerning: auto; font-variant-alternates: =
normal; font-variant-ligatures: normal; font-variant-numeric: normal; font-=
variant-east-asian: normal; font-variant-position: normal; font-feature-set=
tings: normal; font-optical-sizing: auto; font-variation-settings: normal;"=
>);</span></div><p>&nbsp; &nbsp;&nbsp;<span style=3D"background-color: rgb(=
255, 255, 255); font-stretch: normal; line-height: normal; font-size-adjust=
: none; font-kerning: auto; font-variant-alternates: normal; font-variant-l=
igatures: normal; font-variant-numeric: normal; font-variant-east-asian: no=
rmal; font-variant-position: normal; font-feature-settings: normal; font-op=
tical-sizing: auto; font-variation-settings: normal; color: rgb(155, 35, 14=
7);">int</span><span style=3D"background-color: rgb(255, 255, 255);"> error=
 =3D </span><span style=3D"background-color: rgb(255, 255, 255); font-stret=
ch: normal; line-height: normal; font-size-adjust: none; font-kerning: auto=
; font-variant-alternates: normal; font-variant-ligatures: normal; font-var=
iant-numeric: normal; font-variant-east-asian: normal; font-variant-positio=
n: normal; font-feature-settings: normal; font-optical-sizing: auto; font-v=
ariation-settings: normal;"><b>CMS_decrypt</b></span><span style=3D"backgro=
und-color: rgb(255, 255, 255);">(p7Env, key , user, </span><span style=3D"b=
ackground-color: rgb(255, 255, 255); font-stretch: normal; line-height: nor=
mal; font-size-adjust: none; font-kerning: auto; font-variant-alternates: n=
ormal; font-variant-ligatures: normal; font-variant-numeric: normal; font-v=
ariant-east-asian: normal; font-variant-position: normal; font-feature-sett=
ings: normal; font-optical-sizing: auto; font-variation-settings: normal; c=
olor: rgb(155, 35, 147);">NULL</span><span style=3D"background-color: rgb(2=
55, 255, 255);">, outData, </span><span style=3D"background-color: rgb(255,=
 255, 255); font-stretch: normal; line-height: normal; font-size-adjust: no=
ne; font-kerning: auto; font-variant-alternates: normal; font-variant-ligat=
ures: normal; font-variant-numeric: normal; font-variant-east-asian: normal=
; font-variant-position: normal; font-feature-settings: normal; font-optica=
l-sizing: auto; font-variation-settings: normal; color: rgb(28, 0, 207);">0=
</span><span style=3D"background-color: rgb(255, 255, 255);">);</span></p><=
p><b>Additional Notes:</b></p><p>* Looks like with serialization/deserializ=
ation, it is loosing our engine context.&nbsp;</p><p>* Tried i<span style=
=3D"background-color: rgb(255, 255, 255);">2d_CMS_bio &amp;&nbsp;</span><sp=
an style=3D"background-color: rgb(255, 255, 255);">d2i_CMS_bio but no succe=
ss.</span></p><p><span style=3D"background-color: rgb(255, 255, 255);">* If=
 I perform encryption &amp; decryption within same function (and <b>don=E2=
=80=99t use</b> serialisation/deserialization), decryption works.</span></p=
><p><span style=3D"background-color: rgb(255, 255, 255);">* We do have plan=
s to move from engine to provider implementation but it may take some time.=
</span></p><p><span style=3D"background-color: rgb(255, 255, 255);">Looking=
 for your suggestions to fix this issue.</span></p><p><span style=3D"backgr=
ound-color: rgb(255, 255, 255);">Regards,</span></p><p><span style=3D"backg=
round-color: rgb(255, 255, 255);">Madhu</span></p><p><span style=3D"backgro=
und-color: rgb(255, 255, 255);"><br></span></p><p><span style=3D"background=
-color: rgb(255, 255, 255);"><br></span></p></span></div></div></div></div>=
</blockquote></div><br></body></html>

<p></p>

-- <br />
You received this message because you are subscribed to the Google Groups &=
quot;openssl-users&quot; group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:openssl-users+unsubscribe-MCmKBN63+BlAfugRpC6u6w@public.gmane.org">openssl-us=
[email protected]</a>.<br />
To view this discussion visit <a href=3D"https://groups.google.com/a/openss=
l.org/d/msgid/openssl-users/C7E93A8D-8C63-430D-9310-3F5E9AF1CB28%40rimus-te=
ch.com?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.com/a/=
openssl.org/d/msgid/openssl-users/C7E93A8D-8C63-430D-9310-3F5E9AF1CB28%40ri=
mus-tech.com</a>.<br />

--Apple-Mail=_AB48D4E1-F798-4561-844D-BE7E8B8E54A9--