Initialization and Login

Updated: May 17, 2017

SmartKey provides multiple interfaces to application developers. For C/C++ programmers, SmartKey provides a PKCS#11 interface through a library. For Java programmers, SmartKey can be accessed through the JCE interface. SmartKey can also be accessed through its RESTful interface, documented at http://support.smartkey.io/api/index.html

We provide examples for using SmartKey in 3 languages – a C++ program using the PKCS#11 interface, a Java program using the JCE interface, and a Python program using the REST interface.

Download the code to initialize and login. The example programs can be downloaded in full at http://support.smartkey.io/smartkey/resources.html


CK_FUNCTION_LIST_PTR initialize() {
    CK_FUNCTION_LIST_PTR p11;
    void *pDynLib;

    pDynLib = dlopen("/opt/fortanix/pkcs11/sdkms-pkcs11.so", RTLD_NOW);
    if (!pDynLib) {
        cout << "Failed to load the PKCS#11 library" << endl;
        return NULL;
    }
    cout << "Successfully initialized PKCS#11 library to use SmartKey" << endl;
    CK_C_GetFunctionList pGetFunctionList = (CK_C_GetFunctionList) dlsym(pDynLib, "C_GetFunctionList");
    (*pGetFunctionList)(&p11);
    return p11;
}

CK_RV login(CK_FUNCTION_LIST_PTR p11, CK_SESSION_HANDLE_PTR phSession, string pin) {
    CK_RV rv;
    CK_SLOT_ID slotId = 1;

    rv = p11->C_Initialize(NULL_PTR);
    if (rv == CKR_OK) {
        rv = p11->C_OpenSession(slotId, CKF_SERIAL_SESSION | CKF_RW_SESSION, NULL_PTR, NULL_PTR, phSession);
        if (rv == CKR_OK) rv = p11->C_Login(*phSession, CKU_USER, (CK_UTF8CHAR_PTR)pin.c_str(), pin.length());
    }
    if (rv != CKR_OK) {
        cout << "Incorrect API Key. Error code = " << rv << endl;
    } else {
        cout << "Successfully logged in to SmartKey" << endl << endl;
    }
    return rv;
}


private static Provider initialize() {
    try {
        final Path tmpFile = Files.createTempFile("pkcs11", ".cfg");
        tmpFile.toFile().deleteOnExit();
        final List lines = Arrays.asList("name = fortanix", "library = /opt/fortanix/pkcs11/sdkms-pkcs11.so");
        Files.write(tmpFile, lines, Charset.defaultCharset(), StandardOpenOption.WRITE);

        Provider provider = new sun.security.pkcs11.SunPKCS11(tmpFile.toString());
        System.out.println("Successfully initialized JCE to use SmartKey");
        return provider;
    } catch (Exception e) {
        System.out.println("Could not initialize: " + e);
        return null;
    }
}

private static boolean login(Provider provider, String apiKey) {
    try {
        KeyStore ks = KeyStore.getInstance("PKCS11", provider);
        ks.load(null, apiKey.toCharArray());
        System.out.println("Successfully logged in to SmartKey");
        return true;
    } catch (Exception e) {
        System.out.println("Incorrect API Key: " + e);
        return false;
    }
}


def login(api_key):
    res = requests.request(method='POST',
                           url="https://www.smartkey.io/sys/v1/session/auth",
                           headers={'Authorization' : 'Basic ' + api_key})
    if res.status_code != requests.codes.ok:
        print "Can't login: " + str(res.status_code) + ": " + res.text + " to " + FORTANIX_API_ENDPOINT
        return None
    else:
        print "Successfully logged in to SmartKey"
        return res.json()['access_token']