CryptoPro / libcore

44 stars 0 forks source link

SignedCms.CheckSignature - 'LibCore.Common.Reflection.Reflector' does not contain a definition for 'GetContent'" #12

Closed k0st1x closed 1 year ago

k0st1x commented 1 year ago

Здравствуйте, хочу использовать механизм проверки подписи. Взял код из https://github.com/CryptoPro/libcore#readme

// Декодируем подпись
signedCmsVerify.Decode(signature);

// Проверяем подпись
signedCmsVerify.CheckSignature(true);

(под windows) получаю падение:

Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 'LibCore.Common.Reflection.Reflector' does not contain a definition for 'GetDigestAlgorithm'
   at Microsoft.CSharp.RuntimeBinder.RuntimeBinder.BindCall(ICSharpInvokeOrInvokeMemberBinder payload, Expr callingObject, ArgumentObject[] arguments, LocalVariableSymbol[] locals)

    Microsoft.CSharp.dll!Microsoft.CSharp.RuntimeBinder.RuntimeBinder.BindCall(Microsoft.CSharp.RuntimeBinder.ICSharpInvokeOrInvokeMemberBinder payload, Microsoft.CSharp.RuntimeBinder.Semantics.Expr callingObject, Microsoft.CSharp.RuntimeBinder.ArgumentObject[] arguments, Microsoft.CSharp.RuntimeBinder.Semantics.LocalVariableSymbol[] locals) Unknown
    Microsoft.CSharp.dll!Microsoft.CSharp.RuntimeBinder.CSharpInvokeMemberBinder.DispatchPayload(Microsoft.CSharp.RuntimeBinder.RuntimeBinder runtimeBinder, Microsoft.CSharp.RuntimeBinder.ArgumentObject[] arguments, Microsoft.CSharp.RuntimeBinder.Semantics.LocalVariableSymbol[] locals)  Unknown
    Microsoft.CSharp.dll!Microsoft.CSharp.RuntimeBinder.RuntimeBinder.BindCore(Microsoft.CSharp.RuntimeBinder.ICSharpBinder payload, System.Linq.Expressions.Expression[] parameters = {System.Linq.Expressions.Expression[1]}, System.Dynamic.DynamicMetaObject[] args, out System.Dynamic.DynamicMetaObject deferredBinding)  Unknown
    Microsoft.CSharp.dll!Microsoft.CSharp.RuntimeBinder.RuntimeBinder.Bind(Microsoft.CSharp.RuntimeBinder.ICSharpBinder payload, System.Linq.Expressions.Expression[] parameters, System.Dynamic.DynamicMetaObject[] args, out System.Dynamic.DynamicMetaObject deferredBinding)    Unknown
    Microsoft.CSharp.dll!Microsoft.CSharp.RuntimeBinder.BinderHelper.Bind(Microsoft.CSharp.RuntimeBinder.ICSharpBinder action = {Microsoft.CSharp.RuntimeBinder.CSharpInvokeMemberBinder}, Microsoft.CSharp.RuntimeBinder.RuntimeBinder binder, System.Dynamic.DynamicMetaObject[] args = {System.Dynamic.DynamicMetaObject[1]}, System.Collections.Generic.IEnumerable<Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfo> arginfos, System.Dynamic.DynamicMetaObject onBindingError = null)    Unknown
    Microsoft.CSharp.dll!Microsoft.CSharp.RuntimeBinder.CSharpInvokeMemberBinder.FallbackInvokeMember(System.Dynamic.DynamicMetaObject target, System.Dynamic.DynamicMetaObject[] args, System.Dynamic.DynamicMetaObject errorSuggestion)   Unknown
    System.Linq.Expressions.dll!System.Dynamic.DynamicObject.MetaDynamic.BindInvokeMember(System.Dynamic.InvokeMemberBinder binder, System.Dynamic.DynamicMetaObject[] args)    Unknown
    System.Linq.Expressions.dll!System.Dynamic.InvokeMemberBinder.Bind(System.Dynamic.DynamicMetaObject target, System.Dynamic.DynamicMetaObject[] args)    Unknown
    System.Linq.Expressions.dll!System.Dynamic.DynamicMetaObjectBinder.Bind(object[] args = {object[1]}, System.Collections.ObjectModel.ReadOnlyCollection<System.Linq.Expressions.ParameterExpression> parameters, System.Linq.Expressions.LabelTarget returnLabel = {System.Linq.Expressions.LabelTarget})    Unknown
    System.Linq.Expressions.dll!System.Runtime.CompilerServices.CallSiteBinder.BindCore<System.Func<System.Runtime.CompilerServices.CallSite, object, object>>(System.Runtime.CompilerServices.CallSite<System.Func<System.Runtime.CompilerServices.CallSite, object, object>> site, object[] args) Unknown
    System.Linq.Expressions.dll!System.Dynamic.UpdateDelegates.UpdateAndExecute1<object, object>(System.Runtime.CompilerServices.CallSite site = {System.Runtime.CompilerServices.CallSite<System.Func<System.Runtime.CompilerServices.CallSite, object, object>>}, object arg0 = {LibCore.Common.Reflection.Reflector})    Unknown
    LibCore.Security.Cryptography.Pkcs.dll!LibCore.Security.Cryptography.Pkcs.Detours.SignerInfoDetour.PrepareDigest(bool compatMode = false, System.Security.Cryptography.Pkcs.SignerInfo instance = {System.Security.Cryptography.Pkcs.SignerInfo})   Unknown
    LibCore.Security.Cryptography.Pkcs.dll!LibCore.Security.Cryptography.Pkcs.Detours.SignerInfoDetour.VerifySignature(LibCore.Security.Cryptography.GostCmsSignature signatureProcessor = {LibCore.Security.Cryptography.GostCmsSignature.Gost2012_256CmsSignature}, System.Security.Cryptography.X509Certificates.X509Certificate2 certificate = {System.Security.Cryptography.X509Certificates.X509Certificate2}, bool compatMode = false, System.Security.Cryptography.Pkcs.SignerInfo instance = {System.Security.Cryptography.Pkcs.SignerInfo})   Unknown
    LibCore.Security.Cryptography.Pkcs.dll!LibCore.Security.Cryptography.Pkcs.Detours.SignerInfoDetour.Prefix(System.Security.Cryptography.X509Certificates.X509Certificate2Collection extraStore = {System.Security.Cryptography.X509Certificates.X509Certificate2Collection}, System.Security.Cryptography.X509Certificates.X509Certificate2 certificate = {System.Security.Cryptography.X509Certificates.X509Certificate2}, bool verifySignatureOnly = true, System.Security.Cryptography.Pkcs.SignerInfo __instance = {System.Security.Cryptography.Pkcs.SignerInfo})   Unknown
    [Lightweight Function]  
    System.Security.Cryptography.Pkcs.dll!System.Security.Cryptography.Pkcs.SignerInfo.CheckSignature(System.Security.Cryptography.X509Certificates.X509Certificate2Collection extraStore, bool verifySignatureOnly)    Unknown
    System.Security.Cryptography.Pkcs.dll!System.Security.Cryptography.Pkcs.SignedCms.CheckSignatures(System.Security.Cryptography.Pkcs.SignerInfoCollection signers, System.Security.Cryptography.X509Certificates.X509Certificate2Collection extraStore = {System.Security.Cryptography.X509Certificates.X509Certificate2Collection}, bool verifySignatureOnly = true)    Unknown
    System.Security.Cryptography.Pkcs.dll!System.Security.Cryptography.Pkcs.SignedCms.CheckSignature(System.Security.Cryptography.X509Certificates.X509Certificate2Collection extraStore, bool verifySignatureOnly) Unknown
    System.Security.Cryptography.Pkcs.dll!System.Security.Cryptography.Pkcs.SignedCms.CheckSignature(bool verifySignatureOnly)  Unknown

Помогите разорбраться с работоспособностью примера.

upd: Под отладчиком видно, что объект "instance" является типом "SignerInfo", которого нет публичного метода "GetDigestAlgorithm()".

Fasjeit commented 1 year ago

Добрый день.

Для начала, в рамках паранойи, предлагаю убедиться, что это не внутренне исключение, которое перехватывает студия, ибо выставлена галка перехватывать все исключения.

Если поведение наблюдаете под отладчиком - и игнорировать данное исключение (нажимая f5 при его возникновении, или поставив галку на его дальнейший игнор в студии) или же запускать без отладчика - код завершается успешно для вызывающей функции?

k0st1x commented 1 year ago

@Fasjeit спасибо за уточнение. Я дополню. Действительно, то что я написал в изначальном тексте было про "GetDigestAlgorithm()" если я сделаю код

try {
  signedCms.CheckSignature(true);
} catch(Exception e) { } // !!!

то приложение попадет в catch вот с таким исключением

Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 'LibCore.Common.Reflection.Reflector' does not contain a definition for 'GetContent'
   at CallSite.Target(Closure , CallSite , Object , ReadOnlyMemory`1 , Object )
   at System.Dynamic.UpdateDelegates.UpdateAndExecute3[T0,T1,T2,TRet](CallSite site, T0 arg0, T1 arg1, T2 arg2)
   at LibCore.Security.Cryptography.Pkcs.Detours.SignerInfoDetour.PrepareDigest(Boolean compatMode, SignerInfo instance)
   at LibCore.Security.Cryptography.Pkcs.Detours.SignerInfoDetour.VerifySignature(GostCmsSignature signatureProcessor, X509Certificate2 certificate, Boolean compatMode, SignerInfo instance)
   at LibCore.Security.Cryptography.Pkcs.Detours.SignerInfoDetour.Prefix(X509Certificate2Collection extraStore, X509Certificate2 certificate, Boolean verifySignatureOnly, SignerInfo __instance)
   at System.Security.Cryptography.Pkcs.SignerInfo.Verify_Patch1(SignerInfo this, X509Certificate2Collection extraStore, X509Certificate2 certificate, Boolean verifySignatureOnly)
   at System.Security.Cryptography.Pkcs.SignerInfo.CheckSignature(X509Certificate2Collection extraStore, Boolean verifySignatureOnly)
   at System.Security.Cryptography.Pkcs.SignedCms.CheckSignatures(SignerInfoCollection signers, X509Certificate2Collection extraStore, Boolean verifySignatureOnly)
   at System.Security.Cryptography.Pkcs.SignedCms.CheckSignature(X509Certificate2Collection extraStore, Boolean verifySignatureOnly)
   at System.Security.Cryptography.Pkcs.SignedCms.CheckSignature(Boolean verifySignatureOnly)

Видно, что в итоге падение ругается на отсутствие метода "GetContent()".

Fasjeit commented 1 year ago

Попробуйте исправленную сборку по ссылке.

https://file.cryptopro.ru/f/p4QDmnme9JlN7gmCZxVZvJMmW2qlkljs/LibCore.Windows.2023.4.8.1.nupkg

k0st1x commented 1 year ago

@Fasjeit спасибо за быстрый ответ. С новым пакетом ошибка в блоке catch отлавливается, но выглядит уже вот так:

Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: Cannot implicitly convert type 'LibCore.Common.Reflection.Reflector' to 'System.ReadOnlyMemory<byte>'
   at CallSite.Target(Closure , CallSite , Object )
   at System.Dynamic.UpdateDelegates.UpdateAndExecute1[T0,TRet](CallSite site, T0 arg0)
   at LibCore.Security.Cryptography.Pkcs.Detours.SignerInfoDetour.PrepareDigest(Boolean compatMode, SignerInfo instance)
   at LibCore.Security.Cryptography.Pkcs.Detours.SignerInfoDetour.VerifySignature(GostCmsSignature signatureProcessor, X509Certificate2 certificate, Boolean compatMode, SignerInfo instance)
   at LibCore.Security.Cryptography.Pkcs.Detours.SignerInfoDetour.Prefix(X509Certificate2Collection extraStore, X509Certificate2 certificate, Boolean verifySignatureOnly, SignerInfo __instance)
   at System.Security.Cryptography.Pkcs.SignerInfo.Verify_Patch1(SignerInfo this, X509Certificate2Collection extraStore, X509Certificate2 certificate, Boolean verifySignatureOnly)
   at System.Security.Cryptography.Pkcs.SignerInfo.CheckSignature(X509Certificate2Collection extraStore, Boolean verifySignatureOnly)
   at System.Security.Cryptography.Pkcs.SignedCms.CheckSignatures(SignerInfoCollection signers, X509Certificate2Collection extraStore, Boolean verifySignatureOnly)
   at System.Security.Cryptography.Pkcs.SignedCms.CheckSignature(X509Certificate2Collection extraStore, Boolean verifySignatureOnly)
   at System.Security.Cryptography.Pkcs.SignedCms.CheckSignature(Boolean verifySignatureOnly)
Fasjeit commented 1 year ago

... и ещё одна сборка на пробу https://file.cryptopro.ru/f/QiKGNkvdD6V1sRmGm4I46ZzET6WwFxDO/LibCore.Windows.2023.4.10.1.nupkg

k0st1x commented 1 year ago

@Fasjeit Спасибо, теперь работает. Как можно получить билд для Linux?

Fasjeit commented 1 year ago

Linux https://file.cryptopro.ru/f/HrZXO5NR2aJonTDnu6qxAOiBAcrj3YdE/LibCore.Linux.2023.4.10.1.nupkg