#include "test_common.h"

CK_RV test_crypto();
CK_RV test_tls_pre_master_key_gen(CK_SESSION_HANDLE sess);

int main(int argc, char *argv[])
{
    CK_RV rc = CKR_OK;

    rc = test_main(argc, argv);
    if (rc != CKR_OK)
    {
        fprintf(stderr, "test_main failed: 0x%lx\n", rc);
        return rc;
    }
    rc = test_crypto();
    if (rc != CKR_OK)
    {
        fprintf(stderr, "test_crypto failed: 0x%lx\n", rc);
        return rc;
    }
    return CKR_OK;
}

CK_RV test_crypto()
{
    CK_RV             rc, ret;
    CK_SESSION_HANDLE hSession;
    CK_MECHANISM_INFO minfo;

    rc = funcs->C_OpenSession(SlotId,
                              CKF_RW_SESSION | CKF_SERIAL_SESSION,
                              NULL_PTR, NULL_PTR, &hSession);
    if (rc != CKR_OK)
    {
        fprintf(stderr,
                "ERROR call to C_OpenSession failed, rc = 0x%lx\n", rc);
        goto out;
    }
    fprintf(stderr, "C_OpenSession success\n");
/*
        // log in as normal user
        rc = funcs->C_Login(hSession, CKU_USER, user_pin, strlen(user_pin));
        if (rc != CKR_OK) {
                fprintf(stderr, "ERROR call to C_Login failed, rc = 0x%lx\n", rc);
                goto out_close;
        }
        fprintf(stderr, "C_Login success\n");
 */
    rc = funcs->C_GetMechanismInfo(SlotId,
                                   CKM_TLS_GOST_PRE_MASTER_KEY_GEN, &minfo);
    if (rc != CKR_OK)
    {
        fprintf(stderr,
                "\n======== Mechanism CKM_TLS_GOST_PRE_MASTER_KEY_GEN "
                "not supported ===========\n");
    }
    else
    {
        fprintf(stderr,
                "\n==================== CKM_TLS_GOST_PRE_MASTER_KEY_GEN "
                "test ==================\n");
        rc = test_tls_pre_master_key_gen(hSession);
        if (rc != CKR_OK)
        {
            fprintf(stderr,
                    "ERROR CKM_TLS_GOST_PRE_MASTER_KEY_GEN failed, "
                    "rc = 0x%lx\n", rc);
        }
        else
        {
            fprintf(stderr,
                    "CKM_TLS_GOST_PRE_MASTER_KEY_GEN test passed.\n");
        }
    }

    if ((ret = funcs->C_CloseSession(hSession)) != CKR_OK)
    {
        fprintf(stderr,
                "Error: C_CloseSession failed with 0x%lx\n", ret);
        rc = ret;
    }
    else
    {
        fprintf(stderr, "C_CloseSession success\n");
    }

 out:
    return rc;
}

CK_RV test_tls_pre_master_key_gen(CK_SESSION_HANDLE sess)
{
    CK_RV rc = CKR_OK;
    CK_BYTE                value[1024];
    CK_MECHANISM           mechanism_desc =
    { CKM_TLS_GOST_PRE_MASTER_KEY_GEN, NULL, 0 };
    CK_MECHANISM_PTR       mechanism = &mechanism_desc;
    CK_OBJECT_HANDLE       keyh      = CK_INVALID_HANDLE;
    static CK_OBJECT_CLASS oclass    = CKO_SECRET_KEY;
    static CK_KEY_TYPE     key_type  = CKK_GENERIC_SECRET;
    static CK_BBOOL        ltrue     = CK_TRUE;
    static CK_BBOOL        lfalse    = CK_FALSE;
    CK_ATTRIBUTE           attr;
    static CK_ATTRIBUTE    ltemplate[] = {
        { CKA_CLASS,       &oclass,   sizeof(oclass)   },
        { CKA_KEY_TYPE,    &key_type, sizeof(key_type) },
        { CKA_SENSITIVE,   &lfalse,   sizeof(lfalse)   },
        { CKA_EXTRACTABLE, &ltrue,    sizeof(ltrue)    },
        { CKA_DERIVE,      &ltrue,    sizeof(ltrue)    },
        { CKA_SIGN,        &ltrue,    sizeof(ltrue)    },
        { CKA_VERIFY,      &ltrue,    sizeof(ltrue)    }
    };
    CK_VERSION             ver; // TLS version

    // В текущей версии всегда генерируется ключ типа CKK_GENERIC_SECRET
    // со случайным значением длиной 32 байта.
    ver.major                 = 1;
    ver.minor                 = 0;
    mechanism->pParameter     = &ver;
    mechanism->ulParameterLen = sizeof(ver);
    rc                        = funcs->C_GenerateKey(sess, mechanism,
                                                     ltemplate, sizeof(ltemplate) / sizeof(CK_ATTRIBUTE),
                                                     &keyh);
    if (rc != CKR_OK)
    {
        fprintf(stderr, "C_GenerateKey failed: 0x%lx\n", rc);
        return rc;
    }

    attr.type       = CKA_VALUE;
    attr.pValue     = value;
    attr.ulValueLen = 32;
    rc              = funcs->C_GetAttributeValue(sess, keyh, &attr, 1);
    if (rc != CKR_OK)
    {
        fprintf(stderr, "C_GetAttributeValue failed: 0x%lx\n", rc);
        return rc;
    }

    printf("TLS PRE-MASTER KEY:\n");
    print_bytes(value, attr.ulValueLen);

    rc = funcs->C_DestroyObject(sess, keyh);
    if (rc != CKR_OK)
    {
        fprintf(stderr, "C_DestroyObject failed: 0x%lx\n", rc);
        return rc;
    }

    return rc;
}
