Search in sources :

Example 1 with CkAttribute

use of org.openecard.mdlw.sal.struct.CkAttribute in project open-ecard by ecsec.

the class MiddleWareWrapper method getAttributeValues.

public List<CkAttribute> getAttributeValues(long hSession, long hObject, long... types) throws CryptokiException {
    CK_ATTRIBUTE baseAttr = new CK_ATTRIBUTE();
    CK_ATTRIBUTE[] attrs = (CK_ATTRIBUTE[]) baseAttr.toArray(types.length);
    for (int i = 0; i < types.length; i++) {
        CK_ATTRIBUTE attr = attrs[i];
        attr.setType(new NativeLong(types[i]));
        attr.setPValue(Pointer.NULL);
        attr.setUlValueLen(new NativeLong(0));
    }
    try (LockedObject lo = lockInternal()) {
        // determine size of data to read and allocate space
        check("C_GetAttributeValue", lib.C_GetAttributeValue(new NativeLong(hSession), new NativeLong(hObject), baseAttr, new NativeLong(attrs.length)));
        for (CK_ATTRIBUTE next : attrs) {
            long valueLen = next.getUlValueLen().longValue();
            if (valueLen > 0) {
                Memory newMem = new Memory(valueLen);
                next.setPValue(newMem);
            }
        }
        // read attributes
        check("C_GetAttributeValue", lib.C_GetAttributeValue(new NativeLong(hSession), new NativeLong(hObject), baseAttr, new NativeLong(attrs.length)));
        ArrayList<CkAttribute> result = new ArrayList<>();
        for (CK_ATTRIBUTE next : attrs) {
            CkAttribute resultAttr = new CkAttribute(next.getPValue(), next.getUlValueLen());
            result.add(resultAttr);
        }
        return result;
    } catch (InterruptedException ex) {
        throw new IllegalStateException("Failed to release lock for middleware access.");
    }
}
Also used : CkAttribute(org.openecard.mdlw.sal.struct.CkAttribute) Memory(com.sun.jna.Memory) NativeLong(com.sun.jna.NativeLong) ArrayList(java.util.ArrayList) CK_ATTRIBUTE(org.openecard.mdlw.sal.cryptoki.CK_ATTRIBUTE)

Example 2 with CkAttribute

use of org.openecard.mdlw.sal.struct.CkAttribute in project open-ecard by ecsec.

the class MwCertificate method loadAttrValLabel.

/**
 * Loading the Attribute Value for CKA_LABEL
 *
 * @return String
 * @throws CryptokiException
 */
private String loadAttrValLabel() throws CryptokiException {
    CkAttribute raw = mw.getAttributeValue(session.getSessionId(), objectHandle, CryptokiLibrary.CKA_LABEL);
    String labelStr = AttributeUtils.getString(raw);
    // find replacement if needed
    if (labelStr == null && getSubject() != null) {
        labelStr = new X500Principal(getSubject()).getName(X500Principal.RFC2253);
    }
    if (labelStr == null) {
        try {
            byte[] hash = MessageDigest.getInstance("SHA-1").digest(getValue());
            labelStr = ByteUtils.toHexString(hash);
        } catch (NoSuchAlgorithmException ex) {
        // SHA-1 is a standard name and must be present
        }
    }
    return labelStr;
}
Also used : CkAttribute(org.openecard.mdlw.sal.struct.CkAttribute) X500Principal(javax.security.auth.x500.X500Principal) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Aggregations

CkAttribute (org.openecard.mdlw.sal.struct.CkAttribute)2 Memory (com.sun.jna.Memory)1 NativeLong (com.sun.jna.NativeLong)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 ArrayList (java.util.ArrayList)1 X500Principal (javax.security.auth.x500.X500Principal)1 CK_ATTRIBUTE (org.openecard.mdlw.sal.cryptoki.CK_ATTRIBUTE)1