Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Method C_Login returned CKR_SESSION_HANDLE_INVALID #221

Open
ozlemkzn opened this issue Mar 30, 2023 · 0 comments
Open

Method C_Login returned CKR_SESSION_HANDLE_INVALID #221

ozlemkzn opened this issue Mar 30, 2023 · 0 comments

Comments

@ozlemkzn
Copy link

ozlemkzn commented Mar 30, 2023

Hello,

I am experiencing an issue as follows:

Method C_Login returned CKR_SESSION_HANDLE_INVALID

at Net.Pkcs11Interop.HighLevelAPI80.Session.Login(CKU userType, String pin) at PKCS11Service.Initialize() in PKCS11Service.cs:line 81 at PKCS11Service..ctor(IOptions"1 settings, ILogger"1 logger) in PKCS11\PKCS11Service.cs:line 55 at System.RuntimeMethodHandle.InvokeMethod(Object target, Span"1& arguments, Signature sig, Boolean constructor, Boolean wrapExceptions) at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, RuntimeResolverContext context) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor"2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitRootCache(ServiceCallSite callSite, RuntimeResolverContext context) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor"2.VisitCallSite(ServiceCallSite callSite, TArgument argument) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, RuntimeResolverContext context) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor"2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitRootCache(ServiceCallSite callSite, RuntimeResolverContext context) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor"2.VisitCallSite(ServiceCallSite callSite, TArgument argument) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.Resolve(ServiceCallSite callSite, ServiceProviderEngineScope scope) at Microsoft.Extensions.DependencyInjection.ServiceProvider.CreateServiceAccessor(Type serviceType) at System.Collections.Concurrent.ConcurrentDictionary"2.GetOrAdd(TKey key, Func"2 valueFactory) at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type serviceType, ServiceProviderEngineScope serviceProviderEngineScope) at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.GetService(Type serviceType) at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType) at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider) at ConfigureServices.AppServices.<>c__DisplayClass0_0.<AddApplicationServices>b__40() in AppServices.cs:line 40 at System.Lazy"1.ViaFactory(LazyThreadSafetyMode mode) --- End of stack trace from previous location --- at System.Lazy"1.CreateValue()

Here it is my initialize function:

private void Initialize()
{
if (slot == null)
{
try
{
var slotList = pkcs11Library.GetSlotList(SlotsType.WithTokenPresent);
if (!slotList.Any())
throw new Exception("Could not find any slot");

            slot = slotList[0];
            slot.CloseAllSessions();
            var session = slot.OpenSession(SessionType.ReadOnly);
            if (session == null)
            {
                throw new Exception("Could not create new session on the given slot");
            }

            session.Login(CKU.CKU_USER, settings.Value.Password);

            session.FindObjectsInit(privateKeyAttributeList);
            var privateKeyObjectsFound = session.FindObjects(1);
            session.FindObjectsFinal();

            if (!privateKeyObjectsFound.Any() && privateKeyObjectsFound[0].ObjectId == CK.CK_INVALID_HANDLE)
            {
                throw new Exception("no private key with the lable was found");
            }

            keyHandle = privateKeyObjectsFound[0];

            session.FindObjectsInit(certificateAttributeList);
            var certificateObjectsFound = session.FindObjects(1);
            session.FindObjectsFinal();

            if (!certificateObjectsFound.Any() && certificateObjectsFound[0].ObjectId == CK.CK_INVALID_HANDLE)
            {
                throw new Exception("no certificate with the lable was found");
            }

            var foundAttributeValues = session.GetAttributeValue(certificateObjectsFound[0], new List<CKA>() { CKA.CKA_VALUE });
            certificate = new X509Certificate2(foundAttributeValues[0].GetValueAsByteArray());
        }
        catch (Exception exception)
        {
            slot = null;
            throw;
        }
    }
}

Could you please assist me in identifying the issue and suggest a solution? Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant