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

madhu bajpai <madhu.bajpai-KrHygFoaG1gqDJ6do+/[email protected]> Tue, 21 Oct 2025 15:57:57 +0530
Newsgroups gmane.comp.encryption.openssl.user,gmane.comp.encryption.openssl.devel
Message-ID <[email protected]>
--Apple-Mail=_1A6FC02C-E244-4D21-AF7C-DDBEDA8D03A3
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain; charset="UTF-8"

Hello everyone,

We have CMS encryption and decryption implemented in two different function=
s and using i2d_CMS_ContentInfo and d2i_CMS_ContentInfo for CMS_ContentInfo=
 object serialisation and deserialisation across functions. We are using ou=
r own PKCS11 engine.

Code was working fine with OpenSSL 1.1.1x but failing in 3.x.x.

Encryption Code Snippet:
    OSSL_LIB_CTX* libctx =3D OSSL_LIB_CTX_new();=20
    CMS_ContentInfo* pCMS =3D CMS_encrypt_ex(pCertStack, inData, EVP_aes_25=
6_cbc(), CMS_BINARY, libctx, NULL);

    unsigned char *buf =3D NULL;
    int len =3D i2d_CMS_ContentInfo(pCMS, &buf);

Decryption Code Snippet:

    pkcs11_set_pin( [userPIN cStringUsingEncoding:NSASCIIStringEncoding] );
    CMS_ContentInfo* p7Env =3D d2i_CMS_ContentInfo(NULL, (unsigned char con=
st**)&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_OpenS=
SL(), NULL);
    int error =3D CMS_decrypt(p7Env, key , user, NULL, outData, 0);

Additional Notes:

* Looks like with serialization/deserialization, it is loosing our engine c=
ontext.=20

* Tried i2d_CMS_bio & d2i_CMS_bio but no success.

* If I perform encryption & decryption within same function (and don=E2=80=
=99t use serialisation/deserialization), decryption works.

* We do have plans to move from engine to provider implementation but it ma=
y take some time.

Looking for your suggestions to fix this issue.

Regards,

Madhu




--=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/4440D5EA-2F67-4CB8-8913-CC3C4374C85F%40rimus-tech.com.

--Apple-Mail=_1A6FC02C-E244-4D21-AF7C-DDBEDA8D03A3
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;"><div dir=3D"auto" style=3D"overfl=
ow-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-spac=
e;">Hello everyone,<div><br></div><div>We have CMS encryption and decryptio=
n implemented in two different functions and using i2d_CMS_ContentInfo and =
d2i_CMS_ContentInfo for CMS_ContentInfo object serialisation and deserialis=
ation across functions. We are using our own PKCS11 engine.</div><div><br><=
/div><div>Code was working 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 dat=
a-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>&nb=
sp; &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"margin: 0px; font-style: normal; font-variant-caps: norma=
l; font-stretch: normal; line-height: normal; font-size-adjust: none; font-=
kerning: auto; font-variant-alternates: normal; font-variant-ligatures: nor=
mal; font-variant-numeric: normal; font-variant-east-asian: normal; font-va=
riant-position: normal; font-feature-settings: normal; font-optical-sizing:=
 auto; font-variation-settings: normal; background-color: rgb(255, 255, 255=
);"><font color=3D"#326d74">&nbsp; &nbsp; </font><b>pkcs11_set_pin</b><span=
 style=3D"color: rgba(0, 0, 0, 0.85); 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-ligatur=
es: 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;">( [</span><span style=3D"co=
lor: rgb(50, 109, 116);">userPIN</span><span style=3D"color: rgba(0, 0, 0, =
0.85); font-style: normal; font-variant-caps: normal; font-stretch: normal;=
 line-height: normal; font-size-adjust: none; font-kerning: auto; font-vari=
ant-alternates: normal; font-variant-ligatures: normal; font-variant-numeri=
c: normal; font-variant-east-asian: normal; font-variant-position: normal; =
font-feature-settings: normal; font-optical-sizing: auto; font-variation-se=
ttings: normal;"> </span><font color=3D"#6c36a9">cStringUsingEncoding</font=
><span style=3D"color: rgba(0, 0, 0, 0.85); font-style: normal; font-varian=
t-caps: normal; 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;">:</span><font color=
=3D"#6c36a9">NSASCIIStringEncoding</font><span style=3D"color: rgba(0, 0, 0=
, 0.85); font-style: normal; font-variant-caps: normal; font-stretch: norma=
l; line-height: normal; font-size-adjust: none; font-kerning: auto; font-va=
riant-alternates: normal; font-variant-ligatures: normal; font-variant-nume=
ric: 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: 0px; font-style: =
normal; font-variant-caps: normal; font-stretch: normal; line-height: norma=
l; font-size-adjust: none; font-kerning: auto; font-variant-alternates: nor=
mal; font-variant-ligatures: normal; font-variant-numeric: normal; font-var=
iant-east-asian: normal; font-variant-position: normal; font-feature-settin=
gs: normal; font-optical-sizing: auto; font-variation-settings: normal; bac=
kground-color: rgb(255, 255, 255);"><span style=3D"color: rgba(0, 0, 0, 0.8=
5);">&nbsp; &nbsp;&nbsp;</span><span style=3D"color: rgb(28, 70, 74); 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;">CMS_ContentInfo</span><span style=3D"color: rgba(0, 0, 0, 0.85);">* p7=
Env =3D </span><span style=3D"font-style: normal; font-variant-caps: normal=
; font-stretch: normal; line-height: normal; font-size-adjust: none; font-k=
erning: auto; font-variant-alternates: normal; font-variant-ligatures: norm=
al; font-variant-numeric: normal; font-variant-east-asian: normal; font-var=
iant-position: normal; font-feature-settings: normal; font-optical-sizing: =
auto; font-variation-settings: normal;"><b>d2i_CMS_ContentInfo</b></span><s=
pan style=3D"color: rgba(0, 0, 0, 0.85);">(</span><span style=3D"color: rgb=
(155, 35, 147); 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; font-varia=
nt-numeric: normal; font-variant-east-asian: normal; font-variant-position:=
 normal; font-feature-settings: normal; font-optical-sizing: auto; font-var=
iation-settings: normal;"><b>NULL</b></span><span style=3D"color: rgba(0, 0=
, 0, 0.85);">, (</span><span style=3D"color: rgb(155, 35, 147); font-style:=
 normal; font-variant-caps: normal; font-stretch: normal; line-height: norm=
al; font-size-adjust: none; font-kerning: auto; font-variant-alternates: no=
rmal; font-variant-ligatures: normal; font-variant-numeric: normal; font-va=
riant-east-asian: normal; font-variant-position: normal; font-feature-setti=
ngs: normal; font-optical-sizing: auto; font-variation-settings: normal;"><=
b>unsigned</b></span> <span style=3D"color: rgb(155, 35, 147); font-style: =
normal; font-variant-caps: normal; font-stretch: normal; line-height: norma=
l; font-size-adjust: none; font-kerning: auto; font-variant-alternates: nor=
mal; font-variant-ligatures: normal; font-variant-numeric: normal; font-var=
iant-east-asian: normal; font-variant-position: normal; font-feature-settin=
gs: normal; font-optical-sizing: auto; font-variation-settings: normal;"><b=
>char</b></span> <span style=3D"color: rgb(155, 35, 147); font-style: norma=
l; font-variant-caps: normal; font-stretch: normal; line-height: normal; fo=
nt-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: n=
ormal; font-optical-sizing: auto; font-variation-settings: normal;"><b>cons=
t</b></span><span style=3D"color: rgba(0, 0, 0, 0.85);">**)&amp;pEncryptedM=
essage, encryptedMessageLength);</span></div><p style=3D"margin: 0px; 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: rgba(0, 0, 0, 0.85); background-color: rgb(255, 255, 255);"><spa=
n style=3D"font-size: 18px; font-family: Menlo;">&nbsp;&nbsp;</span></p><di=
v style=3D"margin: 0px; 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; color: rgba(0, 0, 0, 0.85); background-col=
or: rgb(255, 255, 255);"><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(57, 0, 160);">&=
nbsp; &nbsp; NSString</span>* idString =3D [keyID <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; c=
olor: rgb(50, 109, 116);">toIdString</span>];</div><div style=3D"margin: 0p=
x; font-style: normal; font-variant-caps: normal; font-stretch: normal; lin=
e-height: normal; font-size-adjust: none; font-kerning: auto; font-variant-=
alternates: normal; font-variant-ligatures: normal; font-variant-numeric: n=
ormal; font-variant-east-asian: normal; font-variant-position: normal; font=
-feature-settings: normal; font-optical-sizing: auto; font-variation-settin=
gs: normal; color: rgba(0, 0, 0, 0.85); background-color: rgb(255, 255, 255=
);">&nbsp; &nbsp; <span style=3D"font-style: normal; font-variant-caps: nor=
mal; font-stretch: normal; line-height: normal; font-size-adjust: none; fon=
t-kerning: auto; font-variant-alternates: normal; font-variant-ligatures: n=
ormal; font-variant-numeric: normal; font-variant-east-asian: normal; font-=
variant-position: normal; font-feature-settings: normal; font-optical-sizin=
g: auto; font-variation-settings: normal; color: rgb(155, 35, 147);"><b>con=
st</b></span> <span style=3D"font-style: normal; font-variant-caps: normal;=
 font-stretch: normal; line-height: normal; font-size-adjust: none; font-ke=
rning: auto; font-variant-alternates: normal; font-variant-ligatures: norma=
l; font-variant-numeric: normal; font-variant-east-asian: normal; font-vari=
ant-position: normal; font-feature-settings: normal; font-optical-sizing: a=
uto; font-variation-settings: normal; color: rgb(155, 35, 147);"><b>char</b=
></span>* szParams =3D [idString <span style=3D"font-style: normal; font-va=
riant-caps: normal; font-stretch: normal; line-height: normal; font-size-ad=
just: none; font-kerning: auto; font-variant-alternates: normal; font-varia=
nt-ligatures: normal; font-variant-numeric: normal; font-variant-east-asian=
: normal; font-variant-position: normal; font-feature-settings: normal; fon=
t-optical-sizing: auto; font-variation-settings: normal; color: rgb(108, 54=
, 169);">UTF8String</span>];</div><div style=3D"margin: 0px; font-style: no=
rmal; font-variant-caps: normal; font-stretch: normal; line-height: normal;=
 font-size-adjust: none; font-kerning: auto; font-variant-alternates: norma=
l; font-variant-ligatures: normal; font-variant-numeric: normal; font-varia=
nt-east-asian: normal; font-variant-position: normal; font-feature-settings=
: normal; font-optical-sizing: auto; font-variation-settings: normal; backg=
round-color: rgb(255, 255, 255);"><span style=3D"color: rgba(0, 0, 0, 0.85)=
; font-style: normal; font-variant-caps: normal; font-stretch: normal; line=
-height: normal; font-size-adjust: none; font-kerning: auto; font-variant-a=
lternates: normal; font-variant-ligatures: normal; font-variant-numeric: no=
rmal; font-variant-east-asian: normal; font-variant-position: normal; font-=
feature-settings: normal; font-optical-sizing: auto; font-variation-setting=
s: normal;">&nbsp; &nbsp;&nbsp;</span><span style=3D"color: rgb(28, 70, 74)=
;">EVP_PKEY</span><span style=3D"color: rgba(0, 0, 0, 0.85); font-style: no=
rmal; font-variant-caps: normal; font-stretch: normal; line-height: normal;=
 font-size-adjust: none; font-kerning: auto; font-variant-alternates: norma=
l; font-variant-ligatures: normal; font-variant-numeric: normal; font-varia=
nt-east-asian: normal; font-variant-position: normal; font-feature-settings=
: normal; font-optical-sizing: auto; font-variation-settings: normal;">* ke=
y =3D </span><b>ENGINE_load_private_key</b><span style=3D"color: rgba(0, 0,=
 0, 0.85); 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;">(engineID, szParams , </span><font color=3D"#326d74">U=
I_OpenSSL</font><span style=3D"color: rgba(0, 0, 0, 0.85); 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;">(), </=
span><span style=3D"color: rgb(155, 35, 147);"><b>NULL</b></span><span styl=
e=3D"color: rgba(0, 0, 0, 0.85); font-style: normal; font-variant-caps: nor=
mal; font-stretch: normal; line-height: normal; font-size-adjust: none; fon=
t-kerning: auto; font-variant-alternates: normal; font-variant-ligatures: n=
ormal; font-variant-numeric: normal; font-variant-east-asian: normal; font-=
variant-position: normal; font-feature-settings: normal; font-optical-sizin=
g: auto; font-variation-settings: normal;">);</span></div><p>&nbsp; &nbsp;&=
nbsp;<span style=3D"background-color: rgb(255, 255, 255); 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; color: rgb(155, 35, 147);">int</span><span style=3D"col=
or: rgba(0, 0, 0, 0.85); background-color: rgb(255, 255, 255);"> error =3D =
</span><span style=3D"background-color: rgb(255, 255, 255); font-stretch: n=
ormal; line-height: normal; font-size-adjust: none; font-kerning: auto; fon=
t-variant-alternates: normal; font-variant-ligatures: normal; font-variant-=
numeric: normal; font-variant-east-asian: normal; font-variant-position: no=
rmal; font-feature-settings: normal; font-optical-sizing: auto; font-variat=
ion-settings: normal;"><b>CMS_decrypt</b></span><span style=3D"color: rgba(=
0, 0, 0, 0.85); background-color: rgb(255, 255, 255);">(p7Env, key , user, =
</span><span style=3D"background-color: rgb(255, 255, 255); font-stretch: n=
ormal; line-height: normal; font-size-adjust: none; font-kerning: auto; fon=
t-variant-alternates: normal; font-variant-ligatures: normal; font-variant-=
numeric: normal; font-variant-east-asian: normal; font-variant-position: no=
rmal; font-feature-settings: normal; font-optical-sizing: auto; font-variat=
ion-settings: normal; color: rgb(155, 35, 147);">NULL</span><span style=3D"=
color: rgba(0, 0, 0, 0.85); background-color: rgb(255, 255, 255);">, outDat=
a, </span><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-ligatures: normal; font-varia=
nt-numeric: normal; font-variant-east-asian: normal; font-variant-position:=
 normal; font-feature-settings: normal; font-optical-sizing: auto; font-var=
iation-settings: normal; color: rgb(28, 0, 207);">0</span><span style=3D"co=
lor: rgba(0, 0, 0, 0.85); background-color: rgb(255, 255, 255);">);</span><=
/p><p><b>Additional Notes:</b></p><p>* Looks like with serialization/deseri=
alization, it is loosing our engine context.&nbsp;</p><p>* Tried i<span sty=
le=3D"background-color: rgb(255, 255, 255);">2d_CMS_bio &amp;&nbsp;</span><=
span style=3D"background-color: rgb(255, 255, 255);">d2i_CMS_bio but no suc=
cess.</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 p=
lans to move from engine to provider implementation but it may take some ti=
me.</span></p><p><span style=3D"background-color: rgb(255, 255, 255);">Look=
ing for your suggestions to fix this issue.</span></p><p><span style=3D"bac=
kground-color: rgb(255, 255, 255);">Regards,</span></p><p><span style=3D"ba=
ckground-color: rgb(255, 255, 255);">Madhu</span></p><p><span style=3D"back=
ground-color: rgb(255, 255, 255);"><br></span></p><p><span style=3D"backgro=
und-color: rgb(255, 255, 255);"><br></span></p></span></div></div></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/4440D5EA-2F67-4CB8-8913-CC3C4374C85F%40rimus-te=
ch.com?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.com/a/=
openssl.org/d/msgid/openssl-users/4440D5EA-2F67-4CB8-8913-CC3C4374C85F%40ri=
mus-tech.com</a>.<br />

--Apple-Mail=_1A6FC02C-E244-4D21-AF7C-DDBEDA8D03A3--