Skip to content

Commit e6cd9d0

Browse files
authored
Attempted to read or write protected memory. (#13)
* Refactor interop marshaling and buffer handling in certificate validation
1 parent a550bd1 commit e6cd9d0

2 files changed

Lines changed: 18 additions & 22 deletions

File tree

NKalkan/InteropDefinitions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ namespace NKalkan;
2929
internal delegate KalkanError KC_SignData(string? alias, int flags, byte[] inData, int inDataLength, string? inSign, int inSignLength, StringBuilder? outSign, ref int outSignoutSignLength);
3030

3131
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
32-
internal delegate KalkanError KC_VerifyData(string? alias, int flags, byte[] inData, int inDataLength, string inoutSign, int inoutSignLength,
32+
internal delegate KalkanError KC_VerifyData(string? alias, int flags, byte[] inData, int inDataLength, string inoutSign, int inoutSignLength,
3333
StringBuilder? outData, ref int outDataLength, StringBuilder? outVerifyInfo, ref int outVerifyInfoLength, int inCertID, StringBuilder? outCert, ref int outCertLength);
3434

3535
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
@@ -42,7 +42,7 @@ internal delegate KalkanError KC_VerifyData(string? alias, int flags, byte[] inD
4242
internal delegate KalkanError KC_X509CertificateGetInfo(string certificateData, int certificateDataLength, int propertyId, [MarshalAs(UnmanagedType.LPUTF8Str)] StringBuilder certificatePropertyData, ref int certificatePropertyDataLength);
4343

4444
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
45-
internal delegate KalkanError KC_X509ValidateCertificate(string certificateData, int certificateDataLength, int validType, [MarshalAs(UnmanagedType.LPUTF8Str)] string validPath, long checkTime, [MarshalAs(UnmanagedType.LPUTF8Str)] StringBuilder? outputInformation, ref int outputInformationLength, int flag, [MarshalAs(UnmanagedType.LPUTF8Str)] StringBuilder? ocsPResponse, ref int ocsPResponseLength);
45+
internal delegate KalkanError KC_X509ValidateCertificate([MarshalAs(UnmanagedType.LPUTF8Str)] string certificateData, int certificateDataLength, int validType, [MarshalAs(UnmanagedType.LPUTF8Str)] string validPath, long checkTime, [Out, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 6)] byte[]? outputInformation, ref int outputInformationLength, int flag, [Out, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 9)] byte[]? ocsPResponse, ref int ocsPResponseLength);
4646

4747
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
4848
internal delegate KalkanError KC_SignWSSE(string? alias, int flags, byte[] inData, int inDataLength, byte[] outSign, ref int outSignoutSignLength, string? signNodeId);

NKalkan/KalkanApi.cs

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -168,28 +168,24 @@ public void ValidateCertificate(string certificate, KalkanValidationType validat
168168
throw new ArgumentNullException(nameof(certificate));
169169
}
170170

171-
int certificateLength = certificate.Length;
172-
int outputInformationLength = 0;
173-
int ospResponseLength = 0;
174-
175-
int flag = (checkCertificateTime ? 0 : KalkanConstants.KC_NOCHECKCERTTIME) + (getOscpResponse ? KalkanConstants.KC_GET_OCSP_RESPONSE : 0);
171+
int certificateLength = Encoding.UTF8.GetByteCount(certificate);
172+
int outputInformationLength = 64 * 1024;
173+
int ospResponseLength = getOscpResponse ? 128 * 1024 : 0;
176174

177-
var errorCode = StKCFunctionsType.X509ValidateCertificate(certificate, certificateLength, (int)validationType, validPath, checkTime: 0, null, ref outputInformationLength, flag, null, ref ospResponseLength);
178-
if (errorCode != KalkanError.BUFFER_TOO_SMALL)
179-
{
180-
ThrowIfError(errorCode);
181-
}
175+
int flag = (checkCertificateTime ? 0 : KalkanConstants.KC_NOCHECKCERTTIME) + (getOscpResponse ? KalkanConstants.KC_GET_OCSP_RESPONSE : 0);
182176

183-
// Fix: BUFFER_TOO_SMALL
184-
ospResponseLength = ospResponseLength + 200;
185-
outputInformationLength = outputInformationLength + 200;
177+
byte[] outputInformationBuf = new byte[outputInformationLength];
178+
byte[]? ospResponseBuf = getOscpResponse ? new byte[ospResponseLength] : null;
186179

187-
StringBuilder ospResponseBuilder = new StringBuilder(ospResponseLength);
188-
StringBuilder outputInformationBuilder = new StringBuilder(outputInformationLength);
189-
errorCode = StKCFunctionsType.X509ValidateCertificate(certificate, certificateLength, (int)validationType, validPath, checkTime: 0, outputInformationBuilder, ref outputInformationLength, flag, ospResponseBuilder, ref ospResponseLength);
180+
var errorCode = StKCFunctionsType.X509ValidateCertificate(certificate, certificateLength, (int)validationType, validPath, checkTime: 0, outputInformation: outputInformationBuf, ref outputInformationLength, flag, ocsPResponse: ospResponseBuf, ref ospResponseLength);
190181
ThrowIfError(errorCode);
191-
outputInformation = outputInformationBuilder.ToString();
192-
ospResponse = ospResponseBuilder.ToString();
182+
183+
outputInformation = Encoding.UTF8.GetString(outputInformationBuf, 0, outputInformationLength);
184+
185+
if (getOscpResponse && ospResponseBuf != null && ospResponseLength > 0)
186+
ospResponse = Convert.ToBase64String(ospResponseBuf, 0, ospResponseLength);
187+
else
188+
ospResponse = string.Empty;
193189
}
194190

195191
public string SignXml(string content, KalkanSignFlags flags = 0, string? certificateAlias = null, string? signNodeId = null, string? parentSignNode = null, string parentNameSpace = "")
@@ -319,7 +315,7 @@ public string SignWsse(string content, string? signNodeId, KalkanSignFlags flags
319315
ThrowIfError(errorCode);
320316
return Encoding.UTF8.GetString(signedPayload, 0, signedPayloadLength).TrimEnd('\0');
321317
}
322-
318+
323319
/// <summary>
324320
/// Sing envelope with custom xml attributes
325321
/// </summary>
@@ -530,7 +526,7 @@ private static void ThrowIfError(KalkanError errorCode)
530526

531527
throw new InvalidOperationException(err.ToString());
532528
}
533-
529+
534530
private static KalkanSignFlags SignFlags(KalkanSignType signType, KalkanInputFormat inputFormat,
535531
KalkanOutputFormat outputFormat)
536532
{

0 commit comments

Comments
 (0)