Skip to content

Commit 4fd2aa9

Browse files
authored
Fix ABI problem when marshalling X509VerifyStatusCode on s390x
The X509VerifyStatusCode type is defined as an enum in C code, but as a struct (with a single member) in C# code. This means that marshalling this type only works correctly on platforms where the ABI treats these two types as equivalent. This fails e.g. on Linux on s390x. Fixed by changing all native functions that take X509VerifyStatusCode as argument or return type to use a plain "int" instead.
1 parent dbc9142 commit 4fd2aa9

File tree

4 files changed

+32
-27
lines changed

4 files changed

+32
-27
lines changed

src/libraries/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.OCSP.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,14 @@ ref MemoryMarshal.GetReference(buf),
3535
internal static extern void OcspResponseDestroy(IntPtr ocspReq);
3636

3737
[DllImport(Libraries.CryptoNative)]
38-
private static extern X509VerifyStatusCode CryptoNative_X509ChainGetCachedOcspStatus(
38+
private static extern int CryptoNative_X509ChainGetCachedOcspStatus(
3939
SafeX509StoreCtxHandle ctx,
4040
string cachePath,
4141
int chainDepth);
4242

4343
internal static X509VerifyStatusCode X509ChainGetCachedOcspStatus(SafeX509StoreCtxHandle ctx, string cachePath, int chainDepth)
4444
{
45-
X509VerifyStatusCode response = CryptoNative_X509ChainGetCachedOcspStatus(ctx, cachePath, chainDepth);
45+
X509VerifyStatusCode response = (X509VerifyStatusCode)CryptoNative_X509ChainGetCachedOcspStatus(ctx, cachePath, chainDepth);
4646

4747
if (response.Code < 0)
4848
{
@@ -54,7 +54,7 @@ internal static X509VerifyStatusCode X509ChainGetCachedOcspStatus(SafeX509StoreC
5454
}
5555

5656
[DllImport(Libraries.CryptoNative)]
57-
private static extern X509VerifyStatusCode CryptoNative_X509ChainVerifyOcsp(
57+
private static extern int CryptoNative_X509ChainVerifyOcsp(
5858
SafeX509StoreCtxHandle ctx,
5959
SafeOcspRequestHandle req,
6060
SafeOcspResponseHandle resp,
@@ -68,7 +68,7 @@ internal static X509VerifyStatusCode X509ChainVerifyOcsp(
6868
string cachePath,
6969
int chainDepth)
7070
{
71-
X509VerifyStatusCode response = CryptoNative_X509ChainVerifyOcsp(ctx, req, resp, cachePath, chainDepth);
71+
X509VerifyStatusCode response = (X509VerifyStatusCode)CryptoNative_X509ChainVerifyOcsp(ctx, req, resp, cachePath, chainDepth);
7272

7373
if (response.Code < 0)
7474
{

src/libraries/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.X509.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,13 @@ internal static bool X509VerifyCert(SafeX509StoreCtxHandle ctx)
195195
return result != 0;
196196
}
197197

198-
[DllImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_X509StoreCtxGetError")]
199-
internal static extern X509VerifyStatusCode X509StoreCtxGetError(SafeX509StoreCtxHandle ctx);
198+
[DllImport(Libraries.CryptoNative)]
199+
internal static extern int CryptoNative_X509StoreCtxGetError(SafeX509StoreCtxHandle ctx);
200+
201+
internal static X509VerifyStatusCode X509StoreCtxGetError(SafeX509StoreCtxHandle ctx)
202+
{
203+
return (X509VerifyStatusCode)CryptoNative_X509StoreCtxGetError(ctx);
204+
}
200205

201206
[DllImport(Libraries.CryptoNative)]
202207
private static extern int CryptoNative_X509StoreCtxReset(SafeX509StoreCtxHandle ctx);

src/libraries/Native/Unix/System.Security.Cryptography.Native/pal_x509.c

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -302,9 +302,9 @@ X509* CryptoNative_X509StoreCtxGetTargetCert(X509_STORE_CTX* ctx)
302302
return NULL;
303303
}
304304

305-
X509VerifyStatusCode CryptoNative_X509StoreCtxGetError(X509_STORE_CTX* ctx)
305+
int32_t CryptoNative_X509StoreCtxGetError(X509_STORE_CTX* ctx)
306306
{
307-
return (unsigned int)X509_STORE_CTX_get_error(ctx);
307+
return (int32_t)X509_STORE_CTX_get_error(ctx);
308308
}
309309

310310
int32_t CryptoNative_X509StoreCtxReset(X509_STORE_CTX* ctx)
@@ -337,7 +337,7 @@ int32_t CryptoNative_X509StoreCtxGetErrorDepth(X509_STORE_CTX* ctx)
337337
return X509_STORE_CTX_get_error_depth(ctx);
338338
}
339339

340-
const char* CryptoNative_X509VerifyCertErrorString(X509VerifyStatusCode n)
340+
const char* CryptoNative_X509VerifyCertErrorString(int32_t n)
341341
{
342342
return X509_verify_cert_error_string((long)n);
343343
}
@@ -949,27 +949,27 @@ static time_t GetIssuanceWindowStart()
949949
return t;
950950
}
951951

952-
X509VerifyStatusCode CryptoNative_X509ChainGetCachedOcspStatus(X509_STORE_CTX* storeCtx, char* cachePath, int chainDepth)
952+
int32_t CryptoNative_X509ChainGetCachedOcspStatus(X509_STORE_CTX* storeCtx, char* cachePath, int chainDepth)
953953
{
954954
if (storeCtx == NULL || cachePath == NULL)
955955
{
956-
return (X509VerifyStatusCode)-1;
956+
return -1;
957957
}
958958

959959
X509* subject;
960960
X509* issuer;
961961

962962
if (!Get0CertAndIssuer(storeCtx, chainDepth, &subject, &issuer))
963963
{
964-
return (X509VerifyStatusCode)-2;
964+
return -2;
965965
}
966966

967967
X509VerifyStatusCode ret = PAL_X509_V_ERR_UNABLE_TO_GET_CRL;
968968
char* fullPath = BuildOcspCacheFilename(cachePath, subject);
969969

970970
if (fullPath == NULL)
971971
{
972-
return ret;
972+
return (int32_t)ret;
973973
}
974974

975975
BIO* bio = BIO_new_file(fullPath, "rb");
@@ -1031,7 +1031,7 @@ X509VerifyStatusCode CryptoNative_X509ChainGetCachedOcspStatus(X509_STORE_CTX* s
10311031
OCSP_RESPONSE_free(resp);
10321032
}
10331033

1034-
return ret;
1034+
return (int32_t)ret;
10351035
}
10361036

10371037
OCSP_REQUEST* CryptoNative_X509ChainBuildOcspRequest(X509_STORE_CTX* storeCtx, int chainDepth)
@@ -1079,28 +1079,28 @@ OCSP_REQUEST* CryptoNative_X509ChainBuildOcspRequest(X509_STORE_CTX* storeCtx, i
10791079
return req;
10801080
}
10811081

1082-
X509VerifyStatusCode
1082+
int32_t
10831083
CryptoNative_X509ChainVerifyOcsp(X509_STORE_CTX* storeCtx, OCSP_REQUEST* req, OCSP_RESPONSE* resp, char* cachePath, int chainDepth)
10841084
{
10851085
if (storeCtx == NULL || req == NULL || resp == NULL)
10861086
{
1087-
return (X509VerifyStatusCode)-1;
1087+
return -1;
10881088
}
10891089

10901090
X509* subject;
10911091
X509* issuer;
10921092

10931093
if (!Get0CertAndIssuer(storeCtx, chainDepth, &subject, &issuer))
10941094
{
1095-
return (X509VerifyStatusCode)-2;
1095+
return -2;
10961096
}
10971097

10981098
X509VerifyStatusCode ret = PAL_X509_V_ERR_UNABLE_TO_GET_CRL;
10991099
OCSP_CERTID* certId = MakeCertId(subject, issuer);
11001100

11011101
if (certId == NULL)
11021102
{
1103-
return (X509VerifyStatusCode)-3;
1103+
return -3;
11041104
}
11051105

11061106
ASN1_GENERALIZEDTIME* thisUpdate = NULL;
@@ -1167,5 +1167,5 @@ CryptoNative_X509ChainVerifyOcsp(X509_STORE_CTX* storeCtx, OCSP_REQUEST* req, OC
11671167
ASN1_GENERALIZEDTIME_free(thisUpdate);
11681168
}
11691169

1170-
return ret;
1170+
return (int32_t)ret;
11711171
}

src/libraries/Native/Unix/System.Security.Cryptography.Native/pal_x509.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ PALEXPORT X509* CryptoNative_X509StoreCtxGetTargetCert(X509_STORE_CTX* ctx);
273273
/*
274274
Shims the X509_STORE_CTX_get_error method.
275275
*/
276-
PALEXPORT X509VerifyStatusCode CryptoNative_X509StoreCtxGetError(X509_STORE_CTX* ctx);
276+
PALEXPORT int32_t CryptoNative_X509StoreCtxGetError(X509_STORE_CTX* ctx);
277277

278278
/*
279279
Resets ctx to before the chain was built, preserving the target cert, trust store, extra cert context,
@@ -301,7 +301,7 @@ PALEXPORT void CryptoNative_X509StoreCtxSetVerifyCallback(X509_STORE_CTX* ctx, X
301301
/*
302302
Shims the X509_verify_cert_error_string method.
303303
*/
304-
PALEXPORT const char* CryptoNative_X509VerifyCertErrorString(X509VerifyStatusCode n);
304+
PALEXPORT const char* CryptoNative_X509VerifyCertErrorString(int32_t n);
305305

306306
/*
307307
Shims the X509_CRL_free method.
@@ -378,7 +378,7 @@ PALEXPORT int32_t CryptoNative_X509StoreCtxResetForSignatureError(X509_STORE_CTX
378378
Look for a cached OCSP response appropriate to the end-entity certificate using the issuer as
379379
determined by the chain in storeCtx.
380380
*/
381-
PALEXPORT X509VerifyStatusCode CryptoNative_X509ChainGetCachedOcspStatus(X509_STORE_CTX* storeCtx, char* cachePath, int chainDepth);
381+
PALEXPORT int32_t CryptoNative_X509ChainGetCachedOcspStatus(X509_STORE_CTX* storeCtx, char* cachePath, int chainDepth);
382382

383383
/*
384384
Build an OCSP request appropriate for the end-entity certificate using the issuer (and trust) as
@@ -390,8 +390,8 @@ PALEXPORT OCSP_REQUEST* CryptoNative_X509ChainBuildOcspRequest(X509_STORE_CTX* s
390390
Determine if the OCSP response is acceptable, and if acceptable report the status and
391391
cache the result (if appropriate)
392392
*/
393-
PALEXPORT X509VerifyStatusCode CryptoNative_X509ChainVerifyOcsp(X509_STORE_CTX* storeCtx,
394-
OCSP_REQUEST* req,
395-
OCSP_RESPONSE* resp,
396-
char* cachePath,
397-
int chainDepth);
393+
PALEXPORT int32_t CryptoNative_X509ChainVerifyOcsp(X509_STORE_CTX* storeCtx,
394+
OCSP_REQUEST* req,
395+
OCSP_RESPONSE* resp,
396+
char* cachePath,
397+
int chainDepth);

0 commit comments

Comments
 (0)