wtorek, 24 września 2013

Szukanie numeru slotu (-a?)

private void initialize()
            throws Exception
    {
        long hSession = 0L;

        CK_C_INITIALIZE_ARGS cia = new CK_C_INITIALIZE_ARGS();
        CK_ATTRIBUTE[] attrs = new CK_ATTRIBUTE[1];
        CK_ATTRIBUTE attr = new CK_ATTRIBUTE();
        CK_TOKEN_INFO ckti = null;

        cia.pReserved = null;
        cia.flags = 0L;

        PKCS11 p11 = null;

        Method[] methods = PKCS11.class.getMethods();
        Method p11GetInstance = null;

        for (int i = 0; i < methods.length; i++) {
            if (!methods[i].getName().equals("getInstance")) {
                continue;
            }
            p11GetInstance = methods[i];
        }

        try {
            File _fpk11LibPath = new File(this.pkcs11Path);
            this.pkcs11Path = _fpk11LibPath.getCanonicalPath();

            String version = System.getProperty("java.version");
            if ((version.indexOf("1.6") > -1) || (version.indexOf("1.7") > -1)) {
                p11 = (PKCS11)p11GetInstance.invoke(null, new Object[] { this.pkcs11Path, "C_GetFunctionList", cia,
                                                                         Boolean.valueOf(false) });
            }
            else if (version.indexOf("1.5") > -1) {
                p11 = (PKCS11)p11GetInstance.invoke(null, new Object[] { this.pkcs11Path, cia, Boolean.valueOf(false) });
            }
            else {
                throw new Exception("Unsuported version of VM");
            }
        }
        catch (Exception e) {
            throw new Exception("Problem using java reflection with pkcs11 classes::" + e.getMessage());
        }
        long[] slots;
        try {

            slots = p11.C_GetSlotList(true);
        }
        catch (Exception e) {
            throw new Exception("Getting Slot List:: " + e.getMessage());
        }

        for (long k : slots) {
        
            try {
                hSession = p11.C_OpenSession(k, 4L, null, null);
            }
            catch (Exception e) {
                throw new Exception("Opening a new Session:: " + e.getMessage());
            }
            attr.type = 0L;
            attr.pValue = Long.valueOf(1L);
            attrs[0] = attr;
            try {
                p11.C_FindObjectsInit(hSession, attrs);
                long[] l = p11.C_FindObjects(hSession, MAX_CERTS);
                p11.C_FindObjectsFinal(hSession);

                for (long i : l) {
                    CK_ATTRIBUTE attrPriv = new CK_ATTRIBUTE();
                    CK_ATTRIBUTE[] attrsP = new CK_ATTRIBUTE[2];

                    attrPriv.type = 0L;
                    attrPriv.pValue = Long.valueOf(1L);

                    attr.type = 258L;
                    attr.pValue = getID(hSession, i, p11);
                    if (attr.pValue == null) {
                        continue;
                    }
                    attrsP[0] = attrPriv;
                    attrsP[1] = attr;

                    p11.C_FindObjectsInit(hSession, attrsP);
                    long[] m = p11.C_FindObjects(hSession, MAX_CERTS);

                    for (long n : m) {
                        list.add(new X509Info(loadX509Cert(hSession, i, p11), Long.valueOf(k)));
                    }
                    p11.C_FindObjectsFinal(hSession);
                }
            }
            catch (Exception e) {
                throw new Exception("Unsuccesfully FindObjects sequence:: " + e.getMessage());
            }

            try {
                p11.C_CloseSession(hSession);
            }
            catch (Exception e) {
                throw new Exception("Cannot close sesion::" + e.getMessage());
            }
        }

        try {
            p11 = null;
            Runtime.getRuntime().gc();
        }
        catch (Exception e) {
            throw new Exception("Cannot Finalize::" + e.getMessage());
        }
    }


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.security.cert.X509Certificate;

public class X509Info
{
    private long slot;
    private X509Certificate x509cert;

    public X509Info(X509Certificate x509cert, Long slotNr)
    {
        this.x509cert = x509cert;
        this.slot = slotNr.longValue();
    }

    /**
     * @return the slot
     */
    public long getSlot()
    {
        return slot;
    }

    /**
     * @param slot the slot to set
     */
    public void setSlot(long slot)
    {
        this.slot = slot;
    }

    public X509Certificate getX509cert()
    {
        return x509cert;
    }
 
    public void setX509cert(X509Certificate x509cert)
    {
        this.x509cert = x509cert;
    }
}



 Jeśli dostawca to KIR lub SIGILLUM getSlot() jako parametr slotNo pkcs11ConfigSettings.append("slotListIndex = ").append(slotNo).append("\n");
    

Brak komentarzy:

Prześlij komentarz