opendnssec / SoftHSMv2

SoftHSM version 2
http://www.softhsm.org/
Other
740 stars 335 forks source link

How to define a correct Java/JNA interface to get Objects from SoftHSM #716

Closed tamar-a-roulettes closed 1 year ago

tamar-a-roulettes commented 1 year ago

I am using SoftHSM and I have to develop a JAVA application to extract object of it. I know I have a "Data" object of label "MY_DATA" and I have succesfully open the session on the right token.

But even if there is no error returned code, I cannot get anything: objectHandles is always empty (and ulObjectCount = 0)

Note that I get a list of handles when I use a null template. Thus, I think the issue is around the definition of the template (types used...) but what ?

Can someone help me finding out what I am doing bad ?

I am trying to get my Object with the following Java/JNA code (my JNA interface is called Pkcs11Library).

        String dataLabel = "MY_DATA";
        CK_ATTRIBUTE[] searchTemplate = (CK_ATTRIBUTE[]) new CK_ATTRIBUTE().toArray(2);

        searchTemplate[0].type = new NativeLong(Pkcs11Library.CKA_LABEL); // long CKA_LABEL = 0x00000003 
        byte[] labelBytes = dataLabel.getBytes();
        searchTemplate[0].pValue = new Memory(labelBytes.length);
        searchTemplate[0].pValue.write(0, labelBytes, 0, labelBytes.length);
        searchTemplate[0].ulValueLen = new NativeLong(labelBytes.length);

        searchTemplate[1].type = new NativeLong(Pkcs11Library.CKA_CLASS); // long CKA_CLASS = 0x00000000
        searchTemplate[1].pValue = new Memory(NativeLong.SIZE);
        searchTemplate[1].pValue.setNativeLong(0, new NativeLong(Pkcs11Library.CKO_DATA)); // long CKO_DATA = 0x00000000
        searchTemplate[1].ulValueLen = new NativeLong(NativeLong.SIZE);

        int rv  = Pkcs11Library.INSTANCE.C_FindObjectsInit(hSession, searchTemplate, searchTemplate.length);
        if (rv != Pkcs11Library.CKR_OK) {
            //TODO
        }

        int[] objectHandles = new int[MAX_OBJECT_COUNT]; // MAX_OBJECT_COUNT = 10
        NativeLongByReference pulObjectCount = new NativeLongByReference();
        rv = Pkcs11Library.INSTANCE.C_FindObjects(hSession, objectHandles, objectHandles.length, pulObjectCount);
        int ulObjectCount = pulObjectCount.getValue().intValue();

My JNA interface is defined as follows:

    // CK_ATTRIBUTE
    public class CK_ATTRIBUTE extends Structure {
        public NativeLong type;
        public Pointer pValue;
        public NativeLong ulValueLen;

        public CK_ATTRIBUTE() {
            super();
        }

        @Override
        protected List<String> getFieldOrder() {
            return Arrays.asList("type", "pValue", "ulValueLen");
        }
    }

    int C_FindObjectsInit(int hSession, CK_ATTRIBUTE[] searchTemplate, int length);
    int C_FindObjects(int hSession, int[] phObject, int ulMaxObjectCount, NativeLongByReference pulObjectCount);
tamar-a-roulettes commented 1 year ago

this is an aligment issue. The solution is:

public CK_ATTRIBUTE() {
        super();
        **setAlignType(ALIGN_NONE);**
    }