Java vs NCryptoki

5/3/2017 8:38:25 PM
Gravatar
Total Posts 1

Java vs NCryptoki

Hi There,

I'm using NCryptoki in a console app, and I'm seeing some confusing results. I'm trying to integrate with a SafeNet HSM. After I initialize the cryptoki object, I look for the slots. One thing I find strange is that I have no ActiveSlots. The other thing I find strange is that all the Slots have no token present. Because I have no token present, I can't open a session. I'm trying to connect to a hapg in the second slot. 

When I try to connect using the LunaProvider.jar  file, it is telling me that I the token is present for each slot. Any ideas on what might be the difference between the two?

C#:

static void Main(string[] args)
        {
            String DllLocation = @"C:\Program Files\SafeNet\LunaClient\cryptoki.dll";
            Cryptoki cryptoki = new Cryptoki(DllLocation);
            int initializeReturnCode = cryptoki.Initialize();

            if (initializeReturnCode != 0)
            {
                throw new Exception("Unable to initialize");
            }

            SlotList slotList = cryptoki.Slots;
            if (slotList.Count == 0)
            {
                throw new Exception("No Slots Available");
            }

            foreach (Slot s in slotList)
            {
                Console.WriteLine("SlotID {0}, is token present {1}" , s.SlotID, s.IsTokenPresent);
            }

            Console.ReadLine();
        }

Java:

    public static void main(String[] args) {
        try{
            String toSign = "THISISATEST";
            LunaSlotManager slotManager = LunaSlotManager.getInstance();
            if(slotManager.isLoggedIn())
            {
                System.out.println("Already logged into one slot");
            }
            String tokenlabel;
            ByteArrayInputStream is1 = new ByteArrayInputStream(("slot:2").getBytes());
            slotManager.login("Label", "Password");
            for (int i = 1; i <= slotManager.getNumberOfSlots(); i++) {
                // Since it is possible to have a slot without a token present
                // check to see if there is a token present
                if (slotManager.isTokenPresent(i)) {
                    tokenlabel = slotManager.getTokenLabel(i);
                    // Each Luna SA partiton or HSM has a label that is created
                    // during setup of the HSM. Labels are commonly used to
                    // distinguish one partition or HSM from another.
                    System.out.println("Slot: " + i + " token label: " + tokenlabel);
                }
            }
            
            KeyStore myStore = KeyStore.getInstance("Luna");
            myStore.load(is1, "password".toCharArray());
            
            Key key = myStore.getKey("KeyLabel", null);
            byte[] toSignBytes = toSign.getBytes();
            Signature sig = Signature.getInstance("SHA256withECDSA", "LunaProvider");
            sig.initSign((PrivateKey)key);
            sig.update(toSignBytes);
            byte realSig[] = sig.sign();
            System.out.println("Done");
        }

5/11/2017 4:06:28 PM
Gravatar
Total Posts 300
Ugo Chirico http://www.ugochirico.com

Re: Java vs NCryptoki

are you sure that C# and Java code are using the same underlying PKCS#11 module?